mq broker: a follower that cannot ack its leader takes over

An ack that failed to send ended the follow stream with an error and
dropped the follower's entries, though they may include messages the
leader had already acked to publishers. A failed ack now takes over
the partition, as a broken receive does.
This commit is contained in:
Chris Lu committed 2026-11-30 23:01:47 -08:00
1 parent 50b8652760
commit 3523e033ff
2 files changed
+70 -4

No files matched your search

+3 -1
View File
@@ -50,7 +50,9 @@ func (b *MessageQueueBroker) PublishFollowMe(stream mq_pb.SeaweedMessaging_Publi
continue
}
if err := stream.Send(&mq_pb.PublishFollowMeResponse{AckOffset: acked}); err != nil {
return err
glog.Warningf("follow stream of %v %v from %s at epoch %d failed to ack with %d unstored entries: %v", t, p, init.Leader, init.Epoch, len(held), err)
b.takeOverPartition(t, p, init, held)
return nil
}
}
}
+67 -3
View File
@@ -5,6 +5,7 @@ import (
"encoding/binary"
"errors"
"fmt"
"io"
"net"
"sort"
"testing"
@@ -248,8 +249,15 @@ func TestFollowerTakesOverALostLeader(t *testing.T) {
if writer := string(f.entry(fencePath).Extended[mq.ExtendedAttrFenceWriter]); writer != follower.option.BrokerAddress().String() {
t.Fatalf("fence held by %s", writer)
}
var stored []string
if stored, want := storedRanges(f, dir), []string{"0-4", "5-9"}; fmt.Sprint(stored) != fmt.Sprint(want) {
t.Fatalf("stored ranges %v, want %v", stored, want)
}
}
// storedRanges lists the offset ranges of the log files in dir.
func storedRanges(f *fencingFiler, dir string) (stored []string) {
f.mu.Lock()
defer f.mu.Unlock()
for path, entry := range f.entries {
if d, name := util.FullPath(path).DirAndName(); d == dir && name != topic.PartitionFenceName {
var r mq.OffsetRange
@@ -258,9 +266,65 @@ func TestFollowerTakesOverALostLeader(t *testing.T) {
stored = append(stored, fmt.Sprintf("%d-%d", earliest, hwm-1))
}
}
f.mu.Unlock()
sort.Strings(stored)
if want := []string{"0-4", "5-9"}; fmt.Sprint(stored) != fmt.Sprint(want) {
return stored
}
// ackFailingStream delivers the leader's messages but fails the follower's ack
// at failAt, as a connection that broke while the follower answered.
type ackFailingStream struct {
grpc.ServerStream
ctx context.Context
reqs []*mq_pb.PublishFollowMeRequest
acks int
failAt int
}
func (s *ackFailingStream) Context() context.Context { return s.ctx }
func (s *ackFailingStream) Recv() (*mq_pb.PublishFollowMeRequest, error) {
if len(s.reqs) == 0 {
<-s.ctx.Done()
return nil, s.ctx.Err()
}
req := s.reqs[0]
s.reqs = s.reqs[1:]
return req, nil
}
func (s *ackFailingStream) Send(*mq_pb.PublishFollowMeResponse) error {
if s.acks++; s.acks == s.failAt {
return io.ErrClosedPipe
}
return nil
}
func TestFollowerTakesOverWhenItCannotAck(t *testing.T) {
f, follower := newFencingFiler(t)
leaderAddr := "127.0.0.1:18888"
fencePath := topic.PartitionFencePath(fenceTestTopic, fenceTestPartition)
f.put(fencePath, fenceAt(leaderAddr, "1", time.Now().Unix(), false))
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
stream := &ackFailingStream{ctx: ctx, failAt: 5}
stream.reqs = append(stream.reqs, &mq_pb.PublishFollowMeRequest{Message: &mq_pb.PublishFollowMeRequest_Init{Init: &mq_pb.PublishFollowMeRequest_InitMessage{
Topic: fenceTestTopic.ToPbTopic(), Partition: fenceTestPartition.ToPbPartition(), Epoch: 1, Leader: leaderAddr,
}}})
for offset := int64(0); offset < 5; offset++ {
stream.reqs = append(stream.reqs, &mq_pb.PublishFollowMeRequest{Message: &mq_pb.PublishFollowMeRequest_Entry{Entry: &filer_pb.LogEntry{TsNs: time.Now().UnixNano(), Key: []byte("k"), Data: []byte("v"), Offset: offset}}})
}
done := make(chan error, 1)
go func() { done <- follower.PublishFollowMe(stream) }()
select {
case <-done:
case <-time.After(20 * time.Second):
t.Fatal("follower kept waiting after its ack failed")
}
if !fenceSealed(f.entry(fencePath)) {
t.Fatal("follower did not take over and seal")
}
if stored, want := storedRanges(f, topic.PartitionDir(fenceTestTopic, fenceTestPartition)), []string{"0-4"}; fmt.Sprint(stored) != fmt.Sprint(want) {
t.Fatalf("stored ranges %v, want %v", stored, want)
}
}