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

Procect forwarder watchOverrides from race condition #1468

Merged
merged 6 commits into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
* [FEATURE] metrics-generator: support per-tenant processor configuration [#1434](https://github.com/grafana/tempo/pull/1434) (@kvrhdn)
* [FEATURE] Include rollout dashboard [#1456](https://github.com/grafana/tempo/pull/1456) (@zalegrala)
* [ENHANCEMENT] Added the ability to have a per tenant max search duration. [#1421](https://github.com/grafana/tempo/pull/1421) (@joe-elliott)
* [ENHANCEMENT] Azure Backend: Add support for authentication with Managed Identities. [#1457](https://github.com/grafana/tempo/pull/1457) (@joe-elliott)
* [ENHANCEMENT] metrics-generator: expose max_active_series as a metric [#1471](https://github.com/grafana/tempo/pull/1471) (@kvrhdn)
* [ENHANCEMENT] Azure Backend: Add support for authentication with Managed Identities. [#1457](https://github.com/grafana/tempo/pull/1457) (@joe-elliott)
* [BUGFIX] Fix nil pointer panic when the trace by id path errors. [#1441](https://github.com/grafana/tempo/pull/1441) (@joe-elliott)
* [BUGFIX] Update tempo microservices Helm values example which missed the 'enabled' key for thriftHttp. [#1472](https://github.com/grafana/tempo/pull/1472) (@hajowieland)
* [ENHANCEMENT] Azure Backend: Add support for authentication with Managed Identities.
* [BUGFIX] Fix race condition in forwarder overrides loop. [1468](https://github.com/grafana/tempo/pull/1468) (@mapno)

## v1.4.1 / 2022-05-05

Expand Down
52 changes: 40 additions & 12 deletions modules/distributor/forwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,28 +134,56 @@ func (f *forwarder) getQueueManager(tenantID string) (*queueManager, bool) {
// watchOverrides watches the overrides for changes
// and updates the queueManagers accordingly
func (f *forwarder) watchOverrides() {
ticker := time.NewTicker(f.overridesInterval)

for {
select {
case <-time.After(f.overridesInterval):
case <-ticker.C:
f.mutex.Lock()

var (
queueManagersToDelete []*queueManager
queueManagersToAdd []struct {
tenantID string
queueSize, workerCount int
}
)

for tenantID, tm := range f.queueManagers {
queueSize, workerCount := f.getQueueManagerConfig(tenantID)

// if the queue size or worker count has changed, shutdown the queue manager and create a new one
if tm.shouldUpdate(queueSize, workerCount) {
go func() {
// shutdown the queue manager
// this will block until all workers have finished and the queue is drained
if err := tm.shutdown(); err != nil {
level.Error(log.Logger).Log("msg", "error shutting down queue manager", "tenant", tenantID, "err", err)
}
}()
delete(f.queueManagers, tenantID)
f.queueManagers[tenantID] = newQueueManager(tenantID, queueSize, workerCount, f.forwardFunc)
level.Info(log.Logger).Log("msg", "Marking queue manager for update", "tenant", tenantID,
"old_queue_size", tm.queueSize, "new_queue_size", queueSize, "old_worker_count", tm.workerCount, "new_worker_count", workerCount)
queueManagersToDelete = append(queueManagersToDelete, tm)
queueManagersToAdd = append(queueManagersToAdd, struct {
tenantID string
queueSize, workerCount int
}{tenantID: tenantID, queueSize: queueSize, workerCount: workerCount})
}
}

// Spawn a goroutine to asynchronously shut down queue managers
go func() {
for _, qm := range queueManagersToDelete {
// shutdown the queue manager
// this will block until all workers have finished and the queue is drained
level.Info(log.Logger).Log("msg", "Shutting down queue manager", "tenant", qm.tenantID)
if err := qm.shutdown(); err != nil {
level.Error(log.Logger).Log("msg", "error shutting down queue manager", "tenant", qm.tenantID, "err", err)
}
}
}()

// Synchronously update queue managers
for _, qm := range queueManagersToAdd {
level.Info(log.Logger).Log("msg", "Updating queue manager", "tenant", qm.tenantID)
f.queueManagers[qm.tenantID] = newQueueManager(qm.tenantID, qm.queueSize, qm.workerCount, f.forwardFunc)
}

f.mutex.Unlock()
case <-f.shutdown:
ticker.Stop()
return
}
}
Expand Down Expand Up @@ -308,7 +336,7 @@ func (m *queueManager) stopWorkers(ctx context.Context) error {
}

// shouldUpdate returns true if the queue size or worker count (alive or total) has changed
func (m *queueManager) shouldUpdate(numWorkers int, queueSize int) bool {
func (m *queueManager) shouldUpdate(queueSize, numWorkers int) bool {
// TODO: worker alive count could be 0 and shutting down the queue manager would be impossible
// it'd be better if we were able to spawn new workers instead of just closing the queueManager
// and creating a new one
Expand Down