error_handler.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. //
  6. #include "db/error_handler.h"
  7. #include "db/db_impl/db_impl.h"
  8. #include "db/event_helpers.h"
  9. #include "file/sst_file_manager_impl.h"
  10. namespace ROCKSDB_NAMESPACE {
  11. // Maps to help decide the severity of an error based on the
  12. // BackgroundErrorReason, Code, SubCode and whether db_options.paranoid_checks
  13. // is set or not. There are 3 maps, going from most specific to least specific
  14. // (i.e from all 4 fields in a tuple to only the BackgroundErrorReason and
  15. // paranoid_checks). The less specific map serves as a catch all in case we miss
  16. // a specific error code or subcode.
  17. std::map<std::tuple<BackgroundErrorReason, Status::Code, Status::SubCode, bool>,
  18. Status::Severity>
  19. ErrorSeverityMap = {
  20. // Errors during BG compaction
  21. {std::make_tuple(BackgroundErrorReason::kCompaction,
  22. Status::Code::kIOError, Status::SubCode::kNoSpace,
  23. true),
  24. Status::Severity::kSoftError},
  25. {std::make_tuple(BackgroundErrorReason::kCompaction,
  26. Status::Code::kIOError, Status::SubCode::kNoSpace,
  27. false),
  28. Status::Severity::kNoError},
  29. {std::make_tuple(BackgroundErrorReason::kCompaction,
  30. Status::Code::kIOError, Status::SubCode::kSpaceLimit,
  31. true),
  32. Status::Severity::kHardError},
  33. // Errors during BG flush
  34. {std::make_tuple(BackgroundErrorReason::kFlush, Status::Code::kIOError,
  35. Status::SubCode::kNoSpace, true),
  36. Status::Severity::kHardError},
  37. {std::make_tuple(BackgroundErrorReason::kFlush, Status::Code::kIOError,
  38. Status::SubCode::kNoSpace, false),
  39. Status::Severity::kNoError},
  40. {std::make_tuple(BackgroundErrorReason::kFlush, Status::Code::kIOError,
  41. Status::SubCode::kSpaceLimit, true),
  42. Status::Severity::kHardError},
  43. // Errors during Write
  44. {std::make_tuple(BackgroundErrorReason::kWriteCallback,
  45. Status::Code::kIOError, Status::SubCode::kNoSpace,
  46. true),
  47. Status::Severity::kHardError},
  48. {std::make_tuple(BackgroundErrorReason::kWriteCallback,
  49. Status::Code::kIOError, Status::SubCode::kNoSpace,
  50. false),
  51. Status::Severity::kHardError},
  52. };
  53. std::map<std::tuple<BackgroundErrorReason, Status::Code, bool>, Status::Severity>
  54. DefaultErrorSeverityMap = {
  55. // Errors during BG compaction
  56. {std::make_tuple(BackgroundErrorReason::kCompaction,
  57. Status::Code::kCorruption, true),
  58. Status::Severity::kUnrecoverableError},
  59. {std::make_tuple(BackgroundErrorReason::kCompaction,
  60. Status::Code::kCorruption, false),
  61. Status::Severity::kNoError},
  62. {std::make_tuple(BackgroundErrorReason::kCompaction,
  63. Status::Code::kIOError, true),
  64. Status::Severity::kFatalError},
  65. {std::make_tuple(BackgroundErrorReason::kCompaction,
  66. Status::Code::kIOError, false),
  67. Status::Severity::kNoError},
  68. // Errors during BG flush
  69. {std::make_tuple(BackgroundErrorReason::kFlush,
  70. Status::Code::kCorruption, true),
  71. Status::Severity::kUnrecoverableError},
  72. {std::make_tuple(BackgroundErrorReason::kFlush,
  73. Status::Code::kCorruption, false),
  74. Status::Severity::kNoError},
  75. {std::make_tuple(BackgroundErrorReason::kFlush,
  76. Status::Code::kIOError, true),
  77. Status::Severity::kFatalError},
  78. {std::make_tuple(BackgroundErrorReason::kFlush,
  79. Status::Code::kIOError, false),
  80. Status::Severity::kNoError},
  81. // Errors during Write
  82. {std::make_tuple(BackgroundErrorReason::kWriteCallback,
  83. Status::Code::kCorruption, true),
  84. Status::Severity::kUnrecoverableError},
  85. {std::make_tuple(BackgroundErrorReason::kWriteCallback,
  86. Status::Code::kCorruption, false),
  87. Status::Severity::kNoError},
  88. {std::make_tuple(BackgroundErrorReason::kWriteCallback,
  89. Status::Code::kIOError, true),
  90. Status::Severity::kFatalError},
  91. {std::make_tuple(BackgroundErrorReason::kWriteCallback,
  92. Status::Code::kIOError, false),
  93. Status::Severity::kNoError},
  94. };
  95. std::map<std::tuple<BackgroundErrorReason, bool>, Status::Severity>
  96. DefaultReasonMap = {
  97. // Errors during BG compaction
  98. {std::make_tuple(BackgroundErrorReason::kCompaction, true),
  99. Status::Severity::kFatalError},
  100. {std::make_tuple(BackgroundErrorReason::kCompaction, false),
  101. Status::Severity::kNoError},
  102. // Errors during BG flush
  103. {std::make_tuple(BackgroundErrorReason::kFlush, true),
  104. Status::Severity::kFatalError},
  105. {std::make_tuple(BackgroundErrorReason::kFlush, false),
  106. Status::Severity::kNoError},
  107. // Errors during Write
  108. {std::make_tuple(BackgroundErrorReason::kWriteCallback, true),
  109. Status::Severity::kFatalError},
  110. {std::make_tuple(BackgroundErrorReason::kWriteCallback, false),
  111. Status::Severity::kFatalError},
  112. // Errors during Memtable update
  113. {std::make_tuple(BackgroundErrorReason::kMemTable, true),
  114. Status::Severity::kFatalError},
  115. {std::make_tuple(BackgroundErrorReason::kMemTable, false),
  116. Status::Severity::kFatalError},
  117. };
  118. void ErrorHandler::CancelErrorRecovery() {
  119. #ifndef ROCKSDB_LITE
  120. db_mutex_->AssertHeld();
  121. // We'll release the lock before calling sfm, so make sure no new
  122. // recovery gets scheduled at that point
  123. auto_recovery_ = false;
  124. SstFileManagerImpl* sfm = reinterpret_cast<SstFileManagerImpl*>(
  125. db_options_.sst_file_manager.get());
  126. if (sfm) {
  127. // This may or may not cancel a pending recovery
  128. db_mutex_->Unlock();
  129. bool cancelled = sfm->CancelErrorRecovery(this);
  130. db_mutex_->Lock();
  131. if (cancelled) {
  132. recovery_in_prog_ = false;
  133. }
  134. }
  135. #endif
  136. }
  137. // This is the main function for looking at an error during a background
  138. // operation and deciding the severity, and error recovery strategy. The high
  139. // level algorithm is as follows -
  140. // 1. Classify the severity of the error based on the ErrorSeverityMap,
  141. // DefaultErrorSeverityMap and DefaultReasonMap defined earlier
  142. // 2. Call a Status code specific override function to adjust the severity
  143. // if needed. The reason for this is our ability to recover may depend on
  144. // the exact options enabled in DBOptions
  145. // 3. Determine if auto recovery is possible. A listener notification callback
  146. // is called, which can disable the auto recovery even if we decide its
  147. // feasible
  148. // 4. For Status::NoSpace() errors, rely on SstFileManagerImpl to control
  149. // the actual recovery. If no sst file manager is specified in DBOptions,
  150. // a default one is allocated during DB::Open(), so there will always be
  151. // one.
  152. // This can also get called as part of a recovery operation. In that case, we
  153. // also track the error separately in recovery_error_ so we can tell in the
  154. // end whether recovery succeeded or not
  155. Status ErrorHandler::SetBGError(const Status& bg_err, BackgroundErrorReason reason) {
  156. db_mutex_->AssertHeld();
  157. if (bg_err.ok()) {
  158. return Status::OK();
  159. }
  160. bool paranoid = db_options_.paranoid_checks;
  161. Status::Severity sev = Status::Severity::kFatalError;
  162. Status new_bg_err;
  163. bool found = false;
  164. {
  165. auto entry = ErrorSeverityMap.find(std::make_tuple(reason, bg_err.code(),
  166. bg_err.subcode(), paranoid));
  167. if (entry != ErrorSeverityMap.end()) {
  168. sev = entry->second;
  169. found = true;
  170. }
  171. }
  172. if (!found) {
  173. auto entry = DefaultErrorSeverityMap.find(std::make_tuple(reason,
  174. bg_err.code(), paranoid));
  175. if (entry != DefaultErrorSeverityMap.end()) {
  176. sev = entry->second;
  177. found = true;
  178. }
  179. }
  180. if (!found) {
  181. auto entry = DefaultReasonMap.find(std::make_tuple(reason, paranoid));
  182. if (entry != DefaultReasonMap.end()) {
  183. sev = entry->second;
  184. }
  185. }
  186. new_bg_err = Status(bg_err, sev);
  187. // Check if recovery is currently in progress. If it is, we will save this
  188. // error so we can check it at the end to see if recovery succeeded or not
  189. if (recovery_in_prog_ && recovery_error_.ok()) {
  190. recovery_error_ = new_bg_err;
  191. }
  192. bool auto_recovery = auto_recovery_;
  193. if (new_bg_err.severity() >= Status::Severity::kFatalError && auto_recovery) {
  194. auto_recovery = false;
  195. }
  196. // Allow some error specific overrides
  197. if (new_bg_err == Status::NoSpace()) {
  198. new_bg_err = OverrideNoSpaceError(new_bg_err, &auto_recovery);
  199. }
  200. if (!new_bg_err.ok()) {
  201. Status s = new_bg_err;
  202. EventHelpers::NotifyOnBackgroundError(db_options_.listeners, reason, &s,
  203. db_mutex_, &auto_recovery);
  204. if (!s.ok() && (s.severity() > bg_error_.severity())) {
  205. bg_error_ = s;
  206. } else {
  207. // This error is less severe than previously encountered error. Don't
  208. // take any further action
  209. return bg_error_;
  210. }
  211. }
  212. if (auto_recovery) {
  213. recovery_in_prog_ = true;
  214. // Kick-off error specific recovery
  215. if (bg_error_ == Status::NoSpace()) {
  216. RecoverFromNoSpace();
  217. }
  218. }
  219. return bg_error_;
  220. }
  221. Status ErrorHandler::OverrideNoSpaceError(Status bg_error,
  222. bool* auto_recovery) {
  223. #ifndef ROCKSDB_LITE
  224. if (bg_error.severity() >= Status::Severity::kFatalError) {
  225. return bg_error;
  226. }
  227. if (db_options_.sst_file_manager.get() == nullptr) {
  228. // We rely on SFM to poll for enough disk space and recover
  229. *auto_recovery = false;
  230. return bg_error;
  231. }
  232. if (db_options_.allow_2pc &&
  233. (bg_error.severity() <= Status::Severity::kSoftError)) {
  234. // Don't know how to recover, as the contents of the current WAL file may
  235. // be inconsistent, and it may be needed for 2PC. If 2PC is not enabled,
  236. // we can just flush the memtable and discard the log
  237. *auto_recovery = false;
  238. return Status(bg_error, Status::Severity::kFatalError);
  239. }
  240. {
  241. uint64_t free_space;
  242. if (db_options_.env->GetFreeSpace(db_options_.db_paths[0].path,
  243. &free_space) == Status::NotSupported()) {
  244. *auto_recovery = false;
  245. }
  246. }
  247. return bg_error;
  248. #else
  249. (void)auto_recovery;
  250. return Status(bg_error, Status::Severity::kFatalError);
  251. #endif
  252. }
  253. void ErrorHandler::RecoverFromNoSpace() {
  254. #ifndef ROCKSDB_LITE
  255. SstFileManagerImpl* sfm =
  256. reinterpret_cast<SstFileManagerImpl*>(db_options_.sst_file_manager.get());
  257. // Inform SFM of the error, so it can kick-off the recovery
  258. if (sfm) {
  259. sfm->StartErrorRecovery(this, bg_error_);
  260. }
  261. #endif
  262. }
  263. Status ErrorHandler::ClearBGError() {
  264. #ifndef ROCKSDB_LITE
  265. db_mutex_->AssertHeld();
  266. // Signal that recovery succeeded
  267. if (recovery_error_.ok()) {
  268. Status old_bg_error = bg_error_;
  269. bg_error_ = Status::OK();
  270. recovery_in_prog_ = false;
  271. EventHelpers::NotifyOnErrorRecoveryCompleted(db_options_.listeners,
  272. old_bg_error, db_mutex_);
  273. }
  274. return recovery_error_;
  275. #else
  276. return bg_error_;
  277. #endif
  278. }
  279. Status ErrorHandler::RecoverFromBGError(bool is_manual) {
  280. #ifndef ROCKSDB_LITE
  281. InstrumentedMutexLock l(db_mutex_);
  282. if (is_manual) {
  283. // If its a manual recovery and there's a background recovery in progress
  284. // return busy status
  285. if (recovery_in_prog_) {
  286. return Status::Busy();
  287. }
  288. recovery_in_prog_ = true;
  289. }
  290. if (bg_error_.severity() == Status::Severity::kSoftError) {
  291. // Simply clear the background error and return
  292. recovery_error_ = Status::OK();
  293. return ClearBGError();
  294. }
  295. // Reset recovery_error_. We will use this to record any errors that happen
  296. // during the recovery process. While recovering, the only operations that
  297. // can generate background errors should be the flush operations
  298. recovery_error_ = Status::OK();
  299. Status s = db_->ResumeImpl();
  300. // For manual recover, shutdown, and fatal error cases, set
  301. // recovery_in_prog_ to false. For automatic background recovery, leave it
  302. // as is regardless of success or failure as it will be retried
  303. if (is_manual || s.IsShutdownInProgress() ||
  304. bg_error_.severity() >= Status::Severity::kFatalError) {
  305. recovery_in_prog_ = false;
  306. }
  307. return s;
  308. #else
  309. (void)is_manual;
  310. return bg_error_;
  311. #endif
  312. }
  313. } // namespace ROCKSDB_NAMESPACE