-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat: reinitialize agents when a prebuilt workspace is claimed #17475
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 1 commit
c09c9b9
476fe71
8c8bca6
7ce4eea
52ac64e
362db7c
dcc7379
ff66b3f
efff5d9
cebd5db
2679138
9feebef
b117b5c
a22b414
9bbd2c7
5804201
7e8dcee
725f97b
a9b1567
21ee970
e54d7e7
2799858
1d93003
763fc12
0f879c7
61784c9
604eb27
bf4d2cf
38b4f0d
20df538
4bb3b68
83972db
146b158
5eb16cd
730d803
150adc0
b4ecf10
3fa3edf
7e45919
a632508
72125ec
b65eea7
e1339f3
c1a8ba6
5363dcc
7ad9b6d
394571d
890747b
b3870db
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,23 +2,16 @@ package prebuilds | |
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "sync" | ||
|
|
||
| "github.com/google/uuid" | ||
| "golang.org/x/xerrors" | ||
|
|
||
| "cdr.dev/slog" | ||
| "github.com/coder/coder/v2/coderd/database/pubsub" | ||
| "github.com/coder/coder/v2/coderd/httpapi" | ||
| "github.com/coder/coder/v2/codersdk" | ||
| "github.com/coder/coder/v2/codersdk/agentsdk" | ||
| ) | ||
|
|
||
| type WorkspaceClaimPublisher interface { | ||
| PublishWorkspaceClaim(agentsdk.ReinitializationEvent) | ||
| } | ||
|
|
||
| func NewPubsubWorkspaceClaimPublisher(ps pubsub.Pubsub) *PubsubWorkspaceClaimPublisher { | ||
| return &PubsubWorkspaceClaimPublisher{ps: ps} | ||
| } | ||
|
|
@@ -35,10 +28,6 @@ func (p PubsubWorkspaceClaimPublisher) PublishWorkspaceClaim(claim agentsdk.Rein | |
| return nil | ||
| } | ||
|
|
||
| type WorkspaceClaimListener interface { | ||
| ListenForWorkspaceClaims(ctx context.Context, workspaceID uuid.UUID) (func(), <-chan agentsdk.ReinitializationEvent, error) | ||
| } | ||
|
|
||
| func NewPubsubWorkspaceClaimListener(ps pubsub.Pubsub, logger slog.Logger) *PubsubWorkspaceClaimListener { | ||
| return &PubsubWorkspaceClaimListener{ps: ps, logger: logger} | ||
| } | ||
|
|
@@ -49,6 +38,12 @@ type PubsubWorkspaceClaimListener struct { | |
| } | ||
|
|
||
| func (p PubsubWorkspaceClaimListener) ListenForWorkspaceClaims(ctx context.Context, workspaceID uuid.UUID) (func(), <-chan agentsdk.ReinitializationEvent, error) { | ||
| select { | ||
| case <-ctx.Done(): | ||
| return func() {}, nil, ctx.Err() | ||
| default: | ||
| } | ||
|
|
||
| workspaceClaims := make(chan agentsdk.ReinitializationEvent, 1) | ||
| cancelSub, err := p.ps.Subscribe(agentsdk.PrebuildClaimedChannel(workspaceID), func(inner context.Context, id []byte) { | ||
|
Contributor
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. As we talked about on a call --- the pubsub is not considered reliable transport, and we can miss events. This can lead to a situation where the agent misses the reinit signal and never reinitializes, even though it has been claimed. The deep problem here is that we are using the PubSub to send material information (who the new owner is), rather than just a kick that there is new information available (the workspace has a new owner). In the latter case, when there is an error, we can recover by re-querying the database to find the owner, and then decide whether we need to signal the agent with new information. This requires the handler to keep track of the last owner it sent, but that's a trivial amount of memory to keep. I don't necessarily think this needs to be fixed in this PR, since the plan is to move to a new "stream of manifest" architecture, but as we implement that, we need to keep error handling in mind on both sides: coderd recovers from a pubsub error by querying the database (or closing the connection if the database query fails), and then deciding whether there is something new to send. The agent recovers from a dropped connection by redialing, and then checking the new manifest against it's existing one to see if it needs to take any action.
Contributor
Author
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. Thanks for identifying this. Let's defer it beyond this PR to the next release if there are no objections. I'd like to get to this as part of the manifest streaming work. |
||
| claimantID, err := uuid.ParseBytes(id) | ||
|
|
@@ -91,52 +86,3 @@ func (p PubsubWorkspaceClaimListener) ListenForWorkspaceClaims(ctx context.Conte | |
|
|
||
| return cancel, workspaceClaims, nil | ||
| } | ||
|
|
||
| func StreamAgentReinitEvents(ctx context.Context, logger slog.Logger, rw http.ResponseWriter, r *http.Request, reinitEvents <-chan agentsdk.ReinitializationEvent) { | ||
| sseSendEvent, sseSenderClosed, err := httpapi.ServerSentEventSender(rw, r) | ||
| if err != nil { | ||
| httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
| Message: "Internal error setting up server-sent events.", | ||
| Detail: err.Error(), | ||
| }) | ||
| return | ||
| } | ||
| // Prevent handler from returning until the sender is closed. | ||
| defer func() { | ||
| <-sseSenderClosed | ||
| }() | ||
|
|
||
| // An initial ping signals to the requester that the server is now ready | ||
| // and the client can begin servicing a channel with data. | ||
| _ = sseSendEvent(codersdk.ServerSentEvent{ | ||
| Type: codersdk.ServerSentEventTypePing, | ||
| }) | ||
|
|
||
| for { | ||
| select { | ||
| case <-ctx.Done(): | ||
| return | ||
| case reinitEvent := <-reinitEvents: | ||
| err = sseSendEvent(codersdk.ServerSentEvent{ | ||
| Type: codersdk.ServerSentEventTypeData, | ||
| Data: reinitEvent, | ||
| }) | ||
| if err != nil { | ||
| logger.Warn(ctx, "failed to send SSE response to trigger reinit", slog.Error(err)) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| type MockClaimCoordinator interface{} | ||
|
|
||
| type ClaimListener interface{} | ||
| type PostgresClaimListener struct{} | ||
|
|
||
| type AgentReinitializer interface{} | ||
| type SSEAgentReinitializer struct{} | ||
|
|
||
| type ClaimCoordinator interface { | ||
| ClaimListener | ||
| AgentReinitializer | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.