1use crate::IntoCString;
2use crate::{Aeron, AeronArchive, AeronArchiveAsyncConnect, AeronArchiveContext, AeronCError, AeronContext};
3use log::info;
4use log::{error, warn};
5use regex::Regex;
6use std::backtrace::Backtrace;
7use std::ffi::CString;
8use std::path::Path;
9use std::process::{Child, Command, ExitStatus, Stdio};
10use std::sync::{Arc, Mutex, Weak};
11use std::thread::sleep;
12use std::time::{Duration, Instant};
13use std::{fs, io, panic, process};
14
15pub(crate) fn running_under_valgrind() -> bool {
18 std::env::var_os("RUSTERON_VALGRIND").is_some()
19}
20
21pub(crate) fn valgrind_timeout(base_secs: u64) -> Duration {
24 if running_under_valgrind() {
25 Duration::from_secs(base_secs.saturating_mul(3))
26 } else {
27 Duration::from_secs(base_secs)
28 }
29}
30
31static ARCHIVE_PROCESSES: Mutex<Vec<Weak<Mutex<Child>>>> = Mutex::new(Vec::new());
32
33pub struct EmbeddedArchiveMediaDriverProcess {
34 child: Arc<Mutex<Child>>,
35 owns_driver: bool,
36 pub aeron_dir: CString,
37 pub archive_dir: CString,
38 pub control_request_channel: String,
39 pub control_response_channel: String,
40 pub recording_events_channel: String,
41}
42
43impl EmbeddedArchiveMediaDriverProcess {
44 pub fn build_and_start(
73 aeron_dir: &str,
74 archive_dir: &str,
75 control_request_channel: &str,
76 control_response_channel: &str,
77 recording_events_channel: &str,
78 ) -> io::Result<Self> {
79 let path = std::path::MAIN_SEPARATOR;
80 let gradle = if cfg!(target_os = "windows") {
81 &format!("{}{path}aeron{path}gradlew.bat", env!("CARGO_MANIFEST_DIR"),)
82 } else {
83 "./gradlew"
84 };
85 let dir = format!("{}{path}aeron", env!("CARGO_MANIFEST_DIR"),);
86 info!("running {} in {}", gradle, dir);
87
88 if !Path::new(&format!(
89 "{}{path}aeron{path}aeron-all{path}build{path}libs",
90 env!("CARGO_MANIFEST_DIR")
91 ))
92 .exists()
93 {
94 Command::new(&gradle)
95 .current_dir(dir)
96 .args([":aeron-samples:jar", ":aeron-archive:jar", ":aeron-all:build"])
97 .stdout(Stdio::inherit())
98 .stderr(Stdio::inherit())
99 .spawn()?
100 .wait()?;
101 }
102
103 return Self::start(
104 &aeron_dir,
105 archive_dir,
106 control_request_channel,
107 control_response_channel,
108 recording_events_channel,
109 );
110 }
111
112 pub fn run_aeron_stats(&self) -> std::io::Result<Child> {
113 let main_dir = env!("CARGO_MANIFEST_DIR");
114 let dir = format!("{}/{}", main_dir, &self.aeron_dir.to_str().unwrap());
115 info!("running 'just aeron-stat {}'", dir);
116 Command::new("just")
117 .args(["aeron-stat", dir.as_str()])
118 .stdout(Stdio::inherit())
119 .stderr(Stdio::inherit())
120 .spawn()
121 }
122
123 pub fn archive_context(&self, aeron: &Aeron) -> Result<AeronArchiveContext, AeronCError> {
130 let context = AeronArchiveContext::new()?;
131 context.set_aeron(aeron)?;
132 context.set_control_request_channel(&self.control_request_channel.as_str().into_c_string())?;
133 context.set_control_response_channel(&self.control_response_channel.as_str().into_c_string())?;
134 context.set_recording_events_channel(&self.recording_events_channel.as_str().into_c_string())?;
135 Ok(context)
136 }
137
138 pub fn archive_connect(&self) -> Result<(AeronArchive, Aeron), io::Error> {
139 let start = Instant::now();
140 let deadline = valgrind_timeout(30);
141 while start.elapsed() < deadline {
142 if let Ok(aeron_context) = AeronContext::new() {
143 aeron_context.set_dir(&self.aeron_dir).expect("invalid dir");
144 aeron_context
145 .set_client_name(&CString::new("unit_test_client")?)
146 .expect("invalid client name");
147 if let Ok(aeron) = Aeron::new(&aeron_context) {
148 if aeron.start().is_ok() {
149 if let Ok(archive_context) = self.archive_context(&aeron) {
150 if let Ok(connect) = AeronArchiveAsyncConnect::new_with_aeron(&archive_context, &aeron) {
151 if let Ok(archive) = connect.poll_blocking(valgrind_timeout(10)) {
152 let i = archive.get_archive_id();
153 assert!(i > 0);
154 info!("aeron archive media driver is up [connected with archive id {i}]");
155 sleep(Duration::from_millis(100));
156 return Ok((archive, aeron));
157 };
158 }
159 }
160 error!("aeron error: {}", Aeron::errmsg());
161 }
162 }
163 }
164 info!("waiting for aeron to start up, retrying...");
165 }
166
167 assert!(start.elapsed() < deadline, "failed to start up aeron media driver");
168
169 return Err(std::io::Error::other("unable to start up aeron media driver client"));
170 }
171
172 pub fn start(
195 aeron_dir: &str,
196 archive_dir: &str,
197 control_request_channel: &str,
198 control_response_channel: &str,
199 recording_events_channel: &str,
200 ) -> io::Result<Self> {
201 Self::start_process(
202 aeron_dir,
203 archive_dir,
204 control_request_channel,
205 control_response_channel,
206 recording_events_channel,
207 None,
208 )
209 }
210
211 #[cfg(test)]
213 pub(crate) fn start_on_existing_driver(
214 &self,
215 archive_dir: &str,
216 control_request_channel: &str,
217 control_response_channel: &str,
218 recording_events_channel: &str,
219 archive_id: i64,
220 ) -> io::Result<Self> {
221 Self::start_process(
222 self.aeron_dir.to_str().unwrap(),
223 archive_dir,
224 control_request_channel,
225 control_response_channel,
226 recording_events_channel,
227 Some(archive_id),
228 )
229 }
230
231 fn start_process(
232 aeron_dir: &str,
233 archive_dir: &str,
234 control_request_channel: &str,
235 control_response_channel: &str,
236 recording_events_channel: &str,
237 existing_driver_archive_id: Option<i64>,
238 ) -> io::Result<Self> {
239 if existing_driver_archive_id.is_none() {
240 Self::clean_directory(aeron_dir)?;
241 }
242 Self::clean_directory(archive_dir)?;
243
244 fs::create_dir_all(aeron_dir)?;
246 fs::create_dir_all(archive_dir)?;
247
248 let binding = fs::read_dir(format!("{}/aeron/aeron-all/build/libs", env!("CARGO_MANIFEST_DIR")))?
249 .filter(|f| f.is_ok())
250 .map(|f| f.unwrap())
251 .filter(|f| f.file_name().to_string_lossy().to_string().ends_with(".jar"))
252 .next()
253 .unwrap()
254 .path();
255 let mut jar_path = binding.to_str().unwrap();
256 assert!(fs::exists(jar_path).unwrap_or_default());
257 let mut args = vec![];
258 let separator = if cfg!(target_os = "windows") { ";" } else { ":" };
259
260 let combined_jars = format!(
261 "{}{separator}{}",
262 jar_path,
263 jar_path.replace("aeron-all", "aeron-archive")
264 );
265 jar_path = &combined_jars;
266
267 args.push("--add-opens".to_string());
268 args.push("java.base/jdk.internal.misc=ALL-UNNAMED".to_string());
269 args.push("-cp".to_string());
270 args.push(jar_path.to_string());
271 args.push(format!("-Daeron.dir={}", aeron_dir));
272 args.push(format!("-Daeron.archive.dir={}", archive_dir));
273 args.push("-Daeron.spies.simulate.connection=true".to_string());
274 args.push("-Daeron.event.log=all".to_string());
275 args.push("-Daeron.event.log.disable=FRAME_IN,FRAME_OUT".to_string());
276 args.push("-Daeron.event.archive.log=all".to_string());
277 args.push("-Daeron.event.cluster.log=all".to_string());
278 args.push("-Dagrona.disable.bounds.checks=true".to_string());
279 args.push(format!("-Daeron.archive.control.channel={}", control_request_channel));
280 args.push(format!(
281 "-Daeron.archive.control.response.channel={}",
282 control_response_channel
283 ));
284 args.push(format!(
285 "-Daeron.archive.recording.events.channel={}",
286 recording_events_channel
287 ));
288 args.push("-Daeron.archive.replication.channel=aeron:udp?endpoint=localhost:0".to_string());
289 args.push("-Daeron.client.liveness.timeout=60000000000".to_string());
290 args.push("-Daeron.image.liveness.timeout=60000000000".to_string());
291 args.push("-Daeron.publication.unblock.timeout=65000000000".to_string());
292 if let Some(archive_id) = existing_driver_archive_id {
293 args.push(format!("-Daeron.archive.id={archive_id}"));
294 args.push("-Daeron.archive.local.control.stream.id=15402".to_string());
296 args.push("io.aeron.archive.Archive".to_string());
297 } else {
298 args.push("io.aeron.archive.ArchivingMediaDriver".to_string());
299 }
300
301 info!("starting archive media driver [\n\tjava {}\n]", args.join(" "));
302
303 let child = Command::new("java")
304 .args(args)
305 .stdout(Stdio::inherit())
306 .stderr(Stdio::inherit())
307 .spawn()?;
308 let child = Arc::new(Mutex::new(child));
309 let mut processes = ARCHIVE_PROCESSES.lock().expect("archive process registry poisoned");
310 processes.retain(|process| process.strong_count() > 0);
311 processes.push(Arc::downgrade(&child));
312 drop(processes);
313
314 info!(
315 "started archive media driver [{:?}",
316 fs::read_dir(aeron_dir)?.collect::<Vec<_>>()
317 );
318
319 Ok(EmbeddedArchiveMediaDriverProcess {
320 child,
321 owns_driver: existing_driver_archive_id.is_none(),
322 aeron_dir: aeron_dir.into_c_string(),
323 archive_dir: archive_dir.into_c_string(),
324 control_request_channel: control_request_channel.to_string(),
325 control_response_channel: control_response_channel.to_string(),
326 recording_events_channel: recording_events_channel.to_string(),
327 })
328 }
329
330 fn clean_directory(dir: &str) -> io::Result<()> {
331 info!("cleaning directory {}", dir);
332 let path = Path::new(dir);
333 if path.exists() {
334 fs::remove_dir_all(path)?;
335 }
336 Ok(())
337 }
338
339 pub fn kill_all_java_processes() -> io::Result<ExitStatus> {
341 let mut processes = ARCHIVE_PROCESSES.lock().expect("archive process registry poisoned");
342 let mut status = ExitStatus::default();
343 for process in processes.iter().filter_map(Weak::upgrade) {
344 let mut child = process.lock().expect("archive child lock poisoned");
345 if let Some(exited) = child.try_wait()? {
346 status = exited;
347 } else {
348 child.kill()?;
349 status = child.wait()?;
350 }
351 }
352 processes.retain(|process| process.strong_count() > 0);
353 Ok(status)
354 }
355}
356
357impl Drop for EmbeddedArchiveMediaDriverProcess {
359 fn drop(&mut self) {
360 warn!("WARN: stopping aeron archive media driver!!!!");
361 let mut child = self.child.lock().expect("archive child lock poisoned");
362 match child.try_wait() {
363 Ok(Some(_)) => {}
364 Ok(None) => {
365 if let Err(e) = child.kill() {
366 error!("Failed to kill Java process: {}", e);
367 }
368 if let Err(e) = child.wait() {
369 error!("Failed to reap Java process: {}", e);
370 }
371 }
372 Err(e) => error!("Failed to check Java process: {}", e),
373 }
374
375 if self.owns_driver {
377 if let Err(e) = Self::clean_directory(&self.aeron_dir.to_str().unwrap()) {
378 error!("Failed to clean up Aeron directory: {}", e);
379 }
380 }
381 if let Err(e) = Self::clean_directory(&self.archive_dir.to_str().unwrap()) {
382 error!("Failed to clean up Archive directory: {}", e);
383 }
384 }
385}
386
387pub fn java_available() -> bool {
394 Command::new("java")
395 .arg("-version")
396 .stdout(Stdio::null())
397 .stderr(Stdio::null())
398 .status()
399 .is_ok()
400}
401
402#[macro_export]
406macro_rules! skip_unless_java {
407 () => {
408 if !$crate::testing::java_available() {
409 eprintln!("skipping: java not available on PATH");
410 return Ok(());
411 }
412 };
413}
414
415pub fn set_panic_hook() {
416 panic::set_hook(Box::new(|info| {
417 let backtrace = Backtrace::force_capture();
419 error!("Stack trace: {backtrace:#?}");
420
421 let backtrace = format!("{:?}", backtrace);
422 let re = Regex::new(r#"fn: "([^"]+)", file: "([^"]+)", line: (\d+)"#).unwrap();
424
425 for cap in re.captures_iter(&backtrace) {
427 let function = &cap[1];
428 let file = &cap[2];
429 let line = &cap[3];
430 info!("{file}:{line} in {function}");
431 }
432
433 error!("Panic occurred: {:#?}", info);
434
435 if let Some(payload) = info.payload().downcast_ref::<&str>() {
436 error!("Panic message: {}", payload);
437 } else if let Some(payload) = info.payload().downcast_ref::<String>() {
438 error!("Panic message: {}", payload);
439 } else {
440 error!("Panic with non-standard payload: {:?}", info.payload().type_id());
442 }
443
444 warn!("shutdown");
445
446 process::abort();
447 }))
448}
449
450pub fn find_unused_udp_port(start: u16) -> Option<u16> {
452 (start..65535).find(|p| std::net::UdpSocket::bind(("127.0.0.1", *p)).is_ok())
453}
454
455pub fn find_counter_id_by_session_blocking(
458 counters_reader: &crate::AeronCountersReader,
459 session_id: i32,
460 wait: Duration,
461) -> Result<i32, crate::AeronCError> {
462 find_counter_id_blocking(wait, || {
463 crate::RecordingPos::find_counter_id_by_session(counters_reader, session_id)
464 })
465}
466
467pub fn find_counter_id_by_session_in_archive_blocking(
470 counters_reader: &crate::AeronCountersReader,
471 session_id: i32,
472 archive_id: i64,
473 wait: Duration,
474) -> Result<i32, crate::AeronCError> {
475 find_counter_id_blocking(wait, || {
476 crate::RecordingPos::find_counter_id_by_session_in_archive(counters_reader, session_id, archive_id)
477 })
478}
479
480fn find_counter_id_blocking(wait: Duration, mut find: impl FnMut() -> i32) -> Result<i32, crate::AeronCError> {
481 let start = Instant::now();
482 loop {
483 let counter_id = find();
484 if counter_id >= 0 {
485 return Ok(counter_id);
486 }
487 if start.elapsed() >= wait {
488 return Err(crate::AeronCError::from_code(-1));
489 }
490 std::thread::sleep(Duration::from_millis(10));
491 }
492}