wilfred-s commented on code in PR #446:
URL: https://github.com/apache/yunikorn-core/pull/446#discussion_r1017234724


##########
pkg/log/logger_test.go:
##########
@@ -62,8 +63,9 @@ func TestIsDebugEnabled(t *testing.T) {
                Encoding: "console",
        }
        var err error
-       logger, err = zapConfig.Build()
+       logger, err := zapConfig.Build()
        assert.NilError(t, err, "debug level logger create failed")
+       InitializeLogger(logger, &zapConfig)

Review Comment:
   These tests explicitly did not trigger the `once.Do()` to make sure we 
re-try things.



##########
pkg/log/logger.go:
##########
@@ -23,50 +23,63 @@ import (
        "fmt"
        "reflect"
        "sync"
+       "sync/atomic"
 
        "go.uber.org/zap"
        "go.uber.org/zap/zapcore"
 )
 
 var once sync.Once
-var logger *zap.Logger
-var config *zap.Config
+var holderValue atomic.Value

Review Comment:
   We do not need an atomic here. The logger variable is protected by the 
`sync.Once`. The logger internally protects itself. We cannot build a logger 
without a config. Neither must change after creation.
   On read we just need to return the global. There is nothing that warrants 
the overhead of the load and the cast of the atomic value.



##########
pkg/entrypoint/entrypoint.go:
##########
@@ -55,6 +55,10 @@ func StartAllServicesWithLogger(logger *zap.Logger, 
zapConfigs *zap.Config) *Ser
        return StartAllServices()
 }
 
+func InitializeLogger(logger *zap.Logger, zapConfigs *zap.Config) {
+       log.InitializeLogger(logger, zapConfigs)
+}
+

Review Comment:
   This is never called, the `StartAllServicesWithLogger()` is called from the 
k8shim entrypoint we never just want to call this as part of the entry 



##########
pkg/log/logger_test.go:
##########
@@ -119,9 +121,9 @@ func TestInitializeLogger(t *testing.T) {
 
        InitializeLogger(localLogger, &zapConfig)
        assert.Equal(t, Logger(), localLogger)
-       // second initialization should not do anything
+       // second initialization should update
        InitializeLogger(localLogger2, &zapConfig)
-       assert.Equal(t, Logger(), localLogger)
+       assert.Equal(t, Logger(), localLogger2)

Review Comment:
   See comment earlier: this should not change anything as it can break the 
link between the k8shim and the core logger which breaks the ability to update 
the k8shim log level.



##########
pkg/log/logger.go:
##########
@@ -116,14 +126,17 @@ func createConfig() *zap.Config {
 }
 
 func SetLogLevel(newLevel string) error {
+       logger := Logger()
+       config := GetConfig()
+
        oldLevel := config.Level.String()
 
        // noop if the input is the same as what is set
        if newLevel == oldLevel {
                return nil
        }
 
-       logger.Info("Updating log level",
+       Logger().Info("Updating log level",

Review Comment:
   consistency: either use  `Logger()` or use the local var logger throughout 
the function:  line 129, 139 & 147



##########
pkg/log/logger.go:
##########
@@ -23,50 +23,63 @@ import (
        "fmt"
        "reflect"
        "sync"
+       "sync/atomic"
 
        "go.uber.org/zap"
        "go.uber.org/zap/zapcore"
 )
 
 var once sync.Once
-var logger *zap.Logger
-var config *zap.Config
+var holderValue atomic.Value
+
+type loggerHolder struct {
+       logger     *zap.Logger
+       zapConfigs *zap.Config
+}
 
 func Logger() *zap.Logger {
-       once.Do(func() {
-               if logger = zap.L(); isNopLogger(logger) {
-                       // If a global logger is not found, this could be 
either scheduler-core
-                       // is running as a deployment mode, or running with 
another non-go code
-                       // shim. In this case, we need to create our own logger.
-                       // TODO support log options when a global logger is not 
there
-                       config = createConfig()
-                       var err error
-                       logger, err = config.Build()
-                       // this should really not happen so just write to 
stdout and set a Nop logger
-                       if err != nil {
-                               fmt.Printf("Logging disabled, logger init 
failed with error: %v\n", err)
-                               logger = zap.NewNop()
-                       }
+       once.Do(initLogger)
+       return holderValue.Load().(loggerHolder).logger
+}
+
+func GetConfig() *zap.Config {
+       once.Do(initLogger)
+       return holderValue.Load().(loggerHolder).zapConfigs
+}
+
+func initLogger() {
+       var logger *zap.Logger
+       var config *zap.Config
+
+       if logger = zap.L(); isNopLogger(logger) {
+               // If a global logger is not found, this could be either 
scheduler-core
+               // is running as a deployment mode, or running with another 
non-go code
+               // shim. In this case, we need to create our own logger.
+               config = createConfig()
+               var err error
+               logger, err = config.Build()
+               // this should really not happen so just write to stdout and 
set a Nop logger
+               if err != nil {
+                       fmt.Printf("Logging disabled, logger init failed with 
error: %v\n", err)
+                       logger = zap.NewNop()
                }
+       }
+       holderValue.Store(loggerHolder{
+               logger:     logger,
+               zapConfigs: config,
        })
-
-       return logger
 }
 
 func InitializeLogger(log *zap.Logger, zapConfig *zap.Config) {
-       once.Do(func() {
-               logger = log
-               config = zapConfig
-               logger.Info("Using an already initialized logger")
+       once.Do(initLogger)

Review Comment:
   This changes the init for the logger from the shim into the core. The 
`InitializeLogger()` passes in a pre-build config from the k8shim and makes 
sure that the shim and the core run with *the exact same* config object. 
Without that changing the log level via the rest call is only going to change 
the core log level.
   
   It also does the work twice in this case: we have a logger and config we 
just want to set it. We can change that in a later jira to decouple things but 
not as part of this change.



##########
pkg/scheduler/context.go:
##########
@@ -313,14 +333,14 @@ func (cc *ClusterContext) UpdateSchedulerConfig(conf 
*configs.SchedulerConfig) e
 // Locked version of the configuration update called outside of event system.
 // Updates the current config via the config loader.
 // Used in test only, normal updates use the internal call and the webservice 
must use the UpdateSchedulerConfig

Review Comment:
   NIT: Can we update this comment top remove the web service reference?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to