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

Improve Trie Node Retrieve and Save #1526

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

ajlopez
Copy link
Contributor

@ajlopez ajlopez commented May 7, 2021

Description

It removes a trie node set, add an internal boolean flag to each trie node to knows if the node was already saved, improve the retrieval of the node (no get hash calculation is needed for adding the retrieved node to the removed set), improve save trie without visiting unloaded child nodes

Motivation and Context

It solves #1509 and #1510

How Has This Been Tested?

Additionally to the usual code tests, the execution of blocks from a testnet node was executed:

java -cp rskj-core-3.0.0-triefix-all.jar -Dlogback.configurationFile=<logbackfile> -Dblockchain.flushNumberOfBlocks=1000 -Dcache.states.max-elements=1000000 co.rsk.cli.tools.ExecuteBlocks 1672391 1673139 --testnet

There are new logs, to obtain some additional information from TrieStoreImpl and DataSourceWithCache. The above command was executed with

    <logger name="triestore" level="TRACE"/>
    <logger name="datasourcewithcache" level="TRACE"/>
    <logger name="execute" level="TRACE"/>
    <logger name="blockvalidator" level="TRACE"/>
    <logger name="blocksyncservice" level="TRACE"/>
    <logger name="blockexecutor" level="TRACE"/>

Only for testing purpose.

Some additional logs obtained:

2021-05-07-16:52:33.317 TRACE [triestore]  Start saving trie root.
2021-05-07-16:52:33.318 TRACE [triestore]  Start saving trie, level : 0
2021-05-07-16:52:57.923 TRACE [triestore]  End saving trie, level : 0, already saved.
2021-05-07-16:52:57.924 TRACE [triestore]  End saving trie root. No. Retrieves: 396. No. Saves: 0. No. No Saves: 1
2021-05-07-16:52:57.924 TRACE [triestore]  End process block. No. Retrieves: 968. No. Saves: 0. No. No Saves: 1
2021-05-07-16:52:57.925 TRACE [datasourcewithcache]  Activity: No. Gets: 969. No. Puts: 0. No. Gets from Store: 967

With this additional logs we have more information about the effectivity of having a cache (and the Trie getHash issue #1511 was discovered using these logs)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • Tests for the changes have been added (for bug fixes / features)
  • Requires Activation Code (Hard Fork)
  • Other information:

It is recommended to test with a long synchronization and also with some blocks added in production environment

@@ -55,33 +57,62 @@ public TrieStoreImpl(KeyValueDataSource store) {
*/
@Override
public void save(Trie trie) {
noRetrievesInSaveTrie = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't this part be thread-safe? what about keeping this tracing data in some object and passing this object as param in the void save(Trie trie, boolean isRootNode, int level) method recursively?

if (savedTries.contains(trie)) {
// it is guaranteed that the children of a saved node are also saved
private void save(Trie trie, boolean isRootNode, int level) {
if (trie.wasSaved()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess there could be cases when multiple sub-tries in a trie have same hash but in fact are different java objects, so this flag wasSaved wouldn't work. Could be a problem when importing an entire state via the --import flag.

Also looks like this wasSaved flag prevents the import feature to work, as it uses Trie.fromMessage to load raw data from a file here

@SergioDemianLerner
Copy link
Contributor

Amazing work! I will test the performance improvement

blockExecutor.execute(block, parent.getHeader(), false, false);
BlockResult blockResult = blockExecutor.execute(block, parent.getHeader(), false, false);

if (!Arrays.equals(blockResult.getFinalState().getHash().getBytes(), block.getStateRoot())) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This check will fail for blocks before 1591000 because those blockstate roots are either not validated, or converted with TrieConeverter

@ajlopez ajlopez mentioned this pull request May 26, 2021
8 tasks
@@ -151,7 +154,10 @@ public static Trie fromMessage(byte[] message, TrieStore store) {
trie = fromMessageRskip107(ByteBuffer.wrap(message), store);
}

trie.saved = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't this saved redundant as it is gonna pass from either frommessacheOrchid or fromMessageRskip107 which already mark the trie as saved?

This was referenced Jun 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants