You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
rocksdb/util/transaction_test_util.h

115 lines
4.0 KiB

// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
#pragma once
#ifndef ROCKSDB_LITE
#include "rocksdb/options.h"
#include "port/port.h"
#include "rocksdb/utilities/optimistic_transaction_db.h"
#include "rocksdb/utilities/transaction_db.h"
namespace rocksdb {
class DB;
class Random64;
// Utility class for stress testing transactions. Can be used to write many
// transactions in parallel and then validate that the data written is logically
// consistent. This class assumes the input DB is initially empty.
//
// Each call to TransactionDBInsert()/OptimisticTransactionDBInsert() will
// increment the value of a key in #num_sets sets of keys. Regardless of
// whether the transaction succeeds, the total sum of values of keys in each
// set is an invariant that should remain equal.
//
// After calling TransactionDBInsert()/OptimisticTransactionDBInsert() many
// times, Verify() can be called to validate that the invariant holds.
//
// To test writing Transaction in parallel, multiple threads can create a
// RandomTransactionInserter with similar arguments using the same DB.
class RandomTransactionInserter {
public:
// num_keys is the number of keys in each set.
// num_sets is the number of sets of keys.
explicit RandomTransactionInserter(
Random64* rand, const WriteOptions& write_options = WriteOptions(),
const ReadOptions& read_options = ReadOptions(), uint64_t num_keys = 1000,
uint16_t num_sets = 3);
~RandomTransactionInserter();
// Increment a key in each set using a Transaction on a TransactionDB.
//
// Returns true if the transaction succeeded OR if any error encountered was
// expected (eg a write-conflict). Error status may be obtained by calling
// GetLastStatus();
bool TransactionDBInsert(
TransactionDB* db,
const TransactionOptions& txn_options = TransactionOptions());
// Increment a key in each set using a Transaction on an
// OptimisticTransactionDB
//
// Returns true if the transaction succeeded OR if any error encountered was
// expected (eg a write-conflict). Error status may be obtained by calling
// GetLastStatus();
bool OptimisticTransactionDBInsert(
OptimisticTransactionDB* db,
const OptimisticTransactionOptions& txn_options =
OptimisticTransactionOptions());
// Increment a key in each set without using a transaction. If this function
// is called in parallel, then Verify() may fail.
//
// Returns true if the write succeeds.
// Error status may be obtained by calling GetLastStatus().
bool DBInsert(DB* db);
// Returns OK if Invariant is true.
static Status Verify(DB* db, uint16_t num_sets);
// Returns the status of the previous Insert operation
Status GetLastStatus() { return last_status_; }
// Returns the number of successfully written calls to
// TransactionDBInsert/OptimisticTransactionDBInsert/DBInsert
uint64_t GetSuccessCount() { return success_count_; }
// Returns the number of calls to
// TransactionDBInsert/OptimisticTransactionDBInsert/DBInsert that did not
// write any data.
uint64_t GetFailureCount() { return failure_count_; }
private:
// Input options
Random64* rand_;
const WriteOptions write_options_;
const ReadOptions read_options_;
const uint64_t num_keys_;
const uint16_t num_sets_;
// Number of successful insert batches performed
uint64_t success_count_ = 0;
// Number of failed insert batches attempted
uint64_t failure_count_ = 0;
// Status returned by most recent insert operation
Status last_status_;
// optimization: re-use allocated transaction objects.
Transaction* txn_ = nullptr;
Transaction* optimistic_txn_ = nullptr;
Optimize for serial commits in 2PC Summary: Throughput: 46k tps in our sysbench settings (filling the details later) The idea is to have the simplest change that gives us a reasonable boost in 2PC throughput. Major design changes: 1. The WAL file internal buffer is not flushed after each write. Instead it is flushed before critical operations (WAL copy via fs) or when FlushWAL is called by MySQL. Flushing the WAL buffer is also protected via mutex_. 2. Use two sequence numbers: last seq, and last seq for write. Last seq is the last visible sequence number for reads. Last seq for write is the next sequence number that should be used to write to WAL/memtable. This allows to have a memtable write be in parallel to WAL writes. 3. BatchGroup is not used for writes. This means that we can have parallel writers which changes a major assumption in the code base. To accommodate for that i) allow only 1 WriteImpl that intends to write to memtable via mem_mutex_--which is fine since in 2PC almost all of the memtable writes come via group commit phase which is serial anyway, ii) make all the parts in the code base that assumed to be the only writer (via EnterUnbatched) to also acquire mem_mutex_, iii) stat updates are protected via a stat_mutex_. Note: the first commit has the approach figured out but is not clean. Submitting the PR anyway to get the early feedback on the approach. If we are ok with the approach I will go ahead with this updates: 0) Rebase with Yi's pipelining changes 1) Currently batching is disabled by default to make sure that it will be consistent with all unit tests. Will make this optional via a config. 2) A couple of unit tests are disabled. They need to be updated with the serial commit of 2PC taken into account. 3) Replacing BatchGroup with mem_mutex_ got a bit ugly as it requires releasing mutex_ beforehand (the same way EnterUnbatched does). This needs to be cleaned up. Closes https://github.com/facebook/rocksdb/pull/2345 Differential Revision: D5210732 Pulled By: maysamyabandeh fbshipit-source-id: 78653bd95a35cd1e831e555e0e57bdfd695355a4
7 years ago
std::atomic<int> txn_id_;
bool DoInsert(DB* db, Transaction* txn, bool is_optimistic);
};
} // namespace rocksdb
#endif // ROCKSDB_LITE