From c414f3bf869c1c22cb11390ad2bb7942da33be93 Mon Sep 17 00:00:00 2001 From: Armin Braun Date: Wed, 21 Jul 2021 14:33:33 +0200 Subject: [PATCH] Fix RoutingTable Lookup by Index (#75530) (#75574) This is likely one source of bugs in at least snapshotting as it can lead to looking up the wrong index from an old shard id (if an index has been deleted and a new index is created in its place concurrently) --- .../cluster/routing/RoutingTable.java | 13 ++-- .../cluster/routing/RoutingTableTests.java | 2 + .../indices/SystemIndexManagerTests.java | 67 ++++++++----------- .../authz/store/NativeRolesStoreTests.java | 3 +- .../support/SecurityIndexManagerTests.java | 67 +++++++++---------- .../security/test/SecurityTestUtils.java | 4 +- 6 files changed, 69 insertions(+), 87 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/cluster/routing/RoutingTable.java b/server/src/main/java/org/elasticsearch/cluster/routing/RoutingTable.java index 4a7f474f3b932..1438b8387e184 100644 --- a/server/src/main/java/org/elasticsearch/cluster/routing/RoutingTable.java +++ b/server/src/main/java/org/elasticsearch/cluster/routing/RoutingTable.java @@ -93,8 +93,8 @@ public boolean hasIndex(String index) { } public boolean hasIndex(Index index) { - IndexRoutingTable indexRouting = index(index.getName()); - return indexRouting != null && indexRouting.getIndex().equals(index); + IndexRoutingTable indexRouting = index(index); + return indexRouting != null; } public IndexRoutingTable index(String index) { @@ -102,7 +102,8 @@ public IndexRoutingTable index(String index) { } public IndexRoutingTable index(Index index) { - return indicesRouting.get(index.getName()); + IndexRoutingTable indexRouting = index(index.getName()); + return indexRouting != null && indexRouting.getIndex().equals(index) ? indexRouting : null; } public ImmutableOpenMap indicesRouting() { @@ -134,8 +135,8 @@ public IndexShardRoutingTable shardRoutingTable(String index, int shardId) { * @throws ShardNotFoundException if provided shard id is unknown */ public IndexShardRoutingTable shardRoutingTable(ShardId shardId) { - IndexRoutingTable indexRouting = index(shardId.getIndexName()); - if (indexRouting == null || indexRouting.getIndex().equals(shardId.getIndex()) == false) { + IndexRoutingTable indexRouting = index(shardId.getIndex()); + if (indexRouting == null) { throw new IndexNotFoundException(shardId.getIndex()); } IndexShardRoutingTable shard = indexRouting.shard(shardId.id()); @@ -147,7 +148,7 @@ public IndexShardRoutingTable shardRoutingTable(ShardId shardId) { @Nullable public ShardRouting getByAllocationId(ShardId shardId, String allocationId) { - final IndexRoutingTable indexRoutingTable = index(shardId.getIndexName()); + final IndexRoutingTable indexRoutingTable = index(shardId.getIndex()); if (indexRoutingTable == null) { return null; } diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/RoutingTableTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/RoutingTableTests.java index fcc24120ebf4a..93c6deec36617 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/RoutingTableTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/RoutingTableTests.java @@ -18,6 +18,7 @@ import org.elasticsearch.cluster.node.DiscoveryNodes.Builder; import org.elasticsearch.cluster.routing.allocation.AllocationService; import org.elasticsearch.common.Randomness; +import org.elasticsearch.common.UUIDs; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexNotFoundException; @@ -127,6 +128,7 @@ public void testHasIndex() { public void testIndex() { assertThat(clusterState.routingTable().index(TEST_INDEX_1).getIndex().getName(), is(TEST_INDEX_1)); + assertThat(clusterState.routingTable().index(new Index(TEST_INDEX_1, UUIDs.randomBase64UUID())), is(nullValue())); assertThat(clusterState.routingTable().index("foobar"), is(nullValue())); } diff --git a/server/src/test/java/org/elasticsearch/indices/SystemIndexManagerTests.java b/server/src/test/java/org/elasticsearch/indices/SystemIndexManagerTests.java index b3af70dc92c98..85f8982714c4e 100644 --- a/server/src/test/java/org/elasticsearch/indices/SystemIndexManagerTests.java +++ b/server/src/test/java/org/elasticsearch/indices/SystemIndexManagerTests.java @@ -46,8 +46,6 @@ import java.io.IOException; import java.io.UncheckedIOException; import java.util.List; -import java.util.UUID; - import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.equalTo; @@ -178,10 +176,7 @@ public void testManagerSkipsIndicesWithRedStatus() { SystemIndices systemIndices = new SystemIndices(Map.of("MyIndex", FEATURE)); SystemIndexManager manager = new SystemIndexManager(systemIndices, client); - final ClusterState.Builder clusterStateBuilder = createClusterState(); - markShardsUnavailable(clusterStateBuilder); - - assertThat(manager.getUpgradeStatus(clusterStateBuilder.build(), DESCRIPTOR), equalTo(UpgradeStatus.UNHEALTHY)); + assertThat(manager.getUpgradeStatus(markShardsUnavailable(createClusterState()), DESCRIPTOR), equalTo(UpgradeStatus.UNHEALTHY)); } /** @@ -192,10 +187,7 @@ public void testManagerSkipsIndicesWithOutdatedFormat() { SystemIndices systemIndices = new SystemIndices(Map.of("MyIndex", FEATURE)); SystemIndexManager manager = new SystemIndexManager(systemIndices, client); - final ClusterState.Builder clusterStateBuilder = createClusterState(5); - markShardsAvailable(clusterStateBuilder); - - assertThat(manager.getUpgradeStatus(clusterStateBuilder.build(), DESCRIPTOR), equalTo(UpgradeStatus.NEEDS_UPGRADE)); + assertThat(manager.getUpgradeStatus(markShardsAvailable(createClusterState(5)), DESCRIPTOR), equalTo(UpgradeStatus.NEEDS_UPGRADE)); } /** @@ -205,10 +197,7 @@ public void testManagerSkipsIndicesWithUpToDateMappings() { SystemIndices systemIndices = new SystemIndices(Map.of("MyIndex", FEATURE)); SystemIndexManager manager = new SystemIndexManager(systemIndices, client); - final ClusterState.Builder clusterStateBuilder = createClusterState(); - markShardsAvailable(clusterStateBuilder); - - assertThat(manager.getUpgradeStatus(clusterStateBuilder.build(), DESCRIPTOR), equalTo(UpgradeStatus.UP_TO_DATE)); + assertThat(manager.getUpgradeStatus(markShardsAvailable(createClusterState()), DESCRIPTOR), equalTo(UpgradeStatus.UP_TO_DATE)); } /** @@ -218,10 +207,10 @@ public void testManagerProcessesIndicesWithOutdatedMappings() { SystemIndices systemIndices = new SystemIndices(Map.of("MyIndex", FEATURE)); SystemIndexManager manager = new SystemIndexManager(systemIndices, client); - final ClusterState.Builder clusterStateBuilder = createClusterState(Strings.toString(getMappings("1.0.0"))); - markShardsAvailable(clusterStateBuilder); - - assertThat(manager.getUpgradeStatus(clusterStateBuilder.build(), DESCRIPTOR), equalTo(UpgradeStatus.NEEDS_MAPPINGS_UPDATE)); + assertThat( + manager.getUpgradeStatus(markShardsAvailable(createClusterState(Strings.toString(getMappings("1.0.0")))), DESCRIPTOR), + equalTo(UpgradeStatus.NEEDS_MAPPINGS_UPDATE) + ); } /** @@ -231,10 +220,10 @@ public void testManagerProcessesIndicesWithNullVersionMetadata() { SystemIndices systemIndices = new SystemIndices(Map.of("MyIndex", FEATURE)); SystemIndexManager manager = new SystemIndexManager(systemIndices, client); - final ClusterState.Builder clusterStateBuilder = createClusterState(Strings.toString(getMappings(null))); - markShardsAvailable(clusterStateBuilder); - - assertThat(manager.getUpgradeStatus(clusterStateBuilder.build(), DESCRIPTOR), equalTo(UpgradeStatus.NEEDS_MAPPINGS_UPDATE)); + assertThat( + manager.getUpgradeStatus(markShardsAvailable(createClusterState(Strings.toString(getMappings(null)))), DESCRIPTOR), + equalTo(UpgradeStatus.NEEDS_MAPPINGS_UPDATE) + ); } /** @@ -244,10 +233,7 @@ public void testManagerSubmitsPutRequest() { SystemIndices systemIndices = new SystemIndices(Map.of("MyIndex", FEATURE)); SystemIndexManager manager = new SystemIndexManager(systemIndices, client); - final ClusterState.Builder clusterStateBuilder = createClusterState(Strings.toString(getMappings("1.0.0"))); - markShardsAvailable(clusterStateBuilder); - - manager.clusterChanged(event(clusterStateBuilder)); + manager.clusterChanged(event(markShardsAvailable(createClusterState(Strings.toString(getMappings("1.0.0")))))); verify(client, times(1)).execute(any(PutMappingAction.class), any(PutMappingRequest.class), any()); } @@ -259,10 +245,10 @@ public void testCanHandleIntegerMetaVersion() { SystemIndices systemIndices = new SystemIndices(Map.of("MyIndex", FEATURE)); SystemIndexManager manager = new SystemIndexManager(systemIndices, client); - final ClusterState.Builder clusterStateBuilder = createClusterState(Strings.toString(getMappings(3))); - markShardsAvailable(clusterStateBuilder); - - assertThat(manager.getUpgradeStatus(clusterStateBuilder.build(), DESCRIPTOR), equalTo(UpgradeStatus.NEEDS_MAPPINGS_UPDATE)); + assertThat( + manager.getUpgradeStatus(markShardsAvailable(createClusterState(Strings.toString(getMappings(3)))), DESCRIPTOR), + equalTo(UpgradeStatus.NEEDS_MAPPINGS_UPDATE) + ); } private static ClusterState.Builder createClusterState() { @@ -294,12 +280,16 @@ private static ClusterState.Builder createClusterState(String mappings, int form return ClusterState.builder(state()).metadata(metadataBuilder.build()); } - private void markShardsAvailable(ClusterState.Builder clusterStateBuilder) { - clusterStateBuilder.routingTable(buildIndexRoutingTable(DESCRIPTOR.getPrimaryIndex())); + private ClusterState markShardsAvailable(ClusterState.Builder clusterStateBuilder) { + final ClusterState cs = clusterStateBuilder.build(); + return ClusterState.builder(cs) + .routingTable(buildIndexRoutingTable(cs.metadata().index(DESCRIPTOR.getPrimaryIndex()).getIndex())) + .build(); } - private void markShardsUnavailable(ClusterState.Builder clusterStateBuilder) { - final RoutingTable routingTable = buildIndexRoutingTable(DESCRIPTOR.getPrimaryIndex()); + private ClusterState markShardsUnavailable(ClusterState.Builder clusterStateBuilder) { + final ClusterState cs = clusterStateBuilder.build(); + final RoutingTable routingTable = buildIndexRoutingTable(cs.metadata().index(DESCRIPTOR.getPrimaryIndex()).getIndex()); Index prevIndex = routingTable.index(DESCRIPTOR.getPrimaryIndex()).getIndex(); @@ -321,7 +311,7 @@ private void markShardsUnavailable(ClusterState.Builder clusterStateBuilder) { ) .build(); - clusterStateBuilder.routingTable(unavailableRoutingTable); + return ClusterState.builder(cs).routingTable(unavailableRoutingTable).build(); } private static ClusterState state() { @@ -367,8 +357,7 @@ private static IndexMetadata.Builder getIndexMetadata( return indexMetadata; } - private static RoutingTable buildIndexRoutingTable(String indexName) { - Index index = new Index(indexName, UUID.randomUUID().toString()); + private static RoutingTable buildIndexRoutingTable(Index index) { ShardRouting shardRouting = ShardRouting.newUnassigned( new ShardId(index, 0), true, @@ -382,8 +371,8 @@ private static RoutingTable buildIndexRoutingTable(String indexName) { return RoutingTable.builder().add(IndexRoutingTable.builder(index).addIndexShard(table).build()).build(); } - private ClusterChangedEvent event(ClusterState.Builder clusterStateBuilder) { - return new ClusterChangedEvent("test-event", clusterStateBuilder.build(), EMPTY_CLUSTER_STATE); + private ClusterChangedEvent event(ClusterState clusterState) { + return new ClusterChangedEvent("test-event", clusterState, EMPTY_CLUSTER_STATE); } private static Settings getSettings() { diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/store/NativeRolesStoreTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/store/NativeRolesStoreTests.java index 8513867afb899..796f0e7f145e2 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/store/NativeRolesStoreTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/store/NativeRolesStoreTests.java @@ -54,7 +54,6 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.List; -import java.util.UUID; import java.util.concurrent.atomic.AtomicBoolean; import static org.elasticsearch.xpack.core.security.index.RestrictedIndicesNames.SECURITY_MAIN_ALIAS; @@ -268,7 +267,7 @@ private ClusterState getClusterStateWithSecurityIndex() { metadata = SecurityTestUtils.addAliasToMetadata(metadata, securityIndexName); } - Index index = new Index(securityIndexName, UUID.randomUUID().toString()); + Index index = metadata.index(securityIndexName).getIndex(); ShardRouting shardRouting = ShardRouting.newUnassigned(new ShardId(index, 0), true, RecoverySource.ExistingStoreRecoverySource.INSTANCE, new UnassignedInfo(Reason.INDEX_CREATED, "")); IndexShardRoutingTable table = new IndexShardRoutingTable.Builder(new ShardId(index, 0)) diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/support/SecurityIndexManagerTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/support/SecurityIndexManagerTests.java index 0700e644b4ac9..f5d4739c81829 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/support/SecurityIndexManagerTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/support/SecurityIndexManagerTests.java @@ -57,7 +57,6 @@ import java.io.IOException; import java.io.UncheckedIOException; -import java.util.UUID; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; import java.util.function.BiConsumer; @@ -114,8 +113,7 @@ public void testIndexWithUpToDateMappingAndTemplate() { final ClusterState.Builder clusterStateBuilder = createClusterState(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7, RestrictedIndicesNames.SECURITY_MAIN_ALIAS); - markShardsAvailable(clusterStateBuilder); - manager.clusterChanged(event(clusterStateBuilder)); + manager.clusterChanged(event(markShardsAvailable(clusterStateBuilder))); assertThat(manager.indexExists(), Matchers.equalTo(true)); assertThat(manager.isAvailable(), Matchers.equalTo(true)); @@ -125,9 +123,10 @@ public void testIndexWithUpToDateMappingAndTemplate() { public void testIndexWithoutPrimaryShards() { assertInitialState(); - final ClusterState.Builder clusterStateBuilder = createClusterState(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7, - RestrictedIndicesNames.SECURITY_MAIN_ALIAS); - Index index = new Index(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7, UUID.randomUUID().toString()); + final ClusterState cs = createClusterState(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7, + RestrictedIndicesNames.SECURITY_MAIN_ALIAS).build(); + final ClusterState.Builder clusterStateBuilder = ClusterState.builder(cs); + Index index = cs.metadata().index(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7).getIndex(); ShardRouting shardRouting = ShardRouting.newUnassigned(new ShardId(index, 0), true, RecoverySource.ExistingStoreRecoverySource.INSTANCE, new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "")); String nodeId = ESTestCase.randomAlphaOfLength(8); @@ -138,13 +137,13 @@ public void testIndexWithoutPrimaryShards() { clusterStateBuilder.routingTable(RoutingTable.builder() .add(IndexRoutingTable.builder(index).addIndexShard(table).build()) .build()); - manager.clusterChanged(event(clusterStateBuilder)); + manager.clusterChanged(event(clusterStateBuilder.build())); assertIndexUpToDateButNotAvailable(); } - private ClusterChangedEvent event(ClusterState.Builder clusterStateBuilder) { - return new ClusterChangedEvent("test-event", clusterStateBuilder.build(), EMPTY_CLUSTER_STATE); + private ClusterChangedEvent event(ClusterState clusterState) { + return new ClusterChangedEvent("test-event", clusterState, EMPTY_CLUSTER_STATE); } public void testIndexHealthChangeListeners() { @@ -161,9 +160,8 @@ public void testIndexHealthChangeListeners() { // index doesn't exist and now exists final ClusterState.Builder clusterStateBuilder = createClusterState(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7, RestrictedIndicesNames.SECURITY_MAIN_ALIAS); - markShardsAvailable(clusterStateBuilder); - final ClusterState clusterState = clusterStateBuilder.build(); - manager.clusterChanged(event(ClusterState.builder(clusterState))); + final ClusterState clusterState = markShardsAvailable(clusterStateBuilder); + manager.clusterChanged(event(clusterState)); assertTrue(listenerCalled.get()); assertNull(previousState.get().indexHealth); @@ -226,7 +224,7 @@ public void testWriteBeforeStateNotRecovered() { prepareRunnableCalled.set(false); // state not recovered final ClusterBlocks.Builder blocks = ClusterBlocks.builder().addGlobalBlock(GatewayService.STATE_NOT_RECOVERED_BLOCK); - manager.clusterChanged(event(new ClusterState.Builder(CLUSTER_NAME).blocks(blocks))); + manager.clusterChanged(event(new ClusterState.Builder(CLUSTER_NAME).blocks(blocks).build())); manager.prepareIndexIfNeededThenExecute(prepareException::set, () -> prepareRunnableCalled.set(true)); assertThat(prepareException.get(), is(notNullValue())); assertThat(prepareException.get(), instanceOf(ElasticsearchStatusException.class)); @@ -238,8 +236,7 @@ public void testWriteBeforeStateNotRecovered() { // state recovered with index ClusterState.Builder clusterStateBuilder = createClusterState(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7, RestrictedIndicesNames.SECURITY_MAIN_ALIAS, SecurityIndexManager.INTERNAL_MAIN_INDEX_FORMAT); - markShardsAvailable(clusterStateBuilder); - manager.clusterChanged(event(clusterStateBuilder)); + manager.clusterChanged(event(markShardsAvailable(clusterStateBuilder))); manager.prepareIndexIfNeededThenExecute(prepareException::set, () -> prepareRunnableCalled.set(true)); assertThat(prepareException.get(), is(nullValue())); assertThat(prepareRunnableCalled.get(), is(true)); @@ -266,8 +263,7 @@ public void testCanUpdateIndexMappings() { IndexMetadata.State.OPEN, getMappings(previousVersion) ); - markShardsAvailable(clusterStateBuilder); - manager.clusterChanged(event(clusterStateBuilder)); + manager.clusterChanged(event(markShardsAvailable(clusterStateBuilder))); manager.prepareIndexIfNeededThenExecute(prepareException::set, () -> prepareRunnableCalled.set(true)); @@ -300,8 +296,7 @@ public void testCannotUpdateIndexMappingsWhenMinNodeVersionTooLow() { IndexMetadata.State.OPEN, getMappings(previousVersion) ); - markShardsAvailable(clusterStateBuilder); - manager.clusterChanged(event(clusterStateBuilder)); + manager.clusterChanged(event(markShardsAvailable(clusterStateBuilder))); manager.prepareIndexIfNeededThenExecute(prepareException::set, () -> prepareRunnableCalled.set(true)); assertThat(prepareRunnableCalled.get(), is(false)); @@ -317,21 +312,20 @@ public void testListenerNotCalledBeforeStateNotRecovered() { manager.addStateListener((prev, current) -> listenerCalled.set(true)); final ClusterBlocks.Builder blocks = ClusterBlocks.builder().addGlobalBlock(GatewayService.STATE_NOT_RECOVERED_BLOCK); // state not recovered - manager.clusterChanged(event(new ClusterState.Builder(CLUSTER_NAME).blocks(blocks))); + manager.clusterChanged(event(new ClusterState.Builder(CLUSTER_NAME).blocks(blocks).build())); assertThat(manager.isStateRecovered(), is(false)); assertThat(listenerCalled.get(), is(false)); // state recovered with index ClusterState.Builder clusterStateBuilder = createClusterState(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7, RestrictedIndicesNames.SECURITY_MAIN_ALIAS, SecurityIndexManager.INTERNAL_MAIN_INDEX_FORMAT); - markShardsAvailable(clusterStateBuilder); - manager.clusterChanged(event(clusterStateBuilder)); + manager.clusterChanged(event(markShardsAvailable(clusterStateBuilder))); assertThat(manager.isStateRecovered(), is(true)); assertThat(listenerCalled.get(), is(true)); } public void testIndexOutOfDateListeners() { final AtomicBoolean listenerCalled = new AtomicBoolean(false); - manager.clusterChanged(event(new ClusterState.Builder(CLUSTER_NAME))); + manager.clusterChanged(event(new ClusterState.Builder(CLUSTER_NAME).build())); AtomicBoolean upToDateChanged = new AtomicBoolean(); manager.addStateListener((prev, current) -> { listenerCalled.set(true); @@ -339,22 +333,21 @@ public void testIndexOutOfDateListeners() { }); assertTrue(manager.isIndexUpToDate()); - manager.clusterChanged(event(new ClusterState.Builder(CLUSTER_NAME))); + manager.clusterChanged(event(new ClusterState.Builder(CLUSTER_NAME).build())); assertFalse(listenerCalled.get()); assertTrue(manager.isIndexUpToDate()); // index doesn't exist and now exists with wrong format ClusterState.Builder clusterStateBuilder = createClusterState(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7, RestrictedIndicesNames.SECURITY_MAIN_ALIAS, SecurityIndexManager.INTERNAL_MAIN_INDEX_FORMAT - 1); - markShardsAvailable(clusterStateBuilder); - manager.clusterChanged(event(clusterStateBuilder)); + manager.clusterChanged(event(markShardsAvailable(clusterStateBuilder))); assertTrue(listenerCalled.get()); assertTrue(upToDateChanged.get()); assertFalse(manager.isIndexUpToDate()); listenerCalled.set(false); assertFalse(listenerCalled.get()); - manager.clusterChanged(event(new ClusterState.Builder(CLUSTER_NAME))); + manager.clusterChanged(event(new ClusterState.Builder(CLUSTER_NAME).build())); assertTrue(listenerCalled.get()); assertTrue(upToDateChanged.get()); assertTrue(manager.isIndexUpToDate()); @@ -363,8 +356,7 @@ public void testIndexOutOfDateListeners() { // index doesn't exist and now exists with correct format clusterStateBuilder = createClusterState(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7, RestrictedIndicesNames.SECURITY_MAIN_ALIAS, SecurityIndexManager.INTERNAL_MAIN_INDEX_FORMAT); - markShardsAvailable(clusterStateBuilder); - manager.clusterChanged(event(clusterStateBuilder)); + manager.clusterChanged(event(markShardsAvailable(clusterStateBuilder))); assertTrue(listenerCalled.get()); assertFalse(upToDateChanged.get()); assertTrue(manager.isIndexUpToDate()); @@ -374,23 +366,21 @@ public void testProcessClosedIndexState() { // Index initially exists final ClusterState.Builder indexAvailable = createClusterState(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7, RestrictedIndicesNames.SECURITY_MAIN_ALIAS, IndexMetadata.State.OPEN); - markShardsAvailable(indexAvailable); - - manager.clusterChanged(event(indexAvailable)); + manager.clusterChanged(event(markShardsAvailable(indexAvailable))); assertThat(manager.indexExists(), is(true)); assertThat(manager.isAvailable(), is(true)); // Now close it - final ClusterState.Builder indexClosed = createClusterState(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7, + ClusterState.Builder indexClosed = createClusterState(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7, RestrictedIndicesNames.SECURITY_MAIN_ALIAS, IndexMetadata.State.CLOSE); if (randomBoolean()) { // In old/mixed cluster versions closed indices have no routing table indexClosed.routingTable(RoutingTable.EMPTY_ROUTING_TABLE); } else { - markShardsAvailable(indexClosed); + indexClosed = ClusterState.builder(markShardsAvailable(indexClosed)); } - manager.clusterChanged(event(indexClosed)); + manager.clusterChanged(event(indexClosed.build())); assertThat(manager.indexExists(), is(true)); assertThat(manager.isAvailable(), is(false)); } @@ -431,8 +421,11 @@ private static ClusterState.Builder createClusterState(String indexName, String return ClusterState.builder(state()).metadata(metadataBuilder.build()); } - private void markShardsAvailable(ClusterState.Builder clusterStateBuilder) { - clusterStateBuilder.routingTable(SecurityTestUtils.buildIndexRoutingTable(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7)); + private ClusterState markShardsAvailable(ClusterState.Builder clusterStateBuilder) { + final ClusterState cs = clusterStateBuilder.build(); + return ClusterState.builder(cs).routingTable( + SecurityTestUtils.buildIndexRoutingTable( + cs.metadata().index(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7).getIndex())).build(); } private static ClusterState state() { 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 8b43df26a2fa2..b45d05def523a 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 @@ -29,7 +29,6 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.Locale; -import java.util.UUID; import static java.nio.file.StandardCopyOption.ATOMIC_MOVE; import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; @@ -67,8 +66,7 @@ public static String writeFile(Path folder, String name, String content) { return writeFile(folder, name, content.getBytes(StandardCharsets.UTF_8)); } - public static RoutingTable buildIndexRoutingTable(String indexName) { - Index index = new Index(indexName, UUID.randomUUID().toString()); + public static RoutingTable buildIndexRoutingTable(Index index) { ShardRouting shardRouting = ShardRouting.newUnassigned(new ShardId(index, 0), true, ExistingStoreRecoverySource.INSTANCE, new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "")); String nodeId = ESTestCase.randomAlphaOfLength(8);