diff --git a/weed/storage/store_ec.go b/weed/storage/store_ec.go index 44b45726b7..1469d674fd 100644 --- a/weed/storage/store_ec.go +++ b/weed/storage/store_ec.go @@ -1191,18 +1191,25 @@ 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. + // Too few to trust as the volume's map, but a read can still go + // straight to a shard it names: put those holders first, keep the + // cached ones as fallback, 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 { + if len(shardIdLocations.Locations) == 0 { continue } + shardId := erasure_coding.ShardId(shardIdLocations.ShardId) + var holders []pb.ServerAddress for _, loc := range shardIdLocations.Locations { - ecVolume.ShardLocations[shardId] = append(ecVolume.ShardLocations[shardId], pb.NewServerAddressFromLocation(loc)) + holders = append(holders, pb.NewServerAddressFromLocation(loc)) } + for _, known := range ecVolume.ShardLocations[shardId] { + if !slices.Contains(holders, known) { + holders = append(holders, known) + } + } + ecVolume.ShardLocations[shardId] = holders } 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 7560a7501f..9af7372dc3 100644 --- a/weed/storage/store_ec_location_cache_test.go +++ b/weed/storage/store_ec_location_cache_test.go @@ -3,8 +3,10 @@ package storage import ( "bytes" "context" + "fmt" "net" "os" + "slices" "sync/atomic" "testing" "time" @@ -33,6 +35,19 @@ type countingMaster struct { // located, when set, answers every shard slot as the master does but // places only the first located shards on a server. located atomic.Int64 + // partialPort is the port of the server those shards are placed on; 0 is 1. + partialPort atomic.Int64 +} + +func partialLocation(port int64) *master_pb.Location { + if port == 0 { + port = 1 + } + return &master_pb.Location{Url: fmt.Sprintf("127.0.0.1:%d", port), GrpcPort: 2} +} + +func partialHolder(port int64) pb.ServerAddress { + return pb.NewServerAddressFromLocation(partialLocation(port)) } func (m *countingMaster) LookupEcVolume(_ context.Context, req *master_pb.LookupEcVolumeRequest) (*master_pb.LookupEcVolumeResponse, error) { @@ -48,7 +63,7 @@ func (m *countingMaster) LookupEcVolume(_ context.Context, req *master_pb.Lookup for shardId := 0; shardId < erasure_coding.MaxShardCount; shardId++ { entry := &master_pb.LookupEcVolumeResponse_EcShardIdLocation{ShardId: uint32(shardId)} if shardId < located { - entry.Locations = []*master_pb.Location{{Url: "127.0.0.1:1", GrpcPort: 2}} + entry.Locations = []*master_pb.Location{partialLocation(m.partialPort.Load())} } resp.ShardIdLocations = append(resp.ShardIdLocations, entry) } @@ -183,23 +198,38 @@ func TestCachedLookupEcShardLocationsRefusesAReplyWithFewLocatedShards(t *testin } master.located.Store(7) + master.partialPort.Store(3) rewindShardLocationsRefresh(ecVolume, 12*time.Second) ecVolume.ShardLocationsLock.Lock() ecVolume.ShardLocationsStale = true + refreshedAt := ecVolume.ShardLocationsRefreshTime ecVolume.ShardLocationsLock.Unlock() 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) + named := ecVolume.ShardLocations[0] + unnamed := ecVolume.ShardLocations[7] + refreshed := ecVolume.ShardLocationsRefreshTime ecVolume.ShardLocationsLock.RUnlock() if cached != erasure_coding.TotalShardsCount { t.Errorf("the cache holds %d shards after the partial reply, want all %d kept", cached, erasure_coding.TotalShardsCount) } + if want := []pb.ServerAddress{partialHolder(3), partialHolder(1)}; !slices.Equal(named, want) { + t.Errorf("a shard the partial reply places holds %v, want its new holder first and the cached one kept: %v", named, want) + } + if want := []pb.ServerAddress{partialHolder(1)}; !slices.Equal(unnamed, want) { + t.Errorf("a shard the partial reply places nowhere holds %v, want its cached %v", unnamed, want) + } + if !refreshed.Equal(refreshedAt) { + t.Errorf("the partial reply moved the refresh time from %v to %v", refreshedAt, refreshed) + } } -// 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. +// A partial reply is no map to trust, but the cache takes the holders it names, +// so a read can still go straight to a shard whose holder is up, including one +// that moved while the replies stayed partial. func TestCachedLookupEcShardLocationsFillsAColdCacheFromAPartialReply(t *testing.T) { master, masterAddr := startCountingMaster(t) master.located.Store(7) @@ -225,6 +255,20 @@ func TestCachedLookupEcShardLocationsFillsAColdCacheFromAPartialReply(t *testing if refreshed || !stale { t.Errorf("a partial reply must leave the map due a lookup: refreshed=%v stale=%v", refreshed, stale) } + + master.partialPort.Store(4) + if err := store.cachedLookupEcShardLocations(ecVolume); err == nil { + t.Error("a second reply placing 7 of 14 shards was accepted") + } + if got := master.lookups.Load(); got != 2 { + t.Fatalf("asked the master %d times, want 2: a partial reply leaves the map due", got) + } + ecVolume.ShardLocationsLock.RLock() + moved := ecVolume.ShardLocations[0] + ecVolume.ShardLocationsLock.RUnlock() + if want := []pb.ServerAddress{partialHolder(4), partialHolder(1)}; !slices.Equal(moved, want) { + t.Errorf("after the shard moved, the cache holds %v, want %v", moved, want) + } } func TestEcShardLocationsTTL(t *testing.T) {