wal_manager.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 <atomic>
  11. #include <deque>
  12. #include <limits>
  13. #include <memory>
  14. #include <set>
  15. #include <string>
  16. #include <utility>
  17. #include <vector>
  18. #include "db/version_set.h"
  19. #include "file/file_util.h"
  20. #include "options/db_options.h"
  21. #include "port/port.h"
  22. #include "rocksdb/env.h"
  23. #include "rocksdb/status.h"
  24. #include "rocksdb/transaction_log.h"
  25. #include "rocksdb/types.h"
  26. #include "util/atomic.h"
  27. namespace ROCKSDB_NAMESPACE {
  28. // WAL manager provides the abstraction for reading the WAL files as a single
  29. // unit. Internally, it opens and reads the files using Reader or Writer
  30. // abstraction.
  31. class WalManager {
  32. public:
  33. WalManager(const ImmutableDBOptions& db_options,
  34. const FileOptions& file_options,
  35. const std::shared_ptr<IOTracer>& io_tracer,
  36. const bool seq_per_batch = false)
  37. : db_options_(db_options),
  38. file_options_(file_options),
  39. env_(db_options.env),
  40. fs_(db_options.fs, io_tracer),
  41. purge_wal_files_last_run_(0),
  42. seq_per_batch_(seq_per_batch),
  43. wal_dir_(db_options_.GetWalDir()),
  44. wal_in_db_path_(db_options_.IsWalDirSameAsDBPath()),
  45. io_tracer_(io_tracer) {}
  46. Status GetSortedWalFiles(VectorWalPtr& files, bool need_seqnos = true,
  47. bool include_archived = true);
  48. // Allow user to tail transaction log to find all recent changes to the
  49. // database that are newer than `seq_number`.
  50. Status GetUpdatesSince(
  51. SequenceNumber seq_number, std::unique_ptr<TransactionLogIterator>* iter,
  52. const TransactionLogIterator::ReadOptions& read_options,
  53. VersionSet* version_set);
  54. void PurgeObsoleteWALFiles();
  55. void ArchiveWALFile(const std::string& fname, uint64_t number);
  56. Status DeleteFile(const std::string& fname, uint64_t number);
  57. Status GetLiveWalFile(uint64_t number, std::unique_ptr<WalFile>* log_file);
  58. Status TEST_ReadFirstRecord(const WalFileType type, const uint64_t number,
  59. SequenceNumber* sequence) {
  60. return ReadFirstRecord(type, number, sequence);
  61. }
  62. Status TEST_ReadFirstLine(const std::string& fname, const uint64_t number,
  63. SequenceNumber* sequence) {
  64. return ReadFirstLine(fname, number, sequence);
  65. }
  66. private:
  67. Status GetSortedWalsOfType(const std::string& path, VectorWalPtr& log_files,
  68. WalFileType type, bool need_seqnos);
  69. // Requires: all_logs should be sorted with earliest log file first
  70. // Retains all log files in all_logs which contain updates with seq no.
  71. // Greater Than or Equal to the requested SequenceNumber.
  72. Status RetainProbableWalFiles(VectorWalPtr& all_logs,
  73. const SequenceNumber target);
  74. // ReadFirstRecord checks the read_first_record_cache_ to see if the entry
  75. // exists or not. If not, it will read the WAL file.
  76. // In case of wal_compression, WAL contains a `kSetCompressionType` record
  77. // which is not associated with any sequence number. So the sequence_number is
  78. // set to 1 if that WAL doesn't include any other record (basically empty) in
  79. // order to include that WAL and is inserted in read_first_record_cache_.
  80. // Therefore, sequence_number is used as boolean if WAL should be included or
  81. // not and that sequence_number shouldn't be use for any other purpose.
  82. Status ReadFirstRecord(const WalFileType type, const uint64_t number,
  83. SequenceNumber* sequence);
  84. // In case of no wal_compression, ReadFirstLine returns status.ok() and
  85. // sequence == 0 if the file exists, but is empty.
  86. // In case of wal_compression, WAL contains
  87. // `kSetCompressionType` record which is not associated with any sequence
  88. // number if that WAL doesn't include any other record (basically empty). As
  89. // result for an empty file, GetSortedWalsOfType() will skip these WALs
  90. // causing the operations to fail. To avoid that, it sets sequence_number to
  91. // 1 inorder to include that WAL.
  92. Status ReadFirstLine(const std::string& fname, const uint64_t number,
  93. SequenceNumber* sequence);
  94. // ------- state from DBImpl ------
  95. const ImmutableDBOptions& db_options_;
  96. const FileOptions file_options_;
  97. Env* env_;
  98. const FileSystemPtr fs_;
  99. // ------- WalManager state -------
  100. // cache for ReadFirstRecord() calls
  101. std::unordered_map<uint64_t, SequenceNumber> read_first_record_cache_;
  102. port::Mutex read_first_record_cache_mutex_;
  103. // last time when PurgeObsoleteWALFiles ran.
  104. RelaxedAtomic<uint64_t> purge_wal_files_last_run_;
  105. bool seq_per_batch_;
  106. const std::string& wal_dir_;
  107. bool wal_in_db_path_;
  108. // obsolete files will be deleted every this seconds if ttl deletion is
  109. // enabled and archive size_limit is disabled.
  110. static constexpr uint64_t kDefaultIntervalToDeleteObsoleteWAL = 600;
  111. std::shared_ptr<IOTracer> io_tracer_;
  112. };
  113. } // namespace ROCKSDB_NAMESPACE