command_collection_list.go 3.0 KB

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