diff --git a/cmd/admin-replicate-resync-cancel.go b/cmd/admin-replicate-resync-cancel.go new file mode 100644 index 00000000..7e981efb --- /dev/null +++ b/cmd/admin-replicate-resync-cancel.go @@ -0,0 +1,111 @@ +// Copyright (c) 2015-2022 MinIO, Inc. +// +// This file is part of MinIO Object Storage stack +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package cmd + +import ( + "fmt" + "strings" + + "github.com/fatih/color" + "github.com/minio/cli" + json "github.com/minio/colorjson" + "github.com/minio/madmin-go" + "github.com/minio/mc/pkg/probe" + "github.com/minio/pkg/console" +) + +var adminReplicateResyncCancelCmd = cli.Command{ + Name: "cancel", + Usage: "cancel ongoing resync operation", + Action: mainAdminReplicateResyncCancel, + OnUsageError: onUsageError, + Before: setGlobalsFromContext, + Flags: globalFlags, + CustomHelpTemplate: `NAME: + {{.HelpName}} - {{.Usage}} + +USAGE: + {{.HelpName}} ALIAS1 ALIAS2 + +FLAGS: + {{range .VisibleFlags}}{{.}} + {{end}} + +EXAMPLES: + 1. Cancel ongoing resync of bucket data from minio1 to minio2 + {{.Prompt}} {{.HelpName}} minio1 minio2 +`, +} + +type resyncCancelMessage madmin.SRResyncOpStatus + +func (m resyncCancelMessage) JSON() string { + bs, e := json.MarshalIndent(madmin.SRResyncOpStatus(m), "", " ") + fatalIf(probe.NewError(e), "Unable to marshal into JSON.") + return string(bs) +} + +func (m resyncCancelMessage) String() string { + v := madmin.SRResyncOpStatus(m) + messages := []string{} + th := "ResyncMessage" + if v.ErrDetail != "" { + messages = append(messages, v.ErrDetail) + th = "ResyncErr" + } else { + messages = append(messages, fmt.Sprintf("Site resync with ID %s canceled successfully.", v.ResyncID)) + } + return console.Colorize(th, strings.Join(messages, "\n")) +} + +func mainAdminReplicateResyncCancel(ctx *cli.Context) error { + { + // Check argument count + argsNr := len(ctx.Args()) + if argsNr != 2 { + cli.ShowCommandHelpAndExit(ctx, "cancel", 1) // last argument is exit code + } + } + console.SetColor("ResyncMessage", color.New(color.FgGreen)) + + // Get the alias parameter from cli + args := ctx.Args() + aliasedURL := args.Get(0) + + // Create a new MinIO Admin Client + client, err := newAdminClient(aliasedURL) + fatalIf(err, "Unable to initialize admin connection.") + info, e := client.SiteReplicationInfo(globalContext) + fatalIf(probe.NewError(e), "Unable to fetch site replication info.") + var peer madmin.PeerInfo + for _, site := range info.Sites { + if args[1] == site.Name { + peer = site + } + } + if peer.DeploymentID == "" { + fatalIf(errInvalidArgument().Trace(ctx.Args().Tail()...), + "alias provided is not part of cluster replication.") + } + res, e := client.SiteReplicationResyncOp(globalContext, peer, madmin.SiteResyncCancel) + fatalIf(probe.NewError(e).Trace(args...), "Unable to cancel replication resync") + + printMsg(resyncCancelMessage(res)) + + return nil +} diff --git a/cmd/admin-replicate-resync-start.go b/cmd/admin-replicate-resync-start.go new file mode 100644 index 00000000..73f9e7d1 --- /dev/null +++ b/cmd/admin-replicate-resync-start.go @@ -0,0 +1,111 @@ +// Copyright (c) 2015-2022 MinIO, Inc. +// +// This file is part of MinIO Object Storage stack +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package cmd + +import ( + "fmt" + "strings" + + "github.com/fatih/color" + "github.com/minio/cli" + json "github.com/minio/colorjson" + "github.com/minio/madmin-go" + "github.com/minio/mc/pkg/probe" + "github.com/minio/pkg/console" +) + +var adminReplicateResyncStartCmd = cli.Command{ + Name: "start", + Usage: "start resync to site", + Action: mainAdminReplicateResyncStart, + OnUsageError: onUsageError, + Before: setGlobalsFromContext, + Flags: globalFlags, + CustomHelpTemplate: `NAME: + {{.HelpName}} - {{.Usage}} + +USAGE: + {{.HelpName}} ALIAS1 ALIAS2 + +FLAGS: + {{range .VisibleFlags}}{{.}} + {{end}} + +EXAMPLES: + 1. Resync bucket data from minio1 to minio2 + {{.Prompt}} {{.HelpName}} minio1 minio2 +`, +} + +type resyncMessage madmin.SRResyncOpStatus + +func (m resyncMessage) JSON() string { + bs, e := json.MarshalIndent(madmin.SRResyncOpStatus(m), "", " ") + fatalIf(probe.NewError(e), "Unable to marshal into JSON.") + return string(bs) +} + +func (m resyncMessage) String() string { + v := madmin.SRResyncOpStatus(m) + messages := []string{} + th := "ResyncMessage" + if v.ErrDetail != "" { + messages = append(messages, v.ErrDetail) + th = "ResyncErr" + } else { + messages = append(messages, fmt.Sprintf("Site resync started with ID %s", v.ResyncID)) + } + return console.Colorize(th, strings.Join(messages, "\n")) +} + +func mainAdminReplicateResyncStart(ctx *cli.Context) error { + { + // Check argument count + argsNr := len(ctx.Args()) + if argsNr != 2 { + cli.ShowCommandHelpAndExit(ctx, "start", 1) // last argument is exit code + } + } + + console.SetColor("ResyncMessage", color.New(color.FgGreen)) + console.SetColor("ResyncErr", color.New(color.FgRed)) + + // Get the alias parameter from cli + args := ctx.Args() + aliasedURL := args.Get(0) + // Create a new MinIO Admin Client + client, err := newAdminClient(aliasedURL) + fatalIf(err, "Unable to initialize admin connection.") + info, e := client.SiteReplicationInfo(globalContext) + fatalIf(probe.NewError(e), "Unable to fetch site replication info.") + var peer madmin.PeerInfo + for _, site := range info.Sites { + if args[1] == site.Name { + peer = site + } + } + if peer.DeploymentID == "" { + fatalIf(errInvalidArgument().Trace(ctx.Args().Tail()...), + "alias provided is not part of cluster replication.") + } + res, e := client.SiteReplicationResyncOp(globalContext, peer, madmin.SiteResyncStart) + fatalIf(probe.NewError(e).Trace(args...), "Unable to start replication resync") + printMsg(resyncMessage(res)) + + return nil +} diff --git a/cmd/admin-replicate-resync-status.go b/cmd/admin-replicate-resync-status.go new file mode 100644 index 00000000..4b8965f2 --- /dev/null +++ b/cmd/admin-replicate-resync-status.go @@ -0,0 +1,252 @@ +// Copyright (c) 2015-2022 MinIO, Inc. +// +// This file is part of MinIO Object Storage stack +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package cmd + +import ( + "context" + "errors" + "fmt" + "os" + "strings" + "time" + + "github.com/charmbracelet/bubbles/spinner" + tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/lipgloss" + "github.com/dustin/go-humanize" + "github.com/fatih/color" + "github.com/minio/cli" + "github.com/minio/madmin-go" + "github.com/minio/mc/pkg/probe" + "github.com/minio/pkg/console" + "github.com/olekukonko/tablewriter" +) + +var adminReplicateResyncStatusCmd = cli.Command{ + Name: "status", + Usage: "show site replication resync status", + Action: mainAdminReplicationResyncStatus, + OnUsageError: onUsageError, + Before: setGlobalsFromContext, + Flags: globalFlags, + CustomHelpTemplate: `NAME: + {{.HelpName}} - {{.Usage}} + +USAGE: + {{.HelpName}} ALIAS1 ALIAS2 + +FLAGS: + {{range .VisibleFlags}}{{.}} + {{end}} + +EXAMPLES: + 1. Display status of resync from minio1 to minio2 + {{.Prompt}} {{.HelpName}} minio1 minio2 +`, +} + +func mainAdminReplicationResyncStatus(ctx *cli.Context) error { + { + // Check argument count + argsNr := len(ctx.Args()) + if argsNr != 2 { + cli.ShowCommandHelpAndExit(ctx, "status", 1) // last argument is exit code + } + } + + console.SetColor("ResyncMessage", color.New(color.FgGreen)) + console.SetColor("THeader", color.New(color.Bold, color.FgHiWhite)) + console.SetColor("THeader2", color.New(color.Bold, color.FgYellow)) + console.SetColor("TDetail", color.New(color.Bold, color.FgCyan)) + + // Get the alias parameter from cli + args := ctx.Args() + aliasedURL := args.Get(0) + + // Create a new MinIO Admin Client + client, err := newAdminClient(aliasedURL) + fatalIf(err, "Unable to initialize admin connection.") + info, e := client.SiteReplicationInfo(globalContext) + fatalIf(probe.NewError(e), "Unable to fetch site replication info.") + if !info.Enabled { + console.Colorize("ResyncMessage", "SiteReplication is not enabled") + return nil + } + var peer madmin.PeerInfo + for _, site := range info.Sites { + if args[1] == site.Name { + peer = site + } + } + if peer.DeploymentID == "" { + fatalIf(errInvalidArgument().Trace(ctx.Args().Tail()...), + "alias provided is not part of cluster replication.") + } + ctxt, cancel := context.WithCancel(globalContext) + defer cancel() + + done := make(chan struct{}) + + ui := tea.NewProgram(initResyncMetricsUI(peer.DeploymentID)) + if !globalJSON { + go func() { + if e := ui.Start(); e != nil { + cancel() + os.Exit(1) + } + close(done) + }() + } + + go func() { + opts := madmin.MetricsOptions{ + Type: madmin.MetricsSiteResync, + ByDepID: peer.DeploymentID, + } + e := client.Metrics(ctxt, opts, func(metrics madmin.RealtimeMetrics) { + if globalJSON { + printMsg(metricsMessage{RealtimeMetrics: metrics}) + return + } + if metrics.Aggregated.SiteResync != nil { + sr := metrics.Aggregated.SiteResync + ui.Send(sr) + if sr.Complete() { + cancel() + return + } + } + }) + if e != nil && !errors.Is(e, context.Canceled) { + fatalIf(probe.NewError(e).Trace(ctx.Args()...), "Unable to get current status") + } + }() + + <-done + return nil +} + +func initResyncMetricsUI(deplID string) *resyncMetricsUI { + s := spinner.New() + s.Spinner = spinner.Points + s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205")) + return &resyncMetricsUI{ + spinner: s, + deplID: deplID, + } +} + +type resyncMetricsUI struct { + current madmin.SiteResyncMetrics + spinner spinner.Model + quitting bool + deplID string +} + +func (m *resyncMetricsUI) Init() tea.Cmd { + return m.spinner.Tick +} + +func (m *resyncMetricsUI) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + switch msg := msg.(type) { + case tea.KeyMsg: + switch msg.String() { + case "ctrl+c": + m.quitting = true + return m, tea.Quit + default: + return m, nil + } + case *madmin.SiteResyncMetrics: + m.current = *msg + if msg.Complete() { + m.quitting = true + return m, tea.Quit + } + return m, nil + case spinner.TickMsg: + var cmd tea.Cmd + m.spinner, cmd = m.spinner.Update(msg) + return m, cmd + default: + return m, nil + } +} + +func (m *resyncMetricsUI) View() string { + var s strings.Builder + + // Set table header + table := tablewriter.NewWriter(&s) + table.SetAutoWrapText(false) + table.SetAutoFormatHeaders(true) + table.SetHeaderAlignment(tablewriter.ALIGN_LEFT) + table.SetAlignment(tablewriter.ALIGN_LEFT) + table.SetCenterSeparator("") + table.SetColumnSeparator("") + table.SetRowSeparator("") + table.SetHeaderLine(false) + table.SetBorder(false) + table.SetTablePadding("\t") // pad with tabs + table.SetNoWhiteSpace(true) + + var data [][]string + addLine := func(prefix string, value interface{}) { + data = append(data, []string{ + prefix, + whiteStyle.Render(fmt.Sprint(value)), + }) + } + + if !m.quitting { + s.WriteString(m.spinner.View()) + } else { + if m.current.Complete() { + if m.current.FailedCount == 0 { + s.WriteString(m.spinner.Style.Render((tickCell + tickCell + tickCell))) + } else { + s.WriteString(m.spinner.Style.Render((crossTickCell + crossTickCell + crossTickCell))) + } + } + } + s.WriteString("\n") + if m.current.ResyncID != "" { + accElapsedTime := m.current.LastUpdate.Sub(m.current.StartTime) + addLine("ResyncID: ", m.current.ResyncID) + addLine("Objects: ", m.current.ReplicatedCount) + addLine("Versions: ", m.current.ReplicatedCount) + addLine("FailedObjects: ", m.current.FailedCount) + if accElapsedTime > 0 { + bytesTransferredPerSec := float64(int64(time.Second)*m.current.ReplicatedSize) / float64(accElapsedTime) + objectsPerSec := float64(int64(time.Second)*m.current.ReplicatedCount) / float64(accElapsedTime) + addLine("Throughput: ", fmt.Sprintf("%s/s", humanize.IBytes(uint64(bytesTransferredPerSec)))) + addLine("IOPs: ", fmt.Sprintf("%.2f objs/s", objectsPerSec)) + } + addLine("Transferred: ", humanize.IBytes(uint64(m.current.ReplicatedSize))) + addLine("Elapsed: ", accElapsedTime.String()) + addLine("CurrObjName: ", fmt.Sprintf("%s/%s", m.current.Bucket, m.current.Object)) + } + table.AppendBulk(data) + table.Render() + + if m.quitting { + s.WriteString("\n") + } + + return s.String() +} diff --git a/cmd/admin-replicate-resync.go b/cmd/admin-replicate-resync.go new file mode 100644 index 00000000..fcc86a6a --- /dev/null +++ b/cmd/admin-replicate-resync.go @@ -0,0 +1,41 @@ +// Copyright (c) 2015-2022 MinIO, Inc. +// +// This file is part of MinIO Object Storage stack +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package cmd + +import "github.com/minio/cli" + +var adminReplicateResyncSubcommands = []cli.Command{ + adminReplicateResyncStartCmd, + adminReplicateResyncStatusCmd, + adminReplicateResyncCancelCmd, +} + +var adminReplicateResyncCmd = cli.Command{ + Name: "resync", + Usage: "resync content to site", + Action: mainAdminReplicateResync, + Before: setGlobalsFromContext, + Flags: globalFlags, + Subcommands: adminReplicateResyncSubcommands, + HideHelpCommand: true, +} + +func mainAdminReplicateResync(ctx *cli.Context) error { + commandNotFound(ctx, adminReplicateResyncSubcommands) + return nil +} diff --git a/cmd/admin-replicate.go b/cmd/admin-replicate.go index fc1ea452..572c51e9 100644 --- a/cmd/admin-replicate.go +++ b/cmd/admin-replicate.go @@ -25,6 +25,7 @@ var adminReplicateSubcommands = []cli.Command{ adminReplicateRemoveCmd, adminReplicateInfoCmd, adminReplicateStatusCmd, + adminReplicateResyncCmd, } var adminReplicateCmd = cli.Command{ diff --git a/cmd/admin-trace.go b/cmd/admin-trace.go index 16b60fb0..fdbea346 100644 --- a/cmd/admin-trace.go +++ b/cmd/admin-trace.go @@ -279,6 +279,7 @@ func tracingOpts(ctx *cli.Context, apis []string) (opts madmin.ServiceTraceOpts, opts.Decommission = true opts.Healing = true opts.BatchReplication = true + opts.ReplicationResync = true return } @@ -309,6 +310,8 @@ func tracingOpts(ctx *cli.Context, apis []string) (opts madmin.ServiceTraceOpts, opts.BatchReplication = true case "rebalance": opts.Rebalance = true + case "replication-resync": + opts.ReplicationResync = true } } return diff --git a/cmd/auto-complete.go b/cmd/auto-complete.go index fab63b0d..650f010d 100644 --- a/cmd/auto-complete.go +++ b/cmd/auto-complete.go @@ -429,11 +429,14 @@ var completeCmds = map[string]complete.Predictor{ "/admin/tier/rm": nil, "/admin/tier/verify": nil, - "/admin/replicate/add": aliasCompleter, - "/admin/replicate/edit": aliasCompleter, - "/admin/replicate/info": aliasCompleter, - "/admin/replicate/status": aliasCompleter, - "/admin/replicate/remove": aliasCompleter, + "/admin/replicate/add": aliasCompleter, + "/admin/replicate/edit": aliasCompleter, + "/admin/replicate/info": aliasCompleter, + "/admin/replicate/status": aliasCompleter, + "/admin/replicate/remove": aliasCompleter, + "/admin/replicate/resync/start": aliasCompleter, + "/admin/replicate/resync/cancel": aliasCompleter, + "/admin/replicate/resync/status": aliasCompleter, "/admin/cluster/bucket/export": aliasCompleter, "/admin/cluster/bucket/import": aliasCompleter,