diff --git a/raft/log.cc b/raft/log.cc index d453fc79a4..7946c1262b 100644 --- a/raft/log.cc +++ b/raft/log.cc @@ -61,7 +61,7 @@ index_t log::next_idx() const { return last_idx() + index_t(1); } -void log::truncate_head(index_t idx) { +void log::truncate_uncommitted(index_t idx) { assert(idx >= _first_idx); auto it = _log.begin() + (idx - _first_idx); _log.erase(it, _log.end()); @@ -76,35 +76,6 @@ void log::truncate_head(index_t idx) { } } -size_t log::truncate_tail(index_t idx, size_t trailing) { - - size_t removed; - if (idx > last_idx()) { - // Remove all entries ignoring the 'trailing' argument, - // since otherwise there would be a gap between old - // entries and the next entry index. - removed = _log.size(); - _log.clear(); - _first_idx = idx + index_t{1}; - } else { - removed = _log.size() - (last_idx() - idx); - removed -= std::min(trailing, removed); - _log.erase(_log.begin(), _log.begin() + removed); - _first_idx = _first_idx + index_t{removed}; - } - - _stable_idx = std::max(idx, _stable_idx); - - if (_first_idx > _prev_conf_idx) { - _prev_conf_idx = index_t{0}; - if (_first_idx > _last_conf_idx) { - _last_conf_idx = index_t{0}; - } - } - - return removed; -} - void log::init_last_conf_idx() { for (auto it = _log.rbegin(); it != _log.rend(); ++it) { if (std::holds_alternative((**it).data)) { @@ -193,7 +164,7 @@ index_t log::maybe_append(std::vector&& entries) { // If an existing entry conflicts with a new one (same // index but different terms), delete the existing // entry and all that follow it (ยง5.3). - truncate_head(e->idx); + truncate_uncommitted(e->idx); } // Assert log monotonicity assert(e->idx == next_idx()); @@ -206,10 +177,35 @@ index_t log::maybe_append(std::vector&& entries) { size_t log::apply_snapshot(snapshot&& snp, size_t trailing) { assert (snp.idx >= _first_idx); - size_t ret = truncate_tail(snp.idx, trailing); + size_t removed; + auto idx = snp.idx; + + if (idx > last_idx()) { + // Remove all entries ignoring the 'trailing' argument, + // since otherwise there would be a gap between old + // entries and the next entry index. + removed = _log.size(); + _log.clear(); + _first_idx = idx + index_t{1}; + } else { + removed = _log.size() - (last_idx() - idx); + removed -= std::min(trailing, removed); + _log.erase(_log.begin(), _log.begin() + removed); + _first_idx = _first_idx + index_t{removed}; + } + + _stable_idx = std::max(idx, _stable_idx); + + if (_first_idx > _prev_conf_idx) { + _prev_conf_idx = index_t{0}; + if (_first_idx > _last_conf_idx) { + _last_conf_idx = index_t{0}; + } + } _snapshot = std::move(snp); - return ret; + + return removed; } std::ostream& operator<<(std::ostream& os, const log& l) { diff --git a/raft/log.hh b/raft/log.hh index a4cf336132..a96eaa871a 100644 --- a/raft/log.hh +++ b/raft/log.hh @@ -72,8 +72,7 @@ class log { index_t _prev_conf_idx = index_t{0}; private: // Drop uncommitted log entries not present on the leader. - void truncate_head(index_t i); - size_t truncate_tail(index_t idx, size_t trailing); + void truncate_uncommitted(index_t i); // A helper used to find the last configuration entry in the // log after it's been loaded from disk. void init_last_conf_idx();