Skip to content

feat: Associate connected workspace agents with replicas #4914

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 1 commit into from
Nov 6, 2022
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
7 changes: 7 additions & 0 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/andybalholm/brotli"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/google/uuid"
"github.com/klauspost/compress/zstd"
"github.com/prometheus/client_golang/prometheus"
"go.opentelemetry.io/otel/trace"
Expand Down Expand Up @@ -165,6 +166,7 @@ func New(options *Options) *API {

r := chi.NewRouter()
api := &API{
ID: uuid.New(),
Options: options,
RootHandler: r,
siteHandler: site.Handler(site.FS(), binFS),
Expand Down Expand Up @@ -579,6 +581,11 @@ func New(options *Options) *API {

type API struct {
*Options
// ID is a uniquely generated ID on initialization.
// This is used to associate objects with a specific
// Coder API instance, like workspace agents to a
// specific replica.
ID uuid.UUID
Auditor atomic.Pointer[audit.Auditor]
WorkspaceClientCoordinateOverride atomic.Pointer[func(rw http.ResponseWriter) bool]
WorkspaceQuotaEnforcer atomic.Pointer[workspacequota.Enforcer]
Expand Down
3 changes: 2 additions & 1 deletion coderd/database/dump.sql

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions coderd/database/migrations/000070_deployment_graph.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE workspace_agents
DROP COLUMN last_connected_replica_id;
2 changes: 2 additions & 0 deletions coderd/database/migrations/000070_deployment_graph.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE workspace_agents
ADD COLUMN last_connected_replica_id uuid;
Comment on lines +1 to +2
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer to make this not null with a default '00000000-0000-0000-0000-000000000000'::uuid since in practice this should never be null.

Copy link
Member Author

Choose a reason for hiding this comment

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

It can be null if the agent hasn't connected yet

Copy link
Contributor

Choose a reason for hiding this comment

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

oh good point

3 changes: 2 additions & 1 deletion coderd/database/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 22 additions & 13 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions coderd/database/queries/workspaceagents.sql
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ UPDATE
SET
first_connected_at = $2,
last_connected_at = $3,
disconnected_at = $4,
updated_at = $5
last_connected_replica_id = $4,
disconnected_at = $5,
updated_at = $6
WHERE
id = $1;

Expand Down
4 changes: 4 additions & 0 deletions coderd/workspaceagents.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,10 @@ func (api *API) workspaceAgentCoordinate(rw http.ResponseWriter, r *http.Request
LastConnectedAt: lastConnectedAt,
DisconnectedAt: disconnectedAt,
UpdatedAt: database.Now(),
LastConnectedReplicaID: uuid.NullUUID{
UUID: api.ID,
Valid: true,
},
})
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions enterprise/coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ func New(ctx context.Context, options *Options) (*API, error) {
}
var err error
api.replicaManager, err = replicasync.New(ctx, options.Logger, options.Database, options.Pubsub, &replicasync.Options{
ID: api.AGPL.ID,
RelayAddress: options.DERPServerRelayAddress,
RegionID: int32(options.DERPServerRegionID),
TLSConfig: meshTLSConfig,
Expand Down
11 changes: 7 additions & 4 deletions enterprise/replicasync/replicasync.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var (
)

type Options struct {
ID uuid.UUID
CleanupInterval time.Duration
UpdateInterval time.Duration
PeerTimeout time.Duration
Expand All @@ -40,6 +41,9 @@ func New(ctx context.Context, logger slog.Logger, db database.Store, pubsub data
if options == nil {
options = &Options{}
}
if options.ID == uuid.Nil {
options.ID = uuid.New()
}
if options.PeerTimeout == 0 {
options.PeerTimeout = 3 * time.Second
}
Expand All @@ -59,9 +63,8 @@ func New(ctx context.Context, logger slog.Logger, db database.Store, pubsub data
if err != nil {
return nil, xerrors.Errorf("ping database: %w", err)
}
id := uuid.New()
replica, err := db.InsertReplica(ctx, database.InsertReplicaParams{
ID: id,
ID: options.ID,
CreatedAt: database.Now(),
StartedAt: database.Now(),
UpdatedAt: database.Now(),
Expand All @@ -74,13 +77,13 @@ func New(ctx context.Context, logger slog.Logger, db database.Store, pubsub data
if err != nil {
return nil, xerrors.Errorf("insert replica: %w", err)
}
err = pubsub.Publish(PubsubEvent, []byte(id.String()))
err = pubsub.Publish(PubsubEvent, []byte(options.ID.String()))
if err != nil {
return nil, xerrors.Errorf("publish new replica: %w", err)
}
ctx, cancelFunc := context.WithCancel(ctx)
manager := &Manager{
id: id,
id: options.ID,
options: options,
db: db,
pubsub: pubsub,
Expand Down