shell: refuse a replica trim that would drop a needle no survivor holds (#12878)

* shell: refuse a replica trim that would drop a needle no survivor holds

The -doCheck pass before volume.fix.replication deletes a surplus
replica compares it with each survivor in turn without resurrecting
absent needles, so a needle live on the doomed replica and missing on
every survivor passed the check. Trimming then deleted its only copy if
it was a missed write. Count, across all survivors, the live needles
none of them holds live or deleted, and skip the trim when there are any.

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

* shell: count a remote-tier survivor's delete as covering the needle

A remote-tier replica records its deletes with offset zero, and the
filtered index load drops those keys, so the trim check saw the needle as
held by no survivor and refused the trim on every run. Walk survivor
indexes unfiltered: any entry for the key covers it.

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

* shell: keep the trim's final survivor check under test

The coverage check reads each survivor's index a second time, so the
survivor that disappeared after its first index read now failed that
read and the final VolumeStatus check was never reached. Make it
disappear after the second read and require the status check to run.

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Chris LuandClaude Opus 5.5 authored and GitHub committed 2026-11-21 21:12:33 -08:00
1 parent 95a4113c1e
commit f8d3722143
2 files changed
+144 -5

No files matched your search

@@ -1,6 +1,7 @@
package shell
import (
"bytes"
"context"
"flag"
"fmt"
@@ -14,6 +15,7 @@ import (
"slices"
"github.com/seaweedfs/seaweedfs/weed/pb"
"github.com/seaweedfs/seaweedfs/weed/storage/idx"
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
"github.com/seaweedfs/seaweedfs/weed/storage/needle_map"
"github.com/seaweedfs/seaweedfs/weed/storage/types"
@@ -334,6 +336,42 @@ func checkOneVolume(a *VolumeReplica, b *VolumeReplica, writer io.Writer, comman
return
}
// countUncoveredNeedles counts the needles live on doomed that no other
// replica holds, live or deleted. Each may be a missed write, so trimming
// doomed could delete its only copy. Survivor indexes are walked unfiltered:
// a remote-tier replica records its deletes with offset zero.
func countUncoveredNeedles(commandEnv *CommandEnv, writer io.Writer, doomed *VolumeReplica, replicas []*VolumeReplica) (int, error) {
vcd := &volumeCheckDisk{writer: writer, commandEnv: commandEnv}
uncovered := needle_map.NewMemDb()
defer uncovered.Close()
if err := vcd.readIndexDatabase(uncovered, doomed.info.Collection, doomed.info.Id, pb.NewServerAddressFromDataNode(doomed.location.dataNode)); err != nil {
return 0, fmt.Errorf("readIndexDatabase %s volume %d: %v", doomed.location.dataNode.Id, doomed.info.Id, err)
}
for _, survivor := range replicas {
if survivor.location.dataNode == doomed.location.dataNode {
continue
}
var index bytes.Buffer
err := vcd.copyVolumeIndexFile(survivor.info.Collection, survivor.info.Id, pb.NewServerAddressFromDataNode(survivor.location.dataNode), &index)
if err == nil {
err = idx.WalkIndexFile(bytes.NewReader(index.Bytes()), 0, func(key types.NeedleId, _ types.Offset, _ types.Size) error {
return uncovered.Delete(key)
})
}
if err != nil {
return 0, fmt.Errorf("readIndexDatabase %s volume %d: %v", survivor.location.dataNode.Id, survivor.info.Id, err)
}
}
count := 0
err := uncovered.AscendingVisit(func(value needle_map.NeedleValue) error {
if !value.Size.IsDeleted() {
count++
}
return nil
})
return count, err
}
// deleteOneVolume trims one replica from each of the given volumes, and
// reports how many replicas it actually deleted.
func (c *commandVolumeFixReplication) deleteOneVolume(commandEnv *CommandEnv, writer io.Writer, applyChanges bool, doCheck bool, volumeIds []uint32, volumeReplicas map[uint32][]*VolumeReplica, selectOneVolumeFn SelectOneVolumeFunc) (deleted int, err error) {
@@ -382,6 +420,15 @@ func (c *commandVolumeFixReplication) deleteOneVolume(commandEnv *CommandEnv, wr
break
}
}
if checkErr == nil {
var uncovered int
if uncovered, checkErr = countUncoveredNeedles(commandEnv, writer, replica, replicas); checkErr != nil {
fmt.Fprintf(writer, "skip deleting volume %d from %s: %v\n", replica.info.Id, replica.location.dataNode.Id, checkErr)
} else if uncovered > 0 {
checkErr = fmt.Errorf("%d uncovered needle(s)", uncovered)
fmt.Fprintf(writer, "skip trimming volume %d from %s: %d live needle(s) there are held by no other replica; reconcile with volume.check.disk first\n", replica.info.Id, replica.location.dataNode.Id, uncovered)
}
}
// A survivor can go away while the indexes are read.
for _, replicaB := range replicas {
if checkErr != nil {
@@ -742,16 +742,22 @@ func TestClassifyReplicaSet(t *testing.T) {
type trimTestVolumeServer struct {
volume_server_pb.UnimplementedVolumeServerServer
afterIndex func()
gone atomic.Bool
deletes atomic.Int32
afterIndex func()
index []byte
gone atomic.Bool
deletes atomic.Int32
statusChecks atomic.Int32
}
func (s *trimTestVolumeServer) CopyFile(_ *volume_server_pb.CopyFileRequest, stream volume_server_pb.VolumeServer_CopyFileServer) error {
if s.gone.Load() {
return status.Error(codes.NotFound, "volume not found")
}
if err := stream.Send(&volume_server_pb.CopyFileResponse{FileContent: needle_map.ToBytes(1, types.ToOffset(8), 4)}); err != nil {
index := s.index
if index == nil {
index = needle_map.ToBytes(1, types.ToOffset(8), 4)
}
if err := stream.Send(&volume_server_pb.CopyFileResponse{FileContent: index}); err != nil {
return err
}
if s.afterIndex != nil {
@@ -761,6 +767,7 @@ func (s *trimTestVolumeServer) CopyFile(_ *volume_server_pb.CopyFileRequest, str
}
func (s *trimTestVolumeServer) VolumeStatus(context.Context, *volume_server_pb.VolumeStatusRequest) (*volume_server_pb.VolumeStatusResponse, error) {
s.statusChecks.Add(1)
if s.gone.Load() {
return nil, status.Error(codes.NotFound, "volume not found")
}
@@ -784,7 +791,13 @@ func TestDeleteOneVolumeRechecksSurvivorsBeforeDelete(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
doomed, survivor := &trimTestVolumeServer{}, &trimTestVolumeServer{}
if tc.survivorGoes {
survivor.afterIndex = func() { survivor.gone.Store(true) }
// Gone after the coverage check, its second index read.
var indexReads atomic.Int32
survivor.afterIndex = func() {
if indexReads.Add(1) == 2 {
survivor.gone.Store(true)
}
}
}
commandEnv, _ := configureReplicationTestEnv()
replicas := []*VolumeReplica{
@@ -801,9 +814,88 @@ func TestDeleteOneVolumeRechecksSurvivorsBeforeDelete(t *testing.T) {
if tc.survivorGoes && !survivor.gone.Load() {
t.Fatal("the survivor never served its index")
}
if survivor.statusChecks.Load() == 0 {
t.Fatal("the final survivor check never ran")
}
if deleted != tc.wantDeleted || int(doomed.deletes.Load()) != tc.wantDeleted {
t.Fatalf("deleted=%d VolumeDelete calls=%d, want %d", deleted, doomed.deletes.Load(), tc.wantDeleted)
}
})
}
}
func trimTestIndex(entries ...needle_map.NeedleValue) []byte {
var index []byte
for _, entry := range entries {
index = append(index, needle_map.ToBytes(entry.Key, entry.Offset, entry.Size)...)
}
return index
}
func TestDeleteOneVolumeRefusesTrimOfUncoveredNeedles(t *testing.T) {
live := func(key types.NeedleId) needle_map.NeedleValue {
return needle_map.NeedleValue{Key: key, Offset: types.ToOffset(int64(key) * 8), Size: 4}
}
deleted := func(key types.NeedleId) needle_map.NeedleValue {
return needle_map.NeedleValue{Key: key, Offset: types.ToOffset(int64(key) * 8), Size: types.TombstoneFileSize}
}
for _, tc := range []struct {
name string
doomed []needle_map.NeedleValue
survivors [][]needle_map.NeedleValue
wantDeleted int
}{
{
name: "needle on no survivor",
doomed: []needle_map.NeedleValue{live(1), live(2)},
survivors: [][]needle_map.NeedleValue{{live(1)}, {live(1)}},
wantDeleted: 0,
},
{
name: "each needle on some survivor",
doomed: []needle_map.NeedleValue{live(1), live(2)},
survivors: [][]needle_map.NeedleValue{{live(1)}, {live(2)}},
wantDeleted: 1,
},
{
name: "survivor holds the delete",
doomed: []needle_map.NeedleValue{live(1), live(2)},
survivors: [][]needle_map.NeedleValue{{live(1), deleted(2)}},
wantDeleted: 1,
},
{
name: "remote-tier survivor holds the delete at offset zero",
doomed: []needle_map.NeedleValue{live(1), live(2)},
survivors: [][]needle_map.NeedleValue{{live(1), {Key: 2, Size: types.TombstoneFileSize}}},
wantDeleted: 1,
},
{
name: "needle already deleted on the doomed replica",
doomed: []needle_map.NeedleValue{live(1), live(2), deleted(2)},
survivors: [][]needle_map.NeedleValue{{live(1)}},
wantDeleted: 1,
},
} {
t.Run(tc.name, func(t *testing.T) {
commandEnv, _ := configureReplicationTestEnv()
doomed := &trimTestVolumeServer{index: trimTestIndex(tc.doomed...)}
replicas := []*VolumeReplica{
{location: &location{dataNode: startConfigureReplicationTestVolumeServer(t, doomed, 1)}, info: &master_pb.VolumeInformationMessage{Id: 1}},
}
for _, entries := range tc.survivors {
survivor := &trimTestVolumeServer{index: trimTestIndex(entries...)}
replicas = append(replicas, &VolumeReplica{location: &location{dataNode: startConfigureReplicationTestVolumeServer(t, survivor, 1)}, info: &master_pb.VolumeInformationMessage{Id: 1}})
}
pickDoomed := func([]*VolumeReplica, *super_block.ReplicaPlacement) *VolumeReplica { return replicas[0] }
c := &commandVolumeFixReplication{}
got, err := c.deleteOneVolume(commandEnv, io.Discard, true, true, []uint32{1}, map[uint32][]*VolumeReplica{1: replicas}, pickDoomed)
if err != nil {
t.Fatalf("deleteOneVolume: %v", err)
}
if got != tc.wantDeleted || int(doomed.deletes.Load()) != tc.wantDeleted {
t.Fatalf("deleted=%d VolumeDelete calls=%d, want %d", got, doomed.deletes.Load(), tc.wantDeleted)
}
})
}
}