|
38 | 38 | -- chain. 'Ouroboros.Consensus.Storage.ChainDB.API.ChainDB' defines the chain |
39 | 39 | -- DB API. |
40 | 40 | -- |
41 | | --- == Resource management in the ChainDB |
| 41 | +-- == Resource Management in the ChainDB |
42 | 42 | -- |
43 | | --- Each of the databases in the ChainDB can produce resources that need to be |
44 | | --- eventually freed. In particular: |
| 43 | +-- Clients of the ChainDB can allocate resources from the databases |
| 44 | +-- it contains (LedgerDB, VolatileDB, and ImmutableDB): |
45 | 45 | -- |
46 | | --- - The LedgerDB is used to create 'Forker's |
| 46 | +-- - The LedgerDB is used to create 'Forker's. |
47 | 47 | -- |
48 | 48 | -- - The ChainDB is used to create 'Follower's (which in turn contain |
49 | 49 | -- 'Iterator's). |
50 | 50 | -- |
51 | | --- The 'runWith' function in Consensus spawns a resource registry (which we will |
52 | | --- name __the consensus registry__) which will contain the ChainDB. Shutting |
53 | | --- down the Consensus layer is what will close the ChainDB by the consensus |
54 | | --- resource registry going out of scope. |
| 51 | +-- These resources must eventually be freed. |
55 | 52 | -- |
56 | | --- The resources above will be created by clients of the databases, not the |
57 | | --- databases themselves. For example, it is Chain selection the one that opens a |
58 | | --- forker using the LedgerDB. This in particular means that any clients that |
59 | | --- create these resources will be created later than the database. |
| 53 | +-- The 'runWith' function in Consensus spawns a resource registry |
| 54 | +-- (which we will refer to as __the consensus registry__) that will include |
| 55 | +-- the ChainDB as one of its resources. When the Consensus layer is |
| 56 | +-- shut down, the consensus resource registry will exit the scope of |
| 57 | +-- the 'withRegistry' function. This causes all resources allocated |
| 58 | +-- in the registry —including the ChainDB— to be closed. |
60 | 59 | -- |
61 | | --- We rely on a specific sequence of events for this schema to be correct: |
| 60 | +-- The resources mentioned above are created by clients of the ChainDB |
| 61 | +-- databases (LedgerDB, VolatileDB, and ImmutableDB), not by the |
| 62 | +-- databases themselves. For example, chain selection opens a |
| 63 | +-- forker using the LedgerDB. Crucially, this means that clients creating |
| 64 | +-- these resources are instantiated after the ChainDB. |
| 65 | +-- |
| 66 | +-- We rely on a specific sequence of events for this design to be correct: |
62 | 67 | -- |
63 | 68 | -- - The ChainDB is only closed by exiting the scope of the consensus |
64 | 69 | -- resource registry. |
65 | 70 | -- |
66 | | --- - If a client that can create resources is forked into a separate thread, |
67 | | --- such thread is linked to the consensus registry. That way, it will be |
68 | | --- deallocated before the ChainDB is closed, and its internal registry will |
69 | | --- release any resources created in the client. |
| 71 | +-- - If a client creates resources tied to any of the |
| 72 | +-- aforementioned databases and is forked into a separate thread, |
| 73 | +-- that thread is linked to the consensus registry. Because resources |
| 74 | +-- in a registry are deallocated in reverse order of allocation, any |
| 75 | +-- resources created by such threads will be deallocated before the |
| 76 | +-- ChainDB is closed, ensuring proper cleanup. |
70 | 77 | -- |
71 | | --- At the moment, we have two different approaches to resources and closing of |
72 | | --- the databases: |
| 78 | +-- Currently, we have two distinct approaches to resource management |
| 79 | +-- and database closure: |
73 | 80 | -- |
74 | | --- - In the LedgerDB, closing the database does not close any of the resources |
75 | | --- but makes them unable to do any action other than being freed. See |
76 | | --- 'ldbForkers'. |
| 81 | +-- - In the LedgerDB, closing the database does not close any resources |
| 82 | +-- created by its clients. We rely on the resource registry to deallocate |
| 83 | +-- these resources before the LedgerDB is closed. However, after closing |
| 84 | +-- the LedgerDB, the only permitted action on these resources is to free them. |
| 85 | +-- See 'ldbForkers'. |
77 | 86 | -- |
78 | | --- - In the ChainDB, closing the database does close all the followers and |
| 87 | +-- - In the ChainDB, closing the database also closes all followers and |
79 | 88 | -- iterators. |
80 | 89 | -- |
81 | | --- Ideally, we would change the ChainDB to follow the same approach as the |
82 | | --- LedgerDB. |
| 90 | +-- TODO: Ideally, the ChainDB and LedgerDB should follow a consistent |
| 91 | +-- approach to resource deallocation. |
83 | 92 | module Ouroboros.Consensus.Storage.ChainDB |
84 | 93 | ( module Ouroboros.Consensus.Storage.ChainDB.API |
85 | 94 | , module Ouroboros.Consensus.Storage.ChainDB.Impl |
|
0 commit comments