fork of https://github.com/oxigraph/rocksdb and https://github.com/facebook/rocksdb for nextgraph and oxigraph
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.
597 lines
19 KiB
597 lines
19 KiB
9 years ago
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||
7 years ago
|
// 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).
|
||
10 years ago
|
|
||
|
#ifndef ROCKSDB_LITE
|
||
|
|
||
7 years ago
|
#include "utilities/transactions/pessimistic_transaction.h"
|
||
10 years ago
|
|
||
|
#include <map>
|
||
|
#include <set>
|
||
|
#include <string>
|
||
|
#include <vector>
|
||
|
|
||
|
#include "db/column_family.h"
|
||
|
#include "db/db_impl.h"
|
||
|
#include "rocksdb/comparator.h"
|
||
|
#include "rocksdb/db.h"
|
||
9 years ago
|
#include "rocksdb/snapshot.h"
|
||
10 years ago
|
#include "rocksdb/status.h"
|
||
|
#include "rocksdb/utilities/transaction_db.h"
|
||
7 years ago
|
#include "util/cast_util.h"
|
||
10 years ago
|
#include "util/string_util.h"
|
||
9 years ago
|
#include "util/sync_point.h"
|
||
7 years ago
|
#include "utilities/transactions/pessimistic_transaction_db.h"
|
||
10 years ago
|
#include "utilities/transactions/transaction_util.h"
|
||
|
|
||
|
namespace rocksdb {
|
||
|
|
||
|
struct WriteOptions;
|
||
|
|
||
7 years ago
|
std::atomic<TransactionID> PessimisticTransaction::txn_id_counter_(1);
|
||
10 years ago
|
|
||
7 years ago
|
TransactionID PessimisticTransaction::GenTxnID() {
|
||
10 years ago
|
return txn_id_counter_.fetch_add(1);
|
||
|
}
|
||
|
|
||
7 years ago
|
PessimisticTransaction::PessimisticTransaction(TransactionDB* txn_db,
|
||
7 years ago
|
const WriteOptions& write_options,
|
||
|
const TransactionOptions& txn_options)
|
||
8 years ago
|
: TransactionBaseImpl(txn_db->GetRootDB(), write_options),
|
||
10 years ago
|
txn_db_impl_(nullptr),
|
||
7 years ago
|
expiration_time_(0),
|
||
9 years ago
|
txn_id_(0),
|
||
8 years ago
|
waiting_cf_id_(0),
|
||
|
waiting_key_(nullptr),
|
||
8 years ago
|
lock_timeout_(0),
|
||
|
deadlock_detect_(false),
|
||
|
deadlock_detect_depth_(0) {
|
||
7 years ago
|
txn_db_impl_ =
|
||
7 years ago
|
static_cast_with_check<PessimisticTransactionDB, TransactionDB>(txn_db);
|
||
7 years ago
|
db_impl_ = static_cast_with_check<DBImpl, DB>(db_);
|
||
9 years ago
|
Initialize(txn_options);
|
||
|
}
|
||
|
|
||
7 years ago
|
void PessimisticTransaction::Initialize(const TransactionOptions& txn_options) {
|
||
9 years ago
|
txn_id_ = GenTxnID();
|
||
|
|
||
8 years ago
|
txn_state_ = STARTED;
|
||
9 years ago
|
|
||
8 years ago
|
deadlock_detect_ = txn_options.deadlock_detect;
|
||
|
deadlock_detect_depth_ = txn_options.deadlock_detect_depth;
|
||
8 years ago
|
write_batch_.SetMaxBytes(txn_options.max_write_batch_size);
|
||
8 years ago
|
|
||
9 years ago
|
lock_timeout_ = txn_options.lock_timeout * 1000;
|
||
10 years ago
|
if (lock_timeout_ < 0) {
|
||
|
// Lock timeout not set, use default
|
||
9 years ago
|
lock_timeout_ =
|
||
|
txn_db_impl_->GetTxnDBOptions().transaction_lock_timeout * 1000;
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
if (txn_options.expiration >= 0) {
|
||
|
expiration_time_ = start_time_ + txn_options.expiration * 1000;
|
||
|
} else {
|
||
|
expiration_time_ = 0;
|
||
|
}
|
||
|
|
||
10 years ago
|
if (txn_options.set_snapshot) {
|
||
|
SetSnapshot();
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
if (expiration_time_ > 0) {
|
||
|
txn_db_impl_->InsertExpirableTransaction(txn_id_, this);
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
7 years ago
|
PessimisticTransaction::~PessimisticTransaction() {
|
||
9 years ago
|
txn_db_impl_->UnLock(this, &GetTrackedKeys());
|
||
9 years ago
|
if (expiration_time_ > 0) {
|
||
|
txn_db_impl_->RemoveExpirableTransaction(txn_id_);
|
||
|
}
|
||
8 years ago
|
if (!name_.empty() && txn_state_ != COMMITED) {
|
||
9 years ago
|
txn_db_impl_->UnregisterTransaction(this);
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
7 years ago
|
void PessimisticTransaction::Clear() {
|
||
9 years ago
|
txn_db_impl_->UnLock(this, &GetTrackedKeys());
|
||
9 years ago
|
TransactionBaseImpl::Clear();
|
||
10 years ago
|
}
|
||
|
|
||
7 years ago
|
void PessimisticTransaction::Reinitialize(TransactionDB* txn_db,
|
||
7 years ago
|
const WriteOptions& write_options,
|
||
|
const TransactionOptions& txn_options) {
|
||
8 years ago
|
if (!name_.empty() && txn_state_ != COMMITED) {
|
||
9 years ago
|
txn_db_impl_->UnregisterTransaction(this);
|
||
|
}
|
||
8 years ago
|
TransactionBaseImpl::Reinitialize(txn_db->GetRootDB(), write_options);
|
||
9 years ago
|
Initialize(txn_options);
|
||
|
}
|
||
|
|
||
7 years ago
|
bool PessimisticTransaction::IsExpired() const {
|
||
10 years ago
|
if (expiration_time_ > 0) {
|
||
9 years ago
|
if (db_->GetEnv()->NowMicros() >= expiration_time_) {
|
||
10 years ago
|
// Transaction is expired.
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
7 years ago
|
WriteCommittedTxn::WriteCommittedTxn(
|
||
7 years ago
|
TransactionDB* txn_db, const WriteOptions& write_options,
|
||
|
const TransactionOptions& txn_options)
|
||
7 years ago
|
: PessimisticTransaction(txn_db, write_options, txn_options){};
|
||
7 years ago
|
|
||
7 years ago
|
Status WriteCommittedTxn::CommitBatch(WriteBatch* batch) {
|
||
10 years ago
|
TransactionKeyMap keys_to_unlock;
|
||
|
Status s = LockBatch(batch, &keys_to_unlock);
|
||
|
|
||
9 years ago
|
if (!s.ok()) {
|
||
|
return s;
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
bool can_commit = false;
|
||
|
|
||
|
if (IsExpired()) {
|
||
|
s = Status::Expired();
|
||
|
} else if (expiration_time_ > 0) {
|
||
8 years ago
|
TransactionState expected = STARTED;
|
||
|
can_commit = std::atomic_compare_exchange_strong(&txn_state_, &expected,
|
||
9 years ago
|
AWAITING_COMMIT);
|
||
8 years ago
|
} else if (txn_state_ == STARTED) {
|
||
9 years ago
|
// lock stealing is not a concern
|
||
|
can_commit = true;
|
||
|
}
|
||
|
|
||
|
if (can_commit) {
|
||
8 years ago
|
txn_state_.store(AWAITING_COMMIT);
|
||
9 years ago
|
s = db_->Write(write_options_, batch);
|
||
|
if (s.ok()) {
|
||
8 years ago
|
txn_state_.store(COMMITED);
|
||
9 years ago
|
}
|
||
8 years ago
|
} else if (txn_state_ == LOCKS_STOLEN) {
|
||
9 years ago
|
s = Status::Expired();
|
||
|
} else {
|
||
|
s = Status::InvalidArgument("Transaction is not in state for commit.");
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
txn_db_impl_->UnLock(this, &keys_to_unlock);
|
||
|
|
||
10 years ago
|
return s;
|
||
|
}
|
||
|
|
||
7 years ago
|
Status PessimisticTransaction::Prepare() {
|
||
9 years ago
|
Status s;
|
||
|
|
||
|
if (name_.empty()) {
|
||
|
return Status::InvalidArgument(
|
||
|
"Cannot prepare a transaction that has not been named.");
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
if (IsExpired()) {
|
||
|
return Status::Expired();
|
||
|
}
|
||
|
|
||
|
bool can_prepare = false;
|
||
|
|
||
|
if (expiration_time_ > 0) {
|
||
|
// must concern ourselves with expiraton and/or lock stealing
|
||
|
// need to compare/exchange bc locks could be stolen under us here
|
||
8 years ago
|
TransactionState expected = STARTED;
|
||
|
can_prepare = std::atomic_compare_exchange_strong(&txn_state_, &expected,
|
||
9 years ago
|
AWAITING_PREPARE);
|
||
8 years ago
|
} else if (txn_state_ == STARTED) {
|
||
9 years ago
|
// expiration and lock stealing is not possible
|
||
|
can_prepare = true;
|
||
|
}
|
||
|
|
||
|
if (can_prepare) {
|
||
8 years ago
|
txn_state_.store(AWAITING_PREPARE);
|
||
9 years ago
|
// transaction can't expire after preparation
|
||
|
expiration_time_ = 0;
|
||
7 years ago
|
s = PrepareInternal();
|
||
9 years ago
|
if (s.ok()) {
|
||
|
assert(log_number_ != 0);
|
||
|
dbimpl_->MarkLogAsContainingPrepSection(log_number_);
|
||
8 years ago
|
txn_state_.store(PREPARED);
|
||
9 years ago
|
}
|
||
8 years ago
|
} else if (txn_state_ == LOCKS_STOLEN) {
|
||
9 years ago
|
s = Status::Expired();
|
||
8 years ago
|
} else if (txn_state_ == PREPARED) {
|
||
9 years ago
|
s = Status::InvalidArgument("Transaction has already been prepared.");
|
||
8 years ago
|
} else if (txn_state_ == COMMITED) {
|
||
9 years ago
|
s = Status::InvalidArgument("Transaction has already been committed.");
|
||
8 years ago
|
} else if (txn_state_ == ROLLEDBACK) {
|
||
9 years ago
|
s = Status::InvalidArgument("Transaction has already been rolledback.");
|
||
|
} else {
|
||
|
s = Status::InvalidArgument("Transaction is not in state for commit.");
|
||
|
}
|
||
10 years ago
|
|
||
|
return s;
|
||
|
}
|
||
|
|
||
7 years ago
|
Status WriteCommittedTxn::PrepareInternal() {
|
||
|
WriteOptions write_options = write_options_;
|
||
|
write_options.disableWAL = false;
|
||
|
WriteBatchInternal::MarkEndPrepare(GetWriteBatch()->GetWriteBatch(), name_);
|
||
|
Status s =
|
||
|
db_impl_->WriteImpl(write_options, GetWriteBatch()->GetWriteBatch(),
|
||
|
/*callback*/ nullptr, &log_number_, /*log ref*/ 0,
|
||
|
/* disable_memtable*/ true);
|
||
|
return s;
|
||
|
}
|
||
|
|
||
|
Status PessimisticTransaction::Commit() {
|
||
10 years ago
|
Status s;
|
||
7 years ago
|
bool commit_without_prepare = false;
|
||
9 years ago
|
bool commit_prepared = false;
|
||
10 years ago
|
|
||
9 years ago
|
if (IsExpired()) {
|
||
|
return Status::Expired();
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
if (expiration_time_ > 0) {
|
||
|
// we must atomicaly compare and exchange the state here because at
|
||
|
// this state in the transaction it is possible for another thread
|
||
|
// to change our state out from under us in the even that we expire and have
|
||
|
// our locks stolen. In this case the only valid state is STARTED because
|
||
|
// a state of PREPARED would have a cleared expiration_time_.
|
||
8 years ago
|
TransactionState expected = STARTED;
|
||
7 years ago
|
commit_without_prepare = std::atomic_compare_exchange_strong(
|
||
|
&txn_state_, &expected, AWAITING_COMMIT);
|
||
9 years ago
|
TEST_SYNC_POINT("TransactionTest::ExpirableTransactionDataRace:1");
|
||
8 years ago
|
} else if (txn_state_ == PREPARED) {
|
||
9 years ago
|
// expiration and lock stealing is not a concern
|
||
|
commit_prepared = true;
|
||
8 years ago
|
} else if (txn_state_ == STARTED) {
|
||
9 years ago
|
// expiration and lock stealing is not a concern
|
||
7 years ago
|
commit_without_prepare = true;
|
||
|
// TODO(myabandeh): what if the user mistakenly forgets prepare? We should
|
||
|
// add an option so that the user explictly express the intention of
|
||
|
// skipping the prepare phase.
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
7 years ago
|
if (commit_without_prepare) {
|
||
9 years ago
|
assert(!commit_prepared);
|
||
|
if (WriteBatchInternal::Count(GetCommitTimeWriteBatch()) > 0) {
|
||
|
s = Status::InvalidArgument(
|
||
|
"Commit-time batch contains values that will not be committed.");
|
||
9 years ago
|
} else {
|
||
8 years ago
|
txn_state_.store(AWAITING_COMMIT);
|
||
7 years ago
|
s = CommitWithoutPrepareInternal();
|
||
9 years ago
|
Clear();
|
||
|
if (s.ok()) {
|
||
8 years ago
|
txn_state_.store(COMMITED);
|
||
9 years ago
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
} else if (commit_prepared) {
|
||
8 years ago
|
txn_state_.store(AWAITING_COMMIT);
|
||
9 years ago
|
|
||
7 years ago
|
s = CommitInternal();
|
||
8 years ago
|
|
||
9 years ago
|
if (!s.ok()) {
|
||
8 years ago
|
ROCKS_LOG_WARN(db_impl_->immutable_db_options().info_log,
|
||
|
"Commit write failed");
|
||
9 years ago
|
return s;
|
||
|
}
|
||
|
|
||
|
// FindObsoleteFiles must now look to the memtables
|
||
|
// to determine what prep logs must be kept around,
|
||
|
// not the prep section heap.
|
||
|
assert(log_number_ > 0);
|
||
|
dbimpl_->MarkLogAsHavingPrepSectionFlushed(log_number_);
|
||
|
txn_db_impl_->UnregisterTransaction(this);
|
||
|
|
||
|
Clear();
|
||
8 years ago
|
txn_state_.store(COMMITED);
|
||
|
} else if (txn_state_ == LOCKS_STOLEN) {
|
||
9 years ago
|
s = Status::Expired();
|
||
8 years ago
|
} else if (txn_state_ == COMMITED) {
|
||
9 years ago
|
s = Status::InvalidArgument("Transaction has already been committed.");
|
||
8 years ago
|
} else if (txn_state_ == ROLLEDBACK) {
|
||
9 years ago
|
s = Status::InvalidArgument("Transaction has already been rolledback.");
|
||
10 years ago
|
} else {
|
||
9 years ago
|
s = Status::InvalidArgument("Transaction is not in state for commit.");
|
||
10 years ago
|
}
|
||
|
|
||
|
return s;
|
||
|
}
|
||
|
|
||
7 years ago
|
Status WriteCommittedTxn::CommitWithoutPrepareInternal() {
|
||
|
Status s = db_->Write(write_options_, GetWriteBatch()->GetWriteBatch());
|
||
|
return s;
|
||
|
}
|
||
|
|
||
|
Status WriteCommittedTxn::CommitInternal() {
|
||
|
// We take the commit-time batch and append the Commit marker.
|
||
|
// The Memtable will ignore the Commit marker in non-recovery mode
|
||
|
WriteBatch* working_batch = GetCommitTimeWriteBatch();
|
||
|
WriteBatchInternal::MarkCommit(working_batch, name_);
|
||
|
|
||
|
// any operations appended to this working_batch will be ignored from WAL
|
||
|
working_batch->MarkWalTerminationPoint();
|
||
|
|
||
|
// insert prepared batch into Memtable only skipping WAL.
|
||
|
// Memtable will ignore BeginPrepare/EndPrepare markers
|
||
|
// in non recovery mode and simply insert the values
|
||
|
WriteBatchInternal::Append(working_batch, GetWriteBatch()->GetWriteBatch());
|
||
|
|
||
|
auto s = db_impl_->WriteImpl(write_options_, working_batch, nullptr, nullptr,
|
||
|
log_number_);
|
||
|
return s;
|
||
|
}
|
||
|
|
||
|
Status WriteCommittedTxn::Rollback() {
|
||
9 years ago
|
Status s;
|
||
8 years ago
|
if (txn_state_ == PREPARED) {
|
||
9 years ago
|
WriteBatch rollback_marker;
|
||
|
WriteBatchInternal::MarkRollback(&rollback_marker, name_);
|
||
8 years ago
|
txn_state_.store(AWAITING_ROLLBACK);
|
||
9 years ago
|
s = db_impl_->WriteImpl(write_options_, &rollback_marker);
|
||
|
if (s.ok()) {
|
||
|
// we do not need to keep our prepared section around
|
||
|
assert(log_number_ > 0);
|
||
|
dbimpl_->MarkLogAsHavingPrepSectionFlushed(log_number_);
|
||
|
Clear();
|
||
8 years ago
|
txn_state_.store(ROLLEDBACK);
|
||
9 years ago
|
}
|
||
8 years ago
|
} else if (txn_state_ == STARTED) {
|
||
9 years ago
|
// prepare couldn't have taken place
|
||
|
Clear();
|
||
8 years ago
|
} else if (txn_state_ == COMMITED) {
|
||
9 years ago
|
s = Status::InvalidArgument("This transaction has already been committed.");
|
||
|
} else {
|
||
|
s = Status::InvalidArgument(
|
||
|
"Two phase transaction is not in state for rollback.");
|
||
|
}
|
||
|
|
||
|
return s;
|
||
|
}
|
||
10 years ago
|
|
||
7 years ago
|
Status PessimisticTransaction::RollbackToSavePoint() {
|
||
8 years ago
|
if (txn_state_ != STARTED) {
|
||
9 years ago
|
return Status::InvalidArgument("Transaction is beyond state for rollback.");
|
||
|
}
|
||
|
|
||
9 years ago
|
// Unlock any keys locked since last transaction
|
||
9 years ago
|
const std::unique_ptr<TransactionKeyMap>& keys =
|
||
|
GetTrackedKeysSinceSavePoint();
|
||
|
|
||
9 years ago
|
if (keys) {
|
||
9 years ago
|
txn_db_impl_->UnLock(this, keys.get());
|
||
9 years ago
|
}
|
||
|
|
||
|
return TransactionBaseImpl::RollbackToSavePoint();
|
||
|
}
|
||
|
|
||
10 years ago
|
// Lock all keys in this batch.
|
||
|
// On success, caller should unlock keys_to_unlock
|
||
7 years ago
|
Status PessimisticTransaction::LockBatch(WriteBatch* batch,
|
||
7 years ago
|
TransactionKeyMap* keys_to_unlock) {
|
||
10 years ago
|
class Handler : public WriteBatch::Handler {
|
||
|
public:
|
||
|
// Sorted map of column_family_id to sorted set of keys.
|
||
|
// Since LockBatch() always locks keys in sorted order, it cannot deadlock
|
||
|
// with itself. We're not using a comparator here since it doesn't matter
|
||
|
// what the sorting is as long as it's consistent.
|
||
|
std::map<uint32_t, std::set<std::string>> keys_;
|
||
|
|
||
|
Handler() {}
|
||
|
|
||
|
void RecordKey(uint32_t column_family_id, const Slice& key) {
|
||
|
std::string key_str = key.ToString();
|
||
|
|
||
|
auto iter = (keys_)[column_family_id].find(key_str);
|
||
|
if (iter == (keys_)[column_family_id].end()) {
|
||
|
// key not yet seen, store it.
|
||
|
(keys_)[column_family_id].insert({std::move(key_str)});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
virtual Status PutCF(uint32_t column_family_id, const Slice& key,
|
||
7 years ago
|
const Slice& /* unused */) override {
|
||
10 years ago
|
RecordKey(column_family_id, key);
|
||
|
return Status::OK();
|
||
|
}
|
||
|
virtual Status MergeCF(uint32_t column_family_id, const Slice& key,
|
||
7 years ago
|
const Slice& /* unused */) override {
|
||
10 years ago
|
RecordKey(column_family_id, key);
|
||
|
return Status::OK();
|
||
|
}
|
||
|
virtual Status DeleteCF(uint32_t column_family_id,
|
||
|
const Slice& key) override {
|
||
|
RecordKey(column_family_id, key);
|
||
|
return Status::OK();
|
||
|
}
|
||
|
};
|
||
|
|
||
|
// Iterating on this handler will add all keys in this batch into keys
|
||
|
Handler handler;
|
||
|
batch->Iterate(&handler);
|
||
|
|
||
|
Status s;
|
||
|
|
||
|
// Attempt to lock all keys
|
||
|
for (const auto& cf_iter : handler.keys_) {
|
||
|
uint32_t cfh_id = cf_iter.first;
|
||
|
auto& cfh_keys = cf_iter.second;
|
||
|
|
||
|
for (const auto& key_iter : cfh_keys) {
|
||
|
const std::string& key = key_iter;
|
||
|
|
||
8 years ago
|
s = txn_db_impl_->TryLock(this, cfh_id, key, true /* exclusive */);
|
||
10 years ago
|
if (!s.ok()) {
|
||
|
break;
|
||
|
}
|
||
9 years ago
|
TrackKey(keys_to_unlock, cfh_id, std::move(key), kMaxSequenceNumber,
|
||
8 years ago
|
false, true /* exclusive */);
|
||
10 years ago
|
}
|
||
|
|
||
|
if (!s.ok()) {
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (!s.ok()) {
|
||
|
txn_db_impl_->UnLock(this, keys_to_unlock);
|
||
|
}
|
||
|
|
||
|
return s;
|
||
|
}
|
||
|
|
||
|
// Attempt to lock this key.
|
||
|
// Returns OK if the key has been successfully locked. Non-ok, otherwise.
|
||
|
// If check_shapshot is true and this transaction has a snapshot set,
|
||
|
// this key will only be locked if there have been no writes to this key since
|
||
|
// the snapshot time.
|
||
7 years ago
|
Status PessimisticTransaction::TryLock(ColumnFamilyHandle* column_family,
|
||
7 years ago
|
const Slice& key, bool read_only, bool exclusive,
|
||
|
bool untracked) {
|
||
10 years ago
|
uint32_t cfh_id = GetColumnFamilyID(column_family);
|
||
|
std::string key_str = key.ToString();
|
||
|
bool previously_locked;
|
||
8 years ago
|
bool lock_upgrade = false;
|
||
10 years ago
|
Status s;
|
||
|
|
||
9 years ago
|
// lock this key if this transactions hasn't already locked it
|
||
|
SequenceNumber current_seqno = kMaxSequenceNumber;
|
||
|
SequenceNumber new_seqno = kMaxSequenceNumber;
|
||
9 years ago
|
|
||
|
const auto& tracked_keys = GetTrackedKeys();
|
||
|
const auto tracked_keys_cf = tracked_keys.find(cfh_id);
|
||
|
if (tracked_keys_cf == tracked_keys.end()) {
|
||
10 years ago
|
previously_locked = false;
|
||
9 years ago
|
} else {
|
||
|
auto iter = tracked_keys_cf->second.find(key_str);
|
||
|
if (iter == tracked_keys_cf->second.end()) {
|
||
|
previously_locked = false;
|
||
|
} else {
|
||
8 years ago
|
if (!iter->second.exclusive && exclusive) {
|
||
|
lock_upgrade = true;
|
||
|
}
|
||
9 years ago
|
previously_locked = true;
|
||
9 years ago
|
current_seqno = iter->second.seq;
|
||
9 years ago
|
}
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
// Lock this key if this transactions hasn't already locked it or we require
|
||
|
// an upgrade.
|
||
|
if (!previously_locked || lock_upgrade) {
|
||
8 years ago
|
s = txn_db_impl_->TryLock(this, cfh_id, key_str, exclusive);
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
SetSnapshotIfNeeded();
|
||
|
|
||
|
// Even though we do not care about doing conflict checking for this write,
|
||
|
// we still need to take a lock to make sure we do not cause a conflict with
|
||
|
// some other write. However, we do not need to check if there have been
|
||
|
// any writes since this transaction's snapshot.
|
||
|
// TODO(agiardullo): could optimize by supporting shared txn locks in the
|
||
|
// future
|
||
|
if (untracked || snapshot_ == nullptr) {
|
||
|
// Need to remember the earliest sequence number that we know that this
|
||
|
// key has not been modified after. This is useful if this same
|
||
|
// transaction
|
||
|
// later tries to lock this key again.
|
||
|
if (current_seqno == kMaxSequenceNumber) {
|
||
|
// Since we haven't checked a snapshot, we only know this key has not
|
||
|
// been modified since after we locked it.
|
||
|
new_seqno = db_->GetLatestSequenceNumber();
|
||
|
} else {
|
||
|
new_seqno = current_seqno;
|
||
|
}
|
||
|
} else {
|
||
10 years ago
|
// If a snapshot is set, we need to make sure the key hasn't been modified
|
||
|
// since the snapshot. This must be done after we locked the key.
|
||
9 years ago
|
if (s.ok()) {
|
||
|
s = ValidateSnapshot(column_family, key, current_seqno, &new_seqno);
|
||
|
|
||
|
if (!s.ok()) {
|
||
|
// Failed to validate key
|
||
|
if (!previously_locked) {
|
||
|
// Unlock key we just locked
|
||
8 years ago
|
if (lock_upgrade) {
|
||
|
s = txn_db_impl_->TryLock(this, cfh_id, key_str,
|
||
|
false /* exclusive */);
|
||
|
assert(s.ok());
|
||
|
} else {
|
||
|
txn_db_impl_->UnLock(this, cfh_id, key.ToString());
|
||
|
}
|
||
10 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
if (s.ok()) {
|
||
|
// Let base class know we've conflict checked this key.
|
||
8 years ago
|
TrackKey(cfh_id, key_str, new_seqno, read_only, exclusive);
|
||
9 years ago
|
}
|
||
|
|
||
10 years ago
|
return s;
|
||
|
}
|
||
|
|
||
|
// Return OK() if this key has not been modified more recently than the
|
||
|
// transaction snapshot_.
|
||
7 years ago
|
Status PessimisticTransaction::ValidateSnapshot(ColumnFamilyHandle* column_family,
|
||
7 years ago
|
const Slice& key,
|
||
|
SequenceNumber prev_seqno,
|
||
|
SequenceNumber* new_seqno) {
|
||
9 years ago
|
assert(snapshot_);
|
||
|
|
||
9 years ago
|
SequenceNumber seq = snapshot_->GetSequenceNumber();
|
||
9 years ago
|
if (prev_seqno <= seq) {
|
||
|
// If the key has been previous validated at a sequence number earlier
|
||
|
// than the curent snapshot's sequence number, we already know it has not
|
||
|
// been modified.
|
||
|
return Status::OK();
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
*new_seqno = seq;
|
||
10 years ago
|
|
||
9 years ago
|
ColumnFamilyHandle* cfh =
|
||
7 years ago
|
column_family ? column_family : db_impl_->DefaultColumnFamily();
|
||
10 years ago
|
|
||
7 years ago
|
return TransactionUtil::CheckKeyForConflicts(db_impl_, cfh, key.ToString(),
|
||
9 years ago
|
snapshot_->GetSequenceNumber(),
|
||
|
false /* cache_only */);
|
||
10 years ago
|
}
|
||
|
|
||
7 years ago
|
bool PessimisticTransaction::TryStealingLocks() {
|
||
9 years ago
|
assert(IsExpired());
|
||
8 years ago
|
TransactionState expected = STARTED;
|
||
|
return std::atomic_compare_exchange_strong(&txn_state_, &expected,
|
||
9 years ago
|
LOCKS_STOLEN);
|
||
|
}
|
||
|
|
||
7 years ago
|
void PessimisticTransaction::UnlockGetForUpdate(ColumnFamilyHandle* column_family,
|
||
7 years ago
|
const Slice& key) {
|
||
9 years ago
|
txn_db_impl_->UnLock(this, GetColumnFamilyID(column_family), key.ToString());
|
||
|
}
|
||
|
|
||
7 years ago
|
Status PessimisticTransaction::SetName(const TransactionName& name) {
|
||
9 years ago
|
Status s;
|
||
8 years ago
|
if (txn_state_ == STARTED) {
|
||
9 years ago
|
if (name_.length()) {
|
||
|
s = Status::InvalidArgument("Transaction has already been named.");
|
||
|
} else if (txn_db_impl_->GetTransactionByName(name) != nullptr) {
|
||
|
s = Status::InvalidArgument("Transaction name must be unique.");
|
||
|
} else if (name.length() < 1 || name.length() > 512) {
|
||
|
s = Status::InvalidArgument(
|
||
|
"Transaction name length must be between 1 and 512 chars.");
|
||
|
} else {
|
||
|
name_ = name;
|
||
|
txn_db_impl_->RegisterTransaction(this);
|
||
|
}
|
||
|
} else {
|
||
|
s = Status::InvalidArgument("Transaction is beyond state for naming.");
|
||
|
}
|
||
|
return s;
|
||
|
}
|
||
|
|
||
10 years ago
|
} // namespace rocksdb
|
||
|
|
||
|
#endif // ROCKSDB_LITE
|