Files

821 lines
33 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Records API & Replication Dashboard</title>
<style>
:root {
--bg: #0d1117;
--card-bg: #161b22;
--border: #30363d;
--text: #c9d1d9;
--text-muted: #8b949e;
--accent: #58a6ff;
--success: #3fb950;
--warning: #d29922;
--danger: #f85149;
--primary-color: #79c0ff;
--replica-color: #bc8cff;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, monospace;
background: var(--bg);
color: var(--text);
line-height: 1.5;
padding: 20px;
}
header {
display: flex;
justify-content: space-between;
align-items: center;
padding-bottom: 16px;
margin-bottom: 20px;
border-bottom: 1px solid var(--border);
}
h1 { font-size: 1.4rem; color: #fff; display: flex; align-items: center; gap: 10px; }
.badge {
font-size: 0.75rem;
padding: 3px 8px;
border-radius: 12px;
font-weight: bold;
text-transform: uppercase;
}
.badge-primary { background: rgba(121, 192, 255, 0.15); color: var(--primary-color); }
.badge-replica { background: rgba(188, 140, 255, 0.15); color: var(--replica-color); }
.badge-ok { background: rgba(63, 185, 80, 0.2); color: var(--success); }
.badge-warn { background: rgba(210, 153, 34, 0.2); color: var(--warning); }
.badge-err { background: rgba(248, 81, 73, 0.2); color: var(--danger); }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
.grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 20px; }
@media (max-width: 900px) { .grid-2, .grid-3 { grid-template-columns: 1fr; } }
.card {
background: var(--card-bg);
border: 1px solid var(--border);
border-radius: 8px;
padding: 16px;
margin-bottom: 20px;
}
.card-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 12px;
padding-bottom: 8px;
border-bottom: 1px solid var(--border);
font-size: 0.95rem;
font-weight: 600;
}
/* Topology Bar */
.topology {
display: flex;
align-items: center;
justify-content: space-around;
padding: 16px;
background: rgba(255,255,255,0.02);
border: 1px dashed var(--border);
border-radius: 8px;
margin-bottom: 20px;
flex-wrap: wrap;
gap: 16px;
}
.node-box {
text-align: center;
padding: 12px 20px;
border-radius: 6px;
border: 1px solid var(--border);
background: var(--card-bg);
min-width: 200px;
flex: 1;
max-width: 320px;
}
.pipe {
display: flex;
flex-direction: column;
align-items: center;
gap: 6px;
font-size: 0.8rem;
color: var(--text-muted);
min-width: 220px;
padding: 0 10px;
}
.arrow {
font-size: 1.4rem;
color: var(--accent);
letter-spacing: 2px;
font-family: monospace;
font-weight: bold;
}
/* Inputs & Buttons */
input, textarea, select {
width: 100%;
background: #0d1117;
border: 1px solid var(--border);
color: #fff;
padding: 8px 10px;
border-radius: 6px;
font-family: inherit;
font-size: 0.85rem;
margin-top: 4px;
margin-bottom: 10px;
}
button {
background: #21262d;
color: var(--text);
border: 1px solid var(--border);
padding: 6px 14px;
border-radius: 6px;
cursor: pointer;
font-size: 0.85rem;
font-weight: 500;
transition: all 0.15s;
}
button:hover:not(:disabled) { background: #30363d; border-color: #8b949e; color: #fff; }
button:disabled { opacity: 0.5; cursor: not-allowed; }
button.btn-primary { background: #238636; border-color: #2ea043; color: #fff; }
button.btn-primary:hover:not(:disabled) { background: #2ea043; }
button.btn-test { background: #1f6feb; border-color: #388bfd; color: #fff; }
button.btn-test:hover:not(:disabled) { background: #388bfd; }
button.btn-switch { background: #8957e5; border-color: #a371f7; color: #fff; font-weight: 600; }
button.btn-switch:hover:not(:disabled) { background: #9a67ea; border-color: #b388ff; }
.write-box {
background: rgba(0, 0, 0, 0.25);
border: 1px solid var(--border);
border-radius: 6px;
padding: 10px;
margin-bottom: 14px;
}
/* Tables */
.table-container { max-height: 380px; overflow-y: auto; }
table { width: 100%; border-collapse: collapse; font-size: 0.82rem; }
th, td { text-align: left; padding: 6px 10px; border-bottom: 1px solid var(--border); }
th { position: sticky; top: 0; background: var(--card-bg); color: var(--text-muted); font-size: 0.75rem; }
tr.synced { background: rgba(63, 185, 80, 0.05); }
tr.lagging { background: rgba(248, 81, 73, 0.1); }
/* Logs */
.log-box {
height: 180px;
overflow-y: auto;
background: #090d13;
border: 1px solid var(--border);
border-radius: 6px;
padding: 10px;
font-size: 0.78rem;
font-family: monospace;
}
.log-line { margin-bottom: 4px; border-bottom: 1px dotted rgba(255,255,255,0.05); padding-bottom: 2px; }
.log-info { color: var(--accent); }
.log-pass { color: var(--success); }
.log-fail { color: var(--danger); }
</style>
</head>
<body>
<header>
<div>
<h1>⚡ Records API & Replication Monitor</h1>
<p style="color: var(--text-muted); font-size: 0.85rem; margin-top: 4px;">
Interactive validation and real-time visualization of SQLite append-only journals & mTLS failover
</p>
</div>
<div style="display:flex; align-items:center; gap: 10px;">
<label style="font-size:0.8rem; display:flex; align-items:center; gap:6px; cursor:pointer;">
<input type="checkbox" id="autoPoll" checked style="width:auto; margin:0;" /> Live Auto-Poll (1.5s)
</label>
<button onclick="fetchAll()">🔄 Refresh Now</button>
</div>
</header>
<!-- Node Addresses & Endpoints Setup -->
<div class="card" style="padding: 10px 16px;">
<div style="display:flex; gap: 20px; align-items:center; flex-wrap:wrap; font-size:0.85rem;">
<div style="flex:1; min-width:240px;">
<label>Node 1 Base URL:</label>
<input type="text" id="node1Url" value="https://127.0.0.1:3000" style="margin-bottom:0;" />
</div>
<div style="flex:1; min-width:240px;">
<label>Node 2 Base URL:</label>
<input type="text" id="node2Url" value="https://127.0.0.1:3001" style="margin-bottom:0;" />
</div>
<div style="flex:1; min-width:180px;">
<label>Replication Bearer Token:</label>
<input type="password" id="replToken" value="change-me" style="margin-bottom:0;" />
</div>
<div style="min-width:220px; display:flex; align-items:flex-end;">
<label style="font-size:0.8rem; display:flex; align-items:center; gap:6px; cursor:pointer; padding-bottom:6px; color:var(--success);">
<input type="checkbox" id="trustAllCerts" checked style="width:auto; margin:0;" /> 🛡️ Trust Self-Signed Certs (Proxy)
</label>
</div>
</div>
</div>
<!-- Live Replication Pipeline Banner -->
<div class="topology">
<!-- Node 1 Box -->
<div class="node-box" id="node1Box">
<div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:4px;">
<span style="font-size:0.75rem; color:var(--text-muted); font-weight:600;">Node 1 (:3000)</span>
<span id="node1RoleBadge" class="badge badge-primary">PRIMARY</span>
</div>
<div style="font-size: 1.3rem; font-weight: bold; margin-top: 5px;" id="node1Head">Seq: --</div>
<div style="font-size: 0.72rem; color: var(--text-muted);" id="node1Id">ID: --</div>
<div style="font-size: 0.72rem; color: var(--text-muted);" id="node1SyncState">State: --</div>
</div>
<!-- Directional Pipeline & Switchover Control -->
<div class="pipe">
<span id="pipeState" class="badge badge-ok">CONNECTED</span>
<div id="pipeArrow" class="arrow">━━━━ ◄─pull── ━━━━</div>
<div id="pipeDetails" style="font-size:0.78rem;">Lag: <span id="lagRecords">0</span> records</div>
<button id="switchoverBtn" class="btn-switch" style="margin-top:4px;" onclick="performSwitchover()">
🔀 Graceful Switchover
</button>
</div>
<!-- Node 2 Box -->
<div class="node-box" id="node2Box">
<div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:4px;">
<span style="font-size:0.75rem; color:var(--text-muted); font-weight:600;">Node 2 (:3001)</span>
<span id="node2RoleBadge" class="badge badge-replica">REPLICA</span>
</div>
<div style="font-size: 1.3rem; font-weight: bold; margin-top: 5px;" id="node2Head">Seq: --</div>
<div style="font-size: 0.72rem; color: var(--text-muted);" id="node2Id">ID: --</div>
<div style="font-size: 0.72rem; color: var(--text-muted);" id="node2SyncState">State: --</div>
</div>
</div>
<!-- Main Side-by-Side Journal Tables with Dedicated Write Box Per Instance -->
<div class="grid-2">
<!-- Node 1 Column -->
<div class="card">
<div class="card-header">
<div style="display:flex; align-items:center; gap:8px;">
<span>📖 Node 1 Records</span>
<span id="node1TableRole" class="badge badge-primary">PRIMARY</span>
</div>
<span id="node1Count" class="badge badge-primary">0 records</span>
</div>
<!-- Write Box for Node 1 -->
<div class="write-box">
<div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:6px;">
<span style="font-size:0.8rem; font-weight:600;">✍️ Insert Record (Node 1)</span>
<span style="font-size:0.72rem; color:var(--text-muted);">POST /api/records</span>
</div>
<textarea id="node1RecordData" rows="2" style="font-family:monospace; margin-bottom:8px;">{ "version": 1, "message": "Written to Node 1" }</textarea>
<button class="btn-primary" style="width:100%;" onclick="submitRecordToNode('node1')">
Send to Node 1 (:3000)
</button>
</div>
<!-- Node 1 Table -->
<div class="table-container">
<table id="node1Table">
<thead><tr><th>Seq</th><th>UUID</th><th>Date (UTC)</th><th>Payload</th></tr></thead>
<tbody><tr><td colspan="4" style="text-align:center; color:var(--text-muted)">Loading...</td></tr></tbody>
</table>
</div>
</div>
<!-- Node 2 Column -->
<div class="card">
<div class="card-header">
<div style="display:flex; align-items:center; gap:8px;">
<span>📖 Node 2 Records</span>
<span id="node2TableRole" class="badge badge-replica">REPLICA</span>
</div>
<span id="node2Count" class="badge badge-replica">0 records</span>
</div>
<!-- Write Box for Node 2 -->
<div class="write-box">
<div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:6px;">
<span style="font-size:0.8rem; font-weight:600;">✍️ Insert Record (Node 2)</span>
<span style="font-size:0.72rem; color:var(--text-muted);">POST /api/records</span>
</div>
<textarea id="node2RecordData" rows="2" style="font-family:monospace; margin-bottom:8px;">{ "version": 1, "message": "Written to Node 2" }</textarea>
<button class="btn-primary" style="width:100%;" onclick="submitRecordToNode('node2')">
Send to Node 2 (:3001)
</button>
</div>
<!-- Node 2 Table -->
<div class="table-container">
<table id="node2Table">
<thead><tr><th>Seq</th><th>UUID</th><th>Date (UTC)</th><th>Payload</th></tr></thead>
<tbody><tr><td colspan="4" style="text-align:center; color:var(--text-muted)">Loading...</td></tr></tbody>
</table>
</div>
</div>
</div>
<!-- Action & Validation Panel -->
<div class="grid-3">
<!-- Automated Validation Tests -->
<div class="card">
<div class="card-header"><span>🧪 Automated Tests</span></div>
<div style="display:flex; flex-direction:column; gap:8px;">
<button class="btn-test" onclick="runSwitchoverTest()">🔀 Test Graceful Role Switchover</button>
<button class="btn-test" onclick="runReplicaWriteGuardTest()">🛡️ Test Replica Read-Only Guard (Expect 403)</button>
<button class="btn-test" onclick="runSchemaValidationTest()">⚠️ Test Invalid Schema Payload (Expect 400)</button>
<button class="btn-test" onclick="runSyncLatencyTest()">⏱️ Test Insert-to-Replica Propagation</button>
</div>
</div>
<!-- Manual Role Management -->
<div class="card">
<div class="card-header"><span>🎛️ Role Controls</span></div>
<div style="display:flex; flex-direction:column; gap:8px;">
<button onclick="manualPromote('node1')">👑 Promote Node 1 to Primary</button>
<button onclick="manualPromote('node2')">👑 Promote Node 2 to Primary</button>
<button onclick="manualStepDown()">⬇️ Step Down Active Primary</button>
</div>
</div>
<!-- Active Schema & Admin -->
<div class="card">
<div class="card-header">
<span>⚙️ Schema Management</span>
<button onclick="fetchSchema()" style="padding:2px 8px; font-size:0.75rem;">Inspect</button>
</div>
<pre id="schemaView" style="font-size:0.75rem; background:#0d1117; padding:8px; border-radius:4px; max-height:85px; overflow:auto;">Click Inspect to view</pre>
<button onclick="reloadSchema()" style="margin-top:8px; width:100%;">Trigger Hot-Reload (`/reload-config`)</button>
</div>
</div>
<!-- Test Console Log -->
<div class="card">
<div class="card-header">
<span>🖥️ Live Verification Console</span>
<button onclick="document.getElementById('consoleLog').innerHTML = '';" style="font-size:0.75rem;">Clear</button>
</div>
<div class="log-box" id="consoleLog"></div>
</div>
<script>
// Transparently proxy external/HTTPS instance calls via dashboard proxy when self-signed cert trust is active
const nativeFetch = window.fetch;
window.fetch = function(input, init) {
const trustAll = document.getElementById('trustAllCerts')?.checked ?? true;
if (trustAll) {
const urlStr = typeof input === 'string' ? input : (input && input.url ? input.url : null);
if (urlStr) {
try {
const parsed = new URL(urlStr, window.location.href);
if (parsed.origin !== window.location.origin) {
const proxyBase = (window.location.protocol === 'file:') ? 'http://127.0.0.1:8080' : '';
const proxiedUrl = `${proxyBase}/proxy?url=${encodeURIComponent(urlStr)}`;
if (typeof input === 'string') {
input = proxiedUrl;
} else {
input = new Request(proxiedUrl, input);
}
}
} catch (_) {}
}
}
return nativeFetch(input, init);
};
let node1HeadSeq = 0;
let node2HeadSeq = 0;
let currentNode1Role = 'unknown';
let currentNode2Role = 'unknown';
function log(msg, type = 'info') {
const box = document.getElementById('consoleLog');
const time = new Date().toISOString().split('T')[1].replace('Z','');
const div = document.createElement('div');
div.className = `log-line log-${type}`;
div.innerText = `[${time}] ${msg}`;
box.prepend(div);
}
function getUrls() {
const n1 = document.getElementById('node1Url')?.value || 'https://127.0.0.1:3000';
const n2 = document.getElementById('node2Url')?.value || 'https://127.0.0.1:3001';
return {
node1: n1.replace(/\/+$/, ''),
node2: n2.replace(/\/+$/, ''),
token: document.getElementById('replToken')?.value || 'change-me'
};
}
async function fetchAll() {
const { node1, node2 } = getUrls();
let node1Status = null;
let node2Status = null;
let node1Records = [];
let node2Records = [];
// 1. Query Node 1 Status & Records
try {
const sRes = await fetch(`${node1}/api/replication/status`);
if (sRes.ok) node1Status = await sRes.json();
} catch (_) {}
try {
const rRes = await fetch(`${node1}/api/records`);
if (rRes.ok) node1Records = await rRes.json();
} catch (_) {}
// 2. Query Node 2 Status & Records
try {
const sRes = await fetch(`${node2}/api/replication/status`);
if (sRes.ok) node2Status = await sRes.json();
} catch (_) {}
try {
const rRes = await fetch(`${node2}/api/records`);
if (rRes.ok) node2Records = await rRes.json();
} catch (_) {}
// Update Node 1 UI elements
if (node1Status) {
currentNode1Role = node1Status.role || 'replica';
node1HeadSeq = node1Status.local_head_seq ?? (node1Records.length > 0 ? node1Records[0].seq : 0);
document.getElementById('node1Head').innerText = `Seq: #${node1HeadSeq}`;
document.getElementById('node1Id').innerText = `ID: ${node1Status.instance_id.slice(0, 8)}…`;
document.getElementById('node1SyncState').innerText = `State: ${node1Status.sync?.state || 'idle'}`;
updateRoleBadge('node1RoleBadge', currentNode1Role);
updateRoleBadge('node1TableRole', currentNode1Role);
} else {
currentNode1Role = 'offline';
document.getElementById('node1Head').innerText = `Offline`;
document.getElementById('node1Id').innerText = `ID: --`;
document.getElementById('node1SyncState').innerText = `State: --`;
updateRoleBadge('node1RoleBadge', 'offline');
updateRoleBadge('node1TableRole', 'offline');
}
renderTable('node1Table', node1Records, Math.max(node1HeadSeq, node2HeadSeq));
document.getElementById('node1Count').innerText = `${node1Records.length} records`;
// Update Node 2 UI elements
if (node2Status) {
currentNode2Role = node2Status.role || 'replica';
node2HeadSeq = node2Status.local_head_seq ?? (node2Records.length > 0 ? node2Records[0].seq : 0);
document.getElementById('node2Head').innerText = `Seq: #${node2HeadSeq}`;
document.getElementById('node2Id').innerText = `ID: ${node2Status.instance_id.slice(0, 8)}…`;
document.getElementById('node2SyncState').innerText = `State: ${node2Status.sync?.state || 'idle'}`;
updateRoleBadge('node2RoleBadge', currentNode2Role);
updateRoleBadge('node2TableRole', currentNode2Role);
} else {
currentNode2Role = 'offline';
document.getElementById('node2Head').innerText = `Offline`;
document.getElementById('node2Id').innerText = `ID: --`;
document.getElementById('node2SyncState').innerText = `State: --`;
updateRoleBadge('node2RoleBadge', 'offline');
updateRoleBadge('node2TableRole', 'offline');
}
renderTable('node2Table', node2Records, Math.max(node1HeadSeq, node2HeadSeq));
document.getElementById('node2Count').innerText = `${node2Records.length} records`;
// 3. Update the Live Replication Pipeline Banner dynamically
updatePipelineBanner(node1Status, node2Status);
}
function updateRoleBadge(badgeId, role) {
const el = document.getElementById(badgeId);
if (!el) return;
if (role === 'primary') {
el.className = 'badge badge-primary';
el.innerText = 'PRIMARY';
} else if (role === 'replica') {
el.className = 'badge badge-replica';
el.innerText = 'REPLICA';
} else {
el.className = 'badge badge-err';
el.innerText = 'OFFLINE';
}
}
function updatePipelineBanner(status1, status2) {
const arrowEl = document.getElementById('pipeArrow');
const detailsEl = document.getElementById('pipeDetails');
const stateBadge = document.getElementById('pipeState');
const switchBtn = document.getElementById('switchoverBtn');
if (currentNode1Role === 'primary' && currentNode2Role === 'replica') {
// Node 2 pulls from Node 1
arrowEl.innerText = '━━━━ ◄─pull── ━━━━';
const lag = status2?.lag_records ?? Math.max(0, node1HeadSeq - node2HeadSeq);
detailsEl.innerHTML = `Node 2 pulling from Node 1 (Lag: <strong>${lag}</strong> records)`;
const syncState = status2?.sync?.state || 'idle';
stateBadge.innerText = syncState.toUpperCase();
stateBadge.className = `badge ${syncState === 'idle' ? 'badge-ok' : syncState === 'syncing' ? 'badge-warn' : 'badge-err'}`;
switchBtn.innerText = '🔀 Switchover: Node 1 ➔ Node 2';
switchBtn.disabled = false;
} else if (currentNode2Role === 'primary' && currentNode1Role === 'replica') {
// Node 1 pulls from Node 2
arrowEl.innerText = '━━━━ ──pull─► ━━━━';
const lag = status1?.lag_records ?? Math.max(0, node2HeadSeq - node1HeadSeq);
detailsEl.innerHTML = `Node 1 pulling from Node 2 (Lag: <strong>${lag}</strong> records)`;
const syncState = status1?.sync?.state || 'idle';
stateBadge.innerText = syncState.toUpperCase();
stateBadge.className = `badge ${syncState === 'idle' ? 'badge-ok' : syncState === 'syncing' ? 'badge-warn' : 'badge-err'}`;
switchBtn.innerText = '🔀 Switchover: Node 2 ➔ Node 1';
switchBtn.disabled = false;
} else if (currentNode1Role === 'primary' && currentNode2Role === 'primary') {
arrowEl.innerText = '⚠️ DUAL PRIMARY ⚠️';
detailsEl.innerText = 'Warning: Split-brain condition detected!';
stateBadge.innerText = 'SPLIT BRAIN';
stateBadge.className = 'badge badge-err';
switchBtn.innerText = '🔀 Resolve Split-Brain';
switchBtn.disabled = false;
} else {
arrowEl.innerText = '━━━━ ✖✖✖ ━━━━';
detailsEl.innerText = 'No active primary/replica replication stream';
stateBadge.innerText = 'DISCONNECTED';
stateBadge.className = 'badge badge-err';
switchBtn.innerText = '🔀 Graceful Switchover';
switchBtn.disabled = true;
}
}
function renderTable(tableId, records, globalHead) {
const tbody = document.querySelector(`#${tableId} tbody`);
if (!records || records.length === 0) {
tbody.innerHTML = `<tr><td colspan="4" style="text-align:center; color:var(--text-muted)">Database is empty</td></tr>`;
return;
}
tbody.innerHTML = records.map(r => `
<tr class="${r.seq === globalHead ? 'synced' : ''}">
<td><strong>#${r.seq}</strong></td>
<td style="font-family:monospace; font-size:0.75rem;" title="${r.uuid}">${r.uuid.slice(0, 8)}…</td>
<td style="font-size:0.75rem;">${r.date.replace('T', ' ').replace('Z', '')}</td>
<td><code style="font-size:0.75rem;">${JSON.stringify(r.data)}</code></td>
</tr>
`).join('');
}
// Submit a record directly to a specific node
async function submitRecordToNode(whichNode) {
const { node1, node2 } = getUrls();
const targetUrl = whichNode === 'node1' ? node1 : node2;
const nodeName = whichNode === 'node1' ? 'Node 1 (:3000)' : 'Node 2 (:3001)';
const textarea = document.getElementById(`${whichNode}RecordData`);
let payload;
try {
payload = JSON.parse(textarea.value);
} catch (err) {
log(`Invalid JSON in ${nodeName} write box: ${err.message}`, 'fail');
return;
}
try {
const res = await fetch(`${targetUrl}/api/records`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ data: payload })
});
const text = await res.text();
if (res.status === 201) {
const data = JSON.parse(text);
log(`Write SUCCESS on ${nodeName}! Created seq #${data.seq} (UUID: ${data.uuid.slice(0, 8)})`, 'pass');
fetchAll();
} else if (res.status === 403) {
log(`${nodeName} BLOCKED write [HTTP 403 Forbidden] (Read-Only Replica): ${text}`, 'fail');
} else {
log(`${nodeName} returned [HTTP ${res.status}]: ${text}`, 'fail');
}
} catch (e) {
log(`Network failure writing to ${nodeName}: ${e.message}`, 'fail');
}
}
// Perform graceful switchover from the active Primary
async function performSwitchover() {
const { node1, node2 } = getUrls();
let primaryUrl = null;
let replicaUrl = null;
let primaryName = '';
let replicaName = '';
if (currentNode1Role === 'primary') {
primaryUrl = node1;
replicaUrl = node2;
primaryName = 'Node 1';
replicaName = 'Node 2';
} else if (currentNode2Role === 'primary') {
primaryUrl = node2;
replicaUrl = node1;
primaryName = 'Node 2';
replicaName = 'Node 1';
} else {
log('Switchover aborted: No active primary detected', 'fail');
return;
}
log(`Initiating graceful switchover: ${primaryName} ➔ ${replicaName}...`, 'info');
const btn = document.getElementById('switchoverBtn');
if (btn) btn.disabled = true;
try {
const res = await fetch(`${primaryUrl}/api/admin/switchover`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ replica_url: replicaUrl, timeout_secs: 15 })
});
const text = await res.text();
if (res.ok) {
log(`PASS: Graceful switchover completed! ${replicaName} is now Primary; ${primaryName} is Replica.`, 'pass');
await fetchAll();
} else {
log(`FAIL: Switchover rejected [HTTP ${res.status}]: ${text}`, 'fail');
}
} catch (e) {
log(`Switchover execution error: ${e.message}`, 'fail');
} finally {
if (btn) btn.disabled = false;
}
}
// Manual promotion helper
async function manualPromote(whichNode) {
const { node1, node2 } = getUrls();
const targetUrl = whichNode === 'node1' ? node1 : node2;
const nodeName = whichNode === 'node1' ? 'Node 1' : 'Node 2';
log(`Promoting ${nodeName} to Primary...`, 'info');
try {
const res = await fetch(`${targetUrl}/api/admin/promote`, { method: 'POST' });
const text = await res.text();
log(`Promote result on ${nodeName} [HTTP ${res.status}]: ${text}`, res.ok ? 'pass' : 'fail');
fetchAll();
} catch (e) {
log(`Promote failed: ${e.message}`, 'fail');
}
}
// Manual step-down helper
async function manualStepDown() {
const { node1, node2 } = getUrls();
const primaryUrl = (currentNode1Role === 'primary') ? node1 : (currentNode2Role === 'primary' ? node2 : null);
if (!primaryUrl) {
log('No active primary found to step down', 'fail');
return;
}
log(`Stepping down active primary (${primaryUrl})...`, 'info');
try {
const res = await fetch(`${primaryUrl}/api/admin/step-down`, { method: 'POST' });
const text = await res.text();
log(`Step-down result [HTTP ${res.status}]: ${text}`, res.ok ? 'pass' : 'fail');
fetchAll();
} catch (e) {
log(`Step-down failed: ${e.message}`, 'fail');
}
}
// TEST 1: Role Switchover automated test
async function runSwitchoverTest() {
log('Starting Automated Role Switchover verification test...', 'info');
await performSwitchover();
}
// TEST 2: Replica read-only safety guard
async function runReplicaWriteGuardTest() {
const { node1, node2 } = getUrls();
const target = (currentNode2Role === 'replica') ? node2 : (currentNode1Role === 'replica' ? node1 : node2);
const targetName = (target === node2) ? 'Node 2 (:3001)' : 'Node 1 (:3000)';
log(`Testing Write Guard against ${targetName} (${target}/api/records)...`, 'info');
try {
const res = await fetch(`${target}/api/records`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ data: { message: "Direct write forbidden test" } })
});
if (res.status === 403) {
const msg = await res.text();
log(`PASS: ${targetName} blocked write with HTTP 403 Forbidden! Response: "${msg}"`, 'pass');
} else {
log(`FAIL: ${targetName} answered with HTTP ${res.status} instead of expected 403!`, 'fail');
}
} catch (e) {
log(`Test failed due to network error: ${e.message}`, 'fail');
}
}
// TEST 3: Schema validation test
async function runSchemaValidationTest() {
const { node1, node2 } = getUrls();
const primaryUrl = (currentNode1Role === 'primary') ? node1 : (currentNode2Role === 'primary' ? node2 : node1);
const primaryName = (primaryUrl === node1) ? 'Node 1' : 'Node 2';
log(`Starting Schema Rejection test on ${primaryName} (sending invalid payload)...`, 'info');
try {
const res = await fetch(`${primaryUrl}/api/records`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ data: { unknown_xyz_field: 999999, version: "not-an-integer" } })
});
const msg = await res.text();
if (res.status === 400) {
log(`PASS: Schema validation rejected bad payload [HTTP 400]: "${msg}"`, 'pass');
} else {
log(`FAIL: Expected HTTP 400, got ${res.status}: "${msg}"`, 'fail');
}
} catch (e) {
log(`Test failed: ${e.message}`, 'fail');
}
}
// TEST 4: Sync Latency propagation benchmark
async function runSyncLatencyTest() {
const { node1, node2 } = getUrls();
const primaryUrl = (currentNode1Role === 'primary') ? node1 : (currentNode2Role === 'primary' ? node2 : null);
const replicaUrl = (currentNode1Role === 'primary') ? node2 : (currentNode2Role === 'primary' ? node1 : null);
if (!primaryUrl || !replicaUrl) {
log('Latency test aborted: Cluster does not have a distinct Primary and Replica', 'fail');
return;
}
const testMsg = `probe-${Date.now()}`;
log(`Triggering probe write on Primary with marker: ${testMsg}...`, 'info');
const startTime = performance.now();
try {
const res = await fetch(`${primaryUrl}/api/records`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ data: { message: testMsg, msg: testMsg, version: 1 } })
});
if (!res.ok) throw new Error(`Primary write failed with HTTP ${res.status}`);
const created = await res.json();
const targetSeq = created.seq;
log(`Record created with seq #${targetSeq}. Waiting for Replica sync poll...`, 'info');
let attempts = 0;
const interval = setInterval(async () => {
attempts++;
try {
const statusRes = await fetch(`${replicaUrl}/api/replication/status`);
const status = await statusRes.json();
if (status.local_head_seq >= targetSeq) {
clearInterval(interval);
const elapsed = ((performance.now() - startTime) / 1000).toFixed(2);
log(`PASS: Record #${targetSeq} successfully replicated in ${elapsed}s (${attempts} checks)!`, 'pass');
fetchAll();
} else if (attempts >= 40) {
clearInterval(interval);
log(`FAIL: Replica did not reach seq #${targetSeq} within 20 seconds.`, 'fail');
}
} catch (e) {
clearInterval(interval);
log(`Replication polling error: ${e.message}`, 'fail');
}
}, 500);
} catch (e) {
log(`Probe failed: ${e.message}`, 'fail');
}
}
async function fetchSchema() {
const { node1, node2 } = getUrls();
const primaryUrl = (currentNode1Role === 'primary') ? node1 : (currentNode2Role === 'primary' ? node2 : node1);
try {
const res = await fetch(`${primaryUrl}/api/admin/config`);
const data = await res.json();
document.getElementById('schemaView').innerText = JSON.stringify(data, null, 2);
log(`Fetched active configuration schema.`, 'info');
} catch (e) {
log(`Failed to fetch schema: ${e.message}`, 'fail');
}
}
async function reloadSchema() {
const { node1, node2 } = getUrls();
const primaryUrl = (currentNode1Role === 'primary') ? node1 : (currentNode2Role === 'primary' ? node2 : node1);
try {
const res = await fetch(`${primaryUrl}/api/admin/reload-config`, { method: 'POST' });
const text = await res.text();
log(`Reload schema result [HTTP ${res.status}]: ${text}`, res.ok ? 'pass' : 'fail');
fetchSchema();
} catch (e) {
log(`Reload failed: ${e.message}`, 'fail');
}
}
// Auto-polling cycle
setInterval(() => {
if (document.getElementById('autoPoll').checked) {
fetchAll();
}
}, 1500);
// Initial load
fetchAll();
</script>
</body>
</html>