Skip to content

Commit

Permalink
Fix race condition with an AtomicReference
Browse files Browse the repository at this point in the history
  • Loading branch information
akang31 committed Jan 19, 2024
1 parent 90be839 commit 1f16928
Showing 1 changed file with 7 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.HashSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicReference;

import org.apache.commons.configuration.AbstractConfiguration;
import org.apache.commons.configuration.Configuration;
Expand Down Expand Up @@ -109,21 +110,18 @@ public class ConcurrentCompositeConfiguration extends ConcurrentMapConfiguration
private final boolean enableStackTrace = Boolean.parseBoolean(System.getProperty(ENABLE_STACK_TRACE));
private final boolean enableInstrumentation = Boolean.parseBoolean(System.getProperty(ENABLE_INSTRUMENTATION));

private Map<String, AbstractConfiguration> namedConfigurations = new ConcurrentHashMap<String, AbstractConfiguration>();
private Map<String, AbstractConfiguration> namedConfigurations = new ConcurrentHashMap<>();

private final Map<String, Integer> stackTraces = new ConcurrentHashMap<>();
private final Set<String> usedProperties = ConcurrentHashMap.newKeySet();
private final AtomicReference<Set<String>> usedPropertiesRef = new AtomicReference<>(ConcurrentHashMap.newKeySet());

public Set<String> getUsedProperties() {
return Collections.unmodifiableSet(new HashSet<>(usedProperties));
return Collections.unmodifiableSet(new HashSet<>(usedPropertiesRef.get()));
}

public Set<String> getAndClearUsedProperties() {
synchronized (usedProperties) {
Set<String> ret = getUsedProperties();
usedProperties.clear();
return ret;
}
Set<String> ret = usedPropertiesRef.getAndSet(ConcurrentHashMap.newKeySet());
return Collections.unmodifiableSet(new HashSet<>(ret));
}

private List<AbstractConfiguration> configList = new CopyOnWriteArrayList<AbstractConfiguration>();
Expand Down Expand Up @@ -590,7 +588,7 @@ private Object getProperty(String key, boolean instrument)
*/
public void recordUsage(String key) {
if (enableInstrumentation) {
usedProperties.add(key);
usedPropertiesRef.get().add(key);
if (enableStackTrace) {
String trace = Arrays.toString(Thread.currentThread().getStackTrace());
stackTraces.merge(trace, 1, (v1, v2) -> v1 + 1);
Expand Down

0 comments on commit 1f16928

Please sign in to comment.