Skip to content
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

Use advisory lock when assigning sequence IDs #401

Merged
merged 4 commits into from
Aug 29, 2024
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
6 changes: 6 additions & 0 deletions pkg/migrations/mls/20240829001344_serial-ids.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
DROP FUNCTION insert_group_message;

DROP FUNCTION insert_welcome_message;

DROP FUNCTION insert_inbox_log;

44 changes: 44 additions & 0 deletions pkg/migrations/mls/20240829001344_serial-ids.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
CREATE FUNCTION insert_group_message(group_id BYTEA, data BYTEA, group_id_data_hash BYTEA)
RETURNS SETOF group_messages
AS $$
BEGIN
-- Ensures that the generated sequence ID matches the insertion order
-- Only released at the end of the enclosing transaction - beware if called within a long transaction
PERFORM
pg_advisory_xact_lock(hashtext('group_messages_sequence'), hashtext(encode(group_id, 'hex')));
Copy link
Collaborator

Choose a reason for hiding this comment

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

We could cut down on the number of hashes by concatenating the two strings before hashing if we're concerned about perf

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another good option if perf becomes an issue. Separated it for this implementation to reduce collisions (especially against other tables)

RETURN QUERY INSERT INTO group_messages(group_id, data, group_id_data_hash)
VALUES(group_id, data, group_id_data_hash)
RETURNING
*;
END;
$$
LANGUAGE plpgsql;

CREATE FUNCTION insert_welcome_message(installation_key BYTEA, data BYTEA, installation_key_data_hash BYTEA, hpke_public_key BYTEA)
RETURNS SETOF welcome_messages
AS $$
BEGIN
PERFORM
pg_advisory_xact_lock(hashtext('welcome_messages_sequence'), hashtext(encode(installation_key, 'hex')));
RETURN QUERY INSERT INTO welcome_messages(installation_key, data, installation_key_data_hash, hpke_public_key)
VALUES(installation_key, data, installation_key_data_hash, hpke_public_key)
RETURNING
*;
END;
$$
LANGUAGE plpgsql;

CREATE FUNCTION insert_inbox_log(inbox_id BYTEA, server_timestamp_ns BIGINT, identity_update_proto BYTEA)
RETURNS SETOF inbox_log
AS $$
BEGIN
PERFORM
pg_advisory_xact_lock(hashtext('inbox_log_sequence'), hashtext(encode(inbox_id, 'hex')));
RETURN QUERY INSERT INTO inbox_log(inbox_id, server_timestamp_ns, identity_update_proto)
VALUES(inbox_id, server_timestamp_ns, identity_update_proto)
RETURNING
*;
END;
$$
LANGUAGE plpgsql;

24 changes: 12 additions & 12 deletions pkg/mls/store/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ RETURNING
*;

-- name: InsertInboxLog :one
INSERT INTO inbox_log(inbox_id, server_timestamp_ns, identity_update_proto)
VALUES (decode(@inbox_id, 'hex'), @server_timestamp_ns, @identity_update_proto)
RETURNING
sequence_id;
SELECT
sequence_id
FROM
insert_inbox_log(decode(@inbox_id, 'hex'), @server_timestamp_ns, @identity_update_proto);

-- name: RevokeAddressFromLog :exec
UPDATE
Expand Down Expand Up @@ -111,16 +111,16 @@ WHERE
id = ANY (@installation_ids::BYTEA[]);

-- name: InsertGroupMessage :one
INSERT INTO group_messages(group_id, data, group_id_data_hash)
VALUES ($1, $2, $3)
RETURNING
*;
SELECT
*
FROM
insert_group_message(@group_id, @data, @group_id_data_hash);

-- name: InsertWelcomeMessage :one
INSERT INTO welcome_messages(installation_key, data, installation_key_data_hash, hpke_public_key)
VALUES ($1, $2, $3, $4)
RETURNING
*;
SELECT
*
FROM
insert_welcome_message(@installation_key, @data, @installation_key_data_hash, @hpke_public_key);

-- name: GetAllGroupMessages :many
SELECT
Expand Down
2 changes: 1 addition & 1 deletion pkg/mls/store/queries/db.go

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

2 changes: 1 addition & 1 deletion pkg/mls/store/queries/models.go

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

20 changes: 10 additions & 10 deletions pkg/mls/store/queries/queries.sql.go

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

28 changes: 14 additions & 14 deletions pkg/proto/identity/api/v1/identity.pb.go

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

Loading
Loading