2016-02-10 00:12:00 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-16 01:03:42 +02:00
|
|
|
// 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).
|
2013-10-16 23:59:46 +02:00
|
|
|
//
|
2011-03-18 23:37:00 +01:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
2013-10-05 07:32:05 +02:00
|
|
|
#pragma once
|
2015-05-06 04:01:12 +02:00
|
|
|
#include <vector>
|
|
|
|
|
2021-09-10 18:59:05 +02:00
|
|
|
#include "db/dbformat.h"
|
2013-08-23 17:38:13 +02:00
|
|
|
#include "rocksdb/db.h"
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
class SnapshotList;
|
|
|
|
|
|
|
|
// Snapshots are kept in a doubly-linked list in the DB.
|
2011-05-21 04:17:43 +02:00
|
|
|
// Each SnapshotImpl corresponds to a particular sequence number.
|
|
|
|
class SnapshotImpl : public Snapshot {
|
2011-03-18 23:37:00 +01:00
|
|
|
public:
|
|
|
|
SequenceNumber number_; // const after creation
|
2018-04-03 05:19:21 +02:00
|
|
|
// It indicates the smallest uncommitted data at the time the snapshot was
|
|
|
|
// taken. This is currently used by WritePrepared transactions to limit the
|
2021-03-26 05:17:17 +01:00
|
|
|
// scope of queries to IsInSnapshot.
|
2019-04-02 23:43:03 +02:00
|
|
|
SequenceNumber min_uncommitted_ = kMinUnCommittedSeq;
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2015-02-26 20:28:41 +01:00
|
|
|
virtual SequenceNumber GetSequenceNumber() const override { return number_; }
|
2015-01-30 03:22:43 +01:00
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
private:
|
|
|
|
friend class SnapshotList;
|
|
|
|
|
2011-05-21 04:17:43 +02:00
|
|
|
// SnapshotImpl is kept in a doubly-linked circular list
|
|
|
|
SnapshotImpl* prev_;
|
|
|
|
SnapshotImpl* next_;
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
SnapshotList* list_; // just for sanity checks
|
2014-12-06 01:12:10 +01:00
|
|
|
|
|
|
|
int64_t unix_time_;
|
2015-12-08 21:25:48 +01:00
|
|
|
|
|
|
|
// Will this snapshot be used by a Transaction to do write-conflict checking?
|
|
|
|
bool is_write_conflict_boundary_;
|
2011-03-18 23:37:00 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class SnapshotList {
|
|
|
|
public:
|
|
|
|
SnapshotList() {
|
|
|
|
list_.prev_ = &list_;
|
|
|
|
list_.next_ = &list_;
|
2012-11-27 06:16:21 +01:00
|
|
|
list_.number_ = 0xFFFFFFFFL; // placeholder marker, for debugging
|
2018-02-07 23:28:50 +01:00
|
|
|
// Set all the variables to make UBSAN happy.
|
|
|
|
list_.list_ = nullptr;
|
|
|
|
list_.unix_time_ = 0;
|
|
|
|
list_.is_write_conflict_boundary_ = false;
|
2014-12-06 01:12:10 +01:00
|
|
|
count_ = 0;
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
|
2018-08-07 01:45:02 +02:00
|
|
|
// No copy-construct.
|
|
|
|
SnapshotList(const SnapshotList&) = delete;
|
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
bool empty() const { return list_.next_ == &list_; }
|
2011-05-21 04:17:43 +02:00
|
|
|
SnapshotImpl* oldest() const { assert(!empty()); return list_.next_; }
|
|
|
|
SnapshotImpl* newest() const { assert(!empty()); return list_.prev_; }
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2018-04-03 05:19:21 +02:00
|
|
|
SnapshotImpl* New(SnapshotImpl* s, SequenceNumber seq, uint64_t unix_time,
|
|
|
|
bool is_write_conflict_boundary) {
|
2011-03-18 23:37:00 +01:00
|
|
|
s->number_ = seq;
|
2014-12-06 01:12:10 +01:00
|
|
|
s->unix_time_ = unix_time;
|
2015-12-08 21:25:48 +01:00
|
|
|
s->is_write_conflict_boundary_ = is_write_conflict_boundary;
|
2011-03-18 23:37:00 +01:00
|
|
|
s->list_ = this;
|
|
|
|
s->next_ = &list_;
|
|
|
|
s->prev_ = list_.prev_;
|
|
|
|
s->prev_->next_ = s;
|
|
|
|
s->next_->prev_ = s;
|
2014-12-06 01:12:10 +01:00
|
|
|
count_++;
|
2011-03-18 23:37:00 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2015-06-09 01:57:44 +02:00
|
|
|
// Do not responsible to free the object.
|
2011-05-21 04:17:43 +02:00
|
|
|
void Delete(const SnapshotImpl* s) {
|
2011-03-18 23:37:00 +01:00
|
|
|
assert(s->list_ == this);
|
|
|
|
s->prev_->next_ = s->next_;
|
|
|
|
s->next_->prev_ = s->prev_;
|
2014-12-06 01:12:10 +01:00
|
|
|
count_--;
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
|
2017-08-26 09:53:13 +02:00
|
|
|
// retrieve all snapshot numbers up until max_seq. They are sorted in
|
2019-01-10 01:09:36 +01:00
|
|
|
// ascending order (with no duplicates).
|
2015-12-08 21:25:48 +01:00
|
|
|
std::vector<SequenceNumber> GetAll(
|
2017-08-26 09:53:13 +02:00
|
|
|
SequenceNumber* oldest_write_conflict_snapshot = nullptr,
|
|
|
|
const SequenceNumber& max_seq = kMaxSequenceNumber) const {
|
2015-05-06 04:01:12 +02:00
|
|
|
std::vector<SequenceNumber> ret;
|
2019-05-04 02:26:20 +02:00
|
|
|
GetAll(&ret, oldest_write_conflict_snapshot, max_seq);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetAll(std::vector<SequenceNumber>* snap_vector,
|
|
|
|
SequenceNumber* oldest_write_conflict_snapshot = nullptr,
|
|
|
|
const SequenceNumber& max_seq = kMaxSequenceNumber) const {
|
|
|
|
std::vector<SequenceNumber>& ret = *snap_vector;
|
|
|
|
// So far we have no use case that would pass a non-empty vector
|
|
|
|
assert(ret.size() == 0);
|
2015-12-08 21:25:48 +01:00
|
|
|
|
|
|
|
if (oldest_write_conflict_snapshot != nullptr) {
|
|
|
|
*oldest_write_conflict_snapshot = kMaxSequenceNumber;
|
|
|
|
}
|
|
|
|
|
2015-05-06 04:01:12 +02:00
|
|
|
if (empty()) {
|
2019-05-04 02:26:20 +02:00
|
|
|
return;
|
2015-05-06 04:01:12 +02:00
|
|
|
}
|
2017-08-17 01:49:11 +02:00
|
|
|
const SnapshotImpl* s = &list_;
|
2012-11-27 06:16:21 +01:00
|
|
|
while (s->next_ != &list_) {
|
2017-08-26 09:53:13 +02:00
|
|
|
if (s->next_->number_ > max_seq) {
|
|
|
|
break;
|
|
|
|
}
|
2019-01-10 01:09:36 +01:00
|
|
|
// Avoid duplicates
|
|
|
|
if (ret.empty() || ret.back() != s->next_->number_) {
|
|
|
|
ret.push_back(s->next_->number_);
|
|
|
|
}
|
2015-12-08 21:25:48 +01:00
|
|
|
|
|
|
|
if (oldest_write_conflict_snapshot != nullptr &&
|
Use SST files for Transaction conflict detection
Summary:
Currently, transactions can fail even if there is no actual write conflict. This is due to relying on only the memtables to check for write-conflicts. Users have to tune memtable settings to try to avoid this, but it's hard to figure out exactly how to tune these settings.
With this diff, TransactionDB will use both memtables and SST files to determine if there are any write conflicts. This relies on the fact that BlockBasedTable stores sequence numbers for all writes that happen after any open snapshot. Also, D50295 is needed to prevent SingleDelete from disappearing writes (the TODOs in this test code will be fixed once the other diff is approved and merged).
Note that Optimistic transactions will still rely on tuning memtable settings as we do not want to read from SST while on the write thread. Also, memtable settings can still be used to reduce how often TransactionDB needs to read SST files.
Test Plan: unit tests, db bench
Reviewers: rven, yhchiang, kradhakrishnan, IslamAbdelRahman, sdong
Reviewed By: sdong
Subscribers: dhruba, leveldb, yoshinorim
Differential Revision: https://reviews.facebook.net/D50475
2015-10-16 01:37:15 +02:00
|
|
|
*oldest_write_conflict_snapshot == kMaxSequenceNumber &&
|
2015-12-08 21:25:48 +01:00
|
|
|
s->next_->is_write_conflict_boundary_) {
|
|
|
|
// If this is the first write-conflict boundary snapshot in the list,
|
|
|
|
// it is the oldest
|
|
|
|
*oldest_write_conflict_snapshot = s->next_->number_;
|
|
|
|
}
|
|
|
|
|
2015-05-06 04:01:12 +02:00
|
|
|
s = s->next_;
|
2012-11-27 06:16:21 +01:00
|
|
|
}
|
2019-05-04 02:26:20 +02:00
|
|
|
return;
|
2012-11-27 06:16:21 +01:00
|
|
|
}
|
|
|
|
|
2013-02-28 23:09:30 +01:00
|
|
|
// get the sequence number of the most recent snapshot
|
2014-09-04 16:04:37 +02:00
|
|
|
SequenceNumber GetNewest() {
|
2013-02-28 23:09:30 +01:00
|
|
|
if (empty()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return newest()->number_;
|
|
|
|
}
|
|
|
|
|
2014-12-06 01:12:10 +01:00
|
|
|
int64_t GetOldestSnapshotTime() const {
|
|
|
|
if (empty()) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return oldest()->unix_time_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-07 17:35:12 +01:00
|
|
|
int64_t GetOldestSnapshotSequence() const {
|
|
|
|
if (empty()) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return oldest()->GetSequenceNumber();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-06 01:12:10 +01:00
|
|
|
uint64_t count() const { return count_; }
|
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
private:
|
|
|
|
// Dummy head of doubly-linked list of snapshots
|
2011-05-21 04:17:43 +02:00
|
|
|
SnapshotImpl list_;
|
2014-12-06 01:12:10 +01:00
|
|
|
uint64_t count_;
|
2011-03-18 23:37:00 +01:00
|
|
|
};
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|