2017-11-02 19:05:55 +01: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
|
|
|
|
|
|
|
|
#ifndef __STDC_FORMAT_MACROS
|
|
|
|
#define __STDC_FORMAT_MACROS
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "utilities/transactions/write_prepared_txn_db.h"
|
|
|
|
|
2018-02-21 22:40:31 +01:00
|
|
|
#include <inttypes.h>
|
2017-12-13 16:52:02 +01:00
|
|
|
#include <algorithm>
|
2017-11-02 19:05:55 +01:00
|
|
|
#include <string>
|
|
|
|
#include <unordered_set>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "db/db_impl.h"
|
|
|
|
#include "rocksdb/db.h"
|
|
|
|
#include "rocksdb/options.h"
|
|
|
|
#include "rocksdb/utilities/transaction_db.h"
|
2019-05-31 02:39:43 +02:00
|
|
|
#include "test_util/sync_point.h"
|
2018-04-03 05:19:21 +02:00
|
|
|
#include "util/cast_util.h"
|
2017-11-02 19:05:55 +01:00
|
|
|
#include "util/mutexlock.h"
|
2018-01-09 17:47:46 +01:00
|
|
|
#include "util/string_util.h"
|
2017-11-02 19:05:55 +01:00
|
|
|
#include "utilities/transactions/pessimistic_transaction.h"
|
|
|
|
#include "utilities/transactions/transaction_db_mutex_impl.h"
|
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
|
|
|
Status WritePreparedTxnDB::Initialize(
|
|
|
|
const std::vector<size_t>& compaction_enabled_cf_indices,
|
|
|
|
const std::vector<ColumnFamilyHandle*>& handles) {
|
|
|
|
auto dbimpl = reinterpret_cast<DBImpl*>(GetRootDB());
|
|
|
|
assert(dbimpl != nullptr);
|
|
|
|
auto rtxns = dbimpl->recovered_transactions();
|
|
|
|
for (auto rtxn : rtxns) {
|
2018-07-07 02:17:36 +02:00
|
|
|
// There should only one batch for WritePrepared policy.
|
|
|
|
assert(rtxn.second->batches_.size() == 1);
|
|
|
|
const auto& seq = rtxn.second->batches_.begin()->first;
|
|
|
|
const auto& batch_info = rtxn.second->batches_.begin()->second;
|
|
|
|
auto cnt = batch_info.batch_cnt_ ? batch_info.batch_cnt_ : 1;
|
2018-03-24 01:21:47 +01:00
|
|
|
for (size_t i = 0; i < cnt; i++) {
|
2018-07-07 02:17:36 +02:00
|
|
|
AddPrepared(seq + i);
|
2018-03-24 01:21:47 +01:00
|
|
|
}
|
2017-11-02 19:05:55 +01:00
|
|
|
}
|
|
|
|
SequenceNumber prev_max = max_evicted_seq_;
|
|
|
|
SequenceNumber last_seq = db_impl_->GetLatestSequenceNumber();
|
|
|
|
AdvanceMaxEvictedSeq(prev_max, last_seq);
|
2019-01-08 20:23:10 +01:00
|
|
|
// Create a gap between max and the next snapshot. This simplifies the logic
|
|
|
|
// in IsInSnapshot by not having to consider the special case of max ==
|
|
|
|
// snapshot after recovery. This is tested in IsInSnapshotEmptyMapTest.
|
|
|
|
if (last_seq) {
|
|
|
|
db_impl_->versions_->SetLastAllocatedSequence(last_seq + 1);
|
|
|
|
db_impl_->versions_->SetLastSequence(last_seq + 1);
|
|
|
|
db_impl_->versions_->SetLastPublishedSequence(last_seq + 1);
|
|
|
|
}
|
2017-11-02 19:05:55 +01:00
|
|
|
|
|
|
|
db_impl_->SetSnapshotChecker(new WritePreparedSnapshotChecker(this));
|
2018-03-28 21:01:09 +02:00
|
|
|
// A callback to commit a single sub-batch
|
|
|
|
class CommitSubBatchPreReleaseCallback : public PreReleaseCallback {
|
|
|
|
public:
|
|
|
|
explicit CommitSubBatchPreReleaseCallback(WritePreparedTxnDB* db)
|
|
|
|
: db_(db) {}
|
2019-04-03 00:14:41 +02:00
|
|
|
Status Callback(SequenceNumber commit_seq,
|
|
|
|
bool is_mem_disabled __attribute__((__unused__)),
|
|
|
|
uint64_t) override {
|
2018-03-28 21:01:09 +02:00
|
|
|
assert(!is_mem_disabled);
|
2018-04-12 04:59:25 +02:00
|
|
|
db_->AddCommitted(commit_seq, commit_seq);
|
2018-03-28 21:01:09 +02:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
WritePreparedTxnDB* db_;
|
|
|
|
};
|
|
|
|
db_impl_->SetRecoverableStatePreReleaseCallback(
|
|
|
|
new CommitSubBatchPreReleaseCallback(this));
|
2017-11-02 19:05:55 +01:00
|
|
|
|
|
|
|
auto s = PessimisticTransactionDB::Initialize(compaction_enabled_cf_indices,
|
|
|
|
handles);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2018-02-06 03:32:54 +01:00
|
|
|
Status WritePreparedTxnDB::VerifyCFOptions(
|
|
|
|
const ColumnFamilyOptions& cf_options) {
|
|
|
|
Status s = PessimisticTransactionDB::VerifyCFOptions(cf_options);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
if (!cf_options.memtable_factory->CanHandleDuplicatedKey()) {
|
|
|
|
return Status::InvalidArgument(
|
|
|
|
"memtable_factory->CanHandleDuplicatedKey() cannot be false with "
|
|
|
|
"WritePrpeared transactions");
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2017-11-02 19:05:55 +01:00
|
|
|
Transaction* WritePreparedTxnDB::BeginTransaction(
|
|
|
|
const WriteOptions& write_options, const TransactionOptions& txn_options,
|
|
|
|
Transaction* old_txn) {
|
|
|
|
if (old_txn != nullptr) {
|
|
|
|
ReinitializeTransaction(old_txn, write_options, txn_options);
|
|
|
|
return old_txn;
|
|
|
|
} else {
|
|
|
|
return new WritePreparedTxn(this, write_options, txn_options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-29 01:26:14 +02:00
|
|
|
Status WritePreparedTxnDB::Write(const WriteOptions& opts,
|
|
|
|
WriteBatch* updates) {
|
|
|
|
if (txn_db_options_.skip_concurrency_control) {
|
|
|
|
// Skip locking the rows
|
|
|
|
const size_t UNKNOWN_BATCH_CNT = 0;
|
|
|
|
WritePreparedTxn* NO_TXN = nullptr;
|
|
|
|
return WriteInternal(opts, updates, UNKNOWN_BATCH_CNT, NO_TXN);
|
|
|
|
} else {
|
|
|
|
return PessimisticTransactionDB::WriteWithConcurrencyControl(opts, updates);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-13 01:27:39 +01:00
|
|
|
Status WritePreparedTxnDB::Write(
|
|
|
|
const WriteOptions& opts,
|
|
|
|
const TransactionDBWriteOptimizations& optimizations, WriteBatch* updates) {
|
|
|
|
if (optimizations.skip_concurrency_control) {
|
|
|
|
// Skip locking the rows
|
|
|
|
const size_t UNKNOWN_BATCH_CNT = 0;
|
|
|
|
const size_t ONE_BATCH_CNT = 1;
|
|
|
|
const size_t batch_cnt = optimizations.skip_duplicate_key_check
|
|
|
|
? ONE_BATCH_CNT
|
|
|
|
: UNKNOWN_BATCH_CNT;
|
|
|
|
WritePreparedTxn* NO_TXN = nullptr;
|
|
|
|
return WriteInternal(opts, updates, batch_cnt, NO_TXN);
|
|
|
|
} else {
|
|
|
|
// TODO(myabandeh): Make use of skip_duplicate_key_check hint
|
|
|
|
// Fall back to unoptimized version
|
2019-05-29 01:26:14 +02:00
|
|
|
return PessimisticTransactionDB::WriteWithConcurrencyControl(opts, updates);
|
2018-02-13 01:27:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Status WritePreparedTxnDB::WriteInternal(const WriteOptions& write_options_orig,
|
|
|
|
WriteBatch* batch, size_t batch_cnt,
|
|
|
|
WritePreparedTxn* txn) {
|
|
|
|
ROCKS_LOG_DETAILS(db_impl_->immutable_db_options().info_log,
|
|
|
|
"CommitBatchInternal");
|
|
|
|
if (batch->Count() == 0) {
|
|
|
|
// Otherwise our 1 seq per batch logic will break since there is no seq
|
|
|
|
// increased for this batch.
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
if (batch_cnt == 0) { // not provided, then compute it
|
|
|
|
// TODO(myabandeh): add an option to allow user skipping this cost
|
|
|
|
SubBatchCounter counter(*GetCFComparatorMap());
|
|
|
|
auto s = batch->Iterate(&counter);
|
|
|
|
assert(s.ok());
|
|
|
|
batch_cnt = counter.BatchCount();
|
2018-04-08 06:55:42 +02:00
|
|
|
WPRecordTick(TXN_DUPLICATE_KEY_OVERHEAD);
|
|
|
|
ROCKS_LOG_DETAILS(info_log_, "Duplicate key overhead: %" PRIu64 " batches",
|
|
|
|
static_cast<uint64_t>(batch_cnt));
|
2018-02-13 01:27:39 +01:00
|
|
|
}
|
|
|
|
assert(batch_cnt);
|
|
|
|
|
|
|
|
bool do_one_write = !db_impl_->immutable_db_options().two_write_queues;
|
|
|
|
WriteOptions write_options(write_options_orig);
|
|
|
|
// In the absence of Prepare markers, use Noop as a batch separator
|
|
|
|
WriteBatchInternal::InsertNoop(batch);
|
|
|
|
const bool DISABLE_MEMTABLE = true;
|
|
|
|
const uint64_t no_log_ref = 0;
|
|
|
|
uint64_t seq_used = kMaxSequenceNumber;
|
|
|
|
const size_t ZERO_PREPARES = 0;
|
2019-04-03 00:14:41 +02:00
|
|
|
const bool kSeperatePrepareCommitBatches = true;
|
2018-04-03 05:19:21 +02:00
|
|
|
// Since this is not 2pc, there is no need for AddPrepared but having it in
|
|
|
|
// the PreReleaseCallback enables an optimization. Refer to
|
|
|
|
// SmallestUnCommittedSeq for more details.
|
|
|
|
AddPreparedCallback add_prepared_callback(
|
2019-04-03 00:14:41 +02:00
|
|
|
this, db_impl_, batch_cnt,
|
|
|
|
db_impl_->immutable_db_options().two_write_queues,
|
|
|
|
!kSeperatePrepareCommitBatches);
|
2018-02-13 01:27:39 +01:00
|
|
|
WritePreparedCommitEntryPreReleaseCallback update_commit_map(
|
|
|
|
this, db_impl_, kMaxSequenceNumber, ZERO_PREPARES, batch_cnt);
|
2018-04-03 05:19:21 +02:00
|
|
|
PreReleaseCallback* pre_release_callback;
|
|
|
|
if (do_one_write) {
|
|
|
|
pre_release_callback = &update_commit_map;
|
|
|
|
} else {
|
|
|
|
pre_release_callback = &add_prepared_callback;
|
|
|
|
}
|
|
|
|
auto s = db_impl_->WriteImpl(write_options, batch, nullptr, nullptr,
|
|
|
|
no_log_ref, !DISABLE_MEMTABLE, &seq_used,
|
|
|
|
batch_cnt, pre_release_callback);
|
2018-02-13 01:27:39 +01:00
|
|
|
assert(!s.ok() || seq_used != kMaxSequenceNumber);
|
2018-04-12 04:59:25 +02:00
|
|
|
uint64_t prepare_seq = seq_used;
|
2018-02-13 01:27:39 +01:00
|
|
|
if (txn != nullptr) {
|
|
|
|
txn->SetId(prepare_seq);
|
|
|
|
}
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
if (do_one_write) {
|
|
|
|
return s;
|
|
|
|
} // else do the 2nd write for commit
|
|
|
|
ROCKS_LOG_DETAILS(db_impl_->immutable_db_options().info_log,
|
|
|
|
"CommitBatchInternal 2nd write prepare_seq: %" PRIu64,
|
|
|
|
prepare_seq);
|
|
|
|
// Commit the batch by writing an empty batch to the 2nd queue that will
|
|
|
|
// release the commit sequence number to readers.
|
2018-02-16 17:36:47 +01:00
|
|
|
const size_t ZERO_COMMITS = 0;
|
2018-02-13 01:27:39 +01:00
|
|
|
WritePreparedCommitEntryPreReleaseCallback update_commit_map_with_prepare(
|
2018-04-12 04:59:25 +02:00
|
|
|
this, db_impl_, prepare_seq, batch_cnt, ZERO_COMMITS);
|
2018-02-13 01:27:39 +01:00
|
|
|
WriteBatch empty_batch;
|
2019-05-28 23:18:24 +02:00
|
|
|
write_options.disableWAL = true;
|
|
|
|
write_options.sync = false;
|
|
|
|
const size_t ONE_BATCH = 1; // Just to inc the seq
|
2018-02-13 01:27:39 +01:00
|
|
|
s = db_impl_->WriteImpl(write_options, &empty_batch, nullptr, nullptr,
|
|
|
|
no_log_ref, DISABLE_MEMTABLE, &seq_used, ONE_BATCH,
|
|
|
|
&update_commit_map_with_prepare);
|
|
|
|
assert(!s.ok() || seq_used != kMaxSequenceNumber);
|
2018-04-12 04:59:25 +02:00
|
|
|
// Note RemovePrepared should be called after WriteImpl that publishsed the
|
|
|
|
// seq. Otherwise SmallestUnCommittedSeq optimization breaks.
|
|
|
|
RemovePrepared(prepare_seq, batch_cnt);
|
2018-02-13 01:27:39 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-11-02 19:05:55 +01:00
|
|
|
Status WritePreparedTxnDB::Get(const ReadOptions& options,
|
|
|
|
ColumnFamilyHandle* column_family,
|
|
|
|
const Slice& key, PinnableSlice* value) {
|
2019-04-12 23:36:36 +02:00
|
|
|
SequenceNumber min_uncommitted, snap_seq;
|
|
|
|
const bool backed_by_snapshot =
|
|
|
|
AssignMinMaxSeqs(options.snapshot, &min_uncommitted, &snap_seq);
|
|
|
|
WritePreparedTxnReadCallback callback(this, snap_seq, min_uncommitted);
|
|
|
|
bool* dont_care = nullptr;
|
|
|
|
auto res = db_impl_->GetImpl(options, column_family, key, value, dont_care,
|
|
|
|
&callback);
|
|
|
|
if (LIKELY(
|
|
|
|
ValidateSnapshot(callback.max_visible_seq(), backed_by_snapshot))) {
|
|
|
|
return res;
|
2018-04-03 05:19:21 +02:00
|
|
|
} else {
|
2019-04-12 23:36:36 +02:00
|
|
|
return Status::TryAgain();
|
2017-11-02 19:05:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-16 17:36:47 +01:00
|
|
|
void WritePreparedTxnDB::UpdateCFComparatorMap(
|
|
|
|
const std::vector<ColumnFamilyHandle*>& handles) {
|
|
|
|
auto cf_map = new std::map<uint32_t, const Comparator*>();
|
2018-05-03 03:09:55 +02:00
|
|
|
auto handle_map = new std::map<uint32_t, ColumnFamilyHandle*>();
|
2018-02-16 17:36:47 +01:00
|
|
|
for (auto h : handles) {
|
|
|
|
auto id = h->GetID();
|
|
|
|
const Comparator* comparator = h->GetComparator();
|
|
|
|
(*cf_map)[id] = comparator;
|
2018-05-03 03:09:55 +02:00
|
|
|
if (id != 0) {
|
|
|
|
(*handle_map)[id] = h;
|
|
|
|
} else {
|
|
|
|
// The pointer to the default cf handle in the handles will be deleted.
|
|
|
|
// Use the pointer maintained by the db instead.
|
|
|
|
(*handle_map)[id] = DefaultColumnFamily();
|
|
|
|
}
|
2018-02-16 17:36:47 +01:00
|
|
|
}
|
2018-05-03 03:09:55 +02:00
|
|
|
cf_map_.reset(cf_map);
|
|
|
|
handle_map_.reset(handle_map);
|
2018-02-16 17:36:47 +01:00
|
|
|
}
|
|
|
|
|
2018-05-03 03:09:55 +02:00
|
|
|
void WritePreparedTxnDB::UpdateCFComparatorMap(ColumnFamilyHandle* h) {
|
|
|
|
auto old_cf_map_ptr = cf_map_.get();
|
2018-02-16 17:36:47 +01:00
|
|
|
assert(old_cf_map_ptr);
|
|
|
|
auto cf_map = new std::map<uint32_t, const Comparator*>(*old_cf_map_ptr);
|
2018-05-03 03:09:55 +02:00
|
|
|
auto old_handle_map_ptr = handle_map_.get();
|
|
|
|
assert(old_handle_map_ptr);
|
|
|
|
auto handle_map =
|
|
|
|
new std::map<uint32_t, ColumnFamilyHandle*>(*old_handle_map_ptr);
|
2018-02-16 17:36:47 +01:00
|
|
|
auto id = h->GetID();
|
|
|
|
const Comparator* comparator = h->GetComparator();
|
|
|
|
(*cf_map)[id] = comparator;
|
2018-05-03 03:09:55 +02:00
|
|
|
(*handle_map)[id] = h;
|
|
|
|
cf_map_.reset(cf_map);
|
|
|
|
handle_map_.reset(handle_map);
|
2018-02-16 17:36:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-27 17:49:30 +01:00
|
|
|
std::vector<Status> WritePreparedTxnDB::MultiGet(
|
|
|
|
const ReadOptions& options,
|
|
|
|
const std::vector<ColumnFamilyHandle*>& column_family,
|
|
|
|
const std::vector<Slice>& keys, std::vector<std::string>* values) {
|
|
|
|
assert(values);
|
|
|
|
size_t num_keys = keys.size();
|
|
|
|
values->resize(num_keys);
|
|
|
|
|
|
|
|
std::vector<Status> stat_list(num_keys);
|
|
|
|
for (size_t i = 0; i < num_keys; ++i) {
|
|
|
|
std::string* value = values ? &(*values)[i] : nullptr;
|
|
|
|
stat_list[i] = this->Get(options, column_family[i], keys[i], value);
|
|
|
|
}
|
|
|
|
return stat_list;
|
|
|
|
}
|
|
|
|
|
2017-11-02 19:05:55 +01:00
|
|
|
// Struct to hold ownership of snapshot and read callback for iterator cleanup.
|
|
|
|
struct WritePreparedTxnDB::IteratorState {
|
|
|
|
IteratorState(WritePreparedTxnDB* txn_db, SequenceNumber sequence,
|
2018-04-03 05:19:21 +02:00
|
|
|
std::shared_ptr<ManagedSnapshot> s,
|
|
|
|
SequenceNumber min_uncommitted)
|
|
|
|
: callback(txn_db, sequence, min_uncommitted), snapshot(s) {}
|
2017-11-02 19:05:55 +01:00
|
|
|
|
|
|
|
WritePreparedTxnReadCallback callback;
|
|
|
|
std::shared_ptr<ManagedSnapshot> snapshot;
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
2018-03-05 22:08:17 +01:00
|
|
|
static void CleanupWritePreparedTxnDBIterator(void* arg1, void* /*arg2*/) {
|
2017-11-02 19:05:55 +01:00
|
|
|
delete reinterpret_cast<WritePreparedTxnDB::IteratorState*>(arg1);
|
|
|
|
}
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
Iterator* WritePreparedTxnDB::NewIterator(const ReadOptions& options,
|
|
|
|
ColumnFamilyHandle* column_family) {
|
2017-12-19 07:13:08 +01:00
|
|
|
constexpr bool ALLOW_BLOB = true;
|
|
|
|
constexpr bool ALLOW_REFRESH = true;
|
2017-11-02 19:05:55 +01:00
|
|
|
std::shared_ptr<ManagedSnapshot> own_snapshot = nullptr;
|
|
|
|
SequenceNumber snapshot_seq = kMaxSequenceNumber;
|
2018-04-03 05:19:21 +02:00
|
|
|
SequenceNumber min_uncommitted = 0;
|
2017-11-02 19:05:55 +01:00
|
|
|
if (options.snapshot != nullptr) {
|
|
|
|
snapshot_seq = options.snapshot->GetSequenceNumber();
|
2018-06-27 21:05:29 +02:00
|
|
|
min_uncommitted =
|
|
|
|
static_cast_with_check<const SnapshotImpl, const Snapshot>(
|
|
|
|
options.snapshot)
|
|
|
|
->min_uncommitted_;
|
2017-11-02 19:05:55 +01:00
|
|
|
} else {
|
2018-04-03 05:19:21 +02:00
|
|
|
auto* snapshot = GetSnapshot();
|
2017-11-11 22:08:22 +01:00
|
|
|
// We take a snapshot to make sure that the related data in the commit map
|
|
|
|
// are not deleted.
|
2017-11-02 19:05:55 +01:00
|
|
|
snapshot_seq = snapshot->GetSequenceNumber();
|
2018-04-03 05:19:21 +02:00
|
|
|
min_uncommitted =
|
|
|
|
static_cast_with_check<const SnapshotImpl, const Snapshot>(snapshot)
|
|
|
|
->min_uncommitted_;
|
2017-11-02 19:05:55 +01:00
|
|
|
own_snapshot = std::make_shared<ManagedSnapshot>(db_impl_, snapshot);
|
|
|
|
}
|
|
|
|
assert(snapshot_seq != kMaxSequenceNumber);
|
|
|
|
auto* cfd = reinterpret_cast<ColumnFamilyHandleImpl*>(column_family)->cfd();
|
2018-04-03 05:19:21 +02:00
|
|
|
auto* state =
|
|
|
|
new IteratorState(this, snapshot_seq, own_snapshot, min_uncommitted);
|
2017-11-02 19:05:55 +01:00
|
|
|
auto* db_iter =
|
2017-12-19 07:13:08 +01:00
|
|
|
db_impl_->NewIteratorImpl(options, cfd, snapshot_seq, &state->callback,
|
|
|
|
!ALLOW_BLOB, !ALLOW_REFRESH);
|
2017-11-02 19:05:55 +01:00
|
|
|
db_iter->RegisterCleanup(CleanupWritePreparedTxnDBIterator, state, nullptr);
|
|
|
|
return db_iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status WritePreparedTxnDB::NewIterators(
|
|
|
|
const ReadOptions& options,
|
|
|
|
const std::vector<ColumnFamilyHandle*>& column_families,
|
|
|
|
std::vector<Iterator*>* iterators) {
|
2017-12-19 07:13:08 +01:00
|
|
|
constexpr bool ALLOW_BLOB = true;
|
|
|
|
constexpr bool ALLOW_REFRESH = true;
|
2017-11-02 19:05:55 +01:00
|
|
|
std::shared_ptr<ManagedSnapshot> own_snapshot = nullptr;
|
|
|
|
SequenceNumber snapshot_seq = kMaxSequenceNumber;
|
2018-04-03 05:19:21 +02:00
|
|
|
SequenceNumber min_uncommitted = 0;
|
2017-11-02 19:05:55 +01:00
|
|
|
if (options.snapshot != nullptr) {
|
|
|
|
snapshot_seq = options.snapshot->GetSequenceNumber();
|
2018-04-03 05:19:21 +02:00
|
|
|
min_uncommitted = static_cast_with_check<const SnapshotImpl, const Snapshot>(
|
|
|
|
options.snapshot)
|
|
|
|
->min_uncommitted_;
|
2017-11-02 19:05:55 +01:00
|
|
|
} else {
|
2018-04-03 05:19:21 +02:00
|
|
|
auto* snapshot = GetSnapshot();
|
2017-11-11 22:08:22 +01:00
|
|
|
// We take a snapshot to make sure that the related data in the commit map
|
|
|
|
// are not deleted.
|
2017-11-02 19:05:55 +01:00
|
|
|
snapshot_seq = snapshot->GetSequenceNumber();
|
|
|
|
own_snapshot = std::make_shared<ManagedSnapshot>(db_impl_, snapshot);
|
2018-04-03 05:19:21 +02:00
|
|
|
min_uncommitted =
|
|
|
|
static_cast_with_check<const SnapshotImpl, const Snapshot>(snapshot)
|
|
|
|
->min_uncommitted_;
|
2017-11-02 19:05:55 +01:00
|
|
|
}
|
|
|
|
iterators->clear();
|
|
|
|
iterators->reserve(column_families.size());
|
|
|
|
for (auto* column_family : column_families) {
|
|
|
|
auto* cfd = reinterpret_cast<ColumnFamilyHandleImpl*>(column_family)->cfd();
|
2018-04-03 05:19:21 +02:00
|
|
|
auto* state =
|
|
|
|
new IteratorState(this, snapshot_seq, own_snapshot, min_uncommitted);
|
2017-11-02 19:05:55 +01:00
|
|
|
auto* db_iter =
|
2017-12-19 07:13:08 +01:00
|
|
|
db_impl_->NewIteratorImpl(options, cfd, snapshot_seq, &state->callback,
|
|
|
|
!ALLOW_BLOB, !ALLOW_REFRESH);
|
2017-11-02 19:05:55 +01:00
|
|
|
db_iter->RegisterCleanup(CleanupWritePreparedTxnDBIterator, state, nullptr);
|
|
|
|
iterators->push_back(db_iter);
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WritePreparedTxnDB::Init(const TransactionDBOptions& /* unused */) {
|
|
|
|
// Adcance max_evicted_seq_ no more than 100 times before the cache wraps
|
|
|
|
// around.
|
|
|
|
INC_STEP_FOR_MAX_EVICTED =
|
2018-01-09 17:47:46 +01:00
|
|
|
std::max(COMMIT_CACHE_SIZE / 100, static_cast<size_t>(1));
|
2018-11-09 20:17:34 +01:00
|
|
|
snapshot_cache_ = std::unique_ptr<std::atomic<SequenceNumber>[]>(
|
2017-11-02 19:05:55 +01:00
|
|
|
new std::atomic<SequenceNumber>[SNAPSHOT_CACHE_SIZE] {});
|
2018-11-09 20:17:34 +01:00
|
|
|
commit_cache_ = std::unique_ptr<std::atomic<CommitEntry64b>[]>(
|
2017-11-02 19:05:55 +01:00
|
|
|
new std::atomic<CommitEntry64b>[COMMIT_CACHE_SIZE] {});
|
|
|
|
}
|
|
|
|
|
2019-03-07 16:32:01 +01:00
|
|
|
void WritePreparedTxnDB::CheckPreparedAgainstMax(SequenceNumber new_max) {
|
|
|
|
prepared_mutex_.AssertHeld();
|
|
|
|
// When max_evicted_seq_ advances, move older entries from prepared_txns_
|
|
|
|
// to delayed_prepared_. This guarantees that if a seq is lower than max,
|
|
|
|
// then it is not in prepared_txns_ and save an expensive, synchronized
|
|
|
|
// lookup from a shared set. delayed_prepared_ is expected to be empty in
|
|
|
|
// normal cases.
|
|
|
|
ROCKS_LOG_DETAILS(
|
|
|
|
info_log_,
|
|
|
|
"CheckPreparedAgainstMax prepared_txns_.empty() %d top: %" PRIu64,
|
|
|
|
prepared_txns_.empty(),
|
|
|
|
prepared_txns_.empty() ? 0 : prepared_txns_.top());
|
|
|
|
while (!prepared_txns_.empty() && prepared_txns_.top() <= new_max) {
|
|
|
|
auto to_be_popped = prepared_txns_.top();
|
|
|
|
delayed_prepared_.insert(to_be_popped);
|
|
|
|
ROCKS_LOG_WARN(info_log_,
|
|
|
|
"prepared_mutex_ overhead %" PRIu64 " (prep=%" PRIu64
|
|
|
|
" new_max=%" PRIu64,
|
|
|
|
static_cast<uint64_t>(delayed_prepared_.size()),
|
|
|
|
to_be_popped, new_max);
|
|
|
|
prepared_txns_.pop();
|
|
|
|
delayed_prepared_empty_.store(false, std::memory_order_release);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-02 19:05:55 +01:00
|
|
|
void WritePreparedTxnDB::AddPrepared(uint64_t seq) {
|
2019-03-07 16:32:01 +01:00
|
|
|
ROCKS_LOG_DETAILS(info_log_, "Txn %" PRIu64 " Preparing with max %" PRIu64,
|
2019-01-18 20:33:30 +01:00
|
|
|
seq, max_evicted_seq_.load());
|
2019-03-07 16:32:01 +01:00
|
|
|
TEST_SYNC_POINT("AddPrepared::begin:pause");
|
|
|
|
TEST_SYNC_POINT("AddPrepared::begin:resume");
|
2017-11-02 19:05:55 +01:00
|
|
|
WriteLock wl(&prepared_mutex_);
|
2019-03-07 16:32:01 +01:00
|
|
|
prepared_txns_.push(seq);
|
|
|
|
auto new_max = future_max_evicted_seq_.load();
|
|
|
|
if (UNLIKELY(seq <= new_max)) {
|
2019-01-18 20:33:30 +01:00
|
|
|
// This should not happen in normal case
|
|
|
|
ROCKS_LOG_ERROR(
|
|
|
|
info_log_,
|
|
|
|
"Added prepare_seq is not larger than max_evicted_seq_: %" PRIu64
|
|
|
|
" <= %" PRIu64,
|
2019-03-07 16:32:01 +01:00
|
|
|
seq, new_max);
|
|
|
|
CheckPreparedAgainstMax(new_max);
|
2019-01-18 20:33:30 +01:00
|
|
|
}
|
2019-03-07 16:32:01 +01:00
|
|
|
TEST_SYNC_POINT("AddPrepared::end");
|
2017-11-02 19:05:55 +01:00
|
|
|
}
|
|
|
|
|
2018-01-09 17:47:46 +01:00
|
|
|
void WritePreparedTxnDB::AddCommitted(uint64_t prepare_seq, uint64_t commit_seq,
|
2018-04-12 04:59:25 +02:00
|
|
|
uint8_t loop_cnt) {
|
|
|
|
ROCKS_LOG_DETAILS(info_log_, "Txn %" PRIu64 " Committing with %" PRIu64,
|
|
|
|
prepare_seq, commit_seq);
|
2017-12-01 08:39:56 +01:00
|
|
|
TEST_SYNC_POINT("WritePreparedTxnDB::AddCommitted:start");
|
|
|
|
TEST_SYNC_POINT("WritePreparedTxnDB::AddCommitted:start:pause");
|
2017-11-02 19:05:55 +01:00
|
|
|
auto indexed_seq = prepare_seq % COMMIT_CACHE_SIZE;
|
|
|
|
CommitEntry64b evicted_64b;
|
|
|
|
CommitEntry evicted;
|
|
|
|
bool to_be_evicted = GetCommitEntry(indexed_seq, &evicted_64b, &evicted);
|
2018-03-05 19:48:29 +01:00
|
|
|
if (LIKELY(to_be_evicted)) {
|
|
|
|
assert(evicted.prep_seq != prepare_seq);
|
2017-11-02 19:05:55 +01:00
|
|
|
auto prev_max = max_evicted_seq_.load(std::memory_order_acquire);
|
2018-01-09 17:47:46 +01:00
|
|
|
ROCKS_LOG_DETAILS(info_log_,
|
|
|
|
"Evicting %" PRIu64 ",%" PRIu64 " with max %" PRIu64,
|
|
|
|
evicted.prep_seq, evicted.commit_seq, prev_max);
|
2017-11-02 19:05:55 +01:00
|
|
|
if (prev_max < evicted.commit_seq) {
|
2019-01-16 03:07:50 +01:00
|
|
|
auto last = db_impl_->GetLastPublishedSequence(); // could be 0
|
|
|
|
SequenceNumber max_evicted_seq;
|
|
|
|
if (LIKELY(evicted.commit_seq < last)) {
|
|
|
|
assert(last > 0);
|
|
|
|
// Inc max in larger steps to avoid frequent updates
|
|
|
|
max_evicted_seq =
|
|
|
|
std::min(evicted.commit_seq + INC_STEP_FOR_MAX_EVICTED, last - 1);
|
|
|
|
} else {
|
|
|
|
// legit when a commit entry in a write batch overwrite the previous one
|
|
|
|
max_evicted_seq = evicted.commit_seq;
|
|
|
|
}
|
|
|
|
ROCKS_LOG_DETAILS(info_log_,
|
|
|
|
"%lu Evicting %" PRIu64 ",%" PRIu64 " with max %" PRIu64
|
|
|
|
" => %lu",
|
|
|
|
prepare_seq, evicted.prep_seq, evicted.commit_seq,
|
|
|
|
prev_max, max_evicted_seq);
|
2017-11-02 19:05:55 +01:00
|
|
|
AdvanceMaxEvictedSeq(prev_max, max_evicted_seq);
|
|
|
|
}
|
|
|
|
// After each eviction from commit cache, check if the commit entry should
|
|
|
|
// be kept around because it overlaps with a live snapshot.
|
|
|
|
CheckAgainstSnapshots(evicted);
|
2019-01-18 20:33:30 +01:00
|
|
|
if (UNLIKELY(!delayed_prepared_empty_.load(std::memory_order_acquire))) {
|
|
|
|
WriteLock wl(&prepared_mutex_);
|
|
|
|
for (auto dp : delayed_prepared_) {
|
|
|
|
if (dp == evicted.prep_seq) {
|
|
|
|
// This is a rare case that txn is committed but prepared_txns_ is not
|
|
|
|
// cleaned up yet. Refer to delayed_prepared_commits_ definition for
|
|
|
|
// why it should be kept updated.
|
|
|
|
delayed_prepared_commits_[evicted.prep_seq] = evicted.commit_seq;
|
|
|
|
ROCKS_LOG_DEBUG(info_log_,
|
|
|
|
"delayed_prepared_commits_[%" PRIu64 "]=%" PRIu64,
|
|
|
|
evicted.prep_seq, evicted.commit_seq);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-02 19:05:55 +01:00
|
|
|
}
|
|
|
|
bool succ =
|
|
|
|
ExchangeCommitEntry(indexed_seq, evicted_64b, {prepare_seq, commit_seq});
|
2018-03-05 19:48:29 +01:00
|
|
|
if (UNLIKELY(!succ)) {
|
|
|
|
ROCKS_LOG_ERROR(info_log_,
|
|
|
|
"ExchangeCommitEntry failed on [%" PRIu64 "] %" PRIu64
|
|
|
|
",%" PRIu64 " retrying...",
|
|
|
|
indexed_seq, prepare_seq, commit_seq);
|
2017-11-02 19:05:55 +01:00
|
|
|
// A very rare event, in which the commit entry is updated before we do.
|
|
|
|
// Here we apply a very simple solution of retrying.
|
2018-01-09 17:47:46 +01:00
|
|
|
if (loop_cnt > 100) {
|
|
|
|
throw std::runtime_error("Infinite loop in AddCommitted!");
|
|
|
|
}
|
2018-04-12 04:59:25 +02:00
|
|
|
AddCommitted(prepare_seq, commit_seq, ++loop_cnt);
|
2017-11-02 19:05:55 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-04-12 04:59:25 +02:00
|
|
|
TEST_SYNC_POINT("WritePreparedTxnDB::AddCommitted:end");
|
|
|
|
TEST_SYNC_POINT("WritePreparedTxnDB::AddCommitted:end:pause");
|
|
|
|
}
|
|
|
|
|
|
|
|
void WritePreparedTxnDB::RemovePrepared(const uint64_t prepare_seq,
|
|
|
|
const size_t batch_cnt) {
|
2019-01-18 20:33:30 +01:00
|
|
|
TEST_SYNC_POINT_CALLBACK(
|
|
|
|
"RemovePrepared:Start",
|
|
|
|
const_cast<void*>(reinterpret_cast<const void*>(&prepare_seq)));
|
2019-02-06 17:43:16 +01:00
|
|
|
TEST_SYNC_POINT("WritePreparedTxnDB::RemovePrepared:pause");
|
|
|
|
TEST_SYNC_POINT("WritePreparedTxnDB::RemovePrepared:resume");
|
2019-01-18 20:33:30 +01:00
|
|
|
ROCKS_LOG_DETAILS(info_log_,
|
|
|
|
"RemovePrepared %" PRIu64 " cnt: %" ROCKSDB_PRIszt,
|
|
|
|
prepare_seq, batch_cnt);
|
2018-04-12 04:59:25 +02:00
|
|
|
WriteLock wl(&prepared_mutex_);
|
|
|
|
for (size_t i = 0; i < batch_cnt; i++) {
|
|
|
|
prepared_txns_.erase(prepare_seq + i);
|
2017-11-02 19:05:55 +01:00
|
|
|
bool was_empty = delayed_prepared_.empty();
|
|
|
|
if (!was_empty) {
|
2018-04-12 04:59:25 +02:00
|
|
|
delayed_prepared_.erase(prepare_seq + i);
|
2019-01-18 20:33:30 +01:00
|
|
|
auto it = delayed_prepared_commits_.find(prepare_seq + i);
|
|
|
|
if (it != delayed_prepared_commits_.end()) {
|
|
|
|
ROCKS_LOG_DETAILS(info_log_, "delayed_prepared_commits_.erase %" PRIu64,
|
|
|
|
prepare_seq + i);
|
|
|
|
delayed_prepared_commits_.erase(it);
|
|
|
|
}
|
2017-11-02 19:05:55 +01:00
|
|
|
bool is_empty = delayed_prepared_.empty();
|
|
|
|
if (was_empty != is_empty) {
|
|
|
|
delayed_prepared_empty_.store(is_empty, std::memory_order_release);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WritePreparedTxnDB::GetCommitEntry(const uint64_t indexed_seq,
|
|
|
|
CommitEntry64b* entry_64b,
|
|
|
|
CommitEntry* entry) const {
|
2018-09-06 03:07:53 +02:00
|
|
|
*entry_64b = commit_cache_[static_cast<size_t>(indexed_seq)].load(std::memory_order_acquire);
|
2017-11-02 19:05:55 +01:00
|
|
|
bool valid = entry_64b->Parse(indexed_seq, entry, FORMAT);
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WritePreparedTxnDB::AddCommitEntry(const uint64_t indexed_seq,
|
|
|
|
const CommitEntry& new_entry,
|
|
|
|
CommitEntry* evicted_entry) {
|
|
|
|
CommitEntry64b new_entry_64b(new_entry, FORMAT);
|
2018-09-06 03:07:53 +02:00
|
|
|
CommitEntry64b evicted_entry_64b = commit_cache_[static_cast<size_t>(indexed_seq)].exchange(
|
2017-11-02 19:05:55 +01:00
|
|
|
new_entry_64b, std::memory_order_acq_rel);
|
|
|
|
bool valid = evicted_entry_64b.Parse(indexed_seq, evicted_entry, FORMAT);
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WritePreparedTxnDB::ExchangeCommitEntry(const uint64_t indexed_seq,
|
|
|
|
CommitEntry64b& expected_entry_64b,
|
|
|
|
const CommitEntry& new_entry) {
|
2018-09-06 03:07:53 +02:00
|
|
|
auto& atomic_entry = commit_cache_[static_cast<size_t>(indexed_seq)];
|
2017-11-02 19:05:55 +01:00
|
|
|
CommitEntry64b new_entry_64b(new_entry, FORMAT);
|
|
|
|
bool succ = atomic_entry.compare_exchange_strong(
|
|
|
|
expected_entry_64b, new_entry_64b, std::memory_order_acq_rel,
|
|
|
|
std::memory_order_acquire);
|
|
|
|
return succ;
|
|
|
|
}
|
|
|
|
|
2018-02-21 22:40:31 +01:00
|
|
|
void WritePreparedTxnDB::AdvanceMaxEvictedSeq(const SequenceNumber& prev_max,
|
|
|
|
const SequenceNumber& new_max) {
|
|
|
|
ROCKS_LOG_DETAILS(info_log_,
|
2019-02-06 17:43:16 +01:00
|
|
|
"AdvanceMaxEvictedSeq overhead %" PRIu64 " => %" PRIu64,
|
|
|
|
prev_max, new_max);
|
2019-01-18 20:33:30 +01:00
|
|
|
// Declare the intention before getting snapshot from the DB. This helps a
|
|
|
|
// concurrent GetSnapshot to wait to catch up with future_max_evicted_seq_ if
|
|
|
|
// it has not already. Otherwise the new snapshot is when we ask DB for
|
|
|
|
// snapshots smaller than future max.
|
|
|
|
auto updated_future_max = prev_max;
|
|
|
|
while (updated_future_max < new_max &&
|
|
|
|
!future_max_evicted_seq_.compare_exchange_weak(
|
|
|
|
updated_future_max, new_max, std::memory_order_acq_rel,
|
|
|
|
std::memory_order_relaxed)) {
|
|
|
|
};
|
2019-03-07 16:32:01 +01:00
|
|
|
|
2017-11-02 19:05:55 +01:00
|
|
|
{
|
|
|
|
WriteLock wl(&prepared_mutex_);
|
2019-03-07 16:32:01 +01:00
|
|
|
CheckPreparedAgainstMax(new_max);
|
2017-11-02 19:05:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// With each change to max_evicted_seq_ fetch the live snapshots behind it.
|
|
|
|
// We use max as the version of snapshots to identify how fresh are the
|
|
|
|
// snapshot list. This works because the snapshots are between 0 and
|
|
|
|
// max, so the larger the max, the more complete they are.
|
|
|
|
SequenceNumber new_snapshots_version = new_max;
|
|
|
|
std::vector<SequenceNumber> snapshots;
|
|
|
|
bool update_snapshots = false;
|
|
|
|
if (new_snapshots_version > snapshots_version_) {
|
|
|
|
// This is to avoid updating the snapshots_ if it already updated
|
|
|
|
// with a more recent vesion by a concrrent thread
|
|
|
|
update_snapshots = true;
|
|
|
|
// We only care about snapshots lower then max
|
|
|
|
snapshots = GetSnapshotListFromDB(new_max);
|
|
|
|
}
|
|
|
|
if (update_snapshots) {
|
|
|
|
UpdateSnapshots(snapshots, new_snapshots_version);
|
2019-01-08 23:43:24 +01:00
|
|
|
if (!snapshots.empty()) {
|
|
|
|
WriteLock wl(&old_commit_map_mutex_);
|
|
|
|
for (auto snap : snapshots) {
|
|
|
|
// This allows IsInSnapshot to tell apart the reads from in valid
|
|
|
|
// snapshots from the reads from committed values in valid snapshots.
|
|
|
|
old_commit_map_[snap];
|
|
|
|
}
|
|
|
|
old_commit_map_empty_.store(false, std::memory_order_release);
|
|
|
|
}
|
2017-11-02 19:05:55 +01:00
|
|
|
}
|
2018-02-21 22:40:31 +01:00
|
|
|
auto updated_prev_max = prev_max;
|
2019-03-07 16:32:01 +01:00
|
|
|
TEST_SYNC_POINT("AdvanceMaxEvictedSeq::update_max:pause");
|
|
|
|
TEST_SYNC_POINT("AdvanceMaxEvictedSeq::update_max:resume");
|
2018-02-21 22:40:31 +01:00
|
|
|
while (updated_prev_max < new_max &&
|
|
|
|
!max_evicted_seq_.compare_exchange_weak(updated_prev_max, new_max,
|
|
|
|
std::memory_order_acq_rel,
|
|
|
|
std::memory_order_relaxed)) {
|
2017-11-02 19:05:55 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-04-03 05:19:21 +02:00
|
|
|
const Snapshot* WritePreparedTxnDB::GetSnapshot() {
|
2019-01-16 03:07:50 +01:00
|
|
|
const bool kForWWConflictCheck = true;
|
|
|
|
return GetSnapshotInternal(!kForWWConflictCheck);
|
|
|
|
}
|
|
|
|
|
|
|
|
SnapshotImpl* WritePreparedTxnDB::GetSnapshotInternal(
|
|
|
|
bool for_ww_conflict_check) {
|
|
|
|
// Note: for this optimization setting the last sequence number and obtaining
|
|
|
|
// the smallest uncommitted seq should be done atomically. However to avoid
|
|
|
|
// the mutex overhead, we call SmallestUnCommittedSeq BEFORE taking the
|
|
|
|
// snapshot. Since we always updated the list of unprepared seq (via
|
|
|
|
// AddPrepared) AFTER the last sequence is updated, this guarantees that the
|
|
|
|
// smallest uncommitted seq that we pair with the snapshot is smaller or equal
|
|
|
|
// the value that would be obtained otherwise atomically. That is ok since
|
|
|
|
// this optimization works as long as min_uncommitted is less than or equal
|
|
|
|
// than the smallest uncommitted seq when the snapshot was taken.
|
2018-04-12 04:59:25 +02:00
|
|
|
auto min_uncommitted = WritePreparedTxnDB::SmallestUnCommittedSeq();
|
2019-01-16 03:07:50 +01:00
|
|
|
SnapshotImpl* snap_impl = db_impl_->GetSnapshotImpl(for_ww_conflict_check);
|
2018-04-03 05:19:21 +02:00
|
|
|
assert(snap_impl);
|
2019-01-16 03:07:50 +01:00
|
|
|
SequenceNumber snap_seq = snap_impl->GetSequenceNumber();
|
2019-01-18 20:33:30 +01:00
|
|
|
// Note: Check against future_max_evicted_seq_ (in contrast with
|
|
|
|
// max_evicted_seq_) in case there is a concurrent AdvanceMaxEvictedSeq.
|
|
|
|
if (UNLIKELY(snap_seq != 0 && snap_seq <= future_max_evicted_seq_)) {
|
2019-01-16 03:07:50 +01:00
|
|
|
// There is a very rare case in which the commit entry evicts another commit
|
|
|
|
// entry that is not published yet thus advancing max evicted seq beyond the
|
|
|
|
// last published seq. This case is not likely in real-world setup so we
|
|
|
|
// handle it with a few retries.
|
|
|
|
size_t retry = 0;
|
2019-01-18 20:33:30 +01:00
|
|
|
SequenceNumber max;
|
|
|
|
while ((max = future_max_evicted_seq_.load()) != 0 &&
|
|
|
|
snap_impl->GetSequenceNumber() <= max && retry < 100) {
|
|
|
|
ROCKS_LOG_WARN(info_log_,
|
|
|
|
"GetSnapshot snap: %" PRIu64 " max: %" PRIu64
|
|
|
|
" retry %" ROCKSDB_PRIszt,
|
|
|
|
snap_impl->GetSequenceNumber(), max, retry);
|
2019-01-16 03:07:50 +01:00
|
|
|
ReleaseSnapshot(snap_impl);
|
|
|
|
// Wait for last visible seq to catch up with max, and also go beyond it
|
|
|
|
// by one.
|
|
|
|
AdvanceSeqByOne();
|
|
|
|
snap_impl = db_impl_->GetSnapshotImpl(for_ww_conflict_check);
|
|
|
|
assert(snap_impl);
|
|
|
|
retry++;
|
|
|
|
}
|
2019-01-18 20:33:30 +01:00
|
|
|
assert(snap_impl->GetSequenceNumber() > max);
|
|
|
|
if (snap_impl->GetSequenceNumber() <= max) {
|
|
|
|
throw std::runtime_error(
|
|
|
|
"Snapshot seq " + ToString(snap_impl->GetSequenceNumber()) +
|
|
|
|
" after " + ToString(retry) +
|
|
|
|
" retries is still less than futre_max_evicted_seq_" + ToString(max));
|
2019-01-16 03:07:50 +01:00
|
|
|
}
|
|
|
|
}
|
2018-04-12 04:59:25 +02:00
|
|
|
EnhanceSnapshot(snap_impl, min_uncommitted);
|
2019-01-16 03:07:50 +01:00
|
|
|
ROCKS_LOG_DETAILS(
|
|
|
|
db_impl_->immutable_db_options().info_log,
|
|
|
|
"GetSnapshot %" PRIu64 " ww:%" PRIi32 " min_uncommitted: %" PRIu64,
|
2019-01-18 20:33:30 +01:00
|
|
|
snap_impl->GetSequenceNumber(), for_ww_conflict_check, min_uncommitted);
|
2018-04-03 05:19:21 +02:00
|
|
|
return snap_impl;
|
|
|
|
}
|
|
|
|
|
2019-01-16 03:07:50 +01:00
|
|
|
void WritePreparedTxnDB::AdvanceSeqByOne() {
|
|
|
|
// Inserting an empty value will i) let the max evicted entry to be
|
|
|
|
// published, i.e., max == last_published, increase the last published to
|
|
|
|
// be one beyond max, i.e., max < last_published.
|
|
|
|
WriteOptions woptions;
|
|
|
|
TransactionOptions txn_options;
|
|
|
|
Transaction* txn0 = BeginTransaction(woptions, txn_options, nullptr);
|
|
|
|
std::hash<std::thread::id> hasher;
|
|
|
|
char name[64];
|
|
|
|
snprintf(name, 64, "txn%" ROCKSDB_PRIszt, hasher(std::this_thread::get_id()));
|
|
|
|
assert(strlen(name) < 64 - 1);
|
|
|
|
Status s = txn0->SetName(name);
|
|
|
|
assert(s.ok());
|
|
|
|
if (s.ok()) {
|
|
|
|
// Without prepare it would simply skip the commit
|
|
|
|
s = txn0->Prepare();
|
|
|
|
}
|
|
|
|
assert(s.ok());
|
|
|
|
if (s.ok()) {
|
|
|
|
s = txn0->Commit();
|
|
|
|
}
|
|
|
|
assert(s.ok());
|
|
|
|
delete txn0;
|
|
|
|
}
|
|
|
|
|
2017-11-02 19:05:55 +01:00
|
|
|
const std::vector<SequenceNumber> WritePreparedTxnDB::GetSnapshotListFromDB(
|
|
|
|
SequenceNumber max) {
|
2018-01-09 17:47:46 +01:00
|
|
|
ROCKS_LOG_DETAILS(info_log_, "GetSnapshotListFromDB with max %" PRIu64, max);
|
2019-01-11 22:39:26 +01:00
|
|
|
InstrumentedMutexLock dblock(db_impl_->mutex());
|
|
|
|
db_impl_->mutex()->AssertHeld();
|
2017-11-02 19:05:55 +01:00
|
|
|
return db_impl_->snapshots().GetAll(nullptr, max);
|
|
|
|
}
|
|
|
|
|
2017-12-13 16:52:02 +01:00
|
|
|
void WritePreparedTxnDB::ReleaseSnapshotInternal(
|
|
|
|
const SequenceNumber snap_seq) {
|
2019-02-04 21:53:55 +01:00
|
|
|
// TODO(myabandeh): relax should enough since the synchronizatin is already
|
|
|
|
// done by snapshots_mutex_ under which this function is called.
|
|
|
|
if (snap_seq <= max_evicted_seq_.load(std::memory_order_acquire)) {
|
2017-12-13 16:52:02 +01:00
|
|
|
// Then this is a rare case that transaction did not finish before max
|
|
|
|
// advances. It is expected for a few read-only backup snapshots. For such
|
|
|
|
// snapshots we might have kept around a couple of entries in the
|
|
|
|
// old_commit_map_. Check and do garbage collection if that is the case.
|
|
|
|
bool need_gc = false;
|
|
|
|
{
|
2018-04-08 06:55:42 +02:00
|
|
|
WPRecordTick(TXN_OLD_COMMIT_MAP_MUTEX_OVERHEAD);
|
2018-11-06 08:02:01 +01:00
|
|
|
ROCKS_LOG_WARN(info_log_, "old_commit_map_mutex_ overhead for %" PRIu64,
|
|
|
|
snap_seq);
|
2017-12-13 16:52:02 +01:00
|
|
|
ReadLock rl(&old_commit_map_mutex_);
|
|
|
|
auto prep_set_entry = old_commit_map_.find(snap_seq);
|
|
|
|
need_gc = prep_set_entry != old_commit_map_.end();
|
|
|
|
}
|
|
|
|
if (need_gc) {
|
2018-04-08 06:55:42 +02:00
|
|
|
WPRecordTick(TXN_OLD_COMMIT_MAP_MUTEX_OVERHEAD);
|
2018-11-06 08:02:01 +01:00
|
|
|
ROCKS_LOG_WARN(info_log_, "old_commit_map_mutex_ overhead for %" PRIu64,
|
|
|
|
snap_seq);
|
2017-12-13 16:52:02 +01:00
|
|
|
WriteLock wl(&old_commit_map_mutex_);
|
|
|
|
old_commit_map_.erase(snap_seq);
|
|
|
|
old_commit_map_empty_.store(old_commit_map_.empty(),
|
|
|
|
std::memory_order_release);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-29 04:01:37 +01:00
|
|
|
void WritePreparedTxnDB::CleanupReleasedSnapshots(
|
|
|
|
const std::vector<SequenceNumber>& new_snapshots,
|
|
|
|
const std::vector<SequenceNumber>& old_snapshots) {
|
|
|
|
auto newi = new_snapshots.begin();
|
|
|
|
auto oldi = old_snapshots.begin();
|
|
|
|
for (; newi != new_snapshots.end() && oldi != old_snapshots.end();) {
|
|
|
|
assert(*newi >= *oldi); // cannot have new snapshots with lower seq
|
|
|
|
if (*newi == *oldi) { // still not released
|
2018-11-30 05:59:52 +01:00
|
|
|
auto value = *newi;
|
|
|
|
while (newi != new_snapshots.end() && *newi == value) {
|
|
|
|
newi++;
|
|
|
|
}
|
|
|
|
while (oldi != old_snapshots.end() && *oldi == value) {
|
|
|
|
oldi++;
|
|
|
|
}
|
2018-11-29 04:01:37 +01:00
|
|
|
} else {
|
|
|
|
assert(*newi > *oldi); // *oldi is released
|
|
|
|
ReleaseSnapshotInternal(*oldi);
|
|
|
|
oldi++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Everything remained in old_snapshots is released and must be cleaned up
|
|
|
|
for (; oldi != old_snapshots.end(); oldi++) {
|
|
|
|
ReleaseSnapshotInternal(*oldi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-02 19:05:55 +01:00
|
|
|
void WritePreparedTxnDB::UpdateSnapshots(
|
|
|
|
const std::vector<SequenceNumber>& snapshots,
|
|
|
|
const SequenceNumber& version) {
|
2018-01-09 17:47:46 +01:00
|
|
|
ROCKS_LOG_DETAILS(info_log_, "UpdateSnapshots with version %" PRIu64,
|
|
|
|
version);
|
2017-11-02 19:05:55 +01:00
|
|
|
TEST_SYNC_POINT("WritePreparedTxnDB::UpdateSnapshots:p:start");
|
|
|
|
TEST_SYNC_POINT("WritePreparedTxnDB::UpdateSnapshots:s:start");
|
|
|
|
#ifndef NDEBUG
|
|
|
|
size_t sync_i = 0;
|
|
|
|
#endif
|
2018-04-08 06:55:42 +02:00
|
|
|
ROCKS_LOG_DETAILS(info_log_, "snapshots_mutex_ overhead");
|
2017-11-02 19:05:55 +01:00
|
|
|
WriteLock wl(&snapshots_mutex_);
|
|
|
|
snapshots_version_ = version;
|
|
|
|
// We update the list concurrently with the readers.
|
|
|
|
// Both new and old lists are sorted and the new list is subset of the
|
|
|
|
// previous list plus some new items. Thus if a snapshot repeats in
|
|
|
|
// both new and old lists, it will appear upper in the new list. So if
|
|
|
|
// we simply insert the new snapshots in order, if an overwritten item
|
|
|
|
// is still valid in the new list is either written to the same place in
|
|
|
|
// the array or it is written in a higher palce before it gets
|
|
|
|
// overwritten by another item. This guarantess a reader that reads the
|
|
|
|
// list bottom-up will eventaully see a snapshot that repeats in the
|
|
|
|
// update, either before it gets overwritten by the writer or
|
|
|
|
// afterwards.
|
|
|
|
size_t i = 0;
|
|
|
|
auto it = snapshots.begin();
|
2019-05-15 22:14:18 +02:00
|
|
|
for (; it != snapshots.end() && i < SNAPSHOT_CACHE_SIZE; ++it, ++i) {
|
2017-11-02 19:05:55 +01:00
|
|
|
snapshot_cache_[i].store(*it, std::memory_order_release);
|
|
|
|
TEST_IDX_SYNC_POINT("WritePreparedTxnDB::UpdateSnapshots:p:", ++sync_i);
|
|
|
|
TEST_IDX_SYNC_POINT("WritePreparedTxnDB::UpdateSnapshots:s:", sync_i);
|
|
|
|
}
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// Release the remaining sync points since they are useless given that the
|
|
|
|
// reader would also use lock to access snapshots
|
|
|
|
for (++sync_i; sync_i <= 10; ++sync_i) {
|
|
|
|
TEST_IDX_SYNC_POINT("WritePreparedTxnDB::UpdateSnapshots:p:", sync_i);
|
|
|
|
TEST_IDX_SYNC_POINT("WritePreparedTxnDB::UpdateSnapshots:s:", sync_i);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
snapshots_.clear();
|
2019-05-15 22:14:18 +02:00
|
|
|
for (; it != snapshots.end(); ++it) {
|
2017-11-02 19:05:55 +01:00
|
|
|
// Insert them to a vector that is less efficient to access
|
|
|
|
// concurrently
|
|
|
|
snapshots_.push_back(*it);
|
|
|
|
}
|
|
|
|
// Update the size at the end. Otherwise a parallel reader might read
|
|
|
|
// items that are not set yet.
|
|
|
|
snapshots_total_.store(snapshots.size(), std::memory_order_release);
|
2018-11-29 04:01:37 +01:00
|
|
|
|
|
|
|
// Note: this must be done after the snapshots data structures are updated
|
|
|
|
// with the new list of snapshots.
|
|
|
|
CleanupReleasedSnapshots(snapshots, snapshots_all_);
|
|
|
|
snapshots_all_ = snapshots;
|
|
|
|
|
2017-11-02 19:05:55 +01:00
|
|
|
TEST_SYNC_POINT("WritePreparedTxnDB::UpdateSnapshots:p:end");
|
|
|
|
TEST_SYNC_POINT("WritePreparedTxnDB::UpdateSnapshots:s:end");
|
|
|
|
}
|
|
|
|
|
|
|
|
void WritePreparedTxnDB::CheckAgainstSnapshots(const CommitEntry& evicted) {
|
|
|
|
TEST_SYNC_POINT("WritePreparedTxnDB::CheckAgainstSnapshots:p:start");
|
|
|
|
TEST_SYNC_POINT("WritePreparedTxnDB::CheckAgainstSnapshots:s:start");
|
|
|
|
#ifndef NDEBUG
|
|
|
|
size_t sync_i = 0;
|
|
|
|
#endif
|
|
|
|
// First check the snapshot cache that is efficient for concurrent access
|
|
|
|
auto cnt = snapshots_total_.load(std::memory_order_acquire);
|
|
|
|
// The list might get updated concurrently as we are reading from it. The
|
|
|
|
// reader should be able to read all the snapshots that are still valid
|
|
|
|
// after the update. Since the survived snapshots are written in a higher
|
|
|
|
// place before gets overwritten the reader that reads bottom-up will
|
|
|
|
// eventully see it.
|
|
|
|
const bool next_is_larger = true;
|
2018-11-06 08:02:01 +01:00
|
|
|
// We will set to true if the border line snapshot suggests that.
|
|
|
|
bool search_larger_list = false;
|
2017-11-02 19:05:55 +01:00
|
|
|
size_t ip1 = std::min(cnt, SNAPSHOT_CACHE_SIZE);
|
|
|
|
for (; 0 < ip1; ip1--) {
|
2018-11-06 08:02:01 +01:00
|
|
|
SequenceNumber snapshot_seq =
|
|
|
|
snapshot_cache_[ip1 - 1].load(std::memory_order_acquire);
|
2017-11-02 19:05:55 +01:00
|
|
|
TEST_IDX_SYNC_POINT("WritePreparedTxnDB::CheckAgainstSnapshots:p:",
|
|
|
|
++sync_i);
|
|
|
|
TEST_IDX_SYNC_POINT("WritePreparedTxnDB::CheckAgainstSnapshots:s:", sync_i);
|
2018-11-06 08:02:01 +01:00
|
|
|
if (ip1 == SNAPSHOT_CACHE_SIZE) { // border line snapshot
|
|
|
|
// snapshot_seq < commit_seq => larger_snapshot_seq <= commit_seq
|
|
|
|
// then later also continue the search to larger snapshots
|
|
|
|
search_larger_list = snapshot_seq < evicted.commit_seq;
|
|
|
|
}
|
2017-11-02 19:05:55 +01:00
|
|
|
if (!MaybeUpdateOldCommitMap(evicted.prep_seq, evicted.commit_seq,
|
|
|
|
snapshot_seq, !next_is_larger)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// Release the remaining sync points before accquiring the lock
|
|
|
|
for (++sync_i; sync_i <= 10; ++sync_i) {
|
|
|
|
TEST_IDX_SYNC_POINT("WritePreparedTxnDB::CheckAgainstSnapshots:p:", sync_i);
|
|
|
|
TEST_IDX_SYNC_POINT("WritePreparedTxnDB::CheckAgainstSnapshots:s:", sync_i);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
TEST_SYNC_POINT("WritePreparedTxnDB::CheckAgainstSnapshots:p:end");
|
|
|
|
TEST_SYNC_POINT("WritePreparedTxnDB::CheckAgainstSnapshots:s:end");
|
2018-11-06 08:02:01 +01:00
|
|
|
if (UNLIKELY(SNAPSHOT_CACHE_SIZE < cnt && search_larger_list)) {
|
2017-11-02 19:05:55 +01:00
|
|
|
// Then access the less efficient list of snapshots_
|
2018-04-08 06:55:42 +02:00
|
|
|
WPRecordTick(TXN_SNAPSHOT_MUTEX_OVERHEAD);
|
2018-11-06 08:02:01 +01:00
|
|
|
ROCKS_LOG_WARN(info_log_,
|
|
|
|
"snapshots_mutex_ overhead for <%" PRIu64 ",%" PRIu64
|
|
|
|
"> with %" ROCKSDB_PRIszt " snapshots",
|
|
|
|
evicted.prep_seq, evicted.commit_seq, cnt);
|
2017-11-02 19:05:55 +01:00
|
|
|
ReadLock rl(&snapshots_mutex_);
|
|
|
|
// Items could have moved from the snapshots_ to snapshot_cache_ before
|
|
|
|
// accquiring the lock. To make sure that we do not miss a valid snapshot,
|
|
|
|
// read snapshot_cache_ again while holding the lock.
|
|
|
|
for (size_t i = 0; i < SNAPSHOT_CACHE_SIZE; i++) {
|
2018-11-06 08:02:01 +01:00
|
|
|
SequenceNumber snapshot_seq =
|
|
|
|
snapshot_cache_[i].load(std::memory_order_acquire);
|
2017-11-02 19:05:55 +01:00
|
|
|
if (!MaybeUpdateOldCommitMap(evicted.prep_seq, evicted.commit_seq,
|
|
|
|
snapshot_seq, next_is_larger)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto snapshot_seq_2 : snapshots_) {
|
|
|
|
if (!MaybeUpdateOldCommitMap(evicted.prep_seq, evicted.commit_seq,
|
|
|
|
snapshot_seq_2, next_is_larger)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WritePreparedTxnDB::MaybeUpdateOldCommitMap(
|
|
|
|
const uint64_t& prep_seq, const uint64_t& commit_seq,
|
|
|
|
const uint64_t& snapshot_seq, const bool next_is_larger = true) {
|
2017-12-13 16:52:02 +01:00
|
|
|
// If we do not store an entry in old_commit_map_ we assume it is committed in
|
|
|
|
// all snapshots. If commit_seq <= snapshot_seq, it is considered already in
|
2017-11-02 19:05:55 +01:00
|
|
|
// the snapshot so we need not to keep the entry around for this snapshot.
|
|
|
|
if (commit_seq <= snapshot_seq) {
|
|
|
|
// continue the search if the next snapshot could be smaller than commit_seq
|
|
|
|
return !next_is_larger;
|
|
|
|
}
|
|
|
|
// then snapshot_seq < commit_seq
|
|
|
|
if (prep_seq <= snapshot_seq) { // overlapping range
|
2018-04-08 06:55:42 +02:00
|
|
|
WPRecordTick(TXN_OLD_COMMIT_MAP_MUTEX_OVERHEAD);
|
2018-11-06 08:02:01 +01:00
|
|
|
ROCKS_LOG_WARN(info_log_,
|
|
|
|
"old_commit_map_mutex_ overhead for %" PRIu64
|
|
|
|
" commit entry: <%" PRIu64 ",%" PRIu64 ">",
|
|
|
|
snapshot_seq, prep_seq, commit_seq);
|
2017-11-02 19:05:55 +01:00
|
|
|
WriteLock wl(&old_commit_map_mutex_);
|
|
|
|
old_commit_map_empty_.store(false, std::memory_order_release);
|
2017-12-13 16:52:02 +01:00
|
|
|
auto& vec = old_commit_map_[snapshot_seq];
|
|
|
|
vec.insert(std::upper_bound(vec.begin(), vec.end(), prep_seq), prep_seq);
|
|
|
|
// We need to store it once for each overlapping snapshot. Returning true to
|
|
|
|
// continue the search if there is more overlapping snapshot.
|
|
|
|
return true;
|
2017-11-02 19:05:55 +01:00
|
|
|
}
|
|
|
|
// continue the search if the next snapshot could be larger than prep_seq
|
|
|
|
return next_is_larger;
|
|
|
|
}
|
|
|
|
|
|
|
|
WritePreparedTxnDB::~WritePreparedTxnDB() {
|
|
|
|
// At this point there could be running compaction/flush holding a
|
|
|
|
// SnapshotChecker, which holds a pointer back to WritePreparedTxnDB.
|
|
|
|
// Make sure those jobs finished before destructing WritePreparedTxnDB.
|
|
|
|
db_impl_->CancelAllBackgroundWork(true /*wait*/);
|
|
|
|
}
|
|
|
|
|
2018-03-05 19:48:29 +01:00
|
|
|
void SubBatchCounter::InitWithComp(const uint32_t cf) {
|
|
|
|
auto cmp = comparators_[cf];
|
|
|
|
keys_[cf] = CFKeys(SetComparator(cmp));
|
|
|
|
}
|
|
|
|
|
2018-02-13 01:27:39 +01:00
|
|
|
void SubBatchCounter::AddKey(const uint32_t cf, const Slice& key) {
|
|
|
|
CFKeys& cf_keys = keys_[cf];
|
|
|
|
if (cf_keys.size() == 0) { // just inserted
|
2018-03-05 19:48:29 +01:00
|
|
|
InitWithComp(cf);
|
2018-02-13 01:27:39 +01:00
|
|
|
}
|
|
|
|
auto it = cf_keys.insert(key);
|
|
|
|
if (it.second == false) { // second is false if a element already existed.
|
|
|
|
batches_++;
|
|
|
|
keys_.clear();
|
2018-03-05 19:48:29 +01:00
|
|
|
InitWithComp(cf);
|
2018-02-13 01:27:39 +01:00
|
|
|
keys_[cf].insert(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-02 19:05:55 +01:00
|
|
|
} // namespace rocksdb
|
|
|
|
#endif // ROCKSDB_LITE
|