diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/Metadata.java b/server/src/main/java/org/elasticsearch/cluster/metadata/Metadata.java index c0b5ce151fd13..7b6ef62aee209 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/Metadata.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/Metadata.java @@ -1020,7 +1020,11 @@ public static Builder builder() { } public static Builder builder(Metadata metadata) { - return new Builder(metadata); + return builder(metadata, false); + } + + public static Builder builder(Metadata metadata, boolean reuseIndicesLookup) { + return new Builder(metadata, reuseIndicesLookup); } public static class Builder { @@ -1037,6 +1041,7 @@ public static class Builder { private final ImmutableOpenMap.Builder indices; private final ImmutableOpenMap.Builder templates; private final ImmutableOpenMap.Builder customs; + private final SortedMap previousIndicesLookup; public Builder() { clusterUUID = UNKNOWN_CLUSTER_UUID; @@ -1044,9 +1049,10 @@ public Builder() { templates = ImmutableOpenMap.builder(); customs = ImmutableOpenMap.builder(); indexGraveyard(IndexGraveyard.builder().build()); // create new empty index graveyard to initialize + previousIndicesLookup = null; } - public Builder(Metadata metadata) { + Builder(Metadata metadata, boolean reuseIndicesLookup) { this.clusterUUID = metadata.clusterUUID; this.clusterUUIDCommitted = metadata.clusterUUIDCommitted; this.coordinationMetadata = metadata.coordinationMetadata; @@ -1057,6 +1063,11 @@ public Builder(Metadata metadata) { this.indices = ImmutableOpenMap.builder(metadata.indices); this.templates = ImmutableOpenMap.builder(metadata.templates); this.customs = ImmutableOpenMap.builder(metadata.customs); + if (reuseIndicesLookup) { + previousIndicesLookup = metadata.getIndicesLookup(); + } else { + previousIndicesLookup = null; + } } public Builder put(IndexMetadata.Builder indexMetadataBuilder) { @@ -1539,8 +1550,14 @@ public Metadata build(boolean builtIndicesLookupEagerly) { SortedMap indicesLookup; if (builtIndicesLookupEagerly) { - indicesLookup = Collections.unmodifiableSortedMap(buildIndicesLookup(dataStreamMetadata, indices)); + if (previousIndicesLookup != null) { + assert previousIndicesLookup.equals(buildIndicesLookup(dataStreamMetadata, indices)); + indicesLookup = previousIndicesLookup; + } else { + indicesLookup = Collections.unmodifiableSortedMap(buildIndicesLookup(dataStreamMetadata, indices)); + } } else { + assert previousIndicesLookup != null : "previous indicesLookup provided, but builtIndicesLookupEagerly=false"; indicesLookup = null; } diff --git a/server/src/main/java/org/elasticsearch/cluster/routing/allocation/IndexMetadataUpdater.java b/server/src/main/java/org/elasticsearch/cluster/routing/allocation/IndexMetadataUpdater.java index 48fc1ffbe6d3e..bc0da376de98f 100644 --- a/server/src/main/java/org/elasticsearch/cluster/routing/allocation/IndexMetadataUpdater.java +++ b/server/src/main/java/org/elasticsearch/cluster/routing/allocation/IndexMetadataUpdater.java @@ -114,7 +114,7 @@ public Metadata applyChanges(Metadata oldMetadata, RoutingTable newRoutingTable) if (indexMetadataBuilder != null) { if (metadataBuilder == null) { - metadataBuilder = Metadata.builder(oldMetadata); + metadataBuilder = Metadata.builder(oldMetadata, true); } metadataBuilder.put(indexMetadataBuilder); } diff --git a/server/src/test/java/org/elasticsearch/cluster/ClusterChangedEventTests.java b/server/src/test/java/org/elasticsearch/cluster/ClusterChangedEventTests.java index 234a3f8f96e07..f8505cfd26d4c 100644 --- a/server/src/test/java/org/elasticsearch/cluster/ClusterChangedEventTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/ClusterChangedEventTests.java @@ -354,7 +354,7 @@ private static ClusterState createNonInitializedState(final int numNodes, final private static ClusterState nextState(final ClusterState previousState, List customMetadataList) { final ClusterState.Builder builder = ClusterState.builder(previousState); builder.stateUUID(UUIDs.randomBase64UUID()); - Metadata.Builder metadataBuilder = new Metadata.Builder(previousState.metadata()); + Metadata.Builder metadataBuilder = Metadata.builder(previousState.metadata()); for (ObjectObjectCursor customMetadata : previousState.metadata().customs()) { if (customMetadata.value instanceof TestCustomMetadata) { metadataBuilder.removeCustom(customMetadata.key); diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/ThrottlingAllocationTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/ThrottlingAllocationTests.java index 81e387473f694..c60d00558cf17 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/ThrottlingAllocationTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/ThrottlingAllocationTests.java @@ -334,7 +334,7 @@ private ClusterState createRecoveryStateAndInitializeAllocations( final TestSnapshotsInfoService snapshotsInfoService ) { DiscoveryNode node1 = newNode("node1"); - Metadata.Builder metadataBuilder = new Metadata.Builder(metadata); + Metadata.Builder metadataBuilder = Metadata.builder(metadata); RoutingTable.Builder routingTableBuilder = RoutingTable.builder(); Snapshot snapshot = new Snapshot("repo", new SnapshotId("snap", "randomId")); Set snapshotIndices = new HashSet<>(); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedConfigAutoUpdaterTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedConfigAutoUpdaterTests.java index 998be8f6181e9..a986cc7ab9ea3 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedConfigAutoUpdaterTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedConfigAutoUpdaterTests.java @@ -179,7 +179,7 @@ public void testIsAbleToRun() { final ClusterState clusterState = csBuilder.build(); assertThat(updater.isAbleToRun(clusterState), is(true)); - metadata = new Metadata.Builder(clusterState.metadata()); + metadata = Metadata.builder(clusterState.metadata()); routingTable = new RoutingTable.Builder(clusterState.routingTable()); if (randomBoolean()) { routingTable.remove(MlConfigIndex.indexName()); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/task/AbstractJobPersistentTasksExecutorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/task/AbstractJobPersistentTasksExecutorTests.java index dfecf00676514..0030c1ddf41b1 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/task/AbstractJobPersistentTasksExecutorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/task/AbstractJobPersistentTasksExecutorTests.java @@ -55,7 +55,7 @@ public void testVerifyIndicesPrimaryShardsAreActive() { MlMetaIndex.indexName(), MlConfigIndex.indexName()).size()); - metadata = new Metadata.Builder(cs.metadata()); + metadata = Metadata.builder(cs.metadata()); routingTable = new RoutingTable.Builder(cs.routingTable()); String indexToRemove = randomFrom(resolver.concreteIndexNames(cs, IndicesOptions.lenientExpandOpen(), ".ml-anomalies-shared", diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/test/SecurityTestUtils.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/test/SecurityTestUtils.java index b45d05def523a..9717c93b5ddbe 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/test/SecurityTestUtils.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/test/SecurityTestUtils.java @@ -83,7 +83,7 @@ public static RoutingTable buildIndexRoutingTable(Index index) { */ public static Metadata addAliasToMetadata(Metadata metadata, String indexName) { AliasMetadata aliasMetadata = AliasMetadata.newAliasMetadataBuilder(SECURITY_MAIN_ALIAS).build(); - Metadata.Builder metadataBuilder = new Metadata.Builder(metadata); + Metadata.Builder metadataBuilder = Metadata.builder(metadata); IndexMetadata indexMetadata = metadata.index(indexName); metadataBuilder.put(IndexMetadata.builder(indexMetadata).putAlias(aliasMetadata)); return metadataBuilder.build(); diff --git a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/TransformPersistentTasksExecutorTests.java b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/TransformPersistentTasksExecutorTests.java index 1df06e3329440..bda9c8b497867 100644 --- a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/TransformPersistentTasksExecutorTests.java +++ b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/TransformPersistentTasksExecutorTests.java @@ -194,7 +194,7 @@ public void testVerifyIndicesPrimaryShardsAreActive() { TransformPersistentTasksExecutor.verifyIndicesPrimaryShardsAreActive(cs, TestIndexNameExpressionResolver.newInstance()).size() ); - metadata = new Metadata.Builder(cs.metadata()); + metadata = Metadata.builder(cs.metadata()); routingTable = new RoutingTable.Builder(cs.routingTable()); String indexToRemove = TransformInternalIndexConstants.LATEST_INDEX_NAME; if (randomBoolean()) {