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 <noreply@anthropic.com>
This commit is contained in:
Chris LuandClaude Opus 5.5 committed 2026-12-01 12:49:51 -08:00
1 parent 7b2f04bb3d
commit c529121bb8
2 files changed
+73

No files matched your search

+3
View File
@@ -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
}
@@ -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)
}
}