blob_dump_tool.cc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 "utilities/blob_db/blob_dump_tool.h"
  6. #include <cinttypes>
  7. #include <cstdio>
  8. #include <iostream>
  9. #include <memory>
  10. #include <string>
  11. #include "file/random_access_file_reader.h"
  12. #include "file/readahead_raf.h"
  13. #include "port/port.h"
  14. #include "rocksdb/convenience.h"
  15. #include "rocksdb/file_system.h"
  16. #include "table/format.h"
  17. #include "util/coding.h"
  18. #include "util/string_util.h"
  19. #include "utilities/blob_db/blob_db_impl.h"
  20. namespace ROCKSDB_NAMESPACE::blob_db {
  21. BlobDumpTool::BlobDumpTool()
  22. : reader_(nullptr), buffer_(nullptr), buffer_size_(0) {}
  23. Status BlobDumpTool::Run(const std::string& filename, DisplayType show_key,
  24. DisplayType show_blob,
  25. DisplayType show_uncompressed_blob,
  26. bool show_summary) {
  27. constexpr size_t kReadaheadSize = 2 * 1024 * 1024;
  28. Status s;
  29. const auto fs = FileSystem::Default();
  30. IOOptions io_opts;
  31. s = fs->FileExists(filename, io_opts, nullptr);
  32. if (!s.ok()) {
  33. return s;
  34. }
  35. uint64_t file_size = 0;
  36. s = fs->GetFileSize(filename, io_opts, &file_size, nullptr);
  37. if (!s.ok()) {
  38. return s;
  39. }
  40. std::unique_ptr<FSRandomAccessFile> file;
  41. s = fs->NewRandomAccessFile(filename, FileOptions(), &file, nullptr);
  42. if (!s.ok()) {
  43. return s;
  44. }
  45. file = NewReadaheadRandomAccessFile(std::move(file), kReadaheadSize);
  46. if (file_size == 0) {
  47. return Status::Corruption("File is empty.");
  48. }
  49. reader_.reset(new RandomAccessFileReader(std::move(file), filename));
  50. uint64_t offset = 0;
  51. uint64_t footer_offset = 0;
  52. CompressionType compression = kNoCompression;
  53. s = DumpBlobLogHeader(&offset, &compression);
  54. if (!s.ok()) {
  55. return s;
  56. }
  57. s = DumpBlobLogFooter(file_size, &footer_offset);
  58. if (!s.ok()) {
  59. return s;
  60. }
  61. uint64_t total_records = 0;
  62. uint64_t total_key_size = 0;
  63. uint64_t total_blob_size = 0;
  64. uint64_t total_uncompressed_blob_size = 0;
  65. if (show_key != DisplayType::kNone || show_summary) {
  66. while (offset < footer_offset) {
  67. s = DumpRecord(show_key, show_blob, show_uncompressed_blob, show_summary,
  68. compression, &offset, &total_records, &total_key_size,
  69. &total_blob_size, &total_uncompressed_blob_size);
  70. if (!s.ok()) {
  71. break;
  72. }
  73. }
  74. }
  75. if (show_summary) {
  76. fprintf(stdout, "Summary:\n");
  77. fprintf(stdout, " total records: %" PRIu64 "\n", total_records);
  78. fprintf(stdout, " total key size: %" PRIu64 "\n", total_key_size);
  79. fprintf(stdout, " total blob size: %" PRIu64 "\n", total_blob_size);
  80. if (compression != kNoCompression) {
  81. fprintf(stdout, " total raw blob size: %" PRIu64 "\n",
  82. total_uncompressed_blob_size);
  83. }
  84. }
  85. return s;
  86. }
  87. Status BlobDumpTool::Read(uint64_t offset, size_t size, Slice* result) {
  88. if (buffer_size_ < size) {
  89. if (buffer_size_ == 0) {
  90. buffer_size_ = 4096;
  91. }
  92. while (buffer_size_ < size) {
  93. buffer_size_ *= 2;
  94. }
  95. buffer_.reset(new char[buffer_size_]);
  96. }
  97. Status s =
  98. reader_->Read(IOOptions(), offset, size, result, buffer_.get(), nullptr);
  99. if (!s.ok()) {
  100. return s;
  101. }
  102. if (result->size() != size) {
  103. return Status::Corruption("Reach the end of the file unexpectedly.");
  104. }
  105. return s;
  106. }
  107. Status BlobDumpTool::DumpBlobLogHeader(uint64_t* offset,
  108. CompressionType* compression) {
  109. Slice slice;
  110. Status s = Read(0, BlobLogHeader::kSize, &slice);
  111. if (!s.ok()) {
  112. return s;
  113. }
  114. BlobLogHeader header;
  115. s = header.DecodeFrom(slice);
  116. if (!s.ok()) {
  117. return s;
  118. }
  119. fprintf(stdout, "Blob log header:\n");
  120. fprintf(stdout, " Version : %" PRIu32 "\n", header.version);
  121. fprintf(stdout, " Column Family ID : %" PRIu32 "\n",
  122. header.column_family_id);
  123. std::string compression_str;
  124. if (!GetStringFromCompressionType(&compression_str, header.compression)
  125. .ok()) {
  126. compression_str = "Unrecongnized compression type (" +
  127. std::to_string((int)header.compression) + ")";
  128. }
  129. fprintf(stdout, " Compression : %s\n", compression_str.c_str());
  130. fprintf(stdout, " Expiration range : %s\n",
  131. GetString(header.expiration_range).c_str());
  132. *offset = BlobLogHeader::kSize;
  133. *compression = header.compression;
  134. return s;
  135. }
  136. Status BlobDumpTool::DumpBlobLogFooter(uint64_t file_size,
  137. uint64_t* footer_offset) {
  138. auto no_footer = [&]() {
  139. *footer_offset = file_size;
  140. fprintf(stdout, "No blob log footer.\n");
  141. return Status::OK();
  142. };
  143. if (file_size < BlobLogHeader::kSize + BlobLogFooter::kSize) {
  144. return no_footer();
  145. }
  146. Slice slice;
  147. *footer_offset = file_size - BlobLogFooter::kSize;
  148. Status s = Read(*footer_offset, BlobLogFooter::kSize, &slice);
  149. if (!s.ok()) {
  150. return s;
  151. }
  152. BlobLogFooter footer;
  153. s = footer.DecodeFrom(slice);
  154. if (!s.ok()) {
  155. return no_footer();
  156. }
  157. fprintf(stdout, "Blob log footer:\n");
  158. fprintf(stdout, " Blob count : %" PRIu64 "\n", footer.blob_count);
  159. fprintf(stdout, " Expiration Range : %s\n",
  160. GetString(footer.expiration_range).c_str());
  161. return s;
  162. }
  163. Status BlobDumpTool::DumpRecord(DisplayType show_key, DisplayType show_blob,
  164. DisplayType show_uncompressed_blob,
  165. bool show_summary, CompressionType compression,
  166. uint64_t* offset, uint64_t* total_records,
  167. uint64_t* total_key_size,
  168. uint64_t* total_blob_size,
  169. uint64_t* total_uncompressed_blob_size) {
  170. if (show_key != DisplayType::kNone) {
  171. fprintf(stdout, "Read record with offset 0x%" PRIx64 " (%" PRIu64 "):\n",
  172. *offset, *offset);
  173. }
  174. Slice slice;
  175. Status s = Read(*offset, BlobLogRecord::kHeaderSize, &slice);
  176. if (!s.ok()) {
  177. return s;
  178. }
  179. BlobLogRecord record;
  180. s = record.DecodeHeaderFrom(slice);
  181. if (!s.ok()) {
  182. return s;
  183. }
  184. uint64_t key_size = record.key_size;
  185. uint64_t value_size = record.value_size;
  186. if (show_key != DisplayType::kNone) {
  187. fprintf(stdout, " key size : %" PRIu64 "\n", key_size);
  188. fprintf(stdout, " value size : %" PRIu64 "\n", value_size);
  189. fprintf(stdout, " expiration : %" PRIu64 "\n", record.expiration);
  190. }
  191. *offset += BlobLogRecord::kHeaderSize;
  192. s = Read(*offset, static_cast<size_t>(key_size + value_size), &slice);
  193. if (!s.ok()) {
  194. return s;
  195. }
  196. // Decompress value
  197. std::string uncompressed_value;
  198. if (compression != kNoCompression &&
  199. (show_uncompressed_blob != DisplayType::kNone || show_summary)) {
  200. BlockContents contents;
  201. UncompressionContext context(compression);
  202. UncompressionInfo info(context, UncompressionDict::GetEmptyDict(),
  203. compression);
  204. s = DecompressBlockData(
  205. slice.data() + key_size, static_cast<size_t>(value_size), compression,
  206. BlobDecompressor(), &contents, ImmutableOptions(Options()));
  207. if (!s.ok()) {
  208. return s;
  209. }
  210. uncompressed_value = contents.data.ToString();
  211. }
  212. if (show_key != DisplayType::kNone) {
  213. fprintf(stdout, " key : ");
  214. DumpSlice(Slice(slice.data(), static_cast<size_t>(key_size)), show_key);
  215. if (show_blob != DisplayType::kNone) {
  216. fprintf(stdout, " blob : ");
  217. DumpSlice(Slice(slice.data() + static_cast<size_t>(key_size),
  218. static_cast<size_t>(value_size)),
  219. show_blob);
  220. }
  221. if (show_uncompressed_blob != DisplayType::kNone) {
  222. fprintf(stdout, " raw blob : ");
  223. DumpSlice(Slice(uncompressed_value), show_uncompressed_blob);
  224. }
  225. }
  226. *offset += key_size + value_size;
  227. *total_records += 1;
  228. *total_key_size += key_size;
  229. *total_blob_size += value_size;
  230. *total_uncompressed_blob_size += uncompressed_value.size();
  231. return s;
  232. }
  233. void BlobDumpTool::DumpSlice(const Slice s, DisplayType type) {
  234. if (type == DisplayType::kRaw) {
  235. fprintf(stdout, "%s\n", s.ToString().c_str());
  236. } else if (type == DisplayType::kHex) {
  237. fprintf(stdout, "%s\n", s.ToString(true /*hex*/).c_str());
  238. } else if (type == DisplayType::kDetail) {
  239. char buf[100];
  240. for (size_t i = 0; i < s.size(); i += 16) {
  241. memset(buf, 0, sizeof(buf));
  242. for (size_t j = 0; j < 16 && i + j < s.size(); j++) {
  243. unsigned char c = s[i + j];
  244. snprintf(buf + j * 3 + 15, 2, "%x", c >> 4);
  245. snprintf(buf + j * 3 + 16, 2, "%x", c & 0xf);
  246. snprintf(buf + j + 65, 2, "%c", (0x20 <= c && c <= 0x7e) ? c : '.');
  247. }
  248. for (size_t p = 0; p + 1 < sizeof(buf); p++) {
  249. if (buf[p] == 0) {
  250. buf[p] = ' ';
  251. }
  252. }
  253. fprintf(stdout, "%s\n", i == 0 ? buf + 15 : buf);
  254. }
  255. }
  256. }
  257. template <class T>
  258. std::string BlobDumpTool::GetString(std::pair<T, T> p) {
  259. if (p.first == 0 && p.second == 0) {
  260. return "nil";
  261. }
  262. return "(" + std::to_string(p.first) + ", " + std::to_string(p.second) + ")";
  263. }
  264. } // namespace ROCKSDB_NAMESPACE::blob_db