diff --git a/weed/mq/broker/broker_partition_fence.go b/weed/mq/broker/broker_partition_fence.go index 83a58783bd..5f02a61de9 100644 --- a/weed/mq/broker/broker_partition_fence.go +++ b/weed/mq/broker/broker_partition_fence.go @@ -24,10 +24,15 @@ var ( errTransactionFailed = errors.New("transaction failed") ) +// fenceTakeTimeout bounds the filer calls a fence take makes while holding accessLock. +var fenceTakeTimeout = 10 * time.Second + // newLeaderPartition takes the partition's fence for epoch before anything reads // the partition's files, so a previous leader can no longer add to them. func (b *MessageQueueBroker) newLeaderPartition(t topic.Topic, partition topic.Partition, epoch int64) (*topic.LocalPartition, error) { - fence, err := b.takePartitionFence(context.Background(), t, partition, epoch) + ctx, cancel := context.WithTimeout(context.Background(), fenceTakeTimeout) + defer cancel() + fence, err := b.takePartitionFence(ctx, t, partition, epoch) if err != nil { return nil, fmt.Errorf("take fence of %s %s at epoch %d: %w", t, partition, epoch, err) } diff --git a/weed/mq/broker/broker_partition_fence_test.go b/weed/mq/broker/broker_partition_fence_test.go index 501622f11f..bac90415b2 100644 --- a/weed/mq/broker/broker_partition_fence_test.go +++ b/weed/mq/broker/broker_partition_fence_test.go @@ -41,14 +41,21 @@ type fencingFiler struct { 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 + // lookups wait for their caller to give up, as a stalled filer does + stalled bool // runs under the lock before a transaction applies, to interleave another writer beforeApply func() volume *httptest.Server uploads atomic.Int32 } -func (f *fencingFiler) LookupDirectoryEntry(_ context.Context, req *filer_pb.LookupDirectoryEntryRequest) (*filer_pb.LookupDirectoryEntryResponse, error) { +func (f *fencingFiler) LookupDirectoryEntry(ctx context.Context, req *filer_pb.LookupDirectoryEntryRequest) (*filer_pb.LookupDirectoryEntryResponse, error) { f.mu.Lock() + if f.stalled { + f.mu.Unlock() + <-ctx.Done() + return nil, ctx.Err() + } defer f.mu.Unlock() entries := f.entries if f.lagging != nil { @@ -272,3 +279,24 @@ func TestTakeFenceLosingTheRace(t *testing.T) { t.Fatalf("took a fence another broker wrote first: %v", err) } } + +func TestTakeFenceGivesUpOnAStalledFiler(t *testing.T) { + f, b := newFencingFiler(t) + f.stalled = true + defer func(timeout time.Duration) { fenceTakeTimeout = timeout }(fenceTakeTimeout) + fenceTakeTimeout = 100 * time.Millisecond + + done := make(chan error, 1) + go func() { + _, err := b.newLeaderPartition(fenceTestTopic, fenceTestPartition, 1) + done <- err + }() + select { + case err := <-done: + if err == nil { + t.Fatal("took a fence through a stalled filer") + } + case <-time.After(10 * time.Second): + t.Fatal("fence take waited on a stalled filer") + } +}