NarInfoDiskCache: Handle SQLite busy errors

This commit is contained in:
Eelco Dolstra 2017-02-28 13:44:11 +01:00
parent 80027144ae
commit 34b12bad59
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE

View file

@ -106,6 +106,7 @@ public:
"select * from NARs where cache = ? and hashPart = ? and ((present = 0 and timestamp > ?) or (present = 1 and timestamp > ?))"); "select * from NARs where cache = ? and hashPart = ? and ((present = 0 and timestamp > ?) or (present = 1 and timestamp > ?))");
/* Periodically purge expired entries from the database. */ /* Periodically purge expired entries from the database. */
retrySQLite<void>([&]() {
auto now = time(0); auto now = time(0);
SQLiteStmt queryLastPurge(state->db, "select value from LastPurge"); SQLiteStmt queryLastPurge(state->db, "select value from LastPurge");
@ -125,6 +126,7 @@ public:
"insert or replace into LastPurge(dummy, value) values ('', ?)") "insert or replace into LastPurge(dummy, value) values ('', ?)")
.use()(now).exec(); .use()(now).exec();
} }
});
} }
Cache & getCache(State & state, const std::string & uri) Cache & getCache(State & state, const std::string & uri)
@ -136,6 +138,7 @@ public:
void createCache(const std::string & uri, const Path & storeDir, bool wantMassQuery, int priority) override void createCache(const std::string & uri, const Path & storeDir, bool wantMassQuery, int priority) override
{ {
retrySQLite<void>([&]() {
auto state(_state.lock()); auto state(_state.lock());
// FIXME: race // FIXME: race
@ -143,11 +146,13 @@ public:
state->insertCache.use()(uri)(time(0))(storeDir)(wantMassQuery)(priority).exec(); state->insertCache.use()(uri)(time(0))(storeDir)(wantMassQuery)(priority).exec();
assert(sqlite3_changes(state->db) == 1); assert(sqlite3_changes(state->db) == 1);
state->caches[uri] = Cache{(int) sqlite3_last_insert_rowid(state->db), storeDir, wantMassQuery, priority}; state->caches[uri] = Cache{(int) sqlite3_last_insert_rowid(state->db), storeDir, wantMassQuery, priority};
});
} }
bool cacheExists(const std::string & uri, bool cacheExists(const std::string & uri,
bool & wantMassQuery, int & priority) override bool & wantMassQuery, int & priority) override
{ {
return retrySQLite<bool>([&]() {
auto state(_state.lock()); auto state(_state.lock());
auto i = state->caches.find(uri); auto i = state->caches.find(uri);
@ -164,11 +169,14 @@ public:
priority = cache.priority; priority = cache.priority;
return true; return true;
});
} }
std::pair<Outcome, std::shared_ptr<NarInfo>> lookupNarInfo( std::pair<Outcome, std::shared_ptr<NarInfo>> lookupNarInfo(
const std::string & uri, const std::string & hashPart) override const std::string & uri, const std::string & hashPart) override
{ {
return retrySQLite<std::pair<Outcome, std::shared_ptr<NarInfo>>>(
[&]() -> std::pair<Outcome, std::shared_ptr<NarInfo>> {
auto state(_state.lock()); auto state(_state.lock());
auto & cache(getCache(*state, uri)); auto & cache(getCache(*state, uri));
@ -207,12 +215,14 @@ public:
narInfo->sigs.insert(sig); narInfo->sigs.insert(sig);
return {oValid, narInfo}; return {oValid, narInfo};
});
} }
void upsertNarInfo( void upsertNarInfo(
const std::string & uri, const std::string & hashPart, const std::string & uri, const std::string & hashPart,
std::shared_ptr<ValidPathInfo> info) override std::shared_ptr<ValidPathInfo> info) override
{ {
retrySQLite<void>([&]() {
auto state(_state.lock()); auto state(_state.lock());
auto & cache(getCache(*state, uri)); auto & cache(getCache(*state, uri));
@ -244,6 +254,7 @@ public:
(hashPart) (hashPart)
(time(0)).exec(); (time(0)).exec();
} }
});
} }
}; };