Skip to content

Commit

Permalink
Merge pull request #1 from tisonkun/patch
Browse files Browse the repository at this point in the history
avoid mutable state
  • Loading branch information
kezhuw authored Jan 16, 2025
2 parents 6b78a3b + 5cf5e1c commit 9cc6766
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
public class GetConfigBuilderImpl
implements GetConfigBuilder, BackgroundOperation<Void>, ErrorListenerEnsembleable<byte[]> {
private final CuratorFrameworkImpl client;
private final WatcherRemovalManager watcherRemovalManager;

private Backgrounding backgrounding;
private Watching watching;
Expand All @@ -45,14 +44,8 @@ public GetConfigBuilderImpl(CuratorFrameworkImpl client) {

public GetConfigBuilderImpl(CuratorFrameworkImpl client, Backgrounding backgrounding, Watcher watcher, Stat stat) {
this.client = (CuratorFrameworkImpl) client.usingNamespace(null);
this.watcherRemovalManager = client.getWatcherRemovalManager();
this.backgrounding = backgrounding;
// We are using `client.usingNamespace(null)` to avoid `unfixNamespace` for "/zookeeper/config"(CURATOR-667)
// events. But `client.usingNamespace(null)` will loss possible `WatcherRemovalManager`(CURATOR-710). So, let's
// reset it.
//
// See also `NamespaceWatchedEvent`.
this.watching = new Watching(this.client, watcher).setWatcherRemovalManager(watcherRemovalManager);
this.watching = new Watching(this.client, watcher);
this.stat = stat;
}

Expand Down Expand Up @@ -115,19 +108,19 @@ public BackgroundEnsembleable<byte[]> usingWatcher(CuratorWatcher watcher) {

@Override
public BackgroundEnsembleable<byte[]> watched() {
watching = new Watching(client, true).setWatcherRemovalManager(watcherRemovalManager);
watching = new Watching(client, true);
return new InternalBackgroundEnsembleable();
}

@Override
public BackgroundEnsembleable<byte[]> usingWatcher(Watcher watcher) {
watching = new Watching(client, watcher).setWatcherRemovalManager(watcherRemovalManager);
watching = new Watching(client, watcher);
return new InternalBackgroundEnsembleable();
}

@Override
public BackgroundEnsembleable<byte[]> usingWatcher(CuratorWatcher watcher) {
watching = new Watching(client, watcher).setWatcherRemovalManager(watcherRemovalManager);
watching = new Watching(client, watcher);
return new InternalBackgroundEnsembleable();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ class WatcherRemovalFacade extends CuratorFrameworkImpl implements WatcherRemove
private final WatcherRemovalManager removalManager;

WatcherRemovalFacade(CuratorFrameworkImpl client) {
this(client, new WatcherRemovalManager(client));
}

private WatcherRemovalFacade(CuratorFrameworkImpl client, WatcherRemovalManager removalManager) {
super(client);
this.client = client;
removalManager = new WatcherRemovalManager(client);
this.removalManager = removalManager;
}

@Override
Expand Down Expand Up @@ -73,7 +77,8 @@ public CuratorFramework nonNamespaceView() {

@Override
public CuratorFramework usingNamespace(String newNamespace) {
return client.usingNamespace(newNamespace);
final CuratorFrameworkImpl newClient = (CuratorFrameworkImpl) client.usingNamespace(newNamespace);
return new WatcherRemovalFacade(newClient, removalManager);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,46 +28,36 @@ public class Watching {
private final CuratorWatcher curatorWatcher;
private final boolean watched;
private final CuratorFrameworkImpl client;
private WatcherRemovalManager watcherRemovalManager;
private NamespaceWatcher namespaceWatcher;

public Watching(CuratorFrameworkImpl client, boolean watched) {
this.client = client;
this.watcherRemovalManager = client.getWatcherRemovalManager();
this.watcher = null;
this.curatorWatcher = null;
this.watched = watched;
}

public Watching(CuratorFrameworkImpl client, Watcher watcher) {
this.client = client;
this.watcherRemovalManager = client.getWatcherRemovalManager();
this.watcher = watcher;
this.curatorWatcher = null;
this.watched = false;
}

public Watching(CuratorFrameworkImpl client, CuratorWatcher watcher) {
this.client = client;
this.watcherRemovalManager = client.getWatcherRemovalManager();
this.watcher = null;
this.curatorWatcher = watcher;
this.watched = false;
}

public Watching(CuratorFrameworkImpl client) {
this.client = client;
this.watcherRemovalManager = client.getWatcherRemovalManager();
watcher = null;
watched = false;
curatorWatcher = null;
}

Watching setWatcherRemovalManager(WatcherRemovalManager watcherRemovalManager) {
this.watcherRemovalManager = watcherRemovalManager;
return this;
}

Watcher getWatcher(String unfixedPath) {
namespaceWatcher = null;
if (watcher != null) {
Expand Down Expand Up @@ -95,8 +85,8 @@ void commitWatcher(int rc, boolean isExists) {
doCommit = (rc == KeeperException.Code.OK.intValue());
}

if (doCommit && namespaceWatcher != null && watcherRemovalManager != null) {
watcherRemovalManager.add(namespaceWatcher);
if (doCommit && namespaceWatcher != null && client.getWatcherRemovalManager() != null) {
client.getWatcherRemovalManager().add(namespaceWatcher);
}
}
}

0 comments on commit 9cc6766

Please sign in to comment.