error_handler.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright (c) 2018-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. #pragma once
  6. #include <sstream>
  7. #include "monitoring/instrumented_mutex.h"
  8. #include "options/db_options.h"
  9. #include "rocksdb/io_status.h"
  10. #include "rocksdb/listener.h"
  11. #include "rocksdb/status.h"
  12. #include "util/autovector.h"
  13. namespace ROCKSDB_NAMESPACE {
  14. class DBImpl;
  15. // This structure is used to store the DB recovery context. The context is
  16. // the information that related to the recover actions. For example, it contains
  17. // FlushReason, which tells the flush job why this flush is called.
  18. struct DBRecoverContext {
  19. FlushReason flush_reason;
  20. bool flush_after_recovery;
  21. DBRecoverContext()
  22. : flush_reason(FlushReason::kErrorRecovery),
  23. flush_after_recovery(false) {}
  24. DBRecoverContext(FlushReason reason)
  25. : flush_reason(reason), flush_after_recovery(false) {}
  26. };
  27. class ErrorHandler {
  28. public:
  29. ErrorHandler(DBImpl* db, const ImmutableDBOptions& db_options,
  30. InstrumentedMutex* db_mutex)
  31. : db_(db),
  32. db_options_(db_options),
  33. cv_(db_mutex),
  34. end_recovery_(false),
  35. recovery_thread_(nullptr),
  36. db_mutex_(db_mutex),
  37. auto_recovery_(false),
  38. recovery_in_prog_(false),
  39. soft_error_no_bg_work_(false),
  40. allow_db_shutdown_(true),
  41. is_db_stopped_(false),
  42. bg_error_stats_(db_options.statistics) {
  43. // Clear the checked flag for uninitialized errors
  44. bg_error_.PermitUncheckedError();
  45. recovery_error_.PermitUncheckedError();
  46. }
  47. void EnableAutoRecovery() { auto_recovery_ = true; }
  48. Status::Severity GetErrorSeverity(BackgroundErrorReason reason,
  49. Status::Code code, Status::SubCode subcode);
  50. void SetBGError(const Status& bg_err, BackgroundErrorReason reason,
  51. bool wal_related = false);
  52. Status GetBGError() const { return bg_error_; }
  53. Status GetRecoveryError() const { return recovery_error_; }
  54. // REQUIREs: db mutex held
  55. //
  56. // Returns non-OK status if encountered error during recovery.
  57. // Returns OK if bg error is successfully cleared. May releases and
  58. // re-acquire db mutex to notify listeners. However, DB close (if initiated)
  59. // will be blocked until db mutex is released after return.
  60. Status ClearBGError();
  61. bool IsDBStopped() { return is_db_stopped_.load(std::memory_order_acquire); }
  62. bool IsBGWorkStopped() {
  63. assert(db_mutex_);
  64. db_mutex_->AssertHeld();
  65. return !bg_error_.ok() &&
  66. (bg_error_.severity() >= Status::Severity::kHardError ||
  67. !auto_recovery_ || soft_error_no_bg_work_);
  68. }
  69. bool IsSoftErrorNoBGWork() { return soft_error_no_bg_work_; }
  70. bool IsRecoveryInProgress() { return recovery_in_prog_; }
  71. // REQUIRES: db mutex held
  72. bool ReadyForShutdown() {
  73. db_mutex_->AssertHeld();
  74. return !recovery_in_prog_ && allow_db_shutdown_;
  75. }
  76. Status RecoverFromBGError(bool is_manual = false);
  77. void CancelErrorRecoveryForShutDown();
  78. void EndAutoRecovery();
  79. void AddFilesToQuarantine(
  80. autovector<const autovector<uint64_t>*> files_to_quarantine);
  81. const autovector<uint64_t>& GetFilesToQuarantine() const {
  82. db_mutex_->AssertHeld();
  83. return files_to_quarantine_;
  84. }
  85. void ClearFilesToQuarantine();
  86. private:
  87. void RecordStats(
  88. const std::vector<Tickers>& ticker_types,
  89. const std::vector<std::tuple<Histograms, uint64_t>>& int_histograms);
  90. DBImpl* db_;
  91. const ImmutableDBOptions& db_options_;
  92. Status bg_error_;
  93. // A separate Status variable used to record any errors during the
  94. // recovery process from hard errors
  95. IOStatus recovery_error_;
  96. // The condition variable used with db_mutex during auto resume for time
  97. // wait.
  98. InstrumentedCondVar cv_;
  99. bool end_recovery_;
  100. std::unique_ptr<port::Thread> recovery_thread_;
  101. InstrumentedMutex* db_mutex_;
  102. // A flag indicating whether automatic recovery from errors is enabled. Auto
  103. // recovery applies for delegating to SstFileManager to handle no space type
  104. // of errors. This flag doesn't control the auto resume behavior to recover
  105. // from retryable IO errors.
  106. bool auto_recovery_;
  107. bool recovery_in_prog_;
  108. // A flag to indicate that for the soft error, we should not allow any
  109. // background work except the work is from recovery.
  110. bool soft_error_no_bg_work_;
  111. // Used in ClearBGError() to prevent DB from being closed.
  112. bool allow_db_shutdown_;
  113. // Used to store the context for recover, such as flush reason.
  114. DBRecoverContext recover_context_;
  115. std::atomic<bool> is_db_stopped_;
  116. // The pointer of DB statistics.
  117. std::shared_ptr<Statistics> bg_error_stats_;
  118. // During recovery from manifest IO errors, files whose VersionEdits entries
  119. // could be in an ambiguous state are quarantined and file deletion refrain
  120. // from deleting them. Successful recovery will clear this vector. Files are
  121. // added to this vector while DB mutex was locked, this data structure is
  122. // unsorted.
  123. autovector<uint64_t> files_to_quarantine_;
  124. void HandleKnownErrors(const Status& bg_err, BackgroundErrorReason reason);
  125. Status OverrideNoSpaceError(const Status& bg_error, bool* auto_recovery);
  126. void RecoverFromNoSpace();
  127. void StartRecoverFromRetryableBGIOError(const IOStatus& io_error);
  128. void RecoverFromRetryableBGIOError();
  129. // First, if it is in recovery and the recovery_error is ok. Set the
  130. // recovery_error_ to bg_err. Second, if the severity is higher than the
  131. // current bg_error_, overwrite it.
  132. void CheckAndSetRecoveryAndBGError(const Status& bg_err);
  133. };
  134. } // namespace ROCKSDB_NAMESPACE