config_cache fixes (#3145)

* add simple backup export/import test

this test fails on current master
until the context is recrated.

* avoid config_cache races

adds needed SQL-statements to config_cache locking.

otherwise, another thread may alter the database
eg. between SELECT and the config_cache update -
resulting in the wrong value being written to config_cache.

* also update config_cache on initializing tables

VERSION_CFG is also set later, however,
not doing it here will result in bugs when we change DBVERSION at some point.

as this alters only VERSION_CFG and that is executed sequentially anyway,
race conditions between SQL and config_cache
seems not to be an issue in this case.

* clear config_cache after backup import

import replaces the whole database,
so config_cache needs to be invalidated as well.

we do that before import,
so in case a backup is imported only partly,
the cache does not add additional problems.

* update CHANGELOG
This commit is contained in:
bjoern
2022-03-22 22:46:29 +01:00
committed by GitHub
parent 86da1aa429
commit 8487eefe46
4 changed files with 61 additions and 6 deletions

View File

@@ -48,7 +48,7 @@ pub struct Sql {
/// open without a passphrase.
is_encrypted: RwLock<Option<bool>>,
config_cache: RwLock<HashMap<String, Option<String>>>,
pub(crate) config_cache: RwLock<HashMap<String, Option<String>>>,
}
impl Sql {
@@ -501,6 +501,7 @@ impl Sql {
pub async fn set_raw_config(&self, key: impl AsRef<str>, value: Option<&str>) -> Result<()> {
let key = key.as_ref();
let mut lock = self.config_cache.write().await;
if let Some(value) = value {
let exists = self
.exists(
@@ -526,8 +527,6 @@ impl Sql {
self.execute("DELETE FROM config WHERE keyname=?;", paramsv![key])
.await?;
}
let mut lock = self.config_cache.write().await;
lock.insert(key.to_string(), value.map(|s| s.to_string()));
drop(lock);
@@ -544,6 +543,7 @@ impl Sql {
return Ok(c);
}
let mut lock = self.config_cache.write().await;
let value = self
.query_get_value(
"SELECT value FROM config WHERE keyname=?;",
@@ -551,8 +551,6 @@ impl Sql {
)
.await
.context(format!("failed to fetch raw config: {}", key.as_ref()))?;
let mut lock = self.config_cache.write().await;
lock.insert(key.as_ref().to_string(), value.clone());
drop(lock);