mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-11-19 10:54:08 +01:00
* mount: move an open file's read view to the chunks a merge wrote When a flush finds that a file's chunks take more than twice its size, maybeMergeChunks reads the file back and writes it again as new chunks. The commit that publishes them makes the filer delete the chunks they replaced. The flush put the new chunks into the handle's entry but left its read view on the old ones, so every descriptor that kept the file open went on reading the replaced chunks. Those read back until their volume is vacuumed. After that a read first retries the fetch of the missing needle for about 13 s; then refreshEntryForRead installs the filer's entry and the read is retried. The refresh skips a handle with writes it has not flushed, and such a read returns EIO instead. Files that are kept open and rewritten in place, such as PostgreSQL data files and SQLite databases, are the ones whose chunks bloat, so they are the ones that hit this. installRewrittenChunks puts the merge's chunks into the entry and the read view together, under the entry lock. Chunks attached while the merge ran were not part of it and stay. Flushes without a merge are unchanged. TestMergeMovesReadViewOffReplacedChunks covers the handle. TestReadOpenHandleAfterChunkMerge runs it on a real mount, with and without an unflushed write; for it, the fuse_integration framework starts mini on a free master port and exposes the address. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: drain uploads before a merge replaces the chunks Uploads attach their chunks without the handle lock, first to the entry and then to the read view, and writes that landed after doFlush drained the pages may still be uploading when the flush takes its lock. An upload that attached during the merge could keep a chunk older than data the merge already holds, or put a chunk the merge replaced back into the read view. Either outranks the merge's zero-timestamp chunk. When the chunks are bloated enough to merge, the flush now drains the pages again while its lock holds writes off, and takes the chunks again. FlushAll returns only after every upload has attached to both the entry and the read view, so nothing attaches during the merge. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: leave a pre-merge upload failure to the next flush The drain before a merge returned an upload failure through the metadata flush. doFlush then retried it as a filer error and answered EIO, where the page flush answers a full disk with ENOSPC. The drain is there only to wait for the uploads: a failure stays with the pages, and the next flush reports it as a write error, as it would without a merge. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: keep chunks attached while a flush installs its compacted list A write that lands after doFlush drained the pages can still be uploading when flushMetadataToFiler takes its chunk snapshot. Its upload attaches the chunk without the handle lock, and the plain flush then replaced the entry's chunks with the list compacted from the snapshot, dropping it from the handle and from every later commit. Keep chunks attached after the snapshot, as the merge path already does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: fail a merge's flush on a failed drain A failed upload found by the drain before a merge was only logged, and the flush went on to publish a size covering bytes that no chunk stores. It now fails the flush as a pageUploadError before anything is published; doFlush maps it with writeErrorToFuseStatus, as it does its own FlushData failure, and does not retry it as a filer error. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: clone a flush's request entry under the entry lock The request snapshot cloned the live entry after GetEntry had released its read lock. Uploads attach chunks without the handle lock, so an AppendChunks that landed during the clone raced it on the Chunks field. Clone inside UpdateEntry, as content flush recovery does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: leave a merge to a later flush while uploads are pending Before a merge the flush drained the pages while it held the handle and publication locks. A writeback upload retries a transient storage error until it succeeds, so that drain could hold both locks for as long as storage stayed down, stalling reads, writes and stat on the handle. The flush now checks for pending uploads and, if any, publishes the compacted chunks and leaves the merge to a later flush. With none pending the lock still holds writes off, so nothing attaches during the merge, and a failed upload still fails the flush as a write error. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: keep room for pending uploads when a flush merges onto a moved entry A flush that leaves the chunk merge to a later flush publishes while uploads are still pending, and writes that landed after the drain can be uploading on any flush. Merging a peer's inline content or a shrink into such a publication lets the upload attach its chunk next to the inline bytes, where reads never reach it. Tell createContentEntry when uploads are pending so the merge keeps room for them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: test chunk merges through the filer's chunk proxy, also under load The tests serve the filer's chunk proxy, and like the filer the fake deletes the chunks each commit replaces, so a read view still naming them fails: - TestFlushMergeThroughChunkProxy/Quiet: the flush merges, and reads through the open handle return the file from the merged chunk alone. - TestFlushMergeThroughChunkProxy/UploadInFlight: an older page is still uploading while a newer page of the same bytes has attached. The flush leaves the merge, reads return the newer page, and the next flush merges. - TestFlushMergeRacesWithWritesAndReads: four writers, three flushers and a reader on one handle, with uploads delayed at random. The writers pause now and then, so 14 to 18 flushes a run merge and the others leave the merge to a later one. Every read must succeed within 2 s, and at the end the handle and the filer must both hold exactly what was written. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: let a held upload go when UploadInFlight fails The subtest's proxy holds the older page's upload until the test releases it, and only the passing path did. A failure in between left the handler blocked, so the proxy's cleanup waited on it and the failure surfaced as a suite timeout. Release it from a cleanup as well, once, and bound the waits for the upload and the drains. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
208 lines
7.5 KiB
Go
208 lines
7.5 KiB
Go
//go:build !freebsd
|
|
|
|
package mount
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"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/protobuf/proto"
|
|
)
|
|
|
|
// attachOnAssignPeer runs onAssign when the flush asks for a volume, which a
|
|
// handle with many small chunks does while it folds them into a manifest.
|
|
type attachOnAssignPeer struct {
|
|
*g6ConditionalPeer
|
|
once sync.Once
|
|
onAssign func()
|
|
}
|
|
|
|
func (p *attachOnAssignPeer) AssignVolume(context.Context, *filer_pb.AssignVolumeRequest) (*filer_pb.AssignVolumeResponse, error) {
|
|
p.once.Do(p.onAssign)
|
|
return &filer_pb.AssignVolumeResponse{Error: "no volume in this test"}, nil
|
|
}
|
|
|
|
// An upload attaches its chunk without the handle lock, so it can land while a
|
|
// flush is between taking its chunk snapshot and installing the compacted
|
|
// list. The chunk must stay on the handle and reach the filer.
|
|
func TestFlushKeepsChunkAttachedDuringCommit(t *testing.T) {
|
|
base := attributeAppendEntry(256)
|
|
base.CommitTsNs = 100
|
|
peer := &g6ConditionalPeer{entry: proto.Clone(base).(*filer_pb.Entry)}
|
|
w, handle := g6AppendHandle(t, peer, base, 1000)
|
|
fh := w.GetHandle(FileHandleId(handle))
|
|
|
|
late := &filer_pb.FileChunk{Fid: &filer_pb.FileId{VolumeId: 9, FileKey: 1, Cookie: 1}, Offset: 0, Size: 256, ModifiedTsNs: 500}
|
|
startFakeFiler(t, w, &attachOnAssignPeer{g6ConditionalPeer: peer, onAssign: func() {
|
|
fh.AddChunks([]*filer_pb.FileChunk{late})
|
|
fh.entryChunkGroup.AddChunk(late)
|
|
}})
|
|
|
|
if n, code := w.Write(nil, &fuse.WriteIn{InHeader: fuse.InHeader{NodeId: 42}, Fh: handle, Offset: 256 * 256, Size: 256}, bytes.Repeat([]byte{'W'}, 256)); n != 256 || code != fuse.OK {
|
|
t.Fatalf("write: %d %v", n, code)
|
|
}
|
|
if code := w.Flush(nil, &fuse.FlushIn{InHeader: fuse.InHeader{NodeId: 42}, Fh: handle}); code != fuse.OK {
|
|
t.Fatalf("flush: %v", code)
|
|
}
|
|
|
|
has := func(chunks []*filer_pb.FileChunk) bool {
|
|
for _, c := range chunks {
|
|
if contentFileIdKey(c) == contentFileIdKey(late) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
if !has(fh.GetEntry().GetChunks()) {
|
|
t.Fatal("the handle lost the chunk an upload attached during the flush")
|
|
}
|
|
peer.mu.Lock()
|
|
defer peer.mu.Unlock()
|
|
if !has(peer.entry.Chunks) {
|
|
t.Fatal("the flush did not publish the chunk an upload attached during it")
|
|
}
|
|
}
|
|
|
|
// Uploads keep attaching chunks while a flush clones its request entry, so the
|
|
// clone must hold the entry lock. Run with -race.
|
|
func TestFlushClonesRequestWhileUploadsAttach(t *testing.T) {
|
|
base := attributeAppendEntry(1)
|
|
base.CommitTsNs = 100
|
|
peer := &g6ConditionalPeer{entry: proto.Clone(base).(*filer_pb.Entry)}
|
|
w, handle := g6AppendHandle(t, peer, base, 1000)
|
|
fh := w.GetHandle(FileHandleId(handle))
|
|
|
|
attached := make(chan struct{})
|
|
go func() {
|
|
defer close(attached)
|
|
for key := uint64(1); key <= 2000; key++ {
|
|
fh.AddChunks([]*filer_pb.FileChunk{{Fid: &filer_pb.FileId{VolumeId: 9, FileKey: key, Cookie: 1}, Offset: 0, Size: 256, ModifiedTsNs: int64(key)}})
|
|
time.Sleep(20 * time.Microsecond)
|
|
}
|
|
}()
|
|
for {
|
|
if err := w.flushMetadataToFiler(context.Background(), fh, "/dir", "file", 0, 0, nil); err != nil {
|
|
t.Fatalf("flush: %v", err)
|
|
}
|
|
select {
|
|
case <-attached:
|
|
return
|
|
default:
|
|
}
|
|
}
|
|
}
|
|
|
|
// bloatedHandleWithPendingUpload opens a handle over chunks bloated enough to
|
|
// merge and starts uploading a write past them. The upload blocks until
|
|
// release is closed; drained reports its result.
|
|
func bloatedHandleWithPendingUpload(t *testing.T) (w *WFS, fh *FileHandle, peer *g6ConditionalPeer, base *filer_pb.Entry, release chan struct{}, drained chan error) {
|
|
t.Helper()
|
|
base = attributeAppendEntry(1)
|
|
base.Chunks = nil
|
|
for i := 0; i < 8; i++ {
|
|
base.Chunks = append(base.Chunks, &filer_pb.FileChunk{Fid: &filer_pb.FileId{VolumeId: 1, FileKey: uint64(i + 1), Cookie: 1}, Offset: int64(i * 64), Size: 256, ModifiedTsNs: int64(i + 1)})
|
|
}
|
|
base.Attributes.FileSize = 7*64 + 256
|
|
base.CommitTsNs = 100
|
|
if _, _, bloated := shouldMergeChunks(base.Chunks, nil); !bloated {
|
|
t.Fatal("the test entry is not bloated enough to merge")
|
|
}
|
|
peer = &g6ConditionalPeer{entry: proto.Clone(base).(*filer_pb.Entry)}
|
|
w, handle := g6AppendHandle(t, peer, base, 1000)
|
|
fh = w.GetHandle(FileHandleId(handle))
|
|
|
|
started, release := make(chan struct{}), make(chan struct{})
|
|
var once sync.Once
|
|
fh.dirtyPages.randomWriter.(*ChunkedDirtyPages).saveDataAsChunk = func(util.FullPath) filer.SaveDataAsChunkFunctionType {
|
|
return func(r io.Reader, _ string, offset, ts int64, _ uint64) (*filer_pb.FileChunk, error) {
|
|
once.Do(func() { close(started) })
|
|
<-release
|
|
body, err := io.ReadAll(r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &filer_pb.FileChunk{Fid: &filer_pb.FileId{VolumeId: 2, FileKey: 1, Cookie: 1}, Offset: offset, Size: uint64(len(body)), ModifiedTsNs: ts}, nil
|
|
}
|
|
}
|
|
if n, code := w.Write(nil, &fuse.WriteIn{InHeader: fuse.InHeader{NodeId: 42}, Fh: handle, Offset: base.Attributes.FileSize, Size: 256}, bytes.Repeat([]byte{'W'}, 256)); n != 256 || code != fuse.OK {
|
|
t.Fatalf("write: %d %v", n, code)
|
|
}
|
|
drained = make(chan error, 1)
|
|
go func() { drained <- fh.dirtyPages.FlushData() }()
|
|
<-started
|
|
return w, fh, peer, base, release, drained
|
|
}
|
|
|
|
// A flush that finds its chunks bloated while an upload is still in flight
|
|
// leaves the merge to a later flush instead of waiting for the upload under
|
|
// the handle and publication locks: a writeback upload retries until storage
|
|
// recovers, and every operation on the handle would wait with it.
|
|
func TestFlushLeavesMergeWhileUploadPending(t *testing.T) {
|
|
w, fh, peer, base, release, drained := bloatedHandleWithPendingUpload(t)
|
|
|
|
flushed := make(chan error, 1)
|
|
go func() { flushed <- w.flushMetadataToFiler(context.Background(), fh, "/dir", "file", 0, 0, nil) }()
|
|
select {
|
|
case err := <-flushed:
|
|
if err != nil {
|
|
t.Fatalf("flush: %v", err)
|
|
}
|
|
case <-time.After(5 * time.Second):
|
|
close(release)
|
|
<-flushed
|
|
t.Fatal("the flush waited for a pending upload under the handle lock")
|
|
}
|
|
close(release)
|
|
if err := <-drained; err != nil {
|
|
t.Fatalf("drain: %v", err)
|
|
}
|
|
|
|
peer.mu.Lock()
|
|
defer peer.mu.Unlock()
|
|
if len(peer.entry.Chunks) != len(base.Chunks) {
|
|
t.Fatalf("published %d chunks, want the %d unmerged ones", len(peer.entry.Chunks), len(base.Chunks))
|
|
}
|
|
}
|
|
|
|
// A flush that publishes while an upload is pending must not adopt a peer's
|
|
// inline content: the upload attaches its chunk next to it, and reads stop at
|
|
// the inline bytes, so the completed write would never be read.
|
|
func TestFlushWithUploadPendingRefusesPeerInlineContent(t *testing.T) {
|
|
w, fh, peer, _, release, drained := bloatedHandleWithPendingUpload(t)
|
|
defer func() {
|
|
close(release)
|
|
if err := <-drained; err != nil {
|
|
t.Fatalf("drain: %v", err)
|
|
}
|
|
}()
|
|
|
|
inline := bytes.Repeat([]byte{'P'}, int(filer.FileSize(fh.GetEntry().GetEntry())))
|
|
peer.mu.Lock()
|
|
peer.entry.Chunks = nil
|
|
peer.entry.Content = inline
|
|
peer.entry.Attributes.FileSize = uint64(len(inline))
|
|
peer.entry.CommitTsNs = 200
|
|
peer.mu.Unlock()
|
|
|
|
if err := w.flushMetadataToFiler(context.Background(), fh, "/dir", "file", 0, 0, nil); !errors.Is(err, filer_pb.ErrPreconditionFailed) {
|
|
t.Fatalf("flush over a peer's inline content with an upload pending: %v, want a refused merge", err)
|
|
}
|
|
if content := fh.GetEntry().GetEntry().GetContent(); len(content) != 0 {
|
|
t.Fatalf("the handle adopted %d bytes of the peer's inline content", len(content))
|
|
}
|
|
peer.mu.Lock()
|
|
defer peer.mu.Unlock()
|
|
if !bytes.Equal(peer.entry.Content, inline) || len(peer.entry.Chunks) != 0 {
|
|
t.Fatal("the flush published over the peer's inline content")
|
|
}
|
|
}
|