diff --git a/crates/chunk-cli/src/local/logs.rs b/crates/chunk-cli/src/local/logs.rs index d03eb1ec..efe4c72f 100644 --- a/crates/chunk-cli/src/local/logs.rs +++ b/crates/chunk-cli/src/local/logs.rs @@ -63,6 +63,37 @@ impl Visit for Line { } } +/// Parsed components of a JVM log line emitted by [`follow_jvms`]. +#[derive(Debug, PartialEq, Eq)] +pub(super) struct JvmLine<'a> { + pub host: &'a str, + pub session: Option<&'a str>, + pub body: &'a str, +} + +/// Splits ` [[session:] ]` into its host prefix, optional session ID, and message body. +pub(super) fn parse_jvm_line(line: &str) -> JvmLine<'_> { + let Some((host, rest)) = line.split_once(' ') else { + return JvmLine { host: "", session: None, body: line }; + }; + if let Some((session, body)) = strip_session_tag(rest) { + JvmLine { host, session: Some(session), body } + } else { + JvmLine { host, session: None, body: rest } + } +} + +fn strip_session_tag(rest: &str) -> Option<(&str, &str)> { + let after_prefix = rest.strip_prefix("[session:")?; + let end = after_prefix.find(']')?; + let id = &after_prefix[..end]; + if id.is_empty() || id.len() > 128 || !id.bytes().all(|b| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_')) { + return None; + } + let trailing = &after_prefix[end + 1..]; + if trailing.is_empty() { Some((id, "")) } else { trailing.strip_prefix(' ').map(|body| (id, body)) } +} + /// Follows every `/*.jvm.log`, skipping output written before the session started. pub(super) async fn follow_jvms(nodes: PathBuf, reporter: Reporter, stop: CancellationToken) { let mut offsets = HashMap::new(); @@ -72,7 +103,8 @@ pub(super) async fn follow_jvms(nodes: PathBuf, reporter: Reporter, stop: Cancel let offset = offsets.entry(path.clone()).or_insert_with(|| { if first { std::fs::metadata(&path).map_or(0, |metadata| metadata.len()) } else { 0 } }); - let host = path.file_name().and_then(|name| name.to_str()).unwrap_or_default(); + let file = path.file_name().and_then(|name| name.to_str()).unwrap_or_default(); + let host = file.strip_suffix(".jvm.log").unwrap_or(file); for line in read_lines(&path, offset) { reporter.log(Source::Jvm, format!("{} {line}", &host[..host.len().min(8)])); } @@ -112,3 +144,32 @@ fn read_lines(path: &Path, offset: &mut u64) -> Vec { *offset += end as u64; String::from_utf8_lossy(&buffer[..end]).lines().map(str::to_owned).collect() } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parses_tagged_and_untagged_jvm_lines() { + assert_eq!( + parse_jvm_line("5a9e4aba [session:f47ac10b-58cc] player joined"), + JvmLine { host: "5a9e4aba", session: Some("f47ac10b-58cc"), body: "player joined" } + ); + assert_eq!( + parse_jvm_line("5a9e4aba [session:sess_1]"), + JvmLine { host: "5a9e4aba", session: Some("sess_1"), body: "" } + ); + assert_eq!( + parse_jvm_line("5a9e4aba JVM started"), + JvmLine { host: "5a9e4aba", session: None, body: "JVM started" } + ); + assert_eq!( + parse_jvm_line("5a9e4aba [session:invalid id] hi"), + JvmLine { host: "5a9e4aba", session: None, body: "[session:invalid id] hi" } + ); + assert_eq!( + parse_jvm_line("5a9e4aba [session:] hi"), + JvmLine { host: "5a9e4aba", session: None, body: "[session:] hi" } + ); + } +} diff --git a/crates/chunk-cli/src/local/tui/model.rs b/crates/chunk-cli/src/local/tui/model.rs index bf35be6e..05fc69f8 100644 --- a/crates/chunk-cli/src/local/tui/model.rs +++ b/crates/chunk-cli/src/local/tui/model.rs @@ -3,9 +3,12 @@ use std::{ time::{Duration, Instant}, }; -use chunk_proto::sync::v1::Node; +use chunk_proto::sync::v1::{Node, NodePhase, NodeSession}; -use super::super::report::{Deployment, Event, Source, Step}; +use super::super::{ + logs, + report::{Deployment, Event, Source, Step}, +}; mod players; @@ -45,6 +48,18 @@ pub(super) enum Focus { Nodes, } +/// Which JVM log scope is selected in the JVM tab. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub(super) enum JvmSelection { + #[default] + All, + Node(String), + Session { + host: String, + session: String, + }, +} + pub(super) struct Progress { pub name: &'static str, pub state: Step, @@ -89,8 +104,8 @@ pub(super) struct Model { /// Lines scrolled up from the newest output of the visible log. pub scroll: usize, pub focus: Focus, - /// Full host identity; None selects the combined JVM output. - pub node: Option, + /// Selected node or nested session; `All` selects the combined JVM output. + pub selection: JvmSelection, pub show_details: bool, /// Player ID the players tab has selected; the first match when absent or gone. pub player: Option, @@ -121,7 +136,7 @@ impl Model { selected: Source::Build.index(), scroll: 0, focus: Focus::Logs, - node: None, + selection: JvmSelection::All, show_details: false, player: None, filter: String::new(), @@ -136,9 +151,18 @@ impl Model { Event::Log { source, line } => self.push(source, line), Event::Deployments(deployments) => { self.deployments = deployments; - if self.node.is_some() && self.selected_node().is_none() { - self.node = None; - self.scroll = 0; + match &self.selection { + JvmSelection::All => {} + JvmSelection::Node(_) if self.selected_node().is_some() => {} + JvmSelection::Session { .. } if self.selected_session().is_some() => {} + JvmSelection::Session { host, .. } if self.find_node(host).is_some() => { + self.selection = JvmSelection::Node(host.clone()); + self.scroll = 0; + } + JvmSelection::Node(_) | JvmSelection::Session { .. } => { + self.selection = JvmSelection::All; + self.scroll = 0; + } } } } @@ -224,18 +248,33 @@ impl Model { } } - /// Selecting a node immediately previews its logs; row zero is all nodes. + /// Selecting a node or nested session immediately previews its logs; row zero is all nodes. pub fn move_node(&mut self, offset: isize) { if !self.on_jvm() { return; } - let hosts = self.hosts(); - let selected = self.node.as_deref().and_then(|node| hosts.iter().position(|host| *host == node)); - let row = selected.map_or(0, |index| index + 1).saturating_add_signed(offset).min(hosts.len()); - self.node = row.checked_sub(1).map(|index| hosts[index].to_owned()); + let entries = self.jvm_entries(); + let current = entries.iter().position(|entry| entry == &self.selection).unwrap_or(0); + let next = current.saturating_add_signed(offset).min(entries.len().saturating_sub(1)); + self.selection = entries[next].clone(); self.scroll = 0; } + pub fn jvm_entries(&self) -> Vec { + let mut entries = vec![JvmSelection::All]; + for deployment in &self.deployments { + for (host, node) in &deployment.nodes { + entries.push(JvmSelection::Node(host.clone())); + if node.phase() != NodePhase::Stopped { + for session in &node.sessions { + entries.push(JvmSelection::Session { host: host.clone(), session: session.id.clone() }); + } + } + } + } + entries + } + pub fn open_node(&mut self) { self.focus = Focus::Logs; } @@ -255,17 +294,41 @@ impl Model { self.deployments.iter().flat_map(|deployment| &deployment.nodes).map(|(host, _)| host.as_str()).collect() } - pub fn selected_node(&self) -> Option<(&Deployment, &(String, Node))> { - let host = self.node.as_deref()?; + fn find_node(&self, host: &str) -> Option<(&Deployment, &(String, Node))> { self.deployments .iter() .find_map(|deployment| deployment.nodes.iter().find(|(id, _)| id == host).map(|node| (deployment, node))) } + pub fn selected_node(&self) -> Option<(&Deployment, &(String, Node))> { + let JvmSelection::Node(host) = &self.selection else { return None }; + self.find_node(host) + } + + pub fn selected_session(&self) -> Option<(&Deployment, &(String, Node), &NodeSession)> { + let JvmSelection::Session { host, session } = &self.selection else { return None }; + let (deployment, node) = self.find_node(host)?; + if node.1.phase() == NodePhase::Stopped { + return None; + } + let open = node.1.sessions.iter().find(|candidate| candidate.id == *session)?; + Some((deployment, node, open)) + } + + pub fn sessions(&self) -> usize { + self.deployments + .iter() + .flat_map(|deployment| &deployment.nodes) + .filter(|(_, node)| node.phase() != NodePhase::Stopped) + .map(|(_, node)| node.sessions.len()) + .sum() + } + pub fn players(&self) -> u32 { self.deployments .iter() .flat_map(|deployment| &deployment.nodes) + .filter(|(_, node)| node.phase() != NodePhase::Stopped) .filter_map(|(_, node)| node.health.as_ref()) .map(|health| health.players) .sum() @@ -285,10 +348,19 @@ impl Model { } fn shows(&self, line: &str) -> bool { - !self.on_jvm() - || self.node.as_ref().is_none_or(|host| { - line.strip_prefix(&host[..host.len().min(8)]).is_some_and(|rest| rest.starts_with(' ')) - }) + if !self.on_jvm() { + return true; + } + match &self.selection { + JvmSelection::All => true, + // Node views keep untagged JVM-wide lines (startup, GC) alongside every session on that host. + JvmSelection::Node(host) => logs::parse_jvm_line(line).host == &host[..host.len().min(8)], + // Session views show only lines tagged with that session; untagged JVM lines stay at the node level. + JvmSelection::Session { host, session } => { + let parsed = logs::parse_jvm_line(line); + parsed.host == &host[..host.len().min(8)] && parsed.session == Some(session.as_str()) + } + } } } diff --git a/crates/chunk-cli/src/local/tui/model/tests.rs b/crates/chunk-cli/src/local/tui/model/tests.rs index 0440c3a3..495b3849 100644 --- a/crates/chunk-cli/src/local/tui/model/tests.rs +++ b/crates/chunk-cli/src/local/tui/model/tests.rs @@ -1,4 +1,4 @@ -use chunk_proto::sync::v1::{Node, OperatorPlayer, SessionDemand}; +use chunk_proto::sync::v1::{JvmSessionPhase, Node, NodePhase, NodeSession, OperatorPlayer, SessionDemand}; use crate::local::Command; @@ -57,12 +57,12 @@ fn node_selection_previews_logs_and_survives_refreshes_and_tab_changes() { assert_eq!(model.focus, Focus::Nodes); assert_eq!(model.visible().len(), 1); model.move_node(-1); - assert!(model.node.is_none()); + assert_eq!(model.selection, JvmSelection::All); assert_eq!(model.visible().len(), 3); model.move_node(1); nodes(&mut model, &["aaaaaaaa-1111"]); - assert!(model.node.is_none()); + assert_eq!(model.selection, JvmSelection::All); assert_eq!(model.scroll, 0); } @@ -79,7 +79,7 @@ fn failed_reload_preserves_the_dashboard_and_selected_node() { state: Step::Failed("compile failed\nthe previous release keeps serving".into()), }); assert!(model.ready()); - assert_eq!(model.node.as_deref(), Some("aaaaaaaa-1111")); + assert_eq!(model.selection, JvmSelection::Node("aaaaaaaa-1111".into())); assert!(matches!(model.step("Compile").unwrap().state, Step::Failed(_))); assert!(model.step("Compile").unwrap().started.is_none()); model.apply(Event::Step { name: "Reload", state: Step::Running("Lobby.kt".into()) }); @@ -178,3 +178,83 @@ fn search_keeps_a_matching_selection_and_the_move_form_targets_it() { model.cancel(); assert!(model.filter.is_empty() && model.input.is_none()); } + +#[test] +fn session_selection_filters_tagged_lines_keeps_untagged_at_node_and_falls_back_to_parent() { + let session = |id: &str, key: &str, players: u32| NodeSession { + id: id.into(), + session_type: "arena/default".into(), + key: key.into(), + players, + phase: JvmSessionPhase::Ready.into(), + }; + let mut model = Model::new(); + ready(&mut model); + model.apply(Event::Deployments(vec![Deployment { + id: "release".into(), + state: "current".into(), + nodes: vec![ + ( + "aaaaaaaa-1111".into(), + Node { + app: "arena".into(), + phase: NodePhase::Online.into(), + sessions: vec![session("sess-1111-aaaa", "duel-1", 2), session("sess-2222-bbbb", "duel-2", 1)], + ..Default::default() + }, + ), + ( + "bbbbbbbb-2222".into(), + Node { + app: "lobby".into(), + phase: NodePhase::Stopped.into(), + sessions: vec![session("stale-session", "main", 5)], + ..Default::default() + }, + ), + ], + players: Vec::new(), + destinations: Vec::new(), + }])); + + assert_eq!(model.sessions(), 2); + log(&mut model, Source::Jvm, "aaaaaaaa JVM started"); + log(&mut model, Source::Jvm, "aaaaaaaa [session:sess-1111-aaaa] round 1"); + log(&mut model, Source::Jvm, "aaaaaaaa [session:sess-2222-bbbb] round 2"); + log(&mut model, Source::Jvm, "bbbbbbbb lobby stopped"); + + // All nodes shows everything. + assert_eq!(model.visible().len(), 4); + + // Row 1 selects node aaaaaaaa: includes untagged startup + both sessions. + model.move_node(1); + assert_eq!(model.selection, JvmSelection::Node("aaaaaaaa-1111".into())); + assert_eq!(model.visible().len(), 3); + + // Row 2 selects sess-1111-aaaa: only lines tagged with that session (untagged stays at node level). + model.move_node(1); + assert_eq!( + model.selection, + JvmSelection::Session { host: "aaaaaaaa-1111".into(), session: "sess-1111-aaaa".into() } + ); + assert_eq!(model.visible(), ["aaaaaaaa [session:sess-1111-aaaa] round 1"]); + + // When the selected session ends, selection falls back to its parent node. + model.apply(Event::Deployments(vec![Deployment { + id: "release".into(), + state: "current".into(), + nodes: vec![( + "aaaaaaaa-1111".into(), + Node { + app: "arena".into(), + phase: NodePhase::Online.into(), + sessions: vec![session("sess-2222-bbbb", "duel-2", 1)], + ..Default::default() + }, + )], + players: Vec::new(), + destinations: Vec::new(), + }])); + assert_eq!(model.selection, JvmSelection::Node("aaaaaaaa-1111".into())); + assert_eq!(model.visible().len(), 3); +} diff --git a/crates/chunk-cli/src/local/tui/view.rs b/crates/chunk-cli/src/local/tui/view.rs index b8500c20..a535bf06 100644 --- a/crates/chunk-cli/src/local/tui/view.rs +++ b/crates/chunk-cli/src/local/tui/view.rs @@ -34,7 +34,7 @@ pub(super) fn render(frame: &mut Frame, model: &Model, info: &Info) { None => "↑/↓ players · / search · m move · ←/→ tabs · r restart · q quit", }, (true, Tab::Log(Source::Jvm), Focus::Nodes) => { - "↑/↓ nodes · Enter logs · ←/→ tabs · b details · r restart · q quit" + "↑/↓ select · Enter logs · ←/→ tabs · b details · r restart · q quit" } (true, Tab::Log(Source::Jvm), Focus::Logs) => "↑/↓ scroll · End follow · Esc/Tab nodes · ←/→ tabs · q quit", (true, _, _) => "↑/↓ scroll · End follow · ←/→ tabs · b details · r restart · q quit", @@ -210,17 +210,8 @@ fn render_output(frame: &mut Frame, model: &Model, area: Rect, title: &str) { fn render_lines(frame: &mut Frame, model: &Model, area: Rect) { let lines = model.visible(); let end = lines.len().saturating_sub(model.scroll); - let visible: Vec = lines[end.saturating_sub(usize::from(area.height))..end] - .iter() - .map(|line| { - let line = if model.tab() == Tab::Log(Source::Jvm) && model.node.is_some() { - line.split_once(' ').map_or(*line, |(_, line)| line) - } else { - line - }; - Line::raw(line) - }) - .collect(); + let visible: Vec = + lines[end.saturating_sub(usize::from(area.height))..end].iter().map(|line| Line::raw(*line)).collect(); frame.render_widget(Paragraph::new(visible), area); } diff --git a/crates/chunk-cli/src/local/tui/view/nodes.rs b/crates/chunk-cli/src/local/tui/view/nodes.rs index 89e0cd16..b721cce2 100644 --- a/crates/chunk-cli/src/local/tui/view/nodes.rs +++ b/crates/chunk-cli/src/local/tui/view/nodes.rs @@ -1,4 +1,4 @@ -use chunk_proto::sync::v1::NodePhase; +use chunk_proto::sync::v1::{JvmSessionPhase, NodePhase, NodeSession}; use ratatui::{ Frame, layout::{Constraint, Layout, Rect}, @@ -7,7 +7,12 @@ use ratatui::{ widgets::{Block, Borders, List, ListItem, ListState, Paragraph}, }; -use super::super::model::{Focus, Model}; +use super::super::model::{Focus, JvmSelection, Model}; +use crate::local::logs; + +const NODE_COLORS: [Color; 6] = [Color::Cyan, Color::Green, Color::Yellow, Color::Magenta, Color::Blue, Color::Red]; +const SESSION_COLORS: [Color; 6] = + [Color::LightBlue, Color::LightMagenta, Color::LightCyan, Color::LightGreen, Color::LightYellow, Color::LightRed]; pub(super) fn render(frame: &mut Frame, model: &Model, area: Rect) { let [nodes, logs] = if area.width >= 80 { @@ -22,7 +27,7 @@ pub(super) fn render(frame: &mut Frame, model: &Model, area: Rect) { render_list(frame, model, nodes, area.width >= 80); let [header, body] = Layout::vertical([Constraint::Length(3), Constraint::Min(0)]).areas(logs); render_header(frame, model, header); - super::render_lines(frame, model, body); + render_lines(frame, model, body); } fn render_list(frame: &mut Frame, model: &Model, area: Rect, sidebar: bool) { @@ -35,22 +40,57 @@ fn render_list(frame: &mut Frame, model: &Model, area: Rect, sidebar: bool) { items.push(ListItem::new(" No nodes yet").dark_gray()); } for (host, node) in &deployment.nodes { - if model.node.as_deref() == Some(host.as_str()) { + if matches!(&model.selection, JvmSelection::Node(id) if id == host) { selected = items.len(); } let phase = node.phase(); - let players = node - .health - .as_ref() - .map_or_else(|| "health unavailable".into(), |health| count(health.players, "player")); + let short_host = short(host); + let mut subtitle = vec![Span::raw(" "), Span::styled(short_host, Style::new().fg(node_color(short_host)))]; + if phase != NodePhase::Stopped { + let players = node + .health + .as_ref() + .map_or_else(|| "health unavailable".into(), |health| count(health.players, "player")); + subtitle.push(Span::raw(format!(" · {players}")).dark_gray()); + } items.push(ListItem::new(vec![ Line::from(vec![ Span::styled("● ", Style::new().fg(color(phase))), Span::raw(&node.app).bold(), Span::raw(format!(" · {}", label(phase))).dark_gray(), ]), - Line::from(format!(" {} · {players}", short(host))).dark_gray(), + Line::from(subtitle), ])); + if phase == NodePhase::Stopped { + continue; + } + for (index, session) in node.sessions.iter().enumerate() { + if matches!( + &model.selection, + JvmSelection::Session { host: selected_host, session: selected_session } + if selected_host == host && selected_session == &session.id + ) { + selected = items.len(); + } + let last = index + 1 == node.sessions.len(); + let branch = if last { " └ " } else { " ├ " }; + let cont = if last { " " } else { " │ " }; + let sphase = session.phase(); + let short_id = short(&session.id); + items.push(ListItem::new(vec![ + Line::from(vec![ + Span::raw(branch).dark_gray(), + Span::styled("● ", Style::new().fg(session_phase_color(sphase))), + Span::raw(session_label(session)), + Span::raw(format!(" · {}", session_phase_label(sphase))).dark_gray(), + ]), + Line::from(vec![ + Span::raw(cont).dark_gray(), + Span::styled(short_id, Style::new().fg(session_color(short_id))), + Span::raw(format!(" · {}", count(session.players, "player"))).dark_gray(), + ]), + ])); + } } } let mut state = ListState::default().with_selected(Some(selected)); @@ -68,33 +108,56 @@ fn render_list(frame: &mut Frame, model: &Model, area: Rect, sidebar: bool) { } fn render_header(frame: &mut Frame, model: &Model, area: Rect) { - let (title, detail) = model.selected_node().map_or_else( - || { - ( - Line::from("All nodes").bold(), - format!("{} · {}", count(model.hosts().len(), "node"), count(model.players(), "player")), - ) - }, - |(deployment, (host, node))| { - let phase = node.phase(); - let title = Line::from(vec![ - Span::raw(format!("{} · {} ", node.app, short(host))).bold(), - Span::styled(label(phase), Style::new().fg(color(phase))), - ]); - let deployment = format!("{} · {}", short(&deployment.id), deployment.state); - let detail = match (phase, &node.health) { - (NodePhase::Stopped, _) => deployment, - (_, None) => format!("health unavailable · {deployment}"), - (_, Some(health)) => format!( - "{} · {}/{}M heap · {deployment}", - count(health.players, "player"), - health.heap_used_bytes >> 20, - health.heap_max_bytes >> 20 - ), - }; - (title, detail) - }, - ); + let (title, detail) = if let Some((deployment, (host, node), session)) = model.selected_session() { + let sphase = session.phase(); + let name = if session.key.is_empty() { + session.session_type.clone() + } else { + format!("{}:{}", session.session_type, session.key) + }; + let title = Line::from(vec![ + Span::raw(format!("{name} · {} ", short(&session.id))).bold(), + Span::styled(session_phase_label(sphase), Style::new().fg(session_phase_color(sphase))), + ]); + let detail = format!( + "{} · {} · {} · {} · {}", + count(session.players, "player"), + node.app, + short(host), + short(&deployment.id), + deployment.state + ); + (title, detail) + } else if let Some((deployment, (host, node))) = model.selected_node() { + let phase = node.phase(); + let title = Line::from(vec![ + Span::raw(format!("{} · {} ", node.app, short(host))).bold(), + Span::styled(label(phase), Style::new().fg(color(phase))), + ]); + let deployment = format!("{} · {}", short(&deployment.id), deployment.state); + let sessions = count(node.sessions.len(), "session"); + let detail = match (phase, &node.health) { + (NodePhase::Stopped, _) => deployment, + (_, None) => format!("{sessions} · health unavailable · {deployment}"), + (_, Some(health)) => format!( + "{} · {sessions} · {}/{}M heap · {deployment}", + count(health.players, "player"), + health.heap_used_bytes >> 20, + health.heap_max_bytes >> 20 + ), + }; + (title, detail) + } else { + ( + Line::from("All nodes").bold(), + format!( + "{} · {} · {}", + count(model.hosts().len(), "node"), + count(model.sessions(), "session"), + count(model.players(), "player") + ), + ) + }; let [heading, metadata] = Layout::vertical([Constraint::Length(1), Constraint::Min(0)]).areas(area); let [name, follow] = Layout::horizontal([Constraint::Min(0), Constraint::Length(15.min(heading.width / 3))]).areas(heading); @@ -111,6 +174,55 @@ fn render_header(frame: &mut Frame, model: &Model, area: Rect) { ); } +fn render_lines(frame: &mut Frame, model: &Model, area: Rect) { + let lines = model.visible(); + let end = lines.len().saturating_sub(model.scroll); + let visible: Vec = lines[end.saturating_sub(usize::from(area.height))..end] + .iter() + .map(|line| format_line(&model.selection, line)) + .collect(); + frame.render_widget(Paragraph::new(visible), area); +} + +fn format_line<'a>(selection: &JvmSelection, line: &'a str) -> Line<'a> { + let parsed = logs::parse_jvm_line(line); + match selection { + JvmSelection::All => { + if parsed.host.is_empty() { + return Line::raw(parsed.body); + } + let mut spans = vec![Span::styled(parsed.host, Style::new().fg(node_color(parsed.host)).bold())]; + if let Some(session) = parsed.session { + let short_id = short(session); + spans.push(Span::styled("/", Style::new().dark_gray())); + spans.push(Span::styled(short_id, Style::new().fg(session_color(short_id)))); + } + if !parsed.body.is_empty() { + spans.push(Span::raw(" ")); + spans.push(Span::raw(parsed.body)); + } + Line::from(spans) + } + JvmSelection::Node(_) => match parsed.session { + Some(session) => { + let short_id = short(session); + let mut spans = vec![Span::styled(short_id, Style::new().fg(session_color(short_id)))]; + if !parsed.body.is_empty() { + spans.push(Span::raw(" ")); + spans.push(Span::raw(parsed.body)); + } + Line::from(spans) + } + None => Line::raw(parsed.body), + }, + JvmSelection::Session { .. } => Line::raw(parsed.body), + } +} + +fn session_label(session: &NodeSession) -> &str { + if session.key.is_empty() { &session.session_type } else { &session.key } +} + fn label(phase: NodePhase) -> &'static str { match phase { NodePhase::Online => "online", @@ -133,6 +245,43 @@ fn color(phase: NodePhase) -> Color { } } +fn session_phase_label(phase: JvmSessionPhase) -> &'static str { + match phase { + JvmSessionPhase::Ready => "ready", + JvmSessionPhase::Starting => "starting", + JvmSessionPhase::Ending => "ending", + JvmSessionPhase::Ended => "ended", + JvmSessionPhase::Failed => "failed", + JvmSessionPhase::Unspecified => "unknown", + } +} + +fn session_phase_color(phase: JvmSessionPhase) -> Color { + match phase { + JvmSessionPhase::Ready => Color::Green, + JvmSessionPhase::Starting | JvmSessionPhase::Ending => Color::Yellow, + JvmSessionPhase::Failed => Color::Red, + JvmSessionPhase::Ended | JvmSessionPhase::Unspecified => Color::DarkGray, + } +} + +fn palette_index(key: &str, len: usize) -> usize { + let mut hash = 0xcbf2_9ce4_8422_2325_u64; + for byte in key.bytes() { + hash ^= u64::from(byte); + hash = hash.wrapping_mul(0x0100_0000_01b3); + } + usize::try_from(hash % (len as u64)).unwrap_or(0) +} + +fn node_color(host: &str) -> Color { + NODE_COLORS[palette_index(host, NODE_COLORS.len())] +} + +fn session_color(session: &str) -> Color { + SESSION_COLORS[palette_index(session, SESSION_COLORS.len())] +} + pub(super) fn short(id: &str) -> &str { &id[..id.len().min(8)] } diff --git a/crates/chunk-cli/src/local/tui/view/tests.rs b/crates/chunk-cli/src/local/tui/view/tests.rs index 020528af..99f7751b 100644 --- a/crates/chunk-cli/src/local/tui/view/tests.rs +++ b/crates/chunk-cli/src/local/tui/view/tests.rs @@ -1,5 +1,7 @@ -use chunk_proto::sync::v1::{ClaimPhase, JvmHealth, Node, NodePhase, OperatorPlayer, SessionDemand}; -use ratatui::{Terminal, backend::TestBackend}; +use chunk_proto::sync::v1::{ + ClaimPhase, JvmHealth, JvmSessionPhase, Node, NodePhase, NodeSession, OperatorPlayer, SessionDemand, +}; +use ratatui::{Terminal, backend::TestBackend, style::Color}; use super::*; use crate::local::report::{Deployment, Event}; @@ -142,3 +144,94 @@ fn players_tab_lists_filters_and_shows_the_move_form() { assert!(screen.contains("> lobby/main · lobby/default:main"), "{width}x{height}:\n{screen}"); } } + +#[test] +fn jvm_tab_nests_sessions_shows_counts_and_details_and_colors_interleaved_logs() { + let mut model = Model::new(); + model.apply(Event::Step { name: "Ready", state: Step::Done("connect".into()) }); + let health = JvmHealth { players: 3, heap_used_bytes: 256 << 20, heap_max_bytes: 512 << 20, ..Default::default() }; + model.apply(Event::Deployments(vec![Deployment { + id: "d87ec655".into(), + state: "current".into(), + nodes: vec![ + ( + "5a9e4aba-1234".into(), + Node { + app: "arena".into(), + phase: NodePhase::Online.into(), + health: Some(health), + sessions: vec![ + NodeSession { + id: "f47ac10b-58cc".into(), + session_type: "arena/default".into(), + key: "duel-1".into(), + players: 2, + phase: JvmSessionPhase::Ready.into(), + }, + NodeSession { + id: "9c2e1d0a-99aa".into(), + session_type: "arena/default".into(), + key: "duel-2".into(), + players: 1, + phase: JvmSessionPhase::Starting.into(), + }, + ], + ..Default::default() + }, + ), + ( + "b1234567-89ab".into(), + Node { app: "lobby".into(), phase: NodePhase::Online.into(), ..Default::default() }, + ), + ], + players: Vec::new(), + destinations: Vec::new(), + }])); + model.apply(Event::Log { source: Source::Jvm, line: "5a9e4aba JVM booted".into() }); + model.apply(Event::Log { source: Source::Jvm, line: "5a9e4aba [session:f47ac10b-58cc] match started".into() }); + model.apply(Event::Log { source: Source::Jvm, line: "b1234567 lobby listening".into() }); + + // All nodes header shows node, session, and player counts, and sidebar nests sessions under their node. + let mut terminal = Terminal::new(TestBackend::new(120, 30)).unwrap(); + let info = + Info { title: "chunk dev · example".into(), address: "127.0.0.1:25565".parse().unwrap(), watching: true }; + terminal.draw(|frame| render(frame, &model, &info)).unwrap(); + let buffer = terminal.backend().buffer(); + let all_nodes = buffer + .content + .chunks(120) + .map(|row| row.iter().map(ratatui::buffer::Cell::symbol).collect::()) + .collect::>() + .join("\n"); + assert!(all_nodes.contains("2 nodes · 2 sessions · 3 players"), "{all_nodes}"); + assert!(all_nodes.contains("duel-1 · ready"), "{all_nodes}"); + assert!(all_nodes.contains("duel-2 · starting"), "{all_nodes}"); + assert!(all_nodes.contains("5a9e4aba/f47ac10b match started"), "{all_nodes}"); + + // Verify interleaved node and session prefixes carry distinct non-default colors. + let tagged_row = all_nodes.lines().position(|row| row.contains("5a9e4aba/f47ac10b match started")).unwrap(); + let line = all_nodes.lines().nth(tagged_row).unwrap(); + let node_byte = line.find("5a9e4aba/f47ac10b").unwrap(); + let node_col = line[..node_byte].chars().count(); + let session_col = node_col + "5a9e4aba/".len(); + let node_fg = buffer.cell((u16::try_from(node_col).unwrap(), u16::try_from(tagged_row).unwrap())).unwrap().fg; + let session_fg = buffer.cell((u16::try_from(session_col).unwrap(), u16::try_from(tagged_row).unwrap())).unwrap().fg; + assert_ne!(node_fg, Color::Reset); + assert_ne!(session_fg, Color::Reset); + assert_ne!(node_fg, session_fg); + + // Selected node header includes session count alongside players and heap. + model.move_node(1); + let node_screen = draw(&model, 120, 30); + assert!(node_screen.contains("3 players · 2 sessions · 256/512M heap"), "{node_screen}"); + assert!(node_screen.contains("f47ac10b match started"), "{node_screen}"); + assert!(node_screen.contains("JVM booted"), "{node_screen}"); + + // Selected session header shows session details and filters log body cleanly. + model.move_node(1); + let session_screen = draw(&model, 120, 30); + assert!(session_screen.contains("arena/default:duel-1 · f47ac10b ready"), "{session_screen}"); + assert!(session_screen.contains("2 players · arena · 5a9e4aba · d87ec655 · current"), "{session_screen}"); + assert!(session_screen.contains("match started"), "{session_screen}"); + assert!(!session_screen.contains("JVM booted"), "{session_screen}"); +} diff --git a/crates/chunk-control/src/operator/nodes.rs b/crates/chunk-control/src/operator/nodes.rs index 27ad3308..524e85c9 100644 --- a/crates/chunk-control/src/operator/nodes.rs +++ b/crates/chunk-control/src/operator/nodes.rs @@ -1,13 +1,10 @@ -//! The `nodes` topic: each host's lifecycle and health, keyed by host ID, with `chunk.sync.v1.Node` values. Health and -//! a passing drain deadline change it without a commit, so it follows health passes and deadlines as well as commits. +//! The `nodes` topic: each host's lifecycle, health and open sessions, keyed by host ID, with `chunk.sync.v1.Node` +//! values. Health and a passing drain deadline change it without a commit, so it follows health passes and deadlines +//! as well as commits. A session's phase comes from its JVM's last report, read whenever the topic recomputes. -use std::{ - collections::{BTreeMap, BTreeSet}, - sync::Arc, - time::Duration, -}; +use std::{collections::BTreeMap, sync::Arc, time::Duration}; -use chunk_proto::sync::v1::{self as sync, NodePhase}; +use chunk_proto::sync::v1::{self as sync, JvmSessionPhase, NodePhase}; use tokio::sync::watch; use super::{changed, entry}; @@ -23,6 +20,8 @@ pub struct Nodes { control: Arc, positions: watch::Receiver, observed: watch::Receiver, + /// Open claims by session, as of `position`. + claims: OpenClaims, /// The value last sent for each host. sent: BTreeMap, /// The position of the last update. @@ -40,6 +39,7 @@ impl Nodes { control: control.clone(), positions: control.subscribe(), observed: control.observed(), + claims: OpenClaims::default(), sent: BTreeMap::new(), position: Generation::default(), wake: None, @@ -72,8 +72,19 @@ impl Nodes { fn diff(&mut self, snapshot: bool) -> Result> { let now = crate::now_ms(); - let state = self.control.state()?; - let nodes = nodes(&state, self.control.statuses(&state)?); + // Only the claims committed since the last update are re-read, unless they fell outside retained history. + let state = + if let Some((changes, state)) = self.control.authority.feed().after(self.position).filter(|_| !snapshot) { + for change in &changes { + self.claims.track(&state, &change.claim); + } + state + } else { + let state = self.control.state()?; + self.claims = OpenClaims::read(&state); + state + }; + let nodes = nodes(&self.control, &state, &self.claims, self.control.statuses(&state)?); self.wake = nodes .values() .filter(|node| node.phase() == sync::NodePhase::Draining) @@ -101,19 +112,89 @@ impl Nodes { } } -/// Each host's node from its status, with its drain's deadline and open claims while it drains or stops. -fn nodes(state: &State, statuses: Vec) -> BTreeMap { +/// Each open claim's session and each session's count of them. +#[derive(Default)] +struct OpenClaims { + sessions: BTreeMap, + counts: BTreeMap, +} + +impl OpenClaims { + fn read(state: &State) -> Self { + let mut claims = Self::default(); + for id in state.claims.keys() { + claims.track(state, id); + } + claims + } + + /// Re-reads `claim` from `state`. + fn track(&mut self, state: &State, claim: &str) { + let session = + state.claims.get(claim).filter(|claim| claim.phase != Phase::Released).map(|claim| claim.session.clone()); + let previous = match &session { + Some(session) => self.sessions.insert(claim.to_owned(), session.clone()), + None => self.sessions.remove(claim), + }; + if previous == session { + return; + } + if let Some(previous) = previous + && let Some(count) = self.counts.get_mut(&previous) + { + *count -= 1; + if *count == 0 { + self.counts.remove(&previous); + } + } + if let Some(session) = session { + *self.counts.entry(session).or_default() += 1; + } + } + + fn count(&self, session: &str) -> u32 { + self.counts.get(session).copied().unwrap_or(0) + } +} + +/// Each host's node from its status, with its open sessions, drain deadline and open claims while it drains or stops. +fn nodes( + control: &Control, + state: &State, + claims: &OpenClaims, + statuses: Vec, +) -> BTreeMap { let retiring = |phase: NodePhase| matches!(phase, NodePhase::Draining | NodePhase::Stopping); - let hosts: BTreeSet<_> = - statuses.iter().filter(|status| retiring(status.phase)).map(|status| status.host.as_str()).collect(); let mut remaining = BTreeMap::<&str, u32>::new(); - if !hosts.is_empty() { - for claim in state.claims.values().filter(|claim| claim.phase != Phase::Released) { - let host = state.sessions.get(&claim.session).map(|session| session.host.as_str()); - if let Some(host) = host.filter(|host| hosts.contains(host)) { - *remaining.entry(host).or_default() += 1; - } + let mut identities = BTreeMap::new(); + let mut sessions = BTreeMap::<&str, Vec>::new(); + for (id, session) in &state.sessions { + let players = claims.count(id); + *remaining.entry(session.host.as_str()).or_default() += players; + if session.finished { + continue; } + let identity = identities + .entry(session.host.as_str()) + .or_insert_with(|| control.host.connection(&session.host).map(|runtime| runtime.identity)); + let reported = identity + .as_ref() + .and_then(|identity| control.links.session(&session.host, identity, id)) + .filter(|observed| crate::sessions::matches(id, session, observed)) + .map(|observed| observed.phase()); + let phase = match reported { + Some(phase @ (JvmSessionPhase::Ending | JvmSessionPhase::Ended | JvmSessionPhase::Failed)) => phase, + _ if session.finish_requested => JvmSessionPhase::Ending, + Some(JvmSessionPhase::Ready) => JvmSessionPhase::Ready, + _ => JvmSessionPhase::Starting, + }; + sessions.entry(session.host.as_str()).or_default().push(sync::NodeSession { + id: id.clone(), + session_type: session.session_type.clone(), + key: session.demand_key.clone(), + players, + phase: phase.into(), + }); } let mut nodes = BTreeMap::new(); for status in statuses { @@ -122,6 +203,7 @@ fn nodes(state: &State, statuses: Vec) -> BTreeMap(&resumed, &host).is_some_and(|node| healthy(&node))); + // Moving the player opens a second session and updates player counts on `nodes`. + let control = fixture.control.clone(); + fixture.operate(&cli, "operator:move-arena", "chunk:move_player", &move_to("arena")).await; + let destination = control.stored_claim("operator:move-arena").unwrap().expect("the queued move").request; + let assignment = control.claim(destination).await.unwrap(); + control.cancel(runtime::login()).await.unwrap(); + control.activate(assignment.claim.unwrap()).await.unwrap(); + let (_, node) = until(&mut nodes, &host, |node: &Node| { + node.sessions + .iter() + .any(|session| session.key == "arena" && session.players == 1 && session.phase() == JvmSessionPhase::Ready) + && node.sessions.iter().any(|session| session.key == "lobby" && session.players == 0) + }) + .await; + assert_eq!(node.sessions.len(), 2); + let host_drain = drain(Target::Host(host.clone()), 1); let drained = fixture.operate(&cli, "operator:drain-host", "chunk:drain", &host_drain).await; let drained: DrainResult = result(&drained); @@ -126,7 +149,8 @@ async fn nodes_follow_health_and_a_host_drain_until_it_stopped() { let (_, node) = until(&mut nodes, &host, |node: &Node| node.phase() == NodePhase::Draining).await; assert_eq!((node.drain_deadline_ms, node.remaining_claims), (drained.deadline_ms, 1)); until(&mut nodes, &host, |node: &Node| node.phase() == NodePhase::Stopping).await; - until(&mut nodes, &host, |node: &Node| node.phase() == NodePhase::Stopped).await; + let (_, stopped) = until(&mut nodes, &host, |node: &Node| node.phase() == NodePhase::Stopped).await; + assert!(stopped.sessions.is_empty()); drop(nodes); fixture.stop().await; jvm.abort(); diff --git a/jvm/multistom-kotlin/src/main/kotlin/com/chunkzero/chunk/multistom/SessionCoroutines.kt b/jvm/multistom-kotlin/src/main/kotlin/com/chunkzero/chunk/multistom/SessionCoroutines.kt index 4a5eccd4..15046da4 100644 --- a/jvm/multistom-kotlin/src/main/kotlin/com/chunkzero/chunk/multistom/SessionCoroutines.kt +++ b/jvm/multistom-kotlin/src/main/kotlin/com/chunkzero/chunk/multistom/SessionCoroutines.kt @@ -3,9 +3,11 @@ package com.chunkzero.chunk.multistom import com.chunkzero.chunk.backend.CoroutineBackend import com.chunkzero.chunk.backend.api.PlayerId import com.chunkzero.chunk.backend.client.BackendSession +import com.chunkzero.chunk.runtime.SessionLogging import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.ThreadContextElement import kotlinx.coroutines.cancel import kotlinx.coroutines.future.future import net.minestom.server.entity.Player @@ -20,7 +22,7 @@ val SessionScope.coroutines: SessionCoroutines class SessionCoroutines internal constructor( private val session: SessionScope, ticks: TickExecutor, -) : CoroutineScope by CoroutineScope(SupervisorJob() + TickDispatcher(ticks)), +) : CoroutineScope by CoroutineScope(SupervisorJob() + TickDispatcher(ticks) + SessionLog(session.id)), AutoCloseable { fun backend(client: BackendSession): CoroutineBackend = session.own(CoroutineBackend(client, this)) @@ -49,6 +51,24 @@ private class TickDispatcher( } } +/** Binds the session for [SessionLogging] whenever one of its coroutines runs, on any thread. */ +private class SessionLog( + private val sessionId: String, +) : ThreadContextElement { + companion object Key : CoroutineContext.Key + + override val key get() = Key + + override fun updateThreadContext(context: CoroutineContext): String? = SessionLogging.bind(sessionId) + + override fun restoreThreadContext( + context: CoroutineContext, + oldState: String?, + ) { + SessionLogging.bind(oldState) + } +} + /** Finish hooks settle before the manager disposes backend scopes, so result mutations can complete. */ abstract class CoroutineSession : Session() { private lateinit var coroutines: SessionCoroutines diff --git a/jvm/multistom-kotlin/src/test/kotlin/com/chunkzero/chunk/multistom/SessionCoroutinesTest.kt b/jvm/multistom-kotlin/src/test/kotlin/com/chunkzero/chunk/multistom/SessionCoroutinesTest.kt index 1a2ca22b..b5be47d4 100644 --- a/jvm/multistom-kotlin/src/test/kotlin/com/chunkzero/chunk/multistom/SessionCoroutinesTest.kt +++ b/jvm/multistom-kotlin/src/test/kotlin/com/chunkzero/chunk/multistom/SessionCoroutinesTest.kt @@ -18,17 +18,22 @@ import com.chunkzero.chunk.backend.api.SessionId import com.chunkzero.chunk.backend.client.BackendSession import com.chunkzero.chunk.backend.client.OperationId import com.chunkzero.chunk.backend.client.SessionIdentity +import com.chunkzero.chunk.runtime.SessionLogging import com.chunkzero.chunk.runtime.TestHosts import com.google.protobuf.ByteString import io.grpc.ManagedChannelBuilder import io.grpc.ServerBuilder import io.grpc.stub.ServerCallStreamObserver import io.grpc.stub.StreamObserver +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import net.minestom.server.ServerProcess import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertFalse +import org.junit.jupiter.api.Assertions.assertNull import org.junit.jupiter.api.Assertions.assertTrue import org.junit.jupiter.api.Test import tools.jackson.core.type.TypeReference @@ -43,6 +48,29 @@ class SessionCoroutinesTest { private val nullType = JsonType.of(object : TypeReference() {}, BackendValues::checkNull) private val integerType = JsonType.of(object : TypeReference() {}, BackendValues::checkInteger) + @Test + fun `coroutines keep their session bound across dispatchers`() { + val process = ServerProcess.create() + val ticks = TickExecutor() + val scope = SessionScope(process, "sess-7", ticks, { CompletableFuture.completedFuture(null) }, null) + val seen = CompletableFuture>() + scope.onTick { + scope.coroutines.launch { + val onTick = SessionLogging.current() + val elsewhere = withContext(Dispatchers.Default) { SessionLogging.current() } + seen.complete(listOf(onTick, elsewhere, SessionLogging.current())) + } + } + while (!seen.isDone) ticks.flush() + + assertEquals(listOf("sess-7", "sess-7", "sess-7"), seen.join()) + assertNull(SessionLogging.current()) + val disposed = scope.onTick { scope.dispose() } + while (!disposed.isDone) ticks.flush() + disposed.join() + process.stop() + } + @Test fun `tick resumptions and finishing await the result before disposing watches`() { val minestom = ServerProcess.create() diff --git a/jvm/multistom/src/main/java/com/chunkzero/chunk/multistom/SessionManager.java b/jvm/multistom/src/main/java/com/chunkzero/chunk/multistom/SessionManager.java index 240250ce..d7f9d96e 100644 --- a/jvm/multistom/src/main/java/com/chunkzero/chunk/multistom/SessionManager.java +++ b/jvm/multistom/src/main/java/com/chunkzero/chunk/multistom/SessionManager.java @@ -7,6 +7,7 @@ import com.chunkzero.chunk.runtime.Delivery; import com.chunkzero.chunk.runtime.SessionControl; import com.chunkzero.chunk.runtime.SessionHandler; +import com.chunkzero.chunk.runtime.SessionLogging; import com.chunkzero.chunk.runtime.SessionMethod; import com.chunkzero.chunk.runtime.SessionMethodRegistry; import com.chunkzero.chunk.runtime.SessionProvider; @@ -94,7 +95,8 @@ public ServerProcess getProcess() { @Override public void create(SessionControl control) { - ticks.submit( + onTick( + control.id(), () -> { var created = types.create( @@ -121,7 +123,8 @@ public void create(SessionControl control) { @Override public void finish(SessionControl control) { - ticks.submit( + onTick( + control.id(), () -> { var managed = sessions.get(control.id()); if (managed == null) control.ended(); @@ -140,7 +143,8 @@ public CompletionStage method(SessionControl control, SessionMethod call if (!methods.declares(control.type(), call.name())) return CompletableFuture.failedFuture( new IllegalArgumentException("Undeclared session method")); - return ticks.submit( + return onTick( + control.id(), () -> { var managed = sessions.get(control.id()); if (managed != null && managed.isDisconnected(call.delivery())) @@ -206,7 +210,8 @@ void start() { invoke(() -> behavior.onCreate(scope)) .whenComplete( (ignored, error) -> - ticks.submit( + onTick( + control.id(), () -> { // A creation past its deadline already ended. if (finishing) return null; @@ -230,7 +235,8 @@ void start() { } public CompletableFuture join(Player player) { - return ticks.submit( + return onTick( + control.id(), () -> { if (!control.isReady()) throw new IllegalStateException("Session unavailable"); @@ -240,7 +246,8 @@ public CompletableFuture join(Player player) { .thenCompose(Function.identity()) .thenCompose( ignored -> - ticks.submit( + onTick( + control.id(), () -> { if (scope.getPlayers().contains(player) && joined.add(player)) { @@ -254,7 +261,8 @@ public CompletableFuture join(Player player) { } public CompletableFuture leave(Player player) { - return ticks.submit( + return onTick( + control.id(), () -> { if (!scope.getPlayers().remove(player)) return CompletableFuture.completedFuture(null); @@ -280,7 +288,8 @@ public CompletableFuture leave(Player player) { } private CompletableFuture notifyLeave(Player player, @Nullable Throwable failure) { - return ticks.submit( + return onTick( + control.id(), () -> { if (joined.remove(player)) process.eventHandler().call(new SessionLeaveEvent(scope, player)); @@ -295,7 +304,8 @@ void finish() { invoke(behavior::onFinish) .whenComplete( (ignored, error) -> - ticks.submit( + onTick( + control.id(), () -> { try { scope.dispose(); @@ -318,6 +328,11 @@ void finish() { } } + /** Runs {@code action} on the tick thread with session {@code id} bound for logging. */ + private CompletableFuture onTick(String id, Supplier action) { + return ticks.submit(() -> SessionLogging.call(id, action)); + } + private static CompletableFuture invoke(Supplier> action) { try { return action.get().toCompletableFuture(); diff --git a/jvm/multistom/src/main/java/com/chunkzero/chunk/multistom/SessionScope.java b/jvm/multistom/src/main/java/com/chunkzero/chunk/multistom/SessionScope.java index d7922387..a9e5b91a 100644 --- a/jvm/multistom/src/main/java/com/chunkzero/chunk/multistom/SessionScope.java +++ b/jvm/multistom/src/main/java/com/chunkzero/chunk/multistom/SessionScope.java @@ -8,12 +8,14 @@ import com.chunkzero.chunk.runtime.Component; import com.chunkzero.chunk.runtime.ComponentRegistry; import com.chunkzero.chunk.runtime.MoveResult; +import com.chunkzero.chunk.runtime.SessionLogging; import net.minestom.server.ServerProcess; import net.minestom.server.entity.Player; import net.minestom.server.event.Event; import net.minestom.server.event.EventFilter; import net.minestom.server.event.EventNode; +import net.minestom.server.event.ListenerHandle; import net.minestom.server.event.trait.EntityEvent; import net.minestom.server.event.trait.InstanceEvent; import net.minestom.server.event.trait.PlayerEvent; @@ -66,6 +68,7 @@ public final class SessionScope { private final Map, AutoCloseable> sharedResources = new HashMap<>(); private final Set players = ConcurrentHashMap.newKeySet(); private final EventNode events; + private final ThreadLocal<@Nullable Event> redispatching = new ThreadLocal<>(); private final Scheduler scheduler; private final List drivers; private boolean disposed; @@ -92,18 +95,19 @@ public final class SessionScope { this.requestFinish = requestFinish; this.backend = backend; this.components = components; - events = EventNode.event("session-" + id, EventFilter.ALL, this::owns); + events = EventNode.event("session-" + id, EventFilter.ALL, this::admit); try { var schedulers = process.schedulerManager(); scheduler = schedulers.createScheduler(); drivers = List.of( schedulers - .buildTask(scheduler::processTick) + .buildTask(() -> SessionLogging.run(id, scheduler::processTick)) .repeat(TaskSchedule.nextTick()) .schedule(), schedulers - .buildTask(scheduler::processTickEnd) + .buildTask( + () -> SessionLogging.run(id, scheduler::processTickEnd)) .repeat(TaskSchedule.nextTick()) .executionType(ExecutionType.TICK_END) .schedule()); @@ -130,7 +134,8 @@ public ServerProcess getProcess() { /** * Receives this scope's lifecycle notifications, its admitted players' events, and events of - * its instances and the entities in them. + * its instances and the entities in them. Its listeners run with this session bound for {@link + * SessionLogging}. */ public @NotNull EventNode getEvents() { return events; @@ -250,10 +255,13 @@ void releasePlayer(Player player) throws Exception { public CompletableFuture onTick(Supplier action) { return ticks.submit( - () -> { - checkActive(); - return action.get(); - }); + () -> + SessionLogging.call( + id, + () -> { + checkActive(); + return action.get(); + })); } public CompletableFuture onTick(Runnable action) { @@ -331,6 +339,33 @@ private void unregisterInstances() throws Exception { .toList()); } + /** + * Minestom runs a node's listeners in one dispatch callers can't wrap, and instance and player + * ticks run on its dispatcher outside this session's tick. So an event this session owns is + * dispatched again through the node with the session bound, and the first pass runs nothing; + * the second consumes its event from {@link #redispatching} and runs the listeners, so a + * dispatch those listeners start is bound and isolated on its own. Unlike a plain node, a + * failing session listener never stops the global listeners after it, bound or not. + */ + private boolean admit(Event event) { + if (!owns(event)) return false; + if (redispatching.get() == event) { + redispatching.remove(); + return true; + } + @SuppressWarnings("unchecked") + var handle = (ListenerHandle) events.getHandle(event.getClass()); + var outer = redispatching.get(); + redispatching.set(event); + try { + SessionLogging.run(id, () -> handle.call(process, event)); + } finally { + if (outer == null) redispatching.remove(); + else redispatching.set(outer); + } + return false; + } + private boolean owns(Event event) { return switch (event) { case SessionEvent lifecycle -> lifecycle.getSession() == this; diff --git a/jvm/multistom/src/test/kotlin/com/chunkzero/chunk/multistom/SessionEventsTest.kt b/jvm/multistom/src/test/kotlin/com/chunkzero/chunk/multistom/SessionEventsTest.kt index b79792b2..fa7adff9 100644 --- a/jvm/multistom/src/test/kotlin/com/chunkzero/chunk/multistom/SessionEventsTest.kt +++ b/jvm/multistom/src/test/kotlin/com/chunkzero/chunk/multistom/SessionEventsTest.kt @@ -30,6 +30,7 @@ import net.minestom.server.timer.TaskSchedule import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertFalse +import org.junit.jupiter.api.Assertions.assertNull import org.junit.jupiter.api.Assertions.assertSame import org.junit.jupiter.api.Assertions.assertThrows import org.junit.jupiter.api.Assertions.assertTrue @@ -372,7 +373,7 @@ class SessionEventsTest { assertEquals(3, reported.size) assertTrue(reported.all { it === listenerFailure }) assertTrue(global.last() is SessionDestroyEvent) - assertFalse(process.eventHandler().children.contains(session.scope.events)) + assertNull(session.scope.events.parent) assertTrue(process.instanceManager().instances.isEmpty()) } diff --git a/jvm/multistom/src/test/kotlin/com/chunkzero/chunk/multistom/SessionLoggingMultistomTest.kt b/jvm/multistom/src/test/kotlin/com/chunkzero/chunk/multistom/SessionLoggingMultistomTest.kt new file mode 100644 index 00000000..74b8e24f --- /dev/null +++ b/jvm/multistom/src/test/kotlin/com/chunkzero/chunk/multistom/SessionLoggingMultistomTest.kt @@ -0,0 +1,211 @@ +package com.chunkzero.chunk.multistom + +import chunk.sync.v1.Jvm.JvmSession +import com.chunkzero.chunk.multistom.event.SessionCreateEvent +import com.chunkzero.chunk.runtime.SessionLogging +import com.chunkzero.chunk.runtime.TestHosts +import net.minestom.server.ServerProcess +import net.minestom.server.event.EventNode +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertNull +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import java.util.concurrent.CompletableFuture +import java.util.function.Supplier + +class SessionLoggingMultistomTest { + private val ticks = TickExecutor() + private lateinit var process: ServerProcess + + @BeforeEach + fun start() { + process = ServerProcess.create() + } + + @AfterEach + fun stop() { + process.stop() + } + + @Test + fun `session hooks, onTick, scheduler tasks and event listeners bind the session id`() { + val observed = mutableListOf() + process.exceptionManager().setExceptionHandler { observed.add("failure:${SessionLogging.current()}") } + lateinit var scope: SessionScope + val manager = + SessionManager( + process, + ticks, + mapOf( + "app/default" to + Supplier { + object : Session() { + override fun onCreate(created: SessionScope): CompletableFuture { + scope = created + observed.add("create:${SessionLogging.current()}") + scope.createInstance() + scope.scheduler + .buildTask { observed.add("scheduler:${SessionLogging.current()}") } + .schedule() + return CompletableFuture.completedFuture(null) + } + + override fun onFinish(): CompletableFuture { + observed.add("finish:${SessionLogging.current()}") + return CompletableFuture.completedFuture(null) + } + } + }, + ), + ) + val host = TestHosts.detached(manager) + val spec = + JvmSession + .newBuilder() + .setSessionType("app/default") + .setCapacity(4) + .build() + val created = TestHosts.create(host, "sess-42", spec) + while (!created.isDone) ticks.flush() + created.join() + + // Events reach the session's listeners from outside any session's context. + scope.events.addListener(SessionCreateEvent::class.java) { + observed.add("event:${SessionLogging.current()}") + error("listener failed") + } + process.eventHandler().call(SessionCreateEvent(scope)) + assertNull(SessionLogging.current()) + + val tickFuture = scope.onTick { observed.add("onTick:${SessionLogging.current()}") } + ticks.flush() + tickFuture.join() + + process.schedulerManager().processTick() + assertNull(SessionLogging.current()) + + val finished = TestHosts.finish(host, "sess-42", spec) + while (!finished.isDone) ticks.flush() + finished.join() + + assertNull(SessionLogging.current()) + assertEquals( + listOf( + "create:sess-42", + "event:sess-42", + "failure:sess-42", + "onTick:sess-42", + "scheduler:sess-42", + "finish:sess-42", + ), + observed, + ) + } + + @Test + fun `session event priority orders its listeners against global siblings`() { + val order = mutableListOf() + val sibling = EventNode.all("sibling").setPriority(5) + sibling.addListener(SessionCreateEvent::class.java) { order.add("global") } + process.eventHandler().addChild(sibling) + val scope = scope("sess-1") + order.clear() + + scope.events.setPriority(10) + scope.events.addListener(SessionCreateEvent::class.java) { order.add("session") } + process.eventHandler().call(SessionCreateEvent(scope)) + + assertEquals(listOf("global", "session"), order) + } + + @Test + fun `dispatching to the session node directly rejects other sessions' events and binds its own`() { + val first = scope("first") + val second = scope("second") + val observed = mutableListOf() + first.events.addListener(SessionCreateEvent::class.java) { + observed.add("${it.session.id}:${SessionLogging.current()}") + } + + first.events.call(SessionCreateEvent(second)) + assertEquals(emptyList(), observed) + + first.events.call(SessionCreateEvent(first)) + assertEquals(listOf("first:first"), observed) + assertNull(SessionLogging.current()) + } + + @Test + fun `a failing session listener leaves global listeners running whether or not a session is bound`() { + val scope = scope("sess-1") + val reported = mutableListOf() + process.exceptionManager().setExceptionHandler { reported.add(it) } + val order = mutableListOf() + val sibling = EventNode.all("sibling").setPriority(20) + sibling.addListener(SessionCreateEvent::class.java) { order.add("global") } + process.eventHandler().addChild(sibling) + scope.events.addListener(SessionCreateEvent::class.java) { + order.add("session") + error("listener failed") + } + + process.eventHandler().call(SessionCreateEvent(scope)) + SessionLogging.run("sess-1") { process.eventHandler().call(SessionCreateEvent(scope)) } + + assertEquals(listOf("session", "global", "session", "global"), order) + assertEquals(2, reported.size) + } + + @Test + fun `a dispatch started by a session listener binds its own session`() { + val first = scope("first") + val second = scope("second") + val firstEvent = SessionCreateEvent(first) + val observed = mutableListOf() + first.events.addListener(SessionCreateEvent::class.java) { + observed.add("first:${SessionLogging.current()}") + if (observed.size == 1) process.eventHandler().call(SessionCreateEvent(second)) + } + second.events.addListener(SessionCreateEvent::class.java) { + observed.add("second:${SessionLogging.current()}") + process.eventHandler().call(firstEvent) + } + + process.eventHandler().call(firstEvent) + + assertEquals(listOf("first:first", "second:second", "first:first"), observed) + assertNull(SessionLogging.current()) + } + + private fun scope(id: String): SessionScope { + lateinit var scope: SessionScope + val manager = + SessionManager( + process, + ticks, + mapOf( + "app/default" to + Supplier { + object : Session() { + override fun onCreate(created: SessionScope): CompletableFuture { + scope = created + scope.createInstance() + return CompletableFuture.completedFuture(null) + } + } + }, + ), + ) + val spec = + JvmSession + .newBuilder() + .setSessionType("app/default") + .setCapacity(4) + .build() + val created = TestHosts.create(TestHosts.detached(manager), id, spec) + while (!created.isDone) ticks.flush() + created.join() + return scope + } +} diff --git a/jvm/runtime/src/main/java/com/chunkzero/chunk/runtime/ChunkProcess.java b/jvm/runtime/src/main/java/com/chunkzero/chunk/runtime/ChunkProcess.java index e740ab9c..186f56c1 100644 --- a/jvm/runtime/src/main/java/com/chunkzero/chunk/runtime/ChunkProcess.java +++ b/jvm/runtime/src/main/java/com/chunkzero/chunk/runtime/ChunkProcess.java @@ -48,6 +48,7 @@ public final class ChunkProcess implements AutoCloseable { } public static ChunkProcess connect() { + SessionLogging.install(); return new ChunkProcess(RuntimeEnvironment.load()); } diff --git a/jvm/runtime/src/main/java/com/chunkzero/chunk/runtime/ChunkSessions.java b/jvm/runtime/src/main/java/com/chunkzero/chunk/runtime/ChunkSessions.java index c4b3ceb5..89c6ed2f 100644 --- a/jvm/runtime/src/main/java/com/chunkzero/chunk/runtime/ChunkSessions.java +++ b/jvm/runtime/src/main/java/com/chunkzero/chunk/runtime/ChunkSessions.java @@ -452,7 +452,7 @@ private CompletableFuture create( synchronized (this) { if (control.handlerFinishing) return; } - handler.create(control); + SessionLogging.run(control.id(), () -> handler.create(control)); })); return control.created; } @@ -564,7 +564,9 @@ private void withdraw(SessionControl control, List after) { dispatch( () -> { try { - handler.finish(control); + SessionLogging.run( + control.id(), + () -> handler.finish(control)); } catch (RuntimeException failure) { ended(control, failure); } @@ -740,14 +742,13 @@ private void execute(MethodCall operation) { } CompletionStage result; try { - result = - handler.method( - session, - new SessionMethod( - call.getMethod(), - call.getArgumentsJson().toStringUtf8(), - delivery, - () -> start(operation))); + var methodCall = + new SessionMethod( + call.getMethod(), + call.getArgumentsJson().toStringUtf8(), + delivery, + () -> start(operation)); + result = SessionLogging.call(session.id(), () -> handler.method(session, methodCall)); } catch (RuntimeException error) { result = CompletableFuture.failedFuture(error); } diff --git a/jvm/runtime/src/main/java/com/chunkzero/chunk/runtime/SessionLogging.java b/jvm/runtime/src/main/java/com/chunkzero/chunk/runtime/SessionLogging.java new file mode 100644 index 00000000..7fd34fb0 --- /dev/null +++ b/jvm/runtime/src/main/java/com/chunkzero/chunk/runtime/SessionLogging.java @@ -0,0 +1,201 @@ +package com.chunkzero.chunk.runtime; + +import org.jetbrains.annotations.Nullable; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintStream; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.function.Supplier; +import java.util.logging.ConsoleHandler; +import java.util.logging.Logger; + +/** + * Tags output with the session it runs for. While a thread has a session bound, each line it starts + * on {@link System#out} or {@link System#err} begins with {@code [session:] }; output from + * other threads, such as startup and GC, stays untagged. Output without a newline is written only + * on {@code flush()}, so a thread that ends without either loses it. + */ +public final class SessionLogging { + private static final ThreadLocal<@Nullable String> CURRENT = new ThreadLocal<>(); + + private SessionLogging() {} + + /** The session bound to the current thread, or {@code null}. */ + public static @Nullable String current() { + return CURRENT.get(); + } + + /** + * Binds {@code sessionId}, or nothing when {@code null}, to the current thread and returns the + * previous binding, for code that restores it itself. + */ + public static @Nullable String bind(@Nullable String sessionId) { + var previous = CURRENT.get(); + if (sessionId == null) CURRENT.remove(); + else CURRENT.set(sessionId); + return previous; + } + + /** Runs {@code action} with {@code sessionId} bound to the current thread. */ + public static void run(String sessionId, Runnable action) { + var previous = bind(sessionId); + try { + action.run(); + } finally { + bind(previous); + } + } + + /** Calls {@code action} with {@code sessionId} bound to the current thread. */ + public static T call(String sessionId, Supplier action) { + var previous = bind(sessionId); + try { + return action.get(); + } finally { + bind(previous); + } + } + + /** + * Replaces {@link System#out} and {@link System#err} with tagging streams, and points console + * handlers {@code java.util.logging} already made at the new {@link System#err}. Logging + * backends that look up the standard streams when they write, such as SLF4J's simple logger, + * follow too. + */ + public static synchronized void install() { + if (System.out instanceof Tagging && System.err instanceof Tagging) return; + System.setOut(wrap(System.out)); + System.setErr(wrap(System.err)); + var root = Logger.getLogger(""); + for (var handler : root.getHandlers()) { + if (handler.getClass() != ConsoleHandler.class) continue; + var replacement = new ConsoleHandler(); + replacement.setLevel(handler.getLevel()); + replacement.setFormatter(handler.getFormatter()); + replacement.setFilter(handler.getFilter()); + root.removeHandler(handler); + root.addHandler(replacement); + } + } + + static PrintStream wrap(PrintStream target) { + if (target instanceof Tagging) return target; + return new Tagging(target); + } + + private static final class Tagging extends PrintStream { + private final PrintStream target; + + Tagging(PrintStream target) { + // No autoflush: PrintStream would flush between the body and the newline of a println. + super(new TaggingStream(target), false, StandardCharsets.UTF_8); + this.target = target; + } + + @Override + public boolean checkError() { + return super.checkError() || target.checkError(); + } + } + + /** + * Holds each thread's output until its line is complete, then writes the line, tagged with the + * session bound when it started, to the target in one write. All wrappers write under one lock, + * so every physical line carries one writer's tag and lines from different threads and streams + * never interleave. A partial line that is flushed, or reaches {@link #LINE_LIMIT}, is written + * as a complete line, so its continuation starts a new tagged line. The target receives UTF-8 + * whatever its own charset. + */ + private static final class TaggingStream extends OutputStream { + private static final int LINE_LIMIT = 8192; + private static final Object LOCK = new Object(); + + private final OutputStream target; + private final ThreadLocal lines = ThreadLocal.withInitial(Line::new); + + TaggingStream(OutputStream target) { + this.target = target; + } + + @Override + public void write(int b) throws IOException { + write(new byte[] {(byte) b}, 0, 1); + } + + @Override + public void write(byte[] bytes, int offset, int length) throws IOException { + var line = lines.get(); + var end = offset + length; + while (offset < end) { + if (line.size == 0) line.session = CURRENT.get(); + var newline = offset; + while (newline < end && bytes[newline] != '\n') newline++; + var segmentEnd = Math.min(newline + 1, end); + var take = Math.min(segmentEnd - offset, LINE_LIMIT - line.size); + line.append(bytes, offset, take); + offset += take; + if (offset == segmentEnd && newline < end) emit(line, line.size, true); + else if (line.size >= LINE_LIMIT) + emit(line, line.size - incompleteCharacter(line), false); + } + } + + @Override + public void flush() throws IOException { + var line = lines.get(); + if (line.size > 0) emit(line, line.size, false); + synchronized (LOCK) { + target.flush(); + } + } + + /** Writes the line's first {@code length} bytes and keeps the rest as its continuation. */ + private void emit(Line line, int length, boolean complete) throws IOException { + var tag = + line.session == null + ? new byte[0] + : ("[session:" + line.session + "] ").getBytes(StandardCharsets.UTF_8); + var output = new byte[tag.length + length + (complete ? 0 : 1)]; + System.arraycopy(tag, 0, output, 0, tag.length); + System.arraycopy(line.bytes, 0, output, tag.length, length); + if (!complete) output[output.length - 1] = '\n'; + System.arraycopy(line.bytes, length, line.bytes, 0, line.size - length); + line.size -= length; + synchronized (LOCK) { + target.write(output, 0, output.length); + target.flush(); + } + } + + /** The bytes at the end of the line that begin a UTF-8 character it doesn't complete. */ + private static int incompleteCharacter(Line line) { + for (var back = 1; back <= Math.min(3, line.size); back++) { + var b = line.bytes[line.size - back]; + if ((b & 0xC0) == 0x80) continue; + var length = + (b & 0xE0) == 0xC0 + ? 2 + : (b & 0xF0) == 0xE0 ? 3 : (b & 0xF8) == 0xF0 ? 4 : 1; + return length > back ? back : 0; + } + return 0; + } + } + + /** One thread's pending output on one stream, never longer than the line limit. */ + private static final class Line { + byte[] bytes = new byte[256]; + int size; + @Nullable String session; + + void append(byte[] source, int offset, int length) { + if (size + length > bytes.length) { + bytes = Arrays.copyOf(bytes, Math.max(bytes.length * 2, size + length)); + } + System.arraycopy(source, offset, bytes, size, length); + size += length; + } + } +} diff --git a/jvm/runtime/src/test/java/com/chunkzero/chunk/runtime/SessionLoggingTest.java b/jvm/runtime/src/test/java/com/chunkzero/chunk/runtime/SessionLoggingTest.java new file mode 100644 index 00000000..9d439f96 --- /dev/null +++ b/jvm/runtime/src/test/java/com/chunkzero/chunk/runtime/SessionLoggingTest.java @@ -0,0 +1,197 @@ +package com.chunkzero.chunk.runtime; + +import static org.junit.jupiter.api.Assertions.*; + +import chunk.sync.v1.Jvm.JvmSession; + +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionStage; +import java.util.concurrent.CountDownLatch; + +class SessionLoggingTest { + @Test + void tagsOnlyLinesEmittedInsideASessionContext() { + var raw = new ByteArrayOutputStream(); + var stream = SessionLogging.wrap(new PrintStream(raw, true, StandardCharsets.UTF_8)); + + stream.println("startup message"); + SessionLogging.run( + "sess-1", + () -> { + stream.print("round "); + stream.println("started"); + stream.println("line 1\nline 2"); + stream.println(); + SessionLogging.run("sess-2", () -> stream.println("nested")); + assertEquals("sess-1", SessionLogging.current()); + stream.println("back to outer"); + }); + assertNull(SessionLogging.current()); + stream.println("gc message"); + + var lines = raw.toString(StandardCharsets.UTF_8).lines().toList(); + assertEquals( + List.of( + "startup message", + "[session:sess-1] round started", + "[session:sess-1] line 1", + "[session:sess-1] line 2", + "[session:sess-1] ", + "[session:sess-2] nested", + "[session:sess-1] back to outer", + "gc message"), + lines); + } + + @Test + void concurrentLinesOnBothStreamsKeepTheirOwnTagAndBody() throws Exception { + var raw = new ByteArrayOutputStream(); + var out = SessionLogging.wrap(new PrintStream(raw, true, StandardCharsets.UTF_8)); + var err = SessionLogging.wrap(new PrintStream(raw, true, StandardCharsets.UTF_8)); + var start = new CountDownLatch(1); + var threads = + List.of(writer("A", out, start), writer("B", err, start), writer("C", out, start)); + threads.forEach(Thread::start); + start.countDown(); + for (var thread : threads) thread.join(); + + var lines = raw.toString(StandardCharsets.UTF_8).lines().toList(); + assertEquals(3 * 2000, lines.size()); + for (var line : lines) { + assertTrue(line.matches("\\[session:(.)\\] \\1 line \\d+"), line); + } + } + + @Test + void encodesInUtf8WhateverTheTargetsCharset() { + var raw = new ByteArrayOutputStream(); + var stream = SessionLogging.wrap(new PrintStream(raw, true, StandardCharsets.UTF_16)); + + SessionLogging.run("s", () -> stream.println("héllo ✓")); + stream.println("plain ✓"); + + assertEquals("[session:s] héllo ✓\nplain ✓\n", raw.toString(StandardCharsets.UTF_8)); + } + + @Test + void partialLinesAreWrittenWholeSoNoTagContinuesAnother() throws Exception { + var raw = new ByteArrayOutputStream(); + var stream = SessionLogging.wrap(new PrintStream(raw, true, StandardCharsets.UTF_8)); + var firstPartial = new CountDownLatch(1); + var other = new CountDownLatch(1); + var first = + new Thread( + () -> + SessionLogging.run( + "A", + () -> { + stream.print("progress"); + stream.flush(); + firstPartial.countDown(); + try { + other.await(); + } catch (InterruptedException interrupted) { + return; + } + stream.println(" done"); + })); + first.start(); + firstPartial.await(); + SessionLogging.run("B", () -> stream.println("message B")); + other.countDown(); + first.join(); + + assertEquals( + List.of("[session:A] progress", "[session:B] message B", "[session:A] done"), + raw.toString(StandardCharsets.UTF_8).lines().toList()); + } + + @Test + void aLineLongerThanTheLimitIsWrittenInBoundedTaggedPieces() { + var raw = new ByteArrayOutputStream(); + var stream = SessionLogging.wrap(new PrintStream(raw, true, StandardCharsets.UTF_8)); + var big = "x".repeat(20_000).getBytes(StandardCharsets.UTF_8); + + SessionLogging.run( + "s", + () -> { + stream.write(big, 0, big.length); + stream.println(); + }); + + var lines = raw.toString(StandardCharsets.UTF_8).lines().toList(); + assertEquals( + List.of(8192, 8192, 3616), lines.stream().map(line -> line.length() - 12).toList()); + assertTrue(lines.stream().allMatch(line -> line.startsWith("[session:s] x"))); + } + + @Test + void aLineSplitAtTheLimitKeepsMultibyteCharactersWhole() { + var raw = new ByteArrayOutputStream(); + var stream = SessionLogging.wrap(new PrintStream(raw, true, StandardCharsets.UTF_8)); + + SessionLogging.run("s", () -> stream.println("x".repeat(8191) + "✓")); + + assertEquals( + List.of("[session:s] " + "x".repeat(8191), "[session:s] ✓"), + raw.toString(StandardCharsets.UTF_8).lines().toList()); + } + + private static Thread writer(String session, PrintStream stream, CountDownLatch start) { + return new Thread( + () -> { + try { + start.await(); + } catch (InterruptedException interrupted) { + return; + } + SessionLogging.run( + session, + () -> { + for (var i = 0; i < 2000; i++) { + stream.println(session + " line " + i); + } + }); + }); + } + + @Test + void chunkSessionsBindsSessionContextForHandlerCallbacks() { + var observed = new ArrayList(); + var handler = + new SessionHandler() { + @Override + public void create(SessionControl session) { + observed.add("create:" + SessionLogging.current()); + session.ready(); + } + + @Override + public void finish(SessionControl session) { + observed.add("finish:" + SessionLogging.current()); + session.ended(); + } + + @Override + public CompletionStage method( + SessionControl session, SessionMethod call) { + observed.add("method:" + SessionLogging.current()); + return CompletableFuture.completedFuture("null"); + } + }; + var host = ChunkSessions.detached(handler, ignored -> null); + var spec = JvmSession.newBuilder().setSessionType("app/default").setCapacity(4).build(); + host.create("abc-123", spec).join(); + host.finish("abc-123", spec).join(); + + assertNull(SessionLogging.current()); + assertEquals(List.of("create:abc-123", "finish:abc-123"), observed); + } +} diff --git a/proto/chunk/sync/v1/operator.proto b/proto/chunk/sync/v1/operator.proto index cf802abb..f8022668 100644 --- a/proto/chunk/sync/v1/operator.proto +++ b/proto/chunk/sync/v1/operator.proto @@ -43,6 +43,21 @@ message Node { uint64 drain_deadline_ms = 8; // While DRAINING or STOPPING: the host's open claims. uint32 remaining_claims = 9; + // The host's open sessions, ordered by session ID. + repeated NodeSession sessions = 10; +} + +// An open session running on a host. +message NodeSession { + string id = 1; + // The session type, as `/`. + string session_type = 2; + // The session's destination key; empty for a session recovery found running without a log row. + string key = 3; + // Players with an open claim in this session. + uint32 players = 4; + // Where the session is in its lifecycle. + JvmSessionPhase phase = 5; } // Where a host is in its lifecycle.