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).
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.
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"
29 namespace ROCKSDB_NAMESPACE
{
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
38 WalManager(const ImmutableDBOptions
& db_options
,
39 const FileOptions
& file_options
,
40 const std::shared_ptr
<IOTracer
>& io_tracer
,
41 const bool seq_per_batch
= false)
42 : db_options_(db_options
),
43 file_options_(file_options
),
45 fs_(db_options
.fs
, io_tracer
),
46 purge_wal_files_last_run_(0),
47 seq_per_batch_(seq_per_batch
),
48 wal_in_db_path_(IsWalDirSameAsDBPath(&db_options
)),
49 io_tracer_(io_tracer
) {}
51 Status
GetSortedWalFiles(VectorLogPtr
& files
);
53 // Allow user to tail transaction log to find all recent changes to the
54 // database that are newer than `seq_number`.
55 Status
GetUpdatesSince(
56 SequenceNumber seq_number
, std::unique_ptr
<TransactionLogIterator
>* iter
,
57 const TransactionLogIterator::ReadOptions
& read_options
,
58 VersionSet
* version_set
);
60 void PurgeObsoleteWALFiles();
62 void ArchiveWALFile(const std::string
& fname
, uint64_t number
);
64 Status
DeleteFile(const std::string
& fname
, uint64_t number
);
66 Status
GetLiveWalFile(uint64_t number
, std::unique_ptr
<LogFile
>* log_file
);
68 Status
TEST_ReadFirstRecord(const WalFileType type
, const uint64_t number
,
69 SequenceNumber
* sequence
) {
70 return ReadFirstRecord(type
, number
, sequence
);
73 Status
TEST_ReadFirstLine(const std::string
& fname
, const uint64_t number
,
74 SequenceNumber
* sequence
) {
75 return ReadFirstLine(fname
, number
, sequence
);
79 Status
GetSortedWalsOfType(const std::string
& path
, VectorLogPtr
& log_files
,
81 // Requires: all_logs should be sorted with earliest log file first
82 // Retains all log files in all_logs which contain updates with seq no.
83 // Greater Than or Equal to the requested SequenceNumber.
84 Status
RetainProbableWalFiles(VectorLogPtr
& all_logs
,
85 const SequenceNumber target
);
87 Status
ReadFirstRecord(const WalFileType type
, const uint64_t number
,
88 SequenceNumber
* sequence
);
90 Status
ReadFirstLine(const std::string
& fname
, const uint64_t number
,
91 SequenceNumber
* sequence
);
93 // ------- state from DBImpl ------
94 const ImmutableDBOptions
& db_options_
;
95 const FileOptions file_options_
;
97 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_
;
104 // last time when PurgeObsoleteWALFiles ran.
105 uint64_t purge_wal_files_last_run_
;
109 bool wal_in_db_path_
;
111 // obsolete files will be deleted every this seconds if ttl deletion is
112 // enabled and archive size_limit is disabled.
113 static const uint64_t kDefaultIntervalToDeleteObsoleteWAL
= 600;
115 std::shared_ptr
<IOTracer
> io_tracer_
;
118 #endif // ROCKSDB_LITE
119 } // namespace ROCKSDB_NAMESPACE