|
| 1 | +import type { ChaosResult } from './network-partition'; |
| 2 | + |
| 3 | +export interface GeoRegionState { |
| 4 | + region: string; |
| 5 | + available: boolean; |
| 6 | + latencyMs: number; |
| 7 | +} |
| 8 | + |
| 9 | +export interface GeoPartitionScenario { |
| 10 | + primary: GeoRegionState; |
| 11 | + replicas: GeoRegionState[]; |
| 12 | +} |
| 13 | + |
| 14 | +const DEFAULT_SCENARIO: GeoPartitionScenario = { |
| 15 | + primary: { region: 'us-east-1', available: true, latencyMs: 5 }, |
| 16 | + replicas: [ |
| 17 | + { region: 'eu-west-1', available: true, latencyMs: 80 }, |
| 18 | + { region: 'ap-southeast-1', available: true, latencyMs: 150 }, |
| 19 | + ], |
| 20 | +}; |
| 21 | + |
| 22 | +export async function simulateGeoRequest<T>( |
| 23 | + region: string, |
| 24 | + fn: () => Promise<T>, |
| 25 | + scenario: GeoPartitionScenario = DEFAULT_SCENARIO |
| 26 | +): Promise<T> { |
| 27 | + const allRegions = [scenario.primary, ...scenario.replicas]; |
| 28 | + const regionState = allRegions.find((r) => r.region === region); |
| 29 | + |
| 30 | + if (!regionState) throw new Error(`Unknown region: ${region}`); |
| 31 | + if (!regionState.available) throw new Error(`Region unavailable: ${region}`); |
| 32 | + |
| 33 | + if (regionState.latencyMs > 0) { |
| 34 | + await new Promise((r) => setTimeout(r, regionState.latencyMs)); |
| 35 | + } |
| 36 | + |
| 37 | + return fn(); |
| 38 | +} |
| 39 | + |
| 40 | +export async function simulateRegionFailover<T>( |
| 41 | + fn: () => Promise<T>, |
| 42 | + scenario: GeoPartitionScenario |
| 43 | +): Promise<{ result: T | null; failoverRegion: string; failoverDurationMs: number }> { |
| 44 | + const start = Date.now(); |
| 45 | + |
| 46 | + if (!scenario.primary.available) { |
| 47 | + for (const replica of scenario.replicas) { |
| 48 | + if (!replica.available) continue; |
| 49 | + const replicaStart = Date.now(); |
| 50 | + try { |
| 51 | + const result = await simulateGeoRequest(replica.region, fn, scenario); |
| 52 | + return { |
| 53 | + result, |
| 54 | + failoverRegion: replica.region, |
| 55 | + failoverDurationMs: Date.now() - replicaStart, |
| 56 | + }; |
| 57 | + } catch { |
| 58 | + continue; |
| 59 | + } |
| 60 | + } |
| 61 | + throw new Error('All regions unavailable'); |
| 62 | + } |
| 63 | + |
| 64 | + const result = await fn(); |
| 65 | + return { |
| 66 | + result, |
| 67 | + failoverRegion: scenario.primary.region, |
| 68 | + failoverDurationMs: Date.now() - start, |
| 69 | + }; |
| 70 | +} |
| 71 | + |
| 72 | +export async function runGeoPartitionExperiment(): Promise<ChaosResult> { |
| 73 | + const start = Date.now(); |
| 74 | + |
| 75 | + const scenario: GeoPartitionScenario = { |
| 76 | + primary: { region: 'us-east-1', available: false, latencyMs: 0 }, |
| 77 | + replicas: [ |
| 78 | + { region: 'eu-west-1', available: true, latencyMs: 80 }, |
| 79 | + { region: 'ap-southeast-1', available: false, latencyMs: 0 }, |
| 80 | + ], |
| 81 | + }; |
| 82 | + |
| 83 | + try { |
| 84 | + const { failoverRegion, failoverDurationMs } = await simulateRegionFailover( |
| 85 | + async () => ({ data: 'recovered' }), |
| 86 | + scenario |
| 87 | + ); |
| 88 | + |
| 89 | + const passed = failoverRegion === 'eu-west-1' && failoverDurationMs < 500; |
| 90 | + |
| 91 | + return { |
| 92 | + experiment: 'geo-partition', |
| 93 | + passed, |
| 94 | + duration: Date.now() - start, |
| 95 | + recovery: `failover-to-${failoverRegion}`, |
| 96 | + error: passed ? undefined : `Failover took ${failoverDurationMs}ms or went to wrong region`, |
| 97 | + }; |
| 98 | + } catch (err) { |
| 99 | + return { |
| 100 | + experiment: 'geo-partition', |
| 101 | + passed: false, |
| 102 | + duration: Date.now() - start, |
| 103 | + error: err instanceof Error ? err.message : String(err), |
| 104 | + }; |
| 105 | + } |
| 106 | +} |
0 commit comments