]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
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 | ||
7 | #include "monitoring/instrumented_mutex.h" | |
8 | #include "options/db_options.h" | |
20effc67 | 9 | #include "rocksdb/io_status.h" |
11fdf7f2 TL |
10 | #include "rocksdb/listener.h" |
11 | #include "rocksdb/status.h" | |
12 | ||
f67539c2 | 13 | namespace ROCKSDB_NAMESPACE { |
11fdf7f2 TL |
14 | |
15 | class DBImpl; | |
16 | ||
20effc67 TL |
17 | // This structure is used to store the DB recovery context. The context is |
18 | // the information that related to the recover actions. For example, it contains | |
19 | // FlushReason, which tells the flush job why this flush is called. | |
20 | struct DBRecoverContext { | |
21 | FlushReason flush_reason; | |
22 | ||
23 | DBRecoverContext() : flush_reason(FlushReason::kErrorRecovery) {} | |
24 | ||
25 | DBRecoverContext(FlushReason reason) : flush_reason(reason) {} | |
26 | }; | |
27 | ||
11fdf7f2 TL |
28 | class ErrorHandler { |
29 | public: | |
30 | ErrorHandler(DBImpl* db, const ImmutableDBOptions& db_options, | |
31 | InstrumentedMutex* db_mutex) | |
32 | : db_(db), | |
33 | db_options_(db_options), | |
34 | bg_error_(Status::OK()), | |
35 | recovery_error_(Status::OK()), | |
20effc67 TL |
36 | recovery_io_error_(IOStatus::OK()), |
37 | cv_(db_mutex), | |
38 | end_recovery_(false), | |
39 | recovery_thread_(nullptr), | |
11fdf7f2 TL |
40 | db_mutex_(db_mutex), |
41 | auto_recovery_(false), | |
20effc67 TL |
42 | recovery_in_prog_(false), |
43 | soft_error_no_bg_work_(false) {} | |
44 | ~ErrorHandler() { | |
45 | bg_error_.PermitUncheckedError(); | |
46 | recovery_error_.PermitUncheckedError(); | |
47 | recovery_io_error_.PermitUncheckedError(); | |
48 | } | |
11fdf7f2 TL |
49 | |
50 | void EnableAutoRecovery() { auto_recovery_ = true; } | |
51 | ||
52 | Status::Severity GetErrorSeverity(BackgroundErrorReason reason, | |
53 | Status::Code code, | |
54 | Status::SubCode subcode); | |
55 | ||
56 | Status SetBGError(const Status& bg_err, BackgroundErrorReason reason); | |
57 | ||
20effc67 TL |
58 | Status SetBGError(const IOStatus& bg_io_err, BackgroundErrorReason reason); |
59 | ||
11fdf7f2 TL |
60 | Status GetBGError() { return bg_error_; } |
61 | ||
62 | Status GetRecoveryError() { return recovery_error_; } | |
63 | ||
64 | Status ClearBGError(); | |
65 | ||
66 | bool IsDBStopped() { | |
67 | return !bg_error_.ok() && | |
68 | bg_error_.severity() >= Status::Severity::kHardError; | |
69 | } | |
70 | ||
71 | bool IsBGWorkStopped() { | |
72 | return !bg_error_.ok() && | |
73 | (bg_error_.severity() >= Status::Severity::kHardError || | |
20effc67 | 74 | !auto_recovery_ || soft_error_no_bg_work_); |
11fdf7f2 TL |
75 | } |
76 | ||
20effc67 TL |
77 | bool IsSoftErrorNoBGWork() { return soft_error_no_bg_work_; } |
78 | ||
11fdf7f2 TL |
79 | bool IsRecoveryInProgress() { return recovery_in_prog_; } |
80 | ||
81 | Status RecoverFromBGError(bool is_manual = false); | |
82 | void CancelErrorRecovery(); | |
83 | ||
20effc67 TL |
84 | void EndAutoRecovery(); |
85 | ||
11fdf7f2 TL |
86 | private: |
87 | DBImpl* db_; | |
88 | const ImmutableDBOptions& db_options_; | |
89 | Status bg_error_; | |
494da23a | 90 | // A separate Status variable used to record any errors during the |
11fdf7f2 TL |
91 | // recovery process from hard errors |
92 | Status recovery_error_; | |
20effc67 TL |
93 | // A separate IO Status variable used to record any IO errors during |
94 | // the recovery process. At the same time, recovery_error_ is also set. | |
95 | IOStatus recovery_io_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 | ||
11fdf7f2 TL |
102 | InstrumentedMutex* db_mutex_; |
103 | // A flag indicating whether automatic recovery from errors is enabled | |
104 | bool auto_recovery_; | |
105 | bool recovery_in_prog_; | |
20effc67 TL |
106 | // A flag to indicate that for the soft error, we should not allow any |
107 | // backrgound work execpt the work is from recovery. | |
108 | bool soft_error_no_bg_work_; | |
109 | ||
110 | // Used to store the context for recover, such as flush reason. | |
111 | DBRecoverContext recover_context_; | |
11fdf7f2 TL |
112 | |
113 | Status OverrideNoSpaceError(Status bg_error, bool* auto_recovery); | |
114 | void RecoverFromNoSpace(); | |
20effc67 TL |
115 | Status StartRecoverFromRetryableBGIOError(IOStatus io_error); |
116 | void RecoverFromRetryableBGIOError(); | |
11fdf7f2 TL |
117 | }; |
118 | ||
f67539c2 | 119 | } // namespace ROCKSDB_NAMESPACE |