]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/db/wal_manager.h
import quincy beta 17.1.0
[ceph.git] / ceph / src / rocksdb / db / wal_manager.h
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,
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),
44 env_(db_options.env),
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) {}
50
51 Status GetSortedWalFiles(VectorLogPtr& files);
52
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);
59
60 void PurgeObsoleteWALFiles();
61
62 void ArchiveWALFile(const std::string& fname, uint64_t number);
63
64 Status DeleteFile(const std::string& fname, uint64_t number);
65
66 Status GetLiveWalFile(uint64_t number, std::unique_ptr<LogFile>* log_file);
67
68 Status TEST_ReadFirstRecord(const WalFileType type, const uint64_t number,
69 SequenceNumber* sequence) {
70 return ReadFirstRecord(type, number, sequence);
71 }
72
73 Status TEST_ReadFirstLine(const std::string& fname, const uint64_t number,
74 SequenceNumber* sequence) {
75 return ReadFirstLine(fname, number, sequence);
76 }
77
78 private:
79 Status GetSortedWalsOfType(const std::string& path, VectorLogPtr& log_files,
80 WalFileType type);
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);
86
87 Status ReadFirstRecord(const WalFileType type, const uint64_t number,
88 SequenceNumber* sequence);
89
90 Status ReadFirstLine(const std::string& fname, const uint64_t number,
91 SequenceNumber* sequence);
92
93 // ------- state from DBImpl ------
94 const ImmutableDBOptions& db_options_;
95 const FileOptions file_options_;
96 Env* env_;
97 const FileSystemPtr fs_;
98
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
104 // last time when PurgeObsoleteWALFiles ran.
105 uint64_t purge_wal_files_last_run_;
106
107 bool seq_per_batch_;
108
109 bool wal_in_db_path_;
110
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;
114
115 std::shared_ptr<IOTracer> io_tracer_;
116 };
117
118 #endif // ROCKSDB_LITE
119 } // namespace ROCKSDB_NAMESPACE