Skip to content

Commit 3048226

Browse files
fab-10matthew1001
authored andcommitted
Expose transaction count by type metrics for the layered txpool (besu-eth#6903)
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
1 parent 7ef6022 commit 3048226

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
- Update Web3j dependencies [#6811](https://github.com/hyperledger/besu/pull/6811)
3838
- Add `tx-pool-blob-price-bump` option to configure the price bump percentage required to replace blob transactions (by default 100%) [#6874](https://github.com/hyperledger/besu/pull/6874)
3939
- Log detailed timing of block creation steps [#6880](https://github.com/hyperledger/besu/pull/6880)
40+
- Expose transaction count by type metrics for the layered txpool [#6903](https://github.com/hyperledger/besu/pull/6903)
4041

4142
### Bug fixes
4243
- Fix txpool dump/restore race condition [#6665](https://github.com/hyperledger/besu/pull/6665)

ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/TransactionPoolMetrics.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515
package org.hyperledger.besu.ethereum.eth.transactions;
1616

17+
import org.hyperledger.besu.datatypes.TransactionType;
1718
import org.hyperledger.besu.ethereum.transaction.TransactionInvalidReason;
1819
import org.hyperledger.besu.metrics.BesuMetricCategory;
1920
import org.hyperledger.besu.metrics.ReplaceableDoubleSupplier;
@@ -27,6 +28,7 @@
2728
import java.util.Map;
2829
import java.util.function.DoubleSupplier;
2930

31+
import org.apache.commons.lang3.tuple.Pair;
3032
import org.slf4j.Logger;
3133
import org.slf4j.LoggerFactory;
3234

@@ -43,12 +45,15 @@ public class TransactionPoolMetrics {
4345
private final LabelledMetric<Counter> rejectedCounter;
4446
private final LabelledGauge spaceUsed;
4547
private final LabelledGauge transactionCount;
48+
private final LabelledGauge transactionCountByType;
4649
private final LabelledGauge uniqueSenderCount;
4750
private final LabelledMetric<Counter> expiredMessagesCounter;
4851
private final Map<String, RunnableCounter> expiredMessagesRunnableCounters = new HashMap<>();
4952
private final LabelledMetric<Counter> alreadySeenTransactionsCounter;
5053
private final Map<String, ReplaceableDoubleSupplier> spaceUsedSuppliers = new HashMap<>();
5154
private final Map<String, ReplaceableDoubleSupplier> transactionCountSuppliers = new HashMap<>();
55+
private final Map<Pair<String, TransactionType>, ReplaceableDoubleSupplier>
56+
transactionCountByTypeSuppliers = new HashMap<>();
5257
private final Map<String, ReplaceableDoubleSupplier> uniqueSendersSuppliers = new HashMap<>();
5358

5459
public TransactionPoolMetrics(final MetricsSystem metricsSystem) {
@@ -97,6 +102,14 @@ public TransactionPoolMetrics(final MetricsSystem metricsSystem) {
97102
"The number of transactions currently present in the layer",
98103
"layer");
99104

105+
transactionCountByType =
106+
metricsSystem.createLabelledGauge(
107+
BesuMetricCategory.TRANSACTION_POOL,
108+
"number_of_transactions_by_type",
109+
"The number of transactions, of a specified type, currently present in the layer",
110+
"layer",
111+
"type");
112+
100113
uniqueSenderCount =
101114
metricsSystem.createLabelledGauge(
102115
BesuMetricCategory.TRANSACTION_POOL,
@@ -136,6 +149,20 @@ public void initSpaceUsed(final DoubleSupplier spaceUsedSupplier, final String l
136149
});
137150
}
138151

152+
public void initTransactionCountByType(
153+
final DoubleSupplier spaceUsedSupplier, final String layer, final TransactionType type) {
154+
transactionCountByTypeSuppliers.compute(
155+
Pair.of(layer, type),
156+
(unused, existingSupplier) -> {
157+
if (existingSupplier == null) {
158+
final var newSupplier = new ReplaceableDoubleSupplier(spaceUsedSupplier);
159+
transactionCountByType.labels(newSupplier, layer, type.name());
160+
return newSupplier;
161+
}
162+
return existingSupplier.replaceDoubleSupplier(spaceUsedSupplier);
163+
});
164+
}
165+
139166
public void initTransactionCount(
140167
final DoubleSupplier transactionCountSupplier, final String layer) {
141168
transactionCountSuppliers.compute(

ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/layered/AbstractTransactionsLayer.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import org.hyperledger.besu.datatypes.Address;
2828
import org.hyperledger.besu.datatypes.Hash;
29+
import org.hyperledger.besu.datatypes.TransactionType;
2930
import org.hyperledger.besu.ethereum.core.BlockHeader;
3031
import org.hyperledger.besu.ethereum.core.Transaction;
3132
import org.hyperledger.besu.ethereum.eth.transactions.BlobCache;
@@ -39,6 +40,7 @@
3940
import org.hyperledger.besu.util.Subscribers;
4041

4142
import java.util.ArrayList;
43+
import java.util.Arrays;
4244
import java.util.HashMap;
4345
import java.util.List;
4446
import java.util.Map;
@@ -74,7 +76,7 @@ public abstract class AbstractTransactionsLayer implements TransactionsLayer {
7476
private OptionalLong nextLayerOnAddedListenerId = OptionalLong.empty();
7577
private OptionalLong nextLayerOnDroppedListenerId = OptionalLong.empty();
7678
protected long spaceUsed = 0;
77-
79+
protected final int[] txCountByType = new int[TransactionType.values().length];
7880
private final BlobCache blobCache;
7981

8082
protected AbstractTransactionsLayer(
@@ -91,6 +93,11 @@ protected AbstractTransactionsLayer(
9193
metrics.initSpaceUsed(this::getLayerSpaceUsed, name());
9294
metrics.initTransactionCount(pendingTransactions::size, name());
9395
metrics.initUniqueSenderCount(txsBySender::size, name());
96+
Arrays.stream(TransactionType.values())
97+
.forEach(
98+
type ->
99+
metrics.initTransactionCountByType(
100+
() -> txCountByType[type.ordinal()], name(), type));
94101
this.blobCache = blobCache;
95102
}
96103

@@ -101,6 +108,7 @@ public void reset() {
101108
pendingTransactions.clear();
102109
txsBySender.clear();
103110
spaceUsed = 0;
111+
Arrays.fill(txCountByType, 0);
104112
nextLayer.reset();
105113
}
106114

@@ -286,7 +294,7 @@ private void processAdded(final PendingTransaction addedTx) {
286294
pendingTransactions.put(addedTx.getHash(), addedTx);
287295
final var senderTxs = txsBySender.computeIfAbsent(addedTx.getSender(), s -> new TreeMap<>());
288296
senderTxs.put(addedTx.getNonce(), addedTx);
289-
increaseSpaceUsed(addedTx);
297+
increaseCounters(addedTx);
290298
metrics.incrementAdded(addedTx, name());
291299
internalAdd(senderTxs, addedTx);
292300
}
@@ -332,7 +340,7 @@ private void evict(final long spaceToFree, final int txsToEvict) {
332340

333341
protected void replaced(final PendingTransaction replacedTx) {
334342
pendingTransactions.remove(replacedTx.getHash());
335-
decreaseSpaceUsed(replacedTx);
343+
decreaseCounters(replacedTx);
336344
metrics.incrementRemoved(replacedTx, REPLACED.label(), name());
337345
internalReplaced(replacedTx);
338346
notifyTransactionDropped(replacedTx);
@@ -368,7 +376,7 @@ protected PendingTransaction processRemove(
368376
final PendingTransaction removedTx = pendingTransactions.remove(transaction.getHash());
369377

370378
if (removedTx != null) {
371-
decreaseSpaceUsed(removedTx);
379+
decreaseCounters(removedTx);
372380
metrics.incrementRemoved(removedTx, removalReason.label(), name());
373381
internalRemove(senderTxs, removedTx, removalReason);
374382
}
@@ -381,7 +389,7 @@ protected PendingTransaction processEvict(
381389
final RemovalReason reason) {
382390
final PendingTransaction removedTx = pendingTransactions.remove(evictedTx.getHash());
383391
if (removedTx != null) {
384-
decreaseSpaceUsed(evictedTx);
392+
decreaseCounters(evictedTx);
385393
metrics.incrementRemoved(evictedTx, reason.label(), name());
386394
internalEvict(senderTxs, removedTx);
387395
}
@@ -467,12 +475,14 @@ protected abstract void internalRemove(
467475

468476
protected abstract PendingTransaction getEvictable();
469477

470-
protected void increaseSpaceUsed(final PendingTransaction pendingTransaction) {
478+
protected void increaseCounters(final PendingTransaction pendingTransaction) {
471479
spaceUsed += pendingTransaction.memorySize();
480+
++txCountByType[pendingTransaction.getTransaction().getType().ordinal()];
472481
}
473482

474-
protected void decreaseSpaceUsed(final PendingTransaction pendingTransaction) {
483+
protected void decreaseCounters(final PendingTransaction pendingTransaction) {
475484
spaceUsed -= pendingTransaction.memorySize();
485+
--txCountByType[pendingTransaction.getTransaction().getType().ordinal()];
476486
}
477487

478488
protected abstract long cacheFreeSpace();

0 commit comments

Comments
 (0)