Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions internal/orchestrator/committer.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ func (c *Committer) Start(ctx context.Context) {
interval := time.Duration(c.triggerIntervalMs) * time.Millisecond

log.Debug().Msgf("Committer running")

// Clean up staging data before starting the committer
c.cleanupStagingData()

for {
select {
case <-ctx.Done():
Expand Down Expand Up @@ -112,6 +116,28 @@ func (c *Committer) Start(ctx context.Context) {
}
}

func (c *Committer) cleanupStagingData() {
// Get the last committed block number from main storage
latestCommittedBlockNumber, err := c.storage.MainStorage.GetMaxBlockNumber(c.rpc.GetChainID())
if err != nil {
log.Error().Msgf("Error getting latest committed block number: %v", err)
return
}

if latestCommittedBlockNumber.Sign() == 0 {
log.Debug().Msg("No blocks committed yet, skipping staging data cleanup")
return
}

// Delete all staging data older than the latest committed block number
if err := c.storage.StagingStorage.DeleteOlderThan(c.rpc.GetChainID(), latestCommittedBlockNumber); err != nil {
log.Error().Msgf("Error deleting staging data older than %v: %v", latestCommittedBlockNumber, err)
return
}

log.Info().Msgf("Deleted staging data older than or equal to %v", latestCommittedBlockNumber)
}

func (c *Committer) getBlockNumbersToCommit(ctx context.Context) ([]*big.Int, error) {
startTime := time.Now()
defer func() {
Expand Down
6 changes: 6 additions & 0 deletions internal/orchestrator/committer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,9 @@ func TestStartCommitter(t *testing.T) {
mockRPC.EXPECT().GetChainID().Return(chainID)
mockMainStorage.EXPECT().GetMaxBlockNumber(chainID).Return(big.NewInt(100), nil)

// Add expectation for DeleteOlderThan call during cleanup
mockStagingStorage.On("DeleteOlderThan", chainID, big.NewInt(100)).Return(nil)

blockData := []common.BlockData{
{Block: common.Block{Number: big.NewInt(101)}},
{Block: common.Block{Number: big.NewInt(102)}},
Expand Down Expand Up @@ -438,6 +441,9 @@ func TestCommitterRespectsSIGTERM(t *testing.T) {
mockRPC.EXPECT().GetChainID().Return(chainID)
mockMainStorage.EXPECT().GetMaxBlockNumber(chainID).Return(big.NewInt(100), nil)

// Add expectation for DeleteOlderThan call during cleanup
mockStagingStorage.On("DeleteOlderThan", chainID, big.NewInt(100)).Return(nil)

blockData := []common.BlockData{
{Block: common.Block{Number: big.NewInt(101)}},
{Block: common.Block{Number: big.NewInt(102)}},
Expand Down
11 changes: 11 additions & 0 deletions internal/storage/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -2114,6 +2114,17 @@ func (c *ClickHouseConnector) GetFullBlockData(chainId *big.Int, blockNumbers []
return blockData, nil
}

func (c *ClickHouseConnector) DeleteOlderThan(chainId *big.Int, blockNumber *big.Int) error {
query := fmt.Sprintf(`
INSERT INTO %s.block_data (chain_id, block_number, is_deleted)
SELECT chain_id, block_number, 1
FROM %s.block_data
WHERE chain_id = ? AND block_number <= ? AND is_deleted = 0
GROUP BY chain_id, block_number
`, c.cfg.Database, c.cfg.Database)
return c.conn.Exec(context.Background(), query, chainId, blockNumber)
}

// Helper function to test query generation
func (c *ClickHouseConnector) TestQueryGeneration(table, columns string, qf QueryFilter) string {
return c.buildQuery(table, columns, qf)
Expand Down
1 change: 1 addition & 0 deletions internal/storage/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ type IStagingStorage interface {
GetStagingData(qf QueryFilter) (data []common.BlockData, err error)
DeleteStagingData(data []common.BlockData) error
GetLastStagedBlockNumber(chainId *big.Int, rangeStart *big.Int, rangeEnd *big.Int) (maxBlockNumber *big.Int, err error)
DeleteOlderThan(chainId *big.Int, blockNumber *big.Int) error
}

type IMainStorage interface {
Expand Down
6 changes: 6 additions & 0 deletions internal/storage/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,12 @@ func (p *PostgresConnector) GetLastStagedBlockNumber(chainId *big.Int, rangeStar
return blockNumber, nil
}

func (p *PostgresConnector) DeleteOlderThan(chainId *big.Int, blockNumber *big.Int) error {
query := `DELETE FROM block_data WHERE chain_id = $1 AND block_number <= $2`
_, err := p.db.Exec(query, chainId.String(), blockNumber.String())
return err
}

// Close closes the database connection
func (p *PostgresConnector) Close() error {
return p.db.Close()
Expand Down
47 changes: 47 additions & 0 deletions test/mocks/MockIStagingStorage.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.