From 5e8e974a311714f14a08dfb155e60c88f17980cf Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Mon, 13 Mar 2023 09:43:57 -0400 Subject: [PATCH] Purge logs without panicking (#12187) * Purge logs without panicking Signed-off-by: Andrew Mason * Purge logs without panicking Signed-off-by: Andrew Mason * try moving the goroutine to the func itself Signed-off-by: Andrew Mason --------- Signed-off-by: Andrew Mason --- go/cmd/query_analyzer/query_analyzer.go | 1 + go/cmd/rulesctl/cmd/main.go | 2 ++ go/cmd/vtadmin/main.go | 2 ++ go/cmd/vtclient/vtclient.go | 2 ++ go/cmd/vtctldclient/command/root.go | 2 ++ go/cmd/zk/zkcmd.go | 1 + go/vt/logutil/purge.go | 9 +++++--- go/vt/servenv/purgelogs.go | 28 ------------------------- go/vt/servenv/servenv.go | 4 ++++ 9 files changed, 20 insertions(+), 31 deletions(-) delete mode 100644 go/vt/servenv/purgelogs.go diff --git a/go/cmd/query_analyzer/query_analyzer.go b/go/cmd/query_analyzer/query_analyzer.go index 087c88fa5f3..2138bde2673 100644 --- a/go/cmd/query_analyzer/query_analyzer.go +++ b/go/cmd/query_analyzer/query_analyzer.go @@ -70,6 +70,7 @@ func main() { acl.RegisterFlags(fs) servenv.RegisterMySQLServerFlags(fs) _flag.Parse(fs) + logutil.PurgeLogs() for _, filename := range _flag.Args() { fmt.Printf("processing: %s\n", filename) if err := processFile(filename); err != nil { diff --git a/go/cmd/rulesctl/cmd/main.go b/go/cmd/rulesctl/cmd/main.go index d100b8c8680..1b5ab30acfd 100644 --- a/go/cmd/rulesctl/cmd/main.go +++ b/go/cmd/rulesctl/cmd/main.go @@ -4,6 +4,7 @@ import ( "github.com/spf13/cobra" _flag "vitess.io/vitess/go/internal/flag" + "vitess.io/vitess/go/vt/logutil" ) var configFile string @@ -14,6 +15,7 @@ func Main() *cobra.Command { Args: cobra.NoArgs, PreRun: func(cmd *cobra.Command, args []string) { _flag.TrickGlog() + logutil.PurgeLogs() }, Run: func(cmd *cobra.Command, _ []string) { cmd.Help() }, } diff --git a/go/cmd/vtadmin/main.go b/go/cmd/vtadmin/main.go index 4c1b3666ebd..210e2edb918 100644 --- a/go/cmd/vtadmin/main.go +++ b/go/cmd/vtadmin/main.go @@ -26,6 +26,7 @@ import ( "vitess.io/vitess/go/trace" "vitess.io/vitess/go/vt/log" + "vitess.io/vitess/go/vt/logutil" "vitess.io/vitess/go/vt/servenv" "vitess.io/vitess/go/vt/vtadmin" "vitess.io/vitess/go/vt/vtadmin/cache" @@ -58,6 +59,7 @@ var ( Use: "vtadmin", PreRun: func(cmd *cobra.Command, args []string) { _flag.TrickGlog() + logutil.PurgeLogs() if opts.EnableTracing || httpOpts.EnableTracing { startTracing(cmd) diff --git a/go/cmd/vtclient/vtclient.go b/go/cmd/vtclient/vtclient.go index 4295766723a..adc060d7737 100644 --- a/go/cmd/vtclient/vtclient.go +++ b/go/cmd/vtclient/vtclient.go @@ -184,6 +184,8 @@ func run() (*results, error) { _flag.Parse(fs) args := _flag.Args() + logutil.PurgeLogs() + if len(args) == 0 { pflag.Usage() return nil, errors.New("no arguments provided. See usage above") diff --git a/go/cmd/vtctldclient/command/root.go b/go/cmd/vtctldclient/command/root.go index ae74ddfb5e6..9e59276993c 100644 --- a/go/cmd/vtctldclient/command/root.go +++ b/go/cmd/vtctldclient/command/root.go @@ -27,6 +27,7 @@ import ( "github.com/spf13/cobra" "vitess.io/vitess/go/trace" + "vitess.io/vitess/go/vt/logutil" "vitess.io/vitess/go/vt/servenv" "vitess.io/vitess/go/vt/vtctl/vtctldclient" ) @@ -50,6 +51,7 @@ var ( // We use PersistentPreRun to set up the tracer, grpc client, and // command context for every command. PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) { + logutil.PurgeLogs() traceCloser = trace.StartTracing("vtctldclient") client, err = getClientForCommand(cmd) ctx := cmd.Context() diff --git a/go/cmd/zk/zkcmd.go b/go/cmd/zk/zkcmd.go index 6b2c4a3840f..5cc736ea959 100644 --- a/go/cmd/zk/zkcmd.go +++ b/go/cmd/zk/zkcmd.go @@ -147,6 +147,7 @@ func main() { } pflag.Parse() + logutil.PurgeLogs() if help || pflag.Arg(0) == "help" { pflag.Usage() diff --git a/go/vt/logutil/purge.go b/go/vt/logutil/purge.go index 20f028d7187..8d85e15c5c9 100644 --- a/go/vt/logutil/purge.go +++ b/go/vt/logutil/purge.go @@ -127,7 +127,10 @@ func PurgeLogs() { logDir := f.Value.String() program := filepath.Base(os.Args[0]) ticker := time.NewTicker(purgeLogsInterval) - for range ticker.C { - purgeLogsOnce(time.Now(), logDir, program, keepLogsByCtime, keepLogsByMtime) - } + + go func() { + for range ticker.C { + purgeLogsOnce(time.Now(), logDir, program, keepLogsByCtime, keepLogsByMtime) + } + }() } diff --git a/go/vt/servenv/purgelogs.go b/go/vt/servenv/purgelogs.go deleted file mode 100644 index e5edc9e7be2..00000000000 --- a/go/vt/servenv/purgelogs.go +++ /dev/null @@ -1,28 +0,0 @@ -/* -Copyright 2019 The Vitess Authors. - -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 servenv - -import ( - "vitess.io/vitess/go/vt/logutil" -) - -func init() { - OnInit(func() { - go logutil.PurgeLogs() - }) - -} diff --git a/go/vt/servenv/servenv.go b/go/vt/servenv/servenv.go index d8dbe510c8d..9f3d63f1a02 100644 --- a/go/vt/servenv/servenv.go +++ b/go/vt/servenv/servenv.go @@ -335,6 +335,8 @@ func ParseFlags(cmd string) { _flag.Usage() log.Exitf("%s doesn't take any positional arguments, got '%s'", cmd, strings.Join(args, " ")) } + + logutil.PurgeLogs() } // GetFlagSetFor returns the flag set for a given command. @@ -364,6 +366,8 @@ func ParseFlagsWithArgs(cmd string) []string { log.Exitf("%s expected at least one positional argument", cmd) } + logutil.PurgeLogs() + return args }