|
| 1 | +/* Copyright 2023 The Matrix.org Foundation C.I.C |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | + |
| 17 | +-- Remove a previous attempt to avoid deadlocks |
| 18 | +DROP TRIGGER IF EXISTS delete_read_write_lock_parent_before_trigger ON worker_read_write_locks; |
| 19 | +DROP FUNCTION IF EXISTS delete_read_write_lock_parent_before; |
| 20 | + |
| 21 | + |
| 22 | +-- Ensure that we keep `worker_read_write_locks_mode` up to date whenever a lock |
| 23 | +-- is released (i.e. a row deleted from `worker_read_write_locks`). Either we |
| 24 | +-- update the `worker_read_write_locks_mode.token` to match another instance |
| 25 | +-- that has currently acquired the lock, or we delete the row if nobody has |
| 26 | +-- currently acquired a lock. |
| 27 | +CREATE OR REPLACE FUNCTION delete_read_write_lock_parent() RETURNS trigger AS $$ |
| 28 | +DECLARE |
| 29 | + new_token TEXT; |
| 30 | + mode_row_token TEXT; |
| 31 | +BEGIN |
| 32 | + -- Only update the token in `_mode` if its our token. This prevents |
| 33 | + -- deadlocks. |
| 34 | + -- |
| 35 | + -- We shove the token into `mode_row_token`, as otherwise postgres complains |
| 36 | + -- we're not using the returned data. |
| 37 | + SELECT token INTO mode_row_token FROM worker_read_write_locks_mode |
| 38 | + WHERE |
| 39 | + lock_name = OLD.lock_name |
| 40 | + AND lock_key = OLD.lock_key |
| 41 | + AND token = OLD.token |
| 42 | + FOR UPDATE; |
| 43 | + |
| 44 | + IF NOT FOUND THEN |
| 45 | + RETURN NEW; |
| 46 | + END IF; |
| 47 | + |
| 48 | + SELECT token INTO new_token FROM worker_read_write_locks |
| 49 | + WHERE |
| 50 | + lock_name = OLD.lock_name |
| 51 | + AND lock_key = OLD.lock_key |
| 52 | + LIMIT 1 FOR UPDATE SKIP LOCKED; |
| 53 | + |
| 54 | + IF NOT FOUND THEN |
| 55 | + DELETE FROM worker_read_write_locks_mode |
| 56 | + WHERE lock_name = OLD.lock_name AND lock_key = OLD.lock_key AND token = OLD.token; |
| 57 | + ELSE |
| 58 | + UPDATE worker_read_write_locks_mode |
| 59 | + SET token = new_token |
| 60 | + WHERE lock_name = OLD.lock_name AND lock_key = OLD.lock_key; |
| 61 | + END IF; |
| 62 | + |
| 63 | + RETURN NEW; |
| 64 | +END |
| 65 | +$$ |
| 66 | +LANGUAGE plpgsql; |
| 67 | + |
| 68 | +DROP TRIGGER IF EXISTS delete_read_write_lock_parent_trigger ON worker_read_write_locks; |
| 69 | +CREATE TRIGGER delete_read_write_lock_parent_trigger AFTER DELETE ON worker_read_write_locks |
| 70 | + FOR EACH ROW |
| 71 | + EXECUTE PROCEDURE delete_read_write_lock_parent(); |
0 commit comments