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

Remove usage of timer.Timer in benchlist #2446

Merged
merged 2 commits into from
Dec 8, 2023
Merged

Conversation

StephenButtolph
Copy link
Contributor

Why this should be merged

timer.Timer is horrible code, I now know better. This helps to remove the abomination.

How this works

Replaces the usage of timer.Timer with the standard lib's time.Timer

How this was tested

CI

@StephenButtolph StephenButtolph added the cleanup Code quality improvement label Dec 7, 2023
@StephenButtolph StephenButtolph added this to the v1.10.18 milestone Dec 7, 2023
@StephenButtolph StephenButtolph self-assigned this Dec 7, 2023
Comment on lines -120 to -122
benchlist.timer = timer.NewTimer(benchlist.update)
go benchlist.timer.Dispatch()
return benchlist, benchlist.metrics.Initialize(ctx.Registerer)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Definitely odd that we initialized the metrics after kicking off the goroutine... Not a bug - but felt pretty close

return benchlist, nil
}

// TODO: Close this goroutine during node shutdown
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We never shutdown the timer previously... We still don't... But we should

// Note: If there are no nodes to remove, [duration] will be 0 and we
// will immediately wait until there are benched nodes.
duration := b.durationToSleep(now)
timer.Reset(duration)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: it is safe to reset a timer with a 0 or negative duration... I don't think that's actually possible... but it isn't a case we need to worry about

Comment on lines -200 to -203
if _, ok := b.benchlistSet[nodeID]; ok {
return true
}
return false
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This code made me sad

Comment on lines +316 to +320
b.ctx.Log.Debug("benching validator after consecutive failed queries",
zap.Stringer("nodeID", nodeID),
zap.Duration("benchDuration", benchedUntil.Sub(now)),
zap.Int("numFailedQueries", b.threshold),
)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It felt weird having this log after marking the node as benched.

benchlist := &benchlist{
ctx: ctx,
resetTimer: make(chan struct{}, 1),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We use the same hack here of a length 1 buffered channel as we do with block building. This allows us to be ensured that the timer will be reset after attempting to push a message without blocking on resetting the timer. This is important because the method attempting to reset the timer is holding a lock that the timer may also be attempting to grab.

@StephenButtolph StephenButtolph added this pull request to the merge queue Dec 8, 2023
Merged via the queue into dev with commit dd2c6ef Dec 8, 2023
16 checks passed
@StephenButtolph StephenButtolph deleted the remove-benchlist-timer branch December 8, 2023 21:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cleanup Code quality improvement
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

3 participants