mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 11:07:31 +01:00
mq broker: keep the fence renewed while a survivor persists
A leader that lost its follower renewed its fence once before storing what it held, and a follower taking over not at all after moving the fence, so a store that ran past 30 seconds let a new leader take the unsealed fence, refusing the survivor's write and dropping what it held. Both now renew every 10 seconds until they seal.
This commit is contained in:
1 parent
1857262d10
commit
3aa513a204
4 files changed
+72
-4
No files matched your search
@@ -68,15 +68,14 @@ func (b *MessageQueueBroker) watchReplica(t topic.Topic, p topic.Partition, lp *
|
||||
}
|
||||
|
||||
// endLeaderGeneration stores what this leader holds, seals its fence and reports
|
||||
// the generation ended. Renewing the fence first makes a new leader wait for that.
|
||||
// the generation ended. The fence is kept renewed meanwhile, so a new leader waits.
|
||||
func (b *MessageQueueBroker) endLeaderGeneration(t topic.Topic, lp *topic.LocalPartition, epoch int64) {
|
||||
ctx := context.Background()
|
||||
p, fence := lp.Partition, lp.Fence
|
||||
if err := b.renewPartitionFence(ctx, fence, false); err != nil {
|
||||
glog.Warningf("topic %s partition %v epoch %d: %v", t, p, epoch, err)
|
||||
}
|
||||
stopRenewing := b.keepFenceRenewed(fence)
|
||||
b.localTopicManager.RemoveLocalPartition(t, lp)
|
||||
b.localTopicManager.WaitRetired(ctx, t, p)
|
||||
stopRenewing()
|
||||
b.sealAndReportGeneration(ctx, t, p, fence, epoch)
|
||||
}
|
||||
|
||||
|
||||
@@ -80,6 +80,8 @@ func (b *MessageQueueBroker) takeOverPartition(t topic.Topic, p topic.Partition,
|
||||
glog.V(0).Infof("not taking over %v %v epoch %d: %v", t, p, init.Epoch, err)
|
||||
return
|
||||
}
|
||||
stopRenewing := b.keepFenceRenewed(fence)
|
||||
defer stopRenewing()
|
||||
// with the fence moved, nothing else adds to what is stored
|
||||
var highWaterMark int64
|
||||
if err := retryLogWrite(ctx, topic.PartitionDir(t, p), func() (err error) {
|
||||
@@ -109,5 +111,6 @@ func (b *MessageQueueBroker) takeOverPartition(t topic.Topic, p topic.Partition,
|
||||
}
|
||||
}
|
||||
glog.V(0).Infof("took over %v %v epoch %d from %s, stored %d entries", t, p, init.Epoch, init.Leader, len(held))
|
||||
stopRenewing()
|
||||
b.sealAndReportGeneration(ctx, t, p, fence, init.Epoch)
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -165,6 +166,37 @@ func (b *MessageQueueBroker) renewPartitionFence(ctx context.Context, fence *top
|
||||
return nil
|
||||
}
|
||||
|
||||
// fenceRenewInterval keeps a persisting holder's fence well inside fenceSealWait.
|
||||
var fenceRenewInterval = fenceSealWait / 3
|
||||
|
||||
// keepFenceRenewed renews fence now and then periodically until stop, so a new
|
||||
// leader keeps waiting while this broker persists what it holds.
|
||||
func (b *MessageQueueBroker) keepFenceRenewed(fence *topic.PartitionFence) (stop func()) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer close(done)
|
||||
for {
|
||||
if err := b.renewPartitionFence(ctx, fence, false); errors.Is(err, errFenceLost) {
|
||||
glog.Warningf("fence %s moved on while persisting: %v", fence.Path, err)
|
||||
return
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-time.After(fenceRenewInterval):
|
||||
}
|
||||
}
|
||||
}()
|
||||
var once sync.Once
|
||||
return func() {
|
||||
once.Do(func() {
|
||||
cancel()
|
||||
<-done
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (b *MessageQueueBroker) putFence(ctx context.Context, fencePath string, condition *filer_pb.WriteCondition_Clause, epoch int64, token string, sealed bool) error {
|
||||
dir, name := util.FullPath(fencePath).DirAndName()
|
||||
now := time.Now().Unix()
|
||||
|
||||
@@ -586,3 +586,37 @@ func TestSealingReplacesTheToken(t *testing.T) {
|
||||
t.Fatalf("renewed a fence another broker holds: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// A holder persisting what it holds keeps its fence fresh, so a new leader keeps
|
||||
// waiting however long that takes, and takes the fence once the holder stops.
|
||||
func TestPersistingHolderKeepsItsFenceRenewed(t *testing.T) {
|
||||
f, b := newFencingFiler(t)
|
||||
defer func(interval time.Duration) { fenceRenewInterval = interval }(fenceRenewInterval)
|
||||
fenceRenewInterval = 20 * time.Millisecond
|
||||
path := topic.PartitionFencePath(fenceTestTopic, fenceTestPartition)
|
||||
fence, err := b.takePartitionFence(context.Background(), fenceTestTopic, fenceTestPartition, 1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// taken long ago, as a leader's fence is when its generation ends
|
||||
stale := proto.Clone(f.entry(path)).(*filer_pb.Entry)
|
||||
stale.Attributes.Mtime = time.Now().Add(-time.Hour).Unix()
|
||||
f.put(path, stale)
|
||||
|
||||
stop := b.keepFenceRenewed(fence)
|
||||
for deadline := time.Now().Add(5 * time.Second); f.entry(path).Attributes.Mtime < time.Now().Add(-fenceSealWait).Unix(); time.Sleep(5 * time.Millisecond) {
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatal("fence not renewed")
|
||||
}
|
||||
}
|
||||
_, other := newFencingFiler(t)
|
||||
other.currentFiler, other.option = b.currentFiler, &MessageQueueBrokerOption{Ip: "127.0.0.1", Port: 18888}
|
||||
if _, err := other.takePartitionFence(context.Background(), fenceTestTopic, fenceTestPartition, 2); !errors.Is(err, errFenceLost) {
|
||||
t.Fatalf("a new leader took a fence its holder is renewing: %v", err)
|
||||
}
|
||||
stop()
|
||||
stop()
|
||||
if string(f.entry(path).Extended[s3_constants.ExtETagKey]) != fence.Token() || fence.IsLost() {
|
||||
t.Fatal("renewals changed the token")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user