-
Notifications
You must be signed in to change notification settings - Fork 886
fix(shard-distributor): fix high-frequent triggering of the rebalancing loop #7696
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,6 +49,7 @@ const ( | |
| _defaultPeriod = time.Second | ||
| _defaultHeartbeatTTL = 10 * time.Second | ||
| _defaultTimeout = 1 * time.Second | ||
| _defaultCooldown = 250 * time.Millisecond | ||
| ) | ||
|
|
||
| type processorFactory struct { | ||
|
|
@@ -90,6 +91,9 @@ func NewProcessorFactory( | |
| if cfg.Process.Timeout == 0 { | ||
| cfg.Process.Timeout = _defaultTimeout | ||
| } | ||
| if cfg.Process.RebalanceCooldown == 0 { | ||
| cfg.Process.RebalanceCooldown = _defaultCooldown | ||
| } | ||
|
|
||
| return &processorFactory{ | ||
| logger: logger, | ||
|
|
@@ -192,13 +196,21 @@ func (p *namespaceProcessor) runRebalancingLoop(ctx context.Context) { | |
| return | ||
| } | ||
|
|
||
| nextRebalanceAllowedAt := p.timeSource.Now() | ||
|
|
||
| for { | ||
| select { | ||
| case <-ctx.Done(): | ||
| p.logger.Info("Rebalancing loop cancelled.") | ||
| return | ||
|
|
||
| case update := <-updateChan: | ||
| // If an update comes in before the cooldown has expired, | ||
| // we wait until the cooldown has passed since the last rebalance before processing it. | ||
| // This ensures that we don't rebalance too frequently in response to a flurry of updates | ||
| p.timeSource.Sleep(nextRebalanceAllowedAt.Sub(p.timeSource.Now())) | ||
|
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.
|
||
| nextRebalanceAllowedAt = p.timeSource.Now().Add(p.cfg.RebalanceCooldown) | ||
|
|
||
| p.logger.Info("Rebalancing triggered", tag.Dynamic("reason", update)) | ||
| if err := p.rebalanceShards(ctx); err != nil { | ||
| p.logger.Error("rebalance failed", tag.Error(err)) | ||
|
|
@@ -213,7 +225,7 @@ func (p *namespaceProcessor) runRebalanceTriggeringLoop(ctx context.Context) (<- | |
| // Buffered channel to allow one pending rebalance trigger. | ||
| triggerChan := make(chan string, 1) | ||
|
|
||
| updateChan, err := p.shardStore.Subscribe(ctx, p.namespaceCfg.Name) | ||
| updateChan, err := p.shardStore.SubscribeToExecutorStatusChanges(ctx, p.namespaceCfg.Name) | ||
| if err != nil { | ||
| p.logger.Error("Failed to subscribe to state changes, stopping rebalancing loop.", tag.Error(err)) | ||
| return nil, err | ||
|
|
||
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.
Just to make sure:
We already de coupled the sender right? So this will not block the sender.
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.
Yeah, this loop already doesn't block the watch event processing, so it's safe to have the cooldown here 👍