blob_counting_iterator_test.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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/blob/blob_counting_iterator.h"
  6. #include <string>
  7. #include <vector>
  8. #include "db/blob/blob_garbage_meter.h"
  9. #include "db/blob/blob_index.h"
  10. #include "db/blob/blob_log_format.h"
  11. #include "db/dbformat.h"
  12. #include "test_util/testharness.h"
  13. #include "test_util/testutil.h"
  14. #include "util/vector_iterator.h"
  15. namespace ROCKSDB_NAMESPACE {
  16. void CheckInFlow(const BlobGarbageMeter& blob_garbage_meter,
  17. uint64_t blob_file_number, uint64_t count, uint64_t bytes) {
  18. const auto& flows = blob_garbage_meter.flows();
  19. const auto it = flows.find(blob_file_number);
  20. if (it == flows.end()) {
  21. ASSERT_EQ(count, 0);
  22. ASSERT_EQ(bytes, 0);
  23. return;
  24. }
  25. const auto& in = it->second.GetInFlow();
  26. ASSERT_EQ(in.GetCount(), count);
  27. ASSERT_EQ(in.GetBytes(), bytes);
  28. }
  29. TEST(BlobCountingIteratorTest, CountBlobs) {
  30. // Note: the input consists of three key-values: two are blob references to
  31. // different blob files, while the third one is a plain value.
  32. constexpr char user_key0[] = "key0";
  33. constexpr char user_key1[] = "key1";
  34. constexpr char user_key2[] = "key2";
  35. const std::vector<std::string> keys{
  36. test::KeyStr(user_key0, 1, kTypeBlobIndex),
  37. test::KeyStr(user_key1, 2, kTypeBlobIndex),
  38. test::KeyStr(user_key2, 3, kTypeValue)};
  39. constexpr uint64_t first_blob_file_number = 4;
  40. constexpr uint64_t first_offset = 1000;
  41. constexpr uint64_t first_size = 2000;
  42. std::string first_blob_index;
  43. BlobIndex::EncodeBlob(&first_blob_index, first_blob_file_number, first_offset,
  44. first_size, kNoCompression);
  45. constexpr uint64_t second_blob_file_number = 6;
  46. constexpr uint64_t second_offset = 2000;
  47. constexpr uint64_t second_size = 4000;
  48. std::string second_blob_index;
  49. BlobIndex::EncodeBlob(&second_blob_index, second_blob_file_number,
  50. second_offset, second_size, kNoCompression);
  51. const std::vector<std::string> values{first_blob_index, second_blob_index,
  52. "raw_value"};
  53. assert(keys.size() == values.size());
  54. VectorIterator input(keys, values);
  55. BlobGarbageMeter blob_garbage_meter;
  56. BlobCountingIterator blob_counter(&input, &blob_garbage_meter);
  57. constexpr uint64_t first_expected_bytes =
  58. first_size +
  59. BlobLogRecord::CalculateAdjustmentForRecordHeader(sizeof(user_key0) - 1);
  60. constexpr uint64_t second_expected_bytes =
  61. second_size +
  62. BlobLogRecord::CalculateAdjustmentForRecordHeader(sizeof(user_key1) - 1);
  63. // Call SeekToFirst and iterate forward
  64. blob_counter.SeekToFirst();
  65. ASSERT_TRUE(blob_counter.Valid());
  66. ASSERT_OK(blob_counter.status());
  67. ASSERT_EQ(blob_counter.key(), keys[0]);
  68. ASSERT_EQ(blob_counter.user_key(), user_key0);
  69. ASSERT_EQ(blob_counter.value(), values[0]);
  70. CheckInFlow(blob_garbage_meter, first_blob_file_number, 1,
  71. first_expected_bytes);
  72. CheckInFlow(blob_garbage_meter, second_blob_file_number, 0, 0);
  73. blob_counter.Next();
  74. ASSERT_TRUE(blob_counter.Valid());
  75. ASSERT_OK(blob_counter.status());
  76. ASSERT_EQ(blob_counter.key(), keys[1]);
  77. ASSERT_EQ(blob_counter.user_key(), user_key1);
  78. ASSERT_EQ(blob_counter.value(), values[1]);
  79. CheckInFlow(blob_garbage_meter, first_blob_file_number, 1,
  80. first_expected_bytes);
  81. CheckInFlow(blob_garbage_meter, second_blob_file_number, 1,
  82. second_expected_bytes);
  83. blob_counter.Next();
  84. ASSERT_TRUE(blob_counter.Valid());
  85. ASSERT_OK(blob_counter.status());
  86. ASSERT_EQ(blob_counter.key(), keys[2]);
  87. ASSERT_EQ(blob_counter.user_key(), user_key2);
  88. ASSERT_EQ(blob_counter.value(), values[2]);
  89. CheckInFlow(blob_garbage_meter, first_blob_file_number, 1,
  90. first_expected_bytes);
  91. CheckInFlow(blob_garbage_meter, second_blob_file_number, 1,
  92. second_expected_bytes);
  93. blob_counter.Next();
  94. ASSERT_FALSE(blob_counter.Valid());
  95. ASSERT_OK(blob_counter.status());
  96. CheckInFlow(blob_garbage_meter, first_blob_file_number, 1,
  97. first_expected_bytes);
  98. CheckInFlow(blob_garbage_meter, second_blob_file_number, 1,
  99. second_expected_bytes);
  100. // Do it again using NextAndGetResult
  101. blob_counter.SeekToFirst();
  102. ASSERT_TRUE(blob_counter.Valid());
  103. ASSERT_OK(blob_counter.status());
  104. ASSERT_EQ(blob_counter.key(), keys[0]);
  105. ASSERT_EQ(blob_counter.user_key(), user_key0);
  106. ASSERT_EQ(blob_counter.value(), values[0]);
  107. CheckInFlow(blob_garbage_meter, first_blob_file_number, 2,
  108. 2 * first_expected_bytes);
  109. CheckInFlow(blob_garbage_meter, second_blob_file_number, 1,
  110. second_expected_bytes);
  111. {
  112. IterateResult result;
  113. ASSERT_TRUE(blob_counter.NextAndGetResult(&result));
  114. ASSERT_EQ(result.key, keys[1]);
  115. ASSERT_EQ(blob_counter.user_key(), user_key1);
  116. ASSERT_TRUE(blob_counter.Valid());
  117. ASSERT_OK(blob_counter.status());
  118. ASSERT_EQ(blob_counter.key(), keys[1]);
  119. ASSERT_EQ(blob_counter.value(), values[1]);
  120. CheckInFlow(blob_garbage_meter, first_blob_file_number, 2,
  121. 2 * first_expected_bytes);
  122. CheckInFlow(blob_garbage_meter, second_blob_file_number, 2,
  123. 2 * second_expected_bytes);
  124. }
  125. {
  126. IterateResult result;
  127. ASSERT_TRUE(blob_counter.NextAndGetResult(&result));
  128. ASSERT_EQ(result.key, keys[2]);
  129. ASSERT_EQ(blob_counter.user_key(), user_key2);
  130. ASSERT_TRUE(blob_counter.Valid());
  131. ASSERT_OK(blob_counter.status());
  132. ASSERT_EQ(blob_counter.key(), keys[2]);
  133. ASSERT_EQ(blob_counter.value(), values[2]);
  134. CheckInFlow(blob_garbage_meter, first_blob_file_number, 2,
  135. 2 * first_expected_bytes);
  136. CheckInFlow(blob_garbage_meter, second_blob_file_number, 2,
  137. 2 * second_expected_bytes);
  138. }
  139. {
  140. IterateResult result;
  141. ASSERT_FALSE(blob_counter.NextAndGetResult(&result));
  142. ASSERT_FALSE(blob_counter.Valid());
  143. ASSERT_OK(blob_counter.status());
  144. CheckInFlow(blob_garbage_meter, first_blob_file_number, 2,
  145. 2 * first_expected_bytes);
  146. CheckInFlow(blob_garbage_meter, second_blob_file_number, 2,
  147. 2 * second_expected_bytes);
  148. }
  149. // Call SeekToLast and iterate backward
  150. blob_counter.SeekToLast();
  151. ASSERT_TRUE(blob_counter.Valid());
  152. ASSERT_OK(blob_counter.status());
  153. ASSERT_EQ(blob_counter.key(), keys[2]);
  154. ASSERT_EQ(blob_counter.user_key(), user_key2);
  155. ASSERT_EQ(blob_counter.value(), values[2]);
  156. CheckInFlow(blob_garbage_meter, first_blob_file_number, 2,
  157. 2 * first_expected_bytes);
  158. CheckInFlow(blob_garbage_meter, second_blob_file_number, 2,
  159. 2 * second_expected_bytes);
  160. blob_counter.Prev();
  161. ASSERT_TRUE(blob_counter.Valid());
  162. ASSERT_OK(blob_counter.status());
  163. ASSERT_EQ(blob_counter.key(), keys[1]);
  164. ASSERT_EQ(blob_counter.user_key(), user_key1);
  165. ASSERT_EQ(blob_counter.value(), values[1]);
  166. CheckInFlow(blob_garbage_meter, first_blob_file_number, 2,
  167. 2 * first_expected_bytes);
  168. CheckInFlow(blob_garbage_meter, second_blob_file_number, 3,
  169. 3 * second_expected_bytes);
  170. blob_counter.Prev();
  171. ASSERT_TRUE(blob_counter.Valid());
  172. ASSERT_OK(blob_counter.status());
  173. ASSERT_EQ(blob_counter.key(), keys[0]);
  174. ASSERT_EQ(blob_counter.user_key(), user_key0);
  175. ASSERT_EQ(blob_counter.value(), values[0]);
  176. CheckInFlow(blob_garbage_meter, first_blob_file_number, 3,
  177. 3 * first_expected_bytes);
  178. CheckInFlow(blob_garbage_meter, second_blob_file_number, 3,
  179. 3 * second_expected_bytes);
  180. blob_counter.Prev();
  181. ASSERT_FALSE(blob_counter.Valid());
  182. ASSERT_OK(blob_counter.status());
  183. CheckInFlow(blob_garbage_meter, first_blob_file_number, 3,
  184. 3 * first_expected_bytes);
  185. CheckInFlow(blob_garbage_meter, second_blob_file_number, 3,
  186. 3 * second_expected_bytes);
  187. // Call Seek for all keys (plus one that's greater than all of them)
  188. blob_counter.Seek(keys[0]);
  189. ASSERT_TRUE(blob_counter.Valid());
  190. ASSERT_OK(blob_counter.status());
  191. ASSERT_EQ(blob_counter.key(), keys[0]);
  192. ASSERT_EQ(blob_counter.user_key(), user_key0);
  193. ASSERT_EQ(blob_counter.value(), values[0]);
  194. CheckInFlow(blob_garbage_meter, first_blob_file_number, 4,
  195. 4 * first_expected_bytes);
  196. CheckInFlow(blob_garbage_meter, second_blob_file_number, 3,
  197. 3 * second_expected_bytes);
  198. blob_counter.Seek(keys[1]);
  199. ASSERT_TRUE(blob_counter.Valid());
  200. ASSERT_OK(blob_counter.status());
  201. ASSERT_EQ(blob_counter.key(), keys[1]);
  202. ASSERT_EQ(blob_counter.user_key(), user_key1);
  203. ASSERT_EQ(blob_counter.value(), values[1]);
  204. CheckInFlow(blob_garbage_meter, first_blob_file_number, 4,
  205. 4 * first_expected_bytes);
  206. CheckInFlow(blob_garbage_meter, second_blob_file_number, 4,
  207. 4 * second_expected_bytes);
  208. blob_counter.Seek(keys[2]);
  209. ASSERT_TRUE(blob_counter.Valid());
  210. ASSERT_OK(blob_counter.status());
  211. ASSERT_EQ(blob_counter.key(), keys[2]);
  212. ASSERT_EQ(blob_counter.user_key(), user_key2);
  213. ASSERT_EQ(blob_counter.value(), values[2]);
  214. CheckInFlow(blob_garbage_meter, first_blob_file_number, 4,
  215. 4 * first_expected_bytes);
  216. CheckInFlow(blob_garbage_meter, second_blob_file_number, 4,
  217. 4 * second_expected_bytes);
  218. blob_counter.Seek("zzz");
  219. ASSERT_FALSE(blob_counter.Valid());
  220. ASSERT_OK(blob_counter.status());
  221. CheckInFlow(blob_garbage_meter, first_blob_file_number, 4,
  222. 4 * first_expected_bytes);
  223. CheckInFlow(blob_garbage_meter, second_blob_file_number, 4,
  224. 4 * second_expected_bytes);
  225. // Call SeekForPrev for all keys (plus one that's less than all of them)
  226. blob_counter.SeekForPrev("aaa");
  227. ASSERT_FALSE(blob_counter.Valid());
  228. ASSERT_OK(blob_counter.status());
  229. CheckInFlow(blob_garbage_meter, first_blob_file_number, 4,
  230. 4 * first_expected_bytes);
  231. CheckInFlow(blob_garbage_meter, second_blob_file_number, 4,
  232. 4 * second_expected_bytes);
  233. blob_counter.SeekForPrev(keys[0]);
  234. ASSERT_TRUE(blob_counter.Valid());
  235. ASSERT_OK(blob_counter.status());
  236. ASSERT_EQ(blob_counter.key(), keys[0]);
  237. ASSERT_EQ(blob_counter.user_key(), user_key0);
  238. ASSERT_EQ(blob_counter.value(), values[0]);
  239. CheckInFlow(blob_garbage_meter, first_blob_file_number, 5,
  240. 5 * first_expected_bytes);
  241. CheckInFlow(blob_garbage_meter, second_blob_file_number, 4,
  242. 4 * second_expected_bytes);
  243. blob_counter.SeekForPrev(keys[1]);
  244. ASSERT_TRUE(blob_counter.Valid());
  245. ASSERT_OK(blob_counter.status());
  246. ASSERT_EQ(blob_counter.key(), keys[1]);
  247. ASSERT_EQ(blob_counter.user_key(), user_key1);
  248. ASSERT_EQ(blob_counter.value(), values[1]);
  249. CheckInFlow(blob_garbage_meter, first_blob_file_number, 5,
  250. 5 * first_expected_bytes);
  251. CheckInFlow(blob_garbage_meter, second_blob_file_number, 5,
  252. 5 * second_expected_bytes);
  253. blob_counter.SeekForPrev(keys[2]);
  254. ASSERT_TRUE(blob_counter.Valid());
  255. ASSERT_OK(blob_counter.status());
  256. ASSERT_EQ(blob_counter.key(), keys[2]);
  257. ASSERT_EQ(blob_counter.user_key(), user_key2);
  258. ASSERT_EQ(blob_counter.value(), values[2]);
  259. CheckInFlow(blob_garbage_meter, first_blob_file_number, 5,
  260. 5 * first_expected_bytes);
  261. CheckInFlow(blob_garbage_meter, second_blob_file_number, 5,
  262. 5 * second_expected_bytes);
  263. }
  264. TEST(BlobCountingIteratorTest, CorruptBlobIndex) {
  265. const std::vector<std::string> keys{
  266. test::KeyStr("user_key", 1, kTypeBlobIndex)};
  267. const std::vector<std::string> values{"i_am_not_a_blob_index"};
  268. assert(keys.size() == values.size());
  269. VectorIterator input(keys, values);
  270. BlobGarbageMeter blob_garbage_meter;
  271. BlobCountingIterator blob_counter(&input, &blob_garbage_meter);
  272. blob_counter.SeekToFirst();
  273. ASSERT_FALSE(blob_counter.Valid());
  274. ASSERT_NOK(blob_counter.status());
  275. }
  276. } // namespace ROCKSDB_NAMESPACE
  277. int main(int argc, char** argv) {
  278. ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
  279. ::testing::InitGoogleTest(&argc, argv);
  280. return RUN_ALL_TESTS();
  281. }