Site administration
Run a failover drill
You want to know that the automatic failover works before the primary git server really stops. A drill stops the primary on purpose, in a planned window, and measures how long pushes and reads stop. It is a real failover of every repository on the git servers, so get the go of the team first.
4 min read
You need:
- The setup of Set up the git servers: the shared secret in
~/gitd-secret, and your SSH key. GITD_IP_US,GITD_IP_EUandGITD_IP_ASIAset to the addresses of the servers, as in step 2 of the setup.- A test repository that you can push to. Every repository is on the git servers, so a new one is fine. The steps below call it
<owner>/<drill>. - To be signed in to the analytics server. See Sign in to the server.
The steps below use us as the primary and eu as the replica that takes over. Use your regions in their place.
1. Pick a quiet time and write down the state
Pick an hour with few pushes. Tell the team when pushes stop.
Find the current primary. It prints the region:
sh apps/gitd/deploy/install.sh primaryRun the smoke test. It must end with
smoke test passed:sh apps/gitd/deploy/install.sh smoke
2. Start the loops that measure
Clone the test repository into a new folder, and start a push loop there. It pushes an empty commit every 5 seconds, and writes each result with its time:
git clone https://source.tanstack.com/@<owner>/<drill>.git drill-push cd drill-push while true; do git commit -q --allow-empty -m "drill $(date -u +%T)" if git push -q origin HEAD:refs/heads/failover-drill 2>/dev/null; then r=ok; else r=FAIL; fi echo "$(date -u +%T) push $r" | tee -a ../push.log sleep 5 doneIn a second terminal, start a read loop. It reads the refs every 2 seconds:
while true; do if git ls-remote https://source.tanstack.com/@<owner>/<drill>.git refs/heads/main >/dev/null 2>&1; then r=ok; else r=FAIL; fi echo "$(date -u +%T) read $r" | tee -a read.log sleep 2 doneLet both loops run for 1 minute. Each line must say
ok.
3. Stop the primary
Stop gitd and the tunnel on the primary, as a server that stops does. Write down the time:
ssh -i ~/.ssh/source_git_ed25519 root@"$GITD_IP_US" systemctl stop gitd.service cloudflared.service; date -u +%T
4. Watch the failover
- Watch the push loop. Pushes fail until the Worker fails over. That takes 1 to 2 minutes.
- Watch the read loop. Reads go on for the first minute, as stale reads of a replica. If the failover takes longer, they stop until it ends.
- When the pushes work again, write down the time of the first
ok. - Make sure that the alert
git-failovercame, with"from":"us"and"to":"eu".
5. Check the new primary
Ask each server for its role.
euanswers with"role":"primary", andasiaanswers with"role":"replica"and"primary":"eu":for ip in "$GITD_IP_EU" "$GITD_IP_ASIA"; do ssh -i ~/.ssh/source_git_ed25519 root@"$ip" curl -fsS 127.0.0.1:8420/healthz; echo doneOn the analytics server, read the times of the failover:
clickhouse-client --password "$(cat /root/.clickhouse-admin)" --query "SELECT timestamp, properties FROM analytics.events WHERE event = 'gitd.failover' ORDER BY timestamp DESC LIMIT 1"Count the stale reads of the drill:
clickhouse-client --password "$(cat /root/.clickhouse-admin)" --query "SELECT JSONExtractBool(properties, 'stale') AS stale, count() FROM analytics.events WHERE event = 'gitd.call' AND timestamp >= now() - INTERVAL 30 MINUTE GROUP BY stale"
6. Bring the old primary back
Start gitd and the tunnel on
us, with its old config:ssh -i ~/.ssh/source_git_ed25519 root@"$GITD_IP_US" systemctl start gitd.service cloudflared.serviceWithin 1 minute,
usanswers with"role":"replica"and"primary":"eu":ssh -i ~/.ssh/source_git_ed25519 root@"$GITD_IP_US" curl -fsS 127.0.0.1:8420/healthzRead the log of gitd on
us. It has the linerole: term <term>, primary eu (this server is a replica):ssh -i ~/.ssh/source_git_ed25519 root@"$GITD_IP_US" journalctl -u gitd -n 50If the alert
git-copy-keptcomes, a push of the loop reached onlyusbefore the stop. Do Save the pushes that only one server has for the test repository.
7. Move the primary back
Do step 4 of Fail over when the primary stops. Then stop both loops with Ctrl+C, and run the smoke test again.
Pass criteria
Read push.log and read.log. The drill passes when all of these are true:
- Pushes fail for at most 2 minutes, from the stop of step 3 to the first
okpush. - Reads fail for at most 30 seconds in a row. A replica answers stale reads for 60 seconds after the stop, and the failover ends at most about 85 seconds after it.
- Each push that said
okmore than 10 seconds before the stop is on the new primary:git log origin/failover-drillafter agit fetchlists it. - The old primary is a replica within 1 minute after its gitd starts.
- The smoke test passes at the end.
Done
You measured the failover on the production servers: how long pushes stopped, how long reads stopped, and that the old primary came back as a replica by itself.