Skip to content

Commit 2bec0ef

Browse files
committed
fix
1 parent 328bd1b commit 2bec0ef

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

server/src/main/java/org/elasticsearch/cluster/routing/OperationRouting.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,9 @@ private static void collectTargetShardsWithRouting(
154154
indexRouting.collectSearchShards(r, s -> set.add(RoutingTable.shardRoutingTable(indexRoutingTable, s)));
155155
}
156156
} else {
157-
for (int i = 0; i < indexRoutingTable.size(); i++) {
158-
set.add(indexRoutingTable.shard(i));
157+
Iterator<IndexShardRoutingTable> iterator = allSearchAddressableShards(projectState, index);
158+
while (iterator.hasNext()) {
159+
set.add(iterator.next());
159160
}
160161
}
161162
}

server/src/test/java/org/elasticsearch/cluster/routing/OperationRoutingTests.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,13 @@ public void testOperationRoutingWithResharding() throws IOException {
519519
assertEquals(shardCount, initialSearchShards.size());
520520
assertEquals(0, initialSearchShards.get(0).shardId().id());
521521

522+
// We are testing a case when there is routing configuration but not for the index in question.
523+
// Actual routing behavior is tested in IndexRoutingTests.
524+
var initialSearchShardsWithRouting = clusterService.operationRouting()
525+
.searchShards(clusterService.state().projectState(projectId), new String[] { indexName }, Map.of("other", Set.of("1")), null);
526+
assertEquals(shardCount, initialSearchShardsWithRouting.size());
527+
assertEquals(0, initialSearchShardsWithRouting.get(0).shardId().id());
528+
522529
var initialWriteableShards = clusterService.operationRouting()
523530
.allWritableShards(clusterService.state().projectState(projectId), indexName);
524531
assertEquals(0, initialWriteableShards.next().shardId().id());
@@ -551,6 +558,11 @@ public void testOperationRoutingWithResharding() throws IOException {
551558
assertEquals(shardCount, searchShardsWithOneShardHandoff.size());
552559
assertEquals(0, searchShardsWithOneShardHandoff.get(0).shardId().id());
553560

561+
var searchShardsWithOneShardHandoffAndRouting = clusterService.operationRouting()
562+
.searchShards(clusterService.state().projectState(projectId), new String[] { indexName }, Map.of("other", Set.of("1")), null);
563+
assertEquals(shardCount, searchShardsWithOneShardHandoffAndRouting.size());
564+
assertEquals(0, searchShardsWithOneShardHandoffAndRouting.get(0).shardId().id());
565+
554566
var writeableShardsWithOneShardHandoff = clusterService.operationRouting()
555567
.allWritableShards(clusterService.state().projectState(projectId), indexName);
556568
assertEquals(0, writeableShardsWithOneShardHandoff.next().shardId().id());
@@ -581,6 +593,12 @@ public void testOperationRoutingWithResharding() throws IOException {
581593
assertEquals(0, searchShardsWithOneShardSplit.get(0).shardId().id());
582594
assertEquals(shardChangingSplitTargetState, searchShardsWithOneShardSplit.get(1).shardId().id());
583595

596+
var searchShardsWithOneShardSplitAndRouting = clusterService.operationRouting()
597+
.searchShards(clusterService.state().projectState(projectId), new String[] { indexName }, Map.of("other", Set.of("1")), null);
598+
assertEquals(shardCount + 1, searchShardsWithOneShardSplitAndRouting.size());
599+
assertEquals(0, searchShardsWithOneShardSplitAndRouting.get(0).shardId().id());
600+
assertEquals(shardChangingSplitTargetState, searchShardsWithOneShardSplitAndRouting.get(1).shardId().id());
601+
584602
var writeableShardsWithOneShardSplit = clusterService.operationRouting()
585603
.allWritableShards(clusterService.state().projectState(projectId), indexName);
586604
assertEquals(0, writeableShardsWithOneShardSplit.next().shardId().id());
@@ -611,6 +629,12 @@ public void testOperationRoutingWithResharding() throws IOException {
611629
assertEquals(0, searchShardsWithOneShardDone.get(0).shardId().id());
612630
assertEquals(shardChangingSplitTargetState, searchShardsWithOneShardDone.get(1).shardId().id());
613631

632+
var searchShardsWithOneShardDoneAndRouting = clusterService.operationRouting()
633+
.searchShards(clusterService.state().projectState(projectId), new String[] { indexName }, Map.of("other", Set.of("1")), null);
634+
assertEquals(shardCount + 1, searchShardsWithOneShardDoneAndRouting.size());
635+
assertEquals(0, searchShardsWithOneShardDoneAndRouting.get(0).shardId().id());
636+
assertEquals(shardChangingSplitTargetState, searchShardsWithOneShardDoneAndRouting.get(1).shardId().id());
637+
614638
var writeableShardsWithOneShardDone = clusterService.operationRouting()
615639
.allWritableShards(clusterService.state().projectState(projectId), indexName);
616640
assertEquals(0, writeableShardsWithOneShardDone.next().shardId().id());

0 commit comments

Comments
 (0)