Skip to content

Commit 08c80b4

Browse files
committed
apply all changes from PR #9327, accomodated to master
1 parent 2fa9986 commit 08c80b4

File tree

14 files changed

+797
-179
lines changed

14 files changed

+797
-179
lines changed

dd-smoke-tests/log-injection/src/main/java/datadog/smoketest/loginjection/BaseApplication.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ public void run() throws InterruptedException {
4444
}
4545

4646
private static Object getLogInjectionEnabled() {
47-
ConfigSetting configSetting = ConfigCollector.get().collect().get(LOGS_INJECTION_ENABLED);
47+
ConfigSetting configSetting =
48+
ConfigCollector.get().getAppliedConfigSetting(LOGS_INJECTION_ENABLED);
4849
if (configSetting == null) {
4950
return null;
5051
}

internal-api/src/main/java/datadog/trace/api/Config.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5219,6 +5219,7 @@ private static boolean isWindowsOS() {
52195219
private static String getEnv(String name) {
52205220
String value = EnvironmentVariables.get(name);
52215221
if (value != null) {
5222+
// TODO: Report seqID?
52225223
ConfigCollector.get().put(name, value, ConfigOrigin.ENV);
52235224
}
52245225
return value;
@@ -5242,6 +5243,7 @@ private static String getProp(String name) {
52425243
private static String getProp(String name, String def) {
52435244
String value = SystemProperties.getOrDefault(name, def);
52445245
if (value != null) {
5246+
// TODO: report seqId?
52455247
ConfigCollector.get().put(name, value, ConfigOrigin.JVM_PROP);
52465248
}
52475249
return value;

internal-api/src/main/java/datadog/trace/api/ConfigCollector.java

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,42 +16,52 @@ public class ConfigCollector {
1616
private static final AtomicReferenceFieldUpdater<ConfigCollector, Map> COLLECTED_UPDATER =
1717
AtomicReferenceFieldUpdater.newUpdater(ConfigCollector.class, Map.class, "collected");
1818

19-
private volatile Map<String, ConfigSetting> collected = new ConcurrentHashMap<>();
19+
private volatile Map<ConfigOrigin, Map<String, ConfigSetting>> collected =
20+
new ConcurrentHashMap<>();
2021

2122
public static ConfigCollector get() {
2223
return INSTANCE;
2324
}
2425

2526
public void put(String key, Object value, ConfigOrigin origin) {
2627
ConfigSetting setting = ConfigSetting.of(key, value, origin);
27-
collected.put(key, setting);
28+
Map<String, ConfigSetting> configMap =
29+
collected.computeIfAbsent(origin, k -> new ConcurrentHashMap<>());
30+
configMap.put(key, setting); // replaces any previous value for this key at origin
31+
}
32+
33+
public void put(String key, Object value, ConfigOrigin origin, int seqId) {
34+
ConfigSetting setting = ConfigSetting.of(key, value, origin, seqId);
35+
Map<String, ConfigSetting> configMap =
36+
collected.computeIfAbsent(origin, k -> new ConcurrentHashMap<>());
37+
configMap.put(key, setting); // replaces any previous value for this key at origin
2838
}
2939

3040
public void putAll(Map<String, Object> keysAndValues, ConfigOrigin origin) {
31-
// attempt merge+replace to avoid collector seeing partial update
32-
Map<String, ConfigSetting> merged =
33-
new ConcurrentHashMap<>(keysAndValues.size() + collected.size());
3441
for (Map.Entry<String, Object> entry : keysAndValues.entrySet()) {
35-
ConfigSetting setting = ConfigSetting.of(entry.getKey(), entry.getValue(), origin);
36-
merged.put(entry.getKey(), setting);
37-
}
38-
while (true) {
39-
Map<String, ConfigSetting> current = collected;
40-
current.forEach(merged::putIfAbsent);
41-
if (COLLECTED_UPDATER.compareAndSet(this, current, merged)) {
42-
break; // success
43-
}
44-
// roll back to original update before next attempt
45-
merged.keySet().retainAll(keysAndValues.keySet());
42+
put(entry.getKey(), entry.getValue(), origin);
4643
}
4744
}
4845

4946
@SuppressWarnings("unchecked")
50-
public Map<String, ConfigSetting> collect() {
47+
public Map<ConfigOrigin, Map<String, ConfigSetting>> collect() {
5148
if (!collected.isEmpty()) {
5249
return COLLECTED_UPDATER.getAndSet(this, new ConcurrentHashMap<>());
5350
} else {
5451
return Collections.emptyMap();
5552
}
5653
}
54+
55+
public ConfigSetting getAppliedConfigSetting(String key) {
56+
ConfigSetting best = null;
57+
for (Map<String, ConfigSetting> configMap : collected.values()) {
58+
ConfigSetting setting = configMap.get(key);
59+
if (setting != null) {
60+
if (best == null || setting.seqId > best.seqId) {
61+
best = setting;
62+
}
63+
}
64+
}
65+
return best;
66+
}
5767
}

internal-api/src/main/java/datadog/trace/api/ConfigSetting.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,28 @@ public final class ConfigSetting {
1111
public final String key;
1212
public final Object value;
1313
public final ConfigOrigin origin;
14+
public final int seqId;
15+
16+
public static final int DEFAULT_SEQ_ID = 1;
17+
private static final int ABSENT_SEQ_ID = 0;
1418

1519
private static final Set<String> CONFIG_FILTER_LIST =
1620
new HashSet<>(
1721
Arrays.asList("DD_API_KEY", "dd.api-key", "dd.profiling.api-key", "dd.profiling.apikey"));
1822

1923
public static ConfigSetting of(String key, Object value, ConfigOrigin origin) {
20-
return new ConfigSetting(key, value, origin);
24+
return new ConfigSetting(key, value, origin, ABSENT_SEQ_ID);
25+
}
26+
27+
public static ConfigSetting of(String key, Object value, ConfigOrigin origin, int seqId) {
28+
return new ConfigSetting(key, value, origin, seqId);
2129
}
2230

23-
private ConfigSetting(String key, Object value, ConfigOrigin origin) {
31+
private ConfigSetting(String key, Object value, ConfigOrigin origin, int seqId) {
2432
this.key = key;
2533
this.value = CONFIG_FILTER_LIST.contains(key) ? "<hidden>" : value;
2634
this.origin = origin;
35+
this.seqId = seqId;
2736
}
2837

2938
public String normalizedKey() {
@@ -99,12 +108,15 @@ public boolean equals(Object o) {
99108
if (this == o) return true;
100109
if (o == null || getClass() != o.getClass()) return false;
101110
ConfigSetting that = (ConfigSetting) o;
102-
return key.equals(that.key) && Objects.equals(value, that.value) && origin == that.origin;
111+
return key.equals(that.key)
112+
&& Objects.equals(value, that.value)
113+
&& origin == that.origin
114+
&& seqId == that.seqId;
103115
}
104116

105117
@Override
106118
public int hashCode() {
107-
return Objects.hash(key, value, origin);
119+
return Objects.hash(key, value, origin, seqId);
108120
}
109121

110122
@Override
@@ -117,6 +129,8 @@ public String toString() {
117129
+ stringValue()
118130
+ ", origin="
119131
+ origin
132+
+ ", seqId="
133+
+ seqId
120134
+ '}';
121135
}
122136
}

0 commit comments

Comments
 (0)