mirror of
https://github.com/facebook/rocksdb.git
synced 2026-10-23 08:09:02 +03:00
Summary: Pull Request resolved: https://github.com/facebook/rocksdb/pull/15387 ## Summary RocksDB's `.clang-tidy` enables `cppcoreguidelines-special-member-functions`, `cppcoreguidelines-virtual-class-destructor` and the noexcept-move checks, and clang warns `-Wdeprecated-copy-with-user-provided-dtor`. A full scan reports 476 findings. In most of them, a class declares a destructor (or copy operations) but leaves the rest of copy/move implicit. That has two costs. Readers can't tell whether a class may be copied or moved. And resource-owning classes keep an implicit copy that would double-free if anyone ever used it. This diff resolves 453 of the 476. Approach, picked per class from least to most invasive: 1. An empty user-provided destructor becomes `= default`. This covers most interface classes, such as `virtual ~X() {}` -> `= default`. If the class is still copied, that would only rename the deprecated-copy warning, so the destructor is removed instead when nothing needs it (`Customizable`, `TableFactory` and `InternalKeyComparator` inherit a virtual one; `IteratorWrapperBase` isn't polymorphic), and `MemTableRep::KeyComparator` gets defaulted copy and move. 2. Classes whose copy is already deleted get explicitly deleted move operations too. Move was already unavailable, so this only documents it. 3. Resource-owning classes that are never copied get deleted copy and move operations. Examples: the C API wrapper structs, which run a `destructor_` callback (edited in `tools/c_api_gen/c_base.cc` and regenerated into `db/c.cc`); `ShardedCache`, which frees its shards; iterator wrappers; and RAII handles. 4. Value types that are copied or moved get the missing operations as `= default`, which keeps their current behavior. 5. Exception specifications of existing functions are unchanged, except for `WriteBatch`'s move assignment (below): no other `noexcept` is added or removed. Only the move operations this diff newly declares (`Comparator`, `FileSampledStats` and the thread-local `Entry`) are `noexcept`; they copy only scalars or atomics. `WriteBatch`'s copy and move assignment destroyed `*this` and placement-new'ed a new `WriteBatch` over it. That is undefined behavior when `*this` is a subclass, and if the copy constructor threw, `*this` was left destroyed and was later destroyed again. Move assignment now assigns member-wise and ends in the same state as the move constructor, which leaves `has_key_with_ts_`, the timestamp-size tracking and the other fields it doesn't copy at their defaults. `rep_` is swapped through a temporary, because a plain string move-assignment can keep the old buffer (e.g. a large batch reset with `batch = WriteBatch()` would keep its memory). Nothing in it can throw, so it is now `noexcept`. Copy assignment copies into a temporary and moves it in, so a throwing copy leaves `*this` unchanged. Bugs this exposed and fixed: - `RestoreAfterCopyOrCreateWorkItem` (backup engine): the hand-written move constructor and assignment moved only some fields, so `from_file` and `to_file` were empty after `push_back(std::move(...))`. A restore that hit a checksum mismatch then reported "While restoring -> : ..." without file names. It now uses the compiler-generated moves, which are still `noexcept`. - `ExternalSstFileIngestionJob` lived in a `std::vector`, and its destructor frees raw-owned pointers, so the implicit copy used on vector reallocation would double-free. Its file batches also point into its own inline file storage, so neither a copy nor a move is safe after `Prepare()`. Copy and move are now deleted, and `DBImpl` holds jobs through `std::unique_ptr`. Compatibility: no public class loses an operation users can call. Public interfaces mostly only get `virtual ~X() = default;`. `WriteBatch`'s move assignment becomes `noexcept`, which is not part of the mangled name, so the ABI doesn't change. `SubcompactionJobInfo` and `CompactionJobInfo` intentionally stay C++20 aggregates, so users' aggregate and designated initializers keep compiling. `Configurable`, `CompareInterface`, `WriteBatchBase`, `ObjectLibrary::Entry` and `MemTableRep::KeyComparator` declare defaulted copy and move, and `Customizable` no longer declares a destructor. As a result, `Configurable` and `Customizable` subclasses that declare no destructor or copy operations of their own now move their registered-options list instead of copying it. In-tree, these are the mutable-options adapters, the table factories, the memory allocators and test classes; all are held through smart pointers or used in place, and `Clone()` copies, so none is moved by value. Not changed (23): `IODebugContext` (custom deep copy plus a `std::shared_mutex`), `DBWithColumnFamilies` in db_bench (stored in a `std::vector` with non-movable members), `Snapshot`'s protected virtual destructor (intended), `ManagedSnapshot`, the `SubcompactionJobInfo` and `CompactionJobInfo` aggregates, and the 16 noexcept-move-constructor and noexcept-swap findings on existing functions, including `autovector`'s move assignment (adding `noexcept` would change their exception specifications). Copying `ManagedSnapshot` already double-releases the snapshot. Fixing that needs a real move implementation and is a behavior change, so it is left for a separate diff. Reviewed By: rban1 Differential Revision: D123186309 fbshipit-source-id: 6f8e57e233e12199a007407130bb9a1f6d52e8d1
382 lines
14 KiB
C++
382 lines
14 KiB
C++
// Copyright (c) 2021-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).
|
|
|
|
#ifdef GFLAGS
|
|
|
|
#pragma once
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <atomic>
|
|
#include <memory>
|
|
|
|
#include "db/dbformat.h"
|
|
#include "db_stress_tool/expected_value.h"
|
|
#include "file/file_util.h"
|
|
#include "rocksdb/db.h"
|
|
#include "rocksdb/env.h"
|
|
#include "rocksdb/file_system.h"
|
|
#include "rocksdb/rocksdb_namespace.h"
|
|
#include "rocksdb/types.h"
|
|
#include "util/string_util.h"
|
|
|
|
namespace ROCKSDB_NAMESPACE {
|
|
// `ExpectedState` provides read/write access to expected values stored in
|
|
// `ExpectedState` for every key.
|
|
class ExpectedState {
|
|
public:
|
|
explicit ExpectedState(size_t max_key, size_t num_column_families);
|
|
|
|
virtual ~ExpectedState() = default;
|
|
|
|
// Requires external locking preventing concurrent execution with any other
|
|
// member function.
|
|
virtual Status Open(bool create) = 0;
|
|
|
|
// Requires external locking covering all keys in `cf`.
|
|
void ClearColumnFamily(int cf);
|
|
|
|
// Requires external locking
|
|
void SetPersistedSeqno(SequenceNumber seqno) {
|
|
persisted_seqno_->store(
|
|
std::max(persisted_seqno_->load(std::memory_order_relaxed), seqno),
|
|
std::memory_order_relaxed);
|
|
}
|
|
|
|
// Requires external locking
|
|
SequenceNumber GetPersistedSeqno() {
|
|
return persisted_seqno_->load(std::memory_order_relaxed);
|
|
}
|
|
|
|
// Prepare a Put that will be started but not finished yet
|
|
// This is useful for crash-recovery testing when the process may crash
|
|
// before updating the corresponding expected value
|
|
//
|
|
// Requires external locking covering `key` in `cf` to prevent concurrent
|
|
// write or delete to the same `key`.
|
|
PendingExpectedValue PreparePut(int cf, int64_t key);
|
|
|
|
// Does not requires external locking.
|
|
ExpectedValue Get(int cf, int64_t key);
|
|
|
|
// Prepare a Delete that will be started but not finished yet.
|
|
// This is useful for crash-recovery testing when the process may crash
|
|
// before updating the corresponding expected value
|
|
//
|
|
// Requires external locking covering `key` in `cf` to prevent concurrent
|
|
// write or delete to the same `key`.
|
|
PendingExpectedValue PrepareDelete(int cf, int64_t key);
|
|
|
|
// Requires external locking covering `key` in `cf` to prevent concurrent
|
|
// write or delete to the same `key`.
|
|
PendingExpectedValue PrepareSingleDelete(int cf, int64_t key);
|
|
|
|
// Requires external locking covering keys in `[begin_key, end_key)` in `cf`
|
|
// to prevent concurrent write or delete to the same `key`.
|
|
std::vector<PendingExpectedValue> PrepareDeleteRange(int cf,
|
|
int64_t begin_key,
|
|
int64_t end_key);
|
|
|
|
// Update the expected value for start of an incomplete write or delete
|
|
// operation on the key assoicated with this expected value
|
|
void Precommit(int cf, int64_t key, const ExpectedValue& value);
|
|
|
|
// Requires external locking covering `key` in `cf` to prevent concurrent
|
|
// delete to the same `key`.
|
|
bool Exists(int cf, int64_t key);
|
|
|
|
// Sync the `value_base` to the corresponding expected value
|
|
//
|
|
// Requires external locking covering `key` in `cf` or be in single thread
|
|
// to prevent concurrent write or delete to the same `key`
|
|
void SyncPut(int cf, int64_t key, uint32_t value_base);
|
|
|
|
// Sync the corresponding expected value to be pending Put
|
|
//
|
|
// Requires external locking covering `key` in `cf` or be in single thread
|
|
// to prevent concurrent write or delete to the same `key`
|
|
void SyncPendingPut(int cf, int64_t key);
|
|
|
|
// Sync the corresponding expected value to be deleted
|
|
//
|
|
// Requires external locking covering `key` in `cf` or be in single thread
|
|
// to prevent concurrent write or delete to the same `key`
|
|
void SyncDelete(int cf, int64_t key);
|
|
|
|
// Sync the corresponding expected values to be deleted
|
|
//
|
|
// Requires external locking covering keys in `[begin_key, end_key)` in `cf`
|
|
// to prevent concurrent write or delete to the same `key`
|
|
void SyncDeleteRange(int cf, int64_t begin_key, int64_t end_key);
|
|
|
|
private:
|
|
// Does not requires external locking.
|
|
std::atomic<uint32_t>& Value(int cf, int64_t key) const {
|
|
return values_[cf * max_key_ + key];
|
|
}
|
|
|
|
// Does not requires external locking
|
|
ExpectedValue Load(int cf, int64_t key) const {
|
|
return ExpectedValue(Value(cf, key).load());
|
|
}
|
|
|
|
const size_t max_key_;
|
|
const size_t num_column_families_;
|
|
|
|
protected:
|
|
size_t GetValuesLen() const {
|
|
return sizeof(std::atomic<uint32_t>) * num_column_families_ * max_key_;
|
|
}
|
|
|
|
// Requires external locking preventing concurrent execution with any other
|
|
// member function.
|
|
void Reset();
|
|
|
|
std::atomic<uint32_t>* values_;
|
|
std::atomic<SequenceNumber>* persisted_seqno_;
|
|
};
|
|
|
|
// A `FileExpectedState` implements `ExpectedState` backed by a file.
|
|
class FileExpectedState : public ExpectedState {
|
|
public:
|
|
explicit FileExpectedState(
|
|
const std::string& expected_state_file_path,
|
|
const std::string& expected_persisted_seqno_file_path, size_t max_key,
|
|
size_t num_column_families);
|
|
|
|
// Requires external locking preventing concurrent execution with any other
|
|
// member function.
|
|
Status Open(bool create) override;
|
|
|
|
private:
|
|
static Status CreateFile(Env* env, const EnvOptions& options,
|
|
const std::string& file_path,
|
|
const std::string& content) {
|
|
std::unique_ptr<WritableFile> wfile;
|
|
Status status = env->NewWritableFile(file_path, &wfile, options);
|
|
if (status.ok()) {
|
|
status = wfile->Append(content);
|
|
}
|
|
return status;
|
|
}
|
|
|
|
static Status MemoryMappedFile(
|
|
Env* env, const std::string& file_path,
|
|
std::unique_ptr<MemoryMappedFileBuffer>& memory_mapped_file_buffer,
|
|
std::size_t size) {
|
|
Status status =
|
|
env->NewMemoryMappedFileBuffer(file_path, &memory_mapped_file_buffer);
|
|
if (status.ok()) {
|
|
assert(memory_mapped_file_buffer->GetLen() == size);
|
|
}
|
|
(void)size;
|
|
return status;
|
|
}
|
|
|
|
const std::string expected_state_file_path_;
|
|
const std::string expected_persisted_seqno_file_path_;
|
|
std::unique_ptr<MemoryMappedFileBuffer> expected_state_mmap_buffer_;
|
|
std::unique_ptr<MemoryMappedFileBuffer> expected_persisted_seqno_mmap_buffer_;
|
|
};
|
|
|
|
// An `AnonExpectedState` implements `ExpectedState` backed by a memory
|
|
// allocation.
|
|
class AnonExpectedState : public ExpectedState {
|
|
public:
|
|
explicit AnonExpectedState(size_t max_key, size_t num_column_families);
|
|
|
|
// Requires external locking preventing concurrent execution with any other
|
|
// member function.
|
|
Status Open(bool create) override;
|
|
|
|
private:
|
|
std::unique_ptr<std::atomic<uint32_t>[]> values_allocation_;
|
|
std::unique_ptr<std::atomic<SequenceNumber>> persisted_seqno_allocation_;
|
|
};
|
|
|
|
// An `ExpectedStateManager` manages data about the expected state of the
|
|
// database. It exposes operations for reading and modifying the latest
|
|
// expected state.
|
|
class ExpectedStateManager {
|
|
public:
|
|
explicit ExpectedStateManager(size_t max_key, size_t num_column_families);
|
|
|
|
virtual ~ExpectedStateManager();
|
|
|
|
// Requires external locking preventing concurrent execution with any other
|
|
// member function.
|
|
virtual Status Open() = 0;
|
|
|
|
// Saves expected values for the current state of `db` and begins tracking
|
|
// changes. Following a successful `SaveAtAndAfter()`, `Restore()` can be
|
|
// called on the same DB, as long as its state does not roll back to before
|
|
// its current state.
|
|
//
|
|
// Requires external locking preventing concurrent execution with any other
|
|
// member function. Furthermore, `db` must not be mutated while this function
|
|
// is executing.
|
|
virtual Status SaveAtAndAfter(DB* db) = 0;
|
|
|
|
// Returns true if at least one state of historical expected values can be
|
|
// restored.
|
|
//
|
|
// Requires external locking preventing concurrent execution with any other
|
|
// member function.
|
|
virtual bool HasHistory() = 0;
|
|
|
|
// Restores expected values according to the current state of `db`. See
|
|
// `SaveAtAndAfter()` for conditions where this can be called.
|
|
//
|
|
// Requires external locking preventing concurrent execution with any other
|
|
// member function. Furthermore, `db` must not be mutated while this function
|
|
// is executing.
|
|
virtual Status Restore(DB* db) = 0;
|
|
|
|
// Requires external locking covering all keys in `cf`.
|
|
void ClearColumnFamily(int cf) { return latest_->ClearColumnFamily(cf); }
|
|
|
|
void SetPersistedSeqno(SequenceNumber seqno) {
|
|
return latest_->SetPersistedSeqno(seqno);
|
|
}
|
|
|
|
SequenceNumber GetPersistedSeqno() { return latest_->GetPersistedSeqno(); }
|
|
|
|
// See ExpectedState::PreparePut()
|
|
PendingExpectedValue PreparePut(int cf, int64_t key) {
|
|
return latest_->PreparePut(cf, key);
|
|
}
|
|
|
|
// See ExpectedState::Get()
|
|
ExpectedValue Get(int cf, int64_t key) { return latest_->Get(cf, key); }
|
|
|
|
// See ExpectedState::PrepareDelete()
|
|
PendingExpectedValue PrepareDelete(int cf, int64_t key) {
|
|
return latest_->PrepareDelete(cf, key);
|
|
}
|
|
|
|
// See ExpectedState::PrepareSingleDelete()
|
|
PendingExpectedValue PrepareSingleDelete(int cf, int64_t key) {
|
|
return latest_->PrepareSingleDelete(cf, key);
|
|
}
|
|
|
|
// See ExpectedState::PrepareDeleteRange()
|
|
std::vector<PendingExpectedValue> PrepareDeleteRange(int cf,
|
|
int64_t begin_key,
|
|
int64_t end_key) {
|
|
return latest_->PrepareDeleteRange(cf, begin_key, end_key);
|
|
}
|
|
|
|
// See ExpectedState::Exists()
|
|
bool Exists(int cf, int64_t key) { return latest_->Exists(cf, key); }
|
|
|
|
// See ExpectedState::SyncPut()
|
|
void SyncPut(int cf, int64_t key, uint32_t value_base) {
|
|
return latest_->SyncPut(cf, key, value_base);
|
|
}
|
|
|
|
// See ExpectedState::SyncPendingPut()
|
|
void SyncPendingPut(int cf, int64_t key) {
|
|
return latest_->SyncPendingPut(cf, key);
|
|
}
|
|
|
|
// See ExpectedState::SyncDelete()
|
|
void SyncDelete(int cf, int64_t key) { return latest_->SyncDelete(cf, key); }
|
|
|
|
// See ExpectedState::SyncDeleteRange()
|
|
void SyncDeleteRange(int cf, int64_t begin_key, int64_t end_key) {
|
|
return latest_->SyncDeleteRange(cf, begin_key, end_key);
|
|
}
|
|
|
|
protected:
|
|
const size_t max_key_;
|
|
const size_t num_column_families_;
|
|
std::unique_ptr<ExpectedState> latest_;
|
|
};
|
|
|
|
// A `FileExpectedStateManager` implements an `ExpectedStateManager` backed by
|
|
// a directory of files containing data about the expected state of the
|
|
// database.
|
|
class FileExpectedStateManager : public ExpectedStateManager {
|
|
public:
|
|
explicit FileExpectedStateManager(size_t max_key, size_t num_column_families,
|
|
std::string expected_state_dir_path);
|
|
|
|
// Requires external locking preventing concurrent execution with any other
|
|
// member function.
|
|
Status Open() override;
|
|
|
|
// See `ExpectedStateManager::SaveAtAndAfter()` API doc.
|
|
//
|
|
// This implementation makes a copy of "LATEST.state" into
|
|
// "<current seqno>.state", and starts a trace in "<current seqno>.trace".
|
|
// Due to using external files, a following `Restore()` can happen even
|
|
// from a different process.
|
|
Status SaveAtAndAfter(DB* db) override;
|
|
|
|
// See `ExpectedStateManager::HasHistory()` API doc.
|
|
bool HasHistory() override;
|
|
|
|
// See `ExpectedStateManager::Restore()` API doc.
|
|
//
|
|
// Say `db->GetLatestSequenceNumber()` was `a` last time `SaveAtAndAfter()`
|
|
// was called and now it is `b`. Then this function replays `b - a` write
|
|
// operations from "`a`.trace" onto "`a`.state", and then copies the resulting
|
|
// file into "LATEST.state". This relies on "`a`.trace" being an ordered
|
|
// superset of writes that could survive recovery: missing trace entries are
|
|
// fatal because replay length comes from DB sequence space, while extra tail
|
|
// entries are tolerated.
|
|
Status Restore(DB* db) override;
|
|
|
|
private:
|
|
// Requires external locking preventing concurrent execution with any other
|
|
// member function.
|
|
Status Clean();
|
|
|
|
std::string GetTempPathForFilename(const std::string& filename);
|
|
std::string GetPathForFilename(const std::string& filename);
|
|
|
|
static const std::string kLatestBasename;
|
|
static const std::string kStateFilenameSuffix;
|
|
static const std::string kTraceFilenameSuffix;
|
|
static const std::string kPersistedSeqnoBasename;
|
|
static const std::string kPersistedSeqnoFilenameSuffix;
|
|
static const std::string kTempFilenamePrefix;
|
|
static const std::string kTempFilenameSuffix;
|
|
|
|
const std::string expected_state_dir_path_;
|
|
SequenceNumber saved_seqno_ = kMaxSequenceNumber;
|
|
};
|
|
|
|
// An `AnonExpectedStateManager` implements an `ExpectedStateManager` backed by
|
|
// a memory allocation containing data about the expected state of the database.
|
|
class AnonExpectedStateManager : public ExpectedStateManager {
|
|
public:
|
|
explicit AnonExpectedStateManager(size_t max_key, size_t num_column_families);
|
|
|
|
// See `ExpectedStateManager::SaveAtAndAfter()` API doc.
|
|
//
|
|
// This implementation returns `Status::NotSupported` since we do not
|
|
// currently have a need to keep history of expected state within a process.
|
|
Status SaveAtAndAfter(DB* /* db */) override {
|
|
return Status::NotSupported();
|
|
}
|
|
|
|
// See `ExpectedStateManager::HasHistory()` API doc.
|
|
bool HasHistory() override { return false; }
|
|
|
|
// See `ExpectedStateManager::Restore()` API doc.
|
|
//
|
|
// This implementation returns `Status::NotSupported` since we do not
|
|
// currently have a need to keep history of expected state within a process.
|
|
Status Restore(DB* /* db */) override { return Status::NotSupported(); }
|
|
|
|
// Requires external locking preventing concurrent execution with any other
|
|
// member function.
|
|
Status Open() override;
|
|
};
|
|
} // namespace ROCKSDB_NAMESPACE
|
|
|
|
#endif // GFLAGS
|