Skip to content

Commit 128b327

Browse files
dnadalesjasagredo
authored andcommitted
Move part of the "Resource Management in the ChainDB" explanation
... to function 'runWith'.
1 parent be4003a commit 128b327

File tree

2 files changed

+45
-40
lines changed
  • ouroboros-consensus-diffusion/src/ouroboros-consensus-diffusion/Ouroboros/Consensus
  • ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage

2 files changed

+45
-40
lines changed

ouroboros-consensus-diffusion/src/ouroboros-consensus-diffusion/Ouroboros/Consensus/Node.hs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,48 @@ type NetworkAddr addr =
553553
-- network layer.
554554
--
555555
-- This function runs forever unless an exception is thrown.
556+
--
557+
-- This function spawns a resource registry (which we will refer to as
558+
-- __the consensus registry__) that will include the ChainDB as one of
559+
-- its resources. When the Consensus layer is shut down, the consensus
560+
-- resource registry will exit the scope of the 'withRegistry'
561+
-- function. This causes all resources allocated in the registry
562+
-- —including the ChainDB— to be closed.
563+
--
564+
-- During it's operation, different consensus threads will create
565+
-- resources associated with the ChainDB, eg Forkers in the LedgerDB,
566+
-- or Followers in the ChainDB. These resources are not created by the
567+
-- database themselves (LedgerDB, VolatileDB, and ImmutableDB). For
568+
-- example, chain selection opens a forker using the LedgerDB.
569+
-- Crucially, this means that clients creating these resources are
570+
-- instantiated after the ChainDB.
571+
--
572+
-- We rely on a specific sequence of events for this design to be correct:
573+
--
574+
-- - The ChainDB is only closed by exiting the scope of the consensus
575+
-- resource registry.
576+
--
577+
-- - If a client creates resources tied to any of the
578+
-- aforementioned databases and is forked into a separate thread,
579+
-- that thread is linked to the consensus registry. Because resources
580+
-- in a registry are deallocated in reverse order of allocation, any
581+
-- resources created by such threads will be deallocated before the
582+
-- ChainDB is closed, ensuring proper cleanup.
583+
--
584+
-- Currently, we have two distinct approaches to resource management
585+
-- and database closure:
586+
--
587+
-- - In the LedgerDB, closing the database does not close any resources
588+
-- created by its clients. We rely on the resource registry to deallocate
589+
-- these resources before the LedgerDB is closed. However, after closing
590+
-- the LedgerDB, the only permitted action on these resources is to free them.
591+
-- See 'ldbForkers'.
592+
--
593+
-- - In the ChainDB, closing the database also closes all followers and
594+
-- iterators.
595+
--
596+
-- TODO: Ideally, the ChainDB and LedgerDB should follow a consistent
597+
-- approach to resource deallocation.
556598
runWith ::
557599
forall m addrNTN addrNTC blk p2p.
558600
( RunNode blk

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

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -48,47 +48,10 @@
4848
-- - The ChainDB is used to create 'Follower's (which in turn contain
4949
-- 'Iterator's).
5050
--
51-
-- These resources must eventually be freed.
51+
-- These resources must eventually be freed. See function
52+
-- 'Ouroboros.Consensus.Node.runWith' for an example of an approach
53+
-- taken to resource management using a resource registry.
5254
--
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.
59-
--
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:
67-
--
68-
-- - The ChainDB is only closed by exiting the scope of the consensus
69-
-- resource registry.
70-
--
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.
77-
--
78-
-- Currently, we have two distinct approaches to resource management
79-
-- and database closure:
80-
--
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'.
86-
--
87-
-- - In the ChainDB, closing the database also closes all followers and
88-
-- iterators.
89-
--
90-
-- TODO: Ideally, the ChainDB and LedgerDB should follow a consistent
91-
-- approach to resource deallocation.
9255
module Ouroboros.Consensus.Storage.ChainDB
9356
( module Ouroboros.Consensus.Storage.ChainDB.API
9457
, module Ouroboros.Consensus.Storage.ChainDB.Impl

0 commit comments

Comments
 (0)