Skip to content

Commit

Permalink
attempt to fix data race
Browse files Browse the repository at this point in the history
  • Loading branch information
Janelle Law committed Oct 18, 2023
1 parent 7abb0aa commit eebb5c7
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 10 deletions.
13 changes: 10 additions & 3 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ var (
pluginGroupPrefix = "plugin"
NoRepeatInterval = 3600 * time.Hour // arbitrarily long time to denote one-time sampling
logFs afero.Fs
logFileName = "opni-logs"
DefaultTimeFormat = "2006 Jan 02 15:04:05"
errKey = "err"
)
Expand Down Expand Up @@ -236,17 +235,25 @@ func (s *sampler) onDroppedHook(_ context.Context, r slog.Record) {
}

func ReadOnlyFile(clusterID string) afero.File {
f, err := logFs.OpenFile(logFileName+clusterID, os.O_RDONLY|os.O_CREATE, 0666)
f, err := logFs.OpenFile(clusterID, os.O_RDONLY|os.O_CREATE, 0666)
if err != nil {
panic(err)
}
return f
}

func WriteOnlyFile(clusterID string) afero.File {
f, err := logFs.OpenFile(logFileName+clusterID, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
f, err := logFs.OpenFile(clusterID, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
panic(err)
}
return f
}

func GetFileIfExists(clusterID string) afero.File {
f, err := logFs.Open(clusterID)
if err != nil {
return nil
}
return f
}
11 changes: 8 additions & 3 deletions pkg/logger/plugin_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
)

var sharedPluginWriter = &pluginWriter{
w: &DefaultWriter,
mu: &sync.Mutex{},
}

Expand All @@ -15,10 +14,16 @@ type pluginWriter struct {
mu *sync.Mutex
}

func SetPluginWriter(agentId string) {
func InitPluginWriter(agentId string) {
sharedPluginWriter.mu.Lock()
defer sharedPluginWriter.mu.Unlock()
f := WriteOnlyFile(agentId)
if sharedPluginWriter.w != nil {
return
}
f := GetFileIfExists(agentId)
if f == nil {
f = WriteOnlyFile(agentId)
}
fileWriter := f.(io.Writer)
sharedPluginWriter.w = &fileWriter
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/alerting/pkg/agent/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ func (p *Plugin) configureLoggers(identityClient controlv1.IdentityClient) {
if err != nil {
p.lg.Error("error fetching node id", logger.Err(err))
}
logger.SetPluginWriter(id.GetId())
logger.InitPluginWriter(id.GetId())
}
2 changes: 1 addition & 1 deletion plugins/logging/pkg/agent/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ func (p *Plugin) configureLoggers(cc grpc.ClientConnInterface) {
if err != nil {
p.logger.Error("error fetching node id", logger.Err(err))
}
logger.SetPluginWriter(id.GetId())
logger.InitPluginWriter(id.GetId())
}
2 changes: 1 addition & 1 deletion plugins/metrics/pkg/agent/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ func (p *Plugin) configureLoggers(identityClient controlv1.IdentityClient) {
if err != nil {
p.logger.Error("error fetching node id", logger.Err(err))
}
logger.SetPluginWriter(id.GetId())
logger.InitPluginWriter(id.GetId())
}
2 changes: 1 addition & 1 deletion plugins/topology/pkg/topology/agent/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ func (p *Plugin) configureLoggers() {
if err != nil {
p.logger.Error("error fetching node id", logger.Err(err))
}
logger.SetPluginWriter(id.GetId())
logger.InitPluginWriter(id.GetId())
}

0 comments on commit eebb5c7

Please sign in to comment.