blob_file.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. #pragma once
  6. #include <atomic>
  7. #include <limits>
  8. #include <memory>
  9. #include <unordered_set>
  10. #include "db/blob/blob_log_format.h"
  11. #include "db/blob/blob_log_writer.h"
  12. #include "file/random_access_file_reader.h"
  13. #include "port/port.h"
  14. #include "rocksdb/env.h"
  15. #include "rocksdb/file_system.h"
  16. #include "rocksdb/options.h"
  17. namespace ROCKSDB_NAMESPACE {
  18. namespace blob_db {
  19. class BlobDBImpl;
  20. class BlobFile {
  21. friend class BlobDBImpl;
  22. friend struct BlobFileComparator;
  23. friend struct BlobFileComparatorTTL;
  24. friend class BlobIndexCompactionFilterBase;
  25. friend class BlobIndexCompactionFilterGC;
  26. private:
  27. // access to parent
  28. const BlobDBImpl* parent_{nullptr};
  29. // path to blob directory
  30. std::string path_to_dir_;
  31. // the id of the file.
  32. // the above 2 are created during file creation and never changed
  33. // after that
  34. uint64_t file_number_{0};
  35. // The file numbers of the SST files whose oldest blob file reference
  36. // points to this blob file.
  37. std::unordered_set<uint64_t> linked_sst_files_;
  38. // Info log.
  39. Logger* info_log_{nullptr};
  40. // Column family id.
  41. uint32_t column_family_id_{std::numeric_limits<uint32_t>::max()};
  42. // Compression type of blobs in the file
  43. CompressionType compression_{kNoCompression};
  44. // If true, the keys in this file all has TTL. Otherwise all keys don't
  45. // have TTL.
  46. bool has_ttl_{false};
  47. // TTL range of blobs in the file.
  48. ExpirationRange expiration_range_;
  49. // number of blobs in the file
  50. std::atomic<uint64_t> blob_count_{0};
  51. // size of the file
  52. std::atomic<uint64_t> file_size_{0};
  53. BlobLogHeader header_;
  54. // closed_ = true implies the file is no more mutable
  55. // no more blobs will be appended and the footer has been written out
  56. std::atomic<bool> closed_{false};
  57. // The latest sequence number when the file was closed/made immutable.
  58. SequenceNumber immutable_sequence_{0};
  59. // Whether the file was marked obsolete (due to either TTL or GC).
  60. // obsolete_ still needs to do iterator/snapshot checks
  61. std::atomic<bool> obsolete_{false};
  62. // The last sequence number by the time the file marked as obsolete.
  63. // Data in this file is visible to a snapshot taken before the sequence.
  64. SequenceNumber obsolete_sequence_{0};
  65. // Sequential/Append writer for blobs
  66. std::shared_ptr<BlobLogWriter> log_writer_;
  67. // random access file reader for GET calls
  68. std::shared_ptr<RandomAccessFileReader> ra_file_reader_;
  69. // This Read-Write mutex is per file specific and protects
  70. // all the datastructures
  71. mutable port::RWMutex mutex_;
  72. // time when the random access reader was last created.
  73. std::atomic<std::int64_t> last_access_{-1};
  74. bool header_valid_{false};
  75. bool footer_valid_{false};
  76. public:
  77. BlobFile() = default;
  78. BlobFile(const BlobDBImpl* parent, const std::string& bdir, uint64_t fnum,
  79. Logger* info_log);
  80. BlobFile(const BlobDBImpl* parent, const std::string& bdir, uint64_t fnum,
  81. Logger* info_log, uint32_t column_family_id,
  82. CompressionType compression, bool has_ttl,
  83. const ExpirationRange& expiration_range);
  84. ~BlobFile();
  85. uint32_t GetColumnFamilyId() const;
  86. // Returns log file's absolute pathname.
  87. std::string PathName() const;
  88. // Primary identifier for blob file.
  89. // once the file is created, this never changes
  90. uint64_t BlobFileNumber() const { return file_number_; }
  91. // Get the set of SST files whose oldest blob file reference points to
  92. // this file.
  93. const std::unordered_set<uint64_t>& GetLinkedSstFiles() const {
  94. return linked_sst_files_;
  95. }
  96. // Link an SST file whose oldest blob file reference points to this file.
  97. void LinkSstFile(uint64_t sst_file_number) {
  98. assert(linked_sst_files_.find(sst_file_number) == linked_sst_files_.end());
  99. linked_sst_files_.insert(sst_file_number);
  100. }
  101. // Unlink an SST file whose oldest blob file reference points to this file.
  102. void UnlinkSstFile(uint64_t sst_file_number) {
  103. auto it = linked_sst_files_.find(sst_file_number);
  104. assert(it != linked_sst_files_.end());
  105. linked_sst_files_.erase(it);
  106. }
  107. // the following functions are atomic, and don't need
  108. // read lock
  109. uint64_t BlobCount() const {
  110. return blob_count_.load(std::memory_order_acquire);
  111. }
  112. std::string DumpState() const;
  113. // if the file is not taking any more appends.
  114. bool Immutable() const { return closed_.load(); }
  115. // Mark the file as immutable.
  116. // REQUIRES: write lock held, or access from single thread (on DB open).
  117. void MarkImmutable(SequenceNumber sequence) {
  118. closed_ = true;
  119. immutable_sequence_ = sequence;
  120. }
  121. SequenceNumber GetImmutableSequence() const {
  122. assert(Immutable());
  123. return immutable_sequence_;
  124. }
  125. // Whether the file was marked obsolete (due to either TTL or GC).
  126. bool Obsolete() const {
  127. assert(Immutable() || !obsolete_.load());
  128. return obsolete_.load();
  129. }
  130. // Mark file as obsolete (due to either TTL or GC). The file is not visible to
  131. // snapshots with sequence greater or equal to the given sequence.
  132. void MarkObsolete(SequenceNumber sequence);
  133. SequenceNumber GetObsoleteSequence() const {
  134. assert(Obsolete());
  135. return obsolete_sequence_;
  136. }
  137. Status Fsync(const WriteOptions& write_options);
  138. uint64_t GetFileSize() const {
  139. return file_size_.load(std::memory_order_acquire);
  140. }
  141. // All Get functions which are not atomic, will need ReadLock on the mutex
  142. const ExpirationRange& GetExpirationRange() const {
  143. return expiration_range_;
  144. }
  145. void ExtendExpirationRange(uint64_t expiration) {
  146. expiration_range_.first = std::min(expiration_range_.first, expiration);
  147. expiration_range_.second = std::max(expiration_range_.second, expiration);
  148. }
  149. bool HasTTL() const { return has_ttl_; }
  150. void SetHasTTL(bool has_ttl) { has_ttl_ = has_ttl; }
  151. CompressionType GetCompressionType() const { return compression_; }
  152. std::shared_ptr<BlobLogWriter> GetWriter() const { return log_writer_; }
  153. // Read blob file header and footer. Return corruption if file header is
  154. // malform or incomplete. If footer is malform or incomplete, set
  155. // footer_valid_ to false and return Status::OK.
  156. Status ReadMetadata(const std::shared_ptr<FileSystem>& fs,
  157. const FileOptions& file_options);
  158. Status GetReader(Env* env, const FileOptions& file_options,
  159. std::shared_ptr<RandomAccessFileReader>* reader,
  160. bool* fresh_open);
  161. private:
  162. Status ReadFooter(BlobLogFooter* footer);
  163. Status WriteFooterAndCloseLocked(const WriteOptions& write_options,
  164. SequenceNumber sequence);
  165. void CloseRandomAccessLocked();
  166. // this is used, when you are reading only the footer of a
  167. // previously closed file
  168. Status SetFromFooterLocked(const BlobLogFooter& footer);
  169. void set_expiration_range(const ExpirationRange& expiration_range) {
  170. expiration_range_ = expiration_range;
  171. }
  172. // The following functions are atomic, and don't need locks
  173. void SetFileSize(uint64_t fs) { file_size_ = fs; }
  174. void SetBlobCount(uint64_t bc) { blob_count_ = bc; }
  175. void BlobRecordAdded(uint64_t record_size) {
  176. ++blob_count_;
  177. file_size_ += record_size;
  178. }
  179. };
  180. } // namespace blob_db
  181. } // namespace ROCKSDB_NAMESPACE