-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[CURATOR-477] added support for turning off zk watches #278
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d4a0d95
added support for zkwatches
ramaraochavali 88df79b
address review feedback
ramaraochavali 60e9be8
fix test case
ramaraochavali 53c4770
address review comments
ramaraochavali 7680e15
reorder imports
ramaraochavali b7c902a
added for exists watcher also
ramaraochavali 7e9628e
use maybeWatch
ramaraochavali File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,8 +27,11 @@ | |
| import org.apache.curator.framework.CuratorFramework; | ||
| import org.apache.curator.framework.WatcherRemoveCuratorFramework; | ||
| import org.apache.curator.framework.api.BackgroundCallback; | ||
| import org.apache.curator.framework.api.BackgroundPathable; | ||
| import org.apache.curator.framework.api.CuratorEvent; | ||
| import org.apache.curator.framework.api.Pathable; | ||
| import org.apache.curator.framework.api.UnhandledErrorListener; | ||
| import org.apache.curator.framework.api.Watchable; | ||
| import org.apache.curator.framework.listen.Listenable; | ||
| import org.apache.curator.framework.listen.ListenerContainer; | ||
| import org.apache.curator.framework.state.ConnectionState; | ||
|
|
@@ -73,6 +76,7 @@ public class TreeCache implements Closeable | |
| { | ||
| private static final Logger LOG = LoggerFactory.getLogger(TreeCache.class); | ||
| private final boolean createParentNodes; | ||
| private final boolean disableZkWatches; | ||
| private final TreeCacheSelector selector; | ||
|
|
||
| public static final class Builder | ||
|
|
@@ -84,6 +88,7 @@ public static final class Builder | |
| private ExecutorService executorService = null; | ||
| private int maxDepth = Integer.MAX_VALUE; | ||
| private boolean createParentNodes = false; | ||
| private boolean disableZkWatches = false; | ||
| private TreeCacheSelector selector = new DefaultTreeCacheSelector(); | ||
|
|
||
| private Builder(CuratorFramework client, String path) | ||
|
|
@@ -102,7 +107,7 @@ public TreeCache build() | |
| { | ||
| executor = Executors.newSingleThreadExecutor(defaultThreadFactory); | ||
| } | ||
| return new TreeCache(client, path, cacheData, dataIsCompressed, maxDepth, executor, createParentNodes, selector); | ||
| return new TreeCache(client, path, cacheData, dataIsCompressed, maxDepth, executor, createParentNodes, disableZkWatches, selector); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -165,6 +170,18 @@ public Builder setCreateParentNodes(boolean createParentNodes) | |
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * By default, TreeCache creates {@link org.apache.zookeeper.ZooKeeper} watches for every created path. | ||
| * Change this behavior with this method. | ||
| * @param disableZkWatches true to disable zk watches | ||
| * @return this for chaining | ||
| */ | ||
| public Builder disableZkWatches(boolean disableZkWatches) | ||
| { | ||
| this.disableZkWatches = disableZkWatches; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * By default, {@link DefaultTreeCacheSelector} is used. Change the selector here. | ||
| * | ||
|
|
@@ -253,7 +270,7 @@ private void doRefreshChildren() throws Exception | |
| { | ||
| if ( treeState.get() == TreeState.STARTED ) | ||
| { | ||
| client.getChildren().usingWatcher(this).inBackground(this).forPath(path); | ||
| maybeWatch(client.getChildren()).forPath(path); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -263,15 +280,24 @@ private void doRefreshData() throws Exception | |
| { | ||
| if ( dataIsCompressed ) | ||
| { | ||
| client.getData().decompressed().usingWatcher(this).inBackground(this).forPath(path); | ||
| maybeWatch(client.getData().decompressed()).forPath(path); | ||
| } | ||
| else | ||
| { | ||
| client.getData().usingWatcher(this).inBackground(this).forPath(path); | ||
| maybeWatch(client.getData()).forPath(path); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private <T, P extends Watchable<BackgroundPathable<T>> & BackgroundPathable<T>> Pathable<T> maybeWatch( | ||
| P dataBuilder) { | ||
| if (disableZkWatches) { | ||
| return dataBuilder.inBackground(this); | ||
| } else { | ||
| return dataBuilder.usingWatcher(this).inBackground(this); | ||
| } | ||
| } | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. private void doRefreshChildren() throws Exception
{
if ( treeState.get() == TreeState.STARTED )
{
maybeWatch(client.getChildren()).forPath(path);
}
}
private void doRefreshData() throws Exception
{
if ( treeState.get() == TreeState.STARTED )
{
if ( dataIsCompressed )
{
maybeWatch(client.getData().decompressed()).forPath(path);
}
else
{
maybeWatch(client.getData()).forPath(path);
}
}
}
private <T, P extends Watchable<BackgroundPathable<T>> & BackgroundPathable<T>> Pathable<T> maybeWatch(P dataBuilder)
{
if ( disableZkWatches )
{
return dataBuilder.inBackground(this);
}
else
{
return dataBuilder.usingWatcher(this).inBackground(this);
}
} |
||
| void wasReconnected() throws Exception | ||
| { | ||
| refresh(); | ||
|
|
@@ -321,7 +347,7 @@ void wasDeleted() throws Exception | |
| if ( parent == null ) | ||
| { | ||
| // Root node; use an exist query to watch for existence. | ||
| client.checkExists().usingWatcher(this).inBackground(this).forPath(path); | ||
| maybeWatch(client.checkExists()).forPath(path); | ||
| } | ||
| else | ||
| { | ||
|
|
@@ -535,7 +561,7 @@ public void stateChanged(CuratorFramework client, ConnectionState newState) | |
| */ | ||
| public TreeCache(CuratorFramework client, String path) | ||
| { | ||
| this(client, path, true, false, Integer.MAX_VALUE, Executors.newSingleThreadExecutor(defaultThreadFactory), false, new DefaultTreeCacheSelector()); | ||
| this(client, path, true, false, Integer.MAX_VALUE, Executors.newSingleThreadExecutor(defaultThreadFactory), false, false, new DefaultTreeCacheSelector()); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -545,9 +571,10 @@ public TreeCache(CuratorFramework client, String path) | |
| * @param dataIsCompressed if true, data in the path is compressed | ||
| * @param executorService Closeable ExecutorService to use for the TreeCache's background thread | ||
| * @param createParentNodes true to create parent nodes as containers | ||
| * @param disableZkWatches true to disable Zookeeper watches | ||
| * @param selector the selector to use | ||
| */ | ||
| TreeCache(CuratorFramework client, String path, boolean cacheData, boolean dataIsCompressed, int maxDepth, final ExecutorService executorService, boolean createParentNodes, TreeCacheSelector selector) | ||
| TreeCache(CuratorFramework client, String path, boolean cacheData, boolean dataIsCompressed, int maxDepth, final ExecutorService executorService, boolean createParentNodes, boolean disableZkWatches, TreeCacheSelector selector) | ||
| { | ||
| this.createParentNodes = createParentNodes; | ||
| this.selector = Preconditions.checkNotNull(selector, "selector cannot be null"); | ||
|
|
@@ -557,6 +584,7 @@ public TreeCache(CuratorFramework client, String path) | |
| this.cacheData = cacheData; | ||
| this.dataIsCompressed = dataIsCompressed; | ||
| this.maxDepth = maxDepth; | ||
| this.disableZkWatches = disableZkWatches; | ||
| this.executorService = Preconditions.checkNotNull(executorService, "executorService cannot be null"); | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and e.g. disableZkWatches(boolean)