2017-08-03 15:46:47 +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
|
|
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <atomic>
|
|
|
|
#include <mutex>
|
|
|
|
#include <stack>
|
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "db/write_callback.h"
|
|
|
|
#include "rocksdb/db.h"
|
|
|
|
#include "rocksdb/slice.h"
|
|
|
|
#include "rocksdb/snapshot.h"
|
|
|
|
#include "rocksdb/status.h"
|
|
|
|
#include "rocksdb/types.h"
|
|
|
|
#include "rocksdb/utilities/transaction.h"
|
|
|
|
#include "rocksdb/utilities/transaction_db.h"
|
|
|
|
#include "rocksdb/utilities/write_batch_with_index.h"
|
|
|
|
#include "util/autovector.h"
|
2017-08-07 23:07:40 +00:00
|
|
|
#include "utilities/transactions/pessimistic_transaction.h"
|
2017-08-16 23:49:11 +00:00
|
|
|
#include "utilities/transactions/pessimistic_transaction_db.h"
|
|
|
|
#include "utilities/transactions/transaction_base.h"
|
2017-08-03 15:46:47 +00:00
|
|
|
#include "utilities/transactions/transaction_util.h"
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2017-08-03 15:46:47 +00:00
|
|
|
|
2017-08-16 23:49:11 +00:00
|
|
|
class WritePreparedTxnDB;
|
2017-08-03 15:46:47 +00:00
|
|
|
|
2018-03-08 18:18:34 +00:00
|
|
|
// This impl could write to DB also uncommitted data and then later tell apart
|
|
|
|
// committed data from uncommitted data. Uncommitted data could be after the
|
2017-08-07 23:07:40 +00:00
|
|
|
// Prepare phase in 2PC (WritePreparedTxn) or before that
|
2017-08-03 15:46:47 +00:00
|
|
|
// (WriteUnpreparedTxnImpl).
|
2017-08-07 23:07:40 +00:00
|
|
|
class WritePreparedTxn : public PessimisticTransaction {
|
2017-08-03 15:46:47 +00:00
|
|
|
public:
|
2017-08-16 23:49:11 +00:00
|
|
|
WritePreparedTxn(WritePreparedTxnDB* db, const WriteOptions& write_options,
|
|
|
|
const TransactionOptions& txn_options);
|
2019-09-12 01:07:12 +00:00
|
|
|
// No copying allowed
|
|
|
|
WritePreparedTxn(const WritePreparedTxn&) = delete;
|
|
|
|
void operator=(const WritePreparedTxn&) = delete;
|
2017-08-03 15:46:47 +00:00
|
|
|
|
2017-08-07 23:07:40 +00:00
|
|
|
virtual ~WritePreparedTxn() {}
|
2017-08-03 15:46:47 +00:00
|
|
|
|
2017-10-23 21:20:22 +00:00
|
|
|
// To make WAL commit markers visible, the snapshot will be based on the last
|
2017-12-01 07:39:56 +00:00
|
|
|
// seq in the WAL that is also published, LastPublishedSequence, as opposed to
|
|
|
|
// the last seq in the memtable.
|
2017-09-11 15:58:52 +00:00
|
|
|
using Transaction::Get;
|
|
|
|
virtual Status Get(const ReadOptions& options,
|
|
|
|
ColumnFamilyHandle* column_family, const Slice& key,
|
|
|
|
PinnableSlice* value) override;
|
|
|
|
|
2019-07-30 00:51:30 +00:00
|
|
|
using Transaction::MultiGet;
|
|
|
|
virtual void MultiGet(const ReadOptions& options,
|
|
|
|
ColumnFamilyHandle* column_family,
|
|
|
|
const size_t num_keys, const Slice* keys,
|
|
|
|
PinnableSlice* values, Status* statuses,
|
2019-11-27 00:55:46 +00:00
|
|
|
const bool sorted_input = false) override;
|
2019-07-30 00:51:30 +00:00
|
|
|
|
2019-04-02 21:43:03 +00:00
|
|
|
// Note: The behavior is undefined in presence of interleaved writes to the
|
|
|
|
// same transaction.
|
|
|
|
// To make WAL commit markers visible, the snapshot will be
|
|
|
|
// based on the last seq in the WAL that is also published,
|
|
|
|
// LastPublishedSequence, as opposed to the last seq in the memtable.
|
2017-10-10 00:05:34 +00:00
|
|
|
using Transaction::GetIterator;
|
|
|
|
virtual Iterator* GetIterator(const ReadOptions& options) override;
|
|
|
|
virtual Iterator* GetIterator(const ReadOptions& options,
|
|
|
|
ColumnFamilyHandle* column_family) override;
|
|
|
|
|
2018-04-03 03:19:21 +00:00
|
|
|
virtual void SetSnapshot() override;
|
|
|
|
|
2018-02-13 00:27:39 +00:00
|
|
|
protected:
|
2018-07-24 07:09:18 +00:00
|
|
|
void Initialize(const TransactionOptions& txn_options) override;
|
2018-02-16 16:36:47 +00:00
|
|
|
// Override the protected SetId to make it visible to the friend class
|
2018-02-13 00:27:39 +00:00
|
|
|
// WritePreparedTxnDB
|
|
|
|
inline void SetId(uint64_t id) override { Transaction::SetId(id); }
|
|
|
|
|
2017-08-03 15:46:47 +00:00
|
|
|
private:
|
2017-09-28 23:43:04 +00:00
|
|
|
friend class WritePreparedTransactionTest_BasicRecoveryTest_Test;
|
2018-02-13 00:27:39 +00:00
|
|
|
friend class WritePreparedTxnDB;
|
2018-07-07 00:17:36 +00:00
|
|
|
friend class WriteUnpreparedTxnDB;
|
2018-07-24 07:09:18 +00:00
|
|
|
friend class WriteUnpreparedTxn;
|
2017-09-28 23:43:04 +00:00
|
|
|
|
2017-08-07 23:07:40 +00:00
|
|
|
Status PrepareInternal() override;
|
|
|
|
|
|
|
|
Status CommitWithoutPrepareInternal() override;
|
|
|
|
|
2018-02-06 02:32:54 +00:00
|
|
|
Status CommitBatchInternal(WriteBatch* batch, size_t batch_cnt) override;
|
2017-09-08 22:53:51 +00:00
|
|
|
|
2017-10-23 21:20:22 +00:00
|
|
|
// Since the data is already written to memtables at the Prepare phase, the
|
|
|
|
// commit entails writing only a commit marker in the WAL. The sequence number
|
|
|
|
// of the commit marker is then the commit timestamp of the transaction. To
|
2017-12-01 07:39:56 +00:00
|
|
|
// make WAL commit markers visible, the snapshot will be based on the last seq
|
|
|
|
// in the WAL that is also published, LastPublishedSequence, as opposed to the
|
|
|
|
// last seq in the memtable.
|
2017-08-07 23:07:40 +00:00
|
|
|
Status CommitInternal() override;
|
|
|
|
|
2017-10-03 02:46:42 +00:00
|
|
|
Status RollbackInternal() override;
|
|
|
|
|
2017-11-02 01:56:25 +00:00
|
|
|
virtual Status ValidateSnapshot(ColumnFamilyHandle* column_family,
|
2017-11-11 21:08:22 +00:00
|
|
|
const Slice& key,
|
|
|
|
SequenceNumber* tracked_at_seq) override;
|
2017-08-03 15:46:47 +00:00
|
|
|
|
2018-02-06 02:32:54 +00:00
|
|
|
virtual Status RebuildFromWriteBatch(WriteBatch* src_batch) override;
|
|
|
|
|
2017-08-16 23:49:11 +00:00
|
|
|
WritePreparedTxnDB* wpt_db_;
|
2018-02-06 02:32:54 +00:00
|
|
|
// Number of sub-batches in prepare
|
|
|
|
size_t prepare_batch_cnt_ = 0;
|
2017-08-03 15:46:47 +00:00
|
|
|
};
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2017-08-03 15:46:47 +00:00
|
|
|
|