Skip to content
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

Merged

Conversation

Mixaster995
Copy link
Contributor

Signed-off-by: Mikhail Avramenko avramenkomihail15@gmail.com

Description

Removed unnecessary fields from context to prevent data races in logs.

Issue

fixes #419

@@ -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"})
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, done

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"})
Copy link
Member

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...

Copy link
Member

@denis-tingaikin denis-tingaikin Nov 22, 2021

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)

Copy link
Contributor Author

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

@edwarnicke
Copy link
Member

Why does trace need its on context?

@Mixaster995 Mixaster995 self-assigned this Nov 23, 2021
@Mixaster995
Copy link
Contributor Author

@edwarnicke you right, it is unnecessary, added some refactoring

main.go Outdated
Comment on lines 51 to 52
traceCtx := log.WithFields(ctx, map[string]interface{}{"cmd": os.Args[:1]})
traceLogger, finish := utils.GetTraceLogger(traceCtx, "nsmgr")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)

Copy link
Member

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

Copy link
Contributor Author

@Mixaster995 Mixaster995 Nov 23, 2021

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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main.go Outdated

// ********************************************************************************
// Debug self if necessary
// ********************************************************************************
if err := debug.Self(); err != nil {
log.FromContext(ctx).Infof("%s", err)
traceLogger.Infof("%s", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
traceLogger.Fatalf("error processing cfg from env: %+v", err)
logger.Fatalf("error processing cfg from env: %+v", err)

m := &manager{
configuration: configuration,
traceLogger: traceLogger,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
traceLogger: traceLogger,
logger: logger,

@@ -87,20 +87,21 @@ func (m *manager) initSecurity() (err error) {
func RunNsmgr(ctx context.Context, configuration *config.Config) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func RunNsmgr(ctx context.Context, configuration *config.Config) error {
func RunNsmgr(ctx context.Context, logger log.Logger, configuration *config.Config) error {

@@ -54,6 +53,7 @@ const (

type manager struct {
ctx context.Context
traceLogger log.Logger
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
traceLogger log.Logger
logger log.Logger

@@ -54,6 +53,7 @@ const (

type manager struct {
Copy link
Member

@denis-tingaikin denis-tingaikin Nov 23, 2021

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?

Copy link
Member

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?

Copy link
Member

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
traceLogger.Fatalf("error executing rootCmd: %v", err)
logger.Fatalf("error executing rootCmd: %v", err)

@edwarnicke
Copy link
Member

I'm a bit confused how this connects to concurrent map panics in #419, could you explain?

@denis-tingaikin
Copy link
Member

denis-tingaikin commented Nov 23, 2021

@edwarnicke

Sure. We are setting a setlogoptions at the top of the application.

So it conflicting with each connection here
https://github.com/networkservicemesh/sdk/blob/main/pkg/networkservice/common/setlogoption/server.go#L47

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.

@edwarnicke
Copy link
Member

Why does our logging involve a map with this risk? Does vanilla logrus have these issues?

@denis-tingaikin
Copy link
Member

denis-tingaikin commented Nov 23, 2021

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.

@edwarnicke
Copy link
Member

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>
@Mixaster995
Copy link
Contributor Author

@denis-tingaikin made requested changes - please, take a look

}
logrus.SetLevel(level)

// Startup is finished
finish()
Copy link
Member

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()?

Copy link
Contributor Author

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?

Copy link
Member

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.

Copy link
Contributor Author

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>
@denis-tingaikin denis-tingaikin merged commit 9c48435 into networkservicemesh:main Nov 25, 2021
nsmbot pushed a commit to networkservicemesh/deployments-k8s that referenced this pull request Nov 25, 2021
…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>
@Mixaster995 Mixaster995 deleted the fix/logs-datarace branch November 25, 2021 09:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NSMgr container restarts due to concurrent map writes
3 participants