forked from TrueCloudLab/frostfs-node
[#1248] node: Do not update cache twice
Do not request morph values on morph cache misses concurrently. Signed-off-by: Pavel Karpy <p.karpy@yadro.com>
This commit is contained in:
parent
dc2e9d70c7
commit
59822f7fb4
2 changed files with 56 additions and 9 deletions
|
@ -70,6 +70,7 @@ Changelog for FrostFS Node
|
|||
- Non-alphabet nodes do not try to handle alphabet events (#181)
|
||||
- Failing SN and IR transactions because of incorrect scopes (#2230, #2263)
|
||||
- Global scope used for some transactions (#2230, #2263)
|
||||
- Concurrent morph cache misses (#30)
|
||||
|
||||
### Removed
|
||||
### Updated
|
||||
|
|
|
@ -24,8 +24,18 @@ type valueWithTime[V any] struct {
|
|||
e error
|
||||
}
|
||||
|
||||
// valueInProgress is a struct that contains
|
||||
// values that are being fetched/updated.
|
||||
type valueInProgress[V any] struct {
|
||||
m sync.RWMutex
|
||||
v V
|
||||
e error
|
||||
}
|
||||
|
||||
// entity that provides TTL cache interface.
|
||||
type ttlNetCache[K comparable, V any] struct {
|
||||
m sync.RWMutex // protects progMap
|
||||
progMap map[K]*valueInProgress[V] // contains fetch-in-progress keys
|
||||
ttl time.Duration
|
||||
|
||||
sz int
|
||||
|
@ -45,28 +55,64 @@ func newNetworkTTLCache[K comparable, V any](sz int, ttl time.Duration, netRdr n
|
|||
sz: sz,
|
||||
cache: cache,
|
||||
netRdr: netRdr,
|
||||
progMap: make(map[K]*valueInProgress[V]),
|
||||
}
|
||||
}
|
||||
|
||||
func waitForUpdate[V any](vip *valueInProgress[V]) (V, error) {
|
||||
vip.m.RLock()
|
||||
defer vip.m.RUnlock()
|
||||
|
||||
return vip.v, vip.e
|
||||
}
|
||||
|
||||
// reads value by the key.
|
||||
//
|
||||
// updates the value from the network on cache miss or by TTL.
|
||||
//
|
||||
// returned value should not be modified.
|
||||
func (c *ttlNetCache[K, V]) get(key K) (V, error) {
|
||||
val, ok := c.cache.Peek(key)
|
||||
valWithTime, ok := c.cache.Peek(key)
|
||||
if ok {
|
||||
if time.Since(val.t) < c.ttl {
|
||||
return val.v, val.e
|
||||
if time.Since(valWithTime.t) < c.ttl {
|
||||
return valWithTime.v, valWithTime.e
|
||||
}
|
||||
|
||||
c.cache.Remove(key)
|
||||
}
|
||||
|
||||
v, err := c.netRdr(key)
|
||||
c.m.RLock()
|
||||
valInProg, ok := c.progMap[key]
|
||||
c.m.RUnlock()
|
||||
|
||||
if ok {
|
||||
return waitForUpdate(valInProg)
|
||||
}
|
||||
|
||||
c.m.Lock()
|
||||
valInProg, ok = c.progMap[key]
|
||||
if ok {
|
||||
c.m.Unlock()
|
||||
return waitForUpdate(valInProg)
|
||||
}
|
||||
|
||||
valInProg = &valueInProgress[V]{}
|
||||
valInProg.m.Lock()
|
||||
c.progMap[key] = valInProg
|
||||
|
||||
c.m.Unlock()
|
||||
|
||||
v, err := c.netRdr(key)
|
||||
c.set(key, v, err)
|
||||
|
||||
valInProg.v = v
|
||||
valInProg.e = err
|
||||
valInProg.m.Unlock()
|
||||
|
||||
c.m.Lock()
|
||||
delete(c.progMap, key)
|
||||
c.m.Unlock()
|
||||
|
||||
return v, err
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue