Skip to content

Commit 1cbd361

Browse files
committedJul 24, 2024·
fix: panic in prometheus
1 parent c2e6331 commit 1cbd361

File tree

3 files changed

+31
-29
lines changed

3 files changed

+31
-29
lines changed
 

‎mempool/cat/pool.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,14 @@ func (txmp *TxPool) purgeExpiredTxs(blockHeight int64) {
783783
for _, tx := range purgedTxs {
784784
txmp.evictedTxCache.Push(tx.key)
785785
}
786-
txmp.metrics.EvictedTxs.Add(float64(numExpired))
786+
txmp.metrics.ExpiredTxs.Add(float64(numExpired))
787+
788+
// purge old evicted and seen transactions
789+
if txmp.config.TTLDuration == 0 {
790+
// ensure that seenByPeersSet are eventually pruned
791+
expirationAge = now.Add(-time.Hour)
792+
}
793+
txmp.seenByPeersSet.Prune(expirationAge)
787794
}
788795

789796
func (txmp *TxPool) notifyTxsAvailable() {

‎mempool/metrics.go

+15-16
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,6 @@ const (
1818
// MetricsSubsystem is a subsystem shared by all metrics exposed by this
1919
// package.
2020
MetricsSubsystem = "mempool"
21-
22-
TypeLabel = "type"
23-
24-
FailedPrecheck = "precheck"
25-
FailedAdding = "adding"
26-
FailedRecheck = "recheck"
27-
28-
EvictedNewTxFullMempool = "full-removed-incoming"
29-
EvictedExistingTxFullMempool = "full-removed-existing"
30-
EvictedTxExpiredBlocks = "expired-ttl-blocks"
31-
EvictedTxExpiredTime = "expired-ttl-time"
3221
)
3322

3423
// Metrics contains metrics exposed by this package.
@@ -49,10 +38,13 @@ type Metrics struct {
4938

5039
// EvictedTxs defines the number of evicted transactions. These are valid
5140
// transactions that passed CheckTx and existed in the mempool but were later
52-
// evicted to make room for higher priority valid transactions that passed
53-
// CheckTx.
41+
// evicted to make room for higher priority valid transactions
5442
EvictedTxs metrics.Counter
5543

44+
// ExpiredTxs defines transactions that were removed from the mempool due
45+
// to a TTL
46+
ExpiredTxs metrics.Counter
47+
5648
// SuccessfulTxs defines the number of transactions that successfully made
5749
// it into a block.
5850
SuccessfulTxs metrics.Counter
@@ -82,7 +74,6 @@ func PrometheusMetrics(namespace string, labelsAndValues ...string) *Metrics {
8274
for i := 0; i < len(labelsAndValues); i += 2 {
8375
labels = append(labels, labelsAndValues[i])
8476
}
85-
typedCounterLabels := append(append(make([]string, 0, len(labels)+1), labels...), TypeLabel)
8677
return &Metrics{
8778
Size: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
8879
Namespace: namespace,
@@ -111,14 +102,21 @@ func PrometheusMetrics(namespace string, labelsAndValues ...string) *Metrics {
111102
Subsystem: MetricsSubsystem,
112103
Name: "failed_txs",
113104
Help: "Number of failed transactions.",
114-
}, typedCounterLabels).With(labelsAndValues...),
105+
}, labels).With(labelsAndValues...),
115106

116107
EvictedTxs: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
117108
Namespace: namespace,
118109
Subsystem: MetricsSubsystem,
119110
Name: "evicted_txs",
120111
Help: "Number of evicted transactions.",
121-
}, typedCounterLabels).With(labelsAndValues...),
112+
}, labels).With(labelsAndValues...),
113+
114+
ExpiredTxs: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
115+
Namespace: namespace,
116+
Subsystem: MetricsSubsystem,
117+
Name: "expired_txs",
118+
Help: "Number of expired transactions.",
119+
}, labels).With(labelsAndValues...),
122120

123121
SuccessfulTxs: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
124122
Namespace: namespace,
@@ -165,6 +163,7 @@ func NopMetrics() *Metrics {
165163
TxSizeBytes: discard.NewHistogram(),
166164
FailedTxs: discard.NewCounter(),
167165
EvictedTxs: discard.NewCounter(),
166+
ExpiredTxs: discard.NewCounter(),
168167
SuccessfulTxs: discard.NewCounter(),
169168
RecheckTimes: discard.NewCounter(),
170169
AlreadySeenTxs: discard.NewCounter(),

‎mempool/v1/mempool.go

+8-12
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ func (txmp *TxMempool) CheckTx(tx types.Tx, cb func(*abci.Response), txInfo memp
204204
// If a precheck hook is defined, call it before invoking the application.
205205
if txmp.preCheck != nil {
206206
if err := txmp.preCheck(tx); err != nil {
207-
txmp.metrics.FailedTxs.With(mempool.TypeLabel, mempool.FailedPrecheck).Add(1)
207+
txmp.metrics.FailedTxs.Add(1)
208208
return 0, mempool.ErrPreCheck{Reason: err}
209209
}
210210
}
@@ -500,7 +500,7 @@ func (txmp *TxMempool) addNewTransaction(wtx *WrappedTx, checkTxRes *abci.Respon
500500
"post_check_err", err,
501501
)
502502

503-
txmp.metrics.FailedTxs.With(mempool.TypeLabel, mempool.FailedAdding).Add(1)
503+
txmp.metrics.FailedTxs.Add(1)
504504

505505
// Remove the invalid transaction from the cache, unless the operator has
506506
// instructed us to keep invalid transactions.
@@ -568,7 +568,7 @@ func (txmp *TxMempool) addNewTransaction(wtx *WrappedTx, checkTxRes *abci.Respon
568568
checkTxRes.MempoolError =
569569
fmt.Sprintf("rejected valid incoming transaction; mempool is full (%X)",
570570
wtx.tx.Hash())
571-
txmp.metrics.EvictedTxs.With(mempool.TypeLabel, mempool.EvictedNewTxFullMempool).Add(1)
571+
txmp.metrics.EvictedTxs.Add(1)
572572
// Add it to evicted transactions cache
573573
txmp.evictedTxs.Push(wtx.tx)
574574
return
@@ -602,7 +602,7 @@ func (txmp *TxMempool) addNewTransaction(wtx *WrappedTx, checkTxRes *abci.Respon
602602
)
603603
txmp.removeTxByElement(vic)
604604
txmp.cache.Remove(w.tx)
605-
txmp.metrics.EvictedTxs.With(mempool.TypeLabel, mempool.EvictedExistingTxFullMempool).Add(1)
605+
txmp.metrics.EvictedTxs.Add(1)
606606
// Add it to evicted transactions cache
607607
txmp.evictedTxs.Push(w.tx)
608608
// We may not need to evict all the eligible transactions. Bail out
@@ -681,7 +681,7 @@ func (txmp *TxMempool) handleRecheckResult(tx types.Tx, checkTxRes *abci.Respons
681681
"code", checkTxRes.Code,
682682
)
683683
txmp.removeTxByElement(elt)
684-
txmp.metrics.FailedTxs.With(mempool.TypeLabel, mempool.FailedRecheck).Add(1)
684+
txmp.metrics.FailedTxs.Add(1)
685685
if !txmp.config.KeepInvalidTxsInCache {
686686
txmp.cache.Remove(wtx.tx)
687687
}
@@ -791,16 +791,12 @@ func (txmp *TxMempool) purgeExpiredTxs(blockHeight int64) {
791791
next := cur.Next()
792792

793793
w := cur.Value.(*WrappedTx)
794-
if txmp.config.TTLNumBlocks > 0 && (blockHeight-w.height) > txmp.config.TTLNumBlocks {
794+
if txmp.config.TTLNumBlocks > 0 && (blockHeight-w.height) > txmp.config.TTLNumBlocks ||
795+
txmp.config.TTLDuration > 0 && now.Sub(w.timestamp) > txmp.config.TTLDuration {
795796
txmp.removeTxByElement(cur)
796797
txmp.cache.Remove(w.tx)
797-
txmp.metrics.EvictedTxs.With(mempool.TypeLabel, mempool.EvictedTxExpiredBlocks).Add(1)
798798
txmp.evictedTxs.Push(w.tx)
799-
} else if txmp.config.TTLDuration > 0 && now.Sub(w.timestamp) > txmp.config.TTLDuration {
800-
txmp.removeTxByElement(cur)
801-
txmp.cache.Remove(w.tx)
802-
txmp.evictedTxs.Push(w.tx)
803-
txmp.metrics.EvictedTxs.With(mempool.TypeLabel, mempool.EvictedTxExpiredTime).Add(1)
799+
txmp.metrics.ExpiredTxs.Add(1)
804800
}
805801
cur = next
806802
}

0 commit comments

Comments
 (0)
Please sign in to comment.