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 executor console command "b" for backup (snapshot). #573

Merged
merged 10 commits into from
Mar 16, 2024
Merged
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
2 changes: 2 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ src_libbitcoin_node_la_SOURCES = \
src/protocols/protocol_header_in_70012.cpp \
src/protocols/protocol_header_out_31800.cpp \
src/protocols/protocol_header_out_70012.cpp \
src/protocols/protocol_observer.cpp \
src/protocols/protocol_transaction_in.cpp \
src/protocols/protocol_transaction_out.cpp \
src/sessions/session.cpp \
Expand Down Expand Up @@ -153,6 +154,7 @@ include_bitcoin_node_protocols_HEADERS = \
include/bitcoin/node/protocols/protocol_header_in_70012.hpp \
include/bitcoin/node/protocols/protocol_header_out_31800.hpp \
include/bitcoin/node/protocols/protocol_header_out_70012.hpp \
include/bitcoin/node/protocols/protocol_observer.hpp \
include/bitcoin/node/protocols/protocol_transaction_in.hpp \
include/bitcoin/node/protocols/protocol_transaction_out.hpp \
include/bitcoin/node/protocols/protocols.hpp
Expand Down
1 change: 1 addition & 0 deletions builds/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ add_library( ${CANONICAL_LIB_NAME}
"../../src/protocols/protocol_header_in_70012.cpp"
"../../src/protocols/protocol_header_out_31800.cpp"
"../../src/protocols/protocol_header_out_70012.cpp"
"../../src/protocols/protocol_observer.cpp"
"../../src/protocols/protocol_transaction_in.cpp"
"../../src/protocols/protocol_transaction_out.cpp"
"../../src/sessions/session.cpp"
Expand Down
2 changes: 2 additions & 0 deletions builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
<ClCompile Include="..\..\..\..\src\protocols\protocol_header_in_70012.cpp" />
<ClCompile Include="..\..\..\..\src\protocols\protocol_header_out_31800.cpp" />
<ClCompile Include="..\..\..\..\src\protocols\protocol_header_out_70012.cpp" />
<ClCompile Include="..\..\..\..\src\protocols\protocol_observer.cpp" />
<ClCompile Include="..\..\..\..\src\protocols\protocol_transaction_in.cpp" />
<ClCompile Include="..\..\..\..\src\protocols\protocol_transaction_out.cpp" />
<ClCompile Include="..\..\..\..\src\sessions\session.cpp" />
Expand Down Expand Up @@ -126,6 +127,7 @@
<ClInclude Include="..\..\..\..\include\bitcoin\node\protocols\protocol_header_in_70012.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\protocols\protocol_header_out_31800.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\protocols\protocol_header_out_70012.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\protocols\protocol_observer.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\protocols\protocol_transaction_in.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\protocols\protocol_transaction_out.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\protocols\protocols.hpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@
<ClCompile Include="..\..\..\..\src\protocols\protocol_header_out_70012.cpp">
<Filter>src\protocols</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\src\protocols\protocol_observer.cpp">
<Filter>src\protocols</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\src\protocols\protocol_transaction_in.cpp">
<Filter>src\protocols</Filter>
</ClCompile>
Expand Down Expand Up @@ -203,6 +206,9 @@
<ClInclude Include="..\..\..\..\include\bitcoin\node\protocols\protocol_header_out_70012.hpp">
<Filter>include\bitcoin\node\protocols</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\node\protocols\protocol_observer.hpp">
<Filter>include\bitcoin\node\protocols</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\node\protocols\protocol_transaction_in.hpp">
<Filter>include\bitcoin\node\protocols</Filter>
</ClInclude>
Expand Down
31 changes: 29 additions & 2 deletions console/executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ using namespace std::placeholders;
// "c" avoids conflict with network "quit" messages.
const std::string executor::name_{ "bn" };
const std::string executor::close_{ "c" };
const std::string executor::backup_{ "b" };
const std::unordered_map<uint8_t, bool> executor::defined_
{
{ levels::application, true },
Expand Down Expand Up @@ -93,10 +94,16 @@ const std::unordered_map<database::event_t, std::string> executor::events_
{ database::event_t::close_file, "close_file" },
{ database::event_t::create_table, "create_table" },
{ database::event_t::verify_table, "verify_table" },
{ database::event_t::close_table, "close_table" }
{ database::event_t::close_table, "close_table" },
{ database::event_t::wait_lock, "wait_lock" },
{ database::event_t::flush_table, "flush_table" },
{ database::event_t::backup_table, "backup_table" },
{ database::event_t::dump_table, "dump_table" },
{ database::event_t::restore_table, "restore_table" }
};
const std::unordered_map<database::table_t, std::string> executor::tables_
{
{ database::table_t::store, "store" },
{ database::table_t::header_table, "header_table" },
{ database::table_t::header_head, "header_head" },
{ database::table_t::header_body, "header_body" },
Expand Down Expand Up @@ -1603,14 +1610,34 @@ void executor::subscribe_capture()
{
const auto token = system::trim_copy(line);

// Close (this isn't a toggle).
// Close (not a toggle).
if (token == close_)
{
logger("CONSOLE: Close");
stop(error::success);
return false;
}

// Backup (not a toggle).
if (token == backup_)
{
logger(BN_NODE_BACKUP_STARTED);
node_->pause();

const auto error = store_.snapshot([&](auto event, auto table)
{
logger(format(BN_BACKUP) % events_.at(event) % tables_.at(table));
});

if (error)
logger(format(BN_NODE_BACKUP_FAIL) % error.message());
else
logger(format(BN_NODE_BACKUP_COMPLETE));

node_->resume();
return !error;
}

if (!keys_.contains(token))
{
logger("CONSOLE: '" + line + "'");
Expand Down
1 change: 1 addition & 0 deletions console/executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class executor

static const std::string name_;
static const std::string close_;
static const std::string backup_;
static const std::unordered_map<uint8_t, bool> defined_;
static const std::unordered_map<uint8_t, std::string> display_;
static const std::unordered_map<std::string, uint8_t> keys_;
Expand Down
8 changes: 8 additions & 0 deletions console/localize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ namespace node {
"open::%1%(%2%)"
#define BN_CLOSE \
"close::%1%(%2%)"
#define BN_BACKUP \
"backup::%1%(%2%)"

#define BN_NODE_INTERRUPT \
"Press CTRL-C to stop the node."
Expand All @@ -141,6 +143,12 @@ namespace node {
"Please wait while the network is starting..."
#define BN_NODE_START_FAIL \
"Node failed to start with error, %1%."
#define BN_NODE_BACKUP_STARTED \
"Node backup started."
#define BN_NODE_BACKUP_FAIL \
"Node failed to backup with error, %1%."
#define BN_NODE_BACKUP_COMPLETE \
"Node backup complete."
#define BN_NODE_STARTED \
"Node is started."
#define BN_NODE_RUNNING \
Expand Down
1 change: 1 addition & 0 deletions include/bitcoin/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include <bitcoin/node/protocols/protocol_header_in_70012.hpp>
#include <bitcoin/node/protocols/protocol_header_out_31800.hpp>
#include <bitcoin/node/protocols/protocol_header_out_70012.hpp>
#include <bitcoin/node/protocols/protocol_observer.hpp>
#include <bitcoin/node/protocols/protocol_transaction_in.hpp>
#include <bitcoin/node/protocols/protocol_transaction_out.hpp>
#include <bitcoin/node/protocols/protocols.hpp>
Expand Down
8 changes: 8 additions & 0 deletions include/bitcoin/node/chasers/chaser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ class BCN_API chaser
/// Issued by 'session_outbound' and handled by 'block_in_31800'.
stall,

/// Channels (all) are directed to pause reading.
/// Iissued by 'full_node' and handled by 'protocol'.
pause,

/// Channels (all) are directed to resume reading.
/// Iissued by 'full_node' and handled by 'protocol'.
resume,

/// A block has been downloaded, checked and stored (height_t).
/// Issued by 'block_in_31800' and handled by 'connect'.
checked,
Expand Down
7 changes: 7 additions & 0 deletions include/bitcoin/node/full_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
namespace libbitcoin {
namespace node {

// Thread safe.
class BCN_API full_node
: public network::p2p
{
Expand Down Expand Up @@ -81,6 +82,12 @@ class BCN_API full_node
/// Methods.
/// -----------------------------------------------------------------------

/// Pause the node.
virtual void pause() NOEXCEPT;

/// Resume the node.
virtual void resume() NOEXCEPT;

/// The candidate chain is current.
virtual bool is_current() const NOEXCEPT;

Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/node/protocols/protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
namespace libbitcoin {
namespace node {

/// Abstract base for node protocols.
/// Abstract base for node protocols, thread safe.
class BCN_API protocol
: public network::protocol
{
Expand Down
3 changes: 2 additions & 1 deletion include/bitcoin/node/protocols/protocol_block_in_31800.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#ifndef LIBBITCOIN_NODE_PROTOCOLS_PROTOCOL_BLOCK_IN_31800_HPP
#define LIBBITCOIN_NODE_PROTOCOLS_PROTOCOL_BLOCK_IN_31800_HPP

#include <functional>
#include <memory>
#include <bitcoin/network.hpp>
#include <bitcoin/node/chasers/chasers.hpp>
Expand Down Expand Up @@ -72,6 +71,8 @@ class BCN_API protocol_block_in_31800
chaser::chase event_, chaser::link value) NOEXCEPT;
virtual void do_get_downloads(chaser::count_t count) NOEXCEPT;
virtual void do_split(chaser::channel_t channel) NOEXCEPT;
void do_pause(chaser::channel_t channel) NOEXCEPT;
void do_resume(chaser::channel_t channel) NOEXCEPT;

/// Accept incoming block message.
virtual bool handle_receive_block(const code& ec,
Expand Down
56 changes: 56 additions & 0 deletions include/bitcoin/node/protocols/protocol_observer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Copyright (c) 2011-2023 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBBITCOIN_NODE_PROTOCOLS_PROTOCOL_OBSERVER_HPP
#define LIBBITCOIN_NODE_PROTOCOLS_PROTOCOL_OBSERVER_HPP

#include <bitcoin/network.hpp>
#include <bitcoin/node/define.hpp>
#include <bitcoin/node/protocols/protocol.hpp>

namespace libbitcoin {
namespace node {

class BCN_API protocol_observer
: public node::protocol,
protected network::tracker<protocol_observer>
{
public:
typedef std::shared_ptr<protocol_observer> ptr;

template <typename Session>
protocol_observer(Session& session,
const channel_ptr& channel) NOEXCEPT
: node::protocol(session, channel),
network::tracker<protocol_observer>(session.log)
{
}

void start() NOEXCEPT override;

virtual void handle_event(const code& ec,
chaser::chase event_, chaser::link value) NOEXCEPT;

void do_pause(chaser::channel_t channel) NOEXCEPT;
void do_resume(chaser::channel_t channel) NOEXCEPT;
};

} // namespace node
} // namespace libbitcoin

#endif
1 change: 1 addition & 0 deletions include/bitcoin/node/protocols/protocols.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <bitcoin/node/protocols/protocol_header_in_70012.hpp>
#include <bitcoin/node/protocols/protocol_header_out_31800.hpp>
#include <bitcoin/node/protocols/protocol_header_out_70012.hpp>
#include <bitcoin/node/protocols/protocol_observer.hpp>
#include <bitcoin/node/protocols/protocol_transaction_in.hpp>
#include <bitcoin/node/protocols/protocol_transaction_out.hpp>

Expand Down
1 change: 1 addition & 0 deletions include/bitcoin/node/sessions/attach.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class attach
channel->attach<protocol_block_out>(self)->start();
channel->attach<protocol_transaction_in>(self)->start();
channel->attach<protocol_transaction_out>(self)->start();
channel->attach<protocol_observer>(self)->start();
}
};

Expand Down
11 changes: 10 additions & 1 deletion src/full_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,16 @@ void full_node::do_notify(const code& ec, chaser::chase event_,
// Methods.
// ----------------------------------------------------------------------------

void full_node::pause() NOEXCEPT
{
notify(error::success, chaser::chase::pause, {});
}

void full_node::resume() NOEXCEPT
{
notify(error::success, chaser::chase::resume, {});
}

bool full_node::is_current() const NOEXCEPT
{
if (is_zero(config_.node.currency_window_minutes))
Expand Down Expand Up @@ -207,7 +217,6 @@ chaser::event_subscriber& full_node::event_subscriber() NOEXCEPT
return event_subscriber_;
}

// protected
const configuration& full_node::config() const NOEXCEPT
{
return config_;
Expand Down
33 changes: 27 additions & 6 deletions src/protocols/protocol_block_in_31800.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,39 +201,60 @@ void protocol_block_in_31800::stopping(const code& ec) NOEXCEPT
void protocol_block_in_31800::handle_event(const code&,
chaser::chase event_, chaser::link value) NOEXCEPT
{
constexpr auto minimum_for_stall_divide = 2_size;

if (stopped())
return;

// There are count blocks to download at/above the given header.
if (event_ == chaser::chase::download)
{
// There are count blocks to download at/above the given header.
if (is_current())
{
BC_ASSERT(std::holds_alternative<chaser::count_t>(value));
POST(do_get_downloads, std::get<chaser::count_t>(value));
}
}

// If value identifies this channel, split work and stop.
else if (event_ == chaser::chase::split)
{
BC_ASSERT(std::holds_alternative<chaser::channel_t>(value));
const auto channel = std::get<chaser::channel_t>(value);

// If value identifies this channel, split work and stop.
if (channel == identifier())
{
POST(do_split, channel);
}
}

// If this channel has work, split it and stop.
else if (event_ == chaser::chase::stall)
{
if (!map_->empty())
// If this channel has divisible work, split it and stop.
if (map_->size() >= minimum_for_stall_divide)
{
POST(do_split, chaser::count_t{});
}
}
else if (event_ == chaser::chase::pause)
{
// Pause local timers due to channel pause.
POST(do_pause, chaser::channel_t{});
}
else if (event_ == chaser::chase::resume)
{
// Resume local timers due to channel resume.
POST(do_resume, chaser::channel_t{});
}
}

void protocol_block_in_31800::do_pause(chaser::channel_t) NOEXCEPT
{
pause_performance();
}

void protocol_block_in_31800::do_resume(chaser::channel_t) NOEXCEPT
{
if (!map_->empty())
start_performance();
}

void protocol_block_in_31800::do_get_downloads(chaser::count_t) NOEXCEPT
Expand Down
Loading