Skip to content

Commit

Permalink
Make backup a cmdline option.
Browse files Browse the repository at this point in the history
  • Loading branch information
evoskuil committed May 8, 2024
1 parent 54e70c1 commit 4581d95
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 46 deletions.
93 changes: 54 additions & 39 deletions console/executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1875,53 +1875,57 @@ bool executor::close_store(bool details)
return true;
}

bool executor::backup_store(bool details)
bool executor::restore_store(bool details)
{
logger(BN_NODE_BACKUP_STARTED);
console(format(BN_RESTORING_CHAIN));
const auto start = logger::now();
if (const auto ec = store_.snapshot([&](auto event, auto table)
if (const auto ec = store_.restore([&](auto event, auto table)
{
// Suspend channels that missed previous suspend events.
if (node_ && event == database::event_t::wait_lock)
node_->suspend(error::store_snapshotting);

if (details)
logger(format(BN_BACKUP) % events_.at(event) % tables_.at(table));
console(format(BN_RESTORE) % events_.at(event) % tables_.at(table));
}))
{
logger(format(BN_NODE_BACKUP_FAIL) % ec.message());
if (ec == database::error::flush_lock)
console(BN_RESTORE_MISSING_FLUSH_LOCK);
else
console(format(BN_RESTORE_FAILURE) % ec.message());

return false;
}

const auto span = duration_cast<seconds>(logger::now() - start);
logger(format(BN_NODE_BACKUP_COMPLETE) % span.count());
console(format(BN_RESTORE_COMPLETE) % span.count());
return true;
}

bool executor::restore_store(bool details)
bool executor::backup_store(auto&& writer, bool details)
{
console(format(BN_RESTORING_CHAIN));
if (const auto ec = store_.restore([&](auto event, auto table)
writer << (BN_NODE_BACKUP_STARTED) << std::endl;
const auto start = logger::now();
if (const auto ec = store_.snapshot([&](auto event, auto table)
{
// Suspend channels that missed previous suspend events.
if (node_ && event == database::event_t::wait_lock)
node_->suspend(error::store_snapshotting);

if (details)
console(format(BN_RESTORE) % events_.at(event) % tables_.at(table));
writer << (format(BN_BACKUP) % events_.at(event) % tables_.at(table))
<< std::endl;
}))
{
if (ec == database::error::flush_lock)
console(BN_RESTORE_MISSING_FLUSH_LOCK);
else
console(format(BN_RESTORE_FAILURE) % ec.message());

writer << (format(BN_NODE_BACKUP_FAIL) % ec.message()) << std::endl;
return false;
}

const auto span = duration_cast<seconds>(logger::now() - start);
writer << (format(BN_NODE_BACKUP_COMPLETE) % span.count()) << std::endl;
return true;
}

// Command line options.
// ----------------------------------------------------------------------------

// --help[h]
// --[h]elp
bool executor::do_help()
{
log_.stop();
Expand All @@ -1931,14 +1935,14 @@ bool executor::do_help()
return true;
}

// --hardware[d]
// --har[d]ware
bool executor::do_hardware()
{
console(format("Coming soon..."));
return true;
}

// --settings[s]
// --[s]ettings
bool executor::do_settings()
{
log_.stop();
Expand All @@ -1948,7 +1952,7 @@ bool executor::do_settings()
return true;
}

// --version[v]
// --[v]ersion
bool executor::do_version()
{
log_.stop();
Expand All @@ -1960,7 +1964,7 @@ bool executor::do_version()
return true;
}

// --initchain[i]
// --[i]nitchain
bool executor::do_initchain()
{
log_.stop();
Expand Down Expand Up @@ -1994,27 +1998,35 @@ bool executor::do_initchain()
return true;
}

// --restore[x]
bool executor::do_restore()
// --[b]ackup
bool executor::do_snapshot()
{
log_.stop();
const auto start = logger::now();
if (!check_store_path() || !restore_store(true))
if (!check_store_path() || !open_store() || !backup_store(output_, true))
return false;

// backup_store leaves open, designed for ongoing runtime.
// This one can take a few seconds on cold iron.
console(BN_MEASURE_PROGRESS_START);
dump_progress(output_);
return close_store(true);
}

if (!close_store(true))
// --restore[x]
bool executor::do_restore()
{
log_.stop();
if (!check_store_path() || !restore_store(true))
return false;

const auto span = duration_cast<seconds>(logger::now() - start);
console(format(BN_RESTORE_COMPLETE) % span.count());
return true;
// restore_store leaves open, designed for startup runtime.
// This one can take a few seconds on cold iron.
console(BN_MEASURE_PROGRESS_START);
dump_progress(output_);
return close_store(true);
}

// --flags[f]
// --[f]lags
bool executor::do_flags()
{
log_.stop();
Expand All @@ -2029,7 +2041,7 @@ bool executor::do_flags()
return true;
}

// --measure[m]
// --[m]easure
bool executor::do_measure()
{
log_.stop();
Expand All @@ -2044,7 +2056,7 @@ bool executor::do_measure()
return true;
}

// --slabs[a]
// --sl[a]bs
bool executor::do_slabs()
{
log_.stop();
Expand All @@ -2059,7 +2071,7 @@ bool executor::do_slabs()
return true;
}

// --buckets[b]
// --buc[k]ets
bool executor::do_buckets()
{
log_.stop();
Expand Down Expand Up @@ -2138,7 +2150,7 @@ void executor::do_backup()
}

node_->suspend(error::store_snapshotting);
backup_store();
backup_store(log_.write(levels::application), true);
do_toggle_suspend();
}

Expand Down Expand Up @@ -2227,8 +2239,8 @@ bool executor::menu()
if (config.slabs)
return do_slabs();

if (config.buckets)
return do_buckets();
if (config.backup)
return do_snapshot();

if (config.hardware)
return do_hardware();
Expand All @@ -2239,6 +2251,9 @@ bool executor::menu()
if (config.initchain)
return do_initchain();

if (config.buckets)
return do_buckets();

if (config.collisions)
return do_collisions();

Expand Down
13 changes: 7 additions & 6 deletions console/executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,20 @@ class executor
void dump_collisions(auto&& writer) const;

// Store functions.
bool check_store_path(bool create = false) const;
bool open_store(bool details = false);
bool close_store(bool details = false);
bool create_store(bool details = false);
bool backup_store(bool details = false);
bool restore_store(bool details = false);
bool open_store(bool details=false);
bool close_store(bool details=false);
bool create_store(bool details=false);
bool restore_store(bool details=false);
bool check_store_path(bool create=false) const;
bool backup_store(auto&& writer, bool details=false);

// Command line options.
bool do_help();
bool do_hardware();
bool do_settings();
bool do_version();
bool do_initchain();
bool do_snapshot();
bool do_restore();
bool do_flags();
bool do_measure();
Expand Down
2 changes: 2 additions & 0 deletions include/bitcoin/node/configuration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace node {
#define BN_SETTINGS_VARIABLE "settings"
#define BN_VERSION_VARIABLE "version"
#define BN_INITCHAIN_VARIABLE "initchain"
#define BN_BACKUP_VARIABLE "backup"
#define BN_RESTORE_VARIABLE "restore"

#define BN_FLAGS_VARIABLE "flags"
Expand Down Expand Up @@ -69,6 +70,7 @@ class BCN_API configuration

/// Actions.
bool initchain{};
bool backup{};
bool restore{};

/// Chain scans.
Expand Down
8 changes: 7 additions & 1 deletion src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ options_metadata parser::load_options() THROWS
default_value(false)->zero_tokens(),
"Initialize store in configured directory."
)
(
BN_BACKUP_VARIABLE ",b",
value<bool>(&configured.backup)->
default_value(false)->zero_tokens(),
"Backup to a snapshot (can also do live)."
)
(
BN_RESTORE_VARIABLE ",x",
value<bool>(&configured.restore)->
Expand All @@ -195,7 +201,7 @@ options_metadata parser::load_options() THROWS
"Scan and display store slab measures."
)
(
BN_BUCKETS_VARIABLE ",b",
BN_BUCKETS_VARIABLE ",k",
value<bool>(&configured.buckets)->
default_value(false)->zero_tokens(),
"Scan and display all bucket densities."
Expand Down
1 change: 1 addition & 0 deletions test/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ BOOST_AUTO_TEST_CASE(configuration__construct1__none_context__expected)
BOOST_REQUIRE(!instance.settings);
BOOST_REQUIRE(!instance.version);
BOOST_REQUIRE(!instance.initchain);
BOOST_REQUIRE(!instance.backup);
BOOST_REQUIRE(!instance.restore);
BOOST_REQUIRE(!instance.flags);
BOOST_REQUIRE(!instance.measure);
Expand Down

0 comments on commit 4581d95

Please sign in to comment.