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/column_family.h"
13 #include "db/db_impl/db_impl.h"
14 #include "db/error_handler.h"
15 #include "db/periodic_task_scheduler.h"
16 #include "monitoring/thread_status_updater.h"
17 #include "util/cast_util.h"
19 namespace ROCKSDB_NAMESPACE
{
20 uint64_t DBImpl::TEST_GetLevel0TotalSize() {
21 InstrumentedMutexLock
l(&mutex_
);
22 return default_cf_handle_
->cfd()->current()->storage_info()->NumLevelBytes(0);
25 Status
DBImpl::TEST_SwitchWAL() {
26 WriteContext write_context
;
27 InstrumentedMutexLock
l(&mutex_
);
28 void* writer
= TEST_BeginWrite();
29 auto s
= SwitchWAL(&write_context
);
30 TEST_EndWrite(writer
);
34 uint64_t DBImpl::TEST_MaxNextLevelOverlappingBytes(
35 ColumnFamilyHandle
* column_family
) {
36 ColumnFamilyData
* cfd
;
37 if (column_family
== nullptr) {
38 cfd
= default_cf_handle_
->cfd();
40 auto cfh
= static_cast_with_check
<ColumnFamilyHandleImpl
>(column_family
);
43 InstrumentedMutexLock
l(&mutex_
);
44 return cfd
->current()->storage_info()->MaxNextLevelOverlappingBytes();
47 void DBImpl::TEST_GetFilesMetaData(
48 ColumnFamilyHandle
* column_family
,
49 std::vector
<std::vector
<FileMetaData
>>* metadata
,
50 std::vector
<std::shared_ptr
<BlobFileMetaData
>>* blob_metadata
) {
53 auto cfh
= static_cast_with_check
<ColumnFamilyHandleImpl
>(column_family
);
56 auto cfd
= cfh
->cfd();
59 InstrumentedMutexLock
l(&mutex_
);
61 const auto* current
= cfd
->current();
64 const auto* vstorage
= current
->storage_info();
67 metadata
->resize(NumberLevels());
69 for (int level
= 0; level
< NumberLevels(); ++level
) {
70 const std::vector
<FileMetaData
*>& files
= vstorage
->LevelFiles(level
);
72 (*metadata
)[level
].clear();
73 (*metadata
)[level
].reserve(files
.size());
75 for (const auto& f
: files
) {
76 (*metadata
)[level
].push_back(*f
);
81 *blob_metadata
= vstorage
->GetBlobFiles();
85 uint64_t DBImpl::TEST_Current_Manifest_FileNo() {
86 return versions_
->manifest_file_number();
89 uint64_t DBImpl::TEST_Current_Next_FileNo() {
90 return versions_
->current_next_file_number();
93 Status
DBImpl::TEST_CompactRange(int level
, const Slice
* begin
,
95 ColumnFamilyHandle
* column_family
,
96 bool disallow_trivial_move
) {
97 ColumnFamilyData
* cfd
;
98 if (column_family
== nullptr) {
99 cfd
= default_cf_handle_
->cfd();
101 auto cfh
= static_cast_with_check
<ColumnFamilyHandleImpl
>(column_family
);
105 (cfd
->ioptions()->compaction_style
== kCompactionStyleUniversal
||
106 cfd
->ioptions()->compaction_style
== kCompactionStyleFIFO
)
109 return RunManualCompaction(
110 cfd
, level
, output_level
, CompactRangeOptions(), begin
, end
, true,
111 disallow_trivial_move
,
112 std::numeric_limits
<uint64_t>::max() /*max_file_num_to_ignore*/,
116 Status
DBImpl::TEST_SwitchMemtable(ColumnFamilyData
* cfd
) {
117 WriteContext write_context
;
118 InstrumentedMutexLock
l(&mutex_
);
119 if (cfd
== nullptr) {
120 cfd
= default_cf_handle_
->cfd();
124 void* writer
= TEST_BeginWrite();
125 if (two_write_queues_
) {
126 WriteThread::Writer nonmem_w
;
127 nonmem_write_thread_
.EnterUnbatched(&nonmem_w
, &mutex_
);
128 s
= SwitchMemtable(cfd
, &write_context
);
129 nonmem_write_thread_
.ExitUnbatched(&nonmem_w
);
131 s
= SwitchMemtable(cfd
, &write_context
);
133 TEST_EndWrite(writer
);
137 Status
DBImpl::TEST_FlushMemTable(bool wait
, bool allow_write_stall
,
138 ColumnFamilyHandle
* cfh
) {
141 fo
.allow_write_stall
= allow_write_stall
;
142 ColumnFamilyData
* cfd
;
143 if (cfh
== nullptr) {
144 cfd
= default_cf_handle_
->cfd();
146 auto cfhi
= static_cast_with_check
<ColumnFamilyHandleImpl
>(cfh
);
149 return FlushMemTable(cfd
, fo
, FlushReason::kTest
);
152 Status
DBImpl::TEST_FlushMemTable(ColumnFamilyData
* cfd
,
153 const FlushOptions
& flush_opts
) {
154 return FlushMemTable(cfd
, flush_opts
, FlushReason::kTest
);
157 Status
DBImpl::TEST_AtomicFlushMemTables(
158 const autovector
<ColumnFamilyData
*>& cfds
, const FlushOptions
& flush_opts
) {
159 return AtomicFlushMemTables(cfds
, flush_opts
, FlushReason::kTest
);
162 Status
DBImpl::TEST_WaitForBackgroundWork() {
163 InstrumentedMutexLock
l(&mutex_
);
164 WaitForBackgroundWork();
165 return error_handler_
.GetBGError();
168 Status
DBImpl::TEST_WaitForFlushMemTable(ColumnFamilyHandle
* column_family
) {
169 ColumnFamilyData
* cfd
;
170 if (column_family
== nullptr) {
171 cfd
= default_cf_handle_
->cfd();
173 auto cfh
= static_cast_with_check
<ColumnFamilyHandleImpl
>(column_family
);
176 return WaitForFlushMemTable(cfd
, nullptr, false);
179 Status
DBImpl::TEST_WaitForCompact(bool wait_unscheduled
) {
180 // Wait until the compaction completes
181 return WaitForCompact(wait_unscheduled
);
184 Status
DBImpl::TEST_WaitForPurge() {
185 InstrumentedMutexLock
l(&mutex_
);
186 while (bg_purge_scheduled_
&& error_handler_
.GetBGError().ok()) {
189 return error_handler_
.GetBGError();
192 Status
DBImpl::TEST_GetBGError() {
193 InstrumentedMutexLock
l(&mutex_
);
194 return error_handler_
.GetBGError();
197 void DBImpl::TEST_LockMutex() { mutex_
.Lock(); }
199 void DBImpl::TEST_UnlockMutex() { mutex_
.Unlock(); }
201 void* DBImpl::TEST_BeginWrite() {
202 auto w
= new WriteThread::Writer();
203 write_thread_
.EnterUnbatched(w
, &mutex_
);
204 return reinterpret_cast<void*>(w
);
207 void DBImpl::TEST_EndWrite(void* w
) {
208 auto writer
= reinterpret_cast<WriteThread::Writer
*>(w
);
209 write_thread_
.ExitUnbatched(writer
);
213 size_t DBImpl::TEST_LogsToFreeSize() {
214 InstrumentedMutexLock
l(&log_write_mutex_
);
215 return logs_to_free_
.size();
218 uint64_t DBImpl::TEST_LogfileNumber() {
219 InstrumentedMutexLock
l(&mutex_
);
220 return logfile_number_
;
223 Status
DBImpl::TEST_GetAllImmutableCFOptions(
224 std::unordered_map
<std::string
, const ImmutableCFOptions
*>* iopts_map
) {
225 std::vector
<std::string
> cf_names
;
226 std::vector
<const ImmutableCFOptions
*> iopts
;
228 InstrumentedMutexLock
l(&mutex_
);
229 for (auto cfd
: *versions_
->GetColumnFamilySet()) {
230 cf_names
.push_back(cfd
->GetName());
231 iopts
.push_back(cfd
->ioptions());
235 for (size_t i
= 0; i
< cf_names
.size(); ++i
) {
236 iopts_map
->insert({cf_names
[i
], iopts
[i
]});
242 uint64_t DBImpl::TEST_FindMinLogContainingOutstandingPrep() {
243 return logs_with_prep_tracker_
.FindMinLogContainingOutstandingPrep();
246 size_t DBImpl::TEST_PreparedSectionCompletedSize() {
247 return logs_with_prep_tracker_
.TEST_PreparedSectionCompletedSize();
250 size_t DBImpl::TEST_LogsWithPrepSize() {
251 return logs_with_prep_tracker_
.TEST_LogsWithPrepSize();
254 uint64_t DBImpl::TEST_FindMinPrepLogReferencedByMemTable() {
255 autovector
<MemTable
*> empty_list
;
256 return FindMinPrepLogReferencedByMemTable(versions_
.get(), empty_list
);
259 Status
DBImpl::TEST_GetLatestMutableCFOptions(
260 ColumnFamilyHandle
* column_family
, MutableCFOptions
* mutable_cf_options
) {
261 InstrumentedMutexLock
l(&mutex_
);
263 auto cfh
= static_cast_with_check
<ColumnFamilyHandleImpl
>(column_family
);
264 *mutable_cf_options
= *cfh
->cfd()->GetLatestMutableCFOptions();
268 int DBImpl::TEST_BGCompactionsAllowed() const {
269 InstrumentedMutexLock
l(&mutex_
);
270 return GetBGJobLimits().max_compactions
;
273 int DBImpl::TEST_BGFlushesAllowed() const {
274 InstrumentedMutexLock
l(&mutex_
);
275 return GetBGJobLimits().max_flushes
;
278 SequenceNumber
DBImpl::TEST_GetLastVisibleSequence() const {
279 if (last_seq_same_as_publish_seq_
) {
280 return versions_
->LastSequence();
282 return versions_
->LastAllocatedSequence();
286 size_t DBImpl::TEST_GetWalPreallocateBlockSize(
287 uint64_t write_buffer_size
) const {
288 InstrumentedMutexLock
l(&mutex_
);
289 return GetWalPreallocateBlockSize(write_buffer_size
);
293 void DBImpl::TEST_WaitForPeridicTaskRun(std::function
<void()> callback
) const {
294 periodic_task_scheduler_
.TEST_WaitForRun(callback
);
297 const PeriodicTaskScheduler
& DBImpl::TEST_GetPeriodicTaskScheduler() const {
298 return periodic_task_scheduler_
;
301 SeqnoToTimeMapping
DBImpl::TEST_GetSeqnoToTimeMapping() const {
302 InstrumentedMutexLock
l(&mutex_
);
303 return seqno_time_mapping_
;
306 #endif // !ROCKSDB_LITE
308 size_t DBImpl::TEST_EstimateInMemoryStatsHistorySize() const {
309 return EstimateInMemoryStatsHistorySize();
311 } // namespace ROCKSDB_NAMESPACE