mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 02:27:26 +01:00
master: start a margin past the file ids handed out without a record (#13366)
* master: start a margin past the file ids handed out without a record A master records how far it hands file ids out, and the one that takes over starts from the record. A master from before the record existed leaves none: the first master that has it, taking over from one that does not, counts on from the largest id written and hands out again the ids that had not landed. That is every cluster on the day it is upgraded. The largest needle id a volume server reports shows it. Under masters that record, every id in use is below the recorded ceiling; one at or past it, or any at all with no ceiling recorded, was handed out without a record, and so may others above it. The sequence then goes on a margin of 2^24 past it. Three masters of the previous version, the two followers restarted with this one, then the leader killed while ids are assigned: 510 of 1228 ids were handed out twice, and none of 1252 with the margin. Three masters of this version alone never take it: the ranges stay a step apart. * master: margin a late server's ids past only other masters' ceilings A volume server that first reports after the master that took over has handed out ids holds none of them, yet its ids can sit under the ceiling this master recorded since and so miss the margin, though they were handed out without a record. Compare a server's first report to this master against the ceilings other masters recorded. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * master: give up its own file id ceiling when the leader changes A master that leads again, having stayed up, counted on from where it stopped under the ceiling it had recorded. A master that records nothing may have led in between, as one of the previous version can during a rolling upgrade, going on from the servers' reports below that ceiling: the ids it handed out that had not landed were handed out again. The margin does not catch it, as the reports stay under the ceiling and the servers are known. On a leader change a master now gives up its own ceiling and moves its sequence to the highest one recorded, so its next assignment records a new ceiling past all of them. goraft calls the leader change listener from its loop, so this takes no lock a raft command is held under. * master: drop a file id drawn before a leader change gave its ceiling up nextFileId read its own ceiling before the draw and checked that copy after it. A leader change that gave the ceiling up between the two left the stale copy passing, so an id drawn below the old ceiling could be handed out after a master in between had handed it out too. Read the own ceiling again after the draw. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * master: pin leading again to the ceiling recorded before The test checked the id against a bound far below the recorded ceiling, so a drop that moved the sequence only a little would still pass. Check the id and the new ceiling against the ceiling recorded before the leader changed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * master: count a server's first report after a leader change as its first The servers that reported to a master were kept for the life of the process. When the master led again after one that records nothing, a server it knew from before was compared against the ceiling it had just recorded, which may already be past the ids the master in between handed out, so a late report took no margin. Forget the reporters on a leader change, so their next report is compared against the other masters' ceilings. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: MapleWhiteZZZ <18653045+MapleWhiteZZZ@users.noreply.github.com> Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
3 files changed
+164
-13
No files matched your search
@@ -296,7 +296,11 @@ func (ms *MasterServer) SendHeartbeat(stream master_pb.Seaweed_SendHeartbeatServ
|
||||
if (dn == nil && heartbeat.Ip != "") || priorState {
|
||||
ms.Topo.NoteAllocationEvidence()
|
||||
}
|
||||
ms.Topo.Sequence.SetMax(heartbeat.MaxFileKey)
|
||||
reporter := util.JoinHostPort(heartbeat.Ip, int(heartbeat.Port))
|
||||
if dn != nil {
|
||||
reporter = dn.Url()
|
||||
}
|
||||
ms.Topo.ReportMaxFileKey(reporter, heartbeat.MaxFileKey)
|
||||
ms.Topo.UpAdjustMaxVolumeId(needle.VolumeId(heartbeat.MaxVolumeIdSeen))
|
||||
// A HasNoVolumes report still carries the reporter's LastSeenTopologyId;
|
||||
// skipping it would let an EC-only veteran's stale id miss the mismatch
|
||||
|
||||
+62
-12
@@ -166,10 +166,15 @@ type Topology struct {
|
||||
// fileIdCeiling is the end of the file ids recorded through raft as
|
||||
// handed out, and ownFileIdCeiling the one this master recorded last,
|
||||
// under fileIdCeilingBy: another master can record the same value.
|
||||
fileIdCeiling atomic.Uint64
|
||||
ownFileIdCeiling atomic.Uint64
|
||||
fileIdCeilingBy string
|
||||
fileIdCeilingLock sync.Mutex
|
||||
// othersFileIdCeiling is the highest one recorded by other masters.
|
||||
fileIdCeiling atomic.Uint64
|
||||
ownFileIdCeiling atomic.Uint64
|
||||
othersFileIdCeiling atomic.Uint64
|
||||
fileIdCeilingBy string
|
||||
fileIdCeilingLock sync.Mutex
|
||||
// fileKeyReporters are the volume servers that reported to this master
|
||||
// since the leader last changed
|
||||
fileKeyReporters sync.Map
|
||||
}
|
||||
|
||||
type quarantinedVolume struct {
|
||||
@@ -480,6 +485,7 @@ func (t *Topology) IsChildLocked() (bool, error) {
|
||||
// does not retroactively flip into "warming up" state — there is no prior
|
||||
// topology to wait for on a bootstrap.
|
||||
func (t *Topology) SetLastLeaderChangeTime(ts time.Time) {
|
||||
t.dropOwnFileIdCeiling()
|
||||
hadVolumes := t.GetMaxVolumeId() > 0
|
||||
t.lastLeaderChangeTimeLock.Lock()
|
||||
defer t.lastLeaderChangeTimeLock.Unlock()
|
||||
@@ -487,6 +493,19 @@ func (t *Topology) SetLastLeaderChangeTime(ts time.Time) {
|
||||
t.hadVolumesAtLeaderChange = hadVolumes
|
||||
}
|
||||
|
||||
// dropOwnFileIdCeiling gives up the ceiling this master recorded last: a master
|
||||
// that records nothing may have led since and handed ids out below it. The next
|
||||
// assignment records a new ceiling, past every one recorded so far, and the
|
||||
// servers' next reports count as their first. It runs in the raft leader change
|
||||
// callback, so it takes no lock a raft command is held under.
|
||||
func (t *Topology) dropOwnFileIdCeiling() {
|
||||
if ceiling := t.fileIdCeiling.Load(); ceiling > 0 {
|
||||
t.Sequence.SetMax(ceiling - 1)
|
||||
}
|
||||
t.ownFileIdCeiling.Store(0)
|
||||
t.fileKeyReporters.Clear()
|
||||
}
|
||||
|
||||
// GetLastLeaderChangeTime returns the time of the most recent leader transition.
|
||||
func (t *Topology) GetLastLeaderChangeTime() time.Time {
|
||||
t.lastLeaderChangeTimeLock.RLock()
|
||||
@@ -768,9 +787,10 @@ const fileIdCeilingStep = 1 << 20
|
||||
// master moves its sequence past what another one recorded.
|
||||
//
|
||||
// An id is handed out only when it was drawn under a ceiling this master
|
||||
// recorded itself. Recording one applies whatever the masters before it
|
||||
// recorded first, so an id drawn after that is past all of theirs; one drawn
|
||||
// before is not, and is dropped.
|
||||
// recorded itself and still holds after the draw. Recording one applies
|
||||
// whatever the masters before it recorded first, so an id drawn after that is
|
||||
// past all of theirs; one drawn before is not, nor one drawn before a leader
|
||||
// change gave the ceiling up, and is dropped.
|
||||
func (t *Topology) nextFileId(count uint64) (uint64, error) {
|
||||
if _, counted := t.Sequence.(*sequence.MemorySequencer); !counted {
|
||||
// ids made from the clock are not handed out twice
|
||||
@@ -785,7 +805,7 @@ func (t *Topology) nextFileId(count uint64) (uint64, error) {
|
||||
if id > math.MaxUint64-2*count-fileIdCeilingStep {
|
||||
return 0, fmt.Errorf("file ids are used up at %d", id)
|
||||
}
|
||||
if own != 0 && own == ceiling && id+count <= ceiling {
|
||||
if own != 0 && own == ceiling && id+count <= ceiling && t.ownFileIdCeiling.Load() == own {
|
||||
return id, nil
|
||||
}
|
||||
// past the dropped draw and the next one, however large count is
|
||||
@@ -838,18 +858,48 @@ func (t *Topology) RaiseFileIdCeiling(ceiling uint64, by string) {
|
||||
if ceiling == 0 {
|
||||
return
|
||||
}
|
||||
others := by == "" || by != t.fileIdCeilingBy
|
||||
// before the ceiling is published, so no draw under it comes from below
|
||||
if by == "" || by != t.fileIdCeilingBy || t.ownFileIdCeiling.Load() != ceiling {
|
||||
if others || t.ownFileIdCeiling.Load() != ceiling {
|
||||
t.Sequence.SetMax(ceiling - 1)
|
||||
}
|
||||
if others {
|
||||
raiseFileIdCeiling(&t.othersFileIdCeiling, ceiling)
|
||||
}
|
||||
raiseFileIdCeiling(&t.fileIdCeiling, ceiling)
|
||||
}
|
||||
|
||||
func raiseFileIdCeiling(v *atomic.Uint64, ceiling uint64) {
|
||||
for {
|
||||
recorded := t.fileIdCeiling.Load()
|
||||
if ceiling <= recorded || t.fileIdCeiling.CompareAndSwap(recorded, ceiling) {
|
||||
break
|
||||
recorded := v.Load()
|
||||
if ceiling <= recorded || v.CompareAndSwap(recorded, ceiling) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// unrecordedFileIdMargin is how far past a reported needle id the sequence
|
||||
// goes on when the id shows that file ids were handed out without a record.
|
||||
const unrecordedFileIdMargin = 1 << 24
|
||||
|
||||
// ReportMaxFileKey takes the largest needle id a volume server holds. One at
|
||||
// or past the recorded ceiling was handed out by a master that kept no record,
|
||||
// as the masters did before the ceiling existed, and so may others above it
|
||||
// that have not landed: the sequence goes on a margin past it. On a server's
|
||||
// first report since the leader changed, only the ceilings other masters
|
||||
// recorded count: the one this master recorded since may already have passed
|
||||
// ids that a master in between, or before it took over, handed out.
|
||||
func (t *Topology) ReportMaxFileKey(reporter string, key uint64) {
|
||||
ceiling := t.fileIdCeiling.Load()
|
||||
if _, known := t.fileKeyReporters.LoadOrStore(reporter, struct{}{}); !known {
|
||||
ceiling = t.othersFileIdCeiling.Load()
|
||||
}
|
||||
if key > 0 && key >= ceiling && key < math.MaxUint64-unrecordedFileIdMargin {
|
||||
key += unrecordedFileIdMargin
|
||||
}
|
||||
t.Sequence.SetMax(key)
|
||||
}
|
||||
|
||||
// FileIdCeiling returns the end of the file ids recorded as handed out.
|
||||
func (t *Topology) FileIdCeiling() uint64 {
|
||||
return t.fileIdCeiling.Load()
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"math"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/seaweedfs/raft"
|
||||
"github.com/seaweedfs/seaweedfs/weed/sequence"
|
||||
@@ -298,3 +299,99 @@ func TestPickForWriteChargesNothingWhenTheFileIdsAreNotRecorded(t *testing.T) {
|
||||
t.Fatalf("an assignment that failed charged the volume %d bytes", pending)
|
||||
}
|
||||
}
|
||||
|
||||
// A master that kept no record of the ids it handed out, as before the ceiling
|
||||
// existed, leaves no ceiling to start from, or one the ids have gone past. The
|
||||
// largest id a volume server reports shows it, and the ids above that one may
|
||||
// be in use without having landed.
|
||||
func TestReportedFileKeyPastTheCeilingStartsAMarginOn(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
ceiling uint64 // recorded by the masters before
|
||||
reported uint64
|
||||
wantPast uint64 // the first id handed out is past this one
|
||||
wantBelow uint64
|
||||
}{
|
||||
{name: "no ceiling recorded", reported: 1000, wantPast: 1000 + unrecordedFileIdMargin, wantBelow: math.MaxUint64},
|
||||
{name: "ids past the recorded ceiling", ceiling: 5000, reported: 6000, wantPast: 6000 + unrecordedFileIdMargin, wantBelow: math.MaxUint64},
|
||||
{name: "ids under the recorded ceiling", ceiling: 5000, reported: 3000, wantPast: 4999, wantBelow: 5000 + unrecordedFileIdMargin},
|
||||
{name: "a server that holds nothing", wantPast: 0, wantBelow: unrecordedFileIdMargin},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
topo := newFileIdTestTopology()
|
||||
topo.RaiseFileIdCeiling(tc.ceiling, "")
|
||||
topo.ReportMaxFileKey("volume:8080", tc.reported)
|
||||
if id := handOut(t, topo); id <= tc.wantPast || id >= tc.wantBelow {
|
||||
t.Fatalf("file id %d handed out first, want one past %d and below %d", id, tc.wantPast, tc.wantBelow)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// A volume server that reports only after the master that took over handed out
|
||||
// ids holds none of them: its ids are past what the masters before recorded,
|
||||
// even when under the ceiling this master recorded since.
|
||||
func TestLateReportedFileKeyPastTheCeilingStartsAMarginOn(t *testing.T) {
|
||||
topo := newFileIdTestTopology()
|
||||
topo.ReportMaxFileKey("a:8080", 1000)
|
||||
first := handOut(t, topo)
|
||||
|
||||
late := first + 10
|
||||
if late >= topo.FileIdCeiling() {
|
||||
t.Fatalf("the late report %d is not under the ceiling %d", late, topo.FileIdCeiling())
|
||||
}
|
||||
topo.ReportMaxFileKey("b:8080", late)
|
||||
if id := handOut(t, topo); id <= late+unrecordedFileIdMargin {
|
||||
t.Fatalf("file id %d handed out after a late report of %d, want one past %d", id, late, late+unrecordedFileIdMargin)
|
||||
}
|
||||
|
||||
// a server that reported before holds the ids this master handed out
|
||||
own := handOut(t, topo)
|
||||
topo.ReportMaxFileKey("a:8080", own)
|
||||
if id := handOut(t, topo); id != own+1 {
|
||||
t.Fatalf("file id %d handed out after a known server reported %d, want %d", id, own, own+1)
|
||||
}
|
||||
}
|
||||
|
||||
// A master that leads again, having stayed up, after one that records nothing
|
||||
// led in between, as one of the previous version can during a rolling upgrade:
|
||||
// that one went on from the servers' reports, below the ceiling this master
|
||||
// recorded, and the ids it handed out may not have landed. This master must
|
||||
// not count on from where it stopped.
|
||||
func TestMasterLeadingAgainStartsPastItsOwnCeiling(t *testing.T) {
|
||||
topo := newFileIdTestTopology()
|
||||
topo.ReportMaxFileKey("a:8080", 1000)
|
||||
stopped := handOut(t, topo)
|
||||
recorded := topo.FileIdCeiling()
|
||||
|
||||
topo.SetLastLeaderChangeTime(time.Now()) // the other master leads
|
||||
// it hands out ids up to recorded-1, of which stopped+50 lands
|
||||
topo.SetLastLeaderChangeTime(time.Now()) // this master leads again
|
||||
topo.ReportMaxFileKey("a:8080", stopped+50)
|
||||
if id := handOut(t, topo); id < recorded {
|
||||
t.Fatalf("file id %d handed out on leading again, the master in between may have handed out up to %d", id, recorded-1)
|
||||
}
|
||||
if ceiling := topo.FileIdCeiling(); ceiling <= recorded {
|
||||
t.Fatalf("file id ceiling %d on leading again, want one recorded past %d", ceiling, recorded)
|
||||
}
|
||||
}
|
||||
|
||||
// A server that reported before a master that records nothing led in between
|
||||
// reports late, after this master leads again and records a ceiling past the
|
||||
// ids the master in between handed out. The report is its first since the
|
||||
// leader changed, so it still starts the margin.
|
||||
func TestKnownServerReportingLateAfterLeadingAgainStartsAMarginOn(t *testing.T) {
|
||||
topo := newFileIdTestTopology()
|
||||
topo.ReportMaxFileKey("a:8080", 1000)
|
||||
handOut(t, topo)
|
||||
recorded := topo.FileIdCeiling()
|
||||
|
||||
topo.SetLastLeaderChangeTime(time.Now()) // the other master leads
|
||||
// it hands out ids past recorded, of which recorded+100 lands
|
||||
topo.SetLastLeaderChangeTime(time.Now()) // this master leads again
|
||||
handOut(t, topo)
|
||||
topo.ReportMaxFileKey("a:8080", recorded+100)
|
||||
if id := handOut(t, topo); id <= recorded+100+unrecordedFileIdMargin {
|
||||
t.Fatalf("file id %d handed out after a late report of %d, want one past %d", id, recorded+100, recorded+100+unrecordedFileIdMargin)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user