From d605d2527f81521dcb8587e51fcfebff8fc44219 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Mon, 30 Nov 2026 19:46:04 -0800 Subject: [PATCH] mq broker: reload a partition whose fence another broker took A partition that lost its fence stayed registered until its last client left, and both partition load paths reused it, so a broker made leader again at a newer epoch kept answering not-leader, or wrote until its next flush was refused. A fenced partition is now dropped and the partition loaded again under the current assignment's epoch. --- weed/mq/broker/broker_grpc_assign.go | 4 +++ weed/mq/broker/broker_partition_fence_test.go | 34 +++++++++++++++++++ .../mq/broker/broker_topic_conf_read_write.go | 8 ++++- 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/weed/mq/broker/broker_grpc_assign.go b/weed/mq/broker/broker_grpc_assign.go index 9377b882a1..93f7118aae 100644 --- a/weed/mq/broker/broker_grpc_assign.go +++ b/weed/mq/broker/broker_grpc_assign.go @@ -32,6 +32,10 @@ func (b *MessageQueueBroker) AssignTopicPartitions(c context.Context, request *m b.accessLock.Unlock() return nil, status.Errorf(codes.Unavailable, "broker %s is stopping", b.option.BrokerAddress()) } + if existing := b.localTopicManager.GetLocalPartition(t, partition); existing != nil && existing.IsFenced() { + // another broker took it; lead it again under this assignment's epoch + b.localTopicManager.RemoveLocalPartition(t, partition) + } if b.localTopicManager.GetLocalPartition(t, partition) == nil { localPartition, genErr := b.newLeaderPartition(t, partition, assignment.Epoch) if genErr != nil { diff --git a/weed/mq/broker/broker_partition_fence_test.go b/weed/mq/broker/broker_partition_fence_test.go index bac90415b2..0ce4fdc445 100644 --- a/weed/mq/broker/broker_partition_fence_test.go +++ b/weed/mq/broker/broker_partition_fence_test.go @@ -14,10 +14,12 @@ import ( "testing" "time" + "github.com/seaweedfs/seaweedfs/weed/filer_client" "github.com/seaweedfs/seaweedfs/weed/mq" "github.com/seaweedfs/seaweedfs/weed/mq/topic" "github.com/seaweedfs/seaweedfs/weed/pb" "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" + "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb" "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants" "github.com/seaweedfs/seaweedfs/weed/util" "google.golang.org/grpc" @@ -300,3 +302,35 @@ func TestTakeFenceGivesUpOnAStalledFiler(t *testing.T) { t.Fatal("fence take waited on a stalled filer") } } + +// A broker that lost a partition's fence and is made its leader again reloads it +// and takes the newer epoch's fence instead of serving the fenced copy. +func TestReassignedLeaderReplacesItsFencedPartition(t *testing.T) { + f, b := newFencingFiler(t) + b.localTopicManager = topic.NewLocalTopicManager() + b.fca = &filer_client.FilerClientAccessor{ + GetGrpcDialOption: b.GetGrpcDialOption, + GetFilers: func() []pb.ServerAddress { return []pb.ServerAddress{b.GetFiler()} }, + } + assign := func(epoch int64) { + t.Helper() + if _, err := b.AssignTopicPartitions(context.Background(), &mq_pb.AssignTopicPartitionsRequest{ + Topic: fenceTestTopic.ToPbTopic(), + BrokerPartitionAssignments: []*mq_pb.BrokerPartitionAssignment{{Partition: fenceTestPartition.ToPbPartition(), Epoch: epoch}}, + }); err != nil { + t.Fatal(err) + } + } + assign(1) + old := b.localTopicManager.GetLocalPartition(fenceTestTopic, fenceTestPartition) + old.Fence.MarkLost() + + assign(3) + current := b.localTopicManager.GetLocalPartition(fenceTestTopic, fenceTestPartition) + if current == old || current.IsFenced() { + t.Fatal("still serving the fenced partition") + } + if epoch, _ := fenceEpoch(f.entry(topic.PartitionFencePath(fenceTestTopic, fenceTestPartition))); epoch != 3 { + t.Fatalf("fence at epoch %d, want 3", epoch) + } +} diff --git a/weed/mq/broker/broker_topic_conf_read_write.go b/weed/mq/broker/broker_topic_conf_read_write.go index f1060f8dfe..4b73f1e440 100644 --- a/weed/mq/broker/broker_topic_conf_read_write.go +++ b/weed/mq/broker/broker_topic_conf_read_write.go @@ -137,7 +137,13 @@ func (b *MessageQueueBroker) doGetOrGenLocalPartition(t topic.Topic, partition t b.accessLock.Lock() defer b.accessLock.Unlock() - if localPartition = b.localTopicManager.GetLocalPartition(t, partition); localPartition == nil { + localPartition = b.localTopicManager.GetLocalPartition(t, partition) + if localPartition != nil && localPartition.IsFenced() { + // another broker took it; this broker may lead it again at a newer epoch + b.localTopicManager.RemoveLocalPartition(t, partition) + localPartition = nil + } + if localPartition == nil { localPartition, isGenerated, err = b.genLocalPartitionFromFiler(t, partition, conf) if err != nil { return nil, false, err