From 5e44147d5e0b8af1242db545a47af61b64959e80 Mon Sep 17 00:00:00 2001 From: Armin Braun Date: Wed, 21 Jul 2021 16:33:42 +0200 Subject: [PATCH] Revert "Fix RoutingTable Lookup by Index (#75530)" This reverts commit 11698287c8721a24f56227cc9a7df45e136e32a8. --- .../cluster/routing/RoutingTable.java | 13 ++-- .../cluster/routing/RoutingTableTests.java | 2 - .../indices/SystemIndexManagerTests.java | 66 ++++++++++-------- .../authz/store/NativeRolesStoreTests.java | 3 +- .../support/SecurityIndexManagerTests.java | 67 ++++++++++--------- .../security/test/SecurityTestUtils.java | 4 +- 6 files changed, 86 insertions(+), 69 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 36acc0ee4e4f6..851fa8ef7951a 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); - return indexRouting != null; + IndexRoutingTable indexRouting = index(index.getName()); + return indexRouting != null && indexRouting.getIndex().equals(index); } public IndexRoutingTable index(String index) { @@ -102,8 +102,7 @@ public IndexRoutingTable index(String index) { } public IndexRoutingTable index(Index index) { - IndexRoutingTable indexRouting = index(index.getName()); - return indexRouting != null && indexRouting.getIndex().equals(index) ? indexRouting : null; + return indicesRouting.get(index.getName()); } public ImmutableOpenMap indicesRouting() { @@ -135,8 +134,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.getIndex()); - if (indexRouting == null) { + IndexRoutingTable indexRouting = index(shardId.getIndexName()); + if (indexRouting == null || indexRouting.getIndex().equals(shardId.getIndex()) == false) { throw new IndexNotFoundException(shardId.getIndex()); } IndexShardRoutingTable shard = indexRouting.shard(shardId.id()); @@ -148,7 +147,7 @@ public IndexShardRoutingTable shardRoutingTable(ShardId shardId) { @Nullable public ShardRouting getByAllocationId(ShardId shardId, String allocationId) { - final IndexRoutingTable indexRoutingTable = index(shardId.getIndex()); + final IndexRoutingTable indexRoutingTable = index(shardId.getIndexName()); 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 d908cd64f1aa7..4e8ff0fe3666f 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/RoutingTableTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/RoutingTableTests.java @@ -18,7 +18,6 @@ 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; @@ -128,7 +127,6 @@ 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 7cfa6fdffb81b..11fea74186de2 100644 --- a/server/src/test/java/org/elasticsearch/indices/SystemIndexManagerTests.java +++ b/server/src/test/java/org/elasticsearch/indices/SystemIndexManagerTests.java @@ -42,6 +42,7 @@ import java.io.UncheckedIOException; import java.util.List; import java.util.Map; +import java.util.UUID; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.contains; @@ -169,7 +170,10 @@ public void testManagerSkipsIndicesWithRedStatus() { SystemIndices systemIndices = new SystemIndices(Map.of("MyIndex", FEATURE)); SystemIndexManager manager = new SystemIndexManager(systemIndices, client); - assertThat(manager.getUpgradeStatus(markShardsUnavailable(createClusterState()), DESCRIPTOR), equalTo(UpgradeStatus.UNHEALTHY)); + final ClusterState.Builder clusterStateBuilder = createClusterState(); + markShardsUnavailable(clusterStateBuilder); + + assertThat(manager.getUpgradeStatus(clusterStateBuilder.build(), DESCRIPTOR), equalTo(UpgradeStatus.UNHEALTHY)); } /** @@ -180,7 +184,10 @@ public void testManagerSkipsIndicesWithOutdatedFormat() { SystemIndices systemIndices = new SystemIndices(Map.of("MyIndex", FEATURE)); SystemIndexManager manager = new SystemIndexManager(systemIndices, client); - assertThat(manager.getUpgradeStatus(markShardsAvailable(createClusterState(5)), DESCRIPTOR), equalTo(UpgradeStatus.NEEDS_UPGRADE)); + final ClusterState.Builder clusterStateBuilder = createClusterState(5); + markShardsAvailable(clusterStateBuilder); + + assertThat(manager.getUpgradeStatus(clusterStateBuilder.build(), DESCRIPTOR), equalTo(UpgradeStatus.NEEDS_UPGRADE)); } /** @@ -190,7 +197,10 @@ public void testManagerSkipsIndicesWithUpToDateMappings() { SystemIndices systemIndices = new SystemIndices(Map.of("MyIndex", FEATURE)); SystemIndexManager manager = new SystemIndexManager(systemIndices, client); - assertThat(manager.getUpgradeStatus(markShardsAvailable(createClusterState()), DESCRIPTOR), equalTo(UpgradeStatus.UP_TO_DATE)); + final ClusterState.Builder clusterStateBuilder = createClusterState(); + markShardsAvailable(clusterStateBuilder); + + assertThat(manager.getUpgradeStatus(clusterStateBuilder.build(), DESCRIPTOR), equalTo(UpgradeStatus.UP_TO_DATE)); } /** @@ -200,10 +210,10 @@ public void testManagerProcessesIndicesWithOutdatedMappings() { SystemIndices systemIndices = new SystemIndices(Map.of("MyIndex", FEATURE)); SystemIndexManager manager = new SystemIndexManager(systemIndices, client); - assertThat( - manager.getUpgradeStatus(markShardsAvailable(createClusterState(Strings.toString(getMappings("1.0.0")))), DESCRIPTOR), - equalTo(UpgradeStatus.NEEDS_MAPPINGS_UPDATE) - ); + final ClusterState.Builder clusterStateBuilder = createClusterState(Strings.toString(getMappings("1.0.0"))); + markShardsAvailable(clusterStateBuilder); + + assertThat(manager.getUpgradeStatus(clusterStateBuilder.build(), DESCRIPTOR), equalTo(UpgradeStatus.NEEDS_MAPPINGS_UPDATE)); } /** @@ -213,10 +223,10 @@ public void testManagerProcessesIndicesWithNullVersionMetadata() { SystemIndices systemIndices = new SystemIndices(Map.of("MyIndex", FEATURE)); SystemIndexManager manager = new SystemIndexManager(systemIndices, client); - assertThat( - manager.getUpgradeStatus(markShardsAvailable(createClusterState(Strings.toString(getMappings(null)))), DESCRIPTOR), - equalTo(UpgradeStatus.NEEDS_MAPPINGS_UPDATE) - ); + final ClusterState.Builder clusterStateBuilder = createClusterState(Strings.toString(getMappings(null))); + markShardsAvailable(clusterStateBuilder); + + assertThat(manager.getUpgradeStatus(clusterStateBuilder.build(), DESCRIPTOR), equalTo(UpgradeStatus.NEEDS_MAPPINGS_UPDATE)); } /** @@ -226,7 +236,10 @@ public void testManagerSubmitsPutRequest() { SystemIndices systemIndices = new SystemIndices(Map.of("MyIndex", FEATURE)); SystemIndexManager manager = new SystemIndexManager(systemIndices, client); - manager.clusterChanged(event(markShardsAvailable(createClusterState(Strings.toString(getMappings("1.0.0")))))); + final ClusterState.Builder clusterStateBuilder = createClusterState(Strings.toString(getMappings("1.0.0"))); + markShardsAvailable(clusterStateBuilder); + + manager.clusterChanged(event(clusterStateBuilder)); verify(client, times(1)).execute(any(PutMappingAction.class), any(PutMappingRequest.class), any()); } @@ -238,10 +251,10 @@ public void testCanHandleIntegerMetaVersion() { SystemIndices systemIndices = new SystemIndices(Map.of("MyIndex", FEATURE)); SystemIndexManager manager = new SystemIndexManager(systemIndices, client); - assertThat( - manager.getUpgradeStatus(markShardsAvailable(createClusterState(Strings.toString(getMappings(3)))), DESCRIPTOR), - equalTo(UpgradeStatus.NEEDS_MAPPINGS_UPDATE) - ); + final ClusterState.Builder clusterStateBuilder = createClusterState(Strings.toString(getMappings(3))); + markShardsAvailable(clusterStateBuilder); + + assertThat(manager.getUpgradeStatus(clusterStateBuilder.build(), DESCRIPTOR), equalTo(UpgradeStatus.NEEDS_MAPPINGS_UPDATE)); } private static ClusterState.Builder createClusterState() { @@ -273,16 +286,12 @@ private static ClusterState.Builder createClusterState(String mappings, int form return ClusterState.builder(state()).metadata(metadataBuilder.build()); } - 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 markShardsAvailable(ClusterState.Builder clusterStateBuilder) { + clusterStateBuilder.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()); + private void markShardsUnavailable(ClusterState.Builder clusterStateBuilder) { + final RoutingTable routingTable = buildIndexRoutingTable(DESCRIPTOR.getPrimaryIndex()); Index prevIndex = routingTable.index(DESCRIPTOR.getPrimaryIndex()).getIndex(); @@ -304,7 +313,7 @@ private ClusterState markShardsUnavailable(ClusterState.Builder clusterStateBuil ) .build(); - return ClusterState.builder(cs).routingTable(unavailableRoutingTable).build(); + clusterStateBuilder.routingTable(unavailableRoutingTable); } private static ClusterState state() { @@ -342,7 +351,8 @@ private static IndexMetadata.Builder getIndexMetadata( return indexMetadata; } - private static RoutingTable buildIndexRoutingTable(Index index) { + private static RoutingTable buildIndexRoutingTable(String indexName) { + Index index = new Index(indexName, UUID.randomUUID().toString()); ShardRouting shardRouting = ShardRouting.newUnassigned( new ShardId(index, 0), true, @@ -356,8 +366,8 @@ private static RoutingTable buildIndexRoutingTable(Index index) { return RoutingTable.builder().add(IndexRoutingTable.builder(index).addIndexShard(table).build()).build(); } - private ClusterChangedEvent event(ClusterState clusterState) { - return new ClusterChangedEvent("test-event", clusterState, EMPTY_CLUSTER_STATE); + private ClusterChangedEvent event(ClusterState.Builder clusterStateBuilder) { + return new ClusterChangedEvent("test-event", clusterStateBuilder.build(), 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 796f0e7f145e2..8513867afb899 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,6 +54,7 @@ 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; @@ -267,7 +268,7 @@ private ClusterState getClusterStateWithSecurityIndex() { metadata = SecurityTestUtils.addAliasToMetadata(metadata, securityIndexName); } - Index index = metadata.index(securityIndexName).getIndex(); + Index index = new Index(securityIndexName, UUID.randomUUID().toString()); 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 46a7c7b5816cc..6a8c54fa71546 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 @@ -53,6 +53,7 @@ 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; @@ -109,7 +110,8 @@ public void testIndexWithUpToDateMappingAndTemplate() { final ClusterState.Builder clusterStateBuilder = createClusterState(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7, RestrictedIndicesNames.SECURITY_MAIN_ALIAS); - manager.clusterChanged(event(markShardsAvailable(clusterStateBuilder))); + markShardsAvailable(clusterStateBuilder); + manager.clusterChanged(event(clusterStateBuilder)); assertThat(manager.indexExists(), Matchers.equalTo(true)); assertThat(manager.isAvailable(), Matchers.equalTo(true)); @@ -119,10 +121,9 @@ public void testIndexWithUpToDateMappingAndTemplate() { public void testIndexWithoutPrimaryShards() { assertInitialState(); - 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(); + 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()); ShardRouting shardRouting = ShardRouting.newUnassigned(new ShardId(index, 0), true, RecoverySource.ExistingStoreRecoverySource.INSTANCE, new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "")); String nodeId = ESTestCase.randomAlphaOfLength(8); @@ -133,13 +134,13 @@ public void testIndexWithoutPrimaryShards() { clusterStateBuilder.routingTable(RoutingTable.builder() .add(IndexRoutingTable.builder(index).addIndexShard(table).build()) .build()); - manager.clusterChanged(event(clusterStateBuilder.build())); + manager.clusterChanged(event(clusterStateBuilder)); assertIndexUpToDateButNotAvailable(); } - private ClusterChangedEvent event(ClusterState clusterState) { - return new ClusterChangedEvent("test-event", clusterState, EMPTY_CLUSTER_STATE); + private ClusterChangedEvent event(ClusterState.Builder clusterStateBuilder) { + return new ClusterChangedEvent("test-event", clusterStateBuilder.build(), EMPTY_CLUSTER_STATE); } public void testIndexHealthChangeListeners() { @@ -156,8 +157,9 @@ 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); - final ClusterState clusterState = markShardsAvailable(clusterStateBuilder); - manager.clusterChanged(event(clusterState)); + markShardsAvailable(clusterStateBuilder); + final ClusterState clusterState = clusterStateBuilder.build(); + manager.clusterChanged(event(ClusterState.builder(clusterState))); assertTrue(listenerCalled.get()); assertNull(previousState.get().indexHealth); @@ -220,7 +222,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).build())); + manager.clusterChanged(event(new ClusterState.Builder(CLUSTER_NAME).blocks(blocks))); manager.prepareIndexIfNeededThenExecute(prepareException::set, () -> prepareRunnableCalled.set(true)); assertThat(prepareException.get(), is(notNullValue())); assertThat(prepareException.get(), instanceOf(ElasticsearchStatusException.class)); @@ -232,7 +234,8 @@ 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); - manager.clusterChanged(event(markShardsAvailable(clusterStateBuilder))); + markShardsAvailable(clusterStateBuilder); + manager.clusterChanged(event(clusterStateBuilder)); manager.prepareIndexIfNeededThenExecute(prepareException::set, () -> prepareRunnableCalled.set(true)); assertThat(prepareException.get(), is(nullValue())); assertThat(prepareRunnableCalled.get(), is(true)); @@ -259,7 +262,8 @@ public void testCanUpdateIndexMappings() { IndexMetadata.State.OPEN, getMappings(previousVersion) ); - manager.clusterChanged(event(markShardsAvailable(clusterStateBuilder))); + markShardsAvailable(clusterStateBuilder); + manager.clusterChanged(event(clusterStateBuilder)); manager.prepareIndexIfNeededThenExecute(prepareException::set, () -> prepareRunnableCalled.set(true)); @@ -292,7 +296,8 @@ public void testCannotUpdateIndexMappingsWhenMinNodeVersionTooLow() { IndexMetadata.State.OPEN, getMappings(previousVersion) ); - manager.clusterChanged(event(markShardsAvailable(clusterStateBuilder))); + markShardsAvailable(clusterStateBuilder); + manager.clusterChanged(event(clusterStateBuilder)); manager.prepareIndexIfNeededThenExecute(prepareException::set, () -> prepareRunnableCalled.set(true)); assertThat(prepareRunnableCalled.get(), is(false)); @@ -308,20 +313,21 @@ 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).build())); + manager.clusterChanged(event(new ClusterState.Builder(CLUSTER_NAME).blocks(blocks))); 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); - manager.clusterChanged(event(markShardsAvailable(clusterStateBuilder))); + markShardsAvailable(clusterStateBuilder); + manager.clusterChanged(event(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).build())); + manager.clusterChanged(event(new ClusterState.Builder(CLUSTER_NAME))); AtomicBoolean upToDateChanged = new AtomicBoolean(); manager.addStateListener((prev, current) -> { listenerCalled.set(true); @@ -329,21 +335,22 @@ public void testIndexOutOfDateListeners() { }); assertTrue(manager.isIndexUpToDate()); - manager.clusterChanged(event(new ClusterState.Builder(CLUSTER_NAME).build())); + manager.clusterChanged(event(new ClusterState.Builder(CLUSTER_NAME))); 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); - manager.clusterChanged(event(markShardsAvailable(clusterStateBuilder))); + markShardsAvailable(clusterStateBuilder); + manager.clusterChanged(event(clusterStateBuilder)); assertTrue(listenerCalled.get()); assertTrue(upToDateChanged.get()); assertFalse(manager.isIndexUpToDate()); listenerCalled.set(false); assertFalse(listenerCalled.get()); - manager.clusterChanged(event(new ClusterState.Builder(CLUSTER_NAME).build())); + manager.clusterChanged(event(new ClusterState.Builder(CLUSTER_NAME))); assertTrue(listenerCalled.get()); assertTrue(upToDateChanged.get()); assertTrue(manager.isIndexUpToDate()); @@ -352,7 +359,8 @@ 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); - manager.clusterChanged(event(markShardsAvailable(clusterStateBuilder))); + markShardsAvailable(clusterStateBuilder); + manager.clusterChanged(event(clusterStateBuilder)); assertTrue(listenerCalled.get()); assertFalse(upToDateChanged.get()); assertTrue(manager.isIndexUpToDate()); @@ -362,21 +370,23 @@ public void testProcessClosedIndexState() { // Index initially exists final ClusterState.Builder indexAvailable = createClusterState(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7, RestrictedIndicesNames.SECURITY_MAIN_ALIAS, IndexMetadata.State.OPEN); - manager.clusterChanged(event(markShardsAvailable(indexAvailable))); + markShardsAvailable(indexAvailable); + + manager.clusterChanged(event(indexAvailable)); assertThat(manager.indexExists(), is(true)); assertThat(manager.isAvailable(), is(true)); // Now close it - ClusterState.Builder indexClosed = createClusterState(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7, + final 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 { - indexClosed = ClusterState.builder(markShardsAvailable(indexClosed)); + markShardsAvailable(indexClosed); } - manager.clusterChanged(event(indexClosed.build())); + manager.clusterChanged(event(indexClosed)); assertThat(manager.indexExists(), is(true)); assertThat(manager.isAvailable(), is(false)); } @@ -417,11 +427,8 @@ private static ClusterState.Builder createClusterState(String indexName, String return ClusterState.builder(state()).metadata(metadataBuilder.build()); } - 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 void markShardsAvailable(ClusterState.Builder clusterStateBuilder) { + clusterStateBuilder.routingTable(SecurityTestUtils.buildIndexRoutingTable(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7)); } 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 b45d05def523a..8b43df26a2fa2 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,6 +29,7 @@ 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; @@ -66,7 +67,8 @@ public static String writeFile(Path folder, String name, String content) { return writeFile(folder, name, content.getBytes(StandardCharsets.UTF_8)); } - public static RoutingTable buildIndexRoutingTable(Index index) { + public static RoutingTable buildIndexRoutingTable(String indexName) { + Index index = new Index(indexName, UUID.randomUUID().toString()); ShardRouting shardRouting = ShardRouting.newUnassigned(new ShardId(index, 0), true, ExistingStoreRecoverySource.INSTANCE, new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "")); String nodeId = ESTestCase.randomAlphaOfLength(8);