forked from facebook/rocksdb
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[sys-6995] maintain epochs of first manifest update after persist rep…
…lication sequence
- Loading branch information
Showing
16 changed files
with
321 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#include "db/replication_epoch_edit.h" | ||
#include <algorithm> | ||
#include <optional> | ||
#include <sstream> | ||
|
||
#include "rocksdb/slice.h" | ||
#include "rocksdb/status.h" | ||
#include "util/coding.h" | ||
|
||
namespace ROCKSDB_NAMESPACE { | ||
|
||
ReplicationEpochAddition::ReplicationEpochAddition(uint64_t epoch, | ||
uint64_t first_mus) | ||
: epoch_(epoch), first_mus_(first_mus) {} | ||
|
||
void ReplicationEpochAddition::EncodeTo(std::string* output) const { | ||
PutVarint64(output, epoch_); | ||
PutVarint64(output, first_mus_); | ||
} | ||
|
||
Status ReplicationEpochAddition::DecodeFrom(Slice* input) { | ||
if (!GetVarint64(input, &epoch_)) { | ||
return Status::Corruption("ReplicationEpochAddition", "Error decoding epoch"); | ||
} | ||
if (!GetVarint64(input, &first_mus_)) { | ||
return Status::Corruption("ReplicationEpochAddition", "Error decoding manifest update sequence"); | ||
} | ||
return Status::OK(); | ||
} | ||
|
||
std::string ReplicationEpochAddition::DebugString() const { | ||
std::ostringstream oss; | ||
oss << *this; | ||
return oss.str(); | ||
} | ||
|
||
std::ostream& operator<<(std::ostream& os, const ReplicationEpochAddition& ea) { | ||
os << "epoch: " << ea.GetEpoch() << ", mus: " << ea.GetFirstMUS(); | ||
return os; | ||
} | ||
|
||
Status ReplicationEpochSet::AddEpoch(const ReplicationEpochAddition& epoch) { | ||
if (!epochs_.empty() && epochs_.back() >= epoch) { | ||
std::stringstream ss; | ||
ss << "Misordered replication epoch. prev: " << epochs_.back() | ||
<< ", next: " << epoch; | ||
return Status::Corruption(ss.str()); | ||
} | ||
epochs_.push_back(epoch); | ||
return Status::OK(); | ||
} | ||
|
||
Status ReplicationEpochSet::AddEpochs(const ReplicationEpochAdditions& epochs) { | ||
for (auto& epoch: epochs) { | ||
auto s = AddEpoch(epoch); | ||
if (!s.ok()) { | ||
return s; | ||
} | ||
} | ||
return Status::OK(); | ||
} | ||
|
||
void ReplicationEpochSet::DeleteEpochsBefore( | ||
uint64_t epoch, uint32_t max_num_replication_epochs) { | ||
while (!epochs_.empty() && (epochs_.front().GetEpoch() < epoch || | ||
epochs_.size() > max_num_replication_epochs)) { | ||
epochs_.pop_front(); | ||
} | ||
} | ||
|
||
std::optional<uint64_t> ReplicationEpochSet::GetEpochForMUS(uint64_t mus) { | ||
if (empty() || epochs_.front().GetFirstMUS() > mus || epochs_.back().GetFirstMUS() < mus) { | ||
return std::nullopt; | ||
} | ||
|
||
auto it = std::upper_bound(epochs_.begin(), epochs_.end(), mus, | ||
[&](auto mus, const auto& epoch_addition) { | ||
return mus < epoch_addition.GetFirstMUS(); | ||
}); | ||
assert(it != epochs_.begin()); | ||
--it; | ||
return it->GetEpoch(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#pragma once | ||
|
||
#include <cstdint> | ||
#include <deque> | ||
#include <optional> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include "rocksdb/rocksdb_namespace.h" | ||
|
||
namespace ROCKSDB_NAMESPACE { | ||
|
||
class Slice; | ||
class Status; | ||
|
||
// Replication epoch and first manifest update sequence in that epoch | ||
class ReplicationEpochAddition { | ||
public: | ||
ReplicationEpochAddition() = default; | ||
ReplicationEpochAddition(uint64_t epoch, uint64_t first_mus); | ||
|
||
uint64_t GetEpoch() const { return epoch_; } | ||
uint64_t GetFirstMUS() const { return first_mus_; } | ||
|
||
void EncodeTo(std::string* output) const; | ||
Status DecodeFrom(Slice* input); | ||
|
||
std::string DebugString() const; | ||
private: | ||
// The replication epoch | ||
uint64_t epoch_{0}; | ||
// First manifest update sequence of the replication epoch | ||
uint64_t first_mus_{0}; | ||
}; | ||
|
||
inline bool operator==(const ReplicationEpochAddition& ea1, const ReplicationEpochAddition& ea2) { | ||
return ea1.GetEpoch() == ea2.GetEpoch() && ea1.GetFirstMUS() == ea2.GetFirstMUS(); | ||
} | ||
inline bool operator<(const ReplicationEpochAddition& ea1, const ReplicationEpochAddition& ea2) { | ||
return (ea1.GetEpoch() < ea2.GetEpoch() && | ||
ea1.GetFirstMUS() < ea2.GetFirstMUS()) || | ||
(ea1.GetEpoch() == ea2.GetEpoch() && | ||
ea1.GetFirstMUS() < ea2.GetFirstMUS()); | ||
} | ||
inline bool operator>=(const ReplicationEpochAddition& ea1, | ||
const ReplicationEpochAddition ea2) { | ||
return !(ea1 < ea2); | ||
} | ||
std::ostream& operator<<(std::ostream& os, const ReplicationEpochAddition& ea); | ||
|
||
using ReplicationEpochAdditions = std::vector<ReplicationEpochAddition>; | ||
|
||
class ReplicationEpochSet { | ||
public: | ||
Status AddEpoch(const ReplicationEpochAddition& epoch); | ||
Status AddEpochs(const ReplicationEpochAdditions& epochs); | ||
|
||
void DeleteEpochsBefore(uint64_t epoch, uint32_t max_num_replication_epochs); | ||
|
||
// Find corresponding epoch for manifest update sequence. | ||
// Return std::nullopt if `mus` is not coverred by the | ||
// `ReplicationEpochSet`. | ||
std::optional<uint64_t> GetEpochForMUS(uint64_t mus); | ||
|
||
const auto& GetEpochs() const { | ||
return epochs_; | ||
} | ||
|
||
bool empty() const { return epochs_.empty(); } | ||
|
||
private: | ||
std::deque<ReplicationEpochAddition> epochs_; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.