I recently worked on a case where one node of a Galera cluster had its schema desynchronized with the other nodes. And that was although Total Order Isolation method was in effect to perform the schema changes. Let’s see what happened.
For those of you who are not familiar with how Galera can perform schema changes, here is a short recap:
wsrep_OSU_method setting. Both have benefits and drawbacks, it is not the main topic of this post.
You can look at the official document here.
If you read carefully the section on TOI, you will see that “[…] TOI transactions will never fail certification and are guaranteed to be executed.” But also that “The system replicates the TOI query before execution and there is no way to know whether it succeeds or fails. Thus, error checking on TOI queries is switched off.”
Confusing? Not really. It simply means that with TOI, a DDL statement will always pass certification. But if for some reason, the DDL statement fails on one of the nodes, it will not be rolled back on the other nodes. This opens the door for schema inconsistencies between nodes.
Let’s create a table on a 3-node Percona XtraDB Cluster 5.6 cluster and insert a few rows:
|
1 |
pxc1> create table t (id int not null auto_increment primary key, c varchar(10));<br>pxc1> insert into t (c) values ('aaaa'),('bbbb');<br> |
Then on node 3, let’s introduce a schema change on t that can make other schema changes fail:
|
1 |
pxc3> set global wsrep_OSU_method=RSU;<br><br>pxc3> alter table t add d int;<br><br>pxc3> set global wsrep_OSU_method=TOI;<br> |
As the schema change was done on node 3 with RSU, it is not replicated to the other nodes.
Now let’s try another schema change on node 1:
|
1 |
pxc1> alter table t add d varchar(10);<br>Query OK, 0 rows affected (0,14 sec)<br>Records: 0 Duplicates: 0 Warnings: 0<br> |
Apparently everything went well and indeed on node 1 and 2, we have the correct schema:
|
1 |
pxc2>show create table tG<br>*************************** 1. row ***************************<br> Table: t<br>Create Table: CREATE TABLE t (<br> id int(11) NOT NULL AUTO_INCREMENT,<br> c varchar(10) DEFAULT NULL,<br> d varchar(10) DEFAULT NULL,<br> PRIMARY KEY (id)<br>) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1<br> |
But on node 3, the statement failed so the schema has not been changed:
|
1 |
pxc3> show create table tG<br>*************************** 1. row ***************************<br> Table: t<br>Create Table: CREATE TABLE t (<br> id int(11) NOT NULL AUTO_INCREMENT,<br> c varchar(10) DEFAULT NULL,<br> d int(11) DEFAULT NULL,<br> PRIMARY KEY (id)<br>) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1<br> |
The error is visible in the error log of node 3:
|
1 |
2014-07-18 10:37:14 9649 [ERROR] Slave SQL: Error 'Duplicate column name 'd'' on query. Default database: 'repl_test'. Query: 'alter table t add d varchar(10)', Error_code: 1060<br>2014-07-18 10:37:14 9649 [Warning] WSREP: RBR event 1 Query apply warning: 1, 200<br>2014-07-18 10:37:14 9649 [Warning] WSREP: Ignoring error for TO isolated action: source: 577ffd51-0e52-11e4-a30e-4bde3a7ad3f2 version: 3 local: 0 state: APPLYING flags: 65 conn_id: 3 trx_id: -1 seqnos (l: 17, g: 200, s: 199, d: 199, ts: 7722177758966)<br> |
But of course it is easy to miss. And then a simple INSERT can trigger a shutdown on node3:
|
1 |
pxc2> insert into t (c,d) values ('cccc','dddd');<br>Query OK, 1 row affected (0,00 sec)<br> |
will trigger this on node 3:
|
1 |
2014-07-18 10:42:27 9649 [ERROR] Slave SQL: Column 2 of table 'repl_test.t' cannot be converted from type 'varchar(10)' to type 'int(11)', Error_code: 1677<br>2014-07-18 10:42:27 9649 [Warning] WSREP: RBR event 3 Write_rows apply warning: 3, 201<br>2014-07-18 10:42:27 9649 [Warning] WSREP: Failed to apply app buffer: seqno: 201, status: 1<br> at galera/src/trx_handle.cpp:apply():340<br>[...]<br>2014-07-18 10:42:27 9649 [Note] WSREP: Received NON-PRIMARY.<br>2014-07-18 10:42:27 9649 [Note] WSREP: Shifting SYNCED -> OPEN (TO: 201)<br>2014-07-18 10:42:27 9649 [Note] WSREP: Received self-leave message.<br>2014-07-18 10:42:27 9649 [Note] WSREP: Flow-control interval: [0, 0]<br>2014-07-18 10:42:27 9649 [Note] WSREP: Received SELF-LEAVE. Closing connection.<br>2014-07-18 10:42:27 9649 [Note] WSREP: Shifting OPEN -> CLOSED (TO: 201)<br>2014-07-18 10:42:27 9649 [Note] WSREP: RECV thread exiting 0: Success<br>2014-07-18 10:42:27 9649 [Note] WSREP: recv_thread() joined.<br>2014-07-18 10:42:27 9649 [Note] WSREP: Closing replication queue.<br>2014-07-18 10:42:27 9649 [Note] WSREP: Closing slave action queue.<br>2014-07-18 10:42:27 9649 [Note] WSREP: bin/mysqld: Terminated.<br> |
As on regular MySQL, schema changes are challenging with Galera. Some subtleties can create a lot of troubles if you are not aware of them. So before running DDL statement, make sure you fully understand how TOI and RSU methods work.