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
@@ -83,6 +83,65 @@ describe('outbound deletion', () => {
expect(tt.routing!.rules![0].balancerTag).toBeUndefined();
});
it('cascade-removes the burst observer when deleting an outbound removes the last leastLoad balancer', () => {
const tt = tpl({
outbounds: [{ tag: 'll-out' }],
routing: {
rules: [],
balancers: [{ tag: 'll', selector: ['ll-out'], strategy: { type: 'leastLoad' } }],
},
burstObservatory: { subjectSelector: ['ll-out'] },
});
const impact = planOutboundDeletion(tt, 0);
expect(impact.balancers).toEqual([{ tag: 'll', reason: 'selectorEmptied' }]);
expect(impact.burst).toBe(true);
applyOutboundDeletion(tt, 0);
expect(tt.burstObservatory).toBeUndefined();
expect(tt.routing!.balancers).toEqual([]);
});
it('cascade-switches from burst to regular observer when only leastPing remains', () => {
const tt = tpl({
outbounds: [{ tag: 'lp-out' }, { tag: 'll-out' }],
routing: {
rules: [],
balancers: [
{ tag: 'lp', selector: ['lp-out'], strategy: { type: 'leastPing' } },
{ tag: 'll', selector: ['ll-out'], strategy: { type: 'leastLoad' } },
],
},
burstObservatory: { subjectSelector: ['lp-out', 'll-out'] },
});
const impact = planOutboundDeletion(tt, 1);
expect(impact.balancers).toEqual([{ tag: 'll', reason: 'selectorEmptied' }]);
expect(impact.burst).toBe(true);
applyOutboundDeletion(tt, 1);
expect(tt.burstObservatory).toBeUndefined();
expect((tt.observatory as { subjectSelector: string[] }).subjectSelector).toEqual(['lp-out']);
expect(tt.routing!.balancers).toEqual([{ tag: 'lp', selector: ['lp-out'], strategy: { type: 'leastPing' } }]);
});
it('cascade-keeps burst observer when leastPing is removed but leastLoad remains', () => {
const tt = tpl({
outbounds: [{ tag: 'lp-out' }, { tag: 'll-out' }],
routing: {
rules: [],
balancers: [
{ tag: 'lp', selector: ['lp-out'], strategy: { type: 'leastPing' } },
{ tag: 'll', selector: ['ll-out'], strategy: { type: 'leastLoad' } },
],
},
burstObservatory: { subjectSelector: ['lp-out', 'll-out'] },
});
const impact = planOutboundDeletion(tt, 0);
expect(impact.balancers).toEqual([{ tag: 'lp', reason: 'selectorEmptied' }]);
expect(impact.burst).toBe(false);
applyOutboundDeletion(tt, 0);
expect(tt.observatory).toBeUndefined();
expect((tt.burstObservatory as { subjectSelector: string[] }).subjectSelector).toEqual(['ll-out']);
expect(tt.routing!.balancers).toEqual([{ tag: 'll', selector: ['ll-out'], strategy: { type: 'leastLoad' } }]);
});
it('clears a fallbackTag and a dialerProxy pointing at the deleted outbound', () => {
const tt = tpl({
outbounds: [
@@ -253,6 +312,68 @@ describe('balancer deletion', () => {
expect(tt.routing!.balancers).toEqual([]);
});
it('reports and removes the burst observer when deleting the last leastLoad balancer', () => {
const tt = tpl({
routing: { rules: [], balancers: [{ tag: 'll', selector: ['a'], strategy: { type: 'leastLoad' } }] },
burstObservatory: { subjectSelector: ['a'] },
});
expect(planBalancerDeletion(tt, 0).burst).toBe(true);
applyBalancerDeletion(tt, 0);
expect(tt.burstObservatory).toBeUndefined();
expect(tt.routing!.balancers).toEqual([]);
});
it('reports and removes the burst observer when deleting the last fallback balancer', () => {
const tt = tpl({
routing: { rules: [], balancers: [{ tag: 'rf', selector: ['a'], fallbackTag: 'direct' }] },
burstObservatory: { subjectSelector: ['a'] },
});
expect(planBalancerDeletion(tt, 0).burst).toBe(true);
applyBalancerDeletion(tt, 0);
expect(tt.burstObservatory).toBeUndefined();
expect(tt.routing!.balancers).toEqual([]);
});
it('switches from burst to regular observer when the deleted balancer was the last burst-required one', () => {
const tt = tpl({
routing: {
rules: [],
balancers: [
{ tag: 'lp', selector: ['lp-out'], strategy: { type: 'leastPing' } },
{ tag: 'll', selector: ['ll-out'], strategy: { type: 'leastLoad' } },
],
},
burstObservatory: { subjectSelector: ['lp-out', 'll-out'] },
});
const impact = planBalancerDeletion(tt, 1);
expect(impact.burst).toBe(true);
expect(impact.observatory).toBe(false);
applyBalancerDeletion(tt, 1);
expect(tt.burstObservatory).toBeUndefined();
expect((tt.observatory as { subjectSelector: string[] }).subjectSelector).toEqual(['lp-out']);
expect(tt.routing!.balancers).toEqual([{ tag: 'lp', selector: ['lp-out'], strategy: { type: 'leastPing' } }]);
});
it('keeps burst observer when deleting leastPing but a burst-required balancer remains', () => {
const tt = tpl({
routing: {
rules: [],
balancers: [
{ tag: 'lp', selector: ['lp-out'], strategy: { type: 'leastPing' } },
{ tag: 'll', selector: ['ll-out'], strategy: { type: 'leastLoad' } },
],
},
burstObservatory: { subjectSelector: ['lp-out', 'll-out'] },
});
const impact = planBalancerDeletion(tt, 0);
expect(impact.burst).toBe(false);
expect(impact.observatory).toBe(false);
applyBalancerDeletion(tt, 0);
expect(tt.observatory).toBeUndefined();
expect((tt.burstObservatory as { subjectSelector: string[] }).subjectSelector).toEqual(['ll-out']);
expect(tt.routing!.balancers).toEqual([{ tag: 'll', selector: ['ll-out'], strategy: { type: 'leastLoad' } }]);
});
it('does not report rules when the deleted balancer is unreferenced', () => {
const tt = tpl({
routing: { rules: [{ type: 'field', inboundTag: ['in'], outboundTag: 'direct' }], balancers: [{ tag: 'pool', selector: ['a'] }] },