diff --git a/weed/server/master_grpc_server.go b/weed/server/master_grpc_server.go index d99159d64b..6bf071cbbf 100644 --- a/weed/server/master_grpc_server.go +++ b/weed/server/master_grpc_server.go @@ -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 diff --git a/weed/topology/topology.go b/weed/topology/topology.go index 6baa0684ff..86220fa541 100644 --- a/weed/topology/topology.go +++ b/weed/topology/topology.go @@ -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() diff --git a/weed/topology/topology_file_id_ceiling_test.go b/weed/topology/topology_file_id_ceiling_test.go index f51e0909a9..521d1b27e7 100644 --- a/weed/topology/topology_file_id_ceiling_test.go +++ b/weed/topology/topology_file_id_ceiling_test.go @@ -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) + } +}