1313// See the License for the specific language governing permissions and
1414// limitations under the License.
1515
16+ //! # KV Cache Block Registration
17+ //!
18+ //! - This module is responsible for maintaining a registry of all blocks currently within a pool.
19+ //! This consists of two components: A global registry of all blocks, and a per-pool registry of blocks.
20+ //! - The global registry is a mapping of sequences hashes to registration handles. If two blocks in different pools
21+ //! have the same sequence hash, then they will share the same registration handle. The global registry is shared across all pools.
22+ //! - The per-pool registry is a mapping of sequence hashes to block handles. This is used to track which blocks are
23+ //! currently within a specific pool. The block handle is unique across pools, and is used to track the block's lifetime.
24+ //! - When a block is in the registered state, it has a unique block handle and a possibly shared registration handle.
25+ //!
26+ //! ## Workflow
27+ //!
28+ //! 1. When a block is registered into a pool, we create a unique block handle.
29+ //! 2. We then check the global registry to see if the block already exists in any other pool.
30+ //! 3. If it does, we use the existing registration handle. Otherwise, we create a new one.
31+ //! 4. When the block handle is dropped, it means that the block is no longer in the pool.
32+ //! 5. When the registration handle is dropped, it means that the block is no longer in any pool.
33+
1634use std:: {
1735 collections:: HashMap ,
1836 sync:: { Arc , Mutex , Weak } ,
@@ -80,6 +98,7 @@ impl BlockRegistry {
8098
8199 BlockState :: Complete ( state) => {
82100 let sequence_hash = state. token_block ( ) . sequence_hash ( ) ;
101+ // If an identical block already exists in this pool, return an error.
83102 if let Some ( handle) = self . blocks . get ( & sequence_hash) {
84103 if let Some ( _handle) = handle. upgrade ( ) {
85104 return Err ( BlockRegistationError :: BlockAlreadyRegistered ( sequence_hash) ) ;
@@ -91,25 +110,30 @@ impl BlockRegistry {
91110 let block_handle = Arc :: new ( ( ) ) ;
92111
93112 let reg_handle = ' reg_block: {
113+ // Now, check the global registry.
94114 let mut global_pool = self . global_pool . lock ( ) . unwrap ( ) ;
95115
116+ // If an identical block exists in other pool, use the same registration handle.
96117 if let Some ( handle) = global_pool. get ( & sequence_hash) {
97118 if let Some ( handle) = handle. upgrade ( ) {
98119 break ' reg_block handle;
99120 }
100121 }
101122
123+ // Otherwise, create a new registration handle.
102124 publish_handle = Some ( Self :: create_publish_handle (
103125 state. token_block ( ) ,
104126 self . event_manager . clone ( ) ,
105127 ) ) ;
106128 let reg_handle = publish_handle. as_ref ( ) . unwrap ( ) . remove_handle ( ) ;
107129
130+ // Insert the registration handle into the global registry.
108131 global_pool. insert ( sequence_hash, Arc :: downgrade ( & reg_handle) ) ;
109132
110133 reg_handle
111134 } ;
112135
136+ // Insert our block handle into the per-pool registry.
113137 self . blocks
114138 . insert ( sequence_hash, Arc :: downgrade ( & block_handle) ) ;
115139
0 commit comments