-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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 race condition for harvester Start / Stop in registry #4314
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,65 +11,78 @@ type Registry struct { | |
sync.RWMutex | ||
harvesters map[uuid.UUID]Harvester | ||
wg sync.WaitGroup | ||
done chan struct{} | ||
} | ||
|
||
// NewRegistry creates a new registry object | ||
func NewRegistry() *Registry { | ||
return &Registry{ | ||
harvesters: map[uuid.UUID]Harvester{}, | ||
done: make(chan struct{}), | ||
} | ||
} | ||
|
||
func (hr *Registry) add(h Harvester) { | ||
hr.Lock() | ||
defer hr.Unlock() | ||
hr.harvesters[h.ID()] = h | ||
} | ||
|
||
func (hr *Registry) remove(h Harvester) { | ||
hr.Lock() | ||
defer hr.Unlock() | ||
delete(hr.harvesters, h.ID()) | ||
func (r *Registry) remove(h Harvester) { | ||
r.Lock() | ||
defer r.Unlock() | ||
delete(r.harvesters, h.ID()) | ||
} | ||
|
||
// Stop stops all harvesters in the registry | ||
func (hr *Registry) Stop() { | ||
hr.Lock() | ||
for _, hv := range hr.harvesters { | ||
hr.wg.Add(1) | ||
func (r *Registry) Stop() { | ||
r.Lock() | ||
defer func() { | ||
r.Unlock() | ||
r.WaitForCompletion() | ||
}() | ||
// Makes sure no new harvesters are added during stopping | ||
close(r.done) | ||
|
||
for _, hv := range r.harvesters { | ||
r.wg.Add(1) | ||
go func(h Harvester) { | ||
hr.wg.Done() | ||
r.wg.Done() | ||
h.Stop() | ||
}(hv) | ||
} | ||
hr.Unlock() | ||
hr.WaitForCompletion() | ||
|
||
} | ||
|
||
// WaitForCompletion can be used to wait until all harvesters are stopped | ||
func (hr *Registry) WaitForCompletion() { | ||
hr.wg.Wait() | ||
func (r *Registry) WaitForCompletion() { | ||
r.wg.Wait() | ||
} | ||
|
||
// Start starts the given harvester and add its to the registry | ||
func (hr *Registry) Start(h Harvester) { | ||
func (r *Registry) Start(h Harvester) { | ||
|
||
// Make sure stop is not called during starting a harvester | ||
r.Lock() | ||
defer r.Unlock() | ||
|
||
// Make sure no new harvesters are started after stop was called | ||
select { | ||
case <-r.done: | ||
return | ||
default: | ||
} | ||
|
||
hr.wg.Add(1) | ||
hr.add(h) | ||
r.wg.Add(1) | ||
r.harvesters[h.ID()] = h | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are no longer using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed the method. |
||
|
||
go func() { | ||
defer func() { | ||
hr.remove(h) | ||
hr.wg.Done() | ||
r.remove(h) | ||
r.wg.Done() | ||
}() | ||
// Starts harvester and picks the right type. In case type is not set, set it to default (log) | ||
h.Start() | ||
}() | ||
} | ||
|
||
// Len returns the current number of harvesters in the registry | ||
func (hr *Registry) Len() uint64 { | ||
hr.RLock() | ||
defer hr.RUnlock() | ||
return uint64(len(hr.harvesters)) | ||
func (r *Registry) Len() uint64 { | ||
r.RLock() | ||
defer r.RUnlock() | ||
return uint64(len(r.harvesters)) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Having that you already have a lock in place
done
could be just a boolean isn't it? not against this approach thoughThere 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.
Agree, it could be also a bool. I would like to keep it that way as it has become quite a common pattern across our code base.