Skip to content

feat: add CacheReader for shared non-blocking reads and resync - #2

Closed
bohutang wants to merge 1 commit into
mainfrom
feat/cache-reader
Closed

bohutang wants to merge 1 commit into
mainfrom
feat/cache-reader

Conversation

@bohutang

@bohutang bohutang commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Summary

Databend query nodes want to answer hot meta-service reads (users, global settings, cluster nodes) from a Cache. Two things block that today:

  • Reads need &mut self, so concurrent readers must wrap the cache in a Mutex and fall back to the remote store whenever try_lock fails; under load a large share of reads miss the cache this way.
  • A watch stream that silently stops delivering (half-open connection, lagging follower) never errors, so the cache stays stale and the caller cannot force a re-fetch.

Changes:

Change Detail
RwLock instead of Mutex readers share the data; the watcher still holds it exclusively during (re)initialization and per change
Cache::reader() → CacheReader cloneable; try_access_now() never waits and returns None while the watcher holds the data exclusively
CacheReader::request_resync() the watcher drops the stream, resets the data and re-fetches the full range
Reconnect jitter backoff and post-error delay scaled by a random factor in [0.5, 1.5) to avoid lockstep reconnects
Version 0.2.1 → 0.3.0 (cache_data() now returns an RwLockWriteGuard)

Tests: tests/reader_resync.rs covers shared reads under concurrency and recovery of a stalled stream via request_resync() (new test helper TestSource::set_silently changes data without notifying subscribers). All existing tests pass; clippy and fmt are clean.

Used by databendlabs/databend#21095 (meta read cache).

Replace the internal Mutex with an RwLock and add Cache::reader(), a cloneable
handle whose try_access_now() never waits: it returns None while the watcher
holds the data exclusively (initial fetch, reconnect, applying a change), so
callers can fall back to the remote store instead of queueing behind it.

CacheReader::request_resync() makes the watcher drop its stream and re-fetch
everything; callers use it when they detect that a stream silently stopped
delivering, e.g. a half-open connection.

Reconnect delays get a random factor in [0.5, 1.5) so that many caches seeing
the same failure do not retry in lockstep. Bump version to 0.3.0.
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.

1 participant