Files
seaweedfs/weed/mount/content_flush_condition_test.go
T
Chris LuandClaude Opus 5.5 46b10efcce mount: refuse a stale truncate over a peer's chunks and keep later times when merging a flush (#13100)
* mount: refuse a merge whose local truncate cuts chunks a peer wrote

A flush refused by IF_ENTRY_EQUAL replays the handle's truncate onto the
stored entry. The truncate was made over the handle's base, so a chunk a
peer committed after that base was dropped or clipped silently, losing
data the peer had already fsynced. Like a stale whole-file rewrite over a
peer's chunk change, it now fails the merge. A base chunk the peer only
shortened carries no peer data and is still cut.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: keep the later mtime and ctime when merging a delayed flush

The merge took every attribute the handle changed from the handle, so a
flush that waited behind a peer's commit moved mtime and ctime back to
the handle's older write. Each time is now merged as one value (seconds
and nanoseconds were merged field by field, so a moved second could take
the peer's nanoseconds): the handle's time stands unless a peer moved it
too and later.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: let a truncate merge pass the handle's own committed chunks

A flush whose commit answer was lost finds its own chunks in the stored
entry while its base is still the older one. Those chunks are the
handle's writes, not a peer's, so they do not refuse the merge.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: do not keep a stored copy of a chunk the merge re-adds from the handle

A stored chunk the base lacks but the handle holds is the handle's own
commit whose answer was lost. The merge appends the handle's chunks after
the stored ones, so keeping the stored copy referenced the chunk twice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: take the manifests of a failed flush's fold for the handle's own chunks

A flush can commit its folded manifests and lose the answer. The handle
reverts to the member chunks and keeps its old base, so the next merge
found the stored manifest, knew neither its file id nor its key, and
refused it as a peer write across the handle's truncate.

The handle now remembers the folds of flushes that failed over its base,
and a merge unfolds them in the stored entry before comparing, so the
members match the handle's chunks. A peer's manifest is still refused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: take a retried flush's own stored fold for its own chunks

The client retries a request whose connection dropped, and the first try
may have stored the flush's folded manifests. The retry is then refused
over them before the fold is recorded as failed, and the merge took the
manifest for a peer's write across the handle's truncate. The merge now
unfolds the flush's own fold in the stored entry as well; the condition
still names the entry as stored.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: keep every fold the filer may hold instead of the last four

Dropping the oldest of four could forget the fold the filer committed,
and the merge then refused that manifest again. A fold is now kept only
when the flush failed without the filer's answer, or when the refusal
lookup shows it stored, so a refused flush no longer adds one, and the
list ends with the base.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: keep a flush's fold when its first request may have been stored

A precondition verdict on a retried send does not prove the first send
stored nothing, and a failed refusal lookup then hid that it had. The
create now counts the sends of the request that carries the fold and
keeps the fold whenever the flush fails and that request was sent more
than once or never got the filer's answer. That covers both flush paths,
so revert is back to only restoring the chunks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: drop the failed folds a refused create shows were not stored

A refusal means the stored entry moved off the handle's base, so no
request guarded by that base can store another fold. After the refusal
lookup the handle keeps only the folds whose manifests the stored entry
holds, at most one, which bounds the list over a base.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: bound the failed folds a handle keeps over one base

Flushes that keep failing with no filer answer each saved a new fold the
filer may hold, so the list grew for as long as the outage lasted. Once a
handle keeps maxFailedFolds folds over its base, its flushes send their
chunks unfolded for the filer to fold, and no kept fold is dropped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: keep the failed folds a refusal lookup does not show

The refusal comes from the file's write owner, but the lookup after it can
read a filer that lags behind and still shows the base. Pruning the folds
against that read forgot a fold the owner had stored, and once its manifest
showed up a pending truncate took it for a peer's chunk on every flush. The
maxFailedFolds limit bounds the list, so the folds stay until the base moves.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: drop the chunks a failed flush sent that the handle has cut since

A flush that commits with its answer lost, then a truncate of the handle,
left the next merge with the handle's own committed chunks in the stored
entry, outside both base and the handle. The truncate check took them for
a peer's writes and refused every later flush over the base. A failed
flush now records the file ids of the chunks it sent that base lacks, and
the merge drops those the handle no longer holds: it cut or replaced them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: copy the failed flush's sent set with a loop

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: keep folding past maxFailedFolds instead of sending chunks unfolded

The filer folds a flat chunk list itself, into manifests the handle does
not know: a lost answer then left one of them across a pending truncate,
and an acknowledged one gave the handle flat chunks under a folded base.
Flushes fold as before, and the handle stops keeping folds past the limit
while it still records the chunks they sent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: read the stored manifests a refused merge cannot name

A fold changes no content, but a manifest the handle does not know sat in
the stored entry as one chunk: across a pending truncate it refused the
merge, though it can hold the handle's own chunks (a fold it did not keep,
or one the filer made). When the merge is refused, the flush reads those
manifests, replaces them with the chunks they hold and merges again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: hold the chunks the filer stored after a plain acknowledged flush

The filer folds a flat chunk list (the mount's own fold failed), and the
acknowledgement makes the stored entry the base while the handle kept the
flat chunks. A later merge took them for writes of the handle, which can
bring back chunks a peer's truncate removed. As after a merge, the handle
now takes the stored chunks, keeping those attached since the request.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: keep a bounded record per failed flush instead of a growing set

The set of chunk ids failed flushes sent grew with every overwrite while
commits failed, and each failure copied it whole. Each failed flush the
filer may have stored (a send got no verdict) now keeps one record, its
fold and the ids it sent, built once and at most maxFailedFlushes over a
base. Past that, a merge refuses the chunks of a flush it did not keep
that the handle has cut since, as a peer's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: register the chunks a rebase or install gives the handle for reads

A handle that takes the stored chunks after a flush (or a peer's, after a
merge) can get manifests the filer made, and resolving them needs the
file's read token. rebaseEntry and installRewrittenChunks now record the
chunks under the file's path before the read view resolves them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: keep every chunk id a failed flush sent, cap only the folds

Past four failed flushes over one base the handle stopped recording what a
later flush sent. If that flush was stored with its answer lost, a truncate
of its chunks was refused as a peer's write and kept failing after the
filer came back. The ids now go into one set shared by the base's marks,
each kept once and never copied, until the base moves. Only the folds stay
capped: a stored manifest past the cap is read when a refused merge cannot
place it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: fail a flush over a peer's chunk change unless -concurrentWriteMerge

A content flush refused because another mount committed first used to
merge the handle's writes onto the stored entry. That merge now runs
only with the new -concurrentWriteMerge mount option (off by default).
By default a stored chunk change that neither the base nor the handle
holds fails the flush with EIO, and a peer's chunk event no longer
rebases a dirty handle. Attribute-only changes and the handle's own
commits (a lost answer, its own truncate) still merge.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: recognize the shortened references a failed flush sent

A truncate shrinks a base chunk in place, keeping its file id. A failed
flush that may have stored such a reference recorded only file ids base
lacks, so a later truncate saw its own stored reference as a peer's
change and failed with EIO by default.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 12:34:30 -08:00

618 lines
25 KiB
Go

package mount
import (
"context"
"errors"
"slices"
"sync"
"testing"
"time"
"github.com/seaweedfs/go-fuse/v2/fuse"
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
)
type contentFlushConditionFiler struct {
filer_pb.UnimplementedSeaweedFilerServer
mu sync.Mutex
entry *filer_pb.Entry
calls int
}
func (s *contentFlushConditionFiler) CreateEntry(ctx context.Context, r *filer_pb.CreateEntryRequest) (*filer_pb.CreateEntryResponse, error) {
s.mu.Lock()
defer s.mu.Unlock()
s.calls++
for _, c := range r.GetCondition().GetClauses() {
matches := false
switch c.Kind {
case filer_pb.WriteCondition_IF_ENTRY_EQUAL:
matches = proto.Equal(c.ExpectedEntry, s.entry)
case filer_pb.WriteCondition_IF_NOT_EXISTS:
matches = s.entry == nil
}
if !matches {
return &filer_pb.CreateEntryResponse{Error: "precondition failed", ErrorCode: filer_pb.FilerError_PRECONDITION_FAILED}, nil
}
}
// Like the filer, stamp the stored version and announce what was stored.
old := s.entry
s.entry = proto.Clone(r.Entry).(*filer_pb.Entry)
s.entry.CommitTsNs = 1000 + int64(s.calls)
return &filer_pb.CreateEntryResponse{LogTsNs: 2000, MetadataEvent: &filer_pb.SubscribeMetadataResponse{
Directory: r.Directory,
TsNs: s.entry.CommitTsNs,
EventNotification: &filer_pb.EventNotification{
OldEntry: old,
NewEntry: proto.Clone(s.entry).(*filer_pb.Entry),
},
}}, nil
}
func (s *contentFlushConditionFiler) UpdateEntry(ctx context.Context, r *filer_pb.UpdateEntryRequest) (*filer_pb.UpdateEntryResponse, error) {
s.mu.Lock()
defer s.mu.Unlock()
if clauses := r.GetCondition().GetClauses(); len(clauses) > 0 && !proto.Equal(clauses[0].GetExpectedEntry(), s.entry) {
return nil, status.Error(codes.FailedPrecondition, "entry changed")
}
s.calls++
s.entry = proto.Clone(r.Entry).(*filer_pb.Entry)
s.entry.CommitTsNs = 1000 + int64(s.calls)
return &filer_pb.UpdateEntryResponse{LogTsNs: 2000}, nil
}
func (s *contentFlushConditionFiler) LookupDirectoryEntry(ctx context.Context, r *filer_pb.LookupDirectoryEntryRequest) (*filer_pb.LookupDirectoryEntryResponse, error) {
s.mu.Lock()
defer s.mu.Unlock()
if s.entry == nil {
return nil, status.Error(codes.NotFound, filer_pb.ErrNotFound.Error())
}
return &filer_pb.LookupDirectoryEntryResponse{Entry: proto.Clone(s.entry).(*filer_pb.Entry)}, nil
}
func flushConditionEntry(ids ...uint64) *filer_pb.Entry {
e := &filer_pb.Entry{Name: "file", Attributes: &filer_pb.FuseAttributes{Inode: 42, FileMode: 0644, Uid: 2000, Gid: 20, FileSize: 3}}
for i, id := range ids {
e.Chunks = append(e.Chunks, &filer_pb.FileChunk{Fid: &filer_pb.FileId{VolumeId: 1, FileKey: id, Cookie: 1}, Offset: int64(i), Size: 1, ModifiedTsNs: int64(id)})
}
return e
}
// Exercise the actual mount content-publication caller. A peer's append is
// merged with ours; a replaced or deleted file is not ours to merge into, so
// the refused flush keeps our dirty entry instead of overwriting the peer.
func TestContentFlushPreservesPeerChunks(t *testing.T) {
for _, name := range []string{"single-writer", "peer-append", "replaced", "deleted", "deferred-create", "deferred-create-race"} {
t.Run(name, func(t *testing.T) {
wfs := newInvalidateTestWFS(t)
wfs.option.ConcurrentWriteMerge = true
observed := flushConditionEntry(1)
live := flushConditionEntry(1, 3)
live.Chunks[1].Offset = 2
current := flushConditionEntry(1)
wantError, wantCalls := false, 1
switch name {
case "peer-append":
current = flushConditionEntry(1, 2)
wantCalls = 2
case "replaced":
current = flushConditionEntry(4)
current.Attributes.Inode = 99
wantError = true
case "deleted":
current = nil
wantError = true
case "deferred-create":
observed = nil
current = nil
case "deferred-create-race":
observed = nil
current = flushConditionEntry(4)
current.Attributes.Inode = 99
wantError = true
}
if observed != nil {
wfs.mapPbIdFromFilerToLocal(observed)
}
wfs.mapPbIdFromFilerToLocal(live)
fake := &contentFlushConditionFiler{entry: current}
startFakeFiler(t, wfs, fake)
fh := newFileHandle(wfs, 1, 42, live)
t.Cleanup(func() { fh.dirtyPages.Destroy() })
fh.baseEntry.Store(observed)
fh.dirtyMetadata = true
fh.RememberPath(util.FullPath("/dir/file"))
before := proto.Clone(current)
code := wfs.doFlush(context.Background(), fh, 1000, 20, false, 0)
fake.mu.Lock()
defer fake.mu.Unlock()
if wantError {
if code == fuse.OK {
t.Fatal("stale content flush acknowledged after a peer changed the entry")
}
if !proto.Equal(fake.entry, before) {
t.Fatalf("peer entry changed: %v", fake.entry)
}
if !fh.dirtyMetadata {
t.Fatal("refused flush lost dirty metadata")
}
if len(fh.entry.Entry.Chunks) != 2 || fh.entry.Entry.Chunks[1].GetFileIdString() != live.Chunks[1].GetFileIdString() {
t.Fatal("refused flush lost pending content")
}
} else {
if code != fuse.OK {
t.Fatal(code)
}
if fh.dirtyMetadata {
t.Fatal("successful flush remains dirty")
}
want := []uint64{1, 3}
if name == "peer-append" {
want = []uint64{1, 2, 3}
}
if got := chunkFileKeys(fake.entry); !slices.Equal(got, want) {
t.Fatalf("stored chunks %v, want %v", got, want)
}
if got := chunkFileKeys(fh.GetEntry().GetEntry()); !slices.Equal(got, want) {
t.Fatalf("handle chunks %v, want %v", got, want)
}
}
if fake.calls != wantCalls {
t.Fatalf("unexpected replay: %d calls", fake.calls)
}
})
}
}
// Each acknowledged flush must leave the handle a base the next flush on the
// same handle can match: the stored entry, with the filer's own commit stamp.
func TestContentFlushRepeatsOnOneHandle(t *testing.T) {
wfs := newInvalidateTestWFS(t)
stored := flushConditionEntry(1)
stored.CommitTsNs = 7
fake := &contentFlushConditionFiler{entry: proto.Clone(stored).(*filer_pb.Entry)}
startFakeFiler(t, wfs, fake)
observed := proto.Clone(stored).(*filer_pb.Entry)
wfs.mapPbIdFromFilerToLocal(observed)
fh := newFileHandle(wfs, 1, 42, proto.Clone(observed).(*filer_pb.Entry))
t.Cleanup(func() { fh.dirtyPages.Destroy() })
fh.RememberPath(util.FullPath("/dir/file"))
for i := uint64(2); i <= 3; i++ {
fh.UpdateEntry(func(e *filer_pb.Entry) {
e.Chunks = append(e.Chunks, &filer_pb.FileChunk{Fid: &filer_pb.FileId{VolumeId: 1, FileKey: i, Cookie: 1}, Offset: int64(i - 1), Size: 1, ModifiedTsNs: int64(i)})
e.Attributes.FileSize = i
})
fh.dirtyMetadata = true
if code := wfs.doFlush(context.Background(), fh, 1000, 20, false, 0); code != fuse.OK {
t.Fatalf("flush %d: %v", i-1, code)
}
}
fake.mu.Lock()
defer fake.mu.Unlock()
if len(fake.entry.Chunks) != 3 || fake.calls != 2 {
t.Fatalf("stored %d chunks after %d calls", len(fake.entry.Chunks), fake.calls)
}
}
type refusedThenStalledFiler struct {
*contentFlushConditionFiler
}
func (s *refusedThenStalledFiler) LookupDirectoryEntry(ctx context.Context, r *filer_pb.LookupDirectoryEntryRequest) (*filer_pb.LookupDirectoryEntryResponse, error) {
<-ctx.Done()
return nil, status.FromContextError(ctx.Err()).Err()
}
// A refused create stays refused when reading the stored entry stalls: the
// flush must not resend the create as if the filer had timed out.
func TestContentFlushRefusalWithStalledLookup(t *testing.T) {
wfs := newInvalidateTestWFS(t)
wfs.filerCallTimeout = 200 * time.Millisecond
fake := &contentFlushConditionFiler{entry: flushConditionEntry(1, 2)}
startFakeFiler(t, wfs, &refusedThenStalledFiler{fake})
oldSleep := metadataFlushSleep
metadataFlushSleep = func(context.Context, time.Duration) {}
t.Cleanup(func() { metadataFlushSleep = oldSleep })
observed := flushConditionEntry(1)
wfs.mapPbIdFromFilerToLocal(observed)
live := flushConditionEntry(1, 3)
live.Chunks[1].Offset = 2
wfs.mapPbIdFromFilerToLocal(live)
fh := newFileHandle(wfs, 1, 42, live)
t.Cleanup(func() { fh.dirtyPages.Destroy() })
fh.baseEntry.Store(observed)
fh.dirtyMetadata = true
fh.RememberPath(util.FullPath("/dir/file"))
start := time.Now()
code := wfs.doFlush(context.Background(), fh, 1000, 20, false, 0)
elapsed := time.Since(start)
fake.mu.Lock()
defer fake.mu.Unlock()
if code == fuse.OK || !fh.dirtyMetadata {
t.Fatalf("refused flush reported %v, dirty=%v", code, fh.dirtyMetadata)
}
if fake.calls != 1 || elapsed > 2*time.Second {
t.Fatalf("refused create sent %d times over %v", fake.calls, elapsed)
}
if got := chunkFileKeys(fake.entry); !slices.Equal(got, []uint64{1, 2}) {
t.Fatalf("stored chunks %v", got)
}
}
func chunkFileKeys(e *filer_pb.Entry) (keys []uint64) {
for _, c := range e.GetChunks() {
keys = append(keys, c.GetFid().GetFileKey())
}
return keys
}
// The periodic metadata flush publishes the same whole entry, so it must not
// overwrite a peer's append either.
func TestPeriodicMetadataFlushPreservesPeerChunks(t *testing.T) {
wfs := newInvalidateTestWFS(t)
wfs.option.ConcurrentWriteMerge = true
current := flushConditionEntry(1, 2)
fake := &contentFlushConditionFiler{entry: proto.Clone(current).(*filer_pb.Entry)}
startFakeFiler(t, wfs, fake)
observed := flushConditionEntry(1)
wfs.mapPbIdFromFilerToLocal(observed)
live := flushConditionEntry(1, 3)
live.Chunks[1].Offset = 2
wfs.mapPbIdFromFilerToLocal(live)
fh := newFileHandle(wfs, 1, 42, live)
t.Cleanup(func() { fh.dirtyPages.Destroy() })
fh.baseEntry.Store(observed)
fh.dirtyMetadata = true
fh.RememberPath(util.FullPath("/dir/file"))
if err := wfs.flushFileMetadata(fh); err != nil {
t.Fatal(err)
}
fake.mu.Lock()
defer fake.mu.Unlock()
if got := chunkFileKeys(fake.entry); !slices.Equal(got, []uint64{1, 2, 3}) || fake.calls != 2 {
t.Fatalf("stored chunks %v after %d calls", got, fake.calls)
}
if got := chunkFileKeys(fh.GetEntry().GetEntry()); !slices.Equal(got, []uint64{1, 2, 3}) {
t.Fatalf("handle chunks %v", got)
}
if !fh.dirtyMetadata {
t.Fatal("periodic flush cleared dirty metadata")
}
}
// Every merged flush is followed by a flush that matches what it stored, so
// the peer's chunk is not dropped by the next publication from this handle.
func TestContentFlushMergeThenRepeat(t *testing.T) {
wfs := newInvalidateTestWFS(t)
wfs.option.ConcurrentWriteMerge = true
fake := &contentFlushConditionFiler{entry: flushConditionEntry(1, 2)}
startFakeFiler(t, wfs, fake)
observed := flushConditionEntry(1)
wfs.mapPbIdFromFilerToLocal(observed)
live := flushConditionEntry(1, 3)
live.Chunks[1].Offset = 2
wfs.mapPbIdFromFilerToLocal(live)
fh := newFileHandle(wfs, 1, 42, live)
t.Cleanup(func() { fh.dirtyPages.Destroy() })
fh.baseEntry.Store(observed)
fh.RememberPath(util.FullPath("/dir/file"))
for i := uint64(4); i <= 5; i++ {
fh.dirtyMetadata = true
if code := wfs.doFlush(context.Background(), fh, 1000, 20, false, 0); code != fuse.OK {
t.Fatalf("flush before chunk %d: %v", i, code)
}
fh.UpdateEntry(func(e *filer_pb.Entry) {
e.Chunks = append(e.Chunks, &filer_pb.FileChunk{Fid: &filer_pb.FileId{VolumeId: 1, FileKey: i, Cookie: 1}, Offset: int64(i - 1), Size: 1, ModifiedTsNs: int64(i)})
e.Attributes.FileSize = i
})
}
fh.dirtyMetadata = true
if code := wfs.doFlush(context.Background(), fh, 1000, 20, false, 0); code != fuse.OK {
t.Fatal(code)
}
fake.mu.Lock()
defer fake.mu.Unlock()
if got := chunkFileKeys(fake.entry); !slices.Equal(got, []uint64{1, 2, 3, 4, 5}) || fake.calls != 4 {
t.Fatalf("stored chunks %v after %d calls", got, fake.calls)
}
}
// The merge keeps a peer's attribute and extended changes the handle did not
// make, applies the ones it did, and lets a local truncation clip the base's
// chunks past the new end, but not the chunks a peer wrote since.
func TestMergeContentEntry(t *testing.T) {
base := flushConditionEntry(1, 2)
base.Extended = map[string][]byte{"user.a": []byte("1"), "user.b": []byte("1")}
stored := proto.Clone(base).(*filer_pb.Entry)
stored.Chunks = append(stored.Chunks, &filer_pb.FileChunk{Fid: &filer_pb.FileId{VolumeId: 1, FileKey: 9, Cookie: 1}, Offset: 2, Size: 4})
stored.Attributes.FileMode, stored.Attributes.FileSize = 0600, 6
stored.Extended["user.b"] = []byte("peer")
stored.CommitTsNs = 77
local := proto.Clone(base).(*filer_pb.Entry)
local.Attributes.Mtime = 123
local.Extended["user.a"] = []byte("mine")
delete(local.Extended, "user.b")
local.Chunks = append(local.Chunks, &filer_pb.FileChunk{FileId: "1,0300000001", Offset: 2, Size: 1, ModifiedTsNs: 3})
merged, ok := mergeContentEntry(stored, base, local, local, -1, false)
if !ok {
t.Fatal("not merged")
}
if got := len(merged.Chunks); got != 4 || merged.Attributes.FileSize != 6 {
t.Fatalf("merged %d chunks, size %d", got, merged.Attributes.FileSize)
}
if merged.Attributes.FileMode != 0600 || merged.Attributes.Mtime != 123 || merged.CommitTsNs != 77 {
t.Fatalf("attributes %v", merged.Attributes)
}
if string(merged.Extended["user.a"]) != "mine" || merged.Extended["user.b"] != nil {
t.Fatalf("extended %v", merged.Extended)
}
truncated := proto.Clone(base).(*filer_pb.Entry)
truncated.Chunks = truncated.Chunks[:1]
truncated.Attributes.FileSize = 1
if _, ok := mergeContentEntry(stored, base, truncated, truncated, -1, false); ok {
t.Fatal("a stale truncate dropped the chunk a peer wrote")
}
peerChmod := proto.Clone(base).(*filer_pb.Entry)
peerChmod.Attributes.FileMode = 0600
merged, ok = mergeContentEntry(peerChmod, base, truncated, truncated, -1, false)
if !ok {
t.Fatal("truncation not merged")
}
if got := chunkFileKeys(merged); !slices.Equal(got, []uint64{1}) || merged.Attributes.FileSize != 1 || merged.Attributes.FileMode != 0600 {
t.Fatalf("truncated merge kept %v, size %d, mode %o", got, merged.Attributes.FileSize, merged.Attributes.FileMode)
}
// Truncated to 1, then written at 7: the base chunk at 1 stays cut, and a
// peer chunk inside the truncated size stays.
regrown := proto.Clone(truncated).(*filer_pb.Entry)
regrown.Chunks = append(regrown.Chunks, &filer_pb.FileChunk{Fid: &filer_pb.FileId{VolumeId: 1, FileKey: 7, Cookie: 1}, Offset: 7, Size: 1, ModifiedTsNs: 7})
regrown.Attributes.FileSize = 8
peerInside := proto.Clone(base).(*filer_pb.Entry)
peerInside.Chunks = append(peerInside.Chunks, &filer_pb.FileChunk{Fid: &filer_pb.FileId{VolumeId: 1, FileKey: 9, Cookie: 1}, Offset: 0, Size: 1, ModifiedTsNs: 9})
merged, ok = mergeContentEntry(peerInside, base, regrown, regrown, 1, false)
if !ok {
t.Fatal("truncate then write not merged")
}
if got := chunkFileKeys(merged); !slices.Equal(got, []uint64{1, 9, 7}) || merged.Attributes.FileSize != 8 {
t.Fatalf("truncate then write kept %v, size %d", got, merged.Attributes.FileSize)
}
if _, ok := mergeContentEntry(stored, base, regrown, regrown, 1, false); ok {
t.Fatal("a stale truncate cut the chunk a peer wrote")
}
// The handle's own commit whose answer was lost is no peer write.
committed := proto.Clone(base).(*filer_pb.Entry)
committed.Chunks = append(committed.Chunks, regrown.Chunks[1])
merged, ok = mergeContentEntry(committed, base, regrown, regrown, 1, false)
if !ok {
t.Fatal("refused the handle's own committed chunk")
}
if got := chunkFileKeys(merged); !slices.Equal(got, []uint64{1, 7}) || merged.Attributes.FileSize != 8 {
t.Fatalf("replayed commit kept %v, size %d", got, merged.Attributes.FileSize)
}
grown := proto.Clone(base).(*filer_pb.Entry)
grown.Chunks = append(grown.Chunks, regrown.Chunks[1])
grown.Attributes.FileSize = 8
committed.Attributes.FileSize = 8
merged, ok = mergeContentEntry(committed, base, grown, grown, -1, false)
if got := chunkFileKeys(merged); !ok || !slices.Equal(got, []uint64{1, 2, 7}) {
t.Fatalf("replayed commit without a truncate kept %v", got)
}
// Shortening a manifest reference leaves its members whole.
peerManifest := proto.Clone(base).(*filer_pb.Entry)
peerManifest.Chunks = append(peerManifest.Chunks, &filer_pb.FileChunk{Fid: &filer_pb.FileId{VolumeId: 1, FileKey: 5, Cookie: 1}, Offset: 0, Size: 10, IsChunkManifest: true})
if _, ok := mergeContentEntry(peerManifest, base, regrown, regrown, 1, false); ok {
t.Fatal("merged a peer manifest across the truncate")
}
// A peer truncate stands when the handle only overwrote inside it.
peerTruncated := proto.Clone(base).(*filer_pb.Entry)
peerTruncated.Chunks, peerTruncated.Attributes.FileSize = peerTruncated.Chunks[:1], 1
overwrote := proto.Clone(base).(*filer_pb.Entry)
overwrote.Chunks = append(overwrote.Chunks, &filer_pb.FileChunk{Fid: &filer_pb.FileId{VolumeId: 1, FileKey: 6, Cookie: 1}, Size: 1, ModifiedTsNs: 6})
merged, ok = mergeContentEntry(peerTruncated, base, overwrote, overwrote, -1, false)
if !ok {
t.Fatal("overwrite over a peer truncate not merged")
}
if got := chunkFileKeys(merged); !slices.Equal(got, []uint64{1, 6}) || merged.Attributes.FileSize != 1 {
t.Fatalf("overwrite over a peer truncate kept %v, size %d", got, merged.Attributes.FileSize)
}
// A rewrite folded into a manifest is still a rewrite.
rewritten := proto.Clone(base).(*filer_pb.Entry)
rewritten.Chunks = []*filer_pb.FileChunk{{Fid: &filer_pb.FileId{VolumeId: 1, FileKey: 8, Cookie: 1}, Size: 2, IsChunkManifest: true}}
if _, ok := mergeContentEntry(stored, base, rewritten, rewritten, -1, true); ok {
t.Fatal("merged a stale whole-file rewrite over a peer append")
}
if _, ok := mergeContentEntry(stored, base, rewritten, rewritten, -1, false); !ok {
t.Fatal("refused new chunks the handle did not rewrite")
}
if _, ok := mergeContentEntry(proto.Clone(base).(*filer_pb.Entry), base, rewritten, rewritten, -1, true); !ok {
t.Fatal("refused a rewrite with no peer chunk change")
}
stored.Attributes.Inode = 99
if _, ok := mergeContentEntry(stored, base, local, local, -1, false); ok {
t.Fatal("merged into a replaced file")
}
unstamped := flushConditionEntry(4)
unstamped.Chunks[0].Size, unstamped.Chunks[0].ModifiedTsNs = 3, 0
shortened := proto.Clone(unstamped).(*filer_pb.Entry)
shortened.Chunks[0].Size, shortened.Attributes.FileSize = 2, 2
peerWrite := proto.Clone(unstamped).(*filer_pb.Entry)
peerWrite.Chunks = append(peerWrite.Chunks, &filer_pb.FileChunk{Fid: &filer_pb.FileId{VolumeId: 1, FileKey: 9, Cookie: 1}, Offset: 1, Size: 1, ModifiedTsNs: 9})
if _, ok := mergeContentEntry(peerWrite, unstamped, shortened, shortened, 2, false); !ok {
t.Fatal("refused a truncate that shortened an unstamped base chunk")
}
// A base chunk the peer shortened holds no peer data: the truncate cuts it.
peerShortened := proto.Clone(unstamped).(*filer_pb.Entry)
peerShortened.Chunks[0].Size, peerShortened.Attributes.FileSize = 2, 2
cut := proto.Clone(unstamped).(*filer_pb.Entry)
cut.Chunks[0].Size, cut.Attributes.FileSize = 1, 1
if merged, ok := mergeContentEntry(peerShortened, unstamped, cut, cut, 1, false); !ok || merged.Attributes.FileSize != 1 {
t.Fatal("refused a truncate over a base chunk the peer shortened")
}
noInode := proto.Clone(base).(*filer_pb.Entry)
noInode.Attributes.Inode, noInode.Attributes.Crtime = 0, 5
sameFile := proto.Clone(noInode).(*filer_pb.Entry)
sameFile.Chunks = append(sameFile.Chunks, stored.Chunks[2])
if _, ok := mergeContentEntry(sameFile, noInode, local, local, -1, false); !ok {
t.Fatal("refused an inode-less file with the same creation time")
}
sameFile.Attributes.Crtime = 6
if _, ok := mergeContentEntry(sameFile, noInode, local, local, -1, false); ok {
t.Fatal("merged into an inode-less replacement")
}
empty := flushConditionEntry()
inline := proto.Clone(empty).(*filer_pb.Entry)
inline.Content = []byte("peer")
appended := proto.Clone(empty).(*filer_pb.Entry)
appended.Chunks = flushConditionEntry(5).Chunks
if _, ok := mergeContentEntry(inline, empty, appended, appended, -1, false); ok {
t.Fatal("merged chunks next to inline content")
}
if _, ok := mergeContentEntry(appended, empty, inline, inline, -1, false); ok {
t.Fatal("merged inline content next to chunks")
}
}
// A delayed flush keeps the later of two moved times, and a time only the
// handle moved, even back.
func TestMergeContentEntryKeepsLaterTime(t *testing.T) {
base := flushConditionEntry(1)
base.Attributes.Mtime, base.Attributes.Ctime = 100, 100
stored := proto.Clone(base).(*filer_pb.Entry)
stored.Chunks = append(stored.Chunks, &filer_pb.FileChunk{Fid: &filer_pb.FileId{VolumeId: 1, FileKey: 9, Cookie: 1}, Offset: 1, Size: 1, ModifiedTsNs: 9})
stored.Attributes.Mtime, stored.Attributes.MtimeNs = 300, 5
stored.Attributes.Ctime, stored.Attributes.CtimeNs = 300, 5
local := proto.Clone(base).(*filer_pb.Entry)
local.Chunks = append(local.Chunks, &filer_pb.FileChunk{Fid: &filer_pb.FileId{VolumeId: 1, FileKey: 7, Cookie: 1}, Offset: 2, Size: 1, ModifiedTsNs: 7})
local.Attributes.Mtime, local.Attributes.MtimeNs = 300, 1
local.Attributes.Ctime, local.Attributes.CtimeNs = 200, 0
merged, ok := mergeContentEntry(stored, base, local, local, -1, false)
if !ok {
t.Fatal("not merged")
}
if a := merged.Attributes; a.Mtime != 300 || a.MtimeNs != 5 || a.Ctime != 300 || a.CtimeNs != 5 {
t.Fatalf("delayed flush moved times back: mtime %d.%d ctime %d.%d", a.Mtime, a.MtimeNs, a.Ctime, a.CtimeNs)
}
local.Attributes.Mtime, local.Attributes.MtimeNs = 400, 0
if merged, _ = mergeContentEntry(stored, base, local, local, -1, false); merged.Attributes.Mtime != 400 || merged.Attributes.MtimeNs != 0 {
t.Fatalf("later local mtime not kept: %d.%d", merged.Attributes.Mtime, merged.Attributes.MtimeNs)
}
peerChmod := proto.Clone(base).(*filer_pb.Entry)
peerChmod.Attributes.FileMode = 0600
local.Attributes.Mtime, local.Attributes.MtimeNs = 50, 0
if merged, _ = mergeContentEntry(peerChmod, base, local, local, -1, false); merged.Attributes.Mtime != 50 {
t.Fatalf("mtime only the handle set: %d", merged.Attributes.Mtime)
}
}
// A miss on state no peer changed is republished once against the stored
// entry: a rename while open, or a filer uid the local id mapping cannot
// reproduce (newInvalidateTestWFS maps local 1000 to filer 2000).
func TestContentFlushRepublishesUnchangedEntry(t *testing.T) {
for _, name := range []string{"renamed", "colliding-uid"} {
t.Run(name, func(t *testing.T) {
wfs := newInvalidateTestWFS(t)
stored := flushConditionEntry(1)
stored.CommitTsNs = 7
observed := proto.Clone(stored).(*filer_pb.Entry)
path := util.FullPath("/dir/file")
switch name {
case "renamed":
wfs.mapPbIdFromFilerToLocal(observed)
stored.Name, stored.CommitTsNs = "moved", 8
path = "/dir/moved"
case "colliding-uid":
stored.Attributes.Uid, observed.Attributes.Uid = 1000, 1000
}
fake := &contentFlushConditionFiler{entry: proto.Clone(stored).(*filer_pb.Entry)}
startFakeFiler(t, wfs, fake)
live := proto.Clone(observed).(*filer_pb.Entry)
live.Name = stored.Name
live.Chunks = append(live.Chunks, &filer_pb.FileChunk{Fid: &filer_pb.FileId{VolumeId: 1, FileKey: 3, Cookie: 1}, Offset: 1, Size: 1, ModifiedTsNs: 3})
fh := newFileHandle(wfs, 1, 42, live)
t.Cleanup(func() { fh.dirtyPages.Destroy() })
fh.baseEntry.Store(observed)
fh.dirtyMetadata = true
fh.RememberPath(path)
if code := wfs.doFlush(context.Background(), fh, 1000, 20, false, 0); code != fuse.OK {
t.Fatal(code)
}
fake.mu.Lock()
defer fake.mu.Unlock()
if len(fake.entry.Chunks) != 2 || fake.calls != 2 {
t.Fatalf("stored %d chunks after %d calls", len(fake.entry.Chunks), fake.calls)
}
})
}
}
// Rebasing keeps a size the handle reached after the flush was taken, not the
// size it sent: a peer's truncate merged in must stand.
func TestRebaseEntryKeepsMergedSize(t *testing.T) {
wfs := newInvalidateTestWFS(t)
inode := wfs.inodeToPath.Lookup(util.FullPath("/dir/file"), time.Now().Unix(), false, false, 0, false)
fh, _, _, _ := wfs.fhMap.AcquireFileHandle(wfs, inode, flushConditionEntry(1, 2), 0, 0)
sent := proto.Clone(fh.GetEntry().GetEntry()).(*filer_pb.Entry)
merged := flushConditionEntry(1)
merged.Attributes.FileSize = 1
fh.rebaseEntry(merged, sent)
if size := fh.GetEntry().GetEntry().Attributes.FileSize; size != 1 {
t.Fatalf("size %d after rebasing onto a truncate, want 1", size)
}
fh.GetEntry().GetEntry().Attributes.FileSize = 5
fh.rebaseEntry(merged, sent)
if size := fh.GetEntry().GetEntry().Attributes.FileSize; size != 5 {
t.Fatalf("size %d after a write grew the handle, want 5", size)
}
}
// A merged entry whose manifest cannot be resolved yet leaves the read view
// marked, and the next read resolves it again instead of failing for good.
func TestRebaseEntryRetriesUnresolvedChunkView(t *testing.T) {
wfs := newInvalidateTestWFS(t)
inode := wfs.inodeToPath.Lookup(util.FullPath("/dir/file"), time.Now().Unix(), false, false, 0, false)
fh, _, _, _ := wfs.fhMap.AcquireFileHandle(wfs, inode, flushConditionEntry(1), 0, 0)
lookupDown := true
group, err := filer.NewChunkGroup(func(ctx context.Context, fileId string) ([]string, error) {
if lookupDown {
return nil, errors.New("volume lookup down")
}
return nil, nil
}, nil, nil, 1, nil, nil)
if err != nil {
t.Fatal(err)
}
fh.entryChunkGroup = group
sent := proto.Clone(fh.GetEntry().GetEntry()).(*filer_pb.Entry)
merged := flushConditionEntry(1, 2)
merged.Chunks[1].IsChunkManifest = true
fh.rebaseEntry(merged, sent)
if !fh.chunkViewStale.Load() {
t.Fatal("unresolved manifest not marked")
}
lookupDown = false
fh.UpdateEntry(func(e *filer_pb.Entry) { e.Chunks = e.Chunks[:1] })
fh.refreshChunkView()
if fh.chunkViewStale.Load() {
t.Fatal("read view still marked after the chunks resolved")
}
}