From aee388ec465db65549a46f7cf0bebe7180a87e36 Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Wed, 11 Dec 2013 10:47:56 +0100 Subject: [PATCH] introduce an internal metadata uuid --- .../cluster/metadata/MetaData.java | 30 +++++++++++++++++-- .../elasticsearch/gateway/GatewayService.java | 2 ++ .../SimpleRecoveryLocalGatewayTests.java | 5 ++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java b/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java index 719288ac4fb..c67007d911d 100644 --- a/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java +++ b/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java @@ -26,6 +26,7 @@ import com.carrotsearch.hppc.cursors.ObjectObjectCursor; import com.google.common.base.Predicate; import com.google.common.collect.*; import org.elasticsearch.ElasticSearchIllegalArgumentException; +import org.elasticsearch.Version; import org.elasticsearch.action.support.IgnoreIndices; import org.elasticsearch.cluster.block.ClusterBlock; import org.elasticsearch.cluster.block.ClusterBlockLevel; @@ -116,6 +117,7 @@ public class MetaData implements Iterable { public static final String GLOBAL_PERSISTENT_ONLY_PARAM = "global_persistent_only"; + private final String uuid; private final long version; private final Settings transientSettings; @@ -136,7 +138,8 @@ public class MetaData implements Iterable { private final ImmutableOpenMap aliasAndIndexToIndexMap; @SuppressWarnings("unchecked") - MetaData(long version, Settings transientSettings, Settings persistentSettings, ImmutableOpenMap indices, ImmutableOpenMap templates, ImmutableOpenMap customs) { + MetaData(String uuid, long version, Settings transientSettings, Settings persistentSettings, ImmutableOpenMap indices, ImmutableOpenMap templates, ImmutableOpenMap customs) { + this.uuid = uuid; this.version = version; this.transientSettings = transientSettings; this.persistentSettings = persistentSettings; @@ -229,6 +232,10 @@ public class MetaData implements Iterable { return this.version; } + public String uuid() { + return this.uuid; + } + /** * Returns the merges transient and persistent settings. */ @@ -964,6 +971,7 @@ public class MetaData implements Iterable { public static class Builder { + private String uuid; private long version; private Settings transientSettings = ImmutableSettings.Builder.EMPTY_SETTINGS; @@ -974,12 +982,14 @@ public class MetaData implements Iterable { private final ImmutableOpenMap.Builder customs; public Builder() { + uuid = "_na_"; indices = ImmutableOpenMap.builder(); templates = ImmutableOpenMap.builder(); customs = ImmutableOpenMap.builder(); } public Builder(MetaData metaData) { + this.uuid = metaData.uuid; this.transientSettings = metaData.transientSettings; this.persistentSettings = metaData.persistentSettings; this.version = metaData.version; @@ -1102,8 +1112,15 @@ public class MetaData implements Iterable { return this; } + public Builder generateUuidIfNeeded() { + if (uuid.equals("_na_")) { + uuid = Strings.randomBase64UUID(); + } + return this; + } + public MetaData build() { - return new MetaData(version, transientSettings, persistentSettings, indices.build(), templates.build(), customs.build()); + return new MetaData(uuid, version, transientSettings, persistentSettings, indices.build(), templates.build(), customs.build()); } public static String toXContent(MetaData metaData) throws IOException { @@ -1119,6 +1136,7 @@ public class MetaData implements Iterable { builder.startObject("meta-data"); builder.field("version", metaData.version()); + builder.field("uuid", metaData.uuid); if (!metaData.persistentSettings().getAsMap().isEmpty()) { builder.startObject("settings"); @@ -1210,6 +1228,8 @@ public class MetaData implements Iterable { } else if (token.isValue()) { if ("version".equals(currentFieldName)) { builder.version = parser.longValue(); + } else if ("uuid".equals(currentFieldName)) { + builder.uuid = parser.text(); } } } @@ -1219,6 +1239,9 @@ public class MetaData implements Iterable { public static MetaData readFrom(StreamInput in) throws IOException { Builder builder = new Builder(); builder.version = in.readLong(); + if (in.getVersion().after(Version.V_0_90_7)) { + builder.uuid = in.readString(); + } builder.transientSettings(readSettingsFromStream(in)); builder.persistentSettings(readSettingsFromStream(in)); int size = in.readVInt(); @@ -1240,6 +1263,9 @@ public class MetaData implements Iterable { public static void writeTo(MetaData metaData, StreamOutput out) throws IOException { out.writeLong(metaData.version); + if (out.getVersion().after(Version.V_0_90_7)) { + out.writeString(metaData.uuid); + } writeSettingsToStream(metaData.transientSettings(), out); writeSettingsToStream(metaData.persistentSettings(), out); out.writeVInt(metaData.indices.size()); diff --git a/src/main/java/org/elasticsearch/gateway/GatewayService.java b/src/main/java/org/elasticsearch/gateway/GatewayService.java index 1dd78249e7c..5fcb57b069b 100644 --- a/src/main/java/org/elasticsearch/gateway/GatewayService.java +++ b/src/main/java/org/elasticsearch/gateway/GatewayService.java @@ -247,6 +247,8 @@ public class GatewayService extends AbstractLifecycleComponent i .removeGlobalBlock(STATE_NOT_RECOVERED_BLOCK); MetaData.Builder metaDataBuilder = MetaData.builder(recoveredState.metaData()); + // automatically generate a UID for the metadata if we need to + metaDataBuilder.generateUuidIfNeeded(); if (recoveredState.metaData().settings().getAsBoolean(MetaData.SETTING_READ_ONLY, false) || currentState.metaData().settings().getAsBoolean(MetaData.SETTING_READ_ONLY, false)) { blocks.addGlobalBlock(MetaData.CLUSTER_READ_ONLY_BLOCK); diff --git a/src/test/java/org/elasticsearch/gateway/local/SimpleRecoveryLocalGatewayTests.java b/src/test/java/org/elasticsearch/gateway/local/SimpleRecoveryLocalGatewayTests.java index 48b6b5edeb8..6b4fcebf8a3 100644 --- a/src/test/java/org/elasticsearch/gateway/local/SimpleRecoveryLocalGatewayTests.java +++ b/src/test/java/org/elasticsearch/gateway/local/SimpleRecoveryLocalGatewayTests.java @@ -274,6 +274,9 @@ public class SimpleRecoveryLocalGatewayTests extends ElasticsearchIntegrationTes assertHitCount(client().prepareCount().setQuery(matchAllQuery()).execute().actionGet(), 2); } + String metaDataUuid = client().admin().cluster().prepareState().execute().get().getState().getMetaData().uuid(); + assertThat(metaDataUuid, not(equalTo("_na_"))); + logger.info("--> closing first node, and indexing more data to the second node"); cluster().fullRestart(new RestartCallback() { @@ -314,6 +317,8 @@ public class SimpleRecoveryLocalGatewayTests extends ElasticsearchIntegrationTes assertThat(clusterHealth.isTimedOut(), equalTo(false)); assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.GREEN)); + assertThat(client().admin().cluster().prepareState().execute().get().getState().getMetaData().uuid(), equalTo(metaDataUuid)); + for (int i = 0; i < 10; i++) { assertHitCount(client().prepareCount().setQuery(matchAllQuery()).execute().actionGet(), 3); }