mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 10:07:28 +01:00
* volume: report the encode run in VolumeEcShardsInfo Go and Rust volume servers now fill EcShardConfig.encode_ts_ns from the mounted EC volume's .vif, next to the block layout they already report. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * erasure_coding: keep the source when holders serve different encode runs VerifyShardsAcrossServers records each holder's encode run, and RequireAgreedEncodeRun fails when holders that report one disagree. The encoder checks it before deleting the source volume, so a shard set that only looks complete by shard ids does not replace the source. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * erasure_coding: refuse a holder that reports more than one encode run A holder whose disks serve shards of different encode runs reports MixedEncodeRuns, and RequireAgreedEncodeRun fails on it even when no other holder answers or every other holder agrees on one run. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * volume: report mixed encode runs across local disks in VolumeEcShardsInfo The reply listed shards from every disk under the first disk's encode run. When the shard-bearing disks of a volume hold different nonzero runs, Go and Rust volume servers now report MixedEncodeRuns instead, so the source-deletion check sees the conflict. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * volume: refuse an EC shard copy of another encode run than the mounted one VolumeEcShardsCopy committed the copied .vif and reopened replaced shards without updating the mounted volume's encode run, so the holder kept reporting the old run while serving the new bytes. Go and Rust volume servers now stage the .vif before anything is committed and fail with FailedPrecondition when it names a nonzero run different from a run this server mounts for the volume. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * volume: list only mounted shards of every disk in Rust VolumeEcShardsInfo The Rust reply listed every shard slot of the first disk's runtime, with unmounted slots as size-0 entries, and skipped shards mounted on other disks. Callers build each holder's shard bits from this list, so a Rust holder counted as holding shards it did not have. The reply now lists the mounted shards of every local disk, each id once, as Go does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * volume: check the source encode run of an EC shard copy without a .vif A copy that leaves out the .vif could still replace a mounted shard with another encode run's bytes. Fetch the source .vif for the run check in every copy, and install it only when asked. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * volume: refuse to serve EC files of a volume held from two encode runs CopyFile looks each requested file up on its own, so a server that holds a volume's shards on one disk from one encode run and its .vif on another disk from a different run would hand a copier mismatched files. Refuse the copy when the .vif files of this server disagree on a nonzero run. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * ec: refuse a rebuild whose holders serve different encode runs The rebuild groups holders by volume id and collection only. When they serve two encode runs, the copied shards land beside the first run's .vif and .ecx and the rebuild regenerates from the mixed set. Before copying, ask every holder for its run and refuse unless they agree. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * ec: bound each holder query in the rebuild's encode-run check A holder that accepts the connection but never answers VolumeEcShardsInfo kept the rebuild waiting forever. Give each holder its own deadline; one that times out is skipped like an unreachable holder. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * ec: check only the rebuilder and copy sources for the encode run The rebuild copies each shard from its first listed holder. Extra copies are never read, so asking them for their run only adds servers that can stall the check. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * ec: do not copy from a source whose encode run went unchecked A copy source whose VolumeEcShardsInfo query fails or times out has an unknown run. The copy fence on the rebuilder compares a source's run with the run the rebuilder mounts, so on a rebuilder that holds none of the volume's shards nothing checks it, and its shards could join a rebuild of another run. The rebuild now skips such a source like a failed copy. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * ec: leave holders of shards the rebuilder has out of the run check The rebuild uses a shard the rebuilder already holds and never reads its other listed holders, but the run check still asked the first of them. A stale replica listed first for such a shard refused a rebuild that would not touch it. Pick the holders to check the way prepareDataToRecover picks its copy sources. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
272 lines
9.8 KiB
Go
272 lines
9.8 KiB
Go
package erasure_coding
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sort"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/operation"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
|
|
"google.golang.org/grpc"
|
|
)
|
|
|
|
type ServerShardInventory struct {
|
|
Bits ShardBits
|
|
QueryError error
|
|
// BlockSize is the shard block layout this holder reports for the volume —
|
|
// the one it will serve reads through. A binary predating the uniform
|
|
// layout drops the field off the wire and reports 0, which is how
|
|
// RequireAgreedBlockLayout tells such a holder from one that agrees.
|
|
BlockSize int64
|
|
// EncodeTsNs is the encode run this holder serves, 0 from a binary that
|
|
// does not report it.
|
|
EncodeTsNs int64
|
|
}
|
|
|
|
// Query errors are recorded per-server and treated as zero shards rather
|
|
// than aborting the scan, so the caller still sees partial coverage from
|
|
// healthy peers when one server is down. The caller gates destructive
|
|
// actions on RequireRecoverableShardSet against the returned union.
|
|
func VerifyShardsAcrossServers(ctx context.Context, volumeID uint32,
|
|
servers []string, dialOption grpc.DialOption) (
|
|
union ShardBits, perServer map[string]ServerShardInventory) {
|
|
|
|
perServer = make(map[string]ServerShardInventory, len(servers))
|
|
|
|
for _, server := range servers {
|
|
if server == "" {
|
|
continue
|
|
}
|
|
if _, seen := perServer[server]; seen {
|
|
continue
|
|
}
|
|
|
|
var inv ServerShardInventory
|
|
|
|
callErr := operation.WithVolumeServerClient(false, pb.ServerAddress(server), dialOption,
|
|
func(client volume_server_pb.VolumeServerClient) error {
|
|
resp, e := client.VolumeEcShardsInfo(ctx, &volume_server_pb.VolumeEcShardsInfoRequest{
|
|
VolumeId: volumeID,
|
|
})
|
|
if e != nil {
|
|
return e
|
|
}
|
|
for _, s := range resp.EcShardInfos {
|
|
if s.VolumeId != volumeID || s.ShardId >= MaxShardCount {
|
|
continue
|
|
}
|
|
inv.Bits = inv.Bits.Set(ShardId(s.ShardId))
|
|
}
|
|
inv.BlockSize = resp.GetEcShardConfig().GetBlockSize()
|
|
inv.EncodeTsNs = resp.GetEcShardConfig().GetEncodeTsNs()
|
|
return nil
|
|
})
|
|
if callErr != nil {
|
|
inv.QueryError = callErr
|
|
}
|
|
|
|
perServer[server] = inv
|
|
union = ShardBits(uint32(union) | uint32(inv.Bits))
|
|
}
|
|
|
|
return union, perServer
|
|
}
|
|
|
|
// RequireRecoverableShardSet gates source-volume deletion after EC encode:
|
|
// a non-empty .dat may only be deleted when enough distinct shards exist to
|
|
// reconstruct the volume (>= dataShards). A full set returns (false, nil); a
|
|
// degraded-but-recoverable set returns (true, nil) so the caller can warn and
|
|
// proceed -- the missing shards can be rebuilt from the survivors, while
|
|
// keeping the source next to live shards is the more dangerous mixed state.
|
|
// Below dataShards it returns an error and the source must be kept.
|
|
// dataShards/totalShards are passed as parameters (not derived from the
|
|
// package constants) so enterprise builds with custom EC ratios share this
|
|
// helper verbatim.
|
|
func RequireRecoverableShardSet(volumeID uint32, shardsPresent ShardBits, dataShards, totalShards int) (degraded bool, err error) {
|
|
if totalShards <= 0 || totalShards > MaxShardCount {
|
|
return false, fmt.Errorf("invalid totalShards %d for volume %d (must be in [1, %d])",
|
|
totalShards, volumeID, MaxShardCount)
|
|
}
|
|
if dataShards <= 0 || dataShards > totalShards {
|
|
return false, fmt.Errorf("invalid dataShards %d for volume %d (must be in [1, %d])",
|
|
dataShards, volumeID, totalShards)
|
|
}
|
|
var missing []int
|
|
for id := 0; id < totalShards; id++ {
|
|
if !shardsPresent.Has(ShardId(id)) {
|
|
missing = append(missing, id)
|
|
}
|
|
}
|
|
if len(missing) == 0 {
|
|
return false, nil
|
|
}
|
|
if totalShards-len(missing) >= dataShards {
|
|
return true, nil
|
|
}
|
|
sort.Ints(missing)
|
|
return false, fmt.Errorf("EC shard set unrecoverable for volume %d: %d/%d shards present, need %d to reconstruct, missing shard ids %v",
|
|
volumeID, totalShards-len(missing), totalShards, dataShards, missing)
|
|
}
|
|
|
|
// RequireAgreedBlockLayout gates source-volume deletion on every holder that
|
|
// answered agreeing with the layout the shards were encoded under.
|
|
//
|
|
// The uniform block layout lives in a `.vif` field that older volume servers
|
|
// have never heard of: such a server parses the file, silently discards the
|
|
// unknown field, and mounts the volume on the legacy 1GiB/1MiB striping. Its
|
|
// reads then land at the wrong shard offsets and return wrong bytes, and
|
|
// nothing else in the encode path notices — the shard files are the same
|
|
// length under either layout. Asking each holder which layout it is serving is
|
|
// the one question that separates the two, and asking it here means the answer
|
|
// arrives while the source .dat is still on disk.
|
|
//
|
|
// Holders that could not be reached, or that report no shard of this volume,
|
|
// are skipped: RequireRecoverableShardSet already covers a missing holder, and
|
|
// one that answered nothing has promised nothing.
|
|
func RequireAgreedBlockLayout(volumeID uint32, encodedBlockSize int64, perServer map[string]ServerShardInventory) error {
|
|
disagreeing := make([]string, 0, len(perServer))
|
|
for server, inv := range perServer {
|
|
if inv.QueryError != nil || inv.Bits.Count() == 0 {
|
|
continue
|
|
}
|
|
if inv.BlockSize != encodedBlockSize {
|
|
disagreeing = append(disagreeing, fmt.Sprintf("%s serves block size %d", server, inv.BlockSize))
|
|
}
|
|
}
|
|
if len(disagreeing) == 0 {
|
|
return nil
|
|
}
|
|
sort.Strings(disagreeing)
|
|
return fmt.Errorf("volume %d was encoded with shard block size %d but %v; upgrade every volume server to a build that understands the uniform shard block layout before encoding",
|
|
volumeID, encodedBlockSize, disagreeing)
|
|
}
|
|
|
|
// MixedEncodeRuns is the encode run a holder reports when its disks serve
|
|
// shards of more than one encode run for the volume.
|
|
const MixedEncodeRuns int64 = -1
|
|
|
|
// RequireAgreedEncodeRun fails when the holders that answered serve shards of
|
|
// different encode runs. A shard set mixing runs counts as complete by shard
|
|
// ids alone, yet reconstructs wrong bytes wherever the runs' inputs differ.
|
|
// Holders that could not be reached, report no shard, or do not report a run
|
|
// are skipped.
|
|
func RequireAgreedEncodeRun(volumeID uint32, perServer map[string]ServerShardInventory) error {
|
|
byRun := make(map[int64][]string)
|
|
for server, inv := range perServer {
|
|
if inv.QueryError != nil || inv.Bits.Count() == 0 || inv.EncodeTsNs == 0 {
|
|
continue
|
|
}
|
|
byRun[inv.EncodeTsNs] = append(byRun[inv.EncodeTsNs], server)
|
|
}
|
|
if _, mixed := byRun[MixedEncodeRuns]; !mixed && len(byRun) <= 1 {
|
|
return nil
|
|
}
|
|
runs := make([]string, 0, len(byRun))
|
|
for ts, servers := range byRun {
|
|
sort.Strings(servers)
|
|
if ts == MixedEncodeRuns {
|
|
runs = append(runs, fmt.Sprintf("more than one run on each of %v", servers))
|
|
continue
|
|
}
|
|
runs = append(runs, fmt.Sprintf("run %d on %v", ts, servers))
|
|
}
|
|
sort.Strings(runs)
|
|
return fmt.Errorf("volume %d holders serve shards of different encode runs: %v", volumeID, runs)
|
|
}
|
|
|
|
func SummarizeShardInventory(perServer map[string]ServerShardInventory) string {
|
|
servers := make([]string, 0, len(perServer))
|
|
for s := range perServer {
|
|
servers = append(servers, s)
|
|
}
|
|
sort.Strings(servers)
|
|
|
|
var b []byte
|
|
for i, s := range servers {
|
|
if i > 0 {
|
|
b = append(b, ' ')
|
|
}
|
|
inv := perServer[s]
|
|
b = append(b, s...)
|
|
b = append(b, '=')
|
|
b = append(b, '[')
|
|
ids := make([]int, 0)
|
|
for id := 0; id < MaxShardCount; id++ {
|
|
if inv.Bits.Has(ShardId(id)) {
|
|
ids = append(ids, id)
|
|
}
|
|
}
|
|
for j, id := range ids {
|
|
if j > 0 {
|
|
b = append(b, ' ')
|
|
}
|
|
b = append(b, []byte(fmt.Sprintf("%d", id))...)
|
|
}
|
|
if inv.QueryError != nil {
|
|
if len(ids) > 0 {
|
|
b = append(b, ' ')
|
|
}
|
|
b = append(b, []byte("ERR:"+inv.QueryError.Error())...)
|
|
}
|
|
b = append(b, ']')
|
|
}
|
|
return string(b)
|
|
}
|
|
|
|
// VerifyShardsOnServer confirms one server really holds the named shards of a
|
|
// specific (collection, volume), for callers about to delete another copy.
|
|
//
|
|
// Collection is checked, unlike in VerifyShardsAcrossServers: the inventory RPC
|
|
// is keyed by volume id alone, so a server answering for volume N says nothing
|
|
// about which collection's volume N it means. Approving a delete on the
|
|
// strength of a different collection's shard would remove the last real copy —
|
|
// the exact outcome the caller is trying to prevent.
|
|
//
|
|
// A server that cannot be queried is unknown, not confirmed, and returns an
|
|
// error: treating an unreachable peer as proof of a surviving copy is how a
|
|
// network blip becomes data loss.
|
|
// CollectShardsOnServer asks one volume server which shards of the volume it
|
|
// actually serves right now — its live inventory, as opposed to what the
|
|
// master's possibly stale topology claims for it.
|
|
func CollectShardsOnServer(ctx context.Context, collection string, volumeID uint32,
|
|
server string, dialOption grpc.DialOption) (present ShardBits, err error) {
|
|
err = operation.WithVolumeServerClient(false, pb.ServerAddress(server), dialOption,
|
|
func(client volume_server_pb.VolumeServerClient) error {
|
|
resp, e := client.VolumeEcShardsInfo(ctx, &volume_server_pb.VolumeEcShardsInfoRequest{
|
|
VolumeId: volumeID,
|
|
})
|
|
if e != nil {
|
|
return e
|
|
}
|
|
for _, s := range resp.EcShardInfos {
|
|
if s.VolumeId != volumeID || s.Collection != collection || s.ShardId >= MaxShardCount {
|
|
continue
|
|
}
|
|
present = present.Set(ShardId(s.ShardId))
|
|
}
|
|
return nil
|
|
})
|
|
return present, err
|
|
}
|
|
|
|
func VerifyShardsOnServer(ctx context.Context, collection string, volumeID uint32,
|
|
server string, shardIDs []uint32, dialOption grpc.DialOption) error {
|
|
|
|
if server == "" {
|
|
return fmt.Errorf("no server given to verify volume %d shard(s) %v", volumeID, shardIDs)
|
|
}
|
|
|
|
present, callErr := CollectShardsOnServer(ctx, collection, volumeID, server, dialOption)
|
|
if callErr != nil {
|
|
return fmt.Errorf("verify volume %d shard(s) %v on %s: %w", volumeID, shardIDs, server, callErr)
|
|
}
|
|
|
|
for _, sid := range shardIDs {
|
|
if !present.Has(ShardId(sid)) {
|
|
return fmt.Errorf("%s does not hold ec shard %d.%d of collection %q", server, volumeID, sid, collection)
|
|
}
|
|
}
|
|
return nil
|
|
}
|