Skip to content

Commit

Permalink
06601 6518 remove get events (#6602)
Browse files Browse the repository at this point in the history
Signed-off-by: Cody Littley <cody@swirldslabs.com>
  • Loading branch information
cody-littley committed May 16, 2023
1 parent 4b01b06 commit 9fe8b07
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 92 deletions.
Expand Up @@ -32,7 +32,6 @@
import com.swirlds.common.system.BasicSoftwareVersion;
import com.swirlds.common.system.NodeId;
import com.swirlds.common.system.Platform;
import com.swirlds.common.system.PlatformWithDeprecatedMethods;
import com.swirlds.common.system.SwirldMain;
import com.swirlds.common.system.SwirldState;
import com.swirlds.common.system.address.Address;
Expand Down Expand Up @@ -477,7 +476,8 @@ public void init(final Platform platform, final NodeId id) {
public void run() {
while (true) {
if (window != null && !freezeCheckbox.getState()) {
eventsCache = ((PlatformWithDeprecatedMethods) platform).getAllEvents();
eventsCache = GuiPlatformAccessor.getInstance()
.getAllEvents(platform.getSelfId().id());
// after this getAllEvents call, the set of events to draw is frozen
// for the duration of this screen redraw. But their status (consensus or not) may change
// while it is being drawn. If an event is discarded while being drawn, then it forgets its
Expand Down
Expand Up @@ -34,7 +34,6 @@
import com.swirlds.common.system.InitTrigger;
import com.swirlds.common.system.NodeId;
import com.swirlds.common.system.Platform;
import com.swirlds.common.system.PlatformWithDeprecatedMethods;
import com.swirlds.common.system.SoftwareVersion;
import com.swirlds.common.system.address.AddressBook;
import com.swirlds.common.test.io.InputOutputStream;
Expand Down Expand Up @@ -83,7 +82,7 @@ public static void setUp() throws ConstructableRegistryException {

mapKey = new MapKey(0, 0, random.nextLong());
state = Mockito.spy(PlatformTestingToolState.class);
Platform platform = Mockito.spy(PlatformWithDeprecatedMethods.class);
final Platform platform = Mockito.mock(Platform.class);
when(platform.getSelfId()).thenReturn(new NodeId(0L));
AddressBook addressBook = Mockito.spy(AddressBook.class);
when(addressBook.getNumberWithWeight()).thenReturn(4);
Expand Down
Expand Up @@ -37,7 +37,6 @@
import com.swirlds.common.system.BasicSoftwareVersion;
import com.swirlds.common.system.NodeId;
import com.swirlds.common.system.Platform;
import com.swirlds.common.system.PlatformWithDeprecatedMethods;
import com.swirlds.common.system.SwirldMain;
import com.swirlds.common.system.SwirldState;
import com.swirlds.common.threading.framework.StoppableThread;
Expand Down Expand Up @@ -154,11 +153,6 @@ public StatsSigningTestingToolMain() {
@Override
public void init(final Platform platform, final NodeId id) {

if (!(platform instanceof PlatformWithDeprecatedMethods)) {
// Don't bother with setup while in recovery mode
return;
}

this.platform = platform;
selfId = id.id();
// parse the config.txt parameters, and allow optional _ as in 1_000_000
Expand Down

This file was deleted.

Expand Up @@ -56,12 +56,10 @@
import com.swirlds.common.system.NodeId;
import com.swirlds.common.system.Platform;
import com.swirlds.common.system.PlatformStatus;
import com.swirlds.common.system.PlatformWithDeprecatedMethods;
import com.swirlds.common.system.SoftwareVersion;
import com.swirlds.common.system.SwirldState;
import com.swirlds.common.system.address.Address;
import com.swirlds.common.system.address.AddressBook;
import com.swirlds.common.system.events.PlatformEvent;
import com.swirlds.common.system.transaction.internal.SwirldTransaction;
import com.swirlds.common.system.transaction.internal.SystemTransaction;
import com.swirlds.common.threading.SyncPermitProvider;
Expand Down Expand Up @@ -226,7 +224,6 @@
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
Expand All @@ -241,7 +238,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class SwirldsPlatform implements Platform, PlatformWithDeprecatedMethods, ConnectionTracker, Startable {
public class SwirldsPlatform implements Platform, ConnectionTracker, Startable {

public static final String PLATFORM_THREAD_POOL_NAME = "platform-core";
/** use this for all logging, as controlled by the optional data/log4j2.xml file */
Expand Down Expand Up @@ -1758,43 +1755,6 @@ public boolean createTransaction(@NonNull final byte[] transaction) {
return transactionSubmitter.submitTransaction(new SwirldTransaction(transaction));
}

/**
* {@inheritDoc}
*/
@Deprecated(forRemoval = true)
@Override
public PlatformEvent[] getAllEvents() {
// There is currently a race condition that can cause an exception if event order changes at
// just the right moment. Since this is just a testing utility method and not used in production
// environments, we can just retry until we succeed.
int maxRetries = 100;
while (maxRetries-- > 0) {
try {
final EventImpl[] allEvents = shadowGraph.getAllEvents();
Arrays.sort(allEvents, (o1, o2) -> {
if (o1.getConsensusOrder() != -1 && o2.getConsensusOrder() != -1) {
// both are consensus
return Long.compare(o1.getConsensusOrder(), o2.getConsensusOrder());
} else if (o1.getConsensusTimestamp() == null && o2.getConsensusTimestamp() == null) {
// neither are consensus
return o1.getTimeReceived().compareTo(o2.getTimeReceived());
} else {
// one is consensus, the other is not
if (o1.getConsensusTimestamp() == null) {
return 1;
} else {
return -1;
}
}
});
return allEvents;
} catch (final IllegalArgumentException e) {
logger.error(EXCEPTION.getMarker(), "Exception while sorting events", e);
}
}
throw new IllegalStateException("Unable to sort events after 100 retries");
}

/**
* {@inheritDoc}
*/
Expand Down
Expand Up @@ -16,15 +16,22 @@

package com.swirlds.platform.gui;

import static com.swirlds.logging.LogMarker.EXCEPTION;

import com.swirlds.common.system.events.PlatformEvent;
import com.swirlds.platform.Consensus;
import com.swirlds.platform.components.state.StateManagementComponent;
import com.swirlds.platform.gossip.shadowgraph.ShadowGraph;
import com.swirlds.platform.internal.EventImpl;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
* Provides a way to access private platform objects from the GUI. Suboptimal, but necessary to preserve the current UI
Expand All @@ -35,6 +42,8 @@
@Deprecated(forRemoval = true)
public final class GuiPlatformAccessor {

private static final Logger logger = LogManager.getLogger(GuiPlatformAccessor.class);

private final Map<Long, String> aboutStrings = new ConcurrentHashMap<>();
private final Map<Long, String> platformNames = new ConcurrentHashMap<>();
private final Map<Long, byte[]> swirldIds = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -165,6 +174,41 @@ public ShadowGraph getShadowGraph(final long nodeId) {
return shadowGraphs.getOrDefault(nodeId, null);
}

/**
* Get a sorted list of events.
*/
public PlatformEvent[] getAllEvents(final long nodeId) {
// There is currently a race condition that can cause an exception if event order changes at
// just the right moment. Since this is just a testing utility method and not used in production
// environments, we can just retry until we succeed.
int maxRetries = 100;
while (maxRetries-- > 0) {
try {
final EventImpl[] allEvents = getShadowGraph(nodeId).getAllEvents();
Arrays.sort(allEvents, (o1, o2) -> {
if (o1.getConsensusOrder() != -1 && o2.getConsensusOrder() != -1) {
// both are consensus
return Long.compare(o1.getConsensusOrder(), o2.getConsensusOrder());
} else if (o1.getConsensusTimestamp() == null && o2.getConsensusTimestamp() == null) {
// neither are consensus
return o1.getTimeReceived().compareTo(o2.getTimeReceived());
} else {
// one is consensus, the other is not
if (o1.getConsensusTimestamp() == null) {
return 1;
} else {
return -1;
}
}
});
return allEvents;
} catch (final IllegalArgumentException e) {
logger.error(EXCEPTION.getMarker(), "Exception while sorting events", e);
}
}
throw new IllegalStateException("Unable to sort events after 100 retries");
}

/**
* Set the state management component for a node.
*
Expand Down

0 comments on commit 9fe8b07

Please sign in to comment.