db_statistics_test.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 <string>
  6. #include "db/db_test_util.h"
  7. #include "db/write_batch_internal.h"
  8. #include "monitoring/thread_status_util.h"
  9. #include "port/stack_trace.h"
  10. #include "rocksdb/statistics.h"
  11. #include "rocksdb/utilities/transaction_db.h"
  12. #include "util/random.h"
  13. namespace ROCKSDB_NAMESPACE {
  14. class DBStatisticsTest : public DBTestBase {
  15. public:
  16. DBStatisticsTest()
  17. : DBTestBase("db_statistics_test", /*env_do_fsync=*/true) {}
  18. };
  19. TEST_F(DBStatisticsTest, CompressionStatsTest) {
  20. for (CompressionType type : GetSupportedCompressions()) {
  21. if (type == kNoCompression) {
  22. continue;
  23. }
  24. if (type == kBZip2Compression) {
  25. // Weird behavior in this test
  26. continue;
  27. }
  28. SCOPED_TRACE("Compression type: " + std::to_string(type));
  29. Options options = CurrentOptions();
  30. options.compression = type;
  31. options.statistics = ROCKSDB_NAMESPACE::CreateDBStatistics();
  32. options.statistics->set_stats_level(StatsLevel::kExceptTimeForMutex);
  33. BlockBasedTableOptions bbto;
  34. bbto.enable_index_compression = false;
  35. options.table_factory.reset(NewBlockBasedTableFactory(bbto));
  36. DestroyAndReopen(options);
  37. auto PopStat = [&](Tickers t) -> uint64_t {
  38. return options.statistics->getAndResetTickerCount(t);
  39. };
  40. int kNumKeysWritten = 100;
  41. double compress_to = 0.5;
  42. // About three KVs per block
  43. int len = static_cast<int>(BlockBasedTableOptions().block_size / 3);
  44. int uncomp_est = kNumKeysWritten * (len + 20);
  45. Random rnd(301);
  46. std::string buf;
  47. // Check that compressions occur and are counted when compression is turned
  48. // on
  49. for (int i = 0; i < kNumKeysWritten; ++i) {
  50. ASSERT_OK(
  51. Put(Key(i), test::CompressibleString(&rnd, compress_to, len, &buf)));
  52. }
  53. ASSERT_OK(Flush());
  54. EXPECT_EQ(34, PopStat(NUMBER_BLOCK_COMPRESSED));
  55. EXPECT_NEAR2(uncomp_est, PopStat(BYTES_COMPRESSED_FROM), uncomp_est / 10);
  56. EXPECT_NEAR2(uncomp_est * compress_to, PopStat(BYTES_COMPRESSED_TO),
  57. uncomp_est / 10);
  58. EXPECT_EQ(0, PopStat(NUMBER_BLOCK_DECOMPRESSED));
  59. EXPECT_EQ(0, PopStat(BYTES_DECOMPRESSED_FROM));
  60. EXPECT_EQ(0, PopStat(BYTES_DECOMPRESSED_TO));
  61. // And decompressions
  62. for (int i = 0; i < kNumKeysWritten; ++i) {
  63. auto r = Get(Key(i));
  64. }
  65. EXPECT_EQ(34, PopStat(NUMBER_BLOCK_DECOMPRESSED));
  66. EXPECT_NEAR2(uncomp_est, PopStat(BYTES_DECOMPRESSED_TO), uncomp_est / 10);
  67. EXPECT_NEAR2(uncomp_est * compress_to, PopStat(BYTES_DECOMPRESSED_FROM),
  68. uncomp_est / 10);
  69. EXPECT_EQ(0, PopStat(BYTES_COMPRESSION_BYPASSED));
  70. EXPECT_EQ(0, PopStat(BYTES_COMPRESSION_REJECTED));
  71. EXPECT_EQ(0, PopStat(NUMBER_BLOCK_COMPRESSION_BYPASSED));
  72. EXPECT_EQ(0, PopStat(NUMBER_BLOCK_COMPRESSION_REJECTED));
  73. // Check when compression is rejected.
  74. DestroyAndReopen(options);
  75. for (int i = 0; i < kNumKeysWritten; ++i) {
  76. ASSERT_OK(Put(Key(i), rnd.RandomBinaryString(len)));
  77. }
  78. ASSERT_OK(Flush());
  79. for (int i = 0; i < kNumKeysWritten; ++i) {
  80. auto r = Get(Key(i));
  81. }
  82. EXPECT_EQ(34, PopStat(NUMBER_BLOCK_COMPRESSION_REJECTED));
  83. EXPECT_NEAR2(uncomp_est, PopStat(BYTES_COMPRESSION_REJECTED),
  84. uncomp_est / 10);
  85. EXPECT_EQ(0, PopStat(NUMBER_BLOCK_COMPRESSED));
  86. EXPECT_EQ(0, PopStat(NUMBER_BLOCK_COMPRESSION_BYPASSED));
  87. EXPECT_EQ(0, PopStat(NUMBER_BLOCK_DECOMPRESSED));
  88. EXPECT_EQ(0, PopStat(BYTES_COMPRESSED_FROM));
  89. EXPECT_EQ(0, PopStat(BYTES_COMPRESSED_TO));
  90. EXPECT_EQ(0, PopStat(BYTES_COMPRESSION_BYPASSED));
  91. EXPECT_EQ(0, PopStat(BYTES_DECOMPRESSED_FROM));
  92. EXPECT_EQ(0, PopStat(BYTES_DECOMPRESSED_TO));
  93. // Check when compression is disabled.
  94. options.compression = kNoCompression;
  95. DestroyAndReopen(options);
  96. for (int i = 0; i < kNumKeysWritten; ++i) {
  97. ASSERT_OK(Put(Key(i), rnd.RandomBinaryString(len)));
  98. }
  99. ASSERT_OK(Flush());
  100. for (int i = 0; i < kNumKeysWritten; ++i) {
  101. auto r = Get(Key(i));
  102. }
  103. EXPECT_EQ(34, PopStat(NUMBER_BLOCK_COMPRESSION_BYPASSED));
  104. EXPECT_NEAR2(uncomp_est, PopStat(BYTES_COMPRESSION_BYPASSED),
  105. uncomp_est / 10);
  106. EXPECT_EQ(0, PopStat(NUMBER_BLOCK_COMPRESSED));
  107. EXPECT_EQ(0, PopStat(NUMBER_BLOCK_COMPRESSION_REJECTED));
  108. EXPECT_EQ(0, PopStat(NUMBER_BLOCK_DECOMPRESSED));
  109. EXPECT_EQ(0, PopStat(BYTES_COMPRESSED_FROM));
  110. EXPECT_EQ(0, PopStat(BYTES_COMPRESSED_TO));
  111. EXPECT_EQ(0, PopStat(BYTES_COMPRESSION_REJECTED));
  112. EXPECT_EQ(0, PopStat(BYTES_DECOMPRESSED_FROM));
  113. EXPECT_EQ(0, PopStat(BYTES_DECOMPRESSED_TO));
  114. }
  115. }
  116. TEST_F(DBStatisticsTest, MutexWaitStatsDisabledByDefault) {
  117. Options options = CurrentOptions();
  118. options.create_if_missing = true;
  119. options.statistics = ROCKSDB_NAMESPACE::CreateDBStatistics();
  120. CreateAndReopenWithCF({"pikachu"}, options);
  121. const uint64_t kMutexWaitDelay = 100;
  122. ThreadStatusUtil::TEST_SetStateDelay(ThreadStatus::STATE_MUTEX_WAIT,
  123. kMutexWaitDelay);
  124. ASSERT_OK(Put("hello", "rocksdb"));
  125. ASSERT_EQ(TestGetTickerCount(options, DB_MUTEX_WAIT_MICROS), 0);
  126. ThreadStatusUtil::TEST_SetStateDelay(ThreadStatus::STATE_MUTEX_WAIT, 0);
  127. }
  128. TEST_F(DBStatisticsTest, MutexWaitStats) {
  129. Options options = CurrentOptions();
  130. options.create_if_missing = true;
  131. options.statistics = ROCKSDB_NAMESPACE::CreateDBStatistics();
  132. options.statistics->set_stats_level(StatsLevel::kAll);
  133. CreateAndReopenWithCF({"pikachu"}, options);
  134. const uint64_t kMutexWaitDelay = 100;
  135. ThreadStatusUtil::TEST_SetStateDelay(ThreadStatus::STATE_MUTEX_WAIT,
  136. kMutexWaitDelay);
  137. ASSERT_OK(Put("hello", "rocksdb"));
  138. ASSERT_GE(TestGetTickerCount(options, DB_MUTEX_WAIT_MICROS), kMutexWaitDelay);
  139. ThreadStatusUtil::TEST_SetStateDelay(ThreadStatus::STATE_MUTEX_WAIT, 0);
  140. }
  141. TEST_F(DBStatisticsTest, ResetStats) {
  142. Options options = CurrentOptions();
  143. options.create_if_missing = true;
  144. options.statistics = ROCKSDB_NAMESPACE::CreateDBStatistics();
  145. DestroyAndReopen(options);
  146. for (int i = 0; i < 2; ++i) {
  147. // pick arbitrary ticker and histogram. On first iteration they're zero
  148. // because db is unused. On second iteration they're zero due to Reset().
  149. ASSERT_EQ(0, TestGetTickerCount(options, NUMBER_KEYS_WRITTEN));
  150. HistogramData histogram_data;
  151. options.statistics->histogramData(DB_WRITE, &histogram_data);
  152. ASSERT_EQ(0.0, histogram_data.max);
  153. if (i == 0) {
  154. // The Put() makes some of the ticker/histogram stats nonzero until we
  155. // Reset().
  156. ASSERT_OK(Put("hello", "rocksdb"));
  157. ASSERT_EQ(1, TestGetTickerCount(options, NUMBER_KEYS_WRITTEN));
  158. options.statistics->histogramData(DB_WRITE, &histogram_data);
  159. ASSERT_GT(histogram_data.max, 0.0);
  160. ASSERT_OK(options.statistics->Reset());
  161. }
  162. }
  163. }
  164. TEST_F(DBStatisticsTest, ExcludeTickers) {
  165. Options options = CurrentOptions();
  166. options.statistics = ROCKSDB_NAMESPACE::CreateDBStatistics();
  167. DestroyAndReopen(options);
  168. options.statistics->set_stats_level(StatsLevel::kExceptTickers);
  169. ASSERT_OK(Put("foo", "value"));
  170. ASSERT_EQ(0, options.statistics->getTickerCount(BYTES_WRITTEN));
  171. options.statistics->set_stats_level(StatsLevel::kExceptHistogramOrTimers);
  172. Reopen(options);
  173. ASSERT_EQ("value", Get("foo"));
  174. ASSERT_GT(options.statistics->getTickerCount(BYTES_READ), 0);
  175. }
  176. TEST_F(DBStatisticsTest, VerifyChecksumReadStat) {
  177. Options options = CurrentOptions();
  178. options.file_checksum_gen_factory = GetFileChecksumGenCrc32cFactory();
  179. options.statistics = ROCKSDB_NAMESPACE::CreateDBStatistics();
  180. Reopen(options);
  181. // Expected to be populated regardless of `PerfLevel` in user thread
  182. SetPerfLevel(kDisable);
  183. {
  184. // Scenario 0: only WAL data. Not verified so require ticker to be zero.
  185. ASSERT_OK(Put("foo", "value"));
  186. ASSERT_OK(db_->VerifyFileChecksums(ReadOptions()));
  187. ASSERT_OK(db_->VerifyChecksum());
  188. ASSERT_EQ(0,
  189. options.statistics->getTickerCount(VERIFY_CHECKSUM_READ_BYTES));
  190. }
  191. // Create one SST.
  192. ASSERT_OK(Flush());
  193. std::unordered_map<std::string, uint64_t> table_files;
  194. uint64_t table_files_size = 0;
  195. ASSERT_OK(GetAllDataFiles(kTableFile, &table_files, &table_files_size));
  196. {
  197. // Scenario 1: Table verified in `VerifyFileChecksums()`. This should read
  198. // the whole file so we require the ticker stat exactly matches the file
  199. // size.
  200. ASSERT_OK(options.statistics->Reset());
  201. ASSERT_OK(db_->VerifyFileChecksums(ReadOptions()));
  202. ASSERT_EQ(table_files_size,
  203. options.statistics->getTickerCount(VERIFY_CHECKSUM_READ_BYTES));
  204. }
  205. {
  206. // Scenario 2: Table verified in `VerifyChecksum()`. This opens a
  207. // `TableReader` to verify each block. It can involve duplicate reads of the
  208. // same data so we set a lower-bound only.
  209. ASSERT_OK(options.statistics->Reset());
  210. ASSERT_OK(db_->VerifyChecksum());
  211. ASSERT_GE(options.statistics->getTickerCount(VERIFY_CHECKSUM_READ_BYTES),
  212. table_files_size);
  213. }
  214. }
  215. TEST_F(DBStatisticsTest, BlockChecksumStats) {
  216. Options options = CurrentOptions();
  217. options.statistics = ROCKSDB_NAMESPACE::CreateDBStatistics();
  218. Reopen(options);
  219. // Scenario 0: only WAL data. Not verified so require ticker to be zero.
  220. ASSERT_OK(Put("foo", "value"));
  221. ASSERT_OK(db_->VerifyChecksum());
  222. ASSERT_EQ(0,
  223. options.statistics->getTickerCount(BLOCK_CHECKSUM_COMPUTE_COUNT));
  224. ASSERT_EQ(0,
  225. options.statistics->getTickerCount(BLOCK_CHECKSUM_MISMATCH_COUNT));
  226. // Scenario 1: Flushed table verified in `VerifyChecksum()`. This opens a
  227. // `TableReader` to verify each of the four blocks (meta-index, table
  228. // properties, index, and data block).
  229. ASSERT_OK(Flush());
  230. ASSERT_OK(options.statistics->Reset());
  231. ASSERT_OK(db_->VerifyChecksum());
  232. ASSERT_EQ(4,
  233. options.statistics->getTickerCount(BLOCK_CHECKSUM_COMPUTE_COUNT));
  234. ASSERT_EQ(0,
  235. options.statistics->getTickerCount(BLOCK_CHECKSUM_MISMATCH_COUNT));
  236. // Scenario 2: Corrupted table verified in `VerifyChecksum()`. The corruption
  237. // is in the fourth and final verified block, i.e., the data block.
  238. std::unordered_map<std::string, uint64_t> table_files;
  239. ASSERT_OK(GetAllDataFiles(kTableFile, &table_files));
  240. ASSERT_EQ(1, table_files.size());
  241. std::string table_name = table_files.begin()->first;
  242. // Assumes the data block starts at offset zero.
  243. ASSERT_OK(test::CorruptFile(options.env, table_name, 0 /* offset */,
  244. 3 /* bytes_to_corrupt */));
  245. ASSERT_OK(options.statistics->Reset());
  246. ASSERT_NOK(db_->VerifyChecksum());
  247. ASSERT_EQ(4,
  248. options.statistics->getTickerCount(BLOCK_CHECKSUM_COMPUTE_COUNT));
  249. ASSERT_EQ(1,
  250. options.statistics->getTickerCount(BLOCK_CHECKSUM_MISMATCH_COUNT));
  251. }
  252. TEST_F(DBStatisticsTest, BytesWrittenStats) {
  253. Options options = CurrentOptions();
  254. options.statistics = ROCKSDB_NAMESPACE::CreateDBStatistics();
  255. options.statistics->set_stats_level(StatsLevel::kExceptHistogramOrTimers);
  256. Reopen(options);
  257. EXPECT_EQ(0, options.statistics->getAndResetTickerCount(WAL_FILE_BYTES));
  258. EXPECT_EQ(0, options.statistics->getAndResetTickerCount(BYTES_WRITTEN));
  259. const int kNumKeysWritten = 100;
  260. // Scenario 0: Not using transactions.
  261. // This will write to WAL and memtable directly.
  262. ASSERT_OK(options.statistics->Reset());
  263. for (int i = 0; i < kNumKeysWritten; ++i) {
  264. ASSERT_OK(Put(Key(i), "val"));
  265. }
  266. EXPECT_EQ(options.statistics->getAndResetTickerCount(WAL_FILE_BYTES),
  267. options.statistics->getAndResetTickerCount(BYTES_WRITTEN));
  268. // Scenario 1: Using transactions.
  269. // This should not double count BYTES_WRITTEN (issue #12061).
  270. for (bool enable_pipelined_write : {false, true}) {
  271. ASSERT_OK(options.statistics->Reset());
  272. // Destroy the DB to recreate as a TransactionDB.
  273. Destroy(options, true);
  274. // Create a TransactionDB.
  275. TransactionDB* txn_db = nullptr;
  276. TransactionDBOptions txn_db_opts;
  277. txn_db_opts.write_policy = TxnDBWritePolicy::WRITE_COMMITTED;
  278. options.enable_pipelined_write = enable_pipelined_write;
  279. ASSERT_OK(TransactionDB::Open(options, txn_db_opts, dbname_, &txn_db));
  280. ASSERT_NE(txn_db, nullptr);
  281. db_ = txn_db->GetBaseDB();
  282. WriteOptions wopts;
  283. TransactionOptions txn_opts;
  284. Transaction* txn = txn_db->BeginTransaction(wopts, txn_opts, nullptr);
  285. ASSERT_NE(txn, nullptr);
  286. ASSERT_OK(txn->SetName("txn1"));
  287. for (int i = 0; i < kNumKeysWritten; ++i) {
  288. ASSERT_OK(txn->Put(Key(i), "val"));
  289. }
  290. // Prepare() writes to WAL, but not to memtable. (WriteCommitted)
  291. ASSERT_OK(txn->Prepare());
  292. EXPECT_NE(0, options.statistics->getTickerCount(WAL_FILE_BYTES));
  293. // BYTES_WRITTEN would have been non-zero previously (issue #12061).
  294. EXPECT_EQ(0, options.statistics->getTickerCount(BYTES_WRITTEN));
  295. // Commit() writes to memtable and also a commit marker to WAL.
  296. ASSERT_OK(txn->Commit());
  297. delete txn;
  298. // The WAL has an extra header of size `kHeader` written to it,
  299. // as we are writing twice to it (first during Prepare, second during
  300. // Commit).
  301. EXPECT_EQ(options.statistics->getAndResetTickerCount(WAL_FILE_BYTES),
  302. options.statistics->getAndResetTickerCount(BYTES_WRITTEN) +
  303. WriteBatchInternal::kHeader);
  304. // Cleanup
  305. db_ = nullptr;
  306. delete txn_db;
  307. }
  308. }
  309. } // namespace ROCKSDB_NAMESPACE
  310. int main(int argc, char** argv) {
  311. ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
  312. ::testing::InitGoogleTest(&argc, argv);
  313. return RUN_ALL_TESTS();
  314. }