refactor: remove dead code, consolidate files
This commit is contained in:
parent
d1b34b70eb
commit
3d596d3278
66 changed files with 2937 additions and 4815 deletions
6
Cargo.lock
generated
6
Cargo.lock
generated
|
|
@ -1327,13 +1327,12 @@ dependencies = [
|
|||
name = "distribution"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"ed25519-dalek 2.2.0",
|
||||
"iroh",
|
||||
"iroh-relay",
|
||||
"rand_core 0.6.4",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"swactor",
|
||||
"swactor-transport",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
|
|
@ -5337,16 +5336,15 @@ dependencies = [
|
|||
"clap",
|
||||
"crossbeam-queue",
|
||||
"ctrlc",
|
||||
"ed25519-dalek 2.2.0",
|
||||
"getrandom 0.2.17",
|
||||
"iroh",
|
||||
"proptest",
|
||||
"proptest-state-machine",
|
||||
"rand_core 0.6.4",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"stateright",
|
||||
"swactor",
|
||||
"swactor-transport",
|
||||
"tempfile",
|
||||
"tiny_http",
|
||||
"tokio",
|
||||
|
|
|
|||
|
|
@ -1,416 +0,0 @@
|
|||
pub const ACTOR_DETAIL_HTML: &str = r##"<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Actor Detail — Swactor Dashboard</title>
|
||||
<style>
|
||||
* { margin: 0; padding: 0; box-sizing: border-box; }
|
||||
body { font-family: 'Menlo', 'Consolas', 'Monaco', monospace; background: #0f1117; color: #e0e0e0; font-size: 13px; }
|
||||
|
||||
.header {
|
||||
display: flex; align-items: center; justify-content: space-between;
|
||||
padding: 12px 20px; background: #161822; border-bottom: 1px solid #2a2d3e;
|
||||
}
|
||||
.header h1 { font-size: 16px; font-weight: 600; color: #fff; }
|
||||
.status-dot {
|
||||
width: 10px; height: 10px; border-radius: 50%; background: #4caf50;
|
||||
display: inline-block; margin-left: 8px; vertical-align: middle;
|
||||
}
|
||||
.status-dot.disconnected { background: #f44336; }
|
||||
|
||||
.header-left { display: flex; align-items: center; }
|
||||
.nav-links { display: flex; gap: 4px; margin-left: 20px; }
|
||||
.nav-link {
|
||||
color: #888; text-decoration: none; font-size: 12px;
|
||||
padding: 4px 10px; border-radius: 3px; transition: color 0.2s;
|
||||
}
|
||||
.nav-link:hover { color: #e0e0e0; }
|
||||
.nav-link.active { color: #fff; background: #2a2d3e; }
|
||||
|
||||
.content { padding: 16px 20px; max-width: 900px; }
|
||||
|
||||
.breadcrumb { color: #555; font-size: 12px; margin-bottom: 12px; }
|
||||
.breadcrumb a { color: #888; text-decoration: none; }
|
||||
.breadcrumb a:hover { color: #e0e0e0; }
|
||||
|
||||
.info-card {
|
||||
background: #161822; border: 1px solid #2a2d3e; border-radius: 6px;
|
||||
padding: 16px; margin-bottom: 12px;
|
||||
}
|
||||
.info-row { display: flex; gap: 24px; margin-bottom: 6px; flex-wrap: wrap; }
|
||||
.info-label { color: #888; font-size: 11px; text-transform: uppercase; }
|
||||
.info-value { color: #fff; font-weight: 700; font-size: 15px; }
|
||||
.info-value.healthy { color: #4caf50; }
|
||||
.info-value.poisoned { color: #f44336; }
|
||||
|
||||
.stats-cards {
|
||||
display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px;
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
.stat-card {
|
||||
background: #161822; border: 1px solid #2a2d3e; border-radius: 4px;
|
||||
padding: 12px; text-align: center;
|
||||
}
|
||||
.stat-card .value { font-size: 22px; font-weight: 700; color: #fff; }
|
||||
.stat-card .label { font-size: 10px; color: #888; text-transform: uppercase; margin-top: 2px; }
|
||||
|
||||
.sparkline-panel {
|
||||
background: #161822; border: 1px solid #2a2d3e; border-radius: 6px;
|
||||
padding: 14px; margin-bottom: 12px;
|
||||
}
|
||||
.sparkline-panel h3 { font-size: 11px; color: #888; text-transform: uppercase; letter-spacing: 1px; margin-bottom: 8px; }
|
||||
.sparkline-panel svg { width: 100%; height: 50px; }
|
||||
|
||||
.type-breakdown {
|
||||
background: #161822; border: 1px solid #2a2d3e; border-radius: 6px;
|
||||
padding: 14px; margin-bottom: 12px;
|
||||
}
|
||||
.type-breakdown h3 { font-size: 11px; color: #888; text-transform: uppercase; letter-spacing: 1px; margin-bottom: 8px; }
|
||||
.type-row { display: flex; align-items: center; gap: 8px; margin-bottom: 4px; font-size: 11px; }
|
||||
.type-name { color: #e0e0e0; min-width: 180px; text-overflow: ellipsis; overflow: hidden; white-space: nowrap; }
|
||||
.type-bar-bg { flex: 1; background: #1e2030; height: 14px; border-radius: 2px; overflow: hidden; }
|
||||
.type-bar-fill { height: 100%; border-radius: 2px; }
|
||||
.type-count { color: #888; min-width: 60px; text-align: right; }
|
||||
.type-pct { color: #555; min-width: 40px; text-align: right; }
|
||||
|
||||
.logs-panel {
|
||||
background: #161822; border: 1px solid #2a2d3e; border-radius: 6px;
|
||||
padding: 14px; margin-bottom: 12px;
|
||||
}
|
||||
.logs-panel h3 {
|
||||
font-size: 11px; color: #888; text-transform: uppercase; letter-spacing: 1px;
|
||||
margin-bottom: 8px; display: flex; align-items: center; gap: 12px;
|
||||
}
|
||||
.level-filter { display: flex; gap: 4px; }
|
||||
.level-btn {
|
||||
background: #1e2030; border: 1px solid #2a2d3e; border-radius: 3px;
|
||||
color: #888; font-size: 10px; padding: 1px 6px; cursor: pointer;
|
||||
font-family: inherit;
|
||||
}
|
||||
.level-btn.active { border-color: #555; color: #fff; }
|
||||
.level-btn.error { color: #f44336; }
|
||||
.level-btn.warn { color: #ff9800; }
|
||||
.level-btn.info { color: #2196f3; }
|
||||
.level-btn.debug { color: #888; }
|
||||
.level-btn.trace { color: #555; }
|
||||
|
||||
.log-list {
|
||||
max-height: 400px; overflow-y: auto; font-size: 11px; line-height: 1.6;
|
||||
}
|
||||
.log-entry { display: flex; gap: 8px; padding: 1px 0; border-bottom: 1px solid #1a1c2e; }
|
||||
.log-time { color: #555; white-space: nowrap; min-width: 80px; }
|
||||
.log-level { font-weight: 700; min-width: 50px; }
|
||||
.log-level.ERROR { color: #f44336; }
|
||||
.log-level.WARN { color: #ff9800; }
|
||||
.log-level.INFO { color: #2196f3; }
|
||||
.log-level.DEBUG { color: #888; }
|
||||
.log-level.TRACE { color: #555; }
|
||||
.log-msg { color: #e0e0e0; word-break: break-all; }
|
||||
|
||||
::-webkit-scrollbar { width: 6px; }
|
||||
::-webkit-scrollbar-track { background: #0f1117; }
|
||||
::-webkit-scrollbar-thumb { background: #2a2d3e; border-radius: 3px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="header">
|
||||
<div class="header-left">
|
||||
<h1>
|
||||
Swactor Runtime Dashboard
|
||||
<span id="statusDot" class="status-dot"></span>
|
||||
</h1>
|
||||
<nav class="nav-links">
|
||||
<a href="/" class="nav-link">Overview</a>
|
||||
<a href="/actors" class="nav-link">Actors</a>
|
||||
<a href="/plugin/distribution" class="nav-link">Distribution</a>
|
||||
<a href="/plugin/datastore" class="nav-link">Datastore</a>
|
||||
</nav>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="content">
|
||||
<div class="breadcrumb">
|
||||
<a href="/">Overview</a> / <a href="/actors">Actors</a> / <span id="addrBreadcrumb">—</span>
|
||||
</div>
|
||||
|
||||
<div class="info-card">
|
||||
<div class="info-row" id="nameRow" style="display:none;">
|
||||
<div><div class="info-label">Name</div><div class="info-value" id="addrName" style="color:#00bcd4;">—</div></div>
|
||||
</div>
|
||||
<div class="info-row">
|
||||
<div><div class="info-label">Address</div><div class="info-value" id="addrFull" style="font-size:12px;color:#aaa;font-weight:400;">—</div></div>
|
||||
<div><div class="info-label">Worker</div><div class="info-value" id="addrWorker">—</div></div>
|
||||
<div><div class="info-label">Status</div><div class="info-value" id="addrStatus">—</div></div>
|
||||
</div>
|
||||
<div class="info-row">
|
||||
<div><div class="info-label">Last Message Type</div><div class="info-value" id="addrLastMsg" style="color:#4caf50;font-size:13px;">—</div></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="stats-cards">
|
||||
<div class="stat-card"><div class="value" id="addrMsgs">0</div><div class="label">Messages</div></div>
|
||||
<div class="stat-card"><div class="value" id="addrMailbox">0</div><div class="label">Mailbox</div></div>
|
||||
<div class="stat-card"><div class="value" id="addrRate">0</div><div class="label">Msg/s</div></div>
|
||||
<div class="stat-card"><div class="value" id="addrWorkerLoad">—</div><div class="label">Worker Load</div></div>
|
||||
</div>
|
||||
|
||||
<div class="sparkline-panel">
|
||||
<h3>Message Rate</h3>
|
||||
<svg id="rateSpark" viewBox="0 0 400 50" preserveAspectRatio="none"></svg>
|
||||
</div>
|
||||
|
||||
<div class="sparkline-panel">
|
||||
<h3>Mailbox Depth</h3>
|
||||
<svg id="mboxSpark" viewBox="0 0 400 50" preserveAspectRatio="none"></svg>
|
||||
</div>
|
||||
|
||||
<div class="type-breakdown" id="typeBreakdown" style="display:none;">
|
||||
<h3>Message Types</h3>
|
||||
<div id="typeRows"></div>
|
||||
</div>
|
||||
|
||||
<div class="logs-panel">
|
||||
<h3>
|
||||
<span>Logs</span>
|
||||
<span id="logCount" style="color:#555;">(0)</span>
|
||||
<div class="level-filter">
|
||||
<button class="level-btn error active" data-level="ERROR" onclick="toggleLevel(this)">ERR</button>
|
||||
<button class="level-btn warn active" data-level="WARN" onclick="toggleLevel(this)">WARN</button>
|
||||
<button class="level-btn info active" data-level="INFO" onclick="toggleLevel(this)">INFO</button>
|
||||
<button class="level-btn debug active" data-level="DEBUG" onclick="toggleLevel(this)">DBG</button>
|
||||
<button class="level-btn trace active" data-level="TRACE" onclick="toggleLevel(this)">TRC</button>
|
||||
</div>
|
||||
</h3>
|
||||
<div class="log-list" id="logList"></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
(function() {
|
||||
var targetAddr = '__ACTOR_ADDR__';
|
||||
var dot = document.getElementById('statusDot');
|
||||
|
||||
var history = { rates: [], mailbox: [], prev_msgs: 0 };
|
||||
|
||||
function formatAddr(addr) {
|
||||
if (!addr) return '';
|
||||
var bytes = Array.isArray(addr) ? addr : Object.values(addr);
|
||||
var hex = '';
|
||||
for (var i = 0; i < bytes.length; i++) {
|
||||
hex += ('0' + bytes[i].toString(16)).slice(-2);
|
||||
}
|
||||
return hex;
|
||||
}
|
||||
|
||||
function shortAddr(hex) {
|
||||
return hex.length > 16 ? hex.substring(0, 16) + '\u2026' : hex;
|
||||
}
|
||||
|
||||
function shortTypeName(full) {
|
||||
if (!full) return '\u2014';
|
||||
var parts = full.split('::');
|
||||
return parts[parts.length - 1];
|
||||
}
|
||||
|
||||
function updateSparklineSvg(svgEl, data, color) {
|
||||
if (!data || data.length < 2) { svgEl.innerHTML = ''; return; }
|
||||
var max = Math.max.apply(null, data);
|
||||
if (max === 0) max = 1;
|
||||
var w = 400, h = 50;
|
||||
var step = w / (data.length - 1);
|
||||
var points = data.map(function(v, i) {
|
||||
return (i * step).toFixed(1) + ',' + (h - (v / max) * (h - 4) - 2).toFixed(1);
|
||||
}).join(' ');
|
||||
svgEl.innerHTML = '<polyline fill="none" stroke="' + color + '" stroke-width="2" points="' + points + '"/>';
|
||||
}
|
||||
|
||||
function findActor(stats) {
|
||||
if (!stats.actor_details) return null;
|
||||
for (var i = 0; i < stats.actor_details.length; i++) {
|
||||
var a = stats.actor_details[i];
|
||||
var hex = formatAddr(a.address);
|
||||
if (hex === targetAddr || hex.indexOf(targetAddr) === 0) return a;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function updateDetail(stats) {
|
||||
var actor = findActor(stats);
|
||||
if (!actor) return;
|
||||
|
||||
var hex = formatAddr(actor.address);
|
||||
if (actor.name) {
|
||||
document.getElementById('addrBreadcrumb').textContent = actor.name;
|
||||
document.getElementById('nameRow').style.display = '';
|
||||
document.getElementById('addrName').textContent = actor.name;
|
||||
} else {
|
||||
document.getElementById('addrBreadcrumb').textContent = shortAddr(hex);
|
||||
document.getElementById('nameRow').style.display = 'none';
|
||||
}
|
||||
document.getElementById('addrFull').textContent = hex;
|
||||
document.getElementById('addrWorker').textContent = 'W' + actor.worker_id;
|
||||
|
||||
var statusEl = document.getElementById('addrStatus');
|
||||
if (actor.poisoned) {
|
||||
statusEl.textContent = 'POISONED';
|
||||
statusEl.className = 'info-value poisoned';
|
||||
} else {
|
||||
statusEl.textContent = 'Healthy';
|
||||
statusEl.className = 'info-value healthy';
|
||||
}
|
||||
|
||||
document.getElementById('addrLastMsg').textContent = shortTypeName(actor.last_msg_type);
|
||||
document.getElementById('addrMsgs').textContent = actor.messages_processed.toLocaleString();
|
||||
document.getElementById('addrMailbox').textContent = actor.mailbox_depth;
|
||||
|
||||
// Compute rate
|
||||
var rate = actor.messages_processed - history.prev_msgs;
|
||||
if (rate < 0) rate = 0;
|
||||
history.prev_msgs = actor.messages_processed;
|
||||
history.rates.push(rate);
|
||||
history.mailbox.push(actor.mailbox_depth);
|
||||
if (history.rates.length > 300) { history.rates.shift(); history.mailbox.shift(); }
|
||||
|
||||
// Rate per second (SSE interval is ~200ms, so multiply by 5)
|
||||
document.getElementById('addrRate').textContent = (rate * 5).toLocaleString();
|
||||
|
||||
// Worker load
|
||||
if (stats.workers) {
|
||||
var w = stats.workers.find(function(w) { return w.id === actor.worker_id; });
|
||||
if (w) {
|
||||
document.getElementById('addrWorkerLoad').textContent =
|
||||
w.num_actors + ' actors, mbox ' + w.mailbox_depth;
|
||||
}
|
||||
}
|
||||
|
||||
updateSparklineSvg(document.getElementById('rateSpark'), history.rates, '#4caf50');
|
||||
updateSparklineSvg(document.getElementById('mboxSpark'), history.mailbox, '#2196f3');
|
||||
|
||||
// Update message type breakdown
|
||||
updateTypeBreakdown(actor.message_type_counts);
|
||||
}
|
||||
|
||||
var typeColors = ['#4caf50','#2196f3','#ff9800','#9c27b0','#00bcd4','#f44336','#ffeb3b','#e91e63'];
|
||||
|
||||
function updateTypeBreakdown(types) {
|
||||
var panel = document.getElementById('typeBreakdown');
|
||||
var container = document.getElementById('typeRows');
|
||||
if (!types || types.length === 0) { panel.style.display = 'none'; return; }
|
||||
panel.style.display = '';
|
||||
var total = 0;
|
||||
for (var i = 0; i < types.length; i++) total += types[i][1];
|
||||
if (total === 0) { panel.style.display = 'none'; return; }
|
||||
|
||||
var html = '';
|
||||
for (var i = 0; i < types.length; i++) {
|
||||
var name = types[i][0];
|
||||
var count = types[i][1];
|
||||
var pct = (count / total * 100).toFixed(1);
|
||||
var barPct = (count / types[0][1] * 100).toFixed(1);
|
||||
var color = typeColors[i % typeColors.length];
|
||||
var shortName = name.split('::').pop();
|
||||
html += '<div class="type-row">' +
|
||||
'<span class="type-name" title="' + escapeHtml(name) + '">' + escapeHtml(shortName) + '</span>' +
|
||||
'<div class="type-bar-bg"><div class="type-bar-fill" style="width:' + barPct + '%;background:' + color + ';"></div></div>' +
|
||||
'<span class="type-count">' + count.toLocaleString() + '</span>' +
|
||||
'<span class="type-pct">' + pct + '%</span>' +
|
||||
'</div>';
|
||||
}
|
||||
container.innerHTML = html;
|
||||
}
|
||||
|
||||
// ─── Logging ─────────────────────────────────────────────────
|
||||
var activeLevels = { ERROR: true, WARN: true, INFO: true, DEBUG: true, TRACE: true };
|
||||
var allLogs = [];
|
||||
var logList = document.getElementById('logList');
|
||||
var logCount = document.getElementById('logCount');
|
||||
var autoScroll = true;
|
||||
|
||||
window.toggleLevel = function(btn) {
|
||||
var lvl = btn.getAttribute('data-level');
|
||||
activeLevels[lvl] = !activeLevels[lvl];
|
||||
btn.classList.toggle('active');
|
||||
renderLogs();
|
||||
};
|
||||
|
||||
function formatLogTime(ms) {
|
||||
var d = new Date(ms);
|
||||
return ('0' + d.getHours()).slice(-2) + ':' +
|
||||
('0' + d.getMinutes()).slice(-2) + ':' +
|
||||
('0' + d.getSeconds()).slice(-2) + '.' +
|
||||
('00' + d.getMilliseconds()).slice(-3);
|
||||
}
|
||||
|
||||
function renderLogs() {
|
||||
var visible = allLogs.filter(function(e) { return activeLevels[e.level]; });
|
||||
logCount.textContent = '(' + visible.length + ')';
|
||||
var html = '';
|
||||
for (var i = 0; i < visible.length; i++) {
|
||||
var e = visible[i];
|
||||
html += '<div class="log-entry">' +
|
||||
'<span class="log-time">' + formatLogTime(e.timestamp_ms) + '</span>' +
|
||||
'<span class="log-level ' + e.level + '">' + e.level + '</span>' +
|
||||
'<span class="log-msg">' + escapeHtml(e.message) + '</span>' +
|
||||
'</div>';
|
||||
}
|
||||
logList.innerHTML = html;
|
||||
if (autoScroll) {
|
||||
logList.scrollTop = logList.scrollHeight;
|
||||
}
|
||||
}
|
||||
|
||||
function escapeHtml(s) {
|
||||
if (!s) return '';
|
||||
return s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
||||
}
|
||||
|
||||
function addLogEntries(events) {
|
||||
for (var i = 0; i < events.length; i++) {
|
||||
var e = events[i];
|
||||
if (!e.actor_addr) continue;
|
||||
// Match if event's actor_addr starts with or contains target
|
||||
var a = e.actor_addr.toLowerCase();
|
||||
if (a.indexOf(targetAddr.toLowerCase()) === 0 || a === targetAddr.toLowerCase()) {
|
||||
allLogs.push(e);
|
||||
}
|
||||
}
|
||||
// Keep bounded
|
||||
while (allLogs.length > 500) allLogs.shift();
|
||||
renderLogs();
|
||||
}
|
||||
|
||||
// Fetch initial logs
|
||||
fetch('/api/logs?actor=' + targetAddr + '&limit=200')
|
||||
.then(function(r) { return r.json(); })
|
||||
.then(function(data) { if (Array.isArray(data)) addLogEntries(data); })
|
||||
.catch(function() {});
|
||||
|
||||
logList.addEventListener('scroll', function() {
|
||||
autoScroll = (logList.scrollTop + logList.clientHeight >= logList.scrollHeight - 20);
|
||||
});
|
||||
|
||||
// ─── SSE ────────────────────────────────────────────────────
|
||||
var es = new EventSource('/events');
|
||||
window.addEventListener('beforeunload', function() { es.close(); });
|
||||
|
||||
es.addEventListener('stats', function(e) {
|
||||
try { updateDetail(JSON.parse(e.data)); } catch(err) { console.error(err); }
|
||||
});
|
||||
|
||||
es.addEventListener('activity', function(e) {
|
||||
try { addLogEntries(JSON.parse(e.data)); } catch(err) { console.error(err); }
|
||||
});
|
||||
|
||||
es.addEventListener('done', function() {
|
||||
dot.className = 'status-dot disconnected';
|
||||
es.close();
|
||||
});
|
||||
|
||||
es.onerror = function() { dot.className = 'status-dot disconnected'; };
|
||||
es.onopen = function() { dot.className = 'status-dot'; };
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
"##;
|
||||
|
|
@ -1,807 +0,0 @@
|
|||
pub const ACTORS_HTML: &str = r##"<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Swactor Runtime – Actors</title>
|
||||
<style>
|
||||
* { margin: 0; padding: 0; box-sizing: border-box; }
|
||||
body { font-family: 'Menlo', 'Consolas', 'Monaco', monospace; background: #0f1117; color: #e0e0e0; font-size: 13px; }
|
||||
|
||||
.header {
|
||||
display: flex; align-items: center; justify-content: space-between;
|
||||
padding: 12px 20px; background: #161822; border-bottom: 1px solid #2a2d3e;
|
||||
}
|
||||
.header-left { display: flex; align-items: center; }
|
||||
.header h1 { font-size: 16px; font-weight: 600; color: #fff; }
|
||||
.status-dot {
|
||||
width: 10px; height: 10px; border-radius: 50%; background: #4caf50;
|
||||
display: inline-block; margin-left: 8px; vertical-align: middle;
|
||||
}
|
||||
.status-dot.disconnected { background: #f44336; }
|
||||
.status-dot.done { background: #ff9800; }
|
||||
.status-dot.replaying { background: #2196f3; animation: pulse 1.5s infinite; }
|
||||
|
||||
@keyframes pulse {
|
||||
0%, 100% { opacity: 1; }
|
||||
50% { opacity: 0.4; }
|
||||
}
|
||||
|
||||
.replay-badge {
|
||||
display: none; background: #2196f3; color: #fff; font-size: 10px; font-weight: 700;
|
||||
padding: 2px 8px; border-radius: 3px; margin-left: 10px; letter-spacing: 1px;
|
||||
vertical-align: middle;
|
||||
}
|
||||
.replay-badge.visible { display: inline-block; }
|
||||
|
||||
.nav-links { display: flex; gap: 4px; margin-left: 20px; }
|
||||
.nav-link {
|
||||
color: #888; text-decoration: none; font-size: 12px;
|
||||
padding: 4px 10px; border-radius: 3px; transition: color 0.2s;
|
||||
}
|
||||
.nav-link:hover { color: #e0e0e0; }
|
||||
.nav-link.active { color: #fff; background: #2a2d3e; }
|
||||
|
||||
.header-right { display: flex; align-items: center; gap: 12px; }
|
||||
|
||||
.progress-bar-wrap {
|
||||
display: none; width: 100%; height: 3px; background: #2a2d3e;
|
||||
}
|
||||
.progress-bar-wrap.visible { display: block; }
|
||||
.progress-fill {
|
||||
height: 100%; width: 0%; background: #2196f3; transition: width 0.3s;
|
||||
}
|
||||
|
||||
.grid {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr 1fr;
|
||||
gap: 12px; padding: 12px;
|
||||
}
|
||||
|
||||
.panel {
|
||||
background: #161822; border: 1px solid #2a2d3e; border-radius: 6px;
|
||||
padding: 14px; overflow: visible;
|
||||
}
|
||||
.panel h2 { font-size: 12px; color: #888; text-transform: uppercase; letter-spacing: 1px; margin-bottom: 10px; }
|
||||
|
||||
.full-width { grid-column: 1 / -1; }
|
||||
|
||||
.stats-cards {
|
||||
display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px;
|
||||
}
|
||||
.stat-card {
|
||||
background: #1c1f2e; border-radius: 4px; padding: 10px; text-align: center;
|
||||
}
|
||||
.stat-card .value { font-size: 22px; font-weight: 700; color: #fff; }
|
||||
.stat-card .label { font-size: 10px; color: #888; text-transform: uppercase; margin-top: 2px; }
|
||||
|
||||
canvas { width: 100%; height: 220px; }
|
||||
|
||||
.search-wrap { margin-bottom: 10px; display: flex; align-items: center; gap: 12px; }
|
||||
.search-input {
|
||||
background: #1c1f2e; border: 1px solid #2a2d3e; color: #e0e0e0;
|
||||
padding: 6px 10px; border-radius: 4px; font-family: inherit;
|
||||
font-size: 12px; width: 300px; outline: none;
|
||||
}
|
||||
.search-input:focus { border-color: #6366f1; }
|
||||
.search-info { color: #555; font-size: 11px; }
|
||||
|
||||
.actor-list-wrap { max-height: 500px; overflow-y: auto; }
|
||||
.actor-list-wrap table { width: 100%; border-collapse: collapse; }
|
||||
.actor-list-wrap th, .actor-list-wrap td {
|
||||
padding: 4px 8px; text-align: left; border-bottom: 1px solid #2a2d3e; font-size: 12px;
|
||||
}
|
||||
.actor-list-wrap th {
|
||||
color: #888; font-weight: 500; position: sticky; top: 0; background: #161822;
|
||||
}
|
||||
.sortable { cursor: pointer; user-select: none; }
|
||||
.sortable:hover { color: #e0e0e0; }
|
||||
.sort-arrow { font-size: 10px; margin-left: 4px; color: #6366f1; }
|
||||
|
||||
.depth-bar {
|
||||
height: 8px; border-radius: 2px; max-width: 120px; min-width: 2px;
|
||||
}
|
||||
|
||||
.msg-type { color: #4caf50; }
|
||||
.msg-type.none { color: #555; font-style: italic; }
|
||||
|
||||
tr.focused { background: #1c1f2e; }
|
||||
tr.clickable { cursor: pointer; }
|
||||
tr.clickable:hover { background: #1a1d2c; }
|
||||
|
||||
.detail-panel {
|
||||
display: none; position: fixed; bottom: 12px; right: 12px;
|
||||
width: 420px; max-height: 320px; overflow-y: auto;
|
||||
background: #161822; border: 1px solid #2a2d3e; border-radius: 6px;
|
||||
padding: 14px; z-index: 100; box-shadow: 0 4px 24px rgba(0,0,0,0.5);
|
||||
}
|
||||
.detail-panel.visible { display: block; }
|
||||
.detail-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
|
||||
.detail-close {
|
||||
background: none; border: 1px solid #2a2d3e; color: #888; border-radius: 3px;
|
||||
padding: 2px 8px; cursor: pointer; font-family: inherit; font-size: 11px;
|
||||
}
|
||||
.detail-close:hover { color: #e0e0e0; border-color: #555; }
|
||||
.detail-grid {
|
||||
display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin-bottom: 10px;
|
||||
}
|
||||
.detail-item { background: #1c1f2e; border-radius: 4px; padding: 6px 8px; }
|
||||
.detail-item .d-label { font-size: 10px; color: #888; text-transform: uppercase; }
|
||||
.detail-item .d-value { font-size: 13px; font-weight: 700; color: #fff; margin-top: 2px; word-break: break-all; }
|
||||
.poisoned-badge {
|
||||
background: #f44336; color: #fff; font-size: 10px; font-weight: 700;
|
||||
padding: 2px 6px; border-radius: 3px; letter-spacing: 0.5px;
|
||||
}
|
||||
.healthy-badge { color: #4caf50; font-size: 12px; }
|
||||
canvas.sparkline { width: 100%; height: 50px; }
|
||||
|
||||
::-webkit-scrollbar { width: 6px; }
|
||||
::-webkit-scrollbar-track { background: #0f1117; }
|
||||
::-webkit-scrollbar-thumb { background: #2a2d3e; border-radius: 3px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="header">
|
||||
<div class="header-left">
|
||||
<h1>
|
||||
Swactor Runtime Dashboard
|
||||
<span id="statusDot" class="status-dot"></span>
|
||||
<span id="replayBadge" class="replay-badge">REPLAY</span>
|
||||
</h1>
|
||||
<nav class="nav-links">
|
||||
<a href="/" class="nav-link">Overview</a>
|
||||
<a href="/actors" class="nav-link active">Actors</a>
|
||||
<a href="/plugin/distribution" class="nav-link">Distribution</a>
|
||||
<a href="/plugin/datastore" class="nav-link">Datastore</a>
|
||||
</nav>
|
||||
</div>
|
||||
<div class="header-right">
|
||||
<span id="replaySpeed" style="color:#2196f3;font-size:12px;display:none;"></span>
|
||||
<span id="replayPct" style="color:#888;font-size:12px;display:none;"></span>
|
||||
<span id="uptimeLabel" style="color:#888;font-size:12px;"></span>
|
||||
</div>
|
||||
</div>
|
||||
<div id="progressBarWrap" class="progress-bar-wrap">
|
||||
<div id="progressFill" class="progress-fill"></div>
|
||||
</div>
|
||||
|
||||
<div class="grid">
|
||||
<!-- Stat cards -->
|
||||
<div class="panel full-width">
|
||||
<h2>Actor Stats</h2>
|
||||
<div class="stats-cards">
|
||||
<div class="stat-card"><div class="value" id="statTotal">0</div><div class="label">Total Actors</div></div>
|
||||
<div class="stat-card"><div class="value" id="statAvgMbox">0</div><div class="label">Avg Mailbox</div></div>
|
||||
<div class="stat-card"><div class="value" id="statMaxMbox">0</div><div class="label">Max Mailbox</div></div>
|
||||
<div class="stat-card"><div class="value" id="statActiveWorkers">0</div><div class="label">Active Workers</div></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Mailbox depth distribution -->
|
||||
<div class="panel">
|
||||
<h2>Mailbox Depth Distribution</h2>
|
||||
<canvas id="depthChart"></canvas>
|
||||
</div>
|
||||
|
||||
<!-- Actors per worker -->
|
||||
<div class="panel">
|
||||
<h2>Actors per Worker</h2>
|
||||
<canvas id="workerChart"></canvas>
|
||||
</div>
|
||||
|
||||
<!-- Actor table -->
|
||||
<div class="panel full-width">
|
||||
<h2>All Actors <span id="actorCount" style="color:#555;font-weight:400;"></span></h2>
|
||||
<div class="search-wrap">
|
||||
<input type="text" id="actorSearch" class="search-input" placeholder="Filter by address, type, or worker..." />
|
||||
<select id="workerFilter" class="search-input" style="width:120px;">
|
||||
<option value="">All Workers</option>
|
||||
</select>
|
||||
<select id="statusFilter" class="search-input" style="width:120px;">
|
||||
<option value="">All Status</option>
|
||||
<option value="healthy">Healthy</option>
|
||||
<option value="poisoned">Poisoned</option>
|
||||
</select>
|
||||
<input type="number" id="minDepth" class="search-input" style="width:100px;" placeholder="Min depth" min="0" />
|
||||
<span id="searchInfo" class="search-info"></span>
|
||||
</div>
|
||||
<div class="actor-list-wrap">
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th class="sortable" data-sort="address">Address <span id="sortArrowAddress" class="sort-arrow"></span></th>
|
||||
<th class="sortable" data-sort="worker">Worker <span id="sortArrowWorker" class="sort-arrow"></span></th>
|
||||
<th class="sortable" data-sort="mailbox">Mailbox <span id="sortArrowMailbox" class="sort-arrow"></span></th>
|
||||
<th class="sortable" data-sort="msgs">Msgs <span id="sortArrowMsgs" class="sort-arrow"></span></th>
|
||||
<th>Last Msg</th>
|
||||
<th>Depth</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="actorTableBody"></tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
</div>
|
||||
|
||||
<!-- Actor detail overlay (fixed position, doesn't affect grid layout) -->
|
||||
<div id="detailPanel" class="detail-panel">
|
||||
<div class="detail-header">
|
||||
<h2 style="font-size:12px;color:#888;text-transform:uppercase;letter-spacing:1px;">Actor <span id="detailAddr" style="color:#aaa;font-weight:400;"></span></h2>
|
||||
<button class="detail-close" id="detailClose">×</button>
|
||||
</div>
|
||||
<div class="detail-grid">
|
||||
<div class="detail-item"><div class="d-label">Address</div><div class="d-value" id="detailFullAddr" style="font-size:10px;"></div></div>
|
||||
<div class="detail-item"><div class="d-label">Worker</div><div class="d-value" id="detailWorker"></div></div>
|
||||
<div class="detail-item"><div class="d-label">Mailbox</div><div class="d-value" id="detailMailbox"></div></div>
|
||||
<div class="detail-item"><div class="d-label">Messages</div><div class="d-value" id="detailMsgCount"></div></div>
|
||||
<div class="detail-item"><div class="d-label">Last Type</div><div class="d-value" id="detailLastMsg"></div></div>
|
||||
<div class="detail-item"><div class="d-label">Status</div><div class="d-value" id="detailStatus"></div></div>
|
||||
</div>
|
||||
<div style="font-size:10px;color:#888;text-transform:uppercase;letter-spacing:1px;margin-bottom:4px;">Mailbox History</div>
|
||||
<canvas id="sparkline" class="sparkline"></canvas>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
(function() {
|
||||
var DASHBOARD_MODE = '__DASHBOARD_MODE__';
|
||||
var isReplay = (DASHBOARD_MODE === 'replay');
|
||||
var lastUptimeMs = null;
|
||||
var lastStatsTime = null;
|
||||
|
||||
var dot = document.getElementById('statusDot');
|
||||
var uptimeLabel = document.getElementById('uptimeLabel');
|
||||
var replayBadge = document.getElementById('replayBadge');
|
||||
var replaySpeed = document.getElementById('replaySpeed');
|
||||
var replayPct = document.getElementById('replayPct');
|
||||
var progressBarWrap = document.getElementById('progressBarWrap');
|
||||
var progressFill = document.getElementById('progressFill');
|
||||
|
||||
if (isReplay) {
|
||||
replayBadge.className = 'replay-badge visible';
|
||||
progressBarWrap.className = 'progress-bar-wrap visible';
|
||||
dot.className = 'status-dot replaying';
|
||||
uptimeLabel.style.display = 'none';
|
||||
}
|
||||
|
||||
function updateUptime() {
|
||||
if (isReplay) return;
|
||||
var up = lastUptimeMs;
|
||||
if (up !== null && lastStatsTime !== null) {
|
||||
up += (Date.now() - lastStatsTime);
|
||||
}
|
||||
if (up === null) { uptimeLabel.textContent = ''; return; }
|
||||
var s = Math.floor(up / 1000);
|
||||
var d = Math.floor(s / 86400);
|
||||
var h = Math.floor((s % 86400) / 3600);
|
||||
var m = Math.floor((s % 3600) / 60);
|
||||
var sec = s % 60;
|
||||
var parts = [];
|
||||
if (d > 0) parts.push(d + 'd');
|
||||
if (h > 0 || d > 0) parts.push(h + 'h');
|
||||
parts.push(m + 'm');
|
||||
parts.push(sec + 's');
|
||||
uptimeLabel.textContent = parts.join(' ');
|
||||
}
|
||||
setInterval(updateUptime, 1000);
|
||||
|
||||
// ── State ──────────────────────────────────────────────
|
||||
var currentActors = [];
|
||||
var sortCol = 'worker';
|
||||
var sortAsc = true;
|
||||
var searchTimer = null;
|
||||
var focusedAddrHex = null;
|
||||
var depthHistory = {};
|
||||
var HISTORY_MAX = 120;
|
||||
|
||||
var colors = ['#4caf50','#2196f3','#ff9800','#f44336','#9c27b0','#00bcd4','#ffeb3b','#e91e63'];
|
||||
|
||||
// ── Helpers ────────────────────────────────────────────
|
||||
function addrToHex(addr) {
|
||||
var bytes = Array.isArray(addr) ? addr : Object.values(addr);
|
||||
var hex = '';
|
||||
for (var j = 0; j < Math.min(8, bytes.length); j++) {
|
||||
hex += ('0' + bytes[j].toString(16)).slice(-2);
|
||||
}
|
||||
return hex + '\u2026';
|
||||
}
|
||||
|
||||
function addrToFullHex(addr) {
|
||||
var bytes = Array.isArray(addr) ? addr : Object.values(addr);
|
||||
var hex = '';
|
||||
for (var j = 0; j < bytes.length; j++) {
|
||||
hex += ('0' + bytes[j].toString(16)).slice(-2);
|
||||
}
|
||||
return hex;
|
||||
}
|
||||
|
||||
function shortTypeName(full) {
|
||||
if (!full) return '';
|
||||
var parts = full.split('::');
|
||||
return parts[parts.length - 1];
|
||||
}
|
||||
|
||||
function escapeHtml(s) {
|
||||
if (!s) return '';
|
||||
return s.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
|
||||
}
|
||||
|
||||
function depthColor(d) {
|
||||
if (d === 0) return '#4caf50';
|
||||
if (d <= 2) return '#8bc34a';
|
||||
if (d <= 5) return '#cddc39';
|
||||
if (d <= 10) return '#ff9800';
|
||||
if (d <= 20) return '#ff5722';
|
||||
return '#f44336';
|
||||
}
|
||||
|
||||
// ── Stat cards ─────────────────────────────────────────
|
||||
function updateStatCards(actors) {
|
||||
var total = actors.length;
|
||||
var sum = 0, max = 0;
|
||||
var workerSet = {};
|
||||
for (var i = 0; i < actors.length; i++) {
|
||||
sum += actors[i].mailbox_depth;
|
||||
if (actors[i].mailbox_depth > max) max = actors[i].mailbox_depth;
|
||||
workerSet[actors[i].worker_id] = true;
|
||||
}
|
||||
var avg = total > 0 ? (sum / total).toFixed(1) : '0';
|
||||
document.getElementById('statTotal').textContent = total;
|
||||
document.getElementById('statAvgMbox').textContent = avg;
|
||||
document.getElementById('statMaxMbox').textContent = max;
|
||||
document.getElementById('statActiveWorkers').textContent = Object.keys(workerSet).length;
|
||||
}
|
||||
|
||||
// ── Mailbox depth distribution chart ───────────────────
|
||||
var depthCanvas = document.getElementById('depthChart');
|
||||
var depthCtx = depthCanvas.getContext('2d');
|
||||
|
||||
var buckets = [
|
||||
{label: '0', min: 0, max: 0},
|
||||
{label: '1-2', min: 1, max: 2},
|
||||
{label: '3-5', min: 3, max: 5},
|
||||
{label: '6-10', min: 6, max: 10},
|
||||
{label: '11-20', min: 11, max: 20},
|
||||
{label: '21-50', min: 21, max: 50},
|
||||
{label: '50+', min: 51, max: Infinity}
|
||||
];
|
||||
|
||||
var bucketColors = ['#4caf50','#8bc34a','#cddc39','#ff9800','#ff5722','#f44336','#d32f2f'];
|
||||
|
||||
function drawDepthChart(actors) {
|
||||
var dpr = window.devicePixelRatio || 1;
|
||||
var rect = depthCanvas.getBoundingClientRect();
|
||||
depthCanvas.width = rect.width * dpr;
|
||||
depthCanvas.height = rect.height * dpr;
|
||||
depthCtx.scale(dpr, dpr);
|
||||
var W = rect.width, H = rect.height;
|
||||
depthCtx.clearRect(0, 0, W, H);
|
||||
|
||||
var counts = buckets.map(function() { return 0; });
|
||||
for (var i = 0; i < actors.length; i++) {
|
||||
var d = actors[i].mailbox_depth;
|
||||
for (var b = 0; b < buckets.length; b++) {
|
||||
if (d >= buckets[b].min && d <= buckets[b].max) {
|
||||
counts[b]++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var maxCount = Math.max(1, Math.max.apply(null, counts));
|
||||
var barW = Math.max(12, Math.floor((W - 40) / buckets.length) - 8);
|
||||
var topPad = 20;
|
||||
var chartH = H - 35;
|
||||
|
||||
for (var i = 0; i < buckets.length; i++) {
|
||||
var x = 20 + i * (barW + 8);
|
||||
var h = (counts[i] / maxCount) * (chartH - topPad);
|
||||
depthCtx.fillStyle = bucketColors[i];
|
||||
depthCtx.globalAlpha = 0.85;
|
||||
depthCtx.fillRect(x, chartH - h, barW, h);
|
||||
|
||||
if (counts[i] > 0) {
|
||||
depthCtx.globalAlpha = 1;
|
||||
depthCtx.fillStyle = '#e0e0e0';
|
||||
depthCtx.font = '10px monospace';
|
||||
depthCtx.textAlign = 'center';
|
||||
depthCtx.fillText(counts[i], x + barW / 2, chartH - h - 4);
|
||||
}
|
||||
|
||||
depthCtx.globalAlpha = 1;
|
||||
depthCtx.fillStyle = '#888';
|
||||
depthCtx.font = '10px monospace';
|
||||
depthCtx.textAlign = 'center';
|
||||
depthCtx.fillText(buckets[i].label, x + barW / 2, H - 4);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Actors per worker chart ────────────────────────────
|
||||
var workerCanvas = document.getElementById('workerChart');
|
||||
var workerCtx = workerCanvas.getContext('2d');
|
||||
|
||||
function drawWorkerChart(actors) {
|
||||
var dpr = window.devicePixelRatio || 1;
|
||||
var rect = workerCanvas.getBoundingClientRect();
|
||||
workerCanvas.width = rect.width * dpr;
|
||||
workerCanvas.height = rect.height * dpr;
|
||||
workerCtx.scale(dpr, dpr);
|
||||
var W = rect.width, H = rect.height;
|
||||
workerCtx.clearRect(0, 0, W, H);
|
||||
|
||||
var workerCounts = {};
|
||||
for (var i = 0; i < actors.length; i++) {
|
||||
var wid = actors[i].worker_id;
|
||||
workerCounts[wid] = (workerCounts[wid] || 0) + 1;
|
||||
}
|
||||
var entries = Object.keys(workerCounts).sort(function(a, b) { return +a - +b; })
|
||||
.map(function(wid) { return {id: +wid, count: workerCounts[wid]}; });
|
||||
|
||||
if (entries.length === 0) return;
|
||||
|
||||
var maxCount = Math.max(1, Math.max.apply(null, entries.map(function(e) { return e.count; })));
|
||||
var barW = Math.max(12, Math.floor((W - 40) / entries.length) - 8);
|
||||
var topPad = 20;
|
||||
var chartH = H - 35;
|
||||
|
||||
for (var i = 0; i < entries.length; i++) {
|
||||
var x = 20 + i * (barW + 8);
|
||||
var h = (entries[i].count / maxCount) * (chartH - topPad);
|
||||
workerCtx.fillStyle = colors[entries[i].id % colors.length];
|
||||
workerCtx.globalAlpha = 0.85;
|
||||
workerCtx.fillRect(x, chartH - h, barW, h);
|
||||
|
||||
workerCtx.globalAlpha = 1;
|
||||
workerCtx.fillStyle = '#e0e0e0';
|
||||
workerCtx.font = '10px monospace';
|
||||
workerCtx.textAlign = 'center';
|
||||
workerCtx.fillText(entries[i].count, x + barW / 2, chartH - h - 4);
|
||||
|
||||
workerCtx.fillStyle = '#888';
|
||||
workerCtx.fillText('W' + entries[i].id, x + barW / 2, H - 4);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Actor table ────────────────────────────────────────
|
||||
var MAX_TABLE_ROWS = 2000;
|
||||
|
||||
function renderActorTable() {
|
||||
var filter = document.getElementById('actorSearch').value.toLowerCase();
|
||||
var workerFilter = document.getElementById('workerFilter').value;
|
||||
var statusFilter = document.getElementById('statusFilter').value;
|
||||
var minDepthVal = document.getElementById('minDepth').value;
|
||||
var minDepth = minDepthVal ? parseInt(minDepthVal, 10) : 0;
|
||||
|
||||
var filtered = currentActors.filter(function(a) {
|
||||
// Text search
|
||||
if (filter) {
|
||||
var hex = addrToHex(a.address).toLowerCase();
|
||||
var msgType = (a.last_msg_type || '').toLowerCase();
|
||||
var actorName = (a.name || '').toLowerCase();
|
||||
if (hex.indexOf(filter) < 0 && ('w' + a.worker_id).indexOf(filter) < 0 && msgType.indexOf(filter) < 0 && actorName.indexOf(filter) < 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// Worker filter
|
||||
if (workerFilter && a.worker_id !== parseInt(workerFilter, 10)) return false;
|
||||
// Status filter
|
||||
if (statusFilter === 'healthy' && a.poisoned) return false;
|
||||
if (statusFilter === 'poisoned' && !a.poisoned) return false;
|
||||
// Min depth
|
||||
if (minDepth > 0 && a.mailbox_depth < minDepth) return false;
|
||||
return true;
|
||||
});
|
||||
|
||||
// Sort
|
||||
filtered.sort(function(a, b) {
|
||||
var va, vb;
|
||||
if (sortCol === 'address') {
|
||||
va = addrToHex(a.address);
|
||||
vb = addrToHex(b.address);
|
||||
return sortAsc ? va.localeCompare(vb) : vb.localeCompare(va);
|
||||
} else if (sortCol === 'worker') {
|
||||
va = a.worker_id; vb = b.worker_id;
|
||||
} else if (sortCol === 'msgs') {
|
||||
va = a.messages_processed || 0; vb = b.messages_processed || 0;
|
||||
} else {
|
||||
va = a.mailbox_depth; vb = b.mailbox_depth;
|
||||
}
|
||||
return sortAsc ? va - vb : vb - va;
|
||||
});
|
||||
|
||||
// Update sort arrows
|
||||
['address', 'worker', 'mailbox', 'msgs'].forEach(function(col) {
|
||||
var key = col.charAt(0).toUpperCase() + col.slice(1);
|
||||
var el = document.getElementById('sortArrow' + key);
|
||||
if (col === sortCol) {
|
||||
el.textContent = sortAsc ? '\u25B2' : '\u25BC';
|
||||
} else {
|
||||
el.textContent = '';
|
||||
}
|
||||
});
|
||||
|
||||
var maxDepth = 1;
|
||||
for (var i = 0; i < currentActors.length; i++) {
|
||||
if (currentActors[i].mailbox_depth > maxDepth) maxDepth = currentActors[i].mailbox_depth;
|
||||
}
|
||||
|
||||
var tbody = document.getElementById('actorTableBody');
|
||||
tbody.innerHTML = '';
|
||||
var count = Math.min(filtered.length, MAX_TABLE_ROWS);
|
||||
for (var i = 0; i < count; i++) {
|
||||
var a = filtered[i];
|
||||
var hex = addrToHex(a.address);
|
||||
var fullHex = addrToFullHex(a.address);
|
||||
var pct = Math.round((a.mailbox_depth / maxDepth) * 120);
|
||||
var bc = depthColor(a.mailbox_depth);
|
||||
var hasMsg = !!a.last_msg_type;
|
||||
var msgShort = hasMsg ? shortTypeName(a.last_msg_type) : '\u2014';
|
||||
var msgClass = hasMsg ? 'msg-type' : 'msg-type none';
|
||||
var msgTitle = hasMsg ? ' title="' + escapeHtml(a.last_msg_type) + '"' : '';
|
||||
var tr = document.createElement('tr');
|
||||
tr.className = 'clickable' + (fullHex === focusedAddrHex ? ' focused' : '');
|
||||
if (a.poisoned) tr.style.opacity = '0.6';
|
||||
tr.setAttribute('data-addr', fullHex);
|
||||
var addrCell = a.name
|
||||
? '<td><span style="color:#00bcd4;font-weight:600;">' + escapeHtml(a.name) + '</span> <span style="color:#555;font-size:10px;">' + escapeHtml(hex) + '</span>' + (a.poisoned ? ' <span style="color:#f44336;font-size:9px;">DEAD</span>' : '') + '</td>'
|
||||
: '<td style="color:#aaa;font-size:11px;">' + escapeHtml(hex) + (a.poisoned ? ' <span style="color:#f44336;font-size:9px;">DEAD</span>' : '') + '</td>';
|
||||
tr.innerHTML =
|
||||
addrCell +
|
||||
'<td>W' + a.worker_id + '</td>' +
|
||||
'<td>' + a.mailbox_depth + '</td>' +
|
||||
'<td>' + (a.messages_processed || 0).toLocaleString() + '</td>' +
|
||||
'<td class="' + msgClass + '"' + msgTitle + '>' + escapeHtml(msgShort) + '</td>' +
|
||||
'<td><div class="depth-bar" style="width:' + pct + 'px;background:' + bc + ';"></div></td>';
|
||||
tr.addEventListener('click', (function(fh) { return function() { focusActor(fh); }; })(fullHex));
|
||||
tbody.appendChild(tr);
|
||||
}
|
||||
|
||||
if (filtered.length > MAX_TABLE_ROWS) {
|
||||
var tr2 = document.createElement('tr');
|
||||
tr2.innerHTML = '<td colspan="6" style="color:#555;">... and ' + (filtered.length - MAX_TABLE_ROWS) + ' more</td>';
|
||||
tbody.appendChild(tr2);
|
||||
}
|
||||
|
||||
var info = '(' + filtered.length + (filtered.length !== currentActors.length ? ' of ' + currentActors.length : '') + ')';
|
||||
document.getElementById('actorCount').textContent = info;
|
||||
}
|
||||
|
||||
// ── Focus / detail panel ───────────────────────────────
|
||||
function focusActor(fullHex) {
|
||||
focusedAddrHex = fullHex;
|
||||
document.getElementById('detailPanel').className = 'detail-panel visible';
|
||||
updateDetailPanel();
|
||||
renderActorTable();
|
||||
}
|
||||
|
||||
function clearFocus() {
|
||||
focusedAddrHex = null;
|
||||
document.getElementById('detailPanel').className = 'detail-panel';
|
||||
renderActorTable();
|
||||
}
|
||||
|
||||
document.getElementById('detailClose').addEventListener('click', clearFocus);
|
||||
|
||||
function updateDetailPanel() {
|
||||
if (!focusedAddrHex) return;
|
||||
var actor = null;
|
||||
for (var i = 0; i < currentActors.length; i++) {
|
||||
if (addrToFullHex(currentActors[i].address) === focusedAddrHex) {
|
||||
actor = currentActors[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!actor) {
|
||||
document.getElementById('detailAddr').textContent = focusedAddrHex.substring(0, 16) + '\u2026 (gone)';
|
||||
return;
|
||||
}
|
||||
|
||||
var detailAddrText = actor.name
|
||||
? '<a href="/actor/' + focusedAddrHex + '" style="color:#00bcd4;text-decoration:none;font-weight:600;">' + escapeHtml(actor.name) + '</a>'
|
||||
: '<a href="/actor/' + focusedAddrHex + '" style="color:#aaa;text-decoration:none;">' + escapeHtml(addrToHex(actor.address)) + '</a>';
|
||||
document.getElementById('detailAddr').innerHTML = detailAddrText;
|
||||
var detailFullText = actor.name
|
||||
? '<span style="color:#00bcd4;font-weight:600;">' + escapeHtml(actor.name) + '</span> <span style="color:#555;font-size:9px;">' + escapeHtml(focusedAddrHex) + '</span>'
|
||||
: '<a href="/actor/' + focusedAddrHex + '" style="color:#fff;text-decoration:none;">' + escapeHtml(focusedAddrHex) + '</a>';
|
||||
document.getElementById('detailFullAddr').innerHTML = detailFullText;
|
||||
document.getElementById('detailWorker').textContent = 'W' + actor.worker_id;
|
||||
document.getElementById('detailMailbox').textContent = actor.mailbox_depth;
|
||||
document.getElementById('detailMsgCount').textContent = (actor.messages_processed || 0).toLocaleString();
|
||||
|
||||
var hasMsg = !!actor.last_msg_type;
|
||||
document.getElementById('detailLastMsg').innerHTML = hasMsg
|
||||
? '<span class="msg-type" title="' + escapeHtml(actor.last_msg_type) + '">' + escapeHtml(shortTypeName(actor.last_msg_type)) + '</span>'
|
||||
: '<span class="msg-type none">\u2014</span>';
|
||||
|
||||
var isPoisoned = !!actor.poisoned;
|
||||
document.getElementById('detailStatus').innerHTML = isPoisoned
|
||||
? '<span class="poisoned-badge">POISONED</span>'
|
||||
: '<span class="healthy-badge">Healthy</span>';
|
||||
|
||||
drawSparkline();
|
||||
}
|
||||
|
||||
// ── Sparkline ──────────────────────────────────────────
|
||||
function drawSparkline() {
|
||||
var canvas = document.getElementById('sparkline');
|
||||
var ctx = canvas.getContext('2d');
|
||||
var dpr = window.devicePixelRatio || 1;
|
||||
var rect = canvas.getBoundingClientRect();
|
||||
canvas.width = rect.width * dpr;
|
||||
canvas.height = rect.height * dpr;
|
||||
ctx.scale(dpr, dpr);
|
||||
var W = rect.width, H = rect.height;
|
||||
ctx.clearRect(0, 0, W, H);
|
||||
|
||||
var hist = depthHistory[focusedAddrHex];
|
||||
if (!hist || hist.length < 2) {
|
||||
ctx.fillStyle = '#555';
|
||||
ctx.font = '11px monospace';
|
||||
ctx.textAlign = 'center';
|
||||
ctx.fillText('Collecting data\u2026', W / 2, H / 2);
|
||||
return;
|
||||
}
|
||||
|
||||
var max = Math.max(1, Math.max.apply(null, hist));
|
||||
var padY = 6, padX = 4;
|
||||
var drawW = W - padX * 2;
|
||||
var drawH = H - padY * 2;
|
||||
|
||||
// Fill area
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(padX, H - padY);
|
||||
for (var i = 0; i < hist.length; i++) {
|
||||
var x = padX + (i / (hist.length - 1)) * drawW;
|
||||
var y = (H - padY) - (hist[i] / max) * drawH;
|
||||
ctx.lineTo(x, y);
|
||||
}
|
||||
ctx.lineTo(padX + drawW, H - padY);
|
||||
ctx.closePath();
|
||||
ctx.fillStyle = 'rgba(99, 102, 241, 0.15)';
|
||||
ctx.fill();
|
||||
|
||||
// Line
|
||||
ctx.beginPath();
|
||||
for (var i = 0; i < hist.length; i++) {
|
||||
var x = padX + (i / (hist.length - 1)) * drawW;
|
||||
var y = (H - padY) - (hist[i] / max) * drawH;
|
||||
if (i === 0) ctx.moveTo(x, y);
|
||||
else ctx.lineTo(x, y);
|
||||
}
|
||||
ctx.strokeStyle = '#6366f1';
|
||||
ctx.lineWidth = 1.5;
|
||||
ctx.stroke();
|
||||
|
||||
// Label
|
||||
var last = hist[hist.length - 1];
|
||||
ctx.fillStyle = '#e0e0e0';
|
||||
ctx.font = '10px monospace';
|
||||
ctx.textAlign = 'right';
|
||||
ctx.fillText('depth: ' + last + ' max: ' + max, W - padX, padY + 8);
|
||||
}
|
||||
|
||||
// ── Sort click handlers ────────────────────────────────
|
||||
var sortHeaders = document.querySelectorAll('.sortable');
|
||||
for (var i = 0; i < sortHeaders.length; i++) {
|
||||
sortHeaders[i].addEventListener('click', function() {
|
||||
var col = this.getAttribute('data-sort');
|
||||
if (sortCol === col) { sortAsc = !sortAsc; }
|
||||
else { sortCol = col; sortAsc = true; }
|
||||
renderActorTable();
|
||||
});
|
||||
}
|
||||
|
||||
// ── Search/filter handlers ─────────────────────────────
|
||||
document.getElementById('actorSearch').addEventListener('keyup', function() {
|
||||
clearTimeout(searchTimer);
|
||||
searchTimer = setTimeout(renderActorTable, 150);
|
||||
});
|
||||
document.getElementById('workerFilter').addEventListener('change', renderActorTable);
|
||||
document.getElementById('statusFilter').addEventListener('change', renderActorTable);
|
||||
document.getElementById('minDepth').addEventListener('input', function() {
|
||||
clearTimeout(searchTimer);
|
||||
searchTimer = setTimeout(renderActorTable, 150);
|
||||
});
|
||||
|
||||
// ── Status helpers ─────────────────────────────────────
|
||||
function setStatus(s) {
|
||||
if (s === 'done') {
|
||||
dot.className = 'status-dot done';
|
||||
if (isReplay) {
|
||||
replayPct.textContent = '100%';
|
||||
progressFill.style.width = '100%';
|
||||
}
|
||||
} else if (s === 'disconnected') {
|
||||
dot.className = 'status-dot disconnected';
|
||||
} else {
|
||||
dot.className = isReplay ? 'status-dot replaying' : 'status-dot';
|
||||
}
|
||||
}
|
||||
|
||||
// ── SSE connection ─────────────────────────────────────
|
||||
var es = new EventSource('/events');
|
||||
window.addEventListener('beforeunload', function() { es.close(); });
|
||||
|
||||
es.addEventListener('stats', function(e) {
|
||||
try {
|
||||
var data = JSON.parse(e.data);
|
||||
if (typeof data.uptime_ms === 'number') {
|
||||
lastUptimeMs = data.uptime_ms;
|
||||
lastStatsTime = Date.now();
|
||||
}
|
||||
currentActors = data.actor_details || [];
|
||||
updateStatCards(currentActors);
|
||||
drawDepthChart(currentActors);
|
||||
drawWorkerChart(currentActors);
|
||||
|
||||
// Accumulate per-actor depth history
|
||||
var liveAddrs = {};
|
||||
for (var i = 0; i < currentActors.length; i++) {
|
||||
var fh = addrToFullHex(currentActors[i].address);
|
||||
liveAddrs[fh] = true;
|
||||
if (!depthHistory[fh]) depthHistory[fh] = [];
|
||||
depthHistory[fh].push(currentActors[i].mailbox_depth);
|
||||
if (depthHistory[fh].length > HISTORY_MAX) depthHistory[fh].shift();
|
||||
}
|
||||
// Clean up stale entries for removed actors
|
||||
for (var key in depthHistory) {
|
||||
if (!liveAddrs[key]) delete depthHistory[key];
|
||||
}
|
||||
|
||||
// Update worker filter dropdown
|
||||
var wSelect = document.getElementById('workerFilter');
|
||||
var curVal = wSelect.value;
|
||||
var workerIds = {};
|
||||
for (var i = 0; i < currentActors.length; i++) workerIds[currentActors[i].worker_id] = true;
|
||||
var wids = Object.keys(workerIds).sort(function(a,b) { return +a - +b; });
|
||||
wSelect.innerHTML = '<option value="">All Workers</option>';
|
||||
wids.forEach(function(wid) {
|
||||
var opt = document.createElement('option');
|
||||
opt.value = wid;
|
||||
opt.textContent = 'W' + wid;
|
||||
wSelect.appendChild(opt);
|
||||
});
|
||||
wSelect.value = curVal;
|
||||
|
||||
renderActorTable();
|
||||
if (focusedAddrHex) updateDetailPanel();
|
||||
} catch(err) { console.error('stats parse error', err); }
|
||||
});
|
||||
|
||||
es.addEventListener('replay_meta', function(e) {
|
||||
try {
|
||||
var meta = JSON.parse(e.data);
|
||||
replaySpeed.textContent = meta.speed + 'x';
|
||||
replaySpeed.style.display = 'inline';
|
||||
replayPct.style.display = 'inline';
|
||||
replayPct.textContent = '0%';
|
||||
} catch(err) { console.error('replay_meta parse error', err); }
|
||||
});
|
||||
|
||||
es.addEventListener('replay_progress', function(e) {
|
||||
try {
|
||||
var data = JSON.parse(e.data);
|
||||
var pct = Math.round(data.progress * 100);
|
||||
replayPct.textContent = pct + '%';
|
||||
progressFill.style.width = pct + '%';
|
||||
} catch(err) { console.error('replay_progress parse error', err); }
|
||||
});
|
||||
|
||||
es.addEventListener('done', function() {
|
||||
setStatus('done');
|
||||
es.close();
|
||||
});
|
||||
|
||||
es.onerror = function() {
|
||||
setStatus('disconnected');
|
||||
};
|
||||
|
||||
es.onopen = function() {
|
||||
setStatus('connected');
|
||||
};
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
"##;
|
||||
|
|
@ -16,9 +16,6 @@
|
|||
//! ```
|
||||
|
||||
pub mod builtins;
|
||||
mod parse;
|
||||
|
||||
pub use parse::{from_query_params, parse_line};
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
|
@ -246,3 +243,78 @@ impl CommandRouter {
|
|||
names
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Input Parsers ──────────────────────────────────────────────────────────
|
||||
|
||||
/// Parse a REPL text line into a [`CommandRequest`].
|
||||
///
|
||||
/// Handles `--flag value` pairs and maps positional arguments to
|
||||
/// command-specific named parameters.
|
||||
pub fn parse_line(line: &str) -> CommandRequest {
|
||||
let parts: Vec<&str> = line.split_whitespace().collect();
|
||||
if parts.is_empty() {
|
||||
return CommandRequest {
|
||||
command: "help".to_string(),
|
||||
args: HashMap::new(),
|
||||
};
|
||||
}
|
||||
let command = parts[0].to_string();
|
||||
let rest = &parts[1..];
|
||||
|
||||
let mut args = HashMap::new();
|
||||
let mut i = 0;
|
||||
let mut positional = 0;
|
||||
|
||||
while i < rest.len() {
|
||||
if let Some(key) = rest[i].strip_prefix("--") {
|
||||
if i + 1 < rest.len() && !rest[i + 1].starts_with("--") {
|
||||
args.insert(
|
||||
key.to_string(),
|
||||
serde_json::Value::String(rest[i + 1].to_string()),
|
||||
);
|
||||
i += 2;
|
||||
} else {
|
||||
args.insert(key.to_string(), serde_json::Value::Bool(true));
|
||||
i += 1;
|
||||
}
|
||||
} else {
|
||||
let name = positional_arg_name(&command, positional);
|
||||
if !name.is_empty() {
|
||||
args.insert(
|
||||
name.to_string(),
|
||||
serde_json::Value::String(rest[i].to_string()),
|
||||
);
|
||||
}
|
||||
positional += 1;
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
CommandRequest { command, args }
|
||||
}
|
||||
|
||||
/// Convert HTTP query parameters to a [`CommandRequest`].
|
||||
pub fn from_query_params(params: &HashMap<String, String>) -> CommandRequest {
|
||||
let command = params
|
||||
.get("cmd")
|
||||
.cloned()
|
||||
.unwrap_or_else(|| "help".into());
|
||||
let args: HashMap<String, serde_json::Value> = params
|
||||
.iter()
|
||||
.filter(|(k, _)| *k != "cmd")
|
||||
.map(|(k, v)| (k.clone(), serde_json::Value::String(v.clone())))
|
||||
.collect();
|
||||
CommandRequest { command, args }
|
||||
}
|
||||
|
||||
/// Map positional argument index to the named parameter for each command.
|
||||
fn positional_arg_name(command: &str, position: usize) -> &'static str {
|
||||
match (command, position) {
|
||||
("worker", 0) => "id",
|
||||
("actor", 0) => "prefix",
|
||||
("hot", 0) => "n",
|
||||
("phases", 0) => "worker",
|
||||
("diff", 0) => "seconds",
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,90 +0,0 @@
|
|||
//! Input parsers for REPL lines and HTTP query parameters.
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use super::CommandRequest;
|
||||
|
||||
/// Parse a REPL text line into a [`CommandRequest`].
|
||||
///
|
||||
/// Handles `--flag value` pairs and maps positional arguments to
|
||||
/// command-specific named parameters.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```text
|
||||
/// "overview" → { command: "overview", args: {} }
|
||||
/// "worker 3" → { command: "worker", args: { "id": "3" } }
|
||||
/// "actors --sort mailbox" → { command: "actors", args: { "sort": "mailbox" } }
|
||||
/// "hot 5" → { command: "hot", args: { "n": "5" } }
|
||||
/// ```
|
||||
pub fn parse_line(line: &str) -> CommandRequest {
|
||||
let parts: Vec<&str> = line.split_whitespace().collect();
|
||||
if parts.is_empty() {
|
||||
return CommandRequest {
|
||||
command: "help".to_string(),
|
||||
args: HashMap::new(),
|
||||
};
|
||||
}
|
||||
let command = parts[0].to_string();
|
||||
let rest = &parts[1..];
|
||||
|
||||
let mut args = HashMap::new();
|
||||
let mut i = 0;
|
||||
let mut positional = 0;
|
||||
|
||||
while i < rest.len() {
|
||||
if let Some(key) = rest[i].strip_prefix("--") {
|
||||
if i + 1 < rest.len() && !rest[i + 1].starts_with("--") {
|
||||
args.insert(
|
||||
key.to_string(),
|
||||
serde_json::Value::String(rest[i + 1].to_string()),
|
||||
);
|
||||
i += 2;
|
||||
} else {
|
||||
args.insert(key.to_string(), serde_json::Value::Bool(true));
|
||||
i += 1;
|
||||
}
|
||||
} else {
|
||||
let name = positional_arg_name(&command, positional);
|
||||
if !name.is_empty() {
|
||||
args.insert(
|
||||
name.to_string(),
|
||||
serde_json::Value::String(rest[i].to_string()),
|
||||
);
|
||||
}
|
||||
positional += 1;
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
CommandRequest { command, args }
|
||||
}
|
||||
|
||||
/// Convert HTTP query parameters to a [`CommandRequest`].
|
||||
///
|
||||
/// The `cmd` parameter becomes the command name; all other parameters
|
||||
/// become string-valued arguments.
|
||||
pub fn from_query_params(params: &HashMap<String, String>) -> CommandRequest {
|
||||
let command = params
|
||||
.get("cmd")
|
||||
.cloned()
|
||||
.unwrap_or_else(|| "help".into());
|
||||
let args: HashMap<String, serde_json::Value> = params
|
||||
.iter()
|
||||
.filter(|(k, _)| *k != "cmd")
|
||||
.map(|(k, v)| (k.clone(), serde_json::Value::String(v.clone())))
|
||||
.collect();
|
||||
CommandRequest { command, args }
|
||||
}
|
||||
|
||||
/// Map positional argument index to the named parameter for each command.
|
||||
fn positional_arg_name(command: &str, position: usize) -> &'static str {
|
||||
match (command, position) {
|
||||
("worker", 0) => "id",
|
||||
("actor", 0) => "prefix",
|
||||
("hot", 0) => "n",
|
||||
("phases", 0) => "worker",
|
||||
("diff", 0) => "seconds",
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
|
|
@ -1,663 +0,0 @@
|
|||
pub const DASHBOARD_HTML: &str = r##"<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Swactor Runtime Dashboard</title>
|
||||
<style>
|
||||
* { margin: 0; padding: 0; box-sizing: border-box; }
|
||||
body { font-family: 'Menlo', 'Consolas', 'Monaco', monospace; background: #0f1117; color: #e0e0e0; font-size: 13px; }
|
||||
|
||||
.header {
|
||||
display: flex; align-items: center; justify-content: space-between;
|
||||
padding: 12px 20px; background: #161822; border-bottom: 1px solid #2a2d3e;
|
||||
}
|
||||
.header h1 { font-size: 16px; font-weight: 600; color: #fff; }
|
||||
.status-dot {
|
||||
width: 10px; height: 10px; border-radius: 50%; background: #4caf50;
|
||||
display: inline-block; margin-left: 8px; vertical-align: middle;
|
||||
}
|
||||
.status-dot.disconnected { background: #f44336; }
|
||||
.status-dot.done { background: #ff9800; }
|
||||
.status-dot.replaying { background: #2196f3; animation: pulse 1.5s infinite; }
|
||||
|
||||
@keyframes pulse {
|
||||
0%, 100% { opacity: 1; }
|
||||
50% { opacity: 0.4; }
|
||||
}
|
||||
|
||||
.replay-badge {
|
||||
display: none; background: #2196f3; color: #fff; font-size: 10px; font-weight: 700;
|
||||
padding: 2px 8px; border-radius: 3px; margin-left: 10px; letter-spacing: 1px;
|
||||
vertical-align: middle;
|
||||
}
|
||||
.replay-badge.visible { display: inline-block; }
|
||||
|
||||
.header-left { display: flex; align-items: center; }
|
||||
.nav-links { display: flex; gap: 4px; margin-left: 20px; }
|
||||
.nav-link {
|
||||
color: #888; text-decoration: none; font-size: 12px;
|
||||
padding: 4px 10px; border-radius: 3px; transition: color 0.2s;
|
||||
}
|
||||
.nav-link:hover { color: #e0e0e0; }
|
||||
.nav-link.active { color: #fff; background: #2a2d3e; }
|
||||
|
||||
.header-right { display: flex; align-items: center; gap: 12px; }
|
||||
|
||||
.progress-bar-wrap {
|
||||
display: none; width: 100%; height: 3px; background: #2a2d3e;
|
||||
}
|
||||
.progress-bar-wrap.visible { display: block; }
|
||||
.progress-fill {
|
||||
height: 100%; width: 0%; background: #2196f3; transition: width 0.3s;
|
||||
}
|
||||
|
||||
.grid {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr 1fr;
|
||||
grid-template-rows: auto auto;
|
||||
gap: 12px; padding: 12px;
|
||||
}
|
||||
|
||||
.panel {
|
||||
background: #161822; border: 1px solid #2a2d3e; border-radius: 6px;
|
||||
padding: 14px; overflow: hidden;
|
||||
}
|
||||
.panel h2 { font-size: 12px; color: #888; text-transform: uppercase; letter-spacing: 1px; margin-bottom: 10px; }
|
||||
|
||||
.stats-cards {
|
||||
display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px;
|
||||
}
|
||||
.stat-card {
|
||||
background: #1c1f2e; border-radius: 4px; padding: 10px; text-align: center;
|
||||
}
|
||||
.stat-card .value { font-size: 22px; font-weight: 700; color: #fff; }
|
||||
.stat-card .label { font-size: 10px; color: #888; text-transform: uppercase; margin-top: 2px; }
|
||||
|
||||
.chart-panel { grid-row: span 2; }
|
||||
canvas#workerChart { width: 100%; height: 200px; }
|
||||
|
||||
.worker-cards { display: flex; flex-direction: column; gap: 6px; }
|
||||
.worker-card {
|
||||
display: flex; align-items: center; gap: 10px;
|
||||
background: #1c1f2e; border-radius: 4px; padding: 6px 10px;
|
||||
}
|
||||
.worker-card .wc-id { font-weight: 700; min-width: 32px; }
|
||||
.worker-card .wc-bar-wrap { flex: 1; height: 14px; background: #0f1117; border-radius: 2px; overflow: hidden; display: flex; }
|
||||
.worker-card .wc-bar-seg { height: 100%; }
|
||||
.worker-card .wc-stats { font-size: 11px; color: #888; min-width: 200px; text-align: right; }
|
||||
.worker-card .wc-spark { display: inline-flex; gap: 4px; margin-left: 6px; }
|
||||
|
||||
.actor-table-wrap { max-height: 200px; overflow-y: auto; }
|
||||
.actor-table-wrap table { width: 100%; border-collapse: collapse; }
|
||||
.actor-table-wrap th, .actor-table-wrap td {
|
||||
padding: 4px 8px; text-align: left; border-bottom: 1px solid #2a2d3e; font-size: 12px;
|
||||
}
|
||||
.actor-table-wrap th { color: #888; font-weight: 500; position: sticky; top: 0; background: #161822; }
|
||||
|
||||
.log-panel {
|
||||
grid-column: 1 / -1;
|
||||
}
|
||||
.log-wrap { max-height: 300px; overflow-y: auto; }
|
||||
.log-wrap table { width: 100%; border-collapse: collapse; }
|
||||
.log-wrap th, .log-wrap td {
|
||||
padding: 3px 8px; text-align: left; border-bottom: 1px solid #1c1f2e; font-size: 11px;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.log-wrap th { color: #888; font-weight: 500; position: sticky; top: 0; background: #161822; }
|
||||
.log-wrap td.msg { white-space: normal; word-break: break-all; max-width: 400px; }
|
||||
|
||||
.level-ERROR { color: #f44336; font-weight: 700; }
|
||||
.level-WARN { color: #ff9800; }
|
||||
.level-INFO { color: #4caf50; }
|
||||
.level-DEBUG { color: #2196f3; }
|
||||
.level-TRACE { color: #666; }
|
||||
|
||||
.worker-detail-panel { grid-column: 1 / -1; }
|
||||
.worker-detail-scroll { max-height: 360px; overflow-y: auto; }
|
||||
.worker-group { margin-bottom: 8px; border: 1px solid #2a2d3e; border-radius: 4px; overflow: hidden; }
|
||||
.worker-group-header {
|
||||
display: flex; align-items: center; justify-content: space-between;
|
||||
padding: 7px 12px; background: #1c1f2e; cursor: pointer; user-select: none;
|
||||
}
|
||||
.worker-group-header:hover { background: #22253a; }
|
||||
.worker-group-header .wid { font-weight: 700; }
|
||||
.worker-group-header .summary { color: #888; font-size: 11px; }
|
||||
.worker-group-header .toggle { color: #555; font-size: 14px; }
|
||||
.worker-group-header .sparkline-wrap { display: inline-flex; gap: 8px; margin-left: 12px; }
|
||||
.worker-group-header .sparkline-wrap svg { vertical-align: middle; }
|
||||
.worker-group-body { display: none; }
|
||||
.worker-group.open .worker-group-body { display: block; }
|
||||
.worker-group-body table { width: 100%; border-collapse: collapse; }
|
||||
.worker-group-body th, .worker-group-body td {
|
||||
padding: 3px 10px; text-align: left; border-bottom: 1px solid #1c1f2e; font-size: 11px;
|
||||
}
|
||||
.worker-group-body th { color: #888; font-weight: 500; background: #161822; }
|
||||
.msg-type { color: #4caf50; }
|
||||
.msg-type.none { color: #555; font-style: italic; }
|
||||
|
||||
::-webkit-scrollbar { width: 6px; }
|
||||
::-webkit-scrollbar-track { background: #0f1117; }
|
||||
::-webkit-scrollbar-thumb { background: #2a2d3e; border-radius: 3px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="header">
|
||||
<div class="header-left">
|
||||
<h1>
|
||||
Swactor Runtime Dashboard
|
||||
<span id="statusDot" class="status-dot"></span>
|
||||
<span id="replayBadge" class="replay-badge">REPLAY</span>
|
||||
</h1>
|
||||
<nav class="nav-links">
|
||||
<a href="/" class="nav-link active">Overview</a>
|
||||
<a href="/actors" class="nav-link">Actors</a>
|
||||
<a href="/plugin/distribution" class="nav-link">Distribution</a>
|
||||
<a href="/plugin/datastore" class="nav-link">Datastore</a>
|
||||
</nav>
|
||||
</div>
|
||||
<div class="header-right">
|
||||
<span id="replaySpeed" style="color:#2196f3;font-size:12px;display:none;"></span>
|
||||
<span id="replayPct" style="color:#888;font-size:12px;display:none;"></span>
|
||||
<span id="uptimeLabel" style="color:#888;font-size:12px;"></span>
|
||||
</div>
|
||||
</div>
|
||||
<div id="progressBarWrap" class="progress-bar-wrap">
|
||||
<div id="progressFill" class="progress-fill"></div>
|
||||
</div>
|
||||
|
||||
<div id="warningBanner" style="display:none;padding:8px 20px;background:#1c1f2e;border-bottom:1px solid #2a2d3e;font-size:12px;"></div>
|
||||
<div class="grid">
|
||||
<div class="panel chart-panel">
|
||||
<h2>Worker Utilization</h2>
|
||||
<div id="workerCards" class="worker-cards"></div>
|
||||
<div style="margin-top:6px;font-size:10px;color:#555;">
|
||||
<span style="color:#4caf50;">\u25A0</span> processing
|
||||
<span style="color:#2196f3;">\u25A0</span> delivery
|
||||
<span style="color:#00bcd4;">\u25A0</span> spawns
|
||||
<span style="color:#f44336;">\u25A0</span> overhead
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="panel">
|
||||
<h2>Stats</h2>
|
||||
<div class="stats-cards">
|
||||
<div class="stat-card"><div class="value" id="statActors">0</div><div class="label">Actors</div></div>
|
||||
<div class="stat-card"><div class="value" id="statMessages">0</div><div class="label">Messages</div></div>
|
||||
<div class="stat-card"><div class="value" id="statWorkers">0</div><div class="label">Workers</div></div>
|
||||
<div class="stat-card"><div class="value" id="statMailbox">0</div><div class="label">Mailbox</div></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="panel">
|
||||
<h2>Actors</h2>
|
||||
<div class="actor-table-wrap">
|
||||
<table>
|
||||
<thead><tr><th>Address</th><th>Worker</th></tr></thead>
|
||||
<tbody id="actorTableBody"></tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="panel worker-detail-panel">
|
||||
<h2>Worker Details</h2>
|
||||
<div class="worker-detail-scroll" id="workerDetailContainer"></div>
|
||||
</div>
|
||||
|
||||
<div class="panel log-panel">
|
||||
<h2>Activity Log <span id="logCount" style="color:#555;font-weight:400;"></span></h2>
|
||||
<div class="log-wrap" id="logWrap">
|
||||
<table>
|
||||
<thead><tr><th>Seq</th><th>Time</th><th>Level</th><th>Worker</th><th>Message</th><th>Fields</th></tr></thead>
|
||||
<tbody id="logTableBody"></tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
(function() {
|
||||
var DASHBOARD_MODE = '__DASHBOARD_MODE__';
|
||||
var logRowCount = 0;
|
||||
var MAX_LOG_ROWS = 2000;
|
||||
var isReplay = (DASHBOARD_MODE === 'replay');
|
||||
var lastUptimeMs = null;
|
||||
var lastStatsTime = null;
|
||||
var workerHistory = {}; // { id: { message_rates: [], mailbox_depths: [] } }
|
||||
|
||||
var dot = document.getElementById('statusDot');
|
||||
var uptimeLabel = document.getElementById('uptimeLabel');
|
||||
var replayBadge = document.getElementById('replayBadge');
|
||||
var replaySpeed = document.getElementById('replaySpeed');
|
||||
var replayPct = document.getElementById('replayPct');
|
||||
var progressBarWrap = document.getElementById('progressBarWrap');
|
||||
var progressFill = document.getElementById('progressFill');
|
||||
|
||||
if (isReplay) {
|
||||
replayBadge.className = 'replay-badge visible';
|
||||
progressBarWrap.className = 'progress-bar-wrap visible';
|
||||
dot.className = 'status-dot replaying';
|
||||
uptimeLabel.style.display = 'none';
|
||||
}
|
||||
|
||||
function updateUptime() {
|
||||
if (isReplay) return;
|
||||
var up = lastUptimeMs;
|
||||
if (up !== null && lastStatsTime !== null) {
|
||||
up += (Date.now() - lastStatsTime);
|
||||
}
|
||||
if (up === null) { uptimeLabel.textContent = ''; return; }
|
||||
var s = Math.floor(up / 1000);
|
||||
var d = Math.floor(s / 86400);
|
||||
var h = Math.floor((s % 86400) / 3600);
|
||||
var m = Math.floor((s % 3600) / 60);
|
||||
var sec = s % 60;
|
||||
var parts = [];
|
||||
if (d > 0) parts.push(d + 'd');
|
||||
if (h > 0 || d > 0) parts.push(h + 'h');
|
||||
parts.push(m + 'm');
|
||||
parts.push(sec + 's');
|
||||
uptimeLabel.textContent = parts.join(' ');
|
||||
}
|
||||
setInterval(updateUptime, 1000);
|
||||
|
||||
var colors = ['#4caf50','#2196f3','#ff9800','#f44336','#9c27b0','#00bcd4','#ffeb3b','#e91e63'];
|
||||
|
||||
var phaseColors = ['#4caf50', '#2196f3', '#00bcd4', '#f44336'];
|
||||
// Group tick phases: processing=2, delivery=1+4, spawns=0+3, overhead=5
|
||||
|
||||
function computePhases(timings) {
|
||||
if (!timings || timings.length === 0) return [0.25, 0.25, 0.25, 0.25];
|
||||
var sums = [0,0,0,0,0,0];
|
||||
var active = 0;
|
||||
for (var i = 0; i < timings.length; i++) {
|
||||
var t = timings[i];
|
||||
if (t.did_work) active++;
|
||||
for (var p = 0; p < 6 && p < t.phase_us.length; p++) sums[p] += t.phase_us[p];
|
||||
}
|
||||
var total = sums.reduce(function(a,b) { return a+b; }, 0);
|
||||
if (total === 0) return [0.25, 0.25, 0.25, 0.25];
|
||||
var processing = sums[2] / total;
|
||||
var delivery = (sums[1] + sums[4]) / total;
|
||||
var spawns = (sums[0] + sums[3]) / total;
|
||||
var overhead = sums[5] / total;
|
||||
var load = timings.length > 0 ? active / timings.length : 0;
|
||||
return { fracs: [processing, delivery, spawns, overhead], load: load };
|
||||
}
|
||||
|
||||
function renderWorkerCards(data) {
|
||||
var container = document.getElementById('workerCards');
|
||||
if (!data.workers) return;
|
||||
container.innerHTML = '';
|
||||
|
||||
data.workers.forEach(function(w, idx) {
|
||||
var timings = data.tick_timings ? data.tick_timings[idx] : null;
|
||||
var phases = computePhases(timings);
|
||||
var load = phases.load || 0;
|
||||
var fracs = phases.fracs || [0.25, 0.25, 0.25, 0.25];
|
||||
|
||||
var card = document.createElement('div');
|
||||
card.className = 'worker-card';
|
||||
|
||||
// ID
|
||||
var idSpan = document.createElement('span');
|
||||
idSpan.className = 'wc-id';
|
||||
idSpan.style.color = colors[w.id % colors.length];
|
||||
idSpan.textContent = 'W' + w.id;
|
||||
card.appendChild(idSpan);
|
||||
|
||||
// Phase bar
|
||||
var barWrap = document.createElement('span');
|
||||
barWrap.className = 'wc-bar-wrap';
|
||||
var filledPct = Math.round(load * 100);
|
||||
for (var p = 0; p < 4; p++) {
|
||||
var seg = document.createElement('span');
|
||||
seg.className = 'wc-bar-seg';
|
||||
seg.style.width = (fracs[p] * filledPct) + '%';
|
||||
seg.style.background = phaseColors[p];
|
||||
barWrap.appendChild(seg);
|
||||
}
|
||||
card.appendChild(barWrap);
|
||||
|
||||
// Sparklines
|
||||
var sparkWrap = document.createElement('span');
|
||||
sparkWrap.className = 'wc-spark';
|
||||
var wh = workerHistory[w.id];
|
||||
if (wh) {
|
||||
sparkWrap.innerHTML = renderSparklineSvg(wh.message_rates, 60, 14, '#4caf50');
|
||||
}
|
||||
card.appendChild(sparkWrap);
|
||||
|
||||
// Stats
|
||||
var statsSpan = document.createElement('span');
|
||||
statsSpan.className = 'wc-stats';
|
||||
statsSpan.textContent = w.num_actors + ' actors ' +
|
||||
w.messages_processed.toLocaleString() + ' msgs mbox ' + w.mailbox_depth +
|
||||
' ' + Math.round(load * 100) + '%';
|
||||
card.appendChild(statsSpan);
|
||||
|
||||
container.appendChild(card);
|
||||
});
|
||||
}
|
||||
|
||||
function updateStats(data) {
|
||||
if (typeof data.uptime_ms === 'number') {
|
||||
lastUptimeMs = data.uptime_ms;
|
||||
lastStatsTime = Date.now();
|
||||
}
|
||||
var totalActors = data.actors ? data.actors.length : 0;
|
||||
var totalMsgs = data.workers ? data.workers.reduce(function(s, w) { return s + w.messages_processed; }, 0) : 0;
|
||||
var totalMailbox = data.workers ? data.workers.reduce(function(s, w) { return s + w.mailbox_depth; }, 0) : 0;
|
||||
|
||||
document.getElementById('statActors').textContent = totalActors;
|
||||
document.getElementById('statMessages').textContent = totalMsgs.toLocaleString();
|
||||
document.getElementById('statWorkers').textContent = data.num_workers || 0;
|
||||
document.getElementById('statMailbox').textContent = totalMailbox;
|
||||
|
||||
renderWorkerCards(data);
|
||||
|
||||
var nameMap = {};
|
||||
if (data.actor_details) {
|
||||
data.actor_details.forEach(function(a) {
|
||||
var h = formatAddr(a.address);
|
||||
if (a.name) nameMap[h] = a.name;
|
||||
});
|
||||
}
|
||||
|
||||
var tbody = document.getElementById('actorTableBody');
|
||||
tbody.innerHTML = '';
|
||||
if (data.actors) {
|
||||
var shown = data.actors.slice(0, 200);
|
||||
shown.forEach(function(entry) {
|
||||
var addr = entry[0];
|
||||
var wid = entry[1];
|
||||
var hex = '';
|
||||
if (addr && addr.length > 0) {
|
||||
var bytes = Array.isArray(addr) ? addr : Object.values(addr);
|
||||
for (var j = 0; j < Math.min(8, bytes.length); j++) {
|
||||
hex += ('0' + bytes[j].toString(16)).slice(-2);
|
||||
}
|
||||
hex += '\u2026';
|
||||
}
|
||||
var tr = document.createElement('tr');
|
||||
var addrCell;
|
||||
if (nameMap[hex]) {
|
||||
addrCell = '<td><a href="/actor/' + hex + '" style="text-decoration:none;"><span style="color:#00bcd4;font-weight:600;">' + escapeHtml(nameMap[hex]) + '</span> <span style="color:#555;font-size:10px;">' + hex + '</span></a></td>';
|
||||
} else {
|
||||
addrCell = '<td style="color:#aaa;font-size:11px;"><a href="/actor/' + hex + '" style="color:#aaa;text-decoration:none;">' + hex + '</a></td>';
|
||||
}
|
||||
tr.innerHTML = addrCell + '<td>W' + wid + '</td>';
|
||||
tbody.appendChild(tr);
|
||||
});
|
||||
if (data.actors.length > 200) {
|
||||
var tr2 = document.createElement('tr');
|
||||
tr2.innerHTML = '<td colspan="2" style="color:#555;">... and ' + (data.actors.length - 200) + ' more</td>';
|
||||
tbody.appendChild(tr2);
|
||||
}
|
||||
}
|
||||
|
||||
updateWorkerDetails(data);
|
||||
}
|
||||
|
||||
function formatAddr(addr) {
|
||||
var hex = '';
|
||||
if (addr && addr.length > 0) {
|
||||
var bytes = Array.isArray(addr) ? addr : Object.values(addr);
|
||||
for (var j = 0; j < Math.min(8, bytes.length); j++) {
|
||||
hex += ('0' + bytes[j].toString(16)).slice(-2);
|
||||
}
|
||||
hex += '\u2026';
|
||||
}
|
||||
return hex;
|
||||
}
|
||||
|
||||
function shortTypeName(full) {
|
||||
if (!full) return '';
|
||||
var parts = full.split('::');
|
||||
return parts[parts.length - 1];
|
||||
}
|
||||
|
||||
function renderSparklineSvg(data, w, h, color) {
|
||||
if (!data || data.length < 2) return '';
|
||||
var max = Math.max.apply(null, data);
|
||||
if (max === 0) max = 1;
|
||||
var step = w / (data.length - 1);
|
||||
var points = data.map(function(v, i) {
|
||||
return (i * step).toFixed(1) + ',' + (h - (v / max) * (h - 2) - 1).toFixed(1);
|
||||
}).join(' ');
|
||||
return '<svg width="' + w + '" height="' + h + '" style="vertical-align:middle">' +
|
||||
'<polyline fill="none" stroke="' + color + '" stroke-width="1.5" points="' + points + '"/></svg>';
|
||||
}
|
||||
|
||||
function pushHistorySample(stats) {
|
||||
if (!stats.workers) return;
|
||||
stats.workers.forEach(function(w) {
|
||||
if (!workerHistory[w.id]) {
|
||||
workerHistory[w.id] = { message_rates: [], mailbox_depths: [], prev_msgs: w.messages_processed };
|
||||
}
|
||||
var wh = workerHistory[w.id];
|
||||
var rate = w.messages_processed - wh.prev_msgs;
|
||||
if (rate < 0) rate = 0;
|
||||
wh.prev_msgs = w.messages_processed;
|
||||
wh.message_rates.push(rate);
|
||||
wh.mailbox_depths.push(w.mailbox_depth);
|
||||
if (wh.message_rates.length > 300) { wh.message_rates.shift(); wh.mailbox_depths.shift(); }
|
||||
});
|
||||
}
|
||||
|
||||
function updateWorkerDetails(data) {
|
||||
var container = document.getElementById('workerDetailContainer');
|
||||
if (!data.workers) return;
|
||||
|
||||
var groups = {};
|
||||
data.workers.forEach(function(w) { groups[w.id] = { info: w, actors: [] }; });
|
||||
if (data.actor_details) {
|
||||
data.actor_details.forEach(function(a) {
|
||||
if (groups[a.worker_id]) groups[a.worker_id].actors.push(a);
|
||||
});
|
||||
}
|
||||
|
||||
var openState = {};
|
||||
container.querySelectorAll('.worker-group').forEach(function(g) {
|
||||
openState[g.dataset.wid] = g.classList.contains('open');
|
||||
});
|
||||
|
||||
container.innerHTML = '';
|
||||
var wids = Object.keys(groups).sort(function(a, b) { return a - b; });
|
||||
|
||||
wids.forEach(function(wid) {
|
||||
var g = groups[wid];
|
||||
var div = document.createElement('div');
|
||||
var isOpen = openState[wid] || false;
|
||||
div.className = 'worker-group' + (isOpen ? ' open' : '');
|
||||
div.dataset.wid = wid;
|
||||
|
||||
var hdr = document.createElement('div');
|
||||
hdr.className = 'worker-group-header';
|
||||
var panicHtml = g.info.panics > 0 ? ', <span style="color:#f44336">' + g.info.panics + ' panics</span>' : '';
|
||||
var wh = workerHistory[wid];
|
||||
var sparkHtml = '';
|
||||
if (wh) {
|
||||
sparkHtml = '<span class="sparkline-wrap">' +
|
||||
renderSparklineSvg(wh.message_rates, 80, 16, '#4caf50') +
|
||||
renderSparklineSvg(wh.mailbox_depths, 80, 16, '#2196f3') +
|
||||
'</span>';
|
||||
}
|
||||
hdr.innerHTML =
|
||||
'<span class="wid" style="color:' + colors[wid % colors.length] + '">W' + wid + '</span>' +
|
||||
sparkHtml +
|
||||
'<span class="summary">' + g.actors.length + ' actors, ' +
|
||||
g.info.messages_processed.toLocaleString() + ' msgs, mbox ' + g.info.mailbox_depth + panicHtml + '</span>' +
|
||||
'<span class="toggle">' + (isOpen ? '\u25BC' : '\u25B6') + '</span>';
|
||||
hdr.onclick = function() {
|
||||
div.classList.toggle('open');
|
||||
hdr.querySelector('.toggle').textContent = div.classList.contains('open') ? '\u25BC' : '\u25B6';
|
||||
};
|
||||
div.appendChild(hdr);
|
||||
|
||||
var body = document.createElement('div');
|
||||
body.className = 'worker-group-body';
|
||||
if (g.actors.length === 0) {
|
||||
body.innerHTML = '<div style="padding:6px 12px;color:#555;">No actors</div>';
|
||||
} else {
|
||||
var rows = '';
|
||||
g.actors.forEach(function(a) {
|
||||
var hex = formatAddr(a.address);
|
||||
var hasMsg = !!a.last_msg_type;
|
||||
var msgShort = hasMsg ? shortTypeName(a.last_msg_type) : 'none';
|
||||
var msgClass = hasMsg ? 'msg-type' : 'msg-type none';
|
||||
var title = hasMsg ? ' title="' + escapeHtml(a.last_msg_type) + '"' : '';
|
||||
var addrHtml;
|
||||
if (a.name) {
|
||||
addrHtml = '<td><a href="/actor/' + hex + '" style="text-decoration:none;"><span style="color:#00bcd4;font-weight:600;">' + escapeHtml(a.name) + '</span> <span style="color:#555;font-size:10px;">' + hex + '</span></a></td>';
|
||||
} else {
|
||||
addrHtml = '<td style="color:#aaa;"><a href="/actor/' + hex + '" style="color:#aaa;text-decoration:none;">' + hex + '</a></td>';
|
||||
}
|
||||
rows += '<tr>' + addrHtml +
|
||||
'<td>' + a.mailbox_depth + '</td>' +
|
||||
'<td class="' + msgClass + '"' + title + '>' + escapeHtml(msgShort) + '</td></tr>';
|
||||
});
|
||||
body.innerHTML = '<table><thead><tr><th>Address</th><th>Mailbox</th><th>Last Message</th></tr></thead><tbody>' + rows + '</tbody></table>';
|
||||
}
|
||||
div.appendChild(body);
|
||||
container.appendChild(div);
|
||||
});
|
||||
}
|
||||
|
||||
function addLogEvents(events) {
|
||||
var tbody = document.getElementById('logTableBody');
|
||||
var wrap = document.getElementById('logWrap');
|
||||
var wasAtBottom = wrap.scrollTop + wrap.clientHeight >= wrap.scrollHeight - 20;
|
||||
|
||||
events.forEach(function(ev) {
|
||||
if (logRowCount >= MAX_LOG_ROWS) {
|
||||
tbody.removeChild(tbody.firstChild);
|
||||
logRowCount--;
|
||||
}
|
||||
var tr = document.createElement('tr');
|
||||
var ts = new Date(ev.timestamp_ms);
|
||||
var timeStr = ts.toLocaleTimeString() + '.' + String(ts.getMilliseconds()).padStart(3, '0');
|
||||
var wid = ev.worker_id !== null && ev.worker_id !== undefined ? 'W' + ev.worker_id : '-';
|
||||
var fieldsStr = Object.keys(ev.fields).length > 0 ? JSON.stringify(ev.fields) : '';
|
||||
|
||||
tr.innerHTML =
|
||||
'<td>' + ev.seq + '</td>' +
|
||||
'<td>' + timeStr + '</td>' +
|
||||
'<td class="level-' + ev.level + '">' + ev.level + '</td>' +
|
||||
'<td>' + wid + '</td>' +
|
||||
'<td class="msg">' + escapeHtml(ev.message) + '</td>' +
|
||||
'<td style="color:#555;max-width:300px;overflow:hidden;text-overflow:ellipsis;">' + escapeHtml(fieldsStr) + '</td>';
|
||||
tbody.appendChild(tr);
|
||||
logRowCount++;
|
||||
});
|
||||
|
||||
document.getElementById('logCount').textContent = '(' + logRowCount + ')';
|
||||
|
||||
if (wasAtBottom) {
|
||||
wrap.scrollTop = wrap.scrollHeight;
|
||||
}
|
||||
}
|
||||
|
||||
function escapeHtml(s) {
|
||||
if (!s) return '';
|
||||
return s.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
|
||||
}
|
||||
|
||||
function setStatus(s) {
|
||||
if (s === 'done') {
|
||||
dot.className = 'status-dot done';
|
||||
if (isReplay) {
|
||||
replayPct.textContent = '100%';
|
||||
progressFill.style.width = '100%';
|
||||
}
|
||||
} else if (s === 'disconnected') {
|
||||
dot.className = 'status-dot disconnected';
|
||||
} else {
|
||||
dot.className = isReplay ? 'status-dot replaying' : 'status-dot';
|
||||
}
|
||||
}
|
||||
|
||||
var es = new EventSource('/events');
|
||||
window.addEventListener('beforeunload', function() { es.close(); });
|
||||
|
||||
es.addEventListener('stats', function(e) {
|
||||
try {
|
||||
var data = JSON.parse(e.data);
|
||||
pushHistorySample(data);
|
||||
updateStats(data);
|
||||
} catch(err) { console.error('stats parse error', err); }
|
||||
});
|
||||
|
||||
es.addEventListener('history', function(e) {
|
||||
try {
|
||||
var data = JSON.parse(e.data);
|
||||
if (data.workers) {
|
||||
data.workers.forEach(function(w) {
|
||||
workerHistory[w.id] = {
|
||||
message_rates: w.message_rates || [],
|
||||
mailbox_depths: w.mailbox_depths || [],
|
||||
prev_msgs: 0
|
||||
};
|
||||
});
|
||||
}
|
||||
} catch(err) { console.error('history parse error', err); }
|
||||
});
|
||||
|
||||
es.addEventListener('warnings', function(e) {
|
||||
try {
|
||||
var warnings = JSON.parse(e.data);
|
||||
var banner = document.getElementById('warningBanner');
|
||||
if (warnings.length === 0) {
|
||||
banner.style.display = 'none';
|
||||
return;
|
||||
}
|
||||
banner.style.display = 'block';
|
||||
var sevColors = {critical:'#f44336',high:'#ff5722',medium:'#ff9800',low:'#888'};
|
||||
var html = warnings.map(function(w) {
|
||||
var c = sevColors[w.severity] || '#888';
|
||||
return '<span style="color:' + c + ';">\u26A0 ' + w.description + '</span>';
|
||||
}).join(' ');
|
||||
banner.innerHTML = '<span style="color:#ff9800;font-weight:700;">WARNINGS (' + warnings.length + ')</span> ' + html;
|
||||
} catch(err) { console.error('warnings parse error', err); }
|
||||
});
|
||||
|
||||
es.addEventListener('activity', function(e) {
|
||||
try { addLogEvents(JSON.parse(e.data)); } catch(err) { console.error('activity parse error', err); }
|
||||
});
|
||||
|
||||
es.addEventListener('replay_meta', function(e) {
|
||||
try {
|
||||
var meta = JSON.parse(e.data);
|
||||
replaySpeed.textContent = meta.speed + 'x';
|
||||
replaySpeed.style.display = 'inline';
|
||||
replayPct.style.display = 'inline';
|
||||
replayPct.textContent = '0%';
|
||||
} catch(err) { console.error('replay_meta parse error', err); }
|
||||
});
|
||||
|
||||
es.addEventListener('replay_progress', function(e) {
|
||||
try {
|
||||
var data = JSON.parse(e.data);
|
||||
var pct = Math.round(data.progress * 100);
|
||||
replayPct.textContent = pct + '%';
|
||||
progressFill.style.width = pct + '%';
|
||||
} catch(err) { console.error('replay_progress parse error', err); }
|
||||
});
|
||||
|
||||
es.addEventListener('done', function() {
|
||||
setStatus('done');
|
||||
es.close();
|
||||
});
|
||||
|
||||
es.onerror = function() {
|
||||
setStatus('disconnected');
|
||||
};
|
||||
|
||||
es.onopen = function() {
|
||||
setStatus('connected');
|
||||
};
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
"##;
|
||||
2168
crates/dashboard/src/html.rs
Normal file
2168
crates/dashboard/src/html.rs
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -4,14 +4,10 @@ pub mod history;
|
|||
pub mod investigate;
|
||||
pub mod layer;
|
||||
pub mod plugin;
|
||||
pub mod trace;
|
||||
pub mod warnings;
|
||||
mod actor_detail_html;
|
||||
mod actors_html;
|
||||
mod dashboard_html;
|
||||
mod html;
|
||||
mod server;
|
||||
pub mod topology;
|
||||
mod topology_html;
|
||||
|
||||
#[cfg(feature = "tui")]
|
||||
pub mod tui;
|
||||
|
|
@ -28,11 +24,29 @@ use swactor::runtime::{Runtime, RuntimeHandle};
|
|||
use tracing_subscriber::layer::SubscriberExt;
|
||||
use tracing_subscriber::util::SubscriberInitExt;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use swactor::stats::RuntimeStats;
|
||||
|
||||
use crate::collector::StatsCollector;
|
||||
use crate::history::{DashboardHistory, HistoryConfig};
|
||||
use crate::layer::{now_ms, DashboardLayer, EventStore};
|
||||
use crate::layer::{now_ms, DashboardEvent, DashboardLayer, EventStore};
|
||||
use crate::plugin::PluginRegistry;
|
||||
use crate::trace::{RuntimeTrace, TimestampedStats};
|
||||
|
||||
// ─── Trace Types ────────────────────────────────────────────────────────────
|
||||
|
||||
/// Complete trace of a runtime execution, suitable for saving/loading.
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct RuntimeTrace {
|
||||
pub events: Vec<DashboardEvent>,
|
||||
pub stats_timeline: Vec<TimestampedStats>,
|
||||
}
|
||||
|
||||
/// A stats snapshot with a wall-clock timestamp.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct TimestampedStats {
|
||||
pub timestamp_ms: u64,
|
||||
pub stats: RuntimeStats,
|
||||
}
|
||||
|
||||
/// Peer info sent through the join channel.
|
||||
pub struct JoinPeerInfo {
|
||||
|
|
@ -150,7 +164,7 @@ impl DashboardHandle {
|
|||
}
|
||||
|
||||
/// Start the HTTP server on a standalone tokio runtime (1 worker thread).
|
||||
/// Use this when no external tokio runtime is available (e.g. TCP transport).
|
||||
/// Use this when no external tokio runtime is available (e.g. non-async transport).
|
||||
pub fn start_http_standalone(&self) {
|
||||
let rt = tokio::runtime::Builder::new_multi_thread()
|
||||
.worker_threads(1)
|
||||
|
|
|
|||
|
|
@ -16,20 +16,17 @@ use tokio_stream::StreamExt;
|
|||
|
||||
use swactor::runtime::Runtime;
|
||||
|
||||
use crate::actor_detail_html::ACTOR_DETAIL_HTML;
|
||||
use crate::actors_html::ACTORS_HTML;
|
||||
use crate::collector::StatsCollector;
|
||||
use crate::command::CommandRouter;
|
||||
use crate::dashboard_html::DASHBOARD_HTML;
|
||||
use crate::history::DashboardHistory;
|
||||
use crate::html::{ACTOR_DETAIL_HTML, ACTORS_HTML, DASHBOARD_HTML, TOPOLOGY_HTML};
|
||||
use crate::layer::EventStore;
|
||||
use crate::topology;
|
||||
use crate::topology_html::TOPOLOGY_HTML;
|
||||
use crate::warnings::{WarningConfig, WarningDetector};
|
||||
|
||||
use crate::plugin::PluginRegistry;
|
||||
|
||||
use crate::trace::RuntimeTrace;
|
||||
use crate::RuntimeTrace;
|
||||
|
||||
/// Format a server-sent event.
|
||||
fn format_sse(event: &str, data: &str) -> Event {
|
||||
|
|
|
|||
|
|
@ -1,282 +0,0 @@
|
|||
pub const TOPOLOGY_HTML: &str = r##"<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Topology — Swactor Dashboard</title>
|
||||
<style>
|
||||
* { margin: 0; padding: 0; box-sizing: border-box; }
|
||||
body { font-family: 'Menlo', 'Consolas', 'Monaco', monospace; background: #0f1117; color: #e0e0e0; font-size: 13px; }
|
||||
|
||||
.header {
|
||||
display: flex; align-items: center; justify-content: space-between;
|
||||
padding: 12px 20px; background: #161822; border-bottom: 1px solid #2a2d3e;
|
||||
}
|
||||
.header h1 { font-size: 16px; font-weight: 600; color: #fff; }
|
||||
.status-dot {
|
||||
width: 10px; height: 10px; border-radius: 50%; background: #4caf50;
|
||||
display: inline-block; margin-left: 8px; vertical-align: middle;
|
||||
}
|
||||
.status-dot.disconnected { background: #f44336; }
|
||||
|
||||
.header-left { display: flex; align-items: center; }
|
||||
.nav-links { display: flex; gap: 4px; margin-left: 20px; }
|
||||
.nav-link {
|
||||
color: #888; text-decoration: none; font-size: 12px;
|
||||
padding: 4px 10px; border-radius: 3px;
|
||||
}
|
||||
.nav-link:hover { color: #e0e0e0; }
|
||||
.nav-link.active { color: #fff; background: #2a2d3e; }
|
||||
|
||||
.content { padding: 0; display: flex; flex-direction: column; height: calc(100vh - 49px); }
|
||||
canvas#topoCanvas { flex: 1; width: 100%; cursor: grab; }
|
||||
canvas#topoCanvas:active { cursor: grabbing; }
|
||||
|
||||
.legend {
|
||||
padding: 8px 20px; background: #161822; border-top: 1px solid #2a2d3e;
|
||||
font-size: 11px; color: #888;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="header">
|
||||
<div class="header-left">
|
||||
<h1>Swactor Runtime Dashboard <span id="statusDot" class="status-dot"></span></h1>
|
||||
<nav class="nav-links">
|
||||
<a href="/" class="nav-link">Overview</a>
|
||||
<a href="/actors" class="nav-link">Actors</a>
|
||||
<a href="/topology" class="nav-link active">Topology</a>
|
||||
<a href="/plugin/distribution" class="nav-link">Distribution</a>
|
||||
<a href="/plugin/datastore" class="nav-link">Datastore</a>
|
||||
</nav>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="content">
|
||||
<canvas id="topoCanvas"></canvas>
|
||||
<div class="legend">
|
||||
Node size = actor count. Edge thickness = message volume. Green = local sends. Blue = cross-worker sends.
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
(function() {
|
||||
var canvas = document.getElementById('topoCanvas');
|
||||
var ctx = canvas.getContext('2d');
|
||||
var dot = document.getElementById('statusDot');
|
||||
|
||||
var colors = ['#4caf50','#2196f3','#ff9800','#f44336','#9c27b0','#00bcd4','#ffeb3b','#e91e63'];
|
||||
var nodes = [];
|
||||
var edges = [];
|
||||
var positions = {};
|
||||
|
||||
function resize() {
|
||||
var dpr = window.devicePixelRatio || 1;
|
||||
var rect = canvas.getBoundingClientRect();
|
||||
canvas.width = rect.width * dpr;
|
||||
canvas.height = rect.height * dpr;
|
||||
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
||||
}
|
||||
window.addEventListener('resize', resize);
|
||||
resize();
|
||||
|
||||
function initPositions() {
|
||||
var W = canvas.getBoundingClientRect().width;
|
||||
var H = canvas.getBoundingClientRect().height;
|
||||
var cx = W / 2, cy = H / 2;
|
||||
var r = Math.min(W, H) * 0.3;
|
||||
|
||||
nodes.forEach(function(n, i) {
|
||||
if (!positions[n.id]) {
|
||||
var angle = (2 * Math.PI * i) / Math.max(1, nodes.length);
|
||||
positions[n.id] = {
|
||||
x: cx + r * Math.cos(angle),
|
||||
y: cy + r * Math.sin(angle),
|
||||
vx: 0, vy: 0
|
||||
};
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function simulate() {
|
||||
var W = canvas.getBoundingClientRect().width;
|
||||
var H = canvas.getBoundingClientRect().height;
|
||||
var cx = W / 2, cy = H / 2;
|
||||
|
||||
// Repulsion between nodes
|
||||
for (var i = 0; i < nodes.length; i++) {
|
||||
var pi = positions[nodes[i].id];
|
||||
if (!pi) continue;
|
||||
for (var j = i + 1; j < nodes.length; j++) {
|
||||
var pj = positions[nodes[j].id];
|
||||
if (!pj) continue;
|
||||
var dx = pi.x - pj.x;
|
||||
var dy = pi.y - pj.y;
|
||||
var dist = Math.sqrt(dx * dx + dy * dy) || 1;
|
||||
var force = 8000 / (dist * dist);
|
||||
pi.vx += dx / dist * force;
|
||||
pi.vy += dy / dist * force;
|
||||
pj.vx -= dx / dist * force;
|
||||
pj.vy -= dy / dist * force;
|
||||
}
|
||||
}
|
||||
|
||||
// Attraction along edges
|
||||
edges.forEach(function(e) {
|
||||
if (e.source === e.target) return;
|
||||
var ps = positions[e.source];
|
||||
var pt = positions[e.target];
|
||||
if (!ps || !pt) return;
|
||||
var dx = pt.x - ps.x;
|
||||
var dy = pt.y - ps.y;
|
||||
var dist = Math.sqrt(dx * dx + dy * dy) || 1;
|
||||
var force = (dist - 150) * 0.01;
|
||||
ps.vx += dx / dist * force;
|
||||
ps.vy += dy / dist * force;
|
||||
pt.vx -= dx / dist * force;
|
||||
pt.vy -= dy / dist * force;
|
||||
});
|
||||
|
||||
// Gravity toward center
|
||||
for (var i = 0; i < nodes.length; i++) {
|
||||
var p = positions[nodes[i].id];
|
||||
if (!p) continue;
|
||||
p.vx += (cx - p.x) * 0.002;
|
||||
p.vy += (cy - p.y) * 0.002;
|
||||
}
|
||||
|
||||
// Apply velocity with damping
|
||||
for (var i = 0; i < nodes.length; i++) {
|
||||
var p = positions[nodes[i].id];
|
||||
if (!p) continue;
|
||||
p.vx *= 0.85;
|
||||
p.vy *= 0.85;
|
||||
p.x += p.vx;
|
||||
p.y += p.vy;
|
||||
p.x = Math.max(30, Math.min(W - 30, p.x));
|
||||
p.y = Math.max(30, Math.min(H - 30, p.y));
|
||||
}
|
||||
}
|
||||
|
||||
function draw() {
|
||||
var W = canvas.getBoundingClientRect().width;
|
||||
var H = canvas.getBoundingClientRect().height;
|
||||
ctx.clearRect(0, 0, W, H);
|
||||
|
||||
if (nodes.length === 0) {
|
||||
ctx.fillStyle = '#555';
|
||||
ctx.font = '14px monospace';
|
||||
ctx.textAlign = 'center';
|
||||
ctx.fillText('Waiting for topology data...', W / 2, H / 2);
|
||||
return;
|
||||
}
|
||||
|
||||
// Draw edges
|
||||
var maxWeight = Math.max(1, Math.max.apply(null, edges.map(function(e) { return e.weight; })));
|
||||
|
||||
edges.forEach(function(e) {
|
||||
var ps = positions[e.source];
|
||||
var pt = positions[e.target];
|
||||
if (!ps || !pt) return;
|
||||
|
||||
var isSelf = e.source === e.target;
|
||||
var thickness = Math.max(1, (e.weight / maxWeight) * 6);
|
||||
var color = isSelf ? 'rgba(76, 175, 80, 0.5)' : 'rgba(33, 150, 243, 0.5)';
|
||||
|
||||
if (isSelf) {
|
||||
// Self-loop: small arc above the node
|
||||
ctx.beginPath();
|
||||
ctx.arc(ps.x, ps.y - 25, 15, 0.3, Math.PI - 0.3);
|
||||
ctx.strokeStyle = color;
|
||||
ctx.lineWidth = thickness;
|
||||
ctx.stroke();
|
||||
} else {
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(ps.x, ps.y);
|
||||
ctx.lineTo(pt.x, pt.y);
|
||||
ctx.strokeStyle = color;
|
||||
ctx.lineWidth = thickness;
|
||||
ctx.stroke();
|
||||
|
||||
// Arrow
|
||||
var angle = Math.atan2(pt.y - ps.y, pt.x - ps.x);
|
||||
var headLen = 8;
|
||||
var mx = (ps.x + pt.x) / 2;
|
||||
var my = (ps.y + pt.y) / 2;
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(mx, my);
|
||||
ctx.lineTo(mx - headLen * Math.cos(angle - 0.3), my - headLen * Math.sin(angle - 0.3));
|
||||
ctx.moveTo(mx, my);
|
||||
ctx.lineTo(mx - headLen * Math.cos(angle + 0.3), my - headLen * Math.sin(angle + 0.3));
|
||||
ctx.strokeStyle = color;
|
||||
ctx.lineWidth = 1.5;
|
||||
ctx.stroke();
|
||||
|
||||
// Edge label
|
||||
ctx.fillStyle = '#666';
|
||||
ctx.font = '9px monospace';
|
||||
ctx.textAlign = 'center';
|
||||
ctx.fillText(e.label, mx, my - 6);
|
||||
}
|
||||
});
|
||||
|
||||
// Draw nodes
|
||||
nodes.forEach(function(n) {
|
||||
var p = positions[n.id];
|
||||
if (!p) return;
|
||||
var r = Math.max(12, 8 + n.actor_count * 2);
|
||||
var color = colors[n.group % colors.length];
|
||||
|
||||
ctx.beginPath();
|
||||
ctx.arc(p.x, p.y, r, 0, 2 * Math.PI);
|
||||
ctx.fillStyle = color;
|
||||
ctx.globalAlpha = 0.7;
|
||||
ctx.fill();
|
||||
ctx.globalAlpha = 1;
|
||||
ctx.strokeStyle = '#fff';
|
||||
ctx.lineWidth = 1.5;
|
||||
ctx.stroke();
|
||||
|
||||
ctx.fillStyle = '#fff';
|
||||
ctx.font = 'bold 11px monospace';
|
||||
ctx.textAlign = 'center';
|
||||
ctx.textBaseline = 'middle';
|
||||
ctx.fillText(n.label, p.x, p.y);
|
||||
|
||||
ctx.fillStyle = '#888';
|
||||
ctx.font = '9px monospace';
|
||||
ctx.fillText(n.actor_count + ' actors', p.x, p.y + r + 12);
|
||||
});
|
||||
}
|
||||
|
||||
function updateTopology(data) {
|
||||
nodes = data.nodes || [];
|
||||
edges = data.edges || [];
|
||||
initPositions();
|
||||
}
|
||||
|
||||
function tick() {
|
||||
simulate();
|
||||
draw();
|
||||
requestAnimationFrame(tick);
|
||||
}
|
||||
tick();
|
||||
|
||||
var es = new EventSource('/events');
|
||||
window.addEventListener('beforeunload', function() { es.close(); });
|
||||
|
||||
es.addEventListener('topology', function(e) {
|
||||
try { updateTopology(JSON.parse(e.data)); } catch(err) { console.error(err); }
|
||||
});
|
||||
|
||||
es.addEventListener('done', function() {
|
||||
dot.className = 'status-dot disconnected';
|
||||
es.close();
|
||||
});
|
||||
es.onerror = function() { dot.className = 'status-dot disconnected'; };
|
||||
es.onopen = function() { dot.className = 'status-dot'; };
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
"##;
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
use swactor::stats::RuntimeStats;
|
||||
|
||||
use crate::layer::DashboardEvent;
|
||||
|
||||
/// Complete trace of a runtime execution, suitable for saving/loading.
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct RuntimeTrace {
|
||||
pub events: Vec<DashboardEvent>,
|
||||
pub stats_timeline: Vec<TimestampedStats>,
|
||||
}
|
||||
|
||||
/// A stats snapshot with a wall-clock timestamp.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct TimestampedStats {
|
||||
pub timestamp_ms: u64,
|
||||
pub stats: RuntimeStats,
|
||||
}
|
||||
|
|
@ -2,7 +2,7 @@ use std::sync::Arc;
|
|||
|
||||
use dashboard::collector::StatsCollector;
|
||||
use dashboard::layer::{DashboardEvent, EventStore};
|
||||
use dashboard::trace::RuntimeTrace;
|
||||
use dashboard::RuntimeTrace;
|
||||
use swactor::actor::ActorAddress;
|
||||
use swactor::stats::{ActorSnapshot, StatsHook};
|
||||
|
||||
|
|
|
|||
|
|
@ -8,8 +8,7 @@ unexpected_cfgs = { level = "warn", check-cfg = ['cfg(kani)'] }
|
|||
|
||||
[dependencies]
|
||||
swactor = { path = "../..", features = ["serde", "transport"] }
|
||||
ed25519-dalek = { version = "2", features = ["rand_core"] }
|
||||
rand_core = { version = "0.6", features = ["getrandom"] }
|
||||
swactor-transport = { path = "../transport" }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
blake3 = "1"
|
||||
|
|
@ -35,6 +34,7 @@ stateright = "0.31"
|
|||
[features]
|
||||
node = ["dep:tiny_http", "dep:clap", "dep:ctrlc", "dep:toml"]
|
||||
cli = ["dep:clap", "dep:ureq"]
|
||||
formal-verification = []
|
||||
|
||||
[[bin]]
|
||||
name = "swactor-store"
|
||||
|
|
|
|||
|
|
@ -316,75 +316,6 @@ fn resolve_authorized_key(base: &str, name_input: &str, kp: &Keypair) -> String
|
|||
}
|
||||
}
|
||||
|
||||
// ── Key file helpers ────────────────────────────────────────────────────────
|
||||
|
||||
fn hex_decode(hex: &str) -> Option<Vec<u8>> {
|
||||
if hex.len() % 2 != 0 {
|
||||
return None;
|
||||
}
|
||||
let mut bytes = Vec::with_capacity(hex.len() / 2);
|
||||
for chunk in hex.as_bytes().chunks(2) {
|
||||
let hi = hex_digit(chunk[0])?;
|
||||
let lo = hex_digit(chunk[1])?;
|
||||
bytes.push((hi << 4) | lo);
|
||||
}
|
||||
Some(bytes)
|
||||
}
|
||||
|
||||
fn hex_digit(b: u8) -> Option<u8> {
|
||||
match b {
|
||||
b'0'..=b'9' => Some(b - b'0'),
|
||||
b'a'..=b'f' => Some(b - b'a' + 10),
|
||||
b'A'..=b'F' => Some(b - b'A' + 10),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn load_keypair(path: &std::path::Path) -> Keypair {
|
||||
let data = fs::read_to_string(path).unwrap_or_else(|e| {
|
||||
eprintln!("Error reading key file {}: {e}", path.display());
|
||||
std::process::exit(1);
|
||||
});
|
||||
let json: serde_json::Value = serde_json::from_str(&data).unwrap_or_else(|e| {
|
||||
eprintln!("Error parsing key file: {e}");
|
||||
std::process::exit(1);
|
||||
});
|
||||
let secret_hex = json
|
||||
.get("secret_key")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or_else(|| {
|
||||
eprintln!("Key file missing secret_key field");
|
||||
std::process::exit(1);
|
||||
});
|
||||
let secret_bytes = hex_decode(secret_hex).unwrap_or_else(|| {
|
||||
eprintln!("Invalid secret_key hex in key file");
|
||||
std::process::exit(1);
|
||||
});
|
||||
let secret: [u8; 32] = secret_bytes.try_into().unwrap_or_else(|_| {
|
||||
eprintln!("secret_key must be exactly 32 bytes");
|
||||
std::process::exit(1);
|
||||
});
|
||||
Keypair::from_bytes(&secret)
|
||||
}
|
||||
|
||||
// ── Auth signing ────────────────────────────────────────────────────────────
|
||||
|
||||
fn sign_action(keypair: &Keypair, action: DatastoreAction) -> String {
|
||||
let timestamp = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_secs();
|
||||
let mut nonce = [0u8; 16];
|
||||
getrandom::getrandom(&mut nonce).expect("failed to generate random nonce");
|
||||
let payload = SignedRequestPayload {
|
||||
action,
|
||||
timestamp,
|
||||
nonce,
|
||||
};
|
||||
let signed = sign_request(keypair, payload);
|
||||
serde_json::to_string(&signed).expect("SignedRequest is always serializable")
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args = Args::parse();
|
||||
let base = args.url.trim_end_matches('/');
|
||||
|
|
|
|||
|
|
@ -1,72 +0,0 @@
|
|||
//! CLI command type definitions for `swactor-store`.
|
||||
//!
|
||||
//! Types only — no implementation. These define the CLI interface that will
|
||||
//! be wired to the actor system in a future milestone.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use swactor::transport::NodeId;
|
||||
|
||||
/// Top-level CLI commands for `swactor-store`.
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum CliCommand {
|
||||
/// Store a local file as a distributed object.
|
||||
///
|
||||
/// ```text
|
||||
/// swactor-store put <local-path> [--name <label>] [--tag key=value...]
|
||||
/// ```
|
||||
Put {
|
||||
/// Path to the local file to store.
|
||||
local_path: PathBuf,
|
||||
/// Optional human-readable name for the object.
|
||||
name: Option<String>,
|
||||
/// Key-value tags to attach to the object.
|
||||
tags: BTreeMap<String, String>,
|
||||
},
|
||||
|
||||
/// Retrieve an object from the datastore by content hash.
|
||||
///
|
||||
/// ```text
|
||||
/// swactor-store get <content-hash>[@<node>] [--output <local-path>]
|
||||
/// ```
|
||||
Get {
|
||||
/// Content hash (hex) of the object to retrieve.
|
||||
content_hash: String,
|
||||
/// Specific node to fetch from (optional).
|
||||
node: Option<NodeId>,
|
||||
/// Local path to write the object to.
|
||||
output: Option<PathBuf>,
|
||||
},
|
||||
|
||||
/// Delete an object from the datastore by content hash.
|
||||
///
|
||||
/// ```text
|
||||
/// swactor-store delete <content-hash>
|
||||
/// ```
|
||||
Delete {
|
||||
/// Content hash (hex) of the object to delete.
|
||||
content_hash: String,
|
||||
},
|
||||
|
||||
/// List objects in the datastore.
|
||||
///
|
||||
/// ```text
|
||||
/// swactor-store list [--name <substring>] [--node <node-name>] [--all]
|
||||
/// ```
|
||||
List {
|
||||
/// Filter by name substring.
|
||||
name: Option<String>,
|
||||
/// List objects from a specific node only.
|
||||
node: Option<NodeId>,
|
||||
/// If true, query all nodes (swarm-wide). Otherwise, local only.
|
||||
all: bool,
|
||||
},
|
||||
|
||||
/// Show node status: identity, chunk count, storage usage.
|
||||
///
|
||||
/// ```text
|
||||
/// swactor-store status
|
||||
/// ```
|
||||
Status,
|
||||
}
|
||||
|
|
@ -1,96 +1,3 @@
|
|||
//! Ed25519 cryptographic primitives for the datastore.
|
||||
|
||||
use std::fmt;
|
||||
|
||||
use ed25519_dalek::{Signer, Verifier};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use swactor::transport::NodeId;
|
||||
|
||||
// ─── Signature ──────────────────────────────────────────────────────────────
|
||||
|
||||
/// An ed25519 signature (64 bytes).
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
pub struct Signature(pub [u8; 64]);
|
||||
|
||||
impl Serialize for Signature {
|
||||
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
serializer.serialize_bytes(&self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for Signature {
|
||||
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
||||
let bytes: Vec<u8> = Deserialize::deserialize(deserializer)?;
|
||||
if bytes.len() != 64 {
|
||||
return Err(serde::de::Error::custom(format!(
|
||||
"expected 64 bytes for Signature, got {}",
|
||||
bytes.len()
|
||||
)));
|
||||
}
|
||||
let mut arr = [0u8; 64];
|
||||
arr.copy_from_slice(&bytes);
|
||||
Ok(Signature(arr))
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Signature {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "Sig(")?;
|
||||
for b in &self.0[..4] {
|
||||
write!(f, "{:02x}", b)?;
|
||||
}
|
||||
write!(f, "\u{2026})")
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Keypair ────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Node identity keypair — wraps ed25519-dalek.
|
||||
pub struct Keypair {
|
||||
inner: ed25519_dalek::SigningKey,
|
||||
}
|
||||
|
||||
impl Keypair {
|
||||
/// Generate a new random keypair.
|
||||
pub fn generate() -> Self {
|
||||
let mut csprng = rand_core::OsRng;
|
||||
Self {
|
||||
inner: ed25519_dalek::SigningKey::generate(&mut csprng),
|
||||
}
|
||||
}
|
||||
|
||||
/// Reconstruct from raw secret key bytes (32 bytes).
|
||||
pub fn from_bytes(secret: &[u8; 32]) -> Self {
|
||||
Self {
|
||||
inner: ed25519_dalek::SigningKey::from_bytes(secret),
|
||||
}
|
||||
}
|
||||
|
||||
/// The public key as a `NodeId`.
|
||||
pub fn node_id(&self) -> NodeId {
|
||||
NodeId(self.inner.verifying_key().to_bytes())
|
||||
}
|
||||
|
||||
/// Raw secret key bytes.
|
||||
pub fn secret_bytes(&self) -> [u8; 32] {
|
||||
self.inner.to_bytes()
|
||||
}
|
||||
|
||||
/// Sign arbitrary bytes.
|
||||
pub fn sign(&self, msg: &[u8]) -> Signature {
|
||||
let sig = self.inner.sign(msg);
|
||||
Signature(sig.to_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Verification ───────────────────────────────────────────────────────────
|
||||
|
||||
/// Verify a signature against a `NodeId` (public key) and message bytes.
|
||||
pub fn verify(node_id: &NodeId, msg: &[u8], sig: &Signature) -> bool {
|
||||
let Ok(vk) = ed25519_dalek::VerifyingKey::from_bytes(&node_id.0) else {
|
||||
return false;
|
||||
};
|
||||
let signature = ed25519_dalek::Signature::from_bytes(&sig.0);
|
||||
vk.verify(msg, &signature).is_ok()
|
||||
}
|
||||
pub use swactor_transport::crypto::{Keypair, Signature, verify};
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@ pub mod actors;
|
|||
pub mod auth;
|
||||
pub mod blob_transfer;
|
||||
pub mod bridge;
|
||||
pub mod cli;
|
||||
pub mod metrics;
|
||||
pub mod streams;
|
||||
#[cfg(feature = "node")]
|
||||
|
|
|
|||
|
|
@ -1,92 +0,0 @@
|
|||
//! In-memory storage backend — useful for tests and browser/WASM targets.
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::types::{ContentHash, ObjectEntry, ObjectManifest};
|
||||
|
||||
use super::StorageBackend;
|
||||
|
||||
/// A purely in-memory storage backend.
|
||||
///
|
||||
/// All data lives in `HashMap`s. No persistence across restarts.
|
||||
/// Useful for unit tests and browser/WASM environments.
|
||||
pub struct InMemoryBackend {
|
||||
chunks: HashMap<ContentHash, Vec<u8>>,
|
||||
manifests: HashMap<ContentHash, ObjectManifest>,
|
||||
entries: HashMap<ContentHash, ObjectEntry>,
|
||||
}
|
||||
|
||||
impl InMemoryBackend {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
chunks: HashMap::new(),
|
||||
manifests: HashMap::new(),
|
||||
entries: HashMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for InMemoryBackend {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl StorageBackend for InMemoryBackend {
|
||||
fn write_chunk(&mut self, hash: &ContentHash, data: &[u8]) -> Result<(), std::io::Error> {
|
||||
self.chunks.insert(*hash, data.to_vec());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read_chunk(&self, hash: &ContentHash) -> Result<Option<Vec<u8>>, std::io::Error> {
|
||||
Ok(self.chunks.get(hash).cloned())
|
||||
}
|
||||
|
||||
fn delete_chunk(&mut self, hash: &ContentHash) -> Result<(), std::io::Error> {
|
||||
self.chunks.remove(hash);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn has_chunk(&self, hash: &ContentHash) -> bool {
|
||||
self.chunks.contains_key(hash)
|
||||
}
|
||||
|
||||
fn list_chunks(&self) -> Vec<ContentHash> {
|
||||
self.chunks.keys().copied().collect()
|
||||
}
|
||||
|
||||
fn write_manifest(&mut self, manifest: &ObjectManifest) -> Result<(), std::io::Error> {
|
||||
self.manifests.insert(manifest.content_hash, manifest.clone());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read_manifest(
|
||||
&self,
|
||||
content_hash: &ContentHash,
|
||||
) -> Result<Option<ObjectManifest>, std::io::Error> {
|
||||
Ok(self.manifests.get(content_hash).cloned())
|
||||
}
|
||||
|
||||
fn delete_manifest(&mut self, content_hash: &ContentHash) -> Result<(), std::io::Error> {
|
||||
self.manifests.remove(content_hash);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write_entry(&mut self, entry: &ObjectEntry) -> Result<(), std::io::Error> {
|
||||
self.entries.insert(entry.content_hash, entry.clone());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read_entry(&self, hash: &ContentHash) -> Result<Option<ObjectEntry>, std::io::Error> {
|
||||
Ok(self.entries.get(hash).cloned())
|
||||
}
|
||||
|
||||
fn delete_entry(&mut self, hash: &ContentHash) -> Result<(), std::io::Error> {
|
||||
self.entries.remove(hash);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn list_entries(&self) -> Result<Vec<ObjectEntry>, std::io::Error> {
|
||||
Ok(self.entries.values().cloned().collect())
|
||||
}
|
||||
}
|
||||
|
|
@ -3,17 +3,13 @@
|
|||
//! Abstracts chunk and manifest I/O so backends can be swapped
|
||||
//! (filesystem for MVP, IndexedDB for browser, in-memory for tests/WASM).
|
||||
|
||||
pub mod in_memory;
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::fs;
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::types::{ContentHash, ObjectEntry, ObjectManifest};
|
||||
|
||||
pub use in_memory::InMemoryBackend;
|
||||
|
||||
/// Pluggable storage backend for chunks and manifests.
|
||||
pub trait StorageBackend: Send {
|
||||
fn write_chunk(&mut self, hash: &ContentHash, data: &[u8]) -> Result<(), std::io::Error>;
|
||||
|
|
@ -244,3 +240,90 @@ impl StorageBackend for FilesystemBackend {
|
|||
}
|
||||
}
|
||||
|
||||
// ─── In-Memory Backend ──────────────────────────────────────────────────────
|
||||
|
||||
/// A purely in-memory storage backend.
|
||||
///
|
||||
/// All data lives in `HashMap`s. No persistence across restarts.
|
||||
/// Useful for unit tests and browser/WASM environments.
|
||||
pub struct InMemoryBackend {
|
||||
chunks: HashMap<ContentHash, Vec<u8>>,
|
||||
manifests: HashMap<ContentHash, ObjectManifest>,
|
||||
entries: HashMap<ContentHash, ObjectEntry>,
|
||||
}
|
||||
|
||||
impl InMemoryBackend {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
chunks: HashMap::new(),
|
||||
manifests: HashMap::new(),
|
||||
entries: HashMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for InMemoryBackend {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl StorageBackend for InMemoryBackend {
|
||||
fn write_chunk(&mut self, hash: &ContentHash, data: &[u8]) -> Result<(), std::io::Error> {
|
||||
self.chunks.insert(*hash, data.to_vec());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read_chunk(&self, hash: &ContentHash) -> Result<Option<Vec<u8>>, std::io::Error> {
|
||||
Ok(self.chunks.get(hash).cloned())
|
||||
}
|
||||
|
||||
fn delete_chunk(&mut self, hash: &ContentHash) -> Result<(), std::io::Error> {
|
||||
self.chunks.remove(hash);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn has_chunk(&self, hash: &ContentHash) -> bool {
|
||||
self.chunks.contains_key(hash)
|
||||
}
|
||||
|
||||
fn list_chunks(&self) -> Vec<ContentHash> {
|
||||
self.chunks.keys().copied().collect()
|
||||
}
|
||||
|
||||
fn write_manifest(&mut self, manifest: &ObjectManifest) -> Result<(), std::io::Error> {
|
||||
self.manifests.insert(manifest.content_hash, manifest.clone());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read_manifest(
|
||||
&self,
|
||||
content_hash: &ContentHash,
|
||||
) -> Result<Option<ObjectManifest>, std::io::Error> {
|
||||
Ok(self.manifests.get(content_hash).cloned())
|
||||
}
|
||||
|
||||
fn delete_manifest(&mut self, content_hash: &ContentHash) -> Result<(), std::io::Error> {
|
||||
self.manifests.remove(content_hash);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write_entry(&mut self, entry: &ObjectEntry) -> Result<(), std::io::Error> {
|
||||
self.entries.insert(entry.content_hash, entry.clone());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read_entry(&self, hash: &ContentHash) -> Result<Option<ObjectEntry>, std::io::Error> {
|
||||
Ok(self.entries.get(hash).cloned())
|
||||
}
|
||||
|
||||
fn delete_entry(&mut self, hash: &ContentHash) -> Result<(), std::io::Error> {
|
||||
self.entries.remove(hash);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn list_entries(&self) -> Result<Vec<ObjectEntry>, std::io::Error> {
|
||||
Ok(self.entries.values().cloned().collect())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ use tokio::io::AsyncWriteExt;
|
|||
use swactor::actor::{ActorAddress, ActorInterface, Ctx, Down};
|
||||
use swactor::runtime::Runtime;
|
||||
|
||||
use crate::streams::connection::StreamConnectionCache;
|
||||
use crate::streams::data_plane;
|
||||
use crate::streams::handle::{create_stream_handle, StreamHandle};
|
||||
use crate::streams::messages::{OneShot, StreamManagerMsg, StreamNotification};
|
||||
|
|
@ -23,16 +22,10 @@ const ACCEPT_BYTE: u8 = 0x01;
|
|||
const REJECT_BYTE: u8 = 0x00;
|
||||
|
||||
struct StreamState {
|
||||
_stream_id: StreamId,
|
||||
owner: ActorAddress,
|
||||
_mode: StreamMode,
|
||||
_remote_node: [u8; 32],
|
||||
}
|
||||
|
||||
struct PendingIncoming {
|
||||
_node_id: [u8; 32],
|
||||
_stream_id: StreamId,
|
||||
_mode: StreamMode,
|
||||
config: StreamConfig,
|
||||
conn: OneShot<iroh::endpoint::Connection>,
|
||||
}
|
||||
|
|
@ -41,8 +34,6 @@ pub struct StreamManager {
|
|||
streams: HashMap<StreamId, StreamState>,
|
||||
pending_incoming: HashMap<StreamId, PendingIncoming>,
|
||||
listeners: HashMap<StreamMode, Vec<ActorAddress>>,
|
||||
#[allow(dead_code)]
|
||||
conn_cache: StreamConnectionCache,
|
||||
endpoint: Endpoint,
|
||||
tokio_handle: tokio::runtime::Handle,
|
||||
runtime: Arc<Runtime>,
|
||||
|
|
@ -59,7 +50,6 @@ impl StreamManager {
|
|||
streams: HashMap::new(),
|
||||
pending_incoming: HashMap::new(),
|
||||
listeners: HashMap::new(),
|
||||
conn_cache: StreamConnectionCache::new(),
|
||||
endpoint,
|
||||
tokio_handle,
|
||||
runtime,
|
||||
|
|
@ -102,10 +92,7 @@ impl StreamManager {
|
|||
self.streams.insert(
|
||||
stream_id,
|
||||
StreamState {
|
||||
_stream_id: stream_id,
|
||||
owner: reply_to,
|
||||
_mode: StreamMode::BlobTransfer,
|
||||
_remote_node: [0; 32],
|
||||
},
|
||||
);
|
||||
let notif = StreamNotification::StreamReady {
|
||||
|
|
@ -154,9 +141,6 @@ impl StreamManager {
|
|||
self.pending_incoming.insert(
|
||||
stream_id,
|
||||
PendingIncoming {
|
||||
_node_id: node_id,
|
||||
_stream_id: stream_id,
|
||||
_mode: mode,
|
||||
config,
|
||||
conn,
|
||||
},
|
||||
|
|
@ -207,10 +191,7 @@ impl StreamManager {
|
|||
self.streams.insert(
|
||||
stream_id,
|
||||
StreamState {
|
||||
_stream_id: stream_id,
|
||||
owner: reply_to,
|
||||
_mode: StreamMode::BlobTransfer,
|
||||
_remote_node: [0; 32],
|
||||
},
|
||||
);
|
||||
let notif = StreamNotification::StreamReady {
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
#![cfg(feature = "formal-verification")]
|
||||
//! Proptest state-machine verification of AuthzEngine.
|
||||
//!
|
||||
//! Drives the auth engine through random sequences of grant/revoke/check/sign
|
||||
|
|
|
|||
|
|
@ -10,8 +10,7 @@ relay = ["iroh", "dep:iroh-relay"]
|
|||
|
||||
[dependencies]
|
||||
swactor = { path = "../..", features = ["serde", "transport"] }
|
||||
ed25519-dalek = { version = "2", features = ["rand_core"] }
|
||||
rand_core = { version = "0.6", features = ["getrandom"] }
|
||||
swactor-transport = { path = "../transport" }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
iroh = { version = "0.96", optional = true }
|
||||
|
|
|
|||
|
|
@ -1,52 +0,0 @@
|
|||
//! Serde-JSON codec for all distribution protocol messages.
|
||||
|
||||
use swactor::transport::{Codec, CodecRegistry};
|
||||
use swactor::Error;
|
||||
|
||||
use crate::messages::*;
|
||||
|
||||
/// JSON codec for distribution protocol messages.
|
||||
///
|
||||
/// Using JSON for simplicity and debuggability. Can be swapped for
|
||||
/// bincode/msgpack in production via the Codec trait.
|
||||
pub struct JsonCodec;
|
||||
|
||||
macro_rules! impl_json_codec {
|
||||
($ty:ty) => {
|
||||
impl Codec<$ty> for JsonCodec {
|
||||
fn encode(&self, msg: &$ty) -> Result<Vec<u8>, Error> {
|
||||
serde_json::to_vec(msg).map_err(|e| Error::from(format!("encode: {e}")))
|
||||
}
|
||||
fn decode(&self, bytes: &[u8]) -> Result<$ty, Error> {
|
||||
serde_json::from_slice(bytes).map_err(|e| Error::from(format!("decode: {e}")))
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl_json_codec!(Ping);
|
||||
impl_json_codec!(Ack);
|
||||
impl_json_codec!(PingReq);
|
||||
impl_json_codec!(JoinRequest);
|
||||
impl_json_codec!(JoinResponse);
|
||||
impl_json_codec!(FindNodeRequest);
|
||||
impl_json_codec!(FindNodeResponse);
|
||||
impl_json_codec!(StoreRequest);
|
||||
impl_json_codec!(FindValueRequest);
|
||||
impl_json_codec!(FindValueResponse);
|
||||
|
||||
/// Build a `CodecRegistry` with all distribution protocol messages registered.
|
||||
pub fn distribution_codec_registry() -> CodecRegistry {
|
||||
let mut cr = CodecRegistry::new();
|
||||
cr.register::<Ping, _>(JsonCodec);
|
||||
cr.register::<Ack, _>(JsonCodec);
|
||||
cr.register::<PingReq, _>(JsonCodec);
|
||||
cr.register::<JoinRequest, _>(JsonCodec);
|
||||
cr.register::<JoinResponse, _>(JsonCodec);
|
||||
cr.register::<FindNodeRequest, _>(JsonCodec);
|
||||
cr.register::<FindNodeResponse, _>(JsonCodec);
|
||||
cr.register::<StoreRequest, _>(JsonCodec);
|
||||
cr.register::<FindValueRequest, _>(JsonCodec);
|
||||
cr.register::<FindValueResponse, _>(JsonCodec);
|
||||
cr
|
||||
}
|
||||
|
|
@ -1,99 +1,8 @@
|
|||
//! Ed25519 cryptographic primitives for distribution.
|
||||
|
||||
use std::fmt;
|
||||
pub use swactor_transport::crypto::{Keypair, Signature, verify};
|
||||
|
||||
use ed25519_dalek::{Signer, Verifier};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::types::{DirectoryEntry, DirectoryEntryPayload, NodeId};
|
||||
|
||||
// ─── Signature ──────────────────────────────────────────────────────────────
|
||||
|
||||
/// An ed25519 signature (64 bytes).
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
pub struct Signature(pub [u8; 64]);
|
||||
|
||||
impl Serialize for Signature {
|
||||
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
serializer.serialize_bytes(&self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for Signature {
|
||||
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
||||
let bytes: Vec<u8> = Deserialize::deserialize(deserializer)?;
|
||||
if bytes.len() != 64 {
|
||||
return Err(serde::de::Error::custom(format!(
|
||||
"expected 64 bytes for Signature, got {}",
|
||||
bytes.len()
|
||||
)));
|
||||
}
|
||||
let mut arr = [0u8; 64];
|
||||
arr.copy_from_slice(&bytes);
|
||||
Ok(Signature(arr))
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Signature {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "Sig(")?;
|
||||
for b in &self.0[..4] {
|
||||
write!(f, "{:02x}", b)?;
|
||||
}
|
||||
write!(f, "\u{2026})")
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Keypair ────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Node identity keypair — wraps ed25519-dalek.
|
||||
pub struct Keypair {
|
||||
inner: ed25519_dalek::SigningKey,
|
||||
}
|
||||
|
||||
impl Keypair {
|
||||
/// Generate a new random keypair.
|
||||
pub fn generate() -> Self {
|
||||
let mut csprng = rand_core::OsRng;
|
||||
Self {
|
||||
inner: ed25519_dalek::SigningKey::generate(&mut csprng),
|
||||
}
|
||||
}
|
||||
|
||||
/// Reconstruct from raw secret key bytes (32 bytes).
|
||||
pub fn from_bytes(secret: &[u8; 32]) -> Self {
|
||||
Self {
|
||||
inner: ed25519_dalek::SigningKey::from_bytes(secret),
|
||||
}
|
||||
}
|
||||
|
||||
/// The public key as a `NodeId`.
|
||||
pub fn node_id(&self) -> NodeId {
|
||||
NodeId(self.inner.verifying_key().to_bytes())
|
||||
}
|
||||
|
||||
/// Raw secret key bytes.
|
||||
pub fn secret_bytes(&self) -> [u8; 32] {
|
||||
self.inner.to_bytes()
|
||||
}
|
||||
|
||||
/// Sign arbitrary bytes.
|
||||
pub fn sign(&self, msg: &[u8]) -> Signature {
|
||||
let sig = self.inner.sign(msg);
|
||||
Signature(sig.to_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Verification ───────────────────────────────────────────────────────────
|
||||
|
||||
/// Verify a signature against a `NodeId` (public key) and message bytes.
|
||||
pub fn verify(node_id: &NodeId, msg: &[u8], sig: &Signature) -> bool {
|
||||
let Ok(vk) = ed25519_dalek::VerifyingKey::from_bytes(&node_id.0) else {
|
||||
return false;
|
||||
};
|
||||
let signature = ed25519_dalek::Signature::from_bytes(&sig.0);
|
||||
vk.verify(msg, &signature).is_ok()
|
||||
}
|
||||
use crate::types::{DirectoryEntry, DirectoryEntryPayload};
|
||||
|
||||
// ─── Directory Entry Helpers ────────────────────────────────────────────────
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
//! iroh-based P2P network driver for `DistributedNode`.
|
||||
//!
|
||||
//! Provides the same driver pattern as `NodeDriver` (TCP), but uses iroh's
|
||||
//! Provides the same driver pattern as `NodeDriver`, but uses iroh's
|
||||
//! QUIC-based peer-to-peer transport with built-in TLS, NAT hole-punching,
|
||||
//! and relay server fallback.
|
||||
//!
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ pub mod types;
|
|||
pub mod crypto;
|
||||
pub mod peer_auth;
|
||||
pub mod messages;
|
||||
pub mod codec;
|
||||
pub mod swim;
|
||||
pub mod kademlia;
|
||||
pub mod cache;
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
//! Protocol messages for SWIM membership and Kademlia directory.
|
||||
//! Protocol messages for SWIM membership and Kademlia directory, plus JSON codec.
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use swactor::actor::ActorAddress;
|
||||
use swactor::transport::NetworkMessage;
|
||||
use swactor::transport::{Codec, CodecRegistry, NetworkMessage};
|
||||
use swactor::Error;
|
||||
|
||||
use crate::types::{DirectoryEntry, MemberState, NodeId, NodeRecord};
|
||||
|
||||
|
|
@ -179,3 +180,51 @@ impl NetworkMessage for FindValueResponse {
|
|||
"swactor_dist::FindValueResponse"
|
||||
}
|
||||
}
|
||||
|
||||
// ─── JSON Codec ─────────────────────────────────────────────────────────────
|
||||
|
||||
/// JSON codec for distribution protocol messages.
|
||||
///
|
||||
/// Using JSON for simplicity and debuggability. Can be swapped for
|
||||
/// bincode/msgpack in production via the Codec trait.
|
||||
pub struct JsonCodec;
|
||||
|
||||
macro_rules! impl_json_codec {
|
||||
($ty:ty) => {
|
||||
impl Codec<$ty> for JsonCodec {
|
||||
fn encode(&self, msg: &$ty) -> Result<Vec<u8>, Error> {
|
||||
serde_json::to_vec(msg).map_err(|e| Error::from(format!("encode: {e}")))
|
||||
}
|
||||
fn decode(&self, bytes: &[u8]) -> Result<$ty, Error> {
|
||||
serde_json::from_slice(bytes).map_err(|e| Error::from(format!("decode: {e}")))
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl_json_codec!(Ping);
|
||||
impl_json_codec!(Ack);
|
||||
impl_json_codec!(PingReq);
|
||||
impl_json_codec!(JoinRequest);
|
||||
impl_json_codec!(JoinResponse);
|
||||
impl_json_codec!(FindNodeRequest);
|
||||
impl_json_codec!(FindNodeResponse);
|
||||
impl_json_codec!(StoreRequest);
|
||||
impl_json_codec!(FindValueRequest);
|
||||
impl_json_codec!(FindValueResponse);
|
||||
|
||||
/// Build a `CodecRegistry` with all distribution protocol messages registered.
|
||||
pub fn distribution_codec_registry() -> CodecRegistry {
|
||||
let mut cr = CodecRegistry::new();
|
||||
cr.register::<Ping, _>(JsonCodec);
|
||||
cr.register::<Ack, _>(JsonCodec);
|
||||
cr.register::<PingReq, _>(JsonCodec);
|
||||
cr.register::<JoinRequest, _>(JsonCodec);
|
||||
cr.register::<JoinResponse, _>(JsonCodec);
|
||||
cr.register::<FindNodeRequest, _>(JsonCodec);
|
||||
cr.register::<FindNodeResponse, _>(JsonCodec);
|
||||
cr.register::<StoreRequest, _>(JsonCodec);
|
||||
cr.register::<FindValueRequest, _>(JsonCodec);
|
||||
cr.register::<FindValueResponse, _>(JsonCodec);
|
||||
cr
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
use distribution::codec::distribution_codec_registry;
|
||||
use distribution::messages::*;
|
||||
use distribution::types::NodeId;
|
||||
|
||||
|
|
|
|||
|
|
@ -3,11 +3,10 @@ use std::sync::{Arc, OnceLock};
|
|||
use swactor::actor::{ActorAddress, ActorInterface, Ctx};
|
||||
|
||||
use crate::action::ProcessAction;
|
||||
use crate::driver::ProcessDriver;
|
||||
use crate::event::ProcessEvent;
|
||||
use crate::message::{ProcessCommand, ProcessNotification};
|
||||
use crate::session::ProcessSession;
|
||||
use crate::waker::ProcessWaker;
|
||||
use crate::types::{ProcessDriver, ProcessWaker};
|
||||
|
||||
/// Actor wrapper around a `ProcessSession` and its driver.
|
||||
///
|
||||
|
|
|
|||
|
|
@ -1,14 +0,0 @@
|
|||
use crate::action::ProcessAction;
|
||||
use crate::event::ProcessEvent;
|
||||
|
||||
/// Abstraction over the mechanism that actually runs a process.
|
||||
///
|
||||
/// Implementations translate `ProcessAction` commands into real I/O (or mock I/O)
|
||||
/// and produce `ProcessEvent`s by polling for state changes.
|
||||
pub trait ProcessDriver: Send {
|
||||
/// Execute an action (spawn, write stdin, send signal, etc.).
|
||||
fn execute(&mut self, action: ProcessAction);
|
||||
|
||||
/// Poll for new events from the underlying process.
|
||||
fn poll(&mut self) -> Vec<ProcessEvent>;
|
||||
}
|
||||
|
|
@ -1,18 +1,13 @@
|
|||
pub mod action;
|
||||
pub mod actor;
|
||||
pub mod driver;
|
||||
pub mod event;
|
||||
pub mod local;
|
||||
pub mod message;
|
||||
pub mod mock;
|
||||
pub mod pipeline;
|
||||
pub mod pipeline_types;
|
||||
pub mod queue;
|
||||
pub mod session;
|
||||
pub mod spawn;
|
||||
pub mod subscriber;
|
||||
pub mod types;
|
||||
pub mod waker;
|
||||
pub mod yaml;
|
||||
|
||||
#[cfg(feature = "ssh")]
|
||||
|
|
@ -20,17 +15,17 @@ pub mod ssh;
|
|||
|
||||
pub use action::{OutputStream, ProcessAction};
|
||||
pub use actor::ProcessActor;
|
||||
pub use driver::ProcessDriver;
|
||||
pub use event::ProcessEvent;
|
||||
pub use local::LocalDriver;
|
||||
pub use message::{ProcessCommand, ProcessNotification};
|
||||
pub use mock::MockDriver;
|
||||
pub use queue::EventQueue;
|
||||
pub use pipeline::{
|
||||
JobComplete, JobDefinition, JobFailure, JobId, JobProgress, JobStatus, JobSuccess,
|
||||
LocalPipelineConfig, LocalStartJob, PipelineId, PipelineStatus,
|
||||
};
|
||||
pub use session::{ProcessSession, ProcessState};
|
||||
pub use spawn::{spawn_local_process, spawn_process};
|
||||
pub use subscriber::SubscriberSet;
|
||||
pub use types::{ExitStatus, FlowControl, ProcessError, ProcessMode, ProcessSpec, PtySize, Signal};
|
||||
pub use waker::ProcessWaker;
|
||||
pub use types::{EventQueue, ExitStatus, FlowControl, ProcessDriver, ProcessError, ProcessMode, ProcessSpec, ProcessWaker, PtySize, Signal};
|
||||
|
||||
#[cfg(feature = "ssh")]
|
||||
pub use ssh::{SshConfig, SshDriver};
|
||||
|
|
|
|||
|
|
@ -1,18 +1,123 @@
|
|||
mod pipes;
|
||||
mod signal;
|
||||
mod wait;
|
||||
|
||||
use std::io::Write;
|
||||
use std::io::{Read, Write};
|
||||
use std::process::{Child, ChildStdin, Command, Stdio};
|
||||
use std::sync::{Arc, OnceLock};
|
||||
use std::thread::{self, JoinHandle};
|
||||
|
||||
use crate::action::ProcessAction;
|
||||
use crate::driver::ProcessDriver;
|
||||
use crate::types::ProcessDriver;
|
||||
use crate::event::ProcessEvent;
|
||||
use crate::queue::EventQueue;
|
||||
use crate::types::ProcessSpec;
|
||||
use crate::waker::ProcessWaker;
|
||||
use crate::types::EventQueue;
|
||||
use crate::types::{ExitStatus, ProcessSpec, Signal};
|
||||
use crate::types::ProcessWaker;
|
||||
|
||||
// ─── Signal ────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Map a `Signal` enum variant to the corresponding libc signal constant.
|
||||
fn signal_to_libc(signal: Signal) -> libc::c_int {
|
||||
match signal {
|
||||
Signal::Terminate => libc::SIGTERM,
|
||||
Signal::Kill => libc::SIGKILL,
|
||||
Signal::Hangup => libc::SIGHUP,
|
||||
Signal::Interrupt => libc::SIGINT,
|
||||
Signal::Other(n) => n,
|
||||
}
|
||||
}
|
||||
|
||||
/// Send a signal to a process by PID. Returns `Ok(())` on success.
|
||||
fn send_signal(pid: u32, signal: Signal) -> Result<(), String> {
|
||||
let sig = signal_to_libc(signal);
|
||||
// Safety: kill() is safe to call with any pid/signal combo;
|
||||
// it returns -1 on error which we check.
|
||||
let ret = unsafe { libc::kill(pid as libc::pid_t, sig) };
|
||||
if ret == 0 {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(format!(
|
||||
"kill({}, {}) failed: {}",
|
||||
pid,
|
||||
sig,
|
||||
std::io::Error::last_os_error()
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Pipes ─────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Read from a pipe in a loop, pushing events to the queue and waking the actor.
|
||||
///
|
||||
/// Runs in a background thread. Exits when the pipe reaches EOF or errors.
|
||||
fn read_pipe(
|
||||
mut pipe: impl Read + Send + 'static,
|
||||
is_stderr: bool,
|
||||
queue: EventQueue,
|
||||
waker: Arc<OnceLock<ProcessWaker>>,
|
||||
) {
|
||||
let mut buf = [0u8; 8192];
|
||||
loop {
|
||||
match pipe.read(&mut buf) {
|
||||
Ok(0) => break, // EOF
|
||||
Ok(n) => {
|
||||
queue.push(ProcessEvent::OutputReceived {
|
||||
data: buf[..n].to_vec(),
|
||||
is_stderr,
|
||||
});
|
||||
if let Some(w) = waker.get() {
|
||||
w.wake();
|
||||
}
|
||||
}
|
||||
Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Wait ──────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Wait for a child process to exit, then push the appropriate event.
|
||||
///
|
||||
/// Runs in a background thread. Uses `libc::waitpid` for accurate exit status.
|
||||
/// After waitpid returns, joins the pipe reader threads so all buffered
|
||||
/// stdout/stderr is drained before the `Exited` event is enqueued.
|
||||
fn wait_for_exit(
|
||||
pid: u32,
|
||||
reader_threads: Vec<JoinHandle<()>>,
|
||||
queue: EventQueue,
|
||||
waker: Arc<OnceLock<ProcessWaker>>,
|
||||
) {
|
||||
let mut status: libc::c_int = 0;
|
||||
let ret = unsafe { libc::waitpid(pid as libc::pid_t, &mut status, 0) };
|
||||
|
||||
let exit_status = if ret < 0 {
|
||||
ExitStatus::Unknown
|
||||
} else {
|
||||
decode_wait_status(status)
|
||||
};
|
||||
|
||||
// Wait for pipe readers to finish draining all output before signaling exit.
|
||||
// Once the process exits, its pipe ends close, so readers will hit EOF shortly.
|
||||
for handle in reader_threads {
|
||||
let _ = handle.join();
|
||||
}
|
||||
|
||||
queue.push(ProcessEvent::Exited {
|
||||
status: exit_status,
|
||||
});
|
||||
if let Some(w) = waker.get() {
|
||||
w.wake();
|
||||
}
|
||||
}
|
||||
|
||||
fn decode_wait_status(status: libc::c_int) -> ExitStatus {
|
||||
if libc::WIFEXITED(status) {
|
||||
ExitStatus::Code(libc::WEXITSTATUS(status))
|
||||
} else if libc::WIFSIGNALED(status) {
|
||||
ExitStatus::Signal(libc::WTERMSIG(status))
|
||||
} else {
|
||||
ExitStatus::Unknown
|
||||
}
|
||||
}
|
||||
|
||||
// ─── LocalDriver ───────────────────────────────────────────────────────────
|
||||
|
||||
/// A `ProcessDriver` that spawns real OS subprocesses via `std::process::Command`.
|
||||
///
|
||||
|
|
@ -23,7 +128,6 @@ pub struct LocalDriver {
|
|||
waker_slot: Arc<OnceLock<ProcessWaker>>,
|
||||
child: Option<Child>,
|
||||
stdin: Option<ChildStdin>,
|
||||
_reader_threads: Vec<JoinHandle<()>>,
|
||||
_wait_thread: Option<JoinHandle<()>>,
|
||||
}
|
||||
|
||||
|
|
@ -34,7 +138,6 @@ impl LocalDriver {
|
|||
waker_slot,
|
||||
child: None,
|
||||
stdin: None,
|
||||
_reader_threads: Vec::new(),
|
||||
_wait_thread: None,
|
||||
}
|
||||
}
|
||||
|
|
@ -60,13 +163,14 @@ impl LocalDriver {
|
|||
self.stdin = child.stdin.take();
|
||||
|
||||
// Spawn stdout reader thread
|
||||
let mut reader_threads = Vec::new();
|
||||
if let Some(stdout) = child.stdout.take() {
|
||||
let queue = self.queue.clone();
|
||||
let waker = self.waker_slot.clone();
|
||||
self._reader_threads.push(
|
||||
reader_threads.push(
|
||||
thread::Builder::new()
|
||||
.name(format!("proc-{}-stdout", pid))
|
||||
.spawn(move || pipes::read_pipe(stdout, false, queue, waker))
|
||||
.spawn(move || read_pipe(stdout, false, queue, waker))
|
||||
.expect("failed to spawn stdout reader"),
|
||||
);
|
||||
}
|
||||
|
|
@ -75,21 +179,22 @@ impl LocalDriver {
|
|||
if let Some(stderr) = child.stderr.take() {
|
||||
let queue = self.queue.clone();
|
||||
let waker = self.waker_slot.clone();
|
||||
self._reader_threads.push(
|
||||
reader_threads.push(
|
||||
thread::Builder::new()
|
||||
.name(format!("proc-{}-stderr", pid))
|
||||
.spawn(move || pipes::read_pipe(stderr, true, queue, waker))
|
||||
.spawn(move || read_pipe(stderr, true, queue, waker))
|
||||
.expect("failed to spawn stderr reader"),
|
||||
);
|
||||
}
|
||||
|
||||
// Spawn wait thread
|
||||
// Spawn wait thread — it joins the reader threads before pushing Exited,
|
||||
// ensuring all output is drained before the exit event.
|
||||
let queue = self.queue.clone();
|
||||
let waker = self.waker_slot.clone();
|
||||
self._wait_thread = Some(
|
||||
thread::Builder::new()
|
||||
.name(format!("proc-{}-wait", pid))
|
||||
.spawn(move || wait::wait_for_exit(pid, queue, waker))
|
||||
.spawn(move || wait_for_exit(pid, reader_threads, queue, waker))
|
||||
.expect("failed to spawn wait thread"),
|
||||
);
|
||||
|
||||
|
|
@ -130,7 +235,7 @@ impl ProcessDriver for LocalDriver {
|
|||
ProcessAction::SendSignal { signal } => {
|
||||
if let Some(ref child) = self.child {
|
||||
let pid = child.id();
|
||||
match signal::send_signal(pid, signal) {
|
||||
match send_signal(pid, signal) {
|
||||
Ok(()) => {
|
||||
self.queue.push(ProcessEvent::SignalSent);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,34 +0,0 @@
|
|||
use std::io::Read;
|
||||
use std::sync::{Arc, OnceLock};
|
||||
|
||||
use crate::event::ProcessEvent;
|
||||
use crate::queue::EventQueue;
|
||||
use crate::waker::ProcessWaker;
|
||||
|
||||
/// Read from a pipe in a loop, pushing events to the queue and waking the actor.
|
||||
///
|
||||
/// Runs in a background thread. Exits when the pipe reaches EOF or errors.
|
||||
pub(crate) fn read_pipe(
|
||||
mut pipe: impl Read + Send + 'static,
|
||||
is_stderr: bool,
|
||||
queue: EventQueue,
|
||||
waker: Arc<OnceLock<ProcessWaker>>,
|
||||
) {
|
||||
let mut buf = [0u8; 8192];
|
||||
loop {
|
||||
match pipe.read(&mut buf) {
|
||||
Ok(0) => break, // EOF
|
||||
Ok(n) => {
|
||||
queue.push(ProcessEvent::OutputReceived {
|
||||
data: buf[..n].to_vec(),
|
||||
is_stderr,
|
||||
});
|
||||
if let Some(w) = waker.get() {
|
||||
w.wake();
|
||||
}
|
||||
}
|
||||
Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,30 +0,0 @@
|
|||
use crate::types::Signal;
|
||||
|
||||
/// Map a `Signal` enum variant to the corresponding libc signal constant.
|
||||
pub(crate) fn signal_to_libc(signal: Signal) -> libc::c_int {
|
||||
match signal {
|
||||
Signal::Terminate => libc::SIGTERM,
|
||||
Signal::Kill => libc::SIGKILL,
|
||||
Signal::Hangup => libc::SIGHUP,
|
||||
Signal::Interrupt => libc::SIGINT,
|
||||
Signal::Other(n) => n,
|
||||
}
|
||||
}
|
||||
|
||||
/// Send a signal to a process by PID. Returns `Ok(())` on success.
|
||||
pub(crate) fn send_signal(pid: u32, signal: Signal) -> Result<(), String> {
|
||||
let sig = signal_to_libc(signal);
|
||||
// Safety: kill() is safe to call with any pid/signal combo;
|
||||
// it returns -1 on error which we check.
|
||||
let ret = unsafe { libc::kill(pid as libc::pid_t, sig) };
|
||||
if ret == 0 {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(format!(
|
||||
"kill({}, {}) failed: {}",
|
||||
pid,
|
||||
sig,
|
||||
std::io::Error::last_os_error()
|
||||
))
|
||||
}
|
||||
}
|
||||
|
|
@ -1,41 +0,0 @@
|
|||
use std::sync::{Arc, OnceLock};
|
||||
|
||||
use crate::event::ProcessEvent;
|
||||
use crate::queue::EventQueue;
|
||||
use crate::types::ExitStatus;
|
||||
use crate::waker::ProcessWaker;
|
||||
|
||||
/// Wait for a child process to exit, then push the appropriate event.
|
||||
///
|
||||
/// Runs in a background thread. Uses `libc::waitpid` for accurate exit status.
|
||||
pub(crate) fn wait_for_exit(
|
||||
pid: u32,
|
||||
queue: EventQueue,
|
||||
waker: Arc<OnceLock<ProcessWaker>>,
|
||||
) {
|
||||
let mut status: libc::c_int = 0;
|
||||
let ret = unsafe { libc::waitpid(pid as libc::pid_t, &mut status, 0) };
|
||||
|
||||
let exit_status = if ret < 0 {
|
||||
ExitStatus::Unknown
|
||||
} else {
|
||||
decode_wait_status(status)
|
||||
};
|
||||
|
||||
queue.push(ProcessEvent::Exited {
|
||||
status: exit_status,
|
||||
});
|
||||
if let Some(w) = waker.get() {
|
||||
w.wake();
|
||||
}
|
||||
}
|
||||
|
||||
fn decode_wait_status(status: libc::c_int) -> ExitStatus {
|
||||
if libc::WIFEXITED(status) {
|
||||
ExitStatus::Code(libc::WEXITSTATUS(status))
|
||||
} else if libc::WIFSIGNALED(status) {
|
||||
ExitStatus::Signal(libc::WTERMSIG(status))
|
||||
} else {
|
||||
ExitStatus::Unknown
|
||||
}
|
||||
}
|
||||
|
|
@ -1,8 +1,8 @@
|
|||
use std::collections::VecDeque;
|
||||
|
||||
use crate::action::ProcessAction;
|
||||
use crate::driver::ProcessDriver;
|
||||
use crate::event::ProcessEvent;
|
||||
use crate::types::ProcessDriver;
|
||||
|
||||
/// A test-oriented driver that records executed actions and lets you inject events.
|
||||
pub struct MockDriver {
|
||||
|
|
|
|||
|
|
@ -1,8 +1,153 @@
|
|||
//! Pipeline resolution, DAG execution logic, and job ordering.
|
||||
//! Pipeline resolution, DAG execution logic, job ordering, and type definitions.
|
||||
|
||||
use std::collections::{HashMap, HashSet, VecDeque};
|
||||
use std::fmt;
|
||||
|
||||
use crate::pipeline_types::{JobDefinition, JobId, JobStatus, PipelineId, PipelineStatus};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
// ─── Core Identifiers ───────────────────────────────────────────────────────
|
||||
|
||||
/// Unique identifier for a pipeline execution.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
pub struct PipelineId(pub u64);
|
||||
|
||||
impl fmt::Display for PipelineId {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "pipeline-{}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
/// Unique identifier for a job within a pipeline.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
pub struct JobId {
|
||||
pub pipeline_id: PipelineId,
|
||||
pub job_name: String,
|
||||
}
|
||||
|
||||
impl fmt::Display for JobId {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}/{}", self.pipeline_id, self.job_name)
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Job Status ─────────────────────────────────────────────────────────────
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum JobStatus {
|
||||
Pending,
|
||||
WaitingForProvisioner,
|
||||
Provisioning,
|
||||
Running,
|
||||
Passed,
|
||||
Failed { reason: String },
|
||||
Skipped,
|
||||
Interrupted,
|
||||
}
|
||||
|
||||
impl JobStatus {
|
||||
pub fn is_terminal(&self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
JobStatus::Passed | JobStatus::Failed { .. } | JobStatus::Skipped | JobStatus::Interrupted
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Pipeline Status ────────────────────────────────────────────────────────
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum PipelineStatus {
|
||||
Pending,
|
||||
Running,
|
||||
Passed,
|
||||
Failed,
|
||||
Error { reason: String },
|
||||
}
|
||||
|
||||
impl PipelineStatus {
|
||||
pub fn is_terminal(&self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
PipelineStatus::Passed | PipelineStatus::Failed | PipelineStatus::Error { .. }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Job Definition ─────────────────────────────────────────────────────────
|
||||
|
||||
/// A parsed job from a pipeline YAML file.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct JobDefinition {
|
||||
pub name: String,
|
||||
pub run: Vec<String>,
|
||||
pub needs: Vec<String>,
|
||||
pub timeout_secs: u64,
|
||||
pub docker: bool,
|
||||
pub artifacts: Vec<String>,
|
||||
pub env: HashMap<String, String>,
|
||||
}
|
||||
|
||||
// ─── Job Execution Results ──────────────────────────────────────────────────
|
||||
|
||||
/// Streamed output from a running job.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct JobProgress {
|
||||
pub job_id: JobId,
|
||||
pub output_line: String,
|
||||
}
|
||||
|
||||
/// Final result of a job execution.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct JobComplete {
|
||||
pub job_id: JobId,
|
||||
pub result: Result<JobSuccess, JobFailure>,
|
||||
pub artifacts: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct JobSuccess;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub enum JobFailure {
|
||||
CommandFailed { exit_code: i32, last_lines: Vec<String> },
|
||||
SshError(String),
|
||||
ExecError(String),
|
||||
Timeout,
|
||||
Interrupted,
|
||||
}
|
||||
|
||||
impl fmt::Display for JobFailure {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
JobFailure::CommandFailed { exit_code, .. } => {
|
||||
write!(f, "command exited with code {exit_code}")
|
||||
}
|
||||
JobFailure::SshError(msg) => write!(f, "SSH error: {msg}"),
|
||||
JobFailure::ExecError(msg) => write!(f, "exec error: {msg}"),
|
||||
JobFailure::Timeout => write!(f, "job timed out"),
|
||||
JobFailure::Interrupted => write!(f, "spot instance interrupted"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Local Pipeline Types ───────────────────────────────────────────────────
|
||||
|
||||
/// Job execution request for a local pipeline runner.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct LocalStartJob {
|
||||
pub job_id: JobId,
|
||||
pub work_dir: String,
|
||||
pub job_def: JobDefinition,
|
||||
pub env_overrides: HashMap<String, String>,
|
||||
}
|
||||
|
||||
/// Configuration for a local pipeline runner.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct LocalPipelineConfig {
|
||||
pub repo_url: String,
|
||||
pub work_dir: String,
|
||||
pub pipeline_yaml_path: String,
|
||||
}
|
||||
|
||||
/// A pipeline execution: tracks the DAG of jobs and their statuses.
|
||||
#[derive(Debug, Clone)]
|
||||
|
|
@ -240,7 +385,7 @@ mod tests {
|
|||
use std::collections::HashMap;
|
||||
|
||||
use super::*;
|
||||
use crate::pipeline_types::JobDefinition;
|
||||
use super::JobDefinition;
|
||||
|
||||
fn make_job(name: &str, needs: &[&str]) -> JobDefinition {
|
||||
JobDefinition {
|
||||
|
|
|
|||
|
|
@ -1,153 +0,0 @@
|
|||
//! Core pipeline types extracted from the CI crate.
|
||||
//!
|
||||
//! General-purpose pipeline execution primitives: identifiers, statuses,
|
||||
//! job definitions, and result types. No Forgejo-specific concerns.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::fmt;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
// ─── Core Identifiers ───────────────────────────────────────────────────────
|
||||
|
||||
/// Unique identifier for a pipeline execution.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
pub struct PipelineId(pub u64);
|
||||
|
||||
impl fmt::Display for PipelineId {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "pipeline-{}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
/// Unique identifier for a job within a pipeline.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
pub struct JobId {
|
||||
pub pipeline_id: PipelineId,
|
||||
pub job_name: String,
|
||||
}
|
||||
|
||||
impl fmt::Display for JobId {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}/{}", self.pipeline_id, self.job_name)
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Job Status ─────────────────────────────────────────────────────────────
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum JobStatus {
|
||||
Pending,
|
||||
WaitingForProvisioner,
|
||||
Provisioning,
|
||||
Running,
|
||||
Passed,
|
||||
Failed { reason: String },
|
||||
Skipped,
|
||||
Interrupted,
|
||||
}
|
||||
|
||||
impl JobStatus {
|
||||
pub fn is_terminal(&self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
JobStatus::Passed | JobStatus::Failed { .. } | JobStatus::Skipped | JobStatus::Interrupted
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Pipeline Status ────────────────────────────────────────────────────────
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum PipelineStatus {
|
||||
Pending,
|
||||
Running,
|
||||
Passed,
|
||||
Failed,
|
||||
Error { reason: String },
|
||||
}
|
||||
|
||||
impl PipelineStatus {
|
||||
pub fn is_terminal(&self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
PipelineStatus::Passed | PipelineStatus::Failed | PipelineStatus::Error { .. }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Job Definition ─────────────────────────────────────────────────────────
|
||||
|
||||
/// A parsed job from a pipeline YAML file.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct JobDefinition {
|
||||
pub name: String,
|
||||
pub run: Vec<String>,
|
||||
pub needs: Vec<String>,
|
||||
pub timeout_secs: u64,
|
||||
pub docker: bool,
|
||||
pub artifacts: Vec<String>,
|
||||
pub env: HashMap<String, String>,
|
||||
}
|
||||
|
||||
// ─── Job Execution Results ──────────────────────────────────────────────────
|
||||
|
||||
/// Streamed output from a running job.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct JobProgress {
|
||||
pub job_id: JobId,
|
||||
pub output_line: String,
|
||||
}
|
||||
|
||||
/// Final result of a job execution.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct JobComplete {
|
||||
pub job_id: JobId,
|
||||
pub result: Result<JobSuccess, JobFailure>,
|
||||
pub artifacts: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct JobSuccess;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub enum JobFailure {
|
||||
CommandFailed { exit_code: i32, last_lines: Vec<String> },
|
||||
SshError(String),
|
||||
ExecError(String),
|
||||
Timeout,
|
||||
Interrupted,
|
||||
}
|
||||
|
||||
impl fmt::Display for JobFailure {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
JobFailure::CommandFailed { exit_code, .. } => {
|
||||
write!(f, "command exited with code {exit_code}")
|
||||
}
|
||||
JobFailure::SshError(msg) => write!(f, "SSH error: {msg}"),
|
||||
JobFailure::ExecError(msg) => write!(f, "exec error: {msg}"),
|
||||
JobFailure::Timeout => write!(f, "job timed out"),
|
||||
JobFailure::Interrupted => write!(f, "spot instance interrupted"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Local Pipeline Types ───────────────────────────────────────────────────
|
||||
|
||||
/// Job execution request for a local pipeline runner.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct LocalStartJob {
|
||||
pub job_id: JobId,
|
||||
pub work_dir: String,
|
||||
pub job_def: JobDefinition,
|
||||
pub env_overrides: HashMap<String, String>,
|
||||
}
|
||||
|
||||
/// Configuration for a local pipeline runner.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct LocalPipelineConfig {
|
||||
pub repo_url: String,
|
||||
pub work_dir: String,
|
||||
pub pipeline_yaml_path: String,
|
||||
}
|
||||
|
|
@ -1,42 +0,0 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use crossbeam_queue::SegQueue;
|
||||
|
||||
use crate::event::ProcessEvent;
|
||||
|
||||
/// Thread-safe queue for buffering process events from I/O threads.
|
||||
///
|
||||
/// Cloneable via inner `Arc` — I/O threads push events, the driver's
|
||||
/// `poll()` drains them.
|
||||
#[derive(Clone)]
|
||||
pub struct EventQueue {
|
||||
inner: Arc<SegQueue<ProcessEvent>>,
|
||||
}
|
||||
|
||||
impl EventQueue {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
inner: Arc::new(SegQueue::new()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Push an event (called from I/O threads).
|
||||
pub fn push(&self, event: ProcessEvent) {
|
||||
self.inner.push(event);
|
||||
}
|
||||
|
||||
/// Drain all pending events (called from driver's `poll()`).
|
||||
pub fn drain(&self) -> Vec<ProcessEvent> {
|
||||
let mut events = Vec::new();
|
||||
while let Some(event) = self.inner.pop() {
|
||||
events.push(event);
|
||||
}
|
||||
events
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for EventQueue {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
|
@ -1,10 +1,47 @@
|
|||
use std::collections::VecDeque;
|
||||
|
||||
use swactor::actor::ActorAddress;
|
||||
|
||||
use crate::action::{OutputStream, ProcessAction};
|
||||
use crate::event::ProcessEvent;
|
||||
use crate::subscriber::SubscriberSet;
|
||||
use crate::types::{ExitStatus, FlowControl, ProcessError, ProcessMode, ProcessSpec, Signal};
|
||||
|
||||
// ─── SubscriberSet ─────────────────────────────────────────────────────────
|
||||
|
||||
/// A deduplicated collection of subscriber addresses.
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) struct SubscriberSet {
|
||||
inner: Vec<ActorAddress>,
|
||||
}
|
||||
|
||||
impl SubscriberSet {
|
||||
pub(crate) fn new() -> Self {
|
||||
Self { inner: Vec::new() }
|
||||
}
|
||||
|
||||
/// Add an address. No-op if already present.
|
||||
pub(crate) fn add(&mut self, address: ActorAddress) {
|
||||
if !self.inner.contains(&address) {
|
||||
self.inner.push(address);
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove an address. No-op if not present.
|
||||
pub(crate) fn remove(&mut self, address: &ActorAddress) {
|
||||
self.inner.retain(|a| a != address);
|
||||
}
|
||||
|
||||
/// Snapshot of current subscribers.
|
||||
pub(crate) fn snapshot(&self) -> Vec<ActorAddress> {
|
||||
self.inner.clone()
|
||||
}
|
||||
|
||||
/// Number of subscribers.
|
||||
pub(crate) fn count(&self) -> usize {
|
||||
self.inner.len()
|
||||
}
|
||||
}
|
||||
|
||||
/// The lifecycle states of a process session.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum ProcessState {
|
||||
|
|
|
|||
|
|
@ -5,13 +5,10 @@ use swactor::runtime::ExternalSender;
|
|||
use swactor::Error;
|
||||
|
||||
use crate::actor::ProcessActor;
|
||||
use crate::driver::ProcessDriver;
|
||||
use crate::local::LocalDriver;
|
||||
use crate::message::ProcessCommand;
|
||||
use crate::queue::EventQueue;
|
||||
use crate::session::ProcessSession;
|
||||
use crate::types::ProcessSpec;
|
||||
use crate::waker::ProcessWaker;
|
||||
use crate::types::{EventQueue, ProcessDriver, ProcessSpec, ProcessWaker};
|
||||
|
||||
/// Spawn a process actor using the real `LocalDriver` (OS subprocess).
|
||||
///
|
||||
|
|
|
|||
|
|
@ -1,17 +0,0 @@
|
|||
use russh::client;
|
||||
use russh_keys::key::PublicKey;
|
||||
|
||||
/// Minimal SSH client handler that accepts all host keys.
|
||||
pub(super) struct SshHandler;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl client::Handler for SshHandler {
|
||||
type Error = russh::Error;
|
||||
|
||||
async fn check_server_key(
|
||||
&mut self,
|
||||
_server_public_key: &PublicKey,
|
||||
) -> Result<bool, Self::Error> {
|
||||
Ok(true)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,4 @@
|
|||
pub mod config;
|
||||
mod handler;
|
||||
mod task;
|
||||
|
||||
use std::sync::{Arc, OnceLock};
|
||||
|
|
@ -7,10 +6,8 @@ use std::sync::{Arc, OnceLock};
|
|||
use tokio::sync::mpsc;
|
||||
|
||||
use crate::action::ProcessAction;
|
||||
use crate::driver::ProcessDriver;
|
||||
use crate::event::ProcessEvent;
|
||||
use crate::queue::EventQueue;
|
||||
use crate::waker::ProcessWaker;
|
||||
use crate::types::{EventQueue, ProcessDriver, ProcessWaker};
|
||||
|
||||
pub use config::SshConfig;
|
||||
use task::SshCommand;
|
||||
|
|
|
|||
|
|
@ -1,17 +1,33 @@
|
|||
use std::sync::{Arc, OnceLock};
|
||||
use std::time::Duration;
|
||||
|
||||
use russh::client;
|
||||
use russh::{ChannelMsg, Sig};
|
||||
use russh_keys::key::PublicKey;
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::time::{Instant, sleep_until};
|
||||
|
||||
use crate::event::ProcessEvent;
|
||||
use crate::queue::EventQueue;
|
||||
use crate::types::EventQueue;
|
||||
use crate::types::{ExitStatus, ProcessMode, ProcessSpec, Signal};
|
||||
use crate::waker::ProcessWaker;
|
||||
use crate::types::ProcessWaker;
|
||||
|
||||
use super::config::SshConfig;
|
||||
use super::handler::SshHandler;
|
||||
|
||||
/// Minimal SSH client handler that accepts all host keys.
|
||||
pub(super) struct SshHandler;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl client::Handler for SshHandler {
|
||||
type Error = russh::Error;
|
||||
|
||||
async fn check_server_key(
|
||||
&mut self,
|
||||
_server_public_key: &PublicKey,
|
||||
) -> Result<bool, Self::Error> {
|
||||
Ok(true)
|
||||
}
|
||||
}
|
||||
|
||||
/// Commands sent from the SshDriver to the background task.
|
||||
pub(super) enum SshCommand {
|
||||
|
|
|
|||
|
|
@ -1,41 +0,0 @@
|
|||
use swactor::actor::ActorAddress;
|
||||
|
||||
/// A deduplicated collection of subscriber addresses.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct SubscriberSet {
|
||||
inner: Vec<ActorAddress>,
|
||||
}
|
||||
|
||||
impl SubscriberSet {
|
||||
pub fn new() -> Self {
|
||||
Self { inner: Vec::new() }
|
||||
}
|
||||
|
||||
/// Add an address. No-op if already present.
|
||||
pub fn add(&mut self, address: ActorAddress) {
|
||||
if !self.inner.contains(&address) {
|
||||
self.inner.push(address);
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove an address. No-op if not present.
|
||||
pub fn remove(&mut self, address: &ActorAddress) {
|
||||
self.inner.retain(|a| a != address);
|
||||
}
|
||||
|
||||
/// Snapshot of current subscribers.
|
||||
pub fn snapshot(&self) -> Vec<ActorAddress> {
|
||||
self.inner.clone()
|
||||
}
|
||||
|
||||
/// Number of subscribers.
|
||||
pub fn count(&self) -> usize {
|
||||
self.inner.len()
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for SubscriberSet {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,12 @@
|
|||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use crossbeam_queue::SegQueue;
|
||||
|
||||
use crate::action::ProcessAction;
|
||||
use crate::event::ProcessEvent;
|
||||
|
||||
/// Describes how to spawn a process.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ProcessSpec {
|
||||
|
|
@ -65,3 +71,82 @@ pub struct FlowControl {
|
|||
pub pending_stdin_bytes: usize,
|
||||
}
|
||||
|
||||
// ─── ProcessDriver ─────────────────────────────────────────────────────────
|
||||
|
||||
/// Abstraction over the mechanism that actually runs a process.
|
||||
///
|
||||
/// Implementations translate `ProcessAction` commands into real I/O (or mock I/O)
|
||||
/// and produce `ProcessEvent`s by polling for state changes.
|
||||
pub trait ProcessDriver: Send {
|
||||
/// Execute an action (spawn, write stdin, send signal, etc.).
|
||||
fn execute(&mut self, action: ProcessAction);
|
||||
|
||||
/// Poll for new events from the underlying process.
|
||||
fn poll(&mut self) -> Vec<ProcessEvent>;
|
||||
}
|
||||
|
||||
// ─── ProcessWaker ──────────────────────────────────────────────────────────
|
||||
|
||||
/// A handle that I/O threads use to wake the owning actor.
|
||||
///
|
||||
/// Constructed with a closure that sends a `ProcessCommand::PollTick`
|
||||
/// to the actor via `ExternalSender`. Thread-safe and cloneable.
|
||||
#[derive(Clone)]
|
||||
pub struct ProcessWaker(Arc<dyn Fn() + Send + Sync>);
|
||||
|
||||
impl std::fmt::Debug for ProcessWaker {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("ProcessWaker").finish_non_exhaustive()
|
||||
}
|
||||
}
|
||||
|
||||
impl ProcessWaker {
|
||||
pub fn new(f: impl Fn() + Send + Sync + 'static) -> Self {
|
||||
Self(Arc::new(f))
|
||||
}
|
||||
|
||||
/// Wake the owning actor so it drains pending events.
|
||||
pub fn wake(&self) {
|
||||
(self.0)();
|
||||
}
|
||||
}
|
||||
|
||||
// ─── EventQueue ────────────────────────────────────────────────────────────
|
||||
|
||||
/// Thread-safe queue for buffering process events from I/O threads.
|
||||
///
|
||||
/// Cloneable via inner `Arc` — I/O threads push events, the driver's
|
||||
/// `poll()` drains them.
|
||||
#[derive(Clone)]
|
||||
pub struct EventQueue {
|
||||
inner: Arc<SegQueue<ProcessEvent>>,
|
||||
}
|
||||
|
||||
impl EventQueue {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
inner: Arc::new(SegQueue::new()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Push an event (called from I/O threads).
|
||||
pub fn push(&self, event: ProcessEvent) {
|
||||
self.inner.push(event);
|
||||
}
|
||||
|
||||
/// Drain all pending events (called from driver's `poll()`).
|
||||
pub fn drain(&self) -> Vec<ProcessEvent> {
|
||||
let mut events = Vec::new();
|
||||
while let Some(event) = self.inner.pop() {
|
||||
events.push(event);
|
||||
}
|
||||
events
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for EventQueue {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,25 +0,0 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
/// A handle that I/O threads use to wake the owning actor.
|
||||
///
|
||||
/// Constructed with a closure that sends a `ProcessCommand::PollTick`
|
||||
/// to the actor via `ExternalSender`. Thread-safe and cloneable.
|
||||
#[derive(Clone)]
|
||||
pub struct ProcessWaker(Arc<dyn Fn() + Send + Sync>);
|
||||
|
||||
impl std::fmt::Debug for ProcessWaker {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("ProcessWaker").finish_non_exhaustive()
|
||||
}
|
||||
}
|
||||
|
||||
impl ProcessWaker {
|
||||
pub fn new(f: impl Fn() + Send + Sync + 'static) -> Self {
|
||||
Self(Arc::new(f))
|
||||
}
|
||||
|
||||
/// Wake the owning actor so it drains pending events.
|
||||
pub fn wake(&self) {
|
||||
(self.0)();
|
||||
}
|
||||
}
|
||||
|
|
@ -4,7 +4,7 @@ use std::collections::HashMap;
|
|||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::pipeline_types::JobDefinition;
|
||||
use crate::pipeline::JobDefinition;
|
||||
|
||||
/// Root of a `.ci.yml` file.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
|
|
|
|||
|
|
@ -1,555 +0,0 @@
|
|||
//! Local CI simulation: deterministic, round-based execution of the local
|
||||
//! coordinator's queue + scheduling logic.
|
||||
//!
|
||||
//! No actors, no IO. Models the one-at-a-time scheduling with supersede.
|
||||
//! Follows the same pattern as `sim.rs`.
|
||||
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
|
||||
use swactor_ci::pipeline::PipelineExecution;
|
||||
use swactor_ci::yaml::{self, CiYaml};
|
||||
use swactor_ci::{JobId, JobStatus, PipelineId, PipelineStatus, StatusUpdate, WebhookEvent};
|
||||
|
||||
// ─── Simulation Config ──────────────────────────────────────────────────────
|
||||
|
||||
/// Configuration for a local CI simulation run.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct LocalSimConfig {
|
||||
pub name: String,
|
||||
pub num_rounds: usize,
|
||||
pub ci_yaml: String,
|
||||
pub webhook_schedule: Vec<(usize, WebhookEvent)>,
|
||||
/// Rounds a job takes to execute.
|
||||
pub job_duration: usize,
|
||||
/// Force specific jobs to fail: (round, job_name_substring).
|
||||
pub job_failure_schedule: Vec<(usize, String)>,
|
||||
}
|
||||
|
||||
impl Default for LocalSimConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
name: "local-sim".into(),
|
||||
num_rounds: 50,
|
||||
ci_yaml: String::new(),
|
||||
webhook_schedule: Vec::new(),
|
||||
job_duration: 3,
|
||||
job_failure_schedule: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Simulation Trace ───────────────────────────────────────────────────────
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum LocalSimEvent {
|
||||
WebhookReceived { commit_sha: String },
|
||||
PipelineCreated { pipeline_id: PipelineId, name: String },
|
||||
PipelineSuperseded { pipeline_id: PipelineId },
|
||||
PipelineCompleted { pipeline_id: PipelineId, status: PipelineStatus },
|
||||
JobStarted { job_id: JobId },
|
||||
JobCompleted { job_id: JobId, passed: bool },
|
||||
JobSkipped { job_id: JobId },
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct LocalSimSnapshot {
|
||||
pub queued_pipelines: usize,
|
||||
pub active_pipeline: Option<PipelineId>,
|
||||
pub running_job: Option<JobId>,
|
||||
pub completed_pipelines: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct LocalSimTrace {
|
||||
pub name: String,
|
||||
pub events: Vec<(usize, LocalSimEvent)>,
|
||||
pub snapshots: Vec<LocalSimSnapshot>,
|
||||
pub status_updates: Vec<StatusUpdate>,
|
||||
pub num_rounds: usize,
|
||||
pub final_pipelines: Vec<PipelineExecution>,
|
||||
}
|
||||
|
||||
// ─── Simulation State ───────────────────────────────────────────────────────
|
||||
|
||||
struct RunningJob {
|
||||
job_id: JobId,
|
||||
started_round: usize,
|
||||
}
|
||||
|
||||
/// Run a local CI simulation and return the trace.
|
||||
pub fn run_simulation(config: LocalSimConfig) -> LocalSimTrace {
|
||||
let ci_yaml: CiYaml =
|
||||
yaml::parse_ci_yaml(&config.ci_yaml).expect("LocalSimConfig.ci_yaml must be valid YAML");
|
||||
|
||||
let mut events: Vec<(usize, LocalSimEvent)> = Vec::new();
|
||||
let mut snapshots: Vec<LocalSimSnapshot> = Vec::new();
|
||||
let mut all_status_updates: Vec<StatusUpdate> = Vec::new();
|
||||
|
||||
// Coordinator state.
|
||||
let mut pipelines: HashMap<PipelineId, PipelineExecution> = HashMap::new();
|
||||
let mut next_pipeline_id: u64 = 1;
|
||||
let mut queue: VecDeque<PipelineId> = VecDeque::new();
|
||||
let mut active_pipeline: Option<PipelineId> = None;
|
||||
let mut running_job: Option<RunningJob> = None;
|
||||
let mut completed_pipelines: Vec<PipelineExecution> = Vec::new();
|
||||
|
||||
for round in 1..=config.num_rounds {
|
||||
// 1. Inject webhook events for this round.
|
||||
for (sched_round, event) in &config.webhook_schedule {
|
||||
if *sched_round == round {
|
||||
events.push((
|
||||
round,
|
||||
LocalSimEvent::WebhookReceived {
|
||||
commit_sha: event.commit_sha.clone(),
|
||||
},
|
||||
));
|
||||
|
||||
let matched = yaml::matching_pipelines(&ci_yaml, event);
|
||||
for pipeline_name in matched {
|
||||
let pipeline_id = PipelineId(next_pipeline_id);
|
||||
next_pipeline_id += 1;
|
||||
|
||||
let pipeline_def = &ci_yaml.pipelines[&pipeline_name];
|
||||
let job_defs: Vec<_> = pipeline_def
|
||||
.jobs
|
||||
.iter()
|
||||
.map(|(name, def)| yaml::to_job_definition(name, def))
|
||||
.collect();
|
||||
|
||||
let pipeline = PipelineExecution::new(
|
||||
pipeline_id,
|
||||
pipeline_name.clone(),
|
||||
event.repo_owner.clone(),
|
||||
event.repo_name.clone(),
|
||||
event.commit_sha.clone(),
|
||||
event.branch.clone(),
|
||||
job_defs,
|
||||
);
|
||||
|
||||
events.push((
|
||||
round,
|
||||
LocalSimEvent::PipelineCreated {
|
||||
pipeline_id,
|
||||
name: pipeline_name.clone(),
|
||||
},
|
||||
));
|
||||
|
||||
all_status_updates.push(StatusUpdate {
|
||||
repo_owner: event.repo_owner.clone(),
|
||||
repo_name: event.repo_name.clone(),
|
||||
commit_sha: event.commit_sha.clone(),
|
||||
state: "pending".into(),
|
||||
context: format!("ci/{pipeline_name}"),
|
||||
description: format!("Pipeline '{pipeline_name}' is pending"),
|
||||
target_url: None,
|
||||
});
|
||||
|
||||
pipelines.insert(pipeline_id, pipeline);
|
||||
|
||||
// Enqueue with supersede logic.
|
||||
let supersede_idx = queue.iter().position(|&qid| {
|
||||
pipelines
|
||||
.get(&qid)
|
||||
.map(|p| p.branch == event.branch)
|
||||
.unwrap_or(false)
|
||||
});
|
||||
|
||||
if let Some(idx) = supersede_idx {
|
||||
let old_id = queue[idx];
|
||||
if let Some(old_pipeline) = pipelines.get_mut(&old_id) {
|
||||
old_pipeline.status = PipelineStatus::Error {
|
||||
reason: "superseded".into(),
|
||||
};
|
||||
let job_names: Vec<String> =
|
||||
old_pipeline.jobs.keys().cloned().collect();
|
||||
for name in job_names {
|
||||
if old_pipeline.jobs[&name].status == JobStatus::Pending {
|
||||
old_pipeline.set_job_status(&name, JobStatus::Skipped);
|
||||
}
|
||||
}
|
||||
}
|
||||
events.push((
|
||||
round,
|
||||
LocalSimEvent::PipelineSuperseded {
|
||||
pipeline_id: old_id,
|
||||
},
|
||||
));
|
||||
if let Some(old_pipeline) = pipelines.remove(&old_id) {
|
||||
// Emit terminal status for superseded pipeline.
|
||||
all_status_updates.push(StatusUpdate {
|
||||
repo_owner: old_pipeline.repo_owner.clone(),
|
||||
repo_name: old_pipeline.repo_name.clone(),
|
||||
commit_sha: old_pipeline.commit_sha.clone(),
|
||||
state: "error".into(),
|
||||
context: format!("ci/{}", old_pipeline.pipeline_name),
|
||||
description: "superseded".into(),
|
||||
target_url: None,
|
||||
});
|
||||
completed_pipelines.push(old_pipeline);
|
||||
}
|
||||
queue[idx] = pipeline_id;
|
||||
} else {
|
||||
queue.push_back(pipeline_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Complete running job if it has reached duration.
|
||||
if let Some(ref rj) = running_job {
|
||||
if round - rj.started_round >= config.job_duration {
|
||||
let job_id = rj.job_id.clone();
|
||||
|
||||
let should_fail = config
|
||||
.job_failure_schedule
|
||||
.iter()
|
||||
.any(|(r, name_sub)| *r <= round && job_id.job_name.contains(name_sub.as_str()));
|
||||
|
||||
let passed = !should_fail;
|
||||
|
||||
if passed {
|
||||
if let Some(pipeline) = pipelines.get_mut(&job_id.pipeline_id) {
|
||||
pipeline.set_job_status(&job_id.job_name, JobStatus::Passed);
|
||||
}
|
||||
} else {
|
||||
if let Some(pipeline) = pipelines.get_mut(&job_id.pipeline_id) {
|
||||
pipeline.set_job_status(
|
||||
&job_id.job_name,
|
||||
JobStatus::Failed {
|
||||
reason: "command failed".into(),
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
events.push((
|
||||
round,
|
||||
LocalSimEvent::JobCompleted {
|
||||
job_id: job_id.clone(),
|
||||
passed,
|
||||
},
|
||||
));
|
||||
|
||||
// Emit skipped events for any jobs that were skipped due to failure.
|
||||
if !passed {
|
||||
if let Some(pipeline) = pipelines.get(&job_id.pipeline_id) {
|
||||
for (_name, job) in &pipeline.jobs {
|
||||
if job.status == JobStatus::Skipped {
|
||||
events.push((
|
||||
round,
|
||||
LocalSimEvent::JobSkipped {
|
||||
job_id: job.job_id.clone(),
|
||||
},
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
running_job = None;
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Schedule next (one-at-a-time).
|
||||
schedule_next(
|
||||
&mut pipelines,
|
||||
&mut queue,
|
||||
&mut active_pipeline,
|
||||
&mut running_job,
|
||||
&mut completed_pipelines,
|
||||
&mut events,
|
||||
&mut all_status_updates,
|
||||
round,
|
||||
);
|
||||
|
||||
// 4. Snapshot.
|
||||
snapshots.push(LocalSimSnapshot {
|
||||
queued_pipelines: queue.len(),
|
||||
active_pipeline,
|
||||
running_job: running_job.as_ref().map(|rj| rj.job_id.clone()),
|
||||
completed_pipelines: completed_pipelines.len(),
|
||||
});
|
||||
}
|
||||
|
||||
// Collect remaining active pipelines into final output.
|
||||
let mut final_pipelines: Vec<PipelineExecution> = pipelines.into_values().collect();
|
||||
final_pipelines.extend(completed_pipelines);
|
||||
|
||||
LocalSimTrace {
|
||||
name: config.name,
|
||||
events,
|
||||
snapshots,
|
||||
status_updates: all_status_updates,
|
||||
num_rounds: config.num_rounds,
|
||||
final_pipelines,
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn schedule_next(
|
||||
pipelines: &mut HashMap<PipelineId, PipelineExecution>,
|
||||
queue: &mut VecDeque<PipelineId>,
|
||||
active_pipeline: &mut Option<PipelineId>,
|
||||
running_job: &mut Option<RunningJob>,
|
||||
completed_pipelines: &mut Vec<PipelineExecution>,
|
||||
events: &mut Vec<(usize, LocalSimEvent)>,
|
||||
status_updates: &mut Vec<StatusUpdate>,
|
||||
round: usize,
|
||||
) {
|
||||
// If a job is running, nothing to do.
|
||||
if running_job.is_some() {
|
||||
return;
|
||||
}
|
||||
|
||||
// If we have an active pipeline, try eligible jobs.
|
||||
if let Some(active_id) = *active_pipeline {
|
||||
if let Some(pipeline) = pipelines.get(&active_id) {
|
||||
let eligible = pipeline.eligible_jobs();
|
||||
if !eligible.is_empty() {
|
||||
let job_name = eligible[0].clone();
|
||||
let job_id = JobId {
|
||||
pipeline_id: active_id,
|
||||
job_name: job_name.clone(),
|
||||
};
|
||||
|
||||
// Mark as running.
|
||||
if let Some(pipeline) = pipelines.get_mut(&active_id) {
|
||||
if let Some(job) = pipeline.jobs.get_mut(&job_name) {
|
||||
job.status = JobStatus::Running;
|
||||
}
|
||||
}
|
||||
|
||||
events.push((round, LocalSimEvent::JobStarted { job_id: job_id.clone() }));
|
||||
|
||||
*running_job = Some(RunningJob {
|
||||
job_id,
|
||||
started_round: round,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// No eligible jobs — check terminal.
|
||||
if pipeline.status.is_terminal() {
|
||||
let pipeline = pipelines.remove(&active_id).unwrap();
|
||||
let status = pipeline.status.clone();
|
||||
|
||||
status_updates.push(StatusUpdate {
|
||||
repo_owner: pipeline.repo_owner.clone(),
|
||||
repo_name: pipeline.repo_name.clone(),
|
||||
commit_sha: pipeline.commit_sha.clone(),
|
||||
state: pipeline.status.forgejo_state().into(),
|
||||
context: format!("ci/{}", pipeline.pipeline_name),
|
||||
description: format!(
|
||||
"Pipeline '{}' {}",
|
||||
pipeline.pipeline_name,
|
||||
pipeline.status.forgejo_state()
|
||||
),
|
||||
target_url: None,
|
||||
});
|
||||
|
||||
events.push((
|
||||
round,
|
||||
LocalSimEvent::PipelineCompleted {
|
||||
pipeline_id: active_id,
|
||||
status,
|
||||
},
|
||||
));
|
||||
|
||||
completed_pipelines.push(pipeline);
|
||||
*active_pipeline = None;
|
||||
|
||||
// Recurse.
|
||||
schedule_next(
|
||||
pipelines,
|
||||
queue,
|
||||
active_pipeline,
|
||||
running_job,
|
||||
completed_pipelines,
|
||||
events,
|
||||
status_updates,
|
||||
round,
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
// Pipeline exists but no eligible jobs and not terminal — waiting.
|
||||
return;
|
||||
}
|
||||
|
||||
// No active pipeline — pop from queue.
|
||||
if let Some(next_id) = queue.pop_front() {
|
||||
*active_pipeline = Some(next_id);
|
||||
schedule_next(
|
||||
pipelines,
|
||||
queue,
|
||||
active_pipeline,
|
||||
running_job,
|
||||
completed_pipelines,
|
||||
events,
|
||||
status_updates,
|
||||
round,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Properties ─────────────────────────────────────────────────────────────
|
||||
|
||||
/// At most one job running in any snapshot.
|
||||
pub fn check_one_at_a_time(trace: &LocalSimTrace) -> bool {
|
||||
trace
|
||||
.snapshots
|
||||
.iter()
|
||||
.all(|s| s.running_job.is_some() as usize <= 1)
|
||||
}
|
||||
|
||||
/// Superseded pipelines never have a Running job.
|
||||
pub fn check_superseded_no_running(trace: &LocalSimTrace) -> bool {
|
||||
let superseded: Vec<PipelineId> = trace
|
||||
.events
|
||||
.iter()
|
||||
.filter_map(|(_, e)| match e {
|
||||
LocalSimEvent::PipelineSuperseded { pipeline_id } => Some(*pipeline_id),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
|
||||
let started_jobs: Vec<&JobId> = trace
|
||||
.events
|
||||
.iter()
|
||||
.filter_map(|(_, e)| match e {
|
||||
LocalSimEvent::JobStarted { job_id } => Some(job_id),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
|
||||
for pid in &superseded {
|
||||
if started_jobs
|
||||
.iter()
|
||||
.any(|jid| jid.pipeline_id == *pid)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
/// All non-superseded pipelines reach terminal status.
|
||||
pub fn check_termination(trace: &LocalSimTrace) -> bool {
|
||||
let superseded: Vec<PipelineId> = trace
|
||||
.events
|
||||
.iter()
|
||||
.filter_map(|(_, e)| match e {
|
||||
LocalSimEvent::PipelineSuperseded { pipeline_id } => Some(*pipeline_id),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
|
||||
for pipeline in &trace.final_pipelines {
|
||||
if superseded.contains(&pipeline.pipeline_id) {
|
||||
continue;
|
||||
}
|
||||
if !pipeline.status.is_terminal() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
/// Within a pipeline, jobs respect dependency order.
|
||||
pub fn check_dag_ordering(trace: &LocalSimTrace) -> bool {
|
||||
let mut started: HashMap<(u64, &str), usize> = HashMap::new();
|
||||
let mut completed: HashMap<(u64, &str), usize> = HashMap::new();
|
||||
|
||||
for (round, event) in &trace.events {
|
||||
match event {
|
||||
LocalSimEvent::JobStarted { job_id } => {
|
||||
started.insert(
|
||||
(job_id.pipeline_id.0, job_id.job_name.as_str()),
|
||||
*round,
|
||||
);
|
||||
}
|
||||
LocalSimEvent::JobCompleted { job_id, .. } => {
|
||||
completed.insert(
|
||||
(job_id.pipeline_id.0, job_id.job_name.as_str()),
|
||||
*round,
|
||||
);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
for pipeline in &trace.final_pipelines {
|
||||
for (name, job) in &pipeline.jobs {
|
||||
if let Some(&start_round) = started.get(&(pipeline.pipeline_id.0, name.as_str())) {
|
||||
for dep in &job.definition.needs {
|
||||
if let Some(&dep_complete_round) =
|
||||
completed.get(&(pipeline.pipeline_id.0, dep.as_str()))
|
||||
{
|
||||
if dep_complete_round > start_round {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
/// Different branches execute in queue order (FIFO).
|
||||
pub fn check_fifo_order(trace: &LocalSimTrace) -> bool {
|
||||
// Collect pipeline creation order and first job start per pipeline.
|
||||
let mut creation_order: Vec<PipelineId> = Vec::new();
|
||||
let mut first_start: HashMap<PipelineId, usize> = HashMap::new();
|
||||
|
||||
for (round, event) in &trace.events {
|
||||
if let LocalSimEvent::PipelineCreated { pipeline_id, .. } = event {
|
||||
creation_order.push(*pipeline_id);
|
||||
}
|
||||
if let LocalSimEvent::JobStarted { job_id } = event {
|
||||
first_start
|
||||
.entry(job_id.pipeline_id)
|
||||
.or_insert(*round);
|
||||
}
|
||||
}
|
||||
|
||||
// For each pair of pipelines created in order, if both started, the earlier-created
|
||||
// one should have started no later.
|
||||
for i in 0..creation_order.len() {
|
||||
for j in (i + 1)..creation_order.len() {
|
||||
let pid_a = creation_order[i];
|
||||
let pid_b = creation_order[j];
|
||||
if let (Some(&start_a), Some(&start_b)) =
|
||||
(first_start.get(&pid_a), first_start.get(&pid_b))
|
||||
{
|
||||
if start_a > start_b {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
/// Every webhook produces a terminal status (success/failure/error).
|
||||
pub fn check_all_webhooks_terminate(trace: &LocalSimTrace) -> bool {
|
||||
let webhook_commits: Vec<&str> = trace
|
||||
.events
|
||||
.iter()
|
||||
.filter_map(|(_, e)| match e {
|
||||
LocalSimEvent::WebhookReceived { commit_sha } => Some(commit_sha.as_str()),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
|
||||
for sha in webhook_commits {
|
||||
let has_terminal = trace.status_updates.iter().any(|u| {
|
||||
u.commit_sha == sha
|
||||
&& (u.state == "success" || u.state == "failure" || u.state == "error")
|
||||
});
|
||||
if !has_terminal {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
pub mod local_sim;
|
||||
pub mod sim;
|
||||
|
|
@ -1,612 +0,0 @@
|
|||
//! CI protocol simulation: deterministic, round-based execution of the CI pipeline
|
||||
//! lifecycle without real IO (no SSH, no cloud API, no HTTP).
|
||||
//!
|
||||
//! Follows the same pattern as `crates/simulation/src/distribution/sim.rs`:
|
||||
//! configure → run rounds → collect trace → analyze properties.
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use swactor_ci::pipeline::PipelineExecution;
|
||||
use swactor_ci::yaml::{self, CiYaml};
|
||||
use swactor_ci::{
|
||||
JobId, JobStatus, PipelineId, ProvisionRequest, StatusUpdate, WebhookEvent,
|
||||
};
|
||||
|
||||
// ─── Simulation Config ──────────────────────────────────────────────────────
|
||||
|
||||
/// Configuration for a CI simulation run.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CiSimConfig {
|
||||
pub name: String,
|
||||
pub num_rounds: usize,
|
||||
|
||||
/// CI YAML to use for all simulated repos.
|
||||
pub ci_yaml: String,
|
||||
|
||||
/// Webhook events to inject at specific rounds.
|
||||
pub webhook_schedule: Vec<(usize, WebhookEvent)>,
|
||||
|
||||
/// Rounds of latency for provisioning to complete.
|
||||
pub provision_latency: usize,
|
||||
|
||||
/// Probability that provisioning fails (0.0-1.0).
|
||||
pub provision_failure_rate: f64,
|
||||
|
||||
/// Rounds of latency for a job to complete.
|
||||
pub job_duration: usize,
|
||||
|
||||
/// Force specific jobs to fail: (round, job_name_substring).
|
||||
pub job_failure_schedule: Vec<(usize, String)>,
|
||||
|
||||
/// Rounds during which the provisioner is offline: (start_round, end_round).
|
||||
pub provisioner_offline_schedule: Vec<(usize, usize)>,
|
||||
|
||||
/// Round at which a specific job's instance is interrupted.
|
||||
pub instance_interrupt_schedule: Vec<(usize, String)>,
|
||||
}
|
||||
|
||||
impl Default for CiSimConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
name: "ci-sim".into(),
|
||||
num_rounds: 50,
|
||||
ci_yaml: String::new(),
|
||||
webhook_schedule: Vec::new(),
|
||||
provision_latency: 2,
|
||||
provision_failure_rate: 0.0,
|
||||
job_duration: 3,
|
||||
job_failure_schedule: Vec::new(),
|
||||
provisioner_offline_schedule: Vec::new(),
|
||||
instance_interrupt_schedule: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Simulation Trace ───────────────────────────────────────────────────────
|
||||
|
||||
/// Event recorded during simulation.
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum CiSimEvent {
|
||||
WebhookReceived { commit_sha: String },
|
||||
PipelineCreated { pipeline_id: PipelineId, name: String },
|
||||
ProvisionRequested { job_id: JobId },
|
||||
ProvisionCompleted { job_id: JobId, success: bool },
|
||||
JobStarted { job_id: JobId },
|
||||
JobCompleted { job_id: JobId, passed: bool },
|
||||
JobSkipped { job_id: JobId },
|
||||
InstanceTerminated { instance_id: String },
|
||||
ProvisionerWentOffline,
|
||||
ProvisionerCameOnline,
|
||||
StatusUpdateEmitted(StatusUpdate),
|
||||
}
|
||||
|
||||
/// Per-round snapshot of simulation state.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CiSimSnapshot {
|
||||
pub active_pipelines: usize,
|
||||
pub completed_pipelines: usize,
|
||||
pub active_provisions: usize,
|
||||
pub active_jobs: usize,
|
||||
pub provisioner_online: bool,
|
||||
pub active_instances: usize,
|
||||
}
|
||||
|
||||
/// Complete trace output from a CI simulation.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CiSimTrace {
|
||||
pub name: String,
|
||||
pub events: Vec<(usize, CiSimEvent)>,
|
||||
pub snapshots: Vec<CiSimSnapshot>,
|
||||
pub status_updates: Vec<StatusUpdate>,
|
||||
pub num_rounds: usize,
|
||||
/// Final state of all pipelines.
|
||||
pub final_pipelines: Vec<PipelineExecution>,
|
||||
/// Instances that were provisioned.
|
||||
pub provisioned_instances: Vec<String>,
|
||||
/// Instances that were terminated.
|
||||
pub terminated_instances: Vec<String>,
|
||||
}
|
||||
|
||||
// ─── Simulation State ───────────────────────────────────────────────────────
|
||||
|
||||
/// Tracks an in-flight provision request.
|
||||
struct PendingProvision {
|
||||
request: ProvisionRequest,
|
||||
started_round: usize,
|
||||
}
|
||||
|
||||
/// Tracks an in-flight job execution.
|
||||
struct RunningJob {
|
||||
job_id: JobId,
|
||||
started_round: usize,
|
||||
instance_id: String,
|
||||
}
|
||||
|
||||
/// Run a CI simulation and return the trace.
|
||||
pub fn run_simulation(config: CiSimConfig) -> CiSimTrace {
|
||||
let ci_yaml: CiYaml = yaml::parse_ci_yaml(&config.ci_yaml)
|
||||
.expect("CiSimConfig.ci_yaml must be valid YAML");
|
||||
|
||||
let mut events: Vec<(usize, CiSimEvent)> = Vec::new();
|
||||
let mut snapshots: Vec<CiSimSnapshot> = Vec::new();
|
||||
|
||||
// Coordinator state (simulated directly, not as actor).
|
||||
let mut pipelines: HashMap<PipelineId, PipelineExecution> = HashMap::new();
|
||||
let mut next_pipeline_id: u64 = 1;
|
||||
let mut all_status_updates: Vec<StatusUpdate> = Vec::new();
|
||||
|
||||
// Provisioner state.
|
||||
let mut provisioner_online = true;
|
||||
let mut pending_provisions: Vec<PendingProvision> = Vec::new();
|
||||
let mut queued_provisions: Vec<ProvisionRequest> = Vec::new();
|
||||
let mut provisioned_instances: Vec<String> = Vec::new();
|
||||
let mut terminated_instances: Vec<String> = Vec::new();
|
||||
let mut active_instances: Vec<String> = Vec::new();
|
||||
let mut next_instance_id: u64 = 1;
|
||||
|
||||
// Runner state.
|
||||
let mut running_jobs: Vec<RunningJob> = Vec::new();
|
||||
|
||||
// Simple deterministic "RNG" for provision failure decisions.
|
||||
let mut rng_counter: u64 = 0x853c49e6748fea9b;
|
||||
let mut det_random = || -> f64 {
|
||||
rng_counter = rng_counter.wrapping_mul(6364136223846793005).wrapping_add(1);
|
||||
(rng_counter >> 33) as f64 / (u32::MAX as f64)
|
||||
};
|
||||
|
||||
for round in 1..=config.num_rounds {
|
||||
// 1. Apply provisioner online/offline schedule.
|
||||
let should_be_offline = config
|
||||
.provisioner_offline_schedule
|
||||
.iter()
|
||||
.any(|(start, end)| round >= *start && round <= *end);
|
||||
|
||||
if should_be_offline && provisioner_online {
|
||||
provisioner_online = false;
|
||||
events.push((round, CiSimEvent::ProvisionerWentOffline));
|
||||
|
||||
// Move pending provisions to queue.
|
||||
for pending in pending_provisions.drain(..) {
|
||||
queued_provisions.push(pending.request);
|
||||
}
|
||||
} else if !should_be_offline && !provisioner_online {
|
||||
provisioner_online = true;
|
||||
events.push((round, CiSimEvent::ProvisionerCameOnline));
|
||||
|
||||
// Flush queued provisions.
|
||||
for request in queued_provisions.drain(..) {
|
||||
pending_provisions.push(PendingProvision {
|
||||
request,
|
||||
started_round: round,
|
||||
});
|
||||
}
|
||||
|
||||
// Also resubmit any WaitingForProvisioner jobs.
|
||||
let mut resubmits = Vec::new();
|
||||
for pipeline in pipelines.values_mut() {
|
||||
for job in pipeline.jobs.values_mut() {
|
||||
if job.status == JobStatus::WaitingForProvisioner {
|
||||
job.status = JobStatus::Provisioning;
|
||||
resubmits.push(ProvisionRequest {
|
||||
job_id: job.job_id.clone(),
|
||||
instance_spec: swactor_ci::InstanceSpec {
|
||||
docker_required: job.definition.docker,
|
||||
..Default::default()
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
for request in resubmits {
|
||||
events.push((round, CiSimEvent::ProvisionRequested { job_id: request.job_id.clone() }));
|
||||
pending_provisions.push(PendingProvision {
|
||||
request,
|
||||
started_round: round,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Inject webhook events for this round.
|
||||
for (sched_round, event) in &config.webhook_schedule {
|
||||
if *sched_round == round {
|
||||
events.push((
|
||||
round,
|
||||
CiSimEvent::WebhookReceived {
|
||||
commit_sha: event.commit_sha.clone(),
|
||||
},
|
||||
));
|
||||
|
||||
let matched = yaml::matching_pipelines(&ci_yaml, event);
|
||||
for pipeline_name in matched {
|
||||
let pipeline_id = PipelineId(next_pipeline_id);
|
||||
next_pipeline_id += 1;
|
||||
|
||||
let pipeline_def = &ci_yaml.pipelines[&pipeline_name];
|
||||
let job_defs: Vec<_> = pipeline_def
|
||||
.jobs
|
||||
.iter()
|
||||
.map(|(name, def)| yaml::to_job_definition(name, def))
|
||||
.collect();
|
||||
|
||||
let pipeline = PipelineExecution::new(
|
||||
pipeline_id,
|
||||
pipeline_name.clone(),
|
||||
event.repo_owner.clone(),
|
||||
event.repo_name.clone(),
|
||||
event.commit_sha.clone(),
|
||||
event.branch.clone(),
|
||||
job_defs,
|
||||
);
|
||||
|
||||
events.push((
|
||||
round,
|
||||
CiSimEvent::PipelineCreated {
|
||||
pipeline_id,
|
||||
name: pipeline_name.clone(),
|
||||
},
|
||||
));
|
||||
|
||||
// Emit pending status.
|
||||
all_status_updates.push(StatusUpdate {
|
||||
repo_owner: event.repo_owner.clone(),
|
||||
repo_name: event.repo_name.clone(),
|
||||
commit_sha: event.commit_sha.clone(),
|
||||
state: "pending".into(),
|
||||
context: format!("ci/{pipeline_name}"),
|
||||
description: format!("Pipeline '{pipeline_name}' is pending"),
|
||||
target_url: None,
|
||||
});
|
||||
|
||||
pipelines.insert(pipeline_id, pipeline);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Complete provisions that have reached latency.
|
||||
let mut completed_provisions = Vec::new();
|
||||
pending_provisions.retain(|pending| {
|
||||
if round - pending.started_round >= config.provision_latency {
|
||||
completed_provisions.push(pending.request.clone());
|
||||
false
|
||||
} else {
|
||||
true
|
||||
}
|
||||
});
|
||||
|
||||
for request in completed_provisions {
|
||||
let should_fail = det_random() < config.provision_failure_rate;
|
||||
|
||||
if should_fail {
|
||||
events.push((
|
||||
round,
|
||||
CiSimEvent::ProvisionCompleted {
|
||||
job_id: request.job_id.clone(),
|
||||
success: false,
|
||||
},
|
||||
));
|
||||
|
||||
if let Some(pipeline) = pipelines.get_mut(&request.job_id.pipeline_id) {
|
||||
pipeline.set_job_status(
|
||||
&request.job_id.job_name,
|
||||
JobStatus::Failed {
|
||||
reason: "provision failed".into(),
|
||||
},
|
||||
);
|
||||
}
|
||||
} else {
|
||||
let instance_id = format!("instance-{next_instance_id}");
|
||||
next_instance_id += 1;
|
||||
provisioned_instances.push(instance_id.clone());
|
||||
active_instances.push(instance_id.clone());
|
||||
|
||||
events.push((
|
||||
round,
|
||||
CiSimEvent::ProvisionCompleted {
|
||||
job_id: request.job_id.clone(),
|
||||
success: true,
|
||||
},
|
||||
));
|
||||
|
||||
// Mark job as running and record instance.
|
||||
if let Some(pipeline) = pipelines.get_mut(&request.job_id.pipeline_id) {
|
||||
if let Some(job) = pipeline.jobs.get_mut(&request.job_id.job_name) {
|
||||
job.status = JobStatus::Running;
|
||||
job.instance_id = Some(instance_id.clone());
|
||||
}
|
||||
}
|
||||
|
||||
events.push((
|
||||
round,
|
||||
CiSimEvent::JobStarted {
|
||||
job_id: request.job_id.clone(),
|
||||
},
|
||||
));
|
||||
|
||||
running_jobs.push(RunningJob {
|
||||
job_id: request.job_id,
|
||||
started_round: round,
|
||||
instance_id,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Apply instance interruptions.
|
||||
for (interrupt_round, job_name_sub) in &config.instance_interrupt_schedule {
|
||||
if *interrupt_round == round {
|
||||
running_jobs.retain(|rj| {
|
||||
if rj.job_id.job_name.contains(job_name_sub.as_str()) {
|
||||
// Instance interrupted.
|
||||
if let Some(pipeline) = pipelines.get_mut(&rj.job_id.pipeline_id) {
|
||||
pipeline.set_job_status(&rj.job_id.job_name, JobStatus::Interrupted);
|
||||
}
|
||||
events.push((
|
||||
round,
|
||||
CiSimEvent::JobCompleted {
|
||||
job_id: rj.job_id.clone(),
|
||||
passed: false,
|
||||
},
|
||||
));
|
||||
// Terminate the instance.
|
||||
active_instances.retain(|id| id != &rj.instance_id);
|
||||
terminated_instances.push(rj.instance_id.clone());
|
||||
events.push((
|
||||
round,
|
||||
CiSimEvent::InstanceTerminated {
|
||||
instance_id: rj.instance_id.clone(),
|
||||
},
|
||||
));
|
||||
false
|
||||
} else {
|
||||
true
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 5. Complete jobs that have reached duration.
|
||||
let mut newly_completed = Vec::new();
|
||||
running_jobs.retain(|rj| {
|
||||
if round - rj.started_round >= config.job_duration {
|
||||
newly_completed.push((rj.job_id.clone(), rj.instance_id.clone()));
|
||||
false
|
||||
} else {
|
||||
true
|
||||
}
|
||||
});
|
||||
|
||||
for (job_id, instance_id) in newly_completed {
|
||||
// Check if this job should fail per the schedule.
|
||||
let should_fail = config
|
||||
.job_failure_schedule
|
||||
.iter()
|
||||
.any(|(r, name_sub)| *r <= round && job_id.job_name.contains(name_sub.as_str()));
|
||||
|
||||
let passed = !should_fail;
|
||||
|
||||
if passed {
|
||||
if let Some(pipeline) = pipelines.get_mut(&job_id.pipeline_id) {
|
||||
pipeline.set_job_status(&job_id.job_name, JobStatus::Passed);
|
||||
}
|
||||
} else {
|
||||
if let Some(pipeline) = pipelines.get_mut(&job_id.pipeline_id) {
|
||||
pipeline.set_job_status(
|
||||
&job_id.job_name,
|
||||
JobStatus::Failed {
|
||||
reason: "command failed".into(),
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
events.push((
|
||||
round,
|
||||
CiSimEvent::JobCompleted {
|
||||
job_id: job_id.clone(),
|
||||
passed,
|
||||
},
|
||||
));
|
||||
|
||||
// Terminate instance.
|
||||
active_instances.retain(|id| id != &instance_id);
|
||||
terminated_instances.push(instance_id.clone());
|
||||
events.push((
|
||||
round,
|
||||
CiSimEvent::InstanceTerminated {
|
||||
instance_id: instance_id.clone(),
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
// 6. Advance all pipelines: schedule newly-eligible jobs.
|
||||
let pipeline_ids: Vec<PipelineId> = pipelines.keys().copied().collect();
|
||||
for pid in pipeline_ids {
|
||||
let eligible = pipelines[&pid].eligible_jobs();
|
||||
for job_name in eligible {
|
||||
let job_id = JobId {
|
||||
pipeline_id: pid,
|
||||
job_name: job_name.clone(),
|
||||
};
|
||||
let docker_required = pipelines[&pid].jobs[&job_name].definition.docker;
|
||||
|
||||
let request = ProvisionRequest {
|
||||
job_id: job_id.clone(),
|
||||
instance_spec: swactor_ci::InstanceSpec {
|
||||
docker_required,
|
||||
..Default::default()
|
||||
},
|
||||
};
|
||||
|
||||
if provisioner_online {
|
||||
if let Some(pipeline) = pipelines.get_mut(&pid) {
|
||||
if let Some(job) = pipeline.jobs.get_mut(&job_name) {
|
||||
job.status = JobStatus::Provisioning;
|
||||
}
|
||||
}
|
||||
events.push((
|
||||
round,
|
||||
CiSimEvent::ProvisionRequested { job_id },
|
||||
));
|
||||
pending_provisions.push(PendingProvision {
|
||||
request,
|
||||
started_round: round,
|
||||
});
|
||||
} else {
|
||||
if let Some(pipeline) = pipelines.get_mut(&pid) {
|
||||
if let Some(job) = pipeline.jobs.get_mut(&job_name) {
|
||||
job.status = JobStatus::WaitingForProvisioner;
|
||||
}
|
||||
}
|
||||
queued_provisions.push(request);
|
||||
}
|
||||
}
|
||||
|
||||
// Emit final status updates for terminal pipelines.
|
||||
if let Some(pipeline) = pipelines.get(&pid) {
|
||||
if pipeline.status.is_terminal() {
|
||||
// Check if we already emitted a terminal status for this pipeline.
|
||||
let context = format!("ci/{}", pipeline.pipeline_name);
|
||||
let already_emitted = all_status_updates.iter().any(|u| {
|
||||
u.context == context
|
||||
&& u.commit_sha == pipeline.commit_sha
|
||||
&& (u.state == "success" || u.state == "failure" || u.state == "error")
|
||||
});
|
||||
if !already_emitted {
|
||||
all_status_updates.push(StatusUpdate {
|
||||
repo_owner: pipeline.repo_owner.clone(),
|
||||
repo_name: pipeline.repo_name.clone(),
|
||||
commit_sha: pipeline.commit_sha.clone(),
|
||||
state: pipeline.status.forgejo_state().into(),
|
||||
context,
|
||||
description: format!(
|
||||
"Pipeline '{}' {}",
|
||||
pipeline.pipeline_name,
|
||||
pipeline.status.forgejo_state()
|
||||
),
|
||||
target_url: None,
|
||||
});
|
||||
|
||||
// Emit per-job skipped events.
|
||||
for (_name, job) in &pipeline.jobs {
|
||||
if job.status == JobStatus::Skipped {
|
||||
events.push((
|
||||
round,
|
||||
CiSimEvent::JobSkipped {
|
||||
job_id: job.job_id.clone(),
|
||||
},
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 7. Snapshot.
|
||||
let completed_count = pipelines.values().filter(|p| p.status.is_terminal()).count();
|
||||
snapshots.push(CiSimSnapshot {
|
||||
active_pipelines: pipelines.len() - completed_count,
|
||||
completed_pipelines: completed_count,
|
||||
active_provisions: pending_provisions.len(),
|
||||
active_jobs: running_jobs.len(),
|
||||
provisioner_online,
|
||||
active_instances: active_instances.len(),
|
||||
});
|
||||
}
|
||||
|
||||
CiSimTrace {
|
||||
name: config.name,
|
||||
events,
|
||||
snapshots,
|
||||
status_updates: all_status_updates,
|
||||
num_rounds: config.num_rounds,
|
||||
final_pipelines: pipelines.into_values().collect(),
|
||||
provisioned_instances,
|
||||
terminated_instances,
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Properties ─────────────────────────────────────────────────────────────
|
||||
|
||||
/// Every webhook eventually produces a terminal Forgejo status (success/failure/error).
|
||||
pub fn check_all_webhooks_terminate(trace: &CiSimTrace) -> bool {
|
||||
let webhook_commits: Vec<&str> = trace
|
||||
.events
|
||||
.iter()
|
||||
.filter_map(|(_, e)| match e {
|
||||
CiSimEvent::WebhookReceived { commit_sha } => Some(commit_sha.as_str()),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
|
||||
for sha in webhook_commits {
|
||||
let has_terminal = trace.status_updates.iter().any(|u| {
|
||||
u.commit_sha == sha && (u.state == "success" || u.state == "failure" || u.state == "error")
|
||||
});
|
||||
if !has_terminal {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
/// Job DAG ordering is always respected: no job runs before its `needs`.
|
||||
pub fn check_dag_ordering(trace: &CiSimTrace) -> bool {
|
||||
// Build a map of (pipeline_id, job_name) → round when started.
|
||||
let mut started: HashMap<(u64, &str), usize> = HashMap::new();
|
||||
let mut completed: HashMap<(u64, &str), usize> = HashMap::new();
|
||||
|
||||
for (round, event) in &trace.events {
|
||||
match event {
|
||||
CiSimEvent::JobStarted { job_id } => {
|
||||
started.insert(
|
||||
(job_id.pipeline_id.0, job_id.job_name.as_str()),
|
||||
*round,
|
||||
);
|
||||
}
|
||||
CiSimEvent::JobCompleted { job_id, .. } => {
|
||||
completed.insert(
|
||||
(job_id.pipeline_id.0, job_id.job_name.as_str()),
|
||||
*round,
|
||||
);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
// For each pipeline, check that if job B needs job A, then A completed before B started.
|
||||
for pipeline in &trace.final_pipelines {
|
||||
for (name, job) in &pipeline.jobs {
|
||||
if let Some(&start_round) = started.get(&(pipeline.pipeline_id.0, name.as_str())) {
|
||||
for dep in &job.definition.needs {
|
||||
if let Some(&dep_complete_round) =
|
||||
completed.get(&(pipeline.pipeline_id.0, dep.as_str()))
|
||||
{
|
||||
if dep_complete_round > start_round {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
/// Every provisioned instance is eventually terminated (no resource leaks).
|
||||
pub fn check_no_instance_leaks(trace: &CiSimTrace) -> bool {
|
||||
// Every instance that was provisioned should also be terminated.
|
||||
for instance_id in &trace.provisioned_instances {
|
||||
if !trace.terminated_instances.contains(instance_id) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
/// Coordinator state is bounded: active pipeline count doesn't grow unboundedly.
|
||||
pub fn check_bounded_state(trace: &CiSimTrace, max_active: usize) -> bool {
|
||||
trace
|
||||
.snapshots
|
||||
.iter()
|
||||
.all(|s| s.active_pipelines <= max_active)
|
||||
}
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
use crate::topology::Topology;
|
||||
|
||||
/// Generic simulation configuration (protocol-agnostic).
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct SimConfig {
|
||||
pub name: String,
|
||||
pub topology: Topology,
|
||||
pub num_nodes: usize,
|
||||
pub num_rounds: usize,
|
||||
pub ticks_per_round: usize,
|
||||
/// If `Some(r)`, cross-partition links are added after round `r`.
|
||||
pub heal_after_round: Option<usize>,
|
||||
/// Number of worker threads: 1 = deterministic single-threaded.
|
||||
pub num_threads: usize,
|
||||
}
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
pub mod sim;
|
||||
pub mod trace;
|
||||
pub mod properties;
|
||||
6
crates/simulation/src/distribution_mod.rs
Normal file
6
crates/simulation/src/distribution_mod.rs
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
#[path = "distribution_sim.rs"]
|
||||
pub mod sim;
|
||||
#[path = "distribution_trace.rs"]
|
||||
pub mod trace;
|
||||
#[path = "distribution_properties.rs"]
|
||||
pub mod properties;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
use crate::trace::SimulationTrace;
|
||||
use crate::SimulationTrace;
|
||||
|
||||
use super::trace::{DistributionEventKind, DistributionSnapshot};
|
||||
|
||||
|
|
@ -6,7 +6,7 @@ use swactor::actor::ActorAddress;
|
|||
|
||||
use crate::runner::NetworkState;
|
||||
pub use crate::runner::{NetworkFault, NetworkTopology, NodeLocation, Partition};
|
||||
use crate::trace::{Event, SimulationTrace};
|
||||
use crate::{Event, SimulationTrace};
|
||||
|
||||
use super::trace::{DistributionEventKind, DistributionSnapshot};
|
||||
|
||||
|
|
@ -1,11 +1,9 @@
|
|||
pub mod node;
|
||||
pub mod runner;
|
||||
pub mod topology;
|
||||
pub mod config;
|
||||
pub mod trace;
|
||||
pub mod properties;
|
||||
|
||||
#[cfg(feature = "distribution")]
|
||||
#[path = "distribution_mod.rs"]
|
||||
pub mod distribution;
|
||||
|
||||
#[cfg(feature = "gossip")]
|
||||
|
|
@ -13,3 +11,86 @@ pub mod gossip;
|
|||
|
||||
#[cfg(feature = "dashboard")]
|
||||
pub mod dashboard;
|
||||
|
||||
// ─── Generic Simulation Node Traits ─────────────────────────────────────────
|
||||
|
||||
/// A message produced by a simulated node.
|
||||
pub trait SimMessage {
|
||||
type NodeId;
|
||||
/// The target node for this message, if any.
|
||||
/// `None` means the message is a local notification (no delivery needed).
|
||||
fn target(&self) -> Option<&Self::NodeId>;
|
||||
}
|
||||
|
||||
/// A simulated protocol node.
|
||||
pub trait SimNode: Sized {
|
||||
type Config: Clone;
|
||||
type NodeId: Clone + Eq + std::hash::Hash + std::fmt::Debug;
|
||||
type Message: SimMessage<NodeId = Self::NodeId>;
|
||||
type Snapshot: serde::Serialize;
|
||||
type EventKind: serde::Serialize;
|
||||
|
||||
fn new(config: Self::Config) -> Self;
|
||||
fn node_id(&self) -> Self::NodeId;
|
||||
fn tick(&mut self) -> Vec<Self::Message>;
|
||||
fn receive(&mut self, from: Self::NodeId, msg: Self::Message) -> Vec<Self::Message>;
|
||||
fn snapshot(&self) -> Self::Snapshot;
|
||||
}
|
||||
|
||||
// ─── Simulation Configuration ───────────────────────────────────────────────
|
||||
|
||||
use crate::topology::Topology;
|
||||
|
||||
/// Generic simulation configuration (protocol-agnostic).
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct SimConfig {
|
||||
pub name: String,
|
||||
pub topology: Topology,
|
||||
pub num_nodes: usize,
|
||||
pub num_rounds: usize,
|
||||
pub ticks_per_round: usize,
|
||||
/// If `Some(r)`, cross-partition links are added after round `r`.
|
||||
pub heal_after_round: Option<usize>,
|
||||
/// Number of worker threads: 1 = deterministic single-threaded.
|
||||
pub num_threads: usize,
|
||||
}
|
||||
|
||||
// ─── Trace Types ────────────────────────────────────────────────────────────
|
||||
|
||||
use std::sync::atomic::AtomicU64;
|
||||
use std::sync::Arc;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Shared tick counter — the simulation harness increments this.
|
||||
pub type TickCounter = Arc<AtomicU64>;
|
||||
|
||||
/// A single simulation event, generic over the event kind `K`.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(bound(
|
||||
serialize = "K: Serialize",
|
||||
deserialize = "K: serde::de::DeserializeOwned"
|
||||
))]
|
||||
pub struct Event<K> {
|
||||
pub tick: u64,
|
||||
pub node_name: String,
|
||||
pub kind: K,
|
||||
}
|
||||
|
||||
/// Complete output of a simulation run, generic over event kind `K` and snapshot type `S`.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(bound(
|
||||
serialize = "K: Serialize, S: Serialize",
|
||||
deserialize = "K: serde::de::DeserializeOwned, S: serde::de::DeserializeOwned"
|
||||
))]
|
||||
pub struct SimulationTrace<K, S> {
|
||||
pub name: String,
|
||||
/// Discriminator for dashboard rendering ("gossip" or "distribution").
|
||||
#[serde(default)]
|
||||
pub trace_type: String,
|
||||
pub node_names: Vec<String>,
|
||||
pub topology_edges: Vec<(String, String)>,
|
||||
pub events: Vec<Event<K>>,
|
||||
pub snapshots_per_round: Vec<Vec<(String, S)>>,
|
||||
pub num_rounds: usize,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,28 +0,0 @@
|
|||
//! Generic simulation node trait.
|
||||
//!
|
||||
//! Defines the interface that any protocol node must implement to be
|
||||
//! driven by the simulation runner. This allows the simulation
|
||||
//! framework to work with different protocol implementations.
|
||||
|
||||
/// A message produced by a simulated node.
|
||||
pub trait SimMessage {
|
||||
type NodeId;
|
||||
/// The target node for this message, if any.
|
||||
/// `None` means the message is a local notification (no delivery needed).
|
||||
fn target(&self) -> Option<&Self::NodeId>;
|
||||
}
|
||||
|
||||
/// A simulated protocol node.
|
||||
pub trait SimNode: Sized {
|
||||
type Config: Clone;
|
||||
type NodeId: Clone + Eq + std::hash::Hash + std::fmt::Debug;
|
||||
type Message: SimMessage<NodeId = Self::NodeId>;
|
||||
type Snapshot: serde::Serialize;
|
||||
type EventKind: serde::Serialize;
|
||||
|
||||
fn new(config: Self::Config) -> Self;
|
||||
fn node_id(&self) -> Self::NodeId;
|
||||
fn tick(&mut self) -> Vec<Self::Message>;
|
||||
fn receive(&mut self, from: Self::NodeId, msg: Self::Message) -> Vec<Self::Message>;
|
||||
fn snapshot(&self) -> Self::Snapshot;
|
||||
}
|
||||
|
|
@ -1,37 +0,0 @@
|
|||
use std::sync::atomic::AtomicU64;
|
||||
use std::sync::Arc;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Shared tick counter — the simulation harness increments this.
|
||||
pub type TickCounter = Arc<AtomicU64>;
|
||||
|
||||
/// A single simulation event, generic over the event kind `K`.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(bound(
|
||||
serialize = "K: Serialize",
|
||||
deserialize = "K: serde::de::DeserializeOwned"
|
||||
))]
|
||||
pub struct Event<K> {
|
||||
pub tick: u64,
|
||||
pub node_name: String,
|
||||
pub kind: K,
|
||||
}
|
||||
|
||||
/// Complete output of a simulation run, generic over event kind `K` and snapshot type `S`.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(bound(
|
||||
serialize = "K: Serialize, S: Serialize",
|
||||
deserialize = "K: serde::de::DeserializeOwned, S: serde::de::DeserializeOwned"
|
||||
))]
|
||||
pub struct SimulationTrace<K, S> {
|
||||
pub name: String,
|
||||
/// Discriminator for dashboard rendering ("gossip" or "distribution").
|
||||
#[serde(default)]
|
||||
pub trace_type: String,
|
||||
pub node_names: Vec<String>,
|
||||
pub topology_edges: Vec<(String, String)>,
|
||||
pub events: Vec<Event<K>>,
|
||||
pub snapshots_per_round: Vec<Vec<(String, S)>>,
|
||||
pub num_rounds: usize,
|
||||
}
|
||||
|
|
@ -4,8 +4,6 @@ version = "0.1.0"
|
|||
edition = "2024"
|
||||
|
||||
[features]
|
||||
default = ["tcp"]
|
||||
tcp = []
|
||||
iroh = ["dep:iroh", "dep:tokio"]
|
||||
relay = ["iroh", "dep:iroh-relay"]
|
||||
|
||||
|
|
|
|||
|
|
@ -66,7 +66,7 @@ pub fn load_or_generate_keypair(path: &Path) -> Keypair {
|
|||
}
|
||||
|
||||
/// Simple ISO-8601 UTC timestamp from epoch seconds.
|
||||
pub fn format_timestamp(secs: u64) -> String {
|
||||
fn format_timestamp(secs: u64) -> String {
|
||||
let s = secs % 60;
|
||||
let m = (secs / 60) % 60;
|
||||
let h = (secs / 3600) % 24;
|
||||
|
|
@ -77,7 +77,7 @@ pub fn format_timestamp(secs: u64) -> String {
|
|||
|
||||
/// Convert days since epoch to (year, month, day).
|
||||
/// Algorithm from <http://howardhinnant.github.io/date_algorithms.html>.
|
||||
pub fn days_to_ymd(mut days: u64) -> (u64, u64, u64) {
|
||||
fn days_to_ymd(mut days: u64) -> (u64, u64, u64) {
|
||||
days += 719468;
|
||||
let era = days / 146097;
|
||||
let doe = days - era * 146097;
|
||||
|
|
|
|||
|
|
@ -1,12 +1,8 @@
|
|||
//! Transport infrastructure for the swactor ecosystem.
|
||||
//!
|
||||
//! Provides cryptographic identity (ed25519 keypairs), encoding utilities,
|
||||
//! and TCP transport primitives.
|
||||
//! Provides cryptographic identity (ed25519 keypairs) and encoding utilities.
|
||||
|
||||
pub mod crypto;
|
||||
pub mod identity;
|
||||
|
||||
pub use swactor::transport::NodeId;
|
||||
|
||||
#[cfg(feature = "tcp")]
|
||||
pub mod tcp;
|
||||
|
|
|
|||
|
|
@ -1,300 +0,0 @@
|
|||
//! TCP transport with connection pooling and length-prefix framing.
|
||||
//!
|
||||
//! Wire format per envelope:
|
||||
//! [4 bytes: total frame len (BE u32)]
|
||||
//! [32 bytes: dest address]
|
||||
//! [4 bytes: type_tag len (BE u32)]
|
||||
//! [N bytes: type_tag UTF-8]
|
||||
//! [4 bytes: hints len (BE u32)]
|
||||
//! [M bytes: hints (JSON, may be empty)]
|
||||
//! [remaining: payload bytes]
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::io::{Read, Write};
|
||||
use std::net::{SocketAddr, TcpListener, TcpStream};
|
||||
use std::sync::Mutex;
|
||||
|
||||
use swactor::actor::ActorAddress;
|
||||
use swactor::transport::{Transport, WireEnvelope};
|
||||
use swactor::Error;
|
||||
|
||||
// ─── TcpTransport ──────────────────────────────────────────────────────────
|
||||
|
||||
/// TCP transport with connection pooling.
|
||||
///
|
||||
/// Maintains a pool of connections keyed by `SocketAddr`. Connections are
|
||||
/// created on first use and reused for subsequent sends.
|
||||
pub struct TcpTransport {
|
||||
pool: Mutex<HashMap<SocketAddr, TcpStream>>,
|
||||
/// Default destination for sends that don't specify an address.
|
||||
/// Used when the transport is registered per-address in a TransportRouter.
|
||||
default_dest: Option<SocketAddr>,
|
||||
}
|
||||
|
||||
impl TcpTransport {
|
||||
/// Create a transport that sends to a specific destination.
|
||||
pub fn new(dest: SocketAddr) -> Self {
|
||||
Self {
|
||||
pool: Mutex::new(HashMap::new()),
|
||||
default_dest: Some(dest),
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a transport with no default destination.
|
||||
/// The destination must be determined by the caller (e.g. via TransportRouter).
|
||||
pub fn pool() -> Self {
|
||||
Self {
|
||||
pool: Mutex::new(HashMap::new()),
|
||||
default_dest: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_or_connect(&self, addr: SocketAddr) -> Result<TcpStream, Error> {
|
||||
let mut pool = self.pool.lock().unwrap();
|
||||
if let Some(stream) = pool.get(&addr) {
|
||||
match stream.try_clone() {
|
||||
Ok(s) => return Ok(s),
|
||||
Err(_) => {
|
||||
pool.remove(&addr);
|
||||
}
|
||||
}
|
||||
}
|
||||
let stream =
|
||||
TcpStream::connect(addr).map_err(|e| Error::from(format!("TCP connect to {addr}: {e}")))?;
|
||||
stream
|
||||
.set_nodelay(true)
|
||||
.map_err(|e| Error::from(format!("set_nodelay: {e}")))?;
|
||||
pool.insert(addr, stream.try_clone().unwrap());
|
||||
Ok(stream)
|
||||
}
|
||||
|
||||
/// Evict a pooled connection for an address.
|
||||
pub fn evict(&self, addr: SocketAddr) {
|
||||
self.pool.lock().unwrap().remove(&addr);
|
||||
}
|
||||
|
||||
/// Send an envelope to a specific address.
|
||||
///
|
||||
/// If the write fails (e.g. stale connection from a dead peer), evicts
|
||||
/// the pooled connection and retries once with a fresh one.
|
||||
pub fn send_to(&self, addr: SocketAddr, envelope: WireEnvelope) -> Result<(), Error> {
|
||||
let buf = encode_wire_envelope(&envelope);
|
||||
let mut stream = self.get_or_connect(addr)?;
|
||||
match stream.write_all(&buf) {
|
||||
Ok(()) => Ok(()),
|
||||
Err(_) => {
|
||||
// Evict stale connection and retry once
|
||||
self.pool.lock().unwrap().remove(&addr);
|
||||
let mut stream = self.get_or_connect(addr)?;
|
||||
stream
|
||||
.write_all(&buf)
|
||||
.map_err(|e| Error::from(format!("TCP send to {addr}: {e}")))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Transport for TcpTransport {
|
||||
fn send(&self, envelope: WireEnvelope) -> Result<(), Error> {
|
||||
let dest = self
|
||||
.default_dest
|
||||
.ok_or_else(|| Error::from("TcpTransport: no default destination"))?;
|
||||
self.send_to(dest, envelope)
|
||||
}
|
||||
}
|
||||
|
||||
// ─── TcpListener wrapper ───────────────────────────────────────────────────
|
||||
|
||||
/// Accept loop that reads wire envelopes from incoming TCP connections.
|
||||
pub struct TcpAcceptor {
|
||||
listener: TcpListener,
|
||||
}
|
||||
|
||||
impl TcpAcceptor {
|
||||
/// Bind to a local address.
|
||||
pub fn bind(addr: SocketAddr) -> Result<Self, Error> {
|
||||
let listener =
|
||||
TcpListener::bind(addr).map_err(|e| Error::from(format!("TCP bind {addr}: {e}")))?;
|
||||
listener
|
||||
.set_nonblocking(true)
|
||||
.map_err(|e| Error::from(format!("set_nonblocking: {e}")))?;
|
||||
Ok(Self { listener })
|
||||
}
|
||||
|
||||
/// The local address this acceptor is bound to.
|
||||
pub fn local_addr(&self) -> SocketAddr {
|
||||
self.listener.local_addr().unwrap()
|
||||
}
|
||||
|
||||
/// Non-blocking: accept new connections, read complete envelopes from them.
|
||||
/// Returns all envelopes that could be read without blocking.
|
||||
/// Each entry contains (envelope, peer address, raw address hint bytes).
|
||||
pub fn try_recv(&self, streams: &mut Vec<TcpStream>) -> Vec<(WireEnvelope, SocketAddr, Vec<u8>)> {
|
||||
// Accept new connections
|
||||
loop {
|
||||
match self.listener.accept() {
|
||||
Ok((stream, _peer)) => {
|
||||
let _ = stream.set_nonblocking(true);
|
||||
streams.push(stream);
|
||||
}
|
||||
Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => break,
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
|
||||
// Read from all streams
|
||||
let mut envelopes = Vec::new();
|
||||
let mut dead = Vec::new();
|
||||
|
||||
for (i, stream) in streams.iter_mut().enumerate() {
|
||||
let peer = stream.peer_addr().unwrap_or_else(|_| "0.0.0.0:0".parse().unwrap());
|
||||
loop {
|
||||
match read_wire_envelope(stream) {
|
||||
Ok((env, hints)) => envelopes.push((env, peer, hints)),
|
||||
Err(ReadError::WouldBlock) => break,
|
||||
Err(ReadError::Disconnected) => {
|
||||
dead.push(i);
|
||||
break;
|
||||
}
|
||||
Err(ReadError::Other(_)) => {
|
||||
dead.push(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Remove dead connections in reverse order
|
||||
dead.sort_unstable();
|
||||
dead.dedup();
|
||||
for i in dead.into_iter().rev() {
|
||||
streams.swap_remove(i);
|
||||
}
|
||||
|
||||
envelopes
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Wire format encoding/decoding ─────────────────────────────────────────
|
||||
|
||||
/// Encode a WireEnvelope to bytes in the length-prefixed wire format.
|
||||
pub fn encode_wire_envelope(envelope: &WireEnvelope) -> Vec<u8> {
|
||||
let tag_bytes = envelope.type_tag.as_bytes();
|
||||
let frame_len: u32 = (32 + 4 + tag_bytes.len() + 4 + envelope.payload.len()) as u32;
|
||||
|
||||
let mut buf = Vec::with_capacity(4 + frame_len as usize);
|
||||
buf.extend_from_slice(&frame_len.to_be_bytes());
|
||||
buf.extend_from_slice(&envelope.dest.0);
|
||||
buf.extend_from_slice(&(tag_bytes.len() as u32).to_be_bytes());
|
||||
buf.extend_from_slice(tag_bytes);
|
||||
buf.extend_from_slice(&0u32.to_be_bytes()); // hints_len = 0
|
||||
buf.extend_from_slice(&envelope.payload);
|
||||
buf
|
||||
}
|
||||
|
||||
/// Encode a wire envelope with address hints appended after the payload.
|
||||
///
|
||||
/// Extended frame format:
|
||||
/// [4B frame_len][32B dest][4B tag_len][tag][4B hints_len][hints][payload]
|
||||
pub fn encode_wire_envelope_with_hints(envelope: &WireEnvelope, hints_bytes: &[u8]) -> Vec<u8> {
|
||||
let tag_bytes = envelope.type_tag.as_bytes();
|
||||
let frame_len: u32 = (32 + 4 + tag_bytes.len() + 4 + hints_bytes.len() + envelope.payload.len()) as u32;
|
||||
|
||||
let mut buf = Vec::with_capacity(4 + frame_len as usize);
|
||||
buf.extend_from_slice(&frame_len.to_be_bytes());
|
||||
buf.extend_from_slice(&envelope.dest.0);
|
||||
buf.extend_from_slice(&(tag_bytes.len() as u32).to_be_bytes());
|
||||
buf.extend_from_slice(tag_bytes);
|
||||
buf.extend_from_slice(&(hints_bytes.len() as u32).to_be_bytes());
|
||||
buf.extend_from_slice(hints_bytes);
|
||||
buf.extend_from_slice(&envelope.payload);
|
||||
buf
|
||||
}
|
||||
|
||||
enum ReadError {
|
||||
WouldBlock,
|
||||
Disconnected,
|
||||
#[allow(dead_code)]
|
||||
Other(std::io::Error),
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for ReadError {
|
||||
fn from(e: std::io::Error) -> Self {
|
||||
match e.kind() {
|
||||
std::io::ErrorKind::WouldBlock => ReadError::WouldBlock,
|
||||
std::io::ErrorKind::UnexpectedEof => ReadError::Disconnected,
|
||||
std::io::ErrorKind::ConnectionReset => ReadError::Disconnected,
|
||||
_ => ReadError::Other(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Read one WireEnvelope and address hints from a TCP stream.
|
||||
fn read_wire_envelope(stream: &mut TcpStream) -> Result<(WireEnvelope, Vec<u8>), ReadError> {
|
||||
let mut len_buf = [0u8; 4];
|
||||
stream.read_exact(&mut len_buf)?;
|
||||
let frame_len = u32::from_be_bytes(len_buf) as usize;
|
||||
|
||||
let mut frame = vec![0u8; frame_len];
|
||||
stream.read_exact(&mut frame)?;
|
||||
|
||||
let mut dest = [0u8; 32];
|
||||
dest.copy_from_slice(&frame[0..32]);
|
||||
|
||||
let tag_len = u32::from_be_bytes(frame[32..36].try_into().unwrap()) as usize;
|
||||
let type_tag = String::from_utf8_lossy(&frame[36..36 + tag_len]).to_string();
|
||||
|
||||
let after_tag = 36 + tag_len;
|
||||
let hints_len = u32::from_be_bytes(frame[after_tag..after_tag + 4].try_into().unwrap()) as usize;
|
||||
let hints_bytes = frame[after_tag + 4..after_tag + 4 + hints_len].to_vec();
|
||||
let payload = frame[after_tag + 4 + hints_len..].to_vec();
|
||||
|
||||
Ok((
|
||||
WireEnvelope {
|
||||
dest: ActorAddress(dest),
|
||||
type_tag,
|
||||
payload,
|
||||
},
|
||||
hints_bytes,
|
||||
))
|
||||
}
|
||||
|
||||
/// Read a single envelope and hints from a blocking stream. Public for use in tests/examples.
|
||||
pub fn read_envelope_blocking(stream: &mut TcpStream) -> Result<(WireEnvelope, Vec<u8>), Error> {
|
||||
// Temporarily set blocking mode
|
||||
stream
|
||||
.set_nonblocking(false)
|
||||
.map_err(|e| Error::from(format!("set_blocking: {e}")))?;
|
||||
let mut len_buf = [0u8; 4];
|
||||
stream
|
||||
.read_exact(&mut len_buf)
|
||||
.map_err(|e| Error::from(format!("read frame len: {e}")))?;
|
||||
let frame_len = u32::from_be_bytes(len_buf) as usize;
|
||||
|
||||
let mut frame = vec![0u8; frame_len];
|
||||
stream
|
||||
.read_exact(&mut frame)
|
||||
.map_err(|e| Error::from(format!("read frame: {e}")))?;
|
||||
|
||||
let mut dest = [0u8; 32];
|
||||
dest.copy_from_slice(&frame[0..32]);
|
||||
|
||||
let tag_len = u32::from_be_bytes(frame[32..36].try_into().unwrap()) as usize;
|
||||
let type_tag = String::from_utf8_lossy(&frame[36..36 + tag_len]).to_string();
|
||||
|
||||
let after_tag = 36 + tag_len;
|
||||
let hints_len = u32::from_be_bytes(frame[after_tag..after_tag + 4].try_into().unwrap()) as usize;
|
||||
let hints_bytes = frame[after_tag + 4..after_tag + 4 + hints_len].to_vec();
|
||||
let payload = frame[after_tag + 4 + hints_len..].to_vec();
|
||||
|
||||
let _ = stream.set_nonblocking(true);
|
||||
|
||||
Ok((
|
||||
WireEnvelope {
|
||||
dest: ActorAddress(dest),
|
||||
type_tag,
|
||||
payload,
|
||||
},
|
||||
hints_bytes,
|
||||
))
|
||||
}
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
//! # Two layers of pluggability
|
||||
//!
|
||||
//! - **[`Codec<M>`]**: HOW bytes are encoded — gRPC/protobuf, bincode, custom, etc.
|
||||
//! - **[`Transport`]**: WHERE bytes are sent — in-memory, gRPC channel, TCP, etc.
|
||||
//! - **[`Transport`]**: WHERE bytes are sent — in-memory, gRPC channel, etc.
|
||||
|
||||
use std::any::{Any, TypeId};
|
||||
use std::collections::HashMap;
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
//!
|
||||
//! These tests mirror the simulation tests in
|
||||
//! `crates/simulation/tests/distribution_sim.rs` but run against real
|
||||
//! Docker containers communicating over TCP.
|
||||
//! Docker containers communicating over iroh/QUIC.
|
||||
//!
|
||||
//! Run with: `cargo test -p docker-tests -- --ignored`
|
||||
//! Requires: Docker with compose v2
|
||||
|
|
|
|||
Loading…
Reference in a new issue