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.
This commit is contained in:
Chris Lu committed 2026-11-30 19:48:06 -08:00
1 parent 9964b86f8b
commit d605d2527f
3 files changed
+45 -1

No files matched your search

+4
View File
@@ -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 {
@@ -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)
}
}
@@ -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