Add failover channel events to activity journal
Build and Deploy Gateway / build-and-push (push) Successful in 24s
Build and Deploy Gateway / deploy (push) Successful in 7s

This commit is contained in:
2026-08-27 14:36:08 +03:00
parent 4a566e082a
commit 8f2f418569
11 changed files with 388 additions and 77 deletions
+65 -15
View File
@@ -91,7 +91,42 @@ export function createFailoverService(dependencies: FailoverServiceDependencies)
let timer: NodeJS.Timeout | null = null;
let collectorEnabled: boolean | null = null;
let roundPromise: Promise<void> | null = null;
let roundGeneration: number | null = null;
let decisionMemory: FailoverDecisionMemory | undefined;
const healthMemory: Record<FailoverRole, 'healthy' | 'unhealthy' | undefined> = {
primary: undefined,
reserve: undefined,
};
function clearHealthMemory() {
healthMemory.primary = undefined;
healthMemory.reserve = undefined;
}
function recordHealthTransition(
role: FailoverRole,
health: FailoverHealth,
capturedGeneration?: number,
) {
if (capturedGeneration !== undefined && capturedGeneration !== generation) return;
if (health !== 'healthy' && health !== 'unhealthy') return;
const previous = healthMemory[role];
healthMemory[role] = health;
if (previous === health) return;
if (previous === undefined && health === 'healthy') return;
const type = health === 'unhealthy'
? role === 'primary' ? 'failover.primary_unavailable' : 'failover.reserve_unavailable'
: role === 'primary' ? 'failover.primary_recovered' : 'failover.reserve_recovered';
dependencies.onEvent?.({
type,
severity: health === 'unhealthy' ? 'warning' : 'info',
source: 'failover',
dedupeKey: null,
data: {
role,
reason: health === 'unhealthy' ? 'probe-failed' : 'probe-recovered',
},
});
}
let snapshot = createIdleFailoverSnapshot(dependencies.state.read().failoverPolicy, epoch, sequence);
function appliedMatchesDesired(state: StoredState) {
@@ -133,6 +168,7 @@ export function createFailoverService(dependencies: FailoverServiceDependencies)
generation += 1;
clearTimer();
decisionMemory = undefined;
clearHealthMemory();
await disableCollector();
const idle = createIdleFailoverSnapshot(policy, epoch, sequence);
if (passiveRole) {
@@ -181,6 +217,7 @@ export function createFailoverService(dependencies: FailoverServiceDependencies)
const running = await dependencies.runtime.isRunning();
if (!running || !state.appliedFailoverPolicy || !currentRole(state)) {
generation += 1;
clearHealthMemory();
clearTimer();
await disableCollector();
const pending = activeSnapshot(state, 'idle');
@@ -355,6 +392,8 @@ export function createFailoverService(dependencies: FailoverServiceDependencies)
reserve = { health: 'unknown' as const, failingServiceIds: [] };
}
if (capturedGeneration !== generation || !dependencies.state.read().failoverPolicy.enabled) return;
recordHealthTransition('primary', primary.health, capturedGeneration);
recordHealthTransition('reserve', reserve.health, capturedGeneration);
const activityResponse = record(await dependencies.dataplane.readFailoverActivity(
policy.trafficGuard.thresholdBytesPerSecond,
));
@@ -437,15 +476,6 @@ export function createFailoverService(dependencies: FailoverServiceDependencies)
data: { reason: decision.reason },
});
}
if (primary.health === 'healthy' && previousSnapshot.primary.health === 'unhealthy') {
dependencies.onEvent?.({
type: 'failover.recovered',
severity: 'info',
source: 'failover',
dedupeKey: `failover.recovered:${state.revision}:primary`,
data: { role: 'primary', reason: decision.reason },
});
}
if (decision.switchTo) {
try {
const switched = await dependencies.serialize(async () => {
@@ -522,11 +552,25 @@ export function createFailoverService(dependencies: FailoverServiceDependencies)
}
}
async function runRound() {
if (roundPromise) return roundPromise;
async function runRound(): Promise<void> {
if (roundPromise) {
const pending = roundPromise;
if (roundGeneration === generation) return pending;
try {
await pending;
} catch {
// The original caller owns the stale round error; continue with current-generation work.
}
if (roundPromise && roundPromise !== pending) return roundPromise;
return runRound();
}
const capturedGeneration = generation;
roundPromise = performRound(capturedGeneration).finally(() => {
roundPromise = null;
let trackedPromise: Promise<void>;
trackedPromise = performRound(capturedGeneration).finally(() => {
if (roundPromise === trackedPromise) {
roundPromise = null;
roundGeneration = null;
}
if (capturedGeneration === generation && snapshot.reason === 'checking-channels') {
const failed = activeSnapshot(dependencies.state.read(), 'error');
failed.reason = 'health-unknown';
@@ -541,7 +585,9 @@ export function createFailoverService(dependencies: FailoverServiceDependencies)
schedule(Math.min(policy.intervalMs, decisionDelay));
}
});
return roundPromise;
roundPromise = trackedPromise;
roundGeneration = capturedGeneration;
return trackedPromise;
}
function save(value: unknown) {
@@ -574,6 +620,7 @@ export function createFailoverService(dependencies: FailoverServiceDependencies)
};
});
decisionMemory = undefined;
if (before.failoverPolicy.enabled !== policy.enabled) clearHealthMemory();
if (before.failoverPolicy.enabled !== policy.enabled) {
dependencies.onEvent?.({
type: policy.enabled ? 'failover.enabled' : 'failover.disabled',
@@ -637,7 +684,7 @@ export function createFailoverService(dependencies: FailoverServiceDependencies)
if (!state.failoverPolicy.enabled || !state.appliedFailoverPolicy || !currentRole(state)) {
throw new HarborError('REQUEST_INVALID');
}
generation += 1;
const capturedGeneration = ++generation;
clearTimer();
const checkedAt = now().toISOString();
publish({ ...snapshot, reason: 'checking-channels' });
@@ -652,6 +699,9 @@ export function createFailoverService(dependencies: FailoverServiceDependencies)
primary = { health: 'unknown' as const, failingServiceIds: [] };
reserve = { health: 'unknown' as const, failingServiceIds: [] };
}
if (capturedGeneration !== generation || !dependencies.state.read().failoverPolicy.enabled) return;
recordHealthTransition('primary', primary.health, capturedGeneration);
recordHealthTransition('reserve', reserve.health, capturedGeneration);
const next = activeSnapshot(dependencies.state.read(), 'observing');
next.primary = { ...next.primary, ...primary, checkedAt, stateSince: checkedAt };
next.reserve = { ...next.reserve, ...reserve, checkedAt, stateSince: checkedAt };
+8 -1
View File
@@ -6,7 +6,10 @@ export const ACTIVITY_EVENT_TYPES = [
'subscription.added', 'subscription.refreshed', 'subscription.refresh_failed', 'subscription.deleted',
'failover.enabled', 'failover.disabled', 'failover.paused', 'failover.resumed',
'failover.waiting_for_idle', 'failover.switched', 'failover.switch_failed',
'failover.both_unhealthy', 'failover.recovered', 'journal.recovered',
'failover.both_unhealthy',
'failover.primary_unavailable', 'failover.primary_recovered',
'failover.reserve_unavailable', 'failover.reserve_recovered',
'failover.recovered', 'journal.recovered',
] as const;
export type ActivityEventType = typeof ACTIVITY_EVENT_TYPES[number];
@@ -49,6 +52,10 @@ const ALLOWED_DATA_KEYS: Record<ActivityEventType, readonly string[]> = {
'failover.switched': ['fromRole', 'toRole', 'primaryLabel', 'reserveLabel', 'reason', 'manual'],
'failover.switch_failed': ['fromRole', 'toRole', 'reason', 'errorCode'],
'failover.both_unhealthy': ['reason'],
'failover.primary_unavailable': ['role', 'reason'],
'failover.primary_recovered': ['role', 'reason'],
'failover.reserve_unavailable': ['role', 'reason'],
'failover.reserve_recovered': ['role', 'reason'],
'failover.recovered': ['role', 'reason'],
'journal.recovered': [],
};
+3 -3
View File
@@ -1,7 +1,7 @@
export const HARBOR_VERSIONS = Object.freeze({
macClient: '0.30.6',
gatewayClient: '0.31.6',
gatewayBackend: '0.31.0',
macClient: '0.31.0',
gatewayClient: '0.32.0',
gatewayBackend: '0.32.0',
});
export interface ParsedVersion {
@@ -1,6 +1,6 @@
import { useEffect, useRef, useState } from 'react';
import { assertActivityJournalPage, type ActivityJournalEvent } from '../../../shared/activityJournal.js';
import { compactActivityJournalEvents, refreshStreakCopy, type ActivityJournalDisplayItem } from './activityJournalModel.js';
import { compactActivityJournalEvents, activityJournalEventCopy, refreshStreakCopy, type ActivityJournalDisplayItem } from './activityJournalModel.js';
import { Drawer } from '../../ui/Drawer.js';
import { RailAction } from '../../ui/RailAction.js';
@@ -56,28 +56,19 @@ export function ActivityJournalToggle({ feature, open, onToggle }: {
</RailAction>;
}
function eventCopy(event: ActivityJournalEvent) {
const value = event.data;
const copies: Record<string, [string, string]> = {
'connection.started': ['VPN включён', [value.profileLabel, value.serverLabel].filter(Boolean).join(' · ')],
'connection.stopped': ['VPN выключен', 'Остановлен пользователем'],
'connection.failed': ['VPN не запущен', String(value.errorCode || '')],
'subscription.added': ['Подписка добавлена', `${value.profileLabel || ''} · серверов: ${value.serverCount || 0}`],
'subscription.refreshed': ['Подписка обновлена', `${value.profileLabel || ''} · серверов: ${value.serverCount || 0} · +${value.added || 0} / ${value.removed || 0}`],
'subscription.refresh_failed': ['Подписка не обновлена', `${value.profileLabel || ''} · ${value.errorCode || ''}`],
'subscription.deleted': ['Подписка удалена', String(value.profileLabel || '')],
'failover.enabled': ['Резервный канал включён', 'Мониторинг начнётся после активации dual-config'],
'failover.disabled': ['Резервный канал выключен', 'Автоматика полностью остановлена'],
'failover.paused': ['Автопереключение на паузе', 'Проверки продолжаются'],
'failover.resumed': ['Автопереключение возобновлено', ''],
'failover.waiting_for_idle': ['Переключение отложено', 'Обнаружен активный трафик'],
'failover.switched': ['Новые соединения переключены', `${value.fromRole || ''}${value.toRole || ''} · ${value.reason || ''}`],
'failover.switch_failed': ['Переключение не выполнено', String(value.errorCode || '')],
'failover.both_unhealthy': ['Оба канала недоступны', 'Текущий маршрут сохранён'],
'failover.recovered': ['Основной канал восстановлен', String(value.reason || '')],
'journal.recovered': ['Журнал восстановлен', 'Повреждённый файл сохранён отдельно'],
};
return copies[event.type] || ['Системное событие', ''];
function eventTargetRole(event: ActivityJournalEvent) {
if (event.type === 'failover.switched') return event.data.toRole;
if (
event.type === 'failover.reserve_unavailable'
|| event.type === 'failover.reserve_recovered'
|| (event.type === 'failover.recovered' && event.data.role === 'reserve')
) return 'reserve';
if (
event.type === 'failover.primary_unavailable'
|| event.type === 'failover.primary_recovered'
|| (event.type === 'failover.recovered' && event.data.role === 'primary')
) return 'primary';
return undefined;
}
function dayLabel(value: string) {
@@ -155,13 +146,13 @@ export function ActivityJournalPanel({ feature, loadPage }: {
<h3>{group.label}</h3>
<ol>{group.items.map((item) => {
const { event } = item;
const [title, details] = refreshStreakCopy(item) || eventCopy(event);
const [title, details] = refreshStreakCopy(item) || activityJournalEventCopy(event);
return <li
key={event.id}
className="client-journal-event"
data-event-type={event.type}
data-severity={event.severity}
data-target-role={event.type === 'failover.switched' ? event.data.toRole : undefined}
data-target-role={eventTargetRole(event)}
>
<div className="client-journal-time"><time dateTime={event.occurredAt}>{new Intl.DateTimeFormat('ru-RU', { hour: '2-digit', minute: '2-digit' }).format(new Date(event.occurredAt))}</time><span>{event.source}</span></div>
<div><strong>{title}</strong>{details && <span>{details}</span>}{event.severity !== 'info' && <em>{event.severity === 'error' ? 'Ошибка' : 'Внимание'}</em>}</div>
@@ -87,3 +87,35 @@ export function refreshStreakCopy(item: ActivityJournalDisplayItem): [string, st
`${formatObservedDuration(streak.firstOccurredAt, item.event.occurredAt)} всё хорошо · ${updates} · ${profile}`,
];
}
export function activityJournalEventCopy(event: ActivityJournalEvent): [string, string] {
const value = event.data;
const copies: Record<string, [string, string]> = {
'connection.started': ['VPN включён', [value.profileLabel, value.serverLabel].filter(Boolean).join(' · ')],
'connection.stopped': ['VPN выключен', 'Остановлен пользователем'],
'connection.failed': ['VPN не запущен', String(value.errorCode || '')],
'subscription.added': ['Подписка добавлена', `${value.profileLabel || ''} · серверов: ${value.serverCount || 0}`],
'subscription.refreshed': ['Подписка обновлена', `${value.profileLabel || ''} · серверов: ${value.serverCount || 0} · +${value.added || 0} / ${value.removed || 0}`],
'subscription.refresh_failed': ['Подписка не обновлена', `${value.profileLabel || ''} · ${value.errorCode || ''}`],
'subscription.deleted': ['Подписка удалена', String(value.profileLabel || '')],
'failover.enabled': ['Резервный канал включён', 'Мониторинг начнётся после активации dual-config'],
'failover.disabled': ['Резервный канал выключен', 'Автоматика полностью остановлена'],
'failover.paused': ['Автопереключение на паузе', 'Проверки продолжаются'],
'failover.resumed': ['Автопереключение возобновлено', ''],
'failover.waiting_for_idle': ['Переключение отложено', 'Обнаружен активный трафик'],
'failover.switched': ['Новые соединения переключены', `${value.fromRole || ''}${value.toRole || ''}`],
'failover.switch_failed': ['Переключение не выполнено', String(value.errorCode || '')],
'failover.both_unhealthy': ['Оба канала недоступны', 'Текущий маршрут сохранён'],
'failover.primary_unavailable': ['Основной канал недоступен', 'Проверки канала не пройдены'],
'failover.reserve_unavailable': ['Резервный канал недоступен', 'Проверки канала не пройдены'],
'failover.primary_recovered': ['Основной канал восстановлен', 'Проверки канала снова проходят успешно'],
'failover.reserve_recovered': ['Резервный канал восстановлен', 'Проверки канала снова проходят успешно'],
'journal.recovered': ['Журнал восстановлен', 'Повреждённый файл сохранён отдельно'],
};
if (event.type === 'failover.recovered') {
return value.role === 'reserve'
? ['Резервный канал восстановлен', 'Проверки канала снова проходят успешно']
: ['Основной канал восстановлен', 'Проверки канала снова проходят успешно'];
}
return copies[event.type] || ['Системное событие', ''];
}
+7 -3
View File
@@ -9,8 +9,9 @@
.client-journal-groups { display: grid; gap: 26px; margin: 0 8px; }
.client-journal-groups section { display: grid; gap: 7px; }
.client-journal-groups h3 { margin: 0; }
.client-journal-groups ol { display: grid; margin: 0; padding: 0; }
.client-journal-groups li { position: relative; min-height: 58px; display: grid; grid-template-columns: 52px minmax(0, 1fr); gap: 14px; align-items: start; padding: 10px 0; border-top: 1px solid color-mix(in oklch, var(--client-border) 48%, transparent); }
.client-journal-groups ol { position: relative; display: grid; margin: 0; padding: 0; }
.client-journal-groups ol::before { content: ''; position: absolute; inset: 0 auto 0 -8px; z-index: 0; width: 1px; background: color-mix(in oklch, var(--client-border) 48%, transparent); }
.client-journal-groups li { position: relative; min-height: 58px; display: grid; grid-template-columns: 52px minmax(0, 1fr); gap: 14px; align-items: start; padding: 10px 0; }
.client-journal-time, .client-journal-groups li > div:last-child { min-width: 0; display: grid; gap: 3px; }
.client-journal-time time { color: var(--client-text); font: var(--type-data); font-variant-numeric: var(--numeric-tabular); letter-spacing: var(--type-data-tracking); text-transform: var(--type-data-transform); }
.client-journal-time span, .client-journal-groups li span { color: var(--client-muted); font: var(--type-label); letter-spacing: var(--type-label-tracking); text-transform: var(--type-label-transform); overflow-wrap: anywhere; }
@@ -18,15 +19,18 @@
.client-journal-groups li strong { font: var(--type-body); letter-spacing: var(--type-body-tracking); text-transform: var(--type-body-transform); }
.client-journal-groups li em { width: fit-content; color: var(--client-journal-tone); font: var(--type-label); font-style: normal; letter-spacing: var(--type-label-tracking); text-transform: var(--type-label-transform); }
.client-journal-event { --client-journal-tone: var(--client-muted); }
.client-journal-event::before { content: ''; position: absolute; inset: 9px auto 9px -8px; width: 3px; border-radius: 999px; background: var(--client-journal-tone); }
.client-journal-event::before { content: ''; position: absolute; inset: 15px auto auto -11.5px; z-index: 1; width: 8px; height: 8px; border-radius: 50%; background: var(--client-journal-tone); }
.client-journal-event .client-journal-time span { color: var(--client-journal-tone); }
.client-journal-event[data-event-type='connection.started'],
.client-journal-event[data-event-type='subscription.added'],
.client-journal-event[data-event-type='subscription.refreshed'],
.client-journal-event[data-event-type='failover.enabled'],
.client-journal-event[data-event-type='failover.resumed'],
.client-journal-event[data-event-type='failover.primary_recovered'],
.client-journal-event[data-event-type='failover.recovered'],
.client-journal-event[data-event-type='failover.switched'][data-target-role='primary'] { --client-journal-tone: var(--harbor-connect); }
.client-journal-event[data-event-type='failover.reserve_recovered'],
.client-journal-event[data-event-type='failover.recovered'][data-target-role='reserve'],
.client-journal-event[data-event-type='failover.switched'][data-target-role='reserve'] { --client-journal-tone: var(--harbor-gateway); }
.client-journal-event[data-event-type='failover.waiting_for_idle'],
.client-journal-event[data-event-type='failover.paused'],