snapshot_impl.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. //
  6. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  7. // Use of this source code is governed by a BSD-style license that can be
  8. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  9. #pragma once
  10. #include <vector>
  11. #include "db/dbformat.h"
  12. #include "rocksdb/db.h"
  13. #include "util/autovector.h"
  14. namespace ROCKSDB_NAMESPACE {
  15. class SnapshotList;
  16. // Snapshots are kept in a doubly-linked list in the DB.
  17. // Each SnapshotImpl corresponds to a particular sequence number.
  18. class SnapshotImpl : public Snapshot {
  19. public:
  20. SequenceNumber number_; // const after creation
  21. // It indicates the smallest uncommitted data at the time the snapshot was
  22. // taken. This is currently used by WritePrepared transactions to limit the
  23. // scope of queries to IsInSnapshot.
  24. SequenceNumber min_uncommitted_ = kMinUnCommittedSeq;
  25. SequenceNumber GetSequenceNumber() const override { return number_; }
  26. int64_t GetUnixTime() const override { return unix_time_; }
  27. uint64_t GetTimestamp() const override { return timestamp_; }
  28. private:
  29. friend class SnapshotList;
  30. // SnapshotImpl is kept in a doubly-linked circular list
  31. SnapshotImpl* prev_;
  32. SnapshotImpl* next_;
  33. SnapshotList* list_; // just for sanity checks
  34. int64_t unix_time_;
  35. uint64_t timestamp_;
  36. // Will this snapshot be used by a Transaction to do write-conflict checking?
  37. bool is_write_conflict_boundary_;
  38. };
  39. class SnapshotList {
  40. public:
  41. SnapshotList() {
  42. list_.prev_ = &list_;
  43. list_.next_ = &list_;
  44. list_.number_ = 0xFFFFFFFFL; // placeholder marker, for debugging
  45. // Set all the variables to make UBSAN happy.
  46. list_.list_ = nullptr;
  47. list_.unix_time_ = 0;
  48. list_.timestamp_ = 0;
  49. list_.is_write_conflict_boundary_ = false;
  50. count_ = 0;
  51. }
  52. // No copy-construct.
  53. SnapshotList(const SnapshotList&) = delete;
  54. bool empty() const {
  55. assert(list_.next_ != &list_ || 0 == count_);
  56. return list_.next_ == &list_;
  57. }
  58. SnapshotImpl* oldest() const {
  59. assert(!empty());
  60. return list_.next_;
  61. }
  62. SnapshotImpl* newest() const {
  63. assert(!empty());
  64. return list_.prev_;
  65. }
  66. SnapshotImpl* New(SnapshotImpl* s, SequenceNumber seq, uint64_t unix_time,
  67. bool is_write_conflict_boundary,
  68. uint64_t ts = std::numeric_limits<uint64_t>::max()) {
  69. s->number_ = seq;
  70. s->unix_time_ = unix_time;
  71. s->timestamp_ = ts;
  72. s->is_write_conflict_boundary_ = is_write_conflict_boundary;
  73. s->list_ = this;
  74. s->next_ = &list_;
  75. s->prev_ = list_.prev_;
  76. s->prev_->next_ = s;
  77. s->next_->prev_ = s;
  78. count_++;
  79. return s;
  80. }
  81. // Do not responsible to free the object.
  82. void Delete(const SnapshotImpl* s) {
  83. assert(s->list_ == this);
  84. s->prev_->next_ = s->next_;
  85. s->next_->prev_ = s->prev_;
  86. count_--;
  87. }
  88. // retrieve all snapshot numbers up until max_seq. They are sorted in
  89. // ascending order (with no duplicates).
  90. std::vector<SequenceNumber> GetAll(
  91. SequenceNumber* oldest_write_conflict_snapshot = nullptr,
  92. const SequenceNumber& max_seq = kMaxSequenceNumber) const {
  93. std::vector<SequenceNumber> ret;
  94. GetAll(&ret, oldest_write_conflict_snapshot, max_seq);
  95. return ret;
  96. }
  97. void GetAll(std::vector<SequenceNumber>* snap_vector,
  98. SequenceNumber* oldest_write_conflict_snapshot = nullptr,
  99. const SequenceNumber& max_seq = kMaxSequenceNumber) const {
  100. std::vector<SequenceNumber>& ret = *snap_vector;
  101. // So far we have no use case that would pass a non-empty vector
  102. assert(ret.size() == 0);
  103. if (oldest_write_conflict_snapshot != nullptr) {
  104. *oldest_write_conflict_snapshot = kMaxSequenceNumber;
  105. }
  106. if (empty()) {
  107. return;
  108. }
  109. const SnapshotImpl* s = &list_;
  110. while (s->next_ != &list_) {
  111. if (s->next_->number_ > max_seq) {
  112. break;
  113. }
  114. // Avoid duplicates
  115. if (ret.empty() || ret.back() != s->next_->number_) {
  116. ret.push_back(s->next_->number_);
  117. }
  118. if (oldest_write_conflict_snapshot != nullptr &&
  119. *oldest_write_conflict_snapshot == kMaxSequenceNumber &&
  120. s->next_->is_write_conflict_boundary_) {
  121. // If this is the first write-conflict boundary snapshot in the list,
  122. // it is the oldest
  123. *oldest_write_conflict_snapshot = s->next_->number_;
  124. }
  125. s = s->next_;
  126. }
  127. return;
  128. }
  129. // get the sequence number of the most recent snapshot
  130. SequenceNumber GetNewest() {
  131. if (empty()) {
  132. return 0;
  133. }
  134. return newest()->number_;
  135. }
  136. int64_t GetOldestSnapshotTime() const {
  137. if (empty()) {
  138. return 0;
  139. } else {
  140. return oldest()->unix_time_;
  141. }
  142. }
  143. int64_t GetOldestSnapshotSequence() const {
  144. if (empty()) {
  145. return 0;
  146. } else {
  147. return oldest()->GetSequenceNumber();
  148. }
  149. }
  150. uint64_t count() const { return count_; }
  151. private:
  152. // Dummy head of doubly-linked list of snapshots
  153. SnapshotImpl list_;
  154. uint64_t count_;
  155. };
  156. // All operations on TimestampedSnapshotList must be protected by db mutex.
  157. class TimestampedSnapshotList {
  158. public:
  159. explicit TimestampedSnapshotList() = default;
  160. std::shared_ptr<const SnapshotImpl> GetSnapshot(uint64_t ts) const {
  161. if (ts == std::numeric_limits<uint64_t>::max() && !snapshots_.empty()) {
  162. auto it = snapshots_.rbegin();
  163. assert(it != snapshots_.rend());
  164. return it->second;
  165. }
  166. auto it = snapshots_.find(ts);
  167. if (it == snapshots_.end()) {
  168. return std::shared_ptr<const SnapshotImpl>();
  169. }
  170. return it->second;
  171. }
  172. void GetSnapshots(
  173. uint64_t ts_lb, uint64_t ts_ub,
  174. std::vector<std::shared_ptr<const Snapshot>>& snapshots) const {
  175. assert(ts_lb < ts_ub);
  176. auto it_low = snapshots_.lower_bound(ts_lb);
  177. auto it_high = snapshots_.lower_bound(ts_ub);
  178. for (auto it = it_low; it != it_high; ++it) {
  179. snapshots.emplace_back(it->second);
  180. }
  181. }
  182. void AddSnapshot(const std::shared_ptr<const SnapshotImpl>& snapshot) {
  183. assert(snapshot);
  184. snapshots_.try_emplace(snapshot->GetTimestamp(), snapshot);
  185. }
  186. // snapshots_to_release: the container to where the timestamped snapshots will
  187. // be moved so that it retains the last reference to the snapshots and the
  188. // snapshots won't be actually released which requires db mutex. The
  189. // snapshots will be released by caller of ReleaseSnapshotsOlderThan().
  190. void ReleaseSnapshotsOlderThan(
  191. uint64_t ts,
  192. autovector<std::shared_ptr<const SnapshotImpl>>& snapshots_to_release) {
  193. auto ub = snapshots_.lower_bound(ts);
  194. for (auto it = snapshots_.begin(); it != ub; ++it) {
  195. snapshots_to_release.emplace_back(it->second);
  196. }
  197. snapshots_.erase(snapshots_.begin(), ub);
  198. }
  199. private:
  200. std::map<uint64_t, std::shared_ptr<const SnapshotImpl>> snapshots_;
  201. };
  202. } // namespace ROCKSDB_NAMESPACE