Skip to content

Commit 537f814

Browse files
authored
refactor: optimzed database reads (#12)
## Description This PR removes the monitoring of the total blocks that where indexed in order to reduce the CPU usage by the database. Additionally ensure that all the metrics that are computed by reading from the database are updated only if the monitoring is enabled. CPU Usage difference: ![cpu-usage](https://github.com/user-attachments/assets/adbee021-1d88-4a50-a148-1037be0b5b00) Closes: API-85 ## Checklist - [x] Targeted PR against correct branch. - [ ] Linked to Github issue with discussion and accepted design OR link to spec that describes this work. - [ ] Wrote unit tests. - [x] Re-reviewed `Files changed` in the Github PR explorer.
1 parent 62b91ca commit 537f814

File tree

2 files changed

+7
-17
lines changed

2 files changed

+7
-17
lines changed

logging/prometheus.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,6 @@ var ErrorCount = prometheus.NewCounter(
3939
},
4040
)
4141

42-
var DBBlockCount = prometheus.NewGaugeVec(
43-
prometheus.GaugeOpts{
44-
Name: "juno_db_total_blocks",
45-
Help: "Total number of blocks in database.",
46-
},
47-
[]string{"total_blocks_in_db"},
48-
)
49-
5042
var RPCRequestErrors = prometheus.NewCounter(
5143
prometheus.CounterOpts{
5244
Name: "juno_rpc_errors_total",
@@ -101,7 +93,6 @@ func init() {
10193
prometheus.MustRegister(WorkerCount)
10294
prometheus.MustRegister(WorkerHeight)
10395
prometheus.MustRegister(ErrorCount)
104-
prometheus.MustRegister(DBBlockCount)
10596
prometheus.MustRegister(DBLatestHeight)
10697
prometheus.MustRegister(RPCRequestErrors)
10798
prometheus.MustRegister(DBOperationErrors)

parser/worker.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ func (w Worker) HandleGenesis(genesisDoc *tmtypes.GenesisDoc, appState map[strin
197197
// consensus public key. An error is returned if the public key cannot be Bech32
198198
// encoded or if the DB write fails.
199199
func (w Worker) SaveValidators(vals []*tmtypes.Validator) error {
200-
var validators = make([]*types.Validator, len(vals))
200+
validators := make([]*types.Validator, len(vals))
201201
for index, val := range vals {
202202
consAddr := sdk.NewConsAddress(val.Address).String()
203203

@@ -372,14 +372,13 @@ func (w Worker) ExportTxs(txs []*types.Tx) error {
372372
}
373373
}
374374

375-
totalBlocks := w.db.GetTotalBlocks()
376-
logging.DBBlockCount.WithLabelValues("total_blocks_in_db").Set(float64(totalBlocks))
377-
378-
dbLatestHeight, err := w.db.GetLastBlockHeight()
379-
if err != nil {
380-
return err
375+
if w.cfg.Monitoring.Enabled {
376+
dbLatestHeight, err := w.db.GetLastBlockHeight()
377+
if err != nil {
378+
return err
379+
}
380+
logging.DBLatestHeight.WithLabelValues("db_latest_height").Set(float64(dbLatestHeight))
381381
}
382-
logging.DBLatestHeight.WithLabelValues("db_latest_height").Set(float64(dbLatestHeight))
383382

384383
return nil
385384
}

0 commit comments

Comments
 (0)