Retry all SQLite operations

To deal with SQLITE_PROTOCOL, we also need to retry read-only
operations.
This commit is contained in:
Eelco Dolstra 2013-10-16 15:58:20 +02:00
parent ff02f5336c
commit a737f51fd9
2 changed files with 168 additions and 127 deletions

View file

@ -692,27 +692,32 @@ void LocalStore::addReference(unsigned long long referrer, unsigned long long re
void LocalStore::registerFailedPath(const Path & path) void LocalStore::registerFailedPath(const Path & path)
{ {
retry_sqlite {
SQLiteStmtUse use(stmtRegisterFailedPath); SQLiteStmtUse use(stmtRegisterFailedPath);
stmtRegisterFailedPath.bind(path); stmtRegisterFailedPath.bind(path);
stmtRegisterFailedPath.bind(time(0)); stmtRegisterFailedPath.bind(time(0));
if (sqlite3_step(stmtRegisterFailedPath) != SQLITE_DONE) if (sqlite3_step(stmtRegisterFailedPath) != SQLITE_DONE)
throwSQLiteError(db, format("registering failed path `%1%'") % path); throwSQLiteError(db, format("registering failed path `%1%'") % path);
} end_retry_sqlite;
} }
bool LocalStore::hasPathFailed(const Path & path) bool LocalStore::hasPathFailed(const Path & path)
{ {
retry_sqlite {
SQLiteStmtUse use(stmtHasPathFailed); SQLiteStmtUse use(stmtHasPathFailed);
stmtHasPathFailed.bind(path); stmtHasPathFailed.bind(path);
int res = sqlite3_step(stmtHasPathFailed); int res = sqlite3_step(stmtHasPathFailed);
if (res != SQLITE_DONE && res != SQLITE_ROW) if (res != SQLITE_DONE && res != SQLITE_ROW)
throwSQLiteError(db, "querying whether path failed"); throwSQLiteError(db, "querying whether path failed");
return res == SQLITE_ROW; return res == SQLITE_ROW;
} end_retry_sqlite;
} }
PathSet LocalStore::queryFailedPaths() PathSet LocalStore::queryFailedPaths()
{ {
retry_sqlite {
SQLiteStmtUse use(stmtQueryFailedPaths); SQLiteStmtUse use(stmtQueryFailedPaths);
PathSet res; PathSet res;
@ -727,11 +732,13 @@ PathSet LocalStore::queryFailedPaths()
throwSQLiteError(db, "error querying failed paths"); throwSQLiteError(db, "error querying failed paths");
return res; return res;
} end_retry_sqlite;
} }
void LocalStore::clearFailedPaths(const PathSet & paths) void LocalStore::clearFailedPaths(const PathSet & paths)
{ {
retry_sqlite {
SQLiteTxn txn(db); SQLiteTxn txn(db);
foreach (PathSet::const_iterator, i, paths) { foreach (PathSet::const_iterator, i, paths) {
@ -742,6 +749,7 @@ void LocalStore::clearFailedPaths(const PathSet & paths)
} }
txn.commit(); txn.commit();
} end_retry_sqlite;
} }
@ -766,6 +774,8 @@ ValidPathInfo LocalStore::queryPathInfo(const Path & path)
assertStorePath(path); assertStorePath(path);
retry_sqlite {
/* Get the path info. */ /* Get the path info. */
SQLiteStmtUse use1(stmtQueryPathInfo); SQLiteStmtUse use1(stmtQueryPathInfo);
@ -804,6 +814,7 @@ ValidPathInfo LocalStore::queryPathInfo(const Path & path)
throwSQLiteError(db, format("error getting references of `%1%'") % path); throwSQLiteError(db, format("error getting references of `%1%'") % path);
return info; return info;
} end_retry_sqlite;
} }
@ -834,7 +845,7 @@ unsigned long long LocalStore::queryValidPathId(const Path & path)
} }
bool LocalStore::isValidPath(const Path & path) bool LocalStore::isValidPath_(const Path & path)
{ {
SQLiteStmtUse use(stmtQueryPathInfo); SQLiteStmtUse use(stmtQueryPathInfo);
stmtQueryPathInfo.bind(path); stmtQueryPathInfo.bind(path);
@ -845,22 +856,32 @@ bool LocalStore::isValidPath(const Path & path)
} }
bool LocalStore::isValidPath(const Path & path)
{
retry_sqlite {
return isValidPath_(path);
} end_retry_sqlite;
}
PathSet LocalStore::queryValidPaths(const PathSet & paths) PathSet LocalStore::queryValidPaths(const PathSet & paths)
{ {
retry_sqlite {
PathSet res; PathSet res;
foreach (PathSet::const_iterator, i, paths) foreach (PathSet::const_iterator, i, paths)
if (isValidPath(*i)) res.insert(*i); if (isValidPath_(*i)) res.insert(*i);
return res; return res;
} end_retry_sqlite;
} }
PathSet LocalStore::queryAllValidPaths() PathSet LocalStore::queryAllValidPaths()
{ {
retry_sqlite {
SQLiteStmt stmt; SQLiteStmt stmt;
stmt.create(db, "select path from ValidPaths"); stmt.create(db, "select path from ValidPaths");
PathSet res; PathSet res;
int r; int r;
while ((r = sqlite3_step(stmt)) == SQLITE_ROW) { while ((r = sqlite3_step(stmt)) == SQLITE_ROW) {
const char * s = (const char *) sqlite3_column_text(stmt, 0); const char * s = (const char *) sqlite3_column_text(stmt, 0);
@ -872,6 +893,7 @@ PathSet LocalStore::queryAllValidPaths()
throwSQLiteError(db, "error getting valid paths"); throwSQLiteError(db, "error getting valid paths");
return res; return res;
} end_retry_sqlite;
} }
@ -883,10 +905,8 @@ void LocalStore::queryReferences(const Path & path,
} }
void LocalStore::queryReferrers(const Path & path, PathSet & referrers) void LocalStore::queryReferrers_(const Path & path, PathSet & referrers)
{ {
assertStorePath(path);
SQLiteStmtUse use(stmtQueryReferrers); SQLiteStmtUse use(stmtQueryReferrers);
stmtQueryReferrers.bind(path); stmtQueryReferrers.bind(path);
@ -903,6 +923,15 @@ void LocalStore::queryReferrers(const Path & path, PathSet & referrers)
} }
void LocalStore::queryReferrers(const Path & path, PathSet & referrers)
{
assertStorePath(path);
retry_sqlite {
queryReferrers_(path, referrers);
} end_retry_sqlite;
}
Path LocalStore::queryDeriver(const Path & path) Path LocalStore::queryDeriver(const Path & path)
{ {
return queryPathInfo(path).deriver; return queryPathInfo(path).deriver;
@ -913,6 +942,7 @@ PathSet LocalStore::queryValidDerivers(const Path & path)
{ {
assertStorePath(path); assertStorePath(path);
retry_sqlite {
SQLiteStmtUse use(stmtQueryValidDerivers); SQLiteStmtUse use(stmtQueryValidDerivers);
stmtQueryValidDerivers.bind(path); stmtQueryValidDerivers.bind(path);
@ -928,11 +958,13 @@ PathSet LocalStore::queryValidDerivers(const Path & path)
throwSQLiteError(db, format("error getting valid derivers of `%1%'") % path); throwSQLiteError(db, format("error getting valid derivers of `%1%'") % path);
return derivers; return derivers;
} end_retry_sqlite;
} }
PathSet LocalStore::queryDerivationOutputs(const Path & path) PathSet LocalStore::queryDerivationOutputs(const Path & path)
{ {
retry_sqlite {
SQLiteStmtUse use(stmtQueryDerivationOutputs); SQLiteStmtUse use(stmtQueryDerivationOutputs);
stmtQueryDerivationOutputs.bind(queryValidPathId(path)); stmtQueryDerivationOutputs.bind(queryValidPathId(path));
@ -948,11 +980,13 @@ PathSet LocalStore::queryDerivationOutputs(const Path & path)
throwSQLiteError(db, format("error getting outputs of `%1%'") % path); throwSQLiteError(db, format("error getting outputs of `%1%'") % path);
return outputs; return outputs;
} end_retry_sqlite;
} }
StringSet LocalStore::queryDerivationOutputNames(const Path & path) StringSet LocalStore::queryDerivationOutputNames(const Path & path)
{ {
retry_sqlite {
SQLiteStmtUse use(stmtQueryDerivationOutputs); SQLiteStmtUse use(stmtQueryDerivationOutputs);
stmtQueryDerivationOutputs.bind(queryValidPathId(path)); stmtQueryDerivationOutputs.bind(queryValidPathId(path));
@ -968,6 +1002,7 @@ StringSet LocalStore::queryDerivationOutputNames(const Path & path)
throwSQLiteError(db, format("error getting output names of `%1%'") % path); throwSQLiteError(db, format("error getting output names of `%1%'") % path);
return outputNames; return outputNames;
} end_retry_sqlite;
} }
@ -977,6 +1012,7 @@ Path LocalStore::queryPathFromHashPart(const string & hashPart)
Path prefix = settings.nixStore + "/" + hashPart; Path prefix = settings.nixStore + "/" + hashPart;
retry_sqlite {
SQLiteStmtUse use(stmtQueryPathFromHashPart); SQLiteStmtUse use(stmtQueryPathFromHashPart);
stmtQueryPathFromHashPart.bind(prefix); stmtQueryPathFromHashPart.bind(prefix);
@ -986,6 +1022,7 @@ Path LocalStore::queryPathFromHashPart(const string & hashPart)
const char * s = (const char *) sqlite3_column_text(stmtQueryPathFromHashPart, 0); const char * s = (const char *) sqlite3_column_text(stmtQueryPathFromHashPart, 0);
return s && prefix.compare(0, prefix.size(), s, prefix.size()) == 0 ? s : ""; return s && prefix.compare(0, prefix.size(), s, prefix.size()) == 0 ? s : "";
} end_retry_sqlite;
} }
@ -1229,7 +1266,7 @@ void LocalStore::registerValidPaths(const ValidPathInfos & infos)
foreach (ValidPathInfos::const_iterator, i, infos) { foreach (ValidPathInfos::const_iterator, i, infos) {
assert(i->hash.type == htSHA256); assert(i->hash.type == htSHA256);
if (isValidPath(i->path)) if (isValidPath_(i->path))
updatePathInfo(*i); updatePathInfo(*i);
else else
addValidPath(*i); addValidPath(*i);
@ -1643,8 +1680,8 @@ void LocalStore::invalidatePathChecked(const Path & path)
retry_sqlite { retry_sqlite {
SQLiteTxn txn(db); SQLiteTxn txn(db);
if (isValidPath(path)) { if (isValidPath_(path)) {
PathSet referrers; queryReferrers(path, referrers); PathSet referrers; queryReferrers_(path, referrers);
referrers.erase(path); /* ignore self-references */ referrers.erase(path); /* ignore self-references */
if (!referrers.empty()) if (!referrers.empty())
throw PathInUse(format("cannot delete path `%1%' because it is in use by %2%") throw PathInUse(format("cannot delete path `%1%' because it is in use by %2%")

View file

@ -304,6 +304,10 @@ private:
void checkDerivationOutputs(const Path & drvPath, const Derivation & drv); void checkDerivationOutputs(const Path & drvPath, const Derivation & drv);
void optimisePath_(OptimiseStats & stats, const Path & path); void optimisePath_(OptimiseStats & stats, const Path & path);
// Internal versions that are not wrapped in retry_sqlite.
bool isValidPath_(const Path & path);
void queryReferrers_(const Path & path, PathSet & referrers);
}; };