Skip to content
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

Update PropertyWrapper to be thread safe. #470

Merged
merged 1 commit into from
Nov 11, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
*/
package com.netflix.config;

import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -38,7 +39,7 @@ public abstract class PropertyWrapper<V> implements Property<V> {
private static final IdentityHashMap<Class<? extends PropertyWrapper>, Object> SUBCLASSES_WITH_NO_CALLBACK
= new IdentityHashMap<Class<? extends PropertyWrapper>, Object>();
private static final Logger logger = LoggerFactory.getLogger(PropertyWrapper.class);
private final List<Runnable> callbackList = Lists.newArrayList();
private final Set<Runnable> callbacks = new CopyOnWriteArraySet<Runnable>();

static {
PropertyWrapper.registerSubClassWithNoCallback(DynamicIntProperty.class);
Expand Down Expand Up @@ -81,7 +82,7 @@ public void run() {
}
};
this.prop.addCallback(callback);
callbackList.add(callback);
callbacks.add(callback);
this.prop.addValidator(new PropertyChangeValidator() {
@Override
public void validate(String newValue) {
Expand Down Expand Up @@ -149,7 +150,7 @@ public long getChangedTimestamp() {
public void addCallback(Runnable callback) {
if (callback != null) {
prop.addCallback(callback);
callbackList.add(callback);
callbacks.add(callback);
}
}

Expand All @@ -158,9 +159,11 @@ public void addCallback(Runnable callback) {
*/
@Override
public void removeAllCallbacks() {
for (Runnable callback: callbackList) {
final Set<Runnable> callbacksToRemove = new HashSet<Runnable>(callbacks);
for (Runnable callback : callbacksToRemove) {
prop.removeCallback(callback);
}
callbacks.removeAll(callbacksToRemove);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not calling callbacks.clear() here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since other threads could add new callbacks while removeAllCallbacks is processing, this is just extra protection to prevent removing callbacks from the set that haven't had removeCallback(Runnable) called on them.

}

/**
Expand Down