Skip to content

Commit

Permalink
fuckit: no patch db locks (#1969)
Browse files Browse the repository at this point in the history
* fuck it: no patchdb locks

* fix: Add the locking to the package during the backup. (#1979)

* fix: Add the locking to the package during the backup.

* fix: Lock for the uninstall of the package

* switch patch-db to next

Co-authored-by: J M <2364004+Blu-J@users.noreply.github.com>
  • Loading branch information
dr-bonez and Blu-J committed Nov 29, 2022
1 parent a3a4fdd commit 2b0efb3
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 28 deletions.
2 changes: 1 addition & 1 deletion backend/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ pub async fn action(
.await
.with_kind(crate::ErrorKind::NotFound)?
.manifest()
.get(&mut db, true)
.get(&mut db)
.await?
.to_owned();

Expand Down
19 changes: 10 additions & 9 deletions backend/src/backup/backup_bulk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ pub async fn backup_all(
.await?;
let all_packages = crate::db::DatabaseModel::new()
.package_data()
.get(&mut db, false)
.get(&mut db)
.await?
.0
.keys()
Expand Down Expand Up @@ -297,7 +297,7 @@ async fn perform_backup<Db: DbHandle>(

for package_id in crate::db::DatabaseModel::new()
.package_data()
.keys(&mut db, false)
.keys(&mut db)
.await?
.into_iter()
.filter(|id| package_ids.contains(id))
Expand All @@ -317,7 +317,7 @@ async fn perform_backup<Db: DbHandle>(
let main_status_model = installed_model.clone().status().main();

main_status_model.lock(&mut tx, LockType::Write).await?;
let (started, health) = match main_status_model.get(&mut tx, true).await?.into_owned() {
let (started, health) = match main_status_model.get(&mut tx).await?.into_owned() {
MainStatus::Starting { .. } => (Some(Utc::now()), Default::default()),
MainStatus::Running { started, health } => (Some(started), health.clone()),
MainStatus::Stopped | MainStatus::Stopping | MainStatus::Restarting => {
Expand Down Expand Up @@ -346,11 +346,7 @@ async fn perform_backup<Db: DbHandle>(
.await?;
tx.save().await?; // drop locks

let manifest = installed_model
.clone()
.manifest()
.get(&mut db, false)
.await?;
let manifest = installed_model.clone().manifest().get(&mut db).await?;

ctx.managers
.get(&(manifest.id.clone(), manifest.version.clone()))
Expand All @@ -362,6 +358,11 @@ async fn perform_backup<Db: DbHandle>(
.await;

let mut tx = db.begin().await?;
let lock_package = crate::db::DatabaseModel::new()
.package_data()
.idx(&package_id)
.lock(&mut tx, LockType::Write)
.await;

installed_model.lock(&mut tx, LockType::Write).await?;

Expand Down Expand Up @@ -445,7 +446,7 @@ async fn perform_backup<Db: DbHandle>(
root_ca_cert,
ui: crate::db::DatabaseModel::new()
.ui()
.get(&mut db, true)
.get(&mut db)
.await?
.into_owned(),
})?,
Expand Down
4 changes: 2 additions & 2 deletions backend/src/backup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl BackupActions {
.expect(db)
.await?
.marketplace_url()
.get(db, true)
.get(db)
.await?
.into_owned();
let tmp_path = Path::new(BACKUP_DIR)
Expand Down Expand Up @@ -237,7 +237,7 @@ impl BackupActions {
.installed()
.expect(db)
.await?
.get(db, true)
.get(db)
.await?;

let receipts = crate::config::ConfigReceipts::new(db).await?;
Expand Down
5 changes: 5 additions & 0 deletions backend/src/install/cleanup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,11 @@ where
for<'a> &'a mut Ex: Executor<'a, Database = Postgres>,
{
let mut tx = db.begin().await?;
crate::db::DatabaseModel::new()
.package_data()
.idx_model(&id)
.lock(&mut tx, LockType::Write)
.await?;
let receipts = UninstallReceipts::new(&mut tx, id).await?;
let entry = receipts.removing.get(&mut tx).await?;
cleanup(ctx, &entry.manifest.id, &entry.manifest.version).await?;
Expand Down
13 changes: 9 additions & 4 deletions backend/src/install/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub async fn list(#[context] ctx: RpcContext) -> Result<Vec<(PackageId, Version)
let mut hdl = ctx.db.handle();
let package_data = crate::db::DatabaseModel::new()
.package_data()
.get(&mut hdl, true)
.get(&mut hdl)
.await?;

Ok(package_data
Expand Down Expand Up @@ -601,6 +601,11 @@ pub async fn uninstall_dry(
pub async fn uninstall_impl(ctx: RpcContext, id: PackageId) -> Result<(), Error> {
let mut handle = ctx.db.handle();
let mut tx = handle.begin().await?;
crate::db::DatabaseModel::new()
.package_data()
.idx_model(&id)
.lock(&mut tx, LockType::Write)
.await?;

let mut pde = crate::db::DatabaseModel::new()
.package_data()
Expand Down Expand Up @@ -879,7 +884,7 @@ pub async fn install_s9pk<R: AsyncRead + AsyncSeek + Unpin + Send + Sync>(
.package_data()
.idx_model(dep)
.map::<_, Manifest>(|pde| pde.manifest())
.get(&mut ctx.db.handle(), false)
.get(&mut ctx.db.handle())
.await?
.into_owned()
{
Expand Down Expand Up @@ -1119,7 +1124,7 @@ pub async fn install_s9pk<R: AsyncRead + AsyncSeek + Unpin + Send + Sync>(
let mut deps = BTreeMap::new();
for package in crate::db::DatabaseModel::new()
.package_data()
.keys(&mut tx, true)
.keys(&mut tx)
.await?
{
// update dependency_info on dependents
Expand Down Expand Up @@ -1153,7 +1158,7 @@ pub async fn install_s9pk<R: AsyncRead + AsyncSeek + Unpin + Send + Sync>(
.await?
.installed()
.and_then(|i| i.current_dependencies().idx_model(pkg_id))
.get(&mut tx, true)
.get(&mut tx)
.await?
.to_owned()
{
Expand Down
4 changes: 2 additions & 2 deletions backend/src/manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ impl ManagerMap {
let mut res = BTreeMap::new();
for package in crate::db::DatabaseModel::new()
.package_data()
.keys(db, true)
.keys(db)
.await?
{
let man: Manifest = if let Some(manifest) = crate::db::DatabaseModel::new()
.package_data()
.idx_model(&package)
.and_then(|pkg| pkg.installed())
.map(|m| m.manifest())
.get(db, true)
.get(db)
.await?
.to_owned()
{
Expand Down
2 changes: 1 addition & 1 deletion backend/src/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub async fn fetch_properties(ctx: RpcContext, id: PackageId) -> Result<Value, E
.idx_model(&id)
.and_then(|p| p.installed())
.map(|m| m.manifest())
.get(&mut db, true)
.get(&mut db)
.await?
.to_owned()
.ok_or_else(|| Error::new(eyre!("{} is not installed", id), ErrorKind::NotFound))?;
Expand Down
2 changes: 1 addition & 1 deletion backend/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ async fn migrate(
old_guid: &str,
embassy_password: String,
) -> Result<(Arc<String>, Hostname, OnionAddressV3, X509), Error> {
use crate::util::Invoke;

*ctx.setup_status.write().await = Some(Ok(SetupStatus {
bytes_transferred: 0,
total_bytes: 110,
Expand Down
6 changes: 1 addition & 5 deletions backend/src/version/v0_3_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ impl VersionT for Version {
crate::hostname::ensure_hostname_is_set(db, &receipts).await?;
receipts.id.set(db, generate_id()).await?;

let mut ui = crate::db::DatabaseModel::new()
.ui()
.get(db, false)
.await?
.clone();
let mut ui = crate::db::DatabaseModel::new().ui().get(db).await?.clone();
ui.merge_with(&DEFAULT_UI);
crate::db::DatabaseModel::new().ui().put(db, &ui).await?;

Expand Down
4 changes: 2 additions & 2 deletions backend/src/version/v0_3_3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl VersionT for Version {

for package_id in crate::db::DatabaseModel::new()
.package_data()
.keys(db, false)
.keys(db)
.await?
.iter()
{
Expand Down Expand Up @@ -126,7 +126,7 @@ impl VersionT for Version {
ui["auto-check-updates"] = Value::Bool(true);
ui["pkg-order"] = json!(crate::db::DatabaseModel::new()
.package_data()
.keys(db, false)
.keys(db)
.await?
.iter()
.map(|x| x.to_string())
Expand Down
2 changes: 1 addition & 1 deletion patch-db
Submodule patch-db updated 1 files
+6 −37 patch-db/src/model.rs

0 comments on commit 2b0efb3

Please sign in to comment.