Skip to content

Commit 5abaf5c

Browse files
committed
Fix termination condition in TestStressNRTReplication. (#14665)
* Fix termination condition in TestStressNRTReplication. Clean up the code a bit. * Rename sleep to pause so that superclass method is not invoked prior to static method.
1 parent f964796 commit 5abaf5c

File tree

1 file changed

+32
-30
lines changed

1 file changed

+32
-30
lines changed

lucene/replicator/src/test/org/apache/lucene/replicator/nrt/TestStressNRTReplication.java

+32-30
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ public class TestStressNRTReplication extends LuceneTestCase {
190190
final Set<Integer> crashingNodes = Collections.synchronizedSet(new HashSet<>());
191191

192192
@Nightly
193-
@SuppressForbidden(reason = "Thread sleep")
194193
public void test() throws Exception {
195194

196195
Node.globalStartNS = System.nanoTime();
@@ -215,7 +214,7 @@ public void test() throws Exception {
215214
numNodes = NUM_NODES;
216215
}
217216

218-
System.out.println("TEST: using " + numNodes + " nodes");
217+
message("TEST: using " + numNodes + " nodes");
219218

220219
transLogPath = createTempDir("NRTReplication").resolve("translog");
221220
transLog = new SimpleTransLog(transLogPath);
@@ -232,7 +231,7 @@ public void test() throws Exception {
232231
}
233232

234233
Thread[] indexers = new Thread[TestUtil.nextInt(random(), 1, 3)];
235-
System.out.println("TEST: launch " + indexers.length + " indexer threads");
234+
message("TEST: launch " + indexers.length + " indexer threads");
236235
for (int i = 0; i < indexers.length; i++) {
237236
indexers[i] = new IndexThread();
238237
indexers[i].setName("indexer" + i);
@@ -241,7 +240,7 @@ public void test() throws Exception {
241240
}
242241

243242
Thread[] searchers = new Thread[TestUtil.nextInt(random(), 1, 3)];
244-
System.out.println("TEST: launch " + searchers.length + " searcher threads");
243+
message("TEST: launch " + searchers.length + " searcher threads");
245244
for (int i = 0; i < searchers.length; i++) {
246245
searchers[i] = new SearchThread();
247246
searchers[i].setName("searcher" + i);
@@ -261,7 +260,7 @@ public void test() throws Exception {
261260
runTimeSec = RANDOM_MULTIPLIER * TestUtil.nextInt(random(), 20, 60);
262261
}
263262

264-
System.out.println("TEST: will run for " + runTimeSec + " sec");
263+
message("TEST: will run for " + runTimeSec + " sec");
265264

266265
long endTime = System.nanoTime() + runTimeSec * 1000000000L;
267266

@@ -270,7 +269,7 @@ public void test() throws Exception {
270269
while (failed.get() == false && System.nanoTime() < endTime) {
271270

272271
// Wait a bit:
273-
Thread.sleep(TestUtil.nextInt(random(), Math.min(runTimeSec * 4, 200), runTimeSec * 4));
272+
pause(TestUtil.nextInt(random(), Math.min(runTimeSec * 4, 200), runTimeSec * 4));
274273
if (primary != null && random().nextBoolean()) {
275274
NodeProcess curPrimary = primary;
276275
if (curPrimary != null) {
@@ -350,7 +349,7 @@ public void test() throws Exception {
350349
restarter.join();
351350

352351
// Close replicas before primary so we cancel any in-progress replications:
353-
System.out.println("TEST: top: now close replicas");
352+
message("TEST: top: now close replicas");
354353
List<Closeable> toClose = new ArrayList<>();
355354
for (NodeProcess node : nodes) {
356355
if (node != primary && node != null) {
@@ -673,7 +672,7 @@ NodeProcess startNode(final int id, Path indexPath, boolean isPrimary, long forc
673672
return null;
674673
} else {
675674
try {
676-
Thread.sleep(10);
675+
pause(10);
677676
} catch (InterruptedException ie) {
678677
throw new ThreadInterruptedException(ie);
679678
}
@@ -723,7 +722,7 @@ NodeProcess startNode(final int id, Path indexPath, boolean isPrimary, long forc
723722
() -> {
724723
while (System.nanoTime() < deadline && p.isAlive()) {
725724
try {
726-
Thread.sleep(250);
725+
pause(250);
727726
} catch (
728727
@SuppressWarnings("unused")
729728
InterruptedException e) {
@@ -815,6 +814,11 @@ NodeProcess startNode(final int id, Path indexPath, boolean isPrimary, long forc
815814
subprocessKiller);
816815
}
817816

817+
@SuppressForbidden(reason = "Thread sleep")
818+
private static void pause(int millis) throws InterruptedException {
819+
Thread.sleep(millis);
820+
}
821+
818822
private void nodeClosed(int id) {
819823
NodeProcess oldNode = nodes[id];
820824
if (primary != null && oldNode == primary) {
@@ -896,7 +900,7 @@ void addTransLogLoc(long version, long loc) {
896900
}
897901

898902
// Periodically wakes up and starts up any down nodes:
899-
@SuppressForbidden(reason = "Thread sleep")
903+
900904
private class RestartThread extends Thread {
901905
@Override
902906
public void run() {
@@ -905,7 +909,7 @@ public void run() {
905909

906910
try {
907911
while (stop.get() == false) {
908-
Thread.sleep(TestUtil.nextInt(random(), 50, 500));
912+
pause(TestUtil.nextInt(random(), 50, 500));
909913
// message("top: restarter cycle");
910914

911915
// Randomly crash full cluster:
@@ -915,7 +919,7 @@ public void run() {
915919
if (starting[i]) {
916920
message("N" + i + ": top: wait for startup so we can crash...");
917921
while (starting[i]) {
918-
Thread.sleep(10);
922+
pause(10);
919923
}
920924
message("N" + i + ": top: done wait for startup");
921925
}
@@ -994,26 +998,26 @@ public void run() {
994998
} finally {
995999
starting[idx] = false;
9961000
startupThreads.remove(Thread.currentThread());
1001+
message("N" + idx + ": top: removed thread");
9971002
}
9981003
}
9991004
};
1005+
startupThreads.add(t);
10001006
t.setName("start R" + idx);
10011007
t.start();
1002-
startupThreads.add(t);
10031008
}
10041009
} else {
10051010
message("node " + idx + " still starting");
10061011
}
10071012
}
10081013
}
10091014

1010-
System.out.println(
1011-
"Restarter: now stop: join " + startupThreads.size() + " startup threads");
1015+
message("Restarter: now stop: join " + startupThreads.size() + " startup threads");
10121016

1013-
while (startupThreads.size() > 0) {
1014-
Thread.sleep(10);
1017+
while (!startupThreads.isEmpty()) {
1018+
pause(1000);
1019+
message("Waiting for startup threads to terminate.");
10151020
}
1016-
10171021
} catch (Throwable t) {
10181022
failed.set(true);
10191023
stop.set(true);
@@ -1023,7 +1027,6 @@ public void run() {
10231027
}
10241028

10251029
/** Randomly picks a node and runs a search against it */
1026-
@SuppressForbidden(reason = "Thread sleep")
10271030
private class SearchThread extends Thread {
10281031

10291032
@Override
@@ -1074,7 +1077,7 @@ public void run() {
10741077
if (node.isOpen == false) {
10751078
throw new IOException("node closed");
10761079
}
1077-
Thread.sleep(1);
1080+
pause(1);
10781081
}
10791082
version = c.in.readVLong();
10801083

@@ -1085,7 +1088,7 @@ public void run() {
10851088
if (node.isOpen == false) {
10861089
throw new IOException("node closed");
10871090
}
1088-
Thread.sleep(1);
1091+
pause(1);
10891092
}
10901093
int hitCount = c.in.readVInt();
10911094

@@ -1161,7 +1164,7 @@ && random().nextInt(10) == 7) {
11611164
if (node.isOpen == false) {
11621165
throw new IOException("node died");
11631166
}
1164-
Thread.sleep(1);
1167+
pause(1);
11651168
}
11661169

11671170
version = c.in.readVLong();
@@ -1173,7 +1176,7 @@ && random().nextInt(10) == 7) {
11731176
if (node.isOpen == false) {
11741177
throw new IOException("node died");
11751178
}
1176-
Thread.sleep(1);
1179+
pause(1);
11771180
}
11781181

11791182
int hitCount = c.in.readVInt();
@@ -1208,7 +1211,7 @@ && random().nextInt(10) == 7) {
12081211
}
12091212
}
12101213

1211-
Thread.sleep(10);
1214+
pause(10);
12121215

12131216
} catch (Throwable t) {
12141217
failed.set(true);
@@ -1223,7 +1226,6 @@ && random().nextInt(10) == 7) {
12231226
}
12241227
}
12251228

1226-
@SuppressForbidden(reason = "Thread sleep")
12271229
private class IndexThread extends Thread {
12281230

12291231
@Override
@@ -1248,7 +1250,7 @@ public void run() {
12481250

12491251
try {
12501252
while (stop.get() == false && curPrimary == null) {
1251-
Thread.sleep(10);
1253+
pause(10);
12521254
curPrimary = primary;
12531255
if (curPrimary != null) {
12541256
c = new Connection(curPrimary.tcpPort);
@@ -1318,14 +1320,14 @@ public void run() {
13181320
}
13191321

13201322
if (random().nextInt(sleepChance) == 0) {
1321-
Thread.sleep(10);
1323+
pause(10);
13221324
}
13231325

13241326
if (random().nextInt(100) == 17) {
13251327
int pauseMS = TestUtil.nextInt(random(), 500, 2000);
1326-
System.out.println("Indexer: now pause for " + pauseMS + " ms...");
1327-
Thread.sleep(pauseMS);
1328-
System.out.println("Indexer: done pause for a bit...");
1328+
message("Indexer: now pause for " + pauseMS + " ms...");
1329+
pause(pauseMS);
1330+
message("Indexer: done pause for a bit...");
13291331
}
13301332
}
13311333
if (curPrimary != null) {

0 commit comments

Comments
 (0)