Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add congestion multiplier metric #17286

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (C) 2025 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hedera.node.app.throttle;

import static java.util.Objects.requireNonNull;

import com.hedera.node.app.fees.congestion.CongestionMultipliers;
import com.hedera.node.app.store.ReadableStoreFactory;
import com.hedera.node.app.workflows.TransactionInfo;
import com.swirlds.metrics.api.LongGauge;
import com.swirlds.metrics.api.Metrics;
import edu.umd.cs.findbugs.annotations.NonNull;
import javax.inject.Inject;
import javax.inject.Singleton;

@Singleton
public class CongestionMetrics {
private final CongestionMultipliers congestionMultipliers;
private final LongGauge congestionMultiplierGauge;

@Inject
public CongestionMetrics(
@NonNull final Metrics metrics, @NonNull final CongestionMultipliers congestionMultipliers) {
requireNonNull(metrics);
this.congestionMultipliers = requireNonNull(congestionMultipliers);

final var config =
new LongGauge.Config("app", "congestionMultiplier").withDescription("Current congestion multiplier");
this.congestionMultiplierGauge = metrics.getOrCreate(config);
}

public void updateMultiplier(
@NonNull final TransactionInfo txnInfo, @NonNull final ReadableStoreFactory storeFactory) {
congestionMultiplierGauge.set(congestionMultipliers.maxCurrentMultiplier(txnInfo, storeFactory));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
import com.hedera.node.app.state.recordcache.LegacyListRecordSource;
import com.hedera.node.app.store.StoreFactoryImpl;
import com.hedera.node.app.store.WritableStoreFactory;
import com.hedera.node.app.throttle.CongestionMetrics;
import com.hedera.node.app.throttle.ThrottleServiceManager;
import com.hedera.node.app.workflows.OpWorkflowMetrics;
import com.hedera.node.app.workflows.TransactionInfo;
Expand Down Expand Up @@ -153,6 +154,7 @@ public class HandleWorkflow {
private final KVStateChangeListener kvStateChangeListener;
private final BoundaryStateChangeListener boundaryStateChangeListener;
private final ScheduleService scheduleService;
private final CongestionMetrics congestionMetrics;

// The last second since the epoch at which the metrics were updated; this does not affect transaction handling
private long lastMetricUpdateSecond;
Expand Down Expand Up @@ -184,7 +186,8 @@ public HandleWorkflow(
@NonNull final AddressBookHelper addressBookHelper,
@NonNull final KVStateChangeListener kvStateChangeListener,
@NonNull final BoundaryStateChangeListener boundaryStateChangeListener,
@NonNull final ScheduleService scheduleService) {
@NonNull final ScheduleService scheduleService,
@NonNull final CongestionMetrics congestionMetrics) {
this.networkInfo = requireNonNull(networkInfo);
this.stakePeriodChanges = requireNonNull(stakePeriodChanges);
this.dispatchProcessor = requireNonNull(dispatchProcessor);
Expand All @@ -209,6 +212,7 @@ public HandleWorkflow(
this.kvStateChangeListener = requireNonNull(kvStateChangeListener);
this.boundaryStateChangeListener = requireNonNull(boundaryStateChangeListener);
this.scheduleService = requireNonNull(scheduleService);
this.congestionMetrics = requireNonNull(congestionMetrics);
this.streamMode = configProvider
.getConfiguration()
.getConfigData(BlockStreamConfig.class)
Expand Down Expand Up @@ -319,7 +323,7 @@ private void handleEvents(
}
}
// Update all throttle metrics once per round
throttleServiceManager.updateAllMetrics();
throttleServiceManager.updateAllMetrics(); // TODO: update congestion multiplier metrics here?
// Inform the BlockRecordManager that the round is complete, so it can update running-hashes in state
// that have been being computed in background threads. The running hash has to be included in
// state, but we want to synchronize with background threads as infrequently as possible. So once per
Expand Down Expand Up @@ -384,6 +388,7 @@ private boolean handlePlatformTransaction(
}

opWorkflowMetrics.updateDuration(userTxn.functionality(), (int) (System.nanoTime() - handleStart));
congestionMetrics.updateMultiplier(userTxn.txnInfo(), userTxn.readableStoreFactory());

if (streamMode == RECORDS) {
// We don't support long-term scheduled transactions if only producing records
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (C) 2025 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hedera.node.app.throttle;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.hedera.node.app.fees.congestion.CongestionMultipliers;
import com.hedera.node.app.store.ReadableStoreFactory;
import com.hedera.node.app.workflows.TransactionInfo;
import com.swirlds.metrics.api.LongGauge;
import com.swirlds.metrics.api.Metrics;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class CongestionMetricsTest {
@Mock
private Metrics metrics;

@Mock
private CongestionMultipliers congestionMultipliers;

@Mock
private LongGauge longGauge;

@Mock
private TransactionInfo txnInfo;

@Mock
private ReadableStoreFactory storeFactory;

private CongestionMetrics congestionMetrics;

@BeforeEach
void setUp() {
when(metrics.getOrCreate(any(LongGauge.Config.class))).thenReturn(longGauge);
congestionMetrics = new CongestionMetrics(metrics, congestionMultipliers);
}

@Test
void testUpdateMultiplier() {
when(congestionMultipliers.maxCurrentMultiplier(txnInfo, storeFactory)).thenReturn(5L);

congestionMetrics.updateMultiplier(txnInfo, storeFactory);

verify(longGauge).set(5L);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.hedera.node.app.service.token.impl.handlers.staking.StakePeriodManager;
import com.hedera.node.app.spi.metrics.StoreMetricsService;
import com.hedera.node.app.state.HederaRecordCache;
import com.hedera.node.app.throttle.CongestionMetrics;
import com.hedera.node.app.throttle.ThrottleServiceManager;
import com.hedera.node.app.workflows.OpWorkflowMetrics;
import com.hedera.node.app.workflows.handle.cache.CacheWarmer;
Expand Down Expand Up @@ -144,6 +145,9 @@ class HandleWorkflowTest {
@Mock
private UserTxnFactory userTxnFactory;

@Mock
private CongestionMetrics congestionMetrics;

private HandleWorkflow subject;

@BeforeEach
Expand Down Expand Up @@ -222,6 +226,7 @@ private void givenSubjectWith(
new AddressBookHelper(),
kvStateChangeListener,
boundaryStateChangeListener,
scheduleService);
scheduleService,
congestionMetrics);
}
}
Loading