2016-05-12 23:43:17 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "LocalStore.h"
|
2016-06-09 00:52:51 +03:00
|
|
|
|
2016-05-12 23:43:17 +03:00
|
|
|
#include <folly/Format.h>
|
|
|
|
#include <folly/String.h>
|
2016-06-16 00:23:24 +03:00
|
|
|
#include <folly/io/Cursor.h>
|
|
|
|
#include <folly/io/IOBuf.h>
|
2016-06-09 04:59:56 +03:00
|
|
|
#include <rocksdb/db.h>
|
2016-05-12 23:43:17 +03:00
|
|
|
#include <array>
|
2016-06-09 04:59:56 +03:00
|
|
|
#include "eden/fs/model/Blob.h"
|
|
|
|
#include "eden/fs/model/Tree.h"
|
2016-05-12 23:43:17 +03:00
|
|
|
#include "eden/fs/model/git/GitBlob.h"
|
|
|
|
#include "eden/fs/model/git/GitTree.h"
|
|
|
|
#include "eden/fs/rocksdb/RocksDbUtil.h"
|
|
|
|
#include "eden/fs/rocksdb/RocksException.h"
|
2016-06-09 04:59:56 +03:00
|
|
|
#include "eden/fs/store/StoreResult.h"
|
2016-05-12 23:43:17 +03:00
|
|
|
|
|
|
|
using facebook::eden::Hash;
|
|
|
|
using folly::ByteRange;
|
2016-06-16 00:23:24 +03:00
|
|
|
using folly::io::Cursor;
|
|
|
|
using folly::IOBuf;
|
2016-05-12 23:43:17 +03:00
|
|
|
using folly::StringPiece;
|
|
|
|
using rocksdb::ReadOptions;
|
|
|
|
using rocksdb::Slice;
|
2016-06-14 01:15:33 +03:00
|
|
|
using rocksdb::SliceParts;
|
2016-05-12 23:43:17 +03:00
|
|
|
using rocksdb::WriteBatch;
|
|
|
|
using rocksdb::WriteOptions;
|
|
|
|
using std::string;
|
|
|
|
using std::unique_ptr;
|
|
|
|
|
2016-06-09 00:52:51 +03:00
|
|
|
namespace {
|
|
|
|
|
2016-05-12 23:43:17 +03:00
|
|
|
/**
|
|
|
|
* For a blob, we write an entry whose key is the same of the blob's with the
|
|
|
|
* SHA1_KEY_SUFFIX suffix appended to it that maps to the SHA-1 of the blob's
|
|
|
|
* contents.
|
|
|
|
*
|
|
|
|
* Note that we use a suffix that is a single byte so that the resulting key can
|
|
|
|
* fit efficiently in an fbstring.
|
|
|
|
*/
|
|
|
|
const unsigned char ATTRIBUTE_SHA_1 = 's';
|
|
|
|
|
2016-06-09 00:52:51 +03:00
|
|
|
class Sha1Key {
|
|
|
|
public:
|
|
|
|
explicit Sha1Key(const Hash& id) {
|
|
|
|
memcpy(key_.data(), id.getBytes().data(), Hash::RAW_SIZE);
|
|
|
|
key_[Hash::RAW_SIZE] = ATTRIBUTE_SHA_1;
|
|
|
|
}
|
2016-05-12 23:43:17 +03:00
|
|
|
|
2016-06-09 00:52:51 +03:00
|
|
|
ByteRange bytes() const {
|
|
|
|
return ByteRange(key_.data(), key_.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
Slice slice() const {
|
|
|
|
return Slice{reinterpret_cast<const char*>(key_.data()), key_.size()};
|
|
|
|
}
|
2016-05-12 23:43:17 +03:00
|
|
|
|
2016-06-09 00:52:51 +03:00
|
|
|
private:
|
|
|
|
std::array<uint8_t, Hash::RAW_SIZE + 1> key_;
|
|
|
|
};
|
|
|
|
|
|
|
|
rocksdb::Slice _createSlice(folly::ByteRange bytes) {
|
|
|
|
return Slice(reinterpret_cast<const char*>(bytes.data()), bytes.size());
|
2016-05-12 23:43:17 +03:00
|
|
|
}
|
2016-06-09 00:52:51 +03:00
|
|
|
|
2016-05-12 23:43:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace facebook {
|
|
|
|
namespace eden {
|
|
|
|
|
|
|
|
LocalStore::LocalStore(StringPiece pathToRocksDb)
|
|
|
|
: db_(createRocksDb(pathToRocksDb)) {}
|
|
|
|
|
2016-07-12 23:06:23 +03:00
|
|
|
LocalStore::~LocalStore() {
|
|
|
|
#ifdef FOLLY_SANITIZE_ADDRESS
|
|
|
|
// RocksDB has some race conditions around setting up and tearing down
|
|
|
|
// the threads that it uses to maintain the database. This manifests
|
|
|
|
// in our test harness, particularly in a test where we quickly mount
|
|
|
|
// and then unmount. We see this as an abort with the message:
|
|
|
|
// "pthread lock: Invalid Argument".
|
|
|
|
// My assumption is that we're shutting things down before rocks has
|
|
|
|
// completed initializing. This sleep call is present in the destructor
|
|
|
|
// to make it more likely that rocks is past that critical point and
|
|
|
|
// so that we can shutdown successfully.
|
|
|
|
/* sleep override */ sleep(1);
|
|
|
|
#endif
|
|
|
|
}
|
2016-06-09 04:59:56 +03:00
|
|
|
|
2016-06-14 01:15:33 +03:00
|
|
|
StoreResult LocalStore::get(ByteRange key) const {
|
|
|
|
string value;
|
|
|
|
auto status = db_.get()->Get(ReadOptions(), _createSlice(key), &value);
|
|
|
|
if (!status.ok()) {
|
|
|
|
if (status.IsNotFound()) {
|
|
|
|
// Return an empty StoreResult
|
|
|
|
return StoreResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: RocksDB can return a "TryAgain" error.
|
|
|
|
// Should we try again for the user, rather than re-throwing the error?
|
|
|
|
|
|
|
|
// We don't use RocksException::check(), since we don't want to waste our
|
|
|
|
// time computing the hex string of the key if we succeeded.
|
|
|
|
throw RocksException::build(
|
|
|
|
status, "failed to get ", folly::hexlify(key), " from local store");
|
|
|
|
}
|
|
|
|
return StoreResult(std::move(value));
|
|
|
|
}
|
|
|
|
|
2016-06-09 04:59:56 +03:00
|
|
|
StoreResult LocalStore::get(const Hash& id) const {
|
2016-06-14 01:15:33 +03:00
|
|
|
return get(id.getBytes());
|
2016-05-12 23:43:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(mbolin): Currently, all objects in our RocksDB are Git objects. We
|
|
|
|
// probably want to namespace these by column family going forward, at which
|
|
|
|
// point we might want to have a GitLocalStore that delegates to an
|
|
|
|
// LocalStore so a vanilla LocalStore has no knowledge of deserializeGitTree()
|
|
|
|
// or deserializeGitBlob().
|
|
|
|
|
|
|
|
std::unique_ptr<Tree> LocalStore::getTree(const Hash& id) const {
|
2016-06-14 01:15:33 +03:00
|
|
|
auto result = get(id.getBytes());
|
2016-06-09 04:59:56 +03:00
|
|
|
if (!result.isValid()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return deserializeGitTree(id, result.bytes());
|
2016-05-12 23:43:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<Blob> LocalStore::getBlob(const Hash& id) const {
|
|
|
|
// We have to hold this string in scope while we deserialize and build
|
|
|
|
// the blob; otherwise, the results are undefined.
|
2016-06-14 01:15:33 +03:00
|
|
|
auto result = get(id.getBytes());
|
2016-06-09 04:59:56 +03:00
|
|
|
if (!result.isValid()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-06-09 04:59:57 +03:00
|
|
|
auto buf = result.extractIOBuf();
|
|
|
|
return deserializeGitBlob(id, &buf);
|
2016-05-12 23:43:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<Hash> LocalStore::getSha1ForBlob(const Hash& id) const {
|
2016-06-09 00:52:51 +03:00
|
|
|
Sha1Key key(id);
|
2016-06-14 01:15:33 +03:00
|
|
|
auto result = get(key.bytes());
|
2016-06-09 04:59:56 +03:00
|
|
|
if (!result.isValid()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
auto bytes = result.bytes();
|
|
|
|
if (bytes.size() != Hash::RAW_SIZE) {
|
2016-05-12 23:43:17 +03:00
|
|
|
throw std::invalid_argument(folly::sformat(
|
|
|
|
"Database entry for {} was not of size {}. Could not convert to SHA-1.",
|
|
|
|
id.toString(),
|
|
|
|
static_cast<size_t>(Hash::RAW_SIZE)));
|
|
|
|
}
|
|
|
|
|
2016-06-09 04:59:56 +03:00
|
|
|
return std::make_unique<Hash>(bytes);
|
2016-05-12 23:43:17 +03:00
|
|
|
}
|
|
|
|
|
2016-06-16 00:23:24 +03:00
|
|
|
void LocalStore::putBlob(const Hash& id, const Blob* blob) {
|
|
|
|
const IOBuf& contents = blob->getContents();
|
|
|
|
|
|
|
|
Sha1Key sha1Key(id);
|
|
|
|
auto sha1 = Hash::sha1(&contents);
|
|
|
|
|
|
|
|
auto hashSlice = _createSlice(id.getBytes());
|
|
|
|
SliceParts keyParts(&hashSlice, 1);
|
|
|
|
|
|
|
|
ByteRange bodyBytes;
|
|
|
|
|
|
|
|
// Add a git-style blob prefix
|
|
|
|
auto prefix = folly::to<string>("blob ", contents.computeChainDataLength());
|
|
|
|
prefix.push_back('\0');
|
|
|
|
std::vector<Slice> bodySlices;
|
|
|
|
bodySlices.emplace_back(prefix);
|
|
|
|
|
|
|
|
// Add all of the IOBuf chunks
|
|
|
|
Cursor cursor(&contents);
|
|
|
|
while (true) {
|
|
|
|
auto bytes = cursor.peekBytes();
|
|
|
|
if (bytes.empty()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
bodySlices.push_back(_createSlice(bytes));
|
|
|
|
cursor.skip(bytes.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
SliceParts bodyParts(bodySlices.data(), bodySlices.size());
|
|
|
|
|
|
|
|
WriteBatch blobWrites;
|
|
|
|
blobWrites.Put(keyParts, bodyParts);
|
|
|
|
blobWrites.Put(sha1Key.slice(), _createSlice(sha1.getBytes()));
|
|
|
|
auto status = db_->Write(WriteOptions(), &blobWrites);
|
|
|
|
if (!status.ok()) {
|
|
|
|
// We don't use RocksException::check(), since we don't want to waste our
|
|
|
|
// time computing the hex string of the key if we succeeded.
|
|
|
|
throw RocksException::build(
|
|
|
|
status,
|
|
|
|
"error putting blob ",
|
|
|
|
folly::hexlify(id.getBytes()),
|
|
|
|
" in local store");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: We should deprecate this function, and update all callers to use
|
|
|
|
// the version of putBlob() above.
|
2016-06-14 01:15:33 +03:00
|
|
|
void LocalStore::putBlob(const Hash& id, ByteRange blobData, const Hash& sha1) {
|
2016-06-09 00:52:51 +03:00
|
|
|
Sha1Key sha1Key(id);
|
2016-05-12 23:43:17 +03:00
|
|
|
|
|
|
|
// Record both the blob and SHA-1 entries in one RocksDB write operation.
|
|
|
|
WriteBatch blobWrites;
|
2016-06-09 00:52:51 +03:00
|
|
|
blobWrites.Put(_createSlice(id.getBytes()), _createSlice(blobData));
|
|
|
|
blobWrites.Put(sha1Key.slice(), _createSlice(sha1.getBytes()));
|
2016-05-12 23:43:17 +03:00
|
|
|
auto status = db_->Write(WriteOptions(), &blobWrites);
|
2016-06-09 00:52:51 +03:00
|
|
|
if (!status.ok()) {
|
|
|
|
// We don't use RocksException::check(), since we don't want to waste our
|
|
|
|
// time computing the hex string of the key if we succeeded.
|
|
|
|
throw RocksException::build(
|
|
|
|
status,
|
|
|
|
"error putting blob ",
|
|
|
|
folly::hexlify(id.getBytes()),
|
|
|
|
" in local store");
|
|
|
|
}
|
2016-05-12 23:43:17 +03:00
|
|
|
}
|
|
|
|
|
2016-07-25 22:33:39 +03:00
|
|
|
Hash LocalStore::putTree(const Tree* tree) {
|
|
|
|
GitTreeSerializer serializer;
|
|
|
|
for (auto& entry : tree->getTreeEntries()) {
|
|
|
|
serializer.addEntry(std::move(entry));
|
|
|
|
}
|
|
|
|
IOBuf treeBuf = serializer.finalize();
|
|
|
|
ByteRange treeData = treeBuf.coalesce();
|
|
|
|
|
|
|
|
auto id = tree->getHash();
|
|
|
|
if (id == Hash()) {
|
|
|
|
id = Hash::sha1(&treeBuf);
|
|
|
|
}
|
2016-06-14 01:15:33 +03:00
|
|
|
put(id.getBytes(), treeData);
|
2016-07-25 22:33:39 +03:00
|
|
|
return id;
|
2016-06-14 01:15:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocalStore::put(folly::ByteRange key, folly::ByteRange value) {
|
|
|
|
auto status =
|
|
|
|
db_->Put(WriteOptions(), _createSlice(key), _createSlice(value));
|
2016-06-09 00:52:51 +03:00
|
|
|
if (!status.ok()) {
|
|
|
|
// We don't use RocksException::check(), since we don't want to waste our
|
|
|
|
// time computing the hex string of the key if we succeeded.
|
|
|
|
throw RocksException::build(
|
|
|
|
status,
|
2016-06-14 01:15:33 +03:00
|
|
|
"error putting data for key ",
|
|
|
|
folly::hexlify(key),
|
2016-06-09 00:52:51 +03:00
|
|
|
" in local store");
|
|
|
|
}
|
2016-05-12 23:43:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|