blob_file_completion_callback.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. // 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.
  9. #pragma once
  10. #include "db/error_handler.h"
  11. #include "db/event_helpers.h"
  12. #include "file/sst_file_manager_impl.h"
  13. #include "rocksdb/status.h"
  14. namespace ROCKSDB_NAMESPACE {
  15. class BlobFileCompletionCallback {
  16. public:
  17. BlobFileCompletionCallback(
  18. SstFileManager* sst_file_manager, InstrumentedMutex* mutex,
  19. ErrorHandler* error_handler, EventLogger* event_logger,
  20. const std::vector<std::shared_ptr<EventListener>>& listeners,
  21. const std::string& dbname)
  22. : event_logger_(event_logger), listeners_(listeners), dbname_(dbname) {
  23. sst_file_manager_ = sst_file_manager;
  24. mutex_ = mutex;
  25. error_handler_ = error_handler;
  26. }
  27. void OnBlobFileCreationStarted(const std::string& file_name,
  28. const std::string& column_family_name,
  29. int job_id,
  30. BlobFileCreationReason creation_reason) {
  31. // Notify the listeners.
  32. EventHelpers::NotifyBlobFileCreationStarted(listeners_, dbname_,
  33. column_family_name, file_name,
  34. job_id, creation_reason);
  35. }
  36. Status OnBlobFileCompleted(const std::string& file_name,
  37. const std::string& column_family_name, int job_id,
  38. uint64_t file_number,
  39. BlobFileCreationReason creation_reason,
  40. const Status& report_status,
  41. const std::string& checksum_value,
  42. const std::string& checksum_method,
  43. uint64_t blob_count, uint64_t blob_bytes) {
  44. Status s;
  45. auto sfm = static_cast<SstFileManagerImpl*>(sst_file_manager_);
  46. if (sfm) {
  47. // Report new blob files to SstFileManagerImpl
  48. s = sfm->OnAddFile(file_name);
  49. if (sfm->IsMaxAllowedSpaceReached()) {
  50. s = Status::SpaceLimit("Max allowed space was reached");
  51. TEST_SYNC_POINT(
  52. "BlobFileCompletionCallback::CallBack::MaxAllowedSpaceReached");
  53. InstrumentedMutexLock l(mutex_);
  54. error_handler_->SetBGError(s, BackgroundErrorReason::kFlush);
  55. }
  56. }
  57. // Notify the listeners.
  58. EventHelpers::LogAndNotifyBlobFileCreationFinished(
  59. event_logger_, listeners_, dbname_, column_family_name, file_name,
  60. job_id, file_number, creation_reason,
  61. (!report_status.ok() ? report_status : s),
  62. (checksum_value.empty() ? kUnknownFileChecksum : checksum_value),
  63. (checksum_method.empty() ? kUnknownFileChecksumFuncName
  64. : checksum_method),
  65. blob_count, blob_bytes);
  66. return s;
  67. }
  68. private:
  69. SstFileManager* sst_file_manager_;
  70. InstrumentedMutex* mutex_;
  71. ErrorHandler* error_handler_;
  72. EventLogger* event_logger_;
  73. std::vector<std::shared_ptr<EventListener>> listeners_;
  74. std::string dbname_;
  75. };
  76. } // namespace ROCKSDB_NAMESPACE