Files
seaweedfs/weed/filer/entry_codec_atime_test.go
T
4476cb282b feat(filer): add atime to FuseAttributes + TouchAccessTime RPC (#9556)
* feat(filer): add atime field and TouchAccessTime RPC to filer proto

Introduce POSIX-style access-time tracking on the filer:
- FuseAttributes gains atime (field 22) and atime_ns (field 23).
- New TouchAccessTime RPC (and Touch{Access,Time}{Request,Response})
  lets read paths bump atime without going through UpdateEntry's
  chunk-rewrite/EqualEntry short-circuit.

Additive proto changes only; zero atime is treated as unset and
existing clients are unaffected. Java client proto is kept in lock
step.

Co-authored-by: Cursor <cursoragent@cursor.com>

* feat(filer): wire Atime through Attr codec with mtime fallback

Add Attr.Atime and round-trip it through EntryAttributeToPb /
EntryAttributeToExistingPb / PbToEntryAttribute. A zero proto atime
decodes as Mtime, so legacy entries report a sensible value and
freshly-created/updated entries default Atime to Mtime when callers
do not set it explicitly.

CreateEntry and UpdateEntry stamp Atime = Mtime (or Crtime) when it
is zero. TouchAccessTime later bypasses this path to write atime
alone via Store.UpdateEntry.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(filer): preserve atime in first epoch second on decode

The Atime decode branch previously treated any attr.Atime == 0 as
unset and overwrote it with Mtime, which drops valid timestamps in
the first second of the unix epoch where attr.Atime is 0 but
attr.AtimeNs > 0. Check both fields so we only fall back to Mtime
when both are zero.

Co-authored-by: Cursor <cursoragent@cursor.com>

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-19 10:22:17 -07:00

66 lines
1.8 KiB
Go

package filer
import (
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
)
func TestEntryCodec_AtimeRoundTrip(t *testing.T) {
mtime := time.Unix(1_700_000_000, 123_456_789)
atime := time.Unix(1_700_001_000, 987_654_321)
source := &Entry{
FullPath: util.FullPath("/bucket/object"),
Attr: Attr{
Mtime: mtime,
Ctime: mtime,
Atime: atime,
},
}
pb := EntryAttributeToPb(source)
if pb.Atime != atime.Unix() {
t.Fatalf("expected proto atime %d, got %d", atime.Unix(), pb.Atime)
}
if pb.AtimeNs != int32(atime.Nanosecond()) {
t.Fatalf("expected proto atime_ns %d, got %d", atime.Nanosecond(), pb.AtimeNs)
}
decoded := PbToEntryAttribute(pb)
if !decoded.Atime.Equal(atime) {
t.Fatalf("expected decoded atime %v, got %v", atime, decoded.Atime)
}
}
func TestEntryCodec_AtimeZeroFallsBackToMtime(t *testing.T) {
mtime := time.Unix(1_700_000_000, 0)
pb := &filer_pb.FuseAttributes{
Mtime: mtime.Unix(),
MtimeNs: int32(mtime.Nanosecond()),
}
decoded := PbToEntryAttribute(pb)
if !decoded.Atime.Equal(mtime) {
t.Fatalf("expected atime to fall back to mtime %v, got %v", mtime, decoded.Atime)
}
}
// Atime in the first second of the unix epoch encodes as Atime=0 with
// AtimeNs>0; the decode path must treat that as a valid timestamp rather than
// falling back to Mtime.
func TestEntryCodec_AtimeSubSecondEpochPreserved(t *testing.T) {
mtime := time.Unix(1_700_000_000, 0)
pb := &filer_pb.FuseAttributes{
Mtime: mtime.Unix(),
MtimeNs: int32(mtime.Nanosecond()),
Atime: 0,
AtimeNs: 500_000,
}
decoded := PbToEntryAttribute(pb)
want := time.Unix(0, 500_000)
if !decoded.Atime.Equal(want) {
t.Fatalf("expected sub-second-epoch atime %v, got %v", want, decoded.Atime)
}
}