mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 11:07:31 +01:00
mq/offset: fail the test lookup the way a filer store does TestListConsumerGroupPositionsReturnsWhatItReadOnAFailure flaked (about 1 in 60 runs): its fake filer failed the unreadable position with codes.Unavailable and no details, which pb treats as a dead channel. The shared ClientConn was closed and the concurrent loads of the other partitions failed with it, so the listing returned one position, not two. A real filer answers a store read error with a plain error (codes.Unknown), which keeps the channel. The fake now does the same. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
438 lines
16 KiB
Go
438 lines
16 KiB
Go
package offset
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net"
|
|
"slices"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer_client"
|
|
"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/s3api/s3_constants"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
"google.golang.org/grpc/status"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
// conditionalFiler keeps entries in memory and evaluates the two write
|
|
// conditions a fenced save uses.
|
|
type conditionalFiler struct {
|
|
filer_pb.UnimplementedSeaweedFilerServer
|
|
mu sync.Mutex
|
|
entries map[string]*filer_pb.Entry
|
|
// beforeWrite runs once, ahead of the next transaction, to model a
|
|
// concurrent writer landing between a save's read and its write.
|
|
beforeWrite func()
|
|
// looked counts the lookups of each path
|
|
looked map[string]int
|
|
// failLookups fails the lookups of these paths the way a filer whose store
|
|
// cannot read them does
|
|
failLookups map[string]bool
|
|
}
|
|
|
|
func (f *conditionalFiler) LookupDirectoryEntry(ctx context.Context, req *filer_pb.LookupDirectoryEntryRequest) (*filer_pb.LookupDirectoryEntryResponse, error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
if f.looked == nil {
|
|
f.looked = map[string]int{}
|
|
}
|
|
f.looked[req.Directory+"/"+req.Name]++
|
|
if f.failLookups[req.Directory+"/"+req.Name] {
|
|
return nil, errors.New("store unavailable")
|
|
}
|
|
entry, ok := f.entries[req.Directory+"/"+req.Name]
|
|
if !ok {
|
|
return nil, status.Error(codes.NotFound, filer_pb.ErrNotFound.Error())
|
|
}
|
|
return &filer_pb.LookupDirectoryEntryResponse{Entry: proto.Clone(entry).(*filer_pb.Entry)}, nil
|
|
}
|
|
|
|
func (f *conditionalFiler) ObjectTransaction(ctx context.Context, req *filer_pb.ObjectTransactionRequest) (*filer_pb.ObjectTransactionResponse, error) {
|
|
f.mu.Lock()
|
|
hook := f.beforeWrite
|
|
f.beforeWrite = nil
|
|
f.mu.Unlock()
|
|
if hook != nil {
|
|
hook()
|
|
}
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
current := f.entries[req.ConditionKey]
|
|
for _, clause := range req.Condition.GetClauses() {
|
|
held := true
|
|
switch clause.Kind {
|
|
case filer_pb.WriteCondition_IF_NOT_EXISTS:
|
|
held = current == nil
|
|
case filer_pb.WriteCondition_IF_ENTRY_EQUAL:
|
|
held = current != nil && proto.Equal(current, clause.ExpectedEntry)
|
|
}
|
|
if !held {
|
|
return &filer_pb.ObjectTransactionResponse{ErrorCode: filer_pb.FilerError_PRECONDITION_FAILED, Error: "precondition failed"}, nil
|
|
}
|
|
}
|
|
for _, m := range req.Mutations {
|
|
f.entries[m.Directory+"/"+m.Entry.Name] = proto.Clone(m.Entry).(*filer_pb.Entry)
|
|
}
|
|
return &filer_pb.ObjectTransactionResponse{}, nil
|
|
}
|
|
|
|
func (f *conditionalFiler) CreateEntry(ctx context.Context, req *filer_pb.CreateEntryRequest) (*filer_pb.CreateEntryResponse, error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.entries[req.Directory+"/"+req.Entry.Name] = proto.Clone(req.Entry).(*filer_pb.Entry)
|
|
return &filer_pb.CreateEntryResponse{}, nil
|
|
}
|
|
|
|
func (f *conditionalFiler) UpdateEntry(ctx context.Context, req *filer_pb.UpdateEntryRequest) (*filer_pb.UpdateEntryResponse, error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.entries[req.Directory+"/"+req.Entry.Name] = proto.Clone(req.Entry).(*filer_pb.Entry)
|
|
return &filer_pb.UpdateEntryResponse{}, nil
|
|
}
|
|
|
|
// ListEntries lists the stored entries directly under req.Directory and the
|
|
// directories their paths imply.
|
|
func (f *conditionalFiler) ListEntries(req *filer_pb.ListEntriesRequest, stream filer_pb.SeaweedFiler_ListEntriesServer) error {
|
|
f.mu.Lock()
|
|
children := map[string]*filer_pb.Entry{}
|
|
for path, entry := range f.entries {
|
|
rest, ok := strings.CutPrefix(path, req.Directory+"/")
|
|
if !ok {
|
|
continue
|
|
}
|
|
if name, _, deeper := strings.Cut(rest, "/"); deeper {
|
|
children[name] = &filer_pb.Entry{Name: name, IsDirectory: true}
|
|
} else {
|
|
children[name] = entry
|
|
}
|
|
}
|
|
f.mu.Unlock()
|
|
names := make([]string, 0, len(children))
|
|
for name := range children {
|
|
names = append(names, name)
|
|
}
|
|
slices.Sort(names)
|
|
for _, name := range names {
|
|
if name <= req.StartFromFileName {
|
|
continue
|
|
}
|
|
if err := stream.Send(&filer_pb.ListEntriesResponse{Entry: children[name]}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func TestListConsumerGroupPositionsAcrossGenerations(t *testing.T) {
|
|
_, s := newFencedStorage(t)
|
|
later := topic.Partition{RingSize: 4096, RangeStart: 1024, RangeStop: 2048, UnixTimeNs: 3_000_000_000}
|
|
if err := savePosition(s, 10, 5, "a"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.SaveConsumerGroupPosition(context.Background(), fenceTopic, later, "forwarder", &ConsumerGroupPosition{Type: "offset", Value: 20}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.SaveConsumerGroupPosition(context.Background(), fenceTopic, later, "other", &ConsumerGroupPosition{Type: "offset", Value: 30}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// a partition only another group has a position in is listed without one
|
|
untouched := topic.Partition{RingSize: 4096, RangeStart: 2048, RangeStop: 4096, UnixTimeNs: 3_000_000_000}
|
|
if err := s.SaveConsumerGroupPosition(context.Background(), fenceTopic, untouched, "other", &ConsumerGroupPosition{Type: "offset", Value: 40}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
positions, err := s.ListConsumerGroupPositions(context.Background(), fenceTopic, "forwarder", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
value := func(p topic.Partition) int64 {
|
|
if position := positions[topic.PartitionDir(fenceTopic, p)]; position != nil {
|
|
return position.Value
|
|
}
|
|
return -1
|
|
}
|
|
none, listed := positions[topic.PartitionDir(fenceTopic, untouched)]
|
|
if len(positions) != 3 || value(fencePartition) != 10 || value(later) != 20 || !listed || none != nil {
|
|
t.Fatalf("listed %v", positions)
|
|
}
|
|
}
|
|
|
|
// A listing whose read of one position fails still returns the positions it
|
|
// read, so a caller retrying need not read them again.
|
|
func TestListConsumerGroupPositionsReturnsWhatItReadOnAFailure(t *testing.T) {
|
|
f, s := newFencedStorage(t)
|
|
var dirs []string
|
|
for generation := int64(1); generation <= 3; generation++ {
|
|
p := topic.Partition{RingSize: 4096, RangeStart: 0, RangeStop: 4096, UnixTimeNs: generation * 1_000_000_000}
|
|
if err := s.SaveConsumerGroupPosition(context.Background(), fenceTopic, p, "forwarder", &ConsumerGroupPosition{Type: "offset", Value: generation}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
dirs = append(dirs, topic.PartitionDir(fenceTopic, p))
|
|
}
|
|
f.mu.Lock()
|
|
f.failLookups = map[string]bool{dirs[1] + "/consumers/forwarder.offset": true}
|
|
f.mu.Unlock()
|
|
|
|
positions, err := s.ListConsumerGroupPositions(context.Background(), fenceTopic, "forwarder", nil)
|
|
if err == nil {
|
|
t.Fatal("listing with an unreadable position succeeded")
|
|
}
|
|
first, third := positions[dirs[0]], positions[dirs[2]]
|
|
if _, failed := positions[dirs[1]]; len(positions) != 2 || failed || first == nil || first.Value != 1 || third == nil || third.Value != 3 {
|
|
t.Fatalf("listed %v, want the two positions that could be read", positions)
|
|
}
|
|
}
|
|
|
|
// Of many historical partition directories, a listing reads the positions of
|
|
// only those the caller does not already know about.
|
|
func TestListConsumerGroupPositionsLoadsOnlyWhatIsNotSkipped(t *testing.T) {
|
|
f, s := newFencedStorage(t)
|
|
var unknown []string
|
|
for generation := int64(1); generation <= 40; generation++ {
|
|
p := topic.Partition{RingSize: 4096, RangeStart: 0, RangeStop: 4096, UnixTimeNs: generation * 1_000_000_000}
|
|
if err := s.SaveConsumerGroupPosition(context.Background(), fenceTopic, p, "forwarder", &ConsumerGroupPosition{Type: "offset", Value: generation}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if generation%20 == 0 {
|
|
unknown = append(unknown, topic.PartitionDir(fenceTopic, p))
|
|
}
|
|
}
|
|
f.mu.Lock()
|
|
f.looked = nil
|
|
f.mu.Unlock()
|
|
|
|
positions, err := s.ListConsumerGroupPositions(context.Background(), fenceTopic, "forwarder", func(dir string) bool {
|
|
return !slices.Contains(unknown, dir)
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
first, second := positions[unknown[0]], positions[unknown[1]]
|
|
if len(positions) != 2 || first == nil || first.Value != 20 || second == nil || second.Value != 40 {
|
|
t.Fatalf("listed %v, want only %v", positions, unknown)
|
|
}
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
loads := 0
|
|
for path, n := range f.looked {
|
|
if strings.HasSuffix(path, ".offset") {
|
|
loads += n
|
|
}
|
|
}
|
|
if loads != 2 {
|
|
t.Fatalf("%d position loads, want 2", loads)
|
|
}
|
|
}
|
|
|
|
func startConditionalFiler(t *testing.T) (*conditionalFiler, pb.ServerAddress) {
|
|
t.Helper()
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f := &conditionalFiler{entries: map[string]*filer_pb.Entry{}}
|
|
server := grpc.NewServer()
|
|
filer_pb.RegisterSeaweedFilerServer(server, f)
|
|
go server.Serve(listener)
|
|
t.Cleanup(server.Stop)
|
|
return f, pb.NewServerAddressWithGrpcPort(listener.Addr().String(), listener.Addr().(*net.TCPAddr).Port)
|
|
}
|
|
|
|
func newFencedStorage(t *testing.T) (*conditionalFiler, *FilerConsumerGroupOffsetStorage) {
|
|
t.Helper()
|
|
f, address := startConditionalFiler(t)
|
|
accessor := filer_client.NewFilerClientAccessor([]pb.ServerAddress{address}, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
|
return f, NewFilerConsumerGroupOffsetStorageWithAccessor(accessor)
|
|
}
|
|
|
|
func TestLoadReadsAtTheOwnerFiler(t *testing.T) {
|
|
// the accessor's filer still holds an older position than the key's owner
|
|
_, s := newFencedStorage(t)
|
|
if err := savePosition(s, 50, 5, "a"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, ownerAddress := startConditionalFiler(t)
|
|
s.WriteOwner = func(routeKey string) pb.ServerAddress { return ownerAddress }
|
|
if _, err := s.LoadConsumerGroupPosition(context.Background(), fenceTopic, fencePartition, "forwarder"); !errors.Is(err, filer_pb.ErrNotFound) {
|
|
t.Fatalf("load before the owner holds a position: %v, want ErrNotFound", err)
|
|
}
|
|
if err := savePosition(s, 100, 6, "b"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := s.LoadConsumerGroupPosition(context.Background(), fenceTopic, fencePartition, "forwarder")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.Value != 100 || got.Generation != 6 {
|
|
t.Fatalf("loaded %+v, want the owner's offset 100 at generation 6", got)
|
|
}
|
|
}
|
|
|
|
// While the owner of a position is not known yet, as before the lock ring is
|
|
// learned, another filer's answer is not taken: its "not found" may only mean
|
|
// it has not caught up, and a takeover there would rewind the partition.
|
|
func TestAPositionWaitsForItsOwnerToBeKnown(t *testing.T) {
|
|
_, s := newFencedStorage(t)
|
|
s.WriteOwner = func(string) pb.ServerAddress { return "" }
|
|
if _, err := s.LoadConsumerGroupPosition(context.Background(), fenceTopic, fencePartition, "forwarder"); err == nil || errors.Is(err, filer_pb.ErrNotFound) {
|
|
t.Fatalf("load with no owner known: %v, want an error other than not found", err)
|
|
}
|
|
if err := savePosition(s, 10, 5, "a"); err == nil {
|
|
t.Fatal("fenced save with no owner known succeeded")
|
|
}
|
|
}
|
|
|
|
func TestFencedSaveReadsAndWritesAtTheOwnerFiler(t *testing.T) {
|
|
// the accessor's filer holds an older copy than the key's owner
|
|
replica, s := newFencedStorage(t)
|
|
owner, ownerAddress := startConditionalFiler(t)
|
|
if err := savePosition(s, 10, 5, "a"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var routed []string
|
|
s.WriteOwner = func(routeKey string) pb.ServerAddress {
|
|
routed = append(routed, routeKey)
|
|
return ownerAddress
|
|
}
|
|
if err := savePosition(s, 20, 7, "c"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := savePosition(s, 30, 6, "b"); !errors.Is(err, ErrStaleGeneration) {
|
|
t.Fatalf("save below the owner's generation: %v, want ErrStaleGeneration", err)
|
|
}
|
|
path := topic.PartitionDir(fenceTopic, fencePartition) + "/consumers/forwarder.offset"
|
|
if len(routed) != 2 || routed[0] != s3_constants.ObjectWriteRouteKeyPrefix+path {
|
|
t.Fatalf("routed %v", routed)
|
|
}
|
|
owner.mu.Lock()
|
|
stored := string(owner.entries[path].GetContent())
|
|
owner.mu.Unlock()
|
|
replica.mu.Lock()
|
|
stale := string(replica.entries[path].GetContent())
|
|
replica.mu.Unlock()
|
|
if !strings.Contains(stored, `"generation":7`) || !strings.Contains(stale, `"generation":5`) {
|
|
t.Fatalf("owner holds %s, replica %s", stored, stale)
|
|
}
|
|
}
|
|
|
|
var (
|
|
fenceTopic = topic.NewTopic("s3", "notify-orders")
|
|
fencePartition = topic.Partition{RingSize: 4096, RangeStart: 0, RangeStop: 1024, UnixTimeNs: 1}
|
|
)
|
|
|
|
func savePosition(s *FilerConsumerGroupOffsetStorage, value, generation int64, writer string) error {
|
|
return s.SaveConsumerGroupPosition(context.Background(), fenceTopic, fencePartition, "forwarder", &ConsumerGroupPosition{
|
|
Type: "offset", Value: value, Generation: generation, Writer: writer,
|
|
})
|
|
}
|
|
|
|
func TestFencedSaveRefusesAnOlderGeneration(t *testing.T) {
|
|
_, s := newFencedStorage(t)
|
|
if _, err := s.LoadConsumerGroupPosition(context.Background(), fenceTopic, fencePartition, "forwarder"); !errors.Is(err, filer_pb.ErrNotFound) {
|
|
t.Fatalf("load of a missing position: %v, want ErrNotFound", err)
|
|
}
|
|
if err := savePosition(s, 10, 5, "a"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := savePosition(s, 20, 6, "b"); err != nil {
|
|
t.Fatalf("newer generation refused: %v", err)
|
|
}
|
|
if err := savePosition(s, 30, 5, "a"); !errors.Is(err, ErrStaleGeneration) {
|
|
t.Fatalf("deposed writer's save: %v, want ErrStaleGeneration", err)
|
|
}
|
|
if err := savePosition(s, 30, 6, "c"); !errors.Is(err, ErrStaleGeneration) {
|
|
t.Fatalf("another writer at the same generation: %v, want ErrStaleGeneration", err)
|
|
}
|
|
if err := savePosition(s, 25, 6, "b"); err != nil {
|
|
t.Fatalf("holder's own save refused: %v", err)
|
|
}
|
|
got, err := s.LoadConsumerGroupPosition(context.Background(), fenceTopic, fencePartition, "forwarder")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.Value != 25 || got.Generation != 6 || got.Writer != "b" {
|
|
t.Fatalf("stored %+v", got)
|
|
}
|
|
}
|
|
|
|
// A new generation's first save takes over the partition at the offset the old
|
|
// generation committed last, not the one the new one loaded before it, even
|
|
// when the old one commits again between that save's read and its write.
|
|
func TestATakeoverKeepsTheOffsetItReplaces(t *testing.T) {
|
|
f, s := newFencedStorage(t)
|
|
if err := savePosition(s, 10, 5, "a"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// generation 6 loaded 10; generation 5 commits 20 and then 30
|
|
if err := savePosition(s, 20, 5, "a"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f.mu.Lock()
|
|
f.beforeWrite = func() {
|
|
if err := savePosition(s, 30, 5, "a"); err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
f.mu.Unlock()
|
|
claim := &ConsumerGroupPosition{Type: "offset", Value: 10, Generation: 6, Writer: "b", Metadata: "claimed"}
|
|
if err := s.SaveConsumerGroupPosition(context.Background(), fenceTopic, fencePartition, "forwarder", claim); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if claim.Value != 30 || claim.Metadata != "" {
|
|
t.Fatalf("takeover answered offset %d metadata %q, want offset 30 and the replaced entry's metadata", claim.Value, claim.Metadata)
|
|
}
|
|
got, err := s.LoadConsumerGroupPosition(context.Background(), fenceTopic, fencePartition, "forwarder")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.Value != 30 || got.Generation != 6 || got.Writer != "b" {
|
|
t.Fatalf("stored %+v, want offset 30 at generation 6 by b", got)
|
|
}
|
|
}
|
|
|
|
func TestFencedSaveRechecksWhenAWriterLandsBetweenReadAndWrite(t *testing.T) {
|
|
f, s := newFencedStorage(t)
|
|
if err := savePosition(s, 10, 5, "a"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// a newer generation lands after this save read generation 5
|
|
f.mu.Lock()
|
|
f.beforeWrite = func() {
|
|
if err := savePosition(s, 40, 7, "c"); err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
f.mu.Unlock()
|
|
if err := savePosition(s, 20, 6, "b"); !errors.Is(err, ErrStaleGeneration) {
|
|
t.Fatalf("save racing a newer generation: %v, want ErrStaleGeneration", err)
|
|
}
|
|
got, err := s.LoadConsumerGroupPosition(context.Background(), fenceTopic, fencePartition, "forwarder")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// generation 7 took over at the offset generation 5 committed
|
|
if got.Value != 10 || got.Generation != 7 || got.Writer != "c" {
|
|
t.Fatalf("stored %+v, want the newer generation's", got)
|
|
}
|
|
}
|
|
|
|
func TestUnfencedSaveStillOverwrites(t *testing.T) {
|
|
_, s := newFencedStorage(t)
|
|
if err := savePosition(s, 10, 5, "a"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.SaveConsumerGroupOffset(fenceTopic, fencePartition, "forwarder", 3); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := s.LoadConsumerGroupOffset(fenceTopic, fencePartition, "forwarder")
|
|
if err != nil || got != 3 {
|
|
t.Fatalf("offset %d, %v", got, err)
|
|
}
|
|
}
|