Пример #1
0
    public static void toXContent(
        MetaData metaData, XContentBuilder builder, ToXContent.Params params) throws IOException {
      XContentContext context = XContentContext.valueOf(params.param(CONTEXT_MODE_PARAM, "API"));

      builder.startObject("meta-data");

      builder.field("version", metaData.version());
      builder.field("cluster_uuid", metaData.clusterUUID);

      if (!metaData.persistentSettings().getAsMap().isEmpty()) {
        builder.startObject("settings");
        for (Map.Entry<String, String> entry :
            metaData.persistentSettings().getAsMap().entrySet()) {
          builder.field(entry.getKey(), entry.getValue());
        }
        builder.endObject();
      }

      if (context == XContentContext.API && !metaData.transientSettings().getAsMap().isEmpty()) {
        builder.startObject("transient_settings");
        for (Map.Entry<String, String> entry : metaData.transientSettings().getAsMap().entrySet()) {
          builder.field(entry.getKey(), entry.getValue());
        }
        builder.endObject();
      }

      builder.startObject("templates");
      for (ObjectCursor<IndexTemplateMetaData> cursor : metaData.templates().values()) {
        IndexTemplateMetaData.Builder.toXContent(cursor.value, builder, params);
      }
      builder.endObject();

      if (context == XContentContext.API && !metaData.indices().isEmpty()) {
        builder.startObject("indices");
        for (IndexMetaData indexMetaData : metaData) {
          IndexMetaData.Builder.toXContent(indexMetaData, builder, params);
        }
        builder.endObject();
      }

      for (ObjectObjectCursor<String, Custom> cursor : metaData.customs()) {
        Custom proto = lookupPrototypeSafe(cursor.key);
        if (proto.context().contains(context)) {
          builder.startObject(cursor.key);
          cursor.value.toXContent(builder, params);
          builder.endObject();
        }
      }
      builder.endObject();
    }
Пример #2
0
  /**
   * As of 2.0 we require units for time and byte-sized settings. This methods adds default units to
   * any cluster settings that don't specify a unit.
   */
  public static MetaData addDefaultUnitsIfNeeded(ESLogger logger, MetaData metaData) {
    Settings.Builder newPersistentSettings = null;
    for (Map.Entry<String, String> ent : metaData.persistentSettings().getAsMap().entrySet()) {
      String settingName = ent.getKey();
      String settingValue = ent.getValue();
      if (CLUSTER_BYTES_SIZE_SETTINGS.contains(settingName)) {
        try {
          Long.parseLong(settingValue);
        } catch (NumberFormatException nfe) {
          continue;
        }
        // It's a naked number that previously would be interpreted as default unit (bytes); now we
        // add it:
        logger.warn(
            "byte-sized cluster setting [{}] with value [{}] is missing units; assuming default units (b) but in future versions this will be a hard error",
            settingName,
            settingValue);
        if (newPersistentSettings == null) {
          newPersistentSettings = Settings.builder();
          newPersistentSettings.put(metaData.persistentSettings());
        }
        newPersistentSettings.put(settingName, settingValue + "b");
      }
      if (CLUSTER_TIME_SETTINGS.contains(settingName)) {
        try {
          Long.parseLong(settingValue);
        } catch (NumberFormatException nfe) {
          continue;
        }
        // It's a naked number that previously would be interpreted as default unit (ms); now we add
        // it:
        logger.warn(
            "time cluster setting [{}] with value [{}] is missing units; assuming default units (ms) but in future versions this will be a hard error",
            settingName,
            settingValue);
        if (newPersistentSettings == null) {
          newPersistentSettings = Settings.builder();
          newPersistentSettings.put(metaData.persistentSettings());
        }
        newPersistentSettings.put(settingName, settingValue + "ms");
      }
    }

    if (newPersistentSettings != null) {
      return new MetaData(
          metaData.clusterUUID(),
          metaData.version(),
          metaData.transientSettings(),
          newPersistentSettings.build(),
          metaData.getIndices(),
          metaData.getTemplates(),
          metaData.getCustoms(),
          metaData.concreteAllIndices(),
          metaData.concreteAllOpenIndices(),
          metaData.concreteAllClosedIndices(),
          metaData.getAliasAndIndexLookup());
    } else {
      // No changes:
      return metaData;
    }
  }