seaweedfs/weed/shell/command_collection_list.go

111 lines
2.7 KiB
Go
Raw Normal View History

package shell
import (
"context"
"fmt"
2019-03-19 12:19:37 +00:00
"github.com/chrislusf/seaweedfs/weed/pb/master_pb"
"io"
)
func init() {
Commands = append(Commands, &commandCollectionList{})
}
type commandCollectionList struct {
}
func (c *commandCollectionList) Name() string {
return "collection.list"
}
func (c *commandCollectionList) Help() string {
2019-03-23 18:54:26 +00:00
return `list all collections`
}
type CollectionInfo struct {
FileCount uint64
DeleteCount uint64
DeletedByteCount uint64
Size uint64
VolumeCount int
}
func (c *commandCollectionList) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
2019-05-30 16:27:23 +00:00
collections, err := ListCollectionNames(commandEnv, true, true)
2019-05-06 05:21:28 +00:00
if err != nil {
return err
}
topologyInfo, _, err := collectTopologyInfo(commandEnv, 0)
if err != nil {
return err
}
collectionInfos := make(map[string]*CollectionInfo)
2022-01-21 08:16:50 +00:00
collectCollectionInfo(topologyInfo, collectionInfos)
2019-05-06 05:21:28 +00:00
for _, c := range collections {
cif, found := collectionInfos[c]
if !found {
continue
}
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)
2019-05-06 05:21:28 +00:00
}
fmt.Fprintf(writer, "Total %d collections.\n", len(collections))
return nil
}
func ListCollectionNames(commandEnv *CommandEnv, includeNormalVolumes, includeEcVolumes bool) (collections []string, err error) {
2019-03-19 12:19:37 +00:00
var resp *master_pb.CollectionListResponse
err = commandEnv.MasterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
resp, err = client.CollectionList(context.Background(), &master_pb.CollectionListRequest{
2019-05-30 16:27:23 +00:00
IncludeNormalVolumes: includeNormalVolumes,
IncludeEcVolumes: includeEcVolumes,
})
2019-03-19 12:19:37 +00:00
return err
})
if err != nil {
2019-05-06 05:21:28 +00:00
return
}
for _, c := range resp.Collections {
2019-05-06 05:21:28 +00:00
collections = append(collections, c.Name)
}
2019-05-06 05:21:28 +00:00
return
}
func addToCollection(collectionInfos map[string]*CollectionInfo, vif *master_pb.VolumeInformationMessage) {
c := vif.Collection
cif, found := collectionInfos[c]
if !found {
cif = &CollectionInfo{}
collectionInfos[c] = cif
}
cif.Size += vif.Size
cif.DeleteCount += vif.DeleteCount
cif.FileCount += vif.FileCount
cif.DeletedByteCount += vif.DeletedByteCount
cif.VolumeCount++
}
2022-01-21 08:16:50 +00:00
func collectCollectionInfo(t *master_pb.TopologyInfo, collectionInfos map[string]*CollectionInfo) {
for _, dc := range t.DataCenterInfos {
for _, r := range dc.RackInfos {
for _, dn := range r.DataNodeInfos {
for _, diskInfo := range dn.DiskInfos {
for _, vi := range diskInfo.VolumeInfos {
addToCollection(collectionInfos, vi)
}
//for _, ecShardInfo := range diskInfo.EcShardInfos {
//
//}
}
}
}
}
}