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

Flatten SwirldsPlatform.init() #6484

Merged
merged 1 commit into from May 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -350,7 +350,7 @@ public class SwirldsPlatform implements Platform, PlatformWithDeprecatedMethods,
/** last time stamp when pause check timer is active */
private long pauseCheckTimeStamp;
/** Tracks recent events created in the network */
private CriticalQuorum criticalQuorum;
private final CriticalQuorum criticalQuorum;

private QueueThread<EventIntakeTask> intakeQueue;
private EventLinker eventLinker;
Expand All @@ -368,7 +368,7 @@ public class SwirldsPlatform implements Platform, PlatformWithDeprecatedMethods,
/** Handles all interaction with {@link SwirldState} */
private SwirldStateManager swirldStateManager;
/** Checks the validity of transactions and submits valid ones to the event transaction pool */
private SwirldTransactionSubmitter transactionSubmitter;
private final SwirldTransactionSubmitter transactionSubmitter;
/** clears all pipelines to prepare for a reconnect */
private Clearable clearAllPipelines;
/** Contains all information and state required for emergency recovery */
Expand Down Expand Up @@ -611,10 +611,64 @@ public class SwirldsPlatform implements Platform, PlatformWithDeprecatedMethods,
// FUTURE WORK remove this when there are no more ShutdownRequestedTriggers being dispatched
components.add(new Shutdown());

// if this setting is 0 or less, there is no startup freeze
if (settings.getFreezeSecondsAfterStartup() > 0) {
final Instant startUpEventFrozenEndTime =
Instant.now().plusSeconds(settings.getFreezeSecondsAfterStartup());
startUpEventFrozenManager.setStartUpEventFrozenEndTime(startUpEventFrozenEndTime);
logger.info(STARTUP.getMarker(), "startUpEventFrozenEndTime: {}", () -> startUpEventFrozenEndTime);
}

// initializes EventStreamManager instance
final Address address = getSelfAddress();
if (address.getMemo() != null && !address.getMemo().isEmpty()) {
initEventStreamManager(address.getMemo());
} else {
initEventStreamManager(String.valueOf(selfId));
}

final LoadedState loadedState = initializeLoadedStateFromSignedState(loadedSignedState);
try (loadedState.signedStateFromDisk) {
init(loadedState.signedStateFromDisk.getNullable(), loadedState.initialState, genesisStateBuilder);
final SignedState signedStateFromDisk = loadedState.signedStateFromDisk.getNullable();

buildEventHandlers(signedStateFromDisk, loadedState.initialState, genesisStateBuilder);

if (signedStateFromDisk != null) {
loadIntoConsensusAndEventMapper(signedStateFromDisk);
eventLinker.loadFromSignedState(signedStateFromDisk);
} else {
consensusRef.set(new ConsensusImpl(
platformContext.getConfiguration().getConfigData(ConsensusConfig.class),
consensusMetrics,
consensusRoundHandler::addMinGenInfo,
getAddressBook()));
}
}

transactionSubmitter = new SwirldTransactionSubmitter(
currentPlatformStatus::get,
PlatformConstructor.settingsProvider(),
swirldStateManager::submitTransaction,
new TransactionMetrics(metrics));

if (settings.getChatter().isChatterUsed()) {
criticalQuorum =
new CriticalQuorumImpl(initialAddressBook, false, settings.getChatter().criticalQuorumSoftening);
} else {
criticalQuorum = new CriticalQuorumImpl(initialAddressBook);
}

buildEventIntake();

clearAllPipelines = new LoggingClearables(
RECONNECT.getMarker(),
List.of(
Pair.of(getIntakeQueue(), "intakeQueue"),
Pair.of(getEventMapper(), "eventMapper"),
Pair.of(getShadowGraph(), "shadowGraph"),
Pair.of(preConsensusEventHandler, "preConsensusEventHandler"),
Pair.of(consensusRoundHandler, "consensusRoundHandler"),
Pair.of(swirldStateManager, "swirldStateManager")));
}

/**
Expand Down Expand Up @@ -931,73 +985,6 @@ void loadReconnectState(final SignedState signedState) {
"`loadReconnectState` : resetting fallen-behind & reloading state, finished, succeeded`");
}

/**
* First part of initialization. This was split up so that appMain.init() could be called before {@link
* StateLoadedFromDiskNotification} would be dispatched. Eventually, this should be split into more discrete parts.
*/
private void init(
@Nullable final SignedState signedStateFromDisk,
@Nullable final State initialState,
@NonNull final Supplier<SwirldState> genesisStateBuilder) {

// if this setting is 0 or less, there is no startup freeze
if (settings.getFreezeSecondsAfterStartup() > 0) {
final Instant startUpEventFrozenEndTime =
Instant.now().plusSeconds(settings.getFreezeSecondsAfterStartup());
startUpEventFrozenManager.setStartUpEventFrozenEndTime(startUpEventFrozenEndTime);
logger.info(STARTUP.getMarker(), "startUpEventFrozenEndTime: {}", () -> startUpEventFrozenEndTime);
}

// initializes EventStreamManager instance
final Address address = getSelfAddress();
if (address.getMemo() != null && !address.getMemo().isEmpty()) {
initEventStreamManager(address.getMemo());
} else {
initEventStreamManager(String.valueOf(selfId));
}

buildEventHandlers(signedStateFromDisk, initialState, genesisStateBuilder);

transactionSubmitter = new SwirldTransactionSubmitter(
currentPlatformStatus::get,
PlatformConstructor.settingsProvider(),
swirldStateManager::submitTransaction,
new TransactionMetrics(metrics));

if (signedStateFromDisk != null) {
loadIntoConsensusAndEventMapper(signedStateFromDisk);
} else {
consensusRef.set(new ConsensusImpl(
platformContext.getConfiguration().getConfigData(ConsensusConfig.class),
consensusMetrics,
consensusRoundHandler::addMinGenInfo,
getAddressBook()));
}

if (settings.getChatter().isChatterUsed()) {
criticalQuorum =
new CriticalQuorumImpl(initialAddressBook, false, settings.getChatter().criticalQuorumSoftening);
} else {
criticalQuorum = new CriticalQuorumImpl(initialAddressBook);
}

// build the event intake classes
buildEventIntake();
if (signedStateFromDisk != null) {
eventLinker.loadFromSignedState(signedStateFromDisk);
}

clearAllPipelines = new LoggingClearables(
RECONNECT.getMarker(),
List.of(
Pair.of(getIntakeQueue(), "intakeQueue"),
Pair.of(getEventMapper(), "eventMapper"),
Pair.of(getShadowGraph(), "shadowGraph"),
Pair.of(preConsensusEventHandler, "preConsensusEventHandler"),
Pair.of(consensusRoundHandler, "consensusRoundHandler"),
Pair.of(swirldStateManager, "swirldStateManager")));
}

/**
* This observer is called when a system freeze is requested. Permanently stops event creation and gossip.
*
Expand Down