Files
seaweedfs/weed/plugin/worker/volume_lookup.go
T
Chris LuandClaude Opus 5.5 3796162ba6 erasure_coding: clear stale EC shards on every holder the master lists (#12876)
* plugin/worker: look up a volume's EC shard holders on the master

LookupEcVolumeLocations returns every server the master currently lists
for any shard of the volume. The master's "ec volume N not found" answer
means no holders; any other failure is returned.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* erasure_coding: clear stale EC shards on every holder the master lists

The encode preflight and the post-distribute rollback cleared stale
shards only on the planned targets and on the EC holders detection saw.
A server that was down at detection and came back with a failed
attempt's shards was outside both, so its shards survived and ended up
beside the new encode: EC balance could later keep that stale copy as a
duplicate.

The cleanup now also asks the master for the volume's current holders
and clears every one of them. A lookup failure counts as a cleanup
failure, so the preflight retries later instead of encoding beside
shards it cannot see. Without a master address the cleanup behaves as
before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* erasure_coding: ask every master for the EC shard holders

The execution defaults keep only the first master address, so a follower
there failed every holder lookup and with it the encode preflight. The
plugin handler now hands the task the whole cluster master list, and the
lookup moves on past a master that refuses.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* erasure_coding: refuse the preflight wipe when a regular replica is gone

A replayed job whose earlier run already committed the EC copy and
deleted the source would wipe those shards, now on every holder the
master lists, before failing on the missing source. Check that every
regular replica is still there before clearing anything.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 21:13:21 -08:00

137 lines
4.0 KiB
Go

package pluginworker
import (
"context"
"fmt"
"slices"
"strconv"
"strings"
"time"
"github.com/seaweedfs/seaweedfs/weed/pb"
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
"google.golang.org/grpc"
"google.golang.org/grpc/status"
)
// LookupVolumeLocations asks the masters for the current registered locations
// of a volume, bypassing any client-side caches. Execution handlers use it to
// re-check a proposal against the live topology, since a proposal can go
// stale between detection and execution. Returns the location URLs
// (host:port) the master reports; an empty slice means the volume is not
// registered anywhere.
func LookupVolumeLocations(ctx context.Context, masterAddresses []string, grpcDialOption grpc.DialOption, volumeID uint32) ([]string, error) {
if grpcDialOption == nil {
return nil, fmt.Errorf("grpc dial option is not configured")
}
if len(masterAddresses) == 0 {
return nil, fmt.Errorf("no master addresses provided in cluster context")
}
vid := strconv.FormatUint(uint64(volumeID), 10)
var lastErr error
for _, address := range masterAddresses {
for _, candidate := range MasterAddressCandidates(address) {
if ctx.Err() != nil {
return nil, ctx.Err()
}
dialCtx, cancelDial := context.WithTimeout(ctx, 5*time.Second)
conn, err := pb.GrpcDial(dialCtx, candidate, false, grpcDialOption)
cancelDial()
if err != nil {
lastErr = err
continue
}
client := master_pb.NewSeaweedClient(conn)
callCtx, cancelCall := context.WithTimeout(ctx, 10*time.Second)
resp, callErr := client.LookupVolume(callCtx, &master_pb.LookupVolumeRequest{
VolumeOrFileIds: []string{vid},
})
cancelCall()
_ = conn.Close()
if callErr != nil {
lastErr = callErr
continue
}
for _, vidLocation := range resp.VolumeIdLocations {
if vidLocation.VolumeOrFileId != vid {
continue
}
locations := make([]string, 0, len(vidLocation.Locations))
for _, location := range vidLocation.Locations {
locations = append(locations, location.Url)
}
return locations, nil
}
// The master answered but does not know the volume.
return nil, nil
}
}
if lastErr == nil {
lastErr = fmt.Errorf("no valid master address candidate")
}
return nil, lastErr
}
// LookupEcVolumeLocations asks the masters which servers currently hold EC
// shards of a volume. An empty slice means the master knows no shard of it.
func LookupEcVolumeLocations(ctx context.Context, masterAddresses []string, grpcDialOption grpc.DialOption, volumeID uint32) ([]pb.ServerAddress, error) {
if grpcDialOption == nil {
return nil, fmt.Errorf("grpc dial option is not configured")
}
if len(masterAddresses) == 0 {
return nil, fmt.Errorf("no master addresses provided in cluster context")
}
notFound := fmt.Sprintf("ec volume %d not found", volumeID)
var lastErr error
for _, address := range masterAddresses {
for _, candidate := range MasterAddressCandidates(address) {
if ctx.Err() != nil {
return nil, ctx.Err()
}
dialCtx, cancelDial := context.WithTimeout(ctx, 5*time.Second)
conn, err := pb.GrpcDial(dialCtx, candidate, false, grpcDialOption)
cancelDial()
if err != nil {
lastErr = err
continue
}
client := master_pb.NewSeaweedClient(conn)
callCtx, cancelCall := context.WithTimeout(ctx, 10*time.Second)
resp, callErr := client.LookupEcVolume(callCtx, &master_pb.LookupEcVolumeRequest{VolumeId: volumeID})
cancelCall()
_ = conn.Close()
if callErr != nil {
if strings.Contains(status.Convert(callErr).Message(), notFound) {
return nil, nil
}
lastErr = callErr
continue
}
var holders []pb.ServerAddress
for _, shardLocations := range resp.ShardIdLocations {
for _, location := range shardLocations.Locations {
holder := pb.NewServerAddressFromLocation(location)
if !slices.ContainsFunc(holders, holder.Equals) {
holders = append(holders, holder)
}
}
}
return holders, nil
}
}
if lastErr == nil {
lastErr = fmt.Errorf("no valid master address candidate")
}
return nil, lastErr
}