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

Feature/multi version flat db #5865

Closed
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
Expand Up @@ -2111,7 +2111,7 @@ private void issueOptionWarnings() {

CommandLineUtils.failIfOptionDoesntMeetRequirement(
commandLine,
"--Xsnapsync-synchronizer-flat option can only be used when -Xsnapsync-synchronizer-flat-db-healing-enabled is true",
"--Xsnapsync-synchronizer-flat option can only be used when --Xsnapsync-synchronizer-flat-db-healing-enabled is true",
unstableSynchronizerOptions.isSnapsyncFlatDbHealingEnabled(),
asList(
"--Xsnapsync-synchronizer-flat-account-healed-count-per-request",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,11 @@ WorldStateArchive createWorldStateArchive(
final CachedMerkleTrieLoader cachedMerkleTrieLoader) {
switch (dataStorageConfiguration.getDataStorageFormat()) {
case BONSAI:
// TODO, better integrate. Just for PoC, explicitly set our bonsai context chain head:
((BonsaiWorldStateKeyValueStorage) worldStateStorage)
.getFlatDbStrategy()
.updateBlockContext(blockchain.getChainHeadHeader());

return new BonsaiWorldStateProvider(
(BonsaiWorldStateKeyValueStorage) worldStateStorage,
blockchain,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright Hyperledger Besu Contributors.
*
* 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.
*
* SPDX-License-Identifier: Apache-2.0
*
*/
package org.hyperledger.besu.ethereum.bonsai;

import org.hyperledger.besu.plugin.data.BlockHeader;

import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;

/** Context which holds information relevant to a bonsai archive storage query. */
public class BonsaiContext {

private final AtomicReference<BlockHeader> blockHeader;

public BonsaiContext() {
blockHeader = new AtomicReference<>();
}

public BonsaiContext copy() {
var newCtx = new BonsaiContext();
Optional.ofNullable(blockHeader.get()).ifPresent(newCtx::setBlockHeader);
return newCtx;
}

public BonsaiContext setBlockHeader(final BlockHeader blockHeader) {
this.blockHeader.set(blockHeader);
return this;
}

public Optional<BlockHeader> getBlockHeader() {
return Optional.ofNullable(blockHeader.get());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.hyperledger.besu.ethereum.bonsai.cache.CachedMerkleTrieLoader;
import org.hyperledger.besu.ethereum.bonsai.cache.CachedWorldStorageManager;
import org.hyperledger.besu.ethereum.bonsai.storage.BonsaiWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.bonsai.storage.flat.ArchiveFlatDbStrategy;
import org.hyperledger.besu.ethereum.bonsai.trielog.TrieLogManager;
import org.hyperledger.besu.ethereum.bonsai.worldview.BonsaiWorldState;
import org.hyperledger.besu.ethereum.bonsai.worldview.BonsaiWorldStateUpdateAccumulator;
Expand Down Expand Up @@ -159,6 +160,19 @@ public Optional<MutableWorldState> getMutable(
if (shouldPersistState) {
return getMutable(blockHeader.getStateRoot(), blockHeader.getHash());
} else {
// TODO this needs to be better integrated && ensure block is canonical
// HACK for kikori PoC, if we have the trielog for this block, we can assume we have it in
// flatDB
// although, in practice we can only serve canonical chain worldstates and need to fall back
// to state rolling if the requested block is a fork.
if (this.worldStateStorage.getFlatDbStrategy() instanceof ArchiveFlatDbStrategy
&& trieLogManager.getTrieLogLayer(blockHeader.getBlockHash()).isPresent()) {

var contextSafeCopy = worldStateStorage.getContextSafeCopy();
contextSafeCopy.getFlatDbStrategy().updateBlockContext(blockHeader);
return Optional.of(new BonsaiWorldState(this, contextSafeCopy));
Copy link
Contributor

@matkt matkt Sep 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure why we are doing that . seems not clear to clone only the flatdb . It seems to be better to follow the logic of bonsai to clone all the storage if we need a clone and pass this clone to BonsaiWorldstate . like that no need to have a specific clone for flat db .

just passing a clone of the storage to the worldstate and the internal flat db will check directly the snapshot automatically

I'm also not fan to clone everytime. we had problem before because of that . I think we can use the same code as before . using the clone in the cache and if not present create a new one (without rollback)

return trieLogManager.getHeadWorldState(blockchain::getBlockHeader)
              .map(MutableWorldState::freeze);

When we will have the checkpointed trie we will have to add again the rollback. maybe an optional rolling depending if we need the state or not

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just saw that the current implemention is a hack for testing 👍

Copy link
Contributor Author

@garyschulte garyschulte Sep 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There isn't really any cloning exactly, contextSafeClone exists only to directly reuse the flatdb storage, but with a different bonsai context for a different block number. For example we don't want RPC queries to modify the bonsai context, because that will mess up the block suffixing when we persist.

We need to guard the bonsai context for the primary worldstate, but otherwise as long as we are not persisting, there is no need to snapshot or clone the database itself.

So clone is probably not the best name for this, better naming suggestions welcome :). My thinking here is that as long as this is a 'non-persisting' mutable worldstate, we can just hand out the primary worldstate storage with whatever bonsai context the caller is requesting.

Copy link
Contributor Author

@garyschulte garyschulte Sep 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we should have a different BonsaiWorldState subclass that better reflects that it is non-persisting and limited to flatdb only for reads. Right now we use non-persisting worldstates to propose blocks, so we might need some other "marker" to indicate we can hand out a worldstate that is both non-persisting and non-mutable

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Layered storage is already doing this. It is not a clone and prevents modification. I would like to limit the number of classes to keep the same logic as much as possible. We also have the notion of Frozen BonsaiWorldState . The idea imo is to have a single worldstate and give a storage to this one. And at the storage level it will be different . Maybe I'm wrong but I think we have to think about this because we did the refactoring to reduce the number of worldstate class etc and I think that we must avoid redoing what we removed

So I would say that we should have just a Layered Storage that wraps it and give it to the worldstate. And we freeze the worldstate as we are doing with bonsai before rpc call. Then find out if we should have a special worldstate for flat DB. I think we have to wait to see the implementation of the checkpointed trie. We could access in one worldstate to the flat db directly and the checkpointed trie in lazy mode.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finally, I think that it is in the creation of the worldstate storage that we will give the context of the block number.

BonsaiWorldstate doesn't need to know this.

}

final BlockHeader chainHeadBlockHeader = blockchain.getChainHeadHeader();
if (chainHeadBlockHeader.getNumber() - blockHeader.getNumber()
>= trieLogManager.getMaxLayersToLoad()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.StorageSlotKey;
import org.hyperledger.besu.ethereum.bonsai.BonsaiContext;
import org.hyperledger.besu.ethereum.bonsai.storage.flat.ArchiveFlatDbStrategy;
import org.hyperledger.besu.ethereum.bonsai.storage.flat.FlatDbStrategy;
import org.hyperledger.besu.ethereum.bonsai.storage.flat.FullFlatDbStrategy;
import org.hyperledger.besu.ethereum.bonsai.storage.flat.PartialFlatDbStrategy;
Expand Down Expand Up @@ -114,21 +116,33 @@ private void loadFlatDbStrategy() {
this.flatDbMode = newFlatDbMode;
if (flatDbMode == FlatDbMode.FULL) {
this.flatDbStrategy = new FullFlatDbStrategy(metricsSystem);
} else if (flatDbMode == FlatDbMode.ARCHIVE) {
this.flatDbStrategy = new ArchiveFlatDbStrategy(new BonsaiContext(), metricsSystem);
} else {
this.flatDbStrategy = new PartialFlatDbStrategy(metricsSystem);
}
}
}

public BonsaiWorldStateKeyValueStorage getContextSafeCopy() {
return new BonsaiWorldStateKeyValueStorage(
flatDbMode,
flatDbStrategy.contextSafeClone(),
composedWorldStateStorage,
trieLogStorage,
metricsSystem);
}

public FlatDbMode deriveFlatDbStrategy() {
var flatDbMode =
FlatDbMode.fromVersion(
composedWorldStateStorage
.get(TRIE_BRANCH_STORAGE, FLAT_DB_MODE)
.map(Bytes::wrap)
.orElse(FlatDbMode.PARTIAL.getVersion()));
// .orElse(FlatDbMode.PARTIAL.getVersion()));
// TODO: this is for archive testing only, remove me.
.orElse(FlatDbMode.ARCHIVE.getVersion()));
LOG.info("Bonsai flat db mode found {}", flatDbMode);

return flatDbMode;
}

Expand Down
Loading