From c529121bb811befcf30675533ebb9d5a7205dcd3 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Tue, 1 Dec 2026 12:49:51 -0800 Subject: [PATCH] filer: return the commit's log position from ObjectTransaction A committed transaction now stamps log_ts_ns under its lock_key lock after its last mutation, the same wall-clock source CreateEntry and UpdateEntry use. All mutations of one transaction sit between that stamp and the next writer's, so one stamp orders the transaction against every other write of the key. A refused or failed transaction leaves it 0, and a replayed request returns the stamp recorded with its first outcome. A non-owner forwards the owner's response unchanged. Co-Authored-By: Claude Opus 5.5 --- weed/server/filer_grpc_server.go | 3 + .../filer_grpc_server_object_txn_test.go | 70 +++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/weed/server/filer_grpc_server.go b/weed/server/filer_grpc_server.go index c0a7ae4f17..5416a87fc8 100644 --- a/weed/server/filer_grpc_server.go +++ b/weed/server/filer_grpc_server.go @@ -552,6 +552,9 @@ func (fs *FilerServer) ObjectTransaction(ctx context.Context, req *filer_pb.Obje } } + // Still under the lock: after every event this transaction logged, before + // the next writer of lock_key stamps its own. + resp.LogTsNs = time.Now().UnixNano() return resp, nil } diff --git a/weed/server/filer_grpc_server_object_txn_test.go b/weed/server/filer_grpc_server_object_txn_test.go index e4d89bf72f..4b2e19b074 100644 --- a/weed/server/filer_grpc_server_object_txn_test.go +++ b/weed/server/filer_grpc_server_object_txn_test.go @@ -927,6 +927,9 @@ func TestObjectTransactionForwardsToOwner(t *testing.T) { if err != nil || resp.Error != "" { t.Fatalf("forwarded txn failed: err=%v resp=%q", err, resp.Error) } + if resp.LogTsNs == 0 { + t.Errorf("forwarded txn should carry the owner's log_ts_ns") + } if string(ownerStore.entries["/buckets/b/obj"].Extended["X-Amz-Meta-k"]) != "v" { t.Errorf("owner should have applied the forwarded mutation: %v", ownerStore.entries["/buckets/b/obj"].Extended) } @@ -1213,3 +1216,70 @@ func TestObjectTransactionConditionPreferAltHonorsNullIsLatest(t *testing.T) { t.Fatal("mutation did not apply over a null-is-latest .versions") } } + +// A committed transaction returns the log position stamped under its lock, +// ordered after an earlier write to the same path; a refused one returns 0. +func TestObjectTransactionLogTsNs(t *testing.T) { + now := time.Unix(1700000000, 0) + fs, _ := newTxnTestServer(t, map[string]*filer.Entry{ + "/buckets/b/obj": { + Attr: filer.Attr{Inode: 1, Mtime: now, Crtime: now, Mode: 0644}, + Extended: map[string][]byte{s3_constants.ExtETagKey: []byte("abc")}, + }, + }) + ctx := context.Background() + + upd, err := fs.UpdateEntry(ctx, &filer_pb.UpdateEntryRequest{ + Directory: "/buckets/b", + Entry: &filer_pb.Entry{ + Name: "obj", + Attributes: &filer_pb.FuseAttributes{Mtime: now.Unix() + 5, FileMode: 0644, Inode: 1}, + Extended: map[string][]byte{s3_constants.ExtETagKey: []byte("abc")}, + }, + }) + if err != nil { + t.Fatalf("UpdateEntry: %v", err) + } + if upd.LogTsNs == 0 { + t.Fatal("UpdateEntry returned no log_ts_ns") + } + + txn := func(etag, mutationEtag string) *filer_pb.ObjectTransactionRequest { + m := &filer_pb.ObjectMutation{ + Type: filer_pb.ObjectMutation_PATCH_EXTENDED, Directory: "/buckets/b", Name: "obj", + SetExtended: map[string][]byte{"X-Amz-Meta-k": []byte("v")}, + } + if mutationEtag != "" { + m.Condition = one(&filer_pb.WriteCondition_Clause{Kind: filer_pb.WriteCondition_IF_ETAG_MATCH, Etags: []string{mutationEtag}}) + } + return &filer_pb.ObjectTransactionRequest{ + LockKey: "/buckets/b/obj", + Condition: one(&filer_pb.WriteCondition_Clause{Kind: filer_pb.WriteCondition_IF_ETAG_MATCH, Etags: []string{etag}}), + Mutations: []*filer_pb.ObjectMutation{m}, + } + } + + resp, err := fs.ObjectTransaction(ctx, txn(`"abc"`, "")) + if err != nil || resp.Error != "" { + t.Fatalf("conditional txn: err=%v resp=%q", err, resp.GetError()) + } + if resp.LogTsNs <= upd.LogTsNs { + t.Fatalf("txn log_ts_ns %d not after the preceding update's %d", resp.LogTsNs, upd.LogTsNs) + } + + refused, err := fs.ObjectTransaction(ctx, txn(`"zzz"`, "")) + if err != nil { + t.Fatalf("refused txn: %v", err) + } + if refused.ErrorCode != filer_pb.FilerError_PRECONDITION_FAILED || refused.LogTsNs != 0 { + t.Fatalf("refused txn: code %v log_ts_ns %d, want PRECONDITION_FAILED and 0", refused.ErrorCode, refused.LogTsNs) + } + + refused, err = fs.ObjectTransaction(ctx, txn(`"abc"`, `"zzz"`)) + if err != nil { + t.Fatalf("refused mutation: %v", err) + } + if refused.FailedMutation == nil || refused.LogTsNs != 0 { + t.Fatalf("refused mutation: failed_mutation %v log_ts_ns %d, want set and 0", refused.FailedMutation, refused.LogTsNs) + } +}