mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-30 01:26:15 +02:00
* Use optimistic profile locking for nix-env operations like `-i' and
`-u'. Instead of acquiring an exclusive lock on the profile for the entire duration of the operation, we just perform the operation optimistically (without an exclusive lock), and check at the end whether the profile changed while we were busy (i.e., the symlink target changed). If so, the operation is restarted. Restarting is generally cheap, since the build results are still in the Nix store. Most of the time, only the user environment has to be rebuilt.
This commit is contained in:
parent
a87b5256e2
commit
339c142009
1 changed files with 167 additions and 142 deletions
|
@ -215,8 +215,34 @@ static DrvInfos queryInstalled(EvalState & state, const Path & userEnv)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void createUserEnv(EvalState & state, const DrvInfos & elems,
|
/* Ensure exclusive access to a profile. Any command that modifies
|
||||||
const Path & profile, bool keepDerivations)
|
the profile first acquires this lock. */
|
||||||
|
static void lockProfile(PathLocks & lock, const Path & profile)
|
||||||
|
{
|
||||||
|
lock.lockPaths(singleton<PathSet>(profile),
|
||||||
|
(format("waiting for lock on profile `%1%'") % profile).str());
|
||||||
|
lock.setDeletion(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Optimistic locking is used by long-running operations like `nix-env
|
||||||
|
-i'. Instead of acquiring the exclusive lock for the entire
|
||||||
|
duration of the operation, we just perform the operation
|
||||||
|
optimistically (without an exclusive lock), and check at the end
|
||||||
|
whether the profile changed while we were busy (i.e., the symlink
|
||||||
|
target changed). If so, the operation is restarted. Restarting is
|
||||||
|
generally cheap, since the build results are still in the Nix
|
||||||
|
store. Most of the time, only the user environment has to be
|
||||||
|
rebuilt. */
|
||||||
|
static string optimisticLockProfile(const Path & profile)
|
||||||
|
{
|
||||||
|
return pathExists(profile) ? readLink(profile) : "";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static bool createUserEnv(EvalState & state, const DrvInfos & elems,
|
||||||
|
const Path & profile, bool keepDerivations,
|
||||||
|
const string & lockToken)
|
||||||
{
|
{
|
||||||
/* Build the components in the user environment, if they don't
|
/* Build the components in the user environment, if they don't
|
||||||
exist already. */
|
exist already. */
|
||||||
|
@ -305,9 +331,20 @@ static void createUserEnv(EvalState & state, const DrvInfos & elems,
|
||||||
store->buildDerivations(singleton<PathSet>(topLevelDrv.queryDrvPath(state)));
|
store->buildDerivations(singleton<PathSet>(topLevelDrv.queryDrvPath(state)));
|
||||||
|
|
||||||
/* Switch the current user environment to the output path. */
|
/* Switch the current user environment to the output path. */
|
||||||
|
PathLocks lock;
|
||||||
|
lockProfile(lock, profile);
|
||||||
|
|
||||||
|
Path lockTokenCur = optimisticLockProfile(profile);
|
||||||
|
if (lockToken != lockTokenCur) {
|
||||||
|
printMsg(lvlError, format("profile `%1%' changed while we were busy; restarting") % profile);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
debug(format("switching to new user environment"));
|
debug(format("switching to new user environment"));
|
||||||
Path generation = createGeneration(profile, topLevelDrv.queryOutPath(state));
|
Path generation = createGeneration(profile, topLevelDrv.queryOutPath(state));
|
||||||
switchLink(profile, generation);
|
switchLink(profile, generation);
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -544,14 +581,6 @@ static void printMissing(EvalState & state, const DrvInfos & elems)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void lockProfile(PathLocks & lock, const Path & profile)
|
|
||||||
{
|
|
||||||
lock.lockPaths(singleton<PathSet>(profile),
|
|
||||||
(format("waiting for lock on profile `%1%'") % profile).str());
|
|
||||||
lock.setDeletion(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void installDerivations(Globals & globals,
|
static void installDerivations(Globals & globals,
|
||||||
const Strings & args, const Path & profile)
|
const Strings & args, const Path & profile)
|
||||||
{
|
{
|
||||||
|
@ -574,14 +603,14 @@ static void installDerivations(Globals & globals,
|
||||||
|
|
||||||
/* Add in the already installed derivations, unless they have the
|
/* Add in the already installed derivations, unless they have the
|
||||||
same name as a to-be-installed element. */
|
same name as a to-be-installed element. */
|
||||||
PathLocks lock;
|
|
||||||
lockProfile(lock, profile);
|
while (true) {
|
||||||
|
string lockToken = optimisticLockProfile(profile);
|
||||||
|
|
||||||
DrvInfos installedElems = queryInstalled(globals.state, profile);
|
DrvInfos installedElems = queryInstalled(globals.state, profile);
|
||||||
|
|
||||||
DrvInfos allElems(newElems);
|
DrvInfos allElems(newElems);
|
||||||
for (DrvInfos::iterator i = installedElems.begin();
|
foreach (DrvInfos::iterator, i, installedElems) {
|
||||||
i != installedElems.end(); ++i)
|
|
||||||
{
|
|
||||||
DrvName drvName(i->name);
|
DrvName drvName(i->name);
|
||||||
MetaInfo meta = i->queryMetaInfo(globals.state);
|
MetaInfo meta = i->queryMetaInfo(globals.state);
|
||||||
if (!globals.preserveInstalled &&
|
if (!globals.preserveInstalled &&
|
||||||
|
@ -593,16 +622,16 @@ static void installDerivations(Globals & globals,
|
||||||
allElems.push_back(*i);
|
allElems.push_back(*i);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (DrvInfos::iterator i = newElems.begin(); i != newElems.end(); ++i)
|
foreach (DrvInfos::iterator, i, newElems)
|
||||||
printMsg(lvlInfo,
|
printMsg(lvlInfo, format("installing `%1%'") % i->name);
|
||||||
format("installing `%1%'") % i->name);
|
|
||||||
|
|
||||||
printMissing(globals.state, newElems);
|
printMissing(globals.state, newElems);
|
||||||
|
|
||||||
if (globals.dryRun) return;
|
if (globals.dryRun) return;
|
||||||
|
|
||||||
createUserEnv(globals.state, allElems,
|
if (createUserEnv(globals.state, allElems,
|
||||||
profile, globals.keepDerivations);
|
profile, globals.keepDerivations, lockToken)) break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -634,9 +663,9 @@ static void upgradeDerivations(Globals & globals,
|
||||||
for a derivation in the input Nix expression that has the same
|
for a derivation in the input Nix expression that has the same
|
||||||
name and a higher version number. */
|
name and a higher version number. */
|
||||||
|
|
||||||
/* Load the currently installed derivations. */
|
while (true) {
|
||||||
PathLocks lock;
|
string lockToken = optimisticLockProfile(globals.profile);
|
||||||
lockProfile(lock, globals.profile);
|
|
||||||
DrvInfos installedElems = queryInstalled(globals.state, globals.profile);
|
DrvInfos installedElems = queryInstalled(globals.state, globals.profile);
|
||||||
|
|
||||||
/* Fetch all derivations from the input file. */
|
/* Fetch all derivations from the input file. */
|
||||||
|
@ -645,9 +674,7 @@ static void upgradeDerivations(Globals & globals,
|
||||||
|
|
||||||
/* Go through all installed derivations. */
|
/* Go through all installed derivations. */
|
||||||
DrvInfos newElems;
|
DrvInfos newElems;
|
||||||
for (DrvInfos::iterator i = installedElems.begin();
|
foreach (DrvInfos::iterator, i, installedElems) {
|
||||||
i != installedElems.end(); ++i)
|
|
||||||
{
|
|
||||||
DrvName drvName(i->name);
|
DrvName drvName(i->name);
|
||||||
|
|
||||||
MetaInfo meta = i->queryMetaInfo(globals.state);
|
MetaInfo meta = i->queryMetaInfo(globals.state);
|
||||||
|
@ -656,16 +683,15 @@ static void upgradeDerivations(Globals & globals,
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Find the derivation in the input Nix expression with the
|
/* Find the derivation in the input Nix expression with
|
||||||
same name that satisfies the version constraints specified
|
the same name that satisfies the version constraints
|
||||||
by upgradeType. If there are multiple matches, take the
|
specified by upgradeType. If there are multiple
|
||||||
one with the highest priority. If there are still multiple
|
matches, take the one with the highest priority. If
|
||||||
matches, take the one with the highest version. */
|
there are still multiple matches, take the one with the
|
||||||
|
highest version. */
|
||||||
DrvInfos::iterator bestElem = availElems.end();
|
DrvInfos::iterator bestElem = availElems.end();
|
||||||
DrvName bestName;
|
DrvName bestName;
|
||||||
for (DrvInfos::iterator j = availElems.begin();
|
foreach (DrvInfos::iterator, j, availElems) {
|
||||||
j != availElems.end(); ++j)
|
|
||||||
{
|
|
||||||
DrvName newName(j->name);
|
DrvName newName(j->name);
|
||||||
if (newName.name == drvName.name) {
|
if (newName.name == drvName.name) {
|
||||||
int d = comparePriorities(globals.state, *i, *j);
|
int d = comparePriorities(globals.state, *i, *j);
|
||||||
|
@ -703,8 +729,9 @@ static void upgradeDerivations(Globals & globals,
|
||||||
|
|
||||||
if (globals.dryRun) return;
|
if (globals.dryRun) return;
|
||||||
|
|
||||||
createUserEnv(globals.state, newElems,
|
if (createUserEnv(globals.state, newElems,
|
||||||
globals.profile, globals.keepDerivations);
|
globals.profile, globals.keepDerivations, lockToken)) break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -748,18 +775,15 @@ static void opSetFlag(Globals & globals,
|
||||||
string flagValue = *arg++;
|
string flagValue = *arg++;
|
||||||
DrvNames selectors = drvNamesFromArgs(Strings(arg, opArgs.end()));
|
DrvNames selectors = drvNamesFromArgs(Strings(arg, opArgs.end()));
|
||||||
|
|
||||||
/* Load the currently installed derivations. */
|
while (true) {
|
||||||
PathLocks lock;
|
string lockToken = optimisticLockProfile(globals.profile);
|
||||||
lockProfile(lock, globals.profile);
|
|
||||||
DrvInfos installedElems = queryInstalled(globals.state, globals.profile);
|
DrvInfos installedElems = queryInstalled(globals.state, globals.profile);
|
||||||
|
|
||||||
/* Update all matching derivations. */
|
/* Update all matching derivations. */
|
||||||
for (DrvInfos::iterator i = installedElems.begin();
|
foreach (DrvInfos::iterator, i, installedElems) {
|
||||||
i != installedElems.end(); ++i)
|
|
||||||
{
|
|
||||||
DrvName drvName(i->name);
|
DrvName drvName(i->name);
|
||||||
for (DrvNames::iterator j = selectors.begin();
|
foreach (DrvNames::iterator, j, selectors)
|
||||||
j != selectors.end(); ++j)
|
|
||||||
if (j->matches(drvName)) {
|
if (j->matches(drvName)) {
|
||||||
printMsg(lvlInfo,
|
printMsg(lvlInfo,
|
||||||
format("setting flag on `%1%'") % i->name);
|
format("setting flag on `%1%'") % i->name);
|
||||||
|
@ -769,8 +793,9 @@ static void opSetFlag(Globals & globals,
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Write the new user environment. */
|
/* Write the new user environment. */
|
||||||
createUserEnv(globals.state, installedElems,
|
if (createUserEnv(globals.state, installedElems,
|
||||||
globals.profile, globals.keepDerivations);
|
globals.profile, globals.keepDerivations, lockToken)) break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -812,19 +837,18 @@ static void opSet(Globals & globals,
|
||||||
static void uninstallDerivations(Globals & globals, Strings & selectors,
|
static void uninstallDerivations(Globals & globals, Strings & selectors,
|
||||||
Path & profile)
|
Path & profile)
|
||||||
{
|
{
|
||||||
PathLocks lock;
|
while (true) {
|
||||||
lockProfile(lock, profile);
|
string lockToken = optimisticLockProfile(profile);
|
||||||
|
|
||||||
DrvInfos installedElems = queryInstalled(globals.state, profile);
|
DrvInfos installedElems = queryInstalled(globals.state, profile);
|
||||||
DrvInfos newElems;
|
DrvInfos newElems;
|
||||||
|
|
||||||
for (DrvInfos::iterator i = installedElems.begin();
|
foreach (DrvInfos::iterator, i, installedElems) {
|
||||||
i != installedElems.end(); ++i)
|
|
||||||
{
|
|
||||||
DrvName drvName(i->name);
|
DrvName drvName(i->name);
|
||||||
bool found = false;
|
bool found = false;
|
||||||
for (Strings::iterator j = selectors.begin(); j != selectors.end(); ++j)
|
foreach (Strings::iterator, j, selectors)
|
||||||
/* !!! the repeated calls to followLinksToStorePath() are
|
/* !!! the repeated calls to followLinksToStorePath()
|
||||||
expensive, should pre-compute them. */
|
are expensive, should pre-compute them. */
|
||||||
if ((isPath(*j) && i->queryOutPath(globals.state) == followLinksToStorePath(*j))
|
if ((isPath(*j) && i->queryOutPath(globals.state) == followLinksToStorePath(*j))
|
||||||
|| DrvName(*j).matches(drvName))
|
|| DrvName(*j).matches(drvName))
|
||||||
{
|
{
|
||||||
|
@ -837,8 +861,9 @@ static void uninstallDerivations(Globals & globals, Strings & selectors,
|
||||||
|
|
||||||
if (globals.dryRun) return;
|
if (globals.dryRun) return;
|
||||||
|
|
||||||
createUserEnv(globals.state, newElems,
|
if (createUserEnv(globals.state, newElems,
|
||||||
profile, globals.keepDerivations);
|
profile, globals.keepDerivations, lockToken)) break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue