Skip to content

Commit

Permalink
fix: reinstate connection pools
Browse files Browse the repository at this point in the history
  • Loading branch information
fosskers committed Mar 18, 2024
1 parent 7170efd commit 1187974
Show file tree
Hide file tree
Showing 21 changed files with 171 additions and 64 deletions.
35 changes: 35 additions & 0 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ members = [
# Core libraries
"aura-core",
# Ecosystem libraries
# "r2d2-alpm",
"r2d2-alpm",
# Documentation Generation
# "doc-gen"
]
Expand Down
3 changes: 3 additions & 0 deletions rust/aura-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ license = "GPL-3.0-only"
keywords = ["archlinux", "alpm", "aur", "pacman"]

[dependencies]
r2d2-alpm = { version = "0.2", path = "../r2d2-alpm" }

alpm = "3.0"
alpm-utils = "3.0"
disown = "1.0"
Expand All @@ -18,6 +20,7 @@ itertools = "0.12"
log = "0.4"
nonempty-collections = "0.1.4"
petgraph = { version = "0.6", default-features = false }
r2d2 = "0.8"
rayon = "1.8"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
Expand Down
54 changes: 44 additions & 10 deletions rust/aura-core/src/aur/dependencies.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
//! AUR package dependency solving.

use crate::{Alpm, Apply};
use crate::Apply;
use disown::Disown;
use log::{debug, info};
use nonempty_collections::NEVec;
use petgraph::graph::NodeIndex;
use petgraph::Graph;
use r2d2::{ManageConnection, Pool};
use r2d2_alpm::Alpm;
use rayon::iter::{IntoParallelIterator, IntoParallelRefIterator, ParallelIterator};
use srcinfo::Srcinfo;
use std::borrow::Borrow;
Expand All @@ -22,6 +24,8 @@ use validated::Validated;
pub enum Error<E> {
/// A [`Mutex`] was poisoned and couldn't be unlocked.
PoisonedMutex,
/// An error pulling from the resource pool.
R2D2(r2d2::Error),
/// An error parsing a `.SRCINFO` file.
Srcinfo(PathBuf, srcinfo::Error),
/// An error cloning or pulling a repo.
Expand All @@ -46,10 +50,17 @@ impl<E> From<crate::git::Error> for Error<E> {
}
}

impl<E> From<r2d2::Error> for Error<E> {
fn from(v: r2d2::Error) -> Self {
Self::R2D2(v)
}
}

impl<E: std::fmt::Display> std::fmt::Display for Error<E> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::PoisonedMutex => write!(f, "Poisoned Mutex"),
Error::R2D2(e) => write!(f, "{}", e),
Error::Resolutions(es) => {
writeln!(f, "Errors during dependency resolution:")?;
for e in (*es).iter() {
Expand Down Expand Up @@ -159,14 +170,15 @@ impl Hash for Buildable {
}

/// Determine all packages to be built and installed.
pub fn resolve<'a, I, F, E>(
alpm: Alpm,
pub fn resolve<'a, I, M, F, E>(
pool: Pool<M>,
fetch: &F,
clone_d: &Path,
pkgs: I,
) -> Result<Resolution, Error<E>>
where
I: IntoIterator<Item = &'a str>,
M: ManageConnection<Connection = Alpm>,
F: Fn(&str) -> Result<Vec<crate::faur::Package>, E> + Sync,
E: Send,
{
Expand All @@ -179,7 +191,7 @@ where

let start = OffsetDateTime::now_utc();
orig.par_iter()
.map(|pkg| resolve_one(alpm.clone(), arc.clone(), fetch, clone_d, &orig, None, pkg))
.map(|pkg| resolve_one(pool.clone(), arc.clone(), fetch, clone_d, &orig, None, pkg))
.collect::<Validated<(), Error<E>>>()
.ok()
.map_err(|es| Error::Resolutions(Box::new(es)))?;
Expand All @@ -196,8 +208,8 @@ where
Ok(res)
}

fn resolve_one<F, E>(
alpm: Alpm,
fn resolve_one<M, F, E>(
pool: Pool<M>,
mutx: Arc<Mutex<Resolution>>,
fetch: &F,
clone_d: &Path,
Expand All @@ -206,6 +218,7 @@ fn resolve_one<F, E>(
pkg_raw: &str,
) -> Result<(), Error<E>>
where
M: ManageConnection<Connection = Alpm>,
F: Fn(&str) -> Result<Vec<crate::faur::Package>, E> + Sync,
E: Send,
{
Expand All @@ -224,7 +237,14 @@ where
// Checks if the current package is installed or otherwise satisfied by
// some package, and then immediately drops the ALPM handle.
let satisfied = {
let db = alpm.as_ref().localdb();
let state = pool.state();
debug!(
"Trying to get ALPM handle ({} idle connections)",
state.idle_connections
);
let alpm = pool.get()?;
debug!("Got a handle.");
let db = alpm.alpm.localdb();
db.pkg(pr).is_ok() || db.pkgs().find_satisfier(pr).is_some()
};

Expand All @@ -236,7 +256,9 @@ where
.satisfied
.insert(pkg);
} else {
match alpm.as_ref().syncdbs().find_satisfier(pr) {
let alpm = pool.get()?;

match alpm.alpm.syncdbs().find_satisfier(pr) {
Some(official) => {
debug!("{} is an official package.", pr);

Expand All @@ -254,16 +276,28 @@ where
.map(|d| d.name().to_string())
.collect();

// FIXME Fri Feb 18 15:07:23 2022
//
// Manual drops are a signal of bad design. For the moment
// these are necessary to avoid running out of ALPM handles
// when we recurse.
drop(alpm);

deps.into_par_iter()
.map(|d| {
let p = Some(prnt.as_str());
resolve_one(alpm.clone(), mutx.clone(), fetch, clone_d, orig, p, &d)
resolve_one(pool.clone(), mutx.clone(), fetch, clone_d, orig, p, &d)
})
.collect::<Validated<(), Error<E>>>()
.ok()
.map_err(|es| Error::Resolutions(Box::new(es)))?;
}
None => {
// FIXME Fri Feb 18 15:13:31 2022
//
// Same here as above.
drop(alpm);

debug!("{} is an AUR package.", pr);
let path = pull_or_clone(fetch, clone_d, parent, &pkg)?;
debug!("Parsing .SRCINFO for {}", pkg);
Expand Down Expand Up @@ -307,7 +341,7 @@ where
.into_par_iter()
.map(|p| {
let prnt = Some(parent.as_str());
resolve_one(alpm.clone(), mutx.clone(), fetch, clone_d, orig, prnt, &p)
resolve_one(pool.clone(), mutx.clone(), fetch, clone_d, orig, prnt, &p)
})
.collect::<Validated<(), Error<E>>>()
.ok()
Expand Down
4 changes: 3 additions & 1 deletion rust/aura-core/src/cache.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Cache manipulation internals.

use crate::{Alpm, Package};
use r2d2_alpm::Alpm;

use crate::Package;
use std::collections::{HashMap, HashSet};
use std::ffi::OsString;
use std::fs::Metadata;
Expand Down
25 changes: 0 additions & 25 deletions rust/aura-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,6 @@ use std::borrow::Cow;
use std::cmp::Ordering;
use std::fs::DirEntry;
use std::path::Path;
use std::sync::Arc;

/// A thread-safe(?) wrapper around a raw [`alpm::Alpm`].
#[derive(Clone)]
pub struct Alpm {
alpm: Arc<alpm::Alpm>,
}

impl Alpm {
/// Construct a new `Alpm` wrapper from an open connection.
pub fn new(alpm: alpm::Alpm) -> Self {
Self {
alpm: Arc::new(alpm),
}
}
}

impl AsRef<alpm::Alpm> for Alpm {
fn as_ref(&self) -> &alpm::Alpm {
self.alpm.as_ref()
}
}

unsafe impl Send for Alpm {}
unsafe impl Sync for Alpm {}

/// The simplest form a package.
#[derive(Debug, PartialEq, Eq)]
Expand Down
3 changes: 1 addition & 2 deletions rust/aura-core/src/snapshot.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
//! Snapshot manipulation internals.

use r2d2_alpm::Alpm;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::fs::File;
use std::io::BufReader;
use std::path::{Path, PathBuf};
use time::OffsetDateTime;

use crate::Alpm;

/// All packages installed at some specific [`DateTime`]. Any "pinned" snapshot
/// should never be considered for deletion.
#[derive(Serialize, Deserialize)]
Expand Down
2 changes: 2 additions & 0 deletions rust/aura-pm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ path = "src/main.rs"

[dependencies]
aura-core = { version = "0.2", path = "../aura-core" }
r2d2-alpm = { version = "0.2", path = "../r2d2-alpm" }

alpm = "3.0"
alpm-utils = "3.0"
Expand All @@ -30,6 +31,7 @@ log = "0.4"
nonempty-collections = "0.1.4"
num_cpus = "1.16"
pacmanconf = "2.0"
r2d2 = "0.8"
rayon = "1.8"
rust-embed = "8.0"
rustyline = "12"
Expand Down
2 changes: 2 additions & 0 deletions rust/aura-pm/i18n/en-US/aura_pm.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ err-json-decode = Failed to decode JSON from: { $url }
err-json-write = Failed to write JSON to: { $file }
err-mutex = A mutex was poisoned.
err-none-exist = None of the specified packages exist.
err-pool-create = Failed to create an ALPM connection pool.
err-pool-get = Failed to get an ALPM handle from the connection pool.
err-read-dir = Failed to read directory: { $dir }
err-srcinfo = Failed to parse .SRCINFO: { $file }
err-sudo = Failed to raise privileges.
Expand Down
11 changes: 6 additions & 5 deletions rust/aura-pm/src/command/aur.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ use crate::error::Nested;
use crate::localization::Localised;
use crate::utils::{Finished, PathStr, ResultVoid, NOTHING};
use crate::{aura, green, proceed};
use aura_core::{Alpm, Apply};
use aura_core::Apply;
use colored::{ColoredString, Colorize};
use from_variants::FromVariants;
use i18n_embed::{fluent::FluentLanguageLoader, LanguageLoader};
use i18n_embed_fl::fl;
use linya::Progress;
use log::{debug, error, info};
use r2d2_alpm::Alpm;
use rayon::prelude::*;
use srcinfo::Srcinfo;
use std::collections::HashSet;
Expand Down Expand Up @@ -194,7 +195,7 @@ pub(crate) fn search(
) -> Result<(), Error> {
debug!("Searching for: {:?}", terms);

let db = alpm.as_ref().localdb();
let db = alpm.alpm.localdb();
let rep = "aur/".magenta();

// Sanitize the input.
Expand Down Expand Up @@ -376,10 +377,10 @@ fn install_work<'a, I>(fll: &FluentLanguageLoader, env: &Env, pkgs: I) -> Result
where
I: IntoIterator<Item = &'a str>,
{
let alpm = env.alpm()?;
let pool = env.alpm_pool()?;
aura!(fll, "A-install-deps");
let rslv = aura_core::aur::dependencies::resolve(
alpm,
pool,
&crate::fetch::fetch_json,
&env.aur.clones,
pkgs,
Expand Down Expand Up @@ -510,7 +511,7 @@ pub(crate) fn upgrade<'a>(
&crate::fetch::fetch_json,
)?;
debug!("Packages pulled: {}", from_api.len());
let db = alpm.as_ref().localdb();
let db = alpm.alpm.localdb();
let mut to_upgrade: Vec<(aura_core::Package<'_>, aura_core::Package<'_>)> = from_api
.into_iter()
.filter_map(|new| db.pkg(new.name.as_str()).ok().map(|old| (old, new)))
Expand Down
2 changes: 1 addition & 1 deletion rust/aura-pm/src/command/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ use crate::localization::Localised;
use crate::utils::{PathStr, NOTHING};
use crate::{aura, green, proceed, yellow};
use aura_core::cache::{CacheSize, PkgPath};
use aura_core::Alpm;
use colored::*;
use from_variants::FromVariants;
use i18n_embed::{fluent::FluentLanguageLoader, LanguageLoader};
use i18n_embed_fl::fl;
use itertools::Itertools;
use linya::Progress;
use log::{debug, error};
use r2d2_alpm::Alpm;
use rayon::prelude::*;
use std::collections::{HashMap, HashSet};
use std::ffi::OsString;
Expand Down
Loading

0 comments on commit 1187974

Please sign in to comment.