Repository navigation
fix(jdbc): serialize LibsqlConnection state under one lock - #153
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #105
Contract. Hrana 3 is the source of truth for transport, values, metadata, batching, transactions, and error reporting: a baton is consumed once and one request may be in flight per stream.
java.sqlis the source of truth for the JDBC surface. xerial/sqlite-jdbc 3.53.4.0 is the behavioral reference, and this PR mirrors its serialization on theDBobject, not its code.Change
LibsqlConnectionnow has onestateLock:executeStatement,executeBatchResults, and the private begin/end/close-stream paths that callsendPipeline;setAutoCommit,commit,rollback,close;closed,autoCommit,readOnly,transactionIsolation, the cached metadata, andbaton/baseUrl.Lock ordering
The only two locks are the statement's
stateLockand the connection'sstateLock. The statement never holds its lock while calling into the connection:LibsqlStatement.checkClosed()releases its lock before callingLibsqlConnection.checkClosed(), and both execute paths call the connection afterbeginExecution()'s critical section exits. The connection never calls back into a statement. The only possible order is statement lock then connection lock, so it cannot invert and the two cannot deadlock. This is documented in theLibsqlConnectionclass javadoc and inbeginPending,beginTransaction,endTransaction,updateStreamState, andcloseStreamSilently.Red and green
Red at trunk
a79ed7dwith the new test file only (3/6 fail):Green at head
17aa994: 6/6 pass, and green on 5 consecutive runs.The tests use a
ProtocolClientthat models the protocol rules (one open stream, one request per stream, a baton consumed by its request) and records every violation instead of throwing, so each failure names the invalid interleaving. It widens the two windows with a 50 ms in-flight pause; the fixed connection blocks the racing thread on its lock instead.Lanes (all at head
17aa994)./mvnw -pl libsql-jdbc -am test -Dtest=LibsqlStatementConcurrencyTestat trunk and head/tmp/xerial-parity-i105/lane-1-regression.log/tmp/xerial-parity-i105/lane-2-specific.log/tmp/xerial-parity-i105/lane-3-specific.log./mvnw -pl libsql-jdbc test/tmp/xerial-parity-i105/lane-4-module-suite.log./mvnw -pl libsql-conformance -am verify/tmp/xerial-parity-i105/lane-5-conformance.log./mvnw -pl libsql-conformance -am -Pxerial-control verify/tmp/xerial-parity-i105/lane-6-control.log./mvnw spotless:check/tmp/xerial-parity-i105/lane-7-spotless.logDOCKER_CONFIG=$PWD/libsql-conformance/src/test/docker-config ./mvnw -pl libsql-itests -am verify/tmp/xerial-parity-i105/lane-8-itests.log./mvnw -pl libsql-conformance -am -Dit.test=PreparedStatementThreadTest verify/tmp/xerial-parity-i105/lane-9-specific.log./mvnw -pl libsql-jdbc -am package -DskipTests/tmp/xerial-parity-i105/lane-10-package.logPerf
Metric: the two-thread interleaved execute-plus-commit workload, three runs per side interleaved, plus the single-thread
libsql-jdbcsuite once per side./tmp/xerial-parity-i105/PerfProbe.java, run from the built classes of each side. Its client parks 200 µs per round trip, the order of a localhost HTTP+JSON round trip, so the number isolates lock overhead from serialization the protocol requires anyway. Log:/tmp/xerial-parity-i105/perf-probe.log.Divergences
DB; this serializes on one connection lock, which is the same contract.Base
a79ed7d, head17aa99459dd2a2be51ec6aa92e5e04a80418dd6a.