Makefile254 lines, 12 KB
| 1 | # Local development. The API reads DATABASE_URL and REDIS_URL from the environment. |
| 2 | DATABASE_URL ?= postgres://murmur:murmur@localhost:5432/murmur?sslmode=disable&connect_timeout=5 |
| 3 | REDIS_URL ?= redis://localhost:6379/0 |
| 4 | # The proxy that a server run by hand believes (make run). The containers get their settings |
| 5 | # from docker-compose.yml, and they believe the Compose network. |
| 6 | TRUSTED_PROXIES ?= 127.0.0.1/32 |
| 7 | export DATABASE_URL REDIS_URL TRUSTED_PROXIES |
| 8 | |
| 9 | .PHONY: up down run test seed load explain redis worker jobs claim dead retry poison ps roll |
| 10 | |
| 11 | # build our image and start everything: Postgres and its replica, Redis, two API servers, the |
| 12 | # worker, nginx, Prometheus (localhost:9090) and Grafana (localhost:3000). More of ours: make scale. |
| 13 | # For load tests: make up IP_PER_MINUTE=6000000 IP_BURST=100000 |
| 14 | up: |
| 15 | docker compose up -d --build --wait |
| 16 | |
| 17 | # stop them (the data stays in its volume) |
| 18 | down: |
| 19 | docker compose down |
| 20 | |
| 21 | # what is running, and how each service says it is doing |
| 22 | ps: |
| 23 | @docker compose ps --format "table {{.Service}}\t{{.Status}}" |
| 24 | |
| 25 | # restart the API servers one at a time: the next one goes when the last one is ready again |
| 26 | roll: |
| 27 | @for c in $$(docker compose ps -q api); do \ |
| 28 | docker restart $$c > /dev/null; \ |
| 29 | until [ "$$(docker inspect -f '{{.State.Health.Status}}' $$c)" = healthy ]; do sleep 0.2; done; \ |
| 30 | echo "$$(docker inspect -f '{{.Name}}' $$c | cut -c2-) is back, and ready"; \ |
| 31 | done |
| 32 | |
| 33 | # one API server by hand, outside Docker, to work on it: it builds into bin/ and listens on 8081. |
| 34 | # nginx doesn't know it, so talk to it directly: curl localhost:8081/users/user1/posts |
| 35 | # the rate limits are settings: make run USER_BURST=3 USER_PER_MINUTE=12 |
| 36 | # metrics, the profiler, /healthz and /readyz are on its private port, 9081 |
| 37 | run: |
| 38 | go build -o bin/api ./cmd/api |
| 39 | ./bin/api |
| 40 | |
| 41 | # build the worker into bin/ and run it. Its private port is 9101 |
| 42 | worker: |
| 43 | go build -o bin/worker ./cmd/worker |
| 44 | ./bin/worker |
| 45 | |
| 46 | # one package at a time: the tests share one database |
| 47 | test: |
| 48 | go test -race -p 1 ./... |
| 49 | |
| 50 | # fill the database with a graph of 100,000 users (it empties it first) |
| 51 | seed: |
| 52 | go run ./cmd/seed --users 100000 --seed 1 |
| 53 | |
| 54 | # run a k6 script from load/, in Docker: make load S=feed |
| 55 | # it goes through nginx. Past nginx, straight to the API servers: make load S=feed BASE_URL=http://api:8081 |
| 56 | # a like storm on one post: make load S=likes POST=2000002 |
| 57 | # the mix at 1,500 requests a second, for a minute: make load S=mix RATE=1500 TIME=60s |
| 58 | load: |
| 59 | docker compose run --rm -e BASE_URL -e VUS -e READERS -e AUTHORS -e FIRST -e POSTS -e RATE -e POST \ |
| 60 | -e TIME -e SPIKE -e STAR k6 run /load/$(S).js |
| 61 | |
| 62 | # what Postgres does to build one user's feed: make explain U=42 |
| 63 | Q ?= feed |
| 64 | explain: |
| 65 | @docker compose exec -T postgres psql -U murmur -v user=$(U) < db/explain/$(Q).sql \ |
| 66 | | grep -E "Scan|Execution" |
| 67 | |
| 68 | # one Redis command, run in the container: make redis C="dbsize" |
| 69 | redis: |
| 70 | @docker compose exec redis redis-cli $(C) |
| 71 | |
| 72 | # the queue: how many jobs are waiting, running and dead, and two of them: running ones first |
| 73 | jobs: |
| 74 | @docker compose exec postgres psql -U murmur \ |
| 75 | -c "select count(*) filter (where status = 'waiting') as waiting, \ |
| 76 | count(*) filter (where status = 'running') as running, \ |
| 77 | count(*) filter (where status = 'dead') as dead, \ |
| 78 | max(now() - created_at) filter (where status <> 'dead')::interval(0) as oldest from jobs" \ |
| 79 | -c "select id, kind, status, payload from jobs where status <> 'dead' order by status, id limit 2" |
| 80 | |
| 81 | # the dead list: jobs that used up their attempts, and the last thing that went wrong |
| 82 | dead: |
| 83 | @docker compose exec postgres psql -U murmur \ |
| 84 | -c "select id, kind, attempts, left(last_error, 84) as last_error \ |
| 85 | from jobs where status = 'dead' order by id" |
| 86 | |
| 87 | # send dead jobs back to the queue with a fresh count: all of them, or make retry ID=7 |
| 88 | retry: |
| 89 | @docker compose exec postgres psql -U murmur \ |
| 90 | -c "update jobs set status = 'waiting', attempts = 0, run_at = now() \ |
| 91 | where status = 'dead' $(if $(ID),and id = $(ID))" |
| 92 | |
| 93 | # a job that can never work: its author is a name, and the handler wants a number |
| 94 | poison: |
| 95 | @docker compose exec postgres psql -U murmur \ |
| 96 | -c "insert into jobs (kind, payload) \ |
| 97 | values ('fanout', jsonb_build_object('author', 'alice', 'post', 1))" |
| 98 | |
| 99 | # a worker, played by hand: begin, pick a job with db/claim.sql, hold it for 8 seconds, roll back |
| 100 | claim: |
| 101 | @docker compose exec -T postgres psql -U murmur -q \ |
| 102 | -c 'begin' -c '\timing on' -f - -c '\timing off' -c '\! sleep 8' -c 'rollback' < db/claim.sql |
| 103 | |
| 104 | .PHONY: watch backlog cpu waits hold head likes followers |
| 105 | |
| 106 | # the queue, every two seconds: jobs waiting, jobs running, and how long the oldest has waited |
| 107 | watch: |
| 108 | @echo "select 'waiting ' || lpad((count(*) filter (where status = 'waiting'))::text, 6) \ |
| 109 | || ' running ' || lpad((count(*) filter (where status = 'running'))::text, 3) \ |
| 110 | || ' oldest ' || coalesce(extract(epoch from max(now() - created_at) \ |
| 111 | filter (where status <> 'dead'))::int, 0) || ' s' from jobs \\watch 2" \ |
| 112 | | docker compose exec -T postgres psql -U murmur -At |
| 113 | |
| 114 | # work for the queue without anybody posting: the newest N posts of small accounts are fanned out |
| 115 | # once more, which changes no timeline. BIG=1 takes posts of big accounts: jobs with nothing to push |
| 116 | N ?= 10000 |
| 117 | backlog: |
| 118 | @docker compose exec -T postgres psql -U murmur -c \ |
| 119 | "insert into jobs (kind, payload) \ |
| 120 | select 'fanout', jsonb_build_object('author', p.user_id, 'post', p.id) \ |
| 121 | from posts p join users u on u.id = p.user_id \ |
| 122 | where u.followers_count $(if $(BIG),>=,<) 1000 order by p.id desc limit $(N)" |
| 123 | |
| 124 | # how busy Postgres and Redis are, right now: 100% is one core |
| 125 | cpu: |
| 126 | @docker compose stats --no-stream --format "table {{.Name}}\t{{.CPUPerc}}" |
| 127 | |
| 128 | # what Postgres is doing: twenty looks at the statements that are running, and what each one waits for |
| 129 | waits: |
| 130 | @for i in $$(seq 20); do docker compose exec -T postgres psql -U murmur -Atc \ |
| 131 | "select coalesce(wait_event, 'working') from pg_stat_activity \ |
| 132 | where state = 'active' and backend_type = 'client backend' and pid <> pg_backend_pid()"; \ |
| 133 | done | sort | uniq -c | sort -rn |
| 134 | |
| 135 | # a transaction that reads once and then stays open, the way a backup does: make hold FOR=35 |
| 136 | hold: |
| 137 | @docker compose exec -T postgres psql -U murmur -q -c 'begin isolation level repeatable read' \ |
| 138 | -c 'select count(*) as users from users' -c '\! sleep $(FOR)' -c 'rollback' |
| 139 | |
| 140 | # what one claim has to read before it finds its job: the pages it touched, and the time it took |
| 141 | head: |
| 142 | @docker compose exec -T postgres psql -U murmur -c "explain (analyze, buffers, costs off, timing off) \ |
| 143 | select id from jobs where status = 'waiting' and run_at <= now() \ |
| 144 | order by run_at, id limit 1 for update skip locked" \ |
| 145 | | grep -E -A 3 "Scan|Execution" | grep -E "Buffers|Execution" | sed 's/^ *//' |
| 146 | |
| 147 | # one post's likes: the rows in the likes table, the count on the post, and what still waits to be counted |
| 148 | likes: |
| 149 | @docker compose exec -T postgres psql -U murmur -c \ |
| 150 | "select (select count(*) from likes where post_id = p.id) as likes, p.likes_count as counted, \ |
| 151 | (select coalesce(sum(delta), 0) from like_deltas where post_id = p.id) as waiting \ |
| 152 | from posts p where p.id = $(P)" |
| 153 | |
| 154 | # the ids of everybody who follows one account, for the stream client: |
| 155 | # ./bin/listen -count $(make followers OF=115) |
| 156 | followers: |
| 157 | @docker compose exec -T postgres psql -U murmur -Atc \ |
| 158 | "select follower_id from follows where followee_id = $(OF) order by 1" |
| 159 | |
| 160 | .PHONY: ramp statements |
| 161 | |
| 162 | # the mix at one rate after another, half a minute each: make ramp RATES="1000 2000 3000" |
| 163 | # between two steps it rests, so that a laptop can cool down |
| 164 | RATES ?= 1000 1500 2000 2500 3000 3500 |
| 165 | REST ?= 30 |
| 166 | ramp: |
| 167 | @for r in $(RATES); do \ |
| 168 | docker compose run --rm -T -e BASE_URL -e RATE=$$r k6 run -q /load/mix.js 2> /dev/null; \ |
| 169 | sleep $(REST); \ |
| 170 | done |
| 171 | |
| 172 | # what Postgres spent its time on, statement by statement, since the last time anybody asked: |
| 173 | # it prints the list, and then it starts counting from zero again |
| 174 | DB ?= postgres |
| 175 | statements: |
| 176 | @docker compose exec -T $(DB) psql -U murmur -q \ |
| 177 | -c "set client_min_messages = warning" \ |
| 178 | $(if $(filter replica,$(DB)),,-c "create extension if not exists pg_stat_statements") \ |
| 179 | -c "select left(regexp_replace(query, '\s+', ' ', 'g'), 52) as statement, calls, plans, \ |
| 180 | round(total_plan_time) as plan_ms, round(total_exec_time) as exec_ms \ |
| 181 | from pg_stat_statements order by total_plan_time + total_exec_time desc limit 6" \ |
| 182 | -c "select pg_stat_statements_reset()" | grep -E "\||-\+-" |
| 183 | |
| 184 | .PHONY: scale drained lag pause resume |
| 185 | |
| 186 | # more API servers or workers, or fewer: make scale API=4 WORKER=3. Nothing is built, and the |
| 187 | # containers that already run are left alone. make scale on its own goes back to two and one |
| 188 | scale: |
| 189 | docker compose up -d --wait --no-build |
| 190 | |
| 191 | # wait until the queue is empty: time make drained says how long that took |
| 192 | drained: |
| 193 | @until docker compose exec -T postgres psql -U murmur -Atc \ |
| 194 | "select count(*) from jobs where status <> 'dead'" | grep -qx 0; do sleep 0.5; done |
| 195 | |
| 196 | # how far behind the replica is: what the primary has sent it, and what it hasn't replayed yet |
| 197 | lag: |
| 198 | @docker compose exec -T postgres psql -U murmur -c "select state, \ |
| 199 | pg_wal_lsn_diff(sent_lsn, replay_lsn) as bytes_behind, replay_lag from pg_stat_replication" |
| 200 | |
| 201 | # for a demo: the replica keeps receiving the log, and stops replaying it. make resume undoes it |
| 202 | pause: |
| 203 | @docker compose exec -T replica psql -U murmur -Atc "select pg_wal_replay_pause()" |
| 204 | resume: |
| 205 | @docker compose exec -T replica psql -U murmur -Atc "select pg_wal_replay_resume()" |
| 206 | |
| 207 | .PHONY: hot |
| 208 | |
| 209 | # a burst of follows, made the way the API makes them: two thousand people, from FROM on, follow |
| 210 | # one account each. Then how many of the rows in users that changed were updated in place (HOT) |
| 211 | FROM ?= 50000 |
| 212 | hot: |
| 213 | @docker compose exec -T postgres psql -U murmur -Atc \ |
| 214 | "select pg_stat_reset_single_table_counters('users'::regclass)" > /dev/null |
| 215 | @docker compose exec -T postgres psql -U murmur -qc "with added as ( \ |
| 216 | insert into follows (follower_id, followee_id) \ |
| 217 | select g, 1 + (g * 7919) % 100000 from generate_series($(FROM), $(FROM) + 1999) g \ |
| 218 | where 1 + (g * 7919) % 100000 <> g on conflict do nothing returning followee_id) \ |
| 219 | update users set followers_count = followers_count + 1 where id in (select followee_id from added)" |
| 220 | @sleep 1 |
| 221 | @docker compose exec -T postgres psql -U murmur -c "select n_tup_upd as updated, \ |
| 222 | n_tup_hot_upd as in_place from pg_stat_user_tables where relname = 'users'" |
| 223 | |
| 224 | .PHONY: plan counts |
| 225 | |
| 226 | # a whole plan, with what the planner expected next to what happened: make plan Q=posts U=1 |
| 227 | # (the lines about conditions and costs are left out) |
| 228 | plan: |
| 229 | @docker compose exec -T postgres psql -U murmur -Aqt -v user=$(U) < db/explain/$(Q).sql \ |
| 230 | | sed -E 's/cost=[^ ]+ //; s/ width=[0-9]+//' \ |
| 231 | | grep -vE "Recheck|Heap Blocks|Index Cond|Filter|Planning|Sort Key|Bitmap Index|Heap Fetches" |
| 232 | |
| 233 | # what the seed made: how many of each, two accounts we know, and a fingerprint of who posted |
| 234 | # when and of who follows whom. The same seed must give the same numbers |
| 235 | counts: |
| 236 | @docker compose exec -T postgres psql -U murmur \ |
| 237 | -c "select (select count(*) from users) as users, (select count(*) from follows) as follows, \ |
| 238 | (select count(*) from posts) as posts, \ |
| 239 | (select followers_count from users where id = 1) as user1_followers, \ |
| 240 | (select count(*) from posts where user_id = 1) as user1_posts, \ |
| 241 | (select followers_count from users where id = 16726) as quiet_followers" \ |
| 242 | -c "select (select md5(string_agg(user_id || ' ' || created_at, ',' order by id)) from posts) \ |
| 243 | as posts_md5, (select md5(string_agg(follower_id || ' ' || followee_id, ',' \ |
| 244 | order by follower_id, followee_id)) from follows) as follows_md5" |
| 245 | |
| 246 | .PHONY: web web-dev |
| 247 | |
| 248 | # build the web app into web/dist, where nginx serves it: localhost:8088 |
| 249 | web: |
| 250 | cd web && npm ci && npm run build |
| 251 | |
| 252 | # the web app while you work on it: localhost:5173, with /api/ sent to nginx |
| 253 | web-dev: |
| 254 | cd web && npm run dev |