System Design

T1

The grader

Tests you write yourself only find the bugs you thought of. The grader checks the spec from outside: start your server, point the grader at it, and read the report.

Not startedSaved in this browser only.
  1. 1The grader speaks plain HTTP, so it grades a server in any language.
  2. 2A failure shows the request and the response, so you can repeat it with curl.
  3. 3Concurrency checks fire 100 parallel requests at one key and count the result.
  4. 4Exit code 0 means every check passed. Use it as the end of each rehearsal.
T1
    A

    One run

    plain HTTP, any server
    arena runexercise: a list of checkseach key unique to the run100 parallel on one keyyour serverany languageany frameworklocalhost:8080HTTP requeststatus and JSONreportPASS or FAIL per checkwith request and responseexit code0 pass, 1 fail2 could not start

    I run the grader at each checkpoint. It tells me which behaviours are missing, not only that my own tests pass.

    B

    How to use it

    two terminals
    build the command onceterminal
    go build -o arena ./cmd/arena
    ./arena list
    kvstore      Key-value store with nested transactions
    grade your serverterminal
    ./arena run kvstore --base http://localhost:8080
    ./arena run kvstore --base http://localhost:8080 \
      --header "Authorization: Bearer my-token"
    exit codemeaningstatus
    0Every check passed.Approved
    1At least one check failed.Not approved
    2The run could not start: no server, or a wrong name or flag.Fix the command

    One terminal runs the server. The other runs the grader.

    C

    What it grades

    30 checks in E1
    groupE1 checksquestion it answersexample
    correctness12Does the API do what the spec says?Nested rollback undoes only the inner level.
    status codes7Does each outcome have the right code?A stale read gives 409 at commit.
    validation5Does bad input fail with a 400 and no change?A missing value is 400; an empty string is a value.
    concurrency6Under 100 parallel requests, is anything lost or counted twice?Parallel commits of one transaction: exactly one wins.

    Correct results come first. Then correct status codes, then bad input, then behaviour under 100 parallel requests.

    D

    A clean report

    the reference build, saved from a real run
    reportsaved output
    arena run kvstore --base http://localhost:8080
    
    correctness
      PASS  get returns what set stored
      PASS  set replaces the value
      PASS  an empty string is a value
      PASS  delete removes the key
      PASS  commit publishes the writes
      PASS  a transaction reads its own writes
      PASS  writes stay hidden until commit
      PASS  rollback discards the writes
      PASS  a delete in a transaction waits for commit
      PASS  nested rollback undoes only the inner level
      PASS  nested commit is undone by an outer rollback
      PASS  three levels unwind in order
    
    status codes
      PASS  get of a missing key is 404
      PASS  delete of a missing key is 404
      PASS  begin is 201 and returns an id
      PASS  an unknown transaction is 404
      PASS  an ended transaction is 404
      PASS  a stale read gives 409 at commit
      PASS  two creators of one key: the second gets 409
    
    validation
      PASS  malformed JSON is 400
      PASS  a missing value is 400
      PASS  a value of the wrong type is 400
      PASS  a key over 256 bytes is 400
      PASS  a key of exactly 256 bytes is fine
    
    concurrency
      PASS  100 parallel sets of one key all succeed
      PASS  100 parallel deletes of one key: exactly one wins
      PASS  100 parallel transactions increment one key: none is lost
      PASS  100 parallel rollbacks leave the key alone
      PASS  100 parallel writes in one transaction all land
      PASS  100 parallel commits of one transaction: exactly one wins
    
    30 checks: 30 passed, 0 failed

    All green means the behaviours in the spec work. It does not mean that the design is good. The talk covers the design.

    E

    A failure

    a server with a real bug
    one failuresaved output
      FAIL  a stale read gives 409 at commit
            want status 409, got 200
            > GET /tx/H6DLVSPC65IW6ECYQHXDCBV3W2/kv/run-18-k
            < 200 {"key":"run-18-k","value":"1"}
            > PUT /tx/H6DLVSPC65IW6ECYQHXDCBV3W2/kv/run-18-k {"value":"2"}
            < 200 {"key":"run-18-k","value":"2"}
            > PUT /kv/run-18-k {"value":"other"}
            < 200 {"key":"run-18-k","value":"other"}
            > POST /tx/H6DLVSPC65IW6ECYQHXDCBV3W2/commit
            < 200 {"id":"H6DLVSPC65IW6ECYQHXDCBV3W2"}
    • The first line says what the grader wanted and what it got.
    • Lines with > are requests. Lines with < are responses. The grader shows the last 4 exchanges.
    • In this saved run, the server failed 4 of 30 checks.

    The bug: commit writes without comparing the versions it read. Every memory access is locked, so the race detector reports nothing.

    The report shows the failing request and response, so I can repeat it with curl.

    F

    How a concurrency check works

    the code that runs
    the lost-update checkgo
    {Name: "100 parallel transactions increment one key: none is lost", Group: grader.Concurrency, Run: func(t *grader.T) {
      key := t.Key("n")
      set(t, "", key, "0")
      var committed atomic.Int64
      t.Parallel(parallel, func(int) {1
        increment(t, key)2
        committed.Add(1)
      })
      got := value(t, "", key)
      if got != strconv.FormatInt(committed.Load(), 10)3 {
        t.Failf("%d transactions committed an increment, but the key holds %s", committed.Load(), got)
      }
    }},
    1. 1100 goroutines. This is the same behaviour a test with a WaitGroup checks, but over a real socket.
    2. 2Begin, read, write the value plus 1, commit. On 409 it waits a random time and starts again, as a real client must.
    3. 3The count of successful commits must equal the stored number. One lost update breaks this.
    the parallel helpergo
    // Parallel runs fn on n goroutines that all start at once, and returns when they finish. A failure
    // in any goroutine stops the check once all have ended.
    func (t *T) Parallel(n int, fn func(i int)) {
      t.parallel.Store(true)
      defer t.parallel.Store(false)
    
      var (
        wg    sync.WaitGroup
        once  sync.Once
        first failed
        hit   bool
        start = make(chan struct{})
      )
      for i := range n {
        wg.Go(func() {
          <-start1
          defer func() {
            if r := recover(); r != nil {
              f, ok := r.(failed)
              if !ok {
                panic(r)
              }
              once.Do(func() { first, hit = f, true })2
            }
          }()
          fn(i)
        })
      }
      close(start)
      wg.Wait()
      if hit {
        panic(first)
      }
    }
    
    1. 1Every goroutine waits here. close(start) releases all of them at once. A test proves that 100 requests overlap in the server.
    2. 2The first failure is the one the report shows. The check stops after all goroutines end.

    All goroutines wait at one gate, so the 100 requests really overlap. Then the check compares the final value with the number of commits that succeeded.

    G

    Add an exercise

    three steps
    a check and an exercisego
    // Check is one graded behaviour. Run talks to the server through t and stops the check with a
    // failure through t.Status, t.Failf or t.Parallel. A check that returns has passed.
    type Check struct {
      Name  string
      Group Group1
      Run   func(t *T)2
    }
    
    // Exercise is a named list of checks.
    type Exercise struct {
      Name   string
      Title  string
      Checks []Check
    }
    
    1. 1One of correctness, status codes, validation or concurrency. The report prints the groups in that order.
    2. 2A check that returns has passed. t.Status, t.Failx and t.Failf stop the check with a message.
    1. List the checks in a package for the exercise. Build every key with t.Key.
    2. Call grader.Register from the package's init function.
    3. Add a one-line file to the command that imports the package.

    An exercise is a list of checks. It knows the API and nothing about any server.

    H

    Why you can trust it

    the grader's own tests
    server under testresult
    The reference buildAll pass
    The production version, over a real socketAll pass
    A commit that ignores what it read: the raceCaught
    Delete answers 200 and not 204Caught
    A missing transaction answers 400 and not 404Caught
    Writes reach the store before commitCaught
    Nothing listens on the portExit code 2
    • Green checks cover the spec and not the design.
    • A race can hide on one run. Run the grader 3 times.

    We test the grader on broken servers. A grader that passes everything proves nothing.

    I

    Drill

    predict, then reveal

    0 of 5 known

    1. Your server is in Python. Can the grader check it?

    2. The run ends with exit code 2 and no report. What happened?

    3. A server passes go test -race but fails the 100 parallel increments. How?

    4. You run the grader twice against the same server. Does the second run see data from the first?

    5. A check passes 100 increments with retries on 409. Why must the grader count commits and not requests?