Skip to content

Commit 0260c6f

Browse files
authored
ThreadPool and ThreadContext are not closeable (#43249)
This commit changes the ThreadContext to just use a regular ThreadLocal over the lucene CloseableThreadLocal. The CloseableThreadLocal solves issues with ThreadLocals that are no longer needed during runtime but in the case of the ThreadContext, we need it for the runtime of the node and it is typically not closed until the node closes, so we miss out on the benefits that this class provides. Additionally by removing the close logic, we simplify code in other places that deal with exceptions and tracking to see if it happens when the node is closing. Closes #42577
1 parent e0aa910 commit 0260c6f

File tree

21 files changed

+640
-838
lines changed

21 files changed

+640
-838
lines changed

qa/logging-config/src/test/java/org/elasticsearch/common/logging/JsonLoggerTests.java

Lines changed: 64 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -387,80 +387,77 @@ public void testJsonInStacktraceMessageIsSplitted() throws IOException {
387387
public void testDuplicateLogMessages() throws IOException {
388388
final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger("test"));
389389

390-
391390
// For the same key and X-Opaque-ID deprecation should be once
392-
try (ThreadContext threadContext = new ThreadContext(Settings.EMPTY)) {
393-
try{
394-
threadContext.putHeader(Task.X_OPAQUE_ID, "ID1");
395-
DeprecationLogger.setThreadContext(threadContext);
396-
deprecationLogger.deprecatedAndMaybeLog("key", "message1");
397-
deprecationLogger.deprecatedAndMaybeLog("key", "message2");
398-
assertWarnings("message1", "message2");
399-
400-
final Path path = PathUtils.get(System.getProperty("es.logs.base_path"),
401-
System.getProperty("es.logs.cluster_name") + "_deprecated.json");
402-
try (Stream<Map<String, String>> stream = JsonLogsStream.mapStreamFrom(path)) {
403-
List<Map<String, String>> jsonLogs = stream
404-
.collect(Collectors.toList());
405-
406-
assertThat(jsonLogs, contains(
407-
allOf(
408-
hasEntry("type", "deprecation"),
409-
hasEntry("level", "WARN"),
410-
hasEntry("component", "d.test"),
411-
hasEntry("cluster.name", "elasticsearch"),
412-
hasEntry("node.name", "sample-name"),
413-
hasEntry("message", "message1"),
414-
hasEntry("x-opaque-id", "ID1"))
415-
)
416-
);
417-
}
418-
}finally{
419-
DeprecationLogger.removeThreadContext(threadContext);
391+
ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
392+
try (ThreadContext.StoredContext ignore = threadContext.stashContext()) {
393+
threadContext.putHeader(Task.X_OPAQUE_ID, "ID1");
394+
DeprecationLogger.setThreadContext(threadContext);
395+
deprecationLogger.deprecatedAndMaybeLog("key", "message1");
396+
deprecationLogger.deprecatedAndMaybeLog("key", "message2");
397+
assertWarnings("message1", "message2");
398+
399+
final Path path = PathUtils.get(System.getProperty("es.logs.base_path"),
400+
System.getProperty("es.logs.cluster_name") + "_deprecated.json");
401+
try (Stream<Map<String, String>> stream = JsonLogsStream.mapStreamFrom(path)) {
402+
List<Map<String, String>> jsonLogs = stream
403+
.collect(Collectors.toList());
404+
405+
assertThat(jsonLogs, contains(
406+
allOf(
407+
hasEntry("type", "deprecation"),
408+
hasEntry("level", "WARN"),
409+
hasEntry("component", "d.test"),
410+
hasEntry("cluster.name", "elasticsearch"),
411+
hasEntry("node.name", "sample-name"),
412+
hasEntry("message", "message1"),
413+
hasEntry("x-opaque-id", "ID1"))
414+
)
415+
);
420416
}
417+
} finally {
418+
DeprecationLogger.removeThreadContext(threadContext);
421419
}
422420

421+
423422
// For the same key and different X-Opaque-ID should be multiple times per key/x-opaque-id
424423
//continuing with message1-ID1 in logs already, adding a new deprecation log line with message2-ID2
425-
try (ThreadContext threadContext = new ThreadContext(Settings.EMPTY)) {
426-
try{
427-
threadContext.putHeader(Task.X_OPAQUE_ID, "ID2");
428-
DeprecationLogger.setThreadContext(threadContext);
429-
deprecationLogger.deprecatedAndMaybeLog("key", "message1");
430-
deprecationLogger.deprecatedAndMaybeLog("key", "message2");
431-
assertWarnings("message1", "message2");
432-
433-
final Path path = PathUtils.get(System.getProperty("es.logs.base_path"),
434-
System.getProperty("es.logs.cluster_name") + "_deprecated.json");
435-
try (Stream<Map<String, String>> stream = JsonLogsStream.mapStreamFrom(path)) {
436-
List<Map<String, String>> jsonLogs = stream
437-
.collect(Collectors.toList());
438-
439-
assertThat(jsonLogs, contains(
440-
allOf(
441-
hasEntry("type", "deprecation"),
442-
hasEntry("level", "WARN"),
443-
hasEntry("component", "d.test"),
444-
hasEntry("cluster.name", "elasticsearch"),
445-
hasEntry("node.name", "sample-name"),
446-
hasEntry("message", "message1"),
447-
hasEntry("x-opaque-id", "ID1")
448-
),
449-
allOf(
450-
hasEntry("type", "deprecation"),
451-
hasEntry("level", "WARN"),
452-
hasEntry("component", "d.test"),
453-
hasEntry("cluster.name", "elasticsearch"),
454-
hasEntry("node.name", "sample-name"),
455-
hasEntry("message", "message1"),
456-
hasEntry("x-opaque-id", "ID2")
457-
)
458-
)
459-
);
460-
}
461-
}finally{
462-
DeprecationLogger.removeThreadContext(threadContext);
424+
try (ThreadContext.StoredContext ignore = threadContext.stashContext()) {
425+
threadContext.putHeader(Task.X_OPAQUE_ID, "ID2");
426+
DeprecationLogger.setThreadContext(threadContext);
427+
deprecationLogger.deprecatedAndMaybeLog("key", "message1");
428+
deprecationLogger.deprecatedAndMaybeLog("key", "message2");
429+
assertWarnings("message1", "message2");
430+
431+
final Path path = PathUtils.get(System.getProperty("es.logs.base_path"),
432+
System.getProperty("es.logs.cluster_name") + "_deprecated.json");
433+
try (Stream<Map<String, String>> stream = JsonLogsStream.mapStreamFrom(path)) {
434+
List<Map<String, String>> jsonLogs = stream
435+
.collect(Collectors.toList());
436+
437+
assertThat(jsonLogs, contains(
438+
allOf(
439+
hasEntry("type", "deprecation"),
440+
hasEntry("level", "WARN"),
441+
hasEntry("component", "d.test"),
442+
hasEntry("cluster.name", "elasticsearch"),
443+
hasEntry("node.name", "sample-name"),
444+
hasEntry("message", "message1"),
445+
hasEntry("x-opaque-id", "ID1")
446+
),
447+
allOf(
448+
hasEntry("type", "deprecation"),
449+
hasEntry("level", "WARN"),
450+
hasEntry("component", "d.test"),
451+
hasEntry("cluster.name", "elasticsearch"),
452+
hasEntry("node.name", "sample-name"),
453+
hasEntry("message", "message1"),
454+
hasEntry("x-opaque-id", "ID2")
455+
)
456+
)
457+
);
463458
}
459+
} finally {
460+
DeprecationLogger.removeThreadContext(threadContext);
464461
}
465462
}
466463

server/src/main/java/org/elasticsearch/common/logging/DeprecationLogger.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,6 @@ public Void run() {
262262

263263
public String getXOpaqueId(Set<ThreadContext> threadContexts) {
264264
return threadContexts.stream()
265-
.filter(t -> t.isClosed() == false)
266265
.filter(t -> t.getHeader(Task.X_OPAQUE_ID) != null)
267266
.findFirst()
268267
.map(t -> t.getHeader(Task.X_OPAQUE_ID))

server/src/main/java/org/elasticsearch/common/util/concurrent/EsThreadPoolExecutor.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,8 @@ protected void afterExecute(Runnable r, Throwable t) {
106106
}
107107

108108
private boolean assertDefaultContext(Runnable r) {
109-
try {
110-
assert contextHolder.isDefaultContext() : "the thread context is not the default context and the thread [" +
111-
Thread.currentThread().getName() + "] is being returned to the pool after executing [" + r + "]";
112-
} catch (IllegalStateException ex) {
113-
// sometimes we execute on a closed context and isDefaultContext doen't bypass the ensureOpen checks
114-
// this must not trigger an exception here since we only assert if the default is restored and
115-
// we don't really care if we are closed
116-
if (contextHolder.isClosed() == false) {
117-
throw ex;
118-
}
119-
}
109+
assert contextHolder.isDefaultContext() : "the thread context is not the default context and the thread [" +
110+
Thread.currentThread().getName() + "] is being returned to the pool after executing [" + r + "]";
120111
return true;
121112
}
122113

0 commit comments

Comments
 (0)