How to Restart a Galera Cluster Without Losing Writes

Updated

MariaDB Galera database cluster

Before restarting a stopped Galera cluster, read grastate.dat from every node and bootstrap the one with safe_to_bootstrap: 1. Bootstrapping any other node makes its data the cluster’s data, and every node that had more is overwritten by a state transfer from it.

$ cat /var/lib/mysql/grastate.dat        # on each node, while it is stopped
# GALERA saved state
version: 2.1
uuid:    e3f4b8c1-1a2b-11f0-9c3d-0242ac120002
seqno:   184231
safe_to_bootstrap: 1

seqno is how far that node got. safe_to_bootstrap is 1 only on the node that left the cluster last, which is the only node guaranteed to have everything. Which node to run galera_new_cluster on is the whole decision.

A script that makes the decision

Copy each stopped node’s grastate.dat to one machine, then run this. It refuses rather than guesses.

#!/usr/bin/env bash
# galera-pick-bootstrap.sh -- read one grastate.dat per node and say which node
# may be bootstrapped. Usage: galera-pick-bootstrap.sh node1.dat node2.dat ...
# Copy each node's /var/lib/mysql/grastate.dat to this host first (while the
# node is STOPPED -- the file is only accurate after a clean shutdown).
set -u
[ $# -ge 1 ] || { echo "usage: $0 <grastate.dat> [...]" >&2; exit 2; }

best=""; best_seq=-2; ties=0; unclean=0
printf '%-24s %-38s %12s %s\n' FILE UUID SEQNO SAFE_TO_BOOTSTRAP
for f in "$@"; do
  [ -r "$f" ] || { printf '%-24s UNREADABLE\n' "$f"; exit 3; }
  uuid=$(awk '/^uuid:/{print $2}' "$f")
  seq=$(awk '/^seqno:/{print $2}' "$f")
  safe=$(awk '/^safe_to_bootstrap:/{print $2}' "$f")
  : "${uuid:=?}"; : "${seq:=?}"; : "${safe:=0}"
  printf '%-24s %-38s %12s %s\n' "$(basename "$f")" "$uuid" "$seq" "$safe"
  case "$seq" in -1) unclean=$((unclean+1)); continue;; ''|*[!0-9-]*) continue;; esac
  if [ "$safe" = "1" ]; then
    if [ "$seq" -gt "$best_seq" ]; then best="$f"; best_seq="$seq"; ties=0
    elif [ "$seq" -eq "$best_seq" ]; then ties=$((ties+1)); fi
  fi
done

echo
if [ -n "$best" ] && [ "$ties" -eq 0 ]; then
  echo "BOOTSTRAP: $(basename "$best")  (seqno $best_seq, safe_to_bootstrap 1)"
  echo "  run 'galera_new_cluster' on that node, then 'systemctl start mariadb' on the rest"
  exit 0
fi
if [ "$ties" -gt 0 ]; then
  echo "REFUSING: $((ties+1)) nodes claim safe_to_bootstrap 1 at seqno $best_seq."
  echo "  They were not shut down as one cluster. Investigate before bootstrapping anything."
  exit 1
fi
echo "REFUSING: no node has safe_to_bootstrap 1."
[ "$unclean" -gt 0 ] && echo "  $unclean node(s) show seqno -1: they crashed. Recover the real position first:"
echo "  mariadbd --wsrep-recover      (reads the position out of the InnoDB log)"
echo "  Bootstrap the node with the HIGHEST recovered seqno. Every other node is then"
echo "  overwritten by a state transfer from it -- pick wrong and you delete the newer data."
exit 1

Against three nodes shut down cleanly, one after another:

FILE                     UUID                                          SEQNO SAFE_TO_BOOTSTRAP
n1.dat                   e3f4b8c1-1a2b-11f0-9c3d-0242ac120002         184219 0
n2.dat                   e3f4b8c1-1a2b-11f0-9c3d-0242ac120002         184219 0
n3.dat                   e3f4b8c1-1a2b-11f0-9c3d-0242ac120002         184231 1

BOOTSTRAP: n3.dat  (seqno 184231, safe_to_bootstrap 1)
  run 'galera_new_cluster' on that node, then 'systemctl start mariadb' on the rest
exitA=0

Note that n3 is both the flagged node and the furthest ahead — 184231 against 184219. Had you picked n1, those twelve transactions would be gone, with no error anywhere.

After a power cut, every node says -1

FILE                     UUID                                          SEQNO SAFE_TO_BOOTSTRAP
c1.dat                   e3f4b8c1-1a2b-11f0-9c3d-0242ac120002             -1 0
c2.dat                   e3f4b8c1-1a2b-11f0-9c3d-0242ac120002             -1 0
c3.dat                   e3f4b8c1-1a2b-11f0-9c3d-0242ac120002             -1 0

REFUSING: no node has safe_to_bootstrap 1.
  3 node(s) show seqno -1: they crashed. Recover the real position first:
  mariadbd --wsrep-recover      (reads the position out of the InnoDB log)
  Bootstrap the node with the HIGHEST recovered seqno. Every other node is then
  overwritten by a state transfer from it -- pick wrong and you delete the newer data.
exitB=1

seqno: -1 means the file was never updated on the way down, so the position has to come out of the InnoDB log instead. On Debian and Ubuntu that recovery already happens for you: mariadb.service runs galera_recovery as an ExecStartPre and feeds the result back in as --wsrep-start-position. To make the bootstrap decision you have to run it yourself first, on each node, and compare the numbers it prints.

Two nodes both claiming to be safe

If more than one node says safe_to_bootstrap: 1 at the same seqno, they did not leave as one cluster, and picking either one discards whatever the other took:

FILE                     UUID                                          SEQNO SAFE_TO_BOOTSTRAP
s1.dat                   e3f4b8c1-1a2b-11f0-9c3d-0242ac120002         184219 1
s3.dat                   e3f4b8c1-1a2b-11f0-9c3d-0242ac120002         184219 1

REFUSING: 2 nodes claim safe_to_bootstrap 1 at seqno 184219.
  They were not shut down as one cluster. Investigate before bootstrapping anything.
exitC=1

This is where data actually disappears. Galera does guard the common case — its source refuses with “It may not be safe to bootstrap the cluster from this node. It was not the last one to leave the cluster and may not contain all the updates.” The advice that follows in that same message is to “edit the grastate.dat file manually and set safe_to_bootstrap to 1”, and once you do that, on the wrong node, nothing warns you again. The cluster comes up healthy and short.

What galera_new_cluster is

It is a shell script, and this is all of it that does anything. From mariadb-server 11.8.8:

echo _WSREP_NEW_CLUSTER='--wsrep-new-cluster' > "/run/mysqld/wsrep-new-cluster" && \
    systemctl restart "${1:-mariadb.service}"
rm -f "/run/mysqld/wsrep-new-cluster"

The unit picks that file up with EnvironmentFile=-/run/mysqld/wsrep-new-cluster and launches ExecStart=/usr/sbin/mariadbd $MYSQLD_OPTS $_WSREP_NEW_CLUSTER $_WSREP_START_POSITION. So galera_new_cluster and mysqld --wsrep-new-cluster are the same thing; the wrapper exists so the flag applies to exactly one systemd start and is then removed, which is what stops the node bootstrapping a second cluster the next time it reboots. Do not put --wsrep-new-cluster in a config file. (MariaDB also ships a drop-in that makes mariadb@bootstrap print “Please use galera_new_cluster” and exit, for people arriving from Percona.)

The order

1. Stop every node:            sudo systemctl stop mariadb
2. Read every grastate.dat and pick the node (script above)
3. On that node only:          sudo galera_new_cluster
4. Wait for it to be Synced:   SHOW STATUS LIKE 'wsrep_local_state_comment';
5. One at a time, elsewhere:   sudo systemctl start mariadb
6. Confirm:                    SHOW STATUS LIKE 'wsrep_cluster_size';

Step 4 matters: starting the others before the first is Synced risks a second node deciding to bootstrap on its own. If a joining node needs a full state transfer it will be unavailable while it copies, which is why you add them one at a time rather than all at once.

Not run on a real Galera cluster

No cluster was restarted. Galera runs on Linux only and nothing was tested on Linux, so no galera_new_cluster, systemctl or --wsrep-recover command here was executed. The decision script was executed, against grastate.dat fixtures in all three shapes; the output above is unedited. The file format, the bootstrap refusal and its message are read from the Galera source, and the systemd wiring from the MariaDB 11.8.8 package. Whether the decision script above still gives those three answers, and whether galera_new_cluster, mariadb.service and Galera’s bootstrap refusal still read as quoted, can be re-checked with restart-galera-cluster.sh, which installs nothing and starts no MariaDB process. Setting up a cluster in the first place is covered in setting up MariaDB Galera on Ubuntu.

Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.