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 #ifndef ROCKSDB_LITE
7 
8 #include "utilities/blob_db/blob_db_impl.h"
9 
10 #include "file/filename.h"
11 #include "logging/logging.h"
12 #include "util/mutexlock.h"
13 
14 // BlobDBImpl methods to get snapshot of files, e.g. for replication.
15 
16 namespace ROCKSDB_NAMESPACE {
17 namespace blob_db {
18 
DisableFileDeletions()19 Status BlobDBImpl::DisableFileDeletions() {
20   // Disable base DB file deletions.
21   Status s = db_impl_->DisableFileDeletions();
22   if (!s.ok()) {
23     return s;
24   }
25 
26   int count = 0;
27   {
28     // Hold delete_file_mutex_ to make sure no DeleteObsoleteFiles job
29     // is running.
30     MutexLock l(&delete_file_mutex_);
31     count = ++disable_file_deletions_;
32   }
33 
34   ROCKS_LOG_INFO(db_options_.info_log,
35                  "Disalbed blob file deletions. count: %d", count);
36   return Status::OK();
37 }
38 
EnableFileDeletions(bool force)39 Status BlobDBImpl::EnableFileDeletions(bool force) {
40   // Enable base DB file deletions.
41   Status s = db_impl_->EnableFileDeletions(force);
42   if (!s.ok()) {
43     return s;
44   }
45 
46   int count = 0;
47   {
48     MutexLock l(&delete_file_mutex_);
49     if (force) {
50       disable_file_deletions_ = 0;
51     } else if (disable_file_deletions_ > 0) {
52       count = --disable_file_deletions_;
53     }
54     assert(count >= 0);
55   }
56 
57   ROCKS_LOG_INFO(db_options_.info_log, "Enabled blob file deletions. count: %d",
58                  count);
59   // Consider trigger DeleteobsoleteFiles once after re-enabled, if we are to
60   // make DeleteobsoleteFiles re-run interval configuration.
61   return Status::OK();
62 }
63 
GetLiveFiles(std::vector<std::string> & ret,uint64_t * manifest_file_size,bool flush_memtable)64 Status BlobDBImpl::GetLiveFiles(std::vector<std::string>& ret,
65                                 uint64_t* manifest_file_size,
66                                 bool flush_memtable) {
67   if (!bdb_options_.path_relative) {
68     return Status::NotSupported(
69         "Not able to get relative blob file path from absolute blob_dir.");
70   }
71   // Hold a lock in the beginning to avoid updates to base DB during the call
72   ReadLock rl(&mutex_);
73   Status s = db_->GetLiveFiles(ret, manifest_file_size, flush_memtable);
74   if (!s.ok()) {
75     return s;
76   }
77   ret.reserve(ret.size() + blob_files_.size());
78   for (auto bfile_pair : blob_files_) {
79     auto blob_file = bfile_pair.second;
80     // Path should be relative to db_name, but begin with slash.
81     ret.emplace_back(
82         BlobFileName("", bdb_options_.blob_dir, blob_file->BlobFileNumber()));
83   }
84   return Status::OK();
85 }
86 
GetLiveFilesMetaData(std::vector<LiveFileMetaData> * metadata)87 void BlobDBImpl::GetLiveFilesMetaData(std::vector<LiveFileMetaData>* metadata) {
88   // Path should be relative to db_name.
89   assert(bdb_options_.path_relative);
90   // Hold a lock in the beginning to avoid updates to base DB during the call
91   ReadLock rl(&mutex_);
92   db_->GetLiveFilesMetaData(metadata);
93   for (auto bfile_pair : blob_files_) {
94     auto blob_file = bfile_pair.second;
95     LiveFileMetaData filemetadata;
96     filemetadata.size = static_cast<size_t>(blob_file->GetFileSize());
97     const uint64_t file_number = blob_file->BlobFileNumber();
98     // Path should be relative to db_name, but begin with slash.
99     filemetadata.name = BlobFileName("", bdb_options_.blob_dir, file_number);
100     filemetadata.file_number = file_number;
101     auto cfh = reinterpret_cast<ColumnFamilyHandleImpl*>(DefaultColumnFamily());
102     filemetadata.column_family_name = cfh->GetName();
103     metadata->emplace_back(filemetadata);
104   }
105 }
106 
107 }  // namespace blob_db
108 }  // namespace ROCKSDB_NAMESPACE
109 #endif  // !ROCKSDB_LITE
110