Skip to content

Commit

Permalink
fix initial sync listener registration and startup ordering
Browse files Browse the repository at this point in the history
Signed-off-by: garyschulte <garyschulte@gmail.com>
  • Loading branch information
garyschulte committed Mar 16, 2024
1 parent a122313 commit a0ec424
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ public SynchronizerConfiguration.Builder toDomainObject() {
.localFlatAccountCountToHealPerRequest(snapsyncFlatAccountHealedCountPerRequest)
.localFlatStorageCountToHealPerRequest(snapsyncFlatStorageHealedCountPerRequest)
.isFlatDbHealingEnabled(snapsyncFlatDbHealingEnabled)
.isSnapServerEnabled(snapsyncServerEnabled)
.build());
builder.checkpointPostMergeEnabled(checkpointPostMergeSyncEnabled);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ class SnapServer implements BesuEvents.InitialSyncCompletionListener {
this.worldStateStorageCoordinator = worldStateStorageCoordinator;
this.protocolContext = Optional.of(protocolContext);
registerResponseConstructors();

// subscribe to initial sync completed events to start/stop snap server:
this.protocolContext
.flatMap(ProtocolContext::getSynchronizer)
.filter(z -> z instanceof DefaultSynchronizer)
.map(DefaultSynchronizer.class::cast)
.ifPresentOrElse(
z -> this.listenerId.set(z.subscribeInitialSync(this)),
() -> LOGGER.warn("SnapServer created without reference to sync status"));
}

/**
Expand Down Expand Up @@ -128,50 +137,43 @@ public void onInitialSyncRestart() {
}

public synchronized SnapServer start() {

// if we are bonsai and full flat, we can provide a worldstate storage:
var worldStateKeyValueStorage = worldStateStorageCoordinator.worldStateKeyValueStorage();
if (worldStateKeyValueStorage.getDataStorageFormat().equals(DataStorageFormat.BONSAI)
&& worldStateStorageCoordinator.isMatchingFlatMode(FlatDbMode.FULL)) {
LOGGER.debug("Starting snap server with Bonsai full flat db");
var bonsaiArchive =
protocolContext
.map(ProtocolContext::getWorldStateArchive)
.map(BonsaiWorldStateProvider.class::cast);
var cachedStorageManagerOpt =
bonsaiArchive.map(archive -> archive.getCachedWorldStorageManager());

if (cachedStorageManagerOpt.isPresent()) {
var cachedStorageManager = cachedStorageManagerOpt.get();
this.worldStateStorageProvider = cachedStorageManager::getStorageByRootHash;

// when we start we need to build the cache of latest 128 worldstates trielogs-to-root-hash:
var blockchain = protocolContext.map(ProtocolContext::getBlockchain).orElse(null);

// at startup, prime the latest worldstates by roothash:
cachedStorageManager.primeRootToBlockHashCache(blockchain, PRIME_STATE_ROOT_CACHE_LIMIT);

// subscribe to initial sync completed events to start/stop snap server:
protocolContext
.flatMap(ProtocolContext::getSynchronizer)
.filter(z -> z instanceof DefaultSynchronizer)
.map(DefaultSynchronizer.class::cast)
.ifPresentOrElse(
z -> this.listenerId.set(z.subscribeInitialSync(this)),
() -> LOGGER.warn("SnapServer created without reference to sync status"));

var flatDbStrategy =
((BonsaiWorldStateKeyValueStorage)
worldStateStorageCoordinator.worldStateKeyValueStorage())
.getFlatDbStrategy();
if (!flatDbStrategy.isCodeByCodeHash()) {
LOGGER.warn("SnapServer requires code stored by codehash, but it is not enabled");
if (!isStarted.get()) {
// if we are bonsai and full flat, we can provide a worldstate storage:
var worldStateKeyValueStorage = worldStateStorageCoordinator.worldStateKeyValueStorage();
if (worldStateKeyValueStorage.getDataStorageFormat().equals(DataStorageFormat.BONSAI)
&& worldStateStorageCoordinator.isMatchingFlatMode(FlatDbMode.FULL)) {
LOGGER.debug("Starting snap server with Bonsai full flat db");
var bonsaiArchive =
protocolContext
.map(ProtocolContext::getWorldStateArchive)
.map(BonsaiWorldStateProvider.class::cast);
var cachedStorageManagerOpt =
bonsaiArchive.map(archive -> archive.getCachedWorldStorageManager());

if (cachedStorageManagerOpt.isPresent()) {
var cachedStorageManager = cachedStorageManagerOpt.get();
this.worldStateStorageProvider = cachedStorageManager::getStorageByRootHash;

// when we start we need to build the cache of latest 128 worldstates
// trielogs-to-root-hash:
var blockchain = protocolContext.map(ProtocolContext::getBlockchain).orElse(null);

// at startup, prime the latest worldstates by roothash:
cachedStorageManager.primeRootToBlockHashCache(blockchain, PRIME_STATE_ROOT_CACHE_LIMIT);

var flatDbStrategy =
((BonsaiWorldStateKeyValueStorage)
worldStateStorageCoordinator.worldStateKeyValueStorage())
.getFlatDbStrategy();
if (!flatDbStrategy.isCodeByCodeHash()) {
LOGGER.warn("SnapServer requires code stored by codehash, but it is not enabled");
}
} else {
LOGGER.warn(
"SnapServer started without cached storage manager, this should only happen in tests");
}
} else {
LOGGER.warn(
"SnapServer started without cached storage manager, this should only happen in tests");
isStarted.set(true);
}
isStarted.set(true);
}
return this;
}
Expand Down Expand Up @@ -563,9 +565,9 @@ public boolean test(final Pair<Bytes32, Bytes> pair) {
.addArgument(byteLimit::get)
.addArgument(recordLimit::get)
.log();
if (stopWatch.getSplitTime() > MAX_MILLIS_PER_REQUEST) {
if (stopWatch.getTime() > MAX_MILLIS_PER_REQUEST) {
shouldContinue.set(false);
LOGGER.debug("{} took too long, stopped at {} ms", forWhat, stopWatch.formatSplitTime());
LOGGER.debug("{} took too long, stopped at {} ms", forWhat, stopWatch.formatTime());
return false;
}

Expand Down

0 comments on commit a0ec424

Please sign in to comment.