mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 02:57:37 +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>
76 lines
3.1 KiB
Go
76 lines
3.1 KiB
Go
package filer
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"math"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/wdclient"
|
|
)
|
|
|
|
// hardLinkStillHeld reports whether replaced, the entry a write has just
|
|
// replaced at its path, was one of several hard links to a file another name
|
|
// still reads: replaced carried a hard link, its replacement is not that same
|
|
// link, and the file's shared record outlived the drop of replaced's name.
|
|
//
|
|
// Ask it after the write. The store drops the replaced name's link as part of
|
|
// the write, so the shared record is gone exactly when replaced was the last
|
|
// link, whatever link count replaced carried when it was read.
|
|
func (f *Filer) hardLinkStillHeld(ctx context.Context, replaced, replacement *Entry) bool {
|
|
return replacesHardLink(replaced, replacement) && !f.hardLinkIsOrphaned(ctx, replaced.HardLinkId)
|
|
}
|
|
|
|
// replacesHardLink reports whether replacement unlinks a hard-linked
|
|
// replaced; the same file written through one of its names does not: the
|
|
// store rewrote the shared record, and no name reads what the write left out.
|
|
func replacesHardLink(replaced, replacement *Entry) bool {
|
|
if replaced == nil || len(replaced.HardLinkId) == 0 {
|
|
return false
|
|
}
|
|
return replacement == nil || !bytes.Equal(replaced.HardLinkId, replacement.HardLinkId)
|
|
}
|
|
|
|
// KeepHardLinkedChunks takes out of garbage, the chunks a write is about to
|
|
// delete, every chunk of replaced's file while another hard link still reads
|
|
// it. The chunks come from the shared record, since replaced may be a stale
|
|
// row when its lookup could not read the record.
|
|
//
|
|
// When the record or the manifests cannot be read nothing is deleted: a
|
|
// leaked chunk can be collected later, a deleted one that a name still reads
|
|
// is lost.
|
|
func (f *Filer) KeepHardLinkedChunks(ctx context.Context, replaced, replacement *Entry, garbage []*filer_pb.FileChunk) []*filer_pb.FileChunk {
|
|
if len(garbage) == 0 || !replacesHardLink(replaced, replacement) {
|
|
return garbage
|
|
}
|
|
value, err := f.Store.KvGet(ctx, replaced.HardLinkId)
|
|
if errors.Is(err, ErrKvNotFound) {
|
|
return garbage
|
|
}
|
|
shared := &Entry{}
|
|
if err == nil {
|
|
err = shared.DecodeAttributesAndChunks(value)
|
|
}
|
|
if err == nil {
|
|
var kept []*filer_pb.FileChunk
|
|
kept, err = withoutChunksOf(ctx, f.MasterClient.GetLookupFileIdFunction(), f.MasterClient, append(shared.GetChunks(), replaced.GetChunks()...), garbage)
|
|
if err == nil {
|
|
return kept
|
|
}
|
|
}
|
|
glog.ErrorfCtx(ctx, "keep %d chunk(s) of %s, which another hard link may still read: %v", len(garbage), replaced.FullPath, err)
|
|
return nil
|
|
}
|
|
|
|
// withoutChunksOf returns garbage without chunks, without the manifests among
|
|
// them, and without every chunk those manifests list, nested ones included.
|
|
func withoutChunksOf(ctx context.Context, lookupFileIdFn wdclient.LookupFileIdFunctionType, invalidator CacheInvalidator, chunks, garbage []*filer_pb.FileChunk) ([]*filer_pb.FileChunk, error) {
|
|
data, manifests, err := ResolveChunkManifest(ctx, lookupFileIdFn, chunks, 0, math.MaxInt64, invalidator)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return DoMinusChunks(garbage, append(data, manifests...)), nil
|
|
}
|