MCPcopy Create free account
hub / github.com/code-pushup/cli / ShardedWal

Class ShardedWal

packages/utils/src/lib/wal-sharded.ts:115–453  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

113 */
114
115export class ShardedWal<T extends WalRecord = WalRecord> {
116 static instanceCount = 0;
117
118 readonly #id: string = getUniqueInstanceId({
119 next() {
120 return ++ShardedWal.instanceCount;
121 },
122 });
123 readonly groupId: string;
124 readonly #debug: boolean = false;
125 readonly #format: WalFormat<T>;
126 readonly #dir: string = process.cwd();
127 readonly #coordinatorIdEnvVar: string;
128 #state: 'active' | 'finalized' | 'cleaned' = 'active';
129 #lastRecovery: {
130 file: string;
131 result: RecoverResult<T | InvalidEntry<string>>;
132 }[] = [];
133 #createdShardFiles: string[] = [];
134
135 /**
136 * Initialize the given environment variable if not already set.
137 * This must be done as early as possible before any user code runs.
138 * Sets envVarName to the current instance ID if not already defined.
139 *
140 * @param envVarName - Environment variable name for storing coordinator ID
141 * @param instanceID - The instance ID to set as coordinator
142 */
143 static setCoordinatorProcess(envVarName: string, instanceID: string): void {
144 if (!process.env[envVarName]) {
145 process.env[envVarName] = instanceID;
146 }
147 }
148
149 /**
150 * Determines if this process is the leader WAL process.
151 *
152 * The leader is the process that first enabled profiling over the given env var.
153 * All descendant processes inherit the environment.
154 *
155 * @param envVarName - Environment variable name for storing coordinator ID
156 * @param instanceID - The instance ID to check
157 * @returns true if this is the leader WAL process, false otherwise
158 */
159 static isCoordinatorProcess(envVarName: string, instanceID: string): boolean {
160 return process.env[envVarName] === instanceID;
161 }
162
163 /**
164 * Create a sharded WAL manager.
165 *
166 * @param opt.dir - Base directory to store shard files (defaults to process.cwd())
167 * @param opt.format - WAL format configuration
168 * @param opt.groupId - Group ID for sharding (defaults to generated group ID)
169 * @param opt.coordinatorIdEnvVar - Environment variable name for storing coordinator ID (defaults to CP_SHARDED_WAL_COORDINATOR_ID)
170 * @param opt.autoCoordinator - Whether to auto-set the coordinator ID on construction (defaults to true)
171 */
172 constructor(opt: {

Callers

nothing calls this directly

Calls 1

getUniqueInstanceIdFunction · 0.85

Tested by

no test coverage detected