Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Aggsender high cpu usage #235

Merged
merged 10 commits into from
Dec 9, 2024
13 changes: 11 additions & 2 deletions aggsender/block_notifier_polling.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const (
minBlockInterval = time.Second
// maxBlockInterval is the maximum interval at which the AggSender will check for new blocks
maxBlockInterval = time.Minute
// Percentage period of reach the next block
percentForNextBlock = 80
)

type ConfigBlockNotifierPolling struct {
Expand Down Expand Up @@ -147,6 +149,13 @@ func (b *BlockNotifierPolling) step(ctx context.Context,
BlockNumber: currentBlock.Number.Uint64(),
BlockFinalityType: b.config.BlockFinalityType,
}
if previousState.lastBlockSeen > currentBlock.Number.Uint64() {
b.logger.Warnf("Block number decreased [finality:%s]: %d -> %d",
b.config.BlockFinalityType, previousState.lastBlockSeen, currentBlock.Number.Uint64())
// It start from scratch because something fails in calculation of block period
newState := previousState.intialBlock(currentBlock.Number.Uint64())
return b.nextBlockRequestDelay(nil, nil), newState, eventToEmit
}

if currentBlock.Number.Uint64()-previousState.lastBlockSeen != 1 {
b.logger.Warnf("Missed block(s) [finality:%s]: %d -> %d",
Expand All @@ -164,7 +173,7 @@ func (b *BlockNotifierPolling) step(ctx context.Context,

func (b *BlockNotifierPolling) nextBlockRequestDelay(status *blockNotifierPollingInternalStatus,
err error) time.Duration {
if b.config.CheckNewBlockInterval == AutomaticBlockInterval {
if b.config.CheckNewBlockInterval != AutomaticBlockInterval {
return b.config.CheckNewBlockInterval
}
// Initial stages wait the minimum interval to increas accuracy
Expand All @@ -179,7 +188,7 @@ func (b *BlockNotifierPolling) nextBlockRequestDelay(status *blockNotifierPollin
now := timeNowFunc()
expectedTimeNextBlock := status.lastBlockTime.Add(*status.previousBlockTime)
distanceToNextBlock := expectedTimeNextBlock.Sub(now)
interval := distanceToNextBlock * 4 / 5 //nolint:mnd // 80% of for reach the next block
interval := distanceToNextBlock * percentForNextBlock / 100 //nolint:mnd // percent period for reach the next block
return max(minBlockInterval, min(maxBlockInterval, interval))
}

Expand Down
8 changes: 4 additions & 4 deletions aggsender/block_notifier_polling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,16 @@ func newBlockNotifierPollingTestData(t *testing.T, config *ConfigBlockNotifierPo
if config == nil {
config = &ConfigBlockNotifierPolling{
BlockFinalityType: etherman.LatestBlock,
CheckNewBlockInterval: time.Second,
CheckNewBlockInterval: 0,
}
}
EthClientMock := mocks.NewEthClient(t)
ethClientMock := mocks.NewEthClient(t)
logger := log.WithFields("test", "BlockNotifierPolling")
sut, err := NewBlockNotifierPolling(EthClientMock, *config, logger, nil)
sut, err := NewBlockNotifierPolling(ethClientMock, *config, logger, nil)
require.NoError(t, err)
return blockNotifierPollingTestData{
sut: sut,
ethClientMock: EthClientMock,
ethClientMock: ethClientMock,
ctx: context.TODO(),
}
}
2 changes: 1 addition & 1 deletion test/combinations/fork12-pessimistic.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
args:
agglayer_image: ghcr.io/agglayer/agglayer:0.2.0-rc.12
agglayer_image: ghcr.io/agglayer/agglayer:0.2.0-rc.20
cdk_erigon_node_image: hermeznetwork/cdk-erigon:v2.60.0-beta8
cdk_node_image: cdk
zkevm_bridge_proxy_image: haproxy:3.0-bookworm
Expand Down
Loading