mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 11:07:31 +01:00
* shell: fs.du walks with a shared treewalk.Walker The walker fs.du used moves to a new package, filer/treewalk, so that the dir_usage job proposed in #13257 walks the same way. fs.du adds up what its visitor is told; what it prints and how it fails stay the same. A page now also carries the snapshot the filer listed it at, and when it was listed. treewalktest serves a tree from memory and logs an event for each change, for the tests of walks and of what follows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer/treewalk: Seed adds up a walk and the events logged while it runs Seed walks a directory tree while it follows the metadata log below it, and reconciles the two, so that the result is the tree as of one log position: - an event at or before the snapshot of the page that lists its name shows in that page, and so does an event in a directory not listed yet; - an event after the page's snapshot replaces what the page counted for the name, since the page may or may not show it; - once a page is older than a margin, a minute by default since a rename logs its events after the move, an event changes the entry from its old to its new entry. A directory missing from its parent's page counts from its events if it was created after that page's snapshot, and not at all otherwise. This is how the mount's metadata cache builds a directory from a listing and the events around it, with a replace per name in place of the cache's overwrite, which a running total cannot do. Follow reads the events of a range of the log. The lookup of where the log starts moves here from filer_ttl as LogStartTsNs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * worker: dir_usage keeps the sizes of buckets and their large directories A plugin worker job, off by default, that keeps what fs.du adds up of each bucket and each top-level directory outside the buckets folder, and of each directory in it holding at least 1% of its bytes, down to 10 levels. Directories at one depth do not overlap, so a bucket has at most 1,000 such records, whatever the shape of its tree. They are one JSON file per bucket under /etc/seaweedfs/dir_usage/<store signature>/. The buckets without a record are seeded in one treewalk.Seed walk of the buckets folder, and the top-level directories in one of /. After that, each run replays the events since the last run, up to a minute before now, applies each to the directories with a record above the entry, writes the files that changed, and saves a cursor that meta_log_purge keeps the log for. A record the log no longer reaches is seeded again, and so is every record when no log is kept and the cursor is older than the purge honors. A tracked directory that moves keeps its record, which the events of the entries it took along fill again. One deleted with totals left had entries that went without events, as when a store drops a whole bucket, and the rest comes off the directories above it. A bucket created since the last run is seeded by the next. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * shell: fs.du -report prints what the dir_usage job keeps fs.du -report [-depth=N] path prints the totals the dir_usage job keeps of path and of the directories below it with a record, down to N levels, the way fs.du prints a walk, and how old they are: as old as the job's cursor for a record the last replay did not change. Nothing is walked. For / and the buckets folder, the buckets and top-level directories add up. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * treewalk, dir_usage: wait for the filer's own position, stop waits on cancel A subscription with an end stops once it has read everything the filer has logged, without waiting for the end to come. So the seed's catch-up now waits until the filer's listing snapshot reaches the seed's end, and a replay ends at the snapshot the filer gives when it starts, both on the filer's clock rather than this process's. - A walk waiting for its pages to free names stops when its context ends. - A seed reports the listing that failed, not the cancellation it caused. - A walk canceled while the last page's subdirectories start fails. - LogStartTsNs passes over a day a purge that stopped midway left empty. - A bucket dropped while the buckets folder is seeded gets no record; the next run seeds it if it is made again. - Detection keys a store's job by its signature, so no two runs share a store whichever filer each starts from. - The buckets folder is taken without a trailing slash. - fs.du -report names the buckets and top-level directories it leaves out for want of a record, and no longer says why a directory has no total of its own. - treewalktest.Filer replays a quiet drop with what was below it, and can refuse a listing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * treewalk: list a page again after a transient failure A seed walks every bucket without a record in one walk, which on a large store runs for hours. One page that failed because the filer restarted or a connection dropped failed the whole walk, and the next run started it over. A page that fails with a transient error is now asked for again, with backoff, for up to a minute. Other failures still end the listing at once. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * dir_usage: keep the other seeded records when one fails to save A save that failed after a seed returned at once, so the records of the batch not saved yet were lost, and the top-level directories were not seeded at all when it was the buckets' batch that failed. The next run walked them all again, which on a large store takes hours. A failed save is now reported and the run goes on; the next run seeds only that root. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * treewalk: measure how far the filer's clock runs ahead in a seed A page's end is on this host's clock and the events it is compared to are on the filer's, so a filer clock ahead by more than the margin expired pages before the events they show arrived, and set the seed's end before positions the filer had already logged. The seed now learns a lower bound of the filer's lead from the page snapshots and the event stamps, and adds it to page expiry and to the end. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mini: expect the dir_usage handler among the default job types 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>
175 lines
6.0 KiB
Go
175 lines
6.0 KiB
Go
// Package treewalk walks a filer directory tree a page at a time, and adds up
|
|
// such a walk with the metadata events that arrive while it runs, so that the
|
|
// walk and the events add up to the tree as of one log position.
|
|
package treewalk
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
)
|
|
|
|
// Page is one page of a directory listing.
|
|
type Page struct {
|
|
Dir util.FullPath
|
|
Depth int // levels below the directory the walk started at
|
|
// After is the name the page was listed after, "" for the first page. The
|
|
// page covers the names after it up to its last entry's name, or up to the
|
|
// end of the directory when Last is set.
|
|
After string
|
|
Entries []*filer_pb.Entry
|
|
Last bool
|
|
// Snapshot is the log position the filer said the page is complete
|
|
// through: every change logged at or before it shows in the page. It is 0
|
|
// when the filer did not say.
|
|
Snapshot int64
|
|
// Start is when the page was asked for and End when its stream ended, on
|
|
// this process's clock.
|
|
Start, End time.Time
|
|
}
|
|
|
|
// Visitor is told about a walk as it goes. Its methods are called from
|
|
// several goroutines at once.
|
|
type Visitor interface {
|
|
// Started is called before the first page of dir is listed.
|
|
Started(dir util.FullPath, depth int)
|
|
// Listing is called before each page is asked for. It can hold the walk
|
|
// back by not returning yet, and stop the listing of dir with an error.
|
|
// It returns once ctx ends.
|
|
Listing(ctx context.Context, dir util.FullPath) error
|
|
// Listed is called with each page once its stream is done. It returns the
|
|
// subdirectories among the page's entries to walk.
|
|
Listed(page *Page) (subDirs []string)
|
|
// Done is called once dir, and every directory below it the walk went
|
|
// into, is listed. err is what ended the listing of dir early, if anything.
|
|
Done(dir util.FullPath, depth int, err error)
|
|
}
|
|
|
|
// Walker lists a directory tree depth first, up to Concurrency directories at
|
|
// a time. A subdirectory gets a goroutine of its own while the concurrency
|
|
// allows one and is walked in place otherwise. Each directory is listed a page
|
|
// at a time, and the walks of a page's subdirectories are under way before the
|
|
// next page is listed, so memory grows with the depth of the tree and the
|
|
// concurrency, not with the number or the width of its directories.
|
|
type Walker struct {
|
|
Client filer_pb.FilerClient
|
|
Concurrency int
|
|
PageSize uint32
|
|
}
|
|
|
|
type walk struct {
|
|
*Walker
|
|
visitor Visitor
|
|
// one slot per goroutine walking beside the caller's
|
|
slots chan struct{}
|
|
}
|
|
|
|
// Walk walks dir and everything below it, or with a prefix only the entries
|
|
// of dir whose names start with it and what is below them. A directory below
|
|
// dir that cannot be listed is reported to the visitor's Done and the walk
|
|
// goes on. Walk returns what ended the listing of dir itself, after stopping
|
|
// the walks under way.
|
|
func (w *Walker) Walk(ctx context.Context, dir util.FullPath, prefix string, visitor Visitor) error {
|
|
if w.PageSize == 0 {
|
|
return fmt.Errorf("walk %s: page size 0", dir)
|
|
}
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
defer cancel()
|
|
walking := &walk{Walker: w, visitor: visitor, slots: make(chan struct{}, max(w.Concurrency, 1)-1)}
|
|
|
|
visitor.Started(dir, 0)
|
|
var children sync.WaitGroup
|
|
err := walking.list(ctx, dir, 0, prefix, &children)
|
|
if err != nil {
|
|
// nothing below dir is wanted once its own listing failed
|
|
cancel()
|
|
}
|
|
children.Wait()
|
|
visitor.Done(dir, 0, err)
|
|
return err
|
|
}
|
|
|
|
func (w *walk) walkDir(ctx context.Context, dir util.FullPath, depth int) {
|
|
w.visitor.Started(dir, depth)
|
|
var children sync.WaitGroup
|
|
err := w.list(ctx, dir, depth, "", &children)
|
|
children.Wait()
|
|
w.visitor.Done(dir, depth, err)
|
|
}
|
|
|
|
// pageRetryFor is how long a page that fails with a transient error, as when
|
|
// the filer restarts, is asked for again before the listing fails.
|
|
const pageRetryFor = time.Minute
|
|
|
|
// list lists dir a page at a time and starts the walks of the subdirectories
|
|
// the visitor picks from each page before listing the next. A page that fails
|
|
// is dropped and its error returned.
|
|
func (w *walk) list(ctx context.Context, dir util.FullPath, depth int, prefix string, children *sync.WaitGroup) error {
|
|
after := ""
|
|
for {
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
if err := w.visitor.Listing(ctx, dir); err != nil {
|
|
return err
|
|
}
|
|
page := &Page{Dir: dir, Depth: depth, After: after, Start: time.Now()}
|
|
err := util.RetryWithBackoff(ctx, "list "+string(dir), pageRetryFor, util.IsTransientError, func() error {
|
|
page.Entries = nil
|
|
return w.Client.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
|
var err error
|
|
page.Snapshot, err = filer_pb.DoSeaweedListWithSnapshot(ctx, client, dir, prefix, func(entry *filer_pb.Entry, isLast bool) error {
|
|
page.Entries = append(page.Entries, entry)
|
|
return nil
|
|
}, after, false, w.PageSize, 0)
|
|
return err
|
|
})
|
|
})
|
|
page.End = time.Now()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
page.Last = uint32(len(page.Entries)) < w.PageSize
|
|
subDirs := w.visitor.Listed(page)
|
|
w.walkSubDirs(ctx, dir, subDirs, depth+1, children)
|
|
if page.Last {
|
|
// a walk stopped midway may have left out some of the page's
|
|
// subdirectories
|
|
return ctx.Err()
|
|
}
|
|
// a store that does not move past the cursor would page forever
|
|
last := page.Entries[len(page.Entries)-1].Name
|
|
if last == after {
|
|
return fmt.Errorf("list %s: pagination stuck at %q", dir, after)
|
|
}
|
|
after = last
|
|
}
|
|
}
|
|
|
|
// walkSubDirs walks the named subdirectories of parent, which are depth levels
|
|
// below the top, each in a goroutine of its own while a slot is free and in
|
|
// place otherwise.
|
|
func (w *walk) walkSubDirs(ctx context.Context, parent util.FullPath, names []string, depth int, children *sync.WaitGroup) {
|
|
for _, name := range names {
|
|
if ctx.Err() != nil {
|
|
return
|
|
}
|
|
child := parent.Child(name)
|
|
select {
|
|
case w.slots <- struct{}{}:
|
|
children.Add(1)
|
|
go func() {
|
|
defer children.Done()
|
|
w.walkDir(ctx, child, depth)
|
|
<-w.slots
|
|
}()
|
|
default:
|
|
w.walkDir(ctx, child, depth)
|
|
}
|
|
}
|
|
}
|