Skip to content

Commit 51b9901

Browse files
committed
Allow tests to permit cluster state application failures
1 parent 689c488 commit 51b9901

4 files changed

Lines changed: 33 additions & 1 deletion

File tree

server/src/main/java/org/elasticsearch/cluster/service/ClusterApplierService.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ private void runTask(UpdateTask task) {
449449
}
450450
// failing to apply a cluster state with an exception indicates a bug in validation or in one of the appliers; if we
451451
// continue we will retry with the same cluster state but that might not help.
452-
assert false;
452+
assert applicationMayFail();
453453
task.listener.onFailure(task.source, e);
454454
}
455455
}
@@ -664,4 +664,8 @@ protected long currentTimeInMillis() {
664664
return threadPool.relativeTimeInMillis();
665665
}
666666

667+
// overridden by tests that need to check behaviour in the event of an application failure
668+
protected boolean applicationMayFail() {
669+
return false;
670+
}
667671
}

server/src/test/java/org/elasticsearch/cluster/coordination/CoordinatorTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,7 @@ public void testAckListenerReceivesNackFromFollower() {
585585
final ClusterNode follower0 = cluster.getAnyNodeExcept(leader);
586586
final ClusterNode follower1 = cluster.getAnyNodeExcept(leader, follower0);
587587

588+
follower0.allowClusterStateApplicationFailure();
588589
follower0.setClusterStateApplyResponse(ClusterStateApplyResponse.FAIL);
589590
AckCollector ackCollector = leader.submitValue(randomLong());
590591
cluster.stabilise(DEFAULT_CLUSTER_STATE_UPDATE_DELAY);
@@ -604,6 +605,7 @@ public void testAckListenerReceivesNackFromLeader() {
604605
final ClusterNode follower1 = cluster.getAnyNodeExcept(leader, follower0);
605606
final long startingTerm = leader.coordinator.getCurrentTerm();
606607

608+
leader.allowClusterStateApplicationFailure();
607609
leader.setClusterStateApplyResponse(ClusterStateApplyResponse.FAIL);
608610
AckCollector ackCollector = leader.submitValue(randomLong());
609611
cluster.runFor(DEFAULT_CLUSTER_STATE_UPDATE_DELAY, "committing value");

server/src/test/java/org/elasticsearch/cluster/service/ClusterApplierServiceTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ public void testClusterStateApplierBubblesUpExceptionsInApplier() throws Interru
359359
clusterApplierService.addStateApplier(event -> {
360360
throw new RuntimeException("dummy exception");
361361
});
362+
clusterApplierService.allowClusterStateApplicationFailure();
362363

363364
CountDownLatch latch = new CountDownLatch(1);
364365
clusterApplierService.onNewClusterState("test", () -> ClusterState.builder(clusterApplierService.state()).build(),
@@ -387,6 +388,7 @@ public void testClusterStateApplierBubblesUpExceptionsInSettingsApplier() throws
387388
AtomicReference<Throwable> error = new AtomicReference<>();
388389
clusterApplierService.clusterSettings.addSettingsUpdateConsumer(EnableAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ENABLE_SETTING,
389390
v -> {});
391+
clusterApplierService.allowClusterStateApplicationFailure();
390392

391393
CountDownLatch latch = new CountDownLatch(1);
392394
clusterApplierService.onNewClusterState("test", () -> ClusterState.builder(clusterApplierService.state())
@@ -497,6 +499,7 @@ static class TimedClusterApplierService extends ClusterApplierService {
497499

498500
final ClusterSettings clusterSettings;
499501
volatile Long currentTimeOverride = null;
502+
boolean applicationMayFail;
500503

501504
TimedClusterApplierService(Settings settings, ClusterSettings clusterSettings, ThreadPool threadPool) {
502505
super("test_node", settings, clusterSettings, threadPool);
@@ -507,6 +510,15 @@ static class TimedClusterApplierService extends ClusterApplierService {
507510
protected long currentTimeInMillis() {
508511
return Objects.requireNonNullElseGet(currentTimeOverride, super::currentTimeInMillis);
509512
}
513+
514+
@Override
515+
protected boolean applicationMayFail() {
516+
return this.applicationMayFail;
517+
}
518+
519+
void allowClusterStateApplicationFailure() {
520+
this.applicationMayFail = true;
521+
}
510522
}
511523

512524
}

test/framework/src/main/java/org/elasticsearch/cluster/coordination/AbstractCoordinatorTestCase.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,10 @@ void applyInitialConfiguration() {
11721172
private boolean isNotUsefullyBootstrapped() {
11731173
return getLocalNode().isMasterNode() == false || coordinator.isInitialConfigurationSet() == false;
11741174
}
1175+
1176+
void allowClusterStateApplicationFailure() {
1177+
clusterApplierService.allowClusterStateApplicationFailure();
1178+
}
11751179
}
11761180

11771181
private List<TransportAddress> provideSeedHosts(SeedHostsProvider.HostsResolver ignored) {
@@ -1282,6 +1286,7 @@ static class DisruptableClusterApplierService extends ClusterApplierService {
12821286
private final String nodeName;
12831287
private final DeterministicTaskQueue deterministicTaskQueue;
12841288
ClusterStateApplyResponse clusterStateApplyResponse = ClusterStateApplyResponse.SUCCEED;
1289+
private boolean applicationMayFail;
12851290

12861291
DisruptableClusterApplierService(String nodeName, Settings settings, ClusterSettings clusterSettings,
12871292
DeterministicTaskQueue deterministicTaskQueue, Function<Runnable, Runnable> runnableWrapper) {
@@ -1326,6 +1331,15 @@ public void onNewClusterState(String source, Supplier<ClusterState> clusterState
13261331
protected void connectToNodesAndWait(ClusterState newClusterState) {
13271332
// don't do anything, and don't block
13281333
}
1334+
1335+
@Override
1336+
protected boolean applicationMayFail() {
1337+
return this.applicationMayFail;
1338+
}
1339+
1340+
void allowClusterStateApplicationFailure() {
1341+
this.applicationMayFail = true;
1342+
}
13291343
}
13301344

13311345
protected DiscoveryNode createDiscoveryNode(int nodeIndex, boolean masterEligible) {

0 commit comments

Comments
 (0)