Skip to content

Commit cac29e0

Browse files
authored
feat: add tables for PGCoordinator v2 (#10442)
Adds tables for a simplified PG Coordinator that only considers Peers and Tunnels, rather than agent/client distinctions we have today.
1 parent 95ce697 commit cac29e0

7 files changed

+248
-0
lines changed

coderd/database/dump.sql

+67
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/foreign_key_constraint.go

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
BEGIN;
2+
3+
DROP TRIGGER IF EXISTS tailnet_notify_tunnel_change ON tailnet_tunnels;
4+
DROP FUNCTION IF EXISTS tailnet_notify_tunnel_change;
5+
DROP TABLE IF EXISTS tailnet_tunnels;
6+
7+
DROP TRIGGER IF EXISTS tailnet_notify_peer_change ON tailnet_peers;
8+
DROP FUNCTION IF EXISTS tailnet_notify_peer_change;
9+
DROP INDEX IF EXISTS idx_tailnet_peers_coordinator;
10+
DROP TABLE IF EXISTS tailnet_peers;
11+
12+
DROP TYPE IF EXISTS tailnet_status;
13+
14+
COMMIT;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
BEGIN;
2+
3+
CREATE TYPE tailnet_status AS ENUM (
4+
'ok',
5+
'lost'
6+
);
7+
8+
CREATE TABLE tailnet_peers (
9+
id uuid NOT NULL,
10+
coordinator_id uuid NOT NULL,
11+
updated_at timestamp with time zone NOT NULL,
12+
node bytea NOT NULL,
13+
status tailnet_status DEFAULT 'ok'::tailnet_status NOT NULL,
14+
PRIMARY KEY (id, coordinator_id),
15+
FOREIGN KEY (coordinator_id) REFERENCES tailnet_coordinators(id) ON DELETE CASCADE
16+
);
17+
18+
-- For shutting down / GC a coordinator
19+
CREATE INDEX idx_tailnet_peers_coordinator ON tailnet_peers (coordinator_id);
20+
21+
-- Any time tailnet_peers table changes, send an update with the affected peer ID.
22+
CREATE FUNCTION tailnet_notify_peer_change() RETURNS trigger
23+
LANGUAGE plpgsql
24+
AS $$
25+
BEGIN
26+
IF (OLD IS NOT NULL) THEN
27+
PERFORM pg_notify('tailnet_peer_update', OLD.id::text);
28+
RETURN NULL;
29+
END IF;
30+
IF (NEW IS NOT NULL) THEN
31+
PERFORM pg_notify('tailnet_peer_update', NEW.id::text);
32+
RETURN NULL;
33+
END IF;
34+
END;
35+
$$;
36+
37+
CREATE TRIGGER tailnet_notify_peer_change
38+
AFTER INSERT OR UPDATE OR DELETE ON tailnet_peers
39+
FOR EACH ROW
40+
EXECUTE PROCEDURE tailnet_notify_peer_change();
41+
42+
CREATE TABLE tailnet_tunnels (
43+
coordinator_id uuid NOT NULL,
44+
-- we don't keep foreign keys for src_id and dst_id because the coordinator doesn't
45+
-- strictly order creating the peers and creating the tunnels
46+
src_id uuid NOT NULL,
47+
dst_id uuid NOT NULL,
48+
updated_at timestamp with time zone NOT NULL,
49+
PRIMARY KEY (coordinator_id, src_id, dst_id),
50+
FOREIGN KEY (coordinator_id) REFERENCES tailnet_coordinators (id) ON DELETE CASCADE
51+
);
52+
53+
CREATE FUNCTION tailnet_notify_tunnel_change() RETURNS trigger
54+
LANGUAGE plpgsql
55+
AS $$
56+
BEGIN
57+
IF (NEW IS NOT NULL) THEN
58+
PERFORM pg_notify('tailnet_tunnel_update', NEW.src_id || ',' || NEW.dst_id);
59+
RETURN NULL;
60+
ELSIF (OLD IS NOT NULL) THEN
61+
PERFORM pg_notify('tailnet_tunnel_update', OLD.src_id || ',' || OLD.dst_id);
62+
RETURN NULL;
63+
END IF;
64+
END;
65+
$$;
66+
67+
CREATE TRIGGER tailnet_notify_tunnel_change
68+
AFTER INSERT OR UPDATE OR DELETE ON tailnet_tunnels
69+
FOR EACH ROW
70+
EXECUTE PROCEDURE tailnet_notify_tunnel_change();
71+
72+
COMMIT;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
INSERT INTO tailnet_peers
2+
(id, coordinator_id, updated_at, node, status)
3+
VALUES (
4+
'c0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11',
5+
'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11',
6+
'2023-06-15 10:23:54+00',
7+
'a fake protobuf byte string',
8+
'ok'
9+
);
10+
11+
INSERT INTO tailnet_tunnels
12+
(coordinator_id, src_id, dst_id, updated_at)
13+
VALUES (
14+
'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11',
15+
'c0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11',
16+
'b0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11',
17+
'2023-06-15 10:23:54+00'
18+
);

coderd/database/models.go

+73
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/unique_constraint.go

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)