From 040c2a64755410cb418ee78306344c953b161dad Mon Sep 17 00:00:00 2001 From: Jake Landis Date: Wed, 27 Sep 2023 11:31:26 -0500 Subject: [PATCH] change method name and avoid using stream --- .../cluster/routing/IndexRoutingTable.java | 19 +++++++++++++------ .../routing/IndexRoutingTableTests.java | 12 ++++++------ .../support/SecurityIndexManager.java | 2 +- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/cluster/routing/IndexRoutingTable.java b/server/src/main/java/org/elasticsearch/cluster/routing/IndexRoutingTable.java index cbb405b86068d..def9b1be1df72 100644 --- a/server/src/main/java/org/elasticsearch/cluster/routing/IndexRoutingTable.java +++ b/server/src/main/java/org/elasticsearch/cluster/routing/IndexRoutingTable.java @@ -232,16 +232,23 @@ public boolean allPrimaryShardsActive() { } /** - * @return true if all search shards are active. This is useful to determine if an index is available to service queries. + * @return true if an index is available to service search queries. */ - public boolean allSearchShardsActive() { - int counter = 0; + public boolean readyForSearch() { for (IndexShardRoutingTable shardRoutingTable : this.shards) { - if (shardRoutingTable.allShards().anyMatch(s -> s.active() && s.role().isSearchable())) { - counter++; + boolean found = false; + for (int idx = 0; idx < shardRoutingTable.size(); idx++) { + ShardRouting shardRouting = shardRoutingTable.shard(idx); + if (shardRouting.active() && shardRouting.role().isSearchable()) { + found = true; + break; + } + } + if (found == false) { + return false; } } - return counter == shards.length; + return true; } public boolean allShardsActive() { diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/IndexRoutingTableTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/IndexRoutingTableTests.java index a60a4501fbb1f..383305180647f 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/IndexRoutingTableTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/IndexRoutingTableTests.java @@ -17,7 +17,7 @@ public class IndexRoutingTableTests extends ESTestCase { - public void testAllSearchShardsActive() { + public void testReadyForSearch() { Index index = new Index(randomIdentifier(), UUIDs.randomBase64UUID()); // 2 primaries that are search and index ShardId p1 = new ShardId(index, 0); @@ -31,7 +31,7 @@ public void testAllSearchShardsActive() { List.of(getShard(p2, true, ShardRoutingState.STARTED, ShardRouting.Role.DEFAULT)) ); IndexRoutingTable indexRoutingTable = new IndexRoutingTable(index, new IndexShardRoutingTable[] { shardTable1, shardTable2 }); - assertTrue(indexRoutingTable.allSearchShardsActive()); + assertTrue(indexRoutingTable.readyForSearch()); // 2 primaries that are index only shardTable1 = new IndexShardRoutingTable( @@ -43,7 +43,7 @@ public void testAllSearchShardsActive() { List.of(getShard(p2, true, randomFrom(ShardRoutingState.values()), ShardRouting.Role.INDEX_ONLY)) ); indexRoutingTable = new IndexRoutingTable(index, new IndexShardRoutingTable[] { shardTable1, shardTable2 }); - assertFalse(indexRoutingTable.allSearchShardsActive()); + assertFalse(indexRoutingTable.readyForSearch()); // 2 primaries that are index only with replicas that are not all available shardTable1 = new IndexShardRoutingTable( @@ -63,7 +63,7 @@ public void testAllSearchShardsActive() { ) ); indexRoutingTable = new IndexRoutingTable(index, new IndexShardRoutingTable[] { shardTable1, shardTable2 }); - assertFalse(indexRoutingTable.allSearchShardsActive()); + assertFalse(indexRoutingTable.readyForSearch()); // 2 primaries that are index only with some replicas that are all available shardTable1 = new IndexShardRoutingTable( @@ -83,7 +83,7 @@ public void testAllSearchShardsActive() { ) ); indexRoutingTable = new IndexRoutingTable(index, new IndexShardRoutingTable[] { shardTable1, shardTable2 }); - assertTrue(indexRoutingTable.allSearchShardsActive()); + assertTrue(indexRoutingTable.readyForSearch()); // 2 primaries that are index only with at least 1 replica per primary that is available shardTable1 = new IndexShardRoutingTable( @@ -103,7 +103,7 @@ public void testAllSearchShardsActive() { ) ); indexRoutingTable = new IndexRoutingTable(index, new IndexShardRoutingTable[] { shardTable1, shardTable2 }); - assertTrue(indexRoutingTable.allSearchShardsActive()); + assertTrue(indexRoutingTable.readyForSearch()); } private ShardRouting getShard(ShardId shardId, boolean isPrimary, ShardRoutingState state, ShardRouting.Role role) { diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/SecurityIndexManager.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/SecurityIndexManager.java index 3912c7a913b11..0edb1a04449e5 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/SecurityIndexManager.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/SecurityIndexManager.java @@ -242,7 +242,7 @@ private boolean checkIndexAvailable(ClusterState state) { return false; } final IndexRoutingTable routingTable = state.routingTable().index(metadata.getIndex()); - if (routingTable == null || routingTable.allSearchShardsActive() == false) { + if (routingTable == null || routingTable.readyForSearch() == false) { logger.debug("Index [{}] is not yet active", aliasName); return false; } else {