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 11 #include <atomic> 12 #include <deque> 13 #include <limits> 14 #include <set> 15 #include <utility> 16 #include <vector> 17 #include <string> 18 #include <memory> 19 20 #include "db/version_set.h" 21 #include "file/file_util.h" 22 #include "options/db_options.h" 23 #include "port/port.h" 24 #include "rocksdb/env.h" 25 #include "rocksdb/status.h" 26 #include "rocksdb/transaction_log.h" 27 #include "rocksdb/types.h" 28 29 namespace ROCKSDB_NAMESPACE { 30 31 #ifndef ROCKSDB_LITE 32 33 // WAL manager provides the abstraction for reading the WAL files as a single 34 // unit. Internally, it opens and reads the files using Reader or Writer 35 // abstraction. 36 class WalManager { 37 public: 38 WalManager(const ImmutableDBOptions& db_options, 39 const FileOptions& file_options, const bool seq_per_batch = false) db_options_(db_options)40 : db_options_(db_options), 41 file_options_(file_options), 42 env_(db_options.env), 43 fs_(db_options.fs.get()), 44 purge_wal_files_last_run_(0), 45 seq_per_batch_(seq_per_batch), 46 wal_in_db_path_(IsWalDirSameAsDBPath(&db_options)) {} 47 48 Status GetSortedWalFiles(VectorLogPtr& files); 49 50 // Allow user to tail transaction log to find all recent changes to the 51 // database that are newer than `seq_number`. 52 Status GetUpdatesSince( 53 SequenceNumber seq_number, std::unique_ptr<TransactionLogIterator>* iter, 54 const TransactionLogIterator::ReadOptions& read_options, 55 VersionSet* version_set); 56 57 void PurgeObsoleteWALFiles(); 58 59 void ArchiveWALFile(const std::string& fname, uint64_t number); 60 61 Status DeleteFile(const std::string& fname, uint64_t number); 62 63 Status GetLiveWalFile(uint64_t number, std::unique_ptr<LogFile>* log_file); 64 TEST_ReadFirstRecord(const WalFileType type,const uint64_t number,SequenceNumber * sequence)65 Status TEST_ReadFirstRecord(const WalFileType type, const uint64_t number, 66 SequenceNumber* sequence) { 67 return ReadFirstRecord(type, number, sequence); 68 } 69 TEST_ReadFirstLine(const std::string & fname,const uint64_t number,SequenceNumber * sequence)70 Status TEST_ReadFirstLine(const std::string& fname, const uint64_t number, 71 SequenceNumber* sequence) { 72 return ReadFirstLine(fname, number, sequence); 73 } 74 75 private: 76 Status GetSortedWalsOfType(const std::string& path, VectorLogPtr& log_files, 77 WalFileType type); 78 // Requires: all_logs should be sorted with earliest log file first 79 // Retains all log files in all_logs which contain updates with seq no. 80 // Greater Than or Equal to the requested SequenceNumber. 81 Status RetainProbableWalFiles(VectorLogPtr& all_logs, 82 const SequenceNumber target); 83 84 Status ReadFirstRecord(const WalFileType type, const uint64_t number, 85 SequenceNumber* sequence); 86 87 Status ReadFirstLine(const std::string& fname, const uint64_t number, 88 SequenceNumber* sequence); 89 90 // ------- state from DBImpl ------ 91 const ImmutableDBOptions& db_options_; 92 const FileOptions file_options_; 93 Env* env_; 94 FileSystem* fs_; 95 96 // ------- WalManager state ------- 97 // cache for ReadFirstRecord() calls 98 std::unordered_map<uint64_t, SequenceNumber> read_first_record_cache_; 99 port::Mutex read_first_record_cache_mutex_; 100 101 // last time when PurgeObsoleteWALFiles ran. 102 uint64_t purge_wal_files_last_run_; 103 104 bool seq_per_batch_; 105 106 bool wal_in_db_path_; 107 108 // obsolete files will be deleted every this seconds if ttl deletion is 109 // enabled and archive size_limit is disabled. 110 static const uint64_t kDefaultIntervalToDeleteObsoleteWAL = 600; 111 }; 112 113 #endif // ROCKSDB_LITE 114 } // namespace ROCKSDB_NAMESPACE 115