Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factor out a LogStore interface #6220

Merged
merged 4 commits into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/libstore/binary-cache-store.hh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "crypto.hh"
#include "store-api.hh"
#include "log-store.hh"

#include "pool.hh"

Expand All @@ -28,7 +29,9 @@ struct BinaryCacheStoreConfig : virtual StoreConfig
"other than -1 which we reserve to indicate Nix defaults should be used"};
};

class BinaryCacheStore : public virtual BinaryCacheStoreConfig, public virtual Store
class BinaryCacheStore : public virtual BinaryCacheStoreConfig,
public virtual Store,
public virtual LogStore
{

private:
Expand Down
6 changes: 6 additions & 0 deletions src/libstore/build/local-derivation-goal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,12 @@ struct RestrictedStore : public virtual RestrictedStoreConfig, public virtual Lo
next->queryMissing(allowed, willBuild, willSubstitute,
unknown, downloadSize, narSize);
}

virtual std::optional<std::string> getBuildLog(const StorePath & path) override
{ return std::nullopt; }

virtual void addBuildLog(const StorePath & path, std::string_view log) override
{ unsupported("addBuildLog"); }
};


Expand Down
11 changes: 7 additions & 4 deletions src/libstore/daemon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#include "worker-protocol.hh"
#include "build-result.hh"
#include "store-api.hh"
#include "store-cast.hh"
#include "gc-store.hh"
#include "log-store.hh"
#include "path-with-outputs.hh"
#include "finally.hh"
#include "archive.hh"
Expand Down Expand Up @@ -645,7 +647,7 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
Path path = absPath(readString(from));

logger->startWork();
auto & gcStore = requireGcStore(*store);
auto & gcStore = require<GcStore>(*store);
gcStore.addIndirectRoot(path);
logger->stopWork();

Expand All @@ -663,7 +665,7 @@ static void performOp(TunnelLogger * logger, ref<Store> store,

case wopFindRoots: {
logger->startWork();
auto & gcStore = requireGcStore(*store);
auto & gcStore = require<GcStore>(*store);
Roots roots = gcStore.findRoots(!trusted);
logger->stopWork();

Expand Down Expand Up @@ -695,7 +697,7 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
logger->startWork();
if (options.ignoreLiveness)
throw Error("you are not allowed to ignore liveness");
auto & gcStore = requireGcStore(*store);
auto & gcStore = require<GcStore>(*store);
gcStore.collectGarbage(options, results);
logger->stopWork();

Expand Down Expand Up @@ -953,11 +955,12 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
logger->startWork();
if (!trusted)
throw Error("you are not privileged to add logs");
auto & logStore = require<LogStore>(*store);
{
FramedSource source(from);
StringSink sink;
source.drainInto(sink);
store->addBuildLog(path, sink.s);
logStore.addBuildLog(path, sink.s);
}
logger->stopWork();
to << 1;
Expand Down
13 changes: 0 additions & 13 deletions src/libstore/gc-store.cc

This file was deleted.

4 changes: 2 additions & 2 deletions src/libstore/gc-store.hh
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ struct GCResults

struct GcStore : public virtual Store
{
inline static std::string operationName = "Garbage collection";

/* Add an indirect root, which is merely a symlink to `path' from
/nix/var/nix/gcroots/auto/<hash of `path'>. `path' is supposed
to be a symlink to a store path. The garbage collector will
Expand All @@ -79,6 +81,4 @@ struct GcStore : public virtual Store
virtual void collectGarbage(const GCOptions & options, GCResults & results) = 0;
};

GcStore & requireGcStore(Store & store);

}
6 changes: 5 additions & 1 deletion src/libstore/local-fs-store.hh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "store-api.hh"
#include "gc-store.hh"
#include "log-store.hh"

namespace nix {

Expand All @@ -24,7 +25,10 @@ struct LocalFSStoreConfig : virtual StoreConfig
"physical path to the Nix store"};
};

class LocalFSStore : public virtual LocalFSStoreConfig, public virtual Store, virtual GcStore
class LocalFSStore : public virtual LocalFSStoreConfig,
public virtual Store,
public virtual GcStore,
public virtual LogStore
{
public:

Expand Down
21 changes: 21 additions & 0 deletions src/libstore/log-store.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include "store-api.hh"


namespace nix {

struct LogStore : public virtual Store
{
inline static std::string operationName = "Build log storage and retrieval";

/* Return the build log of the specified store path, if available,
or null otherwise. */
virtual std::optional<std::string> getBuildLog(const StorePath & path) = 0;

virtual void addBuildLog(const StorePath & path, std::string_view log) = 0;

static LogStore & require(Store & store);
};

}
6 changes: 5 additions & 1 deletion src/libstore/remote-store.hh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "store-api.hh"
#include "gc-store.hh"
#include "log-store.hh"


namespace nix {
Expand All @@ -30,7 +31,10 @@ struct RemoteStoreConfig : virtual StoreConfig

/* FIXME: RemoteStore is a misnomer - should be something like
DaemonStore. */
class RemoteStore : public virtual RemoteStoreConfig, public virtual Store, public virtual GcStore
class RemoteStore : public virtual RemoteStoreConfig,
public virtual Store,
public virtual GcStore,
public virtual LogStore
{
public:

Expand Down
4 changes: 4 additions & 0 deletions src/libstore/ssh-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ class SSHStore : public virtual SSHStoreConfig, public virtual RemoteStore
bool sameMachine() override
{ return false; }

// FIXME extend daemon protocol, move implementation to RemoteStore
std::optional<std::string> getBuildLog(const StorePath & path) override
{ unsupported("getBuildLog"); }

private:

struct Connection : RemoteStore::Connection
Expand Down
8 changes: 0 additions & 8 deletions src/libstore/store-api.hh
Original file line number Diff line number Diff line change
Expand Up @@ -605,14 +605,6 @@ public:
*/
StorePathSet exportReferences(const StorePathSet & storePaths, const StorePathSet & inputPaths);

/* Return the build log of the specified store path, if available,
or null otherwise. */
virtual std::optional<std::string> getBuildLog(const StorePath & path)
{ return std::nullopt; }

virtual void addBuildLog(const StorePath & path, std::string_view log)
{ unsupported("addBuildLog"); }

/* Hack to allow long-running processes like hydra-queue-runner to
occasionally flush their path info cache. */
void clearPathInfoCache()
Expand Down
16 changes: 16 additions & 0 deletions src/libstore/store-cast.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include "store-api.hh"

namespace nix {

template<typename T>
T & require(Store & store)
{
auto * castedStore = dynamic_cast<T *>(&store);
if (!castedStore)
throw UsageError("%s not supported by store '%s'", T::operationName, store.getUri());
return *castedStore;
}

}
3 changes: 2 additions & 1 deletion src/nix-collect-garbage/nix-collect-garbage.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "store-api.hh"
#include "store-cast.hh"
#include "gc-store.hh"
#include "profiles.hh"
#include "shared.hh"
Expand Down Expand Up @@ -81,7 +82,7 @@ static int main_nix_collect_garbage(int argc, char * * argv)
// Run the actual garbage collector.
if (!dryRun) {
auto store = openStore();
auto & gcStore = requireGcStore(*store);
auto & gcStore = require<GcStore>(*store);
options.action = GCOptions::gcDeleteDead;
GCResults results;
PrintFreed freed(true, results);
Expand Down
16 changes: 10 additions & 6 deletions src/nix-store/nix-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#include "dotgraph.hh"
#include "globals.hh"
#include "build-result.hh"
#include "store-cast.hh"
#include "gc-store.hh"
#include "log-store.hh"
#include "local-store.hh"
#include "monitor-fd.hh"
#include "serve-protocol.hh"
Expand Down Expand Up @@ -429,7 +431,7 @@ static void opQuery(Strings opFlags, Strings opArgs)
store->computeFSClosure(
args, referrers, true, settings.gcKeepOutputs, settings.gcKeepDerivations);

auto & gcStore = requireGcStore(*store);
auto & gcStore = require<GcStore>(*store);
Roots roots = gcStore.findRoots(false);
for (auto & [target, links] : roots)
if (referrers.find(target) != referrers.end())
Expand Down Expand Up @@ -474,13 +476,15 @@ static void opReadLog(Strings opFlags, Strings opArgs)
{
if (!opFlags.empty()) throw UsageError("unknown flag");

auto & logStore = require<LogStore>(*store);

RunPager pager;

for (auto & i : opArgs) {
auto path = store->followLinksToStorePath(i);
auto log = store->getBuildLog(path);
auto path = logStore.followLinksToStorePath(i);
auto log = logStore.getBuildLog(path);
if (!log)
throw Error("build log of derivation '%s' is not available", store->printStorePath(path));
throw Error("build log of derivation '%s' is not available", logStore.printStorePath(path));
std::cout << *log;
}
}
Expand Down Expand Up @@ -590,7 +594,7 @@ static void opGC(Strings opFlags, Strings opArgs)

if (!opArgs.empty()) throw UsageError("no arguments expected");

auto & gcStore = requireGcStore(*store);
auto & gcStore = require<GcStore>(*store);

if (printRoots) {
Roots roots = gcStore.findRoots(false);
Expand Down Expand Up @@ -629,7 +633,7 @@ static void opDelete(Strings opFlags, Strings opArgs)
for (auto & i : opArgs)
options.pathsToDelete.insert(store->followLinksToStorePath(i));

auto & gcStore = requireGcStore(*store);
auto & gcStore = require<GcStore>(*store);

GCResults results;
PrintFreed freed(true, results);
Expand Down
14 changes: 11 additions & 3 deletions src/nix/log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "common-args.hh"
#include "shared.hh"
#include "store-api.hh"
#include "log-store.hh"
#include "progress-bar.hh"

using namespace nix;
Expand Down Expand Up @@ -34,17 +35,24 @@ struct CmdLog : InstallableCommand

RunPager pager;
for (auto & sub : subs) {
auto * logSubP = dynamic_cast<LogStore *>(&*sub);
if (!logSubP) {
printInfo("Skipped '%s' which does not support retrieving build logs", sub->getUri());
continue;
}
auto & logSub = *logSubP;

auto log = std::visit(overloaded {
[&](const DerivedPath::Opaque & bo) {
return sub->getBuildLog(bo.path);
return logSub.getBuildLog(bo.path);
},
[&](const DerivedPath::Built & bfd) {
return sub->getBuildLog(bfd.drvPath);
return logSub.getBuildLog(bfd.drvPath);
},
}, b.raw());
if (!log) continue;
stopProgressBar();
printInfo("got build log for '%s' from '%s'", installable->what(), sub->getUri());
printInfo("got build log for '%s' from '%s'", installable->what(), logSub.getUri());
std::cout << *log;
return;
}
Expand Down
12 changes: 10 additions & 2 deletions src/nix/repl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extern "C" {
#include "eval-inline.hh"
#include "attr-path.hh"
#include "store-api.hh"
#include "log-store.hh"
#include "common-eval-args.hh"
#include "get-drvs.hh"
#include "derivations.hh"
Expand Down Expand Up @@ -526,9 +527,16 @@ bool NixRepl::processLine(std::string line)
bool foundLog = false;
RunPager pager;
for (auto & sub : subs) {
auto log = sub->getBuildLog(drvPath);
auto * logSubP = dynamic_cast<LogStore *>(&*sub);
if (!logSubP) {
printInfo("Skipped '%s' which does not support retrieving build logs", sub->getUri());
continue;
}
auto & logSub = *logSubP;

auto log = logSub.getBuildLog(drvPath);
if (log) {
printInfo("got build log for '%s' from '%s'", drvPathRaw, sub->getUri());
printInfo("got build log for '%s' from '%s'", drvPathRaw, logSub.getUri());
logger->writeToStdout(*log);
foundLog = true;
break;
Expand Down
9 changes: 7 additions & 2 deletions src/nix/store-copy-log.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "command.hh"
#include "shared.hh"
#include "store-api.hh"
#include "store-cast.hh"
#include "log-store.hh"
#include "sync.hh"
#include "thread-pool.hh"

Expand All @@ -26,7 +28,10 @@ struct CmdCopyLog : virtual CopyCommand, virtual InstallablesCommand

void run(ref<Store> srcStore) override
{
auto & srcLogStore = require<LogStore>(*srcStore);

auto dstStore = getDstStore();
auto & dstLogStore = require<LogStore>(*dstStore);

StorePathSet drvPaths;

Expand All @@ -35,8 +40,8 @@ struct CmdCopyLog : virtual CopyCommand, virtual InstallablesCommand
drvPaths.insert(drvPath);

for (auto & drvPath : drvPaths) {
if (auto log = srcStore->getBuildLog(drvPath))
dstStore->addBuildLog(drvPath, *log);
if (auto log = srcLogStore.getBuildLog(drvPath))
dstLogStore.addBuildLog(drvPath, *log);
else
throw Error("build log for '%s' is not available", srcStore->printStorePath(drvPath));
}
Expand Down
3 changes: 2 additions & 1 deletion src/nix/store-delete.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "common-args.hh"
#include "shared.hh"
#include "store-api.hh"
#include "store-cast.hh"
#include "gc-store.hh"

using namespace nix;
Expand Down Expand Up @@ -33,7 +34,7 @@ struct CmdStoreDelete : StorePathsCommand

void run(ref<Store> store, std::vector<StorePath> && storePaths) override
{
auto & gcStore = requireGcStore(*store);
auto & gcStore = require<GcStore>(*store);

for (auto & path : storePaths)
options.pathsToDelete.insert(path);
Expand Down
Loading