use super::open::{bounded, open};
use super::{context, Work};
use crate::cli::Cli;
use crate::config::vault::FileVault;
use crate::config::Config;
use crate::conn::profile::{Profile, SecretRef};
use crate::conn::store::ProfileStore;
use crate::error::{Failure, Result};
use crate::output::{Envelope, Gap};
use crate::runtime::{Gate, Shutdown};
use serde_json::{json, Value};
use std::future::Future;
use std::pin::Pin;
use std::task::Poll;
type Queued<'a, T> = Pin<Box<dyn Future<Output = T> + 'a>>;
pub(super) async fn across_destinations(
cli: &Cli,
config: &Config,
permits: usize,
shutdown: &Shutdown,
store: &ProfileStore,
vault: &FileVault,
) -> Result<Envelope> {
let stored = store.list();
if stored.is_empty() {
return Err(Failure::config(
"profile.none_stored",
"there is no destination stored on this host to run against",
)
.with_hint("add one with `conn add`"));
}
let gate = Gate::new(permits)?;
let gate = &gate;
let queued: Vec<Queued<'_, (usize, String, Result<Value>)>> = stored
.iter()
.enumerate()
.map(|(order, profile)| {
let served: Queued<'_, _> = Box::pin(async move {
let outcome = async {
let _licence = gate.acquire(&shutdown.token()).await?;
one_destination(cli, config, shutdown, profile, vault).await
}
.await;
(order, profile.name.clone(), outcome)
});
served
})
.collect();
let mut served = drive_all(queued).await;
served.sort_by_key(|(order, _, _)| *order);
assemble(
cli.command.published_name(),
served.into_iter().map(|(_, name, outcome)| (name, outcome)),
)
}
fn assemble(
command: &'static str,
served: impl Iterator<Item = (String, Result<Value>)>,
) -> Result<Envelope> {
let mut blocks = Vec::new();
let mut envelope = Envelope::new(command, Value::Null);
let mut worst: Option<Failure> = None;
for (name, outcome) in served {
match outcome {
Ok(data) => blocks.push(json!({ "profile": name, "data": data })),
Err(failure) => {
envelope = envelope.with_gap(Gap::new(
name,
failure.code().to_owned(),
failure.message().to_owned(),
));
worst = Some(match worst {
Some(kept) if kept.exit_number().code() >= failure.exit_number().code() => kept,
_ => failure,
});
}
}
}
if let (true, Some(failure)) = (blocks.is_empty(), worst) {
return Err(envelope.gaps.iter().fold(failure, |carried, gap| {
carried.with_cause(format!("{}: {} ({})", gap.object, gap.detail, gap.cause))
}));
}
envelope.data = json!({ "results": blocks });
Ok(envelope)
}
async fn one_destination(
cli: &Cli,
config: &Config,
shutdown: &Shutdown,
profile: &Profile,
vault: &FileVault,
) -> Result<Value> {
let secret =
crate::conn::profile::SecretVault::load(vault, &SecretRef::for_profile(&profile.name))?;
let profile = &super::open::targeted(profile.clone(), cli);
let settings = context::connection(config, cli, Some(profile), secret)?;
let session = open(profile, &settings, cli, config, &shutdown.token()).await?;
let work = Work {
cli,
config,
scope: profile.database.clone(),
shutdown,
many: true,
};
let answered = bounded(&work, session, profile, &settings, &mut std::io::sink()).await?;
Ok(answered.map_or(Value::Null, |envelope| envelope.data))
}
async fn drive_all<T>(mut running: Vec<Queued<'_, T>>) -> Vec<T> {
let mut finished = Vec::with_capacity(running.len());
std::future::poll_fn(|context| {
let mut index = 0;
while index < running.len() {
match running[index].as_mut().poll(context) {
Poll::Ready(value) => {
finished.push(value);
drop(running.remove(index));
}
Poll::Pending => index += 1,
}
}
if running.is_empty() {
Poll::Ready(())
} else {
Poll::Pending
}
})
.await;
finished
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_destination_that_failed_is_declared_and_the_others_still_answer() {
let envelope = assemble(
"query run",
[
("producao".to_owned(), Ok(json!({"rows": [{"id": 1}]}))),
(
"homologacao".to_owned(),
Err(Failure::network(
"conn.unreachable",
"no route to the server",
)),
),
("desenvolvimento".to_owned(), Ok(json!({"rows": []}))),
]
.into_iter(),
)
.expect("a partly served run is still an answer");
let blocks = envelope.data["results"].as_array().expect("one block each");
assert_eq!(blocks.len(), 2, "the reachable destinations both answered");
assert_eq!(blocks[0]["profile"], "producao");
assert_eq!(blocks[1]["profile"], "desenvolvimento");
assert_eq!(envelope.gaps.len(), 1, "the failure must be declared");
assert_eq!(envelope.gaps[0].object, "homologacao");
assert_eq!(envelope.gaps[0].cause, "conn.unreachable");
}
#[test]
fn with_no_destination_reachable_the_worst_failure_is_the_outcome() {
let failure = assemble(
"query run",
[
(
"a".to_owned(),
Err(Failure::network("conn.unreachable", "no route")),
),
(
"b".to_owned(),
Err(Failure::new(
crate::error::Category::Timeout,
"timeout.connect",
"the destination did not answer in time",
)),
),
]
.into_iter(),
)
.expect_err("nothing was served");
assert_eq!(failure.code(), "timeout.connect");
assert_eq!(
failure.causes().len(),
2,
"each destination carries its own reason"
);
assert!(failure
.causes()
.iter()
.any(|cause| cause.starts_with("a: ")));
assert!(failure
.causes()
.iter()
.any(|cause| cause.starts_with("b: ")));
}
#[tokio::test]
async fn work_runs_up_to_the_ceiling_and_no_further() {
use std::cell::Cell;
use tokio_util::sync::CancellationToken;
let gate = Gate::new(2).expect("a legal ceiling");
let gate = &gate;
let token = &CancellationToken::new();
let running = &Cell::new(0usize);
let highest = &Cell::new(0usize);
let queued: Vec<Queued<'_, usize>> = (0..6)
.map(|index| {
let work: Queued<'_, usize> = Box::pin(async move {
let _licence = gate.acquire(token).await.expect("admitted");
running.set(running.get() + 1);
highest.set(highest.get().max(running.get()));
tokio::task::yield_now().await;
running.set(running.get() - 1);
index
});
work
})
.collect();
let finished = drive_all(queued).await;
assert_eq!(finished.len(), 6, "every piece of work is finished");
assert_eq!(highest.get(), 2, "the ceiling is the ceiling");
}
}