error_handler.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 "monitoring/instrumented_mutex.h"
  7. #include "options/db_options.h"
  8. #include "rocksdb/listener.h"
  9. #include "rocksdb/status.h"
  10. namespace ROCKSDB_NAMESPACE {
  11. class DBImpl;
  12. class ErrorHandler {
  13. public:
  14. ErrorHandler(DBImpl* db, const ImmutableDBOptions& db_options,
  15. InstrumentedMutex* db_mutex)
  16. : db_(db),
  17. db_options_(db_options),
  18. bg_error_(Status::OK()),
  19. recovery_error_(Status::OK()),
  20. db_mutex_(db_mutex),
  21. auto_recovery_(false),
  22. recovery_in_prog_(false) {}
  23. ~ErrorHandler() {}
  24. void EnableAutoRecovery() { auto_recovery_ = true; }
  25. Status::Severity GetErrorSeverity(BackgroundErrorReason reason,
  26. Status::Code code,
  27. Status::SubCode subcode);
  28. Status SetBGError(const Status& bg_err, BackgroundErrorReason reason);
  29. Status GetBGError() { return bg_error_; }
  30. Status GetRecoveryError() { return recovery_error_; }
  31. Status ClearBGError();
  32. bool IsDBStopped() {
  33. return !bg_error_.ok() &&
  34. bg_error_.severity() >= Status::Severity::kHardError;
  35. }
  36. bool IsBGWorkStopped() {
  37. return !bg_error_.ok() &&
  38. (bg_error_.severity() >= Status::Severity::kHardError ||
  39. !auto_recovery_);
  40. }
  41. bool IsRecoveryInProgress() { return recovery_in_prog_; }
  42. Status RecoverFromBGError(bool is_manual = false);
  43. void CancelErrorRecovery();
  44. private:
  45. DBImpl* db_;
  46. const ImmutableDBOptions& db_options_;
  47. Status bg_error_;
  48. // A separate Status variable used to record any errors during the
  49. // recovery process from hard errors
  50. Status recovery_error_;
  51. InstrumentedMutex* db_mutex_;
  52. // A flag indicating whether automatic recovery from errors is enabled
  53. bool auto_recovery_;
  54. bool recovery_in_prog_;
  55. Status OverrideNoSpaceError(Status bg_error, bool* auto_recovery);
  56. void RecoverFromNoSpace();
  57. };
  58. } // namespace ROCKSDB_NAMESPACE