From ab2bce93eae17cd9ddeb8a5d63763df2ecd948da Mon Sep 17 00:00:00 2001 From: Shireesh Anjal <355479+anjalshireesh@users.noreply.github.com> Date: Mon, 14 Feb 2022 05:38:52 +0530 Subject: [PATCH] Move `admin profile` to `support profile` (#3972) As part of reorganization of support related commands --- cmd/admin-profile-start.go | 102 +++----------------------- cmd/admin-profile-stop.go | 114 +++-------------------------- cmd/admin-profile.go | 4 +- cmd/auto-complete.go | 10 +-- cmd/support-profile-start.go | 124 ++++++++++++++++++++++++++++++++ cmd/support-profile-stop.go | 135 +++++++++++++++++++++++++++++++++++ cmd/support-profile.go | 44 ++++++++++++ cmd/support.go | 1 + 8 files changed, 332 insertions(+), 202 deletions(-) create mode 100644 cmd/support-profile-start.go create mode 100644 cmd/support-profile-stop.go create mode 100644 cmd/support-profile.go diff --git a/cmd/admin-profile-start.go b/cmd/admin-profile-start.go index c1785a6e..e385ce61 100644 --- a/cmd/admin-profile-start.go +++ b/cmd/admin-profile-start.go @@ -18,107 +18,23 @@ package cmd import ( - "strings" - "github.com/minio/cli" - "github.com/minio/madmin-go" - "github.com/minio/mc/pkg/probe" - "github.com/minio/minio-go/v7/pkg/set" "github.com/minio/pkg/console" ) -var adminProfileStartFlags = []cli.Flag{ - cli.StringFlag{ - Name: "type", - Usage: "start profiler type, possible values are 'cpu', 'mem', 'block', 'mutex', 'trace', 'threads' and 'goroutines'", - Value: "cpu,mem,block,goroutines", - }, -} - var adminProfileStartCmd = cli.Command{ - Name: "start", - Usage: "start recording profile data", - Action: mainAdminProfileStart, - OnUsageError: onUsageError, - Before: setGlobalsFromContext, - Flags: append(adminProfileStartFlags, globalFlags...), - HideHelpCommand: true, - CustomHelpTemplate: `NAME: - {{.HelpName}} - {{.Usage}} - -USAGE: - {{.HelpName}} [FLAGS] TARGET - -FLAGS: - {{range .VisibleFlags}}{{.}} - {{end}} -EXAMPLES: - 1. Start CPU profiling only - {{.Prompt}} {{.HelpName}} --type cpu myminio/ - - 2. Start CPU, Memory and Block profiling concurrently - {{.Prompt}} {{.HelpName}} --type cpu,mem,block myminio/ -`, -} - -func checkAdminProfileStartSyntax(ctx *cli.Context) { - // Check flags combinations - if len(ctx.Args()) != 1 { - cli.ShowCommandHelpAndExit(ctx, "start", 1) // last argument is exit code - } - - s := set.NewStringSet() - supportedProfilerTypes := []madmin.ProfilerType{ - madmin.ProfilerCPU, - madmin.ProfilerMEM, - madmin.ProfilerBlock, - madmin.ProfilerMutex, - madmin.ProfilerTrace, - madmin.ProfilerThreads, - madmin.ProfilerGoroutines, - } - for _, profilerType := range supportedProfilerTypes { - s.Add(string(profilerType)) - } - // Check if the provided profiler type is known and supported - supportedProfiler := false - profilers := strings.Split(strings.ToLower(ctx.String("type")), ",") - for _, profiler := range profilers { - if profiler != "" { - if s.Contains(profiler) { - supportedProfiler = true - break - } - } - } - if !supportedProfiler { - fatalIf(errDummy().Trace(ctx.String("type")), - "Profiler type unrecognized. Possible values are: %v.", supportedProfilerTypes) - } + Name: "start", + Usage: "start recording profile data", + Action: mainAdminProfileStart, + OnUsageError: onUsageError, + Before: setGlobalsFromContext, + HideHelpCommand: true, + Hidden: true, + CustomHelpTemplate: "Please use 'mc support profile start'", } // mainAdminProfileStart - the entry function of profile command func mainAdminProfileStart(ctx *cli.Context) error { - // Check for command syntax - checkAdminProfileStartSyntax(ctx) - - // Get the alias parameter from cli - args := ctx.Args() - aliasedURL := args.Get(0) - - profilers := ctx.String("type") - - // Create a new MinIO Admin Client - client, err := newAdminClient(aliasedURL) - if err != nil { - fatalIf(err.Trace(aliasedURL), "Unable to initialize admin client.") - return nil - } - - // Start profile - _, cmdErr := client.StartProfiling(globalContext, madmin.ProfilerType(profilers)) - fatalIf(probe.NewError(cmdErr), "Unable to start profile.") - - console.Infoln("Profile data successfully started.") + console.Infoln("Please use 'mc support profile start'") return nil } diff --git a/cmd/admin-profile-stop.go b/cmd/admin-profile-stop.go index 01c90186..59f10e4b 100644 --- a/cmd/admin-profile-stop.go +++ b/cmd/admin-profile-stop.go @@ -18,118 +18,24 @@ package cmd import ( - "io" - "io/ioutil" - "os" - "time" - "github.com/minio/cli" - "github.com/minio/mc/pkg/probe" "github.com/minio/pkg/console" ) var adminProfileStopCmd = cli.Command{ - Name: "stop", - Usage: "stop and download profile data", - Action: mainAdminProfileStop, - OnUsageError: onUsageError, - Before: setGlobalsFromContext, - Flags: globalFlags, - HideHelpCommand: true, - CustomHelpTemplate: `NAME: - {{.HelpName}} - {{.Usage}} - -USAGE: - {{.HelpName}} [FLAGS] TARGET - -FLAGS: - {{range .VisibleFlags}}{{.}} - {{end}} -EXAMPLES: - 2. Download latest profile data in the current directory - {{.Prompt}} {{.HelpName}} myminio/ -`, -} - -func checkAdminProfileStopSyntax(ctx *cli.Context) { - if len(ctx.Args()) != 1 { - cli.ShowCommandHelpAndExit(ctx, "stop", 1) // last argument is exit code - } -} - -// moveFile - os.Rename cannot handle cross device renames, in our situation -// it is possible that /tmp is mounted from a separate partition and current -// working directory is a different partition. To allow all situations to -// be handled appropriately use this function instead of os.Rename() -func moveFile(sourcePath, destPath string) error { - inputFile, err := os.Open(sourcePath) - if err != nil { - return err - } - - outputFile, err := os.Create(destPath) - if err != nil { - inputFile.Close() - return err - } - defer outputFile.Close() - - _, err = io.Copy(outputFile, inputFile) - inputFile.Close() - if err != nil { - return err - } - - // The copy was successful, so now delete the original file - return os.Remove(sourcePath) + Name: "stop", + Usage: "stop and download profile data", + Action: mainAdminProfileStop, + OnUsageError: onUsageError, + Before: setGlobalsFromContext, + Flags: globalFlags, + HideHelpCommand: true, + Hidden: true, + CustomHelpTemplate: "Please use 'mc support profile stop'", } // mainAdminProfileStop - the entry function of profile stop command func mainAdminProfileStop(ctx *cli.Context) error { - // Check for command syntax - checkAdminProfileStopSyntax(ctx) - - // Get the alias parameter from cli - args := ctx.Args() - aliasedURL := args.Get(0) - - // Create a new MinIO Admin Client - client, err := newAdminClient(aliasedURL) - if err != nil { - fatalIf(err.Trace(aliasedURL), "Unable to initialize admin client.") - return nil - } - - // Create profile zip file - tmpFile, e := ioutil.TempFile("", "mc-profile-") - fatalIf(probe.NewError(e), "Unable to download profile data.") - - // Ask for profile data, which will come compressed with zip format - zippedData, adminErr := client.DownloadProfilingData(globalContext) - fatalIf(probe.NewError(adminErr), "Unable to download profile data.") - - // Copy zip content to target download file - _, e = io.Copy(tmpFile, zippedData) - fatalIf(probe.NewError(e), "Unable to download profile data.") - - // Close everything - zippedData.Close() - tmpFile.Close() - - downloadPath := "profile.zip" - - fi, e := os.Stat(downloadPath) - if e == nil && !fi.IsDir() { - e = moveFile(downloadPath, downloadPath+"."+time.Now().Format(dateTimeFormatFilename)) - fatalIf(probe.NewError(e), "Unable to create a backup of profile.zip") - } else { - if !os.IsNotExist(e) { - fatal(probe.NewError(e), "Unable to download profile data.") - } - } - - fatalIf(probe.NewError(moveFile(tmpFile.Name(), downloadPath)), "Unable to download profile data.") - - console.Infof("Profile data successfully downloaded as %s\n", downloadPath) + console.Infoln("Please use 'mc support profile stop'") return nil } diff --git a/cmd/admin-profile.go b/cmd/admin-profile.go index 8d78b9e2..59292e2a 100644 --- a/cmd/admin-profile.go +++ b/cmd/admin-profile.go @@ -19,6 +19,7 @@ package cmd import ( "github.com/minio/cli" + "github.com/minio/pkg/console" ) var adminProfileSubcommands = []cli.Command{ @@ -35,10 +36,11 @@ var adminProfileCmd = cli.Command{ Flags: globalFlags, Subcommands: adminProfileSubcommands, HideHelpCommand: true, + Hidden: true, } // mainAdminProfile is the handle for "mc admin profile" command. func mainAdminProfile(ctx *cli.Context) error { - commandNotFound(ctx, adminProfileSubcommands) + console.Infoln("Please use 'mc support profile'") return nil } diff --git a/cmd/auto-complete.go b/cmd/auto-complete.go index 6f3ccd88..2618f5fb 100644 --- a/cmd/auto-complete.go +++ b/cmd/auto-complete.go @@ -400,10 +400,12 @@ var completeCmds = map[string]complete.Predictor{ "/alias/remove": aliasCompleter, "/alias/import": nil, - "/support/callhome/set": aliasCompleter, - "/support/register": aliasCompleter, - "/support/diag": aliasCompleter, - "/support/inspect": aliasCompleter, + "/support/callhome/set": aliasCompleter, + "/support/register": aliasCompleter, + "/support/diag": aliasCompleter, + "/support/profile/start": aliasCompleter, + "/support/profile/stop": aliasCompleter, + "/support/inspect": aliasCompleter, "/update": nil, } diff --git a/cmd/support-profile-start.go b/cmd/support-profile-start.go new file mode 100644 index 00000000..c619a1f9 --- /dev/null +++ b/cmd/support-profile-start.go @@ -0,0 +1,124 @@ +// Copyright (c) 2015-2021 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 ( + "strings" + + "github.com/minio/cli" + "github.com/minio/madmin-go" + "github.com/minio/mc/pkg/probe" + "github.com/minio/minio-go/v7/pkg/set" + "github.com/minio/pkg/console" +) + +var supportProfileStartFlags = []cli.Flag{ + cli.StringFlag{ + Name: "type", + Usage: "start profiler type, possible values are 'cpu', 'mem', 'block', 'mutex', 'trace', 'threads' and 'goroutines'", + Value: "cpu,mem,block,goroutines", + }, +} + +var supportProfileStartCmd = cli.Command{ + Name: "start", + Usage: "start recording profile data", + Action: mainSupportProfileStart, + OnUsageError: onUsageError, + Before: setGlobalsFromContext, + Flags: append(supportProfileStartFlags, globalFlags...), + HideHelpCommand: true, + CustomHelpTemplate: `NAME: + {{.HelpName}} - {{.Usage}} + +USAGE: + {{.HelpName}} [FLAGS] TARGET + +FLAGS: + {{range .VisibleFlags}}{{.}} + {{end}} +EXAMPLES: + 1. Start CPU profiling only + {{.Prompt}} {{.HelpName}} --type cpu myminio/ + + 2. Start CPU, Memory and Block profiling concurrently + {{.Prompt}} {{.HelpName}} --type cpu,mem,block myminio/ +`, +} + +func checkAdminProfileStartSyntax(ctx *cli.Context) { + // Check flags combinations + if len(ctx.Args()) != 1 { + cli.ShowCommandHelpAndExit(ctx, "start", 1) // last argument is exit code + } + + s := set.NewStringSet() + supportedProfilerTypes := []madmin.ProfilerType{ + madmin.ProfilerCPU, + madmin.ProfilerMEM, + madmin.ProfilerBlock, + madmin.ProfilerMutex, + madmin.ProfilerTrace, + madmin.ProfilerThreads, + madmin.ProfilerGoroutines, + } + for _, profilerType := range supportedProfilerTypes { + s.Add(string(profilerType)) + } + // Check if the provided profiler type is known and supported + supportedProfiler := false + profilers := strings.Split(strings.ToLower(ctx.String("type")), ",") + for _, profiler := range profilers { + if profiler != "" { + if s.Contains(profiler) { + supportedProfiler = true + break + } + } + } + if !supportedProfiler { + fatalIf(errDummy().Trace(ctx.String("type")), + "Profiler type unrecognized. Possible values are: %v.", supportedProfilerTypes) + } +} + +// mainSupportProfileStart - the entry function of profile command +func mainSupportProfileStart(ctx *cli.Context) error { + // Check for command syntax + checkAdminProfileStartSyntax(ctx) + + // Get the alias parameter from cli + args := ctx.Args() + aliasedURL := args.Get(0) + + profilers := ctx.String("type") + + // Create a new MinIO Admin Client + client, err := newAdminClient(aliasedURL) + if err != nil { + fatalIf(err.Trace(aliasedURL), "Unable to initialize admin client.") + return nil + } + + // Start profile + _, cmdErr := client.StartProfiling(globalContext, madmin.ProfilerType(profilers)) + fatalIf(probe.NewError(cmdErr), "Unable to start profile.") + + console.Infoln("Profile data successfully started.") + return nil +} diff --git a/cmd/support-profile-stop.go b/cmd/support-profile-stop.go new file mode 100644 index 00000000..60f43419 --- /dev/null +++ b/cmd/support-profile-stop.go @@ -0,0 +1,135 @@ +// Copyright (c) 2015-2021 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 ( + "io" + "io/ioutil" + "os" + "time" + + "github.com/minio/cli" + "github.com/minio/mc/pkg/probe" + "github.com/minio/pkg/console" +) + +var supportProfileStopCmd = cli.Command{ + Name: "stop", + Usage: "stop and download profile data", + Action: mainSupportProfileStop, + OnUsageError: onUsageError, + Before: setGlobalsFromContext, + Flags: globalFlags, + HideHelpCommand: true, + CustomHelpTemplate: `NAME: + {{.HelpName}} - {{.Usage}} + +USAGE: + {{.HelpName}} [FLAGS] TARGET + +FLAGS: + {{range .VisibleFlags}}{{.}} + {{end}} +EXAMPLES: + 2. Download latest profile data in the current directory + {{.Prompt}} {{.HelpName}} myminio/ +`, +} + +func checkAdminProfileStopSyntax(ctx *cli.Context) { + if len(ctx.Args()) != 1 { + cli.ShowCommandHelpAndExit(ctx, "stop", 1) // last argument is exit code + } +} + +// moveFile - os.Rename cannot handle cross device renames, in our situation +// it is possible that /tmp is mounted from a separate partition and current +// working directory is a different partition. To allow all situations to +// be handled appropriately use this function instead of os.Rename() +func moveFile(sourcePath, destPath string) error { + inputFile, err := os.Open(sourcePath) + if err != nil { + return err + } + + outputFile, err := os.Create(destPath) + if err != nil { + inputFile.Close() + return err + } + defer outputFile.Close() + + _, err = io.Copy(outputFile, inputFile) + inputFile.Close() + if err != nil { + return err + } + + // The copy was successful, so now delete the original file + return os.Remove(sourcePath) +} + +// mainSupportProfileStop - the entry function of profile stop command +func mainSupportProfileStop(ctx *cli.Context) error { + // Check for command syntax + checkAdminProfileStopSyntax(ctx) + + // Get the alias parameter from cli + args := ctx.Args() + aliasedURL := args.Get(0) + + // Create a new MinIO Admin Client + client, err := newAdminClient(aliasedURL) + if err != nil { + fatalIf(err.Trace(aliasedURL), "Unable to initialize admin client.") + return nil + } + + // Create profile zip file + tmpFile, e := ioutil.TempFile("", "mc-profile-") + fatalIf(probe.NewError(e), "Unable to download profile data.") + + // Ask for profile data, which will come compressed with zip format + zippedData, adminErr := client.DownloadProfilingData(globalContext) + fatalIf(probe.NewError(adminErr), "Unable to download profile data.") + + // Copy zip content to target download file + _, e = io.Copy(tmpFile, zippedData) + fatalIf(probe.NewError(e), "Unable to download profile data.") + + // Close everything + zippedData.Close() + tmpFile.Close() + + downloadPath := "profile.zip" + + fi, e := os.Stat(downloadPath) + if e == nil && !fi.IsDir() { + e = moveFile(downloadPath, downloadPath+"."+time.Now().Format(dateTimeFormatFilename)) + fatalIf(probe.NewError(e), "Unable to create a backup of profile.zip") + } else { + if !os.IsNotExist(e) { + fatal(probe.NewError(e), "Unable to download profile data.") + } + } + + fatalIf(probe.NewError(moveFile(tmpFile.Name(), downloadPath)), "Unable to download profile data.") + + console.Infof("Profile data successfully downloaded as %s\n", downloadPath) + return nil +} diff --git a/cmd/support-profile.go b/cmd/support-profile.go new file mode 100644 index 00000000..4315fd02 --- /dev/null +++ b/cmd/support-profile.go @@ -0,0 +1,44 @@ +// Copyright (c) 2015-2021 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 supportProfileSubcommands = []cli.Command{ + supportProfileStartCmd, + supportProfileStopCmd, +} + +var supportProfileCmd = cli.Command{ + Name: "profile", + Usage: "generate profile data for debugging purposes", + Action: mainSupportProfile, + OnUsageError: onUsageError, + Before: setGlobalsFromContext, + Flags: globalFlags, + Subcommands: supportProfileSubcommands, + HideHelpCommand: true, +} + +// mainSupportProfile is the handle for "mc support profile" command. +func mainSupportProfile(ctx *cli.Context) error { + commandNotFound(ctx, supportProfileSubcommands) + return nil +} diff --git a/cmd/support.go b/cmd/support.go index 0a415a53..35c9b4ae 100644 --- a/cmd/support.go +++ b/cmd/support.go @@ -25,6 +25,7 @@ var supportSubcommands = []cli.Command{ supportRegisterCmd, supportDiagCmd, supportCallhomeCmd, + supportProfileCmd, supportInspectCmd, }