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

Reuse timers in sealing batch logic #6636

Merged
merged 1 commit into from
Jun 30, 2021
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
22 changes: 16 additions & 6 deletions extern/storage-sealing/commit_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func (b *CommitBatcher) run() {
panic(err)
}

timer := time.NewTimer(b.batchWait(cfg.CommitBatchWait, cfg.CommitBatchSlack))
for {
if forceRes != nil {
forceRes <- lastMsg
Expand All @@ -121,7 +122,7 @@ func (b *CommitBatcher) run() {
return
case <-b.notify:
sendAboveMax = true
case <-b.batchWait(cfg.CommitBatchWait, cfg.CommitBatchSlack):
case <-timer.C:
// do nothing
case fr := <-b.force: // user triggered
forceRes = fr
Expand All @@ -132,17 +133,26 @@ func (b *CommitBatcher) run() {
if err != nil {
log.Warnw("CommitBatcher processBatch error", "error", err)
}

if !timer.Stop() {
select {
case <-timer.C:
default:
}
}

timer.Reset(b.batchWait(cfg.CommitBatchWait, cfg.CommitBatchSlack))
}
}

func (b *CommitBatcher) batchWait(maxWait, slack time.Duration) <-chan time.Time {
func (b *CommitBatcher) batchWait(maxWait, slack time.Duration) time.Duration {
now := time.Now()

b.lk.Lock()
defer b.lk.Unlock()

if len(b.todo) == 0 {
return nil
return maxWait
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we wouldn't be resetting the timer here, but I don't think this micro-optimization is worth the added complexity

}

var cutoff time.Time
Expand All @@ -160,20 +170,20 @@ func (b *CommitBatcher) batchWait(maxWait, slack time.Duration) <-chan time.Time
}

if cutoff.IsZero() {
return time.After(maxWait)
return maxWait
}

cutoff = cutoff.Add(-slack)
if cutoff.Before(now) {
return time.After(time.Nanosecond) // can't return 0
return time.Nanosecond // can't return 0
}

wait := cutoff.Sub(now)
if wait > maxWait {
wait = maxWait
}

return time.After(wait)
return wait
}

func (b *CommitBatcher) maybeStartBatch(notif bool) ([]sealiface.CommitBatchRes, error) {
Expand Down
22 changes: 16 additions & 6 deletions extern/storage-sealing/precommit_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (b *PreCommitBatcher) run() {
panic(err)
}

timer := time.NewTimer(b.batchWait(cfg.CommitBatchWait, cfg.CommitBatchSlack))
for {
if forceRes != nil {
forceRes <- lastRes
Expand All @@ -102,7 +103,7 @@ func (b *PreCommitBatcher) run() {
return
case <-b.notify:
sendAboveMax = true
case <-b.batchWait(cfg.PreCommitBatchWait, cfg.PreCommitBatchSlack):
case <-timer.C:
// do nothing
case fr := <-b.force: // user triggered
forceRes = fr
Expand All @@ -113,17 +114,26 @@ func (b *PreCommitBatcher) run() {
if err != nil {
log.Warnw("PreCommitBatcher processBatch error", "error", err)
}

if !timer.Stop() {
select {
case <-timer.C:
default:
}
}

timer.Reset(b.batchWait(cfg.CommitBatchWait, cfg.CommitBatchSlack))
}
}

func (b *PreCommitBatcher) batchWait(maxWait, slack time.Duration) <-chan time.Time {
func (b *PreCommitBatcher) batchWait(maxWait, slack time.Duration) time.Duration {
now := time.Now()

b.lk.Lock()
defer b.lk.Unlock()

if len(b.todo) == 0 {
return nil
return maxWait
}

var cutoff time.Time
Expand All @@ -141,20 +151,20 @@ func (b *PreCommitBatcher) batchWait(maxWait, slack time.Duration) <-chan time.T
}

if cutoff.IsZero() {
return time.After(maxWait)
return maxWait
}

cutoff = cutoff.Add(-slack)
if cutoff.Before(now) {
return time.After(time.Nanosecond) // can't return 0
return time.Nanosecond // can't return 0
}

wait := cutoff.Sub(now)
if wait > maxWait {
wait = maxWait
}

return time.After(wait)
return wait
}

func (b *PreCommitBatcher) maybeStartBatch(notif bool) ([]sealiface.PreCommitBatchRes, error) {
Expand Down