use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Instant;
use axum::http::StatusCode;
use chrono::{Timelike, Utc};
use littlegraph_core::{
CancellationToken as CoreCancellationToken, Graph, Params, QueryResult, Transaction,
TransactionOptions,
};
use tokio::sync::{mpsc, oneshot, OwnedSemaphorePermit, Semaphore};
use uuid::Uuid;
use crate::database::{map_core_error, validate_result};
use crate::error::HttpError;
use crate::response::TransactionInfo;
use crate::QueryApiLimits;
const TRANSACTION_ID_LENGTH: usize = 6;
type QueryReply = oneshot::Sender<Result<QueryResult, HttpError>>;
type CommitReply = oneshot::Sender<Result<Option<QueryResult>, HttpError>>;
type UnitReply = oneshot::Sender<Result<(), HttpError>>;
enum Command {
Run {
statement: String,
params: Params,
reply: QueryReply,
},
Commit {
query: Option<(String, Params)>,
reply: CommitReply,
},
Rollback(UnitReply),
}
struct Entry {
commands: mpsc::Sender<Command>,
principal: [u8; 32],
leased: AtomicBool,
expires: Mutex<Instant>,
cancellation: CoreCancellationToken,
_permit: OwnedSemaphorePermit,
}
#[derive(Clone)]
pub(crate) struct TransactionManager {
database_path: PathBuf,
limits: QueryApiLimits,
entries: Arc<tokio::sync::Mutex<HashMap<String, Arc<Entry>>>>,
permits: Arc<Semaphore>,
}
struct Lease {
id: String,
entry: Arc<Entry>,
manager: TransactionManager,
keep: bool,
}
impl TransactionManager {
pub(crate) fn new(database_path: PathBuf, limits: QueryApiLimits) -> Self {
Self {
database_path,
permits: Arc::new(Semaphore::new(limits.max_open_transactions)),
limits,
entries: Arc::new(tokio::sync::Mutex::new(HashMap::new())),
}
}
pub(crate) async fn begin(
&self,
mut options: TransactionOptions,
principal: [u8; 32],
) -> Result<TransactionInfo, HttpError> {
let permit = self.permits.clone().try_acquire_owned().map_err(|_| {
HttpError::new(
StatusCode::SERVICE_UNAVAILABLE,
"Neo.TransientError.General.MemoryPoolOutOfMemoryError",
"the maximum number of open Query API transactions was reached",
)
})?;
let cancellation = CoreCancellationToken::new();
options.cancellation_token = Some(cancellation.clone());
let (commands, receiver) = mpsc::channel(self.limits.database_command_capacity);
let (startup_tx, startup_rx) = oneshot::channel();
let database_path = self.database_path.clone();
let max_result_rows = self.limits.max_result_rows;
let max_result_bytes = self.limits.max_result_bytes;
tokio::task::spawn_blocking(move || {
match Graph::open_neo4j_writable(database_path).map_err(map_core_error) {
Ok(mut graph) => match graph
.transaction_with_options(options)
.map_err(map_core_error)
{
Ok(transaction) => {
let _ = startup_tx.send(Ok(()));
transaction_loop(transaction, receiver, max_result_rows, max_result_bytes);
}
Err(error) => {
let _ = startup_tx.send(Err(error));
}
},
Err(error) => {
let _ = startup_tx.send(Err(error));
}
}
});
startup_rx.await.map_err(|_| worker_stopped())??;
let entry = Arc::new(Entry {
commands,
principal,
leased: AtomicBool::new(false),
expires: Mutex::new(Instant::now() + self.limits.transaction_idle_timeout),
cancellation,
_permit: permit,
});
let id = {
let mut entries = self.entries.lock().await;
loop {
let candidate =
Uuid::new_v4().simple().to_string()[..TRANSACTION_ID_LENGTH].to_owned();
if !entries.contains_key(&candidate) {
entries.insert(candidate.clone(), entry.clone());
break candidate;
}
}
};
Ok(self.info(id))
}
pub(crate) async fn run(
&self,
id: &str,
principal: [u8; 32],
statement: String,
params: Params,
) -> Result<(QueryResult, TransactionInfo), HttpError> {
let mut lease = self.lease(id, principal).await?;
let (reply_tx, reply_rx) = oneshot::channel();
if lease
.entry
.commands
.send(Command::Run {
statement,
params,
reply: reply_tx,
})
.await
.is_err()
{
lease.remove().await;
return Err(worker_stopped());
}
let reply = match reply_rx.await {
Ok(reply) => reply,
Err(_) => {
lease.remove().await;
return Err(worker_stopped());
}
};
match reply {
Ok(result) => {
let info = self.info(id.to_owned());
Ok((result, info))
}
Err(error) => {
lease.remove().await;
Err(error)
}
}
}
pub(crate) async fn keep_alive(
&self,
id: &str,
principal: [u8; 32],
) -> Result<TransactionInfo, HttpError> {
let _lease = self.lease(id, principal).await?;
Ok(self.info(id.to_owned()))
}
pub(crate) async fn commit(
&self,
id: &str,
principal: [u8; 32],
query: Option<(String, Params)>,
) -> Result<Option<QueryResult>, HttpError> {
let mut lease = self.lease(id, principal).await?;
let (reply_tx, reply_rx) = oneshot::channel();
if lease
.entry
.commands
.send(Command::Commit {
query,
reply: reply_tx,
})
.await
.is_err()
{
lease.remove().await;
return Err(worker_stopped());
}
let result = match reply_rx.await {
Ok(result) => result,
Err(_) => {
lease.remove().await;
return Err(worker_stopped());
}
};
lease.remove().await;
result
}
pub(crate) async fn rollback(&self, id: &str, principal: [u8; 32]) -> Result<(), HttpError> {
let mut lease = self.lease(id, principal).await?;
let (reply_tx, reply_rx) = oneshot::channel();
if lease
.entry
.commands
.send(Command::Rollback(reply_tx))
.await
.is_err()
{
lease.remove().await;
return Err(worker_stopped());
}
let result = match reply_rx.await {
Ok(result) => result,
Err(_) => {
lease.remove().await;
return Err(worker_stopped());
}
};
lease.remove().await;
result
}
pub(crate) async fn expire(&self) {
let now = Instant::now();
let candidates = {
let entries = self.entries.lock().await;
entries
.iter()
.filter_map(|(id, entry)| {
let expired = entry
.expires
.lock()
.map(|expires| *expires <= now)
.unwrap_or(true);
(expired
&& entry
.leased
.compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire)
.is_ok())
.then(|| (id.clone(), entry.clone()))
})
.collect::<Vec<_>>()
};
for (id, entry) in candidates {
if self.remove_if_current(&id, &entry).await {
entry.cancellation.cancel();
let (reply, _) = oneshot::channel();
let _ = entry.commands.send(Command::Rollback(reply)).await;
}
}
}
pub(crate) async fn shutdown(&self) {
let entries = {
let mut entries = self.entries.lock().await;
entries.drain().map(|(_, entry)| entry).collect::<Vec<_>>()
};
for entry in &entries {
entry.cancellation.cancel();
}
for entry in entries {
let (reply_tx, reply_rx) = oneshot::channel();
if entry
.commands
.send(Command::Rollback(reply_tx))
.await
.is_ok()
{
let _ = reply_rx.await;
}
}
}
async fn lease(&self, id: &str, principal: [u8; 32]) -> Result<Lease, HttpError> {
let entry = self
.entries
.lock()
.await
.get(id)
.cloned()
.ok_or_else(|| HttpError::not_found(id))?;
if entry.principal != principal {
return Err(HttpError::not_found(id));
}
let expired = entry
.expires
.lock()
.map(|expires| *expires <= Instant::now())
.unwrap_or(true);
if expired {
self.remove_if_current(id, &entry).await;
entry.cancellation.cancel();
return Err(HttpError::not_found(id));
}
if entry
.leased
.compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire)
.is_err()
{
return Err(HttpError::new(
StatusCode::BAD_REQUEST,
"Neo.ClientError.Transaction.TransactionAccessedConcurrently",
"Another request is currently accessing this transaction.",
));
}
Ok(Lease {
id: id.to_owned(),
entry,
manager: self.clone(),
keep: true,
})
}
fn info(&self, id: String) -> TransactionInfo {
let expires = (Utc::now()
+ chrono::Duration::from_std(self.limits.transaction_idle_timeout)
.unwrap_or_else(|_| chrono::Duration::seconds(30)))
.with_nanosecond(0)
.expect("zero nanoseconds is always valid");
TransactionInfo { id, expires }
}
async fn remove_if_current(&self, id: &str, entry: &Arc<Entry>) -> bool {
let mut entries = self.entries.lock().await;
if entries
.get(id)
.is_some_and(|current| Arc::ptr_eq(current, entry))
{
entries.remove(id);
true
} else {
false
}
}
}
impl Lease {
async fn remove(&mut self) {
self.keep = false;
self.manager.remove_if_current(&self.id, &self.entry).await;
}
}
impl Drop for Lease {
fn drop(&mut self) {
if self.keep {
if let Ok(mut expires) = self.entry.expires.lock() {
*expires = Instant::now() + self.manager.limits.transaction_idle_timeout;
}
}
self.entry.leased.store(false, Ordering::Release);
}
}
fn transaction_loop(
mut transaction: Transaction<'_>,
mut commands: mpsc::Receiver<Command>,
max_result_rows: usize,
max_result_bytes: usize,
) {
while let Some(command) = commands.blocking_recv() {
match command {
Command::Run {
statement,
params,
reply,
} => match transaction
.query(&statement, params)
.map_err(map_core_error)
.and_then(|result| {
validate_result(&result, max_result_rows, max_result_bytes)?;
Ok(result)
}) {
Ok(result) => {
let _ = reply.send(Ok(result));
}
Err(error) => {
let _ = reply.send(Err(error));
return;
}
},
Command::Commit { query, reply } => {
let result = query
.map(|(statement, params)| {
transaction
.query(&statement, params)
.map_err(map_core_error)
.and_then(|result| {
validate_result(&result, max_result_rows, max_result_bytes)?;
Ok(result)
})
})
.transpose();
let result = match result {
Ok(result) => transaction
.commit()
.map_err(map_core_error)
.map(|()| result),
Err(error) => Err(error),
};
let _ = reply.send(result);
return;
}
Command::Rollback(reply) => {
let result = transaction.rollback().map_err(map_core_error);
let _ = reply.send(result);
return;
}
}
}
}
fn worker_stopped() -> HttpError {
HttpError::new(
StatusCode::SERVICE_UNAVAILABLE,
"Neo.TransientError.General.DatabaseUnavailable",
"the Query API database worker stopped",
)
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn rejects_concurrent_transaction_leases() {
let limits = QueryApiLimits::default();
let manager = TransactionManager::new(PathBuf::from("unused"), limits.clone());
let permit = manager.permits.clone().try_acquire_owned().unwrap();
let (commands, _receiver) = mpsc::channel(1);
let entry = Arc::new(Entry {
commands,
principal: [0; 32],
leased: AtomicBool::new(false),
expires: Mutex::new(Instant::now() + limits.transaction_idle_timeout),
cancellation: CoreCancellationToken::new(),
_permit: permit,
});
manager.entries.lock().await.insert("abcdef".into(), entry);
let _lease = manager.lease("abcdef", [0; 32]).await.unwrap();
let error = match manager.lease("abcdef", [0; 32]).await {
Ok(_) => panic!("a concurrent transaction lease unexpectedly succeeded"),
Err(error) => error,
};
assert_eq!(error.status, StatusCode::BAD_REQUEST);
assert_eq!(
error.code,
"Neo.ClientError.Transaction.TransactionAccessedConcurrently"
);
assert_eq!(
error.message,
"Another request is currently accessing this transaction."
);
}
#[tokio::test]
async fn transaction_leases_are_bound_to_the_authenticated_principal() {
let limits = QueryApiLimits::default();
let manager = TransactionManager::new(PathBuf::from("unused"), limits.clone());
let entry = test_entry(&manager, &limits);
manager.entries.lock().await.insert("abcdef".into(), entry);
let error = match manager.lease("abcdef", [1; 32]).await {
Ok(_) => panic!("a different principal unexpectedly leased the transaction"),
Err(error) => error,
};
assert_eq!(error.status, StatusCode::NOT_FOUND);
assert_eq!(error.code, "Neo.ClientError.Request.Invalid");
manager.lease("abcdef", [0; 32]).await.unwrap();
}
#[tokio::test]
async fn stale_lease_does_not_remove_reused_transaction_id() {
let limits = QueryApiLimits::default();
let manager = TransactionManager::new(PathBuf::from("unused"), limits.clone());
let old_entry = test_entry(&manager, &limits);
let new_entry = test_entry(&manager, &limits);
manager
.entries
.lock()
.await
.insert("abcdef".into(), new_entry.clone());
let mut stale_lease = Lease {
id: "abcdef".into(),
entry: old_entry,
manager: manager.clone(),
keep: true,
};
stale_lease.remove().await;
let current = manager.entries.lock().await["abcdef"].clone();
assert!(Arc::ptr_eq(¤t, &new_entry));
}
fn test_entry(manager: &TransactionManager, limits: &QueryApiLimits) -> Arc<Entry> {
let permit = manager.permits.clone().try_acquire_owned().unwrap();
let (commands, _receiver) = mpsc::channel(1);
Arc::new(Entry {
commands,
principal: [0; 32],
leased: AtomicBool::new(false),
expires: Mutex::new(Instant::now() + limits.transaction_idle_timeout),
cancellation: CoreCancellationToken::new(),
_permit: permit,
})
}
}