Skip to content

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

Closed

Description

@drmingdrmer

Summary

Cache reads (try_get, try_list_dir, try_access) take &mut self, so every caller that shares a Cache wraps it in a mutex. A caller that must not wait, as the meta-service watch caches of databend query nodes (databendlabs/databend#21110), holds a sync mutex just to poll try_access once with now_or_never(), and reads the remote store on None.

That has two costs:

  • Concurrent readers take turns on the outer mutex, though every read is read-only.
  • The poll returns None whenever the task's tokio coop budget is spent, even with the lock free: tokio::sync::Mutex::lock polls batch_semaphore::Acquire, whose poll calls coop::poll_proceed before it looks at the permits (tokio 1.54.1, src/sync/batch_semaphore.rs). Such a read goes to the remote store for no reason.

Proposal

A non-blocking &self read that admits concurrent readers, e.g. on an RwLock with try_read, and answers None while the cache initializes or reconnects. Callers can then drop their mutex and the now_or_never() poll.

#2 tried RwLock reads, together with a resync, and was closed without merging.

The callers in databend: Cache::try_get_ready and Cache::try_list_dir_ready in src/meta/plugins/cache/src/cache.rs, used through the parking_lot::Mutex<Cache> fields of UserMgr, SettingMgr and WarehouseMgr.

From databendlabs/databend#21106.

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions