Skip to content

Commit 5be6c70

Browse files
authored
feat: Associate connected workspace agents with replicas (#4914)
This will enable displaying a graph that associates agents to running replicas.
1 parent 267b81a commit 5be6c70

File tree

10 files changed

+52
-21
lines changed

10 files changed

+52
-21
lines changed

coderd/coderd.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/andybalholm/brotli"
1717
"github.com/go-chi/chi/v5"
1818
"github.com/go-chi/chi/v5/middleware"
19+
"github.com/google/uuid"
1920
"github.com/klauspost/compress/zstd"
2021
"github.com/prometheus/client_golang/prometheus"
2122
"go.opentelemetry.io/otel/trace"
@@ -165,6 +166,7 @@ func New(options *Options) *API {
165166

166167
r := chi.NewRouter()
167168
api := &API{
169+
ID: uuid.New(),
168170
Options: options,
169171
RootHandler: r,
170172
siteHandler: site.Handler(site.FS(), binFS),
@@ -579,6 +581,11 @@ func New(options *Options) *API {
579581

580582
type API struct {
581583
*Options
584+
// ID is a uniquely generated ID on initialization.
585+
// This is used to associate objects with a specific
586+
// Coder API instance, like workspace agents to a
587+
// specific replica.
588+
ID uuid.UUID
582589
Auditor atomic.Pointer[audit.Auditor]
583590
WorkspaceClientCoordinateOverride atomic.Pointer[func(rw http.ResponseWriter) bool]
584591
WorkspaceQuotaEnforcer atomic.Pointer[workspacequota.Enforcer]

coderd/database/dump.sql

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE workspace_agents
2+
DROP COLUMN last_connected_replica_id;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE workspace_agents
2+
ADD COLUMN last_connected_replica_id uuid;

coderd/database/models.go

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

Lines changed: 22 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/workspaceagents.sql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ UPDATE
6464
SET
6565
first_connected_at = $2,
6666
last_connected_at = $3,
67-
disconnected_at = $4,
68-
updated_at = $5
67+
last_connected_replica_id = $4,
68+
disconnected_at = $5,
69+
updated_at = $6
6970
WHERE
7071
id = $1;
7172

coderd/workspaceagents.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,10 @@ func (api *API) workspaceAgentCoordinate(rw http.ResponseWriter, r *http.Request
522522
LastConnectedAt: lastConnectedAt,
523523
DisconnectedAt: disconnectedAt,
524524
UpdatedAt: database.Now(),
525+
LastConnectedReplicaID: uuid.NullUUID{
526+
UUID: api.ID,
527+
Valid: true,
528+
},
525529
})
526530
if err != nil {
527531
return err

enterprise/coderd/coderd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ func New(ctx context.Context, options *Options) (*API, error) {
158158
}
159159
var err error
160160
api.replicaManager, err = replicasync.New(ctx, options.Logger, options.Database, options.Pubsub, &replicasync.Options{
161+
ID: api.AGPL.ID,
161162
RelayAddress: options.DERPServerRelayAddress,
162163
RegionID: int32(options.DERPServerRegionID),
163164
TLSConfig: meshTLSConfig,

enterprise/replicasync/replicasync.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var (
2626
)
2727

2828
type Options struct {
29+
ID uuid.UUID
2930
CleanupInterval time.Duration
3031
UpdateInterval time.Duration
3132
PeerTimeout time.Duration
@@ -40,6 +41,9 @@ func New(ctx context.Context, logger slog.Logger, db database.Store, pubsub data
4041
if options == nil {
4142
options = &Options{}
4243
}
44+
if options.ID == uuid.Nil {
45+
options.ID = uuid.New()
46+
}
4347
if options.PeerTimeout == 0 {
4448
options.PeerTimeout = 3 * time.Second
4549
}
@@ -59,9 +63,8 @@ func New(ctx context.Context, logger slog.Logger, db database.Store, pubsub data
5963
if err != nil {
6064
return nil, xerrors.Errorf("ping database: %w", err)
6165
}
62-
id := uuid.New()
6366
replica, err := db.InsertReplica(ctx, database.InsertReplicaParams{
64-
ID: id,
67+
ID: options.ID,
6568
CreatedAt: database.Now(),
6669
StartedAt: database.Now(),
6770
UpdatedAt: database.Now(),
@@ -74,13 +77,13 @@ func New(ctx context.Context, logger slog.Logger, db database.Store, pubsub data
7477
if err != nil {
7578
return nil, xerrors.Errorf("insert replica: %w", err)
7679
}
77-
err = pubsub.Publish(PubsubEvent, []byte(id.String()))
80+
err = pubsub.Publish(PubsubEvent, []byte(options.ID.String()))
7881
if err != nil {
7982
return nil, xerrors.Errorf("publish new replica: %w", err)
8083
}
8184
ctx, cancelFunc := context.WithCancel(ctx)
8285
manager := &Manager{
83-
id: id,
86+
id: options.ID,
8487
options: options,
8588
db: db,
8689
pubsub: pubsub,

0 commit comments

Comments
 (0)