import {consoleLogSink, LogContext} from '@rocicorp/logger'; import {assert} from '../../../shared/src/asserts.ts'; import {must} from '../../../shared/src/must.ts'; import {promiseVoid} from '../../../shared/src/resolved-promises.ts'; import {resolver} from '../../../shared/src/resolver.ts'; import {DatabaseInitError} from '../../../zqlite/src/db.ts'; import {getServerContext} from '../config/server-context.ts'; import {getNormalizedZeroConfig} from '../config/zero-config.ts'; import {deleteLiteDB} from '../db/delete-lite-db.ts'; import {registerSQLiteCorruptionDiagnosticTarget} from '../db/sqlite-corruption.ts'; import {warmupConnections} from '../db/warmup.ts'; import {initEventSink, publishCriticalEvent} from '../observability/events.ts'; import {getOrCreateGauge} from '../observability/metrics.ts'; import {InitCleanup} from '../services/change-source/common/init-cleanup.ts'; import type {PgChangeLogPurgeLocker} from '../services/change-source/common/replica-restore.ts'; import {initializeCustomChangeSource} from '../services/change-source/custom/change-source.ts'; import {initializePostgresChangeSource} from '../services/change-source/pg/change-source-init.ts'; import {createBackupCleanupMonitor} from '../services/change-streamer/backup-cleanup-monitor-factory.ts'; import type {BackupMonitor} from '../services/change-streamer/backup-monitor.ts'; import {ChangeStreamerHttpServer} from '../services/change-streamer/change-streamer-http.ts'; import {initializeStreamer} from '../services/change-streamer/change-streamer-service.ts'; import type {ChangeStreamerService} from '../services/change-streamer/change-streamer.ts'; import {DEFAULT_READINESS_GRACE_PERIOD_MS} from '../services/change-streamer/restore-admission.ts'; import {initChangeStreamerSchema} from '../services/change-streamer/schema/init.ts'; import {AutoResetSignal} from '../services/change-streamer/schema/tables.ts'; import {ServerStartMetrics} from '../services/change-streamer/server-start-metrics.ts'; import { PurgeLocker, type PurgeLock, } from '../services/change-streamer/storer.ts'; import { exitAfter, runUntilKilled, SupportingProcesses, } from '../services/life-cycle.ts'; import {deleteLitestreamMetaDir} from '../services/litestream/commands.ts'; import { changeLogFileName, deleteChangeLogDB, } from '../services/replicator/change-log-db.ts'; import { replicationStatusError, ReplicationStatusPublisher, } from '../services/replicator/replication-status.ts'; import {sqliteFileBytes} from '../services/replicator/sqlite-change-log-observability.ts'; import {warnIfPostgresUnpatched} from '../types/pg-patch.ts'; import {connectPgClient, type PostgresDB} from '../types/pg.ts'; import { broadcastWorker, childWorker, parentWorker, singleProcessMode, type ProfileResponseMessage, type Worker, } from '../types/processes.ts'; import {installProfileHandler} from '../types/profiler.ts'; import {getShardConfig, type ShardConfig} from '../types/shards.ts'; import type {ReplicaFileMode} from '../workers/replicator.ts'; import {createLogContext} from './logging.ts'; import {startOtelAuto} from './otel-start.ts'; import {REPLICATOR_URL} from './worker-urls.ts'; // Default LogContext, overridden in runWorker let lc = new LogContext('info', {}, consoleLogSink); // Errors thrown once the services are running are reported by the services // themselves (e.g. as 'Replicating' status events), and are thus not // published as 'Initializing' errors. let running = false; export default async function runWorker( parent: Worker, env: NodeJS.ProcessEnv, ...argv: string[] ): Promise { installProfileHandler(parent, 'change-streamer'); const config = getNormalizedZeroConfig({env, argv}); const { taskID, changeStreamer: { port, address, protocol, secret, startupDelayMs, backPressureLimitHeapProportion, flowControlConsensusTimeoutProportion, flowControlSlowSubscriberGracePeriodSeconds, maxConcurrentSwitchoverRestores, sqliteChangeLogRetentionMs, sqliteChangeLogReadBatchRows, sqliteChangeLogPurgeBatchRows, sqliteChangeLogBarrierTimeoutMs, }, autoReset, replicationLag, litestream, upstream, change, replica, initialSync, keepaliveTimeoutMs, sqliteCorruptionChecks, } = config; startOtelAuto( createLogContext(config, 'change-streamer', 0, false), 'change-streamer', 0, ); lc = createLogContext(config, 'change-streamer'); registerSQLiteCorruptionDiagnosticTarget( { debugName: 'change-streamer replica', dbPath: replica.file, }, sqliteCorruptionChecks, ); initEventSink(lc, config); if (upstream.type === 'pg') { warnIfPostgresUnpatched(lc); } const shard = getShardConfig(config); // In high-availability mode (RMv2), the change log is kept in SQLite rather // than in Postgres (i.e. the change DB), to which no connection is made. const sqliteChangeLogEnabled = upstream.pgHighAvailabilityReplication; const pgChangeLogEnabled = !sqliteChangeLogEnabled; const changeDB = pgChangeLogEnabled ? await connectChangeDB(lc, change.db, shard) : null; let purgeLock = null as PurgeLock | null; let changeStreamer: ChangeStreamerService | undefined; let backupMonitor: BackupMonitor | undefined; let backupURL: string | undefined; const context = getServerContext(config); if (sqliteChangeLogEnabled) { const changeLogFile = changeLogFileName(replica.file); registerSQLiteCorruptionDiagnosticTarget( { debugName: 'change-streamer change-log', dbPath: changeLogFile, }, sqliteCorruptionChecks, ); // The log's bytes are accounted for in the process that writes them. This // is local disk only — the log is excluded from the litestream backup — and // it is a whole-database total, because a table that left the replica left // through its wal: the replica's footprint is what shrank and this is where // it went. getOrCreateGauge('replica', 'sqlite_change_log.file_bytes', { description: `The SQLite change log's total on-disk footprint: its main db file ` + `plus its wal sidecars.`, unit: 'By', }).addCallback(async o => o.observe(await sqliteFileBytes(lc, changeLogFile)), ); } const startMetrics = new ServerStartMetrics(); let waitForFirstBackupBeforeServing = false; let newBackupLineage = false; for (const first of [true, false]) { // Resources acquired by an initialization attempt (e.g. a claimed // replication slot, the purge lock) to be released if the attempt fails. const cleanup = new InitCleanup(lc); // When restoring from litestream, a lock is acquired to prevent change-log // purges. This ensures that (this) change-streamer will be able to resume // from the backup. The lock is acquired by the change source initialization // just before restoring (i.e. after a replication slot is created, if // applicable), and released once this change-streamer takes over the // change-log. const acquirePurgeLock: PgChangeLogPurgeLocker | undefined = changeDB && litestream.backupURL && litestream.executable ? async (slotWatermark?: string) => { const lock = await new PurgeLocker(lc, shard, changeDB).acquire( slotWatermark, ); purgeLock = lock === 'behind-slot' ? null : lock; if (purgeLock) { const acquired = purgeLock; cleanup.onFailure('purge lock', () => { purgeLock = null; return acquired.release(); }); } return lock; } : undefined; const restoreOptions = {litestream, acquirePurgeLock, cleanup}; try { // Note: This performs initial sync of the replica if necessary. const { pgReplicationEpoch: epoch, pgReplicationSlotPerReplica: slotPerReplica, pgResumeOrphanedSlotGracePeriodMs: inactiveReplicaGracePeriodMs, } = upstream; const { changeSource, subscriptionState, destinationBackupURL, replicaID, waitForBackupBeforeServing, newBackupLineage: initNewBackupLineage, pgChangeLogBehindSlot, startTimings, } = upstream.type === 'pg' ? await initializePostgresChangeSource( lc, upstream.db, shard, replica.file, { ...initialSync, replicationSlotFailover: upstream.pgReplicationSlotFailover, installPartialIndexTriggers: upstream.pgPartialIndexTriggers, }, context, replicationLag.reportIntervalMs, restoreOptions, { epoch, slotPerReplica, inactiveReplicaGracePeriodMs, backupV5: litestream.backupUsingV5, pgChangeLog: pgChangeLogEnabled, }, upstream.pgStreamInboundTimeoutMs, ) : await initializeCustomChangeSource( lc, upstream.db, shard, replica.file, context, restoreOptions, ); const replicationStatusPublisher = ReplicationStatusPublisher.forReplicaFile(replica.file); // Created before the change-streamer, which tracks its watermarks (and // requests prompt backups from it) from construction. It is run (and // stopped) as a service below. It monitors the backup-replicator's // destination (see `backupURLOverride` below). backupMonitor = createBackupCleanupMonitor({ lc, config: destinationBackupURL ? { ...config, litestream: { ...config.litestream, backupURLOverride: destinationBackupURL, }, } : config, replicaFile: replica.file, }); changeStreamer = await initializeStreamer( lc, shard, taskID, address, protocol, changeDB, changeSource, replicationStatusPublisher, subscriptionState, destinationBackupURL ? { backupURL: destinationBackupURL, litestreamVersion: litestream.backupUsingV5 ? 'v5' : 'legacy', replicaFile: replica.file, } : null, purgeLock, autoReset ?? false, { pgChangeLogBehindSlot: pgChangeLogBehindSlot ?? false, backPressureLimitHeapProportion, flowControlConsensusTimeoutProportion, flowControlSlowSubscriberGracePeriodMs: flowControlSlowSubscriberGracePeriodSeconds > 0 ? flowControlSlowSubscriberGracePeriodSeconds * 1000 : undefined, maxConcurrentSwitchoverRestores, // When lag reporting is enabled, a restored view-syncer waits for // the next lag report (sent at an interval with ~1 second of // jitter) before becoming ready. switchoverRestoreReadinessGracePeriodMs: (replicationLag.reportIntervalMs > 0 ? replicationLag.reportIntervalMs + 1000 : 0) + DEFAULT_READINESS_GRACE_PERIOD_MS, statementTimeoutMs: change.statementTimeoutMs, changeLogBatchSize: change.logBatchSize, // The SQLite change log, which this process writes from its own // stream loop. It performs the restore and the initial sync, so the // replica's identity is in hand at the moment the log is opened. ...(sqliteChangeLogEnabled ? { sqliteChangeLogWriter: { replicaFile: replica.file, identity: { // RMv2 supplies the epoch; until then the generation and // the replica ID are the whole of the identity. epoch: null, generation: subscriptionState.replicaVersion, replicaID, }, }, sqliteChangeLogPurge: { retentionMs: sqliteChangeLogRetentionMs, batchRows: sqliteChangeLogPurgeBatchRows, }, sqliteCatchup: { changeLogFile: changeLogFileName(replica.file), readBatchRows: sqliteChangeLogReadBatchRows, barrierTimeoutMs: sqliteChangeLogBarrierTimeoutMs, }, } : {}), backupMonitor, }, setTimeout, ); backupURL = destinationBackupURL; waitForFirstBackupBeforeServing = waitForBackupBeforeServing; startMetrics.setKind(startTimings?.kind); startMetrics.record('slot_create', startTimings?.slotCreateMs); startMetrics.record('fork_wait', startTimings?.forkWaitMs); startMetrics.record('restore', startTimings?.restoreMs); newBackupLineage = initNewBackupLineage; break; } catch (e) { // Release the resources acquired by the failed attempt, e.g.: // * The claimed replication slot, which would otherwise remain active // indefinitely, preventing a retry from resuming (or forking) it. // * The purge lock. This is safe because the purge lock exists to // preserve change-log entries so the new change-streamer can resume // from the backup replica's watermark. An AutoResetSignal means we // can't resume from the backup replica (e.g. its replication slot is // gone), so the change-log entries the lock was protecting are no // longer needed. The retry performs a fresh initial sync with a new // replication slot, independent of the old change-log. Releasing is // also necessary to avoid a self-deadlock when CHANGE_DB == // UPSTREAM_DB: CREATE_REPLICATION_SLOT waits for all older // transactions to finish, including this lock's open transaction. await cleanup.release(); // Not yet started (the change-streamer starts it), so nothing to wait on. void backupMonitor?.stop(); backupMonitor = undefined; if (first && e instanceof AutoResetSignal) { lc.warn?.(`resetting replica ${replica.file}`, e); // TODO: Make deleteLiteDB work with litestream. It will probably have to be // a semantic wipe instead of a file delete. deleteLiteDB(replica.file); // The change log carries the identity of the replica it was written // beside, and the retry performs a fresh initial sync with a new // replicaVersion. Reconciliation would catch that on its own (as // 'identity-mismatch') but only after the writer opened a file that is // known here to be garbage. deleteChangeLogDB(replica.file); continue; // execute again with a fresh initial-sync } if (e instanceof DatabaseInitError) { throw new Error( `Cannot open ZERO_REPLICA_FILE at "${replica.file}". Please check that the path is valid.`, {cause: e}, ); } throw e; } } // impossible: upstream must have advanced in order for replication to be stuck. assert(changeStreamer, `resetting replica did not advance replicaVersion`); assert(backupMonitor, `backup monitor is created with the change-streamer`); // The backup-replicator is stopped with the other services, i.e. after the // ChangeStreamerHttpServer has drained its subscribers. const processes = new SupportingProcesses(lc, parent); const profileSubWorkers: Worker[] = []; const {promise: replicatorReady, resolve} = resolver(); if (backupURL) { const catchUpStart = performance.now(); void replicatorReady.then(() => startMetrics.record('catch_up', performance.now() - catchUpStart), ); } if (!backupURL) { resolve(); // No backup replicator to wait for, so resolve immediately. } else { lc.info?.('setting up backup to', backupURL); config.litestream.backupURLOverride = backupURL; if (newBackupLineage) { deleteLitestreamMetaDir(replica.file); } // Start a backup replicator, which starts up the corresponding // litestream backup process. const backupReplicator = processes .add( childWorker( REPLICATOR_URL, {...env, ZERO_LITESTREAM_BACKUP_URL_OVERRIDE: backupURL}, 'backup' satisfies ReplicaFileMode, ), 'backup-replicator', ) .onceMessageType('ready', () => resolve()); profileSubWorkers.push(backupReplicator); // Relay profileResponse messages from backup-replicator up to parent backupReplicator.onMessageType( 'profileResponse', res => parent.send(['profileResponse', res]), ); } let backupReady = promiseVoid; if (waitForFirstBackupBeforeServing) { const start = performance.now(); lc.info?.(`awaiting initial backup ...`); backupReady = backupMonitor.firstBackupReceived().then(() => { const elapsed = performance.now() - start; lc.info?.(`initial backup confirmed after ${elapsed.toFixed(2)}ms`); startMetrics.record('backup_confirm', elapsed); }); } // In RMv2, readiness additionally waits for the backup-replicator to catch // up to the replication stream. This is not done in RMv1 (i.e. when the // PG change-log is enabled), in which the change-streamer itself is only // started after the readinessGate (plus a startup delay), and thus the // backup-replicator cannot catch up before then. const readinessGate = pgChangeLogEnabled ? backupReady : Promise.all([backupReady, replicatorReady]); // Create the broadcast facade once: each broadcastWorker() adds permanent // 'message' forwarders to every sub worker, so creating one per /profz // request would leak a forwarder per request. const profileWorker = profileSubWorkers.length > 0 ? broadcastWorker(profileSubWorkers) : undefined; const getProfileWorker = profileWorker ? () => Promise.resolve(profileWorker) : undefined; if (secret === undefined) { lc.warn?.( `No --change-streamer-secret is configured: the change-streamer ` + `accepts replication connections from anything that can reach port ` + `${port}. Set ZERO_CHANGE_STREAMER_SECRET to the same value on the ` + `replication-manager and on every view-syncer.`, ); } const changeStreamerWebServer = new ChangeStreamerHttpServer( lc, { port, secret, keepaliveTimeoutMs, // The startup delay is only relevant when taking over the PG change-log // (RMv1 and RMv1.5), and is disabled for RMv2. startupDelayMs: pgChangeLogEnabled ? startupDelayMs : 0, readinessGate, config, getProfileWorker, }, parent, changeStreamer, ); void readinessGate.then(() => { startMetrics.ready(); parent.send(['ready', {ready: true}]); }); // Note: The changeStreamer itself is not started here; it is started by the // changeStreamerWebServer after a delay to ensure that routing // routing elements have registered the server before the // change-streamer takes over the replication slot. // TODO: Remove this delay and start the changeStreamer normally here once // transitioned to RMv2, since changeStreamer startup will no longer // disrupt an existing task. running = true; await runUntilKilled( lc, parent, changeStreamerWebServer, backupMonitor, processes, ); } /** * Connects the startup-time client used for change-streamer initialization * and handoff / takeover, and ensures that the change DB schema is * initialized / up-to-date. Steady-state clients are managed by the * change-streamer (Storer) implementation. */ async function connectChangeDB( lc: LogContext, connectionURI: string, shard: ShardConfig, ): Promise { const changeDB = await connectPgClient( lc, connectionURI, 'change-streamer-init', {max: 5}, {sendStringAsJson: true}, ); void warmupConnections(lc, changeDB, 'change').catch(() => {}); await initChangeStreamerSchema(lc, changeDB, shard); return changeDB; } // fork() if (!singleProcessMode()) { void exitAfter( () => lc, () => runWorker( must(parentWorker), process.env, ...process.argv.slice(2), ).catch(async e => { if (!running) { await publishCriticalEvent( lc, replicationStatusError(lc, 'Initializing', e), ); } throw e; }), ); }