@rocicorp/zero 0.25.7 → 0.26.0-canary.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/out/shared/src/custom-key-map.d.ts +4 -4
- package/out/shared/src/custom-key-map.d.ts.map +1 -1
- package/out/shared/src/custom-key-map.js.map +1 -1
- package/out/shared/src/iterables.d.ts +6 -8
- package/out/shared/src/iterables.d.ts.map +1 -1
- package/out/shared/src/iterables.js +13 -7
- package/out/shared/src/iterables.js.map +1 -1
- package/out/zero/package.json.js +1 -1
- package/out/zero-cache/src/config/zero-config.d.ts +5 -0
- package/out/zero-cache/src/config/zero-config.d.ts.map +1 -1
- package/out/zero-cache/src/config/zero-config.js +12 -0
- package/out/zero-cache/src/config/zero-config.js.map +1 -1
- package/out/zero-cache/src/observability/events.d.ts.map +1 -1
- package/out/zero-cache/src/observability/events.js +15 -5
- package/out/zero-cache/src/observability/events.js.map +1 -1
- package/out/zero-cache/src/server/change-streamer.d.ts.map +1 -1
- package/out/zero-cache/src/server/change-streamer.js +10 -2
- package/out/zero-cache/src/server/change-streamer.js.map +1 -1
- package/out/zero-cache/src/server/syncer.d.ts +1 -0
- package/out/zero-cache/src/server/syncer.d.ts.map +1 -1
- package/out/zero-cache/src/server/syncer.js +22 -4
- package/out/zero-cache/src/server/syncer.js.map +1 -1
- package/out/zero-cache/src/services/change-source/custom/change-source.js +0 -4
- package/out/zero-cache/src/services/change-source/custom/change-source.js.map +1 -1
- package/out/zero-cache/src/services/change-source/pg/schema/ddl.d.ts.map +1 -1
- package/out/zero-cache/src/services/change-source/pg/schema/ddl.js +1 -10
- package/out/zero-cache/src/services/change-source/pg/schema/ddl.js.map +1 -1
- package/out/zero-cache/src/services/change-source/pg/schema/init.d.ts.map +1 -1
- package/out/zero-cache/src/services/change-source/pg/schema/init.js +8 -2
- package/out/zero-cache/src/services/change-source/pg/schema/init.js.map +1 -1
- package/out/zero-cache/src/services/change-source/pg/schema/shard.d.ts.map +1 -1
- package/out/zero-cache/src/services/change-source/pg/schema/shard.js +1 -14
- package/out/zero-cache/src/services/change-source/pg/schema/shard.js.map +1 -1
- package/out/zero-cache/src/services/change-source/replica-schema.d.ts.map +1 -1
- package/out/zero-cache/src/services/change-source/replica-schema.js +8 -1
- package/out/zero-cache/src/services/change-source/replica-schema.js.map +1 -1
- package/out/zero-cache/src/services/change-streamer/change-streamer-service.d.ts +1 -1
- package/out/zero-cache/src/services/change-streamer/change-streamer-service.d.ts.map +1 -1
- package/out/zero-cache/src/services/change-streamer/change-streamer-service.js +5 -3
- package/out/zero-cache/src/services/change-streamer/change-streamer-service.js.map +1 -1
- package/out/zero-cache/src/services/change-streamer/storer.d.ts +1 -1
- package/out/zero-cache/src/services/change-streamer/storer.d.ts.map +1 -1
- package/out/zero-cache/src/services/change-streamer/storer.js +16 -5
- package/out/zero-cache/src/services/change-streamer/storer.js.map +1 -1
- package/out/zero-cache/src/services/life-cycle.d.ts +1 -1
- package/out/zero-cache/src/services/life-cycle.d.ts.map +1 -1
- package/out/zero-cache/src/services/life-cycle.js.map +1 -1
- package/out/zero-cache/src/services/mutagen/mutagen.d.ts +4 -4
- package/out/zero-cache/src/services/mutagen/mutagen.d.ts.map +1 -1
- package/out/zero-cache/src/services/mutagen/mutagen.js +9 -24
- package/out/zero-cache/src/services/mutagen/mutagen.js.map +1 -1
- package/out/zero-cache/src/services/mutagen/pusher.d.ts +1 -2
- package/out/zero-cache/src/services/mutagen/pusher.d.ts.map +1 -1
- package/out/zero-cache/src/services/mutagen/pusher.js +51 -12
- package/out/zero-cache/src/services/mutagen/pusher.js.map +1 -1
- package/out/zero-cache/src/services/replicator/change-processor.js +4 -3
- package/out/zero-cache/src/services/replicator/change-processor.js.map +1 -1
- package/out/zero-cache/src/services/replicator/schema/change-log.d.ts +3 -2
- package/out/zero-cache/src/services/replicator/schema/change-log.d.ts.map +1 -1
- package/out/zero-cache/src/services/replicator/schema/change-log.js +36 -31
- package/out/zero-cache/src/services/replicator/schema/change-log.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/client-handler.d.ts +5 -6
- package/out/zero-cache/src/services/view-syncer/client-handler.d.ts.map +1 -1
- package/out/zero-cache/src/services/view-syncer/client-handler.js +5 -23
- package/out/zero-cache/src/services/view-syncer/client-handler.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/cvr-store.d.ts.map +1 -1
- package/out/zero-cache/src/services/view-syncer/cvr-store.js +6 -4
- package/out/zero-cache/src/services/view-syncer/cvr-store.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/pipeline-driver.d.ts +1 -8
- package/out/zero-cache/src/services/view-syncer/pipeline-driver.d.ts.map +1 -1
- package/out/zero-cache/src/services/view-syncer/pipeline-driver.js +2 -11
- package/out/zero-cache/src/services/view-syncer/pipeline-driver.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/snapshotter.d.ts +0 -2
- package/out/zero-cache/src/services/view-syncer/snapshotter.d.ts.map +1 -1
- package/out/zero-cache/src/services/view-syncer/snapshotter.js +2 -10
- package/out/zero-cache/src/services/view-syncer/snapshotter.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/view-syncer.d.ts +1 -2
- package/out/zero-cache/src/services/view-syncer/view-syncer.d.ts.map +1 -1
- package/out/zero-cache/src/services/view-syncer/view-syncer.js +40 -42
- package/out/zero-cache/src/services/view-syncer/view-syncer.js.map +1 -1
- package/out/zero-cache/src/workers/connect-params.d.ts +0 -1
- package/out/zero-cache/src/workers/connect-params.d.ts.map +1 -1
- package/out/zero-cache/src/workers/connect-params.js +0 -2
- package/out/zero-cache/src/workers/connect-params.js.map +1 -1
- package/out/zero-cache/src/workers/replicator.d.ts.map +1 -1
- package/out/zero-cache/src/workers/replicator.js +2 -5
- package/out/zero-cache/src/workers/replicator.js.map +1 -1
- package/out/zero-cache/src/workers/syncer-ws-message-handler.d.ts.map +1 -1
- package/out/zero-cache/src/workers/syncer-ws-message-handler.js +1 -4
- package/out/zero-cache/src/workers/syncer-ws-message-handler.js.map +1 -1
- package/out/zero-client/src/client/context.js +1 -0
- package/out/zero-client/src/client/context.js.map +1 -1
- package/out/zero-client/src/client/version.js +1 -1
- package/out/zero-protocol/src/push.d.ts +7 -0
- package/out/zero-protocol/src/push.d.ts.map +1 -1
- package/out/zero-protocol/src/push.js +9 -1
- package/out/zero-protocol/src/push.js.map +1 -1
- package/out/zero-server/src/process-mutations.d.ts +1 -0
- package/out/zero-server/src/process-mutations.d.ts.map +1 -1
- package/out/zero-server/src/process-mutations.js +41 -2
- package/out/zero-server/src/process-mutations.js.map +1 -1
- package/out/zero-server/src/zql-database.d.ts.map +1 -1
- package/out/zero-server/src/zql-database.js +9 -0
- package/out/zero-server/src/zql-database.js.map +1 -1
- package/out/zero-solid/src/solid-view.js +1 -0
- package/out/zero-solid/src/solid-view.js.map +1 -1
- package/out/zero-solid/src/use-query.js +1 -0
- package/out/zero-solid/src/use-query.js.map +1 -1
- package/package.json +3 -3
- package/out/zero-cache/src/types/schema-versions.d.ts +0 -12
- package/out/zero-cache/src/types/schema-versions.d.ts.map +0 -1
- package/out/zero-cache/src/types/schema-versions.js +0 -28
- package/out/zero-cache/src/types/schema-versions.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"change-processor.js","sources":["../../../../../../zero-cache/src/services/replicator/change-processor.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {SqliteError} from '@rocicorp/zero-sqlite3';\nimport {AbortError} from '../../../../shared/src/abort-error.ts';\nimport {assert, unreachable} from '../../../../shared/src/asserts.ts';\nimport {stringify} from '../../../../shared/src/bigint-json.ts';\nimport {must} from '../../../../shared/src/must.ts';\nimport {\n createLiteIndexStatement,\n createLiteTableStatement,\n liteColumnDef,\n} from '../../db/create.ts';\nimport {\n computeZqlSpecs,\n listIndexes,\n listTables,\n} from '../../db/lite-tables.ts';\nimport {\n mapPostgresToLite,\n mapPostgresToLiteColumn,\n mapPostgresToLiteIndex,\n} from '../../db/pg-to-lite.ts';\nimport type {LiteTableSpec} from '../../db/specs.ts';\nimport type {StatementRunner} from '../../db/statements.ts';\nimport {ColumnMetadataStore} from '../change-source/column-metadata.ts';\nimport type {LexiVersion} from '../../types/lexi-version.ts';\nimport {\n JSON_PARSED,\n liteRow,\n type JSONFormat,\n type LiteRow,\n type LiteRowKey,\n type LiteValueType,\n} from '../../types/lite.ts';\nimport {liteTableName} from '../../types/names.ts';\nimport {id} from '../../types/sql.ts';\nimport type {\n Change,\n ColumnAdd,\n ColumnDrop,\n ColumnUpdate,\n IndexCreate,\n IndexDrop,\n MessageCommit,\n MessageDelete,\n MessageInsert,\n MessageRelation,\n MessageTruncate,\n MessageUpdate,\n TableCreate,\n TableDrop,\n TableRename,\n} from '../change-source/protocol/current/data.ts';\nimport type {ChangeStreamData} from '../change-source/protocol/current/downstream.ts';\nimport type {ReplicatorMode} from './replicator.ts';\nimport {\n logDeleteOp,\n logResetOp,\n logSetOp,\n logTruncateOp,\n} from './schema/change-log.ts';\nimport {\n ZERO_VERSION_COLUMN_NAME,\n updateReplicationWatermark,\n} from './schema/replication-state.ts';\n\nexport type ChangeProcessorMode = ReplicatorMode | 'initial-sync';\n\nexport type CommitResult = {\n watermark: string;\n schemaUpdated: boolean;\n};\n\n/**\n * The ChangeProcessor partitions the stream of messages into transactions\n * by creating a {@link TransactionProcessor} when a transaction begins, and dispatching\n * messages to it until the commit is received.\n *\n * From https://www.postgresql.org/docs/current/protocol-logical-replication.html#PROTOCOL-LOGICAL-MESSAGES-FLOW :\n *\n * \"The logical replication protocol sends individual transactions one by one.\n * This means that all messages between a pair of Begin and Commit messages\n * belong to the same transaction.\"\n */\nexport class ChangeProcessor {\n readonly #db: StatementRunner;\n readonly #mode: ChangeProcessorMode;\n readonly #failService: (lc: LogContext, err: unknown) => void;\n\n // The TransactionProcessor lazily loads table specs into this Map,\n // and reloads them after a schema change. It is cached here to avoid\n // reading them from the DB on every transaction.\n readonly #tableSpecs = new Map<string, LiteTableSpec>();\n\n #currentTx: TransactionProcessor | null = null;\n\n #failure: Error | undefined;\n\n constructor(\n db: StatementRunner,\n mode: ChangeProcessorMode,\n failService: (lc: LogContext, err: unknown) => void,\n ) {\n this.#db = db;\n this.#mode = mode;\n this.#failService = failService;\n }\n\n #fail(lc: LogContext, err: unknown) {\n if (!this.#failure) {\n this.#currentTx?.abort(lc); // roll back any pending transaction.\n\n this.#failure = ensureError(err);\n\n if (!(err instanceof AbortError)) {\n // Propagate the failure up to the service.\n lc.error?.('Message Processing failed:', this.#failure);\n this.#failService(lc, this.#failure);\n }\n }\n }\n\n abort(lc: LogContext) {\n this.#fail(lc, new AbortError());\n }\n\n /** @return If a transaction was committed. */\n processMessage(\n lc: LogContext,\n downstream: ChangeStreamData,\n ): CommitResult | null {\n const [type, message] = downstream;\n if (this.#failure) {\n lc.debug?.(`Dropping ${message.tag}`);\n return null;\n }\n try {\n const watermark =\n type === 'begin'\n ? downstream[2].commitWatermark\n : type === 'commit'\n ? downstream[2].watermark\n : undefined;\n return this.#processMessage(lc, message, watermark);\n } catch (e) {\n this.#fail(lc, e);\n }\n return null;\n }\n\n #beginTransaction(\n lc: LogContext,\n commitVersion: string,\n jsonFormat: JSONFormat,\n ): TransactionProcessor {\n const start = Date.now();\n\n // litestream can technically hold the lock for an arbitrary amount of time\n // when checkpointing a large commit. Crashing on the busy-timeout in this\n // scenario will either produce a corrupt backup or otherwise prevent\n // replication from proceeding.\n //\n // Instead, retry the lock acquisition indefinitely. If this masks\n // an unknown deadlock situation, manual intervention will be necessary.\n for (let i = 0; ; i++) {\n try {\n return new TransactionProcessor(\n lc,\n this.#db,\n this.#mode,\n this.#tableSpecs,\n commitVersion,\n jsonFormat,\n );\n } catch (e) {\n if (e instanceof SqliteError && e.code === 'SQLITE_BUSY') {\n lc.warn?.(\n `SQLITE_BUSY for ${Date.now() - start} ms (attempt ${i + 1}). ` +\n `This is only expected if litestream is performing a large ` +\n `checkpoint.`,\n e,\n );\n continue;\n }\n throw e;\n }\n }\n }\n\n /** @return If a transaction was committed. */\n #processMessage(\n lc: LogContext,\n msg: Change,\n watermark: string | undefined,\n ): CommitResult | null {\n if (msg.tag === 'begin') {\n if (this.#currentTx) {\n throw new Error(`Already in a transaction ${stringify(msg)}`);\n }\n this.#currentTx = this.#beginTransaction(\n lc,\n must(watermark),\n msg.json ?? JSON_PARSED,\n );\n return null;\n }\n\n // For non-begin messages, there should be a #currentTx set.\n const tx = this.#currentTx;\n if (!tx) {\n throw new Error(\n `Received message outside of transaction: ${stringify(msg)}`,\n );\n }\n\n if (msg.tag === 'commit') {\n // Undef this.#currentTx to allow the assembly of the next transaction.\n this.#currentTx = null;\n\n assert(watermark);\n const schemaUpdated = tx.processCommit(msg, watermark);\n return {watermark, schemaUpdated};\n }\n\n if (msg.tag === 'rollback') {\n this.#currentTx?.abort(lc);\n this.#currentTx = null;\n return null;\n }\n\n switch (msg.tag) {\n case 'insert':\n tx.processInsert(msg);\n break;\n case 'update':\n tx.processUpdate(msg);\n break;\n case 'delete':\n tx.processDelete(msg);\n break;\n case 'truncate':\n tx.processTruncate(msg);\n break;\n case 'create-table':\n tx.processCreateTable(msg);\n break;\n case 'rename-table':\n tx.processRenameTable(msg);\n break;\n case 'add-column':\n tx.processAddColumn(msg);\n break;\n case 'update-column':\n tx.processUpdateColumn(msg);\n break;\n case 'drop-column':\n tx.processDropColumn(msg);\n break;\n case 'drop-table':\n tx.processDropTable(msg);\n break;\n case 'create-index':\n tx.processCreateIndex(msg);\n break;\n case 'drop-index':\n tx.processDropIndex(msg);\n break;\n default:\n unreachable(msg);\n }\n\n return null;\n }\n}\n\n/**\n * The {@link TransactionProcessor} handles the sequence of messages from\n * upstream, from `BEGIN` to `COMMIT` and executes the corresponding mutations\n * on the {@link postgres.TransactionSql} on the replica.\n *\n * When applying row contents to the replica, the `_0_version` column is added / updated,\n * and a corresponding entry in the `ChangeLog` is added. The version value is derived\n * from the watermark of the preceding transaction (stored as the `nextStateVersion` in the\n * `ReplicationState` table).\n *\n * Side note: For non-streaming Postgres transactions, the commitEndLsn (and thus\n * commit watermark) is available in the `begin` message, so it could theoretically\n * be used for the row version of changes within the transaction. However, the\n * commitEndLsn is not available in the streaming (in-progress) transaction\n * protocol, and may not be available for CDC streams of other upstream types.\n * Therefore, the zero replication protocol is designed to not require the commit\n * watermark when a transaction begins.\n *\n * Also of interest is the fact that all INSERT Messages are logically applied as\n * UPSERTs. See {@link processInsert} for the underlying motivation.\n */\nclass TransactionProcessor {\n readonly #lc: LogContext;\n readonly #startMs: number;\n readonly #db: StatementRunner;\n readonly #mode: ChangeProcessorMode;\n readonly #version: LexiVersion;\n readonly #tableSpecs: Map<string, LiteTableSpec>;\n readonly #jsonFormat: JSONFormat;\n\n #schemaChanged = false;\n\n constructor(\n lc: LogContext,\n db: StatementRunner,\n mode: ChangeProcessorMode,\n tableSpecs: Map<string, LiteTableSpec>,\n commitVersion: LexiVersion,\n jsonFormat: JSONFormat,\n ) {\n this.#startMs = Date.now();\n this.#mode = mode;\n this.#jsonFormat = jsonFormat;\n\n switch (mode) {\n case 'serving':\n // Although the Replicator / Incremental Syncer is the only writer of the replica,\n // a `BEGIN CONCURRENT` transaction is used to allow View Syncers to simulate\n // (i.e. and `ROLLBACK`) changes on historic snapshots of the database for the\n // purpose of IVM).\n //\n // This TransactionProcessor is the only logic that will actually\n // `COMMIT` any transactions to the replica.\n db.beginConcurrent();\n break;\n case 'backup':\n // For the backup-replicator (i.e. replication-manager), there are no View Syncers\n // and thus BEGIN CONCURRENT is not necessary. In fact, BEGIN CONCURRENT can cause\n // deadlocks with forced wal-checkpoints (which `litestream replicate` performs),\n // so it is important to use vanilla transactions in this configuration.\n db.beginImmediate();\n break;\n case 'initial-sync':\n // When the ChangeProcessor is used for initial-sync, the calling code\n // handles the transaction boundaries.\n break;\n default:\n unreachable();\n }\n this.#db = db;\n this.#version = commitVersion;\n this.#lc = lc.withContext('version', commitVersion);\n this.#tableSpecs = tableSpecs;\n\n if (this.#tableSpecs.size === 0) {\n this.#reloadTableSpecs();\n }\n }\n\n #reloadTableSpecs() {\n this.#tableSpecs.clear();\n // zqlSpecs include the primary key derived from unique indexes\n const zqlSpecs = computeZqlSpecs(this.#lc, this.#db.db);\n for (let spec of listTables(this.#db.db)) {\n if (!spec.primaryKey) {\n spec = {\n ...spec,\n primaryKey: [\n ...(zqlSpecs.get(spec.name)?.tableSpec.primaryKey ?? []),\n ],\n };\n }\n this.#tableSpecs.set(spec.name, spec);\n }\n }\n\n #tableSpec(name: string) {\n return must(this.#tableSpecs.get(name), `Unknown table ${name}`);\n }\n\n #getKey(\n {row, numCols}: {row: LiteRow; numCols: number},\n {relation}: {relation: MessageRelation},\n ): LiteRowKey {\n const keyColumns =\n relation.replicaIdentity !== 'full'\n ? relation.keyColumns // already a suitable key\n : this.#tableSpec(liteTableName(relation)).primaryKey;\n if (!keyColumns?.length) {\n throw new Error(\n `Cannot replicate table \"${relation.name}\" without a PRIMARY KEY or UNIQUE INDEX`,\n );\n }\n // For the common case (replica identity default), the row is already the\n // key for deletes and updates, in which case a new object can be avoided.\n if (numCols === keyColumns.length) {\n return row;\n }\n const key: Record<string, LiteValueType> = {};\n for (const col of keyColumns) {\n key[col] = row[col];\n }\n return key;\n }\n\n processInsert(insert: MessageInsert) {\n const table = liteTableName(insert.relation);\n const newRow = liteRow(\n insert.new,\n this.#tableSpec(table),\n this.#jsonFormat,\n );\n\n this.#upsert(table, {\n ...newRow.row,\n [ZERO_VERSION_COLUMN_NAME]: this.#version,\n });\n\n if (insert.relation.keyColumns.length === 0) {\n // INSERTs can be replicated for rows without a PRIMARY KEY or a\n // UNIQUE INDEX. These are written to the replica but not recorded\n // in the changeLog, because these rows cannot participate in IVM.\n //\n // (Once the table schema has been corrected to include a key, the\n // associated schema change will reset pipelines and data can be\n // loaded via hydration.)\n return;\n }\n const key = this.#getKey(newRow, insert);\n this.#logSetOp(table, key);\n }\n\n #upsert(table: string, row: LiteRow) {\n const columns = Object.keys(row).map(c => id(c));\n this.#db.run(\n `\n INSERT OR REPLACE INTO ${id(table)} (${columns.join(',')})\n VALUES (${Array.from({length: columns.length}).fill('?').join(',')})\n `,\n Object.values(row),\n );\n }\n\n // Updates by default are applied as UPDATE commands to support partial\n // row specifications from the change source. In particular, this is needed\n // to handle updates for which unchanged TOASTed values are not sent:\n //\n // https://www.postgresql.org/docs/current/protocol-logicalrep-message-formats.html#PROTOCOL-LOGICALREP-MESSAGE-FORMATS-TUPLEDATA\n //\n // However, in certain cases an UPDATE may be received for a row that\n // was not initially synced, such as when:\n // (1) an existing table is added to the app's publication, or\n // (2) a new sharding key is added to a shard during resharding.\n //\n // In order to facilitate \"resumptive\" replication, the logic falls back to\n // an INSERT if the update did not change any rows.\n // TODO: Figure out a solution for resumptive replication of rows\n // with TOASTed values.\n processUpdate(update: MessageUpdate) {\n const table = liteTableName(update.relation);\n const newRow = liteRow(\n update.new,\n this.#tableSpec(table),\n this.#jsonFormat,\n );\n const row = {...newRow.row, [ZERO_VERSION_COLUMN_NAME]: this.#version};\n\n // update.key is set with the old values if the key has changed.\n const oldKey = update.key\n ? this.#getKey(\n liteRow(update.key, this.#tableSpec(table), this.#jsonFormat),\n update,\n )\n : null;\n const newKey = this.#getKey(newRow, update);\n\n if (oldKey) {\n this.#logDeleteOp(table, oldKey);\n }\n this.#logSetOp(table, newKey);\n\n const currKey = oldKey ?? newKey;\n const conds = Object.keys(currKey).map(col => `${id(col)}=?`);\n const setExprs = Object.keys(row).map(col => `${id(col)}=?`);\n\n const {changes} = this.#db.run(\n `\n UPDATE ${id(table)}\n SET ${setExprs.join(',')}\n WHERE ${conds.join(' AND ')}\n `,\n [...Object.values(row), ...Object.values(currKey)],\n );\n\n // If the UPDATE did not affect any rows, perform an UPSERT of the\n // new row for resumptive replication.\n if (changes === 0) {\n this.#upsert(table, row);\n }\n }\n\n processDelete(del: MessageDelete) {\n const table = liteTableName(del.relation);\n const rowKey = this.#getKey(\n liteRow(del.key, this.#tableSpec(table), this.#jsonFormat),\n del,\n );\n\n this.#delete(table, rowKey);\n\n if (this.#mode === 'serving') {\n this.#logDeleteOp(table, rowKey);\n }\n }\n\n #delete(table: string, rowKey: LiteRowKey) {\n const conds = Object.keys(rowKey).map(col => `${id(col)}=?`);\n this.#db.run(\n `DELETE FROM ${id(table)} WHERE ${conds.join(' AND ')}`,\n Object.values(rowKey),\n );\n }\n\n processTruncate(truncate: MessageTruncate) {\n for (const relation of truncate.relations) {\n const table = liteTableName(relation);\n // Update replica data.\n this.#db.run(`DELETE FROM ${id(table)}`);\n\n // Update change log.\n this.#logTruncateOp(table);\n }\n }\n processCreateTable(create: TableCreate) {\n const table = mapPostgresToLite(create.spec);\n this.#db.db.exec(createLiteTableStatement(table));\n\n // Write to metadata table\n const store = ColumnMetadataStore.getInstance(this.#db.db);\n if (store) {\n for (const [colName, colSpec] of Object.entries(create.spec.columns)) {\n store.insert(table.name, colName, colSpec);\n }\n }\n\n this.#logResetOp(table.name);\n this.#lc.info?.(create.tag, table.name);\n }\n\n processRenameTable(rename: TableRename) {\n const oldName = liteTableName(rename.old);\n const newName = liteTableName(rename.new);\n this.#db.db.exec(`ALTER TABLE ${id(oldName)} RENAME TO ${id(newName)}`);\n\n // Rename in metadata table\n const store = ColumnMetadataStore.getInstance(this.#db.db);\n if (store) {\n store.renameTable(oldName, newName);\n }\n\n this.#bumpVersions(newName);\n this.#logResetOp(oldName);\n this.#lc.info?.(rename.tag, oldName, newName);\n }\n\n processAddColumn(msg: ColumnAdd) {\n const table = liteTableName(msg.table);\n const {name} = msg.column;\n const spec = mapPostgresToLiteColumn(table, msg.column);\n this.#db.db.exec(\n `ALTER TABLE ${id(table)} ADD ${id(name)} ${liteColumnDef(spec)}`,\n );\n\n // Write to metadata table\n const store = ColumnMetadataStore.getInstance(this.#db.db);\n if (store) {\n store.insert(table, name, msg.column.spec);\n }\n\n this.#bumpVersions(table);\n this.#lc.info?.(msg.tag, table, msg.column);\n }\n\n processUpdateColumn(msg: ColumnUpdate) {\n const table = liteTableName(msg.table);\n let oldName = msg.old.name;\n const newName = msg.new.name;\n\n // update-column can ignore defaults because it does not change the values\n // in existing rows.\n //\n // https://www.postgresql.org/docs/current/sql-altertable.html#SQL-ALTERTABLE-DESC-SET-DROP-DEFAULT\n //\n // \"The new default value will only apply in subsequent INSERT or UPDATE\n // commands; it does not cause rows already in the table to change.\"\n //\n // This allows support for _changing_ column defaults to any expression,\n // since it does not affect what the replica needs to do.\n const oldSpec = mapPostgresToLiteColumn(table, msg.old, 'ignore-default');\n const newSpec = mapPostgresToLiteColumn(table, msg.new, 'ignore-default');\n\n // The only updates that are relevant are the column name and the data type.\n if (oldName === newName && oldSpec.dataType === newSpec.dataType) {\n this.#lc.info?.(msg.tag, 'no thing to update', oldSpec, newSpec);\n return;\n }\n // If the data type changes, we have to make a new column with the new data type\n // and copy the values over.\n if (oldSpec.dataType !== newSpec.dataType) {\n // Remember (and drop) the indexes that reference the column.\n const indexes = listIndexes(this.#db.db).filter(\n idx => idx.tableName === table && oldName in idx.columns,\n );\n const stmts = indexes.map(idx => `DROP INDEX IF EXISTS ${id(idx.name)};`);\n const tmpName = `tmp.${newName}`;\n stmts.push(`\n ALTER TABLE ${id(table)} ADD ${id(tmpName)} ${liteColumnDef(newSpec)};\n UPDATE ${id(table)} SET ${id(tmpName)} = ${id(oldName)};\n ALTER TABLE ${id(table)} DROP ${id(oldName)};\n `);\n for (const idx of indexes) {\n // Re-create the indexes to reference the new column.\n idx.columns[tmpName] = idx.columns[oldName];\n delete idx.columns[oldName];\n stmts.push(createLiteIndexStatement(idx));\n }\n this.#db.db.exec(stmts.join(''));\n oldName = tmpName;\n }\n if (oldName !== newName) {\n this.#db.db.exec(\n `ALTER TABLE ${id(table)} RENAME ${id(oldName)} TO ${id(newName)}`,\n );\n }\n\n // Update metadata table\n const store = ColumnMetadataStore.getInstance(this.#db.db);\n if (store) {\n store.update(table, msg.old.name, msg.new.name, msg.new.spec);\n }\n\n this.#bumpVersions(table);\n this.#lc.info?.(msg.tag, table, msg.new);\n }\n\n processDropColumn(msg: ColumnDrop) {\n const table = liteTableName(msg.table);\n const {column} = msg;\n this.#db.db.exec(`ALTER TABLE ${id(table)} DROP ${id(column)}`);\n\n // Delete from metadata table\n const store = ColumnMetadataStore.getInstance(this.#db.db);\n if (store) {\n store.deleteColumn(table, column);\n }\n\n this.#bumpVersions(table);\n this.#lc.info?.(msg.tag, table, column);\n }\n\n processDropTable(drop: TableDrop) {\n const name = liteTableName(drop.id);\n this.#db.db.exec(`DROP TABLE IF EXISTS ${id(name)}`);\n\n // Delete from metadata table\n const store = ColumnMetadataStore.getInstance(this.#db.db);\n if (store) {\n store.deleteTable(name);\n }\n\n this.#logResetOp(name);\n this.#lc.info?.(drop.tag, name);\n }\n\n processCreateIndex(create: IndexCreate) {\n const index = mapPostgresToLiteIndex(create.spec);\n this.#db.db.exec(createLiteIndexStatement(index));\n\n // indexes affect tables visibility (e.g. sync-ability is gated on\n // having a unique index), so reset pipelines to refresh table schemas.\n this.#logResetOp(index.tableName);\n this.#lc.info?.(create.tag, index.name);\n }\n\n processDropIndex(drop: IndexDrop) {\n const name = liteTableName(drop.id);\n this.#db.db.exec(`DROP INDEX IF EXISTS ${id(name)}`);\n this.#lc.info?.(drop.tag, name);\n }\n\n #bumpVersions(table: string) {\n this.#db.run(\n `UPDATE ${id(table)} SET ${id(ZERO_VERSION_COLUMN_NAME)} = ?`,\n this.#version,\n );\n this.#logResetOp(table);\n }\n\n #logSetOp(table: string, key: LiteRowKey) {\n if (this.#mode === 'serving') {\n logSetOp(this.#db, this.#version, table, key);\n }\n }\n\n #logDeleteOp(table: string, key: LiteRowKey) {\n if (this.#mode === 'serving') {\n logDeleteOp(this.#db, this.#version, table, key);\n }\n }\n\n #logTruncateOp(table: string) {\n if (this.#mode === 'serving') {\n logTruncateOp(this.#db, this.#version, table);\n }\n }\n\n #logResetOp(table: string) {\n this.#schemaChanged = true;\n if (this.#mode === 'serving') {\n logResetOp(this.#db, this.#version, table);\n }\n this.#reloadTableSpecs();\n }\n\n /** @returns `true` if the schema was updated. */\n processCommit(commit: MessageCommit, watermark: string): boolean {\n if (watermark !== this.#version) {\n throw new Error(\n `'commit' version ${watermark} does not match 'begin' version ${\n this.#version\n }: ${stringify(commit)}`,\n );\n }\n updateReplicationWatermark(this.#db, watermark);\n\n if (this.#schemaChanged) {\n const start = Date.now();\n this.#db.db.pragma('optimize');\n this.#lc.info?.(\n `PRAGMA optimized after schema change (${Date.now() - start} ms)`,\n );\n }\n\n if (this.#mode !== 'initial-sync') {\n this.#db.commit();\n }\n\n const elapsedMs = Date.now() - this.#startMs;\n this.#lc.debug?.(`Committed tx@${this.#version} (${elapsedMs} ms)`);\n\n return this.#schemaChanged;\n }\n\n abort(lc: LogContext) {\n lc.info?.(`aborting transaction ${this.#version}`);\n this.#db.rollback();\n }\n}\n\nfunction ensureError(err: unknown): Error {\n if (err instanceof Error) {\n return err;\n }\n const error = new Error();\n error.cause = err;\n return error;\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;AAmFO,MAAM,gBAAgB;AAAA,EAClB;AAAA,EACA;AAAA,EACA;AAAA;AAAA;AAAA;AAAA,EAKA,kCAAkB,IAAA;AAAA,EAE3B,aAA0C;AAAA,EAE1C;AAAA,EAEA,YACE,IACA,MACA,aACA;AACA,SAAK,MAAM;AACX,SAAK,QAAQ;AACb,SAAK,eAAe;AAAA,EACtB;AAAA,EAEA,MAAM,IAAgB,KAAc;AAClC,QAAI,CAAC,KAAK,UAAU;AAClB,WAAK,YAAY,MAAM,EAAE;AAEzB,WAAK,WAAW,YAAY,GAAG;AAE/B,UAAI,EAAE,eAAe,aAAa;AAEhC,WAAG,QAAQ,8BAA8B,KAAK,QAAQ;AACtD,aAAK,aAAa,IAAI,KAAK,QAAQ;AAAA,MACrC;AAAA,IACF;AAAA,EACF;AAAA,EAEA,MAAM,IAAgB;AACpB,SAAK,MAAM,IAAI,IAAI,WAAA,CAAY;AAAA,EACjC;AAAA;AAAA,EAGA,eACE,IACA,YACqB;AACrB,UAAM,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,SAAS;AAE9C,QAAI,KAAK,gBAAgB;AACvB,YAAM,QAAQ,KAAK,IAAA;AACnB,WAAK,IAAI,GAAG,OAAO,UAAU;AAC7B,WAAK,IAAI;AAAA,QACP,yCAAyC,KAAK,IAAA,IAAQ,KAAK;AAAA,MAAA;AAAA,IAE/D;AAEA,QAAI,KAAK,UAAU,gBAAgB;AACjC,WAAK,IAAI,OAAA;AAAA,IACX;AAEA,UAAM,YAAY,KAAK,IAAA,IAAQ,KAAK;AACpC,SAAK,IAAI,QAAQ,gBAAgB,KAAK,QAAQ,KAAK,SAAS,MAAM;AAElE,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,MAAM,IAAgB;AACpB,OAAG,OAAO,wBAAwB,KAAK,QAAQ,EAAE;AACjD,SAAK,IAAI,SAAA;AAAA,EACX;AACF;AAEA,SAAS,YAAY,KAAqB;AACxC,MAAI,eAAe,OAAO;AACxB,WAAO;AAAA,EACT;AACA,QAAM,QAAQ,IAAI,MAAA;AAClB,QAAM,QAAQ;AACd,SAAO;AACT;"}
|
|
1
|
+
{"version":3,"file":"change-processor.js","sources":["../../../../../../zero-cache/src/services/replicator/change-processor.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {SqliteError} from '@rocicorp/zero-sqlite3';\nimport {AbortError} from '../../../../shared/src/abort-error.ts';\nimport {assert, unreachable} from '../../../../shared/src/asserts.ts';\nimport {stringify} from '../../../../shared/src/bigint-json.ts';\nimport {must} from '../../../../shared/src/must.ts';\nimport {\n createLiteIndexStatement,\n createLiteTableStatement,\n liteColumnDef,\n} from '../../db/create.ts';\nimport {\n computeZqlSpecs,\n listIndexes,\n listTables,\n} from '../../db/lite-tables.ts';\nimport {\n mapPostgresToLite,\n mapPostgresToLiteColumn,\n mapPostgresToLiteIndex,\n} from '../../db/pg-to-lite.ts';\nimport type {LiteTableSpec} from '../../db/specs.ts';\nimport type {StatementRunner} from '../../db/statements.ts';\nimport type {LexiVersion} from '../../types/lexi-version.ts';\nimport {\n JSON_PARSED,\n liteRow,\n type JSONFormat,\n type LiteRow,\n type LiteRowKey,\n type LiteValueType,\n} from '../../types/lite.ts';\nimport {liteTableName} from '../../types/names.ts';\nimport {id} from '../../types/sql.ts';\nimport {ColumnMetadataStore} from '../change-source/column-metadata.ts';\nimport type {\n Change,\n ColumnAdd,\n ColumnDrop,\n ColumnUpdate,\n IndexCreate,\n IndexDrop,\n MessageCommit,\n MessageDelete,\n MessageInsert,\n MessageRelation,\n MessageTruncate,\n MessageUpdate,\n TableCreate,\n TableDrop,\n TableRename,\n} from '../change-source/protocol/current/data.ts';\nimport type {ChangeStreamData} from '../change-source/protocol/current/downstream.ts';\nimport type {ReplicatorMode} from './replicator.ts';\nimport {\n logDeleteOp,\n logResetOp,\n logSetOp,\n logTruncateOp,\n} from './schema/change-log.ts';\nimport {\n ZERO_VERSION_COLUMN_NAME,\n updateReplicationWatermark,\n} from './schema/replication-state.ts';\n\nexport type ChangeProcessorMode = ReplicatorMode | 'initial-sync';\n\nexport type CommitResult = {\n watermark: string;\n schemaUpdated: boolean;\n};\n\n/**\n * The ChangeProcessor partitions the stream of messages into transactions\n * by creating a {@link TransactionProcessor} when a transaction begins, and dispatching\n * messages to it until the commit is received.\n *\n * From https://www.postgresql.org/docs/current/protocol-logical-replication.html#PROTOCOL-LOGICAL-MESSAGES-FLOW :\n *\n * \"The logical replication protocol sends individual transactions one by one.\n * This means that all messages between a pair of Begin and Commit messages\n * belong to the same transaction.\"\n */\nexport class ChangeProcessor {\n readonly #db: StatementRunner;\n readonly #mode: ChangeProcessorMode;\n readonly #failService: (lc: LogContext, err: unknown) => void;\n\n // The TransactionProcessor lazily loads table specs into this Map,\n // and reloads them after a schema change. It is cached here to avoid\n // reading them from the DB on every transaction.\n readonly #tableSpecs = new Map<string, LiteTableSpec>();\n\n #currentTx: TransactionProcessor | null = null;\n\n #failure: Error | undefined;\n\n constructor(\n db: StatementRunner,\n mode: ChangeProcessorMode,\n failService: (lc: LogContext, err: unknown) => void,\n ) {\n this.#db = db;\n this.#mode = mode;\n this.#failService = failService;\n }\n\n #fail(lc: LogContext, err: unknown) {\n if (!this.#failure) {\n this.#currentTx?.abort(lc); // roll back any pending transaction.\n\n this.#failure = ensureError(err);\n\n if (!(err instanceof AbortError)) {\n // Propagate the failure up to the service.\n lc.error?.('Message Processing failed:', this.#failure);\n this.#failService(lc, this.#failure);\n }\n }\n }\n\n abort(lc: LogContext) {\n this.#fail(lc, new AbortError());\n }\n\n /** @return If a transaction was committed. */\n processMessage(\n lc: LogContext,\n downstream: ChangeStreamData,\n ): CommitResult | null {\n const [type, message] = downstream;\n if (this.#failure) {\n lc.debug?.(`Dropping ${message.tag}`);\n return null;\n }\n try {\n const watermark =\n type === 'begin'\n ? downstream[2].commitWatermark\n : type === 'commit'\n ? downstream[2].watermark\n : undefined;\n return this.#processMessage(lc, message, watermark);\n } catch (e) {\n this.#fail(lc, e);\n }\n return null;\n }\n\n #beginTransaction(\n lc: LogContext,\n commitVersion: string,\n jsonFormat: JSONFormat,\n ): TransactionProcessor {\n const start = Date.now();\n\n // litestream can technically hold the lock for an arbitrary amount of time\n // when checkpointing a large commit. Crashing on the busy-timeout in this\n // scenario will either produce a corrupt backup or otherwise prevent\n // replication from proceeding.\n //\n // Instead, retry the lock acquisition indefinitely. If this masks\n // an unknown deadlock situation, manual intervention will be necessary.\n for (let i = 0; ; i++) {\n try {\n return new TransactionProcessor(\n lc,\n this.#db,\n this.#mode,\n this.#tableSpecs,\n commitVersion,\n jsonFormat,\n );\n } catch (e) {\n if (e instanceof SqliteError && e.code === 'SQLITE_BUSY') {\n lc.warn?.(\n `SQLITE_BUSY for ${Date.now() - start} ms (attempt ${i + 1}). ` +\n `This is only expected if litestream is performing a large ` +\n `checkpoint.`,\n e,\n );\n continue;\n }\n throw e;\n }\n }\n }\n\n /** @return If a transaction was committed. */\n #processMessage(\n lc: LogContext,\n msg: Change,\n watermark: string | undefined,\n ): CommitResult | null {\n if (msg.tag === 'begin') {\n if (this.#currentTx) {\n throw new Error(`Already in a transaction ${stringify(msg)}`);\n }\n this.#currentTx = this.#beginTransaction(\n lc,\n must(watermark),\n msg.json ?? JSON_PARSED,\n );\n return null;\n }\n\n // For non-begin messages, there should be a #currentTx set.\n const tx = this.#currentTx;\n if (!tx) {\n throw new Error(\n `Received message outside of transaction: ${stringify(msg)}`,\n );\n }\n\n if (msg.tag === 'commit') {\n // Undef this.#currentTx to allow the assembly of the next transaction.\n this.#currentTx = null;\n\n assert(watermark);\n const schemaUpdated = tx.processCommit(msg, watermark);\n return {watermark, schemaUpdated};\n }\n\n if (msg.tag === 'rollback') {\n this.#currentTx?.abort(lc);\n this.#currentTx = null;\n return null;\n }\n\n switch (msg.tag) {\n case 'insert':\n tx.processInsert(msg);\n break;\n case 'update':\n tx.processUpdate(msg);\n break;\n case 'delete':\n tx.processDelete(msg);\n break;\n case 'truncate':\n tx.processTruncate(msg);\n break;\n case 'create-table':\n tx.processCreateTable(msg);\n break;\n case 'rename-table':\n tx.processRenameTable(msg);\n break;\n case 'add-column':\n tx.processAddColumn(msg);\n break;\n case 'update-column':\n tx.processUpdateColumn(msg);\n break;\n case 'drop-column':\n tx.processDropColumn(msg);\n break;\n case 'drop-table':\n tx.processDropTable(msg);\n break;\n case 'create-index':\n tx.processCreateIndex(msg);\n break;\n case 'drop-index':\n tx.processDropIndex(msg);\n break;\n default:\n unreachable(msg);\n }\n\n return null;\n }\n}\n\n/**\n * The {@link TransactionProcessor} handles the sequence of messages from\n * upstream, from `BEGIN` to `COMMIT` and executes the corresponding mutations\n * on the {@link postgres.TransactionSql} on the replica.\n *\n * When applying row contents to the replica, the `_0_version` column is added / updated,\n * and a corresponding entry in the `ChangeLog` is added. The version value is derived\n * from the watermark of the preceding transaction (stored as the `nextStateVersion` in the\n * `ReplicationState` table).\n *\n * Side note: For non-streaming Postgres transactions, the commitEndLsn (and thus\n * commit watermark) is available in the `begin` message, so it could theoretically\n * be used for the row version of changes within the transaction. However, the\n * commitEndLsn is not available in the streaming (in-progress) transaction\n * protocol, and may not be available for CDC streams of other upstream types.\n * Therefore, the zero replication protocol is designed to not require the commit\n * watermark when a transaction begins.\n *\n * Also of interest is the fact that all INSERT Messages are logically applied as\n * UPSERTs. See {@link processInsert} for the underlying motivation.\n */\nclass TransactionProcessor {\n readonly #lc: LogContext;\n readonly #startMs: number;\n readonly #db: StatementRunner;\n readonly #mode: ChangeProcessorMode;\n readonly #version: LexiVersion;\n readonly #tableSpecs: Map<string, LiteTableSpec>;\n readonly #jsonFormat: JSONFormat;\n\n #pos = 0;\n #schemaChanged = false;\n\n constructor(\n lc: LogContext,\n db: StatementRunner,\n mode: ChangeProcessorMode,\n tableSpecs: Map<string, LiteTableSpec>,\n commitVersion: LexiVersion,\n jsonFormat: JSONFormat,\n ) {\n this.#startMs = Date.now();\n this.#mode = mode;\n this.#jsonFormat = jsonFormat;\n\n switch (mode) {\n case 'serving':\n // Although the Replicator / Incremental Syncer is the only writer of the replica,\n // a `BEGIN CONCURRENT` transaction is used to allow View Syncers to simulate\n // (i.e. and `ROLLBACK`) changes on historic snapshots of the database for the\n // purpose of IVM).\n //\n // This TransactionProcessor is the only logic that will actually\n // `COMMIT` any transactions to the replica.\n db.beginConcurrent();\n break;\n case 'backup':\n // For the backup-replicator (i.e. replication-manager), there are no View Syncers\n // and thus BEGIN CONCURRENT is not necessary. In fact, BEGIN CONCURRENT can cause\n // deadlocks with forced wal-checkpoints (which `litestream replicate` performs),\n // so it is important to use vanilla transactions in this configuration.\n db.beginImmediate();\n break;\n case 'initial-sync':\n // When the ChangeProcessor is used for initial-sync, the calling code\n // handles the transaction boundaries.\n break;\n default:\n unreachable();\n }\n this.#db = db;\n this.#version = commitVersion;\n this.#lc = lc.withContext('version', commitVersion);\n this.#tableSpecs = tableSpecs;\n\n if (this.#tableSpecs.size === 0) {\n this.#reloadTableSpecs();\n }\n }\n\n #reloadTableSpecs() {\n this.#tableSpecs.clear();\n // zqlSpecs include the primary key derived from unique indexes\n const zqlSpecs = computeZqlSpecs(this.#lc, this.#db.db);\n for (let spec of listTables(this.#db.db)) {\n if (!spec.primaryKey) {\n spec = {\n ...spec,\n primaryKey: [\n ...(zqlSpecs.get(spec.name)?.tableSpec.primaryKey ?? []),\n ],\n };\n }\n this.#tableSpecs.set(spec.name, spec);\n }\n }\n\n #tableSpec(name: string) {\n return must(this.#tableSpecs.get(name), `Unknown table ${name}`);\n }\n\n #getKey(\n {row, numCols}: {row: LiteRow; numCols: number},\n {relation}: {relation: MessageRelation},\n ): LiteRowKey {\n const keyColumns =\n relation.replicaIdentity !== 'full'\n ? relation.keyColumns // already a suitable key\n : this.#tableSpec(liteTableName(relation)).primaryKey;\n if (!keyColumns?.length) {\n throw new Error(\n `Cannot replicate table \"${relation.name}\" without a PRIMARY KEY or UNIQUE INDEX`,\n );\n }\n // For the common case (replica identity default), the row is already the\n // key for deletes and updates, in which case a new object can be avoided.\n if (numCols === keyColumns.length) {\n return row;\n }\n const key: Record<string, LiteValueType> = {};\n for (const col of keyColumns) {\n key[col] = row[col];\n }\n return key;\n }\n\n processInsert(insert: MessageInsert) {\n const table = liteTableName(insert.relation);\n const newRow = liteRow(\n insert.new,\n this.#tableSpec(table),\n this.#jsonFormat,\n );\n\n this.#upsert(table, {\n ...newRow.row,\n [ZERO_VERSION_COLUMN_NAME]: this.#version,\n });\n\n if (insert.relation.keyColumns.length === 0) {\n // INSERTs can be replicated for rows without a PRIMARY KEY or a\n // UNIQUE INDEX. These are written to the replica but not recorded\n // in the changeLog, because these rows cannot participate in IVM.\n //\n // (Once the table schema has been corrected to include a key, the\n // associated schema change will reset pipelines and data can be\n // loaded via hydration.)\n return;\n }\n const key = this.#getKey(newRow, insert);\n this.#logSetOp(table, key);\n }\n\n #upsert(table: string, row: LiteRow) {\n const columns = Object.keys(row).map(c => id(c));\n this.#db.run(\n `\n INSERT OR REPLACE INTO ${id(table)} (${columns.join(',')})\n VALUES (${Array.from({length: columns.length}).fill('?').join(',')})\n `,\n Object.values(row),\n );\n }\n\n // Updates by default are applied as UPDATE commands to support partial\n // row specifications from the change source. In particular, this is needed\n // to handle updates for which unchanged TOASTed values are not sent:\n //\n // https://www.postgresql.org/docs/current/protocol-logicalrep-message-formats.html#PROTOCOL-LOGICALREP-MESSAGE-FORMATS-TUPLEDATA\n //\n // However, in certain cases an UPDATE may be received for a row that\n // was not initially synced, such as when:\n // (1) an existing table is added to the app's publication, or\n // (2) a new sharding key is added to a shard during resharding.\n //\n // In order to facilitate \"resumptive\" replication, the logic falls back to\n // an INSERT if the update did not change any rows.\n // TODO: Figure out a solution for resumptive replication of rows\n // with TOASTed values.\n processUpdate(update: MessageUpdate) {\n const table = liteTableName(update.relation);\n const newRow = liteRow(\n update.new,\n this.#tableSpec(table),\n this.#jsonFormat,\n );\n const row = {...newRow.row, [ZERO_VERSION_COLUMN_NAME]: this.#version};\n\n // update.key is set with the old values if the key has changed.\n const oldKey = update.key\n ? this.#getKey(\n liteRow(update.key, this.#tableSpec(table), this.#jsonFormat),\n update,\n )\n : null;\n const newKey = this.#getKey(newRow, update);\n\n if (oldKey) {\n this.#logDeleteOp(table, oldKey);\n }\n this.#logSetOp(table, newKey);\n\n const currKey = oldKey ?? newKey;\n const conds = Object.keys(currKey).map(col => `${id(col)}=?`);\n const setExprs = Object.keys(row).map(col => `${id(col)}=?`);\n\n const {changes} = this.#db.run(\n `\n UPDATE ${id(table)}\n SET ${setExprs.join(',')}\n WHERE ${conds.join(' AND ')}\n `,\n [...Object.values(row), ...Object.values(currKey)],\n );\n\n // If the UPDATE did not affect any rows, perform an UPSERT of the\n // new row for resumptive replication.\n if (changes === 0) {\n this.#upsert(table, row);\n }\n }\n\n processDelete(del: MessageDelete) {\n const table = liteTableName(del.relation);\n const rowKey = this.#getKey(\n liteRow(del.key, this.#tableSpec(table), this.#jsonFormat),\n del,\n );\n\n this.#delete(table, rowKey);\n\n if (this.#mode === 'serving') {\n this.#logDeleteOp(table, rowKey);\n }\n }\n\n #delete(table: string, rowKey: LiteRowKey) {\n const conds = Object.keys(rowKey).map(col => `${id(col)}=?`);\n this.#db.run(\n `DELETE FROM ${id(table)} WHERE ${conds.join(' AND ')}`,\n Object.values(rowKey),\n );\n }\n\n processTruncate(truncate: MessageTruncate) {\n for (const relation of truncate.relations) {\n const table = liteTableName(relation);\n // Update replica data.\n this.#db.run(`DELETE FROM ${id(table)}`);\n\n // Update change log.\n this.#logTruncateOp(table);\n }\n }\n processCreateTable(create: TableCreate) {\n const table = mapPostgresToLite(create.spec);\n this.#db.db.exec(createLiteTableStatement(table));\n\n // Write to metadata table\n const store = ColumnMetadataStore.getInstance(this.#db.db);\n if (store) {\n for (const [colName, colSpec] of Object.entries(create.spec.columns)) {\n store.insert(table.name, colName, colSpec);\n }\n }\n\n this.#logResetOp(table.name);\n this.#lc.info?.(create.tag, table.name);\n }\n\n processRenameTable(rename: TableRename) {\n const oldName = liteTableName(rename.old);\n const newName = liteTableName(rename.new);\n this.#db.db.exec(`ALTER TABLE ${id(oldName)} RENAME TO ${id(newName)}`);\n\n // Rename in metadata table\n const store = ColumnMetadataStore.getInstance(this.#db.db);\n if (store) {\n store.renameTable(oldName, newName);\n }\n\n this.#bumpVersions(newName);\n this.#logResetOp(oldName);\n this.#lc.info?.(rename.tag, oldName, newName);\n }\n\n processAddColumn(msg: ColumnAdd) {\n const table = liteTableName(msg.table);\n const {name} = msg.column;\n const spec = mapPostgresToLiteColumn(table, msg.column);\n this.#db.db.exec(\n `ALTER TABLE ${id(table)} ADD ${id(name)} ${liteColumnDef(spec)}`,\n );\n\n // Write to metadata table\n const store = ColumnMetadataStore.getInstance(this.#db.db);\n if (store) {\n store.insert(table, name, msg.column.spec);\n }\n\n this.#bumpVersions(table);\n this.#lc.info?.(msg.tag, table, msg.column);\n }\n\n processUpdateColumn(msg: ColumnUpdate) {\n const table = liteTableName(msg.table);\n let oldName = msg.old.name;\n const newName = msg.new.name;\n\n // update-column can ignore defaults because it does not change the values\n // in existing rows.\n //\n // https://www.postgresql.org/docs/current/sql-altertable.html#SQL-ALTERTABLE-DESC-SET-DROP-DEFAULT\n //\n // \"The new default value will only apply in subsequent INSERT or UPDATE\n // commands; it does not cause rows already in the table to change.\"\n //\n // This allows support for _changing_ column defaults to any expression,\n // since it does not affect what the replica needs to do.\n const oldSpec = mapPostgresToLiteColumn(table, msg.old, 'ignore-default');\n const newSpec = mapPostgresToLiteColumn(table, msg.new, 'ignore-default');\n\n // The only updates that are relevant are the column name and the data type.\n if (oldName === newName && oldSpec.dataType === newSpec.dataType) {\n this.#lc.info?.(msg.tag, 'no thing to update', oldSpec, newSpec);\n return;\n }\n // If the data type changes, we have to make a new column with the new data type\n // and copy the values over.\n if (oldSpec.dataType !== newSpec.dataType) {\n // Remember (and drop) the indexes that reference the column.\n const indexes = listIndexes(this.#db.db).filter(\n idx => idx.tableName === table && oldName in idx.columns,\n );\n const stmts = indexes.map(idx => `DROP INDEX IF EXISTS ${id(idx.name)};`);\n const tmpName = `tmp.${newName}`;\n stmts.push(`\n ALTER TABLE ${id(table)} ADD ${id(tmpName)} ${liteColumnDef(newSpec)};\n UPDATE ${id(table)} SET ${id(tmpName)} = ${id(oldName)};\n ALTER TABLE ${id(table)} DROP ${id(oldName)};\n `);\n for (const idx of indexes) {\n // Re-create the indexes to reference the new column.\n idx.columns[tmpName] = idx.columns[oldName];\n delete idx.columns[oldName];\n stmts.push(createLiteIndexStatement(idx));\n }\n this.#db.db.exec(stmts.join(''));\n oldName = tmpName;\n }\n if (oldName !== newName) {\n this.#db.db.exec(\n `ALTER TABLE ${id(table)} RENAME ${id(oldName)} TO ${id(newName)}`,\n );\n }\n\n // Update metadata table\n const store = ColumnMetadataStore.getInstance(this.#db.db);\n if (store) {\n store.update(table, msg.old.name, msg.new.name, msg.new.spec);\n }\n\n this.#bumpVersions(table);\n this.#lc.info?.(msg.tag, table, msg.new);\n }\n\n processDropColumn(msg: ColumnDrop) {\n const table = liteTableName(msg.table);\n const {column} = msg;\n this.#db.db.exec(`ALTER TABLE ${id(table)} DROP ${id(column)}`);\n\n // Delete from metadata table\n const store = ColumnMetadataStore.getInstance(this.#db.db);\n if (store) {\n store.deleteColumn(table, column);\n }\n\n this.#bumpVersions(table);\n this.#lc.info?.(msg.tag, table, column);\n }\n\n processDropTable(drop: TableDrop) {\n const name = liteTableName(drop.id);\n this.#db.db.exec(`DROP TABLE IF EXISTS ${id(name)}`);\n\n // Delete from metadata table\n const store = ColumnMetadataStore.getInstance(this.#db.db);\n if (store) {\n store.deleteTable(name);\n }\n\n this.#logResetOp(name);\n this.#lc.info?.(drop.tag, name);\n }\n\n processCreateIndex(create: IndexCreate) {\n const index = mapPostgresToLiteIndex(create.spec);\n this.#db.db.exec(createLiteIndexStatement(index));\n\n // indexes affect tables visibility (e.g. sync-ability is gated on\n // having a unique index), so reset pipelines to refresh table schemas.\n this.#logResetOp(index.tableName);\n this.#lc.info?.(create.tag, index.name);\n }\n\n processDropIndex(drop: IndexDrop) {\n const name = liteTableName(drop.id);\n this.#db.db.exec(`DROP INDEX IF EXISTS ${id(name)}`);\n this.#lc.info?.(drop.tag, name);\n }\n\n #bumpVersions(table: string) {\n this.#db.run(\n `UPDATE ${id(table)} SET ${id(ZERO_VERSION_COLUMN_NAME)} = ?`,\n this.#version,\n );\n this.#logResetOp(table);\n }\n\n #logSetOp(table: string, key: LiteRowKey) {\n if (this.#mode === 'serving') {\n logSetOp(this.#db, this.#version, this.#pos++, table, key);\n }\n }\n\n #logDeleteOp(table: string, key: LiteRowKey) {\n if (this.#mode === 'serving') {\n logDeleteOp(this.#db, this.#version, this.#pos++, table, key);\n }\n }\n\n #logTruncateOp(table: string) {\n if (this.#mode === 'serving') {\n logTruncateOp(this.#db, this.#version, table);\n }\n }\n\n #logResetOp(table: string) {\n this.#schemaChanged = true;\n if (this.#mode === 'serving') {\n logResetOp(this.#db, this.#version, table);\n }\n this.#reloadTableSpecs();\n }\n\n /** @returns `true` if the schema was updated. */\n processCommit(commit: MessageCommit, watermark: string): boolean {\n if (watermark !== this.#version) {\n throw new Error(\n `'commit' version ${watermark} does not match 'begin' version ${\n this.#version\n }: ${stringify(commit)}`,\n );\n }\n updateReplicationWatermark(this.#db, watermark);\n\n if (this.#schemaChanged) {\n const start = Date.now();\n this.#db.db.pragma('optimize');\n this.#lc.info?.(\n `PRAGMA optimized after schema change (${Date.now() - start} ms)`,\n );\n }\n\n if (this.#mode !== 'initial-sync') {\n this.#db.commit();\n }\n\n const elapsedMs = Date.now() - this.#startMs;\n this.#lc.debug?.(`Committed tx@${this.#version} (${elapsedMs} ms)`);\n\n return this.#schemaChanged;\n }\n\n abort(lc: LogContext) {\n lc.info?.(`aborting transaction ${this.#version}`);\n this.#db.rollback();\n }\n}\n\nfunction ensureError(err: unknown): Error {\n if (err instanceof Error) {\n return err;\n }\n const error = new Error();\n error.cause = err;\n return error;\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;AAmFO,MAAM,gBAAgB;AAAA,EAClB;AAAA,EACA;AAAA,EACA;AAAA;AAAA;AAAA;AAAA,EAKA,kCAAkB,IAAA;AAAA,EAE3B,aAA0C;AAAA,EAE1C;AAAA,EAEA,YACE,IACA,MACA,aACA;AACA,SAAK,MAAM;AACX,SAAK,QAAQ;AACb,SAAK,eAAe;AAAA,EACtB;AAAA,EAEA,MAAM,IAAgB,KAAc;AAClC,QAAI,CAAC,KAAK,UAAU;AAClB,WAAK,YAAY,MAAM,EAAE;AAEzB,WAAK,WAAW,YAAY,GAAG;AAE/B,UAAI,EAAE,eAAe,aAAa;AAEhC,WAAG,QAAQ,8BAA8B,KAAK,QAAQ;AACtD,aAAK,aAAa,IAAI,KAAK,QAAQ;AAAA,MACrC;AAAA,IACF;AAAA,EACF;AAAA,EAEA,MAAM,IAAgB;AACpB,SAAK,MAAM,IAAI,IAAI,WAAA,CAAY;AAAA,EACjC;AAAA;AAAA,EAGA,eACE,IACA,YACqB;AACrB,UAAM,CAAC,MAAM,OAAO,IAAI;AACxB,QAAI,KAAK,UAAU;AACjB,SAAG,QAAQ,YAAY,QAAQ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;AACvB,YAAM,QAAQ,KAAK,IAAA;AACnB,WAAK,IAAI,GAAG,OAAO,UAAU;AAC7B,WAAK,IAAI;AAAA,QACP,yCAAyC,KAAK,IAAA,IAAQ,KAAK;AAAA,MAAA;AAAA,IAE/D;AAEA,QAAI,KAAK,UAAU,gBAAgB;AACjC,WAAK,IAAI,OAAA;AAAA,IACX;AAEA,UAAM,YAAY,KAAK,IAAA,IAAQ,KAAK;AACpC,SAAK,IAAI,QAAQ,gBAAgB,KAAK,QAAQ,KAAK,SAAS,MAAM;AAElE,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,MAAM,IAAgB;AACpB,OAAG,OAAO,wBAAwB,KAAK,QAAQ,EAAE;AACjD,SAAK,IAAI,SAAA;AAAA,EACX;AACF;AAEA,SAAS,YAAY,KAAqB;AACxC,MAAI,eAAe,OAAO;AACxB,WAAO;AAAA,EACT;AACA,QAAM,QAAQ,IAAI,MAAA;AAClB,QAAM,QAAQ;AACd,SAAO;AACT;"}
|
|
@@ -40,13 +40,14 @@ export declare const RESET_OP = "r";
|
|
|
40
40
|
export declare const changeLogEntrySchema: v.Type<{
|
|
41
41
|
rowKey: Readonly<Record<string, import("../../../../../shared/src/bigint-json.ts").JSONValue>> | null;
|
|
42
42
|
stateVersion: string;
|
|
43
|
+
pos: number;
|
|
43
44
|
table: string;
|
|
44
45
|
op: "d" | "r" | "s" | "t";
|
|
45
46
|
}>;
|
|
46
47
|
export type ChangeLogEntry = v.Infer<typeof changeLogEntrySchema>;
|
|
47
48
|
export declare function initChangeLog(db: Database): void;
|
|
48
|
-
export declare function logSetOp(db: StatementRunner, version: LexiVersion, table: string, row: LiteRowKey): string;
|
|
49
|
-
export declare function logDeleteOp(db: StatementRunner, version: LexiVersion, table: string, row: LiteRowKey): string;
|
|
49
|
+
export declare function logSetOp(db: StatementRunner, version: LexiVersion, pos: number, table: string, row: LiteRowKey): string;
|
|
50
|
+
export declare function logDeleteOp(db: StatementRunner, version: LexiVersion, pos: number, table: string, row: LiteRowKey): string;
|
|
50
51
|
export declare function logTruncateOp(db: StatementRunner, version: LexiVersion, table: string): void;
|
|
51
52
|
export declare function logResetOp(db: StatementRunner, version: LexiVersion, table: string): void;
|
|
52
53
|
//# sourceMappingURL=change-log.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"change-log.d.ts","sourceRoot":"","sources":["../../../../../../../zero-cache/src/services/replicator/schema/change-log.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,CAAC,MAAM,qCAAqC,CAAC;AACzD,OAAO,KAAK,EAAC,QAAQ,EAAC,MAAM,iCAAiC,CAAC;AAC9D,OAAO,KAAK,EAAC,eAAe,EAAC,MAAM,2BAA2B,CAAC;AAC/D,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,gCAAgC,CAAC;AAChE,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,wBAAwB,CAAC;AAGvD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH,eAAO,MAAM,MAAM,MAAM,CAAC;AAC1B,eAAO,MAAM,MAAM,MAAM,CAAC;AAC1B,eAAO,MAAM,WAAW,MAAM,CAAC;AAC/B,eAAO,MAAM,QAAQ,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"change-log.d.ts","sourceRoot":"","sources":["../../../../../../../zero-cache/src/services/replicator/schema/change-log.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,CAAC,MAAM,qCAAqC,CAAC;AACzD,OAAO,KAAK,EAAC,QAAQ,EAAC,MAAM,iCAAiC,CAAC;AAC9D,OAAO,KAAK,EAAC,eAAe,EAAC,MAAM,2BAA2B,CAAC;AAC/D,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,gCAAgC,CAAC;AAChE,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,wBAAwB,CAAC;AAGvD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH,eAAO,MAAM,MAAM,MAAM,CAAC;AAC1B,eAAO,MAAM,MAAM,MAAM,CAAC;AAC1B,eAAO,MAAM,WAAW,MAAM,CAAC;AAC/B,eAAO,MAAM,QAAQ,MAAM,CAAC;AAgC5B,eAAO,MAAM,oBAAoB;;;;;;EAe5B,CAAC;AAEN,MAAM,MAAM,cAAc,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,oBAAoB,CAAC,CAAC;AAElE,wBAAgB,aAAa,CAAC,EAAE,EAAE,QAAQ,QAEzC;AAED,wBAAgB,QAAQ,CACtB,EAAE,EAAE,eAAe,EACnB,OAAO,EAAE,WAAW,EACpB,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,MAAM,EACb,GAAG,EAAE,UAAU,GACd,MAAM,CAER;AAED,wBAAgB,WAAW,CACzB,EAAE,EAAE,eAAe,EACnB,OAAO,EAAE,WAAW,EACpB,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,MAAM,EACb,GAAG,EAAE,UAAU,GACd,MAAM,CAER;AAqBD,wBAAgB,aAAa,CAC3B,EAAE,EAAE,eAAe,EACnB,OAAO,EAAE,WAAW,EACpB,KAAK,EAAE,MAAM,QAGd;AAED,wBAAgB,UAAU,CACxB,EAAE,EAAE,eAAe,EACnB,OAAO,EAAE,WAAW,EACpB,KAAK,EAAE,MAAM,QAGd"}
|
|
@@ -1,64 +1,70 @@
|
|
|
1
1
|
import { jsonObjectSchema, parse as parse$1, stringify } from "../../../../../shared/src/bigint-json.js";
|
|
2
2
|
import { literalUnion, parse } from "../../../../../shared/src/valita.js";
|
|
3
3
|
import { normalizedKeyOrder } from "../../../types/row-key.js";
|
|
4
|
-
import { object, string } from "@badrap/valita";
|
|
4
|
+
import { object, string, number } from "@badrap/valita";
|
|
5
5
|
const SET_OP = "s";
|
|
6
6
|
const DEL_OP = "d";
|
|
7
7
|
const TRUNCATE_OP = "t";
|
|
8
8
|
const RESET_OP = "r";
|
|
9
9
|
const CREATE_CHANGELOG_SCHEMA = (
|
|
10
10
|
// stateVersion : a.k.a. row version
|
|
11
|
+
// pos : order in which to process the change (within the version)
|
|
11
12
|
// table : The table associated with the change
|
|
12
|
-
// rowKey : JSON row key for a row change. For table-wide changes
|
|
13
|
-
//
|
|
14
|
-
//
|
|
15
|
-
//
|
|
16
|
-
//
|
|
17
|
-
//
|
|
18
|
-
//
|
|
19
|
-
//
|
|
13
|
+
// rowKey : JSON row key for a row change. For table-wide changes RESET
|
|
14
|
+
// and TRUNCATE, there is no associated row; instead, `pos` is
|
|
15
|
+
// set to -1 and the rowKey is set to the stateVersion,
|
|
16
|
+
// guaranteeing when attempting to process the transaction,
|
|
17
|
+
// the pipeline is reset (and the change log traversal
|
|
18
|
+
// aborted).
|
|
19
|
+
// op : 's' for set (insert/update)
|
|
20
|
+
// : 'd' for delete
|
|
20
21
|
// : 'r' for table reset (schema change)
|
|
21
|
-
//
|
|
22
|
-
//
|
|
22
|
+
// : 't' for table truncation (which also resets the pipeline)
|
|
23
|
+
//
|
|
24
|
+
// Naming note: To maintain compatibility between a new replication-manager
|
|
25
|
+
// and old view-syncers, the previous _zero.changeLog table is preserved
|
|
26
|
+
// and its replacement given a new name "changeLog2".
|
|
23
27
|
`
|
|
24
|
-
CREATE TABLE "_zero.
|
|
28
|
+
CREATE TABLE "_zero.changeLog2" (
|
|
25
29
|
"stateVersion" TEXT NOT NULL,
|
|
30
|
+
"pos" INT NOT NULL,
|
|
26
31
|
"table" TEXT NOT NULL,
|
|
27
|
-
"rowKey" TEXT,
|
|
32
|
+
"rowKey" TEXT NOT NULL,
|
|
28
33
|
"op" TEXT NOT NULL,
|
|
29
|
-
PRIMARY KEY("stateVersion", "
|
|
34
|
+
PRIMARY KEY("stateVersion", "pos"),
|
|
30
35
|
UNIQUE("table", "rowKey")
|
|
31
36
|
)
|
|
32
37
|
`
|
|
33
38
|
);
|
|
34
39
|
const changeLogEntrySchema = object({
|
|
35
40
|
stateVersion: string(),
|
|
41
|
+
pos: number(),
|
|
36
42
|
table: string(),
|
|
37
|
-
rowKey: string()
|
|
43
|
+
rowKey: string(),
|
|
38
44
|
op: literalUnion(SET_OP, DEL_OP, TRUNCATE_OP, RESET_OP)
|
|
39
45
|
}).map((val) => ({
|
|
40
46
|
...val,
|
|
41
|
-
// Note: the
|
|
42
|
-
rowKey: val.
|
|
47
|
+
// Note: sets the rowKey to `null` for table-wide ops / resets
|
|
48
|
+
rowKey: val.op === "t" || val.op === "r" ? null : parse(parse$1(val.rowKey), jsonObjectSchema)
|
|
43
49
|
}));
|
|
44
50
|
function initChangeLog(db) {
|
|
45
51
|
db.exec(CREATE_CHANGELOG_SCHEMA);
|
|
46
52
|
}
|
|
47
|
-
function logSetOp(db, version, table, row) {
|
|
48
|
-
return logRowOp(db, version, table, row, SET_OP);
|
|
53
|
+
function logSetOp(db, version, pos, table, row) {
|
|
54
|
+
return logRowOp(db, version, pos, table, row, SET_OP);
|
|
49
55
|
}
|
|
50
|
-
function logDeleteOp(db, version, table, row) {
|
|
51
|
-
return logRowOp(db, version, table, row, DEL_OP);
|
|
56
|
+
function logDeleteOp(db, version, pos, table, row) {
|
|
57
|
+
return logRowOp(db, version, pos, table, row, DEL_OP);
|
|
52
58
|
}
|
|
53
|
-
function logRowOp(db, version, table, row, op) {
|
|
59
|
+
function logRowOp(db, version, pos, table, row, op) {
|
|
54
60
|
const rowKey = stringify(normalizedKeyOrder(row));
|
|
55
61
|
db.run(
|
|
56
62
|
`
|
|
57
|
-
INSERT OR REPLACE INTO "_zero.
|
|
58
|
-
(stateVersion, "table", rowKey, op)
|
|
59
|
-
VALUES (@version, @table, JSON(@rowKey), @op)
|
|
63
|
+
INSERT OR REPLACE INTO "_zero.changeLog2"
|
|
64
|
+
(stateVersion, pos, "table", rowKey, op)
|
|
65
|
+
VALUES (@version, @pos, @table, JSON(@rowKey), @op)
|
|
60
66
|
`,
|
|
61
|
-
{ version, table, rowKey, op }
|
|
67
|
+
{ version, pos, table, rowKey, op }
|
|
62
68
|
);
|
|
63
69
|
return rowKey;
|
|
64
70
|
}
|
|
@@ -71,18 +77,17 @@ function logResetOp(db, version, table) {
|
|
|
71
77
|
function logTableWideOp(db, version, table, op) {
|
|
72
78
|
db.run(
|
|
73
79
|
`
|
|
74
|
-
DELETE FROM "_zero.
|
|
80
|
+
DELETE FROM "_zero.changeLog2" WHERE stateVersion = ? AND "table" = ?
|
|
75
81
|
`,
|
|
76
82
|
version,
|
|
77
83
|
table
|
|
78
84
|
);
|
|
79
85
|
db.run(
|
|
80
86
|
`
|
|
81
|
-
INSERT OR REPLACE INTO "_zero.
|
|
82
|
-
VALUES (@version, @table, @
|
|
87
|
+
INSERT OR REPLACE INTO "_zero.changeLog2" (stateVersion, pos, "table", rowKey, op)
|
|
88
|
+
VALUES (@version, -1, @table, @version, @op)
|
|
83
89
|
`,
|
|
84
|
-
|
|
85
|
-
{ version, table, rowKey: "", op }
|
|
90
|
+
{ version, table, op }
|
|
86
91
|
);
|
|
87
92
|
}
|
|
88
93
|
export {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"change-log.js","sources":["../../../../../../../zero-cache/src/services/replicator/schema/change-log.ts"],"sourcesContent":["import {\n jsonObjectSchema,\n parse,\n stringify,\n} from '../../../../../shared/src/bigint-json.ts';\nimport * as v from '../../../../../shared/src/valita.ts';\nimport type {Database} from '../../../../../zqlite/src/db.ts';\nimport type {StatementRunner} from '../../../db/statements.ts';\nimport type {LexiVersion} from '../../../types/lexi-version.ts';\nimport type {LiteRowKey} from '../../../types/lite.ts';\nimport {normalizedKeyOrder} from '../../../types/row-key.ts';\n\n/**\n * The Change Log tracks the last operation (set or delete) for each row in the\n * data base, ordered by state version; in other words, a cross-table\n * index of row changes ordered by version. This facilitates a minimal \"diff\"\n * of row changes needed to advance a pipeline from one state version to another.\n *\n * The Change Log stores identifiers only, i.e. it does not store contents.\n * A database snapshot at the previous version can be used to query a row's\n * old contents, if any, and the current snapshot can be used to query a row's\n * new contents. (In the common case, the new contents will have just been applied\n * and thus has a high likelihood of being in the SQLite cache.)\n *\n * There are two table-wide operations:\n * - `t` corresponds to the postgres `TRUNCATE` operation\n * - `r` represents any schema (i.e. column) change\n *\n * For both operations, the corresponding row changes are not explicitly included\n * in the change log. The consumer has the option of simulating them be reading\n * from pre- and post- snapshots, or resetting their state entirely with the current\n * snapshot.\n *\n * To achieve the desired ordering semantics when processing tables that have been\n * truncated, reset, and modified, the \"rowKey\" is set to `null` for resets and\n * the empty string `\"\"` for truncates. This means that resets will be encountered\n * before truncates, which will be processed before any subsequent row changes.\n *\n * This ordering is chosen because resets are currently the more \"destructive\" op\n * and result in aborting the processing (and starting from scratch); doing this\n * earlier reduces wasted work.\n */\n\nexport const SET_OP = 's';\nexport const DEL_OP = 'd';\nexport const TRUNCATE_OP = 't';\nexport const RESET_OP = 'r';\n\nconst CREATE_CHANGELOG_SCHEMA =\n // stateVersion : a.k.a. row version\n // table : The table associated with the change\n // rowKey : JSON row key for a row change. For table-wide changes
|
|
1
|
+
{"version":3,"file":"change-log.js","sources":["../../../../../../../zero-cache/src/services/replicator/schema/change-log.ts"],"sourcesContent":["import {\n jsonObjectSchema,\n parse,\n stringify,\n} from '../../../../../shared/src/bigint-json.ts';\nimport * as v from '../../../../../shared/src/valita.ts';\nimport type {Database} from '../../../../../zqlite/src/db.ts';\nimport type {StatementRunner} from '../../../db/statements.ts';\nimport type {LexiVersion} from '../../../types/lexi-version.ts';\nimport type {LiteRowKey} from '../../../types/lite.ts';\nimport {normalizedKeyOrder} from '../../../types/row-key.ts';\n\n/**\n * The Change Log tracks the last operation (set or delete) for each row in the\n * data base, ordered by state version; in other words, a cross-table\n * index of row changes ordered by version. This facilitates a minimal \"diff\"\n * of row changes needed to advance a pipeline from one state version to another.\n *\n * The Change Log stores identifiers only, i.e. it does not store contents.\n * A database snapshot at the previous version can be used to query a row's\n * old contents, if any, and the current snapshot can be used to query a row's\n * new contents. (In the common case, the new contents will have just been applied\n * and thus has a high likelihood of being in the SQLite cache.)\n *\n * There are two table-wide operations:\n * - `t` corresponds to the postgres `TRUNCATE` operation\n * - `r` represents any schema (i.e. column) change\n *\n * For both operations, the corresponding row changes are not explicitly included\n * in the change log. The consumer has the option of simulating them be reading\n * from pre- and post- snapshots, or resetting their state entirely with the current\n * snapshot.\n *\n * To achieve the desired ordering semantics when processing tables that have been\n * truncated, reset, and modified, the \"rowKey\" is set to `null` for resets and\n * the empty string `\"\"` for truncates. This means that resets will be encountered\n * before truncates, which will be processed before any subsequent row changes.\n *\n * This ordering is chosen because resets are currently the more \"destructive\" op\n * and result in aborting the processing (and starting from scratch); doing this\n * earlier reduces wasted work.\n */\n\nexport const SET_OP = 's';\nexport const DEL_OP = 'd';\nexport const TRUNCATE_OP = 't';\nexport const RESET_OP = 'r';\n\nconst CREATE_CHANGELOG_SCHEMA =\n // stateVersion : a.k.a. row version\n // pos : order in which to process the change (within the version)\n // table : The table associated with the change\n // rowKey : JSON row key for a row change. For table-wide changes RESET\n // and TRUNCATE, there is no associated row; instead, `pos` is\n // set to -1 and the rowKey is set to the stateVersion,\n // guaranteeing when attempting to process the transaction,\n // the pipeline is reset (and the change log traversal\n // aborted).\n // op : 's' for set (insert/update)\n // : 'd' for delete\n // : 'r' for table reset (schema change)\n // : 't' for table truncation (which also resets the pipeline)\n //\n // Naming note: To maintain compatibility between a new replication-manager\n // and old view-syncers, the previous _zero.changeLog table is preserved\n // and its replacement given a new name \"changeLog2\".\n `\n CREATE TABLE \"_zero.changeLog2\" (\n \"stateVersion\" TEXT NOT NULL,\n \"pos\" INT NOT NULL,\n \"table\" TEXT NOT NULL,\n \"rowKey\" TEXT NOT NULL,\n \"op\" TEXT NOT NULL,\n PRIMARY KEY(\"stateVersion\", \"pos\"),\n UNIQUE(\"table\", \"rowKey\")\n )\n `;\n\nexport const changeLogEntrySchema = v\n .object({\n stateVersion: v.string(),\n pos: v.number(),\n table: v.string(),\n rowKey: v.string(),\n op: v.literalUnion(SET_OP, DEL_OP, TRUNCATE_OP, RESET_OP),\n })\n .map(val => ({\n ...val,\n // Note: sets the rowKey to `null` for table-wide ops / resets\n rowKey:\n val.op === 't' || val.op === 'r'\n ? null\n : v.parse(parse(val.rowKey), jsonObjectSchema),\n }));\n\nexport type ChangeLogEntry = v.Infer<typeof changeLogEntrySchema>;\n\nexport function initChangeLog(db: Database) {\n db.exec(CREATE_CHANGELOG_SCHEMA);\n}\n\nexport function logSetOp(\n db: StatementRunner,\n version: LexiVersion,\n pos: number,\n table: string,\n row: LiteRowKey,\n): string {\n return logRowOp(db, version, pos, table, row, SET_OP);\n}\n\nexport function logDeleteOp(\n db: StatementRunner,\n version: LexiVersion,\n pos: number,\n table: string,\n row: LiteRowKey,\n): string {\n return logRowOp(db, version, pos, table, row, DEL_OP);\n}\n\nfunction logRowOp(\n db: StatementRunner,\n version: LexiVersion,\n pos: number,\n table: string,\n row: LiteRowKey,\n op: string,\n): string {\n const rowKey = stringify(normalizedKeyOrder(row));\n db.run(\n `\n INSERT OR REPLACE INTO \"_zero.changeLog2\" \n (stateVersion, pos, \"table\", rowKey, op)\n VALUES (@version, @pos, @table, JSON(@rowKey), @op)\n `,\n {version, pos, table, rowKey, op},\n );\n return rowKey;\n}\nexport function logTruncateOp(\n db: StatementRunner,\n version: LexiVersion,\n table: string,\n) {\n logTableWideOp(db, version, table, TRUNCATE_OP);\n}\n\nexport function logResetOp(\n db: StatementRunner,\n version: LexiVersion,\n table: string,\n) {\n logTableWideOp(db, version, table, RESET_OP);\n}\n\nfunction logTableWideOp(\n db: StatementRunner,\n version: LexiVersion,\n table: string,\n op: 't' | 'r',\n) {\n // Delete any existing changes for the table (in this version) since the\n // table wide op invalidates them.\n db.run(\n `\n DELETE FROM \"_zero.changeLog2\" WHERE stateVersion = ? AND \"table\" = ?\n `,\n version,\n table,\n );\n\n db.run(\n `\n INSERT OR REPLACE INTO \"_zero.changeLog2\" (stateVersion, pos, \"table\", rowKey, op) \n VALUES (@version, -1, @table, @version, @op)\n `,\n {version, table, op},\n );\n}\n"],"names":["v.object","v.string","v.number","v.literalUnion","v.parse","parse"],"mappings":";;;;AA2CO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AAEA,SAAS,eACP,IACA,SACA,OACA,IACA;AAGA,KAAG;AAAA,IACD;AAAA;AAAA;AAAA,IAGA;AAAA,IACA;AAAA,EAAA;AAGF,KAAG;AAAA,IACD;AAAA;AAAA;AAAA;AAAA,IAIA,EAAC,SAAS,OAAO,GAAA;AAAA,EAAE;AAEvB;"}
|
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import type { LogContext } from '@rocicorp/logger';
|
|
2
2
|
import type { JSONObject } from '../../../../shared/src/bigint-json.ts';
|
|
3
3
|
import { type JSONObject as SafeJSONObject } from '../../../../shared/src/json.ts';
|
|
4
|
+
import type { ErroredQuery } from '../../../../zero-protocol/src/custom-queries.ts';
|
|
4
5
|
import type { Downstream } from '../../../../zero-protocol/src/down.ts';
|
|
6
|
+
import { type TransformFailedBody } from '../../../../zero-protocol/src/error.ts';
|
|
5
7
|
import type { InspectDownBody } from '../../../../zero-protocol/src/inspect-down.ts';
|
|
6
|
-
import { type SchemaVersions } from '../../types/schema-versions.ts';
|
|
7
8
|
import { type ShardID } from '../../types/shards.ts';
|
|
8
9
|
import type { Subscription } from '../../types/subscription.ts';
|
|
9
10
|
import { type CVRVersion, type DelQueryPatch, type NullableCVRVersion, type PutQueryPatch, type RowID } from './schema/types.ts';
|
|
10
|
-
import type { ErroredQuery } from '../../../../zero-protocol/src/custom-queries.ts';
|
|
11
|
-
import { type TransformFailedBody } from '../../../../zero-protocol/src/error.ts';
|
|
12
11
|
export type PutRowPatch = {
|
|
13
12
|
type: 'row';
|
|
14
13
|
op: 'put';
|
|
@@ -33,7 +32,7 @@ export interface PokeHandler {
|
|
|
33
32
|
end(finalVersion: CVRVersion): Promise<void>;
|
|
34
33
|
}
|
|
35
34
|
/** Wraps PokeHandlers for multiple clients in a single PokeHandler. */
|
|
36
|
-
export declare function startPoke(clients: ClientHandler[], tentativeVersion: CVRVersion
|
|
35
|
+
export declare function startPoke(clients: ClientHandler[], tentativeVersion: CVRVersion): PokeHandler;
|
|
37
36
|
/**
|
|
38
37
|
* Handles a single `ViewSyncer` connection.
|
|
39
38
|
*/
|
|
@@ -41,11 +40,11 @@ export declare class ClientHandler {
|
|
|
41
40
|
#private;
|
|
42
41
|
readonly clientID: string;
|
|
43
42
|
readonly wsID: string;
|
|
44
|
-
constructor(lc: LogContext, clientGroupID: string, clientID: string, wsID: string, shard: ShardID, baseCookie: string | null,
|
|
43
|
+
constructor(lc: LogContext, clientGroupID: string, clientID: string, wsID: string, shard: ShardID, baseCookie: string | null, downstream: Subscription<Downstream>);
|
|
45
44
|
version(): NullableCVRVersion;
|
|
46
45
|
fail(e: unknown): void;
|
|
47
46
|
close(reason: string): void;
|
|
48
|
-
startPoke(tentativeVersion: CVRVersion
|
|
47
|
+
startPoke(tentativeVersion: CVRVersion): PokeHandler;
|
|
49
48
|
sendDeleteClients(lc: LogContext, deletedClientIDs: string[], deletedClientGroupIDs: string[]): Promise<void>;
|
|
50
49
|
sendQueryTransformApplicationErrors(errors: ErroredQuery[]): void;
|
|
51
50
|
sendQueryTransformFailedError(error: TransformFailedBody): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client-handler.d.ts","sourceRoot":"","sources":["../../../../../../zero-cache/src/services/view-syncer/client-handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAEjD,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,uCAAuC,CAAC;AACtE,OAAO,EAEL,KAAK,UAAU,IAAI,cAAc,EAClC,MAAM,gCAAgC,CAAC;
|
|
1
|
+
{"version":3,"file":"client-handler.d.ts","sourceRoot":"","sources":["../../../../../../zero-cache/src/services/view-syncer/client-handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAEjD,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,uCAAuC,CAAC;AACtE,OAAO,EAEL,KAAK,UAAU,IAAI,cAAc,EAClC,MA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AE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC9C;AAQD,uEAAuE;AACvE,wBAAgB,SAAS,CACvB,OAAO,EAAE,aAAa,EAAE,EACxB,gBAAgB,EAAE,UAAU,GAC3B,WAAW,CAiBb;AAMD;;GAEG;AACH,qBAAa,aAAa;;IAExB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;gBA0BpB,EAAE,EAAE,UAAU,EACd,aAAa,EAAE,MAAM,EACrB,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,OAAO,EACd,UAAU,EAAE,MAAM,GAAG,IAAI,EACzB,UAAU,EAAE,YAAY,CAAC,UAAU,CAAC;IAatC,OAAO,IAAI,kBAAkB;IAS7B,IAAI,CAAC,CAAC,EAAE,OAAO;IAQf,KAAK,CAAC,MAAM,EAAE,MAAM;IAKpB,SAAS,CAAC,gBAAgB,EAAE,UAAU,GAAG,WAAW;IAwJ9C,iBAAiB,CACrB,EAAE,EAAE,UAAU,EACd,gBAAgB,EAAE,MAAM,EAAE,EAC1B,qBAAqB,EAAE,MAAM,EAAE;IAajC,mCAAmC,CAAC,MAAM,EAAE,YAAY,EAAE;IAI1D,6BAA6B,CAAC,KAAK,EAAE,mBAAmB;IAIxD,mBAAmB,CAAC,EAAE,EAAE,UAAU,EAAE,QAAQ,EAAE,eAAe,GAAG,IAAI;CA0BrE;AA0CD;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,UAAU,GAAG,cAAc,CAkB9D"}
|
|
@@ -3,24 +3,21 @@ import { assertJSONValue } from "../../../../shared/src/json.js";
|
|
|
3
3
|
import { promiseVoid } from "../../../../shared/src/resolved-promises.js";
|
|
4
4
|
import { parse } from "../../../../shared/src/valita.js";
|
|
5
5
|
import { rowSchema } from "../../../../zero-protocol/src/data.js";
|
|
6
|
+
import { ProtocolError } from "../../../../zero-protocol/src/error.js";
|
|
6
7
|
import { primaryKeyValueRecordSchema } from "../../../../zero-protocol/src/primary-key.js";
|
|
7
8
|
import { mutationResultSchema } from "../../../../zero-protocol/src/push.js";
|
|
8
9
|
import { getOrCreateHistogram, getOrCreateCounter } from "../../observability/metrics.js";
|
|
9
10
|
import { getLogLevel, wrapWithProtocolError } from "../../types/error-with-level.js";
|
|
10
|
-
import { getProtocolErrorIfSchemaVersionNotSupported } from "../../types/schema-versions.js";
|
|
11
11
|
import { upstreamSchema } from "../../types/shards.js";
|
|
12
12
|
import { cookieToVersion, versionToCookie, cmpVersions, versionToNullableCookie } from "./schema/types.js";
|
|
13
|
-
import { ProtocolError } from "../../../../zero-protocol/src/error.js";
|
|
14
13
|
import { object, number, string } from "@badrap/valita";
|
|
15
14
|
const NOOP = {
|
|
16
15
|
addPatch: () => promiseVoid,
|
|
17
16
|
cancel: () => promiseVoid,
|
|
18
17
|
end: () => promiseVoid
|
|
19
18
|
};
|
|
20
|
-
function startPoke(clients, tentativeVersion
|
|
21
|
-
const pokers = clients.map(
|
|
22
|
-
(c) => c.startPoke(tentativeVersion, schemaVersions)
|
|
23
|
-
);
|
|
19
|
+
function startPoke(clients, tentativeVersion) {
|
|
20
|
+
const pokers = clients.map((c) => c.startPoke(tentativeVersion));
|
|
24
21
|
return {
|
|
25
22
|
addPatch: async (patch) => {
|
|
26
23
|
await Promise.allSettled(pokers.map((poker) => poker.addPatch(patch)));
|
|
@@ -43,7 +40,6 @@ class ClientHandler {
|
|
|
43
40
|
#lc;
|
|
44
41
|
#downstream;
|
|
45
42
|
#baseVersion;
|
|
46
|
-
#schemaVersion;
|
|
47
43
|
#pokeTime = getOrCreateHistogram("sync", "poke.time", {
|
|
48
44
|
description: "Time elapsed for each poke transaction. Canceled / noop pokes are excluded.",
|
|
49
45
|
unit: "s"
|
|
@@ -58,7 +54,7 @@ class ClientHandler {
|
|
|
58
54
|
"poke.rows",
|
|
59
55
|
"Count of poked rows."
|
|
60
56
|
);
|
|
61
|
-
constructor(lc, clientGroupID, clientID, wsID, shard, baseCookie,
|
|
57
|
+
constructor(lc, clientGroupID, clientID, wsID, shard, baseCookie, downstream) {
|
|
62
58
|
lc.debug?.("new client handler");
|
|
63
59
|
this.#clientGroupID = clientGroupID;
|
|
64
60
|
this.clientID = clientID;
|
|
@@ -68,7 +64,6 @@ class ClientHandler {
|
|
|
68
64
|
this.#lc = lc;
|
|
69
65
|
this.#downstream = downstream;
|
|
70
66
|
this.#baseVersion = cookieToVersion(baseCookie);
|
|
71
|
-
this.#schemaVersion = schemaVersion;
|
|
72
67
|
}
|
|
73
68
|
version() {
|
|
74
69
|
return this.#baseVersion;
|
|
@@ -88,19 +83,9 @@ class ClientHandler {
|
|
|
88
83
|
this.#lc.debug?.(`view-syncer closing connection: ${reason}`);
|
|
89
84
|
this.#downstream.cancel();
|
|
90
85
|
}
|
|
91
|
-
startPoke(tentativeVersion
|
|
86
|
+
startPoke(tentativeVersion) {
|
|
92
87
|
const pokeID = versionToCookie(tentativeVersion);
|
|
93
88
|
const lc = this.#lc.withContext("pokeID", pokeID);
|
|
94
|
-
if (schemaVersions && this.#schemaVersion) {
|
|
95
|
-
const schemaVersionError = getProtocolErrorIfSchemaVersionNotSupported(
|
|
96
|
-
this.#schemaVersion,
|
|
97
|
-
schemaVersions
|
|
98
|
-
);
|
|
99
|
-
if (schemaVersionError) {
|
|
100
|
-
this.fail(schemaVersionError);
|
|
101
|
-
return NOOP;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
89
|
if (cmpVersions(this.#baseVersion, tentativeVersion) >= 0) {
|
|
105
90
|
lc.info?.(`already caught up, not sending poke.`);
|
|
106
91
|
return NOOP;
|
|
@@ -110,9 +95,6 @@ class ClientHandler {
|
|
|
110
95
|
lc.info?.(`starting poke from ${baseCookie} to ${cookie}`);
|
|
111
96
|
const start = performance.now();
|
|
112
97
|
const pokeStart = { pokeID, baseCookie };
|
|
113
|
-
if (schemaVersions) {
|
|
114
|
-
pokeStart.schemaVersions = schemaVersions;
|
|
115
|
-
}
|
|
116
98
|
let pokeStarted = false;
|
|
117
99
|
let body;
|
|
118
100
|
let partCount = 0;
|