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 {