Files
seaweedfs/weed/operation/lookup_vid_cache.go
T
Chris Lu 058569c77b operation: index VidCache by map instead of slice (#9853)
VidCache.cache was a []VidInfo indexed directly by volume id, so caching
one volume with a large id grew the backing array to that many entries
(each 48 bytes), allocating a zeroed slot for every unused id below it. A
single id of 32M cost ~1.5GB resident, plus geometric realloc churn as the
append loop doubled the array.

Use map[uint32]VidInfo so memory scales with the number of volumes actually
cached rather than the largest id seen. Parse ids with ParseUint(.,32) so
values outside the uint32 volume-id range are rejected instead of silently
wrapping into a key.
2026-06-07 11:46:57 -07:00

63 lines
1.2 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),
}
}