-
Notifications
You must be signed in to change notification settings - Fork 22
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
Part of #267: Implement in Agora version upgrader #2364
Draft
Geod24
wants to merge
1
commit into
bosagora:v0.x.x
Choose a base branch
from
Geod24:part-267
base: v0.x.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/******************************************************************************* | ||
|
||
Apply version upgrades to a node | ||
|
||
To allow for a smooth upgrade path for Agora node, there is a built-in | ||
upgrade system: When a node starts, it checks a specific table in stateDB, | ||
and gradually run upgrade scripts as needed. | ||
|
||
Copyright: | ||
Copyright (c) 2019-2021 BOSAGORA Foundation | ||
All rights reserved. | ||
|
||
License: | ||
MIT License. See LICENSE for details. | ||
|
||
*******************************************************************************/ | ||
|
||
module agora.node.Versioning; | ||
|
||
import agora.common.Config; | ||
import agora.common.Ensure; | ||
import agora.common.ManagedDatabase; | ||
import agora.node.BlockStorage; | ||
import agora.utils.Log; | ||
|
||
import semver; | ||
|
||
import std.path; | ||
|
||
/******************************************************************************* | ||
|
||
Apply all required version upgrades to the state | ||
|
||
This is called from the FullNode's constructor after all the state handling | ||
members (`stateDB`, `cacheDB`, block `storage`) have been initialized. | ||
|
||
If any upgrade is needed at all (the version stored in the metadata | ||
table is different from the one we're running), we apply the required | ||
fix(es). This process can also handle downgrades. | ||
|
||
The logger is also initialized so we can provide feedback to the user. | ||
The config has already been parsed, so any fix needed would need to | ||
happen in two steps (one version allowing the new syntax and doing the | ||
fix, then a new version rejecting the old syntax). | ||
|
||
Params: | ||
stateDB = The node's `stateDB`, holding the blockchain state | ||
cacheDB = The node's `cacheDB`, holding the node's cached data | ||
storage = The node's block storage, holding known blocks | ||
current = The version we are currently running, as read from | ||
the version file | ||
config = The parsed configuration | ||
log = Logger to output any message to, if any | ||
|
||
Throws: | ||
If an error happened during the upgrade, as we don't want to continue | ||
and throw random errors to the user in a seemingly unrelated place. | ||
|
||
***************************************************************************/ | ||
|
||
package void applyVersionDifferences ( | ||
ManagedDatabase stateDB, ManagedDatabase cacheDB, IBlockStorage storage, | ||
string current, in Config config, Logger log) | ||
{ | ||
// We currently use this in a few places, allow for a transition period | ||
if (current == "HEAD") | ||
{ | ||
log.info("Current version is set to HEAD - cannot check for upgrades"); | ||
return; | ||
} | ||
|
||
void printFatalMessages () | ||
{ | ||
log.fatal("Cannot continue initialization - State DB is in an inconsistent state"); | ||
log.fatal("Please fix your installation if possible, or remove {} ", | ||
config.node.data_dir.buildPath("state.db")); | ||
log.fatal("This will rebuild your blockchain state from scratch, and may take some time"); | ||
log.fatal("It is recommended to also remove the cache DB at {} if the state DB is removed", | ||
config.node.data_dir.buildPath("state.db")); | ||
log.fatal("If you believe this is an issue with Agora, please report an issue at " ~ | ||
"https://github.com/bosagora/agora/"); | ||
} | ||
|
||
const vers = SemVer(current); | ||
ensure(vers.isValid, "Version '{}' is not a valid version", current); | ||
|
||
const exists = stateDB.execute( | ||
"SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='metadata'") | ||
.oneValue!int; | ||
|
||
// TODO: Remove once v0.17.0 has been deployed and all nodes have a metadata table | ||
version (none) | ||
{ | ||
if (!exists) | ||
{ | ||
stateDB.execute("CREATE TABLE metadata (key TEXT UNIQUE NOT NULL, value TEXT NOT NULL)"); | ||
stateDB.setMetadataVersion(current); | ||
return; | ||
} | ||
} | ||
else | ||
{ | ||
if (!exists) | ||
{ | ||
// If the `metadata` table doesn't exists, it means either the DB | ||
// is compromised or this is the first run - Let's check if it's the later. | ||
const tables = stateDB.execute( | ||
"SELECT COUNT(*) FROM sqlite_master WHERE type='table'") | ||
.oneValue!int; | ||
|
||
if (tables == 0) | ||
{ | ||
// Create it and return, since there is no existing state | ||
stateDB.execute("CREATE TABLE metadata (key TEXT UNIQUE NOT NULL, value TEXT NOT NULL)"); | ||
stateDB.setMetadataVersion(current); | ||
return; | ||
} | ||
|
||
log.fatal("No 'metadata' table exists in stateDB, but stateDB has {} tables!", tables); | ||
printFatalMessages(); | ||
ensure(false, "Could not determine previous state of the node - Check logs for more infos"); | ||
} | ||
} | ||
|
||
auto results = stateDB.execute("SELECT value FROM metadata WHERE key='version'"); | ||
if (results.empty()) | ||
{ | ||
printFatalMessages(); | ||
ensure(false, "Could not find version information in metadata table - Check logs for more infos"); | ||
} | ||
const oldVersStr = results.oneValue!string; | ||
const oldVers = SemVer(oldVersStr); | ||
if (!oldVers.isValid()) | ||
{ | ||
printFatalMessages(); | ||
ensure(false, "Version stored in metadata ({}) is not a valid version - " ~ | ||
"Check logs for more infos", oldVersStr); | ||
} | ||
|
||
// Most common case, do not output any message | ||
if (oldVers == vers) return; | ||
|
||
if (oldVers < vers) | ||
{ | ||
const size_t upgrades = 1; | ||
log.info("Need to apply {} upgrades from {} to {}", upgrades, oldVers, vers); | ||
} | ||
else | ||
{ | ||
const size_t downgrades = 1; | ||
log.info("Need to apply {} downgrades from {} to {}", downgrades, oldVers, vers); | ||
} | ||
stateDB.setMetadataVersion(current); | ||
} | ||
|
||
/// Set the current version in the metadata | ||
private void setMetadataVersion (ManagedDatabase stateDB, string version_) | ||
{ | ||
stateDB.execute("INSERT OR REPLACE INTO metadata (key, value) VALUES ('version', ?)", version_); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sqlite have this