TL;DR: Distributed bugs need bad luck, and bad luck is hard to repeat. A deterministic simulator runs your whole cluster in one thread with fake time, network and disk, and tries thousands of seeds to make the bad luck happen. The same seed gives the same run, so when it finds a bug you can replay it as many times as you need.

So I think we all know what distributed systems are. It is hard to test them like normal systems. A unit test only tests one node, not the timing between them. Finding bugs in these systems needs bad luck, like a crash at one exact moment, two messages arriving in the wrong order or a network split at the wrong time.

There are ways to test these kinds of systems:

  1. Start 5 nodes and play with them by hand or with a script to find bugs. But can you repeat the run? Not easily.
  2. Jepsen-style testing.
  3. Deterministic simulator.

What is Jepsen

Jepsen tests distributed systems on real VMs with the real binary, and checks whether they keep the promises they advertise. Nothing is fake. Faults are real too, like network splits made with iptables.

But is it enough?

  1. Time is real, so tests are slow. Imagine testing a raft cluster with 5 nodes and 2 clients. You have to test lots of scenarios, and each one runs on a real clock. If a scenario needs one hour of cluster time, it takes one hour.
  2. It is hard to reproduce. Let’s say you find a bug: a node crashes right after a leader election and you get two leaders. How are you gonna reproduce it later so you know your fix was correct? Jepsen records a history (what each client sent and what came back), so you know what went wrong. But a rerun doesn’t get the same timing, so you may never see the bug again.

But Jepsen runs the real binary on a real OS, so it finds things a simulator can’t (fsync behavior, kernel bugs, config mistakes). The two complement each other.

Jepsen was made by Kyle Kingsbury.

What is a deterministic simulator

It simulates a distributed system with fake time, network and disk. It runs in one process, one thread, no sockets. You connect your code to it and give it a seed, then a plan, and run it. If it finds a failure, it shrinks it and gives you the seed to replay it.

seed → plan → run → failure → shrink → replay

I first read about this in FoundationDB’s simulation testing. I wrote about why I started in my first post.

Some of the words above need a short explanation:

Seed

One number that drives every random choice (network delay, which message is lost, and the faults too, unless you write the plan yourself).

Plan

The list of faults for one run (crash node 1 at t=69, split the network at t=900). Usually detsim makes it from the seed, but you can also write it by hand when you already have a scenario in mind. The seed still drives everything else, like message delays, so one hand-written plan with 1000 seeds gives you 1000 different runs of the same scenario.

One thread

We use one thread because we want to be deterministic, so there’s no scheduler randomness. Time is fake and every random number comes from the seed. So the same seed gives the same run on every machine.

Each run uses one thread. Sweep runs many seeds side by side, one per core, but they share nothing, so each run is still deterministic.

Shrink

Imagine seed 17 fails, and its plan has 40 faults. You don’t need all of them, only the ones that cause the failure. Shrink removes every fault that isn’t needed until only the cause is left.

Detsim as an example

This is how the loop looks in detsim. gen makes the plan from a seed, build makes the cluster, and check runs it and says pass or fail:

353
354
355
356
357
358
359
360
361
362
f, all := sim.Sweep(1, 10_001, gen, build, check)
if f != nil {
	small := sim.Shrink(f.Plan, func(p *sim.Plan) bool {
		s, c := build(f.Seed)
		s.StartWith(p)
		return check(s, c) != nil
	})
	t.Fatalf("seed %d: %v\nplan:\n%s", f.Seed, f.Err, small)
}
_ = all // every failing seed with its plan, for SaveCorpus

cmd/lockcheck uses it to test a small lock server with a planted bug. Run go run -tags simfaults ./cmd/lockcheck -seed 170 and you get:

shrunk 54 faults to 1, 1 buggify to 1

seed 170
invariant: invariant broken at t=1397 #67: clients [1 3] are all inside the critical section, durable owner record says 3
  n0: granted=3
  n1: HOLDING attempt=1.5
  n2: waiting attempt=1.9
  n3: HOLDING attempt=1.12

schedule (1 faults):
t=1332   node 0 crashed; wipe disk: false; downtime: 1
buggify  lockserver/skip-sync

Two clients hold the same lock. Out of 54 faults, only one crash matters.

Corpus

You can keep failing seeds in a corpus. A corpus is a file of seeds that failed, each with its shrunk plan. We rerun them later as regression tests.

Here is a real entry from my raft corpus. The first line is the seed and the error. Each line under it is one fault and when it happens.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
version 2
# raft simulation, workload v1
# flags:

261	invariant broken at t=9497 #3339: two leaders in term 14: node0 and node1
	t=1728 isolate 2
	t=1890 heal all
	t=4020 node 1 crashed; wipe disk: false; downtime: 700
	t=4239 isolate 2
	t=4504 heal all
	t=4715 partition [0]|[1 2]
	t=4850 heal all
	t=5365 node 2 crashed; wipe disk: false; downtime: 224
	t=8418 isolate 0
	t=8678 node 1 crashed; wipe disk: false; downtime: 214
	t=8834 heal all
	t=8932 node 2 paused; duration: 233
	t=9338 node 0 paused; duration: 142

This plan is already shrunk. Even so, it still needs 13 faults to get two leaders. I would never find that by hand.

That’s the idea: fake time, fake network, fake disk, one thread, and a seed that makes every run repeatable.

I will try to write more about detsim and how it works inside.

The code is on GitHub.