2016-05-04 15:16:48 +00:00
|
|
|
|
#if ENABLE_S3
|
|
|
|
|
|
2017-02-14 13:20:00 +00:00
|
|
|
|
#include "s3.hh"
|
2016-04-21 14:02:48 +00:00
|
|
|
|
#include "s3-binary-cache-store.hh"
|
|
|
|
|
#include "nar-info.hh"
|
|
|
|
|
#include "nar-info-disk-cache.hh"
|
|
|
|
|
#include "globals.hh"
|
|
|
|
|
|
2016-12-22 16:39:05 +00:00
|
|
|
|
#include <aws/core/Aws.h>
|
2016-04-21 14:02:48 +00:00
|
|
|
|
#include <aws/core/client/ClientConfiguration.h>
|
|
|
|
|
#include <aws/s3/S3Client.h>
|
|
|
|
|
#include <aws/s3/model/CreateBucketRequest.h>
|
|
|
|
|
#include <aws/s3/model/GetBucketLocationRequest.h>
|
|
|
|
|
#include <aws/s3/model/GetObjectRequest.h>
|
|
|
|
|
#include <aws/s3/model/HeadObjectRequest.h>
|
2016-04-21 15:53:47 +00:00
|
|
|
|
#include <aws/s3/model/ListObjectsRequest.h>
|
2016-12-22 16:39:05 +00:00
|
|
|
|
#include <aws/s3/model/PutObjectRequest.h>
|
2016-04-21 14:02:48 +00:00
|
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
|
struct S3Error : public Error
|
|
|
|
|
{
|
|
|
|
|
Aws::S3::S3Errors err;
|
|
|
|
|
S3Error(Aws::S3::S3Errors err, const FormatOrString & fs)
|
|
|
|
|
: Error(fs), err(err) { };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* Helper: given an Outcome<R, E>, return R in case of success, or
|
|
|
|
|
throw an exception in case of an error. */
|
|
|
|
|
template<typename R, typename E>
|
|
|
|
|
R && checkAws(const FormatOrString & fs, Aws::Utils::Outcome<R, E> && outcome)
|
|
|
|
|
{
|
|
|
|
|
if (!outcome.IsSuccess())
|
|
|
|
|
throw S3Error(
|
|
|
|
|
outcome.GetError().GetErrorType(),
|
|
|
|
|
fs.s + ": " + outcome.GetError().GetMessage());
|
|
|
|
|
return outcome.GetResultWithOwnership();
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-22 16:39:05 +00:00
|
|
|
|
static void initAWS()
|
|
|
|
|
{
|
|
|
|
|
static std::once_flag flag;
|
|
|
|
|
std::call_once(flag, []() {
|
|
|
|
|
Aws::SDKOptions options;
|
|
|
|
|
|
|
|
|
|
/* We install our own OpenSSL locking function (see
|
|
|
|
|
shared.cc), so don't let aws-sdk-cpp override it. */
|
|
|
|
|
options.cryptoOptions.initAndCleanupOpenSSL = false;
|
|
|
|
|
|
|
|
|
|
Aws::InitAPI(options);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-14 13:20:00 +00:00
|
|
|
|
S3Helper::S3Helper()
|
|
|
|
|
: config(makeConfig())
|
|
|
|
|
, client(make_ref<Aws::S3::S3Client>(*config))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ref<Aws::Client::ClientConfiguration> S3Helper::makeConfig()
|
|
|
|
|
{
|
|
|
|
|
initAWS();
|
|
|
|
|
auto res = make_ref<Aws::Client::ClientConfiguration>();
|
|
|
|
|
res->region = Aws::Region::US_EAST_1; // FIXME: make configurable
|
|
|
|
|
res->requestTimeoutMs = 600 * 1000;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
S3Helper::DownloadResult S3Helper::getObject(
|
|
|
|
|
const std::string & bucketName, const std::string & key)
|
|
|
|
|
{
|
|
|
|
|
debug("fetching ‘s3://%s/%s’...", bucketName, key);
|
|
|
|
|
|
|
|
|
|
auto request =
|
|
|
|
|
Aws::S3::Model::GetObjectRequest()
|
|
|
|
|
.WithBucket(bucketName)
|
|
|
|
|
.WithKey(key);
|
|
|
|
|
|
|
|
|
|
request.SetResponseStreamFactory([&]() {
|
|
|
|
|
return Aws::New<std::stringstream>("STRINGSTREAM");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
DownloadResult res;
|
|
|
|
|
|
|
|
|
|
auto now1 = std::chrono::steady_clock::now();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
auto result = checkAws(fmt("AWS error fetching ‘%s’", key),
|
|
|
|
|
client->GetObject(request));
|
|
|
|
|
|
|
|
|
|
res.data = std::make_shared<std::string>(
|
|
|
|
|
dynamic_cast<std::stringstream &>(result.GetBody()).str());
|
|
|
|
|
|
|
|
|
|
} catch (S3Error & e) {
|
|
|
|
|
if (e.err != Aws::S3::S3Errors::NO_SUCH_KEY) throw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto now2 = std::chrono::steady_clock::now();
|
|
|
|
|
|
|
|
|
|
res.durationMs = std::chrono::duration_cast<std::chrono::milliseconds>(now2 - now1).count();
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if __linux__
|
|
|
|
|
|
|
|
|
|
struct istringstream_nocopy : public std::stringstream
|
|
|
|
|
{
|
|
|
|
|
istringstream_nocopy(const std::string & s)
|
|
|
|
|
{
|
|
|
|
|
rdbuf()->pubsetbuf(
|
|
|
|
|
(char *) s.data(), s.size());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-04-21 14:02:48 +00:00
|
|
|
|
struct S3BinaryCacheStoreImpl : public S3BinaryCacheStore
|
|
|
|
|
{
|
|
|
|
|
std::string bucketName;
|
|
|
|
|
|
|
|
|
|
Stats stats;
|
|
|
|
|
|
2017-02-14 13:20:00 +00:00
|
|
|
|
S3Helper s3Helper;
|
|
|
|
|
|
2016-05-04 18:15:41 +00:00
|
|
|
|
S3BinaryCacheStoreImpl(
|
2016-06-01 12:49:12 +00:00
|
|
|
|
const Params & params, const std::string & bucketName)
|
2016-05-04 18:15:41 +00:00
|
|
|
|
: S3BinaryCacheStore(params)
|
2016-04-21 14:02:48 +00:00
|
|
|
|
, bucketName(bucketName)
|
|
|
|
|
{
|
|
|
|
|
diskCache = getNarInfoDiskCache();
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-31 11:31:04 +00:00
|
|
|
|
std::string getUri() override
|
2016-04-21 14:02:48 +00:00
|
|
|
|
{
|
|
|
|
|
return "s3://" + bucketName;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-31 11:31:04 +00:00
|
|
|
|
void init() override
|
2016-04-21 14:02:48 +00:00
|
|
|
|
{
|
2016-06-01 13:15:21 +00:00
|
|
|
|
if (!diskCache->cacheExists(getUri(), wantMassQuery_, priority)) {
|
2016-04-21 14:02:48 +00:00
|
|
|
|
|
|
|
|
|
/* Create the bucket if it doesn't already exists. */
|
|
|
|
|
// FIXME: HeadBucket would be more appropriate, but doesn't return
|
|
|
|
|
// an easily parsed 404 message.
|
2017-02-14 13:20:00 +00:00
|
|
|
|
auto res = s3Helper.client->GetBucketLocation(
|
2016-04-21 14:02:48 +00:00
|
|
|
|
Aws::S3::Model::GetBucketLocationRequest().WithBucket(bucketName));
|
|
|
|
|
|
|
|
|
|
if (!res.IsSuccess()) {
|
|
|
|
|
if (res.GetError().GetErrorType() != Aws::S3::S3Errors::NO_SUCH_BUCKET)
|
2016-11-25 23:37:43 +00:00
|
|
|
|
throw Error(format("AWS error checking bucket ‘%s’: %s") % bucketName % res.GetError().GetMessage());
|
2016-04-21 14:02:48 +00:00
|
|
|
|
|
2016-11-25 23:37:43 +00:00
|
|
|
|
checkAws(format("AWS error creating bucket ‘%s’") % bucketName,
|
2017-02-14 13:20:00 +00:00
|
|
|
|
s3Helper.client->CreateBucket(
|
2016-04-21 14:02:48 +00:00
|
|
|
|
Aws::S3::Model::CreateBucketRequest()
|
|
|
|
|
.WithBucket(bucketName)
|
|
|
|
|
.WithCreateBucketConfiguration(
|
|
|
|
|
Aws::S3::Model::CreateBucketConfiguration()
|
|
|
|
|
/* .WithLocationConstraint(
|
|
|
|
|
Aws::S3::Model::BucketLocationConstraint::US) */ )));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BinaryCacheStore::init();
|
|
|
|
|
|
2016-06-01 12:49:12 +00:00
|
|
|
|
diskCache->createCache(getUri(), storeDir, wantMassQuery_, priority);
|
2016-04-21 14:02:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-06 14:34:14 +00:00
|
|
|
|
const Stats & getS3Stats() override
|
2016-04-21 14:02:48 +00:00
|
|
|
|
{
|
|
|
|
|
return stats;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* This is a specialisation of isValidPath() that optimistically
|
|
|
|
|
fetches the .narinfo file, rather than first checking for its
|
|
|
|
|
existence via a HEAD request. Since .narinfos are small, doing
|
|
|
|
|
a GET is unlikely to be slower than HEAD. */
|
2016-05-31 11:31:04 +00:00
|
|
|
|
bool isValidPathUncached(const Path & storePath) override
|
2016-04-21 14:02:48 +00:00
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
queryPathInfo(storePath);
|
|
|
|
|
return true;
|
|
|
|
|
} catch (InvalidPath & e) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-31 11:31:04 +00:00
|
|
|
|
bool fileExists(const std::string & path) override
|
2016-04-21 14:02:48 +00:00
|
|
|
|
{
|
|
|
|
|
stats.head++;
|
|
|
|
|
|
2017-02-14 13:20:00 +00:00
|
|
|
|
auto res = s3Helper.client->HeadObject(
|
2016-04-21 14:02:48 +00:00
|
|
|
|
Aws::S3::Model::HeadObjectRequest()
|
|
|
|
|
.WithBucket(bucketName)
|
|
|
|
|
.WithKey(path));
|
|
|
|
|
|
|
|
|
|
if (!res.IsSuccess()) {
|
|
|
|
|
auto & error = res.GetError();
|
|
|
|
|
if (error.GetErrorType() == Aws::S3::S3Errors::UNKNOWN // FIXME
|
|
|
|
|
&& error.GetMessage().find("404") != std::string::npos)
|
|
|
|
|
return false;
|
2016-11-25 23:37:43 +00:00
|
|
|
|
throw Error(format("AWS error fetching ‘%s’: %s") % path % error.GetMessage());
|
2016-04-21 14:02:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-31 11:31:04 +00:00
|
|
|
|
void upsertFile(const std::string & path, const std::string & data) override
|
2016-04-21 14:02:48 +00:00
|
|
|
|
{
|
|
|
|
|
auto request =
|
|
|
|
|
Aws::S3::Model::PutObjectRequest()
|
|
|
|
|
.WithBucket(bucketName)
|
|
|
|
|
.WithKey(path);
|
|
|
|
|
|
2016-11-16 15:21:30 +00:00
|
|
|
|
auto stream = std::make_shared<istringstream_nocopy>(data);
|
2016-04-21 14:02:48 +00:00
|
|
|
|
|
|
|
|
|
request.SetBody(stream);
|
|
|
|
|
|
|
|
|
|
stats.put++;
|
|
|
|
|
stats.putBytes += data.size();
|
|
|
|
|
|
|
|
|
|
auto now1 = std::chrono::steady_clock::now();
|
|
|
|
|
|
2016-11-25 23:37:43 +00:00
|
|
|
|
auto result = checkAws(format("AWS error uploading ‘%s’") % path,
|
2017-02-14 13:20:00 +00:00
|
|
|
|
s3Helper.client->PutObject(request));
|
2016-04-21 14:02:48 +00:00
|
|
|
|
|
|
|
|
|
auto now2 = std::chrono::steady_clock::now();
|
|
|
|
|
|
|
|
|
|
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(now2 - now1).count();
|
|
|
|
|
|
2016-11-25 23:37:43 +00:00
|
|
|
|
printInfo(format("uploaded ‘s3://%1%/%2%’ (%3% bytes) in %4% ms")
|
2016-04-21 14:02:48 +00:00
|
|
|
|
% bucketName % path % data.size() % duration);
|
|
|
|
|
|
|
|
|
|
stats.putTimeMs += duration;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-16 16:54:14 +00:00
|
|
|
|
void getFile(const std::string & path,
|
|
|
|
|
std::function<void(std::shared_ptr<std::string>)> success,
|
|
|
|
|
std::function<void(std::exception_ptr exc)> failure) override
|
2016-04-21 14:02:48 +00:00
|
|
|
|
{
|
2016-09-16 16:54:14 +00:00
|
|
|
|
sync2async<std::shared_ptr<std::string>>(success, failure, [&]() {
|
2016-11-25 23:37:43 +00:00
|
|
|
|
debug(format("fetching ‘s3://%1%/%2%’...") % bucketName % path);
|
2016-04-21 14:02:48 +00:00
|
|
|
|
|
2016-09-16 16:54:14 +00:00
|
|
|
|
stats.get++;
|
2016-04-21 14:02:48 +00:00
|
|
|
|
|
2017-02-14 13:20:00 +00:00
|
|
|
|
auto res = s3Helper.getObject(bucketName, path);
|
2016-04-21 14:02:48 +00:00
|
|
|
|
|
2017-02-14 13:20:00 +00:00
|
|
|
|
stats.getBytes += res.data ? res.data->size() : 0;
|
|
|
|
|
stats.getTimeMs += res.durationMs;
|
2016-04-21 14:02:48 +00:00
|
|
|
|
|
2017-02-14 13:20:00 +00:00
|
|
|
|
if (res.data)
|
|
|
|
|
printTalkative("downloaded ‘s3://%s/%s’ (%d bytes) in %d ms",
|
|
|
|
|
bucketName, path, res.data->size(), res.durationMs);
|
2016-04-21 14:02:48 +00:00
|
|
|
|
|
2017-02-14 13:20:00 +00:00
|
|
|
|
return res.data;
|
2016-09-16 16:54:14 +00:00
|
|
|
|
});
|
2016-04-21 14:02:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-21 15:53:47 +00:00
|
|
|
|
PathSet queryAllValidPaths() override
|
|
|
|
|
{
|
|
|
|
|
PathSet paths;
|
|
|
|
|
std::string marker;
|
|
|
|
|
|
|
|
|
|
do {
|
2016-11-25 23:37:43 +00:00
|
|
|
|
debug(format("listing bucket ‘s3://%s’ from key ‘%s’...") % bucketName % marker);
|
2016-04-21 15:53:47 +00:00
|
|
|
|
|
2016-11-25 23:37:43 +00:00
|
|
|
|
auto res = checkAws(format("AWS error listing bucket ‘%s’") % bucketName,
|
2017-02-14 13:20:00 +00:00
|
|
|
|
s3Helper.client->ListObjects(
|
2016-04-21 15:53:47 +00:00
|
|
|
|
Aws::S3::Model::ListObjectsRequest()
|
|
|
|
|
.WithBucket(bucketName)
|
|
|
|
|
.WithDelimiter("/")
|
|
|
|
|
.WithMarker(marker)));
|
|
|
|
|
|
|
|
|
|
auto & contents = res.GetContents();
|
|
|
|
|
|
2016-11-25 23:37:43 +00:00
|
|
|
|
debug(format("got %d keys, next marker ‘%s’")
|
2016-04-21 15:53:47 +00:00
|
|
|
|
% contents.size() % res.GetNextMarker());
|
|
|
|
|
|
|
|
|
|
for (auto object : contents) {
|
|
|
|
|
auto & key = object.GetKey();
|
2016-04-29 15:34:31 +00:00
|
|
|
|
if (key.size() != 40 || !hasSuffix(key, ".narinfo")) continue;
|
2016-06-01 12:49:12 +00:00
|
|
|
|
paths.insert(storeDir + "/" + key.substr(0, key.size() - 8));
|
2016-04-21 15:53:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
marker = res.GetNextMarker();
|
|
|
|
|
} while (!marker.empty());
|
|
|
|
|
|
|
|
|
|
return paths;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-21 14:02:48 +00:00
|
|
|
|
};
|
|
|
|
|
|
2016-04-29 14:26:16 +00:00
|
|
|
|
static RegisterStoreImplementation regStore([](
|
2016-06-01 12:49:12 +00:00
|
|
|
|
const std::string & uri, const Store::Params & params)
|
2016-04-29 14:26:16 +00:00
|
|
|
|
-> std::shared_ptr<Store>
|
|
|
|
|
{
|
2016-04-21 14:02:48 +00:00
|
|
|
|
if (std::string(uri, 0, 5) != "s3://") return 0;
|
2016-05-04 18:15:41 +00:00
|
|
|
|
auto store = std::make_shared<S3BinaryCacheStoreImpl>(params, std::string(uri, 5));
|
2016-04-21 14:02:48 +00:00
|
|
|
|
store->init();
|
|
|
|
|
return store;
|
|
|
|
|
});
|
|
|
|
|
|
2017-02-14 13:20:00 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2016-04-21 14:02:48 +00:00
|
|
|
|
}
|
2016-05-04 15:16:48 +00:00
|
|
|
|
|
|
|
|
|
#endif
|