mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 02:27:26 +01:00
mq broker: a leader that loses its follower stores what it holds and ends the generation
When the follower stream broke, the leader kept serving alone, acking from memory again. A broken pair now ends the generation on the leader: it stops taking publishes, renews its fence so a new leader waits, stores everything it holds under that fence, seals it, and reports the epoch so the balancer assigns a new pair. A follower that cannot be reached when the partition loads ends the generation the same way.
This commit is contained in:
1 parent
abbb19abcf
commit
27711c0145
5 files changed
+120
-2
No files matched your search
@@ -2,6 +2,7 @@ package broker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/glog"
|
||||
@@ -50,3 +51,43 @@ func (b *MessageQueueBroker) EndPartitionGeneration(ctx context.Context, req *mq
|
||||
}
|
||||
return nil, status.Errorf(codes.NotFound, "topic %s has no partition %v", t, topic.FromPbPartition(req.Partition))
|
||||
}
|
||||
|
||||
// watchReplica ends this leader's generation when its follower is lost.
|
||||
func (b *MessageQueueBroker) watchReplica(t topic.Topic, p topic.Partition, lp *topic.LocalPartition, epoch int64) {
|
||||
<-lp.Replica.Broken()
|
||||
if errors.Is(lp.Replica.Err(), topic.ErrReplicaClosed) {
|
||||
return
|
||||
}
|
||||
glog.Warningf("topic %s partition %v epoch %d lost follower %s: %v", t, p, epoch, lp.Follower, lp.Replica.Err())
|
||||
b.endLeaderGeneration(t, lp, epoch)
|
||||
}
|
||||
|
||||
// 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.
|
||||
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)
|
||||
}
|
||||
b.localTopicManager.RemoveLocalPartition(t, lp)
|
||||
b.localTopicManager.WaitRetired(ctx, t, p)
|
||||
b.sealAndReportGeneration(ctx, t, p, fence, epoch)
|
||||
}
|
||||
|
||||
func (b *MessageQueueBroker) sealAndReportGeneration(ctx context.Context, t topic.Topic, p topic.Partition, fence *topic.PartitionFence, epoch int64) {
|
||||
if fence != nil && !fence.IsLost() {
|
||||
if err := b.renewPartitionFence(ctx, fence, true); err != nil {
|
||||
glog.Warningf("seal topic %s partition %v epoch %d: %v", t, p, epoch, err)
|
||||
}
|
||||
}
|
||||
_, err := b.EndPartitionGeneration(ctx, &mq_pb.EndPartitionGenerationRequest{
|
||||
Topic: t.ToPbTopic(),
|
||||
Partition: p.ToPbPartition(),
|
||||
Epoch: epoch,
|
||||
Broker: string(b.option.BrokerAddress()),
|
||||
})
|
||||
if err != nil {
|
||||
glog.Warningf("report topic %s partition %v epoch %d ended: %v", t, p, epoch, err)
|
||||
}
|
||||
}
|
||||
@@ -74,6 +74,10 @@ func (b *MessageQueueBroker) PublishMessage(stream mq_pb.SeaweedMessaging_Publis
|
||||
response.ErrorCode, response.Error = CreateBrokerError(BrokerErrorNotLeaderOrFollower, getOrGenErr.Error())
|
||||
return stream.Send(response)
|
||||
}
|
||||
if errors.Is(getOrGenErr, errGenerationEnded) {
|
||||
response.ErrorCode, response.Error = CreateBrokerError(BrokerErrorGenerationEnded, getOrGenErr.Error())
|
||||
return stream.Send(response)
|
||||
}
|
||||
if getOrGenErr != nil {
|
||||
response.ErrorCode, response.Error = CreateBrokerError(BrokerErrorTopicNotFound, fmt.Sprintf("topic %v not found: %v", t, getOrGenErr))
|
||||
glog.Errorf("topic %v not found: %v", t, getOrGenErr)
|
||||
@@ -88,6 +92,10 @@ func (b *MessageQueueBroker) PublishMessage(stream mq_pb.SeaweedMessaging_Publis
|
||||
response.ErrorCode, response.Error = CreateBrokerError(BrokerErrorNotLeaderOrFollower, fmt.Sprintf("topic %v partition %v is fenced by another broker", t, p))
|
||||
return stream.Send(response)
|
||||
}
|
||||
if replica := localTopicPartition.Replica; replica != nil && replica.Err() != nil {
|
||||
response.ErrorCode, response.Error = CreateBrokerError(BrokerErrorGenerationEnded, fmt.Sprintf("topic %v partition %v: %v", t, p, replica.Err()))
|
||||
return stream.Send(response)
|
||||
}
|
||||
|
||||
// process each published messages
|
||||
clientName := fmt.Sprintf("%v-%4d", findClientAddress(stream.Context()), rand.IntN(10000))
|
||||
|
||||
@@ -21,7 +21,8 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
errFenceLost = errors.New("partition fence is held by another broker")
|
||||
errFenceLost = errors.New("partition fence is held by another broker")
|
||||
errGenerationEnded = errors.New("partition generation ended")
|
||||
// the filer recorded this failure under the request id, so a retry of that id replays it
|
||||
errTransactionFailed = errors.New("transaction failed")
|
||||
)
|
||||
@@ -55,9 +56,11 @@ func (b *MessageQueueBroker) newLeaderPartition(t topic.Topic, partition topic.P
|
||||
})
|
||||
if err != nil {
|
||||
localPartition.Shutdown()
|
||||
return nil, err
|
||||
b.sealAndReportGeneration(context.Background(), t, partition, fence, epoch)
|
||||
return nil, fmt.Errorf("%v: %w", err, errGenerationEnded)
|
||||
}
|
||||
localPartition.Replica, localPartition.Follower = replica, follower
|
||||
go b.watchReplica(t, partition, localPartition, epoch)
|
||||
}
|
||||
return localPartition, nil
|
||||
}
|
||||
|
||||
@@ -6,10 +6,14 @@ 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/pb/schema_pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/util"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
)
|
||||
@@ -103,3 +107,60 @@ func TestReplicaBreaksWhenTheFollowerFallsSilent(t *testing.T) {
|
||||
t.Fatal("pair not broken after the follower fell silent")
|
||||
}
|
||||
}
|
||||
|
||||
// A leader that loses its follower stores what it holds under its fence and seals it.
|
||||
func TestLeaderStoresAndSealsWhenItLosesItsFollower(t *testing.T) {
|
||||
f, leader := newFencingFiler(t)
|
||||
leader.localTopicManager = topic.NewLocalTopicManager()
|
||||
leader.fca = &filer_client.FilerClientAccessor{
|
||||
GetGrpcDialOption: leader.GetGrpcDialOption,
|
||||
GetFilers: func() []pb.ServerAddress { return []pb.ServerAddress{leader.GetFiler()} },
|
||||
}
|
||||
followerAddr, followerServer := startFollower(t, &MessageQueueBroker{})
|
||||
|
||||
lp, err := leader.newLeaderPartition(fenceTestTopic, fenceTestPartition, &mq_pb.BrokerPartitionAssignment{
|
||||
Partition: fenceTestPartition.ToPbPartition(),
|
||||
LeaderBroker: "127.0.0.1:17777",
|
||||
FollowerBroker: followerAddr,
|
||||
Epoch: 1,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
leader.localTopicManager.AddLocalPartition(fenceTestTopic, lp)
|
||||
for i := 0; i < 10; i++ {
|
||||
offset, err := lp.PublishWithOffset(&mq_pb.DataMessage{Key: []byte("k"), Value: []byte("v"), TsNs: time.Now().UnixNano()})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := lp.Replica.WaitAcked(offset); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
followerServer.Stop()
|
||||
|
||||
fencePath := topic.PartitionFencePath(fenceTestTopic, fenceTestPartition)
|
||||
deadline := time.Now().Add(20 * time.Second)
|
||||
for !fenceSealed(f.entry(fencePath)) {
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatal("fence not sealed after the follower was lost")
|
||||
}
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
}
|
||||
if _, err := lp.PublishWithOffset(&mq_pb.DataMessage{Key: []byte("k"), Value: []byte("late"), TsNs: time.Now().UnixNano()}); err == nil {
|
||||
t.Fatal("a broken pair accepted a publish")
|
||||
}
|
||||
var stored mq.OffsetRange
|
||||
dir := topic.PartitionDir(fenceTestTopic, fenceTestPartition)
|
||||
f.mu.Lock()
|
||||
for path, entry := range f.entries {
|
||||
if d, name := util.FullPath(path).DirAndName(); d == dir && name != topic.PartitionFenceName {
|
||||
stored.Add(entry.Extended)
|
||||
}
|
||||
}
|
||||
f.mu.Unlock()
|
||||
if earliest, hwm := stored.Bounds(); earliest != 0 || hwm != 10 {
|
||||
t.Fatalf("stored offsets %d..%d, want 0..9", earliest, hwm-1)
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,11 @@ import (
|
||||
// On a replicated partition the entry is queued for the follower in offset order.
|
||||
func (p *LocalPartition) PublishWithOffset(message *mq_pb.DataMessage) (int64, error) {
|
||||
p.publishMu.Lock()
|
||||
if p.Replica != nil && p.Replica.Err() != nil {
|
||||
// a broken pair stores nothing new; the publisher resends to the next pair
|
||||
p.publishMu.Unlock()
|
||||
return 0, p.Replica.Err()
|
||||
}
|
||||
entry, err := p.addToBuffer(message)
|
||||
if err == nil && p.Replica != nil {
|
||||
err = p.Replica.Send(entry)
|
||||
|
||||
Reference in new issue
Block a user