-
Notifications
You must be signed in to change notification settings - Fork 899
feat: add tables for PGCoordinator v2 #10442
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 all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
BEGIN; | ||
|
||
DROP TRIGGER IF EXISTS tailnet_notify_tunnel_change ON tailnet_tunnels; | ||
DROP FUNCTION IF EXISTS tailnet_notify_tunnel_change; | ||
DROP TABLE IF EXISTS tailnet_tunnels; | ||
|
||
DROP TRIGGER IF EXISTS tailnet_notify_peer_change ON tailnet_peers; | ||
DROP FUNCTION IF EXISTS tailnet_notify_peer_change; | ||
DROP INDEX IF EXISTS idx_tailnet_peers_coordinator; | ||
DROP TABLE IF EXISTS tailnet_peers; | ||
|
||
DROP TYPE IF EXISTS tailnet_status; | ||
|
||
COMMIT; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
BEGIN; | ||
|
||
CREATE TYPE tailnet_status AS ENUM ( | ||
'ok', | ||
'lost' | ||
); | ||
|
||
CREATE TABLE tailnet_peers ( | ||
id uuid NOT NULL, | ||
coordinator_id uuid NOT NULL, | ||
updated_at timestamp with time zone NOT NULL, | ||
node bytea NOT NULL, | ||
status tailnet_status DEFAULT 'ok'::tailnet_status NOT NULL, | ||
PRIMARY KEY (id, coordinator_id), | ||
FOREIGN KEY (coordinator_id) REFERENCES tailnet_coordinators(id) ON DELETE CASCADE | ||
); | ||
|
||
-- For shutting down / GC a coordinator | ||
CREATE INDEX idx_tailnet_peers_coordinator ON tailnet_peers (coordinator_id); | ||
|
||
-- Any time tailnet_peers table changes, send an update with the affected peer ID. | ||
CREATE FUNCTION tailnet_notify_peer_change() RETURNS trigger | ||
spikecurtis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
LANGUAGE plpgsql | ||
AS $$ | ||
BEGIN | ||
IF (OLD IS NOT NULL) THEN | ||
PERFORM pg_notify('tailnet_peer_update', OLD.id::text); | ||
RETURN NULL; | ||
END IF; | ||
IF (NEW IS NOT NULL) THEN | ||
PERFORM pg_notify('tailnet_peer_update', NEW.id::text); | ||
RETURN NULL; | ||
END IF; | ||
END; | ||
$$; | ||
|
||
CREATE TRIGGER tailnet_notify_peer_change | ||
AFTER INSERT OR UPDATE OR DELETE ON tailnet_peers | ||
FOR EACH ROW | ||
EXECUTE PROCEDURE tailnet_notify_peer_change(); | ||
|
||
CREATE TABLE tailnet_tunnels ( | ||
coordinator_id uuid NOT NULL, | ||
-- we don't keep foreign keys for src_id and dst_id because the coordinator doesn't | ||
-- strictly order creating the peers and creating the tunnels | ||
spikecurtis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
src_id uuid NOT NULL, | ||
dst_id uuid NOT NULL, | ||
updated_at timestamp with time zone NOT NULL, | ||
PRIMARY KEY (coordinator_id, src_id, dst_id), | ||
FOREIGN KEY (coordinator_id) REFERENCES tailnet_coordinators (id) ON DELETE CASCADE | ||
); | ||
|
||
CREATE FUNCTION tailnet_notify_tunnel_change() RETURNS trigger | ||
LANGUAGE plpgsql | ||
AS $$ | ||
BEGIN | ||
IF (NEW IS NOT NULL) THEN | ||
PERFORM pg_notify('tailnet_tunnel_update', NEW.src_id || ',' || NEW.dst_id); | ||
RETURN NULL; | ||
ELSIF (OLD IS NOT NULL) THEN | ||
PERFORM pg_notify('tailnet_tunnel_update', OLD.src_id || ',' || OLD.dst_id); | ||
RETURN NULL; | ||
END IF; | ||
END; | ||
$$; | ||
|
||
CREATE TRIGGER tailnet_notify_tunnel_change | ||
AFTER INSERT OR UPDATE OR DELETE ON tailnet_tunnels | ||
FOR EACH ROW | ||
EXECUTE PROCEDURE tailnet_notify_tunnel_change(); | ||
|
||
COMMIT; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
INSERT INTO tailnet_peers | ||
(id, coordinator_id, updated_at, node, status) | ||
VALUES ( | ||
'c0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11', | ||
'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11', | ||
'2023-06-15 10:23:54+00', | ||
'a fake protobuf byte string', | ||
'ok' | ||
); | ||
|
||
INSERT INTO tailnet_tunnels | ||
(coordinator_id, src_id, dst_id, updated_at) | ||
VALUES ( | ||
'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11', | ||
'c0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11', | ||
'b0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11', | ||
'2023-06-15 10:23:54+00' | ||
); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.