Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure cached time elapses in ClusterServiceIT #91986

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.test.ESIntegTestCase.ClusterScope;
import org.elasticsearch.test.ESIntegTestCase.Scope;
import org.elasticsearch.threadpool.ThreadPool;

import java.util.Arrays;
import java.util.HashSet;
Expand All @@ -25,6 +26,7 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.StreamSupport;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
Expand Down Expand Up @@ -414,11 +416,7 @@ public void onFailure(Exception e) {
});
}

final var startNanoTime = System.nanoTime();
while (TimeUnit.MILLISECONDS.convert(System.nanoTime() - startNanoTime, TimeUnit.NANOSECONDS) <= 0) {
// noinspection BusyWait
Thread.sleep(100);
}
waitForTimeToElapse();

pendingClusterTasks = clusterService.getMasterService().pendingTasks();
assertThat(pendingClusterTasks.size(), greaterThanOrEqualTo(5));
Expand All @@ -441,4 +439,28 @@ public void onFailure(Exception e) {
block2.countDown();
}
}

private static void waitForTimeToElapse() throws InterruptedException {
final ThreadPool[] threadPools = StreamSupport.stream(internalCluster().getInstances(ClusterService.class).spliterator(), false)
.map(ClusterService::threadPool)
.toArray(ThreadPool[]::new);
final long[] startTimes = Arrays.stream(threadPools).mapToLong(ThreadPool::relativeTimeInMillis).toArray();

final var startNanoTime = System.nanoTime();
while (TimeUnit.MILLISECONDS.convert(System.nanoTime() - startNanoTime, TimeUnit.NANOSECONDS) <= 100) {
// noinspection BusyWait
Thread.sleep(100);
}

outer: do {
for (int i = 0; i < threadPools.length; i++) {
if (threadPools[i].relativeTimeInMillis() <= startTimes[i]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it even possible that the relativeTimeInMillis() for a threadpool could become less than when you recorded its start time above?

Copy link
Contributor Author

@DaveCTurner DaveCTurner Nov 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It certainly shouldn't go backwards, no. But that doesn't really matter here, and IMO it'd be kind of weird to say threadPools[i].relativeTimeInMillis() == startTimes[i] in this condition here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK I see, so it's sort of a safeguard.

// noinspection BusyWait
Thread.sleep(100);
continue outer;
}
}
return;
} while (true);
}
}