mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-11-12 06:48:45 +01:00
* filer/etcd: scan entries from one revision volume.fsck walked etcd one directory at a time, so a file moved from a directory not yet listed into one already listed was seen under neither name and its chunks were reported as orphans. The etcd store now implements filer.EntryScanner: every page is read at the revision of the first read, and a compacted revision fails the scan instead of falling back to the walk. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer/etcd: read hard links at the scan revision Hard-link records resolved during a scan were read at the current revision, so a link updated meanwhile mixed two instants. Reads made with the scan's context now use its revision. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * shell: list etcd among the stores volume.fsck reads from a snapshot Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer/etcd: skip hard-link records in a scan Hard-link records live beside the entries under their random ids, so a root scan could read one as an entry with a path made of random bytes, which the traversal stream cannot send. Skip hard-link ids and keys whose directory is not valid UTF-8. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer/etcd: recognize a hard-link record by the id it stores A hard-link id can be valid UTF-8, so the key shape alone let some records through a root scan as phantom files. The record's value is the shared entry, whose HardLinkId is the record's own key; no entry key can equal its own hard-link id. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer/etcd: scan entries under an invalid-UTF-8 directory The filer sanitizes names but stores directories as given, so a path written over HTTP can leave entries the listing never reaches. Skipping them let volume.fsck count their chunks as orphans. Send them under the sanitized directory instead, the substitution the filer uses for names. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer/etcd: fail a scan whose sanitized path names another entry An entry under an invalid-UTF-8 directory is reported under the sanitized directory. When that path is also another stored entry at the scan revision, or another sanitized entry in the same scan, acting on the reported path (volume.fsck deleting it) would reach the other file, so the scan fails and names the stored path instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
132 lines
5.0 KiB
Go
132 lines
5.0 KiB
Go
package etcd
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"go.etcd.io/etcd/api/v3/v3rpc/rpctypes"
|
|
clientv3 "go.etcd.io/etcd/client/v3"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
weed_util "github.com/seaweedfs/seaweedfs/weed/util"
|
|
)
|
|
|
|
var _ filer.EntryScanner = (*EtcdStore)(nil)
|
|
|
|
var scanPageSize int64 = 1000
|
|
|
|
type scanRevisionKey struct{}
|
|
|
|
// atScanRevision pins a read made with a ScanEntries context to the scan's
|
|
// revision, so hard-link records resolve as of the same instant.
|
|
func atScanRevision(ctx context.Context, opts ...clientv3.OpOption) []clientv3.OpOption {
|
|
if rev, ok := ctx.Value(scanRevisionKey{}).(int64); ok {
|
|
opts = append(opts, clientv3.WithRev(rev))
|
|
}
|
|
return opts
|
|
}
|
|
|
|
// ScanEntries implements filer.EntryScanner. Every page is read at the
|
|
// revision of the first read, so a rename committed meanwhile shows up under
|
|
// exactly one of its names. If etcd compacts that revision before the scan
|
|
// ends, the scan fails rather than mixing revisions.
|
|
func (store *EtcdStore) ScanEntries(ctx context.Context, dirPath weed_util.FullPath, fn func(ctx context.Context, entry *filer.Entry) error) error {
|
|
return scanEntries(ctx, store.client, store.etcdKeyPrefix, dirPath, fn)
|
|
}
|
|
|
|
func scanEntries(ctx context.Context, kv clientv3.KV, keyPrefix string, dirPath weed_util.FullPath, fn func(ctx context.Context, entry *filer.Entry) error) error {
|
|
resp, err := kv.Get(ctx, keyPrefix+string(genKey(dirPath.DirAndName())))
|
|
if err != nil {
|
|
return fmt.Errorf("scan %s: %v", dirPath, err)
|
|
}
|
|
rev := resp.Header.Revision
|
|
scanCtx := context.WithValue(ctx, scanRevisionKey{}, rev)
|
|
if dirPath != "/" && len(resp.Kvs) > 0 {
|
|
root := &filer.Entry{FullPath: dirPath}
|
|
if err := root.DecodeAttributesAndChunks(weed_util.MaybeDecompressData(resp.Kvs[0].Value)); err != nil {
|
|
return fmt.Errorf("scan decode %s: %v", dirPath, err)
|
|
}
|
|
if err := fn(scanCtx, root); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// Children of dirPath, then everything deeper; "/" covers both.
|
|
prefixes := []string{string(dirPath)}
|
|
if dirPath != "/" {
|
|
prefixes = []string{string(genDirectoryKeyPrefix(dirPath, "")), string(dirPath) + "/"}
|
|
}
|
|
sanitized := map[weed_util.FullPath]struct{}{}
|
|
for _, prefix := range prefixes {
|
|
if err := scanPrefix(scanCtx, kv, keyPrefix, prefix, dirPath, rev, sanitized, fn); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func scanPrefix(ctx context.Context, kv clientv3.KV, keyPrefix, prefix string, dirPath weed_util.FullPath, rev int64, sanitized map[weed_util.FullPath]struct{}, fn func(ctx context.Context, entry *filer.Entry) error) error {
|
|
end := clientv3.GetPrefixRangeEnd(keyPrefix + prefix)
|
|
for start := keyPrefix + prefix; ; {
|
|
resp, err := kv.Get(ctx, start, clientv3.WithRange(end), clientv3.WithLimit(scanPageSize), clientv3.WithRev(rev))
|
|
if errors.Is(err, rpctypes.ErrCompacted) {
|
|
return fmt.Errorf("scan %s: etcd compacted revision %d before the scan finished; rerun it, or keep more revisions (etcd --auto-compaction-retention): %w", dirPath, rev, err)
|
|
}
|
|
if err != nil {
|
|
return fmt.Errorf("scan %s: %w", dirPath, err)
|
|
}
|
|
for _, item := range resp.Kvs {
|
|
key := item.Key[len(keyPrefix):]
|
|
name := getNameFromKey(key)
|
|
if name == "" || len(name) == len(key) {
|
|
continue
|
|
}
|
|
// Names are sanitized when stored, directories are not; the stream
|
|
// needs valid UTF-8, and fsck must still count the chunks.
|
|
rawDir := string(key[:len(key)-len(name)-1])
|
|
dir := weed_util.SanitizeUTF8Name(rawDir)
|
|
entry := &filer.Entry{FullPath: weed_util.NewFullPath(dir, name)}
|
|
if err := entry.DecodeAttributesAndChunks(weed_util.MaybeDecompressData(item.Value)); err != nil {
|
|
return fmt.Errorf("scan decode %s: %v", entry.FullPath, err)
|
|
}
|
|
// A hard-link record is stored under its own id (filer.NewHardLinkId).
|
|
if bytes.Equal(entry.HardLinkId, key) {
|
|
continue
|
|
}
|
|
if dir != rawDir {
|
|
if err := claimSanitizedPath(ctx, kv, keyPrefix, rev, entry.FullPath, weed_util.NewFullPath(rawDir, name), sanitized); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if err := fn(ctx, entry); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if !resp.More || len(resp.Kvs) == 0 {
|
|
return nil
|
|
}
|
|
start = string(resp.Kvs[len(resp.Kvs)-1].Key) + "\x00"
|
|
}
|
|
}
|
|
|
|
// claimSanitizedPath fails the scan when the path an entry is reported under
|
|
// names a different entry, so acting on it (fsck deleting it) cannot reach
|
|
// another file.
|
|
func claimSanitizedPath(ctx context.Context, kv clientv3.KV, keyPrefix string, rev int64, p, rawPath weed_util.FullPath, sanitized map[weed_util.FullPath]struct{}) error {
|
|
_, taken := sanitized[p]
|
|
if !taken {
|
|
resp, err := kv.Get(ctx, keyPrefix+string(genKey(p.DirAndName())), clientv3.WithRev(rev), clientv3.WithKeysOnly())
|
|
if err != nil {
|
|
return fmt.Errorf("scan lookup %s: %w", p, err)
|
|
}
|
|
taken = len(resp.Kvs) > 0
|
|
}
|
|
if taken {
|
|
return fmt.Errorf("scan: entry %q has a directory that is not valid UTF-8 and would be reported as %s, which names another entry; rename or delete one of them", string(rawPath), p)
|
|
}
|
|
sanitized[p] = struct{}{}
|
|
return nil
|
|
}
|