You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

396 line
12 KiB

  1. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. //
  5. // We recover the contents of the descriptor from the other files we find.
  6. // (1) Any log files are first converted to tables
  7. // (2) We scan every table to compute
  8. // (a) smallest/largest for the table
  9. // (b) large value refs from the table
  10. // (c) largest sequence number in the table
  11. // (3) We generate descriptor contents:
  12. // - log number is set to zero
  13. // - next-file-number is set to 1 + largest file number we found
  14. // - last-sequence-number is set to largest sequence# found across
  15. // all tables (see 2c)
  16. // - compaction pointers are cleared
  17. // - every table file is added at level 0
  18. //
  19. // Possible optimization 1:
  20. // (a) Compute total size and use to pick appropriate max-level M
  21. // (b) Sort tables by largest sequence# in the table
  22. // (c) For each table: if it overlaps earlier table, place in level-0,
  23. // else place in level-M.
  24. // Possible optimization 2:
  25. // Store per-table metadata (smallest, largest, largest-seq#,
  26. // large-value-refs, ...) in the table's meta section to speed up
  27. // ScanTable.
  28. #include "db/builder.h"
  29. #include "db/db_impl.h"
  30. #include "db/dbformat.h"
  31. #include "db/filename.h"
  32. #include "db/log_reader.h"
  33. #include "db/log_writer.h"
  34. #include "db/memtable.h"
  35. #include "db/table_cache.h"
  36. #include "db/version_edit.h"
  37. #include "db/write_batch_internal.h"
  38. #include "include/comparator.h"
  39. #include "include/db.h"
  40. #include "include/env.h"
  41. namespace leveldb {
  42. namespace {
  43. class Repairer {
  44. public:
  45. Repairer(const std::string& dbname, const Options& options)
  46. : dbname_(dbname),
  47. env_(options.env),
  48. icmp_(options.comparator),
  49. options_(SanitizeOptions(dbname, &icmp_, options)),
  50. owns_info_log_(options_.info_log != options.info_log),
  51. next_file_number_(1) {
  52. // TableCache can be small since we expect each table to be opened once.
  53. table_cache_ = new TableCache(dbname_, &options_, 10);
  54. }
  55. ~Repairer() {
  56. delete table_cache_;
  57. if (owns_info_log_) {
  58. delete options_.info_log;
  59. }
  60. }
  61. Status Run() {
  62. Status status = FindFiles();
  63. if (status.ok()) {
  64. ConvertLogFilesToTables();
  65. ExtractMetaData();
  66. status = WriteDescriptor();
  67. }
  68. if (status.ok()) {
  69. unsigned long long bytes = 0;
  70. for (int i = 0; i < tables_.size(); i++) {
  71. bytes += tables_[i].meta.file_size;
  72. }
  73. Log(env_, options_.info_log,
  74. "**** Repaired leveldb %s; "
  75. "recovered %d files; %llu bytes. "
  76. "Some data may have been lost. "
  77. "****",
  78. dbname_.c_str(),
  79. static_cast<int>(tables_.size()),
  80. bytes);
  81. }
  82. return status;
  83. }
  84. private:
  85. struct TableInfo {
  86. FileMetaData meta;
  87. SequenceNumber max_sequence;
  88. };
  89. std::string const dbname_;
  90. Env* const env_;
  91. InternalKeyComparator const icmp_;
  92. Options const options_;
  93. bool owns_info_log_;
  94. TableCache* table_cache_;
  95. VersionEdit edit_;
  96. std::vector<std::string> manifests_;
  97. std::vector<uint64_t> table_numbers_;
  98. std::vector<uint64_t> logs_;
  99. std::vector<TableInfo> tables_;
  100. uint64_t next_file_number_;
  101. Status FindFiles() {
  102. std::vector<std::string> filenames;
  103. Status status = env_->GetChildren(dbname_, &filenames);
  104. if (!status.ok()) {
  105. return status;
  106. }
  107. if (filenames.empty()) {
  108. return Status::IOError(dbname_, "repair found no files");
  109. }
  110. uint64_t number;
  111. LargeValueRef large_ref;
  112. FileType type;
  113. for (int i = 0; i < filenames.size(); i++) {
  114. if (ParseFileName(filenames[i], &number, &large_ref, &type)) {
  115. if (type == kLargeValueFile) {
  116. // Will be picked up when we process a Table that points to it
  117. } else if (type == kDescriptorFile) {
  118. manifests_.push_back(filenames[i]);
  119. } else {
  120. if (number + 1 > next_file_number_) {
  121. next_file_number_ = number + 1;
  122. }
  123. if (type == kLogFile) {
  124. logs_.push_back(number);
  125. } else if (type == kTableFile) {
  126. table_numbers_.push_back(number);
  127. } else {
  128. // Ignore other files
  129. }
  130. }
  131. }
  132. }
  133. return status;
  134. }
  135. void ConvertLogFilesToTables() {
  136. for (int i = 0; i < logs_.size(); i++) {
  137. std::string logname = LogFileName(dbname_, logs_[i]);
  138. Status status = ConvertLogToTable(logs_[i]);
  139. if (!status.ok()) {
  140. Log(env_, options_.info_log, "Log #%llu: ignoring conversion error: %s",
  141. (unsigned long long) logs_[i],
  142. status.ToString().c_str());
  143. }
  144. ArchiveFile(logname);
  145. }
  146. }
  147. Status ConvertLogToTable(uint64_t log) {
  148. struct LogReporter : public log::Reader::Reporter {
  149. Env* env;
  150. WritableFile* info_log;
  151. uint64_t lognum;
  152. virtual void Corruption(size_t bytes, const Status& s) {
  153. // We print error messages for corruption, but continue repairing.
  154. Log(env, info_log, "Log #%llu: dropping %d bytes; %s",
  155. (unsigned long long) lognum,
  156. static_cast<int>(bytes),
  157. s.ToString().c_str());
  158. }
  159. };
  160. // Open the log file
  161. std::string logname = LogFileName(dbname_, log);
  162. SequentialFile* lfile;
  163. Status status = env_->NewSequentialFile(logname, &lfile);
  164. if (!status.ok()) {
  165. return status;
  166. }
  167. // Create the log reader.
  168. LogReporter reporter;
  169. reporter.env = env_;
  170. reporter.info_log = options_.info_log;
  171. reporter.lognum = log;
  172. // We intentially make log::Reader do checksumming so that
  173. // corruptions cause entire commits to be skipped instead of
  174. // propagating bad information (like overly large sequence
  175. // numbers).
  176. log::Reader reader(lfile, &reporter, false/*do not checksum*/);
  177. // Read all the records and add to a memtable
  178. std::string scratch;
  179. Slice record;
  180. WriteBatch batch;
  181. MemTable mem(icmp_);
  182. int counter = 0;
  183. while (reader.ReadRecord(&record, &scratch)) {
  184. if (record.size() < 12) {
  185. reporter.Corruption(
  186. record.size(), Status::Corruption("log record too small"));
  187. continue;
  188. }
  189. WriteBatchInternal::SetContents(&batch, record);
  190. status = WriteBatchInternal::InsertInto(&batch, &mem);
  191. if (status.ok()) {
  192. counter += WriteBatchInternal::Count(&batch);
  193. } else {
  194. Log(env_, options_.info_log, "Log #%llu: ignoring %s",
  195. (unsigned long long) log,
  196. status.ToString().c_str());
  197. status = Status::OK(); // Keep going with rest of file
  198. }
  199. }
  200. delete lfile;
  201. // We ignore any version edits generated by the conversion to a Table
  202. // since ExtractMetaData() will also generate edits.
  203. VersionEdit skipped;
  204. FileMetaData meta;
  205. meta.number = next_file_number_++;
  206. Iterator* iter = mem.NewIterator();
  207. status = BuildTable(dbname_, env_, options_, table_cache_, iter,
  208. &meta, &skipped);
  209. delete iter;
  210. if (status.ok()) {
  211. if (meta.file_size > 0) {
  212. table_numbers_.push_back(meta.number);
  213. }
  214. }
  215. Log(env_, options_.info_log, "Log #%llu: %d ops saved to Table #%llu %s",
  216. (unsigned long long) log,
  217. counter,
  218. (unsigned long long) meta.number,
  219. status.ToString().c_str());
  220. return status;
  221. }
  222. void ExtractMetaData() {
  223. std::vector<TableInfo> kept;
  224. for (int i = 0; i < table_numbers_.size(); i++) {
  225. TableInfo t;
  226. t.meta.number = table_numbers_[i];
  227. Status status = ScanTable(&t);
  228. if (!status.ok()) {
  229. std::string fname = TableFileName(dbname_, table_numbers_[i]);
  230. Log(env_, options_.info_log, "Table #%llu: ignoring %s",
  231. (unsigned long long) table_numbers_[i],
  232. status.ToString().c_str());
  233. ArchiveFile(fname);
  234. } else {
  235. tables_.push_back(t);
  236. }
  237. }
  238. }
  239. Status ScanTable(TableInfo* t) {
  240. std::string fname = TableFileName(dbname_, t->meta.number);
  241. int counter = 0;
  242. Status status = env_->GetFileSize(fname, &t->meta.file_size);
  243. if (status.ok()) {
  244. Iterator* iter = table_cache_->NewIterator(
  245. ReadOptions(), t->meta.number);
  246. bool empty = true;
  247. ParsedInternalKey parsed;
  248. t->max_sequence = 0;
  249. for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
  250. Slice key = iter->key();
  251. if (!ParseInternalKey(key, &parsed)) {
  252. Log(env_, options_.info_log, "Table #%llu: unparsable key %s",
  253. (unsigned long long) t->meta.number,
  254. EscapeString(key).c_str());
  255. continue;
  256. }
  257. counter++;
  258. if (empty) {
  259. empty = false;
  260. t->meta.smallest.DecodeFrom(key);
  261. }
  262. t->meta.largest.DecodeFrom(key);
  263. if (parsed.sequence > t->max_sequence) {
  264. t->max_sequence = parsed.sequence;
  265. }
  266. if (ExtractValueType(key) == kTypeLargeValueRef) {
  267. if (iter->value().size() != LargeValueRef::ByteSize()) {
  268. Log(env_, options_.info_log, "Table #%llu: bad large value ref",
  269. (unsigned long long) t->meta.number);
  270. } else {
  271. edit_.AddLargeValueRef(LargeValueRef::FromRef(iter->value()),
  272. t->meta.number,
  273. key);
  274. }
  275. }
  276. }
  277. if (!iter->status().ok()) {
  278. status = iter->status();
  279. }
  280. delete iter;
  281. }
  282. Log(env_, options_.info_log, "Table #%llu: %d entries %s",
  283. (unsigned long long) t->meta.number,
  284. counter,
  285. status.ToString().c_str());
  286. return status;
  287. }
  288. Status WriteDescriptor() {
  289. std::string tmp = TempFileName(dbname_, 1);
  290. WritableFile* file;
  291. Status status = env_->NewWritableFile(tmp, &file);
  292. if (!status.ok()) {
  293. return status;
  294. }
  295. SequenceNumber max_sequence = 0;
  296. for (int i = 0; i < tables_.size(); i++) {
  297. if (max_sequence < tables_[i].max_sequence) {
  298. max_sequence = tables_[i].max_sequence;
  299. }
  300. }
  301. edit_.SetComparatorName(icmp_.user_comparator()->Name());
  302. edit_.SetLogNumber(0);
  303. edit_.SetNextFile(next_file_number_);
  304. edit_.SetLastSequence(max_sequence);
  305. for (int i = 0; i < tables_.size(); i++) {
  306. // TODO(opt): separate out into multiple levels
  307. const TableInfo& t = tables_[i];
  308. edit_.AddFile(0, t.meta.number, t.meta.file_size,
  309. t.meta.smallest, t.meta.largest);
  310. }
  311. //fprintf(stderr, "NewDescriptor:\n%s\n", edit_.DebugString().c_str());
  312. {
  313. log::Writer log(file);
  314. std::string record;
  315. edit_.EncodeTo(&record);
  316. status = log.AddRecord(record);
  317. }
  318. if (status.ok()) {
  319. status = file->Close();
  320. }
  321. delete file;
  322. file = NULL;
  323. if (!status.ok()) {
  324. env_->DeleteFile(tmp);
  325. } else {
  326. // Discard older manifests
  327. for (int i = 0; i < manifests_.size(); i++) {
  328. ArchiveFile(dbname_ + "/" + manifests_[i]);
  329. }
  330. // Install new manifest
  331. status = env_->RenameFile(tmp, DescriptorFileName(dbname_, 1));
  332. if (status.ok()) {
  333. status = SetCurrentFile(env_, dbname_, 1);
  334. } else {
  335. env_->DeleteFile(tmp);
  336. }
  337. }
  338. return status;
  339. }
  340. void ArchiveFile(const std::string& fname) {
  341. // Move into another directory. E.g., for
  342. // dir/foo
  343. // rename to
  344. // dir/lost/foo
  345. const char* slash = strrchr(fname.c_str(), '/');
  346. std::string new_dir;
  347. if (slash != NULL) {
  348. new_dir.assign(fname.data(), slash - fname.data());
  349. }
  350. new_dir.append("/lost");
  351. env_->CreateDir(new_dir); // Ignore error
  352. std::string new_file = new_dir;
  353. new_file.append("/");
  354. new_file.append((slash == NULL) ? fname.c_str() : slash + 1);
  355. Status s = env_->RenameFile(fname, new_file);
  356. Log(env_, options_.info_log, "Archiving %s: %s\n",
  357. fname.c_str(), s.ToString().c_str());
  358. }
  359. };
  360. }
  361. Status RepairDB(const std::string& dbname, const Options& options) {
  362. Repairer repairer(dbname, options);
  363. return repairer.Run();
  364. }
  365. }