Files
seaweedfs/weed/operation/lookup_vid_cache_test.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

49 lines
1.2 KiB
Go

package operation
import (
"fmt"
"testing"
"time"
)
func TestCaching(t *testing.T) {
var (
vc VidCache
)
var locations []Location
locations = append(locations, Location{Url: "a.com:8080"})
vc.Set("123", locations, time.Second)
ret, _ := vc.Get("123")
if ret == nil {
t.Fatal("Not found vid 123")
}
fmt.Printf("vid 123 locations = %v\n", ret)
time.Sleep(2 * time.Second)
ret, _ = vc.Get("123")
if ret != nil {
t.Fatal("Not found vid 123")
}
}
// a single large volume id must not allocate an entry per id below it
func TestCachingLargeVolumeId(t *testing.T) {
var vc VidCache
locations := []Location{{Url: "a.com:8080"}}
vc.Set("32000000", locations, time.Minute)
if got := len(vc.cache); got != 1 {
t.Fatalf("expected 1 cached entry, got %d", got)
}
if ret, _ := vc.Get("32000000"); ret == nil {
t.Fatal("Not found vid 32000000")
}
// ids beyond uint32 are not real volume ids and must not wrap into the cache
vc.Set("4294967296", locations, time.Minute)
if got := len(vc.cache); got != 1 {
t.Fatalf("out-of-range id must not be cached, got %d entries", got)
}
if ret, _ := vc.Get("4294967296"); ret != nil {
t.Fatal("out-of-range vid 4294967296 should not be found")
}
}