wal_edit.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. // WAL related classes used in VersionEdit and VersionSet.
  6. // Modifications to WalAddition and WalDeletion may need to update
  7. // VersionEdit and its related tests.
  8. #pragma once
  9. #include <map>
  10. #include <ostream>
  11. #include <string>
  12. #include <unordered_map>
  13. #include <vector>
  14. #include "logging/event_logger.h"
  15. #include "port/port.h"
  16. #include "rocksdb/rocksdb_namespace.h"
  17. namespace ROCKSDB_NAMESPACE {
  18. class JSONWriter;
  19. class Slice;
  20. class Status;
  21. using WalNumber = uint64_t;
  22. // Metadata of a WAL.
  23. class WalMetadata {
  24. public:
  25. WalMetadata() = default;
  26. explicit WalMetadata(uint64_t synced_size_bytes)
  27. : synced_size_bytes_(synced_size_bytes) {}
  28. bool HasSyncedSize() const { return synced_size_bytes_ != kUnknownWalSize; }
  29. void SetSyncedSizeInBytes(uint64_t bytes) { synced_size_bytes_ = bytes; }
  30. uint64_t GetSyncedSizeInBytes() const { return synced_size_bytes_; }
  31. private:
  32. friend bool operator==(const WalMetadata& lhs, const WalMetadata& rhs);
  33. friend bool operator!=(const WalMetadata& lhs, const WalMetadata& rhs);
  34. // The size of WAL is unknown, used when the WAL is not synced yet or is
  35. // empty.
  36. constexpr static uint64_t kUnknownWalSize =
  37. std::numeric_limits<uint64_t>::max();
  38. // Size of the most recently synced WAL in bytes.
  39. uint64_t synced_size_bytes_ = kUnknownWalSize;
  40. };
  41. inline bool operator==(const WalMetadata& lhs, const WalMetadata& rhs) {
  42. return lhs.synced_size_bytes_ == rhs.synced_size_bytes_;
  43. }
  44. inline bool operator!=(const WalMetadata& lhs, const WalMetadata& rhs) {
  45. return !(lhs == rhs);
  46. }
  47. // These tags are persisted to MANIFEST, so it's part of the user API.
  48. enum class WalAdditionTag : uint32_t {
  49. // Indicates that there are no more tags.
  50. kTerminate = 1,
  51. // Synced Size in bytes.
  52. kSyncedSize = 2,
  53. // Add tags in the future, such as checksum?
  54. };
  55. // Records the event of adding a WAL in VersionEdit.
  56. class WalAddition {
  57. public:
  58. WalAddition() : number_(0), metadata_() {}
  59. explicit WalAddition(WalNumber number) : number_(number), metadata_() {}
  60. WalAddition(WalNumber number, WalMetadata meta)
  61. : number_(number), metadata_(std::move(meta)) {}
  62. WalNumber GetLogNumber() const { return number_; }
  63. const WalMetadata& GetMetadata() const { return metadata_; }
  64. void EncodeTo(std::string* dst) const;
  65. Status DecodeFrom(Slice* src);
  66. std::string DebugString() const;
  67. private:
  68. WalNumber number_;
  69. WalMetadata metadata_;
  70. };
  71. std::ostream& operator<<(std::ostream& os, const WalAddition& wal);
  72. JSONWriter& operator<<(JSONWriter& jw, const WalAddition& wal);
  73. using WalAdditions = std::vector<WalAddition>;
  74. // Records the event of deleting WALs before the specified log number.
  75. class WalDeletion {
  76. public:
  77. WalDeletion() : number_(kEmpty) {}
  78. explicit WalDeletion(WalNumber number) : number_(number) {}
  79. WalNumber GetLogNumber() const { return number_; }
  80. void EncodeTo(std::string* dst) const;
  81. Status DecodeFrom(Slice* src);
  82. std::string DebugString() const;
  83. bool IsEmpty() const { return number_ == kEmpty; }
  84. void Reset() { number_ = kEmpty; }
  85. private:
  86. static constexpr WalNumber kEmpty = 0;
  87. WalNumber number_;
  88. };
  89. std::ostream& operator<<(std::ostream& os, const WalDeletion& wal);
  90. JSONWriter& operator<<(JSONWriter& jw, const WalDeletion& wal);
  91. // Used in VersionSet to keep the current set of WALs.
  92. //
  93. // When a WAL is synced or becomes obsoleted,
  94. // a VersionEdit is logged to MANIFEST and
  95. // the WAL is added to or deleted from WalSet.
  96. //
  97. // Not thread safe, needs external synchronization such as holding DB mutex.
  98. class WalSet {
  99. public:
  100. // Add WAL(s).
  101. // If the WAL is closed,
  102. // then there must be an existing unclosed WAL,
  103. // otherwise, return Status::Corruption.
  104. // Can happen when applying a VersionEdit or recovering from MANIFEST.
  105. Status AddWal(const WalAddition& wal);
  106. Status AddWals(const WalAdditions& wals);
  107. // Delete WALs with log number smaller than the specified wal number.
  108. // Can happen when applying a VersionEdit or recovering from MANIFEST.
  109. Status DeleteWalsBefore(WalNumber wal);
  110. // Resets the internal state.
  111. void Reset();
  112. // WALs with number less than MinWalNumberToKeep should not exist in WalSet.
  113. WalNumber GetMinWalNumberToKeep() const { return min_wal_number_to_keep_; }
  114. const std::map<WalNumber, WalMetadata>& GetWals() const { return wals_; }
  115. // Checks whether there are missing or corrupted WALs.
  116. // Returns Status::OK if there is no missing nor corrupted WAL,
  117. // otherwise returns Status::Corruption.
  118. // logs_on_disk is a map from log number to the log filename.
  119. // Note that logs_on_disk may contain logs that is obsolete but
  120. // haven't been deleted from disk.
  121. Status CheckWals(
  122. Env* env,
  123. const std::unordered_map<WalNumber, std::string>& logs_on_disk) const;
  124. private:
  125. std::map<WalNumber, WalMetadata> wals_;
  126. // WAL number < min_wal_number_to_keep_ should not exist in wals_.
  127. // It's monotonically increasing, in-memory only, not written to MANIFEST.
  128. WalNumber min_wal_number_to_keep_ = 0;
  129. };
  130. } // namespace ROCKSDB_NAMESPACE