hydra/src/hydra-queue-runner/s3-binary-cache-store.cc

181 lines
5.4 KiB
C++
Raw Normal View History

2016-02-18 16:18:50 +01:00
#include "s3-binary-cache-store.hh"
#include "nar-info.hh"
2016-02-18 16:18:50 +01: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>
#include <aws/s3/model/PutObjectRequest.h>
namespace nix {
struct S3Error : public Error
{
Aws::S3::S3Errors err;
S3Error(Aws::S3::S3Errors err, const FormatOrString & fs)
: Error(fs), err(err) { };
};
2016-02-18 16:18:50 +01:00
/* 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>
2016-02-26 22:40:27 +01:00
R && checkAws(const FormatOrString & fs, Aws::Utils::Outcome<R, E> && outcome)
2016-02-18 16:18:50 +01:00
{
if (!outcome.IsSuccess())
throw S3Error(
outcome.GetError().GetErrorType(),
2016-02-26 22:40:27 +01:00
fs.s + ": " + outcome.GetError().GetMessage());
2016-02-18 16:18:50 +01:00
return outcome.GetResultWithOwnership();
}
2016-02-24 14:04:31 +01:00
S3BinaryCacheStore::S3BinaryCacheStore(std::shared_ptr<Store> localStore,
2016-02-18 16:18:50 +01:00
const Path & secretKeyFile, const Path & publicKeyFile,
const std::string & bucketName)
2016-02-24 14:04:31 +01:00
: BinaryCacheStore(localStore, secretKeyFile, publicKeyFile)
2016-02-18 16:18:50 +01:00
, bucketName(bucketName)
, config(makeConfig())
, client(make_ref<Aws::S3::S3Client>(*config))
{
}
ref<Aws::Client::ClientConfiguration> S3BinaryCacheStore::makeConfig()
{
auto res = make_ref<Aws::Client::ClientConfiguration>();
2016-02-26 20:57:47 +01:00
res->region = Aws::Region::US_EAST_1;
2016-02-18 16:18:50 +01:00
res->requestTimeoutMs = 600 * 1000;
return res;
}
void S3BinaryCacheStore::init()
{
/* Create the bucket if it doesn't already exists. */
// FIXME: HeadBucket would be more appropriate, but doesn't return
// an easily parsed 404 message.
auto res = client->GetBucketLocation(
Aws::S3::Model::GetBucketLocationRequest().WithBucket(bucketName));
if (!res.IsSuccess()) {
if (res.GetError().GetErrorType() != Aws::S3::S3Errors::NO_SUCH_BUCKET)
2016-02-26 22:40:27 +01:00
throw Error(format("AWS error checking bucket %s: %s") % bucketName % res.GetError().GetMessage());
checkAws(format("AWS error creating bucket %s") % bucketName,
client->CreateBucket(
Aws::S3::Model::CreateBucketRequest()
.WithBucket(bucketName)
.WithCreateBucketConfiguration(
Aws::S3::Model::CreateBucketConfiguration()
/* .WithLocationConstraint(
Aws::S3::Model::BucketLocationConstraint::US) */ )));
2016-02-18 16:18:50 +01:00
}
BinaryCacheStore::init();
}
const S3BinaryCacheStore::Stats & S3BinaryCacheStore::getS3Stats()
{
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. */
bool S3BinaryCacheStore::isValidPath(const Path & storePath)
{
try {
readNarInfo(storePath);
return true;
} catch (S3Error & e) {
if (e.err == Aws::S3::S3Errors::NO_SUCH_KEY) return false;
throw;
}
}
2016-02-18 16:18:50 +01:00
bool S3BinaryCacheStore::fileExists(const std::string & path)
{
stats.head++;
2016-02-18 16:18:50 +01:00
auto res = client->HeadObject(
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-03-02 20:06:48 +01:00
throw Error(format("AWS error fetching %s: %s") % path % error.GetMessage());
2016-02-18 16:18:50 +01:00
}
return true;
}
void S3BinaryCacheStore::upsertFile(const std::string & path, const std::string & data)
{
auto request =
Aws::S3::Model::PutObjectRequest()
.WithBucket(bucketName)
.WithKey(path);
auto stream = std::make_shared<std::stringstream>(data);
request.SetBody(stream);
stats.put++;
stats.putBytes += data.size();
2016-02-18 16:18:50 +01:00
auto now1 = std::chrono::steady_clock::now();
2016-02-26 22:40:27 +01:00
auto result = checkAws(format("AWS error uploading %s") % path,
client->PutObject(request));
2016-02-18 16:18:50 +01:00
auto now2 = std::chrono::steady_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(now2 - now1).count();
2016-02-26 21:31:08 +01:00
printMsg(lvlInfo, format("uploaded s3://%1%/%2% (%3% bytes) in %4% ms")
% bucketName % path % data.size() % duration);
stats.putTimeMs += duration;
2016-02-18 16:18:50 +01:00
}
std::string S3BinaryCacheStore::getFile(const std::string & path)
{
auto request =
Aws::S3::Model::GetObjectRequest()
.WithBucket(bucketName)
.WithKey(path);
request.SetResponseStreamFactory([&]() {
return Aws::New<std::stringstream>("STRINGSTREAM");
});
stats.get++;
2016-02-18 16:18:50 +01:00
auto now1 = std::chrono::steady_clock::now();
2016-02-26 22:40:27 +01:00
auto result = checkAws(format("AWS error fetching %s") % path,
client->GetObject(request));
2016-02-18 16:18:50 +01:00
auto now2 = std::chrono::steady_clock::now();
auto res = dynamic_cast<std::stringstream &>(result.GetBody()).str();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(now2 - now1).count();
2016-02-26 22:40:27 +01:00
printMsg(lvlTalkative, format("downloaded s3://%1%/%2% (%3% bytes) in %4% ms")
% bucketName % path % res.size() % duration);
stats.getBytes += res.size();
stats.getTimeMs += duration;
2016-02-18 16:18:50 +01:00
return res;
}
}