diff --git a/weed/server/filer_grpc_server.go b/weed/server/filer_grpc_server.go index 3e62acdd02..9215fe0774 100644 --- a/weed/server/filer_grpc_server.go +++ b/weed/server/filer_grpc_server.go @@ -231,7 +231,7 @@ func (fs *FilerServer) CreateEntry(ctx context.Context, req *filer_pb.CreateEntr return ownerResp, nil } } else if req.IsMoved && routed { - if err := fs.checkMovedMarker(ctx, req.IsMoved, fs.writeOwner(entryRouteKey(util.NewFullPath(req.Directory, req.Entry.Name)))); err != nil { + if err := fs.checkForwardedWrite(ctx, entryRouteKey(util.NewFullPath(req.Directory, req.Entry.Name))); err != nil { return &filer_pb.CreateEntryResponse{}, err } } @@ -347,10 +347,12 @@ func (fs *FilerServer) ObjectTransaction(ctx context.Context, req *filer_pb.Obje // Route-by-key: if this filer is not the ring owner of route_key, forward the // whole transaction to the owner so its per-path lock is the single // serialization point — even when the caller's ring view was stale. is_moved - // bounds this to one hop: a forwarded transaction is applied locally, so two - // filers that disagree on the owner during a ring change cannot loop. - if req.RouteKey != "" { - if err := fs.checkMovedMarker(ctx, req.IsMoved, fs.writeOwner(req.RouteKey)); err != nil { + // bounds this to one hop: a forwarded transaction is applied only by the + // owner in this filer's ring, and refused with a status the sender retries. + if req.RouteKey != "" && req.IsMoved { + if err := fs.checkForwardedWrite(ctx, req.RouteKey); isNotWriteOwner(err) { + return &filer_pb.ObjectTransactionResponse{}, err + } else if err != nil { return &filer_pb.ObjectTransactionResponse{Error: err.Error()}, nil } } @@ -954,7 +956,7 @@ func (fs *FilerServer) UpdateEntry(ctx context.Context, req *filer_pb.UpdateEntr return ownerResp, nil } } else if req.IsMoved && routed { - if err := fs.checkMovedMarker(ctx, req.IsMoved, fs.writeOwner(entryRouteKey(util.FullPath(fullpath)))); err != nil { + if err := fs.checkForwardedWrite(ctx, entryRouteKey(util.FullPath(fullpath))); err != nil { return &filer_pb.UpdateEntryResponse{}, err } } @@ -1205,7 +1207,7 @@ func (fs *FilerServer) DeleteEntry(ctx context.Context, req *filer_pb.DeleteEntr return ownerResp, nil } } else { - if err := fs.checkMovedMarker(ctx, req.IsMoved, fs.writeOwner(entryRouteKey(fullpath))); err != nil { + if err := fs.checkForwardedWrite(ctx, entryRouteKey(fullpath)); err != nil { return &filer_pb.DeleteEntryResponse{}, err } } diff --git a/weed/server/filer_grpc_server_create_route_test.go b/weed/server/filer_grpc_server_create_route_test.go index 75197145e2..797a579c4f 100644 --- a/weed/server/filer_grpc_server_create_route_test.go +++ b/weed/server/filer_grpc_server_create_route_test.go @@ -78,6 +78,20 @@ func peerOwnedName(t *testing.T, fs *FilerServer) string { return "" } +// selfOwnedName returns a child name under /test whose entry the ring assigns +// to this filer. +func selfOwnedName(t *testing.T, fs *FilerServer) string { + t.Helper() + for i := 0; i < 4000; i++ { + name := fmt.Sprintf("obj-%d", i) + if fs.writeOwner(entryRouteKey(util.NewFullPath("/test", name))) == "" { + return name + } + } + t.Skip("no name owned by this filer") + return "" +} + func createReq(name string, oExcl bool) *filer_pb.CreateEntryRequest { return &filer_pb.CreateEntryRequest{ Directory: "/test", @@ -128,11 +142,10 @@ func ringPeerCtx() context.Context { }) } -// is_moved bounds forwarding to one hop: the owner applies it even if its own -// ring view says someone else owns the key. +// A forwarded create is applied by the owner without forwarding it again. func TestCreateEntryMovedAppliesLocally(t *testing.T) { fs, store := createRouteServer(t) - name := peerOwnedName(t, fs) + name := selfOwnedName(t, fs) req := createReq(name, true) req.IsMoved = true @@ -145,6 +158,24 @@ func TestCreateEntryMovedAppliesLocally(t *testing.T) { } } +// A filer whose own ring names a peer as the owner refuses a forwarded create +// rather than check it under its own lock: the sender's ring disagrees with +// this one, and the peer's lock may be arbitrating the same entry. +func TestCreateEntryMovedRefusedByNonOwner(t *testing.T) { + fs, store := createRouteServer(t) + name := peerOwnedName(t, fs) + + req := createReq(name, true) + req.IsMoved = true + _, err := fs.CreateEntry(ringPeerCtx(), req) + if !isNotWriteOwner(err) { + t.Fatalf("a forwarded create on a non-owner must be refused as not the write owner, got err=%v", err) + } + if _, found := store.entries[string(util.NewFullPath("/test", name))]; found { + t.Fatal("a refused forward must not write locally") + } +} + // A caller can set is_moved, but without a ring member's connection it is not // trusted: on a non-owner the request is refused rather than evaluated under // the wrong lock or forwarded again, where it could cycle while rings disagree. diff --git a/weed/server/filer_grpc_server_object_txn_test.go b/weed/server/filer_grpc_server_object_txn_test.go index b7fd0b5117..e4d89bf72f 100644 --- a/weed/server/filer_grpc_server_object_txn_test.go +++ b/weed/server/filer_grpc_server_object_txn_test.go @@ -856,12 +856,11 @@ func TestObjectTransactionRouteKeyOwnerAppliesLocally(t *testing.T) { } } -// A forwarded transaction (is_moved over a ring member's connection) applies -// locally even when the ring names a different owner: is_moved bounds -// forwarding to a single hop, so two filers that disagree on the owner during -// a ring change cannot loop. Without the marker this would attempt to dial -// the bogus owner instead of applying. -func TestObjectTransactionIsMovedSkipsForward(t *testing.T) { +// A forwarded transaction (is_moved over a ring member's connection) on a +// filer whose ring names a different owner is refused, neither applied under +// this filer's lock nor forwarded again, so two filers that disagree on the +// owner during a ring change can neither both apply it nor loop. +func TestObjectTransactionIsMovedRefusedByNonOwner(t *testing.T) { self := pb.ServerAddress("localhost:1") other := pb.ServerAddress("localhost:2") fs, store := newTxnTestServer(t, map[string]*filer.Entry{ @@ -869,7 +868,7 @@ func TestObjectTransactionIsMovedSkipsForward(t *testing.T) { }) withRing(fs, self, other) // ring owner is "other", not self - resp, err := fs.ObjectTransaction(ringPeerCtx(), &filer_pb.ObjectTransactionRequest{ + _, err := fs.ObjectTransaction(ringPeerCtx(), &filer_pb.ObjectTransactionRequest{ LockKey: "/buckets/b/obj", RouteKey: "s3.object.write:/buckets/b/obj", IsMoved: true, @@ -878,18 +877,16 @@ func TestObjectTransactionIsMovedSkipsForward(t *testing.T) { SetExtended: map[string][]byte{"X-Amz-Meta-k": []byte("v")}, }}, }) - if err != nil || resp.Error != "" { - t.Fatalf("txn failed: err=%v resp=%q", err, resp.Error) + if !isNotWriteOwner(err) { + t.Fatalf("a forwarded txn on a non-owner must be refused as not the write owner, got err=%v", err) } - if string(store.entries["/buckets/b/obj"].Extended["X-Amz-Meta-k"]) != "v" { - t.Errorf("forwarded txn should apply locally: %v", store.entries["/buckets/b/obj"].Extended) + if _, set := store.entries["/buckets/b/obj"].Extended["X-Amz-Meta-k"]; set { + t.Errorf("a refused txn must not apply: %v", store.entries["/buckets/b/obj"].Extended) } } // End-to-end forward hop: a non-owner filer dials the ring owner and the owner -// applies the transaction. The owner's own ring points back at the (bogus) -// sender, so it would re-forward and fail to dial unless is_moved is set on the -// forwarded request — making this also assert that one-hop bound over the wire. +// applies the transaction. func TestObjectTransactionForwardsToOwner(t *testing.T) { owner, ownerStore := newTxnTestServer(t, map[string]*filer.Entry{ "/buckets/b/obj": {FullPath: "/buckets/b/obj", Attr: filer.Attr{Inode: 1, Mode: 0644}}, @@ -905,9 +902,7 @@ func TestObjectTransactionForwardsToOwner(t *testing.T) { ownerAddr := pb.NewServerAddressWithGrpcPort(lis.Addr().String(), port) sender := pb.ServerAddress("127.0.0.1:1") // bogus: nothing listens here - // owner's ring points back at the sender; only is_moved keeps it from - // re-forwarding to (and failing to dial) that bogus address. - withRing(owner, ownerAddr, sender) + withRing(owner, ownerAddr, ownerAddr) owner.grpcDialOption = grpc.WithTransportCredentials(insecure.NewCredentials()) srv := grpc.NewServer() diff --git a/weed/server/filer_grpc_server_route.go b/weed/server/filer_grpc_server_route.go index 18126eaff4..6ae01f5850 100644 --- a/weed/server/filer_grpc_server_route.go +++ b/weed/server/filer_grpc_server_route.go @@ -11,6 +11,7 @@ import ( "github.com/seaweedfs/seaweedfs/weed/pb" "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants" "github.com/seaweedfs/seaweedfs/weed/util" + "google.golang.org/genproto/googleapis/rpc/errdetails" "google.golang.org/grpc/codes" "google.golang.org/grpc/peer" "google.golang.org/grpc/status" @@ -178,3 +179,46 @@ func (fs *FilerServer) checkMovedMarker(ctx context.Context, isMoved bool, owner } return status.Errorf(codes.PermissionDenied, "is_moved not sent by a ring member; the key's owner is %s", owner) } + +// notWriteOwnerReason marks a forwarded write refused by a filer whose own ring +// names another write owner for the key. +const notWriteOwnerReason = "NOT_WRITE_OWNER" + +// checkForwardedWrite admits a routed write that arrived with is_moved only on +// the key's write owner in this filer's own ring. While two rings disagree, a +// forward applied here would be checked under a second filer's lock, and +// re-forwarding it could cycle, so it is refused. The refusal is Unavailable +// with a NOT_WRITE_OWNER reason, telling the sender nothing was applied. +func (fs *FilerServer) checkForwardedWrite(ctx context.Context, key string) error { + owner := fs.writeOwner(key) + if err := fs.checkMovedMarker(ctx, true, owner); err != nil { + return err + } + if owner == "" { + return nil + } + return notWriteOwnerError(key, owner) +} + +func notWriteOwnerError(key string, owner pb.ServerAddress) error { + st := status.Newf(codes.Unavailable, "not the write owner of %s: %s is", key, owner) + withReason, err := st.WithDetails(&errdetails.ErrorInfo{Reason: notWriteOwnerReason, Domain: "seaweedfs"}) + if err != nil { + return st.Err() + } + return withReason.Err() +} + +// isNotWriteOwner reports whether err is checkForwardedWrite's refusal. +func isNotWriteOwner(err error) bool { + st, ok := util.ServerStatus(err) + if !ok || st.Code() != codes.Unavailable { + return false + } + for _, d := range st.Details() { + if info, ok := d.(*errdetails.ErrorInfo); ok && info.GetReason() == notWriteOwnerReason { + return true + } + } + return false +}