Delegate toXContent logic from ClusterState to its member classes (#54192)

Backport of #52743

Co-authored-by: Yannick Welsch <yannick@welsch.lu>
This commit is contained in:
Yannick Welsch 2020-03-26 15:08:30 +01:00 committed by GitHub
parent ffe1ba3754
commit 8176f1c7f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 1484 additions and 427 deletions

View File

@ -19,7 +19,6 @@
package org.elasticsearch.cluster; package org.elasticsearch.cluster;
import com.carrotsearch.hppc.cursors.IntObjectCursor;
import com.carrotsearch.hppc.cursors.ObjectCursor; import com.carrotsearch.hppc.cursors.ObjectCursor;
import com.carrotsearch.hppc.cursors.ObjectObjectCursor; import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
@ -31,8 +30,6 @@ import org.elasticsearch.cluster.coordination.CoordinationMetaData;
import org.elasticsearch.cluster.coordination.CoordinationMetaData.VotingConfiguration; import org.elasticsearch.cluster.coordination.CoordinationMetaData.VotingConfiguration;
import org.elasticsearch.cluster.coordination.CoordinationMetaData.VotingConfigExclusion; import org.elasticsearch.cluster.coordination.CoordinationMetaData.VotingConfigExclusion;
import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.IndexTemplateMetaData;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.node.DiscoveryNodes; import org.elasticsearch.cluster.node.DiscoveryNodes;
@ -45,10 +42,8 @@ import org.elasticsearch.cluster.routing.ShardRouting;
import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.UUIDs; import org.elasticsearch.common.UUIDs;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.collect.ImmutableOpenMap; import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput; import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
@ -58,13 +53,11 @@ import org.elasticsearch.common.io.stream.VersionedNamedWriteable;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContentFragment; import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.discovery.Discovery; import org.elasticsearch.discovery.Discovery;
import java.io.IOException; import java.io.IOException;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.HashMap; import java.util.HashMap;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
@ -492,101 +485,7 @@ public class ClusterState implements ToXContentFragment, Diffable<ClusterState>
// meta data // meta data
if (metrics.contains(Metric.METADATA)) { if (metrics.contains(Metric.METADATA)) {
builder.startObject("metadata"); metaData.toXContent(builder, params);
builder.field("cluster_uuid", metaData().clusterUUID());
builder.startObject("cluster_coordination");
coordinationMetaData().toXContent(builder, params);
builder.endObject();
builder.startObject("templates");
for (ObjectCursor<IndexTemplateMetaData> cursor : metaData().templates().values()) {
IndexTemplateMetaData templateMetaData = cursor.value;
builder.startObject(templateMetaData.name());
builder.field("index_patterns", templateMetaData.patterns());
builder.field("order", templateMetaData.order());
builder.startObject("settings");
Settings settings = templateMetaData.settings();
settings.toXContent(builder, params);
builder.endObject();
builder.startObject("mappings");
for (ObjectObjectCursor<String, CompressedXContent> cursor1 : templateMetaData.mappings()) {
Map<String, Object> mapping = XContentHelper.convertToMap(new BytesArray(cursor1.value.uncompressed()), false).v2();
if (mapping.size() == 1 && mapping.containsKey(cursor1.key)) {
// the type name is the root value, reduce it
mapping = (Map<String, Object>) mapping.get(cursor1.key);
}
builder.field(cursor1.key);
builder.map(mapping);
}
builder.endObject();
builder.endObject();
}
builder.endObject();
builder.startObject("indices");
for (IndexMetaData indexMetaData : metaData()) {
builder.startObject(indexMetaData.getIndex().getName());
builder.field("state", indexMetaData.getState().toString().toLowerCase(Locale.ENGLISH));
builder.startObject("settings");
Settings settings = indexMetaData.getSettings();
settings.toXContent(builder, params);
builder.endObject();
builder.startObject("mappings");
for (ObjectObjectCursor<String, MappingMetaData> cursor : indexMetaData.getMappings()) {
Map<String, Object> mapping = XContentHelper
.convertToMap(new BytesArray(cursor.value.source().uncompressed()), false).v2();
if (mapping.size() == 1 && mapping.containsKey(cursor.key)) {
// the type name is the root value, reduce it
mapping = (Map<String, Object>) mapping.get(cursor.key);
}
builder.field(cursor.key);
builder.map(mapping);
}
builder.endObject();
builder.startArray("aliases");
for (ObjectCursor<String> cursor : indexMetaData.getAliases().keys()) {
builder.value(cursor.value);
}
builder.endArray();
builder.startObject(IndexMetaData.KEY_PRIMARY_TERMS);
for (int shard = 0; shard < indexMetaData.getNumberOfShards(); shard++) {
builder.field(Integer.toString(shard), indexMetaData.primaryTerm(shard));
}
builder.endObject();
builder.startObject(IndexMetaData.KEY_IN_SYNC_ALLOCATIONS);
for (IntObjectCursor<Set<String>> cursor : indexMetaData.getInSyncAllocationIds()) {
builder.startArray(String.valueOf(cursor.key));
for (String allocationId : cursor.value) {
builder.value(allocationId);
}
builder.endArray();
}
builder.endObject();
// index metadata
builder.endObject();
}
builder.endObject();
for (ObjectObjectCursor<String, MetaData.Custom> cursor : metaData.customs()) {
builder.startObject(cursor.key);
cursor.value.toXContent(builder, params);
builder.endObject();
}
builder.endObject();
} }
// routing table // routing table

View File

@ -394,6 +394,8 @@ public class AliasMetaData extends AbstractDiffable<AliasMetaData> implements To
builder.indexRouting(parser.text()); builder.indexRouting(parser.text());
} else if ("search_routing".equals(currentFieldName) || "searchRouting".equals(currentFieldName)) { } else if ("search_routing".equals(currentFieldName) || "searchRouting".equals(currentFieldName)) {
builder.searchRouting(parser.text()); builder.searchRouting(parser.text());
} else if ("filter".equals(currentFieldName)) {
builder.filter(new CompressedXContent(parser.binaryValue()));
} }
} else if (token == XContentParser.Token.START_ARRAY) { } else if (token == XContentParser.Token.START_ARRAY) {
parser.skipChildren(); parser.skipChildren();

View File

@ -65,6 +65,7 @@ import java.time.ZonedDateTime;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -73,6 +74,7 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.function.Function; import java.util.function.Function;
import static org.elasticsearch.cluster.metadata.MetaData.CONTEXT_MODE_PARAM;
import static org.elasticsearch.cluster.node.DiscoveryNodeFilters.IP_VALIDATOR; import static org.elasticsearch.cluster.node.DiscoveryNodeFilters.IP_VALIDATOR;
import static org.elasticsearch.cluster.node.DiscoveryNodeFilters.OpType.AND; import static org.elasticsearch.cluster.node.DiscoveryNodeFilters.OpType.AND;
import static org.elasticsearch.cluster.node.DiscoveryNodeFilters.OpType.OR; import static org.elasticsearch.cluster.node.DiscoveryNodeFilters.OpType.OR;
@ -1280,6 +1282,9 @@ public class IndexMetaData implements Diffable<IndexMetaData>, ToXContentFragmen
} }
public static void toXContent(IndexMetaData indexMetaData, XContentBuilder builder, ToXContent.Params params) throws IOException { public static void toXContent(IndexMetaData indexMetaData, XContentBuilder builder, ToXContent.Params params) throws IOException {
MetaData.XContentContext context = MetaData.XContentContext.valueOf(
params.param(CONTEXT_MODE_PARAM, MetaData.CONTEXT_MODE_API));
builder.startObject(indexMetaData.getIndex().getName()); builder.startObject(indexMetaData.getIndex().getName());
builder.field(KEY_VERSION, indexMetaData.getVersion()); builder.field(KEY_VERSION, indexMetaData.getVersion());
@ -1287,40 +1292,75 @@ public class IndexMetaData implements Diffable<IndexMetaData>, ToXContentFragmen
builder.field(KEY_SETTINGS_VERSION, indexMetaData.getSettingsVersion()); builder.field(KEY_SETTINGS_VERSION, indexMetaData.getSettingsVersion());
builder.field(KEY_ALIASES_VERSION, indexMetaData.getAliasesVersion()); builder.field(KEY_ALIASES_VERSION, indexMetaData.getAliasesVersion());
builder.field(KEY_ROUTING_NUM_SHARDS, indexMetaData.getRoutingNumShards()); builder.field(KEY_ROUTING_NUM_SHARDS, indexMetaData.getRoutingNumShards());
builder.field(KEY_STATE, indexMetaData.getState().toString().toLowerCase(Locale.ENGLISH)); builder.field(KEY_STATE, indexMetaData.getState().toString().toLowerCase(Locale.ENGLISH));
boolean binary = params.paramAsBoolean("binary", false); boolean binary = params.paramAsBoolean("binary", false);
builder.startObject(KEY_SETTINGS); builder.startObject(KEY_SETTINGS);
indexMetaData.getSettings().toXContent(builder, new MapParams(Collections.singletonMap("flat_settings", "true"))); if (context != MetaData.XContentContext.API) {
indexMetaData.getSettings().toXContent(builder, new MapParams(Collections.singletonMap("flat_settings", "true")));
} else {
indexMetaData.getSettings().toXContent(builder, params);
}
builder.endObject(); builder.endObject();
builder.startArray(KEY_MAPPINGS); if (context != MetaData.XContentContext.API) {
for (ObjectObjectCursor<String, MappingMetaData> cursor : indexMetaData.getMappings()) { builder.startArray(KEY_MAPPINGS);
if (binary) { for (ObjectObjectCursor<String, MappingMetaData> cursor : indexMetaData.getMappings()) {
builder.value(cursor.value.source().compressed()); if (binary) {
} else { builder.value(cursor.value.source().compressed());
builder.map(XContentHelper.convertToMap(new BytesArray(cursor.value.source().uncompressed()), true).v2()); } else {
builder.map(XContentHelper.convertToMap(new BytesArray(cursor.value.source().uncompressed()), true).v2());
}
} }
builder.endArray();
} else {
builder.startObject(KEY_MAPPINGS);
for (ObjectObjectCursor<String, MappingMetaData> cursor : indexMetaData.getMappings()) {
Map<String, Object> mapping = XContentHelper
.convertToMap(new BytesArray(cursor.value.source().uncompressed()), false).v2();
if (mapping.size() == 1 && mapping.containsKey(cursor.key)) {
// the type name is the root value, reduce it
mapping = (Map<String, Object>) mapping.get(cursor.key);
}
builder.field(cursor.key);
builder.map(mapping);
}
builder.endObject();
} }
builder.endArray();
for (ObjectObjectCursor<String, DiffableStringMap> cursor : indexMetaData.customData) { for (ObjectObjectCursor<String, DiffableStringMap> cursor : indexMetaData.customData) {
builder.field(cursor.key); builder.field(cursor.key);
builder.map(cursor.value); builder.map(cursor.value);
} }
builder.startObject(KEY_ALIASES); if (context != MetaData.XContentContext.API) {
for (ObjectCursor<AliasMetaData> cursor : indexMetaData.getAliases().values()) { builder.startObject(KEY_ALIASES);
AliasMetaData.Builder.toXContent(cursor.value, builder, params); for (ObjectCursor<AliasMetaData> cursor : indexMetaData.getAliases().values()) {
} AliasMetaData.Builder.toXContent(cursor.value, builder, params);
builder.endObject(); }
builder.endObject();
builder.startArray(KEY_PRIMARY_TERMS); builder.startArray(KEY_PRIMARY_TERMS);
for (int i = 0; i < indexMetaData.getNumberOfShards(); i++) { for (int i = 0; i < indexMetaData.getNumberOfShards(); i++) {
builder.value(indexMetaData.primaryTerm(i)); builder.value(indexMetaData.primaryTerm(i));
}
builder.endArray();
} else {
builder.startArray(KEY_ALIASES);
for (ObjectCursor<String> cursor : indexMetaData.getAliases().keys()) {
builder.value(cursor.value);
}
builder.endArray();
builder.startObject(IndexMetaData.KEY_PRIMARY_TERMS);
for (int shard = 0; shard < indexMetaData.getNumberOfShards(); shard++) {
builder.field(Integer.toString(shard), indexMetaData.primaryTerm(shard));
}
builder.endObject();
} }
builder.endArray();
builder.startObject(KEY_IN_SYNC_ALLOCATIONS); builder.startObject(KEY_IN_SYNC_ALLOCATIONS);
for (IntObjectCursor<Set<String>> cursor : indexMetaData.inSyncAllocationIds) { for (IntObjectCursor<Set<String>> cursor : indexMetaData.inSyncAllocationIds) {
@ -1506,7 +1546,13 @@ public class IndexMetaData implements Diffable<IndexMetaData>, ToXContentFragmen
return builder.build(); return builder.build();
} }
private static final ToXContent.Params FORMAT_PARAMS = new MapParams(Collections.singletonMap("binary", "true")); private static final ToXContent.Params FORMAT_PARAMS;
static {
Map<String, String> params = new HashMap<>(2);
params.put("binary", "true");
params.put(MetaData.CONTEXT_MODE_PARAM, MetaData.CONTEXT_MODE_GATEWAY);
FORMAT_PARAMS = new MapParams(params);
}
/** /**
* State format for {@link IndexMetaData} to write to and load from disk * State format for {@link IndexMetaData} to write to and load from disk

View File

@ -51,6 +51,8 @@ import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import static org.elasticsearch.cluster.metadata.MetaData.CONTEXT_MODE_PARAM;
public class IndexTemplateMetaData extends AbstractDiffable<IndexTemplateMetaData> { public class IndexTemplateMetaData extends AbstractDiffable<IndexTemplateMetaData> {
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(IndexTemplateMetaData.class)); private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(IndexTemplateMetaData.class));
@ -390,6 +392,8 @@ public class IndexTemplateMetaData extends AbstractDiffable<IndexTemplateMetaDat
XContentBuilder builder, XContentBuilder builder,
ToXContent.Params params, ToXContent.Params params,
boolean includeTypeName) throws IOException { boolean includeTypeName) throws IOException {
MetaData.XContentContext context = params.param(CONTEXT_MODE_PARAM) != null ? MetaData.XContentContext.valueOf(
params.param(CONTEXT_MODE_PARAM)) : null;
builder.field("order", indexTemplateMetaData.order()); builder.field("order", indexTemplateMetaData.order());
if (indexTemplateMetaData.version() != null) { if (indexTemplateMetaData.version() != null) {
@ -401,7 +405,19 @@ public class IndexTemplateMetaData extends AbstractDiffable<IndexTemplateMetaDat
indexTemplateMetaData.settings().toXContent(builder, params); indexTemplateMetaData.settings().toXContent(builder, params);
builder.endObject(); builder.endObject();
if (params.paramAsBoolean("reduce_mappings", false)) { if (context == MetaData.XContentContext.API) {
builder.startObject("mappings");
for (ObjectObjectCursor<String, CompressedXContent> cursor1 : indexTemplateMetaData.mappings()) {
Map<String, Object> mapping = XContentHelper.convertToMap(new BytesArray(cursor1.value.uncompressed()), false).v2();
if (mapping.size() == 1 && mapping.containsKey(cursor1.key)) {
// the type name is the root value, reduce it
mapping = (Map<String, Object>) mapping.get(cursor1.key);
}
builder.field(cursor1.key);
builder.map(mapping);
}
builder.endObject();
} else if (params.paramAsBoolean("reduce_mappings", false)) {
// The parameter include_type_name is only ever used in the REST API, where reduce_mappings is // The parameter include_type_name is only ever used in the REST API, where reduce_mappings is
// always set to true. We therefore only check for include_type_name in this branch. // always set to true. We therefore only check for include_type_name in this branch.
if (includeTypeName == false) { if (includeTypeName == false) {

View File

@ -53,10 +53,8 @@ import org.elasticsearch.common.xcontent.NamedObjectNotFoundException;
import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContentFragment; import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.gateway.MetaDataStateFormat; import org.elasticsearch.gateway.MetaDataStateFormat;
import org.elasticsearch.index.Index; import org.elasticsearch.index.Index;
import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.index.IndexNotFoundException;
@ -158,6 +156,8 @@ public class MetaData implements Iterable<IndexMetaData>, Diffable<MetaData>, To
public static final String CONTEXT_MODE_GATEWAY = XContentContext.GATEWAY.toString(); public static final String CONTEXT_MODE_GATEWAY = XContentContext.GATEWAY.toString();
public static final String CONTEXT_MODE_API = XContentContext.API.toString();
public static final String GLOBAL_STATE_FILE_PREFIX = "global-"; public static final String GLOBAL_STATE_FILE_PREFIX = "global-";
private static final NamedDiffableValueSerializer<Custom> CUSTOM_VALUE_SERIALIZER = new NamedDiffableValueSerializer<>(Custom.class); private static final NamedDiffableValueSerializer<Custom> CUSTOM_VALUE_SERIALIZER = new NamedDiffableValueSerializer<>(Custom.class);
@ -1447,20 +1447,16 @@ public class MetaData implements Iterable<IndexMetaData>, Diffable<MetaData>, To
return aliasAndIndexLookup; return aliasAndIndexLookup;
} }
public static String toXContent(MetaData metaData) throws IOException {
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
builder.startObject();
toXContent(metaData, builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
return Strings.toString(builder);
}
public static void toXContent(MetaData metaData, XContentBuilder builder, ToXContent.Params params) throws IOException { public static void toXContent(MetaData metaData, XContentBuilder builder, ToXContent.Params params) throws IOException {
XContentContext context = XContentContext.valueOf(params.param(CONTEXT_MODE_PARAM, "API")); XContentContext context = XContentContext.valueOf(params.param(CONTEXT_MODE_PARAM, CONTEXT_MODE_API));
builder.startObject("meta-data"); if (context == XContentContext.API) {
builder.startObject("metadata");
} else {
builder.startObject("meta-data");
builder.field("version", metaData.version());
}
builder.field("version", metaData.version());
builder.field("cluster_uuid", metaData.clusterUUID); builder.field("cluster_uuid", metaData.clusterUUID);
builder.field("cluster_uuid_committed", metaData.clusterUUIDCommitted); builder.field("cluster_uuid_committed", metaData.clusterUUIDCommitted);
@ -1468,25 +1464,19 @@ public class MetaData implements Iterable<IndexMetaData>, Diffable<MetaData>, To
metaData.coordinationMetaData().toXContent(builder, params); metaData.coordinationMetaData().toXContent(builder, params);
builder.endObject(); builder.endObject();
if (!metaData.persistentSettings().isEmpty()) { if (context != XContentContext.API && !metaData.persistentSettings().isEmpty()) {
builder.startObject("settings"); builder.startObject("settings");
metaData.persistentSettings().toXContent(builder, new MapParams(Collections.singletonMap("flat_settings", "true"))); metaData.persistentSettings().toXContent(builder, new MapParams(Collections.singletonMap("flat_settings", "true")));
builder.endObject(); builder.endObject();
} }
if (context == XContentContext.API && !metaData.transientSettings().isEmpty()) {
builder.startObject("transient_settings");
metaData.transientSettings().toXContent(builder, new MapParams(Collections.singletonMap("flat_settings", "true")));
builder.endObject();
}
builder.startObject("templates"); builder.startObject("templates");
for (ObjectCursor<IndexTemplateMetaData> cursor : metaData.templates().values()) { for (ObjectCursor<IndexTemplateMetaData> cursor : metaData.templates().values()) {
IndexTemplateMetaData.Builder.toXContentWithTypes(cursor.value, builder, params); IndexTemplateMetaData.Builder.toXContentWithTypes(cursor.value, builder, params);
} }
builder.endObject(); builder.endObject();
if (context == XContentContext.API && !metaData.indices().isEmpty()) { if (context == XContentContext.API) {
builder.startObject("indices"); builder.startObject("indices");
for (IndexMetaData indexMetaData : metaData) { for (IndexMetaData indexMetaData : metaData) {
IndexMetaData.Builder.toXContent(indexMetaData, builder, params); IndexMetaData.Builder.toXContent(indexMetaData, builder, params);

View File

@ -25,9 +25,11 @@ import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.Requests; import org.elasticsearch.client.Requests;
import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.SettingsFilter; import org.elasticsearch.common.settings.SettingsFilter;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.BytesRestResponse;
@ -44,6 +46,7 @@ import java.util.List;
import java.util.Set; import java.util.Set;
import static java.util.Arrays.asList; import static java.util.Arrays.asList;
import static java.util.Collections.singletonMap;
import static java.util.Collections.unmodifiableList; import static java.util.Collections.unmodifiableList;
import static org.elasticsearch.rest.RestRequest.Method.GET; import static org.elasticsearch.rest.RestRequest.Method.GET;
@ -109,7 +112,9 @@ public class RestClusterStateAction extends BaseRestHandler {
builder.field(Fields.WAIT_FOR_TIMED_OUT, response.isWaitForTimedOut()); builder.field(Fields.WAIT_FOR_TIMED_OUT, response.isWaitForTimedOut());
} }
builder.field(Fields.CLUSTER_NAME, response.getClusterName().value()); builder.field(Fields.CLUSTER_NAME, response.getClusterName().value());
response.getState().toXContent(builder, request); ToXContent.Params params = new ToXContent.DelegatingMapParams(
singletonMap(MetaData.CONTEXT_MODE_PARAM, MetaData.CONTEXT_MODE_API), request);
response.getState().toXContent(builder, params);
builder.endObject(); builder.endObject();
return new BytesRestResponse(RestStatus.OK, builder); return new BytesRestResponse(RestStatus.OK, builder);
} }

View File

@ -84,6 +84,7 @@ public class ClusterRerouteResponseTests extends ESTestCase {
" },\n" + " },\n" +
" \"metadata\" : {\n" + " \"metadata\" : {\n" +
" \"cluster_uuid\" : \"_na_\",\n" + " \"cluster_uuid\" : \"_na_\",\n" +
" \"cluster_uuid_committed\" : false,\n" +
" \"cluster_coordination\" : {\n" + " \"cluster_coordination\" : {\n" +
" \"term\" : 0,\n" + " \"term\" : 0,\n" +
" \"last_committed_config\" : [ ],\n" + " \"last_committed_config\" : [ ],\n" +
@ -93,6 +94,11 @@ public class ClusterRerouteResponseTests extends ESTestCase {
" \"templates\" : { },\n" + " \"templates\" : { },\n" +
" \"indices\" : {\n" + " \"indices\" : {\n" +
" \"index\" : {\n" + " \"index\" : {\n" +
" \"version\" : 1,\n" +
" \"mapping_version\" : 1,\n" +
" \"settings_version\" : 1,\n" +
" \"aliases_version\" : 1,\n" +
" \"routing_num_shards\" : 1,\n" +
" \"state\" : \"open\",\n" + " \"state\" : \"open\",\n" +
" \"settings\" : {\n" + " \"settings\" : {\n" +
" \"index\" : {\n" + " \"index\" : {\n" +
@ -114,7 +120,8 @@ public class ClusterRerouteResponseTests extends ESTestCase {
" },\n" + " },\n" +
" \"in_sync_allocations\" : {\n" + " \"in_sync_allocations\" : {\n" +
" \"0\" : [ ]\n" + " \"0\" : [ ]\n" +
" }\n" + " },\n" +
" \"rollover_info\" : { }\n" +
" }\n" + " }\n" +
" },\n" + " },\n" +
" \"index-graveyard\" : {\n" + " \"index-graveyard\" : {\n" +
@ -179,6 +186,7 @@ public class ClusterRerouteResponseTests extends ESTestCase {
" \"cluster_uuid\" : \"_na_\",\n" + " \"cluster_uuid\" : \"_na_\",\n" +
" \"metadata\" : {\n" + " \"metadata\" : {\n" +
" \"cluster_uuid\" : \"_na_\",\n" + " \"cluster_uuid\" : \"_na_\",\n" +
" \"cluster_uuid_committed\" : false,\n" +
" \"cluster_coordination\" : {\n" + " \"cluster_coordination\" : {\n" +
" \"term\" : 0,\n" + " \"term\" : 0,\n" +
" \"last_committed_config\" : [ ],\n" + " \"last_committed_config\" : [ ],\n" +
@ -188,6 +196,11 @@ public class ClusterRerouteResponseTests extends ESTestCase {
" \"templates\" : { },\n" + " \"templates\" : { },\n" +
" \"indices\" : {\n" + " \"indices\" : {\n" +
" \"index\" : {\n" + " \"index\" : {\n" +
" \"version\" : 1,\n" +
" \"mapping_version\" : 1,\n" +
" \"settings_version\" : 1,\n" +
" \"aliases_version\" : 1,\n" +
" \"routing_num_shards\" : 1,\n" +
" \"state\" : \"open\",\n" + " \"state\" : \"open\",\n" +
" \"settings\" : {\n" + " \"settings\" : {\n" +
" \"index\" : {\n" + " \"index\" : {\n" +
@ -204,7 +217,8 @@ public class ClusterRerouteResponseTests extends ESTestCase {
" },\n" + " },\n" +
" \"in_sync_allocations\" : {\n" + " \"in_sync_allocations\" : {\n" +
" \"0\" : [ ]\n" + " \"0\" : [ ]\n" +
" }\n" + " },\n" +
" \"rollover_info\" : { }\n" +
" }\n" + " }\n" +
" },\n" + " },\n" +
" \"index-graveyard\" : {\n" + " \"index-graveyard\" : {\n" +

View File

@ -19,14 +19,49 @@
package org.elasticsearch.cluster; package org.elasticsearch.cluster;
import org.elasticsearch.Version; import org.elasticsearch.Version;
import org.elasticsearch.action.admin.indices.rollover.RolloverInfo;
import org.elasticsearch.cluster.block.ClusterBlock;
import org.elasticsearch.cluster.block.ClusterBlockLevel;
import org.elasticsearch.cluster.block.ClusterBlocks;
import org.elasticsearch.cluster.coordination.CoordinationMetaData;
import org.elasticsearch.cluster.metadata.AliasMetaData;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.IndexTemplateMetaData;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.node.DiscoveryNodes; import org.elasticsearch.cluster.node.DiscoveryNodes;
import org.elasticsearch.cluster.routing.IndexRoutingTable;
import org.elasticsearch.cluster.routing.IndexShardRoutingTable;
import org.elasticsearch.cluster.routing.RoutingTable;
import org.elasticsearch.cluster.routing.ShardRoutingState;
import org.elasticsearch.cluster.routing.TestShardRouting;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.collect.ImmutableOpenMap; import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.TestCustomMetaData;
import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import static java.util.Collections.emptyMap; import static java.util.Collections.emptyMap;
import static java.util.Collections.emptySet; import static java.util.Collections.emptySet;
import static java.util.Collections.singletonMap;
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_VERSION_CREATED;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
@ -73,4 +108,751 @@ public class ClusterStateTests extends ESTestCase {
final ImmutableOpenMap<String, ClusterState.Custom> map = mapBuilder.build(); final ImmutableOpenMap<String, ClusterState.Custom> map = mapBuilder.build();
assertThat(expectThrows(NullPointerException.class, () -> builder.customs(map)).getMessage(), containsString(key)); assertThat(expectThrows(NullPointerException.class, () -> builder.customs(map)).getMessage(), containsString(key));
} }
public void testToXContent() throws IOException {
final ClusterState clusterState = buildClusterState();
IndexRoutingTable index = clusterState.getRoutingTable().getIndicesRouting().get("index");
String ephemeralId = clusterState.getNodes().get("nodeId1").getEphemeralId();
String allocationId = index.getShards().get(1).getAllAllocationIds().iterator().next();
XContentBuilder builder = JsonXContent.contentBuilder().prettyPrint();
builder.startObject();
clusterState.toXContent(builder, new ToXContent.MapParams(singletonMap(MetaData.CONTEXT_MODE_PARAM, MetaData.CONTEXT_MODE_API)));
builder.endObject();
assertEquals("{\n" +
" \"cluster_uuid\" : \"clusterUUID\",\n" +
" \"version\" : 0,\n" +
" \"state_uuid\" : \"stateUUID\",\n" +
" \"master_node\" : \"masterNodeId\",\n" +
" \"blocks\" : {\n" +
" \"global\" : {\n" +
" \"1\" : {\n" +
" \"description\" : \"description\",\n" +
" \"retryable\" : true,\n" +
" \"disable_state_persistence\" : true,\n" +
" \"levels\" : [\n" +
" \"read\",\n" +
" \"write\",\n" +
" \"metadata_read\",\n" +
" \"metadata_write\"\n" +
" ]\n" +
" }\n" +
" },\n" +
" \"indices\" : {\n" +
" \"index\" : {\n" +
" \"2\" : {\n" +
" \"description\" : \"description2\",\n" +
" \"retryable\" : false,\n" +
" \"levels\" : [\n" +
" \"read\",\n" +
" \"write\",\n" +
" \"metadata_read\",\n" +
" \"metadata_write\"\n" +
" ]\n" +
" }\n" +
" }\n" +
" }\n" +
" },\n" +
" \"nodes\" : {\n" +
" \"nodeId1\" : {\n" +
" \"name\" : \"\",\n" +
" \"ephemeral_id\" : \"" + ephemeralId + "\",\n" +
" \"transport_address\" : \"127.0.0.1:111\",\n" +
" \"attributes\" : { }\n" +
" }\n" +
" },\n" +
" \"metadata\" : {\n" +
" \"cluster_uuid\" : \"clusterUUID\",\n" +
" \"cluster_uuid_committed\" : false,\n" +
" \"cluster_coordination\" : {\n" +
" \"term\" : 1,\n" +
" \"last_committed_config\" : [\n" +
" \"commitedConfigurationNodeId\"\n" +
" ],\n" +
" \"last_accepted_config\" : [\n" +
" \"acceptedConfigurationNodeId\"\n" +
" ],\n" +
" \"voting_config_exclusions\" : [\n" +
" {\n" +
" \"node_id\" : \"exlucdedNodeId\",\n" +
" \"node_name\" : \"excludedNodeName\"\n" +
" }\n" +
" ]\n" +
" },\n" +
" \"templates\" : {\n" +
" \"template\" : {\n" +
" \"order\" : 0,\n" +
" \"index_patterns\" : [\n" +
" \"pattern1\",\n" +
" \"pattern2\"\n" +
" ],\n" +
" \"settings\" : {\n" +
" \"index\" : {\n" +
" \"version\" : {\n" +
" \"created\" : \"" + Version.CURRENT.id + "\"\n" +
" }\n" +
" }\n" +
" },\n" +
" \"mappings\" : {\n" +
" \"type\" : {\n" +
" \"key1\" : { }\n" +
" }\n" +
" },\n" +
" \"aliases\" : { }\n" +
" }\n" +
" },\n" +
" \"indices\" : {\n" +
" \"index\" : {\n" +
" \"version\" : 1,\n" +
" \"mapping_version\" : 1,\n" +
" \"settings_version\" : 1,\n" +
" \"aliases_version\" : 1,\n" +
" \"routing_num_shards\" : 1,\n" +
" \"state\" : \"open\",\n" +
" \"settings\" : {\n" +
" \"index\" : {\n" +
" \"number_of_shards\" : \"1\",\n" +
" \"number_of_replicas\" : \"2\",\n" +
" \"version\" : {\n" +
" \"created\" : \"" + Version.CURRENT.id + "\"\n" +
" }\n" +
" }\n" +
" },\n" +
" \"mappings\" : {\n" +
" \"type\" : {\n" +
" \"type1\" : {\n" +
" \"key\" : \"value\"\n" +
" }\n" +
" }\n" +
" },\n" +
" \"aliases\" : [\n" +
" \"alias\"\n" +
" ],\n" +
" \"primary_terms\" : {\n" +
" \"0\" : 1\n" +
" },\n" +
" \"in_sync_allocations\" : {\n" +
" \"0\" : [\n" +
" \"allocationId\"\n" +
" ]\n" +
" },\n" +
" \"rollover_info\" : {\n" +
" \"rolloveAlias\" : {\n" +
" \"met_conditions\" : { },\n" +
" \"time\" : 1\n" +
" }\n" +
" }\n" +
" }\n" +
" },\n" +
" \"index-graveyard\" : {\n" +
" \"tombstones\" : [ ]\n" +
" }\n" +
" },\n" +
" \"routing_table\" : {\n" +
" \"indices\" : {\n" +
" \"index\" : {\n" +
" \"shards\" : {\n" +
" \"1\" : [\n" +
" {\n" +
" \"state\" : \"STARTED\",\n" +
" \"primary\" : true,\n" +
" \"node\" : \"nodeId2\",\n" +
" \"relocating_node\" : null,\n" +
" \"shard\" : 1,\n" +
" \"index\" : \"index\",\n" +
" \"allocation_id\" : {\n" +
" \"id\" : \"" + allocationId + "\"\n" +
" }\n" +
" }\n" +
" ]\n" +
" }\n" +
" }\n" +
" }\n" +
" },\n" +
" \"routing_nodes\" : {\n" +
" \"unassigned\" : [ ],\n" +
" \"nodes\" : {\n" +
" \"nodeId2\" : [\n" +
" {\n" +
" \"state\" : \"STARTED\",\n" +
" \"primary\" : true,\n" +
" \"node\" : \"nodeId2\",\n" +
" \"relocating_node\" : null,\n" +
" \"shard\" : 1,\n" +
" \"index\" : \"index\",\n" +
" \"allocation_id\" : {\n" +
" \"id\" : \"" + allocationId + "\"\n" +
" }\n" +
" }\n" +
" ],\n" +
" \"nodeId1\" : [ ]\n" +
" }\n" +
" }\n" +
"}", Strings.toString(builder));
}
public void testToXContent_FlatSettingTrue_ReduceMappingFalse() throws IOException {
Map<String, String> mapParams = new HashMap<String,String>(){{
put("flat_settings", "true");
put("reduce_mappings", "false");
put(MetaData.CONTEXT_MODE_PARAM, MetaData.CONTEXT_MODE_API);
}};
final ClusterState clusterState = buildClusterState();
IndexRoutingTable index = clusterState.getRoutingTable().getIndicesRouting().get("index");
String ephemeralId = clusterState.getNodes().get("nodeId1").getEphemeralId();
String allocationId = index.getShards().get(1).getAllAllocationIds().iterator().next();
XContentBuilder builder = JsonXContent.contentBuilder().prettyPrint();
builder.startObject();
clusterState.toXContent(builder, new ToXContent.MapParams(mapParams));
builder.endObject();
assertEquals("{\n" +
" \"cluster_uuid\" : \"clusterUUID\",\n" +
" \"version\" : 0,\n" +
" \"state_uuid\" : \"stateUUID\",\n" +
" \"master_node\" : \"masterNodeId\",\n" +
" \"blocks\" : {\n" +
" \"global\" : {\n" +
" \"1\" : {\n" +
" \"description\" : \"description\",\n" +
" \"retryable\" : true,\n" +
" \"disable_state_persistence\" : true,\n" +
" \"levels\" : [\n" +
" \"read\",\n" +
" \"write\",\n" +
" \"metadata_read\",\n" +
" \"metadata_write\"\n" +
" ]\n" +
" }\n" +
" },\n" +
" \"indices\" : {\n" +
" \"index\" : {\n" +
" \"2\" : {\n" +
" \"description\" : \"description2\",\n" +
" \"retryable\" : false,\n" +
" \"levels\" : [\n" +
" \"read\",\n" +
" \"write\",\n" +
" \"metadata_read\",\n" +
" \"metadata_write\"\n" +
" ]\n" +
" }\n" +
" }\n" +
" }\n" +
" },\n" +
" \"nodes\" : {\n" +
" \"nodeId1\" : {\n" +
" \"name\" : \"\",\n" +
" \"ephemeral_id\" : \"" + ephemeralId + "\",\n" +
" \"transport_address\" : \"127.0.0.1:111\",\n" +
" \"attributes\" : { }\n" +
" }\n" +
" },\n" +
" \"metadata\" : {\n" +
" \"cluster_uuid\" : \"clusterUUID\",\n" +
" \"cluster_uuid_committed\" : false,\n" +
" \"cluster_coordination\" : {\n" +
" \"term\" : 1,\n" +
" \"last_committed_config\" : [\n" +
" \"commitedConfigurationNodeId\"\n" +
" ],\n" +
" \"last_accepted_config\" : [\n" +
" \"acceptedConfigurationNodeId\"\n" +
" ],\n" +
" \"voting_config_exclusions\" : [\n" +
" {\n" +
" \"node_id\" : \"exlucdedNodeId\",\n" +
" \"node_name\" : \"excludedNodeName\"\n" +
" }\n" +
" ]\n" +
" },\n" +
" \"templates\" : {\n" +
" \"template\" : {\n" +
" \"order\" : 0,\n" +
" \"index_patterns\" : [\n" +
" \"pattern1\",\n" +
" \"pattern2\"\n" +
" ],\n" +
" \"settings\" : {\n" +
" \"index.version.created\" : \"" + Version.CURRENT.id + "\"\n" +
" },\n" +
" \"mappings\" : {\n" +
" \"type\" : {\n" +
" \"key1\" : { }\n" +
" }\n" +
" },\n" +
" \"aliases\" : { }\n" +
" }\n" +
" },\n" +
" \"indices\" : {\n" +
" \"index\" : {\n" +
" \"version\" : 1,\n" +
" \"mapping_version\" : 1,\n" +
" \"settings_version\" : 1,\n" +
" \"aliases_version\" : 1,\n" +
" \"routing_num_shards\" : 1,\n" +
" \"state\" : \"open\",\n" +
" \"settings\" : {\n" +
" \"index.number_of_replicas\" : \"2\",\n" +
" \"index.number_of_shards\" : \"1\",\n" +
" \"index.version.created\" : \"" + Version.CURRENT.id + "\"\n" +
" },\n" +
" \"mappings\" : {\n" +
" \"type\" : {\n" +
" \"type1\" : {\n" +
" \"key\" : \"value\"\n" +
" }\n" +
" }\n" +
" },\n" +
" \"aliases\" : [\n" +
" \"alias\"\n" +
" ],\n" +
" \"primary_terms\" : {\n" +
" \"0\" : 1\n" +
" },\n" +
" \"in_sync_allocations\" : {\n" +
" \"0\" : [\n" +
" \"allocationId\"\n" +
" ]\n" +
" },\n" +
" \"rollover_info\" : {\n" +
" \"rolloveAlias\" : {\n" +
" \"met_conditions\" : { },\n" +
" \"time\" : 1\n" +
" }\n" +
" }\n" +
" }\n" +
" },\n" +
" \"index-graveyard\" : {\n" +
" \"tombstones\" : [ ]\n" +
" }\n" +
" },\n" +
" \"routing_table\" : {\n" +
" \"indices\" : {\n" +
" \"index\" : {\n" +
" \"shards\" : {\n" +
" \"1\" : [\n" +
" {\n" +
" \"state\" : \"STARTED\",\n" +
" \"primary\" : true,\n" +
" \"node\" : \"nodeId2\",\n" +
" \"relocating_node\" : null,\n" +
" \"shard\" : 1,\n" +
" \"index\" : \"index\",\n" +
" \"allocation_id\" : {\n" +
" \"id\" : \"" + allocationId + "\"\n" +
" }\n" +
" }\n" +
" ]\n" +
" }\n" +
" }\n" +
" }\n" +
" },\n" +
" \"routing_nodes\" : {\n" +
" \"unassigned\" : [ ],\n" +
" \"nodes\" : {\n" +
" \"nodeId2\" : [\n" +
" {\n" +
" \"state\" : \"STARTED\",\n" +
" \"primary\" : true,\n" +
" \"node\" : \"nodeId2\",\n" +
" \"relocating_node\" : null,\n" +
" \"shard\" : 1,\n" +
" \"index\" : \"index\",\n" +
" \"allocation_id\" : {\n" +
" \"id\" : \"" + allocationId + "\"\n" +
" }\n" +
" }\n" +
" ],\n" +
" \"nodeId1\" : [ ]\n" +
" }\n" +
" }\n" +
"}", Strings.toString(builder));
}
public void testToXContent_FlatSettingFalse_ReduceMappingTrue() throws IOException {
Map<String, String> mapParams = new HashMap<String,String>(){{
put("flat_settings", "false");
put("reduce_mappings", "true");
put(MetaData.CONTEXT_MODE_PARAM, MetaData.CONTEXT_MODE_API);
}};
final ClusterState clusterState = buildClusterState();
IndexRoutingTable index = clusterState.getRoutingTable().getIndicesRouting().get("index");
String ephemeralId = clusterState.getNodes().get("nodeId1").getEphemeralId();
String allocationId = index.getShards().get(1).getAllAllocationIds().iterator().next();
XContentBuilder builder = JsonXContent.contentBuilder().prettyPrint();
builder.startObject();
clusterState.toXContent(builder, new ToXContent.MapParams(mapParams));
builder.endObject();
assertEquals("{\n" +
" \"cluster_uuid\" : \"clusterUUID\",\n" +
" \"version\" : 0,\n" +
" \"state_uuid\" : \"stateUUID\",\n" +
" \"master_node\" : \"masterNodeId\",\n" +
" \"blocks\" : {\n" +
" \"global\" : {\n" +
" \"1\" : {\n" +
" \"description\" : \"description\",\n" +
" \"retryable\" : true,\n" +
" \"disable_state_persistence\" : true,\n" +
" \"levels\" : [\n" +
" \"read\",\n" +
" \"write\",\n" +
" \"metadata_read\",\n" +
" \"metadata_write\"\n" +
" ]\n" +
" }\n" +
" },\n" +
" \"indices\" : {\n" +
" \"index\" : {\n" +
" \"2\" : {\n" +
" \"description\" : \"description2\",\n" +
" \"retryable\" : false,\n" +
" \"levels\" : [\n" +
" \"read\",\n" +
" \"write\",\n" +
" \"metadata_read\",\n" +
" \"metadata_write\"\n" +
" ]\n" +
" }\n" +
" }\n" +
" }\n" +
" },\n" +
" \"nodes\" : {\n" +
" \"nodeId1\" : {\n" +
" \"name\" : \"\",\n" +
" \"ephemeral_id\" : \"" + ephemeralId + "\",\n" +
" \"transport_address\" : \"127.0.0.1:111\",\n" +
" \"attributes\" : { }\n" +
" }\n" +
" },\n" +
" \"metadata\" : {\n" +
" \"cluster_uuid\" : \"clusterUUID\",\n" +
" \"cluster_uuid_committed\" : false,\n" +
" \"cluster_coordination\" : {\n" +
" \"term\" : 1,\n" +
" \"last_committed_config\" : [\n" +
" \"commitedConfigurationNodeId\"\n" +
" ],\n" +
" \"last_accepted_config\" : [\n" +
" \"acceptedConfigurationNodeId\"\n" +
" ],\n" +
" \"voting_config_exclusions\" : [\n" +
" {\n" +
" \"node_id\" : \"exlucdedNodeId\",\n" +
" \"node_name\" : \"excludedNodeName\"\n" +
" }\n" +
" ]\n" +
" },\n" +
" \"templates\" : {\n" +
" \"template\" : {\n" +
" \"order\" : 0,\n" +
" \"index_patterns\" : [\n" +
" \"pattern1\",\n" +
" \"pattern2\"\n" +
" ],\n" +
" \"settings\" : {\n" +
" \"index\" : {\n" +
" \"version\" : {\n" +
" \"created\" : \"" + Version.CURRENT.id + "\"\n" +
" }\n" +
" }\n" +
" },\n" +
" \"mappings\" : {\n" +
" \"type\" : {\n" +
" \"key1\" : { }\n" +
" }\n" +
" },\n" +
" \"aliases\" : { }\n" +
" }\n" +
" },\n" +
" \"indices\" : {\n" +
" \"index\" : {\n" +
" \"version\" : 1,\n" +
" \"mapping_version\" : 1,\n" +
" \"settings_version\" : 1,\n" +
" \"aliases_version\" : 1,\n" +
" \"routing_num_shards\" : 1,\n" +
" \"state\" : \"open\",\n" +
" \"settings\" : {\n" +
" \"index\" : {\n" +
" \"number_of_shards\" : \"1\",\n" +
" \"number_of_replicas\" : \"2\",\n" +
" \"version\" : {\n" +
" \"created\" : \"" + Version.CURRENT.id + "\"\n" +
" }\n" +
" }\n" +
" },\n" +
" \"mappings\" : {\n" +
" \"type\" : {\n" +
" \"type1\" : {\n" +
" \"key\" : \"value\"\n" +
" }\n" +
" }\n" +
" },\n" +
" \"aliases\" : [\n" +
" \"alias\"\n" +
" ],\n" +
" \"primary_terms\" : {\n" +
" \"0\" : 1\n" +
" },\n" +
" \"in_sync_allocations\" : {\n" +
" \"0\" : [\n" +
" \"allocationId\"\n" +
" ]\n" +
" },\n" +
" \"rollover_info\" : {\n" +
" \"rolloveAlias\" : {\n" +
" \"met_conditions\" : { },\n" +
" \"time\" : 1\n" +
" }\n" +
" }\n" +
" }\n" +
" },\n" +
" \"index-graveyard\" : {\n" +
" \"tombstones\" : [ ]\n" +
" }\n" +
" },\n" +
" \"routing_table\" : {\n" +
" \"indices\" : {\n" +
" \"index\" : {\n" +
" \"shards\" : {\n" +
" \"1\" : [\n" +
" {\n" +
" \"state\" : \"STARTED\",\n" +
" \"primary\" : true,\n" +
" \"node\" : \"nodeId2\",\n" +
" \"relocating_node\" : null,\n" +
" \"shard\" : 1,\n" +
" \"index\" : \"index\",\n" +
" \"allocation_id\" : {\n" +
" \"id\" : \"" + allocationId + "\"\n" +
" }\n" +
" }\n" +
" ]\n" +
" }\n" +
" }\n" +
" }\n" +
" },\n" +
" \"routing_nodes\" : {\n" +
" \"unassigned\" : [ ],\n" +
" \"nodes\" : {\n" +
" \"nodeId2\" : [\n" +
" {\n" +
" \"state\" : \"STARTED\",\n" +
" \"primary\" : true,\n" +
" \"node\" : \"nodeId2\",\n" +
" \"relocating_node\" : null,\n" +
" \"shard\" : 1,\n" +
" \"index\" : \"index\",\n" +
" \"allocation_id\" : {\n" +
" \"id\" : \"" + allocationId + "\"\n" +
" }\n" +
" }\n" +
" ],\n" +
" \"nodeId1\" : [ ]\n" +
" }\n" +
" }\n" +
"}", Strings.toString(builder));
}
public void testToXContentSameTypeName() throws IOException {
final ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT)
.stateUUID("stateUUID")
.metaData(MetaData.builder()
.clusterUUID("clusterUUID")
.coordinationMetaData(CoordinationMetaData.builder()
.build())
.put(IndexMetaData.builder("index")
.state(IndexMetaData.State.OPEN)
.settings(Settings.builder()
.put(SETTING_VERSION_CREATED, Version.CURRENT.id))
.putMapping(new MappingMetaData("type",
// the type name is the root value,
// the original logic in ClusterState.toXContent will reduce
new HashMap<String,Object>(){{
put("type", new HashMap<String, Object>(){{
put("key", "value");
}});
}}))
.numberOfShards(1)
.primaryTerm(0, 1L)
.numberOfReplicas(2)))
.build();
XContentBuilder builder = JsonXContent.contentBuilder().prettyPrint();
builder.startObject();
clusterState.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
assertEquals("{\n" +
" \"cluster_uuid\" : \"clusterUUID\",\n" +
" \"version\" : 0,\n" +
" \"state_uuid\" : \"stateUUID\",\n" +
" \"master_node\" : null,\n" +
" \"blocks\" : { },\n" +
" \"nodes\" : { },\n" +
" \"metadata\" : {\n" +
" \"cluster_uuid\" : \"clusterUUID\",\n" +
" \"cluster_uuid_committed\" : false,\n" +
" \"cluster_coordination\" : {\n" +
" \"term\" : 0,\n" +
" \"last_committed_config\" : [ ],\n" +
" \"last_accepted_config\" : [ ],\n" +
" \"voting_config_exclusions\" : [ ]\n" +
" },\n" +
" \"templates\" : { },\n" +
" \"indices\" : {\n" +
" \"index\" : {\n" +
" \"version\" : 2,\n" +
" \"mapping_version\" : 1,\n" +
" \"settings_version\" : 1,\n" +
" \"aliases_version\" : 1,\n" +
" \"routing_num_shards\" : 1,\n" +
" \"state\" : \"open\",\n" +
" \"settings\" : {\n" +
" \"index\" : {\n" +
" \"number_of_shards\" : \"1\",\n" +
" \"number_of_replicas\" : \"2\",\n" +
" \"version\" : {\n" +
" \"created\" : \"" + Version.CURRENT.id + "\"\n" +
" }\n" +
" }\n" +
" },\n" +
" \"mappings\" : {\n" +
" \"type\" : {\n" +
" \"key\" : \"value\"\n" +
" }\n" +
" },\n" +
" \"aliases\" : [ ],\n" +
" \"primary_terms\" : {\n" +
" \"0\" : 1\n" +
" },\n" +
" \"in_sync_allocations\" : {\n" +
" \"0\" : [ ]\n" +
" },\n" +
" \"rollover_info\" : { }\n" +
" }\n" +
" },\n" +
" \"index-graveyard\" : {\n" +
" \"tombstones\" : [ ]\n" +
" }\n" +
" },\n" +
" \"routing_table\" : {\n" +
" \"indices\" : { }\n" +
" },\n" +
" \"routing_nodes\" : {\n" +
" \"unassigned\" : [ ],\n" +
" \"nodes\" : { }\n" +
" }\n" +
"}", Strings.toString(builder));
}
private ClusterState buildClusterState() throws IOException {
IndexMetaData indexMetaData = IndexMetaData.builder("index")
.state(IndexMetaData.State.OPEN)
.settings(Settings.builder()
.put(SETTING_VERSION_CREATED, Version.CURRENT.id))
.putMapping(new MappingMetaData("type",
new HashMap<String,Object>() {{
put("type1", new HashMap<String, Object>(){{
put("key", "value");
}});
}}))
.putAlias(AliasMetaData.builder("alias")
.indexRouting("indexRouting")
.build())
.numberOfShards(1)
.primaryTerm(0, 1L)
.putInSyncAllocationIds(0, new HashSet<String>() {{
add("allocationId");
}})
.numberOfReplicas(2)
.putRolloverInfo(new RolloverInfo("rolloveAlias", new ArrayList<>(), 1L))
.build();
return ClusterState.builder(ClusterName.DEFAULT)
.stateUUID("stateUUID")
.nodes(DiscoveryNodes.builder()
.masterNodeId("masterNodeId")
.add(new DiscoveryNode("nodeId1", new TransportAddress(InetAddress.getByName("127.0.0.1"), 111),
Version.CURRENT))
.build())
.blocks(ClusterBlocks.builder()
.addGlobalBlock(
new ClusterBlock(1, "description", true, true, true, RestStatus.ACCEPTED, EnumSet.allOf((ClusterBlockLevel.class))))
.addBlocks(indexMetaData)
.addIndexBlock("index",
new ClusterBlock(2, "description2", false, false, false, RestStatus.ACCEPTED, EnumSet.allOf((ClusterBlockLevel.class))))
.build())
.metaData(MetaData.builder()
.clusterUUID("clusterUUID")
.coordinationMetaData(CoordinationMetaData.builder()
.term(1)
.lastCommittedConfiguration(new CoordinationMetaData.VotingConfiguration(new HashSet<String>(){{
add("commitedConfigurationNodeId");
}}))
.lastAcceptedConfiguration(new CoordinationMetaData.VotingConfiguration(new HashSet<String>(){{
add("acceptedConfigurationNodeId");
}}))
.addVotingConfigExclusion(new CoordinationMetaData.VotingConfigExclusion("exlucdedNodeId", "excludedNodeName"))
.build())
.persistentSettings(Settings.builder()
.put(SETTING_VERSION_CREATED, Version.CURRENT.id).build())
.transientSettings(Settings.builder()
.put(SETTING_VERSION_CREATED, Version.CURRENT.id).build())
.put(indexMetaData, false)
.put(IndexTemplateMetaData.builder("template")
.patterns(Arrays.asList("pattern1", "pattern2"))
.order(0)
.settings(Settings.builder().put(SETTING_VERSION_CREATED, Version.CURRENT.id))
.putMapping("type", "{ \"key1\": {} }")
.build()))
.routingTable(RoutingTable.builder()
.add(IndexRoutingTable.builder(new Index("index", "indexUUID"))
.addIndexShard(new IndexShardRoutingTable.Builder(new ShardId("index", "_na_", 1))
.addShard(TestShardRouting.newShardRouting(
new ShardId("index", "_na_", 1), "nodeId2", true, ShardRoutingState.STARTED))
.build())
.build())
.build())
.build();
}
public static class CustomMetaData extends TestCustomMetaData {
public static final String TYPE = "custom_md";
CustomMetaData(String data) {
super(data);
}
@Override
public String getWriteableName() {
return TYPE;
}
@Override
public Version getMinimalSupportedVersion() {
return Version.CURRENT;
}
@Override
public EnumSet<MetaData.XContentContext> context() {
return EnumSet.of(MetaData.XContentContext.GATEWAY, MetaData.XContentContext.SNAPSHOT);
}
}
} }

View File

@ -19,22 +19,12 @@
package org.elasticsearch.cluster.coordination; package org.elasticsearch.cluster.coordination;
import org.elasticsearch.Version;
import org.elasticsearch.action.admin.indices.rollover.MaxAgeCondition;
import org.elasticsearch.action.admin.indices.rollover.MaxDocsCondition;
import org.elasticsearch.action.admin.indices.rollover.MaxSizeCondition;
import org.elasticsearch.action.admin.indices.rollover.RolloverInfo;
import org.elasticsearch.cluster.ClusterModule; import org.elasticsearch.cluster.ClusterModule;
import org.elasticsearch.cluster.metadata.IndexGraveyard; import org.elasticsearch.cluster.metadata.IndexGraveyard;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.common.UUIDs; import org.elasticsearch.common.UUIDs;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.NamedXContentRegistry; import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.common.xcontent.json.JsonXContent;
@ -44,7 +34,6 @@ import org.elasticsearch.test.ESTestCase;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Arrays;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -52,7 +41,6 @@ import java.util.stream.Stream;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
public class ElasticsearchNodeCommandTests extends ESTestCase { public class ElasticsearchNodeCommandTests extends ESTestCase {
@ -76,7 +64,7 @@ public class ElasticsearchNodeCommandTests extends ESTestCase {
final MetaData latestMetaData = randomMeta(); final MetaData latestMetaData = randomMeta();
final XContentBuilder builder = JsonXContent.contentBuilder(); final XContentBuilder builder = JsonXContent.contentBuilder();
builder.startObject(); builder.startObject();
latestMetaData.toXContent(builder, ToXContent.EMPTY_PARAMS); MetaData.FORMAT.toXContent(builder, latestMetaData);
builder.endObject(); builder.endObject();
MetaData loadedMetaData; MetaData loadedMetaData;
@ -86,17 +74,6 @@ public class ElasticsearchNodeCommandTests extends ESTestCase {
} }
assertThat(loadedMetaData.clusterUUID(), not(equalTo("_na_"))); assertThat(loadedMetaData.clusterUUID(), not(equalTo("_na_")));
assertThat(loadedMetaData.clusterUUID(), equalTo(latestMetaData.clusterUUID())); assertThat(loadedMetaData.clusterUUID(), equalTo(latestMetaData.clusterUUID()));
ImmutableOpenMap<String, IndexMetaData> indices = loadedMetaData.indices();
assertThat(indices.size(), equalTo(latestMetaData.indices().size()));
for (IndexMetaData original : latestMetaData) {
IndexMetaData deserialized = indices.get(original.getIndex().getName());
assertThat(deserialized, notNullValue());
assertThat(deserialized.getVersion(), equalTo(original.getVersion()));
assertThat(deserialized.getMappingVersion(), equalTo(original.getMappingVersion()));
assertThat(deserialized.getSettingsVersion(), equalTo(original.getSettingsVersion()));
assertThat(deserialized.getNumberOfReplicas(), equalTo(original.getNumberOfReplicas()));
assertThat(deserialized.getNumberOfShards(), equalTo(original.getNumberOfShards()));
}
// make sure the index tombstones are the same too // make sure the index tombstones are the same too
if (hasMissingCustoms) { if (hasMissingCustoms) {
@ -116,12 +93,8 @@ public class ElasticsearchNodeCommandTests extends ESTestCase {
} }
private MetaData randomMeta() { private MetaData randomMeta() {
int numIndices = randomIntBetween(1, 10);
MetaData.Builder mdBuilder = MetaData.builder(); MetaData.Builder mdBuilder = MetaData.builder();
mdBuilder.generateClusterUuidIfNeeded(); mdBuilder.generateClusterUuidIfNeeded();
for (int i = 0; i < numIndices; i++) {
mdBuilder.put(indexBuilder(randomAlphaOfLength(10) + "idx-"+i));
}
int numDelIndices = randomIntBetween(0, 5); int numDelIndices = randomIntBetween(0, 5);
final IndexGraveyard.Builder graveyard = IndexGraveyard.builder(); final IndexGraveyard.Builder graveyard = IndexGraveyard.builder();
for (int i = 0; i < numDelIndices; i++) { for (int i = 0; i < numDelIndices; i++) {
@ -131,17 +104,6 @@ public class ElasticsearchNodeCommandTests extends ESTestCase {
return mdBuilder.build(); return mdBuilder.build();
} }
private IndexMetaData.Builder indexBuilder(String index) {
return IndexMetaData.builder(index)
.settings(settings(Version.CURRENT).put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10))
.put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 5)))
.putRolloverInfo(new RolloverInfo("test", randomSubsetOf(Arrays.asList(
new MaxAgeCondition(TimeValue.timeValueSeconds(100)),
new MaxDocsCondition(100L),
new MaxSizeCondition(new ByteSizeValue(100))
)), 0));
}
@Override @Override
protected NamedXContentRegistry xContentRegistry() { protected NamedXContentRegistry xContentRegistry() {
return new NamedXContentRegistry(Stream.of(ClusterModule.getNamedXWriteables().stream(), IndicesModule.getNamedXContents().stream()) return new NamedXContentRegistry(Stream.of(ClusterModule.getNamedXWriteables().stream(), IndicesModule.getNamedXContents().stream())

View File

@ -86,6 +86,11 @@ public class Zen1IT extends ESIntegTestCase {
return Collections.singletonList(MockTransportService.TestPlugin.class); return Collections.singletonList(MockTransportService.TestPlugin.class);
} }
@Override
protected void ensureClusterStateConsistency() {
// Zen1 does not properly handle the cluster_uuid_committed field
}
public void testZen2NodesJoiningZen1Cluster() { public void testZen2NodesJoiningZen1Cluster() {
internalCluster().startNodes(randomIntBetween(1, 3), ZEN1_SETTINGS); internalCluster().startNodes(randomIntBetween(1, 3), ZEN1_SETTINGS);
internalCluster().startNodes(randomIntBetween(1, 3), ZEN2_SETTINGS); internalCluster().startNodes(randomIntBetween(1, 3), ZEN2_SETTINGS);

View File

@ -36,7 +36,6 @@ import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.util.set.Sets; import org.elasticsearch.common.util.set.Sets;
import org.elasticsearch.common.xcontent.NamedXContentRegistry; import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.common.xcontent.json.JsonXContent;
@ -99,7 +98,7 @@ public class IndexMetaDataTests extends ESTestCase {
final XContentBuilder builder = JsonXContent.contentBuilder(); final XContentBuilder builder = JsonXContent.contentBuilder();
builder.startObject(); builder.startObject();
metaData.toXContent(builder, ToXContent.EMPTY_PARAMS); IndexMetaData.FORMAT.toXContent(builder, metaData);
builder.endObject(); builder.endObject();
XContentParser parser = createParser(JsonXContent.jsonXContent, BytesReference.bytes(builder)); XContentParser parser = createParser(JsonXContent.jsonXContent, BytesReference.bytes(builder));
final IndexMetaData fromXContentMeta = IndexMetaData.fromXContent(parser); final IndexMetaData fromXContentMeta = IndexMetaData.fromXContent(parser);

View File

@ -34,7 +34,6 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.set.Sets; import org.elasticsearch.common.util.set.Sets;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
@ -438,7 +437,7 @@ public class MetaDataTests extends ESTestCase {
final MetaData originalMeta = MetaData.builder().indexGraveyard(graveyard).build(); final MetaData originalMeta = MetaData.builder().indexGraveyard(graveyard).build();
final XContentBuilder builder = JsonXContent.contentBuilder(); final XContentBuilder builder = JsonXContent.contentBuilder();
builder.startObject(); builder.startObject();
originalMeta.toXContent(builder, ToXContent.EMPTY_PARAMS); MetaData.FORMAT.toXContent(builder, originalMeta);
builder.endObject(); builder.endObject();
try (XContentParser parser = createParser(JsonXContent.jsonXContent, BytesReference.bytes(builder))) { try (XContentParser parser = createParser(JsonXContent.jsonXContent, BytesReference.bytes(builder))) {
final MetaData fromXContentMeta = MetaData.fromXContent(parser); final MetaData fromXContentMeta = MetaData.fromXContent(parser);
@ -451,7 +450,7 @@ public class MetaDataTests extends ESTestCase {
.clusterUUIDCommitted(randomBoolean()).build(); .clusterUUIDCommitted(randomBoolean()).build();
final XContentBuilder builder = JsonXContent.contentBuilder(); final XContentBuilder builder = JsonXContent.contentBuilder();
builder.startObject(); builder.startObject();
originalMeta.toXContent(builder, ToXContent.EMPTY_PARAMS); MetaData.FORMAT.toXContent(builder, originalMeta);
builder.endObject(); builder.endObject();
try (XContentParser parser = createParser(JsonXContent.jsonXContent, BytesReference.bytes(builder))) { try (XContentParser parser = createParser(JsonXContent.jsonXContent, BytesReference.bytes(builder))) {
final MetaData fromXContentMeta = MetaData.fromXContent(parser); final MetaData fromXContentMeta = MetaData.fromXContent(parser);
@ -504,7 +503,7 @@ public class MetaDataTests extends ESTestCase {
final XContentBuilder builder = JsonXContent.contentBuilder(); final XContentBuilder builder = JsonXContent.contentBuilder();
builder.startObject(); builder.startObject();
metaData.toXContent(builder, ToXContent.EMPTY_PARAMS); MetaData.FORMAT.toXContent(builder, metaData);
builder.endObject(); builder.endObject();
try (XContentParser parser = createParser(JsonXContent.jsonXContent, BytesReference.bytes(builder))) { try (XContentParser parser = createParser(JsonXContent.jsonXContent, BytesReference.bytes(builder))) {

View File

@ -20,101 +20,37 @@
package org.elasticsearch.cluster.metadata; package org.elasticsearch.cluster.metadata;
import org.elasticsearch.Version; import org.elasticsearch.Version;
import org.elasticsearch.action.admin.indices.rollover.RolloverInfo;
import org.elasticsearch.cluster.coordination.CoordinationMetaData;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.TestCustomMetaData;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import static org.elasticsearch.cluster.metadata.AliasMetaData.newAliasMetaDataBuilder; import static org.elasticsearch.cluster.metadata.AliasMetaData.newAliasMetaDataBuilder;
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_VERSION_CREATED;
import static org.elasticsearch.cluster.metadata.MetaData.CONTEXT_MODE_API;
import static org.elasticsearch.cluster.metadata.MetaData.CONTEXT_MODE_GATEWAY;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
public class ToAndFromJsonMetaDataTests extends ESTestCase { public class ToAndFromJsonMetaDataTests extends ESTestCase {
public void testSimpleJsonFromAndTo() throws IOException { public void testSimpleJsonFromAndTo() throws IOException {
MetaData metaData = MetaData.builder() MetaData metaData = MetaData.builder()
.put(IndexMetaData.builder("test1")
.settings(settings(Version.CURRENT))
.numberOfShards(1)
.numberOfReplicas(2)
.primaryTerm(0, 1))
.put(IndexMetaData.builder("test2")
.settings(settings(Version.CURRENT).put("setting1", "value1").put("setting2", "value2"))
.numberOfShards(2)
.numberOfReplicas(3)
.primaryTerm(0, 2)
.primaryTerm(1, 2))
.put(IndexMetaData.builder("test3")
.settings(settings(Version.CURRENT))
.numberOfShards(1)
.numberOfReplicas(2)
.putMapping("mapping1", MAPPING_SOURCE1))
.put(IndexMetaData.builder("test4")
.settings(settings(Version.CURRENT))
.numberOfShards(1)
.numberOfReplicas(2)
.creationDate(2L))
.put(IndexMetaData.builder("test5")
.settings(settings(Version.CURRENT).put("setting1", "value1").put("setting2", "value2"))
.numberOfShards(1)
.numberOfReplicas(2)
.putMapping("mapping1", MAPPING_SOURCE1)
.putMapping("mapping2", MAPPING_SOURCE2))
.put(IndexMetaData.builder("test6")
.settings(settings(Version.CURRENT).put("setting1", "value1").put("setting2", "value2"))
.numberOfShards(1)
.numberOfReplicas(2)
.creationDate(2L))
.put(IndexMetaData.builder("test7")
.settings(settings(Version.CURRENT))
.numberOfShards(1)
.numberOfReplicas(2)
.creationDate(2L)
.putMapping("mapping1", MAPPING_SOURCE1)
.putMapping("mapping2", MAPPING_SOURCE2))
.put(IndexMetaData.builder("test8")
.settings(settings(Version.CURRENT).put("setting1", "value1").put("setting2", "value2"))
.numberOfShards(1)
.numberOfReplicas(2)
.putMapping("mapping1", MAPPING_SOURCE1)
.putMapping("mapping2", MAPPING_SOURCE2)
.putAlias(newAliasMetaDataBuilder("alias1"))
.putAlias(newAliasMetaDataBuilder("alias2")))
.put(IndexMetaData.builder("test9")
.settings(settings(Version.CURRENT).put("setting1", "value1").put("setting2", "value2"))
.creationDate(2L)
.numberOfShards(1)
.numberOfReplicas(2)
.putMapping("mapping1", MAPPING_SOURCE1)
.putMapping("mapping2", MAPPING_SOURCE2)
.putAlias(newAliasMetaDataBuilder("alias1"))
.putAlias(newAliasMetaDataBuilder("alias2")))
.put(IndexMetaData.builder("test10")
.settings(settings(Version.CURRENT)
.put("setting1", "value1")
.put("setting2", "value2"))
.numberOfShards(1)
.numberOfReplicas(2)
.putMapping("mapping1", MAPPING_SOURCE1)
.putMapping("mapping2", MAPPING_SOURCE2)
.putAlias(newAliasMetaDataBuilder("alias1"))
.putAlias(newAliasMetaDataBuilder("alias2")))
.put(IndexMetaData.builder("test11")
.settings(settings(Version.CURRENT)
.put("setting1", "value1")
.put("setting2", "value2"))
.numberOfShards(1)
.numberOfReplicas(2)
.putMapping("mapping1", MAPPING_SOURCE1)
.putMapping("mapping2", MAPPING_SOURCE2)
.putAlias(newAliasMetaDataBuilder("alias1").filter(ALIAS_FILTER1))
.putAlias(newAliasMetaDataBuilder("alias2").writeIndex(randomBoolean() ? null : randomBoolean()))
.putAlias(newAliasMetaDataBuilder("alias4").filter(ALIAS_FILTER2)))
.put(IndexTemplateMetaData.builder("foo") .put(IndexTemplateMetaData.builder("foo")
.patterns(Collections.singletonList("bar")) .patterns(Collections.singletonList("bar"))
.order(1) .order(1)
@ -162,154 +98,12 @@ public class ToAndFromJsonMetaDataTests extends ESTestCase {
.put(new DataStream("data-stream2", "@timestamp2", Collections.emptyList())) .put(new DataStream("data-stream2", "@timestamp2", Collections.emptyList()))
.build(); .build();
String metaDataSource = MetaData.Builder.toXContent(metaData); XContentBuilder builder = JsonXContent.contentBuilder();
builder.startObject();
MetaData.FORMAT.toXContent(builder, metaData);
builder.endObject();
MetaData parsedMetaData = MetaData.Builder.fromXContent(createParser(JsonXContent.jsonXContent, metaDataSource)); MetaData parsedMetaData = MetaData.Builder.fromXContent(createParser(builder));
IndexMetaData indexMetaData = parsedMetaData.index("test1");
assertThat(indexMetaData.primaryTerm(0), equalTo(1L));
assertThat(indexMetaData.getNumberOfShards(), equalTo(1));
assertThat(indexMetaData.getNumberOfReplicas(), equalTo(2));
assertThat(indexMetaData.getCreationDate(), equalTo(-1L));
assertThat(indexMetaData.getSettings().size(), equalTo(3));
assertThat(indexMetaData.getMappings().size(), equalTo(0));
indexMetaData = parsedMetaData.index("test2");
assertThat(indexMetaData.getNumberOfShards(), equalTo(2));
assertThat(indexMetaData.getNumberOfReplicas(), equalTo(3));
assertThat(indexMetaData.primaryTerm(0), equalTo(2L));
assertThat(indexMetaData.primaryTerm(1), equalTo(2L));
assertThat(indexMetaData.getCreationDate(), equalTo(-1L));
assertThat(indexMetaData.getSettings().size(), equalTo(5));
assertThat(indexMetaData.getSettings().get("setting1"), equalTo("value1"));
assertThat(indexMetaData.getSettings().get("setting2"), equalTo("value2"));
assertThat(indexMetaData.getMappings().size(), equalTo(0));
indexMetaData = parsedMetaData.index("test3");
assertThat(indexMetaData.getNumberOfShards(), equalTo(1));
assertThat(indexMetaData.getNumberOfReplicas(), equalTo(2));
assertThat(indexMetaData.getCreationDate(), equalTo(-1L));
assertThat(indexMetaData.getSettings().size(), equalTo(3));
assertThat(indexMetaData.getMappings().size(), equalTo(1));
assertThat(indexMetaData.getMappings().get("mapping1").source().string(), equalTo(MAPPING_SOURCE1));
indexMetaData = parsedMetaData.index("test4");
assertThat(indexMetaData.getCreationDate(), equalTo(2L));
assertThat(indexMetaData.getNumberOfShards(), equalTo(1));
assertThat(indexMetaData.getNumberOfReplicas(), equalTo(2));
assertThat(indexMetaData.getSettings().size(), equalTo(4));
assertThat(indexMetaData.getMappings().size(), equalTo(0));
indexMetaData = parsedMetaData.index("test5");
assertThat(indexMetaData.getNumberOfShards(), equalTo(1));
assertThat(indexMetaData.getNumberOfReplicas(), equalTo(2));
assertThat(indexMetaData.getCreationDate(), equalTo(-1L));
assertThat(indexMetaData.getSettings().size(), equalTo(5));
assertThat(indexMetaData.getSettings().get("setting1"), equalTo("value1"));
assertThat(indexMetaData.getSettings().get("setting2"), equalTo("value2"));
assertThat(indexMetaData.getMappings().size(), equalTo(2));
assertThat(indexMetaData.getMappings().get("mapping1").source().string(), equalTo(MAPPING_SOURCE1));
assertThat(indexMetaData.getMappings().get("mapping2").source().string(), equalTo(MAPPING_SOURCE2));
indexMetaData = parsedMetaData.index("test6");
assertThat(indexMetaData.getNumberOfShards(), equalTo(1));
assertThat(indexMetaData.getNumberOfReplicas(), equalTo(2));
assertThat(indexMetaData.getCreationDate(), equalTo(2L));
assertThat(indexMetaData.getSettings().size(), equalTo(6));
assertThat(indexMetaData.getSettings().get("setting1"), equalTo("value1"));
assertThat(indexMetaData.getSettings().get("setting2"), equalTo("value2"));
assertThat(indexMetaData.getMappings().size(), equalTo(0));
indexMetaData = parsedMetaData.index("test7");
assertThat(indexMetaData.getNumberOfShards(), equalTo(1));
assertThat(indexMetaData.getNumberOfReplicas(), equalTo(2));
assertThat(indexMetaData.getCreationDate(), equalTo(2L));
assertThat(indexMetaData.getSettings().size(), equalTo(4));
assertThat(indexMetaData.getMappings().size(), equalTo(2));
assertThat(indexMetaData.getMappings().get("mapping1").source().string(), equalTo(MAPPING_SOURCE1));
assertThat(indexMetaData.getMappings().get("mapping2").source().string(), equalTo(MAPPING_SOURCE2));
indexMetaData = parsedMetaData.index("test8");
assertThat(indexMetaData.getNumberOfShards(), equalTo(1));
assertThat(indexMetaData.getNumberOfReplicas(), equalTo(2));
assertThat(indexMetaData.getCreationDate(), equalTo(-1L));
assertThat(indexMetaData.getSettings().size(), equalTo(5));
assertThat(indexMetaData.getSettings().get("setting1"), equalTo("value1"));
assertThat(indexMetaData.getSettings().get("setting2"), equalTo("value2"));
assertThat(indexMetaData.getMappings().size(), equalTo(2));
assertThat(indexMetaData.getMappings().get("mapping1").source().string(), equalTo(MAPPING_SOURCE1));
assertThat(indexMetaData.getMappings().get("mapping2").source().string(), equalTo(MAPPING_SOURCE2));
assertThat(indexMetaData.getAliases().size(), equalTo(2));
assertThat(indexMetaData.getAliases().get("alias1").alias(), equalTo("alias1"));
assertThat(indexMetaData.getAliases().get("alias2").alias(), equalTo("alias2"));
indexMetaData = parsedMetaData.index("test9");
assertThat(indexMetaData.getNumberOfShards(), equalTo(1));
assertThat(indexMetaData.getNumberOfReplicas(), equalTo(2));
assertThat(indexMetaData.getCreationDate(), equalTo(2L));
assertThat(indexMetaData.getSettings().size(), equalTo(6));
assertThat(indexMetaData.getSettings().get("setting1"), equalTo("value1"));
assertThat(indexMetaData.getSettings().get("setting2"), equalTo("value2"));
assertThat(indexMetaData.getMappings().size(), equalTo(2));
assertThat(indexMetaData.getMappings().get("mapping1").source().string(), equalTo(MAPPING_SOURCE1));
assertThat(indexMetaData.getMappings().get("mapping2").source().string(), equalTo(MAPPING_SOURCE2));
assertThat(indexMetaData.getAliases().size(), equalTo(2));
assertThat(indexMetaData.getAliases().get("alias1").alias(), equalTo("alias1"));
assertThat(indexMetaData.getAliases().get("alias2").alias(), equalTo("alias2"));
indexMetaData = parsedMetaData.index("test10");
assertThat(indexMetaData.getNumberOfShards(), equalTo(1));
assertThat(indexMetaData.getNumberOfReplicas(), equalTo(2));
assertThat(indexMetaData.getCreationDate(), equalTo(-1L));
assertThat(indexMetaData.getSettings().size(), equalTo(5));
assertThat(indexMetaData.getSettings().get("setting1"), equalTo("value1"));
assertThat(indexMetaData.getSettings().get("setting2"), equalTo("value2"));
assertThat(indexMetaData.getMappings().size(), equalTo(2));
assertThat(indexMetaData.getMappings().get("mapping1").source().string(), equalTo(MAPPING_SOURCE1));
assertThat(indexMetaData.getMappings().get("mapping2").source().string(), equalTo(MAPPING_SOURCE2));
assertThat(indexMetaData.getAliases().size(), equalTo(2));
assertThat(indexMetaData.getAliases().get("alias1").alias(), equalTo("alias1"));
assertThat(indexMetaData.getAliases().get("alias2").alias(), equalTo("alias2"));
indexMetaData = parsedMetaData.index("test11");
assertThat(indexMetaData.getNumberOfShards(), equalTo(1));
assertThat(indexMetaData.getNumberOfReplicas(), equalTo(2));
assertThat(indexMetaData.getCreationDate(), equalTo(-1L));
assertThat(indexMetaData.getSettings().size(), equalTo(5));
assertThat(indexMetaData.getSettings().get("setting1"), equalTo("value1"));
assertThat(indexMetaData.getSettings().get("setting2"), equalTo("value2"));
assertThat(indexMetaData.getMappings().size(), equalTo(2));
assertThat(indexMetaData.getMappings().get("mapping1").source().string(), equalTo(MAPPING_SOURCE1));
assertThat(indexMetaData.getMappings().get("mapping2").source().string(), equalTo(MAPPING_SOURCE2));
assertThat(indexMetaData.getAliases().size(), equalTo(3));
assertThat(indexMetaData.getAliases().get("alias1").alias(), equalTo("alias1"));
assertThat(indexMetaData.getAliases().get("alias1").filter().string(), equalTo(ALIAS_FILTER1));
assertThat(indexMetaData.getAliases().get("alias2").alias(), equalTo("alias2"));
assertThat(indexMetaData.getAliases().get("alias2").filter(), nullValue());
assertThat(indexMetaData.getAliases().get("alias2").writeIndex(),
equalTo(metaData.index("test11").getAliases().get("alias2").writeIndex()));
assertThat(indexMetaData.getAliases().get("alias4").alias(), equalTo("alias4"));
assertThat(indexMetaData.getAliases().get("alias4").filter().string(), equalTo(ALIAS_FILTER2));
indexMetaData = parsedMetaData.index("test12");
assertThat(indexMetaData.getNumberOfShards(), equalTo(1));
assertThat(indexMetaData.getNumberOfReplicas(), equalTo(2));
assertThat(indexMetaData.getCreationDate(), equalTo(2L));
assertThat(indexMetaData.getSettings().size(), equalTo(6));
assertThat(indexMetaData.getSettings().get("setting1"), equalTo("value1"));
assertThat(indexMetaData.getSettings().get("setting2"), equalTo("value2"));
assertThat(indexMetaData.getMappings().size(), equalTo(2));
assertThat(indexMetaData.getMappings().get("mapping1").source().string(), equalTo(MAPPING_SOURCE1));
assertThat(indexMetaData.getMappings().get("mapping2").source().string(), equalTo(MAPPING_SOURCE2));
assertThat(indexMetaData.getAliases().size(), equalTo(3));
assertThat(indexMetaData.getAliases().get("alias1").alias(), equalTo("alias1"));
assertThat(indexMetaData.getAliases().get("alias1").filter().string(), equalTo(ALIAS_FILTER1));
assertThat(indexMetaData.getAliases().get("alias3").alias(), equalTo("alias3"));
assertThat(indexMetaData.getAliases().get("alias3").filter(), nullValue());
assertThat(indexMetaData.getAliases().get("alias3").writeIndex(),
equalTo(metaData.index("test12").getAliases().get("alias3").writeIndex()));
assertThat(indexMetaData.getAliases().get("alias4").alias(), equalTo("alias4"));
assertThat(indexMetaData.getAliases().get("alias4").filter().string(), equalTo(ALIAS_FILTER2));
// templates // templates
assertThat(parsedMetaData.templates().get("foo").name(), is("foo")); assertThat(parsedMetaData.templates().get("foo").name(), is("foo"));
@ -363,4 +157,482 @@ public class ToAndFromJsonMetaDataTests extends ESTestCase {
private static final String MAPPING_SOURCE2 = "{\"mapping2\":{\"text2\":{\"type\":\"string\"}}}"; private static final String MAPPING_SOURCE2 = "{\"mapping2\":{\"text2\":{\"type\":\"string\"}}}";
private static final String ALIAS_FILTER1 = "{\"field1\":\"value1\"}"; private static final String ALIAS_FILTER1 = "{\"field1\":\"value1\"}";
private static final String ALIAS_FILTER2 = "{\"field2\":\"value2\"}"; private static final String ALIAS_FILTER2 = "{\"field2\":\"value2\"}";
public void testToXContentGateway_FlatSettingTrue_ReduceMappingFalse() throws IOException {
Map<String, String> mapParams = new HashMap<String,String>(){{
put(MetaData.CONTEXT_MODE_PARAM, CONTEXT_MODE_GATEWAY);
put("flat_settings", "true");
put("reduce_mappings", "false");
}};
MetaData metaData = buildMetaData();
XContentBuilder builder = JsonXContent.contentBuilder().prettyPrint();
builder.startObject();
metaData.toXContent(builder, new ToXContent.MapParams(mapParams));
builder.endObject();
assertEquals("{\n" +
" \"meta-data\" : {\n" +
" \"version\" : 0,\n" +
" \"cluster_uuid\" : \"clusterUUID\",\n" +
" \"cluster_uuid_committed\" : false,\n" +
" \"cluster_coordination\" : {\n" +
" \"term\" : 1,\n" +
" \"last_committed_config\" : [\n" +
" \"commitedConfigurationNodeId\"\n" +
" ],\n" +
" \"last_accepted_config\" : [\n" +
" \"acceptedConfigurationNodeId\"\n" +
" ],\n" +
" \"voting_config_exclusions\" : [\n" +
" {\n" +
" \"node_id\" : \"exlucdedNodeId\",\n" +
" \"node_name\" : \"excludedNodeName\"\n" +
" }\n" +
" ]\n" +
" },\n" +
" \"settings\" : {\n" +
" \"index.version.created\" : \"" + Version.CURRENT.id + "\"\n" +
" },\n" +
" \"templates\" : {\n" +
" \"template\" : {\n" +
" \"order\" : 0,\n" +
" \"index_patterns\" : [\n" +
" \"pattern1\",\n" +
" \"pattern2\"\n" +
" ],\n" +
" \"settings\" : {\n" +
" \"index.version.created\" : \"" + Version.CURRENT.id + "\"\n" +
" },\n" +
" \"mappings\" : [\n" +
" {\n" +
" \"key1\" : { }\n" +
" }\n" +
" ],\n" +
" \"aliases\" : { }\n" +
" }\n" +
" },\n" +
" \"index-graveyard\" : {\n" +
" \"tombstones\" : [ ]\n" +
" }\n" +
" }\n" +
"}", Strings.toString(builder));
}
public void testToXContentAPI_SameTypeName() throws IOException {
Map<String, String> mapParams = new HashMap<String,String>(){{
put(MetaData.CONTEXT_MODE_PARAM, CONTEXT_MODE_API);
}};
MetaData metaData = MetaData.builder()
.clusterUUID("clusterUUID")
.coordinationMetaData(CoordinationMetaData.builder()
.build())
.put(IndexMetaData.builder("index")
.state(IndexMetaData.State.OPEN)
.settings(Settings.builder()
.put(SETTING_VERSION_CREATED, Version.CURRENT.id))
.putMapping(new MappingMetaData("type",
// the type name is the root value,
// the original logic in ClusterState.toXContent will reduce
new HashMap<String,Object>(){{
put("type", new HashMap<String, Object>(){{
put("key", "value");
}});
}}))
.numberOfShards(1)
.primaryTerm(0, 1L)
.numberOfReplicas(2))
.build();
XContentBuilder builder = JsonXContent.contentBuilder().prettyPrint();
builder.startObject();
metaData.toXContent(builder, new ToXContent.MapParams(mapParams));
builder.endObject();
assertEquals("{\n" +
" \"metadata\" : {\n" +
" \"cluster_uuid\" : \"clusterUUID\",\n" +
" \"cluster_uuid_committed\" : false,\n" +
" \"cluster_coordination\" : {\n" +
" \"term\" : 0,\n" +
" \"last_committed_config\" : [ ],\n" +
" \"last_accepted_config\" : [ ],\n" +
" \"voting_config_exclusions\" : [ ]\n" +
" },\n" +
" \"templates\" : { },\n" +
" \"indices\" : {\n" +
" \"index\" : {\n" +
" \"version\" : 2,\n" +
" \"mapping_version\" : 1,\n" +
" \"settings_version\" : 1,\n" +
" \"aliases_version\" : 1,\n" +
" \"routing_num_shards\" : 1,\n" +
" \"state\" : \"open\",\n" +
" \"settings\" : {\n" +
" \"index\" : {\n" +
" \"number_of_shards\" : \"1\",\n" +
" \"number_of_replicas\" : \"2\",\n" +
" \"version\" : {\n" +
" \"created\" : \"" + Version.CURRENT.id + "\"\n" +
" }\n" +
" }\n" +
" },\n" +
" \"mappings\" : {\n" +
" \"type\" : {\n" +
" \"key\" : \"value\"\n" +
" }\n" +
" },\n" +
" \"aliases\" : [ ],\n" +
" \"primary_terms\" : {\n" +
" \"0\" : 1\n" +
" },\n" +
" \"in_sync_allocations\" : {\n" +
" \"0\" : [ ]\n" +
" },\n" +
" \"rollover_info\" : { }\n" +
" }\n" +
" },\n" +
" \"index-graveyard\" : {\n" +
" \"tombstones\" : [ ]\n" +
" }\n" +
" }\n" +
"}", Strings.toString(builder));
}
public void testToXContentGateway_FlatSettingFalse_ReduceMappingTrue() throws IOException {
Map<String, String> mapParams = new HashMap<String,String>(){{
put(MetaData.CONTEXT_MODE_PARAM, CONTEXT_MODE_GATEWAY);
put("flat_settings", "false");
put("reduce_mappings", "true");
}};
MetaData metaData = buildMetaData();
XContentBuilder builder = JsonXContent.contentBuilder().prettyPrint();
builder.startObject();
metaData.toXContent(builder, new ToXContent.MapParams(mapParams));
builder.endObject();
assertEquals("{\n" +
" \"meta-data\" : {\n" +
" \"version\" : 0,\n" +
" \"cluster_uuid\" : \"clusterUUID\",\n" +
" \"cluster_uuid_committed\" : false,\n" +
" \"cluster_coordination\" : {\n" +
" \"term\" : 1,\n" +
" \"last_committed_config\" : [\n" +
" \"commitedConfigurationNodeId\"\n" +
" ],\n" +
" \"last_accepted_config\" : [\n" +
" \"acceptedConfigurationNodeId\"\n" +
" ],\n" +
" \"voting_config_exclusions\" : [\n" +
" {\n" +
" \"node_id\" : \"exlucdedNodeId\",\n" +
" \"node_name\" : \"excludedNodeName\"\n" +
" }\n" +
" ]\n" +
" },\n" +
" \"settings\" : {\n" +
" \"index.version.created\" : \"" + Version.CURRENT.id + "\"\n" +
" },\n" +
" \"templates\" : {\n" +
" \"template\" : {\n" +
" \"order\" : 0,\n" +
" \"index_patterns\" : [\n" +
" \"pattern1\",\n" +
" \"pattern2\"\n" +
" ],\n" +
" \"settings\" : {\n" +
" \"index\" : {\n" +
" \"version\" : {\n" +
" \"created\" : \"" + Version.CURRENT.id + "\"\n" +
" }\n" +
" }\n" +
" },\n" +
" \"mappings\" : {\n" +
" \"type\" : {\n" +
" \"key1\" : { }\n" +
" }\n" +
" },\n" +
" \"aliases\" : { }\n" +
" }\n" +
" },\n" +
" \"index-graveyard\" : {\n" +
" \"tombstones\" : [ ]\n" +
" }\n" +
" }\n" +
"}", Strings.toString(builder));
}
public void testToXContentAPI_FlatSettingTrue_ReduceMappingFalse() throws IOException {
Map<String, String> mapParams = new HashMap<String,String>(){{
put(MetaData.CONTEXT_MODE_PARAM, CONTEXT_MODE_API);
put("flat_settings", "true");
put("reduce_mappings", "false");
}};
final MetaData metaData = buildMetaData();
XContentBuilder builder = JsonXContent.contentBuilder().prettyPrint();
builder.startObject();
metaData.toXContent(builder, new ToXContent.MapParams(mapParams));
builder.endObject();
assertEquals("{\n" +
" \"metadata\" : {\n" +
" \"cluster_uuid\" : \"clusterUUID\",\n" +
" \"cluster_uuid_committed\" : false,\n" +
" \"cluster_coordination\" : {\n" +
" \"term\" : 1,\n" +
" \"last_committed_config\" : [\n" +
" \"commitedConfigurationNodeId\"\n" +
" ],\n" +
" \"last_accepted_config\" : [\n" +
" \"acceptedConfigurationNodeId\"\n" +
" ],\n" +
" \"voting_config_exclusions\" : [\n" +
" {\n" +
" \"node_id\" : \"exlucdedNodeId\",\n" +
" \"node_name\" : \"excludedNodeName\"\n" +
" }\n" +
" ]\n" +
" },\n" +
" \"templates\" : {\n" +
" \"template\" : {\n" +
" \"order\" : 0,\n" +
" \"index_patterns\" : [\n" +
" \"pattern1\",\n" +
" \"pattern2\"\n" +
" ],\n" +
" \"settings\" : {\n" +
" \"index.version.created\" : \"" + Version.CURRENT.id + "\"\n" +
" },\n" +
" \"mappings\" : {\n" +
" \"type\" : {\n" +
" \"key1\" : { }\n" +
" }\n" +
" },\n" +
" \"aliases\" : { }\n" +
" }\n" +
" },\n" +
" \"indices\" : {\n" +
" \"index\" : {\n" +
" \"version\" : 2,\n" +
" \"mapping_version\" : 1,\n" +
" \"settings_version\" : 1,\n" +
" \"aliases_version\" : 1,\n" +
" \"routing_num_shards\" : 1,\n" +
" \"state\" : \"open\",\n" +
" \"settings\" : {\n" +
" \"index.number_of_replicas\" : \"2\",\n" +
" \"index.number_of_shards\" : \"1\",\n" +
" \"index.version.created\" : \"" + Version.CURRENT.id + "\"\n" +
" },\n" +
" \"mappings\" : {\n" +
" \"type\" : {\n" +
" \"type1\" : {\n" +
" \"key\" : \"value\"\n" +
" }\n" +
" }\n" +
" },\n" +
" \"aliases\" : [\n" +
" \"alias\"\n" +
" ],\n" +
" \"primary_terms\" : {\n" +
" \"0\" : 1\n" +
" },\n" +
" \"in_sync_allocations\" : {\n" +
" \"0\" : [\n" +
" \"allocationId\"\n" +
" ]\n" +
" },\n" +
" \"rollover_info\" : {\n" +
" \"rolloveAlias\" : {\n" +
" \"met_conditions\" : { },\n" +
" \"time\" : 1\n" +
" }\n" +
" }\n" +
" }\n" +
" },\n" +
" \"index-graveyard\" : {\n" +
" \"tombstones\" : [ ]\n" +
" }\n" +
" }\n" +
"}", Strings.toString(builder));
}
public void testToXContentAPI_FlatSettingFalse_ReduceMappingTrue() throws IOException {
Map<String, String> mapParams = new HashMap<String,String>(){{
put(MetaData.CONTEXT_MODE_PARAM, CONTEXT_MODE_API);
put("flat_settings", "false");
put("reduce_mappings", "true");
}};
final MetaData metaData = buildMetaData();
XContentBuilder builder = JsonXContent.contentBuilder().prettyPrint();
builder.startObject();
metaData.toXContent(builder, new ToXContent.MapParams(mapParams));
builder.endObject();
assertEquals("{\n" +
" \"metadata\" : {\n" +
" \"cluster_uuid\" : \"clusterUUID\",\n" +
" \"cluster_uuid_committed\" : false,\n" +
" \"cluster_coordination\" : {\n" +
" \"term\" : 1,\n" +
" \"last_committed_config\" : [\n" +
" \"commitedConfigurationNodeId\"\n" +
" ],\n" +
" \"last_accepted_config\" : [\n" +
" \"acceptedConfigurationNodeId\"\n" +
" ],\n" +
" \"voting_config_exclusions\" : [\n" +
" {\n" +
" \"node_id\" : \"exlucdedNodeId\",\n" +
" \"node_name\" : \"excludedNodeName\"\n" +
" }\n" +
" ]\n" +
" },\n" +
" \"templates\" : {\n" +
" \"template\" : {\n" +
" \"order\" : 0,\n" +
" \"index_patterns\" : [\n" +
" \"pattern1\",\n" +
" \"pattern2\"\n" +
" ],\n" +
" \"settings\" : {\n" +
" \"index\" : {\n" +
" \"version\" : {\n" +
" \"created\" : \"" + Version.CURRENT.id + "\"\n" +
" }\n" +
" }\n" +
" },\n" +
" \"mappings\" : {\n" +
" \"type\" : {\n" +
" \"key1\" : { }\n" +
" }\n" +
" },\n" +
" \"aliases\" : { }\n" +
" }\n" +
" },\n" +
" \"indices\" : {\n" +
" \"index\" : {\n" +
" \"version\" : 2,\n" +
" \"mapping_version\" : 1,\n" +
" \"settings_version\" : 1,\n" +
" \"aliases_version\" : 1,\n" +
" \"routing_num_shards\" : 1,\n" +
" \"state\" : \"open\",\n" +
" \"settings\" : {\n" +
" \"index\" : {\n" +
" \"number_of_shards\" : \"1\",\n" +
" \"number_of_replicas\" : \"2\",\n" +
" \"version\" : {\n" +
" \"created\" : \"" + Version.CURRENT.id + "\"\n" +
" }\n" +
" }\n" +
" },\n" +
" \"mappings\" : {\n" +
" \"type\" : {\n" +
" \"type1\" : {\n" +
" \"key\" : \"value\"\n" +
" }\n" +
" }\n" +
" },\n" +
" \"aliases\" : [\n" +
" \"alias\"\n" +
" ],\n" +
" \"primary_terms\" : {\n" +
" \"0\" : 1\n" +
" },\n" +
" \"in_sync_allocations\" : {\n" +
" \"0\" : [\n" +
" \"allocationId\"\n" +
" ]\n" +
" },\n" +
" \"rollover_info\" : {\n" +
" \"rolloveAlias\" : {\n" +
" \"met_conditions\" : { },\n" +
" \"time\" : 1\n" +
" }\n" +
" }\n" +
" }\n" +
" },\n" +
" \"index-graveyard\" : {\n" +
" \"tombstones\" : [ ]\n" +
" }\n" +
" }\n" +
"}", Strings.toString(builder));
}
private MetaData buildMetaData() throws IOException {
return MetaData.builder()
.clusterUUID("clusterUUID")
.coordinationMetaData(CoordinationMetaData.builder()
.term(1)
.lastCommittedConfiguration(new CoordinationMetaData.VotingConfiguration(new HashSet<String>(){{
add("commitedConfigurationNodeId");
}}))
.lastAcceptedConfiguration(new CoordinationMetaData.VotingConfiguration(new HashSet<String>(){{
add("acceptedConfigurationNodeId");
}}))
.addVotingConfigExclusion(new CoordinationMetaData.VotingConfigExclusion("exlucdedNodeId", "excludedNodeName"))
.build())
.persistentSettings(Settings.builder()
.put(SETTING_VERSION_CREATED, Version.CURRENT.id).build())
.transientSettings(Settings.builder()
.put(SETTING_VERSION_CREATED, Version.CURRENT.id).build())
.put(IndexMetaData.builder("index")
.state(IndexMetaData.State.OPEN)
.settings(Settings.builder()
.put(SETTING_VERSION_CREATED, Version.CURRENT.id))
.putMapping(new MappingMetaData("type",
new HashMap<String,Object>(){{
put("type1", new HashMap<String, Object>(){{
put("key", "value");
}});
}}))
.putAlias(AliasMetaData.builder("alias")
.indexRouting("indexRouting")
.build())
.numberOfShards(1)
.primaryTerm(0, 1L)
.putInSyncAllocationIds(0, new HashSet<String>(){{
add("allocationId");
}})
.numberOfReplicas(2)
.putRolloverInfo(new RolloverInfo("rolloveAlias", new ArrayList<>(), 1L)))
.put(IndexTemplateMetaData.builder("template")
.patterns(Arrays.asList("pattern1", "pattern2"))
.order(0)
.settings(Settings.builder().put(SETTING_VERSION_CREATED, Version.CURRENT.id))
.putMapping("type", "{ \"key1\": {} }")
.build())
.build();
}
public static class CustomMetaData extends TestCustomMetaData {
public static final String TYPE = "custom_md";
CustomMetaData(String data) {
super(data);
}
@Override
public String getWriteableName() {
return TYPE;
}
@Override
public Version getMinimalSupportedVersion() {
return Version.CURRENT;
}
@Override
public EnumSet<MetaData.XContentContext> context() {
return EnumSet.of(MetaData.XContentContext.GATEWAY, MetaData.XContentContext.SNAPSHOT);
}
}
} }

View File

@ -102,9 +102,10 @@ import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.common.xcontent.NamedXContentRegistry; import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.common.xcontent.smile.SmileXContent;
import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.core.internal.io.IOUtils;
import org.elasticsearch.discovery.Discovery; import org.elasticsearch.discovery.Discovery;
import org.elasticsearch.discovery.zen.ElectMasterService; import org.elasticsearch.discovery.zen.ElectMasterService;
@ -1104,20 +1105,85 @@ public abstract class ESIntegTestCase extends ESTestCase {
if (cluster() != null && cluster().size() > 0) { if (cluster() != null && cluster().size() > 0) {
final Client masterClient = client(); final Client masterClient = client();
MetaData metaData = masterClient.admin().cluster().prepareState().all().get().getState().metaData(); MetaData metaData = masterClient.admin().cluster().prepareState().all().get().getState().metaData();
final XContentBuilder builder = JsonXContent.contentBuilder(); final Map<String, String> serializationParams = new HashMap<>(2);
builder.startObject(); serializationParams.put("binary", "true");
metaData.toXContent(builder, ToXContent.EMPTY_PARAMS); serializationParams.put(MetaData.CONTEXT_MODE_PARAM, MetaData.CONTEXT_MODE_GATEWAY);
builder.endObject(); final ToXContent.Params serializationFormatParams = new ToXContent.MapParams(serializationParams);
final MetaData loadedMetaData; // when comparing XContent output, do not use binary format
try (XContentParser parser = createParser(ElasticsearchNodeCommand.namedXContentRegistry, final Map<String, String> compareParams = new HashMap<>(2);
JsonXContent.jsonXContent, BytesReference.bytes(builder))) { compareParams.put(MetaData.CONTEXT_MODE_PARAM, MetaData.CONTEXT_MODE_GATEWAY);
loadedMetaData = MetaData.fromXContent(parser); final ToXContent.Params compareFormatParams = new ToXContent.MapParams(compareParams);
{
MetaData metaDataWithoutIndices = MetaData.builder(metaData).removeAllIndices().build();
XContentBuilder builder = SmileXContent.contentBuilder();
builder.startObject();
metaDataWithoutIndices.toXContent(builder, serializationFormatParams);
builder.endObject();
final BytesReference originalBytes = BytesReference.bytes(builder);
XContentBuilder compareBuilder = SmileXContent.contentBuilder();
compareBuilder.startObject();
metaDataWithoutIndices.toXContent(compareBuilder, compareFormatParams);
compareBuilder.endObject();
final BytesReference compareOriginalBytes = BytesReference.bytes(compareBuilder);
final MetaData loadedMetaData;
try (XContentParser parser = createParser(ElasticsearchNodeCommand.namedXContentRegistry,
SmileXContent.smileXContent, originalBytes)) {
loadedMetaData = MetaData.fromXContent(parser);
}
builder = SmileXContent.contentBuilder();
builder.startObject();
loadedMetaData.toXContent(builder, compareFormatParams);
builder.endObject();
final BytesReference parsedBytes = BytesReference.bytes(builder);
assertNull(
"cluster state XContent serialization does not match, expected " +
XContentHelper.convertToMap(compareOriginalBytes, false, XContentType.SMILE) +
" but got " +
XContentHelper.convertToMap(parsedBytes, false, XContentType.SMILE),
differenceBetweenMapsIgnoringArrayOrder(
XContentHelper.convertToMap(compareOriginalBytes, false, XContentType.SMILE).v2(),
XContentHelper.convertToMap(parsedBytes, false, XContentType.SMILE).v2()));
} }
assertNull( for (IndexMetaData indexMetaData : metaData) {
"cluster state JSON serialization does not match", XContentBuilder builder = SmileXContent.contentBuilder();
differenceBetweenMapsIgnoringArrayOrder(convertToMap(metaData), convertToMap(loadedMetaData))); builder.startObject();
indexMetaData.toXContent(builder, serializationFormatParams);
builder.endObject();
final BytesReference originalBytes = BytesReference.bytes(builder);
XContentBuilder compareBuilder = SmileXContent.contentBuilder();
compareBuilder.startObject();
indexMetaData.toXContent(compareBuilder, compareFormatParams);
compareBuilder.endObject();
final BytesReference compareOriginalBytes = BytesReference.bytes(compareBuilder);
final IndexMetaData loadedIndexMetaData;
try (XContentParser parser = createParser(ElasticsearchNodeCommand.namedXContentRegistry,
SmileXContent.smileXContent, originalBytes)) {
loadedIndexMetaData = IndexMetaData.fromXContent(parser);
}
builder = SmileXContent.contentBuilder();
builder.startObject();
loadedIndexMetaData.toXContent(builder, compareFormatParams);
builder.endObject();
final BytesReference parsedBytes = BytesReference.bytes(builder);
assertNull(
"cluster state XContent serialization does not match, expected " +
XContentHelper.convertToMap(compareOriginalBytes, false, XContentType.SMILE) +
" but got " +
XContentHelper.convertToMap(parsedBytes, false, XContentType.SMILE),
differenceBetweenMapsIgnoringArrayOrder(
XContentHelper.convertToMap(compareOriginalBytes, false, XContentType.SMILE).v2(),
XContentHelper.convertToMap(parsedBytes, false, XContentType.SMILE).v2()));
}
} }
} }