2019-03-18 03:27:08 +00:00
|
|
|
package shell
|
|
|
|
|
|
|
|
import (
|
2021-02-16 10:47:02 +00:00
|
|
|
"bytes"
|
2021-12-06 05:54:40 +00:00
|
|
|
"flag"
|
2019-03-18 03:27:08 +00:00
|
|
|
"fmt"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/master_pb"
|
2019-05-24 20:28:44 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/erasure_coding"
|
2022-04-18 02:35:43 +00:00
|
|
|
"golang.org/x/exp/slices"
|
2019-05-24 20:28:44 +00:00
|
|
|
|
2019-03-18 03:27:08 +00:00
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-06-05 08:30:24 +00:00
|
|
|
Commands = append(Commands, &commandVolumeList{})
|
2019-03-18 03:27:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type commandVolumeList struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandVolumeList) Name() string {
|
|
|
|
return "volume.list"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandVolumeList) Help() string {
|
2019-03-23 18:54:26 +00:00
|
|
|
return `list all volumes
|
|
|
|
|
|
|
|
This command list all volumes as a tree of dataCenter > rack > dataNode > volume.
|
|
|
|
|
|
|
|
`
|
2019-03-18 03:27:08 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 08:30:24 +00:00
|
|
|
func (c *commandVolumeList) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
2019-03-18 03:27:08 +00:00
|
|
|
|
2021-12-06 05:54:40 +00:00
|
|
|
volumeListCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
|
|
|
|
verbosityLevel := volumeListCommand.Int("v", 5, "verbose mode: 0, 1, 2, 3, 4, 5")
|
|
|
|
if err = volumeListCommand.Parse(args); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-22 08:28:42 +00:00
|
|
|
// collect topology information
|
2022-02-08 08:53:55 +00:00
|
|
|
topologyInfo, volumeSizeLimitMb, err := collectTopologyInfo(commandEnv, 0)
|
2019-03-18 03:27:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-06 05:54:40 +00:00
|
|
|
writeTopologyInfo(writer, topologyInfo, volumeSizeLimitMb, *verbosityLevel)
|
2019-03-18 03:27:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-16 10:47:02 +00:00
|
|
|
func diskInfosToString(diskInfos map[string]*master_pb.DiskInfo) string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
for diskType, diskInfo := range diskInfos {
|
2021-02-16 18:55:30 +00:00
|
|
|
if diskType == "" {
|
|
|
|
diskType = "hdd"
|
|
|
|
}
|
2021-02-16 10:47:02 +00:00
|
|
|
fmt.Fprintf(&buf, " %s(volume:%d/%d active:%d free:%d remote:%d)", diskType, diskInfo.VolumeCount, diskInfo.MaxVolumeCount, diskInfo.ActiveVolumeCount, diskInfo.FreeVolumeCount, diskInfo.RemoteVolumeCount)
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func diskInfoToString(diskInfo *master_pb.DiskInfo) string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
fmt.Fprintf(&buf, "volume:%d/%d active:%d free:%d remote:%d", diskInfo.VolumeCount, diskInfo.MaxVolumeCount, diskInfo.ActiveVolumeCount, diskInfo.FreeVolumeCount, diskInfo.RemoteVolumeCount)
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2021-12-06 05:54:40 +00:00
|
|
|
func writeTopologyInfo(writer io.Writer, t *master_pb.TopologyInfo, volumeSizeLimitMb uint64, verbosityLevel int) statistics {
|
|
|
|
output(verbosityLevel >= 0, writer, "Topology volumeSizeLimit:%d MB%s\n", volumeSizeLimitMb, diskInfosToString(t.DiskInfos))
|
2022-04-18 02:35:43 +00:00
|
|
|
slices.SortFunc(t.DataCenterInfos, func(a, b *master_pb.DataCenterInfo) bool {
|
|
|
|
return a.Id < b.Id
|
2019-05-06 03:23:50 +00:00
|
|
|
})
|
2019-04-06 16:25:29 +00:00
|
|
|
var s statistics
|
2019-03-18 03:27:08 +00:00
|
|
|
for _, dc := range t.DataCenterInfos {
|
2021-12-06 05:54:40 +00:00
|
|
|
s = s.plus(writeDataCenterInfo(writer, dc, verbosityLevel))
|
2019-03-18 03:27:08 +00:00
|
|
|
}
|
2021-12-06 05:54:40 +00:00
|
|
|
output(verbosityLevel >= 0, writer, "%+v \n", s)
|
2019-04-06 16:25:29 +00:00
|
|
|
return s
|
2019-03-18 03:27:08 +00:00
|
|
|
}
|
2021-12-06 05:54:40 +00:00
|
|
|
func writeDataCenterInfo(writer io.Writer, t *master_pb.DataCenterInfo, verbosityLevel int) statistics {
|
|
|
|
output(verbosityLevel >= 1, writer, " DataCenter %s%s\n", t.Id, diskInfosToString(t.DiskInfos))
|
2019-04-06 16:25:29 +00:00
|
|
|
var s statistics
|
2022-04-18 02:35:43 +00:00
|
|
|
slices.SortFunc(t.RackInfos, func(a, b *master_pb.RackInfo) bool {
|
|
|
|
return a.Id < b.Id
|
2019-05-06 03:23:50 +00:00
|
|
|
})
|
2019-03-18 03:27:08 +00:00
|
|
|
for _, r := range t.RackInfos {
|
2021-12-06 05:54:40 +00:00
|
|
|
s = s.plus(writeRackInfo(writer, r, verbosityLevel))
|
2019-03-18 03:27:08 +00:00
|
|
|
}
|
2021-12-06 05:54:40 +00:00
|
|
|
output(verbosityLevel >= 1, writer, " DataCenter %s %+v \n", t.Id, s)
|
2019-04-06 16:25:29 +00:00
|
|
|
return s
|
2019-03-18 03:27:08 +00:00
|
|
|
}
|
2021-12-06 05:54:40 +00:00
|
|
|
func writeRackInfo(writer io.Writer, t *master_pb.RackInfo, verbosityLevel int) statistics {
|
|
|
|
output(verbosityLevel >= 2, writer, " Rack %s%s\n", t.Id, diskInfosToString(t.DiskInfos))
|
2019-04-06 16:25:29 +00:00
|
|
|
var s statistics
|
2022-04-18 02:35:43 +00:00
|
|
|
slices.SortFunc(t.DataNodeInfos, func(a, b *master_pb.DataNodeInfo) bool {
|
|
|
|
return a.Id < b.Id
|
2019-05-06 03:23:50 +00:00
|
|
|
})
|
2019-03-18 03:27:08 +00:00
|
|
|
for _, dn := range t.DataNodeInfos {
|
2021-12-06 05:54:40 +00:00
|
|
|
s = s.plus(writeDataNodeInfo(writer, dn, verbosityLevel))
|
2019-03-18 03:27:08 +00:00
|
|
|
}
|
2021-12-06 05:54:40 +00:00
|
|
|
output(verbosityLevel >= 2, writer, " Rack %s %+v \n", t.Id, s)
|
2019-04-06 16:25:29 +00:00
|
|
|
return s
|
2019-03-18 03:27:08 +00:00
|
|
|
}
|
2021-12-06 05:54:40 +00:00
|
|
|
func writeDataNodeInfo(writer io.Writer, t *master_pb.DataNodeInfo, verbosityLevel int) statistics {
|
|
|
|
output(verbosityLevel >= 3, writer, " DataNode %s%s\n", t.Id, diskInfosToString(t.DiskInfos))
|
2019-04-06 16:25:29 +00:00
|
|
|
var s statistics
|
2021-02-16 10:47:02 +00:00
|
|
|
for _, diskInfo := range t.DiskInfos {
|
2021-12-06 05:54:40 +00:00
|
|
|
s = s.plus(writeDiskInfo(writer, diskInfo, verbosityLevel))
|
2021-02-16 10:47:02 +00:00
|
|
|
}
|
2021-12-06 05:54:40 +00:00
|
|
|
output(verbosityLevel >= 3, writer, " DataNode %s %+v \n", t.Id, s)
|
2021-02-16 10:47:02 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2021-12-06 05:54:40 +00:00
|
|
|
func writeDiskInfo(writer io.Writer, t *master_pb.DiskInfo, verbosityLevel int) statistics {
|
2021-02-16 10:47:02 +00:00
|
|
|
var s statistics
|
2021-02-16 18:55:30 +00:00
|
|
|
diskType := t.Type
|
|
|
|
if diskType == "" {
|
|
|
|
diskType = "hdd"
|
|
|
|
}
|
2021-12-06 05:54:40 +00:00
|
|
|
output(verbosityLevel >= 4, writer, " Disk %s(%s)\n", diskType, diskInfoToString(t))
|
2022-04-18 02:35:43 +00:00
|
|
|
slices.SortFunc(t.VolumeInfos, func(a, b *master_pb.VolumeInformationMessage) bool {
|
|
|
|
return a.Id < b.Id
|
2019-05-06 03:23:50 +00:00
|
|
|
})
|
2019-03-18 03:27:08 +00:00
|
|
|
for _, vi := range t.VolumeInfos {
|
2021-12-06 05:54:40 +00:00
|
|
|
s = s.plus(writeVolumeInformationMessage(writer, vi, verbosityLevel))
|
2019-03-18 03:27:08 +00:00
|
|
|
}
|
2019-05-24 18:52:23 +00:00
|
|
|
for _, ecShardInfo := range t.EcShardInfos {
|
2021-12-06 05:54:40 +00:00
|
|
|
output(verbosityLevel >= 5, writer, " ec volume id:%v collection:%v shards:%v\n", ecShardInfo.Id, ecShardInfo.Collection, erasure_coding.ShardBits(ecShardInfo.EcIndexBits).ShardIds())
|
2019-05-24 18:52:23 +00:00
|
|
|
}
|
2021-12-06 05:54:40 +00:00
|
|
|
output(verbosityLevel >= 4, writer, " Disk %s %+v \n", diskType, s)
|
2019-04-06 16:25:29 +00:00
|
|
|
return s
|
2019-03-18 03:27:08 +00:00
|
|
|
}
|
2021-02-16 10:47:02 +00:00
|
|
|
|
2021-12-06 05:54:40 +00:00
|
|
|
func writeVolumeInformationMessage(writer io.Writer, t *master_pb.VolumeInformationMessage, verbosityLevel int) statistics {
|
|
|
|
output(verbosityLevel >= 5, writer, " volume %+v \n", t)
|
2019-09-29 06:17:37 +00:00
|
|
|
return newStatistics(t)
|
2019-04-06 16:25:29 +00:00
|
|
|
}
|
|
|
|
|
2021-12-06 05:54:40 +00:00
|
|
|
func output(condition bool, w io.Writer, format string, a ...interface{}) {
|
|
|
|
if condition {
|
|
|
|
fmt.Fprintf(w, format, a...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-06 16:25:29 +00:00
|
|
|
type statistics struct {
|
|
|
|
Size uint64
|
|
|
|
FileCount uint64
|
|
|
|
DeletedFileCount uint64
|
|
|
|
DeletedBytes uint64
|
|
|
|
}
|
|
|
|
|
2019-09-29 06:17:37 +00:00
|
|
|
func newStatistics(t *master_pb.VolumeInformationMessage) statistics {
|
2019-04-06 16:25:29 +00:00
|
|
|
return statistics{
|
|
|
|
Size: t.Size,
|
|
|
|
FileCount: t.FileCount,
|
|
|
|
DeletedFileCount: t.DeleteCount,
|
|
|
|
DeletedBytes: t.DeletedByteCount,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s statistics) plus(t statistics) statistics {
|
|
|
|
return statistics{
|
|
|
|
Size: s.Size + t.Size,
|
|
|
|
FileCount: s.FileCount + t.FileCount,
|
|
|
|
DeletedFileCount: s.DeletedFileCount + t.DeletedFileCount,
|
|
|
|
DeletedBytes: s.DeletedBytes + t.DeletedBytes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s statistics) String() string {
|
2019-04-06 18:12:35 +00:00
|
|
|
if s.DeletedFileCount > 0 {
|
2019-04-06 16:25:29 +00:00
|
|
|
return fmt.Sprintf("total size:%d file_count:%d deleted_file:%d deleted_bytes:%d", s.Size, s.FileCount, s.DeletedFileCount, s.DeletedBytes)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("total size:%d file_count:%d", s.Size, s.FileCount)
|
2019-03-18 03:27:08 +00:00
|
|
|
}
|