blob_db_impl_filesnapshot.cc 4.1 KB

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