* 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,
|
||||
const Path & profile, bool keepDerivations)
|
||||
/* Ensure exclusive access to a profile. Any command that modifies
|
||||
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
|
||||
exist already. */
|
||||
|
@ -305,9 +331,20 @@ static void createUserEnv(EvalState & state, const DrvInfos & elems,
|
|||
store->buildDerivations(singleton<PathSet>(topLevelDrv.queryDrvPath(state)));
|
||||
|
||||
/* 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"));
|
||||
Path generation = createGeneration(profile, topLevelDrv.queryOutPath(state));
|
||||
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,
|
||||
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
|
||||
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 allElems(newElems);
|
||||
for (DrvInfos::iterator i = installedElems.begin();
|
||||
i != installedElems.end(); ++i)
|
||||
{
|
||||
foreach (DrvInfos::iterator, i, installedElems) {
|
||||
DrvName drvName(i->name);
|
||||
MetaInfo meta = i->queryMetaInfo(globals.state);
|
||||
if (!globals.preserveInstalled &&
|
||||
|
@ -593,16 +622,16 @@ static void installDerivations(Globals & globals,
|
|||
allElems.push_back(*i);
|
||||
}
|
||||
|
||||
for (DrvInfos::iterator i = newElems.begin(); i != newElems.end(); ++i)
|
||||
printMsg(lvlInfo,
|
||||
format("installing `%1%'") % i->name);
|
||||
foreach (DrvInfos::iterator, i, newElems)
|
||||
printMsg(lvlInfo, format("installing `%1%'") % i->name);
|
||||
|
||||
printMissing(globals.state, newElems);
|
||||
|
||||
if (globals.dryRun) return;
|
||||
|
||||
createUserEnv(globals.state, allElems,
|
||||
profile, globals.keepDerivations);
|
||||
if (createUserEnv(globals.state, allElems,
|
||||
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
|
||||
name and a higher version number. */
|
||||
|
||||
/* Load the currently installed derivations. */
|
||||
PathLocks lock;
|
||||
lockProfile(lock, globals.profile);
|
||||
while (true) {
|
||||
string lockToken = optimisticLockProfile(globals.profile);
|
||||
|
||||
DrvInfos installedElems = queryInstalled(globals.state, globals.profile);
|
||||
|
||||
/* Fetch all derivations from the input file. */
|
||||
|
@ -645,9 +674,7 @@ static void upgradeDerivations(Globals & globals,
|
|||
|
||||
/* Go through all installed derivations. */
|
||||
DrvInfos newElems;
|
||||
for (DrvInfos::iterator i = installedElems.begin();
|
||||
i != installedElems.end(); ++i)
|
||||
{
|
||||
foreach (DrvInfos::iterator, i, installedElems) {
|
||||
DrvName drvName(i->name);
|
||||
|
||||
MetaInfo meta = i->queryMetaInfo(globals.state);
|
||||
|
@ -656,16 +683,15 @@ static void upgradeDerivations(Globals & globals,
|
|||
continue;
|
||||
}
|
||||
|
||||
/* Find the derivation in the input Nix expression with the
|
||||
same name that satisfies the version constraints specified
|
||||
by upgradeType. If there are multiple matches, take the
|
||||
one with the highest priority. If there are still multiple
|
||||
matches, take the one with the highest version. */
|
||||
/* Find the derivation in the input Nix expression with
|
||||
the same name that satisfies the version constraints
|
||||
specified by upgradeType. If there are multiple
|
||||
matches, take the one with the highest priority. If
|
||||
there are still multiple matches, take the one with the
|
||||
highest version. */
|
||||
DrvInfos::iterator bestElem = availElems.end();
|
||||
DrvName bestName;
|
||||
for (DrvInfos::iterator j = availElems.begin();
|
||||
j != availElems.end(); ++j)
|
||||
{
|
||||
foreach (DrvInfos::iterator, j, availElems) {
|
||||
DrvName newName(j->name);
|
||||
if (newName.name == drvName.name) {
|
||||
int d = comparePriorities(globals.state, *i, *j);
|
||||
|
@ -703,8 +729,9 @@ static void upgradeDerivations(Globals & globals,
|
|||
|
||||
if (globals.dryRun) return;
|
||||
|
||||
createUserEnv(globals.state, newElems,
|
||||
globals.profile, globals.keepDerivations);
|
||||
if (createUserEnv(globals.state, newElems,
|
||||
globals.profile, globals.keepDerivations, lockToken)) break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -748,18 +775,15 @@ static void opSetFlag(Globals & globals,
|
|||
string flagValue = *arg++;
|
||||
DrvNames selectors = drvNamesFromArgs(Strings(arg, opArgs.end()));
|
||||
|
||||
/* Load the currently installed derivations. */
|
||||
PathLocks lock;
|
||||
lockProfile(lock, globals.profile);
|
||||
while (true) {
|
||||
string lockToken = optimisticLockProfile(globals.profile);
|
||||
|
||||
DrvInfos installedElems = queryInstalled(globals.state, globals.profile);
|
||||
|
||||
/* Update all matching derivations. */
|
||||
for (DrvInfos::iterator i = installedElems.begin();
|
||||
i != installedElems.end(); ++i)
|
||||
{
|
||||
foreach (DrvInfos::iterator, i, installedElems) {
|
||||
DrvName drvName(i->name);
|
||||
for (DrvNames::iterator j = selectors.begin();
|
||||
j != selectors.end(); ++j)
|
||||
foreach (DrvNames::iterator, j, selectors)
|
||||
if (j->matches(drvName)) {
|
||||
printMsg(lvlInfo,
|
||||
format("setting flag on `%1%'") % i->name);
|
||||
|
@ -769,8 +793,9 @@ static void opSetFlag(Globals & globals,
|
|||
}
|
||||
|
||||
/* Write the new user environment. */
|
||||
createUserEnv(globals.state, installedElems,
|
||||
globals.profile, globals.keepDerivations);
|
||||
if (createUserEnv(globals.state, installedElems,
|
||||
globals.profile, globals.keepDerivations, lockToken)) break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -812,19 +837,18 @@ static void opSet(Globals & globals,
|
|||
static void uninstallDerivations(Globals & globals, Strings & selectors,
|
||||
Path & profile)
|
||||
{
|
||||
PathLocks lock;
|
||||
lockProfile(lock, profile);
|
||||
while (true) {
|
||||
string lockToken = optimisticLockProfile(profile);
|
||||
|
||||
DrvInfos installedElems = queryInstalled(globals.state, profile);
|
||||
DrvInfos newElems;
|
||||
|
||||
for (DrvInfos::iterator i = installedElems.begin();
|
||||
i != installedElems.end(); ++i)
|
||||
{
|
||||
foreach (DrvInfos::iterator, i, installedElems) {
|
||||
DrvName drvName(i->name);
|
||||
bool found = false;
|
||||
for (Strings::iterator j = selectors.begin(); j != selectors.end(); ++j)
|
||||
/* !!! the repeated calls to followLinksToStorePath() are
|
||||
expensive, should pre-compute them. */
|
||||
foreach (Strings::iterator, j, selectors)
|
||||
/* !!! the repeated calls to followLinksToStorePath()
|
||||
are expensive, should pre-compute them. */
|
||||
if ((isPath(*j) && i->queryOutPath(globals.state) == followLinksToStorePath(*j))
|
||||
|| DrvName(*j).matches(drvName))
|
||||
{
|
||||
|
@ -837,8 +861,9 @@ static void uninstallDerivations(Globals & globals, Strings & selectors,
|
|||
|
||||
if (globals.dryRun) return;
|
||||
|
||||
createUserEnv(globals.state, newElems,
|
||||
profile, globals.keepDerivations);
|
||||
if (createUserEnv(globals.state, newElems,
|
||||
profile, globals.keepDerivations, lockToken)) break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue