Simplify snapshot and WAL filenames

Reviewers: teon.banek, msantl

Reviewed By: msantl

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D1669
This commit is contained in:
Matej Ferencevic 2018-10-17 14:03:54 +02:00
parent 809e1779b1
commit 22824fd83c
2 changed files with 30 additions and 28 deletions

View File

@ -14,32 +14,34 @@ namespace durability {
namespace fs = std::experimental::filesystem; namespace fs = std::experimental::filesystem;
// This is the prefix used for WAL and Snapshot filenames. It is a timestamp
// format that equals to: YYYYmmddHHMMSSffffff
const std::string kTimestampFormat =
"{:04d}{:02d}{:02d}{:02d}{:02d}{:02d}{:06d}";
// TODO: This shouldn't be used to get the transaction ID from a WAL file,
// instead the file should be parsed and the transaction ID should be read from
// the file.
std::experimental::optional<tx::TransactionId> TransactionIdFromWalFilename( std::experimental::optional<tx::TransactionId> TransactionIdFromWalFilename(
const std::string &name) { const std::string &name) {
auto nullopt = std::experimental::nullopt; if (utils::EndsWith(name, "current"))
return std::numeric_limits<tx::TransactionId>::max();
// Get the max_transaction_id from the file name that has format // Get the max_transaction_id from the file name that has format
// "XXXXX__max_transaction_<MAX_TRANS_ID>" // "XXXXX_tx_<MAX_TRANS_ID>"
auto file_name_split = utils::RSplit(name, "__", 1); auto file_name_split = utils::RSplit(name, "_", 1);
if (file_name_split.size() != 2) { if (file_name_split.size() != 2) {
LOG(WARNING) << "Unable to parse WAL file name: " << name; LOG(WARNING) << "Unable to parse WAL file name: " << name;
return nullopt; return std::experimental::nullopt;
} }
if (utils::StartsWith(file_name_split[1], "current")) auto &tx_id_str = file_name_split[1];
return std::numeric_limits<tx::TransactionId>::max();
file_name_split = utils::Split(file_name_split[1], "_");
if (file_name_split.size() != 3) {
LOG(WARNING) << "Unable to parse WAL file name: " << name;
return nullopt;
}
auto &tx_id_str = file_name_split[2];
try { try {
return std::stoll(tx_id_str); return std::stoll(tx_id_str);
} catch (std::invalid_argument &) { } catch (std::invalid_argument &) {
LOG(WARNING) << "Unable to parse WAL file name tx ID: " << tx_id_str; LOG(WARNING) << "Unable to parse WAL file name tx ID: " << tx_id_str;
return nullopt; return std::experimental::nullopt;
} catch (std::out_of_range &) { } catch (std::out_of_range &) {
LOG(WARNING) << "WAL file name tx ID too large: " << tx_id_str; LOG(WARNING) << "WAL file name tx ID too large: " << tx_id_str;
return nullopt; return std::experimental::nullopt;
} }
} }
@ -49,42 +51,42 @@ std::experimental::optional<tx::TransactionId> TransactionIdFromWalFilename(
fs::path WalFilenameForTransactionId( fs::path WalFilenameForTransactionId(
const std::experimental::filesystem::path &wal_dir, const std::experimental::filesystem::path &wal_dir,
std::experimental::optional<tx::TransactionId> tx_id) { std::experimental::optional<tx::TransactionId> tx_id) {
auto file_name = utils::Timestamp::Now().ToIso8601(); auto file_name = utils::Timestamp::Now().ToString(kTimestampFormat);
if (tx_id) { if (tx_id) {
file_name += "__max_transaction_" + std::to_string(*tx_id); file_name += "_tx_" + std::to_string(*tx_id);
} else { } else {
file_name += "__current"; file_name += "_current";
} }
return wal_dir / file_name; return wal_dir / file_name;
} }
fs::path MakeSnapshotPath(const fs::path &durability_dir, fs::path MakeSnapshotPath(const fs::path &durability_dir,
tx::TransactionId tx_id) { tx::TransactionId tx_id) {
std::string date_str = std::string date_str = utils::Timestamp::Now().ToString(kTimestampFormat);
utils::Timestamp(utils::Timestamp::Now())
.ToString("{:04d}_{:02d}_{:02d}__{:02d}_{:02d}_{:02d}_{:05d}");
auto file_name = date_str + "_tx_" + std::to_string(tx_id); auto file_name = date_str + "_tx_" + std::to_string(tx_id);
return durability_dir / kSnapshotDir / file_name; return durability_dir / kSnapshotDir / file_name;
} }
// TODO: This shouldn't be used to get the transaction ID from a snapshot file,
// instead the file should be parsed and the transaction ID should be read from
// the file.
std::experimental::optional<tx::TransactionId> std::experimental::optional<tx::TransactionId>
TransactionIdFromSnapshotFilename(const std::string &name) { TransactionIdFromSnapshotFilename(const std::string &name) {
auto nullopt = std::experimental::nullopt;
auto file_name_split = utils::RSplit(name, "_tx_", 1); auto file_name_split = utils::RSplit(name, "_tx_", 1);
if (file_name_split.size() != 2) { if (file_name_split.size() != 2) {
LOG(WARNING) << "Unable to parse snapshot file name: " << name; LOG(WARNING) << "Unable to parse snapshot file name: " << name;
return nullopt; return std::experimental::nullopt;
} }
try { try {
return std::stoll(file_name_split[1]); return std::stoll(file_name_split[1]);
} catch (std::invalid_argument &) { } catch (std::invalid_argument &) {
LOG(WARNING) << "Unable to parse snapshot file name tx ID: " LOG(WARNING) << "Unable to parse snapshot file name tx ID: "
<< file_name_split[1]; << file_name_split[1];
return nullopt; return std::experimental::nullopt;
} catch (std::out_of_range &) { } catch (std::out_of_range &) {
LOG(WARNING) << "Unable to parse snapshot file name tx ID: " LOG(WARNING) << "Unable to parse snapshot file name tx ID: "
<< file_name_split[1]; << file_name_split[1];
return nullopt; return std::experimental::nullopt;
} }
} }
} // namespace durability } // namespace durability

View File

@ -58,16 +58,16 @@ class Timestamp : public TotalOrdering<Timestamp> {
long Sec() const { return time.tm_sec; } long Sec() const { return time.tm_sec; }
long Subsec() const { return nsec / 10000; } long Usec() const { return nsec / 1000; }
const std::string ToIso8601() const { const std::string ToIso8601() const {
return fmt::format(fiso8601, Year(), Month(), Day(), Hour(), Min(), Sec(), return fmt::format(fiso8601, Year(), Month(), Day(), Hour(), Min(), Sec(),
Subsec()); Usec());
} }
const std::string ToString(const std::string &format = fiso8601) const { const std::string ToString(const std::string &format = fiso8601) const {
return fmt::format(format, Year(), Month(), Day(), Hour(), Min(), Sec(), return fmt::format(format, Year(), Month(), Day(), Hour(), Min(), Sec(),
Subsec()); Usec());
} }
friend std::ostream &operator<<(std::ostream &stream, const Timestamp &ts) { friend std::ostream &operator<<(std::ostream &stream, const Timestamp &ts) {
@ -93,7 +93,7 @@ class Timestamp : public TotalOrdering<Timestamp> {
long nsec; long nsec;
static constexpr auto fiso8601 = static constexpr auto fiso8601 =
"{:04d}-{:02d}-{:02d}T{:02d}:{:02d}:{:02d}.{:05d}Z"; "{:04d}-{:02d}-{:02d}T{:02d}:{:02d}:{:02d}.{:06d}Z";
}; };
} // namespace utils } // namespace utils