2016-02-09 23:12:00 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2015-05-29 21:36:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
#include "rocksdb/db.h"
|
|
|
|
#include "rocksdb/options.h"
|
|
|
|
#include "rocksdb/slice.h"
|
Pessimistic Transactions
Summary:
Initial implementation of Pessimistic Transactions. This diff contains the api changes discussed in D38913. This diff is pretty large, so let me know if people would prefer to meet up to discuss it.
MyRocks folks: please take a look at the API in include/rocksdb/utilities/transaction[_db].h and let me know if you have any issues.
Also, you'll notice a couple of TODOs in the implementation of RollbackToSavePoint(). After chatting with Siying, I'm going to send out a separate diff for an alternate implementation of this feature that implements the rollback inside of WriteBatch/WriteBatchWithIndex. We can then decide which route is preferable.
Next, I'm planning on doing some perf testing and then integrating this diff into MongoRocks for further testing.
Test Plan: Unit tests, db_bench parallel testing.
Reviewers: igor, rven, sdong, yhchiang, yoshinorim
Reviewed By: sdong
Subscribers: hermanlee4, maykov, spetrunia, leveldb, dhruba
Differential Revision: https://reviews.facebook.net/D40869
2015-05-26 00:37:33 +00:00
|
|
|
#include "rocksdb/utilities/transaction.h"
|
|
|
|
#include "rocksdb/utilities/transaction_db.h"
|
2015-05-29 21:36:35 +00:00
|
|
|
|
2022-01-12 17:28:09 +00:00
|
|
|
using ROCKSDB_NAMESPACE::Options;
|
|
|
|
using ROCKSDB_NAMESPACE::ReadOptions;
|
|
|
|
using ROCKSDB_NAMESPACE::Snapshot;
|
|
|
|
using ROCKSDB_NAMESPACE::Status;
|
|
|
|
using ROCKSDB_NAMESPACE::Transaction;
|
|
|
|
using ROCKSDB_NAMESPACE::TransactionDB;
|
|
|
|
using ROCKSDB_NAMESPACE::TransactionDBOptions;
|
|
|
|
using ROCKSDB_NAMESPACE::TransactionOptions;
|
|
|
|
using ROCKSDB_NAMESPACE::WriteOptions;
|
2015-05-29 21:36:35 +00:00
|
|
|
|
2020-09-02 01:02:25 +00:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
std::string kDBPath = "C:\\Windows\\TEMP\\rocksdb_transaction_example";
|
|
|
|
#else
|
2015-05-29 21:36:35 +00:00
|
|
|
std::string kDBPath = "/tmp/rocksdb_transaction_example";
|
2020-09-02 01:02:25 +00:00
|
|
|
#endif
|
2015-05-29 21:36:35 +00:00
|
|
|
|
|
|
|
int main() {
|
|
|
|
// open DB
|
|
|
|
Options options;
|
Pessimistic Transactions
Summary:
Initial implementation of Pessimistic Transactions. This diff contains the api changes discussed in D38913. This diff is pretty large, so let me know if people would prefer to meet up to discuss it.
MyRocks folks: please take a look at the API in include/rocksdb/utilities/transaction[_db].h and let me know if you have any issues.
Also, you'll notice a couple of TODOs in the implementation of RollbackToSavePoint(). After chatting with Siying, I'm going to send out a separate diff for an alternate implementation of this feature that implements the rollback inside of WriteBatch/WriteBatchWithIndex. We can then decide which route is preferable.
Next, I'm planning on doing some perf testing and then integrating this diff into MongoRocks for further testing.
Test Plan: Unit tests, db_bench parallel testing.
Reviewers: igor, rven, sdong, yhchiang, yoshinorim
Reviewed By: sdong
Subscribers: hermanlee4, maykov, spetrunia, leveldb, dhruba
Differential Revision: https://reviews.facebook.net/D40869
2015-05-26 00:37:33 +00:00
|
|
|
TransactionDBOptions txn_db_options;
|
2015-05-29 21:36:35 +00:00
|
|
|
options.create_if_missing = true;
|
Pessimistic Transactions
Summary:
Initial implementation of Pessimistic Transactions. This diff contains the api changes discussed in D38913. This diff is pretty large, so let me know if people would prefer to meet up to discuss it.
MyRocks folks: please take a look at the API in include/rocksdb/utilities/transaction[_db].h and let me know if you have any issues.
Also, you'll notice a couple of TODOs in the implementation of RollbackToSavePoint(). After chatting with Siying, I'm going to send out a separate diff for an alternate implementation of this feature that implements the rollback inside of WriteBatch/WriteBatchWithIndex. We can then decide which route is preferable.
Next, I'm planning on doing some perf testing and then integrating this diff into MongoRocks for further testing.
Test Plan: Unit tests, db_bench parallel testing.
Reviewers: igor, rven, sdong, yhchiang, yoshinorim
Reviewed By: sdong
Subscribers: hermanlee4, maykov, spetrunia, leveldb, dhruba
Differential Revision: https://reviews.facebook.net/D40869
2015-05-26 00:37:33 +00:00
|
|
|
TransactionDB* txn_db;
|
2015-05-29 21:36:35 +00:00
|
|
|
|
Pessimistic Transactions
Summary:
Initial implementation of Pessimistic Transactions. This diff contains the api changes discussed in D38913. This diff is pretty large, so let me know if people would prefer to meet up to discuss it.
MyRocks folks: please take a look at the API in include/rocksdb/utilities/transaction[_db].h and let me know if you have any issues.
Also, you'll notice a couple of TODOs in the implementation of RollbackToSavePoint(). After chatting with Siying, I'm going to send out a separate diff for an alternate implementation of this feature that implements the rollback inside of WriteBatch/WriteBatchWithIndex. We can then decide which route is preferable.
Next, I'm planning on doing some perf testing and then integrating this diff into MongoRocks for further testing.
Test Plan: Unit tests, db_bench parallel testing.
Reviewers: igor, rven, sdong, yhchiang, yoshinorim
Reviewed By: sdong
Subscribers: hermanlee4, maykov, spetrunia, leveldb, dhruba
Differential Revision: https://reviews.facebook.net/D40869
2015-05-26 00:37:33 +00:00
|
|
|
Status s = TransactionDB::Open(options, txn_db_options, kDBPath, &txn_db);
|
2015-05-29 21:36:35 +00:00
|
|
|
assert(s.ok());
|
|
|
|
|
|
|
|
WriteOptions write_options;
|
|
|
|
ReadOptions read_options;
|
Pessimistic Transactions
Summary:
Initial implementation of Pessimistic Transactions. This diff contains the api changes discussed in D38913. This diff is pretty large, so let me know if people would prefer to meet up to discuss it.
MyRocks folks: please take a look at the API in include/rocksdb/utilities/transaction[_db].h and let me know if you have any issues.
Also, you'll notice a couple of TODOs in the implementation of RollbackToSavePoint(). After chatting with Siying, I'm going to send out a separate diff for an alternate implementation of this feature that implements the rollback inside of WriteBatch/WriteBatchWithIndex. We can then decide which route is preferable.
Next, I'm planning on doing some perf testing and then integrating this diff into MongoRocks for further testing.
Test Plan: Unit tests, db_bench parallel testing.
Reviewers: igor, rven, sdong, yhchiang, yoshinorim
Reviewed By: sdong
Subscribers: hermanlee4, maykov, spetrunia, leveldb, dhruba
Differential Revision: https://reviews.facebook.net/D40869
2015-05-26 00:37:33 +00:00
|
|
|
TransactionOptions txn_options;
|
2015-05-29 21:36:35 +00:00
|
|
|
std::string value;
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
//
|
2016-09-14 21:12:31 +00:00
|
|
|
// Simple Transaction Example ("Read Committed")
|
2015-05-29 21:36:35 +00:00
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// Start a transaction
|
Pessimistic Transactions
Summary:
Initial implementation of Pessimistic Transactions. This diff contains the api changes discussed in D38913. This diff is pretty large, so let me know if people would prefer to meet up to discuss it.
MyRocks folks: please take a look at the API in include/rocksdb/utilities/transaction[_db].h and let me know if you have any issues.
Also, you'll notice a couple of TODOs in the implementation of RollbackToSavePoint(). After chatting with Siying, I'm going to send out a separate diff for an alternate implementation of this feature that implements the rollback inside of WriteBatch/WriteBatchWithIndex. We can then decide which route is preferable.
Next, I'm planning on doing some perf testing and then integrating this diff into MongoRocks for further testing.
Test Plan: Unit tests, db_bench parallel testing.
Reviewers: igor, rven, sdong, yhchiang, yoshinorim
Reviewed By: sdong
Subscribers: hermanlee4, maykov, spetrunia, leveldb, dhruba
Differential Revision: https://reviews.facebook.net/D40869
2015-05-26 00:37:33 +00:00
|
|
|
Transaction* txn = txn_db->BeginTransaction(write_options);
|
2015-05-29 21:36:35 +00:00
|
|
|
assert(txn);
|
|
|
|
|
|
|
|
// Read a key in this transaction
|
|
|
|
s = txn->Get(read_options, "abc", &value);
|
|
|
|
assert(s.IsNotFound());
|
|
|
|
|
|
|
|
// Write a key in this transaction
|
Pessimistic Transactions
Summary:
Initial implementation of Pessimistic Transactions. This diff contains the api changes discussed in D38913. This diff is pretty large, so let me know if people would prefer to meet up to discuss it.
MyRocks folks: please take a look at the API in include/rocksdb/utilities/transaction[_db].h and let me know if you have any issues.
Also, you'll notice a couple of TODOs in the implementation of RollbackToSavePoint(). After chatting with Siying, I'm going to send out a separate diff for an alternate implementation of this feature that implements the rollback inside of WriteBatch/WriteBatchWithIndex. We can then decide which route is preferable.
Next, I'm planning on doing some perf testing and then integrating this diff into MongoRocks for further testing.
Test Plan: Unit tests, db_bench parallel testing.
Reviewers: igor, rven, sdong, yhchiang, yoshinorim
Reviewed By: sdong
Subscribers: hermanlee4, maykov, spetrunia, leveldb, dhruba
Differential Revision: https://reviews.facebook.net/D40869
2015-05-26 00:37:33 +00:00
|
|
|
s = txn->Put("abc", "def");
|
|
|
|
assert(s.ok());
|
2015-05-29 21:36:35 +00:00
|
|
|
|
|
|
|
// Read a key OUTSIDE this transaction. Does not affect txn.
|
Pessimistic Transactions
Summary:
Initial implementation of Pessimistic Transactions. This diff contains the api changes discussed in D38913. This diff is pretty large, so let me know if people would prefer to meet up to discuss it.
MyRocks folks: please take a look at the API in include/rocksdb/utilities/transaction[_db].h and let me know if you have any issues.
Also, you'll notice a couple of TODOs in the implementation of RollbackToSavePoint(). After chatting with Siying, I'm going to send out a separate diff for an alternate implementation of this feature that implements the rollback inside of WriteBatch/WriteBatchWithIndex. We can then decide which route is preferable.
Next, I'm planning on doing some perf testing and then integrating this diff into MongoRocks for further testing.
Test Plan: Unit tests, db_bench parallel testing.
Reviewers: igor, rven, sdong, yhchiang, yoshinorim
Reviewed By: sdong
Subscribers: hermanlee4, maykov, spetrunia, leveldb, dhruba
Differential Revision: https://reviews.facebook.net/D40869
2015-05-26 00:37:33 +00:00
|
|
|
s = txn_db->Get(read_options, "abc", &value);
|
2019-11-20 22:17:16 +00:00
|
|
|
assert(s.IsNotFound());
|
2015-05-29 21:36:35 +00:00
|
|
|
|
|
|
|
// Write a key OUTSIDE of this transaction.
|
2019-11-20 22:17:16 +00:00
|
|
|
// Does not affect txn since this is an unrelated key.
|
Pessimistic Transactions
Summary:
Initial implementation of Pessimistic Transactions. This diff contains the api changes discussed in D38913. This diff is pretty large, so let me know if people would prefer to meet up to discuss it.
MyRocks folks: please take a look at the API in include/rocksdb/utilities/transaction[_db].h and let me know if you have any issues.
Also, you'll notice a couple of TODOs in the implementation of RollbackToSavePoint(). After chatting with Siying, I'm going to send out a separate diff for an alternate implementation of this feature that implements the rollback inside of WriteBatch/WriteBatchWithIndex. We can then decide which route is preferable.
Next, I'm planning on doing some perf testing and then integrating this diff into MongoRocks for further testing.
Test Plan: Unit tests, db_bench parallel testing.
Reviewers: igor, rven, sdong, yhchiang, yoshinorim
Reviewed By: sdong
Subscribers: hermanlee4, maykov, spetrunia, leveldb, dhruba
Differential Revision: https://reviews.facebook.net/D40869
2015-05-26 00:37:33 +00:00
|
|
|
s = txn_db->Put(write_options, "xyz", "zzz");
|
2019-11-20 22:17:16 +00:00
|
|
|
assert(s.ok());
|
|
|
|
|
|
|
|
// Write a key OUTSIDE of this transaction.
|
|
|
|
// Fail because the key conflicts with the key written in txn.
|
|
|
|
s = txn_db->Put(write_options, "abc", "def");
|
|
|
|
assert(s.subcode() == Status::kLockTimeout);
|
|
|
|
|
|
|
|
// Value for key "xyz" has been committed, can be read in txn.
|
|
|
|
s = txn->Get(read_options, "xyz", &value);
|
|
|
|
assert(s.ok());
|
|
|
|
assert(value == "zzz");
|
2015-05-29 21:36:35 +00:00
|
|
|
|
|
|
|
// Commit transaction
|
|
|
|
s = txn->Commit();
|
|
|
|
assert(s.ok());
|
|
|
|
delete txn;
|
|
|
|
|
2019-11-20 22:17:16 +00:00
|
|
|
// Value is committed, can be read now.
|
|
|
|
s = txn_db->Get(read_options, "abc", &value);
|
|
|
|
assert(s.ok());
|
|
|
|
assert(value == "def");
|
|
|
|
|
2015-05-29 21:36:35 +00:00
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// "Repeatable Read" (Snapshot Isolation) Example
|
|
|
|
// -- Using a single Snapshot
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// Set a snapshot at start of transaction by setting set_snapshot=true
|
|
|
|
txn_options.set_snapshot = true;
|
|
|
|
txn = txn_db->BeginTransaction(write_options, txn_options);
|
|
|
|
|
|
|
|
const Snapshot* snapshot = txn->GetSnapshot();
|
|
|
|
|
|
|
|
// Write a key OUTSIDE of transaction
|
Pessimistic Transactions
Summary:
Initial implementation of Pessimistic Transactions. This diff contains the api changes discussed in D38913. This diff is pretty large, so let me know if people would prefer to meet up to discuss it.
MyRocks folks: please take a look at the API in include/rocksdb/utilities/transaction[_db].h and let me know if you have any issues.
Also, you'll notice a couple of TODOs in the implementation of RollbackToSavePoint(). After chatting with Siying, I'm going to send out a separate diff for an alternate implementation of this feature that implements the rollback inside of WriteBatch/WriteBatchWithIndex. We can then decide which route is preferable.
Next, I'm planning on doing some perf testing and then integrating this diff into MongoRocks for further testing.
Test Plan: Unit tests, db_bench parallel testing.
Reviewers: igor, rven, sdong, yhchiang, yoshinorim
Reviewed By: sdong
Subscribers: hermanlee4, maykov, spetrunia, leveldb, dhruba
Differential Revision: https://reviews.facebook.net/D40869
2015-05-26 00:37:33 +00:00
|
|
|
s = txn_db->Put(write_options, "abc", "xyz");
|
|
|
|
assert(s.ok());
|
2015-05-29 21:36:35 +00:00
|
|
|
|
2019-12-11 17:55:20 +00:00
|
|
|
// Read the latest committed value.
|
|
|
|
s = txn->Get(read_options, "abc", &value);
|
|
|
|
assert(s.ok());
|
|
|
|
assert(value == "xyz");
|
|
|
|
|
|
|
|
// Read the snapshotted value.
|
|
|
|
read_options.snapshot = snapshot;
|
|
|
|
s = txn->Get(read_options, "abc", &value);
|
|
|
|
assert(s.ok());
|
|
|
|
assert(value == "def");
|
|
|
|
|
Pessimistic Transactions
Summary:
Initial implementation of Pessimistic Transactions. This diff contains the api changes discussed in D38913. This diff is pretty large, so let me know if people would prefer to meet up to discuss it.
MyRocks folks: please take a look at the API in include/rocksdb/utilities/transaction[_db].h and let me know if you have any issues.
Also, you'll notice a couple of TODOs in the implementation of RollbackToSavePoint(). After chatting with Siying, I'm going to send out a separate diff for an alternate implementation of this feature that implements the rollback inside of WriteBatch/WriteBatchWithIndex. We can then decide which route is preferable.
Next, I'm planning on doing some perf testing and then integrating this diff into MongoRocks for further testing.
Test Plan: Unit tests, db_bench parallel testing.
Reviewers: igor, rven, sdong, yhchiang, yoshinorim
Reviewed By: sdong
Subscribers: hermanlee4, maykov, spetrunia, leveldb, dhruba
Differential Revision: https://reviews.facebook.net/D40869
2015-05-26 00:37:33 +00:00
|
|
|
// Attempt to read a key using the snapshot. This will fail since
|
|
|
|
// the previous write outside this txn conflicts with this read.
|
2015-05-29 21:36:35 +00:00
|
|
|
s = txn->GetForUpdate(read_options, "abc", &value);
|
|
|
|
assert(s.IsBusy());
|
|
|
|
|
Pessimistic Transactions
Summary:
Initial implementation of Pessimistic Transactions. This diff contains the api changes discussed in D38913. This diff is pretty large, so let me know if people would prefer to meet up to discuss it.
MyRocks folks: please take a look at the API in include/rocksdb/utilities/transaction[_db].h and let me know if you have any issues.
Also, you'll notice a couple of TODOs in the implementation of RollbackToSavePoint(). After chatting with Siying, I'm going to send out a separate diff for an alternate implementation of this feature that implements the rollback inside of WriteBatch/WriteBatchWithIndex. We can then decide which route is preferable.
Next, I'm planning on doing some perf testing and then integrating this diff into MongoRocks for further testing.
Test Plan: Unit tests, db_bench parallel testing.
Reviewers: igor, rven, sdong, yhchiang, yoshinorim
Reviewed By: sdong
Subscribers: hermanlee4, maykov, spetrunia, leveldb, dhruba
Differential Revision: https://reviews.facebook.net/D40869
2015-05-26 00:37:33 +00:00
|
|
|
txn->Rollback();
|
|
|
|
|
2019-12-11 17:55:20 +00:00
|
|
|
// Snapshot will be released upon deleting the transaction.
|
2015-05-29 21:36:35 +00:00
|
|
|
delete txn;
|
|
|
|
// Clear snapshot from read options since it is no longer valid
|
|
|
|
read_options.snapshot = nullptr;
|
|
|
|
snapshot = nullptr;
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// "Read Committed" (Monotonic Atomic Views) Example
|
|
|
|
// --Using multiple Snapshots
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// In this example, we set the snapshot multiple times. This is probably
|
|
|
|
// only necessary if you have very strict isolation requirements to
|
|
|
|
// implement.
|
|
|
|
|
|
|
|
// Set a snapshot at start of transaction
|
|
|
|
txn_options.set_snapshot = true;
|
|
|
|
txn = txn_db->BeginTransaction(write_options, txn_options);
|
|
|
|
|
|
|
|
// Do some reads and writes to key "x"
|
Pessimistic Transactions
Summary:
Initial implementation of Pessimistic Transactions. This diff contains the api changes discussed in D38913. This diff is pretty large, so let me know if people would prefer to meet up to discuss it.
MyRocks folks: please take a look at the API in include/rocksdb/utilities/transaction[_db].h and let me know if you have any issues.
Also, you'll notice a couple of TODOs in the implementation of RollbackToSavePoint(). After chatting with Siying, I'm going to send out a separate diff for an alternate implementation of this feature that implements the rollback inside of WriteBatch/WriteBatchWithIndex. We can then decide which route is preferable.
Next, I'm planning on doing some perf testing and then integrating this diff into MongoRocks for further testing.
Test Plan: Unit tests, db_bench parallel testing.
Reviewers: igor, rven, sdong, yhchiang, yoshinorim
Reviewed By: sdong
Subscribers: hermanlee4, maykov, spetrunia, leveldb, dhruba
Differential Revision: https://reviews.facebook.net/D40869
2015-05-26 00:37:33 +00:00
|
|
|
read_options.snapshot = txn_db->GetSnapshot();
|
2015-05-29 21:36:35 +00:00
|
|
|
s = txn->Get(read_options, "x", &value);
|
2019-12-11 17:55:20 +00:00
|
|
|
assert(s.IsNotFound());
|
|
|
|
s = txn->Put("x", "x");
|
|
|
|
assert(s.ok());
|
2015-05-29 21:36:35 +00:00
|
|
|
|
|
|
|
// Do a write outside of the transaction to key "y"
|
2019-12-11 17:55:20 +00:00
|
|
|
s = txn_db->Put(write_options, "y", "y1");
|
|
|
|
assert(s.ok());
|
2015-05-29 21:36:35 +00:00
|
|
|
|
|
|
|
// Set a new snapshot in the transaction
|
|
|
|
txn->SetSnapshot();
|
Pessimistic Transactions
Summary:
Initial implementation of Pessimistic Transactions. This diff contains the api changes discussed in D38913. This diff is pretty large, so let me know if people would prefer to meet up to discuss it.
MyRocks folks: please take a look at the API in include/rocksdb/utilities/transaction[_db].h and let me know if you have any issues.
Also, you'll notice a couple of TODOs in the implementation of RollbackToSavePoint(). After chatting with Siying, I'm going to send out a separate diff for an alternate implementation of this feature that implements the rollback inside of WriteBatch/WriteBatchWithIndex. We can then decide which route is preferable.
Next, I'm planning on doing some perf testing and then integrating this diff into MongoRocks for further testing.
Test Plan: Unit tests, db_bench parallel testing.
Reviewers: igor, rven, sdong, yhchiang, yoshinorim
Reviewed By: sdong
Subscribers: hermanlee4, maykov, spetrunia, leveldb, dhruba
Differential Revision: https://reviews.facebook.net/D40869
2015-05-26 00:37:33 +00:00
|
|
|
txn->SetSavePoint();
|
|
|
|
read_options.snapshot = txn_db->GetSnapshot();
|
2015-05-29 21:36:35 +00:00
|
|
|
|
|
|
|
// Do some reads and writes to key "y"
|
Pessimistic Transactions
Summary:
Initial implementation of Pessimistic Transactions. This diff contains the api changes discussed in D38913. This diff is pretty large, so let me know if people would prefer to meet up to discuss it.
MyRocks folks: please take a look at the API in include/rocksdb/utilities/transaction[_db].h and let me know if you have any issues.
Also, you'll notice a couple of TODOs in the implementation of RollbackToSavePoint(). After chatting with Siying, I'm going to send out a separate diff for an alternate implementation of this feature that implements the rollback inside of WriteBatch/WriteBatchWithIndex. We can then decide which route is preferable.
Next, I'm planning on doing some perf testing and then integrating this diff into MongoRocks for further testing.
Test Plan: Unit tests, db_bench parallel testing.
Reviewers: igor, rven, sdong, yhchiang, yoshinorim
Reviewed By: sdong
Subscribers: hermanlee4, maykov, spetrunia, leveldb, dhruba
Differential Revision: https://reviews.facebook.net/D40869
2015-05-26 00:37:33 +00:00
|
|
|
// Since the snapshot was advanced, the write done outside of the
|
|
|
|
// transaction does not conflict.
|
2015-05-29 21:36:35 +00:00
|
|
|
s = txn->GetForUpdate(read_options, "y", &value);
|
2019-12-11 17:55:20 +00:00
|
|
|
assert(s.ok());
|
|
|
|
assert(value == "y1");
|
|
|
|
s = txn->Put("y", "y2");
|
|
|
|
assert(s.ok());
|
2015-05-29 21:36:35 +00:00
|
|
|
|
Pessimistic Transactions
Summary:
Initial implementation of Pessimistic Transactions. This diff contains the api changes discussed in D38913. This diff is pretty large, so let me know if people would prefer to meet up to discuss it.
MyRocks folks: please take a look at the API in include/rocksdb/utilities/transaction[_db].h and let me know if you have any issues.
Also, you'll notice a couple of TODOs in the implementation of RollbackToSavePoint(). After chatting with Siying, I'm going to send out a separate diff for an alternate implementation of this feature that implements the rollback inside of WriteBatch/WriteBatchWithIndex. We can then decide which route is preferable.
Next, I'm planning on doing some perf testing and then integrating this diff into MongoRocks for further testing.
Test Plan: Unit tests, db_bench parallel testing.
Reviewers: igor, rven, sdong, yhchiang, yoshinorim
Reviewed By: sdong
Subscribers: hermanlee4, maykov, spetrunia, leveldb, dhruba
Differential Revision: https://reviews.facebook.net/D40869
2015-05-26 00:37:33 +00:00
|
|
|
// Decide we want to revert the last write from this transaction.
|
|
|
|
txn->RollbackToSavePoint();
|
|
|
|
|
|
|
|
// Commit.
|
2015-05-29 21:36:35 +00:00
|
|
|
s = txn->Commit();
|
|
|
|
assert(s.ok());
|
|
|
|
delete txn;
|
|
|
|
// Clear snapshot from read options since it is no longer valid
|
|
|
|
read_options.snapshot = nullptr;
|
|
|
|
|
2019-12-11 17:55:20 +00:00
|
|
|
// db state is at the save point.
|
|
|
|
s = txn_db->Get(read_options, "x", &value);
|
|
|
|
assert(s.ok());
|
|
|
|
assert(value == "x");
|
|
|
|
|
|
|
|
s = txn_db->Get(read_options, "y", &value);
|
|
|
|
assert(s.ok());
|
|
|
|
assert(value == "y1");
|
|
|
|
|
2015-05-29 21:36:35 +00:00
|
|
|
// Cleanup
|
|
|
|
delete txn_db;
|
2022-01-12 17:28:09 +00:00
|
|
|
ROCKSDB_NAMESPACE::DestroyDB(kDBPath, options);
|
2015-05-29 21:36:35 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|