Repository navigation
Description
The file metadata cache of the storage drivers uses go-micro's memory store by default (STORAGE_USERS_FILEMETADATA_CACHE_STORE=memory in opencloud). Two findings from reading the code, low priority, but worth keeping track of:
1. The cache has no size limit
The memory store is a thin wrapper around github.com/patrickmn/go-cache (go-micro.dev/v4/store/memory.go), a plain map without a size limit. Entries are only removed by the TTL: cacheStore.PushToCache (pkg/storage/cache/cache.go:193) gives every entry an expiry of cache_ttl, 24 hours by default (STORAGE_USERS_FILEMETADATA_CACHE_TTL), counted from the last write.
So the cache holds the metadata of every node read or written in the last 24 hours, and its memory use depends on how many files were touched, not on anything an admin can configure. A full opencloud posixfs scan, or a sync client walking a large tree, puts every node in it. Entries also vary a lot in size: a file with many shares carries all its grants.
A bounded cache would make the memory use predictable. Evicting entries is safe, as a miss only reads the metadata from disk again. Suggestions:
- limit the cache by bytes, not by number of entries, as entries vary in size;
- keep the TTL: with several storage-users instances it is also the only bound on staleness (a cache hit is not compared with the disk, see
MessagePackBackend.loadAttributes,pkg/storage/pkg/decomposedfs/metadata/messagepack_backend.go:212, and the same in the hybrid backend).
2. Every read copies and decodes the entry (to be measured)
The go-micro Store interface only stores bytes. A cache read:
- looks up the key under go-cache's lock (
cache.Get); - copies the stored value into a new record (
memoryStore.get); - decodes it with msgpack into a new map (
cacheStore.PullFromCache,cache.go:189).
Every write encodes and copies again. That is several allocations and a msgpack round trip for every metadata read, i.e. for every stat, PROPFIND entry and download. My guess is that this costs more than the locking, but it has not been measured: before changing anything, CPU, allocation and mutex profiles (pprof) of storage-users under load, e.g. a PROPFIND-heavy sync, would show whether the cache matters at all.
If it does, a maintained concurrent cache library with a byte limit and approximate LRU eviction (e.g. otter or ristretto), holding the decoded attributes as immutable values, would address both points. Check the write semantics of the chosen library: if writes are applied asynchronously, a read right after a write could return the previous metadata, e.g. a stale nodestatus right after an upload finished.
go-cache itself has had no release since 2017.
Not needed: sharding the map
go-cache protects its map with a single sync.RWMutex (cache.go:43). A sharded map looks like the obvious improvement, but is unlikely to help, and has costs:
- Readers already run in parallel, and the critical sections are tiny: one map lookup or assignment. The copies and the msgpack decoding happen outside the lock. The lock would only be contended with many cores at a very high request rate, which a mutex profile can confirm or rule out.
- Operations over the whole map get more complex: the store interface has prefix reads and
List, and go-cache has its expiry pass,Items()andFlush(). With shards, each of them has to visit every shard and is no longer atomic. The file metadata cache only uses exact keys, but thememorystore serves other caches too. - Eviction becomes per shard: with a size limit, a busy shard evicts entries that a global policy would keep.
- A strict LRU would make it worse, not better: it updates its recency list on every read, so reads would need the exclusive lock. That is why concurrent caches approximate LRU (CLOCK, sampling, W-TinyLFU); a library from point 2 handles this, which a hand-written sharded LRU would have to solve again.
One real cost of the current lock: go-cache's expiry pass (DeleteExpired, every 5 minutes) holds the write lock while it walks the whole map, so it blocks all reads and writes for a time that grows with the cache. A bounded cache shortens that too.
All of the above is from reading the code (reva main, opencloud main with the vendored go-micro fork and go-cache v2.1.0); nothing was measured.
Activity
pbleser-oc commentedon Oct 25, 2026
Some numbers on what the unbounded cache costs and what it brings, measured with
opencloud posixfs scan, which opens its own posix driver with the same file metadata cache as the server (STORAGE_USERS_FILEMETADATA_CACHE_STORE). opencloudmain(8acaef28e2), single binary stopped, only NATS running; trees of 200 directories with 1,000 small files each; every configuration twice, in alternating order:memory(default)noopmemorycache grows the same way with the files it touches within the TTL (24 h by default).noopisn't an alternative today: posix refuses to start with anoopfile metadata cache ("the posix driver requires a file metadata cache",pkg/storage/fs/posix/posix.go), since 688e625 in Fix race condition #650, which also switched the posix integration tests fromnooptomemorybecause "posixfs doesn't work with noop" (f42720a). Why isn't documented yet; for the measurement above, I removed the check in a local build. Every file got an ID withnoopas well, but that says nothing about the races Fix race condition #650 fixed.So a bounded cache looks like the best of both: it should keep most of the ~20%, as the entries that are read again are mostly the directories, a small fraction of all nodes (an expectation, not measured), keeps the memory predictable, and keeps whatever
noopbreaks on posix out of the picture. Until the reason for thenoopcheck is known,noopshouldn't be the way to bound the memory.