2020
2121import org .elasticsearch .cluster .node .DiscoveryNode ;
2222import org .elasticsearch .common .settings .Settings ;
23+ import org .elasticsearch .common .unit .TimeValue ;
2324import org .elasticsearch .test .ESTestCase ;
2425import org .junit .Before ;
2526
2829import java .util .HashSet ;
2930import java .util .Set ;
3031
32+ import static org .elasticsearch .cluster .coordination .LagDetector .CLUSTER_FOLLOWER_LAG_TIMEOUT_SETTING ;
3133import static org .elasticsearch .node .Node .NODE_NAME_SETTING ;
3234import static org .hamcrest .Matchers .contains ;
3335import static org .hamcrest .Matchers .empty ;
@@ -40,13 +42,23 @@ public class LagDetectorTests extends ESTestCase {
4042 private Set <DiscoveryNode > failedNodes ;
4143 private LagDetector lagDetector ;
4244 private DiscoveryNode node1 , node2 , localNode ;
45+ private TimeValue followerLagTimeout ;
4346
4447 @ Before
4548 public void setupFixture () {
4649 deterministicTaskQueue = new DeterministicTaskQueue (Settings .builder ().put (NODE_NAME_SETTING .getKey (), "node" ).build (), random ());
4750
4851 failedNodes = new HashSet <>();
49- lagDetector = new LagDetector (Settings .EMPTY , deterministicTaskQueue .getThreadPool (), failedNodes ::add , () -> localNode );
52+
53+ Settings .Builder settingsBuilder = Settings .builder ();
54+ if (randomBoolean ()) {
55+ followerLagTimeout = TimeValue .timeValueMillis (randomLongBetween (2 , 100000 ));
56+ settingsBuilder .put (CLUSTER_FOLLOWER_LAG_TIMEOUT_SETTING .getKey (), followerLagTimeout .millis () + "ms" );
57+ } else {
58+ followerLagTimeout = CLUSTER_FOLLOWER_LAG_TIMEOUT_SETTING .get (Settings .EMPTY );
59+ }
60+
61+ lagDetector = new LagDetector (settingsBuilder .build (), deterministicTaskQueue .getThreadPool (), failedNodes ::add , () -> localNode );
5062
5163 localNode = CoordinationStateTests .createNode ("local" );
5264 node1 = CoordinationStateTests .createNode ("node1" );
@@ -76,8 +88,7 @@ public void testNoLagDetectedIfNodeAppliesVersionAfterLagDetectorStarted() {
7688 public void testNoLagDetectedIfNodeAppliesVersionJustBeforeTimeout () {
7789 lagDetector .setTrackedNodes (Collections .singletonList (node1 ));
7890 lagDetector .startLagDetector (1 );
79- deterministicTaskQueue .scheduleAt (deterministicTaskQueue .getCurrentTimeMillis ()
80- + LagDetector .CLUSTER_FOLLOWER_LAG_TIMEOUT_SETTING .get (Settings .EMPTY ).millis () - 1 ,
91+ deterministicTaskQueue .scheduleAt (deterministicTaskQueue .getCurrentTimeMillis () + followerLagTimeout .millis () - 1 ,
8192 () -> lagDetector .setAppliedVersion (node1 , 1 ));
8293 deterministicTaskQueue .runAllTasksInTimeOrder ();
8394 assertThat (failedNodes , empty ());
@@ -86,8 +97,7 @@ public void testNoLagDetectedIfNodeAppliesVersionJustBeforeTimeout() {
8697 public void testLagDetectedIfNodeAppliesVersionJustAfterTimeout () {
8798 lagDetector .setTrackedNodes (Collections .singletonList (node1 ));
8899 lagDetector .startLagDetector (1 );
89- deterministicTaskQueue .scheduleAt (deterministicTaskQueue .getCurrentTimeMillis ()
90- + LagDetector .CLUSTER_FOLLOWER_LAG_TIMEOUT_SETTING .get (Settings .EMPTY ).millis () + 1 ,
100+ deterministicTaskQueue .scheduleAt (deterministicTaskQueue .getCurrentTimeMillis () + followerLagTimeout .millis () + 1 ,
91101 () -> lagDetector .setAppliedVersion (node1 , 1 ));
92102 deterministicTaskQueue .runAllTasksInTimeOrder ();
93103 assertThat (failedNodes , contains (node1 ));
@@ -193,14 +203,12 @@ public void testLagDetection() {
193203 assertThat (failedNodes , empty ());
194204
195205 lagDetector .startLagDetector (3 );
196- deterministicTaskQueue .scheduleAt (deterministicTaskQueue .getCurrentTimeMillis ()
197- + LagDetector .CLUSTER_FOLLOWER_LAG_TIMEOUT_SETTING .get (Settings .EMPTY ).millis () - 1 ,
206+ deterministicTaskQueue .scheduleAt (deterministicTaskQueue .getCurrentTimeMillis () + followerLagTimeout .millis () - 1 ,
198207 () -> lagDetector .setAppliedVersion (node1 , 3 ));
199208 assertThat (failedNodes , empty ());
200209
201210 lagDetector .startLagDetector (4 );
202- deterministicTaskQueue .scheduleAt (deterministicTaskQueue .getCurrentTimeMillis ()
203- + LagDetector .CLUSTER_FOLLOWER_LAG_TIMEOUT_SETTING .get (Settings .EMPTY ).millis () + 1 ,
211+ deterministicTaskQueue .scheduleAt (deterministicTaskQueue .getCurrentTimeMillis () + followerLagTimeout .millis () + 1 ,
204212 () -> lagDetector .setAppliedVersion (node1 , 4 ));
205213 deterministicTaskQueue .runAllTasksInTimeOrder ();
206214 assertThat (failedNodes , contains (node1 ));
0 commit comments