| 1 |
Here is the list of some established lock ordering. |
| 2 |
|
| 3 |
In this list, A -> B means that you can have A.Lock() then B.Lock(), not the opposite. |
| 4 |
|
| 5 |
jetStream -> jsAccount -> Server -> client -> Account |
| 6 |
|
| 7 |
jetStream -> jsAccount -> stream -> consumer |
| 8 |
|
| 9 |
A lock to protect jetstream account's usage has been introduced: jsAccount.usageMu. |
| 10 |
This lock is independent and can be invoked under any other lock: jsAccount -> jsa.usageMu, stream -> jsa.usageMu, etc... |
| 11 |
|
| 12 |
A lock to protect the account's leafnodes list was also introduced to |
| 13 |
allow that lock to be held and the acquire a client lock which is not |
| 14 |
possible with the normal account lock. |
| 15 |
|
| 16 |
accountLeafList -> client |
| 17 |
|
| 18 |
AccountResolver interface has various implementations, but assume: AccountResolver -> Server |
| 19 |
|
| 20 |
A reloadMu lock was added to prevent newly connecting clients racing with the configuration reload. |
| 21 |
This must be taken out as soon as a reload is about to happen before any other locks: |
| 22 |
|
| 23 |
reloadMu -> Server |
| 24 |
reloadMu -> optsMu |
| 25 |
|
| 26 |
The "jscmMu" lock in the Account is used to serialise calls to checkJetStreamMigrate and |
| 27 |
clearObserverState so that they cannot interleave which would leave Raft nodes in |
| 28 |
inconsistent observer states. |
| 29 |
|
| 30 |
jscmMu -> Account -> jsAccount |
| 31 |
jscmMu -> stream.clsMu |
| 32 |
jscmMu -> RaftNode |
| 33 |
|
| 34 |
The "clsMu" lock protects the consumer list on a stream, used for signalling consumer activity. |
| 35 |
|
| 36 |
stream -> clsMu |
| 37 |
|
| 38 |
The "clMu" and "ddMu" locks protect clustered and dedupe state respectively. |
| 39 |
The stream lock (`mset.mu`) is optional, but if holding "clMu" or "ddMu", |
| 40 |
locking the stream lock afterward would violate locking order. |
| 41 |
|
| 42 |
stream -> clMu |
| 43 |
stream -> clMu -> ddMu |
| 44 |
|
| 45 |
The "mset.batches.mu" lock protects the batching state without needing to hold the stream lock. |
| 46 |
If "clMu" is used to commit a batch, it should only be acquired while already holding the batch lock. |
| 47 |
|
| 48 |
stream -> mset.batches.mu -> clMu |
| 49 |
|
| 50 |
The "isolateMu" lock isolates direct get and msg get reads from writes, so those reads do not |
| 51 |
observe partially applied rollups or atomic batches. Other read paths (consumer delivery, |
| 52 |
mirrors/sources, info requests, etc.) are sequentially consistent and may observe a prefix of |
| 53 |
an inflight batch. It must be acquired before the stream lock, never while holding it. |
| 54 |
|
| 55 |
isolateMu -> stream |