Skip to content

Commit ecb60fb

Browse files
authored
add logs to detect time consuming (okx#766)
1 parent 203578b commit ecb60fb

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

zk/stages/stage_batches.go

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,17 +171,21 @@ func SpawnStageBatches(
171171
}
172172

173173
// get batch for batches progress
174+
getBatchNoByL2Blockstart := time.Now()
174175
stageProgressBatchNo, err := hermezDb.GetBatchNoByL2Block(stageProgressBlockNo)
175176
if err != nil && !errors.Is(err, hermez_db.ErrorNotStored) {
176177
return fmt.Errorf("GetBatchNoByL2Block: %w", err)
177178
}
179+
getBatchNoByL2BlockCost := time.Since(getBatchNoByL2Blockstart)
178180

179181
startSyncTime := time.Now()
180182

183+
getForkIdstart := time.Now()
181184
latestForkId, err := stages.GetStageProgress(tx, stages.ForkId)
182185
if err != nil {
183186
return fmt.Errorf("GetStageProgress: %w", err)
184187
}
188+
getForkIdCost := time.Since(getForkIdstart)
185189

186190
dsQueryClient, stopDsClient, err := newStreamClient(ctx, cfg, latestForkId)
187191
if err != nil {
@@ -197,14 +201,18 @@ func SpawnStageBatches(
197201
var highestDSL2Block uint64
198202
newBlockCheckStartTIme := time.Now()
199203
newBlockCheckCounter := 0
204+
getHighestDSL2Blockstart := time.Now()
205+
getHighestDSL2BlockCounter := 0
206+
var stats getHighestDSL2BlockStats
200207
for {
201208
select {
202209
case <-ctx.Done():
203210
return nil
204211
default:
205212
}
206213

207-
highestDSL2Block, err = getHighestDSL2Block(ctx, cfg, uint16(latestForkId))
214+
getHighestDSL2BlockCounter++
215+
highestDSL2Block, err = getHighestDSL2Block(logPrefix, ctx, cfg, uint16(latestForkId), &stats)
208216
if err != nil {
209217
// if we return error, stage will replay and block all other stages
210218
log.Warn(fmt.Sprintf("[%s] Failed to get latest l2 block from datastream: %v", logPrefix, err))
@@ -232,6 +240,7 @@ func SpawnStageBatches(
232240
// For X Layer
233241
time.Sleep(5 * time.Millisecond)
234242
}
243+
getHighestDSL2BlockCost := time.Since(getHighestDSL2Blockstart)
235244

236245
log.Debug(fmt.Sprintf("[%s] Highest block in db and datastream", logPrefix), "datastreamBlock", highestDSL2Block, "dbBlock", stageProgressBlockNo)
237246
unwindFn := func(unwindBlock uint64) (uint64, error) {
@@ -377,7 +386,7 @@ func SpawnStageBatches(
377386

378387
// stop printing blocks written progress routine
379388
elapsed := time.Since(startSyncTime)
380-
log.Info(fmt.Sprintf("[%s] Finished writing blocks", logPrefix), "blocksWritten", batchProcessor.TotalBlocksWritten(), "elapsed", elapsed)
389+
log.Info(fmt.Sprintf("[%s] Finished writing blocks", logPrefix), "blocksWritten", batchProcessor.TotalBlocksWritten(), "elapsed", elapsed, "getHighestDSL2BlockCounter", getHighestDSL2BlockCounter, "getHighestDSL2BlockCost", getHighestDSL2BlockCost, "getHighestDSL2BlockStats", stats.toString(), "getBatchNoByL2BlockCost", getBatchNoByL2BlockCost, "getForkIdCost", getForkIdCost)
381390

382391
if freshTx {
383392
if err := tx.Commit(); err != nil {
@@ -844,13 +853,32 @@ func newStreamClient(ctx context.Context, cfg BatchesCfg, latestForkId uint64) (
844853
return dsClient, stopFn, nil
845854
}
846855

847-
func getHighestDSL2Block(ctx context.Context, batchCfg BatchesCfg, latestFork uint16) (uint64, error) {
856+
type getHighestDSL2BlockStats struct {
857+
getSeqCost time.Duration
858+
getSeqCounter int
859+
dsStart time.Duration
860+
dsStartCounter int
861+
dsGetBlockCost time.Duration
862+
dsGetBlockCounter int
863+
dsStopCost time.Duration
864+
dsStopCounter int
865+
}
866+
867+
func (stats getHighestDSL2BlockStats) toString() string {
868+
return fmt.Sprintf("getHighestDSL2BlockStats {getSeqCost: %v, getSeqCounter: %d, dsStart: %v, dsStartCounter: %d, dsGetBlockCost: %v, dsGetBlockCounter: %d, dsStopCost: %v, dsStopCounter: %d}",
869+
stats.getSeqCost, stats.getSeqCounter, stats.dsStart, stats.dsStartCounter, stats.dsGetBlockCost, stats.dsGetBlockCounter, stats.dsStopCost, stats.dsStopCounter)
870+
}
871+
872+
func getHighestDSL2Block(logPrefix string, ctx context.Context, batchCfg BatchesCfg, latestFork uint16, stats *getHighestDSL2BlockStats) (uint64, error) {
848873
cfg := batchCfg.zkCfg
849874

850875
// first try the sequencer rpc endpoint, it might not have been upgraded to the
851876
// latest version yet so if we get an error back from this call we can try the older
852877
// method of calling the datastream directly
878+
getSeqStart := time.Now()
853879
highestBlock, err := GetSequencerHighestDataStreamBlock(cfg.L2RpcUrl)
880+
stats.getSeqCost += time.Since(getSeqStart)
881+
stats.getSeqCounter += 1
854882
if err == nil {
855883
return highestBlock, nil
856884
}
@@ -859,16 +887,26 @@ func getHighestDSL2Block(ctx context.Context, batchCfg BatchesCfg, latestFork ui
859887
// but we're going to open a new connection rather than use the one for syncing blocks.
860888
// This is so we can keep the logic simple and just dispose of the connection when we're done
861889
// greatly simplifying state juggling of the connection if it errors
890+
getDSStart := time.Now()
862891
dsClient := buildNewStreamClient(ctx, batchCfg, latestFork)
863-
if err = dsClient.Start(); err != nil {
892+
err = dsClient.Start()
893+
stats.dsStart += time.Since(getDSStart)
894+
stats.dsStartCounter += 1
895+
if err != nil {
864896
return 0, err
865897
}
866898
defer func() {
899+
dsStopStart := time.Now()
867900
if err := dsClient.Stop(); err != nil {
868901
log.Error("problem stopping datastream client looking up latest ds l2 block", "err", err)
869902
}
903+
stats.dsStopCost += time.Since(dsStopStart)
904+
stats.dsStopCounter += 1
870905
}()
906+
dsGetlockStart := time.Now()
871907
fullBlock, err := dsClient.GetLatestL2Block()
908+
stats.dsGetBlockCost += time.Since(dsGetlockStart)
909+
stats.dsGetBlockCounter += 1
872910
if err != nil {
873911
return 0, err
874912
}

0 commit comments

Comments
 (0)