Optimize single-node CuratorCache storage with AtomicReference - #1306
Open
waterWang wants to merge 3 commits into
Open
Optimize single-node CuratorCache storage with AtomicReference#1306waterWang wants to merge 3 commits into
waterWang wants to merge 3 commits into
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
When a
CuratorCacheis built with theSINGLE_NODE_CACHEoption, only one node is ever cached (the root path). Today the cache still uses aConcurrentHashMap-backedStandardCuratorCacheStorage, which is overkill and wastes memory for this case. This PR adds a dedicatedSingleNodeCuratorCacheStorageimplementation backed by a singleAtomicReferenceand switches the default storage selection to it wheneverSINGLE_NODE_CACHEis set.Fixes #1304
Changes
SingleNodeCuratorCacheStorage— implementsCuratorCacheStoragewith a singleAtomicReference<ChildData>. All interface methods (put,remove,get,size,stream,clear) behave identically toStandardCuratorCacheStoragefor the single-node case:size()returns0or1stream()yields at most one elementput/get/removematch on the stored node's path, so a single-node cache can never hold entries for multiple paths.cacheBytesflag the same wayStandardCuratorCacheStoragedoes.CuratorCacheStorage.java— adds two factory methods:singleNode()andsingleNodeDataNotCached().CuratorCacheImpl.java— the constructor now selectsCuratorCacheStorage.singleNode()(instead ofstandard()) whenSINGLE_NODE_CACHEis set and no explicit storage was provided by the caller. Callingbuilder(client, path).withStorage(...)still takes precedence.Behavioral compatibility
CuratorCacheStorageis a public interface and the storage is only switched when the caller did not supply a custom storage. ExplicitwithStorage()calls are unchanged.SINGLE_NODE_CACHEoption previously only controlled watcher recursion (recursive = !options.contains(SINGLE_NODE_CACHE)); this PR additionally optimizes the backing store without changing any observable semantics —get,size,stream, and events all behave the same.Test plan
Existing
TestCuratorCache(incl.testClearOnClose,testGreaterThan64kZNodes) and related cache tests cover the standard path and should remain green. Single-node behavior is exercised by tests that build caches withSINGLE_NODE_CACHEand verifyget/size/stream semantics.