Track remote replication bandwidth consumption (#3609)

This commit is contained in:
Ritesh H Shukla
2021-03-10 17:30:36 -08:00
committed by GitHub
parent dde89f7604
commit faa895dd75
4 changed files with 216 additions and 5 deletions
+194
View File
@@ -0,0 +1,194 @@
/*
* MinIO Client (C) 2020 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cmd
import (
"fmt"
"sort"
"strings"
"time"
"github.com/dustin/go-humanize"
"github.com/fatih/color"
"github.com/minio/cli"
json "github.com/minio/mc/pkg/colorjson"
"github.com/minio/mc/pkg/probe"
"github.com/minio/minio/pkg/console"
"github.com/minio/minio/pkg/madmin"
)
var adminBandwidthInfoCmdFlags = []cli.Flag{
cli.StringFlag{
Name: "unit",
Value: "b",
Usage: "[b|bi|B|Bi] Display bandwidth in bits (IEC [bi] or SI [b]) or bytes (IEC [Bi] or SI [B])",
},
}
var adminBwInfoCmd = cli.Command{
Name: "bandwidth",
Usage: "Show bandwidth info for buckets on the MinIO server in bits or bytes per second. Ki,Bi,Mi,Gi represent IEC units.",
Action: mainAdminBwInfo,
Before: setGlobalsFromContext,
OnUsageError: onUsageError,
Flags: append(globalFlags, adminBandwidthInfoCmdFlags...),
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} FLAGS TARGET
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Show the bandwidth usage for all the buckets in a MinIO server setup
{{.Prompt}} {{.HelpName}} play/
2. Show the bandwidth usage for the bucket 'source-bucket' in a MinIO server setup
{{.Prompt}} {{.HelpName}} play/source-bucket
`,
}
func printTable(report madmin.Report, bits bool, iec bool) {
bucketMaxLength := 63
bucketColLength := 6
var bucketKeys []string
for bucket := range report.Report.BucketStats {
if len(bucket) <= bucketMaxLength && len(bucket) > bucketColLength {
bucketColLength = len(bucket)
}
bucketKeys = append(bucketKeys, bucket)
}
sort.Strings(bucketKeys)
dspOrder := []col{colGreen} // Header
for i := 0; i < len(report.Report.BucketStats); i++ {
dspOrder = append(dspOrder, colGrey)
}
var printColors []*color.Color
for _, c := range dspOrder {
printColors = append(printColors, getPrintCol(c))
}
unit := "BitsPerSecond"
if !bits {
unit = "BytesPerSecond"
}
cellText := make([][]string, len(report.Report.BucketStats)+1) // 1 for the header
tbl := console.NewTable(printColors, []bool{false, false, false}, 5)
bucketTitle := fmt.Sprintf("%-16v", "Bucket")
cellText[0] = []string{
bucketTitle,
"Max Bandwidth " + unit,
"Current Bandwidth " + unit,
}
tbl.HeaderRowSeparator = true
index := 1
for _, bucket := range bucketKeys {
values := report.Report.BucketStats[bucket]
if len(bucket) > bucketMaxLength {
bucket = bucket[:bucketMaxLength] + ".."
}
var mul uint64
mul = 1
if bits {
mul = 8
}
limit := humanize.Bytes(uint64(values.LimitInBytesPerSecond) * mul)
current := humanize.Bytes(uint64(values.CurrentBandwidthInBytesPerSecond) * mul)
if iec {
limit = humanize.IBytes(uint64(values.LimitInBytesPerSecond) * mul)
current = humanize.IBytes(uint64(values.CurrentBandwidthInBytesPerSecond) * mul)
}
if bits {
limit = strings.ToLower(limit)
current = strings.ToLower(current)
}
cellText[index] = []string{
bucket,
limit,
current,
}
index++
}
if len(report.Report.BucketStats) > 0 {
err := tbl.DisplayTable(cellText)
if err != nil {
console.Error(err)
}
}
}
func checkAdminBwInfoSyntax(ctx *cli.Context) {
u := ctx.String("unit")
if u != "bi" &&
u != "b" &&
u != "Bi" &&
u != "B" &&
u != "" {
cli.ShowCommandHelpAndExit(ctx, "bandwidth", globalErrorExitStatus)
}
if len(ctx.Args()) > 1 || len(ctx.Args()) == 0 {
cli.ShowCommandHelpAndExit(ctx, "bandwidth", globalErrorExitStatus)
}
}
func mainAdminBwInfo(ctx *cli.Context) {
checkAdminBwInfoSyntax(ctx)
aliasURL, bucket := getAliasAndBucket(ctx)
client := getClient(aliasURL)
reportCh := client.GetBucketBandwidth(globalContext, bucket)
firstPrint := true
bandwidthUnitsString := ctx.String("unit")
for {
select {
case report := <-reportCh:
if len(report.Report.BucketStats) == 0 {
continue
}
if report.Err != nil {
if strings.Contains(report.Err.Error(), "EOF") {
continue
}
console.Error(report.Err)
}
printBandwidth(report, firstPrint, bandwidthUnitsString == "bi" || bandwidthUnitsString == "b",
bandwidthUnitsString == "bi" || bandwidthUnitsString == "Bi")
firstPrint = false
case <-globalContext.Done():
return
}
}
}
func printBandwidth(report madmin.Report, firstPrint bool, bits bool, iec bool) {
rewindLines := len(report.Report.BucketStats) + 4
if firstPrint {
rewindLines = 0
}
if globalJSON {
reportJSON, e := json.MarshalIndent(report, "", " ")
fatalIf(probe.NewError(e), "Unable to marshal to JSON")
console.Println(string(reportJSON))
time.Sleep(1 * time.Second)
return
}
if len(report.Report.BucketStats) > 0 {
console.RewindLines(rewindLines)
// For the next iteration, rewind lines
printTable(report, bits, iec)
}
time.Sleep(1 * time.Second)
}
+1
View File
@@ -23,6 +23,7 @@ var adminBucketRemoteSubcommands = []cli.Command{
adminBucketRemoteEditCmd,
adminBucketRemoteListCmd,
adminBucketRemoteRmCmd,
adminBwInfoCmd,
}
var adminBucketRemoteCmd = cli.Command{
+6 -5
View File
@@ -345,11 +345,12 @@ var completeCmds = map[string]complete.Predictor{
"/admin/group/remove": aliasCompleter,
"/admin/group/info": aliasCompleter,
"/admin/bucket/remote/add": aliasCompleter,
"/admin/bucket/remote/edit": aliasCompleter,
"/admin/bucket/remote/ls": aliasCompleter,
"/admin/bucket/remote/rm": aliasCompleter,
"/admin/bucket/quota": aliasCompleter,
"/admin/bucket/remote/add": aliasCompleter,
"/admin/bucket/remote/edit": aliasCompleter,
"/admin/bucket/remote/ls": aliasCompleter,
"/admin/bucket/remote/rm": aliasCompleter,
"/admin/bucket/remote/bandwidth": aliasCompleter,
"/admin/bucket/quota": aliasCompleter,
"/admin/kms/key/create": aliasCompleter,
"/admin/kms/key/status": aliasCompleter,
+15
View File
@@ -32,8 +32,10 @@ import (
"strings"
"time"
"github.com/minio/cli"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/encrypt"
"github.com/minio/minio/pkg/madmin"
"github.com/minio/mc/pkg/ioutils"
"github.com/minio/mc/pkg/probe"
@@ -433,3 +435,16 @@ func centerText(s string, w int) string {
fmt.Fprintf(&sb, "%s", bytes.Repeat([]byte{' '}, int(math.Floor(padding))))
return sb.String()
}
func getAliasAndBucket(ctx *cli.Context) (string, string) {
args := ctx.Args()
aliasedURL := args.Get(0)
aliasedURL = filepath.Clean(aliasedURL)
return url2Alias(aliasedURL)
}
func getClient(aliasURL string) *madmin.AdminClient {
client, err := newAdminClient(aliasURL)
fatalIf(err, "Unable to initialize admin connection.")
return client
}