Skip to content

Commit

Permalink
Fix concurrent read write error for entity store (#1484)
Browse files Browse the repository at this point in the history
  • Loading branch information
sky333999 authored Jan 7, 2025
1 parent dfd2070 commit 07361c7
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions extension/entitystore/serviceprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type serviceprovider struct {
done chan struct{}
logger *zap.Logger
mutex sync.RWMutex
logMutex sync.RWMutex
// logFiles stores the service attributes that were configured for log files in CloudWatch Agent configuration.
// Example:
// "/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log": {ServiceName: "cloudwatch-agent"}
Expand Down Expand Up @@ -106,6 +107,8 @@ func (s *serviceprovider) getAutoScalingGroup() string {
// addEntryForLogFile adds an association between a log file glob and a service attribute, as configured in the
// CloudWatch Agent config.
func (s *serviceprovider) addEntryForLogFile(logFileGlob LogFileGlob, serviceAttr ServiceAttribute) {
s.logMutex.Lock()
defer s.logMutex.Unlock()
if s.logFiles == nil {
s.logFiles = make(map[LogFileGlob]ServiceAttribute)
}
Expand All @@ -115,6 +118,8 @@ func (s *serviceprovider) addEntryForLogFile(logFileGlob LogFileGlob, serviceAtt
// addEntryForLogGroup adds an association between a log group name and a service attribute, as observed from incoming
// telemetry received by CloudWatch Agent.
func (s *serviceprovider) addEntryForLogGroup(logGroupName LogGroupName, serviceAttr ServiceAttribute) {
s.logMutex.Lock()
defer s.logMutex.Unlock()
if s.logGroups == nil {
s.logGroups = make(map[LogGroupName]ServiceAttribute)
}
Expand Down Expand Up @@ -178,15 +183,17 @@ func (s *serviceprovider) serviceAttributeForLogGroup(logGroup LogGroupName) Ser
if logGroup == "" || s.logGroups == nil {
return ServiceAttribute{}
}

s.logMutex.RLock()
defer s.logMutex.RUnlock()
return s.logGroups[logGroup]
}

func (s *serviceprovider) serviceAttributeForLogFile(logFile LogFileGlob) ServiceAttribute {
if logFile == "" || s.logFiles == nil {
return ServiceAttribute{}
}

s.logMutex.RLock()
defer s.logMutex.RUnlock()
return s.logFiles[logFile]
}

Expand Down

0 comments on commit 07361c7

Please sign in to comment.