@@ -3,6 +3,7 @@ import { HyperionConfig, NodeAttributeRequirement, TieredIndexAllocationSettings
33import { ConnectionManager } from "../connections/manager.class.js" ;
44import { hLog } from "../helpers/common_functions.js" ;
55import { HyperionMaster } from "./master.js" ;
6+ import { delay } from "lodash" ;
67
78interface IndexData {
89 index : string ;
@@ -27,6 +28,8 @@ export class HyperionLifecycleManager {
2728 autoPrune = false ;
2829 totalDeletedBytes = 0 ;
2930
31+ private lastPruningCheckBlockNum = 0 ;
32+
3033 constructor ( master : HyperionMaster ) {
3134 this . master = master ;
3235 this . conf = this . master . conf ;
@@ -72,6 +75,40 @@ export class HyperionLifecycleManager {
7275 }
7376 }
7477
78+ async notifyConsumedBlock ( blockNum : number ) {
79+ if ( ! this . autoPrune ) {
80+ return ;
81+ }
82+
83+ const partitionSize = this . conf . settings . index_partition_size ;
84+ if ( partitionSize <= 0 ) {
85+ return ; // Pruning is based on partitions, so this is required.
86+ }
87+
88+ if ( this . lastPruningCheckBlockNum === 0 ) {
89+ // First block seen, initialize and return.
90+ this . lastPruningCheckBlockNum = blockNum ;
91+ return ;
92+ }
93+
94+ // Calculate partitions based on 1-based block numbers
95+ const oldPartition = Math . floor ( ( this . lastPruningCheckBlockNum - 1 ) / partitionSize ) ;
96+ const newPartition = Math . floor ( ( blockNum - 1 ) / partitionSize ) ;
97+
98+ if ( newPartition > oldPartition ) {
99+ hLog ( `New index partition boundary crossed at block ${ blockNum } . Triggering pruning check in 5 seconds...` ) ;
100+ setTimeout ( ( ) => {
101+ this . checkPruning ( ) . catch ( ( err ) => {
102+ hLog ( `Error during pruning check: ${ err . message } ` ) ;
103+ } ) ;
104+ } , 5000 ) ;
105+ }
106+
107+ if ( blockNum > this . lastPruningCheckBlockNum ) {
108+ this . lastPruningCheckBlockNum = blockNum ;
109+ }
110+ }
111+
75112 startAllocationMonitoring ( ) {
76113 if ( ! this . allocationEnabled || ! this . allocation ) {
77114 return ;
@@ -128,7 +165,9 @@ export class HyperionLifecycleManager {
128165 bytes : 'b' ,
129166 index : `${ this . master . chain } -block-${ this . conf . settings . index_version } *`
130167 } ) ;
131- console . dir ( blockIndices , { depth : Infinity } ) ;
168+
169+ // console.dir(blockIndices, { depth: Infinity });
170+
132171 if ( blockIndices . length === 1 ) {
133172
134173 const indexName = blockIndices [ 0 ] . index ;
@@ -137,6 +176,8 @@ export class HyperionLifecycleManager {
137176 return ;
138177 }
139178
179+ hLog ( `Found block index: ${ indexName } ` ) ;
180+
140181 // Get the current head block number
141182 const chainInfo = await this . master . rpc . v1 . chain . get_info ( ) ;
142183
@@ -167,7 +208,7 @@ export class HyperionLifecycleManager {
167208 query : { bool : { must : [ { range : { block_num : { lte : finalBlockToKeep } } } ] } }
168209 } ) ;
169210
170- console . dir ( response , { depth : Infinity } ) ;
211+ // console.dir(response, { depth: Infinity });
171212
172213 const totalHits = response . hits . total as estypes . SearchTotalHits ;
173214 if ( totalHits && totalHits . value > 0 ) {
@@ -189,6 +230,7 @@ export class HyperionLifecycleManager {
189230
190231 } else if ( blockIndices . length > 1 ) {
191232 // Partitioned block index, we can prune by index
233+ hLog ( `Multiple block indices found. Pruning by index...` ) ;
192234 await this . pruneIndices ( 'block' ) ;
193235 }
194236 }
0 commit comments