diff --git a/weed/mount/flush_fold.go b/weed/mount/flush_fold.go new file mode 100644 index 0000000000..f8d93520dd --- /dev/null +++ b/weed/mount/flush_fold.go @@ -0,0 +1,90 @@ +package mount + +import ( + "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" + "google.golang.org/protobuf/proto" +) + +// chunkFold is what one flush's MaybeManifestize packed into manifests: the +// manifests it saved and the chunks they hold. Until the filer acknowledges +// the flush, those manifests only wrap chunks the handle already had. A merge +// onto a peer's entry must see the chunks instead: it treats a chunk that is +// not in the base as the handle's own write and a base chunk missing from the +// handle as one it dropped, so a manifest of base chunks would put back chunks +// the peer dropped (a truncate, or a rewrite of the whole file), and a peer +// that folded the same chunks would end up with both manifests. +type chunkFold struct { + manifests map[string]bool // contentChunkKey of each manifest the fold saved + members []*filer_pb.FileChunk +} + +// newChunkFold compares the chunks a flush passed to MaybeManifestize with +// the chunks it got back. nil when nothing was folded. +func newChunkFold(before, after []*filer_pb.FileChunk) *chunkFold { + inBefore := make(map[string]bool, len(before)) + for _, c := range before { + inBefore[contentChunkKey(c)] = true + } + fold := &chunkFold{manifests: make(map[string]bool)} + inAfter := make(map[string]bool, len(after)) + for _, c := range after { + key := contentChunkKey(c) + inAfter[key] = true + if c.IsChunkManifest && !inBefore[key] { + fold.manifests[key] = true + } + } + if len(fold.manifests) == 0 { + return nil + } + for _, c := range before { + if !inAfter[contentChunkKey(c)] { + fold.members = append(fold.members, c) + } + } + return fold +} + +// unfoldChunks returns chunks with the fold's manifests replaced by the chunks +// they hold. A list that lacks any of those manifests is returned as it is: +// it was not built from this fold. +func (f *chunkFold) unfoldChunks(chunks []*filer_pb.FileChunk) []*filer_pb.FileChunk { + if f == nil { + return chunks + } + unfolded := make([]*filer_pb.FileChunk, 0, len(chunks)-len(f.manifests)+len(f.members)) + found := 0 + for _, c := range chunks { + if c.IsChunkManifest && f.manifests[contentChunkKey(c)] { + found++ + continue + } + unfolded = append(unfolded, c) + } + if found != len(f.manifests) { + return chunks + } + return append(unfolded, f.members...) +} + +// unfold returns a copy of entry with the fold undone (see unfoldChunks). +func (f *chunkFold) unfold(entry *filer_pb.Entry) *filer_pb.Entry { + if f == nil || entry == nil { + return entry + } + unfolded := proto.Clone(entry).(*filer_pb.Entry) + unfolded.Chunks = f.unfoldChunks(entry.Chunks) + return unfolded +} + +// revert puts the folded chunks back into the handle after the flush failed: +// the handle's base still has them, and a later merge or flush must not take +// the manifests for writes of its own. +func (f *chunkFold) revert(fh *FileHandle) { + if f == nil { + return + } + fh.UpdateEntry(func(e *filer_pb.Entry) { + e.Chunks = f.unfoldChunks(e.Chunks) + }) +} diff --git a/weed/mount/flush_fold_test.go b/weed/mount/flush_fold_test.go new file mode 100644 index 0000000000..31a6b911fe --- /dev/null +++ b/weed/mount/flush_fold_test.go @@ -0,0 +1,267 @@ +package mount + +import ( + "context" + "fmt" + "io" + "sync/atomic" + "testing" + + "github.com/seaweedfs/go-fuse/v2/fuse" + "github.com/stretchr/testify/require" + "google.golang.org/protobuf/proto" + + "github.com/seaweedfs/seaweedfs/weed/filer" + "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" + "github.com/seaweedfs/seaweedfs/weed/util" + "github.com/seaweedfs/seaweedfs/weed/wdclient" +) + +// foldMergeSaver stands in for wfs.saveDataAsChunk when a test folds chunks: +// each saved manifest gets a fresh file id. +type foldMergeSaver struct{ saved uint64 } + +func (s *foldMergeSaver) save(reader io.Reader, name string, offset int64, tsNs int64, expectedDataSize uint64) (*filer_pb.FileChunk, error) { + if _, err := io.Copy(io.Discard, reader); err != nil { + return nil, err + } + s.saved++ + return &filer_pb.FileChunk{Fid: &filer_pb.FileId{VolumeId: 9, FileKey: s.saved, Cookie: 1}, ModifiedTsNs: tsNs}, nil +} + +// mergeFoldFile is a file of n small appended chunks (base) whose handle +// appended one more chunk and folded on flush, as flushMetadataToFiler does +// (local, fold). +func mergeFoldFile(t *testing.T, n int) (base, local *filer_pb.Entry, fold *chunkFold) { + t.Helper() + var chunks []*filer_pb.FileChunk + for i := 0; i < n; i++ { + chunks = append(chunks, &filer_pb.FileChunk{Fid: &filer_pb.FileId{VolumeId: 1, FileKey: uint64(i + 1), Cookie: 1}, Offset: int64(i) * 300, Size: 300, ModifiedTsNs: int64(i + 1)}) + } + attrs := &filer_pb.FuseAttributes{Inode: 7, FileSize: uint64(n) * 300, FileMode: 0644} + base = &filer_pb.Entry{Name: "progress.jsonl", Attributes: attrs, Chunks: chunks} + + appended := &filer_pb.FileChunk{Fid: &filer_pb.FileId{VolumeId: 1, FileKey: uint64(n + 1), Cookie: 1}, Offset: int64(n) * 300, Size: 300, ModifiedTsNs: int64(n + 1)} + before := append(append([]*filer_pb.FileChunk(nil), chunks...), appended) + saver := &foldMergeSaver{} + after, err := filer.MaybeManifestize(saver.save, nil, before) + require.NoError(t, err) + fold = newChunkFold(before, after) + require.NotNil(t, fold) + localAttrs := proto.Clone(attrs).(*filer_pb.FuseAttributes) + localAttrs.FileSize = uint64(n+1) * 300 + local = &filer_pb.Entry{Name: "progress.jsonl", Attributes: localAttrs, Chunks: after} + return base, local, fold +} + +func referenceCounts(chunks []*filer_pb.FileChunk) map[string]int { + keys := make(map[string]int, len(chunks)) + for _, c := range chunks { + keys[contentChunkKey(c)]++ + } + return keys +} + +func TestChunkFoldUnfoldsToTheChunksItHolds(t *testing.T) { + base, local, fold := mergeFoldFile(t, filer.SmallChunkManifestBatch+44) + manifests, _ := filer.SeparateManifestChunks(local.Chunks) + require.Len(t, manifests, 1) + require.Len(t, fold.members, filer.SmallChunkManifestBatch) + + unfolded := fold.unfoldChunks(local.Chunks) + want := referenceCounts(base.Chunks) + got := referenceCounts(unfolded) + require.Len(t, got, len(want)+1, "the base chunks and the appended one") + for key := range want { + require.Equal(t, 1, got[key], "chunk %s", key) + } + + // a list that is missing one of the fold's manifests is left alone + require.Equal(t, base.Chunks, fold.unfoldChunks(base.Chunks)) + var nilFold *chunkFold + require.Equal(t, local.Chunks, nilFold.unfoldChunks(local.Chunks)) + require.Nil(t, newChunkFold(base.Chunks, base.Chunks)) +} + +// A peer that folded the same chunks: the merge keeps the peer's manifest and +// does not add the handle's, so each chunk is referenced once. +func TestMergeAfterFoldKeepsThePeersFoldOnly(t *testing.T) { + base, local, fold := mergeFoldFile(t, filer.SmallChunkManifestBatch+44) + peerSaver := &foldMergeSaver{saved: 1000} + peerChunks, err := filer.MaybeManifestize(peerSaver.save, nil, append([]*filer_pb.FileChunk(nil), base.Chunks...)) + require.NoError(t, err) + peerManifests, _ := filer.SeparateManifestChunks(peerChunks) + require.Len(t, peerManifests, 1) + stored := proto.Clone(base).(*filer_pb.Entry) + stored.Chunks = peerChunks + + merged, ok := mergeContentEntry(stored, base, fold.unfold(local), fold.unfold(local), -1, false) + require.True(t, ok) + manifests, _ := filer.SeparateManifestChunks(merged.Chunks) + require.Len(t, manifests, 1) + require.Equal(t, contentChunkKey(peerManifests[0]), contentChunkKey(manifests[0])) + + // without the unfold the handle's manifest of the same chunks is added too + merged, ok = mergeContentEntry(stored, base, local, local, -1, false) + require.True(t, ok) + manifests, _ = filer.SeparateManifestChunks(merged.Chunks) + require.Len(t, manifests, 2) +} + +// A peer that rewrote the whole file: the merge must not bring the base +// chunks back inside the handle's manifest, where they would cover the peer's +// rewritten bytes wherever their stamps are newer. +func TestMergeAfterFoldDoesNotRestoreChunksThePeerDropped(t *testing.T) { + base, local, fold := mergeFoldFile(t, filer.SmallChunkManifestBatch+44) + stored := proto.Clone(base).(*filer_pb.Entry) + stored.Chunks = []*filer_pb.FileChunk{{Fid: &filer_pb.FileId{VolumeId: 2, FileKey: 1, Cookie: 1}, Offset: 0, Size: base.Attributes.FileSize, ModifiedTsNs: 1}} + + merged, ok := mergeContentEntry(stored, base, fold.unfold(local), fold.unfold(local), -1, false) + require.True(t, ok) + keys := referenceCounts(merged.Chunks) + require.Len(t, keys, 2, "the peer's rewrite and the handle's appended chunk") + for _, c := range merged.Chunks { + require.False(t, c.IsChunkManifest, "no manifest of dropped chunks") + } + for _, c := range base.Chunks { + require.Zero(t, keys[contentChunkKey(c)], "base chunk %s came back", contentChunkKey(c)) + } + + // without the unfold the manifest of base chunks is added as a local write + merged, ok = mergeContentEntry(stored, base, local, local, -1, false) + require.True(t, ok) + manifests, _ := filer.SeparateManifestChunks(merged.Chunks) + require.Len(t, manifests, 1) +} + +// foldMergeFiler keeps the stored entry and its IF_ENTRY_EQUAL condition like +// contentFlushConditionFiler, and points AssignVolume at a volume stub, so a +// flush can store the manifests it folds. +type foldMergeFiler struct { + contentFlushConditionFiler + volume string + assigns atomic.Int32 +} + +func (s *foldMergeFiler) AssignVolume(ctx context.Context, req *filer_pb.AssignVolumeRequest) (*filer_pb.AssignVolumeResponse, error) { + n := s.assigns.Add(1) + return &filer_pb.AssignVolumeResponse{ + FileId: fmt.Sprintf("3,%02x637037d6", n), + Count: 1, + Location: &filer_pb.Location{Url: s.volume, PublicUrl: s.volume}, + }, nil +} + +// A flush that folds the handle's chunks finds the entry rewritten by a peer +// (a bloat merge on another mount: one chunk, stamped older than the bytes it +// replaced). The merged commit carries the peer's rewrite and the handle's own +// append, not a manifest of the base chunks the peer dropped. +func TestFlushMergeAfterFoldLeavesThePeersRewrite(t *testing.T) { + const n = filer.SmallChunkManifestBatch + 44 + wfs := newInvalidateTestWFS(t) + wfs.option.ConcurrentWriters = 4 + wfs.option.uniqueCacheDirForWrite = t.TempDir() + wfs.concurrentWriters = util.NewLimitedConcurrentExecutor(4) + fake := &foldMergeFiler{volume: newVolumeStub(t)} + startFakeFiler(t, wfs, fake) + wfs.option.VolumeServerAccess = "direct" + wfs.filerClient = wdclient.NewFilerClient(wfs.option.FilerAddresses, wfs.option.GrpcDialOption, "") + + attrs := &filer_pb.FuseAttributes{Inode: 42, FileMode: 0644, Uid: 2000, Gid: 20, FileSize: n * 300} + base := &filer_pb.Entry{Name: "file", Attributes: attrs} + for i := 0; i < n; i++ { + base.Chunks = append(base.Chunks, &filer_pb.FileChunk{Fid: &filer_pb.FileId{VolumeId: 1, FileKey: uint64(i + 1), Cookie: 1}, Offset: int64(i) * 300, Size: 300, ModifiedTsNs: int64(100 + i)}) + } + rewrite := &filer_pb.FileChunk{Fid: &filer_pb.FileId{VolumeId: 2, FileKey: 999, Cookie: 1}, Offset: 0, Size: n * 300, ModifiedTsNs: 1} + peer := proto.Clone(base).(*filer_pb.Entry) + peer.Chunks = []*filer_pb.FileChunk{rewrite} + fake.entry = peer + + appended := &filer_pb.FileChunk{Fid: &filer_pb.FileId{VolumeId: 1, FileKey: n + 1, Cookie: 1}, Offset: n * 300, Size: 300, ModifiedTsNs: 100 + n} + live := proto.Clone(base).(*filer_pb.Entry) + live.Chunks = append(live.Chunks, appended) + live.Attributes.FileSize = (n + 1) * 300 + observed := proto.Clone(base).(*filer_pb.Entry) + wfs.mapPbIdFromFilerToLocal(observed) + 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")) + fh.dirtyMetadata = true + + require.Equal(t, fuse.OK, wfs.doFlush(context.Background(), fh, 1000, 20, false, 0)) + require.NotZero(t, fake.assigns.Load(), "the flush folded and stored a manifest") + + fake.mu.Lock() + defer fake.mu.Unlock() + keys := referenceCounts(fake.entry.Chunks) + require.Len(t, keys, 2, "the peer's rewrite and the handle's append: %v", keys) + require.Equal(t, 1, keys[contentChunkKey(rewrite)]) + require.Equal(t, 1, keys[contentChunkKey(appended)]) +} + +// refoldingFiler folds the small chunks of a create before it stores and +// acknowledges it, as the filer's cleanupChunks does. +type refoldingFiler struct { + foldMergeFiler + saver foldMergeSaver +} + +func (s *refoldingFiler) CreateEntry(ctx context.Context, r *filer_pb.CreateEntryRequest) (*filer_pb.CreateEntryResponse, error) { + r = proto.Clone(r).(*filer_pb.CreateEntryRequest) + folded, err := filer.MaybeManifestize(s.saver.save, nil, r.Entry.Chunks) + if err != nil { + return nil, err + } + r.Entry.Chunks = folded + return s.foldMergeFiler.CreateEntry(ctx, r) +} + +// A peer's chmod refuses the flush's first commit; the merged retry sends the +// handle's 256 small chunks unfolded and the filer folds them before storing. +// The handle must hold what the filer stored, which is also its base: holding +// the flat chunks against a base that lists a manifest, a later merge would +// take them for writes of its own, after a peer truncated the file too. +func TestFlushMergeInstallsTheEntryTheFilerStored(t *testing.T) { + const n = filer.SmallChunkManifestBatch - 1 + wfs := newInvalidateTestWFS(t) + wfs.option.ConcurrentWriters = 4 + wfs.option.uniqueCacheDirForWrite = t.TempDir() + wfs.concurrentWriters = util.NewLimitedConcurrentExecutor(4) + fake := &refoldingFiler{foldMergeFiler: foldMergeFiler{volume: newVolumeStub(t)}, saver: foldMergeSaver{saved: 500}} + startFakeFiler(t, wfs, fake) + wfs.option.VolumeServerAccess = "direct" + wfs.filerClient = wdclient.NewFilerClient(wfs.option.FilerAddresses, wfs.option.GrpcDialOption, "") + + attrs := &filer_pb.FuseAttributes{Inode: 42, FileMode: 0644, Uid: 2000, Gid: 20, FileSize: n * 300} + base := &filer_pb.Entry{Name: "file", Attributes: attrs} + for i := 0; i < n; i++ { + base.Chunks = append(base.Chunks, &filer_pb.FileChunk{Fid: &filer_pb.FileId{VolumeId: 1, FileKey: uint64(i + 1), Cookie: 1}, Offset: int64(i) * 300, Size: 300, ModifiedTsNs: int64(100 + i)}) + } + peer := proto.Clone(base).(*filer_pb.Entry) + peer.Attributes.FileMode = 0600 // a chmod on another mount + fake.entry = peer + + live := proto.Clone(base).(*filer_pb.Entry) + live.Chunks = append(live.Chunks, &filer_pb.FileChunk{Fid: &filer_pb.FileId{VolumeId: 1, FileKey: n + 1, Cookie: 1}, Offset: n * 300, Size: 300, ModifiedTsNs: 100 + n}) + live.Attributes.FileSize = (n + 1) * 300 + observed := proto.Clone(base).(*filer_pb.Entry) + wfs.mapPbIdFromFilerToLocal(observed) + 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")) + fh.dirtyMetadata = true + + require.Equal(t, fuse.OK, wfs.doFlush(context.Background(), fh, 1000, 20, false, 0)) + + fake.mu.Lock() + stored := referenceCounts(fake.entry.Chunks) + fake.mu.Unlock() + manifests, _ := filer.SeparateManifestChunks(fake.entry.Chunks) + require.Len(t, manifests, 1, "the filer folded the merged chunks") + require.Equal(t, stored, referenceCounts(fh.GetEntry().GetEntry().Chunks), "the handle holds what the filer stored") + require.Equal(t, stored, referenceCounts(fh.baseEntry.Load().Chunks), "the base is what the filer stored") +} diff --git a/weed/mount/weedfs_file_sync.go b/weed/mount/weedfs_file_sync.go index 61903cfa03..6db24cf4eb 100644 --- a/weed/mount/weedfs_file_sync.go +++ b/weed/mount/weedfs_file_sync.go @@ -376,11 +376,13 @@ func (wfs *WFS) flushMetadataToFiler(ctx context.Context, fh *FileHandle, dir, n } // no chunk deleter here: a failed fold reports the blobs it saved + unfolded := chunks chunks, manifestErr := filer.MaybeManifestize(wfs.saveDataAsChunk(fileFullPath), nil, chunks) if manifestErr != nil { // not good, but should be ok glog.V(0).Infof("MaybeManifestize: %v", manifestErr) } + fold := newChunkFold(unfolded, chunks) if rewrote { fh.installRewrittenChunks(snapshot, append(chunks, manifestChunks...)) } else { @@ -415,13 +417,17 @@ func (wfs *WFS) flushMetadataToFiler(ctx context.Context, fh *FileHandle, dir, n // moved entry must keep room for them. buffered := fh.dirtyPages.HasPendingUploads() commitCtx, end := commit.begin(ctx) - resp, merged, err := wfs.createContentEntry(commitCtx, fh, request, baseSnapshot, buffered, publication) + resp, merged, err := wfs.createContentEntry(commitCtx, fh, request, baseSnapshot, buffered, publication, fold) end() if err != nil { + fold.revert(fh) glog.Errorf("fh flush create %s: %v", fileFullPath, err) return &contentPublicationError{cause: fmt.Errorf("fh flush create %s: %w", fileFullPath, err), request: proto.Clone(request).(*filer_pb.CreateEntryRequest)} } if merged != nil { + // Install what the filer stored, which becomes the base: it may have + // folded the merged chunks the request sent unfolded. + merged = wfs.ackedBase(resp, merged) fh.rebaseEntry(merged, requestEntry) baseSnapshot = merged } @@ -501,7 +507,7 @@ func (wfs *WFS) flushTarget(fh *FileHandle, dir, name string) (util.FullPath, st // replayed onto it and published against it, so a peer's acknowledged chunks // survive. merged is that entry in local ids; nil when the request went as is. // With writes still buffered, the merge must keep room for them. -func (wfs *WFS) createContentEntry(ctx context.Context, fh *FileHandle, request *filer_pb.CreateEntryRequest, local *filer_pb.Entry, buffered bool, publication *contentPublication) (resp *filer_pb.CreateEntryResponse, merged *filer_pb.Entry, err error) { +func (wfs *WFS) createContentEntry(ctx context.Context, fh *FileHandle, request *filer_pb.CreateEntryRequest, local *filer_pb.Entry, buffered bool, publication *contentPublication, fold *chunkFold) (resp *filer_pb.CreateEntryResponse, merged *filer_pb.Entry, err error) { base := fh.baseEntry.Load() guard := &filer_pb.WriteCondition_Clause{Kind: filer_pb.WriteCondition_IF_NOT_EXISTS} if base != nil { @@ -535,8 +541,10 @@ func (wfs *WFS) createContentEntry(ctx context.Context, fh *FileHandle, request return resp, nil, err } mark := fh.markSince(base) - next, ok := mergeContentEntry(stored, base, local, sent, mark.truncatedTo, mark.rewrote) - if !ok || buffered && !mergeKeepsBuffered(next, local) { + // The flush's own fold is no change of content: merge the chunks. + unfoldedLocal := fold.unfold(local) + next, ok := mergeContentEntry(stored, base, unfoldedLocal, fold.unfold(sent), mark.truncatedTo, mark.rewrote) + if !ok || buffered && !mergeKeepsBuffered(next, unfoldedLocal) { return resp, nil, err } glog.V(1).Infof("content flush %s/%s: entry changed since the handle's base, merging", request.Directory, request.Entry.Name) diff --git a/weed/mount/weedfs_metadata_flush.go b/weed/mount/weedfs_metadata_flush.go index a55c034bb9..624cc3cc12 100644 --- a/weed/mount/weedfs_metadata_flush.go +++ b/weed/mount/weedfs_metadata_flush.go @@ -155,10 +155,12 @@ func (wfs *WFS) flushFileMetadata(fh *FileHandle) error { // Try to create manifest chunks for large files // no chunk deleter here: a failed fold reports the blobs it saved + unfolded := compactedChunks compactedChunks, manifestErr := filer.MaybeManifestize(wfs.saveDataAsChunk(fileFullPath), nil, compactedChunks) if manifestErr != nil { glog.V(0).Infof("flushFileMetadata MaybeManifestize: %v", manifestErr) } + fold := newChunkFold(unfolded, compactedChunks) processedPrefix := append(compactedChunks, manifestChunks...) @@ -191,11 +193,15 @@ func (wfs *WFS) flushFileMetadata(fh *FileHandle) error { requestEntry.Chunks = append(requestEntry.Chunks, fh.unpublishedChunks(covered, requestEntry.Chunks)...) wfs.mapPbIdFromLocalToFiler(request.Entry) - resp, merged, err := wfs.createContentEntry(context.Background(), fh, request, baseSnapshot, true, &contentPublication{}) + resp, merged, err := wfs.createContentEntry(context.Background(), fh, request, baseSnapshot, true, &contentPublication{}, fold) if err != nil { + fold.revert(fh) return err } if merged != nil { + // Install what the filer stored, which becomes the base: it may have + // folded the merged chunks the request sent unfolded. + merged = wfs.ackedBase(resp, merged) fh.rebaseEntry(merged, requestEntry) baseSnapshot = merged }