package shell import ( "context" "fmt" "io" "sort" "strings" "time" "github.com/seaweedfs/seaweedfs/weed/filer/dirusage" "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" "github.com/seaweedfs/seaweedfs/weed/util" ) // duReport prints what the dir_usage worker job keeps of path and of the // directories below it with a record of their own, down to depth levels // below path, the way fs.du prints what it walks. For / or the buckets // folder, the totals of the buckets and top-level directories below add up. func duReport(commandEnv *CommandEnv, path util.FullPath, depth int, writer io.Writer) error { if path != "/" { path = util.FullPath(strings.TrimSuffix(string(path), "/")) } ctx := context.Background() var roots dirusage.Roots var signature int32 err := commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error { var err error roots, signature, err = dirusage.RootsOf(ctx, client) return err }) if err != nil { return err } store := &dirusage.Store{Client: commandEnv, Signature: signature, Roots: roots} // a record the last replay did not change is at the cursor cursor, err := store.Cursor(ctx) if err != nil { return err } var records []*dirusage.Record // roots below path with no record yet, which the totals leave out var missing []string root, inRoot := roots.RootOf(path) switch { case inRoot: record, err := store.Load(ctx, root) if err != nil { return err } if record == nil { return fmt.Errorf("%s has no record yet: the dir_usage worker job keeps them once it is enabled", root) } records = append(records, record) case path == "/" || path == roots.Buckets: all, _, err := store.LoadAll(ctx) if err != nil { return err } for recordRoot, record := range all { if recordRoot.IsUnder(path) { records = append(records, record) } } if len(records) == 0 { return fmt.Errorf("no records below %s yet: the dir_usage worker job keeps them once it is enabled", path) } current, err := roots.List(ctx, commandEnv) if err != nil { return err } for currentRoot := range current { if currentRoot.IsUnder(path) && all[currentRoot] == nil { missing = append(missing, string(currentRoot)) } } sort.Strings(missing) default: return fmt.Errorf("the dir_usage worker job keeps no totals of %s", path) } totals := make(map[util.FullPath]duUsage) var oldest int64 for _, record := range records { recordRoot := util.FullPath(record.Root) for rel, usage := range record.Dirs { dir := recordRoot if rel != "" { dir = util.FullPath(string(recordRoot) + "/" + rel) } totals[dir] = duUsage{blocks: uint64(max(usage.Chunks, 0)), bytes: uint64(max(usage.Bytes, 0))} } // the directories that hold the roots add them up for above := recordRoot; !inRoot && above != path; { dir, _ := above.DirAndName() above = util.FullPath(dir) sum := totals[above] sum.add(totals[recordRoot]) totals[above] = sum } if at := max(record.TsNs, cursor); oldest == 0 || at < oldest { oldest = at } } below := make(map[util.FullPath]duUsage) for dir, usage := range totals { if dir.IsUnder(path) && dir != path && levelsBelow(path, dir) <= depth { below[dir] = usage } } printDuOrder(writer, below) if usage, found := totals[path]; found { fmt.Fprintf(writer, "block:%4d\tlogical size:%10d\t%s\n", usage.blocks, usage.bytes, path) } else { fmt.Fprintf(writer, "the record of %s keeps no total of %s: what is there counts toward the directory above it\n", root, path) } if len(missing) > 0 { fmt.Fprintf(writer, "not counted, no record yet: %s\n", strings.Join(missing, " ")) } at := time.Unix(0, oldest) fmt.Fprintf(writer, "as of %s, %s ago\n", at.UTC().Format(time.RFC3339), time.Since(at).Round(time.Second)) return nil } // levelsBelow is how many levels dir is below path. func levelsBelow(path, dir util.FullPath) int { return len(dir.Split()) - len(path.Split()) }