sapling/eden/fs/rocksdb/RocksHandles.h

65 lines
1.8 KiB
C
Raw Normal View History

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This software may be used and distributed according to the terms of the
* GNU General Public License version 2.
*/
#pragma once
#include <folly/String.h>
#include <rocksdb/db.h>
#include <memory>
#include <string>
namespace facebook {
namespace eden {
enum class RocksDBOpenMode {
ReadOnly,
ReadWrite,
};
/**
* This class is the holder of the database and column family handles
* required to interact with our local rocksdb store.
* RocksDB requires that we delete the column family handles prior
* to deleting the DB so we need to manage the lifetime and
* destruction order with this class.
*/
struct RocksHandles {
std::unique_ptr<rocksdb::DB> db;
// The order of these columns matches the descriptors passed
// as column_descriptors to createRocksDb().
std::vector<std::unique_ptr<rocksdb::ColumnFamilyHandle>> columns;
/**
* Note that the columns MUST be destroyed prior to the DB,
* so we have a custom destructor for that purpose.
*/
~RocksHandles();
/**
* Returns an instance of a RocksDB that uses the specified directory for
* storage. If there is an existing RocksDB at that path with
* column_descriptors that match the requested set then it will be opened and
* returned. If there is no existing RocksDB at that location a new one will
* be initialized using the requested column_descriptors. Otherwise (an
* existing RocksDB has mismatched column_descriptors) will throw an
* exception.
*/
RocksHandles(
folly::StringPiece dbPath,
RocksDBOpenMode mode,
const rocksdb::Options& options,
const std::vector<rocksdb::ColumnFamilyDescriptor>& columnDescriptors);
fix race conditions in RocksDbLocalStore access during shutdown Summary: This contains several fixes to LocalStore handling during shutdown. - Have EdenServer explicitly call localStore_->close() during shutdown. This ensures that the local store really gets close, just in case some other part of the code somehow still has an outstanding shared_ptr reference to it. - Add synchronization around internal access to the RocksDB object in RocksDbLocalStore. This ensures that calling `close()` is safe even if there happens to still be some outstanding I/O operations. In particular this helps ensure that if background GC operation is in progress that `close()` will wait until it completes before destroying the DB object. This also improves the code so that calling subsequent methods on a closed RocksDbLocalStore throws an exception, instead of simply crashing. I don't believe the additional synchronization in RocksDbLocalStore should have much impact on performance: the synchronization overhead should be very low compared to the cost of the RocksDB reads/writes. Ideally some of this synchronization logic should perhaps be moved into the base `LocalStore` class: all of the different `LocalStore` implementations should ideally ensure that `close()` is thread-safe and blocks until other pending I/O operations are complete. However, that requires a bigger refactoring. I may attempt that in a subsequent diff, but for now I mainly want to address this problem just for RocksDbLocalStore. Reviewed By: strager Differential Revision: D15948382 fbshipit-source-id: 96d633ac0879b3321f596224907fcfe72691b3f0
2019-06-25 04:26:34 +03:00
RocksHandles(RocksHandles&&) = default;
RocksHandles& operator=(RocksHandles&&) = default;
void close();
};
} // namespace eden
} // namespace facebook