use super::{error, Engine, EngineTransaction, MemoryLog, State};
use crate::{LinkError, TransitionLogStore};
use doublets::data::LinkReference;
use std::sync::Arc;
#[derive(Debug, PartialEq, Eq)]
pub enum GroupCommitOutcome {
Committed(u64),
Aborted(String),
Indeterminate(String),
}
impl<T: LinkReference, L: TransitionLogStore> Engine<T, L> {
pub fn commit_group(
&self,
mut sessions: Vec<EngineTransaction<T, L>>,
) -> Result<Vec<GroupCommitOutcome>, LinkError> {
if sessions
.iter()
.any(|tx| !Arc::ptr_eq(&self.state, &tx.engine.state))
{
return Err(error("group sessions must belong to the same engine"));
}
let mut state = self.lock()?;
let mut options = state.options.clone();
options.trace = false;
let mut working = State {
log: MemoryLog::default(),
current: state.current.clone(),
history: state.history.clone(),
changed: state.changed.clone(),
write_owners: state.write_owners.clone(),
lock_waits: None,
next: state.next,
committed_next: state.committed_next,
active: state.active,
poisoned: false,
compacted_revision: state.compacted_revision,
options,
constraint_index: state.constraint_index.clone(),
};
let mut outcomes = Vec::with_capacity(sessions.len());
for tx in &mut sessions {
let base = tx.base.clone();
let outcome = match tx.commit_locked(&mut working) {
Ok(revision) => GroupCommitOutcome::Committed(revision),
Err(cause) => GroupCommitOutcome::Aborted(cause.to_string()),
};
tx.base = base;
outcomes.push(outcome);
}
state.active = working.active;
state.write_owners = working.write_owners;
if let Some(waits) = &mut state.lock_waits {
waits.wake_all();
}
let persisted = (|| {
for record in &working.log.entries {
state.log.append_log_entry(record)?;
}
if !working.log.entries.is_empty() {
state.log.flush_log()?;
}
Ok::<(), LinkError>(())
})();
if let Err(cause) = persisted {
state.poisoned = true;
if state.active == 0 {
state.changed.clear();
}
let message = format!("indeterminate group commit: {cause}; reopen the engine");
for outcome in &mut outcomes {
if matches!(outcome, GroupCommitOutcome::Committed(_)) {
*outcome = GroupCommitOutcome::Indeterminate(message.clone());
}
}
return Ok(outcomes);
}
state.current = working.current;
state.constraint_index = working.constraint_index;
state.history = working.history;
state.changed = working.changed;
state.committed_next = working.committed_next;
if state.options.trace && !working.log.entries.is_empty() {
crate::diagnostics::stderr!(
"[Engine] group committed revision={} records={}",
state.current.revision,
working.log.entries.len()
);
}
Ok(outcomes)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
engine::{ConcurrencyControl, EngineOptions},
LinksStorage,
};
struct Unavailable;
impl TransitionLogStore for Unavailable {
fn read_log_entries(&mut self) -> Result<Vec<String>, LinkError> {
Ok(vec![])
}
fn append_log_entry(&mut self, _: &str) -> Result<(), LinkError> {
Err(error("append unavailable"))
}
fn flush_log(&mut self) -> Result<(), LinkError> {
Ok(())
}
}
#[test]
fn uncertain_and_already_fenced_groups_release_admission_and_point_ownership() {
for concurrency in [
ConcurrencyControl::Exclusive,
ConcurrencyControl::Pessimistic,
] {
let db = Engine::<u32, _>::open(
Unavailable,
EngineOptions {
concurrency,
..Default::default()
},
)
.unwrap();
let mut first = db.begin().unwrap();
first.create_link(0, 0).unwrap();
assert!(matches!(
db.commit_group(vec![first]).unwrap()[0],
GroupCommitOutcome::Indeterminate(_)
));
let state = db.state.lock().unwrap();
assert_eq!(state.active, 0);
assert!(state.write_owners.is_empty());
assert!(state.changed.is_empty());
drop(state);
let db = Engine::<u32, _>::open(
Unavailable,
EngineOptions {
concurrency,
..Default::default()
},
)
.unwrap();
let mut held = db.begin().unwrap();
held.create_link(0, 0).unwrap();
assert!(db.with_log::<()>(|_| Err(error("unknown"))).is_err());
assert!(db.commit_group(vec![held]).is_err());
let state = db.state.lock().unwrap();
assert_eq!(state.active, 0);
assert!(state.write_owners.is_empty());
}
}
}