Skip to content

Commit

Permalink
Start factoring out the serve protocol for Hydra to share
Browse files Browse the repository at this point in the history
Hydra uses the lock argument differently than Nix, so we un-hard-code
it.

The parent commit is chosen such that this should work for 2.6 and
master alike.
  • Loading branch information
Ericson2314 committed Oct 25, 2023
1 parent a58d7f1 commit a457a8e
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 45 deletions.
48 changes: 3 additions & 45 deletions src/libstore/legacy-ssh-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,45 +40,10 @@ struct LegacySSHStore : public virtual LegacySSHStoreConfig, public virtual Stor
// the documentation
const Setting<int> logFD{(StoreConfig*) this, -1, "log-fd", "file descriptor to which SSH's stderr is connected"};

struct Connection
struct Connection : public ServeProto::BasicClientConnection
{
std::unique_ptr<SSHMaster::Connection> sshConn;
FdSink to;
FdSource from;
ServeProto::Version remoteVersion;
bool good = true;

/**
* Coercion to `ServeProto::ReadConn`. This makes it easy to use the
* factored out serve protocol searlizers with a
* `LegacySSHStore::Connection`.
*
* The serve protocol connection types are unidirectional, unlike
* this type.
*/
operator ServeProto::ReadConn ()
{
return ServeProto::ReadConn {
.from = from,
.version = remoteVersion,
};
}

/*
* Coercion to `ServeProto::WriteConn`. This makes it easy to use the
* factored out serve protocol searlizers with a
* `LegacySSHStore::Connection`.
*
* The serve protocol connection types are unidirectional, unlike
* this type.
*/
operator ServeProto::WriteConn ()
{
return ServeProto::WriteConn {
.to = to,
.version = remoteVersion,
};
}
};

std::string host;
Expand Down Expand Up @@ -402,15 +367,8 @@ struct LegacySSHStore : public virtual LegacySSHStoreConfig, public virtual Stor
SubstituteFlag maybeSubstitute = NoSubstitute) override
{
auto conn(connections->get());

conn->to
<< ServeProto::Command::QueryValidPaths
<< false // lock
<< maybeSubstitute;
ServeProto::write(*this, *conn, paths);
conn->to.flush();

return ServeProto::Serialise<StorePathSet>::read(*this, *conn);
return conn->queryValidPaths(*this,
false, paths, maybeSubstitute);
}

void connect() override
Expand Down
20 changes: 20 additions & 0 deletions src/libstore/serve-protocol-impl.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "serve-protocol-impl.hh"

namespace nix {

StorePathSet ServeProto::BasicClientConnection::queryValidPaths(
const Store & store,
bool lock, const StorePathSet & paths,
SubstituteFlag maybeSubstitute)
{
to
<< ServeProto::Command::QueryValidPaths
<< lock
<< maybeSubstitute;
write(store, *this, paths);
to.flush();

return Serialise<StorePathSet>::read(store, *this);
}

}
45 changes: 45 additions & 0 deletions src/libstore/serve-protocol-impl.hh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "serve-protocol.hh"
#include "length-prefixed-protocol-helper.hh"
#include "store-api.hh"

namespace nix {

Expand Down Expand Up @@ -56,4 +57,48 @@ struct ServeProto::Serialise

/* protocol-specific templates */

struct ServeProto::BasicClientConnection
{
FdSink to;
FdSource from;
ServeProto::Version remoteVersion;

/**
* Coercion to `ServeProto::ReadConn`. This makes it easy to use the
* factored out serve protocol searlizers with a
* `LegacySSHStore::Connection`.
*
* The serve protocol connection types are unidirectional, unlike
* this type.
*/
operator ServeProto::ReadConn ()
{
return ServeProto::ReadConn {
.from = from,
.version = remoteVersion,
};
}

/*
* Coercion to `ServeProto::WriteConn`. This makes it easy to use the
* factored out serve protocol searlizers with a
* `LegacySSHStore::Connection`.
*
* The serve protocol connection types are unidirectional, unlike
* this type.
*/
operator ServeProto::WriteConn ()
{
return ServeProto::WriteConn {
.to = to,
.version = remoteVersion,
};
}

StorePathSet queryValidPaths(
const Store & remoteStore,
bool lock, const StorePathSet & paths,
SubstituteFlag maybeSubstitute);
};

}
7 changes: 7 additions & 0 deletions src/libstore/serve-protocol.hh
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ struct ServeProto
Version version;
};

/**
* Stripped down serialization logic suitable for sharing with Hydra.
*
* @todo remove once Hydra uses Store abstraction consistently.
*/
struct BasicClientConnection;

/**
* Data type for canonical pairs of serialisers for the serve protocol.
*
Expand Down

0 comments on commit a457a8e

Please sign in to comment.