2017-11-02 18:05:55 +00: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).
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2019-06-06 20:52:39 +00:00
|
|
|
#include <cinttypes>
|
2017-11-02 18:05:55 +00:00
|
|
|
#include <mutex>
|
|
|
|
#include <queue>
|
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "db/db_iter.h"
|
2017-12-01 07:39:56 +00:00
|
|
|
#include "db/pre_release_callback.h"
|
2017-11-02 18:05:55 +00:00
|
|
|
#include "db/read_callback.h"
|
|
|
|
#include "db/snapshot_checker.h"
|
2021-09-29 11:01:57 +00:00
|
|
|
#include "logging/logging.h"
|
2017-11-02 18:05:55 +00:00
|
|
|
#include "rocksdb/db.h"
|
|
|
|
#include "rocksdb/options.h"
|
|
|
|
#include "rocksdb/utilities/transaction_db.h"
|
2019-04-12 21:36:36 +00:00
|
|
|
#include "util/cast_util.h"
|
2018-03-06 07:48:23 +00:00
|
|
|
#include "util/set_comparator.h"
|
2018-01-09 16:47:46 +00:00
|
|
|
#include "util/string_util.h"
|
2017-11-02 18:05:55 +00:00
|
|
|
#include "utilities/transactions/pessimistic_transaction.h"
|
|
|
|
#include "utilities/transactions/pessimistic_transaction_db.h"
|
|
|
|
#include "utilities/transactions/write_prepared_txn.h"
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2019-08-05 20:30:56 +00:00
|
|
|
enum SnapshotBackup : bool { kUnbackedByDBSnapshot, kBackedByDBSnapshot };
|
2017-11-02 18:05:55 +00:00
|
|
|
|
|
|
|
// A PessimisticTransactionDB that writes data to DB after prepare phase of 2PC.
|
|
|
|
// In this way some data in the DB might not be committed. The DB provides
|
|
|
|
// mechanisms to tell such data apart from committed data.
|
|
|
|
class WritePreparedTxnDB : public PessimisticTransactionDB {
|
|
|
|
public:
|
2019-02-11 22:18:51 +00:00
|
|
|
explicit WritePreparedTxnDB(DB* db,
|
|
|
|
const TransactionDBOptions& txn_db_options)
|
2017-11-02 18:05:55 +00:00
|
|
|
: PessimisticTransactionDB(db, txn_db_options),
|
2019-02-11 22:18:51 +00:00
|
|
|
SNAPSHOT_CACHE_BITS(txn_db_options.wp_snapshot_cache_bits),
|
2017-11-02 18:05:55 +00:00
|
|
|
SNAPSHOT_CACHE_SIZE(static_cast<size_t>(1ull << SNAPSHOT_CACHE_BITS)),
|
2019-02-11 22:18:51 +00:00
|
|
|
COMMIT_CACHE_BITS(txn_db_options.wp_commit_cache_bits),
|
2017-11-02 18:05:55 +00:00
|
|
|
COMMIT_CACHE_SIZE(static_cast<size_t>(1ull << COMMIT_CACHE_BITS)),
|
|
|
|
FORMAT(COMMIT_CACHE_BITS) {
|
|
|
|
Init(txn_db_options);
|
|
|
|
}
|
|
|
|
|
2019-02-11 22:18:51 +00:00
|
|
|
explicit WritePreparedTxnDB(StackableDB* db,
|
|
|
|
const TransactionDBOptions& txn_db_options)
|
2017-11-02 18:05:55 +00:00
|
|
|
: PessimisticTransactionDB(db, txn_db_options),
|
2019-02-11 22:18:51 +00:00
|
|
|
SNAPSHOT_CACHE_BITS(txn_db_options.wp_snapshot_cache_bits),
|
2017-11-02 18:05:55 +00:00
|
|
|
SNAPSHOT_CACHE_SIZE(static_cast<size_t>(1ull << SNAPSHOT_CACHE_BITS)),
|
2019-02-11 22:18:51 +00:00
|
|
|
COMMIT_CACHE_BITS(txn_db_options.wp_commit_cache_bits),
|
2017-11-02 18:05:55 +00:00
|
|
|
COMMIT_CACHE_SIZE(static_cast<size_t>(1ull << COMMIT_CACHE_BITS)),
|
|
|
|
FORMAT(COMMIT_CACHE_BITS) {
|
|
|
|
Init(txn_db_options);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~WritePreparedTxnDB();
|
|
|
|
|
|
|
|
virtual Status Initialize(
|
|
|
|
const std::vector<size_t>& compaction_enabled_cf_indices,
|
|
|
|
const std::vector<ColumnFamilyHandle*>& handles) override;
|
|
|
|
|
|
|
|
Transaction* BeginTransaction(const WriteOptions& write_options,
|
|
|
|
const TransactionOptions& txn_options,
|
|
|
|
Transaction* old_txn) override;
|
|
|
|
|
2019-05-28 23:26:14 +00:00
|
|
|
using TransactionDB::Write;
|
|
|
|
Status Write(const WriteOptions& opts, WriteBatch* updates) override;
|
|
|
|
|
2018-02-06 23:22:58 +00:00
|
|
|
// Optimized version of ::Write that receives more optimization request such
|
|
|
|
// as skip_concurrency_control.
|
2018-02-13 00:27:39 +00:00
|
|
|
using PessimisticTransactionDB::Write;
|
|
|
|
Status Write(const WriteOptions& opts, const TransactionDBWriteOptimizations&,
|
|
|
|
WriteBatch* updates) override;
|
|
|
|
|
|
|
|
// Write the batch to the underlying DB and mark it as committed. Could be
|
|
|
|
// used by both directly from TxnDB or through a transaction.
|
|
|
|
Status WriteInternal(const WriteOptions& write_options, WriteBatch* batch,
|
|
|
|
size_t batch_cnt, WritePreparedTxn* txn);
|
2018-02-06 23:22:58 +00:00
|
|
|
|
2017-11-02 18:05:55 +00:00
|
|
|
using DB::Get;
|
|
|
|
virtual Status Get(const ReadOptions& options,
|
|
|
|
ColumnFamilyHandle* column_family, const Slice& key,
|
|
|
|
PinnableSlice* value) override;
|
|
|
|
|
2017-11-27 16:49:30 +00:00
|
|
|
using DB::MultiGet;
|
|
|
|
virtual std::vector<Status> MultiGet(
|
|
|
|
const ReadOptions& options,
|
|
|
|
const std::vector<ColumnFamilyHandle*>& column_family,
|
|
|
|
const std::vector<Slice>& keys,
|
|
|
|
std::vector<std::string>* values) override;
|
|
|
|
|
2017-11-02 18:05:55 +00:00
|
|
|
using DB::NewIterator;
|
|
|
|
virtual Iterator* NewIterator(const ReadOptions& options,
|
|
|
|
ColumnFamilyHandle* column_family) override;
|
|
|
|
|
|
|
|
using DB::NewIterators;
|
|
|
|
virtual Status NewIterators(
|
|
|
|
const ReadOptions& options,
|
|
|
|
const std::vector<ColumnFamilyHandle*>& column_families,
|
|
|
|
std::vector<Iterator*>* iterators) override;
|
|
|
|
|
2018-03-08 18:18:34 +00:00
|
|
|
// Check whether the transaction that wrote the value with sequence number seq
|
2018-04-03 03:19:21 +00:00
|
|
|
// is visible to the snapshot with sequence number snapshot_seq.
|
|
|
|
// Returns true if commit_seq <= snapshot_seq
|
2019-01-08 22:43:24 +00:00
|
|
|
// If the snapshot_seq is already released and snapshot_seq <= max, sets
|
|
|
|
// *snap_released to true and returns true as well.
|
2018-04-03 03:19:21 +00:00
|
|
|
inline bool IsInSnapshot(uint64_t prep_seq, uint64_t snapshot_seq,
|
2019-04-02 21:43:03 +00:00
|
|
|
uint64_t min_uncommitted = kMinUnCommittedSeq,
|
2019-01-08 22:43:24 +00:00
|
|
|
bool* snap_released = nullptr) const {
|
2018-04-03 03:19:21 +00:00
|
|
|
ROCKS_LOG_DETAILS(info_log_,
|
|
|
|
"IsInSnapshot %" PRIu64 " in %" PRIu64
|
|
|
|
" min_uncommitted %" PRIu64,
|
|
|
|
prep_seq, snapshot_seq, min_uncommitted);
|
2019-04-02 21:43:03 +00:00
|
|
|
assert(min_uncommitted >= kMinUnCommittedSeq);
|
2019-01-16 17:48:10 +00:00
|
|
|
// Caller is responsible to initialize snap_released.
|
|
|
|
assert(snap_released == nullptr || *snap_released == false);
|
2018-04-03 03:19:21 +00:00
|
|
|
// Here we try to infer the return value without looking into prepare list.
|
|
|
|
// This would help avoiding synchronization over a shared map.
|
|
|
|
// TODO(myabandeh): optimize this. This sequence of checks must be correct
|
|
|
|
// but not necessary efficient
|
|
|
|
if (prep_seq == 0) {
|
|
|
|
// Compaction will output keys to bottom-level with sequence number 0 if
|
|
|
|
// it is visible to the earliest snapshot.
|
|
|
|
ROCKS_LOG_DETAILS(
|
|
|
|
info_log_, "IsInSnapshot %" PRIu64 " in %" PRIu64 " returns %" PRId32,
|
|
|
|
prep_seq, snapshot_seq, 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (snapshot_seq < prep_seq) {
|
|
|
|
// snapshot_seq < prep_seq <= commit_seq => snapshot_seq < commit_seq
|
|
|
|
ROCKS_LOG_DETAILS(
|
|
|
|
info_log_, "IsInSnapshot %" PRIu64 " in %" PRIu64 " returns %" PRId32,
|
|
|
|
prep_seq, snapshot_seq, 0);
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-15 17:13:49 +00:00
|
|
|
if (prep_seq < min_uncommitted) {
|
|
|
|
ROCKS_LOG_DETAILS(info_log_,
|
|
|
|
"IsInSnapshot %" PRIu64 " in %" PRIu64
|
|
|
|
" returns %" PRId32
|
|
|
|
" because of min_uncommitted %" PRIu64,
|
|
|
|
prep_seq, snapshot_seq, 1, min_uncommitted);
|
|
|
|
return true;
|
|
|
|
}
|
2019-02-06 16:43:16 +00:00
|
|
|
// Commit of delayed prepared has two non-atomic steps: add to commit cache,
|
|
|
|
// remove from delayed prepared. Our reads from these two is also
|
|
|
|
// non-atomic. By looking into commit cache first thus we might not find the
|
|
|
|
// prep_seq neither in commit cache not in delayed_prepared_. To fix that i)
|
|
|
|
// we check if there was any delayed prepared BEFORE looking into commit
|
|
|
|
// cache, ii) if there was, we complete the search steps to be these: i)
|
|
|
|
// commit cache, ii) delayed prepared, commit cache again. In this way if
|
|
|
|
// the first query to commit cache missed the commit, the 2nd will catch it.
|
2019-02-20 00:09:50 +00:00
|
|
|
bool was_empty;
|
|
|
|
SequenceNumber max_evicted_seq_lb, max_evicted_seq_ub;
|
2018-04-03 03:19:21 +00:00
|
|
|
CommitEntry64b dont_care;
|
2019-02-20 00:09:50 +00:00
|
|
|
auto indexed_seq = prep_seq % COMMIT_CACHE_SIZE;
|
|
|
|
size_t repeats = 0;
|
|
|
|
do {
|
|
|
|
repeats++;
|
|
|
|
assert(repeats < 100);
|
|
|
|
if (UNLIKELY(repeats >= 100)) {
|
|
|
|
throw std::runtime_error(
|
|
|
|
"The read was intrupted 100 times by update to max_evicted_seq_. "
|
|
|
|
"This is unexpected in all setups");
|
|
|
|
}
|
|
|
|
max_evicted_seq_lb = max_evicted_seq_.load(std::memory_order_acquire);
|
|
|
|
TEST_SYNC_POINT(
|
|
|
|
"WritePreparedTxnDB::IsInSnapshot:max_evicted_seq_:pause");
|
|
|
|
TEST_SYNC_POINT(
|
|
|
|
"WritePreparedTxnDB::IsInSnapshot:max_evicted_seq_:resume");
|
|
|
|
was_empty = delayed_prepared_empty_.load(std::memory_order_acquire);
|
|
|
|
TEST_SYNC_POINT(
|
|
|
|
"WritePreparedTxnDB::IsInSnapshot:delayed_prepared_empty_:pause");
|
|
|
|
TEST_SYNC_POINT(
|
|
|
|
"WritePreparedTxnDB::IsInSnapshot:delayed_prepared_empty_:resume");
|
|
|
|
CommitEntry cached;
|
|
|
|
bool exist = GetCommitEntry(indexed_seq, &dont_care, &cached);
|
|
|
|
TEST_SYNC_POINT("WritePreparedTxnDB::IsInSnapshot:GetCommitEntry:pause");
|
|
|
|
TEST_SYNC_POINT("WritePreparedTxnDB::IsInSnapshot:GetCommitEntry:resume");
|
|
|
|
if (exist && prep_seq == cached.prep_seq) {
|
|
|
|
// It is committed and also not evicted from commit cache
|
|
|
|
ROCKS_LOG_DETAILS(
|
|
|
|
info_log_,
|
|
|
|
"IsInSnapshot %" PRIu64 " in %" PRIu64 " returns %" PRId32,
|
|
|
|
prep_seq, snapshot_seq, cached.commit_seq <= snapshot_seq);
|
|
|
|
return cached.commit_seq <= snapshot_seq;
|
|
|
|
}
|
|
|
|
// else it could be committed but not inserted in the map which could
|
|
|
|
// happen after recovery, or it could be committed and evicted by another
|
|
|
|
// commit, or never committed.
|
|
|
|
|
2020-02-28 22:10:51 +00:00
|
|
|
// At this point we don't know if it was committed or it is still prepared
|
2019-02-20 00:09:50 +00:00
|
|
|
max_evicted_seq_ub = max_evicted_seq_.load(std::memory_order_acquire);
|
|
|
|
if (UNLIKELY(max_evicted_seq_lb != max_evicted_seq_ub)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Note: max_evicted_seq_ when we did GetCommitEntry <= max_evicted_seq_ub
|
|
|
|
if (max_evicted_seq_ub < prep_seq) {
|
|
|
|
// Not evicted from cache and also not present, so must be still
|
|
|
|
// prepared
|
|
|
|
ROCKS_LOG_DETAILS(info_log_,
|
|
|
|
"IsInSnapshot %" PRIu64 " in %" PRIu64
|
|
|
|
" returns %" PRId32,
|
|
|
|
prep_seq, snapshot_seq, 0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
TEST_SYNC_POINT("WritePreparedTxnDB::IsInSnapshot:prepared_mutex_:pause");
|
|
|
|
TEST_SYNC_POINT(
|
|
|
|
"WritePreparedTxnDB::IsInSnapshot:prepared_mutex_:resume");
|
|
|
|
if (!was_empty) {
|
|
|
|
// We should not normally reach here
|
|
|
|
WPRecordTick(TXN_PREPARE_MUTEX_OVERHEAD);
|
|
|
|
ReadLock rl(&prepared_mutex_);
|
|
|
|
ROCKS_LOG_WARN(
|
|
|
|
info_log_, "prepared_mutex_ overhead %" PRIu64 " for %" PRIu64,
|
|
|
|
static_cast<uint64_t>(delayed_prepared_.size()), prep_seq);
|
|
|
|
if (delayed_prepared_.find(prep_seq) != delayed_prepared_.end()) {
|
|
|
|
// This is the order: 1) delayed_prepared_commits_ update, 2) publish
|
|
|
|
// 3) delayed_prepared_ clean up. So check if it is the case of a late
|
|
|
|
// clenaup.
|
|
|
|
auto it = delayed_prepared_commits_.find(prep_seq);
|
|
|
|
if (it == delayed_prepared_commits_.end()) {
|
|
|
|
// Then it is not committed yet
|
|
|
|
ROCKS_LOG_DETAILS(info_log_,
|
|
|
|
"IsInSnapshot %" PRIu64 " in %" PRIu64
|
|
|
|
" returns %" PRId32,
|
|
|
|
prep_seq, snapshot_seq, 0);
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
ROCKS_LOG_DETAILS(info_log_,
|
|
|
|
"IsInSnapshot %" PRIu64 " in %" PRIu64
|
|
|
|
" commit: %" PRIu64 " returns %" PRId32,
|
|
|
|
prep_seq, snapshot_seq, it->second,
|
|
|
|
snapshot_seq <= it->second);
|
|
|
|
return it->second <= snapshot_seq;
|
|
|
|
}
|
2019-01-18 19:33:30 +00:00
|
|
|
} else {
|
2019-02-20 00:09:50 +00:00
|
|
|
// 2nd query to commit cache. Refer to was_empty comment above.
|
|
|
|
exist = GetCommitEntry(indexed_seq, &dont_care, &cached);
|
|
|
|
if (exist && prep_seq == cached.prep_seq) {
|
|
|
|
ROCKS_LOG_DETAILS(
|
|
|
|
info_log_,
|
|
|
|
"IsInSnapshot %" PRIu64 " in %" PRIu64 " returns %" PRId32,
|
|
|
|
prep_seq, snapshot_seq, cached.commit_seq <= snapshot_seq);
|
|
|
|
return cached.commit_seq <= snapshot_seq;
|
|
|
|
}
|
|
|
|
max_evicted_seq_ub = max_evicted_seq_.load(std::memory_order_acquire);
|
2019-02-06 16:43:16 +00:00
|
|
|
}
|
2019-01-18 19:33:30 +00:00
|
|
|
}
|
2019-02-20 00:09:50 +00:00
|
|
|
} while (UNLIKELY(max_evicted_seq_lb != max_evicted_seq_ub));
|
2018-04-03 03:19:21 +00:00
|
|
|
// When advancing max_evicted_seq_, we move older entires from prepared to
|
|
|
|
// delayed_prepared_. Also we move evicted entries from commit cache to
|
|
|
|
// old_commit_map_ if it overlaps with any snapshot. Since prep_seq <=
|
|
|
|
// max_evicted_seq_, we have three cases: i) in delayed_prepared_, ii) in
|
|
|
|
// old_commit_map_, iii) committed with no conflict with any snapshot. Case
|
|
|
|
// (i) delayed_prepared_ is checked above
|
2019-02-20 00:09:50 +00:00
|
|
|
if (max_evicted_seq_ub < snapshot_seq) { // then (ii) cannot be the case
|
2018-04-03 03:19:21 +00:00
|
|
|
// only (iii) is the case: committed
|
|
|
|
// commit_seq <= max_evicted_seq_ < snapshot_seq => commit_seq <
|
|
|
|
// snapshot_seq
|
|
|
|
ROCKS_LOG_DETAILS(
|
|
|
|
info_log_, "IsInSnapshot %" PRIu64 " in %" PRIu64 " returns %" PRId32,
|
|
|
|
prep_seq, snapshot_seq, 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// else (ii) might be the case: check the commit data saved for this
|
|
|
|
// snapshot. If there was no overlapping commit entry, then it is committed
|
|
|
|
// with a commit_seq lower than any live snapshot, including snapshot_seq.
|
|
|
|
if (old_commit_map_empty_.load(std::memory_order_acquire)) {
|
2019-01-08 22:43:24 +00:00
|
|
|
ROCKS_LOG_DETAILS(info_log_,
|
|
|
|
"IsInSnapshot %" PRIu64 " in %" PRIu64
|
|
|
|
" returns %" PRId32 " released=1",
|
|
|
|
prep_seq, snapshot_seq, 0);
|
|
|
|
assert(snap_released);
|
|
|
|
// This snapshot is not valid anymore. We cannot tell if prep_seq is
|
|
|
|
// committed before or after the snapshot. Return true but also set
|
|
|
|
// snap_released to true.
|
|
|
|
*snap_released = true;
|
2018-04-03 03:19:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// We should not normally reach here unless sapshot_seq is old. This is a
|
|
|
|
// rare case and it is ok to pay the cost of mutex ReadLock for such old,
|
|
|
|
// reading transactions.
|
2018-04-08 04:55:42 +00:00
|
|
|
WPRecordTick(TXN_OLD_COMMIT_MAP_MUTEX_OVERHEAD);
|
2018-04-03 03:19:21 +00:00
|
|
|
ReadLock rl(&old_commit_map_mutex_);
|
|
|
|
auto prep_set_entry = old_commit_map_.find(snapshot_seq);
|
|
|
|
bool found = prep_set_entry != old_commit_map_.end();
|
|
|
|
if (found) {
|
|
|
|
auto& vec = prep_set_entry->second;
|
|
|
|
found = std::binary_search(vec.begin(), vec.end(), prep_seq);
|
2019-01-08 22:43:24 +00:00
|
|
|
} else {
|
|
|
|
// coming from compaction
|
|
|
|
ROCKS_LOG_DETAILS(info_log_,
|
|
|
|
"IsInSnapshot %" PRIu64 " in %" PRIu64
|
|
|
|
" returns %" PRId32 " released=1",
|
|
|
|
prep_seq, snapshot_seq, 0);
|
|
|
|
// This snapshot is not valid anymore. We cannot tell if prep_seq is
|
|
|
|
// committed before or after the snapshot. Return true but also set
|
|
|
|
// snap_released to true.
|
|
|
|
assert(snap_released);
|
|
|
|
*snap_released = true;
|
|
|
|
return true;
|
2018-04-03 03:19:21 +00:00
|
|
|
}
|
2019-01-08 22:43:24 +00:00
|
|
|
|
2018-04-03 03:19:21 +00:00
|
|
|
if (!found) {
|
|
|
|
ROCKS_LOG_DETAILS(info_log_,
|
|
|
|
"IsInSnapshot %" PRIu64 " in %" PRIu64
|
|
|
|
" returns %" PRId32,
|
|
|
|
prep_seq, snapshot_seq, 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// (ii) it the case: it is committed but after the snapshot_seq
|
|
|
|
ROCKS_LOG_DETAILS(
|
|
|
|
info_log_, "IsInSnapshot %" PRIu64 " in %" PRIu64 " returns %" PRId32,
|
|
|
|
prep_seq, snapshot_seq, 0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-28 23:20:40 +00:00
|
|
|
// Add the transaction with prepare sequence seq to the prepared list.
|
|
|
|
// Note: must be called serially with increasing seq on each call.
|
2019-06-10 18:47:16 +00:00
|
|
|
// locked is true if prepared_mutex_ is already locked.
|
|
|
|
void AddPrepared(uint64_t seq, bool locked = false);
|
2019-03-07 15:32:01 +00:00
|
|
|
// Check if any of the prepared txns are less than new max_evicted_seq_. Must
|
|
|
|
// be called with prepared_mutex_ write locked.
|
2019-06-10 18:47:16 +00:00
|
|
|
void CheckPreparedAgainstMax(SequenceNumber new_max, bool locked);
|
2018-04-12 02:59:25 +00:00
|
|
|
// Remove the transaction with prepare sequence seq from the prepared list
|
|
|
|
void RemovePrepared(const uint64_t seq, const size_t batch_cnt = 1);
|
2019-02-28 23:20:40 +00:00
|
|
|
// Add the transaction with prepare sequence prepare_seq and commit sequence
|
2018-04-12 02:59:25 +00:00
|
|
|
// commit_seq to the commit map. loop_cnt is to detect infinite loops.
|
2019-02-28 23:20:40 +00:00
|
|
|
// Note: must be called serially.
|
2018-01-09 16:47:46 +00:00
|
|
|
void AddCommitted(uint64_t prepare_seq, uint64_t commit_seq,
|
2018-04-12 02:59:25 +00:00
|
|
|
uint8_t loop_cnt = 0);
|
2017-11-02 18:05:55 +00:00
|
|
|
|
|
|
|
struct CommitEntry {
|
|
|
|
uint64_t prep_seq;
|
|
|
|
uint64_t commit_seq;
|
|
|
|
CommitEntry() : prep_seq(0), commit_seq(0) {}
|
|
|
|
CommitEntry(uint64_t ps, uint64_t cs) : prep_seq(ps), commit_seq(cs) {}
|
|
|
|
bool operator==(const CommitEntry& rhs) const {
|
|
|
|
return prep_seq == rhs.prep_seq && commit_seq == rhs.commit_seq;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CommitEntry64bFormat {
|
|
|
|
explicit CommitEntry64bFormat(size_t index_bits)
|
|
|
|
: INDEX_BITS(index_bits),
|
|
|
|
PREP_BITS(static_cast<size_t>(64 - PAD_BITS - INDEX_BITS)),
|
|
|
|
COMMIT_BITS(static_cast<size_t>(64 - PREP_BITS)),
|
2018-01-09 16:47:46 +00:00
|
|
|
COMMIT_FILTER(static_cast<uint64_t>((1ull << COMMIT_BITS) - 1)),
|
|
|
|
DELTA_UPPERBOUND(static_cast<uint64_t>((1ull << COMMIT_BITS))) {}
|
2017-11-02 18:05:55 +00:00
|
|
|
// Number of higher bits of a sequence number that is not used. They are
|
|
|
|
// used to encode the value type, ...
|
|
|
|
const size_t PAD_BITS = static_cast<size_t>(8);
|
|
|
|
// Number of lower bits from prepare seq that can be skipped as they are
|
|
|
|
// implied by the index of the entry in the array
|
|
|
|
const size_t INDEX_BITS;
|
|
|
|
// Number of bits we use to encode the prepare seq
|
|
|
|
const size_t PREP_BITS;
|
|
|
|
// Number of bits we use to encode the commit seq.
|
|
|
|
const size_t COMMIT_BITS;
|
|
|
|
// Filter to encode/decode commit seq
|
|
|
|
const uint64_t COMMIT_FILTER;
|
2018-01-09 16:47:46 +00:00
|
|
|
// The value of commit_seq - prepare_seq + 1 must be less than this bound
|
|
|
|
const uint64_t DELTA_UPPERBOUND;
|
2017-11-02 18:05:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Prepare Seq (64 bits) = PAD ... PAD PREP PREP ... PREP INDEX INDEX ...
|
2018-03-08 18:18:34 +00:00
|
|
|
// INDEX Delta Seq (64 bits) = 0 0 0 0 0 0 0 0 0 0 0 0 DELTA DELTA ...
|
2017-11-02 18:05:55 +00:00
|
|
|
// DELTA DELTA Encoded Value = PREP PREP .... PREP PREP DELTA DELTA
|
|
|
|
// ... DELTA DELTA PAD: first bits of a seq that is reserved for tagging and
|
|
|
|
// hence ignored PREP/INDEX: the used bits in a prepare seq number INDEX: the
|
|
|
|
// bits that do not have to be encoded (will be provided externally) DELTA:
|
|
|
|
// prep seq - commit seq + 1 Number of DELTA bits should be equal to number of
|
|
|
|
// index bits + PADs
|
|
|
|
struct CommitEntry64b {
|
|
|
|
constexpr CommitEntry64b() noexcept : rep_(0) {}
|
|
|
|
|
|
|
|
CommitEntry64b(const CommitEntry& entry, const CommitEntry64bFormat& format)
|
|
|
|
: CommitEntry64b(entry.prep_seq, entry.commit_seq, format) {}
|
|
|
|
|
|
|
|
CommitEntry64b(const uint64_t ps, const uint64_t cs,
|
|
|
|
const CommitEntry64bFormat& format) {
|
|
|
|
assert(ps < static_cast<uint64_t>(
|
|
|
|
(1ull << (format.PREP_BITS + format.INDEX_BITS))));
|
|
|
|
assert(ps <= cs);
|
|
|
|
uint64_t delta = cs - ps + 1; // make initialized delta always >= 1
|
|
|
|
// zero is reserved for uninitialized entries
|
|
|
|
assert(0 < delta);
|
2018-01-09 16:47:46 +00:00
|
|
|
assert(delta < format.DELTA_UPPERBOUND);
|
|
|
|
if (delta >= format.DELTA_UPPERBOUND) {
|
|
|
|
throw std::runtime_error(
|
|
|
|
"commit_seq >> prepare_seq. The allowed distance is " +
|
2022-05-06 20:03:58 +00:00
|
|
|
std::to_string(format.DELTA_UPPERBOUND) + " commit_seq is " +
|
|
|
|
std::to_string(cs) + " prepare_seq is " + std::to_string(ps));
|
2018-01-09 16:47:46 +00:00
|
|
|
}
|
2017-11-02 18:05:55 +00:00
|
|
|
rep_ = (ps << format.PAD_BITS) & ~format.COMMIT_FILTER;
|
|
|
|
rep_ = rep_ | delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return false if the entry is empty
|
|
|
|
bool Parse(const uint64_t indexed_seq, CommitEntry* entry,
|
|
|
|
const CommitEntry64bFormat& format) {
|
|
|
|
uint64_t delta = rep_ & format.COMMIT_FILTER;
|
|
|
|
// zero is reserved for uninitialized entries
|
|
|
|
assert(delta < static_cast<uint64_t>((1ull << format.COMMIT_BITS)));
|
|
|
|
if (delta == 0) {
|
|
|
|
return false; // initialized entry would have non-zero delta
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(indexed_seq < static_cast<uint64_t>((1ull << format.INDEX_BITS)));
|
|
|
|
uint64_t prep_up = rep_ & ~format.COMMIT_FILTER;
|
|
|
|
prep_up >>= format.PAD_BITS;
|
|
|
|
const uint64_t& prep_low = indexed_seq;
|
|
|
|
entry->prep_seq = prep_up | prep_low;
|
|
|
|
|
|
|
|
entry->commit_seq = entry->prep_seq + delta - 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint64_t rep_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Struct to hold ownership of snapshot and read callback for cleanup.
|
|
|
|
struct IteratorState;
|
|
|
|
|
2018-05-03 01:09:55 +00:00
|
|
|
std::shared_ptr<std::map<uint32_t, const Comparator*>> GetCFComparatorMap() {
|
|
|
|
return cf_map_;
|
|
|
|
}
|
|
|
|
std::shared_ptr<std::map<uint32_t, ColumnFamilyHandle*>> GetCFHandleMap() {
|
|
|
|
return handle_map_;
|
2018-02-16 16:36:47 +00:00
|
|
|
}
|
|
|
|
void UpdateCFComparatorMap(
|
|
|
|
const std::vector<ColumnFamilyHandle*>& handles) override;
|
2018-05-03 01:09:55 +00:00
|
|
|
void UpdateCFComparatorMap(ColumnFamilyHandle* handle) override;
|
2018-02-16 16:36:47 +00:00
|
|
|
|
2018-04-03 03:19:21 +00:00
|
|
|
virtual const Snapshot* GetSnapshot() override;
|
2019-01-16 02:07:50 +00:00
|
|
|
SnapshotImpl* GetSnapshotInternal(bool for_ww_conflict_check);
|
2018-04-03 03:19:21 +00:00
|
|
|
|
2018-02-06 02:32:54 +00:00
|
|
|
protected:
|
|
|
|
virtual Status VerifyCFOptions(
|
|
|
|
const ColumnFamilyOptions& cf_options) override;
|
2019-04-12 21:36:36 +00:00
|
|
|
// Assign the min and max sequence numbers for reading from the db. A seq >
|
|
|
|
// max is not valid, and a seq < min is valid, and a min <= seq < max requires
|
2019-08-05 20:30:56 +00:00
|
|
|
// further checking. Normally max is defined by the snapshot and min is by
|
2019-04-12 21:36:36 +00:00
|
|
|
// minimum uncommitted seq.
|
2019-08-05 20:30:56 +00:00
|
|
|
inline SnapshotBackup AssignMinMaxSeqs(const Snapshot* snapshot,
|
|
|
|
SequenceNumber* min,
|
|
|
|
SequenceNumber* max);
|
2019-04-12 21:36:36 +00:00
|
|
|
// Validate is a snapshot sequence number is still valid based on the latest
|
|
|
|
// db status. backed_by_snapshot specifies if the number is baked by an actual
|
|
|
|
// snapshot object. order specified the memory order with which we load the
|
|
|
|
// atomic variables: relax is enough for the default since we care about last
|
|
|
|
// value seen by same thread.
|
|
|
|
inline bool ValidateSnapshot(
|
2019-08-05 20:30:56 +00:00
|
|
|
const SequenceNumber snap_seq, const SnapshotBackup backed_by_snapshot,
|
2019-04-12 21:36:36 +00:00
|
|
|
std::memory_order order = std::memory_order_relaxed);
|
2019-08-05 20:30:56 +00:00
|
|
|
// Get a dummy snapshot that refers to kMaxSequenceNumber
|
|
|
|
Snapshot* GetMaxSnapshot() { return &dummy_max_snapshot_; }
|
2018-02-06 02:32:54 +00:00
|
|
|
|
Add rollback_deletion_type_callback to TxnDBOptions (#9873)
Summary:
This PR does not affect write-committed.
Add a member, `rollback_deletion_type_callback` to TransactionDBOptions
so that a write-prepared transaction, when rolling back, can call this
callback to decide if a `Delete` or `SingleDelete` should be used to
cancel a prior `Put` written to the database during prepare phase.
The purpose of this PR is to prevent mixing `Delete` and `SingleDelete`
for the same key, causing undefined behaviors. Without this PR, the
following can happen:
```
// The application always issues SingleDelete when deleting keys.
txn1->Put('a');
txn1->Prepare(); // writes to memtable and potentially gets flushed/compacted to Lmax
txn1->Rollback(); // inserts DELETE('a')
txn2->Put('a');
txn2->Commit(); // writes to memtable and potentially gets flushed/compacted
```
In the database, we may have
```
L0: [PUT('a', s=100)]
L1: [DELETE('a', s=90)]
Lmax: [PUT('a', s=0)]
```
If a compaction compacts L0 and L1, then we have
```
L1: [PUT('a', s=100)]
Lmax: [PUT('a', s=0)]
```
If a future transaction issues a SingleDelete, we have
```
L0: [SD('a', s=110)]
L1: [PUT('a', s=100)]
Lmax: [PUT('a', s=0)]
```
Then, a compaction including L0, L1 and Lmax leads to
```
Lmax: [PUT('a', s=0)]
```
which is incorrect.
Similar bugs reported and addressed in
https://github.com/cockroachdb/pebble/issues/1255. Based on our team's
current priority, we have decided to take this approach for now. We may
come back and revisit in the future.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/9873
Test Plan: make check
Reviewed By: ltamasi
Differential Revision: D35762170
Pulled By: riversand963
fbshipit-source-id: b28d56eefc786b53c9844b9ef4a7807acdd82c8d
2022-04-21 01:57:32 +00:00
|
|
|
bool ShouldRollbackWithSingleDelete(ColumnFamilyHandle* column_family,
|
|
|
|
const Slice& key) {
|
|
|
|
return rollback_deletion_type_callback_
|
|
|
|
? rollback_deletion_type_callback_(this, column_family, key)
|
|
|
|
: false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::function<bool(TransactionDB*, ColumnFamilyHandle*, const Slice&)>
|
|
|
|
rollback_deletion_type_callback_;
|
|
|
|
|
2017-11-02 18:05:55 +00:00
|
|
|
private:
|
2019-06-10 18:47:16 +00:00
|
|
|
friend class AddPreparedCallback;
|
2017-11-02 18:05:55 +00:00
|
|
|
friend class PreparedHeap_BasicsTest_Test;
|
2018-02-21 21:40:31 +00:00
|
|
|
friend class PreparedHeap_Concurrent_Test;
|
2019-03-07 15:32:01 +00:00
|
|
|
friend class PreparedHeap_EmptyAtTheEnd_Test;
|
2020-01-31 07:08:56 +00:00
|
|
|
friend class SnapshotConcurrentAccessTest_SnapshotConcurrentAccess_Test;
|
2019-03-07 15:32:01 +00:00
|
|
|
friend class WritePreparedCommitEntryPreReleaseCallback;
|
|
|
|
friend class WritePreparedTransactionTestBase;
|
2018-04-03 03:19:21 +00:00
|
|
|
friend class WritePreparedTxn;
|
2017-11-02 18:05:55 +00:00
|
|
|
friend class WritePreparedTxnDBMock;
|
2019-03-07 15:32:01 +00:00
|
|
|
friend class WritePreparedTransactionTest_AddPreparedBeforeMax_Test;
|
2020-01-31 07:08:56 +00:00
|
|
|
friend class WritePreparedTransactionTest_AdvanceMaxEvictedSeqBasic_Test;
|
2018-03-24 00:21:47 +00:00
|
|
|
friend class
|
2020-01-31 07:08:56 +00:00
|
|
|
WritePreparedTransactionTest_AdvanceMaxEvictedSeqWithDuplicates_Test;
|
2019-01-16 02:07:50 +00:00
|
|
|
friend class WritePreparedTransactionTest_AdvanceSeqByOne_Test;
|
2020-01-31 07:08:56 +00:00
|
|
|
friend class WritePreparedTransactionTest_BasicRecovery_Test;
|
|
|
|
friend class WritePreparedTransactionTest_CheckAgainstSnapshots_Test;
|
2019-02-04 20:53:55 +00:00
|
|
|
friend class WritePreparedTransactionTest_CleanupSnapshotEqualToMax_Test;
|
2020-01-31 07:08:56 +00:00
|
|
|
friend class WritePreparedTransactionTest_ConflictDetectionAfterRecovery_Test;
|
|
|
|
friend class WritePreparedTransactionTest_CommitMap_Test;
|
2018-11-29 03:01:37 +00:00
|
|
|
friend class WritePreparedTransactionTest_DoubleSnapshot_Test;
|
2020-01-31 07:08:56 +00:00
|
|
|
friend class WritePreparedTransactionTest_IsInSnapshotEmptyMap_Test;
|
2019-01-08 22:43:24 +00:00
|
|
|
friend class WritePreparedTransactionTest_IsInSnapshotReleased_Test;
|
2020-01-31 07:08:56 +00:00
|
|
|
friend class WritePreparedTransactionTest_IsInSnapshot_Test;
|
2019-01-16 02:07:50 +00:00
|
|
|
friend class WritePreparedTransactionTest_NewSnapshotLargerThanMax_Test;
|
|
|
|
friend class WritePreparedTransactionTest_MaxCatchupWithNewSnapshot_Test;
|
2019-08-05 20:30:56 +00:00
|
|
|
friend class WritePreparedTransactionTest_MaxCatchupWithUnbackedSnapshot_Test;
|
2019-02-20 00:09:50 +00:00
|
|
|
friend class
|
|
|
|
WritePreparedTransactionTest_NonAtomicCommitOfDelayedPrepared_Test;
|
|
|
|
friend class
|
|
|
|
WritePreparedTransactionTest_NonAtomicUpdateOfDelayedPrepared_Test;
|
|
|
|
friend class WritePreparedTransactionTest_NonAtomicUpdateOfMaxEvictedSeq_Test;
|
2017-12-13 15:52:02 +00:00
|
|
|
friend class WritePreparedTransactionTest_OldCommitMapGC_Test;
|
2020-01-31 07:08:56 +00:00
|
|
|
friend class WritePreparedTransactionTest_Rollback_Test;
|
2019-08-09 23:35:16 +00:00
|
|
|
friend class WritePreparedTransactionTest_SmallestUnCommittedSeq_Test;
|
2019-04-12 21:36:36 +00:00
|
|
|
friend class WriteUnpreparedTxn;
|
2018-07-07 00:17:36 +00:00
|
|
|
friend class WriteUnpreparedTxnDB;
|
2018-07-24 07:09:18 +00:00
|
|
|
friend class WriteUnpreparedTransactionTest_RecoveryTest_Test;
|
2022-04-28 00:50:54 +00:00
|
|
|
friend class MultiOpsTxnsStressTest;
|
2017-11-02 18:05:55 +00:00
|
|
|
|
Add rollback_deletion_type_callback to TxnDBOptions (#9873)
Summary:
This PR does not affect write-committed.
Add a member, `rollback_deletion_type_callback` to TransactionDBOptions
so that a write-prepared transaction, when rolling back, can call this
callback to decide if a `Delete` or `SingleDelete` should be used to
cancel a prior `Put` written to the database during prepare phase.
The purpose of this PR is to prevent mixing `Delete` and `SingleDelete`
for the same key, causing undefined behaviors. Without this PR, the
following can happen:
```
// The application always issues SingleDelete when deleting keys.
txn1->Put('a');
txn1->Prepare(); // writes to memtable and potentially gets flushed/compacted to Lmax
txn1->Rollback(); // inserts DELETE('a')
txn2->Put('a');
txn2->Commit(); // writes to memtable and potentially gets flushed/compacted
```
In the database, we may have
```
L0: [PUT('a', s=100)]
L1: [DELETE('a', s=90)]
Lmax: [PUT('a', s=0)]
```
If a compaction compacts L0 and L1, then we have
```
L1: [PUT('a', s=100)]
Lmax: [PUT('a', s=0)]
```
If a future transaction issues a SingleDelete, we have
```
L0: [SD('a', s=110)]
L1: [PUT('a', s=100)]
Lmax: [PUT('a', s=0)]
```
Then, a compaction including L0, L1 and Lmax leads to
```
Lmax: [PUT('a', s=0)]
```
which is incorrect.
Similar bugs reported and addressed in
https://github.com/cockroachdb/pebble/issues/1255. Based on our team's
current priority, we have decided to take this approach for now. We may
come back and revisit in the future.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/9873
Test Plan: make check
Reviewed By: ltamasi
Differential Revision: D35762170
Pulled By: riversand963
fbshipit-source-id: b28d56eefc786b53c9844b9ef4a7807acdd82c8d
2022-04-21 01:57:32 +00:00
|
|
|
void Init(const TransactionDBOptions& txn_db_opts);
|
2017-11-02 18:05:55 +00:00
|
|
|
|
2018-04-08 04:55:42 +00:00
|
|
|
void WPRecordTick(uint32_t ticker_type) const {
|
|
|
|
RecordTick(db_impl_->immutable_db_options_.statistics.get(), ticker_type);
|
|
|
|
}
|
|
|
|
|
2017-11-02 18:05:55 +00:00
|
|
|
// A heap with the amortized O(1) complexity for erase. It uses one extra heap
|
|
|
|
// to keep track of erased entries that are not yet on top of the main heap.
|
|
|
|
class PreparedHeap {
|
2019-06-10 18:47:16 +00:00
|
|
|
// The mutex is required for push and pop from PreparedHeap. ::erase will
|
|
|
|
// use external synchronization via prepared_mutex_.
|
|
|
|
port::Mutex push_pop_mutex_;
|
2019-06-12 02:52:08 +00:00
|
|
|
std::deque<uint64_t> heap_;
|
2017-11-02 18:05:55 +00:00
|
|
|
std::priority_queue<uint64_t, std::vector<uint64_t>, std::greater<uint64_t>>
|
|
|
|
erased_heap_;
|
2019-06-10 18:47:16 +00:00
|
|
|
std::atomic<uint64_t> heap_top_ = {kMaxSequenceNumber};
|
2018-02-21 21:40:31 +00:00
|
|
|
// True when testing crash recovery
|
|
|
|
bool TEST_CRASH_ = false;
|
|
|
|
friend class WritePreparedTxnDB;
|
2017-11-02 18:05:55 +00:00
|
|
|
|
|
|
|
public:
|
2018-02-21 21:40:31 +00:00
|
|
|
~PreparedHeap() {
|
|
|
|
if (!TEST_CRASH_) {
|
|
|
|
assert(heap_.empty());
|
|
|
|
assert(erased_heap_.empty());
|
|
|
|
}
|
|
|
|
}
|
2019-06-10 18:47:16 +00:00
|
|
|
port::Mutex* push_pop_mutex() { return &push_pop_mutex_; }
|
|
|
|
|
|
|
|
inline bool empty() { return top() == kMaxSequenceNumber; }
|
|
|
|
// Returns kMaxSequenceNumber if empty() and the smallest otherwise.
|
|
|
|
inline uint64_t top() { return heap_top_.load(std::memory_order_acquire); }
|
|
|
|
inline void push(uint64_t v) {
|
2019-06-12 02:52:08 +00:00
|
|
|
push_pop_mutex_.AssertHeld();
|
|
|
|
if (heap_.empty()) {
|
|
|
|
heap_top_.store(v, std::memory_order_release);
|
|
|
|
} else {
|
|
|
|
assert(heap_top_.load() < v);
|
|
|
|
}
|
2019-09-20 19:00:55 +00:00
|
|
|
heap_.push_back(v);
|
2019-06-10 18:47:16 +00:00
|
|
|
}
|
|
|
|
void pop(bool locked = false) {
|
|
|
|
if (!locked) {
|
|
|
|
push_pop_mutex()->Lock();
|
|
|
|
}
|
2019-06-12 02:52:08 +00:00
|
|
|
push_pop_mutex_.AssertHeld();
|
|
|
|
heap_.pop_front();
|
2017-11-02 18:05:55 +00:00
|
|
|
while (!heap_.empty() && !erased_heap_.empty() &&
|
2018-02-21 21:40:31 +00:00
|
|
|
// heap_.top() > erased_heap_.top() could happen if we have erased
|
|
|
|
// a non-existent entry. Ideally the user should not do that but we
|
2018-03-08 18:18:34 +00:00
|
|
|
// should be resilient against it.
|
2019-06-12 02:52:08 +00:00
|
|
|
heap_.front() >= erased_heap_.top()) {
|
|
|
|
if (heap_.front() == erased_heap_.top()) {
|
|
|
|
heap_.pop_front();
|
2018-02-21 21:40:31 +00:00
|
|
|
}
|
2018-03-06 19:47:42 +00:00
|
|
|
uint64_t erased __attribute__((__unused__));
|
|
|
|
erased = erased_heap_.top();
|
2017-11-02 18:05:55 +00:00
|
|
|
erased_heap_.pop();
|
2018-02-21 21:40:31 +00:00
|
|
|
// No duplicate prepare sequence numbers
|
|
|
|
assert(erased_heap_.empty() || erased_heap_.top() != erased);
|
2017-11-02 18:05:55 +00:00
|
|
|
}
|
|
|
|
while (heap_.empty() && !erased_heap_.empty()) {
|
|
|
|
erased_heap_.pop();
|
|
|
|
}
|
2019-06-12 02:52:08 +00:00
|
|
|
heap_top_.store(!heap_.empty() ? heap_.front() : kMaxSequenceNumber,
|
2019-06-10 18:47:16 +00:00
|
|
|
std::memory_order_release);
|
|
|
|
if (!locked) {
|
|
|
|
push_pop_mutex()->Unlock();
|
|
|
|
}
|
2017-11-02 18:05:55 +00:00
|
|
|
}
|
2019-06-10 18:47:16 +00:00
|
|
|
// Concurrrent calls needs external synchronization. It is safe to be called
|
|
|
|
// concurrent to push and pop though.
|
2017-11-02 18:05:55 +00:00
|
|
|
void erase(uint64_t seq) {
|
2019-06-12 02:52:08 +00:00
|
|
|
if (!empty()) {
|
2019-06-10 18:47:16 +00:00
|
|
|
auto top_seq = top();
|
|
|
|
if (seq < top_seq) {
|
2017-11-02 18:05:55 +00:00
|
|
|
// Already popped, ignore it.
|
2019-06-10 18:47:16 +00:00
|
|
|
} else if (top_seq == seq) {
|
2017-11-02 18:05:55 +00:00
|
|
|
pop();
|
2019-06-12 02:52:08 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
MutexLock ml(push_pop_mutex());
|
|
|
|
assert(heap_.empty() || heap_.front() != seq);
|
|
|
|
#endif
|
2019-06-10 18:47:16 +00:00
|
|
|
} else { // top() > seq
|
2017-11-02 18:05:55 +00:00
|
|
|
// Down the heap, remember to pop it later
|
|
|
|
erased_heap_.push(seq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-21 21:40:31 +00:00
|
|
|
void TEST_Crash() override { prepared_txns_.TEST_CRASH_ = true; }
|
|
|
|
|
2017-11-02 18:05:55 +00:00
|
|
|
// Get the commit entry with index indexed_seq from the commit table. It
|
|
|
|
// returns true if such entry exists.
|
|
|
|
bool GetCommitEntry(const uint64_t indexed_seq, CommitEntry64b* entry_64b,
|
|
|
|
CommitEntry* entry) const;
|
|
|
|
|
|
|
|
// Rewrite the entry with the index indexed_seq in the commit table with the
|
|
|
|
// commit entry <prep_seq, commit_seq>. If the rewrite results into eviction,
|
|
|
|
// sets the evicted_entry and returns true.
|
|
|
|
bool AddCommitEntry(const uint64_t indexed_seq, const CommitEntry& new_entry,
|
|
|
|
CommitEntry* evicted_entry);
|
|
|
|
|
|
|
|
// Rewrite the entry with the index indexed_seq in the commit table with the
|
|
|
|
// commit entry new_entry only if the existing entry matches the
|
|
|
|
// expected_entry. Returns false otherwise.
|
|
|
|
bool ExchangeCommitEntry(const uint64_t indexed_seq,
|
|
|
|
CommitEntry64b& expected_entry,
|
|
|
|
const CommitEntry& new_entry);
|
|
|
|
|
|
|
|
// Increase max_evicted_seq_ from the previous value prev_max to the new
|
|
|
|
// value. This also involves taking care of prepared txns that are not
|
|
|
|
// committed before new_max, as well as updating the list of live snapshots at
|
|
|
|
// the time of updating the max. Thread-safety: this function can be called
|
|
|
|
// concurrently. The concurrent invocations of this function is equivalent to
|
|
|
|
// a serial invocation in which the last invocation is the one with the
|
2018-03-08 18:18:34 +00:00
|
|
|
// largest new_max value.
|
2018-02-21 21:40:31 +00:00
|
|
|
void AdvanceMaxEvictedSeq(const SequenceNumber& prev_max,
|
|
|
|
const SequenceNumber& new_max);
|
2017-11-02 18:05:55 +00:00
|
|
|
|
2018-04-03 03:19:21 +00:00
|
|
|
inline SequenceNumber SmallestUnCommittedSeq() {
|
2019-08-09 23:35:16 +00:00
|
|
|
// Note: We have two lists to look into, but for performance reasons they
|
|
|
|
// are not read atomically. Since CheckPreparedAgainstMax copies the entry
|
|
|
|
// to delayed_prepared_ before removing it from prepared_txns_, to ensure
|
|
|
|
// that a prepared entry will not go unmissed, we look into them in opposite
|
|
|
|
// order: first read prepared_txns_ and then delayed_prepared_.
|
|
|
|
|
|
|
|
// This must be called before calling ::top. This is because the concurrent
|
|
|
|
// thread would call ::RemovePrepared before updating
|
|
|
|
// GetLatestSequenceNumber(). Reading then in opposite order here guarantees
|
|
|
|
// that the ::top that we read would be lower the ::top if we had otherwise
|
|
|
|
// update/read them atomically.
|
|
|
|
auto next_prepare = db_impl_->GetLatestSequenceNumber() + 1;
|
|
|
|
auto min_prepare = prepared_txns_.top();
|
2018-04-03 03:19:21 +00:00
|
|
|
// Since we update the prepare_heap always from the main write queue via
|
|
|
|
// PreReleaseCallback, the prepared_txns_.top() indicates the smallest
|
|
|
|
// prepared data in 2pc transactions. For non-2pc transactions that are
|
|
|
|
// written in two steps, we also update prepared_txns_ at the first step
|
|
|
|
// (via the same mechanism) so that their uncommitted data is reflected in
|
|
|
|
// SmallestUnCommittedSeq.
|
2019-06-10 18:47:16 +00:00
|
|
|
if (!delayed_prepared_empty_.load()) {
|
|
|
|
ReadLock rl(&prepared_mutex_);
|
|
|
|
if (!delayed_prepared_.empty()) {
|
|
|
|
return *delayed_prepared_.begin();
|
|
|
|
}
|
2019-01-15 17:13:49 +00:00
|
|
|
}
|
2019-06-10 18:47:16 +00:00
|
|
|
bool empty = min_prepare == kMaxSequenceNumber;
|
|
|
|
if (empty) {
|
|
|
|
// Since GetLatestSequenceNumber is updated
|
|
|
|
// after prepared_txns_ are, the value of GetLatestSequenceNumber would
|
|
|
|
// reflect any uncommitted data that is not added to prepared_txns_ yet.
|
|
|
|
// Otherwise, if there is no concurrent txn, this value simply reflects
|
|
|
|
// that latest value in the memtable.
|
|
|
|
return next_prepare;
|
2018-04-03 03:19:21 +00:00
|
|
|
} else {
|
2019-06-10 18:47:16 +00:00
|
|
|
return std::min(min_prepare, next_prepare);
|
2018-04-03 03:19:21 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-10 18:47:16 +00:00
|
|
|
|
2018-04-03 03:19:21 +00:00
|
|
|
// Enhance the snapshot object by recording in it the smallest uncommitted seq
|
2018-04-12 02:59:25 +00:00
|
|
|
inline void EnhanceSnapshot(SnapshotImpl* snapshot,
|
|
|
|
SequenceNumber min_uncommitted) {
|
2018-04-03 03:19:21 +00:00
|
|
|
assert(snapshot);
|
2019-06-10 18:47:16 +00:00
|
|
|
assert(min_uncommitted <= snapshot->number_ + 1);
|
2018-04-12 02:59:25 +00:00
|
|
|
snapshot->min_uncommitted_ = min_uncommitted;
|
2018-04-03 03:19:21 +00:00
|
|
|
}
|
|
|
|
|
2017-11-02 18:05:55 +00:00
|
|
|
virtual const std::vector<SequenceNumber> GetSnapshotListFromDB(
|
|
|
|
SequenceNumber max);
|
|
|
|
|
2017-12-13 15:52:02 +00:00
|
|
|
// Will be called by the public ReleaseSnapshot method. Does the maintenance
|
|
|
|
// internal to WritePreparedTxnDB
|
|
|
|
void ReleaseSnapshotInternal(const SequenceNumber snap_seq);
|
|
|
|
|
2017-11-02 18:05:55 +00:00
|
|
|
// Update the list of snapshots corresponding to the soon-to-be-updated
|
2018-03-08 18:18:34 +00:00
|
|
|
// max_evicted_seq_. Thread-safety: this function can be called concurrently.
|
2017-11-02 18:05:55 +00:00
|
|
|
// The concurrent invocations of this function is equivalent to a serial
|
2018-03-08 18:18:34 +00:00
|
|
|
// invocation in which the last invocation is the one with the largest
|
2017-11-02 18:05:55 +00:00
|
|
|
// version value.
|
|
|
|
void UpdateSnapshots(const std::vector<SequenceNumber>& snapshots,
|
|
|
|
const SequenceNumber& version);
|
2018-11-29 03:01:37 +00:00
|
|
|
// Check the new list of new snapshots against the old one to see if any of
|
|
|
|
// the snapshots are released and to do the cleanup for the released snapshot.
|
|
|
|
void CleanupReleasedSnapshots(
|
|
|
|
const std::vector<SequenceNumber>& new_snapshots,
|
|
|
|
const std::vector<SequenceNumber>& old_snapshots);
|
2017-11-02 18:05:55 +00:00
|
|
|
|
|
|
|
// Check an evicted entry against live snapshots to see if it should be kept
|
|
|
|
// around or it can be safely discarded (and hence assume committed for all
|
|
|
|
// snapshots). Thread-safety: this function can be called concurrently. If it
|
|
|
|
// is called concurrently with multiple UpdateSnapshots, the result is the
|
|
|
|
// same as checking the intersection of the snapshot list before updates with
|
|
|
|
// the snapshot list of all the concurrent updates.
|
|
|
|
void CheckAgainstSnapshots(const CommitEntry& evicted);
|
|
|
|
|
|
|
|
// Add a new entry to old_commit_map_ if prep_seq <= snapshot_seq <
|
|
|
|
// commit_seq. Return false if checking the next snapshot(s) is not needed.
|
2017-12-13 15:52:02 +00:00
|
|
|
// This is the case if none of the next snapshots could satisfy the condition.
|
|
|
|
// next_is_larger: the next snapshot will be a larger value
|
2017-11-02 18:05:55 +00:00
|
|
|
bool MaybeUpdateOldCommitMap(const uint64_t& prep_seq,
|
|
|
|
const uint64_t& commit_seq,
|
|
|
|
const uint64_t& snapshot_seq,
|
|
|
|
const bool next_is_larger);
|
|
|
|
|
2019-01-16 02:07:50 +00:00
|
|
|
// A trick to increase the last visible sequence number by one and also wait
|
|
|
|
// for the in-flight commits to be visible.
|
|
|
|
void AdvanceSeqByOne();
|
|
|
|
|
2017-11-02 18:05:55 +00:00
|
|
|
// The list of live snapshots at the last time that max_evicted_seq_ advanced.
|
|
|
|
// The list stored into two data structures: in snapshot_cache_ that is
|
|
|
|
// efficient for concurrent reads, and in snapshots_ if the data does not fit
|
|
|
|
// into snapshot_cache_. The total number of snapshots in the two lists
|
|
|
|
std::atomic<size_t> snapshots_total_ = {};
|
|
|
|
// The list sorted in ascending order. Thread-safety for writes is provided
|
|
|
|
// with snapshots_mutex_ and concurrent reads are safe due to std::atomic for
|
|
|
|
// each entry. In x86_64 architecture such reads are compiled to simple read
|
2019-02-11 22:18:51 +00:00
|
|
|
// instructions.
|
2017-11-02 18:05:55 +00:00
|
|
|
const size_t SNAPSHOT_CACHE_BITS;
|
|
|
|
const size_t SNAPSHOT_CACHE_SIZE;
|
2018-10-23 17:33:55 +00:00
|
|
|
std::unique_ptr<std::atomic<SequenceNumber>[]> snapshot_cache_;
|
2017-11-02 18:05:55 +00:00
|
|
|
// 2nd list for storing snapshots. The list sorted in ascending order.
|
|
|
|
// Thread-safety is provided with snapshots_mutex_.
|
|
|
|
std::vector<SequenceNumber> snapshots_;
|
2018-11-29 03:01:37 +00:00
|
|
|
// The list of all snapshots: snapshots_ + snapshot_cache_. This list although
|
|
|
|
// redundant but simplifies CleanupOldSnapshots implementation.
|
|
|
|
// Thread-safety is provided with snapshots_mutex_.
|
|
|
|
std::vector<SequenceNumber> snapshots_all_;
|
2017-11-02 18:05:55 +00:00
|
|
|
// The version of the latest list of snapshots. This can be used to avoid
|
2018-03-08 18:18:34 +00:00
|
|
|
// rewriting a list that is concurrently updated with a more recent version.
|
2017-11-02 18:05:55 +00:00
|
|
|
SequenceNumber snapshots_version_ = 0;
|
|
|
|
|
|
|
|
// A heap of prepared transactions. Thread-safety is provided with
|
|
|
|
// prepared_mutex_.
|
|
|
|
PreparedHeap prepared_txns_;
|
|
|
|
const size_t COMMIT_CACHE_BITS;
|
|
|
|
const size_t COMMIT_CACHE_SIZE;
|
|
|
|
const CommitEntry64bFormat FORMAT;
|
|
|
|
// commit_cache_ must be initialized to zero to tell apart an empty index from
|
|
|
|
// a filled one. Thread-safety is provided with commit_cache_mutex_.
|
2018-10-23 17:33:55 +00:00
|
|
|
std::unique_ptr<std::atomic<CommitEntry64b>[]> commit_cache_;
|
2018-01-09 16:47:46 +00:00
|
|
|
// The largest evicted *commit* sequence number from the commit_cache_. If a
|
|
|
|
// seq is smaller than max_evicted_seq_ is might or might not be present in
|
|
|
|
// commit_cache_. So commit_cache_ must first be checked before consulting
|
|
|
|
// with max_evicted_seq_.
|
2017-11-02 18:05:55 +00:00
|
|
|
std::atomic<uint64_t> max_evicted_seq_ = {};
|
2019-01-18 19:33:30 +00:00
|
|
|
// Order: 1) update future_max_evicted_seq_ = new_max, 2)
|
|
|
|
// GetSnapshotListFromDB(new_max), max_evicted_seq_ = new_max. Since
|
|
|
|
// GetSnapshotInternal guarantess that the snapshot seq is larger than
|
|
|
|
// future_max_evicted_seq_, this guarantes that if a snapshot is not larger
|
|
|
|
// than max has already being looked at via a GetSnapshotListFromDB(new_max).
|
|
|
|
std::atomic<uint64_t> future_max_evicted_seq_ = {};
|
2017-11-02 18:05:55 +00:00
|
|
|
// Advance max_evicted_seq_ by this value each time it needs an update. The
|
|
|
|
// larger the value, the less frequent advances we would have. We do not want
|
|
|
|
// it to be too large either as it would cause stalls by doing too much
|
|
|
|
// maintenance work under the lock.
|
|
|
|
size_t INC_STEP_FOR_MAX_EVICTED = 1;
|
2017-12-13 15:52:02 +00:00
|
|
|
// A map from old snapshots (expected to be used by a few read-only txns) to
|
2018-03-08 18:18:34 +00:00
|
|
|
// prepared sequence number of the evicted entries from commit_cache_ that
|
2017-12-13 15:52:02 +00:00
|
|
|
// overlaps with such snapshot. These are the prepared sequence numbers that
|
|
|
|
// the snapshot, to which they are mapped, cannot assume to be committed just
|
|
|
|
// because it is no longer in the commit_cache_. The vector must be sorted
|
|
|
|
// after each update.
|
2017-11-02 18:05:55 +00:00
|
|
|
// Thread-safety is provided with old_commit_map_mutex_.
|
2017-12-13 15:52:02 +00:00
|
|
|
std::map<SequenceNumber, std::vector<SequenceNumber>> old_commit_map_;
|
2017-11-02 18:05:55 +00:00
|
|
|
// A set of long-running prepared transactions that are not finished by the
|
|
|
|
// time max_evicted_seq_ advances their sequence number. This is expected to
|
|
|
|
// be empty normally. Thread-safety is provided with prepared_mutex_.
|
|
|
|
std::set<uint64_t> delayed_prepared_;
|
2019-01-18 19:33:30 +00:00
|
|
|
// Commit of a delayed prepared: 1) update commit cache, 2) update
|
|
|
|
// delayed_prepared_commits_, 3) publish seq, 3) clean up delayed_prepared_.
|
|
|
|
// delayed_prepared_commits_ will help us tell apart the unprepared txns from
|
|
|
|
// the ones that are committed but not cleaned up yet.
|
|
|
|
std::unordered_map<SequenceNumber, SequenceNumber> delayed_prepared_commits_;
|
2017-11-02 18:05:55 +00:00
|
|
|
// Update when delayed_prepared_.empty() changes. Expected to be true
|
|
|
|
// normally.
|
|
|
|
std::atomic<bool> delayed_prepared_empty_ = {true};
|
|
|
|
// Update when old_commit_map_.empty() changes. Expected to be true normally.
|
|
|
|
std::atomic<bool> old_commit_map_empty_ = {true};
|
|
|
|
mutable port::RWMutex prepared_mutex_;
|
|
|
|
mutable port::RWMutex old_commit_map_mutex_;
|
|
|
|
mutable port::RWMutex commit_cache_mutex_;
|
|
|
|
mutable port::RWMutex snapshots_mutex_;
|
2018-02-16 16:36:47 +00:00
|
|
|
// A cache of the cf comparators
|
2018-05-03 01:09:55 +00:00
|
|
|
// Thread safety: since it is a const it is safe to read it concurrently
|
|
|
|
std::shared_ptr<std::map<uint32_t, const Comparator*>> cf_map_;
|
|
|
|
// A cache of the cf handles
|
|
|
|
// Thread safety: since the handle is read-only object it is a const it is
|
|
|
|
// safe to read it concurrently
|
|
|
|
std::shared_ptr<std::map<uint32_t, ColumnFamilyHandle*>> handle_map_;
|
2019-08-05 20:30:56 +00:00
|
|
|
// A dummy snapshot object that refers to kMaxSequenceNumber
|
|
|
|
SnapshotImpl dummy_max_snapshot_;
|
2017-11-02 18:05:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class WritePreparedTxnReadCallback : public ReadCallback {
|
|
|
|
public:
|
2019-04-02 21:43:03 +00:00
|
|
|
WritePreparedTxnReadCallback(WritePreparedTxnDB* db, SequenceNumber snapshot)
|
2019-08-05 20:30:56 +00:00
|
|
|
: ReadCallback(snapshot),
|
|
|
|
db_(db),
|
|
|
|
backed_by_snapshot_(kBackedByDBSnapshot) {}
|
2018-04-03 03:19:21 +00:00
|
|
|
WritePreparedTxnReadCallback(WritePreparedTxnDB* db, SequenceNumber snapshot,
|
2019-08-05 20:30:56 +00:00
|
|
|
SequenceNumber min_uncommitted,
|
|
|
|
SnapshotBackup backed_by_snapshot)
|
|
|
|
: ReadCallback(snapshot, min_uncommitted),
|
|
|
|
db_(db),
|
|
|
|
backed_by_snapshot_(backed_by_snapshot) {
|
|
|
|
(void)backed_by_snapshot_; // to silence unused private field warning
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~WritePreparedTxnReadCallback() {
|
|
|
|
// If it is not backed by snapshot, the caller must check validity
|
|
|
|
assert(valid_checked_ || backed_by_snapshot_ == kBackedByDBSnapshot);
|
|
|
|
}
|
2017-11-02 18:05:55 +00:00
|
|
|
|
2018-06-27 19:05:29 +00:00
|
|
|
// Will be called to see if the seq number visible; if not it moves on to
|
|
|
|
// the next seq number.
|
2019-02-27 00:52:20 +00:00
|
|
|
inline virtual bool IsVisibleFullCheck(SequenceNumber seq) override {
|
2019-04-02 21:43:03 +00:00
|
|
|
auto snapshot = max_visible_seq_;
|
2019-08-05 20:30:56 +00:00
|
|
|
bool snap_released = false;
|
|
|
|
auto ret =
|
|
|
|
db_->IsInSnapshot(seq, snapshot, min_uncommitted_, &snap_released);
|
|
|
|
assert(!snap_released || backed_by_snapshot_ == kUnbackedByDBSnapshot);
|
|
|
|
snap_released_ |= snap_released;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool valid() {
|
|
|
|
valid_checked_ = true;
|
|
|
|
return snap_released_ == false;
|
2017-11-02 18:05:55 +00:00
|
|
|
}
|
|
|
|
|
2019-04-02 21:43:03 +00:00
|
|
|
// TODO(myabandeh): override Refresh when Iterator::Refresh is supported
|
2017-11-02 18:05:55 +00:00
|
|
|
private:
|
|
|
|
WritePreparedTxnDB* db_;
|
2019-08-05 20:30:56 +00:00
|
|
|
// Whether max_visible_seq_ is backed by a snapshot
|
|
|
|
const SnapshotBackup backed_by_snapshot_;
|
|
|
|
bool snap_released_ = false;
|
|
|
|
// Safety check to ensure that the caller has checked invalid statuses
|
|
|
|
bool valid_checked_ = false;
|
2018-04-03 03:19:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class AddPreparedCallback : public PreReleaseCallback {
|
|
|
|
public:
|
2019-04-02 22:14:41 +00:00
|
|
|
AddPreparedCallback(WritePreparedTxnDB* db, DBImpl* db_impl,
|
|
|
|
size_t sub_batch_cnt, bool two_write_queues,
|
|
|
|
bool first_prepare_batch)
|
2018-04-03 03:19:21 +00:00
|
|
|
: db_(db),
|
2019-04-02 22:14:41 +00:00
|
|
|
db_impl_(db_impl),
|
2018-04-03 03:19:21 +00:00
|
|
|
sub_batch_cnt_(sub_batch_cnt),
|
2019-04-02 22:14:41 +00:00
|
|
|
two_write_queues_(two_write_queues),
|
|
|
|
first_prepare_batch_(first_prepare_batch) {
|
2018-04-03 03:19:21 +00:00
|
|
|
(void)two_write_queues_; // to silence unused private field warning
|
|
|
|
}
|
|
|
|
virtual Status Callback(SequenceNumber prepare_seq,
|
2019-04-02 22:14:41 +00:00
|
|
|
bool is_mem_disabled __attribute__((__unused__)),
|
2019-06-10 18:47:16 +00:00
|
|
|
uint64_t log_number, size_t index,
|
|
|
|
size_t total) override {
|
|
|
|
assert(index < total);
|
|
|
|
// To reduce the cost of lock acquisition competing with the concurrent
|
|
|
|
// prepare requests, lock on the first callback and unlock on the last.
|
|
|
|
const bool do_lock = !two_write_queues_ || index == 0;
|
|
|
|
const bool do_unlock = !two_write_queues_ || index + 1 == total;
|
2019-02-28 23:20:40 +00:00
|
|
|
// Always Prepare from the main queue
|
2018-04-03 03:19:21 +00:00
|
|
|
assert(!two_write_queues_ || !is_mem_disabled); // implies the 1st queue
|
2019-06-10 18:47:16 +00:00
|
|
|
TEST_SYNC_POINT("AddPreparedCallback::AddPrepared::begin:pause");
|
|
|
|
TEST_SYNC_POINT("AddPreparedCallback::AddPrepared::begin:resume");
|
|
|
|
if (do_lock) {
|
|
|
|
db_->prepared_txns_.push_pop_mutex()->Lock();
|
|
|
|
}
|
|
|
|
const bool kLocked = true;
|
2018-04-03 03:19:21 +00:00
|
|
|
for (size_t i = 0; i < sub_batch_cnt_; i++) {
|
2019-06-10 18:47:16 +00:00
|
|
|
db_->AddPrepared(prepare_seq + i, kLocked);
|
2018-04-03 03:19:21 +00:00
|
|
|
}
|
2019-06-10 18:47:16 +00:00
|
|
|
if (do_unlock) {
|
|
|
|
db_->prepared_txns_.push_pop_mutex()->Unlock();
|
|
|
|
}
|
|
|
|
TEST_SYNC_POINT("AddPreparedCallback::AddPrepared::end");
|
2019-04-02 22:14:41 +00:00
|
|
|
if (first_prepare_batch_) {
|
|
|
|
assert(log_number != 0);
|
|
|
|
db_impl_->logs_with_prep_tracker()->MarkLogAsContainingPrepSection(
|
|
|
|
log_number);
|
|
|
|
}
|
2018-04-03 03:19:21 +00:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
WritePreparedTxnDB* db_;
|
2019-04-02 22:14:41 +00:00
|
|
|
DBImpl* db_impl_;
|
2018-04-03 03:19:21 +00:00
|
|
|
size_t sub_batch_cnt_;
|
|
|
|
bool two_write_queues_;
|
2019-04-02 22:14:41 +00:00
|
|
|
// It is 2PC and this is the first prepare batch. Always the case in 2PC
|
|
|
|
// unless it is WriteUnPrepared.
|
|
|
|
bool first_prepare_batch_;
|
2017-11-02 18:05:55 +00:00
|
|
|
};
|
|
|
|
|
2017-12-01 07:39:56 +00:00
|
|
|
class WritePreparedCommitEntryPreReleaseCallback : public PreReleaseCallback {
|
|
|
|
public:
|
|
|
|
// includes_data indicates that the commit also writes non-empty
|
|
|
|
// CommitTimeWriteBatch to memtable, which needs to be committed separately.
|
2019-02-28 23:20:40 +00:00
|
|
|
WritePreparedCommitEntryPreReleaseCallback(
|
|
|
|
WritePreparedTxnDB* db, DBImpl* db_impl, SequenceNumber prep_seq,
|
|
|
|
size_t prep_batch_cnt, size_t data_batch_cnt = 0,
|
|
|
|
SequenceNumber aux_seq = kMaxSequenceNumber, size_t aux_batch_cnt = 0)
|
2017-12-01 07:39:56 +00:00
|
|
|
: db_(db),
|
|
|
|
db_impl_(db_impl),
|
|
|
|
prep_seq_(prep_seq),
|
2018-02-06 02:32:54 +00:00
|
|
|
prep_batch_cnt_(prep_batch_cnt),
|
|
|
|
data_batch_cnt_(data_batch_cnt),
|
2018-03-22 21:27:44 +00:00
|
|
|
includes_data_(data_batch_cnt_ > 0),
|
2019-02-28 23:20:40 +00:00
|
|
|
aux_seq_(aux_seq),
|
|
|
|
aux_batch_cnt_(aux_batch_cnt),
|
|
|
|
includes_aux_batch_(aux_batch_cnt > 0) {
|
2018-02-06 02:32:54 +00:00
|
|
|
assert((prep_batch_cnt_ > 0) != (prep_seq == kMaxSequenceNumber)); // xor
|
|
|
|
assert(prep_batch_cnt_ > 0 || data_batch_cnt_ > 0);
|
2019-02-28 23:20:40 +00:00
|
|
|
assert((aux_batch_cnt_ > 0) != (aux_seq == kMaxSequenceNumber)); // xor
|
2018-02-06 02:32:54 +00:00
|
|
|
}
|
2017-12-01 07:39:56 +00:00
|
|
|
|
2018-03-22 21:27:44 +00:00
|
|
|
virtual Status Callback(SequenceNumber commit_seq,
|
2019-04-02 22:14:41 +00:00
|
|
|
bool is_mem_disabled __attribute__((__unused__)),
|
2019-06-10 18:47:16 +00:00
|
|
|
uint64_t, size_t /*index*/,
|
|
|
|
size_t /*total*/) override {
|
2019-02-28 23:20:40 +00:00
|
|
|
// Always commit from the 2nd queue
|
|
|
|
assert(!db_impl_->immutable_db_options().two_write_queues ||
|
|
|
|
is_mem_disabled);
|
2017-12-18 16:03:18 +00:00
|
|
|
assert(includes_data_ || prep_seq_ != kMaxSequenceNumber);
|
2019-02-28 23:20:40 +00:00
|
|
|
// Data batch is what accompanied with the commit marker and affects the
|
|
|
|
// last seq in the commit batch.
|
2018-02-06 02:32:54 +00:00
|
|
|
const uint64_t last_commit_seq = LIKELY(data_batch_cnt_ <= 1)
|
|
|
|
? commit_seq
|
|
|
|
: commit_seq + data_batch_cnt_ - 1;
|
2017-12-18 16:03:18 +00:00
|
|
|
if (prep_seq_ != kMaxSequenceNumber) {
|
2018-02-06 02:32:54 +00:00
|
|
|
for (size_t i = 0; i < prep_batch_cnt_; i++) {
|
2018-04-12 02:59:25 +00:00
|
|
|
db_->AddCommitted(prep_seq_ + i, last_commit_seq);
|
2018-02-06 02:32:54 +00:00
|
|
|
}
|
2017-12-18 16:03:18 +00:00
|
|
|
} // else there was no prepare phase
|
2019-02-28 23:20:40 +00:00
|
|
|
if (includes_aux_batch_) {
|
|
|
|
for (size_t i = 0; i < aux_batch_cnt_; i++) {
|
|
|
|
db_->AddCommitted(aux_seq_ + i, last_commit_seq);
|
|
|
|
}
|
|
|
|
}
|
2017-12-01 07:39:56 +00:00
|
|
|
if (includes_data_) {
|
2018-02-06 02:32:54 +00:00
|
|
|
assert(data_batch_cnt_);
|
2018-03-08 18:18:34 +00:00
|
|
|
// Commit the data that is accompanied with the commit request
|
2018-02-06 02:32:54 +00:00
|
|
|
for (size_t i = 0; i < data_batch_cnt_; i++) {
|
|
|
|
// For commit seq of each batch use the commit seq of the last batch.
|
|
|
|
// This would make debugging easier by having all the batches having
|
|
|
|
// the same sequence number.
|
2018-04-12 02:59:25 +00:00
|
|
|
db_->AddCommitted(commit_seq + i, last_commit_seq);
|
2018-02-06 02:32:54 +00:00
|
|
|
}
|
2017-12-01 07:39:56 +00:00
|
|
|
}
|
2019-02-28 23:20:40 +00:00
|
|
|
if (db_impl_->immutable_db_options().two_write_queues) {
|
2018-03-22 21:27:44 +00:00
|
|
|
assert(is_mem_disabled); // implies the 2nd queue
|
2017-12-18 16:03:18 +00:00
|
|
|
// Publish the sequence number. We can do that here assuming the callback
|
|
|
|
// is invoked only from one write queue, which would guarantee that the
|
|
|
|
// publish sequence numbers will be in order, i.e., once a seq is
|
|
|
|
// published all the seq prior to that are also publishable.
|
2018-02-06 02:32:54 +00:00
|
|
|
db_impl_->SetLastPublishedSequence(last_commit_seq);
|
2019-06-10 18:47:16 +00:00
|
|
|
// Note RemovePrepared should be called after publishing the seq.
|
|
|
|
// Otherwise SmallestUnCommittedSeq optimization breaks.
|
|
|
|
if (prep_seq_ != kMaxSequenceNumber) {
|
|
|
|
db_->RemovePrepared(prep_seq_, prep_batch_cnt_);
|
|
|
|
} // else there was no prepare phase
|
|
|
|
if (includes_aux_batch_) {
|
|
|
|
db_->RemovePrepared(aux_seq_, aux_batch_cnt_);
|
|
|
|
}
|
2017-12-18 16:03:18 +00:00
|
|
|
}
|
|
|
|
// else SequenceNumber that is updated as part of the write already does the
|
|
|
|
// publishing
|
2017-12-01 07:39:56 +00:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
WritePreparedTxnDB* db_;
|
|
|
|
DBImpl* db_impl_;
|
2017-12-18 16:03:18 +00:00
|
|
|
// kMaxSequenceNumber if there was no prepare phase
|
2017-12-01 07:39:56 +00:00
|
|
|
SequenceNumber prep_seq_;
|
2018-02-06 02:32:54 +00:00
|
|
|
size_t prep_batch_cnt_;
|
|
|
|
size_t data_batch_cnt_;
|
2019-02-28 23:20:40 +00:00
|
|
|
// Data here is the batch that is written with the commit marker, either
|
|
|
|
// because it is commit without prepare or commit has a CommitTimeWriteBatch.
|
2017-12-01 07:39:56 +00:00
|
|
|
bool includes_data_;
|
2019-02-28 23:20:40 +00:00
|
|
|
// Auxiliary batch (if there is any) is a batch that is written before, but
|
|
|
|
// gets the same commit seq as prepare batch or data batch. This is used in
|
|
|
|
// two write queues where the CommitTimeWriteBatch becomes the aux batch and
|
|
|
|
// we do a separate write to actually commit everything.
|
|
|
|
SequenceNumber aux_seq_;
|
|
|
|
size_t aux_batch_cnt_;
|
|
|
|
bool includes_aux_batch_;
|
2017-12-01 07:39:56 +00:00
|
|
|
};
|
|
|
|
|
2019-01-30 00:23:21 +00:00
|
|
|
// For two_write_queues commit both the aborted batch and the cleanup batch and
|
|
|
|
// then published the seq
|
WritePrepared: fix two versions in compaction see different status for released snapshots (#4890)
Summary:
Fix how CompactionIterator::findEarliestVisibleSnapshots handles released snapshot. It fixing the two scenarios:
Scenario 1:
key1 has two values v1 and v2. There're two snapshots s1 and s2 taken after v1 and v2 are committed. Right after compaction output v2, s1 is released. Now findEarliestVisibleSnapshot may see s1 being released, and return the next snapshot, which is s2. That's larger than v2's earliest visible snapshot, which was s1.
The fix: the only place we check against last snapshot and current key snapshot is when we decide whether to compact out a value if it is hidden by a later value. In the check if we see current snapshot is even larger than last snapshot, we know last snapshot is released, and we are safe to compact out current key.
Scenario 2:
key1 has two values v1 and v2. there are two snapshots s1 and s2 taken after v1 and v2 are committed. During compaction before we process the key, s1 is released. When compaction process v2, snapshot checker may return kSnapshotReleased, and the earliest visible snapshot for v2 become s2. When compaction process v1, snapshot checker may return kIsInSnapshot (for WritePrepared transaction, it could be because v1 is still in commit cache). The result will become inconsistent here.
The fix: remember the set of released snapshots ever reported by snapshot checker, and ignore them when finding result for findEarliestVisibleSnapshot.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/4890
Differential Revision: D13705538
Pulled By: maysamyabandeh
fbshipit-source-id: e577f0d9ee1ff5a6035f26859e56902ecc85a5a4
2019-01-19 01:20:13 +00:00
|
|
|
class WritePreparedRollbackPreReleaseCallback : public PreReleaseCallback {
|
|
|
|
public:
|
|
|
|
WritePreparedRollbackPreReleaseCallback(WritePreparedTxnDB* db,
|
2019-01-30 00:23:21 +00:00
|
|
|
DBImpl* db_impl,
|
|
|
|
SequenceNumber prep_seq,
|
|
|
|
SequenceNumber rollback_seq,
|
|
|
|
size_t prep_batch_cnt)
|
WritePrepared: fix two versions in compaction see different status for released snapshots (#4890)
Summary:
Fix how CompactionIterator::findEarliestVisibleSnapshots handles released snapshot. It fixing the two scenarios:
Scenario 1:
key1 has two values v1 and v2. There're two snapshots s1 and s2 taken after v1 and v2 are committed. Right after compaction output v2, s1 is released. Now findEarliestVisibleSnapshot may see s1 being released, and return the next snapshot, which is s2. That's larger than v2's earliest visible snapshot, which was s1.
The fix: the only place we check against last snapshot and current key snapshot is when we decide whether to compact out a value if it is hidden by a later value. In the check if we see current snapshot is even larger than last snapshot, we know last snapshot is released, and we are safe to compact out current key.
Scenario 2:
key1 has two values v1 and v2. there are two snapshots s1 and s2 taken after v1 and v2 are committed. During compaction before we process the key, s1 is released. When compaction process v2, snapshot checker may return kSnapshotReleased, and the earliest visible snapshot for v2 become s2. When compaction process v1, snapshot checker may return kIsInSnapshot (for WritePrepared transaction, it could be because v1 is still in commit cache). The result will become inconsistent here.
The fix: remember the set of released snapshots ever reported by snapshot checker, and ignore them when finding result for findEarliestVisibleSnapshot.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/4890
Differential Revision: D13705538
Pulled By: maysamyabandeh
fbshipit-source-id: e577f0d9ee1ff5a6035f26859e56902ecc85a5a4
2019-01-19 01:20:13 +00:00
|
|
|
: db_(db),
|
|
|
|
db_impl_(db_impl),
|
|
|
|
prep_seq_(prep_seq),
|
|
|
|
rollback_seq_(rollback_seq),
|
|
|
|
prep_batch_cnt_(prep_batch_cnt) {
|
|
|
|
assert(prep_seq != kMaxSequenceNumber);
|
|
|
|
assert(rollback_seq != kMaxSequenceNumber);
|
|
|
|
assert(prep_batch_cnt_ > 0);
|
|
|
|
}
|
|
|
|
|
2019-06-10 18:47:16 +00:00
|
|
|
Status Callback(SequenceNumber commit_seq, bool is_mem_disabled, uint64_t,
|
|
|
|
size_t /*index*/, size_t /*total*/) override {
|
2019-02-28 23:20:40 +00:00
|
|
|
// Always commit from the 2nd queue
|
WritePrepared: fix two versions in compaction see different status for released snapshots (#4890)
Summary:
Fix how CompactionIterator::findEarliestVisibleSnapshots handles released snapshot. It fixing the two scenarios:
Scenario 1:
key1 has two values v1 and v2. There're two snapshots s1 and s2 taken after v1 and v2 are committed. Right after compaction output v2, s1 is released. Now findEarliestVisibleSnapshot may see s1 being released, and return the next snapshot, which is s2. That's larger than v2's earliest visible snapshot, which was s1.
The fix: the only place we check against last snapshot and current key snapshot is when we decide whether to compact out a value if it is hidden by a later value. In the check if we see current snapshot is even larger than last snapshot, we know last snapshot is released, and we are safe to compact out current key.
Scenario 2:
key1 has two values v1 and v2. there are two snapshots s1 and s2 taken after v1 and v2 are committed. During compaction before we process the key, s1 is released. When compaction process v2, snapshot checker may return kSnapshotReleased, and the earliest visible snapshot for v2 become s2. When compaction process v1, snapshot checker may return kIsInSnapshot (for WritePrepared transaction, it could be because v1 is still in commit cache). The result will become inconsistent here.
The fix: remember the set of released snapshots ever reported by snapshot checker, and ignore them when finding result for findEarliestVisibleSnapshot.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/4890
Differential Revision: D13705538
Pulled By: maysamyabandeh
fbshipit-source-id: e577f0d9ee1ff5a6035f26859e56902ecc85a5a4
2019-01-19 01:20:13 +00:00
|
|
|
assert(is_mem_disabled); // implies the 2nd queue
|
2019-02-28 23:20:40 +00:00
|
|
|
assert(db_impl_->immutable_db_options().two_write_queues);
|
WritePrepared: fix two versions in compaction see different status for released snapshots (#4890)
Summary:
Fix how CompactionIterator::findEarliestVisibleSnapshots handles released snapshot. It fixing the two scenarios:
Scenario 1:
key1 has two values v1 and v2. There're two snapshots s1 and s2 taken after v1 and v2 are committed. Right after compaction output v2, s1 is released. Now findEarliestVisibleSnapshot may see s1 being released, and return the next snapshot, which is s2. That's larger than v2's earliest visible snapshot, which was s1.
The fix: the only place we check against last snapshot and current key snapshot is when we decide whether to compact out a value if it is hidden by a later value. In the check if we see current snapshot is even larger than last snapshot, we know last snapshot is released, and we are safe to compact out current key.
Scenario 2:
key1 has two values v1 and v2. there are two snapshots s1 and s2 taken after v1 and v2 are committed. During compaction before we process the key, s1 is released. When compaction process v2, snapshot checker may return kSnapshotReleased, and the earliest visible snapshot for v2 become s2. When compaction process v1, snapshot checker may return kIsInSnapshot (for WritePrepared transaction, it could be because v1 is still in commit cache). The result will become inconsistent here.
The fix: remember the set of released snapshots ever reported by snapshot checker, and ignore them when finding result for findEarliestVisibleSnapshot.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/4890
Differential Revision: D13705538
Pulled By: maysamyabandeh
fbshipit-source-id: e577f0d9ee1ff5a6035f26859e56902ecc85a5a4
2019-01-19 01:20:13 +00:00
|
|
|
#ifdef NDEBUG
|
|
|
|
(void)is_mem_disabled;
|
|
|
|
#endif
|
|
|
|
const uint64_t last_commit_seq = commit_seq;
|
|
|
|
db_->AddCommitted(rollback_seq_, last_commit_seq);
|
|
|
|
for (size_t i = 0; i < prep_batch_cnt_; i++) {
|
|
|
|
db_->AddCommitted(prep_seq_ + i, last_commit_seq);
|
|
|
|
}
|
|
|
|
db_impl_->SetLastPublishedSequence(last_commit_seq);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
WritePreparedTxnDB* db_;
|
|
|
|
DBImpl* db_impl_;
|
|
|
|
SequenceNumber prep_seq_;
|
|
|
|
SequenceNumber rollback_seq_;
|
|
|
|
size_t prep_batch_cnt_;
|
|
|
|
};
|
|
|
|
|
2018-02-13 00:27:39 +00:00
|
|
|
// Count the number of sub-batches inside a batch. A sub-batch does not have
|
|
|
|
// duplicate keys.
|
|
|
|
struct SubBatchCounter : public WriteBatch::Handler {
|
|
|
|
explicit SubBatchCounter(std::map<uint32_t, const Comparator*>& comparators)
|
|
|
|
: comparators_(comparators), batches_(1) {}
|
|
|
|
std::map<uint32_t, const Comparator*>& comparators_;
|
|
|
|
using CFKeys = std::set<Slice, SetComparator>;
|
|
|
|
std::map<uint32_t, CFKeys> keys_;
|
|
|
|
size_t batches_;
|
|
|
|
size_t BatchCount() { return batches_; }
|
|
|
|
void AddKey(const uint32_t cf, const Slice& key);
|
2018-03-05 18:48:29 +00:00
|
|
|
void InitWithComp(const uint32_t cf);
|
2018-02-13 00:27:39 +00:00
|
|
|
Status MarkNoop(bool) override { return Status::OK(); }
|
|
|
|
Status MarkEndPrepare(const Slice&) override { return Status::OK(); }
|
|
|
|
Status MarkCommit(const Slice&) override { return Status::OK(); }
|
|
|
|
Status PutCF(uint32_t cf, const Slice& key, const Slice&) override {
|
|
|
|
AddKey(cf, key);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
Status DeleteCF(uint32_t cf, const Slice& key) override {
|
|
|
|
AddKey(cf, key);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
Status SingleDeleteCF(uint32_t cf, const Slice& key) override {
|
|
|
|
AddKey(cf, key);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
Status MergeCF(uint32_t cf, const Slice& key, const Slice&) override {
|
|
|
|
AddKey(cf, key);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2018-07-07 00:17:36 +00:00
|
|
|
Status MarkBeginPrepare(bool) override { return Status::OK(); }
|
2018-02-13 00:27:39 +00:00
|
|
|
Status MarkRollback(const Slice&) override { return Status::OK(); }
|
2022-04-28 21:42:00 +00:00
|
|
|
Handler::OptionState WriteAfterCommit() const override {
|
|
|
|
return Handler::OptionState::kDisabled;
|
|
|
|
}
|
2018-02-13 00:27:39 +00:00
|
|
|
};
|
|
|
|
|
2019-08-05 20:30:56 +00:00
|
|
|
SnapshotBackup WritePreparedTxnDB::AssignMinMaxSeqs(const Snapshot* snapshot,
|
|
|
|
SequenceNumber* min,
|
|
|
|
SequenceNumber* max) {
|
2019-04-12 21:36:36 +00:00
|
|
|
if (snapshot != nullptr) {
|
2020-04-29 20:06:27 +00:00
|
|
|
*min =
|
|
|
|
static_cast_with_check<const SnapshotImpl>(snapshot)->min_uncommitted_;
|
|
|
|
*max = static_cast_with_check<const SnapshotImpl>(snapshot)->number_;
|
2021-07-30 19:06:47 +00:00
|
|
|
// A duplicate of the check in EnhanceSnapshot().
|
|
|
|
assert(*min <= *max + 1);
|
2019-08-05 20:30:56 +00:00
|
|
|
return kBackedByDBSnapshot;
|
2019-04-12 21:36:36 +00:00
|
|
|
} else {
|
|
|
|
*min = SmallestUnCommittedSeq();
|
|
|
|
*max = 0; // to be assigned later after sv is referenced.
|
2019-08-05 20:30:56 +00:00
|
|
|
return kUnbackedByDBSnapshot;
|
2019-04-12 21:36:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-05 20:30:56 +00:00
|
|
|
bool WritePreparedTxnDB::ValidateSnapshot(
|
|
|
|
const SequenceNumber snap_seq, const SnapshotBackup backed_by_snapshot,
|
|
|
|
std::memory_order order) {
|
|
|
|
if (backed_by_snapshot == kBackedByDBSnapshot) {
|
2019-04-12 21:36:36 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
SequenceNumber max = max_evicted_seq_.load(order);
|
|
|
|
// Validate that max has not advanced the snapshot seq that is not backed
|
|
|
|
// by a real snapshot. This is a very rare case that should not happen in
|
|
|
|
// real workloads.
|
|
|
|
if (UNLIKELY(snap_seq <= max && snap_seq != 0)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|