blob_db_impl_filesnapshot.cc 3.5 KB

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