command_collection_list.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. func (c *commandCollectionList) HasTag(CommandTag) bool {
  21. return false
  22. }
  23. type CollectionInfo struct {
  24. FileCount float64
  25. DeleteCount float64
  26. DeletedByteCount float64
  27. Size float64
  28. VolumeCount int
  29. }
  30. func (c *commandCollectionList) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  31. collections, err := ListCollectionNames(commandEnv, true, true)
  32. if err != nil {
  33. return err
  34. }
  35. topologyInfo, _, err := collectTopologyInfo(commandEnv, 0)
  36. if err != nil {
  37. return err
  38. }
  39. collectionInfos := make(map[string]*CollectionInfo)
  40. collectCollectionInfo(topologyInfo, collectionInfos)
  41. for _, c := range collections {
  42. cif, found := collectionInfos[c]
  43. if !found {
  44. continue
  45. }
  46. 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)
  47. }
  48. fmt.Fprintf(writer, "Total %d collections.\n", len(collections))
  49. return nil
  50. }
  51. func ListCollectionNames(commandEnv *CommandEnv, includeNormalVolumes, includeEcVolumes bool) (collections []string, err error) {
  52. var resp *master_pb.CollectionListResponse
  53. err = commandEnv.MasterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
  54. resp, err = client.CollectionList(context.Background(), &master_pb.CollectionListRequest{
  55. IncludeNormalVolumes: includeNormalVolumes,
  56. IncludeEcVolumes: includeEcVolumes,
  57. })
  58. return err
  59. })
  60. if err != nil {
  61. return
  62. }
  63. for _, c := range resp.Collections {
  64. collections = append(collections, c.Name)
  65. }
  66. return
  67. }
  68. func addToCollection(collectionInfos map[string]*CollectionInfo, vif *master_pb.VolumeInformationMessage) {
  69. c := vif.Collection
  70. cif, found := collectionInfos[c]
  71. if !found {
  72. cif = &CollectionInfo{}
  73. collectionInfos[c] = cif
  74. }
  75. replicaPlacement, _ := super_block.NewReplicaPlacementFromByte(byte(vif.ReplicaPlacement))
  76. copyCount := float64(replicaPlacement.GetCopyCount())
  77. cif.Size += float64(vif.Size) / copyCount
  78. cif.DeleteCount += float64(vif.DeleteCount) / copyCount
  79. cif.FileCount += float64(vif.FileCount) / copyCount
  80. cif.DeletedByteCount += float64(vif.DeletedByteCount) / copyCount
  81. cif.VolumeCount++
  82. }
  83. func collectCollectionInfo(t *master_pb.TopologyInfo, collectionInfos map[string]*CollectionInfo) {
  84. for _, dc := range t.DataCenterInfos {
  85. for _, r := range dc.RackInfos {
  86. for _, dn := range r.DataNodeInfos {
  87. for _, diskInfo := range dn.DiskInfos {
  88. for _, vi := range diskInfo.VolumeInfos {
  89. addToCollection(collectionInfos, vi)
  90. }
  91. //for _, ecShardInfo := range diskInfo.EcShardInfos {
  92. //
  93. //}
  94. }
  95. }
  96. }
  97. }
  98. }