From e861733a8a095e7690926a1b725b48cfdf800613 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 29 Nov 2026 10:37:29 -0800 Subject: [PATCH] test: require conditional HEAD refusal at catalog startup --- .../chunkstore/startup_head_probe_test.go | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 seaweed-block/core/frontend/chunkstore/startup_head_probe_test.go diff --git a/seaweed-block/core/frontend/chunkstore/startup_head_probe_test.go b/seaweed-block/core/frontend/chunkstore/startup_head_probe_test.go new file mode 100644 index 0000000000..dcb7c774b5 --- /dev/null +++ b/seaweed-block/core/frontend/chunkstore/startup_head_probe_test.go @@ -0,0 +1,107 @@ +package chunkstore + +import ( + "context" + "errors" + "net" + "path" + "strings" + "testing" + "time" + + "github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/filerpb" + "google.golang.org/grpc" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" +) + +type conditionalProbeServer struct { + configFiler + store *fakeFiler + broken bool + updates int +} + +func (f *conditionalProbeServer) CreateEntry(ctx context.Context, r *filerpb.CreateEntryRequest) (*filerpb.CreateEntryResponse, error) { + if err := f.store.CreateEntry(ctx, r.Directory, r.Entry, r.OExcl); err != nil { + return nil, status.Error(codes.Internal, err.Error()) + } + return &filerpb.CreateEntryResponse{}, nil +} +func (f *conditionalProbeServer) UpdateEntry(ctx context.Context, r *filerpb.UpdateEntryRequest) (*filerpb.UpdateEntryResponse, error) { + f.mu.Lock() + f.updates++ + broken := f.broken + f.mu.Unlock() + expected := r.ExpectedExtended + if broken { + expected = nil + } + if err := f.store.UpdateEntry(ctx, r.Directory, r.Entry, expected); err != nil { + if errors.Is(err, ErrPrecondition) { + return nil, status.Error(codes.FailedPrecondition, err.Error()) + } + return nil, status.Error(codes.Internal, err.Error()) + } + return &filerpb.UpdateEntryResponse{}, nil +} +func (f *conditionalProbeServer) LookupDirectoryEntry(ctx context.Context, r *filerpb.LookupDirectoryEntryRequest) (*filerpb.LookupDirectoryEntryResponse, error) { + e, err := f.store.LookupEntry(ctx, r.Directory, r.Name) + if err != nil { + return nil, status.Error(codes.NotFound, err.Error()) + } + return &filerpb.LookupDirectoryEntryResponse{Entry: e}, nil +} +func (f *conditionalProbeServer) DeleteEntry(ctx context.Context, r *filerpb.DeleteEntryRequest) (*filerpb.DeleteEntryResponse, error) { + if r.IsRecursive { + return nil, status.Error(codes.InvalidArgument, "recursive probe cleanup") + } + if err := f.store.DeleteEntry(ctx, r.Directory, r.Name, r.ExpectedExtended); err != nil { + return nil, status.Error(codes.Internal, err.Error()) + } + return &filerpb.DeleteEntryResponse{}, nil +} +func TestCatalogStartupRequiresWrongGenerationHeadRefusal(t *testing.T) { + for _, broken := range []bool{true, false} { + t.Run(map[bool]string{true: "ignores-conditions", false: "conditional-filer"}[broken], func(t *testing.T) { + srv := grpc.NewServer() + lis, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + f := &conditionalProbeServer{store: newFakeFiler(), broken: broken} + filerpb.RegisterSeaweedFilerServer(srv, f) + go func() { _ = srv.Serve(lis) }() + t.Cleanup(srv.Stop) + f.masters = []string{startMaster(t, &nodesMaster{filers: []string{lis.Addr().String()}})} + // Preserve an unrelated namespace entry across the complete startup probe. + putFile(t, f.store, "/blocks/existing/keep", []byte("untouched")) + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + cat, client, err := DialBCatalog(ctx, SWFSConfig{FilerGRPC: lis.Addr().String(), AssignPath: "/blocks"}) + if client != nil { + defer client.Close() + } + if broken { + if err == nil || cat != nil || !strings.Contains(err.Error(), "conditional HEAD") { + t.Fatalf("broken conditional startup: cat=%v err=%v", cat, err) + } + } else if err != nil || cat == nil { + t.Fatalf("conditional startup: %v", err) + } + f.mu.Lock() + updates := f.updates + f.mu.Unlock() + if updates != 1 { + t.Fatalf("wrong-generation exchanges=%d want1", updates) + } + f.store.mu.Lock() + defer f.store.mu.Unlock() + for p, e := range f.store.entries { + if p != "/blocks/existing/keep" || string(e.Content) != "untouched" { + t.Fatalf("probe leftover or unrelated change: %s", path.Clean(p)) + } + } + }) + } +}