From 05f041faefbc1016014a226642174a68d5372d38 Mon Sep 17 00:00:00 2001 From: Amaury Chamayou Date: Mon, 25 Jan 2021 18:00:23 +0000 Subject: [PATCH 1/4] . --- src/kv/kv_types.h | 3 ++- src/kv/store.h | 5 ++++- src/node/history.h | 18 ++++++++++++++++-- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/kv/kv_types.h b/src/kv/kv_types.h index bad9a8ef59d8..578d8ff74831 100644 --- a/src/kv/kv_types.h +++ b/src/kv/kv_types.h @@ -258,8 +258,9 @@ namespace kv const std::vector& request, uint8_t frame_format) = 0; virtual void append(const std::vector& replicated) = 0; - virtual void rollback(Version v) = 0; + virtual void rollback(Version v, kv::Term) = 0; virtual void compact(Version v) = 0; + virtual void set_term(kv::Term) = 0; }; class Consensus : public ConfigurableConsensus diff --git a/src/kv/store.h b/src/kv/store.h index 722ca86ca2a9..cab4481b77af 100644 --- a/src/kv/store.h +++ b/src/kv/store.h @@ -556,7 +556,7 @@ namespace kv pending_txs.clear(); auto h = get_history(); if (h) - h->rollback(v); + h->rollback(v, term); auto e = get_encryptor(); if (e) e->rollback(v); @@ -599,6 +599,9 @@ namespace kv { std::lock_guard vguard(version_lock); term = t; + auto h = get_history(); + if (h) + h->set_term(term); } DeserialiseSuccess deserialise_views( diff --git a/src/node/history.h b/src/node/history.h index 606527330471..fa92e0180456 100644 --- a/src/node/history.h +++ b/src/node/history.h @@ -132,7 +132,9 @@ namespace ccf return true; } - void rollback(kv::Version) override {} + void set_term(kv::Term) override {} + + void rollback(kv::Version, kv::Term) override {} void compact(kv::Version) override {} @@ -459,6 +461,9 @@ namespace ccf size_t sig_tx_interval; size_t sig_ms_interval; + SpinLock term_lock; + kv::Term term = 0; + public: HashedTxHistory( kv::Store& store_, @@ -657,8 +662,15 @@ namespace ccf return true; } - void rollback(kv::Version v) override + void set_term(kv::Term t) override + { + std::lock_guard tguard(term_lock); + term = t; + } + + void rollback(kv::Version v, kv::Term t) override { + set_term(t); replicated_state_tree.retract(v); log_hash(replicated_state_tree.get_root(), ROLLBACK); } @@ -770,6 +782,8 @@ namespace ccf void append(const std::vector& replicated) override { + std::lock_guard tguard(term_lock); + LOG_INFO_FMT("Term is {}", term); crypto::Sha256Hash rh({replicated.data(), replicated.size()}); log_hash(rh, APPEND); replicated_state_tree.append(rh); From faef95484da7947420b4e8b999495a06d4f565e2 Mon Sep 17 00:00:00 2001 From: Amaury Chamayou Date: Mon, 25 Jan 2021 19:12:32 +0000 Subject: [PATCH 2/4] . --- src/consensus/aft/raft.h | 3 ++- src/kv/store.h | 23 +++++++++++++---------- src/node/history.h | 2 -- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/consensus/aft/raft.h b/src/consensus/aft/raft.h index fbbb037e71e9..6e13d5b0c4cd 100644 --- a/src/consensus/aft/raft.h +++ b/src/consensus/aft/raft.h @@ -1857,7 +1857,8 @@ namespace aft void become_leader() { election_index = last_committable_index(); - LOG_DEBUG_FMT("Election index is {}", election_index); + LOG_DEBUG_FMT( + "Election index is {} in term", election_index, state->current_view); // Discard any un-committable updates we may hold, // since we have no signature for them. Except at startup, // where we do not want to roll back the genesis transaction. diff --git a/src/kv/store.h b/src/kv/store.h index cab4481b77af..a65aba55759f 100644 --- a/src/kv/store.h +++ b/src/kv/store.h @@ -534,13 +534,6 @@ namespace kv { std::lock_guard vguard(version_lock); - // The term should always be updated on rollback() when passed - // regardless of whether version needs to be updated or not - if (t.has_value()) - term = t.value(); - if (v >= version) - return; - if (v < compacted) { throw std::logic_error(fmt::format( @@ -549,14 +542,24 @@ namespace kv compacted)); } + // The term should always be updated on rollback() when passed + // regardless of whether version needs to be updated or not + if (t.has_value()) + term = t.value(); + // History must be informed of the term change, even if no + // actual rollback is required + auto h = get_history(); + if (h) + h->rollback(v, term); + + if (v >= version) + return; + version = v; last_replicated = v; last_committable = v; rollback_count++; pending_txs.clear(); - auto h = get_history(); - if (h) - h->rollback(v, term); auto e = get_encryptor(); if (e) e->rollback(v); diff --git a/src/node/history.h b/src/node/history.h index fa92e0180456..479bb4892c72 100644 --- a/src/node/history.h +++ b/src/node/history.h @@ -782,8 +782,6 @@ namespace ccf void append(const std::vector& replicated) override { - std::lock_guard tguard(term_lock); - LOG_INFO_FMT("Term is {}", term); crypto::Sha256Hash rh({replicated.data(), replicated.size()}); log_hash(rh, APPEND); replicated_state_tree.append(rh); From 7a7af26d69893c4e1ebafa564392aeeb958e6adb Mon Sep 17 00:00:00 2001 From: Amaury Chamayou Date: Tue, 26 Jan 2021 08:46:18 +0000 Subject: [PATCH 3/4] . --- src/consensus/aft/raft.h | 2 +- src/kv/store.h | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/consensus/aft/raft.h b/src/consensus/aft/raft.h index 6e13d5b0c4cd..5a505c20fa6a 100644 --- a/src/consensus/aft/raft.h +++ b/src/consensus/aft/raft.h @@ -1858,7 +1858,7 @@ namespace aft { election_index = last_committable_index(); LOG_DEBUG_FMT( - "Election index is {} in term", election_index, state->current_view); + "Election index is {} in term {}", election_index, state->current_view); // Discard any un-committable updates we may hold, // since we have no signature for them. Except at startup, // where we do not want to roll back the genesis transaction. diff --git a/src/kv/store.h b/src/kv/store.h index a65aba55759f..4cb2b7fe75b4 100644 --- a/src/kv/store.h +++ b/src/kv/store.h @@ -545,15 +545,21 @@ namespace kv // The term should always be updated on rollback() when passed // regardless of whether version needs to be updated or not if (t.has_value()) + { term = t.value(); + } // History must be informed of the term change, even if no // actual rollback is required auto h = get_history(); if (h) + { h->rollback(v, term); + } if (v >= version) + { return; + } version = v; last_replicated = v; @@ -562,7 +568,9 @@ namespace kv pending_txs.clear(); auto e = get_encryptor(); if (e) + { e->rollback(v); + } } for (auto& it : maps) From 96076ac4c57a315c91811da20ff7f0cea748ba7d Mon Sep 17 00:00:00 2001 From: Amaury Chamayou Date: Tue, 26 Jan 2021 10:08:45 +0000 Subject: [PATCH 4/4] Update src/kv/store.h Co-authored-by: Eddy Ashton --- src/kv/store.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/kv/store.h b/src/kv/store.h index 4cb2b7fe75b4..d6453e2e0b7a 100644 --- a/src/kv/store.h +++ b/src/kv/store.h @@ -612,7 +612,9 @@ namespace kv term = t; auto h = get_history(); if (h) + { h->set_term(term); + } } DeserialiseSuccess deserialise_views( @@ -1273,4 +1275,4 @@ namespace kv return ReservedTx(this, v); } }; -} \ No newline at end of file +}