use std::collections::HashMap;
use std::io::{BufRead, Write};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Duration;
use tilo_lock::MutexRecover as _;
use super::{answer_message, frame_line, line_refusal};
use crate::outcome::InvocationId;
use crate::remote::refuse;
use crate::remote::{
ActionCallError, ActionProvider, Caller, ContentGetAnswer, HostedContentPuts,
MAX_REMOTE_ACTION_FRAME_BYTES, RemoteActionFrame, RemoteActionMessage,
};
const WAITING_AT_ONCE: usize = 64;
pub fn serve_lines<H: ActionProvider + Clone + Send + Sync + 'static>(
tree: impl FnMut(&RemoteActionFrame) -> Result<(H, Caller), ActionCallError>,
uploads: &HostedContentPuts,
first: Option<Vec<u8>>,
input: impl BufRead,
output: impl Write + Send + 'static,
slack: Duration,
) {
serve_lines_with(tree, uploads, first, input, output, slack, None);
}
pub enum Take {
NotCall,
Silent,
Answer(String),
}
pub trait CallLanes: Send + Sync {
fn take(&self, message: &RemoteActionMessage) -> Take;
}
pub fn serve_lines_with<H: ActionProvider + Clone + Send + Sync + 'static>(
mut tree: impl FnMut(&RemoteActionFrame) -> Result<(H, Caller), ActionCallError>,
uploads: &HostedContentPuts,
first: Option<Vec<u8>>,
mut input: impl BufRead,
output: impl Write + Send + 'static,
slack: Duration,
lanes: Option<&dyn CallLanes>,
) {
let connection = Arc::new(Connection {
output: Mutex::new(output),
waiting: AtomicUsize::new(0),
in_flight: Mutex::default(),
});
let mut next = first;
loop {
let line = match next.take() {
Some(line) => line,
None => match read_line(&mut input) {
Ok(Some(line)) => line,
Ok(None) | Err(_) => break,
},
};
let frame = match RemoteActionFrame::decode(&line) {
Ok(frame) => frame,
Err(error) => {
if !write_line(
&connection.output,
&line_refusal("parse_error", &error.to_string()),
) {
break;
}
continue;
}
};
if let Some(lanes) = lanes {
match lanes.take(&frame.message) {
Take::NotCall => {}
Take::Silent => continue,
Take::Answer(answer) => {
if !write_line(&connection.output, &answer) {
break;
}
continue;
}
}
}
let (provider, caller) = match tree(&frame) {
Ok(served) => served,
Err(refusal) => {
let answer = refuse(&frame.message, refusal).map_or_else(
|| line_refusal("parse_error", "a frame sent here asks something"),
frame_line,
);
if !write_line(&connection.output, &answer) {
break;
}
continue;
}
};
let (provider, caller) = (&provider, &caller);
let invocation = invocation_of(&frame.message);
if let Some(invocation) = invocation {
connection
.in_flight
.lock_recover()
.insert(invocation, (provider.clone(), caller.clone()));
}
match answer_frame(
provider,
caller,
uploads,
frame.message,
slack,
&connection,
invocation,
) {
Answered::Spawned => {}
Answered::Ended => break,
Answered::Line(answer) => {
if !write_line(&connection.output, &answer) {
break;
}
}
}
}
let running: Vec<(InvocationId, H, Caller)> = connection
.in_flight
.lock_recover()
.drain()
.map(|(invocation, (provider, caller))| (invocation, provider, caller))
.collect();
for (invocation, provider, caller) in running {
let _cancelling = provider.cancel(&caller, &invocation);
}
}
struct Connection<H, W> {
output: Mutex<W>,
waiting: AtomicUsize,
in_flight: Mutex<HashMap<InvocationId, (H, Caller)>>,
}
enum Answered {
Spawned,
Line(String),
Ended,
}
fn answer_frame<H: ActionProvider + Clone + Send + Sync + 'static, W: Write + Send + 'static>(
provider: &H,
caller: &Caller,
uploads: &HostedContentPuts,
message: RemoteActionMessage,
slack: Duration,
connection: &Arc<Connection<H, W>>,
invocation: Option<InvocationId>,
) -> Answered {
let line = match message {
RemoteActionMessage::ContentPutRequest { correlation, step } => {
let result = uploads.step(&|target| provider.content_puts_for(target), caller, step);
frame_line(RemoteActionMessage::ContentPutResponse {
correlation,
result,
})
}
RemoteActionMessage::ContentGetRequest {
correlation,
content,
offset,
length,
} => {
let result = provider
.content_gets()
.ok_or_else(no_content_store)
.and_then(|store| store.get(caller, &content, offset, length))
.and_then(ContentGetAnswer::new);
frame_line(RemoteActionMessage::ContentGetResponse {
correlation,
result,
})
}
message
if message.waits() && connection.waiting.load(Ordering::Relaxed) < WAITING_AT_ONCE =>
{
connection.waiting.fetch_add(1, Ordering::Relaxed);
let (provider, caller) = (provider.clone(), caller.clone());
let connection = Arc::clone(connection);
let spawned = std::thread::Builder::new()
.name("tilo-line-wait".into())
.spawn(move || {
let answer = answer_message(&provider, &caller, message, slack);
if let Some(invocation) = invocation
&& answers_the_end(&answer)
{
connection.in_flight.lock_recover().remove(&invocation);
}
write_line(&connection.output, &answer);
connection.waiting.fetch_sub(1, Ordering::Relaxed);
});
return if spawned.is_err() {
Answered::Ended
} else {
Answered::Spawned
};
}
message => answer_message(provider, caller, message, slack),
};
if let Some(invocation) = invocation
&& answers_the_end(&line)
{
connection.in_flight.lock_recover().remove(&invocation);
}
Answered::Line(line)
}
fn invocation_of(message: &RemoteActionMessage) -> Option<InvocationId> {
match message {
RemoteActionMessage::Invoke(request) => Some(request.invocation),
_ => None,
}
}
fn answers_the_end(answer: &str) -> bool {
let Ok(frame) = RemoteActionFrame::decode(answer.as_bytes()) else {
return true;
};
match frame.message {
RemoteActionMessage::Receipt(receipt) => receipt.status.is_terminal(),
RemoteActionMessage::Update { state, .. } => state.is_none_or(|state| state.is_terminal()),
_ => true,
}
}
fn no_content_store() -> ActionCallError {
ActionCallError::Unavailable {
reason: "no content store is open here".into(),
}
}
fn write_line(output: &Mutex<impl Write>, line: &str) -> bool {
let mut output = output.lock_recover();
writeln!(output, "{line}")
.and_then(|()| output.flush())
.is_ok()
}
fn read_line(input: &mut impl BufRead) -> std::io::Result<Option<Vec<u8>>> {
let mut line = Vec::new();
let limit = u64::try_from(MAX_REMOTE_ACTION_FRAME_BYTES.saturating_add(1)).unwrap_or(u64::MAX);
let read = std::io::Read::take(&mut *input, limit).read_until(b'\n', &mut line)?;
if read == 0 {
return Ok(None);
}
if line.last() == Some(&b'\n') {
line.pop();
return Ok(Some(line));
}
if line.len() <= MAX_REMOTE_ACTION_FRAME_BYTES {
return Ok(Some(line));
}
let mut rest = Vec::new();
loop {
rest.clear();
let read = std::io::Read::take(&mut *input, limit).read_until(b'\n', &mut rest)?;
if read == 0 || rest.last() == Some(&b'\n') {
return Ok(Some(Vec::new()));
}
}
}
#[cfg(test)]
#[cfg(unix)]
mod tests {
use super::*;
use crate::api::ActionAddress;
use crate::outcome::{ActionOutput, InvocationId};
use crate::remote::{
CataloguePage, CatalogueQuery, Completer, ContentGets, ContentPiece, ContentPut,
ContentPuts, InvocationReceipt, InvocationState, InvokeRequest, LineClient, Pending,
ReceiptStatus, RemoteActionTarget, TestProvider, pending,
};
use std::collections::HashMap;
use std::io::BufReader;
use std::os::unix::net::UnixStream;
#[derive(Clone)]
struct Kept {
actions: Arc<TestProvider>,
store: Arc<Mutex<HashMap<String, Vec<u8>>>>,
}
struct Upload {
store: Arc<Mutex<HashMap<String, Vec<u8>>>>,
size: u64,
bytes: Vec<u8>,
}
impl ContentPut for Upload {
fn size(&self) -> u64 {
self.size
}
fn written(&self) -> u64 {
self.bytes.len() as u64
}
fn append(&mut self, offset: u64, chunk: &[u8]) -> Result<u64, ActionCallError> {
if offset != self.written() {
return Err(ActionCallError::InvalidRequest {
reason: "a piece out of order".into(),
});
}
self.bytes.extend_from_slice(chunk);
Ok(self.written())
}
fn finish(self: Box<Self>) -> Result<String, ActionCallError> {
if self.written() != self.size {
return Err(ActionCallError::InvalidRequest {
reason: "an upload finished short".into(),
});
}
let content = format!("tilo.content:{}", blake3::hash(&self.bytes).to_hex());
self.store
.lock_recover()
.insert(content.clone(), self.bytes);
Ok(content)
}
}
impl ContentPuts for Kept {
fn begin(&self, _: &Caller, size: u64) -> Result<Box<dyn ContentPut>, ActionCallError> {
Ok(Box::new(Upload {
store: Arc::clone(&self.store),
size,
bytes: Vec::new(),
}))
}
fn have(&self, _: &Caller, hashes: &[String]) -> Result<Vec<bool>, ActionCallError> {
let store = self.store.lock_recover();
Ok(hashes
.iter()
.map(|hash| store.contains_key(&format!("tilo.content:{hash}")))
.collect())
}
}
impl ContentGets for Kept {
fn get(
&self,
_: &Caller,
content: &str,
offset: u64,
length: u64,
) -> Result<ContentPiece, ActionCallError> {
let bytes = self
.store
.lock_recover()
.get(content)
.cloned()
.ok_or_else(|| ActionCallError::InvalidRequest {
reason: "no such content".into(),
})?;
let length = if length == 0 {
300_000
} else {
length.min(300_000)
};
let start = usize::try_from(offset).unwrap().min(bytes.len());
let end = start
.saturating_add(usize::try_from(length).unwrap())
.min(bytes.len());
Ok(ContentPiece {
size: bytes.len() as u64,
offset,
bytes: bytes[start..end].to_vec(),
})
}
}
impl ActionProvider for Kept {
fn query(
&self,
caller: &Caller,
query: CatalogueQuery,
) -> Pending<Result<CataloguePage, ActionCallError>> {
self.actions.query(caller, query)
}
fn invoke(
&self,
caller: &Caller,
request: InvokeRequest,
) -> Pending<Result<InvocationReceipt, ActionCallError>> {
self.actions.invoke(caller, request)
}
fn observe(&self, caller: &Caller, id: &InvocationId) -> Option<InvocationState> {
self.actions.observe(caller, id)
}
fn cancel(
&self,
caller: &Caller,
id: &InvocationId,
) -> Pending<Result<(), ActionCallError>> {
self.actions.cancel(caller, id)
}
fn content_puts(&self) -> Option<Arc<dyn ContentPuts>> {
Some(Arc::new(self.clone()))
}
fn content_gets(&self) -> Option<Arc<dyn ContentGets>> {
Some(Arc::new(self.clone()))
}
}
fn connected<H: ActionProvider + Clone + Send + Sync + 'static>(
provider: H,
caller: Caller,
) -> LineClient {
let (near, far) = UnixStream::pair().unwrap();
std::thread::spawn(move || {
let output = far.try_clone().unwrap();
serve_lines(
|_| Ok((provider.clone(), caller.clone())),
&HostedContentPuts::default(),
None,
BufReader::new(far),
output,
Duration::ZERO,
);
});
LineClient::of_stream(near).unwrap()
}
fn target() -> RemoteActionTarget {
RemoteActionTarget {
provider: "notes".into(),
address: ActionAddress::new("notes", "create"),
}
}
#[test]
fn answers_reach_their_asks_as_the_serving_ends_caller() {
let actions = Arc::new(
TestProvider::new()
.with_catalogue(|_, query| {
Ok(CataloguePage {
revision: "r1".into(),
..CataloguePage::from_rows("r1".into(), Vec::new(), &query)
})
})
.completing(|request| ActionOutput {
value: Some(serde_json::json!({ "asked": request.invocation.0.to_string() })),
resources: Vec::new(),
}),
);
let served = Caller::local_app("notes").in_workspace("w");
let client = connected(
Kept {
actions: Arc::clone(&actions),
store: Arc::default(),
},
served.clone(),
);
let threads: Vec<_> = (0..8)
.map(|_| {
let client = client.clone();
std::thread::spawn(move || {
let request = InvokeRequest::plain(target(), serde_json::Map::new());
let invocation = request.invocation;
let receipt = client
.invoke(&Caller::local_person("someone else"), request)
.wait_timeout(Duration::from_secs(10))
.ok()
.flatten()
.expect("answered")
.unwrap();
assert_eq!(receipt.invocation, invocation);
let ReceiptStatus::Completed { output } = receipt.status else {
panic!("not completed: {receipt:?}");
};
assert_eq!(
output.value.unwrap()["asked"],
invocation.0.to_string(),
"each receipt answers its own invocation"
);
})
})
.collect();
let page = client
.query(
&Caller::local_person("someone else"),
CatalogueQuery::default(),
)
.wait_timeout(Duration::from_secs(10))
.ok()
.flatten()
.expect("answered")
.unwrap();
assert_eq!(page.revision, "r1");
for thread in threads {
thread.join().unwrap();
}
assert_eq!(actions.asked().len(), 8);
assert!(
actions.callers().iter().all(|caller| *caller == served),
"every call is the serving end's caller: {:?}",
actions.callers()
);
}
#[test]
fn content_goes_up_in_frames_and_comes_back_whole() {
let kept = Kept {
actions: Arc::new(TestProvider::new()),
store: Arc::default(),
};
let client = connected(kept, Caller::local_person("ada"));
let caller = Caller::local_person("ada");
let large: Vec<u8> = (0..1_000_000_u32).map(|at| (at % 251) as u8).collect();
let small = vec![b"one".to_vec(), b"two".to_vec(), large.clone()];
let puts = client.content_puts().unwrap();
let contents = puts.put_many(&caller, &small).unwrap();
assert_eq!(contents.len(), 3);
let present = puts
.have(&caller, &[contents[0].clone(), "0".repeat(64)])
.unwrap();
assert_eq!(
present,
[true, false],
"a put file is there, another is not"
);
let gets = client.content_gets().unwrap();
let whole = gets
.get(&caller, &contents[2], 0, large.len() as u64)
.unwrap();
assert_eq!(whole.size, large.len() as u64);
assert!(whole.bytes == large, "the large file comes back whole");
let first = gets.get(&caller, &contents[2], 10, 0).unwrap();
assert_eq!(first.bytes.len(), 300_000, "zero asks for one piece");
assert_eq!(first.bytes[..], large[10..300_010]);
assert_eq!(gets.get(&caller, &contents[1], 0, 0).unwrap().bytes, b"two");
let refused = gets.get(&caller, "tilo.content:nothing", 0, 0).unwrap_err();
assert!(
matches!(refused, ActionCallError::InvalidRequest { .. }),
"{refused:?}"
);
}
#[test]
fn a_closed_connection_answers_every_ask() {
let (near, far) = UnixStream::pair().unwrap();
let client = LineClient::of_stream(near).unwrap();
let mut far_writer = far.try_clone().unwrap();
let mut reader = BufReader::new(far);
let asked = client.query(&Caller::local_person("ada"), CatalogueQuery::default());
let mut line = String::new();
reader.read_line(&mut line).unwrap();
assert!(RemoteActionFrame::decode(line.trim_end().as_bytes()).is_ok());
writeln!(far_writer, "{}", line_refusal("not_permitted", "not yours")).unwrap();
let refused = asked
.wait_timeout(Duration::from_secs(10))
.ok()
.flatten()
.expect("answered")
.unwrap_err();
assert_eq!(
refused,
ActionCallError::Refused {
reason: "not yours".into()
}
);
let later = client
.cancel(&Caller::local_person("ada"), &InvocationId::new())
.wait_timeout(Duration::from_secs(10))
.ok()
.flatten()
.expect("answered")
.unwrap_err();
assert_eq!(later, refused);
drop(client);
let mut rest = String::new();
assert_eq!(reader.read_line(&mut rest).unwrap(), 0, "{rest}");
}
type Held = Completer<Result<InvocationReceipt, ActionCallError>>;
#[derive(Clone, Default)]
struct Waiting {
asked: Arc<Mutex<Vec<InvocationId>>>,
cancels: Arc<Mutex<Vec<InvocationId>>>,
held: Arc<Mutex<Vec<Held>>>,
}
impl ActionProvider for Waiting {
fn query(
&self,
_: &Caller,
_: CatalogueQuery,
) -> Pending<Result<CataloguePage, ActionCallError>> {
Pending::ready(Ok(CataloguePage::default()))
}
fn invoke(
&self,
_: &Caller,
request: InvokeRequest,
) -> Pending<Result<InvocationReceipt, ActionCallError>> {
self.asked.lock_recover().push(request.invocation);
let (complete, answer) = pending();
self.held.lock_recover().push(complete);
answer
}
fn observe(&self, _: &Caller, _: &InvocationId) -> Option<InvocationState> {
None
}
fn cancel(&self, _: &Caller, id: &InvocationId) -> Pending<Result<(), ActionCallError>> {
self.cancels.lock_recover().push(*id);
Pending::ready(Ok(()))
}
}
#[test]
fn a_connection_that_ends_cancels_the_call_it_waited_on() {
let provider = Waiting::default();
let caller = Caller::local_app("notes").in_workspace("w");
let (near, far) = UnixStream::pair().unwrap();
let output = far.try_clone().unwrap();
let serving = provider.clone();
std::thread::spawn(move || {
serve_lines(
move |_| Ok((serving.clone(), caller.clone())),
&HostedContentPuts::default(),
None,
BufReader::new(far),
output,
Duration::ZERO,
);
});
let client = LineClient::of_stream(near).unwrap();
let mut request = InvokeRequest::plain(target(), serde_json::Map::new());
request.wait_ms = Some(0);
let invocation = request.invocation;
let pending = client.invoke(&Caller::local_person("ada"), request);
tilo_test_env::wait_until("the provider to hold the call", || {
provider.asked.lock_recover().contains(&invocation)
});
drop(client);
tilo_test_env::wait_until("the call to be cancelled", || {
provider.cancels.lock_recover().contains(&invocation)
});
drop(pending);
}
}