You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
2.9 KiB

5 years ago
  1. package shell
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "google.golang.org/grpc"
  7. "github.com/chrislusf/seaweedfs/weed/filer2"
  8. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. )
  11. func init() {
  12. Commands = append(Commands, &commandFsDu{})
  13. }
  14. type commandFsDu struct {
  15. }
  16. func (c *commandFsDu) Name() string {
  17. return "fs.du"
  18. }
  19. func (c *commandFsDu) Help() string {
  20. return `show disk usage
  21. fs.du http://<filer_server>:<port>/dir
  22. fs.du http://<filer_server>:<port>/dir/file_name
  23. fs.du http://<filer_server>:<port>/dir/file_prefix
  24. `
  25. }
  26. func (c *commandFsDu) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  27. filerServer, filerPort, path, err := commandEnv.parseUrl(findInputDirectory(args))
  28. if err != nil {
  29. return err
  30. }
  31. ctx := context.Background()
  32. if commandEnv.isDirectory(ctx, filerServer, filerPort, path) {
  33. path = path + "/"
  34. }
  35. var blockCount, byteCount uint64
  36. dir, name := filer2.FullPath(path).DirAndName()
  37. blockCount, byteCount, err = duTraverseDirectory(ctx, writer, commandEnv.getFilerClient(filerServer, filerPort), dir, name)
  38. if name == "" && err == nil {
  39. fmt.Fprintf(writer, "block:%4d\tbyte:%10d\t%s\n", blockCount, byteCount, dir)
  40. }
  41. return
  42. }
  43. func duTraverseDirectory(ctx context.Context, writer io.Writer, filerClient filer2.FilerClient, dir, name string) (blockCount uint64, byteCount uint64, err error) {
  44. err = filer2.ReadDirAllEntries(ctx, filerClient, filer2.FullPath(dir), name, func(entry *filer_pb.Entry, isLast bool) {
  45. if entry.IsDirectory {
  46. subDir := fmt.Sprintf("%s/%s", dir, entry.Name)
  47. if dir == "/" {
  48. subDir = "/" + entry.Name
  49. }
  50. numBlock, numByte, err := duTraverseDirectory(ctx, writer, filerClient, subDir, "")
  51. if err == nil {
  52. blockCount += numBlock
  53. byteCount += numByte
  54. }
  55. } else {
  56. blockCount += uint64(len(entry.Chunks))
  57. byteCount += filer2.TotalSize(entry.Chunks)
  58. }
  59. if name != "" && !entry.IsDirectory {
  60. fmt.Fprintf(writer, "block:%4d\tbyte:%10d\t%s/%s\n", blockCount, byteCount, dir, name)
  61. }
  62. })
  63. return
  64. }
  65. func (env *CommandEnv) withFilerClient(ctx context.Context, filerServer string, filerPort int64, fn func(filer_pb.SeaweedFilerClient) error) error {
  66. filerGrpcAddress := fmt.Sprintf("%s:%d", filerServer, filerPort+10000)
  67. return util.WithCachedGrpcClient(ctx, func(grpcConnection *grpc.ClientConn) error {
  68. client := filer_pb.NewSeaweedFilerClient(grpcConnection)
  69. return fn(client)
  70. }, filerGrpcAddress, env.option.GrpcDialOption)
  71. }
  72. type commandFilerClient struct {
  73. env *CommandEnv
  74. filerServer string
  75. filerPort int64
  76. }
  77. func (env *CommandEnv) getFilerClient(filerServer string, filerPort int64) *commandFilerClient {
  78. return &commandFilerClient{
  79. env: env,
  80. filerServer: filerServer,
  81. filerPort: filerPort,
  82. }
  83. }
  84. func (c *commandFilerClient) WithFilerClient(ctx context.Context, fn func(filer_pb.SeaweedFilerClient) error) error {
  85. return c.env.withFilerClient(ctx, c.filerServer, c.filerPort, fn)
  86. }