Compare commits

...
Author SHA1 Message Date
Chris LuandClaude Opus 5.5 06333ca84e topology: reach the layout through GetVolumeLayout in the EC quarantine tests
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-06 00:56:53 -08:00
Chris LuandClaude Opus 5.5 0164ad8464 topology: name both collections in the EC-shard conflict log
The log line takes the copy's collection and the shards' collection
directly, so the helper needs no layout key and the duplicate-id error
keeps its one volumeLayoutKey call.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-06 00:56:52 -08:00
Chris Lu 81b420c71a topology: keep a regular copy quarantined while its shard holders are away
Losing the last holder of a volume's EC shards unregistered them the same
way as a reported deletion, so the release pass found no shards and
reopened the quarantined regular copy, though the shards were still on
the holder's disks and came back with it. Writes taken meanwhile would be
missing from the EC volume.

Drop a volume's EC entry only when its last shards are reported deleted;
a disconnect leaves the entry, and the conflict, in place until the node
returns or the shards are reported gone.
2026-12-06 00:45:11 -08:00
Chris Lu d46eff96da topology: quarantine a copy beside EC shards in the step that registers it
The quarantine record for a regular copy reported beside EC shards was
created by the heartbeat before the copy was registered, and the mark was
pushed while the copy was still in no layout. A release pass running in
that window, such as the one ending another copy's duplicate check, found
no copy for the record, dropped it, and the finished push then sent a
writable release; the copy registered without a quarantine.

Create the record under the same lock hold that adds the copy to its
layout, so a release pass always finds the copy beside its record. The
duplicate check registration starts pushes the mark as before.
2026-12-06 00:43:30 -08:00
Chris Lu b083f40cfd master: keep a quarantined copy out of the writables when its server marks it writable
A volume server that makes a volume writable, as a vacuum does after its
commit, tells the master straight away through VolumeMarkReadonly with
IsReadonly unset. The master applied that to the layout directly, so a
quarantined copy returned to the writable list until its next heartbeat
put the quarantine back, and file ids could be assigned on it meanwhile.

Record the server's flag but keep the layout mark while the copy is
quarantined; the heartbeat that repeats the flag pushes the mark again.
2026-12-06 00:41:02 -08:00
Chris Lu 0aea318693 topology: log an EC-shard conflict loudly only when the regular copy is writable
Every shard registration rechecks the regular copies of its volume id, so
a single encode logged the conflict once per registration, and every
encode and decode passes through that state by design.

Report the conflict when its quarantine record is created, not on each
recheck. Warn only when the regular copy is writable, the copy that could
have taken writes the shards lack; a read-only copy, which is what an
encode holds until it deletes its source, is logged at V(1).
2026-12-06 00:28:02 -08:00
Chris Lu 2a06745590 topology: keep a regular copy read-only while its volume id has EC shards
A regular copy of a volume id that also has EC shards registered was
treated as an ordinary writable volume. When the post-encode delete of
the source did not stick, the leftover copy kept re-registering, vacuum
compacted it and reopened it, and the master assigned new file ids on it.
The EC volume never sees those writes, so they are lost once the stray
copy goes away.

Treat the EC shards as one more conflicting holder in the duplicate
volume id check, so the existing quarantine covers it: the regular copies
are marked read-only in the layout and on their servers, the mark is
pushed again if a server reopens the copy, and shard holders themselves
are left alone. The check runs from both sides: when a regular copy
registers while shards exist (recorded before it registers, so it never
reaches the writable list), and when shards register while a regular
copy exists. Once the last shard of the id is unregistered the release
pass runs, so a decoded volume ends writable as before.

An encode passes through this state by design, with its source frozen,
and a decode with its new volume mounted, so a conflict with EC shards
alone is logged as a warning and not counted as a duplicate id.
2026-12-05 23:55:02 -08:00
Chris Lu 5ec35006cc 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.
2026-12-05 23:54:19 -08:00
5 changed files with 593 additions and 55 deletions

No files matched your search

+1 -1
View File
@@ -532,7 +532,7 @@ func (ms *MasterServer) VolumeMarkReadonly(ctx context.Context, req *master_pb.V
glog.V(0).Infof("volume %d marked readonly with %d pending bytes", vid, pending)
}
} else {
vl.SetVolumeWritable(dn, vid)
ms.Topo.SetVolumeWritable(vl, dn, vid)
}
}
}
+423
View File
@@ -2,6 +2,7 @@ package topology
import (
"errors"
"sync"
"sync/atomic"
"testing"
"time"
@@ -10,6 +11,7 @@ import (
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
"github.com/seaweedfs/seaweedfs/weed/storage"
"github.com/seaweedfs/seaweedfs/weed/storage/erasure_coding"
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
"github.com/seaweedfs/seaweedfs/weed/storage/super_block"
"github.com/seaweedfs/seaweedfs/weed/storage/types"
@@ -425,3 +427,424 @@ 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))
}
func mountEcShards(topo *Topology, dn *DataNode, vid uint32, shardIds ...erasure_coding.ShardId) {
msg := buildEcShardMessage(vid, "a", "", 0, shardIds)
topo.IncrementalSyncDataNodeEcShards([]*master_pb.VolumeEcShardInformationMessage{msg}, nil, dn)
}
func deleteEcShards(topo *Topology, dn *DataNode, vid uint32, shardIds ...erasure_coding.ShardId) {
msg := buildEcShardMessage(vid, "a", "", 0, shardIds)
topo.IncrementalSyncDataNodeEcShards(nil, []*master_pb.VolumeEcShardInformationMessage{msg}, dn)
}
// requireOnlyMarked checks that marks were pushed and none of them opened
// the copy; how many land depends on how many checks saw it.
func requireOnlyMarked(t *testing.T, marks []bool) {
t.Helper()
require.NotEmpty(t, marks)
require.NotContains(t, marks, false)
}
// requireReleasedLast checks the copy was marked and then opened once, last.
func requireReleasedLast(t *testing.T, marks []bool) {
t.Helper()
require.GreaterOrEqual(t, len(marks), 2)
requireOnlyMarked(t, marks[:len(marks)-1])
require.False(t, marks[len(marks)-1])
}
func reportVolume(topo *Topology, dn *DataNode, readOnly bool) {
topo.ApplyVolumeChanges([]*master_pb.VolumeInformationMessage{{
Id: 7, Collection: "a", Version: uint32(needle.Version3), ReadOnly: readOnly,
}}, dn)
}
func TestEcShardsQuarantineRegularCopy(t *testing.T) {
topo := NewTopology("topo", nil, 128, 3, false)
var recorder quarantinePushRecorder
topo.QuarantineVolumeFn = recorder.push
dn1, dn2, dn3 := NewDataNode("server1"), NewDataNode("server2"), NewDataNode("server3")
rp, _ := super_block.NewReplicaPlacementFromString("000")
v := storage.VolumeInfo{Id: 7, Collection: "a", ReplicaPlacement: rp, Ttl: needle.EMPTY_TTL, Version: needle.Version3}
reportVolume(topo, dn1, false)
topo.duplicateFlagChecks.Wait()
require.True(t, isWritable(t, topo, v))
mountEcShards(topo, dn2, 7, 0, 1, 2, 3, 4, 5, 6)
mountEcShards(topo, dn3, 7, 7, 8, 9, 10, 11, 12, 13)
topo.duplicateFlagChecks.Wait()
require.False(t, isWritable(t, topo, v), "a regular copy must leave the writables once its id has EC shards")
require.True(t, topo.isQuarantined(dn1, v.Id))
requireOnlyMarked(t, recorder.to(dn1))
require.Empty(t, recorder.to(dn2), "shard holders are not regular volumes")
require.Empty(t, recorder.to(dn3), "shard holders are not regular volumes")
}
func TestEcShardsKeepLateRegularCopyOutOfWritables(t *testing.T) {
topo := NewTopology("topo", nil, 128, 3, false)
var recorder quarantinePushRecorder
topo.QuarantineVolumeFn = recorder.push
dn1, dn2 := NewDataNode("server1"), NewDataNode("server2")
rp, _ := super_block.NewReplicaPlacementFromString("000")
v := storage.VolumeInfo{Id: 7, Collection: "a", ReplicaPlacement: rp, Ttl: needle.EMPTY_TTL, Version: needle.Version3}
mountEcShards(topo, dn2, 7, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)
topo.duplicateFlagChecks.Wait()
// a stale copy of the source comes back with its server, writable
topo.SyncDataNodeRegistration([]*master_pb.VolumeInformationMessage{{
Id: 7, Collection: "a", Version: uint32(needle.Version3),
}}, dn1)
require.False(t, isWritable(t, topo, v), "the copy must be read-only from the moment it registers")
topo.duplicateFlagChecks.Wait()
require.False(t, isWritable(t, topo, v))
require.True(t, topo.isQuarantined(dn1, v.Id))
requireOnlyMarked(t, recorder.to(dn1))
}
func TestEcShardsQuarantineSurvivesVacuumReopen(t *testing.T) {
topo := NewTopology("topo", nil, 128, 3, false)
var recorder quarantinePushRecorder
topo.QuarantineVolumeFn = recorder.push
dn1, dn2 := NewDataNode("server1"), NewDataNode("server2")
rp, _ := super_block.NewReplicaPlacementFromString("000")
v := storage.VolumeInfo{Id: 7, Collection: "a", ReplicaPlacement: rp, Ttl: needle.EMPTY_TTL, Version: needle.Version3}
reportVolume(topo, dn1, false)
mountEcShards(topo, dn2, 7, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)
topo.duplicateFlagChecks.Wait()
marked := len(recorder.to(dn1))
requireOnlyMarked(t, recorder.to(dn1))
// the mark lands, then a compaction ends by marking the copy writable
reportVolume(topo, dn1, true)
reportVolume(topo, dn1, false)
topo.duplicateFlagChecks.Wait()
require.False(t, isWritable(t, topo, v), "a reopened copy must stay out of the writables while the shards exist")
require.Greater(t, len(recorder.to(dn1)), marked, "the reopened copy must be marked again")
requireOnlyMarked(t, recorder.to(dn1))
// a full heartbeat repeating the writable flag does not lift it either
topo.SyncDataNodeRegistration([]*master_pb.VolumeInformationMessage{{
Id: 7, Collection: "a", Version: uint32(needle.Version3),
}}, dn1)
topo.duplicateFlagChecks.Wait()
require.False(t, isWritable(t, topo, v))
}
func TestEcShardsGoneReleasesDecodedVolume(t *testing.T) {
topo := NewTopology("topo", nil, 128, 3, false)
var recorder quarantinePushRecorder
topo.QuarantineVolumeFn = recorder.push
dn1, dn2 := NewDataNode("server1"), NewDataNode("server2")
rp, _ := super_block.NewReplicaPlacementFromString("000")
v := storage.VolumeInfo{Id: 7, Collection: "a", ReplicaPlacement: rp, Ttl: needle.EMPTY_TTL, Version: needle.Version3}
mountEcShards(topo, dn1, 7, 0, 1, 2, 3, 4, 5, 6)
mountEcShards(topo, dn2, 7, 7, 8, 9, 10, 11, 12, 13)
topo.duplicateFlagChecks.Wait()
// the decode collects the data shards on dn1, generates the volume there
// and mounts it, then deletes every shard
reportVolume(topo, dn1, false)
topo.duplicateFlagChecks.Wait()
require.False(t, isWritable(t, topo, v), "the decoded volume stays read-only while shards remain")
deleteEcShards(topo, dn2, 7, 7, 8, 9, 10, 11, 12, 13)
require.False(t, isWritable(t, topo, v), "some shards still remain")
require.True(t, topo.isQuarantined(dn1, v.Id))
deleteEcShards(topo, dn1, 7, 0, 1, 2, 3, 4, 5, 6)
topo.duplicateFlagChecks.Wait()
require.False(t, topo.isQuarantined(dn1, v.Id))
require.True(t, isWritable(t, topo, v), "the decoded volume must end writable")
requireReleasedLast(t, recorder.to(dn1))
}
func TestEcEncodeSourceStaysReadOnlyUntilDeleted(t *testing.T) {
topo := NewTopology("topo", nil, 128, 3, false)
var recorder quarantinePushRecorder
topo.QuarantineVolumeFn = recorder.push
dn1, dn2, dn3 := NewDataNode("server1"), NewDataNode("server2"), NewDataNode("server3")
rp, _ := super_block.NewReplicaPlacementFromString("000")
v := storage.VolumeInfo{Id: 7, Collection: "a", ReplicaPlacement: rp, Ttl: needle.EMPTY_TTL, Version: needle.Version3}
reportVolume(topo, dn1, false)
topo.duplicateFlagChecks.Wait()
// the encode freezes the source, mounts the shards, then deletes the source
reportVolume(topo, dn1, true)
mountEcShards(topo, dn2, 7, 0, 1, 2, 3, 4, 5, 6)
mountEcShards(topo, dn3, 7, 7, 8, 9, 10, 11, 12, 13)
topo.duplicateFlagChecks.Wait()
require.False(t, isWritable(t, topo, v))
require.Empty(t, recorder.to(dn1), "a frozen source needs no mark, which would let it take deletes")
topo.SyncDataNodeRegistration(nil, dn1)
topo.duplicateFlagChecks.Wait()
require.False(t, topo.isQuarantined(dn1, v.Id), "the record goes with the source")
require.Empty(t, recorder.to(dn1))
_, found := topo.FindCollection("a")
require.False(t, found, "no regular copy is left to write to")
require.NotEmpty(t, topo.Lookup("a", v.Id), "lookups resolve to the shard holders")
}
func TestEcEncodeRollbackRestoresWritableSource(t *testing.T) {
topo := NewTopology("topo", nil, 128, 3, false)
var recorder quarantinePushRecorder
topo.QuarantineVolumeFn = recorder.push
dn1, dn2 := NewDataNode("server1"), NewDataNode("server2")
rp, _ := super_block.NewReplicaPlacementFromString("000")
v := storage.VolumeInfo{Id: 7, Collection: "a", ReplicaPlacement: rp, Ttl: needle.EMPTY_TTL, Version: needle.Version3}
reportVolume(topo, dn1, false)
reportVolume(topo, dn1, true)
mountEcShards(topo, dn2, 7, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)
topo.duplicateFlagChecks.Wait()
// a failed encode tears the shards down and reopens the source; the
// reopening can reach the master first
reportVolume(topo, dn1, false)
require.False(t, isWritable(t, topo, v))
deleteEcShards(topo, dn2, 7, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)
topo.duplicateFlagChecks.Wait()
require.True(t, isWritable(t, topo, v), "a rolled back source must end writable")
requireReleasedLast(t, recorder.to(dn1))
}
func TestEcStaleShardsReleaseKeepsEncodeFreeze(t *testing.T) {
topo := NewTopology("topo", nil, 128, 3, false)
var recorder quarantinePushRecorder
topo.QuarantineVolumeFn = recorder.push
dn1, dn2 := NewDataNode("server1"), NewDataNode("server2")
rp, _ := super_block.NewReplicaPlacementFromString("000")
v := storage.VolumeInfo{Id: 7, Collection: "a", ReplicaPlacement: rp, Ttl: needle.EMPTY_TTL, Version: needle.Version3}
// shards left by an earlier failed encode mark the writable source
reportVolume(topo, dn1, false)
mountEcShards(topo, dn2, 7, 0, 1, 2)
topo.duplicateFlagChecks.Wait()
requireOnlyMarked(t, recorder.to(dn1))
// the next encode tears them down and freezes the source, and the master
// can learn of the freeze first
topo.ApplyVolumeChanges([]*master_pb.VolumeInformationMessage{{
Id: 7, Collection: "a", Version: uint32(needle.Version3), ReadOnly: true,
}}, dn1)
deleteEcShards(topo, dn2, 7, 0, 1, 2)
topo.duplicateFlagChecks.Wait()
require.False(t, topo.isQuarantined(dn1, v.Id))
require.False(t, isWritable(t, topo, v), "the release must not reopen a source an encode froze")
requireOnlyMarked(t, recorder.to(dn1))
}
func TestQuarantineSurvivesServerMarkingItWritable(t *testing.T) {
topo := NewTopology("topo", nil, 128, 3, false)
dn1, dn2 := NewDataNode("server1"), NewDataNode("server2")
rp, _ := super_block.NewReplicaPlacementFromString("000")
v := storage.VolumeInfo{Id: 7, Collection: "a", ReplicaPlacement: rp, Ttl: needle.EMPTY_TTL, Version: needle.Version3}
reportVolume(topo, dn1, false)
mountEcShards(topo, dn2, 7, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)
topo.duplicateFlagChecks.Wait()
require.True(t, topo.isQuarantined(dn1, v.Id))
// a vacuum's commit ends with the server telling the master the copy is
// writable again, ahead of its next heartbeat
vl := topo.GetVolumeLayout(v.Collection, v.ReplicaPlacement, v.Ttl, types.ToDiskType(v.DiskType))
topo.SetVolumeWritable(vl, dn1, v.Id)
require.False(t, isWritable(t, topo, v), "the server's word must not lift the quarantine")
// without a quarantine the same call reopens the copy
deleteEcShards(topo, dn2, 7, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)
reportVolume(topo, dn1, true)
topo.SetVolumeWritable(vl, dn1, v.Id)
require.True(t, isWritable(t, topo, v))
}
func TestEcShardsQuarantineSurvivesReleaseBeforeRegistration(t *testing.T) {
topo := NewTopology("topo", nil, 128, 3, false)
var recorder quarantinePushRecorder
var released atomic.Bool
topo.QuarantineVolumeFn = func(dn *DataNode, vid needle.VolumeId, readOnly bool) error {
// a release pass for the id, finishing some other check, runs while
// the first mark is in flight
if readOnly && released.CompareAndSwap(false, true) {
topo.releaseResolvedQuarantine(vid)
}
return recorder.push(dn, vid, readOnly)
}
dn1, dn2 := NewDataNode("server1"), NewDataNode("server2")
rp, _ := super_block.NewReplicaPlacementFromString("000")
v := storage.VolumeInfo{Id: 7, Collection: "a", ReplicaPlacement: rp, Ttl: needle.EMPTY_TTL, Version: needle.Version3}
mountEcShards(topo, dn2, 7, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)
topo.duplicateFlagChecks.Wait()
reportVolume(topo, dn1, false)
topo.duplicateFlagChecks.Wait()
require.True(t, released.Load())
require.True(t, topo.isQuarantined(dn1, v.Id))
require.False(t, isWritable(t, topo, v), "the copy must stay quarantined")
requireOnlyMarked(t, recorder.to(dn1))
}
func TestEcShardsQuarantineSurvivesHolderDisconnect(t *testing.T) {
topo := NewTopology("topo", nil, 128, 3, false)
var recorder quarantinePushRecorder
topo.QuarantineVolumeFn = recorder.push
dn1, dn2 := NewDataNode("server1"), NewDataNode("server2")
rp, _ := super_block.NewReplicaPlacementFromString("000")
v := storage.VolumeInfo{Id: 7, Collection: "a", ReplicaPlacement: rp, Ttl: needle.EMPTY_TTL, Version: needle.Version3}
reportVolume(topo, dn1, false)
mountEcShards(topo, dn2, 7, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)
topo.duplicateFlagChecks.Wait()
require.True(t, topo.isQuarantined(dn1, v.Id))
// the only shard holder drops out; its shards are still on its disks
topo.UnRegisterDataNode(dn2)
topo.duplicateFlagChecks.Wait()
require.True(t, topo.isQuarantined(dn1, v.Id), "a disconnect must not release the quarantine")
require.False(t, isWritable(t, topo, v))
requireOnlyMarked(t, recorder.to(dn1))
back := NewDataNode("server2")
mountEcShards(topo, back, 7, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)
topo.duplicateFlagChecks.Wait()
require.False(t, isWritable(t, topo, v))
// only shards reported deleted end the conflict
deleteEcShards(topo, back, 7, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)
topo.duplicateFlagChecks.Wait()
require.False(t, topo.isQuarantined(dn1, v.Id))
require.True(t, isWritable(t, topo, v))
requireReleasedLast(t, recorder.to(dn1))
}
+115 -46
View File
@@ -139,8 +139,9 @@ type Topology struct {
growthFreezeApplyLock sync.Mutex
// quarantinedVolumeIds are (node, vid) pairs force-read-only because the
// vid collided across layouts. Kept separate from the reported flag so
// later heartbeats from the same server cannot quietly undo it.
// vid collided across layouts or with EC shards of the same id. Kept
// separate from the reported flag so later heartbeats from the same
// server cannot quietly undo it.
quarantinedVolumeIds sync.Map
// quarantineLock serializes each quarantine record update with the layout
// mark it justifies: a release pass and a heartbeat read-only flag update
@@ -316,19 +317,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 +344,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)
@@ -354,6 +359,20 @@ func (t *Topology) enforceQuarantine(dn *DataNode, vi storage.VolumeInfo) storag
return vi
}
// SetVolumeWritable applies a volume server's own word that its copy of vid
// takes writes again, as a vacuum sends after its commit. A quarantined copy
// keeps its layout mark; the heartbeat that repeats the flag marks the server
// again.
func (t *Topology) SetVolumeWritable(vl *VolumeLayout, dn *DataNode, vid needle.VolumeId) {
t.quarantineLock.Lock()
defer t.quarantineLock.Unlock()
if t.isQuarantined(dn, vid) {
dn.SetVolumeReadOnly(vid, false)
return
}
vl.SetVolumeWritable(dn, vid)
}
func NewTopology(id string, seq sequence.Sequencer, volumeSizeLimit uint64, pulse int, replicationAsMin bool) *Topology {
t := &Topology{}
t.id = NodeId(id)
@@ -1054,15 +1073,7 @@ func (t *Topology) RegisterVolumeLayout(v storage.VolumeInfo, dn *DataNode) {
diskType := types.ToDiskType(v.DiskType)
for {
vl := t.GetVolumeLayout(v.Collection, v.ReplicaPlacement, v.Ttl, diskType)
// RegisterVolume reads the quarantine record and marks under
// accessLock; holding quarantineLock keeps that pair serialized with
// releaseResolvedQuarantine's clear-and-delete.
t.quarantineLock.Lock()
registered := vl.RegisterVolume(&v, dn, func(n *DataNode) bool {
return t.isQuarantined(n, v.Id)
})
t.quarantineLock.Unlock()
if registered {
if t.registerVolume(vl, &v, dn) {
// RegisterVolume rebuilds only the read-only flags, so a
// location list recreated flagless would pass the writable
// check before the next heartbeat re-measures the volume.
@@ -1079,6 +1090,32 @@ func (t *Topology) RegisterVolumeLayout(v storage.VolumeInfo, dn *DataNode) {
}
}
// registerVolume records dn's copy of v in vl with the quarantine marks folded
// in. A copy registering beside EC shards of its id is quarantined in the
// same step, so it never reaches the writable list and no release pass can
// see its record before the copy is in a layout.
func (t *Topology) registerVolume(vl *VolumeLayout, v *storage.VolumeInfo, dn *DataNode) bool {
ecCollection, conflict := "", false
// RegisterVolume reads the quarantine record and marks under accessLock;
// holding quarantineLock keeps that pair serialized with
// releaseResolvedQuarantine's clear-and-delete.
t.quarantineLock.Lock()
if !t.isQuarantined(dn, v.Id) {
if collection, held := t.ecShardsHeld(v.Id); held {
t.quarantinedVolumeIds.Store(quarantinedVolume{dn.Id(), v.Id}, quarantinedVolumeState{reported: v.ReadOnly})
ecCollection, conflict = collection, true
}
}
registered := vl.RegisterVolume(v, dn, func(n *DataNode) bool {
return t.isQuarantined(n, v.Id)
})
t.quarantineLock.Unlock()
if conflict {
logEcShardConflict(*v, dn, ecCollection)
}
return registered
}
func (t *Topology) flagDuplicateVolumeIdAsync(v storage.VolumeInfo, dn *DataNode) {
t.duplicateFlagChecks.Add(1)
go func() {
@@ -1087,12 +1124,26 @@ func (t *Topology) flagDuplicateVolumeIdAsync(v storage.VolumeInfo, dn *DataNode
}()
}
// flagRegularCopiesAsync runs the duplicate check for every regular copy of
// vid, for a conflicting holder that registers outside the volume layouts.
func (t *Topology) flagRegularCopiesAsync(vid needle.VolumeId) {
for _, item := range t.collectionMap.Items() {
for _, vl := range item.(*Collection).GetAllVolumeLayouts() {
for _, n := range vl.Lookup(vid) {
if vi, err := n.GetVolumesById(vid); err == nil {
t.flagDuplicateVolumeIdAsync(vi, n)
}
}
}
}
}
// flagDuplicateVolumeId reports and quarantines v.Id when another data node
// already serves it under a different layout key. It runs after registration
// so a legitimate same-node move (disk type or collection change) settles
// first and does not trip it. Every copy across the conflicting layouts is
// quarantined: either could be the impostor, and arrival order must not
// decide which copy stays writable.
// already serves it under a different layout key or as EC shards. It runs
// after registration so a legitimate same-node move (disk type or collection
// change) settles first and does not trip it. Every regular copy across the
// conflicting layouts is quarantined: either could be the impostor, and
// arrival order must not decide which copy stays writable.
func (t *Topology) flagDuplicateVolumeId(v storage.VolumeInfo, dn *DataNode) {
// Every pass ends with a release: a conflict could resolve between the
// scan below and the marks, and a check finding no hits is also the retry
@@ -1110,12 +1161,15 @@ func (t *Topology) flagDuplicateVolumeId(v storage.VolumeInfo, dn *DataNode) {
if own == nil || !own.HasDataNode(v.Id, dn) {
return
}
stats.MasterDuplicateVolumeIds.WithLabelValues(v.Collection, v.Id.String()).Inc()
var otherKeys []string
for _, h := range hits {
otherKeys = append(otherKeys, h.layoutKey())
ecOnly := len(hits) == 1 && hits[0].vl == nil
if !ecOnly {
stats.MasterDuplicateVolumeIds.WithLabelValues(v.Collection, v.Id.String()).Inc()
var otherKeys []string
for _, h := range hits {
otherKeys = append(otherKeys, h.layoutKey())
}
glog.Errorf("duplicate volume id %d: %s reports it under %s but other data nodes also serve it under %s; marking all copies read-only", v.Id, dn.Id(), volumeLayoutKey(v.Collection, v.ReplicaPlacement, v.Ttl, types.ToDiskType(v.DiskType)), strings.Join(otherKeys, ", "))
}
glog.Errorf("duplicate volume id %d: %s reports it under %s but other data nodes also serve it under %s; marking all copies read-only", v.Id, dn.Id(), volumeLayoutKey(v.Collection, v.ReplicaPlacement, v.Ttl, types.ToDiskType(v.DiskType)), strings.Join(otherKeys, ", "))
quarantine := func(n *DataNode, vl *VolumeLayout) {
q := quarantinedVolume{n.Id(), v.Id}
reported := false
@@ -1123,12 +1177,17 @@ func (t *Topology) flagDuplicateVolumeId(v storage.VolumeInfo, dn *DataNode) {
reported = vi.ReadOnly
}
t.quarantineLock.Lock()
t.quarantinedVolumeIds.LoadOrStore(q, quarantinedVolumeState{reported: reported})
_, loaded := t.quarantinedVolumeIds.LoadOrStore(q, quarantinedVolumeState{reported: reported})
if vl != nil {
vl.SetVolumeQuarantined(n, v.Id)
}
t.quarantineLock.Unlock()
if t.QuarantineVolumeFn == nil {
// every shard registration rechecks; report the record's creation only
if ecOnly && !loaded {
logEcShardConflict(v, n, hits[0].collection)
}
if t.QuarantineVolumeFn == nil || reported {
// enforceQuarantine pushes once the server reports it writable
return
}
pushed, err := t.pushQuarantine(n, v.Id, true)
@@ -1159,6 +1218,17 @@ func (t *Topology) flagDuplicateVolumeId(v storage.VolumeInfo, dn *DataNode) {
}
}
// logEcShardConflict reports a regular copy newly quarantined beside EC shards
// of its id. Only a writable copy could have taken writes the shards lack; a
// read-only one is what an encode holds until it deletes its source.
func logEcShardConflict(v storage.VolumeInfo, dn *DataNode, ecCollection string) {
if v.ReadOnly {
glog.V(1).Infof("volume id %d: %s holds a read-only regular copy in collection %q beside EC shards in collection %q; keeping it read-only", v.Id, dn.Id(), v.Collection, ecCollection)
return
}
glog.Warningf("volume id %d: %s reports a writable regular copy in collection %q beside EC shards in collection %q; marking it read-only", v.Id, dn.Id(), v.Collection, ecCollection)
}
// releaseResolvedQuarantine lifts read-only marks once the conflicting copy
// is gone, so a finished move does not leave the surviving copy quarantined.
// The writable push is a remote call, so the locked pass only applies the
@@ -1244,7 +1314,8 @@ func (t *Topology) findQuarantinedVolume(dnId NodeId, vid needle.VolumeId) (*Dat
}
// duplicateHit is one layout that holds v.Id on data nodes other than the
// reporter's.
// reporter's, or, with no layout, the EC shards of v.Id. Shard holders are
// not regular volumes, so that hit names no nodes to mark.
type duplicateHit struct {
collection string
vl *VolumeLayout
@@ -1252,6 +1323,9 @@ type duplicateHit struct {
}
func (h duplicateHit) layoutKey() string {
if h.vl == nil {
return fmt.Sprintf("ec shards collection=%q", h.collection)
}
return volumeLayoutKey(h.collection, h.vl.rp, h.vl.ttl, h.vl.diskType)
}
@@ -1265,7 +1339,9 @@ func ttlEqual(a, b *needle.TTL) bool {
// findDuplicateVolumeLayouts reports every layout that already holds v.Id on
// data nodes other than dn. Volume ids are global, so the same id held by two
// nodes under two layouts means the allocator reused an id.
// nodes under two layouts means the allocator reused an id. EC shards of v.Id
// conflict too: a read resolves to whichever holder the lookup prefers, and
// the shards never see what the regular copy took.
func (t *Topology) findDuplicateVolumeLayouts(v storage.VolumeInfo, dn *DataNode) []duplicateHit {
if v.ReplicaPlacement == nil {
return nil
@@ -1293,6 +1369,9 @@ func (t *Topology) findDuplicateVolumeLayouts(v storage.VolumeInfo, dn *DataNode
}
}
}
if collection, held := t.ecShardsHeld(v.Id); held {
hits = append(hits, duplicateHit{collection: collection})
}
return hits
}
@@ -1552,12 +1631,7 @@ func (t *Topology) SyncDataNodeRegistration(volumes []*master_pb.VolumeInformati
// LookupVolume returns "volume id not found".
if !vl.HasDataNode(v.Id, dn) {
for {
t.quarantineLock.Lock()
registered := vl.RegisterVolume(&v, dn, func(n *DataNode) bool {
return t.isQuarantined(n, v.Id)
})
t.quarantineLock.Unlock()
if registered {
if t.registerVolume(vl, &v, dn) {
break
}
// Dropped with its collection; the next lookup creates a fresh
@@ -1666,12 +1740,7 @@ func (t *Topology) ApplyVolumeChanges(changed []*master_pb.VolumeInformationMess
// concerned: they were told it went when the node dropped out.
becameServable := !vl.HasDataNode(vi.Id, dn)
for becameServable {
t.quarantineLock.Lock()
registered := vl.RegisterVolume(&vi, dn, func(n *DataNode) bool {
return t.isQuarantined(n, vi.Id)
})
t.quarantineLock.Unlock()
if registered {
if t.registerVolume(vl, &vi, dn) {
break
}
// Dropped with its collection; the next lookup creates a fresh one.
+51 -6
View File
@@ -137,8 +137,6 @@ func (t *Topology) RegisterEcShards(ecvi *erasure_coding.EcVolumeInfo, dn *DataN
t.UpAdjustMaxVolumeId(ecvi.VolumeId)
t.ecShardMapLock.Lock()
defer t.ecShardMapLock.Unlock()
locations, found := t.ecShardMap[ecvi.VolumeId]
if !found {
locations = NewEcShardLocations(ecvi.Collection)
@@ -147,15 +145,28 @@ func (t *Topology) RegisterEcShards(ecvi *erasure_coding.EcVolumeInfo, dn *DataN
for _, shardId := range ecvi.ShardsInfo.Ids() {
locations.AddShard(shardId, dn)
}
t.ecShardMapLock.Unlock()
// a regular copy registered before the shards must not stay writable
// beside them
t.flagRegularCopiesAsync(ecvi.VolumeId)
}
// UnRegisterEcShards drops shards dn reported deleted.
func (t *Topology) UnRegisterEcShards(ecvi *erasure_coding.EcVolumeInfo, dn *DataNode) {
t.unregisterEcShards(ecvi, dn, true)
}
// unregisterEcShards drops dn's shards of ecvi from the lookup. Once a
// volume's last shards are reported deleted its entry goes, and with it the
// conflict that quarantines regular copies of the id. A node that only went
// away may come back with its shards, so its last shards leave the entry.
func (t *Topology) unregisterEcShards(ecvi *erasure_coding.EcVolumeInfo, dn *DataNode, reportedDeleted bool) {
glog.Infof("removing ec shard info:%+v", ecvi)
t.ecShardMapLock.Lock()
defer t.ecShardMapLock.Unlock()
locations, found := t.ecShardMap[ecvi.VolumeId]
if !found {
t.ecShardMapLock.Unlock()
return
}
for _, shardId := range ecvi.ShardsInfo.Ids() {
@@ -164,6 +175,37 @@ func (t *Topology) UnRegisterEcShards(ecvi *erasure_coding.EcVolumeInfo, dn *Dat
}
locations.DeleteShard(shardId, dn)
}
gone := reportedDeleted && !locations.hasShards()
if gone {
delete(t.ecShardMap, ecvi.VolumeId)
}
t.ecShardMapLock.Unlock()
if gone {
// the regular copy a decode left behind is no longer in conflict
t.releaseResolvedQuarantine(ecvi.VolumeId)
}
}
func (loc *EcShardLocations) hasShards() bool {
for _, nodes := range loc.Locations {
if len(nodes) > 0 {
return true
}
}
return false
}
// ecShardsHeld reports whether vid has EC shards, as far as the master knows,
// and under which collection: some node holds them, or their holders went
// away without reporting them deleted.
func (t *Topology) ecShardsHeld(vid needle.VolumeId) (collection string, held bool) {
t.ecShardMapLock.RLock()
defer t.ecShardMapLock.RUnlock()
if locations, found := t.ecShardMap[vid]; found {
return locations.Collection, true
}
return "", false
}
func (t *Topology) LookupEcShards(vid needle.VolumeId) (locations *EcShardLocations, found bool) {
@@ -269,8 +311,6 @@ func (t *Topology) ListEcServersByCollection(collection string) (dataNodes []pb.
func (t *Topology) DeleteEcCollection(collection string) {
t.ecShardMapLock.Lock()
defer t.ecShardMapLock.Unlock()
var vids []needle.VolumeId
for vid, ecVolumeLocation := range t.ecShardMap {
if ecVolumeLocation.Collection == collection {
@@ -281,4 +321,9 @@ func (t *Topology) DeleteEcCollection(collection string) {
for _, vid := range vids {
delete(t.ecShardMap, vid)
}
t.ecShardMapLock.Unlock()
for _, vid := range vids {
t.releaseResolvedQuarantine(vid)
}
}
+3 -2
View File
@@ -126,12 +126,13 @@ func (t *Topology) UnRegisterDataNode(dn *DataNode) {
// unregister ec shards when volume server disconnected. The node's own
// records clear first so the still-held check cannot skip the drop, and
// each unregister gets a copy — DeleteEcShard drains the shared
// ShardsInfo.
// ShardsInfo. The shards may come back with the node, so regular copies
// of their ids stay quarantined.
for _, s := range dn.GetEcShards() {
held := *s
held.ShardsInfo = s.ShardsInfo.Copy()
dn.DeleteEcShard(s)
t.UnRegisterEcShards(&held, dn)
t.unregisterEcShards(&held, dn, false)
}
negativeUsages := dn.GetDiskUsages().negative()