Files
seaweedfs/weed/mount/flush_fold.go
T
Yip Leo eb655f46ba mount: merge a flush's folded chunks as the chunks, not as a new manifest (#13043)
* mount: merge a flush's folded chunks as the chunks, not as a new manifest

A flush folds the handle's chunks into manifests before it commits, and
installs the folded list in the handle. When the commit finds that a
peer changed the entry, mergeContentEntry replays the handle's changes
since its base onto the peer's entry: a chunk not in the base counts as
the handle's own write, a base chunk missing from the handle as one it
dropped. A manifest the flush just made of base chunks therefore goes in
as a write of the handle's, with every chunk it holds:

- after a peer rewrote the whole file (a bloat merge, stamped with the
  oldest data it holds) or dropped chunks, the manifest brings the old
  chunks back, and where their stamps are newer they hide the peer's
  bytes;
- after a peer folded the same chunks, the entry holds both manifests.

Since #12462 a flush folds every 256 small chunks, so two mounts that
append to one file, as the ranks of a training job often do with its
log, reach this.

The flush now records what its fold packed (chunkFold). The merge sees
the folded chunks instead of the new manifests, so the fold is no change
at all; the merged entry goes out unfolded, and the next flush folds it
again. When the commit fails, the fold is taken back out of the handle,
whose base still has the chunks, so a later merge or flush does not take
the manifests for writes either. Their blobs are left unreferenced, as a
failed fold leaves them.

* mount: rebase a merged flush onto the entry the filer stored

Review follow-up. A merged commit sends the merged chunks unfolded, and the
filer can fold them before it stores the entry (cleanupChunks). The flush
installed the request in the handle, but took the stored entry as the
handle's base (ackedBase), so the handle held flat chunks against a base
that lists a manifest. A later merge, such as a peer's truncate reaching the
dirty handle, would take those chunks for writes of the handle's and
publish them again.

The handle is now rebased onto the stored entry it takes as its base, in
both flush paths. rebaseEntry still gets the request, so chunks attached
after it was taken are kept.

TestFlushMergeInstallsTheEntryTheFilerStored: a peer's chmod refuses the
first commit, the merged retry sends 256 flat chunks, and a filer that folds
them stores a manifest; the handle and its base must both hold the stored
entry. It fails with the request installed.
2026-11-26 22:48:58 -08:00

91 lines
2.9 KiB
Go

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)
})
}