Files
seaweedfs/weed/operation/lookup_vid_cache.go
T
Chris LuandGitHub cf64cafc3b volume: drop stale volume-location cache on under-replication (#10185)
* volume: drop stale volume-location cache on under-replication

A replicated write looks up the volume's locations and caches them for 10
minutes. When the master briefly reports fewer replicas than the copy count
(e.g. a stale heartbeat drops a just-added volume), that under-replicated
result got cached, so every write failed with "replicating operations is less
than replication copy count" until the entry expired -- long after the master
re-registered the replica.

Invalidate the cached entry when the location count is below the copy count, so
the next write re-queries the master and recovers as soon as it heals.

* volume: mirror the replication copy-count guard in seaweed-volume

do_replicated_request accepted a write even when the master reported fewer
locations than the volume's copy count, silently under-replicating. Reject it,
matching Go's GetWritableRemoteReplications. lookup_volume is uncached, so the
next write recovers as soon as the missing replica re-registers.
2026-07-01 13:51:59 -07:00

73 lines
1.4 KiB
Go

package operation
import (
"errors"
"strconv"
"sync"
"time"
"github.com/seaweedfs/seaweedfs/weed/glog"
)
var ErrorNotFound = errors.New("not found")
type VidInfo struct {
Locations []Location
NextRefreshTime time.Time
}
type VidCache struct {
sync.RWMutex
cache map[uint32]VidInfo
}
func (vc *VidCache) Get(vid string) ([]Location, error) {
id, err := strconv.ParseUint(vid, 10, 32)
if err != nil {
glog.V(1).Infof("Unknown volume id %s", vid)
return nil, err
}
if id == 0 {
return nil, ErrorNotFound
}
vc.RLock()
defer vc.RUnlock()
info, found := vc.cache[uint32(id)]
if !found || info.Locations == nil {
return nil, ErrorNotFound
}
if info.NextRefreshTime.Before(time.Now()) {
return nil, errors.New("expired")
}
return info.Locations, nil
}
func (vc *VidCache) Set(vid string, locations []Location, duration time.Duration) {
id, err := strconv.ParseUint(vid, 10, 32)
if err != nil {
glog.V(1).Infof("Unknown volume id %s", vid)
return
}
if id == 0 {
return
}
vc.Lock()
defer vc.Unlock()
if vc.cache == nil {
vc.cache = make(map[uint32]VidInfo)
}
vc.cache[uint32(id)] = VidInfo{
Locations: locations,
NextRefreshTime: time.Now().Add(duration),
}
}
func (vc *VidCache) Delete(vid string) {
id, err := strconv.ParseUint(vid, 10, 32)
if err != nil || id == 0 {
return
}
vc.Lock()
defer vc.Unlock()
delete(vc.cache, uint32(id))
}