Skip to content

# Un-split brain MySQL via gh-mysql-rewind

MySQL Open Source Replication March 5, 2019March 5, 2019

We are pleased to release gh-mysql-rewind, a tool that allows us to move MySQL back in time, automatically identify and rewind split brain changes, restoring a split brain server into a healthy replication chain.

I recently had the pleasure of presenting `gh-mysql-rewind` at FOSDEM. Video and slides are available. Consider following along with the video.

## Motivation

Consider a split brain scenario: a “standard” MySQL replication topology suffered network isolation, and one of the replicas was promoted as new master. Meanwhile, the old master was still receiving writes from co-located apps.

Once the network isolation is over, we have a new master and an old master, and a split-brain situation: some writes only took place on one master; others only took place on the other. What if we wanted to converge the two? What paths do we have to, say, restore the old, demoted master, as a replica of the newly promoted master?

The old master is unlikely to agree to replicate from the new master. Changes have been made. `AUTO_INCREMENT` values have been taken. `UNIQUE` constraints will fail.

A few months ago, we at GitHub had exactly this scenario. An entire data center went network isolated. Automation failed over to a 2nd DC. Masters in the isolated DC meanwhile kept receiving writes. At the end of the failover we ended up with a split brain scenario – which we expected. However, an additional, unexpected constraint forced us to fail back to the original DC.

We had to make a choice: we’ve already operated for a long time in the 2nd DC and took many writes, that we were unwilling to lose. We were OK to lose (after auditing) the few seconds of writes on the isolated DC. But, how do we converge the data?

Backups are the trivial way out, but they incur long recovery time. Shipping backup data over the network for dozens of servers takes time. Restore time, catching up with changes since backup took place, warming up the servers so that they can handle production traffic, all take time.

Could we have reduces the time for recovery?

There are multiple ways to do that: local backups, local delayed replicas, snapshots… We have embarked on several. In this post I wish to outline gh-mysql-rewind, which programmatically identifies the rogue (aka “bad”) transactions on the network isolated master, rewinds/reverts them, applies some bookkeeping and restores the demoted master as a healthy replica under the newly promoted master, thereby prepared to be promoted if needed.

## General overview

`gh-mysql-rewind` is a `shell` script. It utilizes multiple technologies, some of which do not speak to each other, to be able to do the magic. It assumes and utilizes the following:

- MySQL GTID replication
- Row based replication (`binlog_format=ROW`)
- `binlog_row_image=FULL`
- Use of MariaDB Flashback
- Some limitations apply

Some breakdown follows.

## GTID

MySQL GTIDs keep track of all transactions executed on a given server. GTIDs indicate which server (UUID) originated a write, and ranges of transaction sequences. In a clean state, only one writer will generate GTIDs, and on all the replicas we would see the same GTID set, originated with the writer’s UUID.

In a split brain scenario, we would see divergence. It is possible to use GTID_SUBTRACT(old_master-GTIDs, new-master-GTIDs) to identify the exact set of transactions executed on the old, demoted master, right after the failover. This is the essence of the split brain.

For example, assume that just before the network partition, GTID on the master was `00020192-1111-1111-1111-111111111111:1-5000`. Assume after the network partition the new master has UUID of `00020193-2222-2222-2222-222222222222`. It began to take writes, and after some time its GTID set showed `00020192-1111-1111-1111-111111111111:1-5000,00020193-2222-2222-2222-222222222222:1-200`.

On the demoted master, other writes took place, leading to the GTID set `00020192-1111-1111-1111-111111111111:1-5042`.

We will run…

    SELECT GTID_SUBTRACT(
      '00020192-1111-1111-1111-111111111111:1-5042',
      '00020192-1111-1111-1111-111111111111:1-5000,00020193-2222-2222-2222-222222222222:1-200'
    );

    > '00020192-1111-1111-1111-111111111111:5001-5042'

…to identify the exact set of “bad transactions” on the demoted master.

## Row Based Replication

With row based replication, and with `FULL` image format, each DML (`INSERT`, `UPDATE`, `DELETE`) writes to the binary log the complete row data before and after the operation. This means the binary log has enough information for us to revert the operation.

## Flashback

Developed by Alibaba, `flashback` has been incorporated in MariaDB. MariaDB’s `mysqlbinlog` utility supports a `--flashback` flag, which interprets the binary log in a special way. Instead of printing out the events in the binary log in order, it prints the inverted operations in reverse order.

To illustrate, let’s assume this pseudo-code sequence of events in the binary log:

    insert(1, 'a')
    insert(2, 'b')
    insert(3, 'c')
    update(2, 'b')->(2, 'second')
    update(3, 'c')->(3, 'third')
    insert(4, 'd')
    delete(1, 'a')

A `--flashback` of this binary log would produce:

    insert(1, 'a')
    delete(4, 'd')
    update(3, 'third')->(3, 'c')
    update(2, 'second')->(2, 'b')
    delete(3, 'c')
    delete(2, 'b')
    delete(1, 'a')

Alas, MariaDB and `flashback` do not speak MySQL GTID language. GTIDs are one of the major points where MySQL and MariaDB have diverged beyond compatibility.

The output of MariaDB’s `mysqlbinlog --flashback` has neither any mention of GTIDs, nor does the tool take notice of GTIDs in the binary logs in the first place.

## gh-mysql-rewind

This is where we step in. GTIDs provide the information about what went wrong. `flashback` has the mechanism to generate the reverse sequence of statements. `gh-mysql-rewind`:

- uses GTIDs to detect what went wrong
- correlates those GTID entries with binary log files: identifies which binary logs actually contain those GTID events
- invokes MariaDB’s `mysqlbinlog --flashback` to generate the reverse of those binary logs
- injects (dummy) GTID information into the output
- computes ETA

This last part is worth elaborating. We have created a time machine. We have the mechanics to make it work. But as any Sci-Fi fan knows, one of the most important parts of time travel is knowing ahead where (when) you are going to land. Are you back in the Renaissance? Or are you suddenly to appear on board the French Revolution? Better dress accordingly.

In our scenario it is not enough to move MySQL back in time to some consistent state. We want to know at what time we landed, so that we can instruct the rewinded server to join the replication chain as a healthy replica. In MySQL terms, we need to make MySQL “forget” everything that ever happened after the split brain: not only in terms of data (which we already did), but in terms of GTID history.

`gh-mysql-rewind` will do the math to project, ahead of time, at what “time” (i.e. GTID set) our time machine arrived. It will issue a `RESET MASTER; SET GLOBAL gtid_purged=’gtid-of-the-landing-time'” to make our re-winded MySQL consistent not only with some past dataset, but also with its own perception of the point in time where that dataset existed.

## Limitations

Some limitations are due to MariaDB’s incompatibility with MySQL, some are due to MySQL DDL nature, some due to the fact `gh-mysql-rewind` is a `shell` script.

- Cannot rewind DDL. DDLs are silently ignored, and will impose a problem when trying to re-apply them.
- `JSON`, `POINT` data types are not supported.
- The logic rewinds the MySQL server farther into the past than strictly required. This simplifies the code considerably, but imposed superfluous time to rewind+reapply, i.e. time to recover.
- Currently, this only works one server at a time. If a group of 10 servers were network isolated together, the operation would need to run on each of these 10 servers.
- Runs locally on each server. Requires both MySQL’s `mysqlbinlog` as well as MariaDB’s `mysqlbinlog`.

## Testing

There’s lot of moving parts to this mechanism. A mixture of technologies that don’t normally speak to each other, injection of data, prediction of ETA… How reliable is all this?

We run continuous `gh-mysql-rewind` testing in production to consistently prove that it works as expected. Our testing uses a non-production, dedicated, functional replica. It contaminates the data on the replica. It lets `gh-mysql-rewind` automatically move it back in time, it joins the replica back into the healthy chain.

That’s not enough. We actually create a scenario where we can predict, ahead of testing, what the time-of-arrival will be. We checksum the data on that replica at that time. After contaminating and effectively breaking replication, we expect `gh-mysql-rewind` to revert the changes back to our predicted point in time. We checksum the data again. We expect 100% match.

See the video or slides for more detail on our testing setup.

## Status

At this time the tool in one of several solutions we hope to never need to employ. It is stable and tested. We are looking forward to a promising MySQL development that will provide GTID-revert capabilities using standard commands, such as `SELECT undo_transaction('00020192-1111-1111-1111-111111111111:5042')`.

We have released `gh-mysql-rewind` as open source, under the MIT license. The public release is a stripped down version of our own script, which has some GitHub-specific integration. We have general ideas in incorporating this functionality into higher level tools.

`gh-mysql-rewind` is developed by the database-infrastructure team at GitHub.

### Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe
Notify of
wpDiscuz