ENGINEERING NOTE / 16 MARIADBPRODUCTION FORENSICS

The Backup's GTID Was Not the Master's Position

An automated rebuild of a MariaDB DR replica restored the backup without a fault and then failed at the last step, with the master refusing the start position. The position came from the backup itself. It was the wrong number to ask for, and the check meant to catch it was asking the wrong question too.

9 MIN READ

REPLICATIONGTIDDISASTER-RECOVERYMARIABACKUPAUTOMATION

TABLE OF CONTENTS
  1. A copy step measured in hours
  2. Restored fine, then error 1236
  3. Where the backup’s GTID comes from
  4. Two mistakes that looked like one fix
  5. Starting from the binlog coordinates
  6. The check that passed a position the master refused
  7. Status, and what has been verified
  8. What carries over

The operations portal I built can rebuild the DR side of a MariaDB geo-pair from a backup of the live master. It is the last resort in the replication repair workflow: the portal queues a job, the action executor runs the rebuild tooling, and the tooling works through a fixed list of steps. It stops the DR node, gets the backup onto it, extracts and prepares it, bootstraps the DR Galera cluster from it, points the link node at the live master with CHANGE MASTER, and then lets the other DR members join.

A production rebuild of the DR link node went wrong twice. The copy step was going to take most of a day. When that was fixed, the restore went through cleanly and the very last replication step failed. The second failure is the more interesting one, because the number that broke it came from the backup itself.

The replication repair tab: the link from the live master to the DR link node, its health and when it was last diagnosed, the last rebuild with its job, and three numbered steps — diagnose, apply a ranked fix, rebuild DR.
Replication repair in the portal. The rebuild is step three, after a diagnosis and the ranked fixes.

A copy step measured in hours§

The first version streamed the backup from the master to the DR node through the executor: two ssh hops, every byte crossing the network twice, and no output at all until the copy finished. A slow copy and a stuck one looked the same.

So I changed it to copy directly from the master to the DR node, and to log progress every minute. The progress line answered the question straight away, and not in the way I had hoped:

OBSERVED The direct copy, master to DR, first progress line
copied 0.1 GiB of 48.6 GiB ... about 1300 min left

That is about 0.6 MB/s. The network path between the two sites runs at that rate, and removing a hop could not change it. A copy like that runs for about 21 hours, and at the time the portal had no way to stop a running job, so it had to be killed by hand inside the executor’s container.

The fix was not to make the copy faster but to notice that it was not needed. The backup volume on each site is already replicated to the other site for disaster recovery, with the same directory layout. The DR node already had the backup:

VERIFIED The same backup stream, on both sites
db-live-1   backup volume    .../db-live-1/<date>/<time>/full_stream.gz   52208675375 bytes
db-dr-1     restore volume   .../db-live-1/<date>/<time>/full_stream.gz   52208675375 bytes

The copy step now looks for the master’s backup on the DR node’s replicated volume first. It checks that the stream is exactly the size of the master’s, and links it into the staging directory, so nothing is copied and nothing on the replicated volume is changed. If a fresh backup is still arriving, it waits and reports progress, for up to 30 minutes by default. Only if there is no usable copy does it fall back to the network copy.

NETWORK PATH
≈0.6 MB/s
BACKUP STREAM
48.6 GiB
NETWORK COPY, ESTIMATED
≈21 h
REPLICATED COPY, TO CHANGE MASTER
≈11 min

The next rebuild reached CHANGE MASTER about eleven minutes after it started. And failed there.

Restored fine, then error 1236§

OBSERVED The rebuild's last step
Using GTID 2-20-30551870 (domain 2); discarding 0-10-41207318,5-3-9120044,7-4-4082213

Last_IO_Error: Got fatal error 1236 from master when reading data from binary
log: 'Could not find GTID state requested by slave in any binlog files. ...'

Error 1236 here means the replica asked the master for a GTID position the master cannot serve from the binary logs it still has. The backup had been taken that morning, and the master still had the binlog file it was taken at, so “the backup is too old” did not fit. The first line of the log was the clue: the tooling had been given a four-domain GTID, kept one domain and thrown the other three away.

(GTIDs, server ids, domain ids and file names in this note are illustrative. They keep the relationships of the real values, which is all that matters here.)

Where the backup’s GTID comes from§

mariabackup records a GTID with every backup. It is the value of gtid_current_pos at the moment the backup was taken, and that variable is not what its name suggests on a server that has ever been a replica.

MariaDB keeps two GTID positions. gtid_binlog_pos is the last GTID per domain in the server’s own binary log. gtid_slave_pos is the last GTID per domain that the server has applied as a replica. gtid_current_pos is a blend of the two: for each domain, it takes the binlog’s GTID if that was written under the server’s own server_id and is ahead of the replica position, and the replica position otherwise.

The live master had not always been the master. Before a failover it had replicated from the other site, and before that from an older topology. Its gtid_slave_pos still carried all of that, and so did the GTID in the backup:

VERIFIED On the master (server_id 20, Galera domain 2)
gtid_current_pos, as recorded by mariabackup
  0-10-41207318,2-20-30551870,5-3-9120044,7-4-4082213

@@gtid_slave_pos    (its own history as a replica)
  0-10-41207318,5-3-9120044,7-4-4082213

BINLOG_GTID_POS('mysql-bin.000318', 517204883)    (the backup's coordinates)
  0-20-27311905,2-20-30551870

@@gtid_binlog_pos   (now)
  0-20-27311905,2-20-30558629

Read domain by domain, the blend is exact. Domain 2 is only in the binlog, under the master’s own server id, so the binlog value is used. Domains 5 and 7 are only in the replica position. Domain 0 is in both: the binlog has 0-20-… from writes the master made itself, but the replica position’s 0-10-…, received from the server that is now the DR node, has the higher sequence number, so that one wins.

The result is a GTID that describes the history this server has seen, not the binlog it can serve. Three of its four entries are domains or server ids that the master’s binary log does not contain.

Two mistakes that looked like one fix§

The rebuild tooling already knew the backup’s GTID was messy. It treated the extra domains as historical noise, kept only the entry for the master’s live Galera domain and discarded the rest. That handled the first mistake by accident and made a second one.

The recorded GTID is the wrong source. Sending it whole would not have worked either: it names domains and server ids the master never logged.

Keeping only the live domain is wrong. The master’s binlog has domain 0 as well, under its own server id. When a replica connects with a position that has no entry for a domain, the master has to send that domain from its very first event. Domain 0’s first event was purged long ago, so the master refused: could not find GTID state requested by slave in any binlog files.

Starting from the binlog coordinates§

The backup records something better than its GTID: the master’s binlog file and position at the moment of the backup, taken under the backup locks, so they are exact. BINLOG_GTID_POS(file, pos) asks the master to turn those coordinates into a GTID position from its own binlog. That is the position the master itself compares against, and it has exactly the master’s binlog domains, here 0-20-27311905,2-20-30551870.

The start position is now that value, and the whole of it goes into gtid_slave_pos before CHANGE MASTER … MASTER_USE_GTID=current_pos. The recorded GTID is kept only as a fallback for when the master no longer has the backup’s binlog file, and then only the domains in the master’s gtid_binlog_state are kept, with a warning. That is the weaker path, and the log says so when it is used.

The same blend has to be dealt with on the replica side. The restored node’s gtid_current_pos would mix in the restored node’s own binlog history, so the step runs RESET MASTER before it sets gtid_slave_pos. That is safe only because it runs before the other DR members have joined the cluster.

The check that passed a position the master refused§

The tooling already had a check meant to catch exactly this kind of failure early. Once the backup is extracted, it reads the Gtid_list event at the start of the master’s oldest retained binlog, which records, per domain and server id, the last GTID logged before that file began, and compares the start position with it.

It passed. It only ever looked at the live domain, and for domain 2 the position was fine. Domain 0, the one that failed, was not in the position, so the check never looked at it.

The fix was to make the check apply the master’s own rule to the whole position:

RESULT The check, now: oldest retained binlog against the start position
Gtid_list of the oldest retained binlog    [0-20-27311905, 2-20-30490000]

for every entry in the list:
the position must have that domain        else: "has no domain 0"
on the same server_id, it must not be     else: the events after it
behind the list entry                           have rotated out
under another server_id                   says nothing either way

0-20-27311905,2-20-30551870   pass
2-20-30551870                 fail: has no domain 0

A position like the one that failed now stops the rebuild straight after extraction, before the prepare, the bootstrap and the secondaries, rather than at CHANGE MASTER. It cannot run any earlier: the file that holds the backup’s GTID and coordinates is inside the compressed backup stream, so it only exists once the stream has been extracted. By then the DR node’s old data has already been wiped, so the check saves time, not the old replica.

How much time depends on the copy. With the replicated backup, this run reached CHANGE MASTER in about eleven minutes, so the early check saves minutes. When the rebuild has to fall back to the network copy, the difference between failing after extraction and failing at the end is the prepare, the bootstrap and the cluster sync on top of a copy that can take most of a day. That is the case the check is for.

  1. 01 OBSERVED OBSERVED

    Restore clean; CHANGE MASTER fails with 1236 on a backup only hours old.

  2. 02 CLUE

    The log shows three of four GTID domains discarded before CHANGE MASTER.

  3. 03 SOURCE

    The recorded GTID is gtid_current_pos, which blends in the master's own replica history.

  4. 04 CAUSE

    Domain 0 is in the master's binlog but not in the position, so the master owes it from its first event.

  5. 05 FIX CONFIRMED

    Start from BINLOG_GTID_POS at the backup coordinates; check every domain against the oldest Gtid_list.

Status, and what has been verified§

The stale gtid_slave_pos is still on the live master. Clearing it would tidy the GTID future backups record, but it is a change on the live master, and the rebuild now only falls back to that value when the backup’s binlog is gone. It stays an optional follow-up.

What carries over§

  • The GTID a MariaDB backup records is gtid_current_pos. On a server that has ever been a replica, that includes its replica history. It is a record of what the server has seen, not of what it can serve.
  • Start a replica from the master’s binlog, with BINLOG_GTID_POS at the backup’s coordinates. That is a position in the master’s own terms.
  • Never drop a domain from a start position to tidy it up. A missing domain is a request for all of that domain’s history.
  • A pre-check has to ask the question the server will ask. Mine checked one domain while the master checks all of them, so it passed the position the master then refused.
  • Before speeding up a copy, check whether the data is already there. The faster copy path made no difference. The replicated volume made the copy unnecessary.