db_info_dumper.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright (c) 2011-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. #include "db/db_info_dumper.h"
  6. #include <algorithm>
  7. #include <cinttypes>
  8. #include <cstdio>
  9. #include <string>
  10. #include <vector>
  11. #include "file/filename.h"
  12. #include "rocksdb/env.h"
  13. namespace ROCKSDB_NAMESPACE {
  14. void DumpDBFileSummary(const ImmutableDBOptions& options,
  15. const std::string& dbname,
  16. const std::string& session_id) {
  17. if (options.info_log == nullptr) {
  18. return;
  19. }
  20. auto* env = options.env;
  21. uint64_t number = 0;
  22. FileType type = kInfoLogFile;
  23. std::vector<std::string> files;
  24. uint64_t file_num = 0;
  25. uint64_t file_size;
  26. std::string file_info, wal_info;
  27. Header(options.info_log, "DB SUMMARY\n");
  28. {
  29. std::string hostname;
  30. if (env->GetHostNameString(&hostname).ok()) {
  31. Header(options.info_log, "Host name (Env): %s\n", hostname.c_str());
  32. }
  33. }
  34. Header(options.info_log, "DB Session ID: %s\n", session_id.c_str());
  35. Status s;
  36. // Get files in dbname dir
  37. s = env->GetChildren(dbname, &files);
  38. if (!s.ok()) {
  39. Error(options.info_log, "Error when reading %s dir %s\n", dbname.c_str(),
  40. s.ToString().c_str());
  41. }
  42. std::sort(files.begin(), files.end());
  43. for (const std::string& file : files) {
  44. if (!ParseFileName(file, &number, &type)) {
  45. continue;
  46. }
  47. switch (type) {
  48. case kCurrentFile:
  49. Header(options.info_log, "CURRENT file: %s\n", file.c_str());
  50. break;
  51. case kIdentityFile:
  52. Header(options.info_log, "IDENTITY file: %s\n", file.c_str());
  53. break;
  54. case kDescriptorFile:
  55. s = env->GetFileSize(dbname + "/" + file, &file_size);
  56. if (s.ok()) {
  57. Header(options.info_log,
  58. "MANIFEST file: %s size: %" PRIu64 " Bytes\n", file.c_str(),
  59. file_size);
  60. } else {
  61. Error(options.info_log,
  62. "Error when reading MANIFEST file: %s/%s %s\n", dbname.c_str(),
  63. file.c_str(), s.ToString().c_str());
  64. }
  65. break;
  66. case kWalFile:
  67. s = env->GetFileSize(dbname + "/" + file, &file_size);
  68. if (s.ok()) {
  69. wal_info.append(file)
  70. .append(" size: ")
  71. .append(std::to_string(file_size))
  72. .append(" ; ");
  73. } else {
  74. Error(options.info_log, "Error when reading LOG file: %s/%s %s\n",
  75. dbname.c_str(), file.c_str(), s.ToString().c_str());
  76. }
  77. break;
  78. case kTableFile:
  79. if (++file_num < 10) {
  80. file_info.append(file).append(" ");
  81. }
  82. break;
  83. default:
  84. break;
  85. }
  86. }
  87. // Get sst files in db_path dir
  88. for (auto& db_path : options.db_paths) {
  89. if (dbname.compare(db_path.path) != 0) {
  90. s = env->GetChildren(db_path.path, &files);
  91. if (s.IsNotFound() || s.IsPathNotFound()) {
  92. Header(options.info_log,
  93. "Directory from db_paths/cf_paths does not yet exist: %s\n",
  94. db_path.path.c_str());
  95. continue;
  96. } else if (!s.ok()) {
  97. Error(options.info_log, "Error when reading %s dir %s\n",
  98. db_path.path.c_str(), s.ToString().c_str());
  99. continue;
  100. }
  101. std::sort(files.begin(), files.end());
  102. for (const std::string& file : files) {
  103. if (ParseFileName(file, &number, &type)) {
  104. if (type == kTableFile && ++file_num < 10) {
  105. file_info.append(file).append(" ");
  106. }
  107. }
  108. }
  109. }
  110. Header(options.info_log,
  111. "SST files in %s dir, Total Num: %" PRIu64 ", files: %s\n",
  112. db_path.path.c_str(), file_num, file_info.c_str());
  113. file_num = 0;
  114. file_info.clear();
  115. }
  116. // Get wal file in wal_dir
  117. const auto& wal_dir = options.GetWalDir(dbname);
  118. bool log_wal_info = true;
  119. if (!options.IsWalDirSameAsDBPath(dbname)) {
  120. s = env->GetChildren(wal_dir, &files);
  121. if (s.IsNotFound() || s.IsPathNotFound()) {
  122. Header(options.info_log,
  123. "Write Ahead Log directory does not yet exist: %s\n",
  124. wal_dir.c_str());
  125. log_wal_info = false;
  126. } else if (!s.ok()) {
  127. Error(options.info_log, "Error when reading wal dir %s: %s\n",
  128. wal_dir.c_str(), s.ToString().c_str());
  129. log_wal_info = false;
  130. }
  131. wal_info.clear();
  132. for (const std::string& file : files) {
  133. if (ParseFileName(file, &number, &type)) {
  134. if (type == kWalFile) {
  135. s = env->GetFileSize(wal_dir + "/" + file, &file_size);
  136. if (s.ok()) {
  137. wal_info.append(file)
  138. .append(" size: ")
  139. .append(std::to_string(file_size))
  140. .append(" ; ");
  141. } else {
  142. Error(options.info_log, "Error when reading LOG file %s/%s %s\n",
  143. wal_dir.c_str(), file.c_str(), s.ToString().c_str());
  144. }
  145. }
  146. }
  147. }
  148. }
  149. if (log_wal_info) {
  150. Header(options.info_log, "Write Ahead Log file in %s: %s\n",
  151. wal_dir.c_str(), wal_info.c_str());
  152. }
  153. }
  154. } // namespace ROCKSDB_NAMESPACE