Skip to content

Commit

Permalink
Tighten up ThreadPool#assertCurrentThreadPool (#108943)
Browse files Browse the repository at this point in the history
As suggested in #108934, we can extract the exact executor name from the
thread name with some simple string manipulations. Using this utility,
this commit tightens up the existing assertions about the current
executor.

Co-authored-by: Henning Andersen <henning.andersen@elastic.co>
  • Loading branch information
DaveCTurner and henningandersen committed May 23, 2024
1 parent 13b36c1 commit 331b78f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,16 @@ public static String threadName(final String nodeName, final String namePrefix)
return "elasticsearch" + (nodeName.isEmpty() ? "" : "[") + nodeName + (nodeName.isEmpty() ? "" : "]") + "[" + namePrefix + "]";
}

public static String executorName(String threadName) {
// subtract 2 to avoid the `]` of the thread number part.
int executorNameEnd = threadName.lastIndexOf(']', threadName.length() - 2);
int executorNameStart = threadName.lastIndexOf('[', executorNameEnd);
if (executorNameStart == -1 || executorNameEnd - executorNameStart <= 1) {
return null;
}
return threadName.substring(executorNameStart + 1, executorNameEnd);
}

public static ThreadFactory daemonThreadFactory(Settings settings, String namePrefix) {
return daemonThreadFactory(threadName(settings, namePrefix));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1118,9 +1118,10 @@ public static boolean assertNotScheduleThread(String reason) {

public static boolean assertCurrentThreadPool(String... permittedThreadPoolNames) {
final var threadName = Thread.currentThread().getName();
final var executorName = EsExecutors.executorName(threadName);
assert threadName.startsWith("TEST-")
|| threadName.startsWith("LuceneTestCase")
|| Arrays.stream(permittedThreadPoolNames).anyMatch(n -> threadName.contains('[' + n + ']'))
|| Arrays.asList(permittedThreadPoolNames).contains(executorName)
: threadName + " not in " + Arrays.toString(permittedThreadPoolNames) + " nor a test thread";
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,17 @@ public void testFixedUnboundedRejectOnShutdown() {
);
}

public void testParseExecutorName() throws InterruptedException {
final var executorName = randomAlphaOfLength(10);
final var threadFactory = EsExecutors.daemonThreadFactory(rarely() ? null : randomAlphaOfLength(10), executorName);
final var thread = threadFactory.newThread(() -> {});
try {
assertThat(EsExecutors.executorName(thread.getName()), equalTo(executorName));
} finally {
thread.join();
}
}

private static void runRejectOnShutdownTest(ExecutorService executor) {
for (int i = between(0, 10); i > 0; i--) {
final var delayMillis = between(0, 100);
Expand Down

0 comments on commit 331b78f

Please sign in to comment.