command_collection_list.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package shell
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  6. "io"
  7. )
  8. func init() {
  9. Commands = append(Commands, &commandCollectionList{})
  10. }
  11. type commandCollectionList struct {
  12. }
  13. func (c *commandCollectionList) Name() string {
  14. return "collection.list"
  15. }
  16. func (c *commandCollectionList) Help() string {
  17. return `list all collections`
  18. }
  19. type CollectionInfo struct {
  20. FileCount uint64
  21. DeleteCount uint64
  22. DeletedByteCount uint64
  23. Size uint64
  24. VolumeCount int
  25. }
  26. func (c *commandCollectionList) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  27. collections, err := ListCollectionNames(commandEnv, true, true)
  28. if err != nil {
  29. return err
  30. }
  31. topologyInfo, _, err := collectTopologyInfo(commandEnv)
  32. if err != nil {
  33. return err
  34. }
  35. collectionInfos := make(map[string]*CollectionInfo)
  36. writeCollectionInfo(writer, topologyInfo, collectionInfos)
  37. for _, c := range collections {
  38. cif, found := collectionInfos[c]
  39. if !found {
  40. continue
  41. }
  42. fmt.Fprintf(writer, "collection:\"%s\"\tvolumeCount:%d\tsize:%d\tfileCount:%d\tdeletedBytes:%d\tdeletion:%d\n", c, cif.VolumeCount, cif.Size, cif.FileCount, cif.DeletedByteCount, cif.DeleteCount)
  43. }
  44. fmt.Fprintf(writer, "Total %d collections.\n", len(collections))
  45. return nil
  46. }
  47. func ListCollectionNames(commandEnv *CommandEnv, includeNormalVolumes, includeEcVolumes bool) (collections []string, err error) {
  48. var resp *master_pb.CollectionListResponse
  49. err = commandEnv.MasterClient.WithClient(func(client master_pb.SeaweedClient) error {
  50. resp, err = client.CollectionList(context.Background(), &master_pb.CollectionListRequest{
  51. IncludeNormalVolumes: includeNormalVolumes,
  52. IncludeEcVolumes: includeEcVolumes,
  53. })
  54. return err
  55. })
  56. if err != nil {
  57. return
  58. }
  59. for _, c := range resp.Collections {
  60. collections = append(collections, c.Name)
  61. }
  62. return
  63. }
  64. func addToCollection(collectionInfos map[string]*CollectionInfo, vif *master_pb.VolumeInformationMessage) {
  65. c := vif.Collection
  66. cif, found := collectionInfos[c]
  67. if !found {
  68. cif = &CollectionInfo{}
  69. collectionInfos[c] = cif
  70. }
  71. cif.Size += vif.Size
  72. cif.DeleteCount += vif.DeleteCount
  73. cif.FileCount += vif.FileCount
  74. cif.DeletedByteCount += vif.DeletedByteCount
  75. cif.VolumeCount++
  76. }
  77. func writeCollectionInfo(writer io.Writer, t *master_pb.TopologyInfo, collectionInfos map[string]*CollectionInfo) {
  78. for _, dc := range t.DataCenterInfos {
  79. for _, r := range dc.RackInfos {
  80. for _, dn := range r.DataNodeInfos {
  81. for _, diskInfo := range dn.DiskInfos {
  82. for _, vi := range diskInfo.VolumeInfos {
  83. addToCollection(collectionInfos, vi)
  84. }
  85. //for _, ecShardInfo := range diskInfo.EcShardInfos {
  86. //
  87. //}
  88. }
  89. }
  90. }
  91. }
  92. }