-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for thread context map propagation
Adds a new `ExtendedThreadContext` SPI class to help external libraries with an efficient propagation of the `ThreadContextMap`. Currently integrators can retrieve and set the context map through a combination of `ThreadContext#getImmutableContext`, `#clear` and `#putAll`. This inevitable leads to the creation of temporary objects if the underlying implementation does not use the JDK `Map` class internally. As a replacement we provide two `saveMap` and `restoreMap` methods that can be used to access the `ThreadLocal`s directly. Due to the inherent unsafety of such operations, these methods are only available from the `o.a.l.l.spi` package. We also reimplement `CloseableThreadContext.Instance` to take advantage of this SPI.
- Loading branch information
Showing
17 changed files
with
496 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
log4j-api-test/src/main/java/org/apache/logging/log4j/test/spi/ThreadContextMapSuite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to you under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.logging.log4j.test.spi; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.time.Duration; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import org.apache.logging.log4j.spi.ThreadContextMap; | ||
|
||
/** | ||
* A simple suite of tests for {@link ThreadContextMap} implementations. | ||
*/ | ||
public abstract class ThreadContextMapSuite { | ||
private static final String KEY = "key"; | ||
|
||
/** | ||
* Checks if new threads either inherit or do not inherit the context data of the current thread. | ||
* | ||
* @param threadContext A {@link ThreadContextMap implementation}. | ||
* @param key The key to use. | ||
* @param expectedValue The expected value on a new thread. | ||
*/ | ||
protected static void assertThreadContextValueOnANewThread( | ||
final ThreadContextMap threadContext, final String key, final String expectedValue) { | ||
final ExecutorService executorService = Executors.newSingleThreadExecutor(); | ||
try { | ||
assertThat(executorService.submit(() -> threadContext.get(key))) | ||
.succeedsWithin(Duration.ofSeconds(1)) | ||
.isEqualTo(expectedValue); | ||
} finally { | ||
executorService.shutdown(); | ||
} | ||
} | ||
|
||
/** | ||
* Ensures that {@code save/restore} works correctly on the current thread. | ||
*/ | ||
protected static void assertContextDataCanBeSavedAndRestored(final ThreadContextMap threadContext) { | ||
final String externalValue = "externalValue"; | ||
final String internalValue = "internalValue"; | ||
final RuntimeException throwable = new RuntimeException(); | ||
threadContext.put(KEY, externalValue); | ||
final Object saved = threadContext.save(); | ||
try { | ||
threadContext.put(KEY, internalValue); | ||
assertThat(threadContext.get(KEY)).isEqualTo(internalValue); | ||
throw throwable; | ||
} catch (final RuntimeException e) { | ||
assertThat(e).isSameAs(throwable); | ||
assertThat(threadContext.get(KEY)).isEqualTo(internalValue); | ||
} finally { | ||
threadContext.restore(saved); | ||
} | ||
assertThat(threadContext.get(KEY)).isEqualTo(externalValue); | ||
} | ||
|
||
/** | ||
* Ensures that the context data obtained through {@link ThreadContextMap#save} can be safely transferred to another | ||
* thread. | ||
* | ||
* @param threadContext The {@link ThreadContextMap} to test. | ||
*/ | ||
protected static void assertContextDataCanBeTransferred(final ThreadContextMap threadContext) { | ||
final String mainThreadValue = "mainThreadValue"; | ||
final String newThreadValue = "newThreadValue"; | ||
final RuntimeException throwable = new RuntimeException(); | ||
threadContext.put(KEY, mainThreadValue); | ||
final Object mainThreadSaved = threadContext.save(); | ||
threadContext.remove(KEY); | ||
// Move to new thread | ||
final ExecutorService executorService = Executors.newSingleThreadExecutor(); | ||
try { | ||
assertThat(executorService.submit(() -> { | ||
threadContext.put(KEY, newThreadValue); | ||
final Object newThreadSaved = threadContext.restore(mainThreadSaved); | ||
try { | ||
assertThat(threadContext.get(KEY)).isEqualTo(mainThreadValue); | ||
throw throwable; | ||
} catch (final RuntimeException e) { | ||
assertThat(e).isSameAs(throwable); | ||
assertThat(threadContext.get(KEY)).isEqualTo(mainThreadValue); | ||
} finally { | ||
threadContext.restore(newThreadSaved); | ||
} | ||
assertThat(threadContext.get(KEY)).isEqualTo(newThreadValue); | ||
})) | ||
.succeedsWithin(Duration.ofSeconds(1)); | ||
} finally { | ||
executorService.shutdown(); | ||
} | ||
} | ||
|
||
/** | ||
* Ensures the {@code restore(null)} can be used to clear the context data. | ||
* | ||
* @param threadContext The {@link ThreadContextMap} to test. | ||
*/ | ||
protected static void assertNullValueClearsTheContextData(final ThreadContextMap threadContext) { | ||
final String value = "restoreAcceptsNull"; | ||
final RuntimeException throwable = new RuntimeException(); | ||
threadContext.put(KEY, value); | ||
final Object saved = threadContext.restore(null); | ||
try { | ||
assertThat(threadContext.getImmutableMapOrNull()).isNullOrEmpty(); | ||
throw throwable; | ||
} catch (final RuntimeException e) { | ||
assertThat(e).isSameAs(throwable); | ||
assertThat(threadContext.getImmutableMapOrNull()).isNullOrEmpty(); | ||
} finally { | ||
threadContext.restore(saved); | ||
} | ||
assertThat(threadContext.get(KEY)).isEqualTo(value); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
log4j-api-test/src/main/java/org/apache/logging/log4j/test/spi/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache license, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the license for the specific language governing permissions and | ||
* limitations under the license. | ||
*/ | ||
/** | ||
* Contains helper classes to blackbox test implementations of SPI interfaces. | ||
* @since 2.24.0 | ||
*/ | ||
@Export | ||
@Version("2.24.0") | ||
package org.apache.logging.log4j.test.spi; | ||
|
||
import org.osgi.annotation.bundle.Export; | ||
import org.osgi.annotation.versioning.Version; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.