mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-11-19 02:15:02 +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
6.7 KiB
Go
208 lines
6.7 KiB
Go
//go:build linux
|
|
|
|
package fuse_test
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
const (
|
|
mergePageSize = 8192
|
|
mergePages = 32
|
|
)
|
|
|
|
// mergeHTTP bounds every HTTP call the test makes to the cluster.
|
|
var mergeHTTP = &http.Client{Timeout: 30 * time.Second}
|
|
|
|
// TestReadOpenHandleAfterChunkMerge covers a file a flush merged while a
|
|
// descriptor kept it open. The merge rewrites the file into new chunks and the
|
|
// filer deletes the chunks it replaced, so after a vacuum only the new chunks
|
|
// exist. Reads through the open handle must find them at once, whether or not
|
|
// the handle has a write it has not flushed yet.
|
|
func TestReadOpenHandleAfterChunkMerge(t *testing.T) {
|
|
config := DefaultTestConfig()
|
|
// The client chunk cache could still hold a replaced chunk; the reads
|
|
// below have to reach the volume server.
|
|
config.CacheSizeMB = 0
|
|
config.MountOptions = append(config.MountOptions, "-cacheCapacityMB=0")
|
|
framework := NewFuseTestFramework(t, config)
|
|
defer framework.Cleanup()
|
|
require.NoError(t, framework.Setup(config))
|
|
|
|
t.Run("CleanHandle", func(t *testing.T) {
|
|
testReadOpenHandleAfterChunkMerge(t, framework, "merged_clean.dat", false)
|
|
})
|
|
t.Run("HandleWithUnflushedWrite", func(t *testing.T) {
|
|
testReadOpenHandleAfterChunkMerge(t, framework, "merged_dirty.dat", true)
|
|
})
|
|
}
|
|
|
|
func testReadOpenHandleAfterChunkMerge(t *testing.T, framework *FuseTestFramework, name string, unflushedWrite bool) {
|
|
path := filepath.Join(framework.GetMountPoint(), name)
|
|
held, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
|
|
require.NoError(t, err)
|
|
defer held.Close()
|
|
|
|
version := make([]int, mergePages)
|
|
whole := make([]byte, 0, mergePages*mergePageSize)
|
|
for p := 0; p < mergePages; p++ {
|
|
whole = append(whole, mergePagePattern(p, 0)...)
|
|
}
|
|
_, err = held.WriteAt(whole, 0)
|
|
require.NoError(t, err)
|
|
require.NoError(t, held.Sync())
|
|
|
|
// Overlapping 16-page windows: each leaves part of every older window
|
|
// visible, so the stored chunks soon add up to more than twice the file
|
|
// size and the next flush merges them.
|
|
var replaced []mergeChunk
|
|
for i, start := range []int{0, 3, 6, 9, 12, 15} {
|
|
before := mergeEntryChunks(t, framework, name)
|
|
window := make([]byte, 0, 16*mergePageSize)
|
|
for p := start; p < start+16; p++ {
|
|
window = append(window, mergePagePattern(p, i+1)...)
|
|
version[p] = i + 1
|
|
}
|
|
_, err = held.WriteAt(window, int64(start*mergePageSize))
|
|
require.NoError(t, err)
|
|
require.NoError(t, held.Sync())
|
|
if after := mergeEntryChunks(t, framework, name); len(after) == 1 && after[0].ModifiedTsNs == 0 {
|
|
replaced = before
|
|
break
|
|
}
|
|
}
|
|
require.NotEmpty(t, replaced, "the overlapping writes never made a flush merge the chunks")
|
|
|
|
// The filer deletes the replaced chunks after the merge commits; vacuum
|
|
// once they are deleted so that only the merged chunk is left on disk.
|
|
master := framework.GetMasterAddr()
|
|
require.Eventually(t, func() bool {
|
|
for _, c := range replaced {
|
|
if mergeChunkStatus(master, c, false) != http.StatusNotFound {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}, 30*time.Second, 200*time.Millisecond, "the replaced chunks were never deleted")
|
|
require.Eventually(t, func() bool {
|
|
resp, err := mergeHTTP.Get(fmt.Sprintf("http://%s/vol/vacuum?garbageThreshold=0.0001", master))
|
|
if err != nil {
|
|
return false
|
|
}
|
|
resp.Body.Close()
|
|
for _, c := range replaced {
|
|
if mergeChunkStatus(master, c, true) != http.StatusNotFound {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}, 60*time.Second, time.Second, "vacuum left the replaced chunks on disk")
|
|
|
|
if unflushedWrite {
|
|
_, err = held.WriteAt(mergePagePattern(mergePages-1, 99), int64((mergePages-1)*mergePageSize))
|
|
require.NoError(t, err)
|
|
version[mergePages-1] = 99
|
|
}
|
|
|
|
// A second descriptor shares the handle the first one keeps open. Drop its
|
|
// pages from the kernel cache so every read goes to the mount.
|
|
reader, err := os.Open(path)
|
|
require.NoError(t, err)
|
|
defer reader.Close()
|
|
require.NoError(t, unix.Fadvise(int(reader.Fd()), 0, 0, unix.FADV_DONTNEED))
|
|
started := time.Now()
|
|
buf := make([]byte, mergePageSize)
|
|
for p := 0; p < mergePages; p++ {
|
|
n, err := reader.ReadAt(buf, int64(p*mergePageSize))
|
|
require.NoError(t, err, "read page %d through the open handle after the merge", p)
|
|
require.Equal(t, mergePageSize, n, "page %d", p)
|
|
require.Equal(t, mergePagePattern(p, version[p]), buf, "page %d", p)
|
|
}
|
|
assert.Less(t, time.Since(started), 5*time.Second, "reads through the open handle waited on chunks the merge replaced")
|
|
}
|
|
|
|
func mergePagePattern(page, version int) []byte {
|
|
b := make([]byte, mergePageSize)
|
|
for i := 0; i < len(b); i += 8 {
|
|
binary.LittleEndian.PutUint32(b[i:], uint32(page))
|
|
binary.LittleEndian.PutUint32(b[i+4:], uint32(version))
|
|
}
|
|
return b
|
|
}
|
|
|
|
type mergeChunk struct {
|
|
FileId string `json:"file_id"`
|
|
ModifiedTsNs int64 `json:"modified_ts_ns"`
|
|
Fid *struct {
|
|
VolumeId uint32 `json:"volume_id"`
|
|
FileKey uint64 `json:"file_key"`
|
|
Cookie uint32 `json:"cookie"`
|
|
} `json:"fid"`
|
|
}
|
|
|
|
func (c mergeChunk) fileId() string {
|
|
if c.FileId != "" || c.Fid == nil {
|
|
return c.FileId
|
|
}
|
|
return fmt.Sprintf("%d,%x%08x", c.Fid.VolumeId, c.Fid.FileKey, c.Fid.Cookie)
|
|
}
|
|
|
|
// mergeEntryChunks returns the chunks the filer stores for the file.
|
|
func mergeEntryChunks(t *testing.T, framework *FuseTestFramework, name string) []mergeChunk {
|
|
t.Helper()
|
|
resp, err := mergeHTTP.Get(fmt.Sprintf("http://%s/%s?metadata=true", framework.GetFilerAddr(), url.PathEscape(name)))
|
|
require.NoError(t, err)
|
|
defer resp.Body.Close()
|
|
require.Equal(t, http.StatusOK, resp.StatusCode, "filer metadata of %s", name)
|
|
var entry struct {
|
|
Chunks []mergeChunk `json:"chunks"`
|
|
}
|
|
require.NoError(t, json.NewDecoder(resp.Body).Decode(&entry))
|
|
return entry.Chunks
|
|
}
|
|
|
|
// mergeChunkStatus HEADs a chunk on the volume server holding it, and returns
|
|
// 0 when it cannot ask. With readDeleted, a deleted needle still answers until
|
|
// a vacuum removes it.
|
|
func mergeChunkStatus(master string, c mergeChunk, readDeleted bool) int {
|
|
fid := c.fileId()
|
|
volumeId := strings.SplitN(fid, ",", 2)[0]
|
|
resp, err := mergeHTTP.Get(fmt.Sprintf("http://%s/dir/lookup?volumeId=%s", master, volumeId))
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
var lookup struct {
|
|
Locations []struct {
|
|
Url string `json:"url"`
|
|
} `json:"locations"`
|
|
}
|
|
err = json.NewDecoder(resp.Body).Decode(&lookup)
|
|
resp.Body.Close()
|
|
if err != nil || len(lookup.Locations) == 0 {
|
|
return 0
|
|
}
|
|
target := fmt.Sprintf("http://%s/%s", lookup.Locations[0].Url, fid)
|
|
if readDeleted {
|
|
target += "?readDeleted=true"
|
|
}
|
|
head, err := mergeHTTP.Head(target)
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
head.Body.Close()
|
|
return head.StatusCode
|
|
}
|