|
1 | 1 | package datadog.trace.api.debugger; |
2 | 2 |
|
3 | 3 | import datadog.trace.api.Config; |
4 | | -import java.util.concurrent.atomic.AtomicReference; |
5 | 4 | import javax.annotation.Nonnull; |
6 | 5 | import org.slf4j.Logger; |
7 | 6 | import org.slf4j.LoggerFactory; |
8 | 7 |
|
9 | 8 | public final class DebuggerConfigBridge { |
10 | 9 | private static final Logger LOGGER = LoggerFactory.getLogger(DebuggerConfigBridge.class); |
11 | 10 |
|
12 | | - private static final AtomicReference<DebuggerConfigUpdate> DEFERRED_UPDATE = |
13 | | - new AtomicReference<>(); |
| 11 | + private static DebuggerConfigUpdate DEFERRED_UPDATE; |
| 12 | + private static volatile DebuggerConfigUpdater UPDATER; |
14 | 13 |
|
15 | | - private static final AtomicReference<DebuggerConfigUpdater> UPDATER = new AtomicReference<>(); |
16 | | - |
17 | | - public static void updateConfig(DebuggerConfigUpdate update) { |
| 14 | + public static synchronized void updateConfig(DebuggerConfigUpdate update) { |
18 | 15 | if (!update.hasUpdates()) { |
19 | 16 | LOGGER.debug("No config update detected, skipping"); |
20 | 17 | return; |
21 | 18 | } |
22 | | - DebuggerConfigUpdater updater = UPDATER.get(); |
23 | | - if (updater != null) { |
| 19 | + if (UPDATER != null) { |
24 | 20 | LOGGER.debug("DebuggerConfigUpdater available, performing update: {}", update); |
25 | | - updater.updateConfig(update); |
| 21 | + UPDATER.updateConfig(update); |
26 | 22 | } else { |
27 | 23 | LOGGER.debug("DebuggerConfigUpdater not available, deferring update"); |
28 | | - DEFERRED_UPDATE.updateAndGet(existing -> DebuggerConfigUpdate.coalesce(existing, update)); |
| 24 | + DEFERRED_UPDATE = DebuggerConfigUpdate.coalesce(DEFERRED_UPDATE, update); |
29 | 25 | } |
30 | 26 | } |
31 | 27 |
|
32 | | - public static void setUpdater(@Nonnull DebuggerConfigUpdater updater) { |
33 | | - DebuggerConfigUpdater oldUpdater = UPDATER.getAndSet(updater); |
34 | | - if (oldUpdater == null) { |
35 | | - LOGGER.debug("DebuggerConfigUpdater set for first time, processing deferred updates"); |
36 | | - processDeferredUpdate(updater); |
| 28 | + public static synchronized void setUpdater(@Nonnull DebuggerConfigUpdater updater) { |
| 29 | + UPDATER = updater; |
| 30 | + if (DEFERRED_UPDATE != null && DEFERRED_UPDATE.hasUpdates()) { |
| 31 | + DebuggerConfigUpdate toApply = DEFERRED_UPDATE; |
| 32 | + DEFERRED_UPDATE = null; |
| 33 | + LOGGER.debug("Processing deferred update {}", toApply); |
| 34 | + updater.updateConfig(toApply); |
37 | 35 | } |
38 | 36 | } |
39 | 37 |
|
40 | 38 | // for testing purposes |
41 | 39 | static void reset() { |
42 | | - UPDATER.set(null); |
43 | | - DEFERRED_UPDATE.set(null); |
| 40 | + UPDATER = null; |
| 41 | + DEFERRED_UPDATE = null; |
44 | 42 | } |
45 | 43 |
|
46 | 44 | public static boolean isDynamicInstrumentationEnabled() { |
47 | | - DebuggerConfigUpdater updater = UPDATER.get(); |
48 | | - if (updater != null) { |
49 | | - return updater.isDynamicInstrumentationEnabled(); |
| 45 | + if (UPDATER != null) { |
| 46 | + return UPDATER.isDynamicInstrumentationEnabled(); |
50 | 47 | } |
51 | 48 | return Config.get().isDynamicInstrumentationEnabled(); |
52 | 49 | } |
53 | 50 |
|
54 | 51 | public static boolean isExceptionReplayEnabled() { |
55 | | - DebuggerConfigUpdater updater = UPDATER.get(); |
56 | | - if (updater != null) { |
57 | | - return updater.isExceptionReplayEnabled(); |
| 52 | + if (UPDATER != null) { |
| 53 | + return UPDATER.isExceptionReplayEnabled(); |
58 | 54 | } |
59 | 55 | return Config.get().isDebuggerExceptionEnabled(); |
60 | 56 | } |
61 | 57 |
|
62 | 58 | public static boolean isCodeOriginEnabled() { |
63 | | - DebuggerConfigUpdater updater = UPDATER.get(); |
64 | | - if (updater != null) { |
65 | | - return updater.isCodeOriginEnabled(); |
| 59 | + if (UPDATER != null) { |
| 60 | + return UPDATER.isCodeOriginEnabled(); |
66 | 61 | } |
67 | 62 | return Config.get().isDebuggerCodeOriginEnabled(); |
68 | 63 | } |
69 | 64 |
|
70 | 65 | public static boolean isDistributedDebuggerEnabled() { |
71 | | - DebuggerConfigUpdater updater = UPDATER.get(); |
72 | | - if (updater != null) { |
73 | | - return updater.isDistributedDebuggerEnabled(); |
| 66 | + if (UPDATER != null) { |
| 67 | + return UPDATER.isDistributedDebuggerEnabled(); |
74 | 68 | } |
75 | 69 | return Config.get().isDistributedDebuggerEnabled(); |
76 | 70 | } |
77 | | - |
78 | | - private static void processDeferredUpdate(DebuggerConfigUpdater updater) { |
79 | | - DebuggerConfigUpdate deferredUpdate = DEFERRED_UPDATE.getAndSet(null); |
80 | | - if (deferredUpdate != null) { |
81 | | - updater.updateConfig(deferredUpdate); |
82 | | - LOGGER.debug("Processed deferred update {}", deferredUpdate); |
83 | | - } |
84 | | - } |
85 | 71 | } |
0 commit comments