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

Add --X-trie-log subcommand #6303

Merged
merged 27 commits into from
Jan 8, 2024
Merged
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
16c0a49
Add x-trie-log subcommand for one-off backlog prune
siladu Nov 20, 2023
7dd4928
long -> int
siladu Nov 20, 2023
bf2b098
Removed banned method
gfukushima Dec 12, 2023
e67ae51
Preload process stream in parallel
gfukushima Dec 12, 2023
9b4e0c9
Drop unwanted trielogs and keep reatain layers only
gfukushima Dec 14, 2023
0b9fe83
Add output to user and cleanup refactor
gfukushima Dec 15, 2023
426848e
small tweak to display cf that had reference dropped by RocksDbSegmen…
gfukushima Dec 15, 2023
7401b59
spotless
gfukushima Dec 15, 2023
1b7fb72
Fix classes that changed package
gfukushima Dec 15, 2023
11e6b05
spotless
gfukushima Dec 15, 2023
f2d01e2
Code review
gfukushima Dec 15, 2023
04f1aaa
Only clear DB when we have the exact amount of trie logs we want in m…
gfukushima Dec 15, 2023
2f01c5a
Trielogs stream to and from file to avoid possibly OOM
gfukushima Dec 18, 2023
56e4c8e
Process trie logs in chunks to avoid OOM
gfukushima Dec 18, 2023
78561b0
save and read in batches to handle edge cases
gfukushima Dec 19, 2023
42c72cf
save and read files to/from database dir
gfukushima Dec 20, 2023
9961fc2
Merge branch 'main' into x-trie-log-subcommand-2
gfukushima Dec 20, 2023
9389540
add unit tests and PR review fixes
gfukushima Dec 21, 2023
e3d4fbc
Merge branch 'main' into x-trie-log-subcommand-2
gfukushima Dec 21, 2023
c7144fe
spdx
gfukushima Dec 21, 2023
20b0ba5
Fix unit tests directory creation and deletion
gfukushima Dec 21, 2023
e7d175c
Merge branch 'main' into x-trie-log-subcommand-2
gfukushima Jan 3, 2024
b214bf2
PR review
gfukushima Jan 4, 2024
aa75348
Merge branch 'main' into x-trie-log-subcommand-2
gfukushima Jan 4, 2024
2bc0732
Merge remote-tracking branch 'origin/x-trie-log-subcommand-2' into x-…
gfukushima Jan 4, 2024
deec021
Merge branch 'main' into x-trie-log-subcommand-2
gfukushima Jan 8, 2024
5d7b68e
Merge branch 'main' into x-trie-log-subcommand-2
gfukushima Jan 8, 2024
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
Prev Previous commit
Next Next commit
Only clear DB when we have the exact amount of trie logs we want in m…
…emory

Signed-off-by: Gabriel Fukushima <gabrielfukushima@gmail.com>
  • Loading branch information
gfukushima committed Dec 15, 2023
commit 04f1aaa3e5809cf0d8b155728eb2f664813665d2
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,21 @@ static void prune(
}

// retrieve the layersToRetains hashes from blockchain
final List<Hash> hashesToRetain = new ArrayList<>();
final List<Hash> trieLogKeys = new ArrayList<>();

for (long i = chainHeight; i > lastBlockToRetainTrieLogsFor; i--) {
final Optional<BlockHeader> header = blockchain.getBlockHeader(i);
header.ifPresent(blockHeader -> hashesToRetain.add(blockHeader.getHash()));
header.ifPresent(blockHeader -> trieLogKeys.add(blockHeader.getHash()));
}

IdentityHashMap<byte[], byte[]> trieLogsToRetain;

// TODO: maybe stop the method here if we don't find enough hashes to retain
if ((long) hashesToRetain.size() == layersToRetain) {
if ((long) trieLogKeys.size() == layersToRetain) {
trieLogsToRetain = new IdentityHashMap<>();
// save trielogs in a flatfile as a fail-safe
// save trielogs in a flatfile in case something goes wrong
out.println("Obtaining trielogs to retain...");
hashesToRetain.forEach(
trieLogKeys.forEach(
hash -> {
rootWorldStateStorage
.getTrieLog(hash)
Expand All @@ -100,30 +100,36 @@ static void prune(
out.println("Saving trielogs to retain in file...");
saveTrieLogsInFile(trieLogsToRetain);
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure how the fail-safe is being used. Expected to see that if we got an error clearing and inserting entries we ready from the file or allow specifying an existing backup file to restore from.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a comment and instructions that will be logged for the user. Basically the idea here is, we retain the logs in a file in case something goes wrong.
In that case the user can re-run the subcommand and we will read the trielogs from the file since the column family might have been dropped already.

} else {
// try to read the triLogs from the flatfile
// in case something went wrong and we already pruned trielogs
// users can re-un the subcommand and we will read trielogs from file
trieLogsToRetain = readTrieLogsFromFile();
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't understand how reading from the file helps in this case. We didn't find all the hashes but also haven't created the file at this point either unless we are relying running the command again to read the previous state

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 this is a failsafe in for a subsequent execution where something went terribly wrong during the truncation and restore-from-file process.

what would be nice, but not idiomatic, is if we had a few "are you sure?" or "do you want to?" prompts that could be bypassed by -y similar to ubuntu apt commands

}
out.println("Clear trielogs...");
// clear trielogs storage
// TODO: Add a check to ensure we have trieLogsToRetain.size() == layersToRetain
rootWorldStateStorage.clearTrieLog();

// get an update and insert the trielogs we retained
if (trieLogsToRetain.size() == layersToRetain) {
out.println("Clear trielogs...");
rootWorldStateStorage.clearTrieLog();
out.println("Restoring trielogs retained into db...");
recreateTrieLogs(rootWorldStateStorage, trieLogsToRetain);
}
if (rootWorldStateStorage.streamTrieLogKeys(layersToRetain).count() == layersToRetain) {
out.println("Prune ran successfully. Deleting file...");
deleteTrieLogFile();
out.println("Enjoy some GBs of storage back!...");
gfukushima marked this conversation as resolved.
Show resolved Hide resolved
} else {
out.println("Prune failed. Re-run the subcommand to load the trielogs from file.");
}
}

private static void recreateTrieLogs(
final BonsaiWorldStateKeyValueStorage rootWorldStateStorage,
final IdentityHashMap<byte[], byte[]> trieLogsToRetain) {
var updater = rootWorldStateStorage.updater();
out.println("restore trielogs retained into db...");

trieLogsToRetain.forEach(
(key, value) -> {
updater.getTrieLogStorageTransaction().put(key, value);
});
updater.getTrieLogStorageTransaction().commit();
Copy link
Contributor

Choose a reason for hiding this comment

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

If this fails then do have a way of restoring the trielog from the backup?


if (rootWorldStateStorage.streamTrieLogKeys(layersToRetain).count() == layersToRetain) {
out.println("Prune ran successfully. Deleting file...");
deleteTrieLogFile();
} else {
out.println("Prune failed. Please check the logs for more details.");
}
out.println("Enjoy some GBs of storage back!...");
}

private static void validatePruneConfiguration(final DataStorageConfiguration config) {
Expand Down Expand Up @@ -220,7 +226,9 @@ private static IdentityHashMap<byte[], byte[]> readTrieLogsFromFile() throws IOE

private static void deleteTrieLogFile() {
File file = new File(trieLogFile);
Copy link
Contributor

Choose a reason for hiding this comment

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

When we create the file we could use the File.deleteOnExit so we don't need to worry about manually deleting this file

file.delete();
if (file.exists()) {
file.delete();
}
}

static void printCount(final PrintWriter out, final TrieLogCount count) {
Expand Down
Loading