From 3aa513a20491270338bb3b5aa78dda1fbbb36d20 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Mon, 30 Nov 2026 23:03:15 -0800 Subject: [PATCH] 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. --- weed/mq/broker/broker_grpc_generation.go | 7 ++-- weed/mq/broker/broker_grpc_pub_follow.go | 3 ++ weed/mq/broker/broker_partition_fence.go | 32 +++++++++++++++++ weed/mq/broker/broker_partition_fence_test.go | 34 +++++++++++++++++++ 4 files changed, 72 insertions(+), 4 deletions(-) diff --git a/weed/mq/broker/broker_grpc_generation.go b/weed/mq/broker/broker_grpc_generation.go index 9aa2fc0583..0dfd3dfad3 100644 --- a/weed/mq/broker/broker_grpc_generation.go +++ b/weed/mq/broker/broker_grpc_generation.go @@ -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) } diff --git a/weed/mq/broker/broker_grpc_pub_follow.go b/weed/mq/broker/broker_grpc_pub_follow.go index 72de4a2327..48d7e8b207 100644 --- a/weed/mq/broker/broker_grpc_pub_follow.go +++ b/weed/mq/broker/broker_grpc_pub_follow.go @@ -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) } diff --git a/weed/mq/broker/broker_partition_fence.go b/weed/mq/broker/broker_partition_fence.go index 95d8e6e15a..48985ae00d 100644 --- a/weed/mq/broker/broker_partition_fence.go +++ b/weed/mq/broker/broker_partition_fence.go @@ -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() diff --git a/weed/mq/broker/broker_partition_fence_test.go b/weed/mq/broker/broker_partition_fence_test.go index 4e4f0c4a15..968aeafb44 100644 --- a/weed/mq/broker/broker_partition_fence_test.go +++ b/weed/mq/broker/broker_partition_fence_test.go @@ -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") + } +}