Bug: DEFINE INDEX … CONCURRENTLY does not become ready while the table receives sustained writes (RocksDB)
What component does this affect?
SurrealDB Server
Describe the bug
On RocksDB, a DEFINE INDEX ... CONCURRENTLY build stayed at indexing for as long as the table kept receiving a steady stream of writes, and became ready at about the time the writes stopped. The builder did not fall behind: it kept pace with the writes the whole time.
- With 8 concurrent writer loops, the build was still
indexingwhen the writers were stopped after 120–181 s, in all 9 runs (8 on 3.2.4, 1 on 3.3.0; 615–784 writes/s where measured). - With 4 writer loops, 4 of 5 runs were still
indexingwhen the writers were stopped after 73–221 s (315–413 writes/s). The fifth run (~250 writes/s) becamereadyafter 23 s. - With 1 writer loop (232 writes/s), the build became
readyafter 5.2 s.
Setup: a single node on rocksdb:, a 100,000-row table, and N concurrent writer loops. Each loop sends HTTP /sql requests of 200 single-row UPDATEs. Each UPDATE is its own transaction and changes the indexed field.
| version | writer loops | writes/s during the build | ready while the writers ran? |
ready after the writer loops were stopped ¹ |
|---|---|---|---|---|
| 3.2.4 | 8 | 615 | no, indexing for all 181 s |
+1.8 s |
| 3.2.4 | 8 | 640, 702, 659 (3 runs) | no, indexing for all 180.6–181.0 s in each |
+2.2 s, +2.6 s, +1.9 s |
| 3.2.4 | 8 | 639 | no, indexing for all 120 s |
+1.9 s |
| 3.2.4 | 8 | 784 (drain probe run, see below) | no, indexing for all 121 s |
+1.7 to +2.0 s |
| 3.2.4 | 8 | ~690 ² (earlier version of the snippet) | no, indexing for all 120 s |
by the +2 s poll |
| 3.2.4 | 8 | ~845 ² (snippet below) | no, indexing for all 120 s |
by the first poll 1 s after the last request finished |
| 3.3.0 | 8 | 687 | no, indexing for all 180 s |
+1.8 s |
| 3.2.4 | 4 | 401 | no, indexing for all 221 s |
+2.1 s |
| 3.2.4 | 4 | 371 | no, indexing for all 121 s |
+1.5 s |
| 3.2.4 | 4 | 413 | no, indexing for all 73 s |
+1.5 s |
| 3.2.4 | 4 | ~250 ² (earlier version of the snippet, 4 loops) | yes, after 23 s | – |
| 3.3.0 | 4 | 315 | no, indexing for all 180 s |
+1.6 s |
| 3.2.4 | 1 | 232 | yes, after 5.2 s | – |
¹ Time from the moment the writer loops were stopped. The requests already in flight (up to 200 statements each) keep committing for a short time after that, so this overstates the time from the last committed write. The drain probe below measures that directly.
² Estimated from the growth of the builder's updated counter; the snippet does not count writes.
Drain after the writers stop. In one run with a finer probe (3.2.4, 8 writer loops, 784 writes/s, still indexing after 120.6 s), the loops were told to stop and each one finished its in-flight request. The probe then sampled the committed-write count (math::sum(x); each UPDATE adds 1) and INFO FOR INDEX about every 0.2 s:
- the last in-flight request finished between +1.59 s and +1.83 s after the stop;
- the committed-write count last changed between +1.59 s and +1.95 s;
- the status was first
readybetween +1.72 s and +1.99 s.
So the build became ready between 0.23 s before and 0.40 s after the last committed write, i.e. at about the time the writes stopped.
Other observations:
- The initial scan of the 100,000 rows completed 3.5–9 s after
DEFINE INDEX. - After the initial scan,
updatedgrew at the same rate as the committed writes. In each non-converging run of the longer script (11 runs), the difference between the committed-write count andupdated(sampled every 10th poll, or every poll in three runs) stayed within a band 24–121 wide while 30,000–123,000 writes were committed. It did not grow, so the replay kept pace with the writers. - With debug logging (one 4-loop run, 73 s), the builder logged 40 retryable-conflict retries. Each retry sleeps 100 ms (builder.rs L1156–L1171), about 4 s in total, so these retries do not account for the stall by themselves.
Relevant source (v3.2.4)
The following is my reading of the code. I did not instrument the server, so this is an explanation that fits the measurements, not a confirmed cause.
- After the initial scan, the builder runs a "first replay pass" while the build is still
Building. As the code comment says, new writers can keep reserving tickets in this phase. The build moves toClosingonly after this pass returns: builder.rs L1797–L1815. index_appending_loopreturns only when a single read finds both queues empty ("until the visible queues are empty"): replay.rs L124 and L142–L172. I found no limit on passes or time, and no backlog threshold.- In
Closing, the admission of a new indexed write cancels, sleeps 100 ms and retries until the build isOnline(orError): admission.rs L340–L347. In the non-converging runs, indexed writes kept committing at a steady rate for the whole window. If I read the admission code correctly, the build therefore did not reachClosingduring the window: it was still in the first replay pass. - Taken together, the first pass seems to end only when one of its reads happens to find no new queued entry. With a steady stream of commits, that rarely happens.
- Per-commit latency seems to matter. With the in-memory engine (
memory) and the same workload, the build becamereadyunder load at higher write rates than on RocksDB: after 5.0 s at ~5,900 writes/s (8 loops) and after 1.3 s at ~11,000 writes/s (4 loops), one run each. My guess, which I did not verify: on RocksDB each replay iteration (read, apply, commit, progress report) takes much longer, so it is much less likely that no commit lands between two reads. - The phase is not visible from
INFO FOR INDEX:BuildingandClosingboth reportindexing: state.rs L150–L156.pending: 0is written after every committed batch (replay.rs L225–L234, L275–L284), so it does not mean the queue is empty.
v3.3.0 has the same structure: builder.rs L2048–L2066, and replay.rs L196 and L234.
Steps to reproduce
Needs docker, curl and awk. The script starts a RocksDB server, seeds 100,000 rows and starts 8 concurrent writer loops. Then it runs DEFINE INDEX ... CONCURRENTLY and polls INFO FOR INDEX every second for SECS seconds (default 180). After that it tells the writer loops to stop, waits until their in-flight requests have finished, and polls for 30 s more. Ctrl-C stops the writer loops and removes the container.
#!/usr/bin/env bash
# Needs docker, curl and awk. Ctrl-C stops the writer loops and removes the container.
IMAGE=${IMAGE:-surrealdb/surrealdb:v3.2.4}; PORT=${PORT:-8000}; SECS=${SECS:-180}; NAME=${NAME:-ixlive}
D=$(mktemp -d); PIDS=""
cleanup() {
touch "$D/stop"; for p in $PIDS; do pkill -P "$p"; kill "$p"; done 2>/dev/null
docker rm -fv "$NAME" >/dev/null 2>&1; rm -rf "$D"
}
trap cleanup EXIT; trap 'exit 130' INT TERM
docker run -d --name "$NAME" -p "127.0.0.1:$PORT:8000" "$IMAGE" start --user root --pass root rocksdb:/tmp/db >/dev/null || exit 1
sleep 3
Q() { curl -s -u root:root -H 'Accept: application/json' -H 'Surreal-NS: repro' -H 'Surreal-DB: repro' \
--data-binary "$1" "http://127.0.0.1:$PORT/sql"; }
Q 'DEFINE NAMESPACE repro; USE NS repro; DEFINE DATABASE repro;' >/dev/null
Q 'DEFINE TABLE t SCHEMALESS; CREATE |t:1..=100000| SET x = 0 RETURN NONE;' >/dev/null
# 8 concurrent writer loops (HTTP /sql requests of 200 single-row UPDATEs);
# each statement is its own transaction and changes the indexed field
for w in 1 2 3 4 5 6 7 8; do
awk -v s=$w 'BEGIN{srand(s); for(i=0;i<200;i++) printf "UPDATE t:%d SET x += 1 RETURN NONE;\n", int(rand()*100000)+1}' > "$D/w$w.surql"
( while [ ! -e "$D/stop" ]; do Q "@$D/w$w.surql" >/dev/null; done ) &
PIDS="$PIDS $!"
done
sleep 5
Q 'DEFINE INDEX ix ON t FIELDS x CONCURRENTLY;'; echo
for i in $(seq "$SECS"); do sleep 1; echo "writers on, t+${i}s: $(Q 'INFO FOR INDEX ix ON t;')"; done
# stop: each loop finishes the request it is sending; wait until all of them have
touch "$D/stop"; wait $PIDS; PIDS=""
echo "writers done, +0s: $(Q 'INFO FOR INDEX ix ON t;')"
for i in $(seq 30); do sleep 1; echo "writers done, +${i}s: $(Q 'INFO FOR INDEX ix ON t;')"; doneOutput on 3.2.4 with SECS=120, trimmed to the building object:
writers on, t+1s: {"initial":38266,"pending":0,"status":"indexing"}
writers on, t+4s: {"initial":100000,"pending":8,"status":"indexing","updated":3642}
writers on, t+30s: {"initial":100000,"pending":0,"status":"indexing","updated":26279}
writers on, t+60s: {"initial":100000,"pending":0,"status":"indexing","updated":51465}
writers on, t+90s: {"initial":100000,"pending":0,"status":"indexing","updated":79276}
writers on, t+120s: {"initial":100000,"pending":21,"status":"indexing","updated":101682}
writers done, +0s: {"initial":100000,"pending":0,"status":"indexing","updated":102425}
writers done, +1s: {"initial":100000,"pending":0,"status":"ready","updated":102425}- Expected: the build reaches
status: "ready"while the writers are still running, some time after the initial scan completes (here by t+4 s). - Observed: the build stays
indexingfor the whole 120 s write period. It becomesreadywithin 1 s after the last writer request has finished.
The outcome varies with the number of writer loops and the write rate (see the table): with 4 writer loops, one run of five became ready under load; with 1 writer loop the build became ready after 5.2 s.
A longer version of the script is available if useful. It also counts the committed writes, measures the write rate and the drain after the stop (the probe above), can use the in-memory engine, and exits 0 when the bug reproduces.
Expected behaviour
A CONCURRENTLY build should reach ready in bounded time while writes continue, at least when the builder keeps up with them. For example, the build could move to Closing once the backlog is below a threshold, or after a bounded number of passes or amount of time, instead of waiting for a read that finds the queue empty. Closing already makes new writers wait while the last entries are replayed. In the drain probe, the build finished within 0.4 s of the last committed write, which suggests that this wait would be short.
If that is not possible, the documentation should say that a concurrent index build may not finish while the table receives writes, and that writes must pause for the build to complete.
SurrealDB version
3.2.4+20260803.93ab219 for linux on aarch64(Docker imagesurrealdb/surrealdb:v3.2.4)3.3.0 for linux on aarch64(Docker imagesurrealdb/surrealdb:v3.3.0; same digest aslatestwhen tested)
Single node, rocksdb: storage, Docker Desktop on macOS (arm64). Other containers were running on the same Docker VM during the runs, so the absolute write rates vary between runs.
Contact Details
No response
Could this be a bug with your application?
- I confirm this is a bug with SurrealDB, not with my own application
Is there an existing issue for this?
- I confirm that I have searched the existing issues
Related issues (none of them reports this):
- #7425: new indexed-field writes wait during
Closinguntil the build isOnline. That is what writers see after the handoff; this report is about the build not reaching the handoff while writes continue. - #7533: a blocking
DEFINE INDEXmissesUPDATEs committed during the build. Because of this, switching to a blocking build is not a safe workaround while writes continue. - #7391 (closed): an interrupted
CONCURRENTLYbuild is not resumed. - #6206: the status alternates between
indexingandready(2.x).
Code of Conduct
- I agree to follow this project's Code of Conduct
Source: surrealdb/surrealdb