fix(balancers): keep mixed strategies on one observer (#5674)

* fix(balancers): keep mixed strategies on one observer

Xray resolves Observatory and Burst Observatory through the same global observer feature. When any burst-required strategy is present, keep all observer-backed balancer selectors on burstObservatory and remove the regular observatory so mixed leastPing configs cannot generate two competing observer blocks.

* test(balancers): cover observer strategy combinations

Exercise the observer sync matrix for random, round-robin, leastPing, and leastLoad balancers. Include mixed and stale-observer cases so the panel keeps only the observer type that Xray should consume.

* fix(balancers): clarify observer empty state

Update the Observatory tab empty hint to describe the actual auto-managed cases. Least Ping, Least Load, and fallback Random or Round-robin balancers now explain why an observer is added before the balancer can choose a target.

* fix(balancers): remove mixed observer switch

Show only the observer settings panel that matches the current balancer requirements. Legacy configs that still contain both observatory blocks now display a warning instead of a tab switch, since saving balancers normalizes the config back to one global observer.

* test(balancers): cover observer cleanup on deletion

Add direct balancer deletion and outbound cascade cases for leastLoad, fallback, and mixed leastPing scenarios. These tests pin that the final unneeded observer is removed, burst switches back to regular observatory when only leastPing remains, and burst remains when a burst-required balancer survives.
This commit is contained in:
nima1024m
2026-07-02 18:18:30 +02:00
committed by GitHub
parent 97e2c9e7ba
commit ade74eb321
19 changed files with 496 additions and 63 deletions
@@ -367,7 +367,6 @@ export default function BalancersTab({
<ObservatorySettingsTab
templateSettings={templateSettings}
mutate={mutate}
isMobile={isMobile}
/>
),
},
@@ -1,6 +1,6 @@
import { useMemo, useState } from 'react';
import { useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { Alert, Empty, Input, InputNumber, Segmented, Select, Space, Switch, Tag } from 'antd';
import { Alert, Empty, Input, InputNumber, Select, Space, Switch, Tag } from 'antd';
import { SettingListItem } from '@/components/ui';
import {
@@ -13,11 +13,11 @@ import {
type PingConfigObject,
} from '@/schemas/observatory';
import type { XraySettingsValue } from '@/hooks/useXraySetting';
import { settingsRequireBurstObservatory } from './balancer-helpers';
interface ObservatorySettingsTabProps {
templateSettings: XraySettingsValue | null;
mutate: (mutator: (next: XraySettingsValue) => void) => void;
isMobile: boolean;
}
const OBSERVATORY_DEFAULTS = ObservatorySchema.parse({});
@@ -43,7 +43,6 @@ function SelectorTags({ tags }: { tags: string[] }) {
export default function ObservatorySettingsTab({
templateSettings,
mutate,
isMobile,
}: ObservatorySettingsTabProps) {
const { t } = useTranslation();
@@ -63,13 +62,10 @@ export default function ObservatorySettingsTab({
const hasObservatory = observatory != null;
const hasBurst = burst != null;
const [view, setView] = useState<'observatory' | 'burstObservatory'>('observatory');
const effectiveView = !hasObservatory && hasBurst
const hasMixedObservers = hasObservatory && hasBurst;
const activeView = hasBurst && (!hasObservatory || settingsRequireBurstObservatory(templateSettings))
? 'burstObservatory'
: !hasBurst && hasObservatory
? 'observatory'
: view;
: 'observatory';
function patchObservatory(patch: Partial<ObservatoryObject>) {
mutate((tt) => {
@@ -220,19 +216,15 @@ export default function ObservatorySettingsTab({
return (
<Space orientation="vertical" size="middle" style={{ width: '100%' }}>
<Alert type="info" showIcon message={t('pages.xray.observatory.autoManaged')} />
{hasObservatory && hasBurst && (
<Segmented
block={isMobile}
value={effectiveView}
onChange={(v) => setView(v as 'observatory' | 'burstObservatory')}
options={[
{ label: t('pages.xray.observatory.title'), value: 'observatory' },
{ label: t('pages.xray.observatory.burstTitle'), value: 'burstObservatory' },
]}
<Alert type="info" showIcon title={t('pages.xray.observatory.autoManaged')} />
{hasMixedObservers && (
<Alert
type="warning"
showIcon
title={t('pages.xray.observatory.mixedLegacy')}
/>
)}
<div>{effectiveView === 'observatory' ? observatorySection : burstSection}</div>
<div>{activeView === 'observatory' ? observatorySection : burstSection}</div>
</Space>
);
}
@@ -26,6 +26,16 @@ export function collectSelectors(list: BalancerObject[]): string[] {
return [...out];
}
export function balancerRequiresBurstObservatory(b: BalancerObject): boolean {
const type = b.strategy?.type || 'random';
return type === 'leastLoad' || ((type === 'random' || type === 'roundRobin') && (b.fallbackTag ?? '').length > 0);
}
export function settingsRequireBurstObservatory(t: XraySettingsValue | null): boolean {
const balancers = (t?.routing?.balancers || []) as BalancerObject[];
return balancers.some(balancerRequiresBurstObservatory);
}
// syncObservatories keeps the (burst)observatory sections aligned with the
// balancer strategies that actually require them. Observatories have no runtime
// reload API in xray-core, so creating OR removing one forces a full process
@@ -36,39 +46,35 @@ export function collectSelectors(list: BalancerObject[]): string[] {
// when its fallbackTag is set (issue #5605): with a fallbackTag the strategy
// calls RequireFeatures(Observatory) and the core aborts startup with "not all
// dependencies are resolved" if none exists; without a fallbackTag it never even
// consults an observatory. leastLoad always needs the burst observer, leastPing
// the regular one.
// consults an observatory. leastLoad needs the burst observer, while leastPing
// can use any extension.Observatory result with Alive/Delay. When a burst
// observer is required, keep all observer-backed balancers on burstObservatory
// to avoid xray-core resolving the earlier regular observatory feature instead.
//
// So each observer lives exactly as long as something requires it, and is
// dropped the moment nothing does — clearing the last fallbackTag (or deleting
// the last leastLoad) removes the burst observer again. A no-fallback balancer's
// selector is still probed while the observer exists for another reason, but
// never keeps it alive on its own.
// the last leastLoad) removes the burst observer again. A no-fallback
// Random/RoundRobin balancer never expands the observer either, because those
// strategies do not consume observer data.
export function syncObservatories(t: XraySettingsValue) {
const balancers = (t.routing?.balancers || []) as BalancerObject[];
const leastPings = balancers.filter((b) => b.strategy?.type === 'leastPing');
if (leastPings.length > 0) {
if (!t.observatory) t.observatory = JSON.parse(JSON.stringify(DEFAULT_OBSERVATORY));
(t.observatory as { subjectSelector: string[] }).subjectSelector = collectSelectors(leastPings);
} else {
delete t.observatory;
}
const hasFallback = (b: BalancerObject) => (b.fallbackTag ?? '').length > 0;
const required = balancers.filter((b) => {
const type = b.strategy?.type || 'random';
if (type === 'leastLoad') return true;
return (type === 'random' || type === 'roundRobin') && hasFallback(b);
});
const optional = balancers.filter((b) => {
const type = b.strategy?.type || 'random';
return (type === 'random' || type === 'roundRobin') && !hasFallback(b);
});
const required = balancers.filter(balancerRequiresBurstObservatory);
if (required.length > 0) {
delete t.observatory;
if (!t.burstObservatory) t.burstObservatory = JSON.parse(JSON.stringify(DEFAULT_BURST_OBSERVATORY));
(t.burstObservatory as { subjectSelector: string[] }).subjectSelector = collectSelectors([...required, ...optional]);
(t.burstObservatory as { subjectSelector: string[] }).subjectSelector = collectSelectors([
...required,
...leastPings,
]);
} else {
delete t.burstObservatory;
if (leastPings.length > 0) {
if (!t.observatory) t.observatory = JSON.parse(JSON.stringify(DEFAULT_OBSERVATORY));
(t.observatory as { subjectSelector: string[] }).subjectSelector = collectSelectors(leastPings);
} else {
delete t.observatory;
}
}
}