Skip to content

Commit

Permalink
reducing build roster calls.
Browse files Browse the repository at this point in the history
Signed-off-by: Edward Wertz <edward@swirldslabs.com>
  • Loading branch information
edward-swirldslabs committed Dec 2, 2024
1 parent 35e81ee commit 85c006c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.swirlds.platform.FreezePeriodChecker;
import com.swirlds.platform.internal.ConsensusRound;
import com.swirlds.platform.metrics.SwirldStateMetrics;
import com.swirlds.platform.roster.RosterRetriever;
import com.swirlds.platform.state.signed.SignedState;
import com.swirlds.platform.system.Round;
import com.swirlds.platform.system.SoftwareVersion;
Expand Down Expand Up @@ -126,9 +125,7 @@ public void setInitialState(@NonNull final MerkleRoot state) {
public void handleConsensusRound(final ConsensusRound round) {
final MerkleRoot state = stateRef.get();

uptimeTracker.handleRound(
round,
RosterRetriever.buildRoster(state.getReadablePlatformState().getAddressBook()));
uptimeTracker.handleRound(round);
transactionHandler.handleRound(round, state);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import com.swirlds.platform.system.status.StatusActionSubmitter;
import com.swirlds.platform.system.status.actions.SelfEventReachedConsensusAction;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.time.Duration;
import java.time.Instant;
import java.util.HashMap;
Expand All @@ -50,8 +49,6 @@ public class UptimeTracker {

private final NodeId selfId;
private final Time time;
private final Roster roster;

private final UptimeMetrics uptimeMetrics;
private final Duration degradationThreshold;

Expand Down Expand Up @@ -88,7 +85,6 @@ public UptimeTracker(

this.selfId = Objects.requireNonNull(selfId, "selfId must not be null");
this.time = Objects.requireNonNull(time);
this.roster = Objects.requireNonNull(roster);
this.statusActionSubmitter = Objects.requireNonNull(statusActionSubmitter);
this.degradationThreshold = platformContext
.getConfiguration()
Expand All @@ -102,26 +98,26 @@ public UptimeTracker(
* Look at the events in a round to determine which nodes are up and which nodes are down.
*
* @param round the round to analyze
* @param roster the roster for this round
*/
public void handleRound(@NonNull final ConsensusRound round, @Nullable final Roster roster) {
public void handleRound(@NonNull final ConsensusRound round) {

if (round.isEmpty()) {
return;
}

if (roster == null) {
return;
}

final Instant start = time.now();

addAndRemoveNodes(uptimeData, roster);
addAndRemoveNodes(uptimeData, round.getConsensusRoster());
final Map<NodeId, ConsensusEvent> lastEventsInRoundByCreator = new HashMap<>();
final Map<NodeId, ConsensusEvent> judgesByCreator = new HashMap<>();
scanRound(round, lastEventsInRoundByCreator, judgesByCreator);
updateUptimeData(roster, uptimeData, lastEventsInRoundByCreator, judgesByCreator, round.getRoundNum());
reportUptime(uptimeData, round.getConsensusTimestamp(), round.getRoundNum());
updateUptimeData(
round.getConsensusRoster(),
uptimeData,
lastEventsInRoundByCreator,
judgesByCreator,
round.getRoundNum());
reportUptime(round.getConsensusRoster(), uptimeData, round.getConsensusTimestamp(), round.getRoundNum());

final Instant end = time.now();
final Duration elapsed = Duration.between(start, end);
Expand Down Expand Up @@ -247,7 +243,10 @@ private void updateUptimeData(
* @param uptimeData the uptime data
*/
private void reportUptime(
@NonNull final UptimeData uptimeData, @NonNull final Instant lastRoundEndTime, final long currentRound) {
@NonNull final Roster roster,
@NonNull final UptimeData uptimeData,
@NonNull final Instant lastRoundEndTime,
final long currentRound) {

long nonDegradedConsensusWeight = 0;
for (final RosterEntry entry : roster.rosterEntries()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,11 @@ private static List<PlatformEvent> generateEvents(
return events;
}

private static ConsensusRound mockRound(@NonNull final List<PlatformEvent> events, final long roundNum) {
private static ConsensusRound mockRound(
@NonNull final List<PlatformEvent> events, @NonNull final Roster roster, final long roundNum) {
final ConsensusSnapshot snapshot = mock(ConsensusSnapshot.class);
final ConsensusRound round = new ConsensusRound(
mock(Roster.class),
roster,
events,
mock(PlatformEvent.class),
mock(GraphGenerations.class),
Expand Down Expand Up @@ -138,8 +139,8 @@ void roundScanTest() {
final List<PlatformEvent> firstRoundEvents = generateEvents(
random, time, Duration.ofSeconds(1), roster, eventCount, noFirstRoundEvents, noFirstRoundJudges);

final ConsensusRound roundOne = mockRound(firstRoundEvents, 1);
uptimeTracker.handleRound(roundOne, roster);
final ConsensusRound roundOne = mockRound(firstRoundEvents, roster, 1);
uptimeTracker.handleRound(roundOne);

roster.rosterEntries().forEach(entry -> {
final NodeId nodeId = NodeId.of(entry.nodeId());
Expand Down Expand Up @@ -185,8 +186,8 @@ void roundScanTest() {
final List<PlatformEvent> secondRoundEvents = generateEvents(
random, time, Duration.ofSeconds(1), roster, eventCount, noSecondRoundEvents, noSecondRoundJudges);

final ConsensusRound roundTwo = mockRound(secondRoundEvents, 2);
uptimeTracker.handleRound(roundTwo, roster);
final ConsensusRound roundTwo = mockRound(secondRoundEvents, roster, 2);
uptimeTracker.handleRound(roundTwo);

roster.rosterEntries().forEach(entry -> {
final NodeId nodeId = NodeId.of(entry.nodeId());
Expand Down Expand Up @@ -260,8 +261,8 @@ void roundScanChangingRosterTest() {
assertEquals(NO_ROUND, uptimeData.getLastJudgeRound(nodeId));
});

final ConsensusRound roundOne = mockRound(firstRoundEvents, 1);
uptimeTracker.handleRound(roundOne, roster);
final ConsensusRound roundOne = mockRound(firstRoundEvents, roster, 1);
uptimeTracker.handleRound(roundOne);

roster.rosterEntries().forEach(entry -> {
final NodeId nodeId = NodeId.of(entry.nodeId());
Expand Down Expand Up @@ -306,9 +307,9 @@ void roundScanChangingRosterTest() {
final List<PlatformEvent> secondRoundEvents = generateEvents(
random, time, Duration.ofSeconds(1), newRoster, eventCount, noSecondRoundEvents, noSecondRoundJudges);

final ConsensusRound roundTwo = mockRound(secondRoundEvents, 2);
final ConsensusRound roundTwo = mockRound(secondRoundEvents, newRoster, 2);

uptimeTracker.handleRound(roundTwo, newRoster);
uptimeTracker.handleRound(roundTwo);

newRoster.rosterEntries().forEach(entry -> {
final NodeId nodeId = NodeId.of(entry.nodeId());
Expand Down Expand Up @@ -376,8 +377,8 @@ void degradedTest() {
assertEquals(NO_ROUND, uptimeData.getLastJudgeRound(nodeId));
});

final ConsensusRound roundOne = mockRound(firstRoundEvents, 1);
uptimeTracker.handleRound(roundOne, roster);
final ConsensusRound roundOne = mockRound(firstRoundEvents, roster, 1);
uptimeTracker.handleRound(roundOne);

// Simulate a following round, but allow a long time to pass
time.tick(Duration.ofSeconds(30));
Expand All @@ -387,8 +388,8 @@ void degradedTest() {
final List<PlatformEvent> secondRoundEvents =
generateEvents(random, time, Duration.ofSeconds(1), roster, eventCount, noSecondRoundEvents, Set.of());

final ConsensusRound roundTwo = mockRound(secondRoundEvents, 2);
uptimeTracker.handleRound(roundTwo, roster);
final ConsensusRound roundTwo = mockRound(secondRoundEvents, roster, 2);
uptimeTracker.handleRound(roundTwo);

assertTrue(uptimeTracker.isSelfDegraded());

Expand All @@ -397,8 +398,8 @@ void degradedTest() {
final List<PlatformEvent> thirdRoundEvents =
generateEvents(random, time, Duration.ofSeconds(1), roster, eventCount, Set.of(), Set.of());

final ConsensusRound roundThree = mockRound(thirdRoundEvents, 3);
uptimeTracker.handleRound(roundThree, roster);
final ConsensusRound roundThree = mockRound(thirdRoundEvents, roster, 3);
uptimeTracker.handleRound(roundThree);

assertFalse(uptimeTracker.isSelfDegraded());
}
Expand Down

0 comments on commit 85c006c

Please sign in to comment.