From 5e642403a91831855f40e87b38449f3499b7e148 Mon Sep 17 00:00:00 2001 From: Feng Zhu Date: Wed, 13 Aug 2014 13:45:13 -0700 Subject: [PATCH] log db path info before open Summary: 1. write db MANIFEST, CURRENT, IDENTITY, sst files, log files to log before open Test Plan: run db and check LOG file Reviewers: ljin, yhchiang, igor, dhruba, sdong Reviewed By: sdong Subscribers: leveldb Differential Revision: https://reviews.facebook.net/D21459 --- db/db_impl.cc | 1 + db/db_impl.h | 4 ++ util/db_info_dummper.cc | 115 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 120 insertions(+) create mode 100644 util/db_info_dummper.cc diff --git a/db/db_impl.cc b/db/db_impl.cc index ea7938e2f..d08fd1f0c 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -364,6 +364,7 @@ DBImpl::DBImpl(const DBOptions& options, const std::string& dbname) new ColumnFamilyMemTablesImpl(versions_->GetColumnFamilySet())); DumpLeveldbBuildVersion(options_.info_log.get()); + DumpDBFileSummary(options_, dbname_); options_.Dump(options_.info_log.get()); LogFlush(options_.info_log); diff --git a/db/db_impl.h b/db/db_impl.h index aff4b8e50..988e2d8d8 100644 --- a/db/db_impl.h +++ b/db/db_impl.h @@ -680,4 +680,8 @@ static void ClipToRange(T* ptr, V minvalue, V maxvalue) { if (static_cast(*ptr) < minvalue) *ptr = minvalue; } +// Dump db file summary, implemented in util/ +extern void DumpDBFileSummary(const DBOptions& options, + const std::string& dbname); + } // namespace rocksdb diff --git a/util/db_info_dummper.cc b/util/db_info_dummper.cc new file mode 100644 index 000000000..3236552f1 --- /dev/null +++ b/util/db_info_dummper.cc @@ -0,0 +1,115 @@ +// 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. +// +// Must not be included from any .h files to avoid polluting the namespace +// with macros. + +#include +#include +#include +#include + +#include "rocksdb/options.h" +#include "rocksdb/env.h" +#include "db/filename.h" + +namespace rocksdb { + +void DumpDBFileSummary(const DBOptions& options, const std::string& dbname) { + if (options.info_log == nullptr) { + return; + } + + auto* env = options.env; + uint64_t number; + FileType type; + + std::vector files; + uint64_t file_num = 0; + uint64_t file_size; + std::string file_info, wal_info; + + Log(options.info_log, "DB SUMMARY\n"); + // Get files in dbname dir + if (!env->GetChildren(dbname, &files).ok()) { + Log(options.info_log, "Error when reading %s dir\n", dbname.c_str()); + } + std::sort(files.begin(), files.end()); + for (std::string file : files) { + ParseFileName(file, &number, &type); + switch (type) { + case kCurrentFile: + Log(options.info_log, "CURRENT file: %s\n", file.c_str()); + break; + case kIdentityFile: + Log(options.info_log, "IDENTITY file: %s\n", file.c_str()); + break; + case kDescriptorFile: + env->GetFileSize(dbname + "/" + file, &file_size); + Log(options.info_log, "MANIFEST file: %s size: %lu Bytes\n", + file.c_str(), file_size); + break; + case kLogFile: + env->GetFileSize(dbname + "/" + file, &file_size); + char str[8]; + snprintf(str, sizeof(str), "%lu", file_size); + wal_info.append(file).append(" size: "). + append(str, sizeof(str)).append(" ;"); + break; + case kTableFile: + if (++file_num < 10) { + file_info.append(file).append(" "); + } + break; + default: + break; + } + } + + // Get sst files in db_path dir + for (auto& db_path : options.db_paths) { + if (dbname.compare(db_path.path) != 0) { + if (!env->GetChildren(db_path.path, &files).ok()) { + Log(options.info_log, "Error when reading %s dir\n", + db_path.path.c_str()); + continue; + } + std::sort(files.begin(), files.end()); + for (std::string file : files) { + ParseFileName(file, &number, &type); + if (type == kTableFile && ++file_num < 10) { + file_info.append(file).append(" "); + } + } + } + Log(options.info_log, "SST files in %s dir, Total Num: %lu, files: %s\n", + db_path.path.c_str(), file_num, file_info.c_str()); + file_num = 0; + file_info.clear(); + } + + // Get wal file in wal_dir + if (dbname.compare(options.wal_dir) != 0) { + if (!env->GetChildren(options.wal_dir, &files).ok()) { + Log(options.info_log, "Error when reading %s dir\n", + options.wal_dir.c_str()); + return; + } + wal_info.clear(); + for (std::string file : files) { + ParseFileName(file, &number, &type); + if (type == kLogFile) { + env->GetFileSize(options.wal_dir + "/" + file, &file_size); + char str[8]; + snprintf(str, sizeof(str), "%lu", file_size); + wal_info.append(file).append(" size: "). + append(str, sizeof(str)).append(" ;"); + } + } + } + Log(options.info_log, "Write Ahead Log file in %s: %s\n", + options.wal_dir.c_str(), wal_info.c_str()); +} +} // namespace rocksdb