repair.cc 12.1 KB
Newer Older
1 2 3 4 5
//  Copyright (c) 2013, Facebook, Inc.  All rights reserved.
//  This source code is licensed under the BSD-style license found in the
//  LICENSE file in the root directory of this source tree. An additional grant
//  of patent rights can be found in the PATENTS file in the same directory.
//
J
jorlow@chromium.org 已提交
6 7 8 9 10 11 12 13
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.
//
// We recover the contents of the descriptor from the other files we find.
// (1) Any log files are first converted to tables
// (2) We scan every table to compute
//     (a) smallest/largest for the table
D
dgrogan@chromium.org 已提交
14
//     (b) largest sequence number in the table
J
jorlow@chromium.org 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28
// (3) We generate descriptor contents:
//      - log number is set to zero
//      - next-file-number is set to 1 + largest file number we found
//      - last-sequence-number is set to largest sequence# found across
//        all tables (see 2c)
//      - compaction pointers are cleared
//      - every table file is added at level 0
//
// Possible optimization 1:
//   (a) Compute total size and use to pick appropriate max-level M
//   (b) Sort tables by largest sequence# in the table
//   (c) For each table: if it overlaps earlier table, place in level-0,
//       else place in level-M.
// Possible optimization 2:
D
dgrogan@chromium.org 已提交
29 30
//   Store per-table metadata (smallest, largest, largest-seq#, ...)
//   in the table's meta section to speed up ScanTable.
J
jorlow@chromium.org 已提交
31 32 33 34 35 36 37 38 39 40 41

#include "db/builder.h"
#include "db/db_impl.h"
#include "db/dbformat.h"
#include "db/filename.h"
#include "db/log_reader.h"
#include "db/log_writer.h"
#include "db/memtable.h"
#include "db/table_cache.h"
#include "db/version_edit.h"
#include "db/write_batch_internal.h"
42 43 44
#include "rocksdb/comparator.h"
#include "rocksdb/db.h"
#include "rocksdb/env.h"
J
jorlow@chromium.org 已提交
45

46
namespace rocksdb {
J
jorlow@chromium.org 已提交
47 48 49 50 51 52 53 54 55

namespace {

class Repairer {
 public:
  Repairer(const std::string& dbname, const Options& options)
      : dbname_(dbname),
        env_(options.env),
        icmp_(options.comparator),
S
Sanjay Ghemawat 已提交
56 57
        ipolicy_(options.filter_policy),
        options_(SanitizeOptions(dbname, &icmp_, &ipolicy_, options)),
J
jorlow@chromium.org 已提交
58 59
        next_file_number_(1) {
    // TableCache can be small since we expect each table to be opened once.
60
    table_cache_ = new TableCache(dbname_, &options_, storage_options_, 10);
61
    edit_ = new VersionEdit();
J
jorlow@chromium.org 已提交
62 63 64 65
  }

  ~Repairer() {
    delete table_cache_;
66
    delete edit_;
J
jorlow@chromium.org 已提交
67 68 69 70 71 72 73 74 75 76 77
  }

  Status Run() {
    Status status = FindFiles();
    if (status.ok()) {
      ConvertLogFilesToTables();
      ExtractMetaData();
      status = WriteDescriptor();
    }
    if (status.ok()) {
      unsigned long long bytes = 0;
D
dgrogan@chromium.org 已提交
78
      for (size_t i = 0; i < tables_.size(); i++) {
J
jorlow@chromium.org 已提交
79 80
        bytes += tables_[i].meta.file_size;
      }
81
      Log(options_.info_log,
82
          "**** Repaired rocksdb %s; "
J
jorlow@chromium.org 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95
          "recovered %d files; %llu bytes. "
          "Some data may have been lost. "
          "****",
          dbname_.c_str(),
          static_cast<int>(tables_.size()),
          bytes);
    }
    return status;
  }

 private:
  struct TableInfo {
    FileMetaData meta;
96
    SequenceNumber min_sequence;
J
jorlow@chromium.org 已提交
97 98 99 100 101 102
    SequenceNumber max_sequence;
  };

  std::string const dbname_;
  Env* const env_;
  InternalKeyComparator const icmp_;
S
Sanjay Ghemawat 已提交
103
  InternalFilterPolicy const ipolicy_;
J
jorlow@chromium.org 已提交
104 105
  Options const options_;
  TableCache* table_cache_;
106
  VersionEdit* edit_;
J
jorlow@chromium.org 已提交
107 108 109 110 111 112

  std::vector<std::string> manifests_;
  std::vector<uint64_t> table_numbers_;
  std::vector<uint64_t> logs_;
  std::vector<TableInfo> tables_;
  uint64_t next_file_number_;
H
Haobo Xu 已提交
113
  const EnvOptions storage_options_;
J
jorlow@chromium.org 已提交
114 115 116 117 118 119 120 121

  Status FindFiles() {
    std::vector<std::string> filenames;
    Status status = env_->GetChildren(dbname_, &filenames);
    if (!status.ok()) {
      return status;
    }
    if (filenames.empty()) {
L
Lei Jin 已提交
122
      return Status::Corruption(dbname_, "repair found no files");
J
jorlow@chromium.org 已提交
123 124 125 126
    }

    uint64_t number;
    FileType type;
D
dgrogan@chromium.org 已提交
127 128 129
    for (size_t i = 0; i < filenames.size(); i++) {
      if (ParseFileName(filenames[i], &number, &type)) {
        if (type == kDescriptorFile) {
J
jorlow@chromium.org 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
          manifests_.push_back(filenames[i]);
        } else {
          if (number + 1 > next_file_number_) {
            next_file_number_ = number + 1;
          }
          if (type == kLogFile) {
            logs_.push_back(number);
          } else if (type == kTableFile) {
            table_numbers_.push_back(number);
          } else {
            // Ignore other files
          }
        }
      }
    }
    return status;
  }

  void ConvertLogFilesToTables() {
D
dgrogan@chromium.org 已提交
149
    for (size_t i = 0; i < logs_.size(); i++) {
J
jorlow@chromium.org 已提交
150 151 152
      std::string logname = LogFileName(dbname_, logs_[i]);
      Status status = ConvertLogToTable(logs_[i]);
      if (!status.ok()) {
153
        Log(options_.info_log, "Log #%llu: ignoring conversion error: %s",
J
jorlow@chromium.org 已提交
154 155 156 157 158 159 160 161 162 163
            (unsigned long long) logs_[i],
            status.ToString().c_str());
      }
      ArchiveFile(logname);
    }
  }

  Status ConvertLogToTable(uint64_t log) {
    struct LogReporter : public log::Reader::Reporter {
      Env* env;
164
      std::shared_ptr<Logger> info_log;
J
jorlow@chromium.org 已提交
165 166 167
      uint64_t lognum;
      virtual void Corruption(size_t bytes, const Status& s) {
        // We print error messages for corruption, but continue repairing.
168
        Log(info_log, "Log #%llu: dropping %d bytes; %s",
J
jorlow@chromium.org 已提交
169 170 171 172 173 174 175 176
            (unsigned long long) lognum,
            static_cast<int>(bytes),
            s.ToString().c_str());
      }
    };

    // Open the log file
    std::string logname = LogFileName(dbname_, log);
177
    unique_ptr<SequentialFile> lfile;
178
    Status status = env_->NewSequentialFile(logname, &lfile, storage_options_);
J
jorlow@chromium.org 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191
    if (!status.ok()) {
      return status;
    }

    // Create the log reader.
    LogReporter reporter;
    reporter.env = env_;
    reporter.info_log = options_.info_log;
    reporter.lognum = log;
    // We intentially make log::Reader do checksumming so that
    // corruptions cause entire commits to be skipped instead of
    // propagating bad information (like overly large sequence
    // numbers).
192
    log::Reader reader(std::move(lfile), &reporter, false/*do not checksum*/,
193
                       0/*initial_offset*/);
J
jorlow@chromium.org 已提交
194 195 196 197 198

    // Read all the records and add to a memtable
    std::string scratch;
    Slice record;
    WriteBatch batch;
199
    MemTable* mem = new MemTable(icmp_, options_);
200
    mem->Ref();
J
jorlow@chromium.org 已提交
201 202 203 204 205 206 207 208
    int counter = 0;
    while (reader.ReadRecord(&record, &scratch)) {
      if (record.size() < 12) {
        reporter.Corruption(
            record.size(), Status::Corruption("log record too small"));
        continue;
      }
      WriteBatchInternal::SetContents(&batch, record);
209
      status = WriteBatchInternal::InsertInto(&batch, mem, &options_);
J
jorlow@chromium.org 已提交
210 211 212
      if (status.ok()) {
        counter += WriteBatchInternal::Count(&batch);
      } else {
213
        Log(options_.info_log, "Log #%llu: ignoring %s",
J
jorlow@chromium.org 已提交
214 215 216 217 218 219
            (unsigned long long) log,
            status.ToString().c_str());
        status = Status::OK();  // Keep going with rest of file
      }
    }

220
    // Do not record a version edit for this conversion to a Table
J
jorlow@chromium.org 已提交
221 222 223
    // since ExtractMetaData() will also generate edits.
    FileMetaData meta;
    meta.number = next_file_number_++;
224
    Iterator* iter = mem->NewIterator();
225 226
    status = BuildTable(dbname_, env_, options_, storage_options_, table_cache_,
                        iter, &meta, icmp_, 0, 0, kNoCompression);
J
jorlow@chromium.org 已提交
227
    delete iter;
228
    delete mem->Unref();
A
Abhishek Kona 已提交
229
    mem = nullptr;
J
jorlow@chromium.org 已提交
230 231 232 233 234
    if (status.ok()) {
      if (meta.file_size > 0) {
        table_numbers_.push_back(meta.number);
      }
    }
235
    Log(options_.info_log, "Log #%llu: %d ops saved to Table #%llu %s",
J
jorlow@chromium.org 已提交
236 237 238 239 240 241 242 243
        (unsigned long long) log,
        counter,
        (unsigned long long) meta.number,
        status.ToString().c_str());
    return status;
  }

  void ExtractMetaData() {
D
dgrogan@chromium.org 已提交
244
    for (size_t i = 0; i < table_numbers_.size(); i++) {
J
jorlow@chromium.org 已提交
245 246 247 248 249
      TableInfo t;
      t.meta.number = table_numbers_[i];
      Status status = ScanTable(&t);
      if (!status.ok()) {
        std::string fname = TableFileName(dbname_, table_numbers_[i]);
250
        Log(options_.info_log, "Table #%llu: ignoring %s",
J
jorlow@chromium.org 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264
            (unsigned long long) table_numbers_[i],
            status.ToString().c_str());
        ArchiveFile(fname);
      } else {
        tables_.push_back(t);
      }
    }
  }

  Status ScanTable(TableInfo* t) {
    std::string fname = TableFileName(dbname_, t->meta.number);
    int counter = 0;
    Status status = env_->GetFileSize(fname, &t->meta.file_size);
    if (status.ok()) {
265
      FileMetaData dummy_meta(t->meta.number, t->meta.file_size);
J
jorlow@chromium.org 已提交
266
      Iterator* iter = table_cache_->NewIterator(
267
          ReadOptions(), storage_options_, icmp_, dummy_meta);
J
jorlow@chromium.org 已提交
268 269
      bool empty = true;
      ParsedInternalKey parsed;
270
      t->min_sequence = 0;
J
jorlow@chromium.org 已提交
271 272 273 274
      t->max_sequence = 0;
      for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
        Slice key = iter->key();
        if (!ParseInternalKey(key, &parsed)) {
275
          Log(options_.info_log, "Table #%llu: unparsable key %s",
J
jorlow@chromium.org 已提交
276 277 278 279 280 281 282 283 284 285 286
              (unsigned long long) t->meta.number,
              EscapeString(key).c_str());
          continue;
        }

        counter++;
        if (empty) {
          empty = false;
          t->meta.smallest.DecodeFrom(key);
        }
        t->meta.largest.DecodeFrom(key);
287 288 289
        if (parsed.sequence < t->min_sequence) {
          t->min_sequence = parsed.sequence;
        }
J
jorlow@chromium.org 已提交
290 291 292 293 294 295 296 297 298
        if (parsed.sequence > t->max_sequence) {
          t->max_sequence = parsed.sequence;
        }
      }
      if (!iter->status().ok()) {
        status = iter->status();
      }
      delete iter;
    }
299
    Log(options_.info_log, "Table #%llu: %d entries %s",
J
jorlow@chromium.org 已提交
300 301 302 303 304 305 306 307
        (unsigned long long) t->meta.number,
        counter,
        status.ToString().c_str());
    return status;
  }

  Status WriteDescriptor() {
    std::string tmp = TempFileName(dbname_, 1);
308
    unique_ptr<WritableFile> file;
I
Igor Canadi 已提交
309 310
    Status status =
        env_->NewWritableFile(tmp, &file, storage_options_.AdaptForLogWrite());
J
jorlow@chromium.org 已提交
311 312 313 314 315
    if (!status.ok()) {
      return status;
    }

    SequenceNumber max_sequence = 0;
D
dgrogan@chromium.org 已提交
316
    for (size_t i = 0; i < tables_.size(); i++) {
J
jorlow@chromium.org 已提交
317 318 319 320 321
      if (max_sequence < tables_[i].max_sequence) {
        max_sequence = tables_[i].max_sequence;
      }
    }

322 323 324 325
    edit_->SetComparatorName(icmp_.user_comparator()->Name());
    edit_->SetLogNumber(0);
    edit_->SetNextFile(next_file_number_);
    edit_->SetLastSequence(max_sequence);
J
jorlow@chromium.org 已提交
326

D
dgrogan@chromium.org 已提交
327
    for (size_t i = 0; i < tables_.size(); i++) {
J
jorlow@chromium.org 已提交
328 329
      // TODO(opt): separate out into multiple levels
      const TableInfo& t = tables_[i];
330
      edit_->AddFile(0, t.meta.number, t.meta.file_size,
331 332
                    t.meta.smallest, t.meta.largest,
                    t.min_sequence, t.max_sequence);
J
jorlow@chromium.org 已提交
333 334 335 336
    }

    //fprintf(stderr, "NewDescriptor:\n%s\n", edit_.DebugString().c_str());
    {
337
      log::Writer log(std::move(file));
J
jorlow@chromium.org 已提交
338
      std::string record;
339
      edit_->EncodeTo(&record);
J
jorlow@chromium.org 已提交
340 341 342 343 344 345 346
      status = log.AddRecord(record);
    }

    if (!status.ok()) {
      env_->DeleteFile(tmp);
    } else {
      // Discard older manifests
D
dgrogan@chromium.org 已提交
347
      for (size_t i = 0; i < manifests_.size(); i++) {
J
jorlow@chromium.org 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
        ArchiveFile(dbname_ + "/" + manifests_[i]);
      }

      // Install new manifest
      status = env_->RenameFile(tmp, DescriptorFileName(dbname_, 1));
      if (status.ok()) {
        status = SetCurrentFile(env_, dbname_, 1);
      } else {
        env_->DeleteFile(tmp);
      }
    }
    return status;
  }

  void ArchiveFile(const std::string& fname) {
    // Move into another directory.  E.g., for
    //    dir/foo
    // rename to
    //    dir/lost/foo
    const char* slash = strrchr(fname.c_str(), '/');
    std::string new_dir;
A
Abhishek Kona 已提交
369
    if (slash != nullptr) {
J
jorlow@chromium.org 已提交
370 371 372 373 374 375
      new_dir.assign(fname.data(), slash - fname.data());
    }
    new_dir.append("/lost");
    env_->CreateDir(new_dir);  // Ignore error
    std::string new_file = new_dir;
    new_file.append("/");
A
Abhishek Kona 已提交
376
    new_file.append((slash == nullptr) ? fname.c_str() : slash + 1);
J
jorlow@chromium.org 已提交
377
    Status s = env_->RenameFile(fname, new_file);
378
    Log(options_.info_log, "Archiving %s: %s\n",
J
jorlow@chromium.org 已提交
379 380 381
        fname.c_str(), s.ToString().c_str());
  }
};
H
Hans Wennborg 已提交
382
}  // namespace
J
jorlow@chromium.org 已提交
383 384 385 386 387 388

Status RepairDB(const std::string& dbname, const Options& options) {
  Repairer repairer(dbname, options);
  return repairer.Run();
}

389
}  // namespace rocksdb