Commit Graph

8869 Commits

Author SHA1 Message Date
Pekka Enberg
877b28bcb3 perf/throughput/turso: Use 30 second busy timeout like in rusqlite 2025-09-15 13:57:58 +03:00
Pekka Enberg
380b27f58a Merge 'Busy handler' from Pedro Muniz
I searched using deepwiki how SQLite implements their busy handler. They
use a callback system with exponential backoff, where it stores the
callback in the pager and in the database. I confess I found this
slightly confusing, so I just implemented a simple exponential backoff
directly in the `Statement` struct. I imagine SQLite does this in a more
convoluted manner, as they do not have a concept of yielding as we do.
https://deepwiki.com/search/where-is-the-code-for-the-
busy_4a5ed006-4eed-479f-80c3-dd038832831b
I also fixed the rust bindings so that it yields when we return
`StepResult::IO`, instead of just blocking the async function. To
achieve this I implemented the `Stream` trait for `Rows` struct, which
unfortunately came with a slight change to the function signature of
`rows.next()` to `rows.try_next()`.
EDIT:
~test `test_multiple_connections_fuzz` timeouts because now it has the
busy handler "slowing" things down (this test generates a lot of busy
transactions), so it takes a lot longer for the test to run. Not sure if
it is acceptable for us to reduce the number of operations so the test
is shorter.~
EDIT:
Adjusted the API to be more in line with
https://www.sqlite.org/c3ref/busy_timeout.html.
Sets maximum total accumulated timeout. If the duration is None or Zero,
we unset the busy handler for this Connection.
This api defers slightly from SQLite as instead of sleeping for linear
amount of time specified by the user, we will sleep in phases until the
the total amount of time requested is reached. This means we first sleep
of 1ms, then if we still return busy, we sleep for 2 ms, and repeat
until a maximum of 100 ms per phase or we reached the total timeout.
Example:
1. Set duration to 5ms
2. Step through query -> returns Busy -> sleep/yield for 1 ms
3. Step through query -> returns Busy -> sleep/yield for 2 ms
4. Step through query -> returns Busy -> sleep/yield for 2 ms (totaling
5 ms of sleep)
5. Step through query -> returns Busy -> return Busy to user
This slight api change demonstrated a better throughtput in
`perf/throughput/turso` benchmark
```sh
cargo run -p write-throughput --release -- -t 2

Running write throughput benchmark with 2 threads, 100 batch size, 10 iterations, mode: Legacy
Database created at: write_throughput_test.db
Thread 1: 1000 inserts in 0.04s (23438.42 inserts/sec)
Thread 0: 1000 inserts in 0.08s (12385.64 inserts/sec)

=== BENCHMARK RESULTS ===
Total inserts: 2000
Total time: 0.08s
Overall throughput: 24762.60 inserts/sec
Threads: 2
Batch size: 100
Iterations per thread: 10
Database file exists: true
Database file size: 4096 bytes
```
Depends on #3102
Closes #3067

Closes #3074
2025-09-15 13:52:49 +03:00
Pekka Enberg
07c580aadf Merge 'mvcc: fix hang when CONCURRENT tx tries to commit and non-CONCURRENT tx is active' from Jussi Saurio
Based on #3125
Closes #3120

Closes #3126
2025-09-15 11:45:30 +03:00
Pekka Enberg
54c79b879b Merge 'mvcc: fix two sources of panic' from Jussi Saurio
1. commit state machine was assuming that begin_write_tx() cannot fail,
but it can fail if there is another tx that is not using BEGIN
CONCURRENT.
2. if a brand new non-CONCURRENT transaction attempts to start exclusive
transaction but fails with Busy, we must end the read pager read tx it
just started, because otherwise the next time it attempts to do
something it will panic with: `"cannot start a new read tx without
ending an existing one"`

Closes #3125
2025-09-15 11:26:05 +03:00
Jussi Saurio
aa7a853cd2 mvcc: fix hang when CONCURRENT tx tries to commit and non-CONCURRENT tx is active 2025-09-15 11:09:19 +03:00
Jussi Saurio
9234ef86ae mvcc: fix two sources of panic
1. commit state machine was assuming that begin_write_tx() cannot
fail, but it can fail if there is another tx that is not using
BEGIN CONCURRENT.

2. if a brand new non-CONCURRENT transaction attempts to start
exclusive transaction but fails with Busy, we must end the read
pager read tx it just started, because otherwise the next time
it attempts to do something it will panic with:

"cannot start a new read tx without ending an existing one"
2025-09-15 10:59:44 +03:00
Pekka Enberg
eb3f17a0a9 Merge 'Fix MVCC rollback' from Jussi Saurio
Closes #3119
Closes #3121
executing ROLLBACK did not rollback the mv-store transaction

Closes #3123
2025-09-15 10:05:59 +03:00
Jussi Saurio
8f43741513 fix mvcc rollback
executing ROLLBACK did not rollback the mv-store transaction
2025-09-15 09:29:08 +03:00
Jussi Saurio
0e7eecc7a1 Merge 'test/fuzz: improve maintainability/usability of tx isolation test' from Jussi Saurio
**test/fuzz: introduce fuzzoptions to tx isolation test**
this makes it significantly easier to tweak the tx isolation test
parameters, and also makes it much easier to run the MVCC version of the
test without manually tweaking code inline to make it work.
introduces default options for the non-mvcc and mvcc test variants.
---
**test/fuzz: improve error handling in tx isolation fuzz test**
- extract out common behavior for checking acceptable errors
- add functionality to check which errors require rolling back
  a transaction

Closes #3118
2025-09-15 09:21:49 +03:00
pedrocarlo
bd5dcd8d3c add timeout flag to throughput benchmark 2025-09-15 02:20:32 -03:00
pedrocarlo
3d265489dc modify semantics of busy_timeout to be more on par with sqlite 2025-09-15 02:20:32 -03:00
pedrocarlo
0586b75fbe expose function to set busy timeout duration 2025-09-15 02:20:32 -03:00
Pekka Enberg
246799c603 Fix simulator and Antithesis Docker images 2025-09-15 08:16:38 +03:00
pedrocarlo
16e79ed508 slight adjustment in perf throughtput printing 2025-09-15 02:16:18 -03:00
pedrocarlo
a56680f79e implement Busy Handler in Turso statements 2025-09-15 02:16:18 -03:00
Jussi Saurio
f2079d8f07 test/fuzz: improve error handling in tx isolation fuzz test
- extract out common behavior for checking acceptable errors
- add functionality to check which errors require rolling back
  a transaction
2025-09-15 08:03:08 +03:00
Jussi Saurio
1c5febf047 test/fuzz: introduce fuzzoptions to tx isolation test
this makes it significantly easier to tweak the tx isolation test parameters,
and also makes it much easier to run the MVCC version of the test without
manually tweaking code inline to make it work.

introduces default options for the non-mvcc and mvcc test variants.
2025-09-15 07:44:16 +03:00
Pekka Enberg
24c50597ad Merge 'Random fixes for MVCC' from Jussi Saurio
based on #3110
closes #3111
closes #3113
closes #3114
all discovered using `test_multiple_connections_fuzz_mvcc`, so no
separate tests. i can add regression unit/integration tests tomorrow
    mvcc: properly remove mutations of rolled back tx
    mvstore was not removing deletions made by a tx that rolled back.
    deletions are removed by clearing the `end` mark from the row
    version.
---
    mvcc: properly clear tx states when mvcc tx rolls back
---
    mvcc: don't double-rollback on write-write-conflict
    handle_program_error() already rolls back if this error happens.
    double rollback causes a crash.

Closes #3115
2025-09-15 07:39:35 +03:00
Pekka Enberg
27d4572c2e Merge 'mvcc: Complete commit state machine early if write set is empty' from Jussi Saurio
Closes #3104
2025-09-15 07:38:28 +03:00
Jussi Saurio
f4c15a37d3 add manual hack to mvcc test
we rollback the mvcc transaction in the VDBE, so manually roll it
back in the test
2025-09-14 23:46:38 +03:00
Jussi Saurio
db3428a7a9 remove unused pager parameter 2025-09-14 23:44:24 +03:00
Jussi Saurio
d598775e33 mvcc: properly remove mutations of rolled back tx
mvstore was not removing deletions made by a tx that rolled back.
deletions are removed by clearing the `end` mark from the row
version.
2025-09-14 23:29:14 +03:00
Jussi Saurio
dccf8b9472 mvcc: properly clear tx states when mvcc tx rolls back 2025-09-14 23:29:07 +03:00
Jussi Saurio
487b8710d9 mvcc: don't double-rollback on write-write-conflict
handle_program_error() already rolls back if this error happens.
double rollback causes a crash.
2025-09-14 23:28:21 +03:00
Jussi Saurio
8510721c5e Merge 'add perf/throughput/turso to workspace' from Pedro Muniz
Closes #3102
2025-09-14 22:30:49 +03:00
Jussi Saurio
2ca1640a2a not always write 2025-09-14 22:24:07 +03:00
pedrocarlo
01a99f84a6 add perf/throughput/turso to workspace 2025-09-14 16:19:34 -03:00
Jussi Saurio
396091044e store tx_mode in conn.mv_tx
otherwise op_transaction works completely wrong because each separate
insert statement overrides the tx_mode to Write
2025-09-14 21:59:08 +03:00
Jussi Saurio
7fe25a1d0e mvcc: remove conn.mv_transactions
afaict this isn't needed for anything since there is already
conn.mv_tx_id
2025-09-14 21:26:58 +03:00
Jussi Saurio
5feb9ea2f0 mvcc: fix non-concurrent transaction semantics
on the main branch, mvcc allows concurrent inserts from multiple
txns even without BEGIN CONCURRENT, and then always hangs whenever
one of the txns tries to commit.

this commit fixes that issue.
2025-09-14 21:23:06 +03:00
Jussi Saurio
2ea1798d6e mvcc: end commit state machine early when write set is empty 2025-09-14 20:02:35 +03:00
Pekka Enberg
bc4aa63203 Merge 'make whopper run with checksums' from Avinash Sajjanshetty
pass `--enable-checksums` to either `run` or `explore` commands

Closes #3100
2025-09-14 18:26:12 +03:00
Pekka Enberg
76a3c20a48 Merge 'perf/throughput/turso: Don't use spawn_blocking()' from Pekka Enberg
Let's just use normal spawn()

Reviewed-by: Preston Thorpe <preston@turso.tech>

Closes #3096
2025-09-14 17:55:44 +03:00
Pekka Enberg
3273d78969 Merge 'Whopper + MVCC' from Pekka Enberg
Reviewed-by: Avinash Sajjanshetty (@avinassh)

Closes #3097
2025-09-14 17:55:25 +03:00
Avinash Sajjanshetty
d35789690e make whopper run with checksums
pass `--enable-checksums` to either `run` or `explore` commands
2025-09-14 20:08:34 +05:30
Pekka Enberg
1bdfabcac7 whopper: Generate different transaction modes with MVCC 2025-09-14 13:55:08 +03:00
Pekka Enberg
5783e3edf8 whopper: Add "--enable-mvcc" command line option 2025-09-14 13:15:52 +03:00
Pekka Enberg
db190c9e3d whopper: Add "--disable-indexes" option
MVCC does not support indexes so let's add an option to disable that.
2025-09-14 13:13:49 +03:00
Pekka Enberg
95660535da core/storage: Demote info logging to debug 2025-09-14 13:10:46 +03:00
Pekka Enberg
a5f1bdbc9d Merge 'perf/throughput/turso: Don't interleave concurrent transactions in on…' from Pekka Enberg
…e connection
With BEGIN CONCURRENT, we cannot use the same connection all the time
because there's no way for the transaction manager to know which
transaction belongs to what "session" -- they're all individual
statements executed in the context of the one connection.
Fixes #3093

Reviewed-by: Jussi Saurio <jussi.saurio@gmail.com>

Closes #3094
2025-09-14 11:33:20 +03:00
Pekka Enberg
ed8b02d83e perf/throughput/turso: Don't use spawn_blocking()
Let's just use normal spawn()
2025-09-14 11:32:17 +03:00
Pekka Enberg
222b9e7ca2 Merge 'perf/throughput/turso: Don't execute futures serially' from Pekka Enberg
Looping through the futures means we're essentially executing them
serially.

Reviewed-by: Jussi Saurio <jussi.saurio@gmail.com>

Closes #3092
2025-09-14 11:01:06 +03:00
Pekka Enberg
6e9ba4c561 perf/throughput/turso: Don't interleave concurrent transactions in one connection
With BEGIN CONCURRENT, we cannot use the same connection all the time
because there's no way for the transaction manager to know which
transaction belongs to what "session" -- they're all individual
statements executed in the context of the one connection.

Fixes #3093
2025-09-14 10:52:56 +03:00
Pekka Enberg
98af9386e2 perf/throughput/turso: Don't execute futures serially
Looping through the futures means we're essentially executing them
serially.
2025-09-14 10:07:21 +03:00
Preston Thorpe
4e4f0ea61f Merge 'Dont grab page cache write lock in a loop' from Preston Thorpe
durp

Reviewed-by: Jussi Saurio <jussi.saurio@gmail.com>

Closes #3088
2025-09-13 12:48:19 -04:00
PThorpe92
f6dd0bc4d6 Dont grab page cache write lock in a loop 2025-09-13 12:21:13 -04:00
Pekka Enberg
6a2f0d6061 Merge 'Add per page checksums' from Avinash Sajjanshetty
This patch adds checksums to Turso DB. You may check the design here in
the [RFC](https://github.com/tursodatabase/turso/issues/2178).
1. We use reserved bytes (8 bytes) to store the checksums. On every IO
read, we verify that the checksum matches.
2. We use twox hash for checksums.
3. Checksum works only on 4K pages now. It's a small change to enable
for all other sizes, I will send another PR.
4. Right now, it's not possible to switch to different algorithm or turn
off altogether. That will be added in the future PRs.
5. Checksums can be enabled only for new dbs. For existing DBs, we will
disable it.
6. To add checksums for existing DBs, we need vacuum since it would
require rewrite of whole db.

Closes #2840
2025-09-13 18:46:53 +03:00
Pekka Enberg
7f5038f7c9 Merge 'perf/throughput/turso: Async transactions with concurrent mode' from Pekka Enberg
With `BEGIN CONCURRENT`, we should also take advantage of async
transaction processing to maximize concurrency.

Closes #3082
2025-09-13 15:07:29 +03:00
Pekka Enberg
7d3ce68695 Merge 'core/throughput: Add per transaction think time support' from Pekka Enberg
Closes #3080
2025-09-13 14:07:30 +03:00
Pekka Enberg
898f32f7f7 Fix Antithesis Dockerfile to include whopper 2025-09-13 13:33:11 +03:00