mirror of
https://github.com/redis/go-redis.git
synced 2026-10-24 07:59:20 +03:00
* feat(csc)!: always refresh invalidated entries ClientSideCacheRefreshRecencyWindow bounded refresh to entries read within a window. It was the only consumer that needed a per-READ recency timestamp on the cache entry, and that timestamp is the dominant cost of a cache hit. Removing the option lets the read path stop stamping entries, which the following commit does. Refresh now covers every invalidated entry. BREAKING CHANGE: Options.ClientSideCacheRefreshRecencyWindow is removed. Refresh-on-invalidate always refreshes every invalidated entry. * perf(csc): mark reads with a second-chance bit A cache hit stamped entry.lastAccessNs. The read already touches the entry's cache line, but a store dirties it, so the line must be held exclusive and costs a writeback when evicted from the CPU cache. With many concurrent readers over a working set larger than L2, that store dominates the cost of a hit. A read now sets a readSinceSweep bit instead. A load that finds it set leaves the line SHARED, so every core can hold it at once and only the first read after an eviction sweep writes. Eviction consumes the bit as a second chance; ordering still uses lastAccessNs, written on insert. Measured at 256 concurrent callers, get=90/set=10, 99.94% hit rate: 263,801 -> 328,842 reads/s (+24.7%), CPU 27.0 -> 18.9 us/op, hit rate unchanged. Removing the recency update entirely measured +40.5%, so this recovers about three fifths of the headroom while keeping eviction honest. * perf(csc): decode simple cached replies without a reader Serving a hit allocated a proto.Reader (and its bufio buffer) per hit and re-parsed the cached bytes. applyCachedFast decodes the simple single-frame replies -- nil, status, bulk string -- straight into the Cmder, and getShared serves the bytes without the defensive copy where the caller provably cannot retain them. * fix(csc): do not evict the entry just fulfilled fulfill publishes an entry with its second-chance bit clear and then evicts. In a warm memory-capped shard, where every resident entry has been read, that entry was the only clear-bit candidate and was evicted on the spot; and since a clear candidate always existed, the sweep that resets the others never ran, so the shard admitted nothing new. The entry just fulfilled is now skipped in its own eviction pass. A refresh republish also restores the invalidated entry's read bit with its access token, so a refreshed hot key is not the first victim. * fix(csc): decline negative bulk lengths other than -1 The fast cached-reply decoder took any negative bulk length as a RESP2 null. The reader rejects lengths below -1, and that rejection is what makes processCached drop a bad entry and refetch; the fast path turned it into a false miss. It now handles only -1 and declines the rest. * fix(csc): reject bare LF in cached status replies The fast cached-reply decoder rejected a status frame with an interior CR but took one with a bare LF, such as "+OK\nMORE\r\n", as a valid string. The reader stops at the first LF and rejects the frame, and that rejection is what makes processCached drop the entry and refetch. The fast path now declines an interior CR or LF. * fix(csc): keep refresh recency in the publish lock A refresh republish published the entry with its second-chance bit clear and restored the bit in a second lock. On a warm shard the republished entry was then the only clear-bit victim, so an insert in that gap evicted the key the refresh had just fetched. The refresher now stages the kept recency on its reservation (stageRefreshAccess), and fulfill applies it in the same lock as the publish. * docs(csc)!: refresh-on-invalidate refreshes all ClientSideCacheRefreshOnInvalidate still said it re-fetches recently read keys. Since the recency window was removed, it re-fetches every cached entry of an invalidated key, so on a write-heavy keyspace the refetch traffic covers the whole resident cache. BREAKING CHANGE: Options.ClientSideCacheRefreshRecencyWindow and UniversalOptions.ClientSideCacheRefreshRecencyWindow are removed. They shipped only in v9.23.0-beta.1; delete the field where it is set. * docs(csc): note reads are not monotonic per caller A read that loses the reservation twice reads the server directly. Another caller's fetch, reserved earlier and still running, can store an older value afterwards, and a later hit returns it until the key's invalidation arrives. This is within the staleness a hit already has, so document it rather than drop the older fetch, which would cost its waiters a miss each under the churn that leads here.
53 lines
1.8 KiB
Go
53 lines
1.8 KiB
Go
package redis
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
)
|
|
|
|
// TestRefreshRepublishSurvivesConcurrentInsert pins that a refresh republish
|
|
// keeps the invalidated entry's recency in the same lock as the publish. It
|
|
// used to publish with the second-chance bit clear and restore the bit in a
|
|
// second lock. On a warm shard the republished entry was then the only
|
|
// clear-bit victim, so an insert in that gap evicted the key the refresh had
|
|
// just fetched.
|
|
func TestRefreshRepublishSurvivesConcurrentInsert(t *testing.T) {
|
|
ctx := context.Background()
|
|
lc := NewLocalCache(CacheConfig{MaxEntries: 2})
|
|
fill := func(key, rk, val string) {
|
|
t.Helper()
|
|
tok, _ := lc.Reserve(key, []string{rk})
|
|
if !lc.fulfill(key, tok, 0, []byte(val)) {
|
|
t.Fatalf("fill %s failed", key)
|
|
}
|
|
}
|
|
// b is older than a; both are read, so the shard is warm.
|
|
fill("get:b", "b", "vb")
|
|
fill("get:a", "a", "va")
|
|
for _, k := range []string{"get:a", "get:b"} {
|
|
if _, ok := lc.Get(ctx, k); !ok {
|
|
t.Fatalf("%s missing", k)
|
|
}
|
|
}
|
|
|
|
// Refresh a: collect its target, reserve, stage the kept recency, publish.
|
|
targets := lc.deleteByRedisKeyCollectingHot("a", cscInvalNoHorizon, ^uint64(0), nil)
|
|
if len(targets) != 1 || !targets[0].read {
|
|
t.Fatalf("want 1 hot target carrying the read bit, got %+v", targets)
|
|
}
|
|
tok, _ := lc.Reserve("get:a", []string{"a"})
|
|
lc.stageRefreshAccess("get:a", tok, targets[0].accessNs, targets[0].read)
|
|
if !lc.fulfill("get:a", tok, 0, []byte("va2")) {
|
|
t.Fatal("republish fulfill failed")
|
|
}
|
|
|
|
// An insert right after the publish. Both residents were read, so the
|
|
// sweep consumes their second chances and evicts the older one, b.
|
|
if tok, _ := lc.Reserve("get:c", []string{"c"}); tok == 0 {
|
|
t.Fatal("reserve c failed")
|
|
}
|
|
if _, ok := lc.Get(ctx, "get:a"); !ok {
|
|
t.Fatal("the refreshed key was evicted by the next insert")
|
|
}
|
|
}
|