diff --git a/weed/filer/filer_notify.go b/weed/filer/filer_notify.go index da3c387e45..ec27805360 100644 --- a/weed/filer/filer_notify.go +++ b/weed/filer/filer_notify.go @@ -256,6 +256,14 @@ func (f *Filer) ReleaseEntryCommitTsNs(tsNs int64) { f.metaLogInflight.release(tsNs) } +// LogFenceTsNs returns a stamp above every event stamped so far; every later +// event stamps above it, even after the wall clock steps back. +func (f *Filer) LogFenceTsNs() int64 { + tsNs := f.metaLogInflight.stamp() + f.metaLogInflight.release(tsNs) + return tsNs +} + // stampOf returns the event's timestamp: the entry's in-flight stamp, or a // fresh one for events with no newly stamped entry (deletes). func (f *Filer) stampOf(newEntry *Entry) int64 { diff --git a/weed/pb/filer.proto b/weed/pb/filer.proto index c0ed942a9c..cc32b5f2e2 100644 --- a/weed/pb/filer.proto +++ b/weed/pb/filer.proto @@ -416,6 +416,9 @@ message ObjectTransactionResponse { string error = 1; FilerError error_code = 2; optional int32 failed_mutation = 3; // index of the mutation that failed (its condition or its apply); unset when the request-level condition failed + // filer log position stamped under the lock after the last mutation + // committed; 0 when the transaction was refused or failed + int64 log_ts_ns = 4; } // PosixLockRange is one advisory byte-range lock. Owner identity is (sid, owner): diff --git a/weed/pb/filer_pb/filer.pb.go b/weed/pb/filer_pb/filer.pb.go index fef06d720e..ac39495150 100644 --- a/weed/pb/filer_pb/filer.pb.go +++ b/weed/pb/filer_pb/filer.pb.go @@ -1992,8 +1992,11 @@ type ObjectTransactionResponse struct { Error string `protobuf:"bytes,1,opt,name=error,proto3" json:"error,omitempty"` ErrorCode FilerError `protobuf:"varint,2,opt,name=error_code,json=errorCode,proto3,enum=filer_pb.FilerError" json:"error_code,omitempty"` FailedMutation *int32 `protobuf:"varint,3,opt,name=failed_mutation,json=failedMutation,proto3,oneof" json:"failed_mutation,omitempty"` // index of the mutation that failed (its condition or its apply); unset when the request-level condition failed - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache + // filer log position stamped under the lock after the last mutation + // committed; 0 when the transaction was refused or failed + LogTsNs int64 `protobuf:"varint,4,opt,name=log_ts_ns,json=logTsNs,proto3" json:"log_ts_ns,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache } func (x *ObjectTransactionResponse) Reset() { @@ -2047,6 +2050,13 @@ func (x *ObjectTransactionResponse) GetFailedMutation() int32 { return 0 } +func (x *ObjectTransactionResponse) GetLogTsNs() int64 { + if x != nil { + return x.LogTsNs + } + return 0 +} + // PosixLockRange is one advisory byte-range lock. Owner identity is (sid, owner): // sid is the mount session, owner the FUSE lock owner within it, so owners from // different mounts never alias. end is inclusive (max uint64 = to EOF); is_flock @@ -7691,12 +7701,13 @@ const file_filer_proto_rawDesc = "" + "request_id\x18\t \x01(\tR\trequestId\x12*\n" + "\x11condition_alt_key\x18\n" + " \x01(\tR\x0fconditionAltKey\x120\n" + - "\x14condition_prefer_alt\x18\v \x01(\bR\x12conditionPreferAlt\"\xa8\x01\n" + + "\x14condition_prefer_alt\x18\v \x01(\bR\x12conditionPreferAlt\"\xc4\x01\n" + "\x19ObjectTransactionResponse\x12\x14\n" + "\x05error\x18\x01 \x01(\tR\x05error\x123\n" + "\n" + "error_code\x18\x02 \x01(\x0e2\x14.filer_pb.FilerErrorR\terrorCode\x12,\n" + - "\x0ffailed_mutation\x18\x03 \x01(\x05H\x00R\x0efailedMutation\x88\x01\x01B\x12\n" + + "\x0ffailed_mutation\x18\x03 \x01(\x05H\x00R\x0efailedMutation\x88\x01\x01\x12\x1a\n" + + "\tlog_ts_ns\x18\x04 \x01(\x03R\alogTsNsB\x12\n" + "\x10_failed_mutation\"\xa1\x01\n" + "\x0ePosixLockRange\x12\x14\n" + "\x05start\x18\x01 \x01(\x04R\x05start\x12\x10\n" + diff --git a/weed/pb/filer_pb/filer_vtproto.pb.go b/weed/pb/filer_pb/filer_vtproto.pb.go index 5f54af4fb5..60a348b62e 100644 --- a/weed/pb/filer_pb/filer_vtproto.pb.go +++ b/weed/pb/filer_pb/filer_vtproto.pb.go @@ -1768,6 +1768,11 @@ func (m *ObjectTransactionResponse) MarshalToSizedBufferVT(dAtA []byte) (int, er i -= len(m.unknownFields) copy(dAtA[i:], m.unknownFields) } + if m.LogTsNs != 0 { + i = protohelpers.EncodeVarint(dAtA, i, uint64(m.LogTsNs)) + i-- + dAtA[i] = 0x20 + } if m.FailedMutation != nil { i = protohelpers.EncodeVarint(dAtA, i, uint64(*m.FailedMutation)) i-- @@ -7481,6 +7486,9 @@ func (m *ObjectTransactionResponse) SizeVT() (n int) { if m.FailedMutation != nil { n += 1 + protohelpers.SizeOfVarint(uint64(*m.FailedMutation)) } + if m.LogTsNs != 0 { + n += 1 + protohelpers.SizeOfVarint(uint64(m.LogTsNs)) + } n += len(m.unknownFields) return n } @@ -14654,6 +14662,25 @@ func (m *ObjectTransactionResponse) UnmarshalVT(dAtA []byte) error { } } m.FailedMutation = &v + case 4: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field LogTsNs", wireType) + } + m.LogTsNs = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return protohelpers.ErrIntOverflow + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.LogTsNs |= int64(b&0x7F) << shift + if b < 0x80 { + break + } + } default: iNdEx = preIndex skippy, err := protohelpers.Skip(dAtA[iNdEx:]) diff --git a/weed/server/filer_grpc_server.go b/weed/server/filer_grpc_server.go index 849f5937bd..0536d9cba2 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 = fs.filer.LogFenceTsNs() 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..668260af0e 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,99 @@ 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) + } +} + +// With the wall clock behind stamps already issued, each transaction's stamp +// still follows its own event and the previous transaction's stamp. +func TestObjectTransactionLogTsNsClockBehind(t *testing.T) { + now := time.Unix(1700000000, 0) + fs, store := newTxnTestServer(t, map[string]*filer.Entry{ + "/buckets/b/obj": {Attr: filer.Attr{Inode: 1, Mtime: now, Crtime: now, Mode: 0644}}, + }) + prev := time.Now().Add(time.Hour).UnixNano() + fs.filer.LocalDeliveredThroughTsNs(prev) + + for i := 0; i < 2; i++ { + resp, err := fs.ObjectTransaction(context.Background(), &filer_pb.ObjectTransactionRequest{ + LockKey: "/buckets/b/obj", + Mutations: []*filer_pb.ObjectMutation{{ + Type: filer_pb.ObjectMutation_PATCH_EXTENDED, Directory: "/buckets/b", Name: "obj", + SetExtended: map[string][]byte{"X-Amz-Meta-k": []byte(strconv.Itoa(i))}, + }}, + }) + if err != nil || resp.Error != "" { + t.Fatalf("txn %d: err=%v resp=%q", i, err, resp.GetError()) + } + event := store.entries["/buckets/b/obj"].CommitTsNs + if event <= prev || resp.LogTsNs <= event { + t.Fatalf("txn %d: previous %d, event %d, log_ts_ns %d; want increasing", i, prev, event, resp.LogTsNs) + } + prev = resp.LogTsNs + } +}