2017-08-03 17:46:47 +02:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
|
|
|
|
#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-08 01:07:40 +02:00
|
|
|
#include "utilities/transactions/pessimistic_transaction.h"
|
2017-08-17 01:49:11 +02:00
|
|
|
#include "utilities/transactions/pessimistic_transaction_db.h"
|
|
|
|
#include "utilities/transactions/transaction_base.h"
|
2017-08-03 17:46:47 +02:00
|
|
|
#include "utilities/transactions/transaction_util.h"
|
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
2017-08-17 01:49:11 +02:00
|
|
|
class WritePreparedTxnDB;
|
2017-08-03 17:46:47 +02:00
|
|
|
|
2018-03-08 19:18:34 +01: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-08 01:07:40 +02:00
|
|
|
// Prepare phase in 2PC (WritePreparedTxn) or before that
|
2017-08-03 17:46:47 +02:00
|
|
|
// (WriteUnpreparedTxnImpl).
|
2017-08-08 01:07:40 +02:00
|
|
|
class WritePreparedTxn : public PessimisticTransaction {
|
2017-08-03 17:46:47 +02:00
|
|
|
public:
|
2017-08-17 01:49:11 +02:00
|
|
|
WritePreparedTxn(WritePreparedTxnDB* db, const WriteOptions& write_options,
|
|
|
|
const TransactionOptions& txn_options);
|
2019-09-12 03:07:12 +02:00
|
|
|
// No copying allowed
|
|
|
|
WritePreparedTxn(const WritePreparedTxn&) = delete;
|
|
|
|
void operator=(const WritePreparedTxn&) = delete;
|
2017-08-03 17:46:47 +02:00
|
|
|
|
2017-08-08 01:07:40 +02:00
|
|
|
virtual ~WritePreparedTxn() {}
|
2017-08-03 17:46:47 +02:00
|
|
|
|
2017-10-23 23:20:22 +02:00
|
|
|
// To make WAL commit markers visible, the snapshot will be based on the last
|
2017-12-01 08:39:56 +01:00
|
|
|
// seq in the WAL that is also published, LastPublishedSequence, as opposed to
|
|
|
|
// the last seq in the memtable.
|
2017-09-11 17:58:52 +02:00
|
|
|
using Transaction::Get;
|
|
|
|
virtual Status Get(const ReadOptions& options,
|
|
|
|
ColumnFamilyHandle* column_family, const Slice& key,
|
|
|
|
PinnableSlice* value) override;
|
|
|
|
|
2019-07-30 02:51:30 +02: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 01:55:46 +01:00
|
|
|
const bool sorted_input = false) override;
|
2019-07-30 02:51:30 +02:00
|
|
|
|
2019-04-02 23:43:03 +02: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 02:05:34 +02:00
|
|
|
using Transaction::GetIterator;
|
|
|
|
virtual Iterator* GetIterator(const ReadOptions& options) override;
|
|
|
|
virtual Iterator* GetIterator(const ReadOptions& options,
|
|
|
|
ColumnFamilyHandle* column_family) override;
|
|
|
|
|
2018-04-03 05:19:21 +02:00
|
|
|
virtual void SetSnapshot() override;
|
|
|
|
|
2018-02-13 01:27:39 +01:00
|
|
|
protected:
|
2018-07-24 09:09:18 +02:00
|
|
|
void Initialize(const TransactionOptions& txn_options) override;
|
2018-02-16 17:36:47 +01:00
|
|
|
// Override the protected SetId to make it visible to the friend class
|
2018-02-13 01:27:39 +01:00
|
|
|
// WritePreparedTxnDB
|
|
|
|
inline void SetId(uint64_t id) override { Transaction::SetId(id); }
|
|
|
|
|
2017-08-03 17:46:47 +02:00
|
|
|
private:
|
2017-09-29 01:43:04 +02:00
|
|
|
friend class WritePreparedTransactionTest_BasicRecoveryTest_Test;
|
2018-02-13 01:27:39 +01:00
|
|
|
friend class WritePreparedTxnDB;
|
2018-07-07 02:17:36 +02:00
|
|
|
friend class WriteUnpreparedTxnDB;
|
2018-07-24 09:09:18 +02:00
|
|
|
friend class WriteUnpreparedTxn;
|
2017-09-29 01:43:04 +02:00
|
|
|
|
2017-08-08 01:07:40 +02:00
|
|
|
Status PrepareInternal() override;
|
|
|
|
|
|
|
|
Status CommitWithoutPrepareInternal() override;
|
|
|
|
|
2018-02-06 03:32:54 +01:00
|
|
|
Status CommitBatchInternal(WriteBatch* batch, size_t batch_cnt) override;
|
2017-09-09 00:53:51 +02:00
|
|
|
|
2017-10-23 23:20:22 +02: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 08:39:56 +01: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-08 01:07:40 +02:00
|
|
|
Status CommitInternal() override;
|
|
|
|
|
2017-10-03 04:46:42 +02:00
|
|
|
Status RollbackInternal() override;
|
|
|
|
|
2017-11-02 02:56:25 +01:00
|
|
|
virtual Status ValidateSnapshot(ColumnFamilyHandle* column_family,
|
2017-11-11 22:08:22 +01:00
|
|
|
const Slice& key,
|
|
|
|
SequenceNumber* tracked_at_seq) override;
|
2017-08-03 17:46:47 +02:00
|
|
|
|
2018-02-06 03:32:54 +01:00
|
|
|
virtual Status RebuildFromWriteBatch(WriteBatch* src_batch) override;
|
|
|
|
|
2017-08-17 01:49:11 +02:00
|
|
|
WritePreparedTxnDB* wpt_db_;
|
2018-02-06 03:32:54 +01:00
|
|
|
// Number of sub-batches in prepare
|
|
|
|
size_t prepare_batch_cnt_ = 0;
|
2017-08-03 17:46:47 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace rocksdb
|
|
|
|
|
|
|
|
#endif // ROCKSDB_LITE
|