implement "eden_store_util show_sizes"

Summary:
Implement the `show_sizes` function which reports the approximate size on disk
used by each column family.

The initial skeleton for this command was accidentally included in D15162813.

Reviewed By: wez

Differential Revision: D15307391

fbshipit-source-id: 03efee939d65500e17b48047552dec1dbb7aff7e
This commit is contained in:
Adam Simpkins 2019-05-15 12:15:49 -07:00 committed by Facebook Github Bot
parent ec2fbbf595
commit c8fe073a12
3 changed files with 47 additions and 11 deletions

View File

@ -91,6 +91,24 @@ const std::vector<rocksdb::ColumnFamilyDescriptor>& columnFamilies() {
return families;
}
/**
* Return a rocksdb::Range that contains all possible keys that we store.
*
* The input string will be used to store data for the Range slices.
* The caller must ensure that the rangeStorage parameter remains valid and
* unmodified until they are done using the returned Range.
*/
rocksdb::Range getFullRange(std::string& rangeStorage) {
// An empty slice is the lowest possible value.
Slice begin;
// All of our keys are currently 20 bytes.
// Use a longer key to ensure that this is greater than any valid key.
rangeStorage = std::string(
21, static_cast<char>(std::numeric_limits<unsigned char>::max()));
Slice end(rangeStorage);
return rocksdb::Range(begin, end);
}
rocksdb::Slice _createSlice(folly::ByteRange bytes) {
return Slice(reinterpret_cast<const char*>(bytes.data()), bytes.size());
}
@ -274,18 +292,14 @@ void RocksDbLocalStore::clearKeySpace(KeySpace keySpace) {
std::unique_ptr<rocksdb::Iterator> it{
dbHandles_.db->NewIterator(ReadOptions(), columnFamily)};
XLOG(DBG2) << "clearing column family \"" << columnFamily->GetName() << "\"";
Slice begin;
// All of our keys are currently 20 bytes. Use a longer key to ensure that
// this is greater than any valid key.
std::string endStr(
21, static_cast<char>(std::numeric_limits<unsigned char>::max()));
Slice end(endStr);
std::string rangeStorage;
const auto fullRange = getFullRange(rangeStorage);
// Delete all SST files that only contain keys in the specified range.
// Since we are deleting everything in this column family this should
// effectively delete everything.
auto status =
DeleteFilesInRange(dbHandles_.db.get(), columnFamily, &begin, &end);
auto status = DeleteFilesInRange(
dbHandles_.db.get(), columnFamily, &fullRange.start, &fullRange.limit);
if (!status.ok()) {
throw RocksException::build(
status,
@ -298,7 +312,8 @@ void RocksDbLocalStore::clearKeySpace(KeySpace keySpace) {
// everything in the range (but it probably will in our case since we are
// intending to delete everything).
const WriteOptions writeOptions;
status = dbHandles_.db->DeleteRange(writeOptions, columnFamily, begin, end);
status = dbHandles_.db->DeleteRange(
writeOptions, columnFamily, fullRange.start, fullRange.limit);
if (!status.ok()) {
throw RocksException::build(
status,
@ -489,5 +504,16 @@ void RocksDbLocalStore::put(
_createSlice(value));
}
uint64_t RocksDbLocalStore::getApproximateSize(
LocalStore::KeySpace keySpace) const {
std::string rangeStorage;
const auto range = getFullRange(rangeStorage);
uint64_t size = 0;
dbHandles_.db->GetApproximateSizes(
dbHandles_.columns[keySpace].get(), &range, 1, &size);
return size;
}
} // namespace eden
} // namespace facebook

View File

@ -54,6 +54,10 @@ class RocksDbLocalStore : public LocalStore {
// Call RocksDB's RepairDB() function on the DB at the specified location
static void repairDB(AbsolutePathPiece path);
// Get the approximate number of bytes stored on disk for the
// specified key space.
uint64_t getApproximateSize(KeySpace keySpace) const;
private:
FaultInjector& faultInjector_;
RocksHandles dbHandles_;

View File

@ -13,6 +13,7 @@
#include <folly/Range.h>
#include <folly/container/Array.h>
#include <folly/container/Enumerate.h>
#include <folly/init/Init.h>
#include <folly/logging/Init.h>
#include <folly/logging/xlog.h>
@ -23,6 +24,7 @@
#include "eden/fs/fuse/privhelper/UserInfo.h"
#include "eden/fs/service/EdenInit.h"
#include "eden/fs/service/EdenStateDir.h"
#include "eden/fs/store/KeySpaces.h"
#include "eden/fs/store/RocksDbLocalStore.h"
#include "eden/fs/utils/FaultInjector.h"
@ -194,8 +196,12 @@ class ShowSizesCommand : public Command {
void run() override {
auto localStore = openLocalStore();
(void)localStore;
printf("show sizes\n");
for (const auto& iter : folly::enumerate(kKeySpaceRecords)) {
LOG(INFO) << "Column family \"" << iter->name << "\": "
<< localStore->getApproximateSize(
static_cast<LocalStore::KeySpace>(iter.index));
}
}
};