Skip to content

Optimize single-node CuratorCache storage with AtomicReference - #1306

Open
waterWang wants to merge 3 commits into
apache:masterfrom
waterWang:fix/single-node-cache-storage
Open

Optimize single-node CuratorCache storage with AtomicReference#1306
waterWang wants to merge 3 commits into
apache:masterfrom
waterWang:fix/single-node-cache-storage

Conversation

@waterWang

Copy link
Copy Markdown

Summary

When a CuratorCache is built with the SINGLE_NODE_CACHE option, only one node is ever cached (the root path). Today the cache still uses a ConcurrentHashMap-backed StandardCuratorCacheStorage, which is overkill and wastes memory for this case. This PR adds a dedicated SingleNodeCuratorCacheStorage implementation backed by a single AtomicReference and switches the default storage selection to it whenever SINGLE_NODE_CACHE is set.

Fixes #1304

Changes

  • New SingleNodeCuratorCacheStorage — implements CuratorCacheStorage with a single AtomicReference<ChildData>. All interface methods (put, remove, get, size, stream, clear) behave identically to StandardCuratorCacheStorage for the single-node case:
    • size() returns 0 or 1
    • stream() yields at most one element
    • put/get/remove match on the stored node's path, so a single-node cache can never hold entries for multiple paths.
    • Honors the cacheBytes flag the same way StandardCuratorCacheStorage does.
  • CuratorCacheStorage.java — adds two factory methods: singleNode() and singleNodeDataNotCached().
  • CuratorCacheImpl.java — the constructor now selects CuratorCacheStorage.singleNode() (instead of standard()) when SINGLE_NODE_CACHE is set and no explicit storage was provided by the caller. Calling builder(client, path).withStorage(...) still takes precedence.

Behavioral compatibility

  • CuratorCacheStorage is a public interface and the storage is only switched when the caller did not supply a custom storage. Explicit withStorage() calls are unchanged.
  • The SINGLE_NODE_CACHE option 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 with SINGLE_NODE_CACHE and verify get/size/stream semantics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optimize storage for single-node CuratorCache

1 participant