Skip to content

Commit 1883430

Browse files
committed
Add replicas
1 parent 443173c commit 1883430

File tree

11 files changed

+949
-4
lines changed

11 files changed

+949
-4
lines changed

coderd/database/databasefake/databasefake.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ type data struct {
107107
workspaceApps []database.WorkspaceApp
108108
workspaces []database.Workspace
109109
licenses []database.License
110+
replicas []database.Replica
110111

111112
deploymentID string
112113
lastLicenseID int32
@@ -3025,3 +3026,80 @@ func (q *fakeQuerier) DeleteGroupByID(_ context.Context, id uuid.UUID) error {
30253026

30263027
return sql.ErrNoRows
30273028
}
3029+
3030+
func (q *fakeQuerier) DeleteReplicasUpdatedBefore(ctx context.Context, before time.Time) error {
3031+
q.mutex.Lock()
3032+
defer q.mutex.Unlock()
3033+
3034+
for i, replica := range q.replicas {
3035+
if replica.UpdatedAt.Before(before) {
3036+
q.replicas = append(q.replicas[:i], q.replicas[i+1:]...)
3037+
}
3038+
}
3039+
3040+
return nil
3041+
}
3042+
3043+
func (q *fakeQuerier) InsertReplica(_ context.Context, arg database.InsertReplicaParams) (database.Replica, error) {
3044+
q.mutex.Lock()
3045+
defer q.mutex.Unlock()
3046+
3047+
replica := database.Replica{
3048+
ID: arg.ID,
3049+
CreatedAt: arg.CreatedAt,
3050+
StartedAt: arg.StartedAt,
3051+
UpdatedAt: arg.UpdatedAt,
3052+
Hostname: arg.Hostname,
3053+
RegionID: arg.RegionID,
3054+
RelayAddress: arg.RelayAddress,
3055+
Version: arg.Version,
3056+
}
3057+
q.replicas = append(q.replicas, replica)
3058+
return replica, nil
3059+
}
3060+
3061+
func (q *fakeQuerier) UpdateReplica(_ context.Context, arg database.UpdateReplicaParams) (database.Replica, error) {
3062+
q.mutex.Lock()
3063+
defer q.mutex.Unlock()
3064+
3065+
for index, replica := range q.replicas {
3066+
if replica.ID != arg.ID {
3067+
continue
3068+
}
3069+
replica.Hostname = arg.Hostname
3070+
replica.StartedAt = arg.StartedAt
3071+
replica.StoppedAt = arg.StoppedAt
3072+
replica.UpdatedAt = arg.UpdatedAt
3073+
replica.RelayAddress = arg.RelayAddress
3074+
replica.RegionID = arg.RegionID
3075+
replica.Version = arg.Version
3076+
replica.Error = arg.Error
3077+
q.replicas[index] = replica
3078+
return replica, nil
3079+
}
3080+
return database.Replica{}, sql.ErrNoRows
3081+
}
3082+
3083+
func (q *fakeQuerier) GetReplicasUpdatedAfter(_ context.Context, updatedAt time.Time) ([]database.Replica, error) {
3084+
q.mutex.RLock()
3085+
defer q.mutex.RUnlock()
3086+
replicas := make([]database.Replica, 0)
3087+
for _, replica := range q.replicas {
3088+
if replica.UpdatedAt.After(updatedAt) && !replica.StoppedAt.Valid {
3089+
replicas = append(replicas, replica)
3090+
}
3091+
}
3092+
return replicas, nil
3093+
}
3094+
3095+
func (q *fakeQuerier) GetReplicaByID(_ context.Context, id uuid.UUID) (database.Replica, error) {
3096+
q.mutex.RLock()
3097+
defer q.mutex.RUnlock()
3098+
3099+
for _, replica := range q.replicas {
3100+
if replica.ID == id {
3101+
return replica, nil
3102+
}
3103+
}
3104+
return database.Replica{}, sql.ErrNoRows
3105+
}

coderd/database/dump.sql

Lines changed: 15 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+
DROP TABLE replicas;
2+
ALTER TABLE provisioner_daemons DROP COLUMN replica_id;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
CREATE TABLE IF NOT EXISTS replicas (
2+
-- A unique identifier for the replica that is stored on disk.
3+
-- For persistent replicas, this will be reused.
4+
-- For ephemeral replicas, this will be a new UUID for each one.
5+
id uuid NOT NULL,
6+
created_at timestamp with time zone NOT NULL,
7+
-- The time the replica was created.
8+
started_at timestamp with time zone NOT NULL,
9+
-- The time the replica was last seen.
10+
stopped_at timestamp with time zone,
11+
-- Updated periodically to ensure the replica is still alive.
12+
updated_at timestamp with time zone NOT NULL,
13+
-- Hostname is the hostname of the replica.
14+
hostname text NOT NULL,
15+
-- Region is the region the replica is in.
16+
-- We only DERP mesh to the same region ID of a running replica.
17+
region_id integer NOT NULL,
18+
-- An address that should be accessible to other replicas.
19+
relay_address text NOT NULL,
20+
-- Version is the Coder version of the replica.
21+
version text NOT NULL,
22+
error text
23+
);
24+
25+
-- Associates a provisioner daemon with a replica.
26+
ALTER TABLE provisioner_daemons ADD COLUMN replica_id uuid;

coderd/database/models.go

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

coderd/database/querier.go

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

0 commit comments

Comments
 (0)