-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix to prevent data races #422
Fix to prevent data races #422
Conversation
@@ -96,7 +96,6 @@ func RunNsmgr(ctx context.Context, configuration *config.Config) error { | |||
|
|||
// Context to use for all things started in main | |||
m.ctx, m.cancelFunc = context.WithCancel(ctx) | |||
m.ctx = log.WithFields(m.ctx, map[string]interface{}{"cmd": "Nsmgr"}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is needed for https://github.com/networkservicemesh/cmd-nsmgr/blob/main/internal/manager/manager.go#L164
Please consider using separate contexts for chain nsmgr and for logging in RunNsmgr
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, done
internal/manager/manager.go
Outdated
m.ctx = log.WithFields(m.ctx, map[string]interface{}{"cmd": "Nsmgr"}) | ||
m.ctx = log.WithLog(m.ctx, logruslogger.New(m.ctx)) | ||
logCtx := log.WithLog(m.ctx, logruslogger.New(m.ctx)) | ||
logCtx = log.WithFields(logCtx, map[string]interface{}{"cmd": "Nsmgr"}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logger = logruslogger.New(m.ctx)
logger...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO1: add spanlogger
TODO2: use the logger in each place where we are logging in this code file. (remove all direct logrus using)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, removed logrus, added separate context with spanlogger
Why does trace need its on context? |
@edwarnicke you right, it is unnecessary, added some refactoring |
main.go
Outdated
traceCtx := log.WithFields(ctx, map[string]interface{}{"cmd": os.Args[:1]}) | ||
traceLogger, finish := utils.GetTraceLogger(traceCtx, "nsmgr") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
traceCtx := log.WithFields(ctx, map[string]interface{}{"cmd": os.Args[:1]}) | |
traceLogger, finish := utils.GetTraceLogger(traceCtx, "nsmgr") | |
_, sLogger, _, sFinish := spanlogger.FromContext(ctx, "cmd-nsmgr") | |
defer sFinish() | |
_ , lLogger, lFinish := logruslogger.FromSpan(ctx, span, "cmd-nsmgr") | |
defer lFinish() | |
logger := log.New(sLogger, lLogger) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: I think we need to expose group func from sdk
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@denis-tingaikin should i prepare separate PR to sdk for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Mixaster995 yes
main.go
Outdated
|
||
// ******************************************************************************** | ||
// Debug self if necessary | ||
// ******************************************************************************** | ||
if err := debug.Self(); err != nil { | ||
log.FromContext(ctx).Infof("%s", err) | ||
traceLogger.Infof("%s", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
traceLogger.Infof("%s", err) | |
logger.Infof("%s", err) |
main.go
Outdated
} | ||
|
||
// ******************************************************************************** | ||
// Configure open tracing | ||
// ******************************************************************************** | ||
// Enable Jaeger | ||
log.EnableTracing(true) | ||
jaegerCloser := jaeger.InitJaeger(ctx, "nsmgr") | ||
jaegerCloser := jaeger.InitJaeger(traceCtx, "nsmgr") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
jaegerCloser := jaeger.InitJaeger(traceCtx, "nsmgr") | |
jaegerCloser := jaeger.InitJaeger(ctx, "nsmgr") |
main.go
Outdated
|
||
// Get cfg from environment | ||
cfg := &config.Config{} | ||
if err := envconfig.Usage("nsm", cfg); err != nil { | ||
logrus.Fatal(err) | ||
traceLogger.Fatal(err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
traceLogger.Fatal(err) | |
logger.Fatal(err) |
main.go
Outdated
} | ||
if err := envconfig.Process("nsm", cfg); err != nil { | ||
logrus.Fatalf("error processing cfg from env: %+v", err) | ||
traceLogger.Fatalf("error processing cfg from env: %+v", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
traceLogger.Fatalf("error processing cfg from env: %+v", err) | |
logger.Fatalf("error processing cfg from env: %+v", err) |
internal/manager/manager.go
Outdated
m := &manager{ | ||
configuration: configuration, | ||
traceLogger: traceLogger, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
traceLogger: traceLogger, | |
logger: logger, |
@@ -87,20 +87,21 @@ func (m *manager) initSecurity() (err error) { | |||
func RunNsmgr(ctx context.Context, configuration *config.Config) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
func RunNsmgr(ctx context.Context, configuration *config.Config) error { | |
func RunNsmgr(ctx context.Context, logger log.Logger, configuration *config.Config) error { |
internal/manager/manager.go
Outdated
@@ -54,6 +53,7 @@ const ( | |||
|
|||
type manager struct { | |||
ctx context.Context | |||
traceLogger log.Logger |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
traceLogger log.Logger | |
logger log.Logger |
@@ -54,6 +53,7 @@ const ( | |||
|
|||
type manager struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would be suggested to remove this package. And use all this code inline. This can be done in separate PR.
@edwarnicke Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the generic I agree... not sure whether its necessary before the release or not though. How much work would it be and how much risk would it entail?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you're correct. Let's consider this after release.
main.go
Outdated
return log.WithLog(ctx, sLogger, lLogger), func() { | ||
sFinish() | ||
lFinish() | ||
traceLogger.Fatalf("error executing rootCmd: %v", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
traceLogger.Fatalf("error executing rootCmd: %v", err) | |
logger.Fatalf("error executing rootCmd: %v", err) |
I'm a bit confused how this connects to concurrent map panics in #419, could you explain? |
Sure. We are setting a setlogoptions at the top of the application. So it conflicting with each connection here Imagine a few goroutines that are trying to work with this map. Definitely, it is incorrect using of logging for top level of the application. We should not use the chain logging in the top of applications. |
Why does our logging involve a map with this risk? Does vanilla logrus have these issues? |
No, vanilla logrus doesn't have this issue. Currently, we are incorrect in using our logger from sdk for cmd-nsmgr. |
Got it. I look forward to seeing this land :) |
Signed-off-by: Mikhail Avramenko <avramenkomihail15@gmail.com>
Signed-off-by: Mikhail Avramenko <avramenkomihail15@gmail.com>
Signed-off-by: Mikhail Avramenko <avramenkomihail15@gmail.com>
Signed-off-by: Mikhail Avramenko <avramenkomihail15@gmail.com>
Signed-off-by: Mikhail Avramenko <avramenkomihail15@gmail.com>
4098b9f
to
223cdd5
Compare
@denis-tingaikin made requested changes - please, take a look |
} | ||
logrus.SetLevel(level) | ||
|
||
// Startup is finished | ||
finish() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, this can be a major. Can we run here sFinish()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are passing combined logger further in manager. Isn't calling sFinish()
will close span logger?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we can re-recreate the span logger for the next operation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, done
Signed-off-by: Mikhail Avramenko <avramenkomihail15@gmail.com>
…d-nsmgr@main PR link: networkservicemesh/cmd-nsmgr#422 Commit: 9c48435 Author: Авраменко Михаил Date: 2021-11-25 15:12:52 +0700 Message: - Fix to prevent data races (#422) * removed unnecessary fields from context to prevent data races Signed-off-by: Mikhail Avramenko <avramenkomihail15@gmail.com> * fixed review comments Signed-off-by: Mikhail Avramenko <avramenkomihail15@gmail.com> * added traceCtx to manager Signed-off-by: Mikhail Avramenko <avramenkomihail15@gmail.com> * refactored after review comments Signed-off-by: Mikhail Avramenko <avramenkomihail15@gmail.com> * review refactoring Signed-off-by: Mikhail Avramenko <avramenkomihail15@gmail.com> * added spanlogger finish Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
Signed-off-by: Mikhail Avramenko avramenkomihail15@gmail.com
Description
Removed unnecessary fields from context to prevent data races in logs.
Issue
fixes #419