-
Notifications
You must be signed in to change notification settings - Fork 315
WIP: Replace weak-map per context store with a single global weak-map #9749
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
Draft
mcculls
wants to merge
2
commits into
master
Choose a base branch
from
mcculls/global-weak-context-store
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
...agent/agent-bootstrap/src/jmh/java/datadog/trace/bootstrap/WeakContextStoreBenchmark.java
This file contains hidden or 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,94 @@ | ||
| package datadog.trace.bootstrap; | ||
|
|
||
| import static java.util.concurrent.TimeUnit.MICROSECONDS; | ||
| import static org.openjdk.jmh.annotations.Mode.AverageTime; | ||
|
|
||
| import datadog.trace.bootstrap.weakmap.WeakMapContextStore; | ||
| import datadog.trace.bootstrap.weakmap.WeakMaps; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import java.util.function.BiFunction; | ||
| import org.openjdk.jmh.annotations.Benchmark; | ||
| import org.openjdk.jmh.annotations.BenchmarkMode; | ||
| import org.openjdk.jmh.annotations.Fork; | ||
| import org.openjdk.jmh.annotations.Level; | ||
| import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
| import org.openjdk.jmh.annotations.Param; | ||
| import org.openjdk.jmh.annotations.Scope; | ||
| import org.openjdk.jmh.annotations.Setup; | ||
| import org.openjdk.jmh.annotations.State; | ||
| import org.openjdk.jmh.annotations.Threads; | ||
| import org.openjdk.jmh.infra.Blackhole; | ||
|
|
||
| @State(Scope.Benchmark) | ||
| @BenchmarkMode(AverageTime) | ||
| @OutputTimeUnit(MICROSECONDS) | ||
| @SuppressWarnings({"unused", "rawtypes", "unchecked"}) | ||
| public class WeakContextStoreBenchmark { | ||
| private static final int NUM_STORES = 3; | ||
|
|
||
| @Param({"false", "true"}) | ||
| public boolean global; | ||
|
|
||
| private BiFunction[] stores; | ||
|
|
||
| private Map<String, String> kvs; | ||
|
|
||
| private AtomicInteger threadNumber; | ||
|
|
||
| @Setup(Level.Trial) | ||
| public void setup() { | ||
| WeakMaps.registerAsSupplier(); | ||
|
|
||
| stores = new BiFunction[NUM_STORES]; | ||
| for (int i = 0; i < NUM_STORES; i++) { | ||
| stores[i] = global ? globalWeakStorePutIfAbsent(i) : weakMapStorePutIfAbsent(i); | ||
| } | ||
|
|
||
| kvs = new HashMap<>(); | ||
| for (int i = 0; i < 1_000; i++) { | ||
| kvs.put("key_" + i, "value_" + i); | ||
| } | ||
|
|
||
| threadNumber = new AtomicInteger(); | ||
| } | ||
|
|
||
| @Benchmark | ||
| @Fork(value = 1) | ||
| @Threads(value = 1) | ||
| public void singleThreaded(Blackhole blackhole) { | ||
| test(blackhole); | ||
| } | ||
|
|
||
| @Benchmark | ||
| @Fork(value = 1) | ||
| @Threads(value = 10) | ||
| public void multiThreaded10(Blackhole blackhole) { | ||
| test(blackhole); | ||
| } | ||
|
|
||
| @Benchmark | ||
| @Fork(value = 1) | ||
| @Threads(value = 100) | ||
| public void multiThreaded100(Blackhole blackhole) { | ||
| test(blackhole); | ||
| } | ||
|
|
||
| private void test(Blackhole blackhole) { | ||
| // assign each benchmark thread a single store to operate on during the benchmark | ||
| // the number of concurrent requests to a store goes up as more threads are added | ||
| BiFunction store = stores[threadNumber.getAndIncrement() % NUM_STORES]; | ||
| for (Map.Entry e : kvs.entrySet()) { | ||
| blackhole.consume(store.apply(e.getKey(), e.getValue())); | ||
| } | ||
| } | ||
|
|
||
| private static BiFunction globalWeakStorePutIfAbsent(int storeId) { | ||
| return (k, v) -> GlobalWeakContextStore.weakPutIfAbsent(k, storeId, v); | ||
| } | ||
|
|
||
| private static BiFunction weakMapStorePutIfAbsent(int storeId) { | ||
| return new WeakMapContextStore<>(storeId)::putIfAbsent; | ||
| } | ||
| } |
2 changes: 1 addition & 1 deletion
2
...java/datadog/trace/bootstrap/WeakMap.java → ...adog/trace/bootstrap/weakmap/WeakMap.java
This file contains hidden or 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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package datadog.trace.bootstrap; | ||
| package datadog.trace.bootstrap.weakmap; | ||
|
|
||
| import java.util.function.Function; | ||
|
|
||
|
|
||
6 changes: 4 additions & 2 deletions
6
.../trace/bootstrap/WeakMapContextStore.java → ...ootstrap/weakmap/WeakMapContextStore.java
This file contains hidden or 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
3 changes: 1 addition & 2 deletions
3
...datadog/trace/agent/tooling/WeakMaps.java → ...dog/trace/bootstrap/weakmap/WeakMaps.java
This file contains hidden or 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 hidden or 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 hidden or 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
172 changes: 172 additions & 0 deletions
172
...a-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/GlobalWeakContextStore.java
This file contains hidden or 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,172 @@ | ||
| package datadog.trace.bootstrap; | ||
|
|
||
| import datadog.trace.api.Platform; | ||
| import datadog.trace.bootstrap.ContextStore.KeyAwareFactory; | ||
| import datadog.trace.util.AgentTaskScheduler; | ||
| import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
| import java.lang.ref.Reference; | ||
| import java.lang.ref.ReferenceQueue; | ||
| import java.lang.ref.WeakReference; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| /** | ||
| * Global weak {@link ContextStore} that acts as a fall-back when field-injection isn't possible. | ||
| */ | ||
| public final class GlobalWeakContextStore { | ||
|
|
||
| // global map of weak (key + store-id) wrappers mapped to context values | ||
| private static final Map<Object, Object> globalMap = new ConcurrentHashMap<>(); | ||
|
|
||
| // stale key wrappers that are now eligible for collection | ||
| private static final ReferenceQueue<Object> staleKeys = new ReferenceQueue<>(); | ||
|
|
||
| private static final long CLEAN_FREQUENCY_SECONDS = 1; | ||
|
|
||
| private static final int MAX_KEYS_CLEANED_PER_CYCLE = 1_000; | ||
|
|
||
| static { | ||
| if (!Platform.isNativeImageBuilder()) { | ||
| AgentTaskScheduler.get() | ||
| .scheduleAtFixedRate( | ||
| GlobalWeakContextStore::cleanStaleKeys, | ||
| CLEAN_FREQUENCY_SECONDS, | ||
| CLEAN_FREQUENCY_SECONDS, | ||
| TimeUnit.SECONDS); | ||
| } | ||
| } | ||
|
|
||
| /** Checks for stale key wrappers and removes them from the global map. */ | ||
| static void cleanStaleKeys() { | ||
| int count = 0; | ||
| Reference<?> ref; | ||
| while ((ref = staleKeys.poll()) != null) { | ||
| globalMap.remove(ref); | ||
| if (++count >= MAX_KEYS_CLEANED_PER_CYCLE) { | ||
| break; // limit work done per call | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private GlobalWeakContextStore() {} | ||
|
|
||
| public static Object weakGet(Object key, int storeId) { | ||
| return globalMap.get(new LookupKey(key, storeId)); | ||
| } | ||
|
|
||
| public static void weakPut(Object key, int storeId, Object context) { | ||
| if (context != null) { | ||
| globalMap.put(new StoreKey(key, storeId), context); | ||
| } else { | ||
| globalMap.remove(new LookupKey(key, storeId)); | ||
| } | ||
| } | ||
|
|
||
| public static Object weakPutIfAbsent(Object key, int storeId, Object context) { | ||
| LookupKey lookupKey = new LookupKey(key, storeId); | ||
| Object existing; | ||
| if (null == (existing = globalMap.get(lookupKey))) { | ||
| // This whole part with using synchronized is only because | ||
| // we want to avoid prematurely calling the factory if | ||
| // someone else is doing a putIfAbsent at the same time. | ||
| // There is still the possibility that there is a concurrent | ||
| // call to put that will win, but that is indistinguishable | ||
| // from the put happening right after the putIfAbsent. | ||
| synchronized (key) { | ||
| if (null == (existing = globalMap.get(lookupKey))) { | ||
| weakPut(key, storeId, existing = context); | ||
| } | ||
| } | ||
| } | ||
| return existing; | ||
| } | ||
|
|
||
| @SuppressWarnings({"rawtypes", "unchecked"}) | ||
| public static Object weakComputeIfAbsent( | ||
| Object key, int storeId, KeyAwareFactory contextFactory) { | ||
| LookupKey lookupKey = new LookupKey(key, storeId); | ||
| Object existing; | ||
| if (null == (existing = globalMap.get(lookupKey))) { | ||
| // This whole part with using synchronized is only because | ||
| // we want to avoid prematurely calling the factory if | ||
| // someone else is doing a putIfAbsent at the same time. | ||
| // There is still the possibility that there is a concurrent | ||
| // call to put that will win, but that is indistinguishable | ||
| // from the put happening right after the putIfAbsent. | ||
| synchronized (key) { | ||
| if (null == (existing = globalMap.get(lookupKey))) { | ||
| weakPut(key, storeId, existing = contextFactory.create(key)); | ||
| } | ||
| } | ||
| } | ||
| return existing; | ||
| } | ||
|
|
||
| public static Object weakRemove(Object key, int storeId) { | ||
| return globalMap.remove(new LookupKey(key, storeId)); | ||
| } | ||
|
|
||
| /** Reference key used to weakly associate a key and store-id with a context value. */ | ||
| static final class StoreKey extends WeakReference<Object> { | ||
| final int hash; | ||
| final int storeId; | ||
|
|
||
| StoreKey(Object key, int storeId) { | ||
| super(key, staleKeys); | ||
| this.hash = (31 * storeId) + System.identityHashCode(key); | ||
| this.storeId = storeId; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return hash; | ||
| } | ||
|
|
||
| @Override | ||
| @SuppressFBWarnings("Eq") // symmetric because it mirrors LookupKey.equals | ||
| public boolean equals(Object o) { | ||
| if (o instanceof LookupKey) { | ||
| LookupKey lookupKey = (LookupKey) o; | ||
| return storeId == lookupKey.storeId && get() == lookupKey.key; | ||
| } else if (o instanceof StoreKey) { | ||
| StoreKey storeKey = (StoreKey) o; | ||
| return storeId == storeKey.storeId && get() == storeKey.get(); | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** Temporary key used for lookup purposes without the reference tracking overhead. */ | ||
| static final class LookupKey { | ||
| final Object key; | ||
| final int hash; | ||
| final int storeId; | ||
|
|
||
| LookupKey(Object key, int storeId) { | ||
| this.key = key; | ||
| this.hash = (31 * storeId) + System.identityHashCode(key); | ||
| this.storeId = storeId; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return hash; | ||
| } | ||
|
|
||
| @Override | ||
| @SuppressFBWarnings("Eq") // symmetric because it mirrors StoreKey.equals | ||
| public boolean equals(Object o) { | ||
| if (o instanceof StoreKey) { | ||
| StoreKey storeKey = (StoreKey) o; | ||
| return storeId == storeKey.storeId && key == storeKey.get(); | ||
| } else if (o instanceof LookupKey) { | ||
| LookupKey lookupKey = (LookupKey) o; | ||
| return storeId == lookupKey.storeId && key == lookupKey.key; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious if it make sense to cover this class with unit tests, maybe with some threading?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's a draft PR with
WIPin the title... tests and benchmarks will turn upBut yes - this will definitely have both functional and multi-threaded test coverage.