mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 18:47:20 +01:00
* filer: keep a hard-linked file's chunks when one of its names is replaced Replacing one name of a hard-linked file deleted the file's chunks while its other names still pointed at them. Reads through those names then return EIO once the volumes are vacuumed; until then they only work through readDeleted. Two everyday ways to get there through a mount: - conda links an environment's files to its package cache with hard links. Python saves a __pycache__/*.pyc by writing a temporary file and renaming it over the old name, so the cache's copy loses its data. - A training script saves model.pt, hard-links it into an epoch directory and saves the next epoch by renaming a new file over model.pt. Each archived epoch loses its data. Three write paths deleted the chunks of the entry they replaced without asking whether that entry was one of several hard links: - a rename over a file: moveSelfEntry drops the target's link, then queues all of the target's chunks for deletion; - CreateEntry over an existing path (an S3 PUT, for one): the update drops the old entry's link, then deleteChunksIfNotNew deletes its chunks, and the CreateEntry handler deletes chunks its own list covers; - UpdateEntry with an entry that no longer carries the old one's link. A plain delete already keeps the chunks while HardLinkCounter > 1, and a folder delete asks hardLinkIsOrphaned. The three paths now go through Filer.KeepHardLinkedChunks, which asks after the write, once the replaced name's link is gone: the chunks stay unless the file's shared record went with it, that is, unless the replaced name was the last link. Writing the file through one of its names (an entry with the same hard link id) still deletes the chunks the write replaced, since every name reads the new content. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: keep hard-linked chunks from the shared record, not the replaced row A lookup that cannot read the hard link record returns the name's own row, which can lag the file. Read the record when filtering the garbage, and keep everything when it cannot be read. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: test the manifest reads behind KeepHardLinkedChunks The manifest resolution moves into withoutChunksOf, which takes the lookup function, so tests can serve manifests. Chunks behind nested manifests and the manifests themselves stay, unrelated garbage is still deleted, and an unreadable manifest returns an error, on which KeepHardLinkedChunks deletes nothing. Behavior is unchanged. 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>
108 lines
3.6 KiB
Go
108 lines
3.6 KiB
Go
package filer
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
)
|
|
|
|
// chunkServer serves each manifest's content at /<fid> and answers 500 for
|
|
// any other fid; lookup points every fid at it.
|
|
func chunkServer(t *testing.T, manifests map[string][]*filer_pb.FileChunk) (lookup func(context.Context, string) ([]string, error)) {
|
|
t.Helper()
|
|
blobs := map[string][]byte{}
|
|
for fid, chunks := range manifests {
|
|
blob, err := proto.Marshal(&filer_pb.FileChunkManifest{Chunks: chunks})
|
|
require.NoError(t, err)
|
|
blobs[fid] = blob
|
|
}
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
blob, found := blobs[strings.TrimPrefix(r.URL.Path, "/")]
|
|
if !found {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Length", strconv.Itoa(len(blob)))
|
|
w.Write(blob)
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
return func(_ context.Context, fileId string) ([]string, error) {
|
|
return []string{srv.URL + "/" + fileId}, nil
|
|
}
|
|
}
|
|
|
|
func plainChunk(fid string, offset int64) *filer_pb.FileChunk {
|
|
return &filer_pb.FileChunk{FileId: fid, Offset: offset, Size: 8}
|
|
}
|
|
|
|
func manifestChunk(fid string, offset int64, size uint64) *filer_pb.FileChunk {
|
|
return &filer_pb.FileChunk{FileId: fid, Offset: offset, Size: size, IsChunkManifest: true}
|
|
}
|
|
|
|
func fileIds(chunks []*filer_pb.FileChunk) []string {
|
|
ids := []string{}
|
|
for _, c := range chunks {
|
|
ids = append(ids, c.GetFileIdString())
|
|
}
|
|
sort.Strings(ids)
|
|
return ids
|
|
}
|
|
|
|
// A file whose chunks sit behind two levels of manifests: every chunk it
|
|
// references, manifests included, stays; the rest is still deleted.
|
|
func TestWithoutChunksOfKeepsChunksBehindNestedManifests(t *testing.T) {
|
|
lookup := chunkServer(t, map[string][]*filer_pb.FileChunk{
|
|
"7,0b": {plainChunk("8,01", 8), manifestChunk("7,0c", 16, 16)},
|
|
"7,0c": {plainChunk("8,02", 16), plainChunk("8,03", 24)},
|
|
})
|
|
held := []*filer_pb.FileChunk{plainChunk("7,0a", 0), manifestChunk("7,0b", 8, 24)}
|
|
garbage := []*filer_pb.FileChunk{
|
|
plainChunk("7,0a", 0), manifestChunk("7,0b", 8, 24), plainChunk("8,01", 8),
|
|
manifestChunk("7,0c", 16, 16), plainChunk("8,02", 16), plainChunk("8,03", 24),
|
|
plainChunk("9,0f", 0), plainChunk("9,10", 8),
|
|
}
|
|
|
|
kept, err := withoutChunksOf(context.Background(), lookup, nil, held, garbage)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, []string{"9,0f", "9,10"}, fileIds(kept))
|
|
}
|
|
|
|
// When a manifest cannot be read, which chunks the file references is
|
|
// unknown: the error makes KeepHardLinkedChunks delete nothing, unrelated
|
|
// garbage included.
|
|
func TestWithoutChunksOfUnreadableManifest(t *testing.T) {
|
|
lookup := chunkServer(t, map[string][]*filer_pb.FileChunk{
|
|
"7,0b": {plainChunk("8,01", 8), manifestChunk("7,0c", 16, 16)},
|
|
// 7,0c is not served
|
|
})
|
|
held := []*filer_pb.FileChunk{plainChunk("7,0a", 0), manifestChunk("7,0b", 8, 24)}
|
|
garbage := []*filer_pb.FileChunk{plainChunk("8,01", 8), plainChunk("9,0f", 0)}
|
|
|
|
kept, err := withoutChunksOf(context.Background(), lookup, nil, held, garbage)
|
|
assert.Error(t, err)
|
|
assert.Empty(t, kept)
|
|
}
|
|
|
|
func TestWithoutChunksOfPlainChunks(t *testing.T) {
|
|
held := []*filer_pb.FileChunk{plainChunk("7,0a", 0)}
|
|
garbage := []*filer_pb.FileChunk{plainChunk("7,0a", 0), plainChunk("9,0f", 0)}
|
|
noLookup := func(context.Context, string) ([]string, error) {
|
|
t.Fatal("no manifest to read")
|
|
return nil, nil
|
|
}
|
|
|
|
kept, err := withoutChunksOf(context.Background(), noLookup, nil, held, garbage)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, []string{"9,0f"}, fileIds(kept))
|
|
}
|