-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsplit-brain-guard.ts
More file actions
78 lines (71 loc) · 3.29 KB
/
Copy pathsplit-brain-guard.ts
File metadata and controls
78 lines (71 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
/**
* Split-brain guard (ADR-0010, path A).
*
* The `memory` cluster driver keeps PubSub/Lock/KV/Counter state **per
* process**. On a single node that is correct; across multiple replicas
* each process holds its own state, so:
* - `ILock` is acquired locally by every replica -> no mutual exclusion;
* - `ICounter`/`IKV` versions diverge per replica;
* - `IPubSub` does not fan out across processes.
*
* This fails silently -- single-node tests and dev pass, only production
* multi-replica corrupts. The guard turns that silent corruption into a
* loud startup error when the operator has *declared* a multi-node
* topology yet wired an in-process driver.
*
* Detection is deliberately conservative (path A): it keys off an
* explicit operator declaration, not active peer discovery (that is the
* optional path B in ADR-0010). It cannot know at boot whether the
* cluster primitives are actually used cross-node, so it offers an
* escape hatch for the rare "replicas declared but primitives unused"
* case.
*/
/** In-process drivers whose state does not coordinate across replicas. */
const IN_PROCESS_DRIVERS = new Set(['memory']);
/** Environment inputs the guard reads. */
export interface SplitBrainGuardEnv {
/** `'true'` -> operator declares a multi-node deployment. */
OS_EXPECT_MULTI_NODE?: string;
/** Replica count; `> 1` -> multi-node. */
OS_CLUSTER_REPLICAS?: string;
/** `'true'` -> bypass the guard (replicas declared but primitives unused cross-node). */
OS_ALLOW_MEMORY_CLUSTER_MULTINODE?: string;
}
function isTrue(v: string | undefined): boolean {
return String(v).trim().toLowerCase() === 'true';
}
/**
* True when the operator has declared a multi-node topology via
* `OS_EXPECT_MULTI_NODE=true` or `OS_CLUSTER_REPLICAS` greater than 1.
*/
export function declaresMultiNode(env: SplitBrainGuardEnv = process.env): boolean {
if (isTrue(env.OS_EXPECT_MULTI_NODE)) return true;
const replicas = Number(env.OS_CLUSTER_REPLICAS);
return Number.isFinite(replicas) && replicas > 1;
}
/**
* Throw if a multi-node topology is declared while the resolved cluster
* `driver` is in-process (`memory`), unless explicitly allowed via
* `OS_ALLOW_MEMORY_CLUSTER_MULTINODE=true`.
*
* Call this at startup *before* registering the cluster service so a
* misconfiguration fails fast.
*/
export function assertClusterDriverSafeForTopology(
driver: string,
env: SplitBrainGuardEnv = process.env,
): void {
if (!declaresMultiNode(env)) return;
if (!IN_PROCESS_DRIVERS.has(driver)) return;
if (isTrue(env.OS_ALLOW_MEMORY_CLUSTER_MULTINODE)) return;
throw new Error(
`ClusterServicePlugin: multi-node deployment declared ` +
`(OS_EXPECT_MULTI_NODE / OS_CLUSTER_REPLICAS>1) but the cluster driver is ` +
`in-process "${driver}" -- its locks/counters/pub-sub do not coordinate across ` +
`processes, so multiple replicas silently split-brain. Configure a remote cluster ` +
`driver (e.g. @objectstack/service-cluster-redis) or a DB-backed driver. To override ` +
`(replicas declared but cluster primitives unused cross-node), set ` +
`OS_ALLOW_MEMORY_CLUSTER_MULTINODE=true. See cloud ADR-0010.`,
);
}