Skip to content

Commit 525dbe9

Browse files
dnadalesjasagredo
authored andcommitted
Rework "Resource management in the ChainDB"
1 parent 517b501 commit 525dbe9

File tree

1 file changed

+34
-25
lines changed
  • ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage

1 file changed

+34
-25
lines changed

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/ChainDB.hs

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,48 +38,57 @@
3838
-- chain. 'Ouroboros.Consensus.Storage.ChainDB.API.ChainDB' defines the chain
3939
-- DB API.
4040
--
41-
-- == Resource management in the ChainDB
41+
-- == Resource Management in the ChainDB
4242
--
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):
4545
--
46-
-- - The LedgerDB is used to create 'Forker's
46+
-- - The LedgerDB is used to create 'Forker's.
4747
--
4848
-- - The ChainDB is used to create 'Follower's (which in turn contain
4949
-- 'Iterator's).
5050
--
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.
5552
--
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.
6059
--
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:
6267
--
6368
-- - The ChainDB is only closed by exiting the scope of the consensus
6469
-- resource registry.
6570
--
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.
7077
--
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:
7380
--
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'.
7786
--
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
7988
-- iterators.
8089
--
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.
8392
module Ouroboros.Consensus.Storage.ChainDB
8493
( module Ouroboros.Consensus.Storage.ChainDB.API
8594
, module Ouroboros.Consensus.Storage.ChainDB.Impl

0 commit comments

Comments
 (0)