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.
12 #include "db/db_impl.h"
13 #include "db/error_handler.h"
14 #include "monitoring/thread_status_updater.h"
18 uint64_t DBImpl::TEST_GetLevel0TotalSize() {
19 InstrumentedMutexLock
l(&mutex_
);
20 return default_cf_handle_
->cfd()->current()->storage_info()->NumLevelBytes(0);
23 void DBImpl::TEST_SwitchWAL() {
24 WriteContext write_context
;
25 InstrumentedMutexLock
l(&mutex_
);
26 SwitchWAL(&write_context
);
29 bool DBImpl::TEST_WALBufferIsEmpty() {
30 InstrumentedMutexLock
wl(&log_write_mutex_
);
31 log::Writer
* cur_log_writer
= logs_
.back().writer
;
32 return cur_log_writer
->TEST_BufferIsEmpty();
35 int64_t DBImpl::TEST_MaxNextLevelOverlappingBytes(
36 ColumnFamilyHandle
* column_family
) {
37 ColumnFamilyData
* cfd
;
38 if (column_family
== nullptr) {
39 cfd
= default_cf_handle_
->cfd();
41 auto cfh
= reinterpret_cast<ColumnFamilyHandleImpl
*>(column_family
);
44 InstrumentedMutexLock
l(&mutex_
);
45 return cfd
->current()->storage_info()->MaxNextLevelOverlappingBytes();
48 void DBImpl::TEST_GetFilesMetaData(
49 ColumnFamilyHandle
* column_family
,
50 std::vector
<std::vector
<FileMetaData
>>* metadata
) {
51 auto cfh
= reinterpret_cast<ColumnFamilyHandleImpl
*>(column_family
);
52 auto cfd
= cfh
->cfd();
53 InstrumentedMutexLock
l(&mutex_
);
54 metadata
->resize(NumberLevels());
55 for (int level
= 0; level
< NumberLevels(); level
++) {
56 const std::vector
<FileMetaData
*>& files
=
57 cfd
->current()->storage_info()->LevelFiles(level
);
59 (*metadata
)[level
].clear();
60 for (const auto& f
: files
) {
61 (*metadata
)[level
].push_back(*f
);
66 uint64_t DBImpl::TEST_Current_Manifest_FileNo() {
67 return versions_
->manifest_file_number();
70 uint64_t DBImpl::TEST_Current_Next_FileNo() {
71 return versions_
->current_next_file_number();
74 Status
DBImpl::TEST_CompactRange(int level
, const Slice
* begin
,
76 ColumnFamilyHandle
* column_family
,
77 bool disallow_trivial_move
) {
78 ColumnFamilyData
* cfd
;
79 if (column_family
== nullptr) {
80 cfd
= default_cf_handle_
->cfd();
82 auto cfh
= reinterpret_cast<ColumnFamilyHandleImpl
*>(column_family
);
86 (cfd
->ioptions()->compaction_style
== kCompactionStyleUniversal
||
87 cfd
->ioptions()->compaction_style
== kCompactionStyleFIFO
)
90 return RunManualCompaction(cfd
, level
, output_level
, 0, 0, begin
, end
, true,
91 disallow_trivial_move
);
94 Status
DBImpl::TEST_SwitchMemtable(ColumnFamilyData
* cfd
) {
95 WriteContext write_context
;
96 InstrumentedMutexLock
l(&mutex_
);
98 cfd
= default_cf_handle_
->cfd();
100 return SwitchMemtable(cfd
, &write_context
);
103 Status
DBImpl::TEST_FlushMemTable(bool wait
, bool allow_write_stall
,
104 ColumnFamilyHandle
* cfh
) {
107 fo
.allow_write_stall
= allow_write_stall
;
108 ColumnFamilyData
* cfd
;
109 if (cfh
== nullptr) {
110 cfd
= default_cf_handle_
->cfd();
112 auto cfhi
= reinterpret_cast<ColumnFamilyHandleImpl
*>(cfh
);
115 return FlushMemTable(cfd
, fo
, FlushReason::kTest
);
118 Status
DBImpl::TEST_WaitForFlushMemTable(ColumnFamilyHandle
* column_family
) {
119 ColumnFamilyData
* cfd
;
120 if (column_family
== nullptr) {
121 cfd
= default_cf_handle_
->cfd();
123 auto cfh
= reinterpret_cast<ColumnFamilyHandleImpl
*>(column_family
);
126 return WaitForFlushMemTable(cfd
);
129 Status
DBImpl::TEST_WaitForCompact(bool wait_unscheduled
) {
130 // Wait until the compaction completes
132 // TODO: a bug here. This function actually does not necessarily
133 // wait for compact. It actually waits for scheduled compaction
134 // OR flush to finish.
136 InstrumentedMutexLock
l(&mutex_
);
137 while ((bg_bottom_compaction_scheduled_
|| bg_compaction_scheduled_
||
138 bg_flush_scheduled_
||
139 (wait_unscheduled
&& unscheduled_compactions_
)) &&
140 (error_handler_
.GetBGError() == Status::OK())) {
143 return error_handler_
.GetBGError();
146 void DBImpl::TEST_LockMutex() {
150 void DBImpl::TEST_UnlockMutex() {
154 void* DBImpl::TEST_BeginWrite() {
155 auto w
= new WriteThread::Writer();
156 write_thread_
.EnterUnbatched(w
, &mutex_
);
157 return reinterpret_cast<void*>(w
);
160 void DBImpl::TEST_EndWrite(void* w
) {
161 auto writer
= reinterpret_cast<WriteThread::Writer
*>(w
);
162 write_thread_
.ExitUnbatched(writer
);
166 size_t DBImpl::TEST_LogsToFreeSize() {
167 InstrumentedMutexLock
l(&mutex_
);
168 return logs_to_free_
.size();
171 uint64_t DBImpl::TEST_LogfileNumber() {
172 InstrumentedMutexLock
l(&mutex_
);
173 return logfile_number_
;
176 Status
DBImpl::TEST_GetAllImmutableCFOptions(
177 std::unordered_map
<std::string
, const ImmutableCFOptions
*>* iopts_map
) {
178 std::vector
<std::string
> cf_names
;
179 std::vector
<const ImmutableCFOptions
*> iopts
;
181 InstrumentedMutexLock
l(&mutex_
);
182 for (auto cfd
: *versions_
->GetColumnFamilySet()) {
183 cf_names
.push_back(cfd
->GetName());
184 iopts
.push_back(cfd
->ioptions());
188 for (size_t i
= 0; i
< cf_names
.size(); ++i
) {
189 iopts_map
->insert({cf_names
[i
], iopts
[i
]});
195 uint64_t DBImpl::TEST_FindMinLogContainingOutstandingPrep() {
196 return logs_with_prep_tracker_
.FindMinLogContainingOutstandingPrep();
199 size_t DBImpl::TEST_PreparedSectionCompletedSize() {
200 return logs_with_prep_tracker_
.TEST_PreparedSectionCompletedSize();
203 size_t DBImpl::TEST_LogsWithPrepSize() {
204 return logs_with_prep_tracker_
.TEST_LogsWithPrepSize();
207 uint64_t DBImpl::TEST_FindMinPrepLogReferencedByMemTable() {
208 autovector
<MemTable
*> empty_list
;
209 return FindMinPrepLogReferencedByMemTable(versions_
.get(), nullptr,
213 Status
DBImpl::TEST_GetLatestMutableCFOptions(
214 ColumnFamilyHandle
* column_family
, MutableCFOptions
* mutable_cf_options
) {
215 InstrumentedMutexLock
l(&mutex_
);
217 auto cfh
= reinterpret_cast<ColumnFamilyHandleImpl
*>(column_family
);
218 *mutable_cf_options
= *cfh
->cfd()->GetLatestMutableCFOptions();
222 int DBImpl::TEST_BGCompactionsAllowed() const {
223 InstrumentedMutexLock
l(&mutex_
);
224 return GetBGJobLimits().max_compactions
;
227 int DBImpl::TEST_BGFlushesAllowed() const {
228 InstrumentedMutexLock
l(&mutex_
);
229 return GetBGJobLimits().max_flushes
;
232 SequenceNumber
DBImpl::TEST_GetLastVisibleSequence() const {
233 if (last_seq_same_as_publish_seq_
) {
234 return versions_
->LastSequence();
236 return versions_
->LastAllocatedSequence();
240 size_t DBImpl::TEST_GetWalPreallocateBlockSize(
241 uint64_t write_buffer_size
) const {
242 InstrumentedMutexLock
l(&mutex_
);
243 return GetWalPreallocateBlockSize(write_buffer_size
);
246 } // namespace rocksdb