Skip to content

feat: cache: read through &self, and without waiting - #8

Merged
drmingdrmer merged 1 commit into
databendlabs:mainfrom
drmingdrmer:cache-non-blocking-self-reads
Nov 18, 2026
Merged

drmingdrmer merged 1 commit into
databendlabs:mainfrom
drmingdrmer:cache-non-blocking-self-reads

Conversation

@drmingdrmer

Copy link
Copy Markdown
Member

Changelog

feat: cache: read through &self, and without waiting

Summary

Cache reads take &self, and the data is behind a tokio RwLock
instead of a Mutex, so concurrent readers share a cache, by reference
or Arc, without an outer lock. The new try_access_now,
try_get_now and try_list_dir_now never wait: they return None
while the watcher holds the data, as while it initializes or
reconnects.

Details

The watcher holds the lock exclusively, briefly to apply each change,
and for the whole reset-and-refetch of an initialization. The waiting
reads, try_get, try_list_dir, try_last_seq and try_access, wait
for it as before. The non-waiting reads use RwLock::try_read, which is
not a future: a lock polled once with now_or_never() returned None
whenever the task's tokio coop budget was spent, even with the lock
free, and its caller read the remote store for no reason.
Some(Err(_)) still reports a source that cannot be subscribed.

Upgrade tip:

cache_data() takes &self and returns a RwLockReadGuard instead of
a MutexGuard. A caller that kept a cache behind a mutex only to read
it can drop the mutex.

Fixes: #4


  • New Feature

# Summary

`Cache` reads take `&self`, and the data is behind a tokio `RwLock`
instead of a `Mutex`, so concurrent readers share a cache, by reference
or `Arc`, without an outer lock. The new `try_access_now`,
`try_get_now` and `try_list_dir_now` never wait: they return `None`
while the watcher holds the data, as while it initializes or
reconnects.

# Details

The watcher holds the lock exclusively, briefly to apply each change,
and for the whole reset-and-refetch of an initialization. The waiting
reads, `try_get`, `try_list_dir`, `try_last_seq` and `try_access`, wait
for it as before. The non-waiting reads use `RwLock::try_read`, which is
not a future: a lock polled once with `now_or_never()` returned `None`
whenever the task's tokio coop budget was spent, even with the lock
free, and its caller read the remote store for no reason.
`Some(Err(_))` still reports a source that cannot be subscribed.

Upgrade tip:

`cache_data()` takes `&self` and returns a `RwLockReadGuard` instead of
a `MutexGuard`. A caller that kept a cache behind a mutex only to read
it can drop the mutex.

Fixes: databendlabs#4

@xp-trumpet xp-trumpet left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xp-trumpet reviewed 9 files and all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on drmingdrmer).

@drmingdrmer
drmingdrmer merged commit 34593a4 into databendlabs:main Nov 18, 2026
6 checks passed
@drmingdrmer
drmingdrmer deleted the cache-non-blocking-self-reads branch November 18, 2026 10:31
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.

Cache: non-blocking &self reads, so that callers need no mutex

2 participants