Skip to content
Open
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
4 changes: 3 additions & 1 deletion doc/operations/recovery.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ To initiate the first phase of the recovery procedure, one or several nodes shou
...
$ /opt/ccf/bin/js_generic --config /path/to/config/file

Each node will then immediately restore the public entries of its ledger (``ledger.directory`` and ``ledger.read_only_directories`` configuration entries). Because deserialising the public entries present in the ledger may take some time, operators can query the progress of the public recovery by calling :http:GET:`/node/state` which returns the version of the last signed recovered ledger entry. Once the public ledger is fully recovered, the recovered node automatically becomes part of the public network, allowing other nodes to join the network.
Each node will then immediately restore the public entries of its ledger (``ledger.directory`` and ``ledger.read_only_directories`` configuration entries).
Because installing a snapshot and deserialising the public entries present in the ledger may take some time, operators can query progress throughout snapshot installation and public ledger recovery by calling :http:GET:`/node/state`, which returns the version of the last signed recovered ledger entry.
Once the public ledger is fully recovered, the recovered node automatically becomes part of the public network, allowing other nodes to join the network.

At startup, the host discovers the latest committed chunk end across all configured ledger directories before restoring mutable chunks. Mutable chunks whose reconstructed end is at or before this committed frontier, including incomplete chunks without an end index in their filename, are renamed with an ``.ignored`` suffix rather than reused for new appends. Chunks which overlap the committed history but extend beyond that frontier are retained.

Expand Down
2 changes: 1 addition & 1 deletion doc/releases/migration_7_x_to_8_x.rst
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,4 @@ From CCF 8.0, ``public:ccf.internal.signatures``, ``public:ccf.internal.cose_sig
Use historical KV views, :doc:`receipt APIs </use_apps/rpc_api>` or :doc:`offline ledger parsing </audit/python_library>` instead; see :ref:`audit/read_write_restrictions:Signature-Transaction Tables` and :ref:`build_apps/kv/kv_how_to:Signature Metadata`.

- Custom constitutions based on releases before 7.0.17 that read ``public:ccf.internal.signatures`` or ``public:ccf.internal.cose_signatures`` when adding or resetting a member must remove those reads and use the :ref:`state-digest update and acknowledgement workflow <governance/adding_member:Activating a New Member>`.
- :http:GET:`/node/state` is now non-transactional: it no longer returns the ``x-ms-ccf-transaction-id`` header, and it is served before the KV store is ready, including during public ledger recovery.
- :http:GET:`/node/state` is now non-transactional: it no longer returns the ``x-ms-ccf-transaction-id`` header, and it is served before the KV store is ready, including throughout public ledger recovery and snapshot installation.
138 changes: 100 additions & 38 deletions src/node/node_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,58 @@ namespace ccf
ds::Mutex lock;
StartType start_type = StartType::Start;

friend struct NodeStateTestPeer;

struct PublishedProgress
{
NodeStartupState state = NodeStartupState::uninitialized;
ccf::kv::Version recovery_target_seqno = 0;
ccf::kv::Version last_recovered_private_seqno = 0;
};

// Serializes state-machine transitions and progress publication so a
// concurrent transition (e.g. join advancing to partOfPublicNetwork vs.
// global commit hook starting private recovery) cannot publish an older
// state over a newer one. Never acquires NodeState::lock or KV locks.
ccf::ds::Mutex state_transition_lock;

// Dedicated leaf mutex for the coupled state/private-recovery progress
// fields polled by /node/state. Never held across snapshot deserialisation,
// ledger replay, KV operations, logging, or NodeState::lock acquisition.
mutable ccf::ds::Mutex progress_lock;
PublishedProgress published_progress CCF_GUARDED_BY(progress_lock);

void advance_state(NodeStartupState next_state)
{
CCF_ASSERT_FMT(
next_state != NodeStartupState::readingPrivateLedger,
"Use advance_to_reading_private_ledger() to enter "
"readingPrivateLedger");
ccf::ds::MutexGuard transition_guard(state_transition_lock);
sm.advance(next_state);
ccf::ds::MutexGuard progress_guard(progress_lock);
published_progress.state = next_state;
published_progress.recovery_target_seqno = 0;
published_progress.last_recovered_private_seqno = 0;
}

void advance_to_reading_private_ledger(
ccf::kv::Version target_seqno, ccf::kv::Version recovered_seqno)
{
ccf::ds::MutexGuard transition_guard(state_transition_lock);
sm.advance(NodeStartupState::readingPrivateLedger);
ccf::ds::MutexGuard progress_guard(progress_lock);
published_progress.state = NodeStartupState::readingPrivateLedger;
published_progress.recovery_target_seqno = target_seqno;
published_progress.last_recovered_private_seqno = recovered_seqno;
}

void publish_private_recovery_progress(ccf::kv::Version recovered_seqno)
{
ccf::ds::MutexGuard guard(progress_lock);
published_progress.last_recovered_private_seqno = recovered_seqno;
}

ccf::crypto::CurveID curve_id;
std::vector<ccf::crypto::SubjectAltName> subject_alt_names;

Expand Down Expand Up @@ -525,7 +577,7 @@ namespace ccf
ccf::kv::Version recovery_v = 0;
ccf::crypto::Sha256Hash recovery_root;
std::vector<ccf::kv::Version> view_history;
::consensus::Index last_recovered_signed_idx = 0;
std::atomic<::consensus::Index> last_recovered_signed_idx = 0;

// Dedicated mutex for the two recovery fields below, which are written
// from KV hooks and read from governance paths which must not take the
Expand Down Expand Up @@ -580,7 +632,7 @@ namespace ccf
bool startup_snapshot_consumed = false;
// Set to the snapshot seqno when a node starts from one and remembered for
// the lifetime of the node
ccf::kv::Version startup_seqno = 0;
std::atomic<ccf::kv::Version> startup_seqno = 0;

ccf::tasks::Task join_periodic_task;
ccf::tasks::Task snapshot_fetch_task;
Expand Down Expand Up @@ -831,9 +883,10 @@ namespace ccf
LOG_INFO_FMT(
"Setting startup snapshot seqno to {}", startup_snapshot_info->seqno);

startup_seqno = startup_snapshot_info->seqno;
last_recovered_idx = startup_seqno;
last_recovered_signed_idx = last_recovered_idx;
const auto snapshot_seqno = startup_snapshot_info->seqno;
last_recovered_idx = snapshot_seqno;
last_recovered_signed_idx.store(last_recovered_idx);
startup_seqno.store(snapshot_seqno);

if (start_type == StartType::Join)
{
Expand Down Expand Up @@ -874,7 +927,7 @@ namespace ccf

void start_public_ledger_recovery_unsafe()
{
sm.advance(NodeStartupState::readingPublicLedger);
advance_state(NodeStartupState::readingPublicLedger);
start_ledger_recovery_unsafe();
}

Expand Down Expand Up @@ -1119,8 +1172,8 @@ namespace ccf

if (resp.network_info->public_only)
{
last_recovered_signed_idx =
resp.network_info->last_recovered_signed_idx;
last_recovered_signed_idx.store(
resp.network_info->last_recovered_signed_idx);
setup_recovery_hook();
snapshotter->set_snapshot_generation(false);
}
Expand Down Expand Up @@ -1202,7 +1255,7 @@ namespace ccf
network.tables->current_version(),
view,
view_history_,
last_recovered_signed_idx);
last_recovered_signed_idx.load());

{
auto snap_tx = network.tables->create_read_only_tx();
Expand All @@ -1218,13 +1271,13 @@ namespace ccf

if (resp.network_info->public_only)
{
sm.advance(NodeStartupState::partOfPublicNetwork);
advance_state(NodeStartupState::partOfPublicNetwork);
}
else
{
reset_data(quote_info.quote);
reset_data(quote_info.endorsements);
sm.advance(NodeStartupState::partOfNetwork);
advance_state(NodeStartupState::partOfNetwork);
}

if (join_periodic_task != nullptr)
Expand Down Expand Up @@ -1333,7 +1386,7 @@ namespace ccf
cmd_forwarder = std::make_shared<Forwarder<NodeToNode>>(
rpc_sessions_, n2n_channels, rpc_map);

sm.advance(NodeStartupState::initialized);
advance_state(NodeStartupState::initialized);

for (auto& [actor, fe] : rpc_map->frontends())
{
Expand Down Expand Up @@ -1456,7 +1509,7 @@ namespace ccf
{
find_local_startup_snapshot();

sm.advance(NodeStartupState::pending);
advance_state(NodeStartupState::pending);
start_join_timer();
return;
}
Expand Down Expand Up @@ -1881,7 +1934,7 @@ namespace ccf
join_params.node_info_network = config.network;
join_params.public_encryption_key = node_encrypt_kp->public_key_pem();
join_params.quote_info = quote_info;
join_params.startup_seqno = startup_seqno;
join_params.startup_seqno = startup_seqno.load();
if (config.command.join.fetch_recent_snapshot)
{
join_params.join_fetch_count = join_fetch_count;
Expand Down Expand Up @@ -2270,14 +2323,14 @@ namespace ccf
// necessarily start in view 1), it cannot _change_ view before a
// valid signature.
const auto view_start_idx =
view_history.empty() ? 1 : last_recovered_signed_idx + 1;
view_history.empty() ? 1 : last_recovered_signed_idx.load() + 1;
CCF_ASSERT_FMT(sig_view >= 0, "sig_view is invalid, {}", sig_view);
for (auto i = view_history.size(); i < static_cast<size_t>(sig_view);
++i)
{
view_history.push_back(view_start_idx);
}
last_recovered_signed_idx = last_recovered_idx;
last_recovered_signed_idx.store(last_recovered_idx);
}
}

Expand All @@ -2290,7 +2343,7 @@ namespace ccf
std::lock_guard<ds::Mutex> guard(lock);
sm.expect(NodeStartupState::readingPublicLedger);
history->start_signature_emit_timer();
sm.advance(NodeStartupState::partOfPublicNetwork);
advance_state(NodeStartupState::partOfPublicNetwork);
}

void advance_part_of_network()
Expand All @@ -2301,7 +2354,7 @@ namespace ccf
auto_refresh_jwt_keys();
reset_data(quote_info.quote);
reset_data(quote_info.endorsements);
sm.advance(NodeStartupState::partOfNetwork);
advance_state(NodeStartupState::partOfNetwork);
}

void recover_public_ledger_end_unsafe()
Expand All @@ -2314,20 +2367,22 @@ namespace ccf
auto new_term = last_recovered_term + aft::starting_view_change;
LOG_INFO_FMT("Setting term on public recovery store to {}", new_term);

const auto recovered_signed_idx = last_recovered_signed_idx.load();

// Note: KV term must be set before the first Tx is committed
network.tables->rollback(
{last_recovered_term, last_recovered_signed_idx}, new_term);
truncate_ledger(last_recovered_signed_idx, true);
snapshotter->rollback(last_recovered_signed_idx);
{last_recovered_term, recovered_signed_idx}, new_term);
truncate_ledger(recovered_signed_idx, true);
snapshotter->rollback(recovered_signed_idx);

LOG_INFO_FMT(
"End of public ledger recovery - Truncating ledger to last signed "
"TxID: {}.{}",
last_recovered_term,
last_recovered_signed_idx);
recovered_signed_idx);

auto tx = network.tables->create_read_only_tx_at_replicated_state();
network.ledger_secrets->init(last_recovered_signed_idx + 1);
network.ledger_secrets->init(recovered_signed_idx + 1);

// Initialise snapshotter after public recovery
snapshotter->init_after_public_recovery();
Expand Down Expand Up @@ -2470,7 +2525,7 @@ namespace ccf
"Failed to find sealed recovery data for location ({}) in ledger "
"at {}",
name,
last_recovered_signed_idx));
last_recovered_signed_idx.load()));
}

{
Expand Down Expand Up @@ -2536,6 +2591,8 @@ namespace ccf
// Note: rollback terms do not matter here as recovery store is
// about to be discarded
recovery_store->rollback({0, last_recovered_idx}, 0);
publish_private_recovery_progress(
recovery_store->current_version());
recover_private_ledger_end_unsafe();
return;
}
Expand All @@ -2545,10 +2602,13 @@ namespace ccf
{
LOG_FAIL_FMT(
"Failed to deserialise private ledger entry: {}", e.what());
publish_private_recovery_progress(recovery_store->current_version());
recover_private_ledger_end_unsafe();
return;
}

publish_private_recovery_progress(recovery_store->current_version());

if (result == ccf::kv::ApplyResult::PASS_SIGNATURE)
{
recovery_store->compact(last_recovered_idx);
Expand Down Expand Up @@ -2627,7 +2687,7 @@ namespace ccf
}
reset_data(quote_info.quote);
reset_data(quote_info.endorsements);
sm.advance(NodeStartupState::partOfNetwork);
advance_state(NodeStartupState::partOfNetwork);
if (recovered_service_opening.load() == RecoveredServiceOpening::Pending)
{
auto& board = ccf::tasks::get_main_job_board();
Expand Down Expand Up @@ -2786,7 +2846,7 @@ namespace ccf
"Startup snapshot at {} was consumed by an earlier private "
"recovery "
"attempt. Restart the node to retry recovery.",
startup_seqno));
startup_seqno.load()));
}

if (startup_snapshot_info)
Expand Down Expand Up @@ -3177,14 +3237,16 @@ namespace ccf

ExtendedState state() override
{
std::lock_guard<ds::Mutex> guard(lock);
auto s = sm.value();
if (s == NodeStartupState::readingPrivateLedger)
ccf::ds::MutexGuard guard(progress_lock);
if (published_progress.state == NodeStartupState::readingPrivateLedger)
{
return {s, recovery_v, recovery_store->current_version()};
return {
published_progress.state,
published_progress.recovery_target_seqno,
published_progress.last_recovered_private_seqno};
}

return {s, std::nullopt, std::nullopt};
return {published_progress.state, std::nullopt, std::nullopt};
}

bool rekey_ledger(ccf::kv::Tx& tx) override
Expand Down Expand Up @@ -3228,8 +3290,7 @@ namespace ccf

ccf::kv::Version get_startup_snapshot_seqno() override
{
std::lock_guard<ds::Mutex> guard(lock);
return startup_seqno;
return startup_seqno.load();
}

SessionMetrics get_session_metrics() override
Expand Down Expand Up @@ -3402,7 +3463,8 @@ namespace ccf
create_params.node_info_network = config.network;
create_params.node_data = startup_inputs.node_data;
create_params.service_data = startup_inputs.service_data;
create_params.create_txid = {create_view, last_recovered_signed_idx + 1};
create_params.create_txid = {
create_view, last_recovered_signed_idx.load() + 1};

if (config.sealing_recovery.has_value() && snp_tcb_version.has_value())
{
Expand Down Expand Up @@ -3511,8 +3573,9 @@ namespace ccf
opening, RecoveredServiceOpening::Pending);

// Start reading private security domain of ledger
sm.advance(NodeStartupState::readingPrivateLedger);
last_recovered_idx = recovery_store->current_version();
const auto initial_recovered_version = recovery_store->current_version();
advance_to_reading_private_ledger(recovery_v, initial_recovered_version);
last_recovered_idx = initial_recovered_version;
read_ledger_entries(
last_recovered_idx + 1, last_recovered_idx + recovery_batch_size);
}
Expand Down Expand Up @@ -3888,8 +3951,7 @@ namespace ccf
// from. If the primary changes while the network is public-only, the
// new primary should also know at which version the new ledger secret
// is applicable from.
std::lock_guard<ds::Mutex> guard(lock);
return last_recovered_signed_idx;
return last_recovered_signed_idx.load();
}

void setup_recovery_hook()
Expand Down
2 changes: 1 addition & 1 deletion src/node/recovery_decision_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ namespace ccf

ccf::TxID RecoveryDecisionProtocolSubsystem::get_last_recovered_signed_txid()
{
auto recovery_seqno = node_state->last_recovered_signed_idx;
auto recovery_seqno = node_state->get_last_recovered_signed_idx();
Comment thread
achamayou marked this conversation as resolved.
Resolved
auto recovery_view = node_state->consensus->get_view(recovery_seqno);
// get_view returns VIEW_UNKNOWN=InvalidView if the view is not in the view
// history (too old or too new)
Expand Down
Loading