2017-08-03 17:46:47 +02:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
|
|
|
// 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).
|
|
|
|
|
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
|
2017-08-08 01:07:40 +02:00
|
|
|
#include "utilities/transactions/write_prepared_txn.h"
|
2017-08-03 17:46:47 +02:00
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
#include "db/column_family.h"
|
|
|
|
#include "db/db_impl.h"
|
|
|
|
#include "rocksdb/db.h"
|
|
|
|
#include "rocksdb/status.h"
|
|
|
|
#include "rocksdb/utilities/transaction_db.h"
|
2017-08-08 01:07:40 +02:00
|
|
|
#include "utilities/transactions/pessimistic_transaction.h"
|
2017-11-02 19:05:55 +01:00
|
|
|
#include "utilities/transactions/write_prepared_txn_db.h"
|
2017-08-03 17:46:47 +02:00
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
|
|
|
struct WriteOptions;
|
|
|
|
|
2017-08-17 01:49:11 +02:00
|
|
|
WritePreparedTxn::WritePreparedTxn(WritePreparedTxnDB* txn_db,
|
|
|
|
const WriteOptions& write_options,
|
|
|
|
const TransactionOptions& txn_options)
|
|
|
|
: PessimisticTransaction(txn_db, write_options, txn_options),
|
|
|
|
wpt_db_(txn_db) {
|
2017-10-05 16:28:37 +02:00
|
|
|
GetWriteBatch()->DisableDuplicateMergeKeys();
|
2017-08-03 17:46:47 +02:00
|
|
|
}
|
|
|
|
|
2017-09-11 17:58:52 +02:00
|
|
|
Status WritePreparedTxn::Get(const ReadOptions& read_options,
|
|
|
|
ColumnFamilyHandle* column_family,
|
|
|
|
const Slice& key, PinnableSlice* pinnable_val) {
|
2017-10-06 23:18:30 +02:00
|
|
|
auto snapshot = read_options.snapshot;
|
2017-09-11 17:58:52 +02:00
|
|
|
auto snap_seq =
|
|
|
|
snapshot != nullptr ? snapshot->GetSequenceNumber() : kMaxSequenceNumber;
|
|
|
|
|
|
|
|
WritePreparedTxnReadCallback callback(wpt_db_, snap_seq);
|
|
|
|
return write_batch_.GetFromBatchAndDB(db_, read_options, column_family, key,
|
|
|
|
pinnable_val, &callback);
|
|
|
|
}
|
|
|
|
|
2017-10-10 02:05:34 +02:00
|
|
|
Iterator* WritePreparedTxn::GetIterator(const ReadOptions& options) {
|
|
|
|
// Make sure to get iterator from WritePrepareTxnDB, not the root db.
|
|
|
|
Iterator* db_iter = wpt_db_->NewIterator(options);
|
|
|
|
assert(db_iter);
|
|
|
|
|
|
|
|
return write_batch_.NewIteratorWithBase(db_iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
Iterator* WritePreparedTxn::GetIterator(const ReadOptions& options,
|
|
|
|
ColumnFamilyHandle* column_family) {
|
|
|
|
// Make sure to get iterator from WritePrepareTxnDB, not the root db.
|
|
|
|
Iterator* db_iter = wpt_db_->NewIterator(options, column_family);
|
|
|
|
assert(db_iter);
|
|
|
|
|
|
|
|
return write_batch_.NewIteratorWithBase(db_iter);
|
|
|
|
}
|
|
|
|
|
2017-08-08 01:07:40 +02:00
|
|
|
Status WritePreparedTxn::PrepareInternal() {
|
2017-08-17 01:49:11 +02:00
|
|
|
WriteOptions write_options = write_options_;
|
|
|
|
write_options.disableWAL = false;
|
|
|
|
WriteBatchInternal::MarkEndPrepare(GetWriteBatch()->GetWriteBatch(), name_);
|
|
|
|
const bool disable_memtable = true;
|
2017-09-18 23:36:53 +02:00
|
|
|
uint64_t seq_used = kMaxSequenceNumber;
|
2017-10-05 16:28:37 +02:00
|
|
|
bool collapsed = GetWriteBatch()->Collapse();
|
|
|
|
if (collapsed) {
|
|
|
|
ROCKS_LOG_WARN(db_impl_->immutable_db_options().info_log,
|
|
|
|
"Collapse overhead due to duplicate keys");
|
|
|
|
}
|
2017-08-17 01:49:11 +02:00
|
|
|
Status s =
|
|
|
|
db_impl_->WriteImpl(write_options, GetWriteBatch()->GetWriteBatch(),
|
|
|
|
/*callback*/ nullptr, &log_number_, /*log ref*/ 0,
|
|
|
|
!disable_memtable, &seq_used);
|
2017-09-18 23:36:53 +02:00
|
|
|
assert(seq_used != kMaxSequenceNumber);
|
2017-09-29 01:43:04 +02:00
|
|
|
auto prepare_seq = seq_used;
|
|
|
|
SetId(prepare_seq);
|
|
|
|
wpt_db_->AddPrepared(prepare_seq);
|
2017-08-17 01:49:11 +02:00
|
|
|
return s;
|
2017-08-03 17:46:47 +02:00
|
|
|
}
|
|
|
|
|
2017-08-08 01:07:40 +02:00
|
|
|
Status WritePreparedTxn::CommitWithoutPrepareInternal() {
|
2017-10-05 16:28:37 +02:00
|
|
|
bool collapsed = GetWriteBatch()->Collapse();
|
|
|
|
if (collapsed) {
|
|
|
|
ROCKS_LOG_WARN(db_impl_->immutable_db_options().info_log,
|
|
|
|
"Collapse overhead due to duplicate keys");
|
|
|
|
}
|
2017-09-09 00:53:51 +02:00
|
|
|
return CommitBatchInternal(GetWriteBatch()->GetWriteBatch());
|
|
|
|
}
|
|
|
|
|
2017-10-23 23:20:22 +02:00
|
|
|
SequenceNumber WritePreparedTxn::GetACommitSeqNumber(SequenceNumber prep_seq) {
|
|
|
|
if (db_impl_->immutable_db_options().concurrent_prepare) {
|
|
|
|
return db_impl_->IncAndFetchSequenceNumber();
|
|
|
|
} else {
|
|
|
|
return prep_seq;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-09 00:53:51 +02:00
|
|
|
Status WritePreparedTxn::CommitBatchInternal(WriteBatch* batch) {
|
2017-10-05 16:28:37 +02:00
|
|
|
// TODO(myabandeh): handle the duplicate keys in the batch
|
|
|
|
// In the absence of Prepare markers, use Noop as a batch separator
|
2017-09-18 23:36:53 +02:00
|
|
|
WriteBatchInternal::InsertNoop(batch);
|
2017-09-09 00:53:51 +02:00
|
|
|
const bool disable_memtable = true;
|
|
|
|
const uint64_t no_log_ref = 0;
|
2017-09-29 01:43:04 +02:00
|
|
|
uint64_t seq_used = kMaxSequenceNumber;
|
2017-09-09 00:53:51 +02:00
|
|
|
auto s = db_impl_->WriteImpl(write_options_, batch, nullptr, nullptr,
|
|
|
|
no_log_ref, !disable_memtable, &seq_used);
|
2017-09-29 01:43:04 +02:00
|
|
|
assert(seq_used != kMaxSequenceNumber);
|
2017-09-09 00:53:51 +02:00
|
|
|
uint64_t& prepare_seq = seq_used;
|
2017-10-23 23:20:22 +02:00
|
|
|
uint64_t commit_seq = GetACommitSeqNumber(prepare_seq);
|
2017-09-09 00:53:51 +02:00
|
|
|
// TODO(myabandeh): skip AddPrepared
|
|
|
|
wpt_db_->AddPrepared(prepare_seq);
|
|
|
|
wpt_db_->AddCommitted(prepare_seq, commit_seq);
|
|
|
|
return s;
|
2017-08-03 17:46:47 +02:00
|
|
|
}
|
|
|
|
|
2017-08-08 01:07:40 +02:00
|
|
|
Status WritePreparedTxn::CommitInternal() {
|
2017-08-17 01:49:11 +02:00
|
|
|
// 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();
|
2017-10-06 23:18:30 +02:00
|
|
|
const bool empty = working_batch->Count() == 0;
|
2017-08-17 01:49:11 +02:00
|
|
|
WriteBatchInternal::MarkCommit(working_batch, name_);
|
|
|
|
|
2017-11-02 01:23:52 +01:00
|
|
|
const bool for_recovery = use_only_the_last_commit_time_batch_for_recovery_;
|
|
|
|
if (!empty && for_recovery) {
|
|
|
|
// When not writing to memtable, we can still cache the latest write batch.
|
|
|
|
// The cached batch will be written to memtable in WriteRecoverableState
|
|
|
|
// during FlushMemTable
|
|
|
|
WriteBatchInternal::SetAsLastestPersistentState(working_batch);
|
|
|
|
}
|
2017-08-17 01:49:11 +02:00
|
|
|
|
|
|
|
const bool disable_memtable = true;
|
2017-09-29 01:43:04 +02:00
|
|
|
uint64_t seq_used = kMaxSequenceNumber;
|
|
|
|
// Since the prepared batch is directly written to memtable, there is already
|
|
|
|
// a connection between the memtable and its WAL, so there is no need to
|
|
|
|
// redundantly reference the log that contains the prepared data.
|
|
|
|
const uint64_t zero_log_number = 0ull;
|
2017-10-06 23:18:30 +02:00
|
|
|
auto s = db_impl_->WriteImpl(
|
|
|
|
write_options_, working_batch, nullptr, nullptr, zero_log_number,
|
2017-11-02 01:23:52 +01:00
|
|
|
empty || for_recovery ? disable_memtable : !disable_memtable, &seq_used);
|
2017-09-29 01:43:04 +02:00
|
|
|
assert(seq_used != kMaxSequenceNumber);
|
2017-08-17 01:49:11 +02:00
|
|
|
uint64_t& commit_seq = seq_used;
|
2017-09-13 20:56:27 +02:00
|
|
|
// TODO(myabandeh): Reject a commit request if AddCommitted cannot encode
|
|
|
|
// commit_seq. This happens if prep_seq <<< commit_seq.
|
2017-09-29 01:43:04 +02:00
|
|
|
auto prepare_seq = GetId();
|
|
|
|
wpt_db_->AddCommitted(prepare_seq, commit_seq);
|
2017-11-02 01:23:52 +01:00
|
|
|
if (!empty && !for_recovery) {
|
2017-10-06 23:18:30 +02:00
|
|
|
// Commit the data that is accompnaied with the commit marker
|
|
|
|
// TODO(myabandeh): skip AddPrepared
|
|
|
|
wpt_db_->AddPrepared(commit_seq);
|
2017-10-23 23:20:22 +02:00
|
|
|
uint64_t commit_seq_2 = GetACommitSeqNumber(commit_seq);
|
|
|
|
wpt_db_->AddCommitted(commit_seq, commit_seq_2);
|
2017-10-06 23:18:30 +02:00
|
|
|
}
|
2017-08-17 01:49:11 +02:00
|
|
|
return s;
|
2017-08-08 01:07:40 +02:00
|
|
|
}
|
|
|
|
|
2017-10-03 04:46:42 +02:00
|
|
|
Status WritePreparedTxn::RollbackInternal() {
|
|
|
|
WriteBatch rollback_batch;
|
|
|
|
assert(GetId() != kMaxSequenceNumber);
|
|
|
|
assert(GetId() > 0);
|
2017-10-05 16:28:37 +02:00
|
|
|
// In the absence of Prepare markers, use Noop as a batch separator
|
2017-10-03 04:46:42 +02:00
|
|
|
WriteBatchInternal::InsertNoop(&rollback_batch);
|
|
|
|
// In WritePrepared, the txn is is the same as prepare seq
|
|
|
|
auto last_visible_txn = GetId() - 1;
|
|
|
|
struct RollbackWriteBatchBuilder : public WriteBatch::Handler {
|
|
|
|
DBImpl* db_;
|
|
|
|
ReadOptions roptions;
|
|
|
|
WritePreparedTxnReadCallback callback;
|
|
|
|
WriteBatch* rollback_batch_;
|
|
|
|
RollbackWriteBatchBuilder(DBImpl* db, WritePreparedTxnDB* wpt_db,
|
|
|
|
SequenceNumber snap_seq, WriteBatch* dst_batch)
|
|
|
|
: db_(db), callback(wpt_db, snap_seq), rollback_batch_(dst_batch) {}
|
|
|
|
|
|
|
|
Status Rollback(uint32_t cf, const Slice& key) {
|
|
|
|
PinnableSlice pinnable_val;
|
|
|
|
bool not_used;
|
|
|
|
auto cf_handle = db_->GetColumnFamilyHandle(cf);
|
|
|
|
auto s = db_->GetImpl(roptions, cf_handle, key, &pinnable_val, ¬_used,
|
|
|
|
&callback);
|
|
|
|
assert(s.ok() || s.IsNotFound());
|
|
|
|
if (s.ok()) {
|
|
|
|
s = rollback_batch_->Put(cf_handle, key, pinnable_val);
|
|
|
|
assert(s.ok());
|
|
|
|
} else if (s.IsNotFound()) {
|
|
|
|
// There has been no readable value before txn. By adding a delete we
|
|
|
|
// make sure that there will be none afterwards either.
|
|
|
|
s = rollback_batch_->Delete(cf_handle, key);
|
|
|
|
assert(s.ok());
|
|
|
|
} else {
|
|
|
|
// Unexpected status. Return it to the user.
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status PutCF(uint32_t cf, const Slice& key, const Slice& val) override {
|
|
|
|
return Rollback(cf, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status DeleteCF(uint32_t cf, const Slice& key) override {
|
|
|
|
return Rollback(cf, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status SingleDeleteCF(uint32_t cf, const Slice& key) override {
|
|
|
|
return Rollback(cf, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status MergeCF(uint32_t cf, const Slice& key, const Slice& val) override {
|
|
|
|
return Rollback(cf, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status MarkNoop(bool) override { return Status::OK(); }
|
|
|
|
Status MarkBeginPrepare() override { return Status::OK(); }
|
|
|
|
Status MarkEndPrepare(const Slice&) override { return Status::OK(); }
|
|
|
|
Status MarkCommit(const Slice&) override { return Status::OK(); }
|
|
|
|
Status MarkRollback(const Slice&) override {
|
|
|
|
return Status::InvalidArgument();
|
|
|
|
}
|
|
|
|
} rollback_handler(db_impl_, wpt_db_, last_visible_txn, &rollback_batch);
|
|
|
|
auto s = GetWriteBatch()->GetWriteBatch()->Iterate(&rollback_handler);
|
|
|
|
assert(s.ok());
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
WriteBatchInternal::MarkRollback(&rollback_batch, name_);
|
|
|
|
const bool disable_memtable = true;
|
|
|
|
const uint64_t no_log_ref = 0;
|
|
|
|
uint64_t seq_used = kMaxSequenceNumber;
|
|
|
|
s = db_impl_->WriteImpl(write_options_, &rollback_batch, nullptr, nullptr,
|
|
|
|
no_log_ref, !disable_memtable, &seq_used);
|
|
|
|
assert(seq_used != kMaxSequenceNumber);
|
|
|
|
uint64_t& prepare_seq = seq_used;
|
2017-10-23 23:20:22 +02:00
|
|
|
uint64_t commit_seq = GetACommitSeqNumber(prepare_seq);
|
2017-10-03 04:46:42 +02:00
|
|
|
// TODO(myabandeh): skip AddPrepared
|
|
|
|
wpt_db_->AddPrepared(prepare_seq);
|
|
|
|
wpt_db_->AddCommitted(prepare_seq, commit_seq);
|
|
|
|
// Mark the txn as rolled back
|
|
|
|
wpt_db_->RollbackPrepared(GetId(), commit_seq);
|
|
|
|
|
|
|
|
return s;
|
2017-08-03 17:46:47 +02:00
|
|
|
}
|
|
|
|
|
2017-11-02 02:56:25 +01:00
|
|
|
Status WritePreparedTxn::ValidateSnapshot(ColumnFamilyHandle* column_family,
|
|
|
|
const Slice& key,
|
|
|
|
SequenceNumber prev_seqno,
|
|
|
|
SequenceNumber* new_seqno) {
|
|
|
|
assert(snapshot_);
|
|
|
|
|
|
|
|
SequenceNumber snap_seq = snapshot_->GetSequenceNumber();
|
|
|
|
// prev_seqno is either max or the last snapshot with which this key was
|
|
|
|
// trackeed so there is no need to apply the IsInSnapshot to this comparison
|
|
|
|
// here as prev_seqno is not a prepare seq.
|
|
|
|
if (prev_seqno <= snap_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();
|
|
|
|
}
|
|
|
|
|
|
|
|
*new_seqno = snap_seq;
|
|
|
|
|
|
|
|
ColumnFamilyHandle* cfh =
|
|
|
|
column_family ? column_family : db_impl_->DefaultColumnFamily();
|
|
|
|
|
|
|
|
WritePreparedTxnReadCallback snap_checker(wpt_db_, snap_seq);
|
|
|
|
return TransactionUtil::CheckKeyForConflicts(
|
|
|
|
db_impl_, cfh, key.ToString(), snapshot_->GetSequenceNumber(),
|
|
|
|
false /* cache_only */, &snap_checker);
|
|
|
|
}
|
|
|
|
|
2017-08-03 17:46:47 +02:00
|
|
|
} // namespace rocksdb
|
|
|
|
|
|
|
|
#endif // ROCKSDB_LITE
|