From d674199290460eef95ae338ca85f2d4a58c683b8 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sun, 22 Nov 2026 10:38:36 -0800 Subject: [PATCH] storage: fill a cold EC location cache from a partial lookup A reply placing fewer than the data shards no longer reaches the cache at all, so a cold cache stays empty and a read cannot go straight to a shard whose holder is up, as it could before the guard counted placed shards. Take the holders it names for shards the cache does not know, without touching known shards or the refresh time. Co-Authored-By: Claude Opus 5.5 --- weed/storage/store_ec.go | 14 ++++++++++ weed/storage/store_ec_location_cache_test.go | 29 ++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/weed/storage/store_ec.go b/weed/storage/store_ec.go index 0a6e2a4946..44b45726b7 100644 --- a/weed/storage/store_ec.go +++ b/weed/storage/store_ec.go @@ -1191,6 +1191,20 @@ func (s *Store) cachedLookupEcShardLocations(ecVolume *erasure_coding.EcVolume) } } if located < ecCtx.DataShards { + // Too few to trust as the volume's map, but a cold cache can still + // read the shards it names directly: fill only the shards the cache + // does not know, and leave the refresh time alone. + ecVolume.ShardLocationsLock.Lock() + for _, shardIdLocations := range resp.ShardIdLocations { + shardId := erasure_coding.ShardId(shardIdLocations.ShardId) + if len(ecVolume.ShardLocations[shardId]) > 0 { + continue + } + for _, loc := range shardIdLocations.Locations { + ecVolume.ShardLocations[shardId] = append(ecVolume.ShardLocations[shardId], pb.NewServerAddressFromLocation(loc)) + } + } + ecVolume.ShardLocationsLock.Unlock() return fmt.Errorf("only %d shards found but %d required", located, ecCtx.DataShards) } diff --git a/weed/storage/store_ec_location_cache_test.go b/weed/storage/store_ec_location_cache_test.go index 9ee67e331a..7560a7501f 100644 --- a/weed/storage/store_ec_location_cache_test.go +++ b/weed/storage/store_ec_location_cache_test.go @@ -198,6 +198,35 @@ func TestCachedLookupEcShardLocationsRefusesAReplyWithFewLocatedShards(t *testin } } +// A partial reply is no map to trust, but a cold cache takes the holders it +// names, so a read can still go straight to a shard whose holder is up. +func TestCachedLookupEcShardLocationsFillsAColdCacheFromAPartialReply(t *testing.T) { + master, masterAddr := startCountingMaster(t) + master.located.Store(7) + store := &Store{ + MasterFn: func() pb.ServerAddress { return masterAddr }, + grpcDialOption: grpc.WithTransportCredentials(insecure.NewCredentials()), + } + ecVolume := &erasure_coding.EcVolume{ + VolumeId: 9, + ShardLocations: make(map[erasure_coding.ShardId][]pb.ServerAddress), + } + if err := store.cachedLookupEcShardLocations(ecVolume); err == nil { + t.Error("a reply placing 7 of 14 shards was accepted") + } + ecVolume.ShardLocationsLock.RLock() + cached := len(ecVolume.ShardLocations) + refreshed := !ecVolume.ShardLocationsRefreshTime.IsZero() + stale := ecVolume.ShardLocationsStale + ecVolume.ShardLocationsLock.RUnlock() + if cached != 7 { + t.Errorf("the cold cache holds %d shards after the partial reply, want the 7 it placed", cached) + } + if refreshed || !stale { + t.Errorf("a partial reply must leave the map due a lookup: refreshed=%v stale=%v", refreshed, stale) + } +} + func TestEcShardLocationsTTL(t *testing.T) { ecCtx := erasure_coding.NewDefaultECContext("", 1) for _, tc := range []struct {