Skip to content

Commit f862423

Browse files
Fix PrimaryAllocationIT Race Condition (#37355)
* Fix PrimaryAllocationIT Race Condition * Forcing a stale primary allocation on a green index was tripping the assertion that was removed * Added a test that this case still errors out correctly * Made the ability to wipe stopped datanode's data public on the internal test cluster and used it to ensure correct behaviour on the fixed test * Previously it simply passed because the test finished before the index went green and would NPE when the index was green at the time of the shard store status request, that would then come up empty * Closes #37345
1 parent 6637bbf commit f862423

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

server/src/main/java/org/elasticsearch/action/admin/cluster/reroute/TransportClusterRerouteAction.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,11 @@ private void verifyThenSubmitUpdate(ClusterRerouteRequest request, ActionListene
114114
for (Map.Entry<String, List<AbstractAllocateAllocationCommand>> entry : stalePrimaryAllocations.entrySet()) {
115115
final String index = entry.getKey();
116116
final ImmutableOpenIntMap<List<IndicesShardStoresResponse.StoreStatus>> indexStatus = status.get(index);
117-
assert indexStatus != null;
117+
if (indexStatus == null) {
118+
// The index in the stale primary allocation request was green and hence filtered out by the store status
119+
// request. We ignore it here since the relevant exception will be thrown by the reroute action later on.
120+
continue;
121+
}
118122
for (AbstractAllocateAllocationCommand command : entry.getValue()) {
119123
final List<IndicesShardStoresResponse.StoreStatus> shardStatus =
120124
indexStatus.get(command.shardId());

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,10 @@ public void testForceStaleReplicaToBePromotedToPrimaryOnWrongNode() throws Excep
305305
.put("index.number_of_replicas", 1)).get());
306306
ensureGreen();
307307
createStaleReplicaScenario(master);
308-
internalCluster().startDataOnlyNodes(2);
308+
// Ensure the stopped primary's data is deleted so that it doesn't get picked up by the next datanode we start
309+
internalCluster().wipePendingDataDirectories();
310+
internalCluster().startDataOnlyNodes(1);
311+
ensureStableCluster(3, master);
309312
final int shardId = 0;
310313
final List<String> nodeNames = new ArrayList<>(Arrays.asList(internalCluster().getNodeNames()));
311314
nodeNames.remove(master);
@@ -322,6 +325,25 @@ public void testForceStaleReplicaToBePromotedToPrimaryOnWrongNode() throws Excep
322325
equalTo("No data for shard [" + shardId + "] of index [" + idxName + "] found on node [" + nodeWithoutData + ']'));
323326
}
324327

328+
public void testForceStaleReplicaToBePromotedForGreenIndex() {
329+
internalCluster().startMasterOnlyNode(Settings.EMPTY);
330+
final List<String> dataNodes = internalCluster().startDataOnlyNodes(2);
331+
final String idxName = "test";
332+
assertAcked(client().admin().indices().prepareCreate(idxName)
333+
.setSettings(Settings.builder().put("index.number_of_shards", 1)
334+
.put("index.number_of_replicas", 1)).get());
335+
ensureGreen();
336+
final String nodeWithoutData = randomFrom(dataNodes);
337+
final int shardId = 0;
338+
IllegalArgumentException iae = expectThrows(
339+
IllegalArgumentException.class,
340+
() -> client().admin().cluster().prepareReroute()
341+
.add(new AllocateStalePrimaryAllocationCommand(idxName, shardId, nodeWithoutData, true)).get());
342+
assertThat(
343+
iae.getMessage(),
344+
equalTo("[allocate_stale_primary] primary [" + idxName+ "][" + shardId + "] is already assigned"));
345+
}
346+
325347
public void testForceStaleReplicaToBePromotedForMissingIndex() {
326348
internalCluster().startMasterOnlyNode(Settings.EMPTY);
327349
final String dataNode = internalCluster().startDataOnlyNode();

test/framework/src/main/java/org/elasticsearch/test/InternalTestCluster.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,8 +1386,7 @@ private void randomlyResetClients() throws IOException {
13861386
}
13871387
}
13881388

1389-
private void wipePendingDataDirectories() {
1390-
assert Thread.holdsLock(this);
1389+
public synchronized void wipePendingDataDirectories() {
13911390
if (!dataDirToClean.isEmpty()) {
13921391
try {
13931392
for (Path path : dataDirToClean) {

0 commit comments

Comments
 (0)