-
Notifications
You must be signed in to change notification settings - Fork 888
feat: add debouncing to provisionerd rpc calls #5198
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 |
---|---|---|
|
@@ -14,6 +14,7 @@ vendor | |
.eslintcache | ||
yarn-error.log | ||
gotests.coverage | ||
gotests.xml | ||
.idea | ||
.gitpod.yml | ||
.DS_Store | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -23,6 +23,7 @@ import ( | |
|
||
"cdr.dev/slog" | ||
"github.com/coder/coder/coderd/tracing" | ||
"github.com/coder/coder/cryptorand" | ||
"github.com/coder/coder/provisionerd/proto" | ||
"github.com/coder/coder/provisionerd/runner" | ||
sdkproto "github.com/coder/coder/provisionersdk/proto" | ||
|
@@ -52,7 +53,9 @@ type Options struct { | |
ForceCancelInterval time.Duration | ||
UpdateInterval time.Duration | ||
LogBufferInterval time.Duration | ||
PollInterval time.Duration | ||
JobPollInterval time.Duration | ||
JobPollJitter time.Duration | ||
JobPollDebounce time.Duration | ||
Provisioners Provisioners | ||
WorkDirectory string | ||
} | ||
|
@@ -62,8 +65,11 @@ func New(clientDialer Dialer, opts *Options) *Server { | |
if opts == nil { | ||
opts = &Options{} | ||
} | ||
if opts.PollInterval == 0 { | ||
opts.PollInterval = 5 * time.Second | ||
if opts.JobPollInterval == 0 { | ||
opts.JobPollInterval = 5 * time.Second | ||
} | ||
if opts.JobPollJitter == 0 { | ||
opts.JobPollJitter = time.Second | ||
} | ||
if opts.UpdateInterval == 0 { | ||
opts.UpdateInterval = 5 * time.Second | ||
|
@@ -207,8 +213,8 @@ func (p *Server) connect(ctx context.Context) { | |
if p.isClosed() { | ||
return | ||
} | ||
ticker := time.NewTicker(p.opts.PollInterval) | ||
defer ticker.Stop() | ||
timer := time.NewTimer(p.opts.JobPollInterval) | ||
defer timer.Stop() | ||
for { | ||
client, ok := p.client() | ||
if !ok { | ||
|
@@ -219,13 +225,23 @@ func (p *Server) connect(ctx context.Context) { | |
return | ||
case <-client.DRPCConn().Closed(): | ||
return | ||
case <-ticker.C: | ||
case <-timer.C: | ||
p.acquireJob(ctx) | ||
timer.Reset(p.nextInterval()) | ||
} | ||
} | ||
}() | ||
} | ||
|
||
func (p *Server) nextInterval() time.Duration { | ||
r, err := cryptorand.Float64() | ||
if err != nil { | ||
panic("get random float:" + err.Error()) | ||
} | ||
|
||
return p.opts.JobPollInterval + time.Duration(float64(p.opts.JobPollJitter)*r) | ||
} | ||
|
||
func (p *Server) client() (proto.DRPCProvisionerDaemonClient, bool) { | ||
rawClient := p.clientValue.Load() | ||
if rawClient == nil { | ||
|
@@ -248,6 +264,11 @@ func (p *Server) isRunningJob() bool { | |
} | ||
} | ||
|
||
var ( | ||
lastAcquire time.Time | ||
lastAcquireMutex sync.RWMutex | ||
) | ||
|
||
// Locks a job in the database, and runs it! | ||
func (p *Server) acquireJob(ctx context.Context) { | ||
p.mutex.Lock() | ||
|
@@ -263,6 +284,18 @@ func (p *Server) acquireJob(ctx context.Context) { | |
return | ||
} | ||
|
||
// This prevents loads of provisioner daemons from consistently sending | ||
// requests when no jobs are available. | ||
// | ||
// The debounce only occurs when no job is returned, so if loads of jobs are | ||
// added at once, they will start after at most this duration. | ||
lastAcquireMutex.RLock() | ||
if !lastAcquire.IsZero() && time.Since(lastAcquire) < p.opts.JobPollDebounce { | ||
lastAcquireMutex.RUnlock() | ||
return | ||
} | ||
lastAcquireMutex.RUnlock() | ||
Comment on lines
+292
to
+297
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. Should disable this debounce if running in tests 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. It's only enabled in |
||
|
||
var err error | ||
client, ok := p.client() | ||
if !ok { | ||
|
@@ -271,17 +304,19 @@ func (p *Server) acquireJob(ctx context.Context) { | |
|
||
job, err := client.AcquireJob(ctx, &proto.Empty{}) | ||
if err != nil { | ||
if errors.Is(err, context.Canceled) { | ||
return | ||
} | ||
if errors.Is(err, yamux.ErrSessionShutdown) { | ||
if errors.Is(err, context.Canceled) || | ||
errors.Is(err, yamux.ErrSessionShutdown) || | ||
errors.Is(err, fasthttputil.ErrInmemoryListenerClosed) { | ||
return | ||
} | ||
|
||
p.opts.Logger.Warn(ctx, "acquire job", slog.Error(err)) | ||
return | ||
} | ||
if job.JobId == "" { | ||
lastAcquireMutex.Lock() | ||
lastAcquire = time.Now() | ||
lastAcquireMutex.Unlock() | ||
return | ||
} | ||
|
||
|
This file contains hidden or 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 hidden or 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
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.
Wouldn't it be better to only apply this delay if it failed to acquire a job? This means we will acquire them as quick as possible without delays if there are a lot of jobs queued, but during periods of low jobs we will only attempt to acquire a job every 5 seconds.
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.
I guess in theory yeah, but it's currently architected in a way where this is still ticking while the provisioner is running a job, so it'd require a bit of a refactor for this to work.
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.
couldn't you just do something like:
or something? time.After's only leak if they haven't fired yet, so since the interval is small and the only time this for loop will exit is if the app is shutting down it seems fine here