blob: ca00d4209a0ccbe2a1ed225670e29c439f9ec2c6 [file] [log] [blame]
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +00001// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file. See the AUTHORS file for names of contributors.
4
5#ifndef STORAGE_LEVELDB_DB_DB_IMPL_H_
6#define STORAGE_LEVELDB_DB_DB_IMPL_H_
7
costan7d8e41e2019-03-11 13:04:53 -07008#include <atomic>
Sanjay Ghemawatd79762e2012-03-08 16:23:21 -08009#include <deque>
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000010#include <set>
costan7d8e41e2019-03-11 13:04:53 -070011#include <string>
12
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000013#include "db/dbformat.h"
14#include "db/log_writer.h"
15#include "db/snapshot.h"
jorlow@chromium.org4671a692011-03-30 18:35:40 +000016#include "leveldb/db.h"
17#include "leveldb/env.h"
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000018#include "port/port.h"
David Grogan946e5b52012-10-12 11:53:12 -070019#include "port/thread_annotations.h"
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000020
21namespace leveldb {
22
23class MemTable;
24class TableCache;
25class Version;
26class VersionEdit;
27class VersionSet;
28
29class DBImpl : public DB {
30 public:
31 DBImpl(const Options& options, const std::string& dbname);
32 virtual ~DBImpl();
33
34 // Implementations of the DB interface
35 virtual Status Put(const WriteOptions&, const Slice& key, const Slice& value);
36 virtual Status Delete(const WriteOptions&, const Slice& key);
37 virtual Status Write(const WriteOptions& options, WriteBatch* updates);
38 virtual Status Get(const ReadOptions& options,
39 const Slice& key,
40 std::string* value);
41 virtual Iterator* NewIterator(const ReadOptions&);
42 virtual const Snapshot* GetSnapshot();
43 virtual void ReleaseSnapshot(const Snapshot* snapshot);
dgrogan@chromium.orgf779e7a2011-04-12 19:38:58 +000044 virtual bool GetProperty(const Slice& property, std::string* value);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000045 virtual void GetApproximateSizes(const Range* range, int n, uint64_t* sizes);
Gabor Cselle299cced2011-10-05 16:30:28 -070046 virtual void CompactRange(const Slice* begin, const Slice* end);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000047
48 // Extra methods (for testing) that are not in the public DB interface
49
Gabor Cselle299cced2011-10-05 16:30:28 -070050 // Compact any files in the named level that overlap [*begin,*end]
51 void TEST_CompactRange(int level, const Slice* begin, const Slice* end);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000052
53 // Force current memtable contents to be compacted.
54 Status TEST_CompactMemTable();
55
56 // Return an internal iterator over the current state of the database.
57 // The keys of this iterator are internal keys (see format.h).
58 // The returned iterator should be deleted when no longer needed.
59 Iterator* TEST_NewInternalIterator();
60
jorlow@chromium.org13b72af2011-03-22 18:32:49 +000061 // Return the maximum overlapping data (in bytes) at next level for any
62 // file at a level >= 1.
jorlow@chromium.org8303bb12011-03-22 23:24:02 +000063 int64_t TEST_MaxNextLevelOverlappingBytes();
jorlow@chromium.org13b72af2011-03-22 18:32:49 +000064
David Grogan748539c2013-08-21 11:12:47 -070065 // Record a sample of bytes read at the specified internal key.
66 // Samples are taken approximately once every config::kReadBytesPeriod
67 // bytes.
68 void RecordReadSample(Slice key);
69
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000070 private:
71 friend class DB;
Sanjay Ghemawatd79762e2012-03-08 16:23:21 -080072 struct CompactionState;
73 struct Writer;
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000074
75 Iterator* NewInternalIterator(const ReadOptions&,
David Grogan748539c2013-08-21 11:12:47 -070076 SequenceNumber* latest_snapshot,
77 uint32_t* seed);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000078
79 Status NewDB();
80
81 // Recover the descriptor from persistent storage. May do a significant
82 // amount of work to recover recently logged updates. Any changes to
83 // be made to the descriptor are added to *edit.
Sanjay Ghemawatac1d69d2014-12-11 08:13:18 -080084 Status Recover(VersionEdit* edit, bool* save_manifest)
85 EXCLUSIVE_LOCKS_REQUIRED(mutex_);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000086
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000087 void MaybeIgnoreError(Status* s) const;
88
89 // Delete any unneeded files and stale in-memory entries.
costan0fa5a4f2018-03-16 10:06:35 -070090 void DeleteObsoleteFiles() EXCLUSIVE_LOCKS_REQUIRED(mutex_);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000091
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000092 // Compact the in-memory write buffer to disk. Switches to a new
93 // log-file/memtable and writes a new descriptor iff successful.
David Grogan0cfb9902013-12-10 10:36:31 -080094 // Errors are recorded in bg_error_.
95 void CompactMemTable() EXCLUSIVE_LOCKS_REQUIRED(mutex_);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000096
Sanjay Ghemawatac1d69d2014-12-11 08:13:18 -080097 Status RecoverLogFile(uint64_t log_number, bool last_log, bool* save_manifest,
98 VersionEdit* edit, SequenceNumber* max_sequence)
David Grogan946e5b52012-10-12 11:53:12 -070099 EXCLUSIVE_LOCKS_REQUIRED(mutex_);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000100
David Grogan946e5b52012-10-12 11:53:12 -0700101 Status WriteLevel0Table(MemTable* mem, VersionEdit* edit, Version* base)
102 EXCLUSIVE_LOCKS_REQUIRED(mutex_);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000103
David Grogan946e5b52012-10-12 11:53:12 -0700104 Status MakeRoomForWrite(bool force /* compact even if there is room? */)
105 EXCLUSIVE_LOCKS_REQUIRED(mutex_);
costan0fa5a4f2018-03-16 10:06:35 -0700106 WriteBatch* BuildBatchGroup(Writer** last_writer)
107 EXCLUSIVE_LOCKS_REQUIRED(mutex_);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000108
David Grogan0cfb9902013-12-10 10:36:31 -0800109 void RecordBackgroundError(const Status& s);
110
David Grogan946e5b52012-10-12 11:53:12 -0700111 void MaybeScheduleCompaction() EXCLUSIVE_LOCKS_REQUIRED(mutex_);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000112 static void BGWork(void* db);
113 void BackgroundCall();
costan0fa5a4f2018-03-16 10:06:35 -0700114 void BackgroundCompaction() EXCLUSIVE_LOCKS_REQUIRED(mutex_);
David Grogan946e5b52012-10-12 11:53:12 -0700115 void CleanupCompaction(CompactionState* compact)
116 EXCLUSIVE_LOCKS_REQUIRED(mutex_);
117 Status DoCompactionWork(CompactionState* compact)
118 EXCLUSIVE_LOCKS_REQUIRED(mutex_);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000119
120 Status OpenCompactionOutputFile(CompactionState* compact);
121 Status FinishCompactionOutputFile(CompactionState* compact, Iterator* input);
David Grogan946e5b52012-10-12 11:53:12 -0700122 Status InstallCompactionResults(CompactionState* compact)
123 EXCLUSIVE_LOCKS_REQUIRED(mutex_);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000124
125 // Constant after construction
126 Env* const env_;
127 const InternalKeyComparator internal_comparator_;
Sanjay Ghemawat85584d42012-04-17 08:36:46 -0700128 const InternalFilterPolicy internal_filter_policy_;
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000129 const Options options_; // options_.comparator == &internal_comparator_
costan0fa5a4f2018-03-16 10:06:35 -0700130 const bool owns_info_log_;
131 const bool owns_cache_;
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000132 const std::string dbname_;
133
134 // table_cache_ provides its own synchronization
costan0fa5a4f2018-03-16 10:06:35 -0700135 TableCache* const table_cache_;
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000136
costan09217fd2018-04-10 16:18:06 -0700137 // Lock over the persistent DB state. Non-null iff successfully acquired.
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000138 FileLock* db_lock_;
139
140 // State below is protected by mutex_
141 port::Mutex mutex_;
costan7d8e41e2019-03-11 13:04:53 -0700142 std::atomic<bool> shutting_down_;
costan0fa5a4f2018-03-16 10:06:35 -0700143 port::CondVar background_work_finished_signal_ GUARDED_BY(mutex_);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000144 MemTable* mem_;
costan0fa5a4f2018-03-16 10:06:35 -0700145 MemTable* imm_ GUARDED_BY(mutex_); // Memtable being compacted
costan7d8e41e2019-03-11 13:04:53 -0700146 std::atomic<bool> has_imm_; // So bg thread can detect non-null imm_
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000147 WritableFile* logfile_;
costan0fa5a4f2018-03-16 10:06:35 -0700148 uint64_t logfile_number_ GUARDED_BY(mutex_);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000149 log::Writer* log_;
costan0fa5a4f2018-03-16 10:06:35 -0700150 uint32_t seed_ GUARDED_BY(mutex_); // For sampling.
Sanjay Ghemawatd79762e2012-03-08 16:23:21 -0800151
152 // Queue of writers.
costan0fa5a4f2018-03-16 10:06:35 -0700153 std::deque<Writer*> writers_ GUARDED_BY(mutex_);
154 WriteBatch* tmp_batch_ GUARDED_BY(mutex_);
Sanjay Ghemawatd79762e2012-03-08 16:23:21 -0800155
costan0fa5a4f2018-03-16 10:06:35 -0700156 SnapshotList snapshots_ GUARDED_BY(mutex_);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000157
158 // Set of table files to protect from deletion because they are
159 // part of ongoing compactions.
costan0fa5a4f2018-03-16 10:06:35 -0700160 std::set<uint64_t> pending_outputs_ GUARDED_BY(mutex_);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000161
162 // Has a background compaction been scheduled or is running?
costan0fa5a4f2018-03-16 10:06:35 -0700163 bool background_compaction_scheduled_ GUARDED_BY(mutex_);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000164
hans@chromium.org80e5b0d2011-06-07 14:40:26 +0000165 // Information for a manual compaction
166 struct ManualCompaction {
167 int level;
Gabor Cselle299cced2011-10-05 16:30:28 -0700168 bool done;
costan09217fd2018-04-10 16:18:06 -0700169 const InternalKey* begin; // null means beginning of key range
170 const InternalKey* end; // null means end of key range
Gabor Cselle299cced2011-10-05 16:30:28 -0700171 InternalKey tmp_storage; // Used to keep track of compaction progress
hans@chromium.org80e5b0d2011-06-07 14:40:26 +0000172 };
costan0fa5a4f2018-03-16 10:06:35 -0700173 ManualCompaction* manual_compaction_ GUARDED_BY(mutex_);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000174
costan0fa5a4f2018-03-16 10:06:35 -0700175 VersionSet* const versions_;
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000176
177 // Have we encountered a background error in paranoid mode?
costan0fa5a4f2018-03-16 10:06:35 -0700178 Status bg_error_ GUARDED_BY(mutex_);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000179
dgrogan@chromium.orgf779e7a2011-04-12 19:38:58 +0000180 // Per level compaction stats. stats_[level] stores the stats for
181 // compactions that produced data for the specified "level".
182 struct CompactionStats {
183 int64_t micros;
184 int64_t bytes_read;
185 int64_t bytes_written;
186
187 CompactionStats() : micros(0), bytes_read(0), bytes_written(0) { }
188
189 void Add(const CompactionStats& c) {
190 this->micros += c.micros;
191 this->bytes_read += c.bytes_read;
192 this->bytes_written += c.bytes_written;
193 }
194 };
costan0fa5a4f2018-03-16 10:06:35 -0700195 CompactionStats stats_[config::kNumLevels] GUARDED_BY(mutex_);
dgrogan@chromium.orgf779e7a2011-04-12 19:38:58 +0000196
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000197 // No copying allowed
198 DBImpl(const DBImpl&);
199 void operator=(const DBImpl&);
200
201 const Comparator* user_comparator() const {
202 return internal_comparator_.user_comparator();
203 }
204};
205
206// Sanitize db options. The caller should delete result.info_log if
207// it is not equal to src.info_log.
costanaece2062018-03-12 09:14:44 -0700208Options SanitizeOptions(const std::string& db,
209 const InternalKeyComparator* icmp,
210 const InternalFilterPolicy* ipolicy,
211 const Options& src);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000212
Hans Wennborg36a5f8e2011-10-31 17:22:06 +0000213} // namespace leveldb
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000214
215#endif // STORAGE_LEVELDB_DB_DB_IMPL_H_