- Notifications
You must be signed in to change notification settings - Fork1k
feat: use v2 API for agent metadata updates#12281
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
77 changes: 40 additions & 37 deletionsagent/agent.go
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 |
---|---|---|
@@ -90,7 +90,6 @@ type Options struct { | ||
type Client interface { | ||
ConnectRPC(ctx context.Context) (drpc.Conn, error) | ||
RewriteDERPMap(derpMap *tailcfg.DERPMap) | ||
} | ||
@@ -298,7 +297,6 @@ func (a *agent) init() { | ||
// may be happening, but regardless after the intermittent | ||
// failure, you'll want the agent to reconnect. | ||
func (a *agent) runLoop() { | ||
go a.manageProcessPriorityUntilGracefulShutdown() | ||
// need to keep retrying up to the hardCtx so that we can send graceful shutdown-related | ||
@@ -405,9 +403,7 @@ func (t *trySingleflight) Do(key string, fn func()) { | ||
fn() | ||
} | ||
func (a *agent) reportMetadata(ctx context.Context, conn drpc.Conn) error { | ||
tickerDone := make(chan struct{}) | ||
collectDone := make(chan struct{}) | ||
ctx, cancel := context.WithCancel(ctx) | ||
@@ -567,51 +563,55 @@ func (a *agent) reportMetadataUntilGracefulShutdown() { | ||
var ( | ||
updatedMetadata = make(map[string]*codersdk.WorkspaceAgentMetadataResult) | ||
reportTimeout = 30 * time.Second | ||
reportError = make(chan error, 1) | ||
reportInFlight = false | ||
aAPI = proto.NewDRPCAgentClient(conn) | ||
) | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
case mr := <-metadataResults: | ||
// This can overwrite unsent values, but that's fine because | ||
// we're only interested about up-to-date values. | ||
updatedMetadata[mr.key] = mr.result | ||
continue | ||
case err := <-reportError: | ||
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. FYI: I moved this up to the main select so that if there is an error, we won't wait around until the next tick to exit. | ||
a.logger.Debug(ctx, "batch update metadata complete", slog.Error(err)) | ||
if err != nil { | ||
return xerrors.Errorf("failed to report metadata: %w", err) | ||
} | ||
reportInFlight = false | ||
case <-report: | ||
if len(updatedMetadata) == 0 { | ||
continue | ||
} | ||
if reportInFlight { | ||
// If there's already a report in flight, don't send | ||
// another one, wait for next tick instead. | ||
a.logger.Debug(ctx, "skipped metadata report tick because report is in flight") | ||
continue | ||
} | ||
metadata := make([]*proto.Metadata, 0, len(updatedMetadata)) | ||
for key, result := range updatedMetadata { | ||
pr := agentsdk.ProtoFromMetadataResult(*result) | ||
metadata = append(metadata, &proto.Metadata{ | ||
Key: key, | ||
Result: pr, | ||
}) | ||
delete(updatedMetadata, key) | ||
} | ||
reportInFlight = true | ||
go func() { | ||
a.logger.Debug(ctx, "batch updating metadata") | ||
ctx, cancel := context.WithTimeout(ctx, reportTimeout) | ||
defer cancel() | ||
_, err := aAPI.BatchUpdateMetadata(ctx, &proto.BatchUpdateMetadataRequest{Metadata: metadata}) | ||
reportError <- err | ||
}() | ||
} | ||
} | ||
} | ||
@@ -783,6 +783,9 @@ func (a *agent) run() (retErr error) { | ||
// lifecycle reporting has to be via gracefulShutdownBehaviorRemain | ||
connMan.start("report lifecycle", gracefulShutdownBehaviorRemain, a.reportLifecycle) | ||
// metadata reporting can cease as soon as we start gracefully shutting down | ||
connMan.start("report metadata", gracefulShutdownBehaviorStop, a.reportMetadata) | ||
// channels to sync goroutines below | ||
// handle manifest | ||
// | | ||
40 changes: 20 additions & 20 deletionsagent/agenttest/client.go
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
3 changes: 3 additions & 0 deletionscodersdk/agentsdk/agentsdk.go
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
25 changes: 25 additions & 0 deletionscodersdk/agentsdk/convert.go
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
40 changes: 40 additions & 0 deletionscodersdk/agentsdk/convert_test.go
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.