mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 02:27:26 +01:00
mq broker: hold the partition fence by a token, not a read-back entry
Taking the fence read the written entry back through the broker's filer, which may lag the fence's owner filer; a successful take then looked lost and the new leader did not load. The fence entry now carries a fresh token as its etag, and later writes require that etag on the owner, so nothing is read back. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6cadc69026
commit
a3e08cc1ae
5 files changed
+55
-26
No files matched your search
@@ -67,8 +67,14 @@ func (b *MessageQueueBroker) takePartitionFence(ctx context.Context, t topic.Top
|
||||
return nil, fmt.Errorf("fence %s is sealed", fencePath)
|
||||
}
|
||||
condition = &filer_pb.WriteCondition_Clause{Kind: filer_pb.WriteCondition_IF_ENTRY_EQUAL, ExpectedEntry: current}
|
||||
if token := current.Extended[s3_constants.ExtETagKey]; len(token) > 0 {
|
||||
condition = fenceTokenCondition(string(token))
|
||||
}
|
||||
}
|
||||
|
||||
// The token is the fence entry's etag, so writes can require it without reading
|
||||
// the entry back through a filer that may not have caught up with the owner.
|
||||
token := uuid.NewString()
|
||||
now := time.Now().Unix()
|
||||
fenceEntry := &filer_pb.Entry{
|
||||
Name: name,
|
||||
@@ -80,6 +86,7 @@ func (b *MessageQueueBroker) takePartitionFence(ctx context.Context, t topic.Top
|
||||
Extended: map[string][]byte{
|
||||
mq.ExtendedAttrFenceWriter: []byte(self),
|
||||
mq.ExtendedAttrFenceEpoch: []byte(strconv.FormatInt(epoch, 10)),
|
||||
s3_constants.ExtETagKey: []byte(token),
|
||||
},
|
||||
}
|
||||
if err := b.fencedTransaction(ctx, string(fencePath), condition, uuid.NewString(), &filer_pb.ObjectMutation{
|
||||
@@ -89,17 +96,13 @@ func (b *MessageQueueBroker) takePartitionFence(ctx context.Context, t topic.Top
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// The filer fills in attributes, and later writes must present the entry as stored.
|
||||
held, _, _, err := filer_pb.GetEntry(ctx, b, fencePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read back fence %s: %w", fencePath, err)
|
||||
}
|
||||
if heldEpoch, _ := fenceEpoch(held); string(held.Extended[mq.ExtendedAttrFenceWriter]) != self || heldEpoch != epoch {
|
||||
return nil, fmt.Errorf("fence %s taken by %s at epoch %d: %w", fencePath, held.Extended[mq.ExtendedAttrFenceWriter], heldEpoch, errFenceLost)
|
||||
}
|
||||
glog.V(0).Infof("broker %s took fence %s at epoch %d", self, fencePath, epoch)
|
||||
return &topic.PartitionFence{Path: string(fencePath), Entry: held}, nil
|
||||
return &topic.PartitionFence{Path: string(fencePath), Token: token}, nil
|
||||
}
|
||||
|
||||
// fenceTokenCondition holds while the fence entry is still the one written with token.
|
||||
func fenceTokenCondition(token string) *filer_pb.WriteCondition_Clause {
|
||||
return &filer_pb.WriteCondition_Clause{Kind: filer_pb.WriteCondition_IF_ETAG_MATCH, Etags: []string{token}}
|
||||
}
|
||||
|
||||
// fencedTransaction applies mutation on the filer owning the fence while condition
|
||||
|
||||
@@ -7,10 +7,12 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/mq"
|
||||
"github.com/seaweedfs/seaweedfs/weed/mq/topic"
|
||||
@@ -37,6 +39,8 @@ type fencingFiler struct {
|
||||
dropResponses int
|
||||
// records a mutation failure for the next transactions, as a store error does
|
||||
failMutations int
|
||||
// lookups answer from these entries, as a filer that has not caught up with the owner does
|
||||
lagging map[string]*filer_pb.Entry
|
||||
// runs under the lock before a transaction applies, to interleave another writer
|
||||
beforeApply func()
|
||||
volume *httptest.Server
|
||||
@@ -46,7 +50,11 @@ type fencingFiler struct {
|
||||
func (f *fencingFiler) LookupDirectoryEntry(_ context.Context, req *filer_pb.LookupDirectoryEntryRequest) (*filer_pb.LookupDirectoryEntryResponse, error) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
entry, found := f.entries[string(util.NewFullPath(req.Directory, req.Name))]
|
||||
entries := f.entries
|
||||
if f.lagging != nil {
|
||||
entries = f.lagging
|
||||
}
|
||||
entry, found := entries[string(util.NewFullPath(req.Directory, req.Name))]
|
||||
if !found {
|
||||
return nil, status.Error(codes.NotFound, filer_pb.ErrNotFound.Error())
|
||||
}
|
||||
@@ -86,6 +94,8 @@ func (f *fencingFiler) apply(req *filer_pb.ObjectTransactionRequest) *filer_pb.O
|
||||
holds = current == nil
|
||||
case filer_pb.WriteCondition_IF_ENTRY_EQUAL:
|
||||
holds = current != nil && clause.ExpectedEntry != nil && proto.Equal(current, clause.ExpectedEntry)
|
||||
case filer_pb.WriteCondition_IF_ETAG_MATCH:
|
||||
holds = current != nil && slices.Contains(clause.Etags, string(current.Extended[s3_constants.ExtETagKey]))
|
||||
}
|
||||
if !holds {
|
||||
return &filer_pb.ObjectTransactionResponse{Error: "precondition failed", ErrorCode: filer_pb.FilerError_PRECONDITION_FAILED}
|
||||
@@ -183,8 +193,8 @@ func TestTakeAbsentFenceRequiresItStillAbsent(t *testing.T) {
|
||||
t.Errorf("fence carries %s", key)
|
||||
}
|
||||
}
|
||||
if fence.Path != path || !proto.Equal(fence.Entry, f.entry(path)) {
|
||||
t.Errorf("held fence %v is not the stored %v", fence.Entry, f.entry(path))
|
||||
if fence.Path != path || fence.Token == "" || string(f.entry(path).Extended[s3_constants.ExtETagKey]) != fence.Token {
|
||||
t.Errorf("held fence token %q is not the stored %v", fence.Token, f.entry(path))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -206,14 +216,33 @@ func TestTakePresentFenceRequiresTheEntryAsRead(t *testing.T) {
|
||||
if len(clauses) != 1 || clauses[0].Kind != filer_pb.WriteCondition_IF_ENTRY_EQUAL || !proto.Equal(clauses[0].ExpectedEntry, previous) {
|
||||
t.Errorf("condition %v, want IF_ENTRY_EQUAL on %v", clauses, previous)
|
||||
}
|
||||
if string(fence.Entry.Extended[mq.ExtendedAttrFenceEpoch]) != "3" || !proto.Equal(fence.Entry, f.entry(path)) {
|
||||
t.Errorf("held fence %v", fence.Entry)
|
||||
if stored := f.entry(path); string(stored.Extended[mq.ExtendedAttrFenceEpoch]) != "3" || string(stored.Extended[s3_constants.ExtETagKey]) != fence.Token {
|
||||
t.Errorf("stored fence %v, held token %q", stored, fence.Token)
|
||||
}
|
||||
|
||||
// the same epoch can be taken again, as a restarted leader does
|
||||
if _, err := b.takePartitionFence(context.Background(), fenceTestTopic, fenceTestPartition, 3); err != nil {
|
||||
// the same epoch can be taken again, as a restarted leader does, requiring the token as read
|
||||
again, err := b.takePartitionFence(context.Background(), fenceTestTopic, fenceTestPartition, 3)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if clauses := f.transactions()[1].Condition.GetClauses(); len(clauses) != 1 || clauses[0].Kind != filer_pb.WriteCondition_IF_ETAG_MATCH || !slices.Equal(clauses[0].Etags, []string{fence.Token}) {
|
||||
t.Errorf("retake condition %v, want the token %q", clauses, fence.Token)
|
||||
}
|
||||
if again.Token == fence.Token {
|
||||
t.Error("a retaken fence kept its token")
|
||||
}
|
||||
}
|
||||
|
||||
// The take is decided by the owner's transaction, so a filer whose reads lag
|
||||
// behind the owner does not turn a taken fence into a lost one.
|
||||
func TestTakeFenceThroughALaggingFiler(t *testing.T) {
|
||||
f, b := newFencingFiler(t)
|
||||
f.lagging = map[string]*filer_pb.Entry{}
|
||||
localPartition, flush := newFencedPartition(t, b)
|
||||
flush(localPartition.LogBuffer, time.Now(), time.Now(), []byte("data"), 0, 9)
|
||||
if localPartition.IsFenced() || len(f.transactions()) != 2 {
|
||||
t.Errorf("fenced=%v after %d transactions", localPartition.IsFenced(), len(f.transactions()))
|
||||
}
|
||||
}
|
||||
|
||||
func TestTakeFenceRefusesAHigherEpoch(t *testing.T) {
|
||||
|
||||
@@ -74,7 +74,7 @@ func (b *MessageQueueBroker) writeLogFile(ctx context.Context, fence *topic.Part
|
||||
}
|
||||
}
|
||||
|
||||
condition := &filer_pb.WriteCondition_Clause{Kind: filer_pb.WriteCondition_IF_ENTRY_EQUAL, ExpectedEntry: fence.Entry}
|
||||
condition := fenceTokenCondition(fence.Token)
|
||||
mutation := &filer_pb.ObjectMutation{Type: filer_pb.ObjectMutation_PUT, Directory: dir, Entry: entry}
|
||||
requestId := uuid.NewString()
|
||||
err := retryLogWrite(ctx, targetFile, func() error {
|
||||
|
||||
@@ -16,7 +16,6 @@ import (
|
||||
"github.com/seaweedfs/seaweedfs/weed/util/log_buffer"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
func TestOffsetRangeIncludesFirstWindow(t *testing.T) {
|
||||
@@ -73,7 +72,7 @@ func TestStoppingBrokerAbandonsAFailingFlush(t *testing.T) {
|
||||
currentFiler: pb.NewServerAddressWithGrpcPort(listener.Addr().String(), listener.Addr().(*net.TCPAddr).Port),
|
||||
flushCtx: flushCtx,
|
||||
}
|
||||
fence := &topic.PartitionFence{Path: topic.PartitionFencePath(fenceTestTopic, fenceTestPartition), Entry: &filer_pb.Entry{}}
|
||||
fence := &topic.PartitionFence{Path: topic.PartitionFencePath(fenceTestTopic, fenceTestPartition), Token: "token"}
|
||||
flush := b.genLogFlushFunc(fenceTestTopic, fenceTestPartition, fence)
|
||||
logBuffer := log_buffer.NewLogBuffer("test", time.Hour, flush, nil, nil)
|
||||
defer logBuffer.ShutdownLogBuffer()
|
||||
@@ -137,8 +136,8 @@ func TestFlushWritesOneFencedFilePerWindow(t *testing.T) {
|
||||
if txn.LockKey != fence.Path || txn.ConditionKey != fence.Path || txn.RouteKey != s3_constants.ObjectWriteRouteKeyPrefix+fence.Path {
|
||||
t.Errorf("transaction keys lock=%q condition=%q route=%q", txn.LockKey, txn.ConditionKey, txn.RouteKey)
|
||||
}
|
||||
if clauses := txn.Condition.GetClauses(); len(clauses) != 1 || clauses[0].Kind != filer_pb.WriteCondition_IF_ENTRY_EQUAL || !proto.Equal(clauses[0].ExpectedEntry, fence.Entry) {
|
||||
t.Errorf("condition %v, want IF_ENTRY_EQUAL on the held fence", txn.Condition)
|
||||
if clauses := txn.Condition.GetClauses(); len(clauses) != 1 || clauses[0].Kind != filer_pb.WriteCondition_IF_ETAG_MATCH || len(clauses[0].Etags) != 1 || clauses[0].Etags[0] != fence.Token {
|
||||
t.Errorf("condition %v, want the held fence's token", txn.Condition)
|
||||
}
|
||||
|
||||
partitionDir := topic.PartitionDir(fenceTestTopic, fenceTestPartition)
|
||||
|
||||
@@ -2,17 +2,15 @@ package topic
|
||||
|
||||
import (
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
||||
)
|
||||
|
||||
// PartitionFenceName is the partition directory entry naming the one broker allowed to write it.
|
||||
const PartitionFenceName = ".fence"
|
||||
|
||||
// PartitionFence is the fence entry exactly as this broker stored it; every write presents it unchanged.
|
||||
// PartitionFence is the fence this broker stored; every write requires its token unchanged.
|
||||
type PartitionFence struct {
|
||||
Path string
|
||||
Entry *filer_pb.Entry
|
||||
Token string
|
||||
lost atomic.Bool
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user