From 5ec35006cc86c4a12af09b6315f5978a41bba649 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sat, 5 Dec 2026 23:44:56 -0800 Subject: [PATCH] topology: push the quarantine mark only onto a copy its server holds writable The quarantine mark is a read-only mark that still takes deletes. Pushed onto a copy its server already holds read-only by a stricter mark, it relaxes that copy to accept deletes, which is the opposite of what the quarantine is for. A copy that is already read-only takes no writes, so the master now leaves it alone and pushes only when the server reports the copy writable. Once the mark has landed, a report that reads like it (read-only, taking deletes) may be the mark itself and says nothing about the server's own state. Any other report is the server's own: writable after a restart, volume.mark or a vacuum reopening the copy, or frozen harder by a mark that refuses deletes as well. Record those, so a release restores what the server last chose instead of the state the copy had when the quarantine began, and never reopens a copy someone froze meanwhile. --- weed/topology/duplicate_volume_id_test.go | 120 ++++++++++++++++++++++ weed/topology/topology.go | 25 +++-- 2 files changed, 135 insertions(+), 10 deletions(-) diff --git a/weed/topology/duplicate_volume_id_test.go b/weed/topology/duplicate_volume_id_test.go index 201583410b..fee2fc7cf4 100644 --- a/weed/topology/duplicate_volume_id_test.go +++ b/weed/topology/duplicate_volume_id_test.go @@ -2,6 +2,7 @@ package topology import ( "errors" + "sync" "sync/atomic" "testing" "time" @@ -425,3 +426,122 @@ func TestFailedWritablePushRetriedOnNextReport(t *testing.T) { return !topo.isQuarantined(dn1, v1.Id) && isWritable(t, topo, v1) && writablePushes.Load() >= 2 }, 5*time.Second, time.Millisecond, "the next volume report must retry the release and its writable push") } + +type quarantinePush struct { + dn NodeId + readOnly bool +} + +type quarantinePushRecorder struct { + mu sync.Mutex + pushes []quarantinePush +} + +func (r *quarantinePushRecorder) push(dn *DataNode, _ needle.VolumeId, readOnly bool) error { + r.mu.Lock() + defer r.mu.Unlock() + r.pushes = append(r.pushes, quarantinePush{dn.Id(), readOnly}) + return nil +} + +func (r *quarantinePushRecorder) to(dn *DataNode) []bool { + r.mu.Lock() + defer r.mu.Unlock() + var marks []bool + for _, p := range r.pushes { + if p.dn == dn.Id() { + marks = append(marks, p.readOnly) + } + } + return marks +} + +func TestQuarantineLeavesReadOnlyCopyUnpushed(t *testing.T) { + topo := NewTopology("topo", nil, 128, 3, false) + rp, _ := super_block.NewReplicaPlacementFromString("000") + var recorder quarantinePushRecorder + topo.QuarantineVolumeFn = recorder.push + + dn1 := NewDataNode("server1") + dn2 := NewDataNode("server2") + v1 := storage.VolumeInfo{Id: 7, Collection: "a", ReplicaPlacement: rp, Ttl: needle.EMPTY_TTL, ReadOnly: true} + v2 := storage.VolumeInfo{Id: 7, Collection: "b", ReplicaPlacement: rp, Ttl: needle.EMPTY_TTL} + dn1.UpdateVolumes([]storage.VolumeInfo{v1}) + dn2.UpdateVolumes([]storage.VolumeInfo{v2}) + topo.GetVolumeLayout(v1.Collection, v1.ReplicaPlacement, v1.Ttl, types.ToDiskType(v1.DiskType)).RegisterVolume(&v1, dn1, nil) + topo.GetVolumeLayout(v2.Collection, v2.ReplicaPlacement, v2.Ttl, types.ToDiskType(v2.DiskType)).RegisterVolume(&v2, dn2, nil) + topo.flagDuplicateVolumeId(v2, dn2) + + // the mark still takes deletes, so pushing it onto a copy its server + // already holds read-only could only relax that copy + require.Empty(t, recorder.to(dn1), "a copy already read-only must not be pushed") + require.Equal(t, []bool{true}, recorder.to(dn2)) + + topo.UnRegisterVolumeLayout(v2, dn2) + require.False(t, topo.isQuarantined(dn1, v1.Id)) + require.Empty(t, recorder.to(dn1), "the release must not open a copy the master never marked") + require.False(t, isWritable(t, topo, v1), "the survivor keeps its own read-only state") +} + +func TestQuarantineReleaseFollowsWritableReportAfterMark(t *testing.T) { + topo := NewTopology("topo", nil, 128, 3, false) + rp, _ := super_block.NewReplicaPlacementFromString("000") + var recorder quarantinePushRecorder + topo.QuarantineVolumeFn = recorder.push + + dn1 := NewDataNode("server1") + dn2 := NewDataNode("server2") + v1 := storage.VolumeInfo{Id: 7, Collection: "a", ReplicaPlacement: rp, Ttl: needle.EMPTY_TTL, ReadOnly: true, Version: needle.Version3} + v2 := storage.VolumeInfo{Id: 7, Collection: "b", ReplicaPlacement: rp, Ttl: needle.EMPTY_TTL} + dn1.UpdateVolumes([]storage.VolumeInfo{v1}) + dn2.UpdateVolumes([]storage.VolumeInfo{v2}) + topo.GetVolumeLayout(v1.Collection, v1.ReplicaPlacement, v1.Ttl, types.ToDiskType(v1.DiskType)).RegisterVolume(&v1, dn1, nil) + topo.GetVolumeLayout(v2.Collection, v2.ReplicaPlacement, v2.Ttl, types.ToDiskType(v2.DiskType)).RegisterVolume(&v2, dn2, nil) + topo.flagDuplicateVolumeId(v2, dn2) + + // whoever froze the copy reopens it while the conflict stands: the + // master marks it again, and the reopening is what a release restores + topo.SyncDataNodeRegistration([]*master_pb.VolumeInformationMessage{{ + Id: 7, Collection: "a", ReplicaPlacement: 0, Version: uint32(needle.Version3), + }}, dn1) + topo.duplicateFlagChecks.Wait() + require.Equal(t, []bool{true}, recorder.to(dn1)) + require.False(t, isWritable(t, topo, v1), "the writable report must not lift the quarantine") + + topo.UnRegisterVolumeLayout(v2, dn2) + require.False(t, topo.isQuarantined(dn1, v1.Id)) + require.Equal(t, []bool{true, false}, recorder.to(dn1)) + require.True(t, isWritable(t, topo, v1), "the release must restore the state the server last chose") +} + +func TestQuarantineReleaseKeepsStricterMarkAfterMark(t *testing.T) { + topo := NewTopology("topo", nil, 128, 3, false) + rp, _ := super_block.NewReplicaPlacementFromString("000") + var recorder quarantinePushRecorder + topo.QuarantineVolumeFn = recorder.push + + dn1 := NewDataNode("server1") + dn2 := NewDataNode("server2") + v1 := storage.VolumeInfo{Id: 7, Collection: "a", ReplicaPlacement: rp, Ttl: needle.EMPTY_TTL, Version: needle.Version3} + v2 := storage.VolumeInfo{Id: 7, Collection: "b", ReplicaPlacement: rp, Ttl: needle.EMPTY_TTL} + dn1.UpdateVolumes([]storage.VolumeInfo{v1}) + dn2.UpdateVolumes([]storage.VolumeInfo{v2}) + topo.GetVolumeLayout(v1.Collection, v1.ReplicaPlacement, v1.Ttl, types.ToDiskType(v1.DiskType)).RegisterVolume(&v1, dn1, nil) + topo.GetVolumeLayout(v2.Collection, v2.ReplicaPlacement, v2.Ttl, types.ToDiskType(v2.DiskType)).RegisterVolume(&v2, dn2, nil) + topo.flagDuplicateVolumeId(v2, dn2) + require.Equal(t, []bool{true}, recorder.to(dn1)) + + // the mark lands, then something freezes the copy so it refuses deletes + // too; that is no longer the master's mark, and a release must keep it + for _, canDelete := range []bool{true, false} { + topo.ApplyVolumeChanges([]*master_pb.VolumeInformationMessage{{ + Id: 7, Collection: "a", Version: uint32(needle.Version3), ReadOnly: true, ReadOnlyCanDelete: canDelete, + }}, dn1) + } + topo.duplicateFlagChecks.Wait() + + topo.UnRegisterVolumeLayout(v2, dn2) + require.False(t, topo.isQuarantined(dn1, v1.Id)) + require.Equal(t, []bool{true}, recorder.to(dn1), "the release must not reopen a copy frozen by someone else") + require.False(t, isWritable(t, topo, v1)) +} diff --git a/weed/topology/topology.go b/weed/topology/topology.go index 10c4863cb7..aad00f7e44 100644 --- a/weed/topology/topology.go +++ b/weed/topology/topology.go @@ -316,19 +316,25 @@ func (t *Topology) enforceQuarantine(dn *DataNode, vi storage.VolumeInfo) storag return vi } state := v.(quarantinedVolumeState) - if !state.enforced { + // Once the mark lands a report that reads like it, read-only but taking + // deletes, may be the mark itself. Any other report is the server's own: + // writable after a restart, volume.mark or a vacuum reopening the copy, + // or frozen harder by a mark that refuses deletes too. + if !state.enforced || !vi.ReadOnly || !vi.ReadOnlyCanDelete { state.reported = vi.ReadOnly } - if t.QuarantineVolumeFn == nil || (state.enforced && vi.ReadOnly) { + // A copy its server already holds read-only takes no writes, and the + // mark, which still takes deletes, would relax a stricter one. + if t.QuarantineVolumeFn == nil || vi.ReadOnly { t.quarantinedVolumeIds.Store(q, state) t.quarantineLock.Unlock() return vi } t.quarantineLock.Unlock() - // The mark never landed on the server, or the server lost it (restart, - // manual unquarantine); push it while the conflict stands. The call is - // remote, so it runs outside the lock. + // The server holds the copy writable: the mark never landed, or the + // server lost it (restart, manual unquarantine); push it while the + // conflict stands. The call is remote, so it runs outside the lock. pushed, err := t.pushQuarantine(dn, vi.Id, true) if pushed && err == nil { state.enforced = true @@ -337,16 +343,14 @@ func (t *Topology) enforceQuarantine(dn *DataNode, vi storage.VolumeInfo) storag cur, still := t.quarantinedVolumeIds.Load(q) if still { latest := cur.(quarantinedVolumeState) - if !latest.enforced { - latest.reported = state.reported - } + latest.reported = false latest.enforced = latest.enforced || state.enforced t.quarantinedVolumeIds.Store(q, latest) t.quarantineLock.Unlock() return vi } t.quarantineLock.Unlock() - if state.enforced && !state.reported { + if state.enforced { // The conflict resolved while the push was in flight; leave the // server consistent with the released layout. t.releaseQuarantineWritable(dn, vi.Id) @@ -1128,7 +1132,8 @@ func (t *Topology) flagDuplicateVolumeId(v storage.VolumeInfo, dn *DataNode) { vl.SetVolumeQuarantined(n, v.Id) } t.quarantineLock.Unlock() - if t.QuarantineVolumeFn == nil { + if t.QuarantineVolumeFn == nil || reported { + // enforceQuarantine pushes once the server reports it writable return } pushed, err := t.pushQuarantine(n, v.Id, true)