use std::collections::{BTreeSet, HashMap};
use std::future::Future;
use std::time::Duration;
use tonic::codegen::http::uri::PathAndQuery;
use tonic::metadata::{MetadataMap, MetadataValue};
use tonic::transport::{Channel, ClientTlsConfig, Endpoint};
use tonic::{Code, Status};
use super::{Params, Rows, Run};
use crate::Error;
const CONNECT: Duration = Duration::from_secs(5);
const CALL: Duration = Duration::from_secs(30);
const BUSY: Duration = Duration::from_secs(10);
const EXECUTE: &str = "/kosmonaut.v1.Query/ExecuteQuery";
const BEGIN: &str = "/kosmonaut.v1.Sessions/Begin";
const COMMIT: &str = "/kosmonaut.v1.Sessions/Commit";
const ROLLBACK: &str = "/kosmonaut.v1.Sessions/Rollback";
const LIST_TABLES: &str = "/kosmonaut.v1.Catalog/ListTables";
const TX_ID: &str = "kosmonaut-tx-id";
#[derive(Clone, PartialEq, prost::Message)]
struct SessionId {
#[prost(string, tag = "1")]
value: String,
}
#[derive(Clone, PartialEq, prost::Message)]
struct QueryRequest {
#[prost(string, tag = "1")]
statement: String,
#[prost(map = "string, string", tag = "3")]
params: HashMap<String, String>,
}
#[derive(Clone, PartialEq, prost::Message)]
struct QueryResponse {
#[prost(string, tag = "3")]
json_body: String,
}
#[derive(Clone, PartialEq, prost::Message)]
struct BeginRequest {
#[prost(bool, tag = "1")]
read_only: bool,
}
#[derive(Clone, PartialEq, prost::Message)]
struct BeginResponse {
#[prost(message, optional, tag = "1")]
session: Option<SessionId>,
}
#[derive(Clone, PartialEq, prost::Message)]
struct SessionRef {
#[prost(message, optional, tag = "1")]
session: Option<SessionId>,
}
#[derive(Clone, PartialEq, prost::Message)]
struct Done {}
#[derive(Clone, PartialEq, prost::Message)]
struct ListTablesRequest {
#[prost(string, tag = "1")]
namespace: String,
}
#[derive(Clone, PartialEq, prost::Message)]
struct TableEntry {
#[prost(string, tag = "2")]
name: String,
}
#[derive(Clone, PartialEq, prost::Message)]
struct ListTablesResponse {
#[prost(message, repeated, tag = "1")]
tables: Vec<TableEntry>,
}
#[derive(serde::Deserialize)]
struct Body {
#[serde(default)]
rows: Rows,
}
pub struct Server {
url: String,
token_env: Option<String>,
channel: Channel,
runtime: Option<tokio::runtime::Runtime>,
}
impl std::fmt::Debug for Server {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Server")
.field("url", &self.url)
.field("token_env", &self.token_env)
.finish_non_exhaustive()
}
}
impl Drop for Server {
fn drop(&mut self) {
if let Some(rt) = self.runtime.take() {
rt.shutdown_background();
}
}
}
impl Server {
pub fn new(url: &str, tls: bool, token_env: Option<String>) -> Result<Server, Error> {
let bad = |why: String| Error::Invalid(format!("memory.kosmonaut.url `{url}`: {why}"));
let target = if url.starts_with("unix:") {
if tls {
return Err(bad("a socket takes no TLS".into()));
}
url.to_owned()
} else {
let rest = url
.strip_prefix("grpc://")
.ok_or_else(|| bad("not grpc://host:port or unix:/path".into()))?;
format!("{}://{rest}", if tls { "https" } else { "http" })
};
let mut endpoint = Endpoint::from_shared(target)
.map_err(|e| bad(e.to_string()))?
.connect_timeout(CONNECT)
.timeout(CALL);
if tls {
endpoint = endpoint
.tls_config(ClientTlsConfig::new().with_webpki_roots())
.map_err(|e| bad(e.to_string()))?;
}
let runtime = tokio::runtime::Builder::new_multi_thread()
.worker_threads(1)
.thread_name("kosmonaut-client")
.enable_all()
.build()
.map_err(|e| Error::Io(format!("kosmonaut client: {e}")))?;
let channel = {
let _in = runtime.enter();
endpoint.connect_lazy()
};
Ok(Server {
url: url.to_owned(),
token_env,
channel,
runtime: Some(runtime),
})
}
pub fn url(&self) -> &str {
&self.url
}
fn headers(&self, session: Option<&str>) -> Result<MetadataMap, Error> {
let mut r = MetadataMap::new();
if let Some(name) = &self.token_env {
let token = std::env::var(name).ok().filter(|t| !t.is_empty());
let token = token.ok_or_else(|| {
Error::Unavailable(format!(
"kosmonaut server {}: no token: the environment variable {name} is not set",
self.url
))
})?;
let mut v = MetadataValue::try_from(format!("Bearer {token}")).map_err(|_| {
Error::Unavailable(format!(
"kosmonaut server {}: the token in {name} is not one line of text",
self.url
))
})?;
v.set_sensitive(true);
r.insert("authorization", v);
}
if let Some(id) = session {
let v = MetadataValue::try_from(id)
.map_err(|_| Error::Io(format!("kosmonaut: session id `{id}`")))?;
r.insert(TX_ID, v);
}
Ok(r)
}
fn wait<T: Send + 'static>(
&self,
f: impl Future<Output = Result<T, Status>> + Send + 'static,
) -> Result<T, Error> {
let Some(rt) = self.runtime.as_ref() else {
return Err(Error::Unavailable("kosmonaut client stopped".into()));
};
let (tx, rx) = std::sync::mpsc::channel();
rt.spawn(async move {
let _ = tx.send(f.await);
});
match rx.recv() {
Ok(r) => r.map_err(|e| self.failed(&e)),
Err(_) => Err(Error::Unavailable(format!(
"kosmonaut server {}: the call was dropped",
self.url
))),
}
}
fn unary<Q, A>(&self, path: &'static str, m: Q, session: Option<&str>) -> Result<A, Error>
where
Q: prost::Message + Clone + Send + Sync + 'static,
A: prost::Message + Default + Send + Sync + 'static,
{
let headers = self.headers(session)?;
let channel = self.channel.clone();
self.wait(async move {
let until = tokio::time::Instant::now() + BUSY;
let mut pause = Duration::from_millis(25);
loop {
let mut grpc = tonic::client::Grpc::new(channel.clone());
grpc.ready()
.await
.map_err(|e| Status::from_error(Box::new(e)))?;
let mut req = tonic::Request::new(m.clone());
*req.metadata_mut() = headers.clone();
let codec = tonic_prost::ProstCodec::<Q, A>::default();
match grpc
.unary(req, PathAndQuery::from_static(path), codec)
.await
{
Err(e)
if e.code() == Code::ResourceExhausted
&& tokio::time::Instant::now() + pause < until =>
{
tokio::time::sleep(pause).await;
pause = (pause * 2).min(Duration::from_secs(1));
}
r => return r.map(tonic::Response::into_inner),
}
}
})
}
fn failed(&self, e: &Status) -> Error {
let detail = match std::error::Error::source(e) {
Some(s) if !e.message().contains(&s.to_string()) => format!("{}: {s}", e.message()),
_ => e.message().to_owned(),
};
match e.code() {
Code::Unavailable | Code::DeadlineExceeded | Code::Cancelled => {
Error::Unavailable(format!("kosmonaut server {}: {detail}", self.url))
}
Code::Unknown if std::error::Error::source(e).is_some() => {
Error::Unavailable(format!("kosmonaut server {}: {detail}", self.url))
}
Code::Unauthenticated | Code::PermissionDenied => Error::Unavailable(format!(
"kosmonaut server {} refused the token: {detail}",
self.url
)),
_ => Error::Io(format!("kosmonaut: {detail}")),
}
}
fn query(&self, cypher: &str, p: &Params, session: Option<&str>) -> Result<Rows, Error> {
let m = QueryRequest {
statement: cypher.to_owned(),
params: p.iter().map(|(k, v)| (k.clone(), v.to_string())).collect(),
};
let r: QueryResponse = self.unary(EXECUTE, m, session)?;
if r.json_body.is_empty() {
return Ok(Rows::new());
}
serde_json::from_str::<Body>(&r.json_body)
.map(|b| b.rows)
.map_err(|e| Error::Io(format!("kosmonaut: the server's answer: {e}")))
}
pub(super) fn read(&self, cypher: &str, p: &Params) -> Result<Rows, Error> {
self.query(cypher, p, None)
}
pub(super) fn write<T>(
&self,
f: impl FnOnce(&mut dyn Run) -> Result<T, Error>,
) -> Result<T, Error> {
let begun: BeginResponse = self.unary(BEGIN, BeginRequest { read_only: false }, None)?;
let id = begun
.session
.map(|s| s.value)
.filter(|v| !v.is_empty())
.ok_or_else(|| Error::Io("kosmonaut: the server began no session".into()))?;
let at = |id: &str| SessionRef {
session: Some(SessionId {
value: id.to_owned(),
}),
};
let mut tx = Tx {
server: self,
id: &id,
};
match f(&mut tx) {
Ok(out) => {
let _: Done = self.unary(COMMIT, at(&id), None)?;
Ok(out)
}
Err(e) => {
let _: Result<Done, _> = self.unary(ROLLBACK, at(&id), None);
Err(e)
}
}
}
pub(super) fn tables(&self) -> Result<BTreeSet<String>, Error> {
let r: ListTablesResponse = self.unary(
LIST_TABLES,
ListTablesRequest {
namespace: String::new(),
},
None,
)?;
Ok(r.tables.into_iter().map(|t| t.name).collect())
}
pub(super) fn execute(&self, ddl: &str) -> Result<(), Error> {
self.query(ddl, &Params::new(), None).map(|_| ())
}
}
struct Tx<'a> {
server: &'a Server,
id: &'a str,
}
impl Run for Tx<'_> {
fn run(&mut self, cypher: &str, p: &Params) -> Result<Rows, Error> {
self.server.query(cypher, p, Some(self.id))
}
}