2018-02-07 22:45:41 +03:00
|
|
|
/*
|
2019-06-20 02:58:25 +03:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2018-02-07 22:45:41 +03:00
|
|
|
*
|
2019-06-20 02:58:25 +03:00
|
|
|
* This software may be used and distributed according to the terms of the
|
|
|
|
* GNU General Public License version 2.
|
2018-02-07 22:45:41 +03:00
|
|
|
*/
|
2019-10-11 15:26:59 +03:00
|
|
|
|
2018-02-07 22:45:41 +03:00
|
|
|
#pragma once
|
2019-05-15 22:15:49 +03:00
|
|
|
#include <folly/CppAttributes.h>
|
2019-06-21 06:53:39 +03:00
|
|
|
#include <folly/Synchronized.h>
|
2019-05-15 22:15:49 +03:00
|
|
|
|
2018-02-07 22:45:41 +03:00
|
|
|
#include "eden/fs/rocksdb/RocksHandles.h"
|
|
|
|
#include "eden/fs/store/LocalStore.h"
|
2018-08-03 23:08:37 +03:00
|
|
|
#include "eden/fs/utils/UnboundedQueueExecutor.h"
|
2018-02-07 22:45:41 +03:00
|
|
|
|
|
|
|
namespace facebook {
|
|
|
|
namespace eden {
|
|
|
|
|
2019-03-13 05:25:54 +03:00
|
|
|
class FaultInjector;
|
2019-12-11 03:14:59 +03:00
|
|
|
class StructuredLogger;
|
2018-11-09 22:20:16 +03:00
|
|
|
|
2018-02-07 22:45:41 +03:00
|
|
|
/** An implementation of LocalStore that uses RocksDB for the underlying
|
|
|
|
* storage.
|
|
|
|
*/
|
|
|
|
class RocksDbLocalStore : public LocalStore {
|
|
|
|
public:
|
2019-03-13 05:25:54 +03:00
|
|
|
/**
|
|
|
|
* The given FaultInjector must be valid during the lifetime of this
|
|
|
|
* RocksDbLocalStore object.
|
|
|
|
*/
|
2019-05-15 22:15:49 +03:00
|
|
|
explicit RocksDbLocalStore(
|
|
|
|
AbsolutePathPiece pathToRocksDb,
|
2019-12-11 03:14:59 +03:00
|
|
|
std::shared_ptr<StructuredLogger> structuredLogger,
|
2019-05-21 00:07:33 +03:00
|
|
|
FaultInjector* FOLLY_NONNULL faultInjector,
|
|
|
|
RocksDBOpenMode mode = RocksDBOpenMode::ReadWrite);
|
2018-02-07 22:45:41 +03:00
|
|
|
~RocksDbLocalStore();
|
|
|
|
void close() override;
|
2018-05-31 20:39:49 +03:00
|
|
|
void clearKeySpace(KeySpace keySpace) override;
|
2018-08-10 21:09:48 +03:00
|
|
|
void compactKeySpace(KeySpace keySpace) override;
|
2018-02-07 22:45:41 +03:00
|
|
|
StoreResult get(LocalStore::KeySpace keySpace, folly::ByteRange key)
|
|
|
|
const override;
|
2018-05-10 04:41:22 +03:00
|
|
|
FOLLY_NODISCARD folly::Future<StoreResult> getFuture(
|
|
|
|
KeySpace keySpace,
|
|
|
|
folly::ByteRange key) const override;
|
2018-05-25 23:47:54 +03:00
|
|
|
FOLLY_NODISCARD folly::Future<std::vector<StoreResult>> getBatch(
|
|
|
|
KeySpace keySpace,
|
|
|
|
const std::vector<folly::ByteRange>& keys) const override;
|
2018-02-07 22:45:41 +03:00
|
|
|
bool hasKey(LocalStore::KeySpace keySpace, folly::ByteRange key)
|
|
|
|
const override;
|
|
|
|
void put(
|
|
|
|
LocalStore::KeySpace keySpace,
|
|
|
|
folly::ByteRange key,
|
|
|
|
folly::ByteRange value) override;
|
|
|
|
std::unique_ptr<WriteBatch> beginWrite(size_t bufSize = 0) override;
|
|
|
|
|
2019-04-25 05:59:48 +03:00
|
|
|
// Call RocksDB's RepairDB() function on the DB at the specified location
|
|
|
|
static void repairDB(AbsolutePathPiece path);
|
|
|
|
|
2019-05-15 22:15:49 +03:00
|
|
|
// Get the approximate number of bytes stored on disk for the
|
|
|
|
// specified key space.
|
|
|
|
uint64_t getApproximateSize(KeySpace keySpace) const;
|
|
|
|
|
2019-06-21 06:53:39 +03:00
|
|
|
void periodicManagementTask(const EdenConfig& config) override;
|
|
|
|
|
2018-02-07 22:45:41 +03:00
|
|
|
private:
|
2019-06-25 04:26:34 +03:00
|
|
|
/**
|
|
|
|
* Get a pointer to the RocksHandles object in order to perform an I/O
|
|
|
|
* operation.
|
|
|
|
*
|
|
|
|
* Note that even though this acquires a read-lock, write operations to the
|
|
|
|
* DB may still be performed. The lock exists to prevent the DB from being
|
|
|
|
* closed while the I/O operation is in progress.
|
|
|
|
*/
|
2019-08-23 21:04:47 +03:00
|
|
|
folly::Synchronized<RocksHandles>::ConstRLockedPtr getHandles() const {
|
2019-06-25 04:26:34 +03:00
|
|
|
auto handles = dbHandles_.rlock();
|
|
|
|
if (!handles->db) {
|
|
|
|
throwStoreClosedError();
|
|
|
|
}
|
|
|
|
return handles;
|
|
|
|
}
|
|
|
|
[[noreturn]] void throwStoreClosedError() const;
|
|
|
|
std::shared_ptr<RocksDbLocalStore> getSharedFromThis() {
|
|
|
|
return std::static_pointer_cast<RocksDbLocalStore>(shared_from_this());
|
|
|
|
}
|
|
|
|
std::shared_ptr<const RocksDbLocalStore> getSharedFromThis() const {
|
|
|
|
return std::static_pointer_cast<const RocksDbLocalStore>(
|
|
|
|
shared_from_this());
|
|
|
|
}
|
|
|
|
|
2019-06-21 06:53:39 +03:00
|
|
|
struct AutoGCState {
|
|
|
|
bool inProgress_{false};
|
|
|
|
std::chrono::steady_clock::time_point startTime_;
|
|
|
|
};
|
|
|
|
|
2019-12-11 03:14:59 +03:00
|
|
|
struct SizeSummary {
|
|
|
|
uint64_t ephemeral = 0;
|
|
|
|
uint64_t persistent = 0;
|
|
|
|
};
|
|
|
|
|
2019-06-21 06:53:39 +03:00
|
|
|
/**
|
|
|
|
* Publish fb303 counters.
|
2019-12-11 03:14:59 +03:00
|
|
|
* Returns the approximate sizes of all column families.
|
2019-06-21 06:53:39 +03:00
|
|
|
*/
|
2019-12-11 03:14:59 +03:00
|
|
|
SizeSummary computeStats(bool publish);
|
2019-06-21 06:53:39 +03:00
|
|
|
|
2019-12-11 03:14:59 +03:00
|
|
|
void triggerAutoGC(uint64_t ephemeralSize);
|
|
|
|
void autoGCFinished(bool successful, uint64_t ephemeralSizeBefore);
|
2019-06-21 06:53:39 +03:00
|
|
|
|
2019-12-11 03:14:59 +03:00
|
|
|
std::shared_ptr<StructuredLogger> structuredLogger_;
|
2019-06-21 06:53:39 +03:00
|
|
|
const std::string statsPrefix_{"local_store."};
|
2019-03-13 05:25:54 +03:00
|
|
|
FaultInjector& faultInjector_;
|
2018-08-03 23:08:37 +03:00
|
|
|
mutable UnboundedQueueExecutor ioPool_;
|
2019-06-21 06:53:39 +03:00
|
|
|
folly::Synchronized<AutoGCState> autoGCState_;
|
2019-06-25 04:26:34 +03:00
|
|
|
folly::Synchronized<RocksHandles> dbHandles_;
|
2018-02-07 22:45:41 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace eden
|
|
|
|
} // namespace facebook
|