Skip to content

Commit cd324a1

Browse files
authored
Add test to verify force primary allocation on closed indices (#42458)
This change adds a test verifying that we can force primary allocation on closed indices.
1 parent 43848fc commit cd324a1

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
import org.elasticsearch.action.DocWriteResponse;
2424
import org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteRequestBuilder;
2525
import org.elasticsearch.action.admin.indices.shards.IndicesShardStoresResponse;
26+
import org.elasticsearch.action.admin.indices.stats.ShardStats;
2627
import org.elasticsearch.action.bulk.BulkResponse;
2728
import org.elasticsearch.action.index.IndexResponse;
2829
import org.elasticsearch.action.support.ActiveShardCount;
30+
import org.elasticsearch.action.support.IndicesOptions;
2931
import org.elasticsearch.cluster.ClusterState;
3032
import org.elasticsearch.cluster.ClusterStateListener;
3133
import org.elasticsearch.cluster.metadata.IndexMetaData;
@@ -65,6 +67,7 @@
6567
import java.util.concurrent.ExecutionException;
6668
import java.util.concurrent.TimeUnit;
6769
import java.util.stream.Collectors;
70+
import java.util.stream.Stream;
6871

6972
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS;
7073
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_SHARDS;
@@ -231,7 +234,9 @@ public void testForceStaleReplicaToBePromotedToPrimary() throws Exception {
231234
Set<String> historyUUIDs = Arrays.stream(client().admin().indices().prepareStats("test").clear().get().getShards())
232235
.map(shard -> shard.getCommitStats().getUserData().get(Engine.HISTORY_UUID_KEY)).collect(Collectors.toSet());
233236
createStaleReplicaScenario(master);
234-
237+
if (randomBoolean()) {
238+
assertAcked(client().admin().indices().prepareClose("test").setWaitForActiveShards(0));
239+
}
235240
boolean useStaleReplica = randomBoolean(); // if true, use stale replica, otherwise a completely empty copy
236241
logger.info("--> explicitly promote old primary shard");
237242
final String idxName = "test";
@@ -281,15 +286,18 @@ public void testForceStaleReplicaToBePromotedToPrimary() throws Exception {
281286
assertBusy(() -> assertTrue(client().admin().cluster().prepareState().get()
282287
.getState().routingTable().index(idxName).allPrimaryShardsActive()));
283288
}
284-
assertHitCount(client().prepareSearch(idxName).setSize(0).setQuery(matchAllQuery()).get(), useStaleReplica ? 1L : 0L);
285-
289+
ShardStats[] shardStats = client().admin().indices().prepareStats("test")
290+
.setIndicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED).get().getShards();
291+
for (ShardStats shardStat : shardStats) {
292+
assertThat(shardStat.getCommitStats().getNumDocs(), equalTo(useStaleReplica ? 1 : 0));
293+
}
286294
// allocation id of old primary was cleaned from the in-sync set
287295
final ClusterState state = client().admin().cluster().prepareState().get().getState();
288296

289297
assertEquals(Collections.singleton(state.routingTable().index(idxName).shard(0).primary.allocationId().getId()),
290298
state.metaData().index(idxName).inSyncAllocationIds(0));
291299

292-
Set<String> newHistoryUUIds = Arrays.stream(client().admin().indices().prepareStats("test").clear().get().getShards())
300+
Set<String> newHistoryUUIds = Stream.of(shardStats)
293301
.map(shard -> shard.getCommitStats().getUserData().get(Engine.HISTORY_UUID_KEY)).collect(Collectors.toSet());
294302
assertThat(newHistoryUUIds, everyItem(not(isIn(historyUUIDs))));
295303
assertThat(newHistoryUUIds, hasSize(1));

0 commit comments

Comments
 (0)