-
Notifications
You must be signed in to change notification settings - Fork 25
MySQL replication fixes #170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
570f94d
Handle MySQL servers with non-UTC timezone.
rkistner 4889504
Use same config for ZongJi in tests as in the service.
rkistner d3c8659
Only set timezone offset on the pool, for compatibility with mysql 5.7.
rkistner ec8b6ef
Use query.stream() to improve error handling of snapshots.
rkistner 1008b1e
Do not modify original rows when replicating.
rkistner 65da8be
Gracefully handle invalid dates.
rkistner 0f28898
Specifically set time_zone on snapshot connections.
rkistner 8728878
Fix resuming replication after a restart.
rkistner 0241924
Avoid duplicating row conversion on update.
rkistner 7fb0f21
Properly escape table name in snapshot query.
rkistner ae803e6
Test updating a row with timestamp values.
rkistner 5cb51bd
Refactor BinLogStream tests to use `await using`.
rkistner b4c668c
NULLable columns in tests, to avoid auto-updating timestamp in MySQL 5.7
rkistner 4f0b804
Test resuming replication.
rkistner 900cb15
Add changesets.
rkistner 79a4d5d
Remove debug conditional.
rkistner 5666fb2
Remove defunct test.
rkistner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@powersync/service-module-mysql': patch | ||
| '@powersync/service-image': patch | ||
| --- | ||
|
|
||
| Fix timestamp replication issues for MySQL. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@powersync/service-module-mysql': patch | ||
| '@powersync/service-image': patch | ||
| --- | ||
|
|
||
| Fix resuming MySQL replication after a restart. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,7 +11,7 @@ import * as zongji_utils from './zongji/zongji-utils.js'; | |
| import { MySQLConnectionManager } from './MySQLConnectionManager.js'; | ||
| import { isBinlogStillAvailable, ReplicatedGTID, toColumnDescriptors } from '../common/common-index.js'; | ||
| import mysqlPromise from 'mysql2/promise'; | ||
| import { createRandomServerId } from '../utils/mysql-utils.js'; | ||
| import { createRandomServerId, escapeMysqlTableName } from '../utils/mysql-utils.js'; | ||
|
|
||
| export interface BinLogStreamOptions { | ||
| connections: MySQLConnectionManager; | ||
|
|
@@ -114,8 +114,10 @@ export class BinLogStream { | |
| // Start the snapshot inside a transaction. | ||
| // We use a dedicated connection for this. | ||
| const connection = await this.connections.getStreamingConnection(); | ||
|
|
||
| const promiseConnection = (connection as mysql.Connection).promise(); | ||
| try { | ||
| await promiseConnection.query(`SET time_zone = '+00:00'`); | ||
| await promiseConnection.query('BEGIN'); | ||
| try { | ||
| gtid = await common.readExecutedGtid(promiseConnection); | ||
|
|
@@ -258,6 +260,8 @@ AND table_type = 'BASE TABLE';`, | |
| 'SET TRANSACTION ISOLATION LEVEL REPEATABLE READ, READ ONLY' | ||
| ); | ||
| await promiseConnection.query<mysqlPromise.RowDataPacket[]>('START TRANSACTION'); | ||
| await promiseConnection.query(`SET time_zone = '+00:00'`); | ||
|
|
||
| const sourceTables = this.syncRules.getSourceTables(); | ||
| await this.storage.startBatch( | ||
| { zeroLSN: ReplicatedGTID.ZERO.comparable, defaultSchema: this.defaultSchema, storeCurrentData: true }, | ||
|
|
@@ -291,38 +295,32 @@ AND table_type = 'BASE TABLE';`, | |
| logger.info(`Replicating ${table.qualifiedName}`); | ||
| // TODO count rows and log progress at certain batch sizes | ||
|
|
||
| let columns: Map<string, ColumnDescriptor>; | ||
| return new Promise<void>((resolve, reject) => { | ||
| // MAX_EXECUTION_TIME(0) hint disables execution timeout for this query | ||
| connection | ||
| .query(`SELECT /*+ MAX_EXECUTION_TIME(0) */ * FROM ${table.schema}.${table.table}`) | ||
| .on('error', (err) => { | ||
| reject(err); | ||
| }) | ||
| .on('fields', (fields: FieldPacket[]) => { | ||
| // Map the columns and their types | ||
| columns = toColumnDescriptors(fields); | ||
| }) | ||
| .on('result', async (row) => { | ||
| connection.pause(); | ||
| const record = common.toSQLiteRow(row, columns); | ||
|
|
||
| await batch.save({ | ||
| tag: storage.SaveOperationTag.INSERT, | ||
| sourceTable: table, | ||
| before: undefined, | ||
| beforeReplicaId: undefined, | ||
| after: record, | ||
| afterReplicaId: getUuidReplicaIdentityBson(record, table.replicaIdColumns) | ||
| }); | ||
| connection.resume(); | ||
| Metrics.getInstance().rows_replicated_total.add(1); | ||
| }) | ||
| .on('end', async function () { | ||
| await batch.flush(); | ||
| resolve(); | ||
| }); | ||
| // MAX_EXECUTION_TIME(0) hint disables execution timeout for this query | ||
| const query = connection.query(`SELECT /*+ MAX_EXECUTION_TIME(0) */ * FROM ${escapeMysqlTableName(table)}`); | ||
| const stream = query.stream(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a nice simplification to the streaming process! 🥳 |
||
|
|
||
| let columns: Map<string, ColumnDescriptor> | undefined = undefined; | ||
| stream.on('fields', (fields: FieldPacket[]) => { | ||
| // Map the columns and their types | ||
| columns = toColumnDescriptors(fields); | ||
| }); | ||
|
|
||
| for await (let row of query.stream()) { | ||
| if (columns == null) { | ||
| throw new Error(`No 'fields' event emitted`); | ||
| } | ||
| const record = common.toSQLiteRow(row, columns!); | ||
|
|
||
| await batch.save({ | ||
| tag: storage.SaveOperationTag.INSERT, | ||
| sourceTable: table, | ||
| before: undefined, | ||
| beforeReplicaId: undefined, | ||
| after: record, | ||
| afterReplicaId: getUuidReplicaIdentityBson(record, table.replicaIdColumns) | ||
| }); | ||
| } | ||
| await batch.flush(); | ||
| } | ||
|
|
||
| async replicate() { | ||
|
|
@@ -350,6 +348,18 @@ AND table_type = 'BASE TABLE';`, | |
| const initialReplicationCompleted = await this.checkInitialReplicated(); | ||
| if (!initialReplicationCompleted) { | ||
| await this.startInitialReplication(); | ||
| } else { | ||
| // We need to find the existing tables, to populate our table cache. | ||
| // This is needed for includeSchema to work correctly. | ||
| const sourceTables = this.syncRules.getSourceTables(); | ||
| await this.storage.startBatch( | ||
| { zeroLSN: ReplicatedGTID.ZERO.comparable, defaultSchema: this.defaultSchema, storeCurrentData: true }, | ||
| async (batch) => { | ||
| for (let tablePattern of sourceTables) { | ||
| await this.getQualifiedTableNames(batch, tablePattern); | ||
| } | ||
| } | ||
| ); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -578,7 +588,7 @@ AND table_type = 'BASE TABLE';`, | |
| beforeReplicaId: beforeUpdated | ||
| ? getUuidReplicaIdentityBson(beforeUpdated, payload.sourceTable.replicaIdColumns) | ||
| : undefined, | ||
| after: common.toSQLiteRow(payload.data, payload.columns), | ||
| after: after, | ||
| afterReplicaId: getUuidReplicaIdentityBson(after, payload.sourceTable.replicaIdColumns) | ||
| }); | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.