Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

vole
Clone

Read only, no credentials:

https://rodent.software/vole.git

Write over SSH with a registered key:

git clone ssh://git@rodent.software:2222/vole.git

A free and open-source forge that lives in your Git repo.

Metadata writers queue on the branch instead of racing for it

122cbe4f62bf87a941538450584ba1b20444c135 · Keenan Nemetz · 7d ago · parent 4739664b

Metadata writers queue on the branch instead of racing for it

Every write to refs/heads/vole read the whole branch, decided, and moved the
ref with a compare-and-swap, retrying up to 64 times with no backoff and no
coordination. Concurrent writers therefore did not queue: they each paid a full
read, lost, and paid it again. Measured on a 2,000-file branch, one write needed
one attempt alone and 21 with 32 writers, and throughput fell as writers were
added because the retries were the load. At 64 simultaneous writers the loop ran
out and the write failed for being unlucky.

The read-modify-write now runs with one writer at a time, holding a flock in the
repository so the SSH hook and the server queue behind each other. The
compare-and-swap stays for writers that go around the lock — a push, a hand-run
git command, another host — and losing one now waits with jittered backoff
before deciding again.

Writers read the branch once. The load and the decision happen inside the commit
instead of before it, which also stops an edit from replacing a change that
landed while the page was open: the decision is made against the tree it writes.

A push only does that work when it has metadata to move. Post-receive asks the
index which open requests follow the branch it moved, and a branch nobody is
proposing from now writes an audit line and no commit.

Measurements, 32 concurrent writers: 21.3 CAS attempts per write before, 1.0
after. Through the web, posting comments at concurrency 32: p99 4.7 s -> 0.95 s,
and throughput no longer falls as workers are added. The load harness gains a
`comment` scenario so the number is visible.

Closes: 01M6D9G8H6E0RCQ3AD5TMAGX34

forge/index/requests.go +37

@@ -10,9 +10,12 @@ package index
10 10 // head, and a backlink has to find the request from the issue.
11 11
12 12 import (
13 "database/sql"
14 "errors"
13 15 "strings"
14 16 "time"
15 17
18 "github.com/rodent-software/vole/forge/repo"
16 19 "github.com/rodent-software/vole/metadata"
17 20 )
18 21
@@ -48,6 +51,40 @@ const requestSelect = `SELECT r.id, r.title, r.state, r.author, r.created, r.bas
48 51 FROM requests r
49 52 LEFT JOIN entity_numbers n ON n.repo_id = r.repo_id AND n.id = r.id`
50 53
54 // RequestsFollowing reports whether an open request follows branch, and whether that
55 // answer can be trusted.
56 //
57 // It exists so a push to a branch nobody is proposing from does not read the metadata
58 // branch: the answer is one indexed row rather than the whole tree, and that push is
59 // the common case by a wide margin. ok is false when the rows were built from a
60 // different metadata commit than the branch is at now — a push that landed while the
61 // index was behind, or a repository the index has not seen. A caller that must not
62 // miss a follower reads that as "possibly", because a sync that does not happen is not
63 // repaired by anything afterwards.
64 func (s *Store) RequestsFollowing(m *repo.Meta, branch string) (follows, ok bool, err error) {
65 var stored string
66 if err := s.db.QueryRow(`SELECT issues_sha FROM repos WHERE id=?`, m.ID).Scan(&stored); err != nil {
67 return false, false, err
68 }
69 current, err := m.Git().RefCommit(metadata.Ref)
70 if err != nil {
71 return false, false, err
72 }
73 if stored != current {
74 return false, false, nil
75 }
76 var one int
77 err = s.db.QueryRow(`SELECT 1 FROM requests WHERE repo_id=? AND state='open' AND head_ref=? LIMIT 1`,
78 m.ID, branch).Scan(&one)
79 if errors.Is(err, sql.ErrNoRows) {
80 return false, true, nil
81 }
82 if err != nil {
83 return false, false, err
84 }
85 return true, true, nil
86 }
87
51 88 // Requests returns a repo's indexed requests, open first and newest first within
52 89 // each state. That is the order the list page shows, so paging does not have to
53 90 // re-sort what the database already ordered.