Karta. Messaging. Locks
(Karta v1.0.0)
The one order in which the messaging module takes row locks. Two paths that take several locks in different orders can wait for each other for ever (a deadlock; Postgres aborts one of them after a second, and a socket frame or an Oban job fails). So every path that takes more than one of these locks takes them in this order, and no path goes back:
connector -> command -> request -> message(messaging_connectors, messaging_commands, messaging_send_requests,
messaging_messages). Rows of conversations, media, message events and
recipient checks are leaves: they are written while holding the locks above
and nothing waits for a lock while holding one of them.
The paths and what they take:
Router.advance/2(a new attempt): the connectors of the request's remote channels (connectors_of/1, by id), then the request, then it inserts the message and the command (new rows, nothing to wait for);Inbound.process/3(an agent's events): the connector, then for a status event the request and the message (Attempts.transition/3);Commands.finished/3andCommands.sweep/1(an answer, an expiry): the command, then the request and the message;ProviderEvents.apply/2(a provider's webhook): the connector, then the request and the message of each event;Attempts.transition/3,Router.attempt_changed/2,Router.receipt_check/1,Attempts.late_sent/3: the request, then the message;Commands.enqueue/4andConnectors.revoke/2: the connector, then commands.
A function that is called while a lock is held never takes one that comes
before it in the order; to add a path, find where it fits and use the helpers
here. retry/1 is the net under that rule: if Postgres aborts a transaction
as a deadlock all the same, the top-level work is done again (it is idempotent
everywhere it is used), a few times, instead of being lost.
Summary
Functions
Locks a connector.
Locks the connectors a request may be sent through (its remote channels), by id.
Locks a message (take its request first, request_of/1).
Locks a request.
Locks the request a message belongs to (before the message itself); nil when it has none.
Runs fun again when Postgres aborted it as a deadlock (40P01), up to
tries times in all. Only for work done at the top level (not inside a
transaction of the caller) and idempotent.
Functions
@spec connector(Ecto.UUID.t()) :: Karta.Messaging.Connector.t() | nil
Locks a connector.
@spec connectors_of(Karta.Messaging.SendRequest.t()) :: [ Karta.Messaging.Connector.t() ]
Locks the connectors a request may be sent through (its remote channels), by id.
@spec message(Ecto.UUID.t()) :: Karta.Messaging.Message.t() | nil
Locks a message (take its request first, request_of/1).
@spec request(Ecto.UUID.t() | nil) :: Karta.Messaging.SendRequest.t() | nil
Locks a request.
@spec request_of(Karta.Messaging.Message.t() | Ecto.UUID.t() | nil) :: Karta.Messaging.SendRequest.t() | nil
Locks the request a message belongs to (before the message itself); nil when it has none.
@spec retry((-> result), pos_integer() | nil) :: result when result: term()
Runs fun again when Postgres aborted it as a deadlock (40P01), up to
tries times in all. Only for work done at the top level (not inside a
transaction of the caller) and idempotent.