wal_manager.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 <set>
  14. #include <utility>
  15. #include <vector>
  16. #include <string>
  17. #include <memory>
  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. namespace ROCKSDB_NAMESPACE {
  27. #ifndef ROCKSDB_LITE
  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, const bool seq_per_batch = false)
  35. : db_options_(db_options),
  36. file_options_(file_options),
  37. env_(db_options.env),
  38. fs_(db_options.fs.get()),
  39. purge_wal_files_last_run_(0),
  40. seq_per_batch_(seq_per_batch),
  41. wal_in_db_path_(IsWalDirSameAsDBPath(&db_options)) {}
  42. Status GetSortedWalFiles(VectorLogPtr& files);
  43. // Allow user to tail transaction log to find all recent changes to the
  44. // database that are newer than `seq_number`.
  45. Status GetUpdatesSince(
  46. SequenceNumber seq_number, std::unique_ptr<TransactionLogIterator>* iter,
  47. const TransactionLogIterator::ReadOptions& read_options,
  48. VersionSet* version_set);
  49. void PurgeObsoleteWALFiles();
  50. void ArchiveWALFile(const std::string& fname, uint64_t number);
  51. Status DeleteFile(const std::string& fname, uint64_t number);
  52. Status GetLiveWalFile(uint64_t number, std::unique_ptr<LogFile>* log_file);
  53. Status TEST_ReadFirstRecord(const WalFileType type, const uint64_t number,
  54. SequenceNumber* sequence) {
  55. return ReadFirstRecord(type, number, sequence);
  56. }
  57. Status TEST_ReadFirstLine(const std::string& fname, const uint64_t number,
  58. SequenceNumber* sequence) {
  59. return ReadFirstLine(fname, number, sequence);
  60. }
  61. private:
  62. Status GetSortedWalsOfType(const std::string& path, VectorLogPtr& log_files,
  63. WalFileType type);
  64. // Requires: all_logs should be sorted with earliest log file first
  65. // Retains all log files in all_logs which contain updates with seq no.
  66. // Greater Than or Equal to the requested SequenceNumber.
  67. Status RetainProbableWalFiles(VectorLogPtr& all_logs,
  68. const SequenceNumber target);
  69. Status ReadFirstRecord(const WalFileType type, const uint64_t number,
  70. SequenceNumber* sequence);
  71. Status ReadFirstLine(const std::string& fname, const uint64_t number,
  72. SequenceNumber* sequence);
  73. // ------- state from DBImpl ------
  74. const ImmutableDBOptions& db_options_;
  75. const FileOptions file_options_;
  76. Env* env_;
  77. FileSystem* fs_;
  78. // ------- WalManager state -------
  79. // cache for ReadFirstRecord() calls
  80. std::unordered_map<uint64_t, SequenceNumber> read_first_record_cache_;
  81. port::Mutex read_first_record_cache_mutex_;
  82. // last time when PurgeObsoleteWALFiles ran.
  83. uint64_t purge_wal_files_last_run_;
  84. bool seq_per_batch_;
  85. bool wal_in_db_path_;
  86. // obsolete files will be deleted every this seconds if ttl deletion is
  87. // enabled and archive size_limit is disabled.
  88. static const uint64_t kDefaultIntervalToDeleteObsoleteWAL = 600;
  89. };
  90. #endif // ROCKSDB_LITE
  91. } // namespace ROCKSDB_NAMESPACE