skaidb 1.4.1 → 1.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +131 -0
- data/README.md +143 -28
- data/docs/api.md +113 -15
- data/docs/getting-started.md +19 -1
- data/docs/pooling.md +17 -1
- data/docs/streaming.md +21 -4
- data/lib/skaidb.rb +531 -82
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2b6ffb8cdd67d36ca6fabbb531c62d8c9285ef1b4d9b5a5e74644d0e0078a6e7
|
|
4
|
+
data.tar.gz: be9737657d133eaab3011ba3a6349de7fa253efb805350a281358eab03c9fcac
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9bde166e85122d8b47d8ddaa201d13c7bb837c864527a62f6e2410ef5a5e8fb0f6b9a5be947a488bc2084ff7e41130acb3daad1d18b71bb66b47f63887c4538a
|
|
7
|
+
data.tar.gz: 02e0836e7f27f9cc7cb18c0b04f09ac26e5829908c38571be9fab9ce6886558c014e98a4df20ffbcc612df54097886fdc4989a3084962358fbce1cffe166676b
|
data/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,135 @@ All notable changes to the skaidb Ruby driver. The format follows
|
|
|
4
4
|
[Keep a Changelog](https://keepachangelog.com/en/1.1.0/); versions follow
|
|
5
5
|
[Semantic Versioning](https://semver.org/).
|
|
6
6
|
|
|
7
|
+
## [1.5.1] - 2026-11-16
|
|
8
|
+
|
|
9
|
+
### Fixed
|
|
10
|
+
- **A connection marked broken closes its socket at once.** A connection
|
|
11
|
+
whose transport failed, or that was left in the middle of a reply
|
|
12
|
+
(`Timeout.timeout`, an `Interrupt`, `Thread#kill`), kept its socket open
|
|
13
|
+
until its next statement re-dialled or it was closed. The server keeps a
|
|
14
|
+
session for as long as its socket is open, so a statement that timed out
|
|
15
|
+
inside a transaction left that transaction open on the server until the
|
|
16
|
+
application came back to the connection. The socket is now closed when
|
|
17
|
+
the connection is marked broken.
|
|
18
|
+
- **A stream the `transaction` block leaves open no longer leaves a
|
|
19
|
+
transaction to join.** A block that returned or raised with an Enumerator
|
|
20
|
+
from `stream` advanced by `next` and not read to its end left `COMMIT` and
|
|
21
|
+
`ROLLBACK` unable to be sent (`COMMIT` raised `ThreadError`), and the
|
|
22
|
+
transaction open; once the Enumerator was read to its end, every later
|
|
23
|
+
statement on the connection ran inside that transaction and was lost
|
|
24
|
+
with it. The helper now closes the socket — the server drops the
|
|
25
|
+
transaction — and ends the stream, so the next statement re-dials; a
|
|
26
|
+
block that ran to its end gets `ConnectionError`, one that was leaving
|
|
27
|
+
with an exception, `break` or `throw` leaves with that.
|
|
28
|
+
- **The session database survives a re-dial when it was entered with a
|
|
29
|
+
`USE` statement.** Only `database:` was re-entered; after `USE app` sent
|
|
30
|
+
with `exec`, a re-dialled connection was in the default database, and its
|
|
31
|
+
statements ran against that database's tables of the same names. The
|
|
32
|
+
connection now follows the last `USE [DATABASE] <name>` the server
|
|
33
|
+
accepted — written with a `--` comment or without — and re-enters it on
|
|
34
|
+
every dial before anything else is sent; if the server refuses, the
|
|
35
|
+
statement raises that error instead of running elsewhere. A pool with
|
|
36
|
+
`database:` puts a connection a borrower moved with `USE` back in that
|
|
37
|
+
database before it hands it out again.
|
|
38
|
+
- **Any number of distinct parameterised statements on one connection.**
|
|
39
|
+
The server holds 256 prepared statements per connection, and the driver
|
|
40
|
+
never closed one: from the 257th distinct statement on, the prepare was
|
|
41
|
+
refused and `exec_params` silently sent the statement as text with
|
|
42
|
+
client-side literals — a `Time` stored as a number, bytes as a hex
|
|
43
|
+
string — or failed for an Array or Hash parameter. The cache now closes
|
|
44
|
+
its least recently used statement on the server before it would pass 240,
|
|
45
|
+
and a prepare refused with `too many prepared statements` raises
|
|
46
|
+
`QueryError` instead of falling back.
|
|
47
|
+
- **An id from `prepare_server` never runs another statement.** An id held
|
|
48
|
+
across a re-dial was sent to the new socket, where the server did not
|
|
49
|
+
know it or, worse, had given the number to a different statement since.
|
|
50
|
+
An id now carries the socket that issued it: `exec_prepared` prepares the
|
|
51
|
+
statement again on the current socket when the connection has re-dialled
|
|
52
|
+
(re-dialling first if it is broken, where it used to raise `connection is
|
|
53
|
+
broken; prepare the statement again`), and raises `QueryError` for an id
|
|
54
|
+
another connection issued. Ids are larger numbers than before; statements
|
|
55
|
+
handed out by `prepare_server` are never closed by the driver's cache.
|
|
56
|
+
- **A stream Enumerator dropped half-read no longer crashes the process.**
|
|
57
|
+
`conn.stream(sql).next` runs the stream in the Enumerator's Fiber, inside
|
|
58
|
+
the connection's lock. Dropped there, the Fiber was collected still
|
|
59
|
+
owning the lock, and Ruby (3.3.8 measured) crashed when the lock was
|
|
60
|
+
freed after it: a segfault or an abort at a later garbage collection or
|
|
61
|
+
at exit, or a spin at exit. The driver now keeps the lock of a stream in
|
|
62
|
+
flight alive until the stream ends. The connection of a stream left that
|
|
63
|
+
way is still lost — read the Enumerator to its end to keep it.
|
|
64
|
+
- **`Timeout.timeout` around a stream times out.** A stream cut short while
|
|
65
|
+
it waited for a frame (a `Timeout`, an `Interrupt`, `Thread#kill`) went on
|
|
66
|
+
to drain the rest of the result before the exception could propagate —
|
|
67
|
+
for as long as the server took, or for ever — and could read on from the
|
|
68
|
+
middle of a frame. It now marks the connection broken and raises at once.
|
|
69
|
+
- A drain cut short the same way (a stream whose block breaks out, under a
|
|
70
|
+
timeout) left the rest of the stream on a connection that was not marked
|
|
71
|
+
broken: the next statements read the leftover chunks, and after them each
|
|
72
|
+
statement got the reply of an earlier one. The connection is now marked
|
|
73
|
+
broken.
|
|
74
|
+
- A re-dial cut short the same way left a half-made connection whose next
|
|
75
|
+
statement read the handshake's or `Hello`'s reply as its own. It is now
|
|
76
|
+
marked broken and dialled again.
|
|
77
|
+
- A re-dial that fails because no member is reachable no longer closes the
|
|
78
|
+
connection for good: the next statement dials again, as documented.
|
|
79
|
+
|
|
80
|
+
## [1.5.0] - 2026-11-15
|
|
81
|
+
|
|
82
|
+
### Added
|
|
83
|
+
- **`Skaidb::TransactionConflict`**, a `QueryError` raised for a server
|
|
84
|
+
error whose message starts with `transaction conflict:` — what a
|
|
85
|
+
standalone server (≥ 0.346) answers a `COMMIT` with when another writer
|
|
86
|
+
changed a row the transaction writes after its `BEGIN`. Nothing was
|
|
87
|
+
applied, the transaction is over and the connection stays usable; the
|
|
88
|
+
message is the server's, unchanged. Every call that runs a statement
|
|
89
|
+
raises it (`exec`, `exec_params`, `exec_prepared`, `exec_batch`,
|
|
90
|
+
`stream`), and `rescue Skaidb::QueryError` catches it as before. It is a
|
|
91
|
+
statement error: it does not count against a pool member's health, close
|
|
92
|
+
the connection or cause a re-dial.
|
|
93
|
+
- **`conn.transaction(attempts: 3, begin: "BEGIN") { |conn| … }`** sends
|
|
94
|
+
`begin`, runs the block, sends `COMMIT` and returns the block's value. A
|
|
95
|
+
`COMMIT` refused with `TransactionConflict` runs the whole transaction
|
|
96
|
+
again from `begin` — up to `attempts` runs in all, with no pause between
|
|
97
|
+
them — so the block must be safe to run more than once; the conflict of
|
|
98
|
+
the last attempt is raised. A refused `begin` is raised without running
|
|
99
|
+
the block or sending anything else. A block that does not run to its end
|
|
100
|
+
(an exception, `break`, `throw`) is rolled back and never retried, and
|
|
101
|
+
what the `ROLLBACK` answers never replaces the block's exception. Any
|
|
102
|
+
other `COMMIT` error is raised with no retry and no `ROLLBACK`. `begin:`
|
|
103
|
+
is the opening statement, sent as given (`BEGIN ATOMIC` or
|
|
104
|
+
`BEGIN ATOMIC PARTITION (col = lit)` on a cluster). `attempts` below 1,
|
|
105
|
+
or no block, raises `ArgumentError` before anything is sent.
|
|
106
|
+
- Inside `transaction` a connection that loses its transport does not
|
|
107
|
+
re-dial: the `ConnectionError` is not retried, no `ROLLBACK` is sent to a
|
|
108
|
+
new session, and a statement the block still runs after rescuing the
|
|
109
|
+
failure raises `ConnectionError` instead of being committed on its own
|
|
110
|
+
there. The first statement after `transaction` returns re-dials as always.
|
|
111
|
+
- **`pool.transaction(attempts: 3, begin: "BEGIN", affinity: nil) { |conn| … }`**:
|
|
112
|
+
the same helper on one checked-out connection, held for every attempt and
|
|
113
|
+
returned afterwards.
|
|
114
|
+
|
|
115
|
+
### Fixed
|
|
116
|
+
- A statement cut short between its request and its reply by something
|
|
117
|
+
other than a transport error — `Timeout.timeout`, an `Interrupt`,
|
|
118
|
+
`Thread#kill` — left the reply on the socket with `usable?` still `true`,
|
|
119
|
+
and the next statement on that connection read it as its own: a
|
|
120
|
+
`SELECT 1` returned the rows of the statement that had timed out. The
|
|
121
|
+
connection is now marked broken at that point, so `usable?` is `false`, a
|
|
122
|
+
pool closes it on check-in and the next statement re-dials. `exec`,
|
|
123
|
+
`exec_params`, `exec_batch` and the first frame of a `stream` are covered.
|
|
124
|
+
`transaction` relies on it: a block cut short that way is not followed by
|
|
125
|
+
a `ROLLBACK` that would wait for the outstanding reply.
|
|
126
|
+
- A broken connection is re-dialled by whichever statement comes next. A
|
|
127
|
+
parameterised statement the connection had already prepared
|
|
128
|
+
(`exec_params`, `exec_batch`) took its id from the cache before that
|
|
129
|
+
check and sent it down the old socket, so a connection that ran only
|
|
130
|
+
such statements raised `ConnectionError` on every call after a transport
|
|
131
|
+
failure and never recovered. The cache is now read after the re-dial,
|
|
132
|
+
which empties it. `exec_prepared` with an id from before the failure
|
|
133
|
+
raises `ConnectionError` (`connection is broken; prepare the statement
|
|
134
|
+
again`).
|
|
135
|
+
|
|
7
136
|
## [1.4.1] - 2026-10-31
|
|
8
137
|
|
|
9
138
|
### Fixed
|
|
@@ -224,6 +353,8 @@ batches, a 2 500-row stream, error handling, and the `drivers` table showing
|
|
|
224
353
|
because it is a bundled gem since Ruby 3.4.
|
|
225
354
|
- The example moved to `examples/basic.rb`.
|
|
226
355
|
|
|
356
|
+
[1.5.1]: https://github.com/porcupin26/skaidb-ruby/compare/v1.5.0...v1.5.1
|
|
357
|
+
[1.5.0]: https://github.com/porcupin26/skaidb-ruby/compare/v1.4.1...v1.5.0
|
|
227
358
|
[1.4.1]: https://github.com/porcupin26/skaidb-ruby/compare/v1.4.0...v1.4.1
|
|
228
359
|
[1.4.0]: https://github.com/porcupin26/skaidb-ruby/compare/v1.3.0...v1.4.0
|
|
229
360
|
[1.3.0]: https://github.com/porcupin26/skaidb-ruby/compare/v1.2.0...v1.3.0
|
data/README.md
CHANGED
|
@@ -12,9 +12,9 @@ and the result behaves like `PG::Result`. **Pure standard library** (`socket`,
|
|
|
12
12
|
`openssl`, `securerandom`, `bigdecimal`) — no third-party code, one file,
|
|
13
13
|
Ruby 2.7 through 3.4. It speaks skaidb's binary protocol directly:
|
|
14
14
|
SCRAM-SHA-256 authentication, server-side prepared statements with typed
|
|
15
|
-
parameters, one-round-trip batches, streamed result sets,
|
|
16
|
-
transparent reconnect, TLS and a
|
|
17
|
-
cluster's members.
|
|
15
|
+
parameters, one-round-trip batches, streamed result sets, a retrying
|
|
16
|
+
transaction helper, multi-seed failover, transparent reconnect, TLS and a
|
|
17
|
+
connection pool that balances across the cluster's members.
|
|
18
18
|
|
|
19
19
|
- Repository: <https://github.com/porcupin26/skaidb-ruby>
|
|
20
20
|
- RubyGems: <https://rubygems.org/gems/skaidb>
|
|
@@ -55,13 +55,13 @@ Xcode command-line tools on macOS). `gem install skaidb` and vendoring reuse
|
|
|
55
55
|
the `bigdecimal` already installed.
|
|
56
56
|
|
|
57
57
|
Or vendor the single file: copy
|
|
58
|
-
[`lib/skaidb.rb`](https://github.com/porcupin26/skaidb-ruby/blob/v1.
|
|
58
|
+
[`lib/skaidb.rb`](https://github.com/porcupin26/skaidb-ruby/blob/v1.5.1/lib/skaidb.rb)
|
|
59
59
|
into your project and `require_relative` it. It has no dependencies beyond
|
|
60
60
|
Ruby's standard library.
|
|
61
61
|
|
|
62
62
|
Every release is also attached as a `.gem` to its
|
|
63
63
|
[GitHub release](https://github.com/porcupin26/skaidb-ruby/releases) for
|
|
64
|
-
installs without registry access: `gem install ./skaidb-1.
|
|
64
|
+
installs without registry access: `gem install ./skaidb-1.5.1.gem`.
|
|
65
65
|
|
|
66
66
|
## Quick start
|
|
67
67
|
|
|
@@ -104,7 +104,8 @@ end
|
|
|
104
104
|
- [Streams (`CREATE STREAM`)](#streams-create-stream) — `subscribe`
|
|
105
105
|
- [Type mapping](#type-mapping)
|
|
106
106
|
- [Errors](#errors)
|
|
107
|
-
- [Transactions](#transactions)
|
|
107
|
+
- [Transactions](#transactions) — `transaction`, `TransactionConflict`,
|
|
108
|
+
hand-written `BEGIN`/`COMMIT`
|
|
108
109
|
- [Thread safety](#thread-safety)
|
|
109
110
|
- [Client identification and version](#client-identification-and-version)
|
|
110
111
|
- [Compatibility](#compatibility)
|
|
@@ -145,17 +146,27 @@ conn = Skaidb.connect(seeds: ["db1", "db2:7000", "db3"], database: "app")
|
|
|
145
146
|
Each seed is `"host"` or `"host:port"` (the port after the last colon wins; a
|
|
146
147
|
seed without a port uses `port:`). When a connection's transport dies, the
|
|
147
148
|
**next statement re-dials** across the same seeds, re-authenticates, re-sends
|
|
148
|
-
`Hello`, re-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
149
|
+
`Hello`, re-enters the session's database, and drops the connection's prepared
|
|
150
|
+
statements. The statement that discovered the loss raises
|
|
151
|
+
`Skaidb::ConnectionError`; the one after it goes through.
|
|
152
|
+
|
|
153
|
+
The database re-entered is the one the session was last in: `database:`, or
|
|
154
|
+
the one a later `USE <name>` statement sent with `exec` moved it to. It is
|
|
155
|
+
entered before anything else is sent on the new socket, and if the server
|
|
156
|
+
refuses it the statement raises that error instead of running in another
|
|
157
|
+
database.
|
|
152
158
|
|
|
153
159
|
### Timeout
|
|
154
160
|
|
|
155
161
|
`timeout` bounds the TCP dial only. Reads have no deadline: a peer that is
|
|
156
162
|
alive but silent blocks the caller, as in ruby-pg. `close` deliberately does
|
|
157
163
|
not take the connection's lock, so another thread can break a stuck read by
|
|
158
|
-
closing the connection.
|
|
164
|
+
closing the connection. A statement cut short while its reply is outstanding
|
|
165
|
+
— by `Timeout.timeout`, an `Interrupt`, `Thread#kill` — leaves the connection
|
|
166
|
+
marked broken, so the next statement re-dials instead of reading that reply.
|
|
167
|
+
A connection marked broken closes its socket there and then: the server
|
|
168
|
+
drops the session, and a transaction open on it, without waiting for that
|
|
169
|
+
next statement.
|
|
159
170
|
|
|
160
171
|
### TLS
|
|
161
172
|
|
|
@@ -202,8 +213,8 @@ the server regardless of this setting.
|
|
|
202
213
|
### Health
|
|
203
214
|
|
|
204
215
|
- `conn.usable?` — no round-trip. `true` unless the connection is closed, was
|
|
205
|
-
left out of sync by a transport error
|
|
206
|
-
is currently mid-stream.
|
|
216
|
+
left out of sync by a transport error, a round trip cut short or an
|
|
217
|
+
undrainable abandoned stream, or is currently mid-stream.
|
|
207
218
|
- `conn.closed` / `conn.finished?` — `true` after `close`.
|
|
208
219
|
|
|
209
220
|
## Statements and parameters
|
|
@@ -232,9 +243,12 @@ conn.exec_params("INSERT INTO docs (id, meta) VALUES ($1, $2)",
|
|
|
232
243
|
conn.exec_params("SELECT id FROM users WHERE id IN ($1)", [[1, 2, 3]]) # set membership
|
|
233
244
|
```
|
|
234
245
|
|
|
235
|
-
Prepared statements are cached per connection
|
|
236
|
-
|
|
237
|
-
|
|
246
|
+
Prepared statements are cached per connection and reused. The server holds
|
|
247
|
+
at most 256 per connection, so the cache keeps to 240: before a new statement
|
|
248
|
+
would exceed that, the least recently used one is closed on the server. A
|
|
249
|
+
connection can therefore run any number of distinct parameterised statements,
|
|
250
|
+
every one with typed values. The cache is dropped when the connection
|
|
251
|
+
re-dials.
|
|
238
252
|
|
|
239
253
|
Statement kinds the server refuses to prepare (DDL and session control such
|
|
240
254
|
as `USE`) fall back to **client-side text binding** for scalar parameters: a
|
|
@@ -318,8 +332,9 @@ form — drains the frames the server is still sending so the socket sits at a
|
|
|
318
332
|
request boundary again. Draining transfers the rest of the result: if you only
|
|
319
333
|
want the first rows, say so with `LIMIT`. External iteration (`next`/`peek`)
|
|
320
334
|
on the Enumerator form cannot unwind and leaves the connection marked busy;
|
|
321
|
-
iterate with a block or `each`. A frame that makes no sense mid-stream,
|
|
322
|
-
dead socket,
|
|
335
|
+
iterate with a block or `each`. A frame that makes no sense mid-stream, a
|
|
336
|
+
dead socket, or a stream cut short while it waits for a frame (a `Timeout`,
|
|
337
|
+
an `Interrupt`) marks the connection broken instead; `usable?` turns `false`
|
|
323
338
|
and the next statement re-dials. Details: [docs/streaming.md](https://github.com/porcupin26/skaidb-ruby/blob/main/docs/streaming.md).
|
|
324
339
|
|
|
325
340
|
## Connection pool
|
|
@@ -429,24 +444,124 @@ StandardError
|
|
|
429
444
|
└── Skaidb::Error # also: invalid consistency, pool closed
|
|
430
445
|
├── Skaidb::ConnectionError # dial/auth/transport failures, framing, closed connection
|
|
431
446
|
└── Skaidb::QueryError # the server rejected the statement; bad parameters
|
|
447
|
+
└── Skaidb::TransactionConflict # a COMMIT refused: another writer changed a row it writes
|
|
432
448
|
```
|
|
433
449
|
|
|
434
450
|
- A server `Error` frame is a **statement** error (`QueryError`); the
|
|
435
451
|
connection stays usable.
|
|
436
|
-
- A
|
|
437
|
-
|
|
452
|
+
- A server error whose message starts with `transaction conflict:` is a
|
|
453
|
+
`TransactionConflict`, from every call that runs a statement. It is a
|
|
454
|
+
`QueryError`, so `rescue Skaidb::QueryError` catches it too; see
|
|
455
|
+
[Transactions](#transactions).
|
|
456
|
+
- A transport failure (`ConnectionError`) marks the connection broken and
|
|
457
|
+
closes its socket; the next statement re-dials, or a pool replaces it. So
|
|
458
|
+
does anything else that cuts a statement short between its request and its
|
|
459
|
+
reply (an `Interrupt`, a `Timeout`); that exception propagates as it is.
|
|
438
460
|
- `ArgumentError` for `Pool.new(maxsize: 0)` or an unknown
|
|
439
461
|
`load_balancing`/`idle_order`, for `Skaidb::Uuid.new` of a string that
|
|
440
|
-
is not a UUID,
|
|
462
|
+
is not a UUID, for `stream` parameters that are not an Array, and for
|
|
463
|
+
`transaction` with `attempts` below 1 or without a block.
|
|
441
464
|
|
|
442
465
|
## Transactions
|
|
443
466
|
|
|
444
|
-
skaidb autocommits every statement
|
|
445
|
-
|
|
446
|
-
`
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
467
|
+
skaidb autocommits every statement outside a transaction. A transaction is
|
|
468
|
+
ordinary statements on **one connection**: an opening statement, the
|
|
469
|
+
statements, then `COMMIT` or `ROLLBACK`. `conn.transaction` does that around
|
|
470
|
+
a block and returns the block's value:
|
|
471
|
+
|
|
472
|
+
```ruby
|
|
473
|
+
conn.transaction do |c|
|
|
474
|
+
c.exec_params("UPDATE accounts SET balance = balance - $1 WHERE id = $2", [10, 1])
|
|
475
|
+
c.exec_params("UPDATE accounts SET balance = balance + $1 WHERE id = $2", [10, 2])
|
|
476
|
+
end
|
|
477
|
+
|
|
478
|
+
conn.transaction(attempts: 5, begin: "BEGIN ATOMIC") { |c| … }
|
|
479
|
+
pool.transaction { |c| … } # one checked-out connection for all of it
|
|
480
|
+
```
|
|
481
|
+
|
|
482
|
+
`begin:` is the opening statement, sent as given: `"BEGIN"` (the default) on
|
|
483
|
+
a standalone server; a cluster takes `BEGIN ATOMIC` or
|
|
484
|
+
`BEGIN ATOMIC PARTITION (col = lit)`. The rest is the same on both.
|
|
485
|
+
|
|
486
|
+
**Conflicts.** A standalone server (≥ 0.346) refuses a `COMMIT` when another
|
|
487
|
+
writer changed a row the transaction writes after its `BEGIN`. Nothing was
|
|
488
|
+
applied, the transaction is over, and the connection is back in autocommit
|
|
489
|
+
and stays usable. The driver raises `Skaidb::TransactionConflict` for it — a
|
|
490
|
+
`QueryError` carrying the server's message, recognised by its
|
|
491
|
+
`transaction conflict:` prefix, from `exec` and every other call as well as
|
|
492
|
+
from the helper. Running the whole transaction again from its opening
|
|
493
|
+
statement is safe, and that is what `transaction` does: up to `attempts` runs
|
|
494
|
+
in all (default 3), with no pause between them — the other writer has
|
|
495
|
+
already finished, and the next run reads the rows as they are now. So **the
|
|
496
|
+
block must be safe to run more than once**, and should do nothing outside the
|
|
497
|
+
database that a repeat would do twice. The conflict of the last attempt is
|
|
498
|
+
raised.
|
|
499
|
+
|
|
500
|
+
What else `transaction` does:
|
|
501
|
+
|
|
502
|
+
- The opening statement is refused: its error is raised, the block never
|
|
503
|
+
runs and nothing else is sent.
|
|
504
|
+
- The block does not run to its end (an exception, `break`, `throw`):
|
|
505
|
+
`ROLLBACK` is sent, and the block's exception propagates unchanged
|
|
506
|
+
whatever the `ROLLBACK` itself answers. Nothing is retried, a
|
|
507
|
+
`TransactionConflict` raised inside the block included.
|
|
508
|
+
- Any other `COMMIT` error is raised as it is, with no retry and no
|
|
509
|
+
`ROLLBACK`: a `COMMIT` that fails has ended the transaction.
|
|
510
|
+
- A transport failure is the usual `ConnectionError` and is not retried. The
|
|
511
|
+
transaction lived on the socket that died, so until `transaction` returns
|
|
512
|
+
the connection does not re-dial: no `ROLLBACK` is sent, and a statement the
|
|
513
|
+
block still runs after rescuing the failure raises `ConnectionError`
|
|
514
|
+
instead of running on a new session. The same goes for a block cut short
|
|
515
|
+
in the middle of a round trip (an `Interrupt`, a `Timeout`): the reply is
|
|
516
|
+
still on its way, so nothing more is written to that socket. Either way
|
|
517
|
+
the socket is closed at once, and the server drops the transaction with
|
|
518
|
+
it.
|
|
519
|
+
- A stream the block leaves open — an Enumerator from `stream` advanced with
|
|
520
|
+
`next` or `peek` and not read to its end — sits in the middle of its
|
|
521
|
+
reply, so neither `COMMIT` nor `ROLLBACK` can be sent. The socket is
|
|
522
|
+
closed instead, the server drops the transaction with it, and
|
|
523
|
+
`transaction` raises `ConnectionError` — unless the block was itself
|
|
524
|
+
leaving with an exception, `break` or `throw`, which carries on. The
|
|
525
|
+
stream is ended too, so the connection's next statement re-dials as after
|
|
526
|
+
any lost connection. Read a stream to its end, or give `stream` a block,
|
|
527
|
+
before the transaction's block returns.
|
|
528
|
+
- `pool.transaction(attempts: 3, begin: "BEGIN", affinity: nil)` checks one
|
|
529
|
+
connection out, runs every attempt on it, and returns it afterwards.
|
|
530
|
+
|
|
531
|
+
Written by hand, the same transaction is:
|
|
532
|
+
|
|
533
|
+
```ruby
|
|
534
|
+
conn.exec("BEGIN") # if this raises, stop here
|
|
535
|
+
begin
|
|
536
|
+
conn.exec_params("UPDATE accounts SET balance = balance - $1 WHERE id = $2", [10, 1])
|
|
537
|
+
conn.exec_params("UPDATE accounts SET balance = balance + $1 WHERE id = $2", [10, 2])
|
|
538
|
+
rescue StandardError
|
|
539
|
+
begin
|
|
540
|
+
conn.exec("ROLLBACK") if conn.usable?
|
|
541
|
+
rescue Skaidb::Error
|
|
542
|
+
nil # the clean-up never replaces the error it follows
|
|
543
|
+
end
|
|
544
|
+
raise
|
|
545
|
+
end
|
|
546
|
+
conn.exec("COMMIT") # Skaidb::TransactionConflict: nothing was applied; run it again from BEGIN
|
|
547
|
+
```
|
|
548
|
+
|
|
549
|
+
Two rules hold however the statements are sent:
|
|
550
|
+
|
|
551
|
+
- **One connection for the whole transaction.** Statements sent on another
|
|
552
|
+
connection are not part of it, so with a pool check one connection out
|
|
553
|
+
(`pool.with`) and keep it until the `COMMIT`; a statement another thread
|
|
554
|
+
sends on that connection meanwhile is part of it. A `ConnectionError` ends
|
|
555
|
+
the transaction with its socket; the connection's next statement re-dials
|
|
556
|
+
and runs on a new session, in autocommit. That is why the example sends
|
|
557
|
+
`ROLLBACK` only while the connection is `usable?`.
|
|
558
|
+
- **Stop if `BEGIN` fails.** No transaction is open, so every statement sent
|
|
559
|
+
after it would be committed on its own.
|
|
560
|
+
|
|
561
|
+
A statement that fails inside a transaction leaves it open: decide between
|
|
562
|
+
`ROLLBACK` and carrying on. A `ROLLBACK` with no transaction open succeeds on
|
|
563
|
+
a server ≥ 0.347 and is a statement error (`no transaction in progress`) on
|
|
564
|
+
an older one.
|
|
450
565
|
|
|
451
566
|
## Thread safety
|
|
452
567
|
|
|
@@ -464,7 +579,7 @@ frame after every handshake. It shows up as `client_name = 'ruby'` /
|
|
|
464
579
|
|
|
465
580
|
```sql
|
|
466
581
|
SELECT client_name, client_version FROM drivers;
|
|
467
|
-
-- ruby | 1.
|
|
582
|
+
-- ruby | 1.5.1
|
|
468
583
|
```
|
|
469
584
|
|
|
470
585
|
The server records the row asynchronously, so a `SELECT` immediately after
|
data/docs/api.md
CHANGED
|
@@ -12,7 +12,7 @@ Everything lives under the `Skaidb` module in `lib/skaidb.rb`.
|
|
|
12
12
|
| `password` | `""` | SCRAM password. Empty skips server-signature verification (anonymous connect). |
|
|
13
13
|
| `consistency` | `:quorum` | Default level for every statement: `:one`, `:quorum`, `:all`, `"ONE"`…, or `0`/`1`/`2`. |
|
|
14
14
|
| `timeout` | `10.0` | TCP connect timeout in seconds; `nil` for none. Reads are not bounded. |
|
|
15
|
-
| `database` | `nil` | Runs `USE "<database>"` after the handshake
|
|
15
|
+
| `database` | `nil` | Runs `USE "<database>"` after the handshake. Every re-dial re-enters the session's database: this one, or the one a later `USE` statement moved the session to. |
|
|
16
16
|
| `seeds` | `nil` | `["host", "host:port", …]`, tried in random order until one connects **and** authenticates. |
|
|
17
17
|
| `tls` | `false` | Enable TLS with the system trust store. |
|
|
18
18
|
| `tls_ca` | `nil` | PEM file to trust instead of the system store; implies `tls`. |
|
|
@@ -42,8 +42,10 @@ Raises `Skaidb::Error` before dialing for an unknown `auth_mechanism`, a
|
|
|
42
42
|
sending the values typed. Falls back to client-side text binding when the
|
|
43
43
|
server refuses to prepare the statement (DDL, `USE`, old servers); on that
|
|
44
44
|
path an Array/Hash parameter raises `QueryError` that includes the server's
|
|
45
|
-
refusal reason.
|
|
46
|
-
|
|
45
|
+
refusal reason. A server that refuses because its statement table is full
|
|
46
|
+
(`too many prepared statements …`) is not answered with that fallback: the
|
|
47
|
+
`QueryError` is raised. `consistency:` overrides the connection default
|
|
48
|
+
for this statement.
|
|
47
49
|
- `exec_batch(sql, rows, consistency: nil) → Integer` — prepare once, execute
|
|
48
50
|
once per row in one frame, return the total affected count. `rows` is an
|
|
49
51
|
Array of parameter Arrays. An empty `rows` returns `0` with no round-trip.
|
|
@@ -52,9 +54,22 @@ Raises `Skaidb::Error` before dialing for an unknown `auth_mechanism`, a
|
|
|
52
54
|
(which names the failing row index; earlier rows stay applied).
|
|
53
55
|
- `exec_prepared(id, params, consistency) → Result` — run a statement by the
|
|
54
56
|
id `prepare_server` returned, with an Array of typed values in `?` order.
|
|
57
|
+
An id names its statement for as long as you hold it: it carries the
|
|
58
|
+
socket that issued it, and when the connection has re-dialled since, the
|
|
59
|
+
statement is prepared again on the new socket before it runs — an id is
|
|
60
|
+
never sent to a socket that did not issue it, where the same number could
|
|
61
|
+
name another statement. A broken connection re-dials here as for any
|
|
62
|
+
statement. An id another connection issued raises `QueryError`.
|
|
55
63
|
- `prepare_server(sql) → [id, nparams] | nil` — prepare a statement with `?`
|
|
56
|
-
placeholders on the server; `nil` when the server
|
|
57
|
-
|
|
64
|
+
placeholders on the server; `nil` when the server declines the statement
|
|
65
|
+
kind, `QueryError` when its statement table is full. The id is yours to
|
|
66
|
+
keep for `exec_prepared`: the driver never closes a statement it handed
|
|
67
|
+
out here, and `exec_params`/`exec_batch` with the same SQL reuse it. The
|
|
68
|
+
statements the driver prepares itself for `exec_params` and `exec_batch`
|
|
69
|
+
are a cache of the least recently used, closed on the server (`OP_CLOSE`)
|
|
70
|
+
as they leave it, so that held and cached statements together stay at 240
|
|
71
|
+
— under the server's 256 per connection. The server-side statements are
|
|
72
|
+
gone after a re-dial; the ids you hold stay valid (see `exec_prepared`).
|
|
58
73
|
- `stream(sql, params = [], consistency: nil) { |row| … } → Result` — see
|
|
59
74
|
[streaming.md](streaming.md). `params` (an Array) bind `$1`, `$2`, …
|
|
60
75
|
exactly as `exec_params` binds them, typed, over `OP_QUERY_STREAM_PARAMS`
|
|
@@ -77,13 +92,68 @@ Raises `Skaidb::Error` before dialing for an unknown `auth_mechanism`, a
|
|
|
77
92
|
keyset cursor, sleeping `poll` seconds when caught up. `after:` is the last
|
|
78
93
|
`"id"` (an opaque String) already handled.
|
|
79
94
|
|
|
95
|
+
### Transactions
|
|
96
|
+
|
|
97
|
+
`transaction(attempts: 3, begin: "BEGIN") { |conn| … } → the block's value`
|
|
98
|
+
runs the block inside a transaction on this connection: it sends `begin`,
|
|
99
|
+
yields the connection, then sends `COMMIT`.
|
|
100
|
+
|
|
101
|
+
- `begin:` is the opening statement, sent as given: `"BEGIN"` on a standalone
|
|
102
|
+
server, `"BEGIN ATOMIC"` or `"BEGIN ATOMIC PARTITION (col = lit)"` on a
|
|
103
|
+
cluster.
|
|
104
|
+
- A `COMMIT` refused with `TransactionConflict` (a standalone server ≥ 0.346:
|
|
105
|
+
another writer changed a row the transaction writes after its `BEGIN`;
|
|
106
|
+
nothing was applied) runs the whole transaction again from `begin`, up to
|
|
107
|
+
`attempts` runs in all with no pause between them. The block may therefore
|
|
108
|
+
run more than once: it must be safe to repeat and should do nothing outside
|
|
109
|
+
the database that a repeat would do twice. The conflict of the last
|
|
110
|
+
attempt is raised.
|
|
111
|
+
- `begin` refused: its error is raised, the block never runs and nothing
|
|
112
|
+
else is sent.
|
|
113
|
+
- The block does not run to its end (an exception of any class, `break`,
|
|
114
|
+
`throw`, a `return` out of it): `ROLLBACK` is sent and the exception
|
|
115
|
+
propagates unchanged, whatever the `ROLLBACK` itself answers — an error
|
|
116
|
+
from it is ignored. Nothing is retried, a `TransactionConflict` raised
|
|
117
|
+
inside the block included.
|
|
118
|
+
- Any other `COMMIT` error is raised as it is, with no retry and no
|
|
119
|
+
`ROLLBACK` (a `COMMIT` that fails has ended the transaction).
|
|
120
|
+
- A transport failure is the usual `ConnectionError` and is not retried.
|
|
121
|
+
Until `transaction` returns the connection does not re-dial: no `ROLLBACK`
|
|
122
|
+
is sent, and a statement the block runs after rescuing the failure raises
|
|
123
|
+
`ConnectionError` (`connection lost inside a transaction; …`). A
|
|
124
|
+
`ConnectionError` from the `COMMIT` leaves its outcome unknown.
|
|
125
|
+
- A block cut short in the middle of a round trip (an `Interrupt`, a
|
|
126
|
+
`Timeout`, `Thread#kill`) leaves a reply on its way, so the connection is
|
|
127
|
+
not `usable?` and nothing more is written to it: no `ROLLBACK`. The
|
|
128
|
+
transaction ends with that socket, which is closed at once — the server
|
|
129
|
+
drops the transaction with it — and which the next statement after
|
|
130
|
+
`transaction` replaces.
|
|
131
|
+
- A stream the block leaves open (an Enumerator from `stream` advanced with
|
|
132
|
+
`next`/`peek` and not read to its end) sits in the middle of its reply, so
|
|
133
|
+
neither `COMMIT` nor `ROLLBACK` can be sent. The connection is marked
|
|
134
|
+
broken — its socket closed, so the server drops the transaction — and the
|
|
135
|
+
stream is ended, which frees the connection: its next statement re-dials.
|
|
136
|
+
`transaction` raises `ConnectionError` (`a stream opened inside the
|
|
137
|
+
transaction was left open, …`) when the block ran to its end; a block
|
|
138
|
+
that was leaving with an exception, `break` or `throw` leaves with that.
|
|
139
|
+
The Enumerator, advanced again, starts its statement again on the
|
|
140
|
+
re-dialled connection, outside any transaction. (A stream opened by
|
|
141
|
+
another thread cannot be ended from here: the connection is closed for
|
|
142
|
+
good instead, `finished?` is `true`.)
|
|
143
|
+
- Raises `ArgumentError`, before anything is sent, when `attempts` is not an
|
|
144
|
+
Integer ≥ 1 or no block is given.
|
|
145
|
+
|
|
146
|
+
The whole transaction runs on this one connection, so no other thread may
|
|
147
|
+
use it meanwhile.
|
|
148
|
+
|
|
80
149
|
### State
|
|
81
150
|
|
|
82
151
|
- `consistency` / `consistency=` — the default level (`0`/`1`/`2`; the setter
|
|
83
152
|
accepts anything `Consistency.resolve` does).
|
|
84
|
-
- `usable?` — `false` once closed, once a transport error broke the socket
|
|
85
|
-
or
|
|
86
|
-
iteration).
|
|
153
|
+
- `usable?` — `false` once closed, once a transport error broke the socket
|
|
154
|
+
or a round trip was cut short with its reply outstanding, or while a
|
|
155
|
+
stream is in flight (including one abandoned through external iteration).
|
|
156
|
+
No round-trip.
|
|
87
157
|
- `endpoint` — the member the connection reached, as the `"host:port"` it
|
|
88
158
|
dialed (it changes when a re-dial lands on another member).
|
|
89
159
|
- `closed`, `finished?` — `true` after `close`.
|
|
@@ -93,12 +163,28 @@ Raises `Skaidb::Error` before dialing for an unknown `auth_mechanism`, a
|
|
|
93
163
|
### Reconnect
|
|
94
164
|
|
|
95
165
|
A transport failure raises `Skaidb::ConnectionError` and marks the
|
|
96
|
-
connection broken.
|
|
166
|
+
connection broken. A statement cut short between its request and its reply
|
|
167
|
+
by anything else (an `Interrupt`, `Timeout.timeout`, `Thread#kill`) marks it
|
|
168
|
+
broken too, since that reply is still on its way; the exception propagates
|
|
169
|
+
as it is. A connection marked broken closes its socket there and then, so
|
|
170
|
+
the server drops the session — and a transaction left open on it — without
|
|
171
|
+
waiting for the connection's next statement or its `close`. The next
|
|
172
|
+
`exec`/`exec_params`/`exec_batch`/`stream`
|
|
97
173
|
re-dials across the original seeds (a pooled connection: in the pool's
|
|
98
174
|
order, usable members first and the one it lost last), re-authenticates,
|
|
99
|
-
re-sends `Hello`, re-
|
|
100
|
-
cache.
|
|
101
|
-
|
|
175
|
+
re-sends `Hello`, re-enters the session's database, and starts with an empty
|
|
176
|
+
prepared-statement cache. The database is the one the session was last in:
|
|
177
|
+
`database:`, or the name of the last `USE [DATABASE] <name>` statement the
|
|
178
|
+
server accepted from `exec`, `query`, `exec_params` or `stream` (the name
|
|
179
|
+
bare or double-quoted, an optional trailing `;`, `--` comments skipped as
|
|
180
|
+
the server skips them; the name is kept as it was written and sent again
|
|
181
|
+
in a `USE` of its own). It is entered before anything else is sent, and a refusal is
|
|
182
|
+
raised to the statement that caused the re-dial — which is not sent — so no
|
|
183
|
+
statement runs in another database. If the re-dial fails, that statement
|
|
184
|
+
raises `ConnectionError` and the
|
|
185
|
+
one after it tries again. Inside `transaction` there is no re-dial: the
|
|
186
|
+
transaction ended with its socket, so every further statement raises
|
|
187
|
+
`ConnectionError` until the helper returns.
|
|
102
188
|
|
|
103
189
|
## `Skaidb::Result`
|
|
104
190
|
|
|
@@ -131,6 +217,10 @@ A thread-safe pool balanced across the cluster's members — see
|
|
|
131
217
|
|
|
132
218
|
- `with(affinity: nil) { |conn| … }` — check out, yield, check in; returns
|
|
133
219
|
the block's value.
|
|
220
|
+
- `transaction(attempts: 3, begin: "BEGIN", affinity: nil) { |conn| … }` —
|
|
221
|
+
`Connection#transaction` on one checked-out connection, held for every
|
|
222
|
+
attempt and checked in afterwards; returns the block's value. The
|
|
223
|
+
arguments are checked before a connection is checked out.
|
|
134
224
|
- `checkout(affinity: nil) → Connection` — an idle connection to the member
|
|
135
225
|
the policy picks, or a new one. Never blocks. `affinity` (any Hash key;
|
|
136
226
|
`nil` = pool-wide) scopes `read_your_writes`.
|
|
@@ -143,9 +233,9 @@ A thread-safe pool balanced across the cluster's members — see
|
|
|
143
233
|
|
|
144
234
|
`load_balancing` (`:random`, `:round_robin`, `:least_latency`) and
|
|
145
235
|
`idle_order` (`:lifo`, `:fifo`) take a Symbol or a String. Raises
|
|
146
|
-
`ArgumentError` for `maxsize < 1`, `eject_errors < 1
|
|
147
|
-
`load_balancing`/`idle_order
|
|
148
|
-
pool.
|
|
236
|
+
`ArgumentError` for `maxsize < 1`, `eject_errors < 1`, an unknown
|
|
237
|
+
`load_balancing`/`idle_order` or a `transaction` with `attempts` below 1 or
|
|
238
|
+
no block, and `Skaidb::Error` on checkout from a closed pool.
|
|
149
239
|
|
|
150
240
|
## `Skaidb::Uuid`
|
|
151
241
|
|
|
@@ -173,6 +263,14 @@ otherwise.
|
|
|
173
263
|
message is the server's), or a parameter could not be bound
|
|
174
264
|
(placeholder/parameter mismatch, out-of-range Integer, non-finite Float or
|
|
175
265
|
BigDecimal, unsupported type). The connection stays usable.
|
|
266
|
+
- `Skaidb::TransactionConflict < QueryError` — the server refused a `COMMIT`
|
|
267
|
+
because another writer changed a row the transaction writes after its
|
|
268
|
+
`BEGIN` (a standalone server ≥ 0.346). Raised by every call that runs a
|
|
269
|
+
statement, for a server error whose message starts with
|
|
270
|
+
`Skaidb::TransactionConflict::PREFIX` (`"transaction conflict:"`); the
|
|
271
|
+
message is the server's. Nothing was applied, the transaction is over and
|
|
272
|
+
the connection stays usable, in autocommit; running the whole transaction
|
|
273
|
+
again from `BEGIN` is safe, which is what `transaction` does.
|
|
176
274
|
|
|
177
275
|
## Codec helpers
|
|
178
276
|
|
data/docs/getting-started.md
CHANGED
|
@@ -93,6 +93,23 @@ Rows arrive chunk by chunk; the driver holds one chunk at a time. See
|
|
|
93
93
|
[streaming.md](streaming.md), in particular what happens when you leave the
|
|
94
94
|
block early.
|
|
95
95
|
|
|
96
|
+
## Transactions
|
|
97
|
+
|
|
98
|
+
```ruby
|
|
99
|
+
conn.transaction do |c| # BEGIN, the block, COMMIT; returns the block's value
|
|
100
|
+
c.exec_params("UPDATE accounts SET balance = balance - $1 WHERE id = $2", [10, 1])
|
|
101
|
+
c.exec_params("UPDATE accounts SET balance = balance + $1 WHERE id = $2", [10, 2])
|
|
102
|
+
end
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
A block that raises is rolled back. A `COMMIT` the server refuses because
|
|
106
|
+
another writer changed a row the transaction writes raises
|
|
107
|
+
`Skaidb::TransactionConflict` with nothing applied, and `transaction` then
|
|
108
|
+
runs the block again (three runs in all by default), so the block must be
|
|
109
|
+
safe to run more than once. On a cluster pass the opening statement:
|
|
110
|
+
`conn.transaction(begin: "BEGIN ATOMIC") { |c| … }`. Details:
|
|
111
|
+
[api.md](api.md#transactions).
|
|
112
|
+
|
|
96
113
|
## Errors
|
|
97
114
|
|
|
98
115
|
```ruby
|
|
@@ -105,7 +122,8 @@ rescue Skaidb::ConnectionError => e # transport/handshake trouble; next stateme
|
|
|
105
122
|
end
|
|
106
123
|
```
|
|
107
124
|
|
|
108
|
-
Both inherit from `Skaidb::Error`.
|
|
125
|
+
Both inherit from `Skaidb::Error`. `Skaidb::TransactionConflict` is a
|
|
126
|
+
`QueryError`.
|
|
109
127
|
|
|
110
128
|
## Close
|
|
111
129
|
|