From 5d3507582ac317c9636d5a41cebdf7ff6ac88bfc Mon Sep 17 00:00:00 2001 From: Dan Moran Date: Mon, 24 May 2021 11:13:56 -0400 Subject: [PATCH 1/4] fix: add work-around for logging bug when upgrading on windows --- cmd/influxd/upgrade/logging_unix.go | 24 +++++++++++++++ cmd/influxd/upgrade/logging_windows.go | 41 ++++++++++++++++++++++++++ cmd/influxd/upgrade/upgrade.go | 21 ------------- 3 files changed, 65 insertions(+), 21 deletions(-) create mode 100644 cmd/influxd/upgrade/logging_unix.go create mode 100644 cmd/influxd/upgrade/logging_windows.go diff --git a/cmd/influxd/upgrade/logging_unix.go b/cmd/influxd/upgrade/logging_unix.go new file mode 100644 index 00000000000..5b57ebc7eaf --- /dev/null +++ b/cmd/influxd/upgrade/logging_unix.go @@ -0,0 +1,24 @@ +package upgrade + +import "go.uber.org/zap" + +func buildLogger(options *logOptions, verbose bool) (*zap.Logger, error) { + config := zap.NewProductionConfig() + + config.Level = zap.NewAtomicLevelAt(options.logLevel) + if verbose { + config.Level.SetLevel(zap.DebugLevel) + } + + config.OutputPaths = append(config.OutputPaths, options.logPath) + config.ErrorOutputPaths = append(config.ErrorOutputPaths, options.logPath) + + log, err := config.Build() + if err != nil { + return nil, err + } + if verbose { + log.Warn("--verbose is deprecated, use --log-level=debug instead") + } + return log, nil +} diff --git a/cmd/influxd/upgrade/logging_windows.go b/cmd/influxd/upgrade/logging_windows.go new file mode 100644 index 00000000000..05a1f789364 --- /dev/null +++ b/cmd/influxd/upgrade/logging_windows.go @@ -0,0 +1,41 @@ +package upgrade + +import ( + "net/url" + "os" + "path/filepath" + + "go.uber.org/zap" +) + +func init() { + // NOTE: Work around a bug in zap's handling of absolute paths on Windows. + // See https://github.com/uber-go/zap/issues/621 + newWinFileSink := func(u *url.URL) (zap.Sink, error) { + // Remove leading slash left by url.Parse() + return os.OpenFile(u.Path[1:], os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644) + } + zap.RegisterSink("winfile", newWinFileSink) +} + +func buildLogger(options *logOptions, verbose bool) (*zap.Logger, error) { + config := zap.NewProductionConfig() + + config.Level = zap.NewAtomicLevelAt(options.logLevel) + if verbose { + config.Level.SetLevel(zap.DebugLevel) + } + + logPath := "winfile:///" + filepath.Abs(options.logPath) + config.OutputPaths = append(config.OutputPaths, logPath) + config.ErrorOutputPaths = append(config.ErrorOutputPaths, logPath) + + log, err := config.Build() + if err != nil { + return nil, err + } + if verbose { + log.Warn("--verbose is deprecated, use --log-level=debug instead") + } + return log, nil +} diff --git a/cmd/influxd/upgrade/upgrade.go b/cmd/influxd/upgrade/upgrade.go index 396ced0bc6d..6d8eeaa9bd6 100644 --- a/cmd/influxd/upgrade/upgrade.go +++ b/cmd/influxd/upgrade/upgrade.go @@ -326,27 +326,6 @@ func (i *influxDBv2) close() error { return nil } -func buildLogger(options *logOptions, verbose bool) (*zap.Logger, error) { - config := zap.NewProductionConfig() - - config.Level = zap.NewAtomicLevelAt(options.logLevel) - if verbose { - config.Level.SetLevel(zap.DebugLevel) - } - - config.OutputPaths = append(config.OutputPaths, options.logPath) - config.ErrorOutputPaths = append(config.ErrorOutputPaths, options.logPath) - - log, err := config.Build() - if err != nil { - return nil, err - } - if verbose { - log.Warn("--verbose is deprecated, use --log-level=debug instead") - } - return log, nil -} - func runUpgradeE(ctx context.Context, ui *input.UI, options *options, log *zap.Logger) error { if options.source.configFile != "" && options.source.dbDir != "" { return errors.New("only one of --v1-dir or --config-file may be specified") From 72a44eb7cc16060562a7f36c6954a678463095f3 Mon Sep 17 00:00:00 2001 From: Dan Moran Date: Mon, 24 May 2021 11:43:32 -0400 Subject: [PATCH 2/4] fix: add build tag to prevent double-definition on windows --- cmd/influxd/upgrade/logging_unix.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cmd/influxd/upgrade/logging_unix.go b/cmd/influxd/upgrade/logging_unix.go index 5b57ebc7eaf..7686eb03a7b 100644 --- a/cmd/influxd/upgrade/logging_unix.go +++ b/cmd/influxd/upgrade/logging_unix.go @@ -1,3 +1,5 @@ +// +build !windows + package upgrade import "go.uber.org/zap" From a1d1e9865c21fc02998b07a720689021de4881cc Mon Sep 17 00:00:00 2001 From: Dan Moran Date: Mon, 24 May 2021 12:33:59 -0400 Subject: [PATCH 3/4] fix: handle err returned from filepath.Abs --- cmd/influxd/upgrade/logging_windows.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cmd/influxd/upgrade/logging_windows.go b/cmd/influxd/upgrade/logging_windows.go index 05a1f789364..1ce149f18cf 100644 --- a/cmd/influxd/upgrade/logging_windows.go +++ b/cmd/influxd/upgrade/logging_windows.go @@ -26,7 +26,12 @@ func buildLogger(options *logOptions, verbose bool) (*zap.Logger, error) { config.Level.SetLevel(zap.DebugLevel) } - logPath := "winfile:///" + filepath.Abs(options.logPath) + logPath, err := filepath.Abs(options.logPath) + if err != nil { + return nil, err + } + logPath = "winfile:///" + logPath + config.OutputPaths = append(config.OutputPaths, logPath) config.ErrorOutputPaths = append(config.ErrorOutputPaths, logPath) From c8be66447c58c319bf9a138ef1d00096221be6f9 Mon Sep 17 00:00:00 2001 From: Dan Moran Date: Mon, 24 May 2021 14:25:39 -0400 Subject: [PATCH 4/4] refactor: DRY up fix --- cmd/influxd/upgrade/logging_unix.go | 24 +++-------------- cmd/influxd/upgrade/logging_windows.go | 36 +++++++------------------- cmd/influxd/upgrade/upgrade.go | 25 ++++++++++++++++++ 3 files changed, 38 insertions(+), 47 deletions(-) diff --git a/cmd/influxd/upgrade/logging_unix.go b/cmd/influxd/upgrade/logging_unix.go index 7686eb03a7b..aca3ecb1cca 100644 --- a/cmd/influxd/upgrade/logging_unix.go +++ b/cmd/influxd/upgrade/logging_unix.go @@ -2,25 +2,7 @@ package upgrade -import "go.uber.org/zap" - -func buildLogger(options *logOptions, verbose bool) (*zap.Logger, error) { - config := zap.NewProductionConfig() - - config.Level = zap.NewAtomicLevelAt(options.logLevel) - if verbose { - config.Level.SetLevel(zap.DebugLevel) - } - - config.OutputPaths = append(config.OutputPaths, options.logPath) - config.ErrorOutputPaths = append(config.ErrorOutputPaths, options.logPath) - - log, err := config.Build() - if err != nil { - return nil, err - } - if verbose { - log.Warn("--verbose is deprecated, use --log-level=debug instead") - } - return log, nil +// Zap only has problems with Windows paths, so this is a no-op on Unix systems. +func (o *logOptions) zapSafeLogPath() (string, error) { + return o.logPath, nil } diff --git a/cmd/influxd/upgrade/logging_windows.go b/cmd/influxd/upgrade/logging_windows.go index 1ce149f18cf..69730c1795b 100644 --- a/cmd/influxd/upgrade/logging_windows.go +++ b/cmd/influxd/upgrade/logging_windows.go @@ -8,39 +8,23 @@ import ( "go.uber.org/zap" ) +// Work around a bug in zap's handling of absolute paths on Windows. +// See https://github.com/uber-go/zap/issues/621 + +const FakeWindowsScheme = "winfile" + func init() { - // NOTE: Work around a bug in zap's handling of absolute paths on Windows. - // See https://github.com/uber-go/zap/issues/621 newWinFileSink := func(u *url.URL) (zap.Sink, error) { // Remove leading slash left by url.Parse() return os.OpenFile(u.Path[1:], os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644) } - zap.RegisterSink("winfile", newWinFileSink) + zap.RegisterSink(FakeWindowsScheme, newWinFileSink) } -func buildLogger(options *logOptions, verbose bool) (*zap.Logger, error) { - config := zap.NewProductionConfig() - - config.Level = zap.NewAtomicLevelAt(options.logLevel) - if verbose { - config.Level.SetLevel(zap.DebugLevel) - } - - logPath, err := filepath.Abs(options.logPath) +func (o *logOptions) zapSafeLogPath() (string, error) { + logPath, err := filepath.Abs(o.logPath) if err != nil { - return nil, err - } - logPath = "winfile:///" + logPath - - config.OutputPaths = append(config.OutputPaths, logPath) - config.ErrorOutputPaths = append(config.ErrorOutputPaths, logPath) - - log, err := config.Build() - if err != nil { - return nil, err - } - if verbose { - log.Warn("--verbose is deprecated, use --log-level=debug instead") + return "", err } - return log, nil + return FakeWindowsScheme + ":///" + logPath, nil } diff --git a/cmd/influxd/upgrade/upgrade.go b/cmd/influxd/upgrade/upgrade.go index 6d8eeaa9bd6..386106cd759 100644 --- a/cmd/influxd/upgrade/upgrade.go +++ b/cmd/influxd/upgrade/upgrade.go @@ -326,6 +326,31 @@ func (i *influxDBv2) close() error { return nil } +func buildLogger(options *logOptions, verbose bool) (*zap.Logger, error) { + config := zap.NewProductionConfig() + + config.Level = zap.NewAtomicLevelAt(options.logLevel) + if verbose { + config.Level.SetLevel(zap.DebugLevel) + } + logPath, err := options.zapSafeLogPath() + if err != nil { + return nil, err + } + + config.OutputPaths = append(config.OutputPaths, logPath) + config.ErrorOutputPaths = append(config.ErrorOutputPaths, logPath) + + log, err := config.Build() + if err != nil { + return nil, err + } + if verbose { + log.Warn("--verbose is deprecated, use --log-level=debug instead") + } + return log, nil +} + func runUpgradeE(ctx context.Context, ui *input.UI, options *options, log *zap.Logger) error { if options.source.configFile != "" && options.source.dbDir != "" { return errors.New("only one of --v1-dir or --config-file may be specified")