Skip to content

Commit

Permalink
Add false get metrics in all places
Browse files Browse the repository at this point in the history
  • Loading branch information
zilm13 committed Sep 13, 2023
1 parent 41b7666 commit 4616204
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.apache.tuweni.bytes.Bytes32;
import org.hyperledger.besu.plugin.services.metrics.Counter;
import org.hyperledger.besu.plugin.services.metrics.LabelledMetric;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock;

Expand Down Expand Up @@ -85,6 +89,33 @@ static BlockProvider combined(
};
}

static BlockProvider meteredFalse(
final BlockProvider blockProvider,
final LabelledMetric<Counter> blocksLabelledCounter,
final BiFunction<Bytes32, SignedBeaconBlock, String> blockToSlotMetrics) {
return (final Set<Bytes32> blockRoots) -> {
final SafeFuture<Map<Bytes32, SignedBeaconBlock>> blocks =
blockProvider.getBlocks(blockRoots);
return blocks.thenPeek(
blocksMap -> {
blocksMap
.values()
.forEach(
block ->
blocksLabelledCounter
.labels(
"get",
"false",
blockToSlotMetrics.apply(block.getRoot(), block),
"meteredFalse")
.inc());
IntStream.range(0, blocksMap.size() - blockRoots.size())
.forEach(
__ -> blocksLabelledCounter.labels("get", "false", "-1", "meteredFalse").inc());
});
};
}

SafeFuture<Map<Bytes32, SignedBeaconBlock>> getBlocks(final Set<Bytes32> blockRoots);

default SafeFuture<Map<Bytes32, SignedBeaconBlock>> getBlocks(final List<Bytes32> blockRoots) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
Expand All @@ -27,12 +26,12 @@
final class MeteredMap<K, V> implements Map<K, V> {
final Map<K, V> delegate;
final LabelledMetric<Counter> labelledCounter;
final BiFunction<K, Optional<V>, Integer> valueFunction;
final BiFunction<K, V, String> valueFunction;

public MeteredMap(
final Map<K, V> delegate,
final LabelledMetric<Counter> labelledCounter,
final BiFunction<K, Optional<V>, Integer> valueFunction) {
final BiFunction<K, V, String> valueFunction) {
this.delegate = delegate;
this.labelledCounter = labelledCounter;
this.valueFunction = valueFunction;
Expand Down Expand Up @@ -114,13 +113,12 @@ public boolean isEmpty() {
}

@Override
@SuppressWarnings("unchecked")
public boolean containsKey(final Object key) {
final boolean containsKey = delegate.containsKey(key);
if (containsKey) {
labelledCounter.labels("contains", "true").inc();
labelledCounter.labels("contains", "true", "-1", "containsKey").inc();
} else {
labelledCounter.labels("contains", "false").inc();
labelledCounter.labels("contains", "false", "-1", "containsKey").inc();
}
return containsKey;
}
Expand All @@ -135,13 +133,7 @@ public boolean containsValue(final Object value) {
public V get(final Object key) {
final V maybeValue = delegate.get(key);
if (maybeValue != null) {
labelledCounter
.labels("get", String.valueOf(valueFunction.apply((K) key, Optional.of(maybeValue))))
.inc();
} else {
labelledCounter
.labels("get", String.valueOf(valueFunction.apply((K) key, Optional.empty())))
.inc();
labelledCounter.labels("get", "true", valueFunction.apply((K) key, maybeValue), "get").inc();
}
return maybeValue;
}
Expand All @@ -150,7 +142,7 @@ public V get(final Object key) {
public V put(final K key, final V value) {
final V oldValue = delegate.put(key, value);
labelledCounter
.labels("put", String.valueOf(valueFunction.apply(key, Optional.of(value))))
.labels("put", String.valueOf(oldValue != null), valueFunction.apply(key, value), "put")
.inc();
return oldValue;
}
Expand All @@ -161,11 +153,7 @@ public V remove(final Object key) {
final V maybeValue = delegate.remove(key);
if (maybeValue != null) {
labelledCounter
.labels("remove", String.valueOf(valueFunction.apply((K) key, Optional.of(maybeValue))))
.inc();
} else {
labelledCounter
.labels("remove", String.valueOf(valueFunction.apply((K) key, Optional.empty())))
.labels("remove", "true", valueFunction.apply((K) key, maybeValue), "remove")
.inc();
}
return maybeValue;
Expand Down
45 changes: 29 additions & 16 deletions storage/src/main/java/tech/pegasys/teku/storage/store/Store.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -111,6 +112,8 @@ class Store implements UpdatableStore {
final CachingTaskQueue<SlotAndBlockRoot, BeaconState> checkpointStates;
VoteTracker[] votes;
UInt64 highestVotedValidatorIndex;
final LabelledMetric<Counter> blocksLabelledCounter;
final BiFunction<Bytes32, SignedBeaconBlock, String> blockToSlotMetrics;

private Store(
final MetricsSystem metricsSystem,
Expand Down Expand Up @@ -155,25 +158,24 @@ private Store(
this.genesisTime = genesisTime;
this.justifiedCheckpoint = justifiedCheckpoint;
this.bestJustifiedCheckpoint = bestJustifiedCheckpoint;
final LabelledMetric<Counter> blocksLabelledCounter =
this.blocksLabelledCounter =
metricsSystem.createLabelledCounter(
TekuMetricCategory.STORAGE,
"store_blocks_hits",
"store_blocks_cache",
"Number of Store blocks hits",
"type",
"hit");
this.blocks =
new MeteredMap<>(
blocks,
blocksLabelledCounter,
(__, signedBeaconBlock) ->
signedBeaconBlock
.map(
beaconBlock ->
spec.getCurrentSlot(this)
.minusMinZero(beaconBlock.getSlot())
.intValue())
.orElse(-1));
"success",
"slots",
"path");
this.blockToSlotMetrics =
(__, beaconBlock) -> {
int diff = spec.getCurrentSlot(this).minusMinZero(beaconBlock.getSlot()).intValue();
if (diff > 99) {
diff = 99;
}
return String.format("%02d", diff);
};
this.blocks = new MeteredMap<>(blocks, blocksLabelledCounter, blockToSlotMetrics);
this.highestVotedValidatorIndex =
votes.keySet().stream().max(Comparator.naturalOrder()).orElse(UInt64.ZERO);
this.votes =
Expand All @@ -196,7 +198,7 @@ private Store(
.map((b) -> Map.of(b.getRoot(), b))
.orElseGet(Collections::emptyMap)),
fromMap(this.blocks),
blockProvider);
BlockProvider.meteredFalse(blockProvider, blocksLabelledCounter, blockToSlotMetrics));
this.blobSidecarsProvider = blobSidecarsProvider;
this.earliestBlobSidecarSlotProvider = earliestBlobSidecarSlotProvider;
}
Expand Down Expand Up @@ -519,6 +521,17 @@ public SafeFuture<Optional<SignedBeaconBlock>> retrieveSignedBlock(final Bytes32
.getBlock(blockRoot)
.thenApply(
block -> {
if (block.isPresent()) {
blocksLabelledCounter
.labels(
"get",
"false",
blockToSlotMetrics.apply(blockRoot, block.get()),
"retrieveSignedBlock")
.inc();
} else {
blocksLabelledCounter.labels("get", "false", "-1", "retrieveSignedBlock").inc();
}
block.ifPresent(this::putBlock);
return block;
});
Expand Down

0 comments on commit 4616204

Please sign in to comment.