gh-ost icon indicating copy to clipboard operation
gh-ost copied to clipboard

Convert column to destination charset in DML applications

Open jbielick opened this issue 5 years ago β€’ 14 comments

Related issue: https://github.com/github/gh-ost/issues/290

Description

Avoid DML apply errors by converting character data when charset changes for a column.

Background

When applying DML events (UPDATE, DELETE, etc) the values are sprintf'd into a prepared statement with the row snapshot (values). Due to the possibility of migrating text column data containing characters in the source table that are invalid in the destination table (due to charset), a conversion step is often necessary. This conversion does not occur when applying DML events and an error occurs writing invalid byte sequences to the ghost table.

For example, when migrating a table/column from latin1 to utf8mb4, the latin1 column may contain characters that are invalid single-byte utf8 characters. Characters in the \x80-\xFF range are most common. When written to utf8mb4 column without conversion, they fail as they do not exist in the utf8 codepage.

Converting these texts/characters to the destination charset using convert(? using {charset}) will convert appropriately and the update/replace will succeed.

Note: there is currently no issue backfilling the ghost table when the characterset changes, likely because it's a insert-into-select-from and it all occurs within mysql. I only point this out because there are two tests added for this: latin1text-to-utf8mb4 and latin1text-to-ut8mb4-insertβ€”the former is a test that fails prior to this commit. The latter is a test that succeeds prior to this comment. Both are affected by the code in this commit. Please let me know if you would like these renamed or consolidated into convert-utf8mb4. They were helpful to do TDD.

In case this PR introduced Go code changes:

  • [x] contributed code is using same conventions as original code
  • [x] script/cibuild returns with no formatting errors, build errors or unit test errors.

jbielick avatar Jul 09 '21 21:07 jbielick

@jbielick I'm curious to see how the tests run. Would you care to push same branch (and create similar PR) in https://github.com/openark/gh-ost ? (If not, I can to the same on your behalf; but you're the author so it's best if you do it)

Notwithstanding #290, which I completely missed as I was phasing out, I'm surprised this issue only shows up now.

shlomi-noach avatar Jul 11 '21 11:07 shlomi-noach

@shlomi-noach me, too. I got them running locally with mysql 5.7.25 and a couple changes to the Dockerfile and they all passed. Looking forward to a CI run.

Rebased and opened at:

https://github.com/openark/gh-ost/pull/27

jbielick avatar Jul 12 '21 13:07 jbielick

Thank you!

shlomi-noach avatar Jul 12 '21 13:07 shlomi-noach

@shlomi-noach cherry-picked commits here.

jbielick avatar Jul 14 '21 13:07 jbielick

@timvaillancourt :wave: this looks legit to me. However, it works on data transfer (conversion of character sets) and I'd like to see how this works under production tests. Would you be able to deploy this change?

shlomi-noach avatar Jul 14 '21 18:07 shlomi-noach

@timvaillancourt πŸ‘‹ this looks legit to me. However, it works on data transfer (conversion of character sets) and I'd like to see how this works under production tests. Would you be able to deploy this change?

@shlomi-noach πŸ‘‹ absolutely! I can get this on some testing hosts next week πŸ‘

timvaillancourt avatar Jul 17 '21 17:07 timvaillancourt

Anything go awry?

jbielick avatar Jul 26 '21 15:07 jbielick

@timvaillancourt @shlomi-noach Hey y'all. :wave:

I'm wondering if there is anything I can do to help here. Does this still seem like an appropriate change to merge?

To add some more context, we're in need of this change to successfully convert large tables from latin1 to utf8mb4. Backfilling goes just fine, but updates that are applied encounter errors (as they they do SQL writes/update differently than the backfill) and the whole things dies. Any issues or edge cases I can look into? This is understandably high-risk.

If I were to build a binary from this branch to test in our prod DB, should I rebase and do so from the openark repo?

jbielick avatar Sep 09 '21 13:09 jbielick

I'm wondering if there is anything I can do to help here. Does this still seem like an appropriate change to merge?

@jbielick / @shlomi-noach: πŸ‘‹ the production tests of this PR ran into some unrelated test failures that are still under investigation. I think tests of this PR should be ran again to make sure this didn't break anything. cc'ing @dm-2 / @rashiq for help on tests

And just calling out that while the automated tests will prove gh-ost still works in general, it won't trigger the charset logic from this PR as the production test runs ENGINE=InnoDB only. localtests/ might be our best bet to test this logic. If there is any more test scenarios that can be added, that would make this safer @jbielick (no specific ideas right now)

Lastly, I deleted the ignore_versions file for the 5.5 version because we dropped support/tests for 5.5 so no need to ignore it

timvaillancourt avatar Jun 29 '22 21:06 timvaillancourt

Thanks for the update.

it won't trigger the charset logic from this PR as the production test runs ENGINE=InnoDB only

May I ask what you mean in this statement? I was expecting this to affect InnoDB tables in test and production in the intended way, but perhaps I'm missing or misunderstanding something.

jbielick avatar Jun 30 '22 16:06 jbielick

May I ask what you mean in this statement? I was expecting this to affect InnoDB tables in test and production in the intended way, but perhaps I'm missing or misunderstanding something.

@jbielick what I meant there is the tests on Production replicas at GitHub use --alter="ENGINE=InnoDB" as an ALTER statement, meaning this logic won't be triggered in that testing

That's why I suggest test scenarios are added to localtests/ because these tests CAN run ALTERs that trigger the logic in this PR (see create.sql and extra_args files for example)

timvaillancourt avatar Jul 20 '22 20:07 timvaillancourt

May I ask what you mean in this statement? I was expecting this to affect InnoDB tables in test and production in the intended way, but perhaps I'm missing or misunderstanding something.

@jbielick what I meant there is the tests on Production replicas at GitHub use --alter="ENGINE=InnoDB" as an ALTER statement, meaning this logic won't be triggered in that testing

That's why I suggest test scenarios are added to localtests/ because these tests CAN run ALTERs that trigger the logic in this PR (see create.sql and extra_args files for example)

@timvaillancourt makes sense. I've got some tests added here in localtests/ that reproduce the failure and assert success. Do those tests look sufficient or am I missing something that I can add here?

jbielick avatar Jul 21 '22 14:07 jbielick

@jbielick in light of https://github.com/github/gh-ost/pull/1158 being merged I just wanted to check if this is still needed? cc @wangzihuacool who can add some insight also

Thanks all πŸ™‡

timvaillancourt avatar Sep 07 '22 02:09 timvaillancourt

@jbielick in light of #1158 being merged I just wanted to check if this is still needed? cc @wangzihuacool who can add some insight also

Thanks all πŸ™‡

Hi @timvaillancourt

No, unfortunately the test I added in this PR fails on master.

Copy: 3/3 100.0%; Applied: 0; Backlog: 20/1000; Time: 5s(total), 5s(copy); streamer: mysql-bin.000003:3226925; Lag: 0.00s, HeartbeatLag: 0.03s, State: throttled, throttle-query; ETA: due
2022-09-21 17:47:43 ERROR Error 1366: Incorrect string value: '\x80' for column 't' at row 1; query=
			replace /* gh-ost `test`.`_gh_ost_test_gho` */ into
				`test`.`_gh_ost_test_gho`
					(`id`, `t`)
				values
					(?, ?)
		; args=[7 [128]]
goroutine 60 [running]:
runtime/debug.Stack()
	/usr/local/go/src/runtime/debug/stack.go:24 +0x65
runtime/debug.PrintStack()
	/usr/local/go/src/runtime/debug/stack.go:16 +0x19
github.com/openark/golib/log.logErrorEntry(0xc000481100, {0x7f5540, 0xc000481110})
	/go/src/github.com/github/gh-ost/vendor/github.com/openark/golib/log/log.go:188 +0x98
github.com/openark/golib/log.Errore(...)
	/go/src/github.com/github/gh-ost/vendor/github.com/openark/golib/log/log.go:234
github.com/github/gh-ost/go/base.(*simpleLogger).Errore(0xc0002402c0, {0x7f5540, 0xc000481110})
	/go/src/github.com/github/gh-ost/go/base/default_logger.go:51 +0x28
github.com/github/gh-ost/go/logic.(*Applier).ApplyDMLEventQueries(0xc0002402c0, {0xc0004a4a80, 0xa, 0xc000488e10})
	/go/src/github.com/github/gh-ost/go/logic/applier.go:1158 +0x12d
github.com/github/gh-ost/go/logic.(*Migrator).onApplyEventStruct.func2()
	/go/src/github.com/github/gh-ost/go/logic/migrator.go:1269 +0x2d
github.com/github/gh-ost/go/logic.(*Migrator).retryOperation(0xc000218000, 0xc0000d9f18, {0x0, 0x0, 0xc000216000})
	/go/src/github.com/github/gh-ost/go/logic/migrator.go:149 +0x79
github.com/github/gh-ost/go/logic.(*Migrator).onApplyEventStruct(0xc000218000, 0x0)
	/go/src/github.com/github/gh-ost/go/logic/migrator.go:1271 +0x25c
github.com/github/gh-ost/go/logic.(*Migrator).executeWriteFuncs(0xc000218000)
	/go/src/github.com/github/gh-ost/go/logic/migrator.go:1305 +0xb8
created by github.com/github/gh-ost/go/logic.(*Migrator).Migrate
	/go/src/github.com/github/gh-ost/go/logic/migrator.go:411 +0x625

jbielick avatar Sep 21 '22 17:09 jbielick