Skip to content

Commit

Permalink
Avoid repeated calls to getState in AbstractDependentConfig
Browse files Browse the repository at this point in the history
The getState method was called repeatedly in various AbstractDependentConfig methods, which could
incur repeated volatile reads or correctness issues in edge cases.
  • Loading branch information
kilink committed Sep 23, 2024
1 parent e9bbde9 commit 592bcd9
Showing 1 changed file with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.netflix.archaius.config;

import com.netflix.archaius.api.Config;
import com.netflix.archaius.api.PropertyDetails;

import java.util.Iterator;
import java.util.Map;
import java.util.function.BiConsumer;

/**
Expand All @@ -27,9 +29,11 @@ public AbstractDependentConfig() {

@Override
public Object getRawProperty(String key) {
Object value = getState().getData().get(key);
if (getState().getInstrumentedKeys().containsKey(key)) {
getState().getInstrumentedKeys().get(key).recordUsage(createPropertyDetails(key, value));
CachedState state = getState();
Object value = state.getData().get(key);
Config config = state.getInstrumentedKeys().get(key);
if (config != null) {
config.recordUsage(createPropertyDetails(key, value));
}
return value;
}
Expand All @@ -52,9 +56,11 @@ public Iterable<String> keys() {

@Override
public void forEachProperty(BiConsumer<String, Object> consumer) {
getState().getData().forEach((k, v) -> {
if (getState().getInstrumentedKeys().containsKey(k)) {
getState().getInstrumentedKeys().get(k).recordUsage(createPropertyDetails(k, v));
CachedState state = getState();
state.getData().forEach((k, v) -> {
Config config = state.getInstrumentedKeys().get(k);
if (config != null) {
config.recordUsage(createPropertyDetails(k, v));
}
consumer.accept(k, v);
});
Expand All @@ -77,8 +83,10 @@ public boolean isEmpty() {

@Override
public void recordUsage(PropertyDetails propertyDetails) {
if (getState().getInstrumentedKeys().containsKey(propertyDetails.getKey())) {
getState().getInstrumentedKeys().get(propertyDetails.getKey()).recordUsage(createPropertyDetails(propertyDetails.getKey(), propertyDetails.getValue()));
CachedState state = getState();
Config config = state.getInstrumentedKeys().get(propertyDetails.getKey());
if (config != null) {
config.recordUsage(createPropertyDetails(propertyDetails.getKey(), propertyDetails.getValue()));
}
}

Expand Down

0 comments on commit 592bcd9

Please sign in to comment.