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
7 #ifndef ROCKSDB_LITE
8
9 #include <stdint.h>
10 #include <algorithm>
11 #include <cinttypes>
12 #include <string>
13 #include "db/db_impl/db_impl.h"
14 #include "db/job_context.h"
15 #include "db/version_set.h"
16 #include "file/file_util.h"
17 #include "file/filename.h"
18 #include "port/port.h"
19 #include "rocksdb/db.h"
20 #include "rocksdb/env.h"
21 #include "test_util/sync_point.h"
22 #include "util/mutexlock.h"
23
24 namespace ROCKSDB_NAMESPACE {
25
DisableFileDeletions()26 Status DBImpl::DisableFileDeletions() {
27 InstrumentedMutexLock l(&mutex_);
28 ++disable_delete_obsolete_files_;
29 if (disable_delete_obsolete_files_ == 1) {
30 ROCKS_LOG_INFO(immutable_db_options_.info_log, "File Deletions Disabled");
31 } else {
32 ROCKS_LOG_WARN(immutable_db_options_.info_log,
33 "File Deletions Disabled, but already disabled. Counter: %d",
34 disable_delete_obsolete_files_);
35 }
36 return Status::OK();
37 }
38
EnableFileDeletions(bool force)39 Status DBImpl::EnableFileDeletions(bool force) {
40 // Job id == 0 means that this is not our background process, but rather
41 // user thread
42 JobContext job_context(0);
43 bool file_deletion_enabled = false;
44 {
45 InstrumentedMutexLock l(&mutex_);
46 if (force) {
47 // if force, we need to enable file deletions right away
48 disable_delete_obsolete_files_ = 0;
49 } else if (disable_delete_obsolete_files_ > 0) {
50 --disable_delete_obsolete_files_;
51 }
52 if (disable_delete_obsolete_files_ == 0) {
53 file_deletion_enabled = true;
54 FindObsoleteFiles(&job_context, true);
55 bg_cv_.SignalAll();
56 }
57 }
58 if (file_deletion_enabled) {
59 ROCKS_LOG_INFO(immutable_db_options_.info_log, "File Deletions Enabled");
60 if (job_context.HaveSomethingToDelete()) {
61 PurgeObsoleteFiles(job_context);
62 }
63 } else {
64 ROCKS_LOG_WARN(immutable_db_options_.info_log,
65 "File Deletions Enable, but not really enabled. Counter: %d",
66 disable_delete_obsolete_files_);
67 }
68 job_context.Clean();
69 LogFlush(immutable_db_options_.info_log);
70 return Status::OK();
71 }
72
IsFileDeletionsEnabled() const73 int DBImpl::IsFileDeletionsEnabled() const {
74 return !disable_delete_obsolete_files_;
75 }
76
GetLiveFiles(std::vector<std::string> & ret,uint64_t * manifest_file_size,bool flush_memtable)77 Status DBImpl::GetLiveFiles(std::vector<std::string>& ret,
78 uint64_t* manifest_file_size,
79 bool flush_memtable) {
80 *manifest_file_size = 0;
81
82 mutex_.Lock();
83
84 if (flush_memtable) {
85 // flush all dirty data to disk.
86 Status status;
87 if (immutable_db_options_.atomic_flush) {
88 autovector<ColumnFamilyData*> cfds;
89 SelectColumnFamiliesForAtomicFlush(&cfds);
90 mutex_.Unlock();
91 status = AtomicFlushMemTables(cfds, FlushOptions(),
92 FlushReason::kGetLiveFiles);
93 mutex_.Lock();
94 } else {
95 for (auto cfd : *versions_->GetColumnFamilySet()) {
96 if (cfd->IsDropped()) {
97 continue;
98 }
99 cfd->Ref();
100 mutex_.Unlock();
101 status = FlushMemTable(cfd, FlushOptions(), FlushReason::kGetLiveFiles);
102 TEST_SYNC_POINT("DBImpl::GetLiveFiles:1");
103 TEST_SYNC_POINT("DBImpl::GetLiveFiles:2");
104 mutex_.Lock();
105 cfd->UnrefAndTryDelete();
106 if (!status.ok()) {
107 break;
108 }
109 }
110 }
111 versions_->GetColumnFamilySet()->FreeDeadColumnFamilies();
112
113 if (!status.ok()) {
114 mutex_.Unlock();
115 ROCKS_LOG_ERROR(immutable_db_options_.info_log, "Cannot Flush data %s\n",
116 status.ToString().c_str());
117 return status;
118 }
119 }
120
121 // Make a set of all of the live *.sst files
122 std::vector<FileDescriptor> live;
123 for (auto cfd : *versions_->GetColumnFamilySet()) {
124 if (cfd->IsDropped()) {
125 continue;
126 }
127 cfd->current()->AddLiveFiles(&live);
128 }
129
130 ret.clear();
131 ret.reserve(live.size() + 3); // *.sst + CURRENT + MANIFEST + OPTIONS
132
133 // create names of the live files. The names are not absolute
134 // paths, instead they are relative to dbname_;
135 for (const auto& live_file : live) {
136 ret.push_back(MakeTableFileName("", live_file.GetNumber()));
137 }
138
139 ret.push_back(CurrentFileName(""));
140 ret.push_back(DescriptorFileName("", versions_->manifest_file_number()));
141 ret.push_back(OptionsFileName("", versions_->options_file_number()));
142
143 // find length of manifest file while holding the mutex lock
144 *manifest_file_size = versions_->manifest_file_size();
145
146 mutex_.Unlock();
147 return Status::OK();
148 }
149
GetSortedWalFiles(VectorLogPtr & files)150 Status DBImpl::GetSortedWalFiles(VectorLogPtr& files) {
151 {
152 // If caller disabled deletions, this function should return files that are
153 // guaranteed not to be deleted until deletions are re-enabled. We need to
154 // wait for pending purges to finish since WalManager doesn't know which
155 // files are going to be purged. Additional purges won't be scheduled as
156 // long as deletions are disabled (so the below loop must terminate).
157 InstrumentedMutexLock l(&mutex_);
158 while (disable_delete_obsolete_files_ > 0 &&
159 pending_purge_obsolete_files_ > 0) {
160 bg_cv_.Wait();
161 }
162 }
163 return wal_manager_.GetSortedWalFiles(files);
164 }
165
GetCurrentWalFile(std::unique_ptr<LogFile> * current_log_file)166 Status DBImpl::GetCurrentWalFile(std::unique_ptr<LogFile>* current_log_file) {
167 uint64_t current_logfile_number;
168 {
169 InstrumentedMutexLock l(&mutex_);
170 current_logfile_number = logfile_number_;
171 }
172
173 return wal_manager_.GetLiveWalFile(current_logfile_number, current_log_file);
174 }
175 } // namespace ROCKSDB_NAMESPACE
176
177 #endif // ROCKSDB_LITE
178