Use a single BinaryCacheStore for all threads

This will make it easier to do caching / keep stats. Also, we won't
have S3Client's connection pooling if we create multiple S3Client
instances.
This commit is contained in:
Eelco Dolstra 2016-02-18 17:31:19 +01:00
parent 00a7be13a2
commit dc4a00347d
8 changed files with 39 additions and 23 deletions

View file

@ -11,9 +11,9 @@
namespace nix { namespace nix {
BinaryCacheStore::BinaryCacheStore(ref<Store> localStore, BinaryCacheStore::BinaryCacheStore(const StoreFactory & storeFactory,
const Path & secretKeyFile, const Path & publicKeyFile) const Path & secretKeyFile, const Path & publicKeyFile)
: localStore(localStore) : storeFactory(storeFactory)
{ {
if (secretKeyFile != "") if (secretKeyFile != "")
secretKey = std::unique_ptr<SecretKey>(new SecretKey(readFile(secretKeyFile))); secretKey = std::unique_ptr<SecretKey>(new SecretKey(readFile(secretKeyFile)));
@ -192,6 +192,8 @@ void BinaryCacheStore::querySubstitutablePathInfos(const PathSet & paths,
{ {
PathSet left; PathSet left;
auto localStore = storeFactory();
for (auto & storePath : paths) { for (auto & storePath : paths) {
if (!localStore->isValidPath(storePath)) { if (!localStore->isValidPath(storePath)) {
left.insert(storePath); left.insert(storePath);
@ -210,6 +212,8 @@ void BinaryCacheStore::querySubstitutablePathInfos(const PathSet & paths,
void BinaryCacheStore::buildPaths(const PathSet & paths, BuildMode buildMode) void BinaryCacheStore::buildPaths(const PathSet & paths, BuildMode buildMode)
{ {
auto localStore = storeFactory();
for (auto & storePath : paths) { for (auto & storePath : paths) {
assert(!isDerivation(storePath)); assert(!isDerivation(storePath));

View file

@ -7,18 +7,23 @@ namespace nix {
struct NarInfo; struct NarInfo;
/* While BinaryCacheStore is thread-safe, LocalStore and RemoteStore
aren't. Until they are, use a factory to produce a thread-local
local store. */
typedef std::function<ref<Store>()> StoreFactory;
class BinaryCacheStore : public Store class BinaryCacheStore : public Store
{ {
private: private:
ref<Store> localStore;
std::unique_ptr<SecretKey> secretKey; std::unique_ptr<SecretKey> secretKey;
std::unique_ptr<PublicKeys> publicKeys; std::unique_ptr<PublicKeys> publicKeys;
StoreFactory storeFactory;
protected: protected:
BinaryCacheStore(ref<Store> localStore, BinaryCacheStore(const StoreFactory & storeFactory,
const Path & secretKeyFile, const Path & publicKeyFile); const Path & secretKeyFile, const Path & publicKeyFile);
virtual bool fileExists(const std::string & path) = 0; virtual bool fileExists(const std::string & path) = 0;

View file

@ -23,6 +23,21 @@ State::State()
if (hydraData == "") throw Error("$HYDRA_DATA must be set"); if (hydraData == "") throw Error("$HYDRA_DATA must be set");
logDir = canonPath(hydraData + "/build-logs"); logDir = canonPath(hydraData + "/build-logs");
#if 0
auto store = make_ref<LocalBinaryCacheStore>(getLocalStore(),
"/home/eelco/Misc/Keys/test.nixos.org/secret",
"/home/eelco/Misc/Keys/test.nixos.org/public",
"/tmp/binary-cache");
#endif
auto store = std::make_shared<S3BinaryCacheStore>(
[]() { return openStore(); },
"/home/eelco/Misc/Keys/test.nixos.org/secret",
"/home/eelco/Misc/Keys/test.nixos.org/public",
"nix-test-cache-3");;
store->init();
_destStore = store;
} }
@ -34,18 +49,7 @@ ref<Store> State::getLocalStore()
ref<Store> State::getDestStore() ref<Store> State::getDestStore()
{ {
#if 0 return ref<Store>(_destStore);
auto store = make_ref<LocalBinaryCacheStore>(getLocalStore(),
"/home/eelco/Misc/Keys/test.nixos.org/secret",
"/home/eelco/Misc/Keys/test.nixos.org/public",
"/tmp/binary-cache");
#endif
auto store = make_ref<S3BinaryCacheStore>(getLocalStore(),
"/home/eelco/Misc/Keys/test.nixos.org/secret",
"/home/eelco/Misc/Keys/test.nixos.org/public",
"nix-test-cache-3");
store->init();
return store;
} }

View file

@ -2,10 +2,10 @@
namespace nix { namespace nix {
LocalBinaryCacheStore::LocalBinaryCacheStore(ref<Store> localStore, LocalBinaryCacheStore::LocalBinaryCacheStore(const StoreFactory & storeFactory,
const Path & secretKeyFile, const Path & publicKeyFile, const Path & secretKeyFile, const Path & publicKeyFile,
const Path & binaryCacheDir) const Path & binaryCacheDir)
: BinaryCacheStore(localStore, secretKeyFile, publicKeyFile) : BinaryCacheStore(storeFactory, secretKeyFile, publicKeyFile)
, binaryCacheDir(binaryCacheDir) , binaryCacheDir(binaryCacheDir)
{ {
} }

View file

@ -12,7 +12,7 @@ private:
public: public:
LocalBinaryCacheStore(ref<Store> localStore, LocalBinaryCacheStore(const StoreFactory & storeFactory,
const Path & secretKeyFile, const Path & publicKeyFile, const Path & secretKeyFile, const Path & publicKeyFile,
const Path & binaryCacheDir); const Path & binaryCacheDir);

View file

@ -20,10 +20,10 @@ R && checkAws(Aws::Utils::Outcome<R, E> && outcome)
return outcome.GetResultWithOwnership(); return outcome.GetResultWithOwnership();
} }
S3BinaryCacheStore::S3BinaryCacheStore(ref<Store> localStore, S3BinaryCacheStore::S3BinaryCacheStore(const StoreFactory & storeFactory,
const Path & secretKeyFile, const Path & publicKeyFile, const Path & secretKeyFile, const Path & publicKeyFile,
const std::string & bucketName) const std::string & bucketName)
: BinaryCacheStore(localStore, secretKeyFile, publicKeyFile) : BinaryCacheStore(storeFactory, secretKeyFile, publicKeyFile)
, bucketName(bucketName) , bucketName(bucketName)
, config(makeConfig()) , config(makeConfig())
, client(make_ref<Aws::S3::S3Client>(*config)) , client(make_ref<Aws::S3::S3Client>(*config))

View file

@ -18,7 +18,7 @@ private:
public: public:
S3BinaryCacheStore(ref<Store> localStore, S3BinaryCacheStore(const StoreFactory & storeFactory,
const Path & secretKeyFile, const Path & publicKeyFile, const Path & secretKeyFile, const Path & publicKeyFile,
const std::string & bucketName); const std::string & bucketName);

View file

@ -346,6 +346,9 @@ private:
std::atomic<time_t> lastDispatcherCheck{0}; std::atomic<time_t> lastDispatcherCheck{0};
/* Destination store. */
std::shared_ptr<nix::Store> _destStore;
public: public:
State(); State();