transaction_test_util.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #pragma once
  6. #ifndef ROCKSDB_LITE
  7. #include "rocksdb/options.h"
  8. #include "port/port.h"
  9. #include "rocksdb/utilities/optimistic_transaction_db.h"
  10. #include "rocksdb/utilities/transaction_db.h"
  11. namespace ROCKSDB_NAMESPACE {
  12. class DB;
  13. class Random64;
  14. // Utility class for stress testing transactions. Can be used to write many
  15. // transactions in parallel and then validate that the data written is logically
  16. // consistent. This class assumes the input DB is initially empty.
  17. //
  18. // Each call to TransactionDBInsert()/OptimisticTransactionDBInsert() will
  19. // increment the value of a key in #num_sets sets of keys. Regardless of
  20. // whether the transaction succeeds, the total sum of values of keys in each
  21. // set is an invariant that should remain equal.
  22. //
  23. // After calling TransactionDBInsert()/OptimisticTransactionDBInsert() many
  24. // times, Verify() can be called to validate that the invariant holds.
  25. //
  26. // To test writing Transaction in parallel, multiple threads can create a
  27. // RandomTransactionInserter with similar arguments using the same DB.
  28. class RandomTransactionInserter {
  29. public:
  30. // num_keys is the number of keys in each set.
  31. // num_sets is the number of sets of keys.
  32. // cmt_delay_ms is the delay between prepare (if there is any) and commit
  33. // first_id is the id of the first transaction
  34. explicit RandomTransactionInserter(
  35. Random64* rand, const WriteOptions& write_options = WriteOptions(),
  36. const ReadOptions& read_options = ReadOptions(), uint64_t num_keys = 1000,
  37. uint16_t num_sets = 3, const uint64_t cmt_delay_ms = 0,
  38. const uint64_t first_id = 0);
  39. ~RandomTransactionInserter();
  40. // Increment a key in each set using a Transaction on a TransactionDB.
  41. //
  42. // Returns true if the transaction succeeded OR if any error encountered was
  43. // expected (eg a write-conflict). Error status may be obtained by calling
  44. // GetLastStatus();
  45. bool TransactionDBInsert(
  46. TransactionDB* db,
  47. const TransactionOptions& txn_options = TransactionOptions());
  48. // Increment a key in each set using a Transaction on an
  49. // OptimisticTransactionDB
  50. //
  51. // Returns true if the transaction succeeded OR if any error encountered was
  52. // expected (eg a write-conflict). Error status may be obtained by calling
  53. // GetLastStatus();
  54. bool OptimisticTransactionDBInsert(
  55. OptimisticTransactionDB* db,
  56. const OptimisticTransactionOptions& txn_options =
  57. OptimisticTransactionOptions());
  58. // Increment a key in each set without using a transaction. If this function
  59. // is called in parallel, then Verify() may fail.
  60. //
  61. // Returns true if the write succeeds.
  62. // Error status may be obtained by calling GetLastStatus().
  63. bool DBInsert(DB* db);
  64. // Get the ikey'th key from set set_i
  65. static Status DBGet(DB* db, Transaction* txn, ReadOptions& read_options,
  66. uint16_t set_i, uint64_t ikey, bool get_for_update,
  67. uint64_t* int_value, std::string* full_key,
  68. bool* unexpected_error);
  69. // Returns OK if Invariant is true.
  70. static Status Verify(DB* db, uint16_t num_sets, uint64_t num_keys_per_set = 0,
  71. bool take_snapshot = false, Random64* rand = nullptr,
  72. uint64_t delay_ms = 0);
  73. // Returns the status of the previous Insert operation
  74. Status GetLastStatus() { return last_status_; }
  75. // Returns the number of successfully written calls to
  76. // TransactionDBInsert/OptimisticTransactionDBInsert/DBInsert
  77. uint64_t GetSuccessCount() { return success_count_; }
  78. // Returns the number of calls to
  79. // TransactionDBInsert/OptimisticTransactionDBInsert/DBInsert that did not
  80. // write any data.
  81. uint64_t GetFailureCount() { return failure_count_; }
  82. // Returns the sum of user keys/values Put() to the DB.
  83. size_t GetBytesInserted() { return bytes_inserted_; }
  84. private:
  85. // Input options
  86. Random64* rand_;
  87. const WriteOptions write_options_;
  88. ReadOptions read_options_;
  89. const uint64_t num_keys_;
  90. const uint16_t num_sets_;
  91. // Number of successful insert batches performed
  92. uint64_t success_count_ = 0;
  93. // Number of failed insert batches attempted
  94. uint64_t failure_count_ = 0;
  95. size_t bytes_inserted_ = 0;
  96. // Status returned by most recent insert operation
  97. Status last_status_;
  98. // optimization: re-use allocated transaction objects.
  99. Transaction* txn_ = nullptr;
  100. Transaction* optimistic_txn_ = nullptr;
  101. uint64_t txn_id_;
  102. // The delay between ::Prepare and ::Commit
  103. const uint64_t cmt_delay_ms_;
  104. bool DoInsert(DB* db, Transaction* txn, bool is_optimistic);
  105. };
  106. } // namespace ROCKSDB_NAMESPACE
  107. #endif // ROCKSDB_LITE