skaidb 1.5.0 → 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 +74 -0
- data/README.md +40 -17
- data/docs/api.md +48 -12
- data/docs/pooling.md +8 -1
- data/docs/streaming.md +21 -4
- data/lib/skaidb.rb +374 -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,79 @@ 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
|
+
|
|
7
80
|
## [1.5.0] - 2026-11-15
|
|
8
81
|
|
|
9
82
|
### Added
|
|
@@ -280,6 +353,7 @@ batches, a 2 500-row stream, error handling, and the `drivers` table showing
|
|
|
280
353
|
because it is a bundled gem since Ruby 3.4.
|
|
281
354
|
- The example moved to `examples/basic.rb`.
|
|
282
355
|
|
|
356
|
+
[1.5.1]: https://github.com/porcupin26/skaidb-ruby/compare/v1.5.0...v1.5.1
|
|
283
357
|
[1.5.0]: https://github.com/porcupin26/skaidb-ruby/compare/v1.4.1...v1.5.0
|
|
284
358
|
[1.4.1]: https://github.com/porcupin26/skaidb-ruby/compare/v1.4.0...v1.4.1
|
|
285
359
|
[1.4.0]: https://github.com/porcupin26/skaidb-ruby/compare/v1.3.0...v1.4.0
|
data/README.md
CHANGED
|
@@ -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.5.
|
|
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.5.
|
|
64
|
+
installs without registry access: `gem install ./skaidb-1.5.1.gem`.
|
|
65
65
|
|
|
66
66
|
## Quick start
|
|
67
67
|
|
|
@@ -146,10 +146,15 @@ conn = Skaidb.connect(seeds: ["db1", "db2:7000", "db3"], database: "app")
|
|
|
146
146
|
Each seed is `"host"` or `"host:port"` (the port after the last colon wins; a
|
|
147
147
|
seed without a port uses `port:`). When a connection's transport dies, the
|
|
148
148
|
**next statement re-dials** across the same seeds, re-authenticates, re-sends
|
|
149
|
-
`Hello`, re-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
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.
|
|
153
158
|
|
|
154
159
|
### Timeout
|
|
155
160
|
|
|
@@ -159,6 +164,9 @@ not take the connection's lock, so another thread can break a stuck read by
|
|
|
159
164
|
closing the connection. A statement cut short while its reply is outstanding
|
|
160
165
|
— by `Timeout.timeout`, an `Interrupt`, `Thread#kill` — leaves the connection
|
|
161
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.
|
|
162
170
|
|
|
163
171
|
### TLS
|
|
164
172
|
|
|
@@ -235,9 +243,12 @@ conn.exec_params("INSERT INTO docs (id, meta) VALUES ($1, $2)",
|
|
|
235
243
|
conn.exec_params("SELECT id FROM users WHERE id IN ($1)", [[1, 2, 3]]) # set membership
|
|
236
244
|
```
|
|
237
245
|
|
|
238
|
-
Prepared statements are cached per connection
|
|
239
|
-
|
|
240
|
-
|
|
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.
|
|
241
252
|
|
|
242
253
|
Statement kinds the server refuses to prepare (DDL and session control such
|
|
243
254
|
as `USE`) fall back to **client-side text binding** for scalar parameters: a
|
|
@@ -321,8 +332,9 @@ form — drains the frames the server is still sending so the socket sits at a
|
|
|
321
332
|
request boundary again. Draining transfers the rest of the result: if you only
|
|
322
333
|
want the first rows, say so with `LIMIT`. External iteration (`next`/`peek`)
|
|
323
334
|
on the Enumerator form cannot unwind and leaves the connection marked busy;
|
|
324
|
-
iterate with a block or `each`. A frame that makes no sense mid-stream,
|
|
325
|
-
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`
|
|
326
338
|
and the next statement re-dials. Details: [docs/streaming.md](https://github.com/porcupin26/skaidb-ruby/blob/main/docs/streaming.md).
|
|
327
339
|
|
|
328
340
|
## Connection pool
|
|
@@ -441,10 +453,10 @@ StandardError
|
|
|
441
453
|
`TransactionConflict`, from every call that runs a statement. It is a
|
|
442
454
|
`QueryError`, so `rescue Skaidb::QueryError` catches it too; see
|
|
443
455
|
[Transactions](#transactions).
|
|
444
|
-
- A transport failure (`ConnectionError`) marks the connection broken
|
|
445
|
-
next statement re-dials, or a pool replaces it. So
|
|
446
|
-
cuts a statement short between its request and its
|
|
447
|
-
a `Timeout`); that exception propagates as it is.
|
|
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.
|
|
448
460
|
- `ArgumentError` for `Pool.new(maxsize: 0)` or an unknown
|
|
449
461
|
`load_balancing`/`idle_order`, for `Skaidb::Uuid.new` of a string that
|
|
450
462
|
is not a UUID, for `stream` parameters that are not an Array, and for
|
|
@@ -501,7 +513,18 @@ What else `transaction` does:
|
|
|
501
513
|
block still runs after rescuing the failure raises `ConnectionError`
|
|
502
514
|
instead of running on a new session. The same goes for a block cut short
|
|
503
515
|
in the middle of a round trip (an `Interrupt`, a `Timeout`): the reply is
|
|
504
|
-
still on its way, so nothing more is written to that socket.
|
|
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.
|
|
505
528
|
- `pool.transaction(attempts: 3, begin: "BEGIN", affinity: nil)` checks one
|
|
506
529
|
connection out, runs every attempt on it, and returns it afterwards.
|
|
507
530
|
|
|
@@ -556,7 +579,7 @@ frame after every handshake. It shows up as `client_name = 'ruby'` /
|
|
|
556
579
|
|
|
557
580
|
```sql
|
|
558
581
|
SELECT client_name, client_version FROM drivers;
|
|
559
|
-
-- ruby | 1.5.
|
|
582
|
+
-- ruby | 1.5.1
|
|
560
583
|
```
|
|
561
584
|
|
|
562
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,12 +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.
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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`.
|
|
58
63
|
- `prepare_server(sql) → [id, nparams] | nil` — prepare a statement with `?`
|
|
59
|
-
placeholders on the server; `nil` when the server
|
|
60
|
-
|
|
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`).
|
|
61
73
|
- `stream(sql, params = [], consistency: nil) { |row| … } → Result` — see
|
|
62
74
|
[streaming.md](streaming.md). `params` (an Array) bind `$1`, `$2`, …
|
|
63
75
|
exactly as `exec_params` binds them, typed, over `OP_QUERY_STREAM_PARAMS`
|
|
@@ -113,8 +125,21 @@ yields the connection, then sends `COMMIT`.
|
|
|
113
125
|
- A block cut short in the middle of a round trip (an `Interrupt`, a
|
|
114
126
|
`Timeout`, `Thread#kill`) leaves a reply on its way, so the connection is
|
|
115
127
|
not `usable?` and nothing more is written to it: no `ROLLBACK`. The
|
|
116
|
-
transaction ends with that socket, which
|
|
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
|
|
117
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`.)
|
|
118
143
|
- Raises `ArgumentError`, before anything is sent, when `attempts` is not an
|
|
119
144
|
Integer ≥ 1 or no block is given.
|
|
120
145
|
|
|
@@ -141,11 +166,22 @@ A transport failure raises `Skaidb::ConnectionError` and marks the
|
|
|
141
166
|
connection broken. A statement cut short between its request and its reply
|
|
142
167
|
by anything else (an `Interrupt`, `Timeout.timeout`, `Thread#kill`) marks it
|
|
143
168
|
broken too, since that reply is still on its way; the exception propagates
|
|
144
|
-
as it is.
|
|
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`
|
|
145
173
|
re-dials across the original seeds (a pooled connection: in the pool's
|
|
146
174
|
order, usable members first and the one it lost last), re-authenticates,
|
|
147
|
-
re-sends `Hello`, re-
|
|
148
|
-
cache.
|
|
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
|
|
149
185
|
one after it tries again. Inside `transaction` there is no re-dial: the
|
|
150
186
|
transaction ended with its socket, so every further statement raises
|
|
151
187
|
`ConnectionError` until the helper returns.
|
data/docs/pooling.md
CHANGED
|
@@ -56,7 +56,14 @@ pool.close
|
|
|
56
56
|
(`database`, TLS, `consistency`, `timeout`) apply to every connection, so
|
|
57
57
|
pooled connections run `USE` and authenticate exactly like single ones. A
|
|
58
58
|
pooled connection that re-dials after a transport error does so in the
|
|
59
|
-
pool's order: usable members first, the one it lost last.
|
|
59
|
+
pool's order: usable members first, the one it lost last. A borrower that
|
|
60
|
+
sends its own `USE` moves that connection only while it holds it: a pool
|
|
61
|
+
with `database:` puts an idle connection back in that database before it
|
|
62
|
+
hands it out again (one extra round trip, only when it was moved; a
|
|
63
|
+
connection that cannot be put back is dropped). A pool without `database:`
|
|
64
|
+
has no database to put it back in, so there a borrower's `USE` stays with
|
|
65
|
+
the connection — send `USE` after every checkout if borrowers use different
|
|
66
|
+
databases.
|
|
60
67
|
`Connection#endpoint` names the member a connection reached.
|
|
61
68
|
|
|
62
69
|
## Balancing across members
|
data/docs/streaming.md
CHANGED
|
@@ -94,17 +94,34 @@ Draining is not free and there is no cancel opcode: breaking out of a
|
|
|
94
94
|
million-row scan still transfers the rest of it before the connection is
|
|
95
95
|
usable again. If you only want a few rows, say so in SQL (`LIMIT`).
|
|
96
96
|
|
|
97
|
-
|
|
97
|
+
Three things defeat the drain:
|
|
98
98
|
|
|
99
99
|
- **External iteration** — `e = conn.stream(sql); e.next` — runs the stream
|
|
100
100
|
inside the Enumerator's Fiber. A Fiber abandoned part-way is collected
|
|
101
101
|
without running any `ensure`, so nothing drains and the connection stays
|
|
102
102
|
marked mid-stream: `usable?` is `false` for good and a pool will not hand
|
|
103
|
-
it out again. Iterate with a block or `each`.
|
|
103
|
+
it out again. Iterate with a block or `each`. Dropping such an Enumerator
|
|
104
|
+
costs the connection, not the process: its Fiber is collected while it
|
|
105
|
+
owns the connection's lock, and Ruby 3.3 crashes when a lock is freed
|
|
106
|
+
after the Fiber that owns it, so the driver keeps the lock of every
|
|
107
|
+
stream in flight alive until the stream ends — for a stream abandoned
|
|
108
|
+
this way, one small object for the life of the process. Inside
|
|
109
|
+
[`transaction`](api.md#transactions) a stream left that way when the block
|
|
110
|
+
ends costs the session: neither `COMMIT` nor `ROLLBACK` can be sent past
|
|
111
|
+
it, and the transaction must not stay open on a live session, so the
|
|
112
|
+
socket is closed and the stream ended.
|
|
104
113
|
- **A frame that makes no sense mid-stream**, or a dead socket. The driver
|
|
105
114
|
no longer knows where the reply ends, so it marks the connection broken
|
|
106
|
-
instead of guessing; `usable?` turns `false`, and
|
|
107
|
-
re-dials.
|
|
115
|
+
and closes the socket instead of guessing; `usable?` turns `false`, and
|
|
116
|
+
the next statement re-dials.
|
|
117
|
+
- **A stream cut short while it waits for a frame** — `Timeout.timeout`
|
|
118
|
+
around the stream, an `Interrupt`, `Thread#kill`. Part of that frame may
|
|
119
|
+
already be read, and a drain would hold the exception back until the
|
|
120
|
+
server had sent the rest of the result, so there is none: the connection
|
|
121
|
+
is marked broken, its socket closed, and the exception propagates at
|
|
122
|
+
once. (The same exception arriving while your block runs finds the socket
|
|
123
|
+
at a frame boundary, and the stream is drained as for any other way of
|
|
124
|
+
leaving the block.)
|
|
108
125
|
|
|
109
126
|
There is no read deadline: a peer that is alive but silent blocks the drain,
|
|
110
127
|
holding the lock. `close` does not take the lock, so another thread can break
|
data/lib/skaidb.rb
CHANGED
|
@@ -31,11 +31,16 @@ require "socket"
|
|
|
31
31
|
require "openssl"
|
|
32
32
|
require "securerandom"
|
|
33
33
|
require "bigdecimal"
|
|
34
|
+
begin
|
|
35
|
+
require "fiber" # Fiber.current, before Ruby 3.1 made it core
|
|
36
|
+
rescue LoadError
|
|
37
|
+
nil
|
|
38
|
+
end
|
|
34
39
|
|
|
35
40
|
module Skaidb
|
|
36
41
|
# The package version — the single source of truth. The gemspec reads it,
|
|
37
42
|
# and it is what the driver reports to the server in the Hello frame.
|
|
38
|
-
VERSION = "1.5.
|
|
43
|
+
VERSION = "1.5.1"
|
|
39
44
|
|
|
40
45
|
# Base class for every error raised by this driver.
|
|
41
46
|
class Error < StandardError; end
|
|
@@ -703,6 +708,40 @@ module Skaidb
|
|
|
703
708
|
@nonce_mutex.synchronize { @nonce_counter += 1 }
|
|
704
709
|
end
|
|
705
710
|
|
|
711
|
+
@stream_locks = {}.compare_by_identity
|
|
712
|
+
|
|
713
|
+
# Internal: the locks of the connections that have a stream in flight,
|
|
714
|
+
# held here from the stream's first row frame to its end.
|
|
715
|
+
#
|
|
716
|
+
# A stream read by external iteration (e = conn.stream(sql); e.next) runs
|
|
717
|
+
# in the Enumerator's Fiber, inside the connection's lock. Dropped
|
|
718
|
+
# half-read, that Fiber never runs again: it is collected still owning
|
|
719
|
+
# the lock. Ruby (3.3.8 measured) does not survive the lock then being
|
|
720
|
+
# freed AFTER the Fiber that owns it — a segfault or an abort at that
|
|
721
|
+
# collection or at exit, or a spin at exit. A lock that is referenced from
|
|
722
|
+
# here is not freed while the process runs, and at exit the interpreter
|
|
723
|
+
# releases a thread's locks before it frees anything. So the lock of an
|
|
724
|
+
# abandoned stream stays here for good: one small object per stream left
|
|
725
|
+
# that way, in exchange for the process.
|
|
726
|
+
def self.keep_stream_lock(lock)
|
|
727
|
+
@nonce_mutex.synchronize { @stream_locks[lock] = true }
|
|
728
|
+
end
|
|
729
|
+
|
|
730
|
+
# Internal: the stream ended (its ensure ran): its lock is the
|
|
731
|
+
# connection's own again.
|
|
732
|
+
def self.release_stream_lock(lock)
|
|
733
|
+
@nonce_mutex.synchronize { @stream_locks.delete(lock) }
|
|
734
|
+
end
|
|
735
|
+
|
|
736
|
+
@dial_counter = 0
|
|
737
|
+
|
|
738
|
+
# Internal: a process-wide number for each socket a connection dials. It
|
|
739
|
+
# is the upper half of the prepared-statement ids that socket's
|
|
740
|
+
# connection hands out, so an id says which socket issued it.
|
|
741
|
+
def self.next_dial_serial
|
|
742
|
+
@nonce_mutex.synchronize { @dial_counter += 1 }
|
|
743
|
+
end
|
|
744
|
+
|
|
706
745
|
# A connection to one skaidb node. Modelled on +PG::Connection+.
|
|
707
746
|
class Connection
|
|
708
747
|
# @return [Boolean] whether the connection has been closed
|
|
@@ -740,15 +779,34 @@ module Skaidb
|
|
|
740
779
|
end
|
|
741
780
|
@mutex = Mutex.new
|
|
742
781
|
@closed = false
|
|
743
|
-
# Transport died
|
|
782
|
+
# Transport died, and its socket is closed (see mark_broken!); the next
|
|
783
|
+
# statement re-dials (see ensure_live!).
|
|
744
784
|
@broken = false
|
|
745
|
-
# A stream is in flight, so the socket sits mid-reply (see stream)
|
|
785
|
+
# A stream is in flight, so the socket sits mid-reply (see stream), and
|
|
786
|
+
# the Fiber it runs in: the transaction helper unwinds a stream its
|
|
787
|
+
# block left open (see abandon_open_stream).
|
|
746
788
|
@streaming = false
|
|
789
|
+
@stream_fiber = nil
|
|
747
790
|
# The transaction helper is between its BEGIN and its COMMIT/ROLLBACK,
|
|
748
791
|
# so a lost socket must not be re-dialled (see ensure_live!).
|
|
749
792
|
@in_transaction = false
|
|
793
|
+
# Statements prepared on this socket, SQL => [id, nparams]: the ones
|
|
794
|
+
# the driver prepared for exec_params / exec_batch, least recently used
|
|
795
|
+
# first, and the ones prepare_server handed to the application (see
|
|
796
|
+
# prepare_cached).
|
|
750
797
|
@prepared = {}
|
|
798
|
+
@held = {}
|
|
799
|
+
# Every id prepare_server has handed out => its SQL, kept across
|
|
800
|
+
# re-dials: exec_prepared prepares the statement again when the id is
|
|
801
|
+
# from a socket this connection has since replaced.
|
|
802
|
+
@handles = {}
|
|
803
|
+
@serial = 0
|
|
751
804
|
@last_prepare_error = nil
|
|
805
|
+
# The session database, as the USE target every dial re-enters: the one
|
|
806
|
+
# the connection was opened in (+database:+), and the one it is in now
|
|
807
|
+
# — a USE statement the server accepted moves it (see note_use).
|
|
808
|
+
@dial_use = database.to_s.empty? ? nil : %("#{database.to_s.gsub('"', '""')}")
|
|
809
|
+
@session_use = @dial_use
|
|
752
810
|
# Retained so a reconnect repeats the original connect exactly.
|
|
753
811
|
@dial_args = { host: host, port: port, user: user, password: password,
|
|
754
812
|
timeout: timeout, database: database, tls: tls, tls_ca: tls_ca,
|
|
@@ -762,12 +820,12 @@ module Skaidb
|
|
|
762
820
|
# connect and for every reconnect, so a recovered connection is
|
|
763
821
|
# indistinguishable from a fresh one.
|
|
764
822
|
def dial!
|
|
823
|
+
@serial = Skaidb.next_dial_serial
|
|
765
824
|
host = @dial_args[:host]
|
|
766
825
|
port = @dial_args[:port]
|
|
767
826
|
user = @dial_args[:user]
|
|
768
827
|
password = @dial_args[:password]
|
|
769
828
|
timeout = @dial_args[:timeout]
|
|
770
|
-
database = @dial_args[:database]
|
|
771
829
|
tls = @dial_args[:tls]
|
|
772
830
|
tls_ca = @dial_args[:tls_ca]
|
|
773
831
|
tls_insecure = @dial_args[:tls_insecure]
|
|
@@ -818,13 +876,17 @@ module Skaidb
|
|
|
818
876
|
end
|
|
819
877
|
end
|
|
820
878
|
if last
|
|
821
|
-
|
|
879
|
+
# Not closed: this is also how a re-dial fails while the members are
|
|
880
|
+
# down, and the next statement dials again (see ensure_live!).
|
|
822
881
|
raise ConnectionError,
|
|
823
882
|
"no reachable endpoint in #{endpoints.map { |(h, q)| "#{h}:#{q}" }.join(', ')}: #{last.message}"
|
|
824
883
|
end
|
|
825
884
|
send_hello
|
|
826
|
-
#
|
|
827
|
-
|
|
885
|
+
# The database is per-connection session state, so it is entered on
|
|
886
|
+
# every dial — the one the session was last in, before anything else
|
|
887
|
+
# is sent. If the server refuses, the dial has failed and its error is
|
|
888
|
+
# the statement's: nothing runs in another database.
|
|
889
|
+
exec("USE #{@session_use}") if @session_use
|
|
828
890
|
end
|
|
829
891
|
|
|
830
892
|
# Best-effort self-identification: fills the server's +drivers+ table
|
|
@@ -901,7 +963,7 @@ module Skaidb
|
|
|
901
963
|
# and Hashes have no SQL literal form. $N is rewritten to the
|
|
902
964
|
# positional ? the server expects.
|
|
903
965
|
qsql, order = Skaidb.to_qmark(sql.to_s, params)
|
|
904
|
-
prep =
|
|
966
|
+
prep = prepare_cached(qsql)
|
|
905
967
|
unless prep.nil?
|
|
906
968
|
id, nparams = prep
|
|
907
969
|
if nparams != order.length
|
|
@@ -934,7 +996,7 @@ module Skaidb
|
|
|
934
996
|
|
|
935
997
|
level = consistency.nil? ? @consistency : Consistency.resolve(consistency)
|
|
936
998
|
qsql, = Skaidb.to_qmark(sql.to_s, rows.first)
|
|
937
|
-
prep =
|
|
999
|
+
prep = prepare_cached(qsql)
|
|
938
1000
|
raise QueryError, "statement cannot be prepared, so it cannot be batched" if prep.nil?
|
|
939
1001
|
|
|
940
1002
|
id, nparams = prep
|
|
@@ -944,7 +1006,7 @@ module Skaidb
|
|
|
944
1006
|
end
|
|
945
1007
|
raise ConnectionError, "connection is closed" if @closed
|
|
946
1008
|
|
|
947
|
-
req = +([7, level].pack("CC") + [id].pack("V") + [ordered.length].pack("V"))
|
|
1009
|
+
req = +([7, level].pack("CC") + [id & WIRE_ID].pack("V") + [ordered.length].pack("V"))
|
|
948
1010
|
ordered.each do |r|
|
|
949
1011
|
req << [r.length].pack("v")
|
|
950
1012
|
r.each do |v|
|
|
@@ -996,7 +1058,19 @@ module Skaidb
|
|
|
996
1058
|
# ConnectionError instead of autocommitting on a new session. The same
|
|
997
1059
|
# goes for a block cut short in the middle of a round trip (an
|
|
998
1060
|
# Interrupt, a Timeout): the reply is still on its way, so nothing more
|
|
999
|
-
# is written to that socket.
|
|
1061
|
+
# is written to that socket. Either way the socket is closed at once,
|
|
1062
|
+
# and the server drops the transaction with it.
|
|
1063
|
+
# - A stream the block leaves open — an Enumerator from +stream+ advanced
|
|
1064
|
+
# with +next+ or +peek+ and not read to its end — sits in the middle of
|
|
1065
|
+
# its reply, so neither COMMIT nor ROLLBACK can be sent. The socket is
|
|
1066
|
+
# closed instead and the server drops the transaction with it:
|
|
1067
|
+
# ConnectionError is raised, unless the block was itself leaving with
|
|
1068
|
+
# an exception, +break+ or +throw+, which carries on. The stream is
|
|
1069
|
+
# ended with it, so the connection is what it is after any lost
|
|
1070
|
+
# connection (the next statement re-dials), and the Enumerator starts
|
|
1071
|
+
# its statement again if it is advanced again. Read a stream to its
|
|
1072
|
+
# end, or give +stream+ a block, before the transaction's block
|
|
1073
|
+
# returns.
|
|
1000
1074
|
#
|
|
1001
1075
|
# The whole transaction runs on this one connection, so no other thread
|
|
1002
1076
|
# may use it meanwhile. +attempts+ below 1, or no block, is an
|
|
@@ -1017,8 +1091,13 @@ module Skaidb
|
|
|
1017
1091
|
value = yield self
|
|
1018
1092
|
finished = true
|
|
1019
1093
|
ensure
|
|
1094
|
+
abandoned = abandon_open_stream
|
|
1020
1095
|
rollback_quietly unless finished
|
|
1021
1096
|
end
|
|
1097
|
+
if abandoned
|
|
1098
|
+
raise ConnectionError, "a stream opened inside the transaction was left open, so COMMIT could not " \
|
|
1099
|
+
"be sent: its session was dropped and nothing was committed"
|
|
1100
|
+
end
|
|
1022
1101
|
committing = true
|
|
1023
1102
|
exec("COMMIT")
|
|
1024
1103
|
value
|
|
@@ -1061,7 +1140,10 @@ module Skaidb
|
|
|
1061
1140
|
# sending so the connection sits at a request boundary again. When it
|
|
1062
1141
|
# cannot (dead socket, a frame that makes no sense mid-stream) the
|
|
1063
1142
|
# connection is marked broken instead, so +usable?+ turns false and Pool
|
|
1064
|
-
# drops it rather than handing out a desynced socket.
|
|
1143
|
+
# drops it rather than handing out a desynced socket. So it is when the
|
|
1144
|
+
# stream is cut short while it waits for a frame (Timeout.timeout, an
|
|
1145
|
+
# Interrupt, Thread#kill): nothing is drained, and the exception
|
|
1146
|
+
# propagates at once.
|
|
1065
1147
|
#
|
|
1066
1148
|
# Draining is not free and there is no cancel opcode: breaking out of a
|
|
1067
1149
|
# million-row scan still transfers the rest of it before the connection
|
|
@@ -1127,6 +1209,7 @@ module Skaidb
|
|
|
1127
1209
|
when 1
|
|
1128
1210
|
return Result.new(fields: [], rows: [], cmd_tuples: r.u64, kind: :mutation) # not row-producing
|
|
1129
1211
|
when 2
|
|
1212
|
+
note_use(sql.to_s) if params.empty?
|
|
1130
1213
|
return Result.new(fields: [], rows: [], cmd_tuples: 0, kind: :ddl)
|
|
1131
1214
|
when 8
|
|
1132
1215
|
# ResultSets: a CALL whose body EMITted is answered with every set
|
|
@@ -1147,11 +1230,17 @@ module Skaidb
|
|
|
1147
1230
|
# two-line window.
|
|
1148
1231
|
live = true
|
|
1149
1232
|
@streaming = true
|
|
1233
|
+
@stream_fiber = Fiber.current
|
|
1234
|
+
Skaidb.keep_stream_lock(@mutex)
|
|
1150
1235
|
cols = Array.new(r.u32) { r.text }
|
|
1151
1236
|
summary = Result.new(fields: cols, rows: [], cmd_tuples: 0, kind: :stream)
|
|
1237
|
+
reading = false
|
|
1152
1238
|
begin
|
|
1153
1239
|
while live
|
|
1154
|
-
|
|
1240
|
+
reading = true
|
|
1241
|
+
frame = read_frame
|
|
1242
|
+
reading = false
|
|
1243
|
+
fr = Reader.new(frame)
|
|
1155
1244
|
case fr.u8
|
|
1156
1245
|
when 6
|
|
1157
1246
|
fr.u32.times do
|
|
@@ -1169,16 +1258,35 @@ module Skaidb
|
|
|
1169
1258
|
# Anything else means we no longer know where the reply ends, so
|
|
1170
1259
|
# there is no draining back to a request boundary: retire it.
|
|
1171
1260
|
live = false
|
|
1172
|
-
|
|
1261
|
+
mark_broken!
|
|
1173
1262
|
raise QueryError, "unexpected frame in stream"
|
|
1174
1263
|
end
|
|
1175
1264
|
end
|
|
1176
1265
|
ensure
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1266
|
+
begin
|
|
1267
|
+
if reading
|
|
1268
|
+
# Left while a frame was being read — the transport failed,
|
|
1269
|
+
# or a Timeout, an Interrupt or Thread#kill cut the read
|
|
1270
|
+
# short. The socket is not at a frame boundary (part of that
|
|
1271
|
+
# frame may be consumed), so there is nothing to drain from;
|
|
1272
|
+
# and draining would hold back the exception until the
|
|
1273
|
+
# server had sent the rest of the stream, which a stalled
|
|
1274
|
+
# server never does.
|
|
1275
|
+
mark_broken!
|
|
1276
|
+
elsif live
|
|
1277
|
+
drain_stream!
|
|
1278
|
+
end
|
|
1279
|
+
ensure
|
|
1280
|
+
# Cleared last: until this point the connection is mid-reply,
|
|
1281
|
+
# and a caller who never unwinds here (an abandoned Enumerator
|
|
1282
|
+
# Fiber) leaves it set, so usable? keeps reporting the truth.
|
|
1283
|
+
# In an ensure of its own: a drain that is itself cut short
|
|
1284
|
+
# has retired the socket, and the connection the next
|
|
1285
|
+
# statement re-dials is not in the middle of any stream.
|
|
1286
|
+
@streaming = false
|
|
1287
|
+
@stream_fiber = nil
|
|
1288
|
+
Skaidb.release_stream_lock(@mutex)
|
|
1289
|
+
end
|
|
1182
1290
|
end
|
|
1183
1291
|
else
|
|
1184
1292
|
raise QueryError, "unexpected response tag #{tag} to stream request"
|
|
@@ -1194,45 +1302,37 @@ module Skaidb
|
|
|
1194
1302
|
end
|
|
1195
1303
|
|
|
1196
1304
|
# Prepare +sql+ on the SERVER, returning [id, nparams], or nil when the
|
|
1197
|
-
# server declines the statement kind (DDL, session statements)
|
|
1198
|
-
# caller
|
|
1305
|
+
# server declines the statement kind (DDL, session statements). The id is
|
|
1306
|
+
# the caller's to keep for exec_prepared: the driver's cache never closes
|
|
1307
|
+
# a statement handed out here (exec_params and exec_batch with the same
|
|
1308
|
+
# SQL use it too). A server whose statement table is full refuses with
|
|
1309
|
+
# QueryError.
|
|
1199
1310
|
def prepare_server(sql)
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
# server answers "unknown opcode". Kept for the caller's error message.
|
|
1221
|
-
@last_prepare_error = reader.text
|
|
1222
|
-
nil
|
|
1223
|
-
else
|
|
1224
|
-
raise QueryError, "unexpected prepare response tag #{tag}"
|
|
1225
|
-
end
|
|
1226
|
-
end
|
|
1227
|
-
|
|
1228
|
-
# Execute a prepared statement with TYPED parameters. The id belongs to
|
|
1229
|
-
# the socket that prepared it, so a broken connection is not re-dialled
|
|
1230
|
-
# here: prepare_server does that, and hands out a new id.
|
|
1311
|
+
prepare_cached(sql, hold: true)
|
|
1312
|
+
end
|
|
1313
|
+
|
|
1314
|
+
# The server holds at most 256 prepared statements per connection
|
|
1315
|
+
# (PROTOCOL.md §3.3). The driver keeps under it with room to spare.
|
|
1316
|
+
PREPARED_LIMIT = 240
|
|
1317
|
+
private_constant :PREPARED_LIMIT
|
|
1318
|
+
|
|
1319
|
+
# The lower half of an id is the server's statement number, the upper
|
|
1320
|
+
# half the socket that issued it (see Skaidb.next_dial_serial).
|
|
1321
|
+
WIRE_ID = 0xFFFF_FFFF
|
|
1322
|
+
private_constant :WIRE_ID
|
|
1323
|
+
|
|
1324
|
+
# Execute a prepared statement with TYPED parameters, by the id
|
|
1325
|
+
# prepare_server returned. An id is a number on the socket that issued
|
|
1326
|
+
# it; on another socket the same number is unknown, or names a different
|
|
1327
|
+
# statement prepared there since. So an id from a socket this connection
|
|
1328
|
+
# has since replaced is not sent: its statement is prepared again on the
|
|
1329
|
+
# current socket first (a broken connection re-dials for it, as for any
|
|
1330
|
+
# statement). An id another connection issued is refused with QueryError.
|
|
1231
1331
|
def exec_prepared(id, params, consistency)
|
|
1232
1332
|
raise ConnectionError, "connection is closed" if @closed
|
|
1233
|
-
raise ConnectionError, "connection is broken; prepare the statement again" if @broken
|
|
1234
1333
|
|
|
1235
|
-
|
|
1334
|
+
id = current_id(id)
|
|
1335
|
+
req = +([3, consistency].pack("CC") + [id & WIRE_ID].pack("V") + [params.length].pack("v"))
|
|
1236
1336
|
params.each do |p|
|
|
1237
1337
|
v = Skaidb.encode_value(p)
|
|
1238
1338
|
req << [v.bytesize].pack("V") << v
|
|
@@ -1301,6 +1401,13 @@ module Skaidb
|
|
|
1301
1401
|
@closed
|
|
1302
1402
|
end
|
|
1303
1403
|
|
|
1404
|
+
# Internal, for Pool: put the session back in the database the connection
|
|
1405
|
+
# was opened in (+database:+) when a USE statement has moved it since.
|
|
1406
|
+
# One round trip, and none when there is nothing to put back.
|
|
1407
|
+
def reenter_dial_database
|
|
1408
|
+
exec("USE #{@dial_use}") if @dial_use && @session_use != @dial_use
|
|
1409
|
+
end
|
|
1410
|
+
|
|
1304
1411
|
private
|
|
1305
1412
|
|
|
1306
1413
|
# -- framing --
|
|
@@ -1309,10 +1416,26 @@ module Skaidb
|
|
|
1309
1416
|
payload = payload.b
|
|
1310
1417
|
@sock.write([payload.bytesize].pack("N") + payload) # length is BE
|
|
1311
1418
|
rescue StandardError => e
|
|
1312
|
-
|
|
1419
|
+
mark_broken!
|
|
1313
1420
|
raise ConnectionError, "write failed: #{e.message}"
|
|
1314
1421
|
end
|
|
1315
1422
|
|
|
1423
|
+
# This socket carries no further statement: its transport failed, or it
|
|
1424
|
+
# was left in the middle of a reply. usable? turns false and the next
|
|
1425
|
+
# statement re-dials — and the socket is closed HERE, not when that
|
|
1426
|
+
# statement comes. The server keeps a session for as long as its socket
|
|
1427
|
+
# is open, and with it the transaction left open on it; a connection that
|
|
1428
|
+
# timed out inside a transaction and is not used again would hold that
|
|
1429
|
+
# transaction until it is closed or collected. The TCP socket is closed
|
|
1430
|
+
# as it is, with no TLS goodbye written first: nothing more is written
|
|
1431
|
+
# to a socket in this state.
|
|
1432
|
+
def mark_broken!
|
|
1433
|
+
@broken = true
|
|
1434
|
+
@sock&.to_io&.close
|
|
1435
|
+
rescue StandardError
|
|
1436
|
+
nil
|
|
1437
|
+
end
|
|
1438
|
+
|
|
1316
1439
|
# Re-dial if the transport died since the last statement, BEFORE anything
|
|
1317
1440
|
# is prepared on it.
|
|
1318
1441
|
#
|
|
@@ -1328,6 +1451,7 @@ module Skaidb
|
|
|
1328
1451
|
raise ConnectionError, "connection lost inside a transaction; not re-dialling until it ends" if @in_transaction
|
|
1329
1452
|
|
|
1330
1453
|
@prepared.clear
|
|
1454
|
+
@held.clear
|
|
1331
1455
|
begin
|
|
1332
1456
|
@sock&.close
|
|
1333
1457
|
rescue StandardError
|
|
@@ -1337,11 +1461,16 @@ module Skaidb
|
|
|
1337
1461
|
# Cleared BEFORE dialling: dial! issues USE, which runs a statement and
|
|
1338
1462
|
# would otherwise re-enter this method forever.
|
|
1339
1463
|
@broken = false
|
|
1464
|
+
dialled = false
|
|
1340
1465
|
begin
|
|
1341
1466
|
dial!
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1467
|
+
dialled = true
|
|
1468
|
+
ensure
|
|
1469
|
+
# Still down, so the next statement retries — or the dial was cut
|
|
1470
|
+
# short (an Interrupt, a Timeout) with the handshake's or Hello's
|
|
1471
|
+
# reply outstanding on the new socket, which the next statement
|
|
1472
|
+
# would read as its own.
|
|
1473
|
+
mark_broken! unless dialled
|
|
1345
1474
|
end
|
|
1346
1475
|
end
|
|
1347
1476
|
|
|
@@ -1352,38 +1481,43 @@ module Skaidb
|
|
|
1352
1481
|
end
|
|
1353
1482
|
|
|
1354
1483
|
# Read out the frames left over from a stream the caller walked away from,
|
|
1355
|
-
# so the connection is positioned at a request boundary again.
|
|
1484
|
+
# so the connection is positioned at a request boundary again.
|
|
1485
|
+
#
|
|
1356
1486
|
# Runs from an ensure, where an exception would replace whatever the
|
|
1357
1487
|
# caller's block was already unwinding with — so an ordinary failure
|
|
1358
1488
|
# marks the connection broken rather than raising. usable? then fails
|
|
1359
1489
|
# and ensure_live! re-dials before the next statement.
|
|
1360
1490
|
#
|
|
1361
|
-
#
|
|
1362
|
-
# catches StandardError, so
|
|
1363
|
-
#
|
|
1364
|
-
#
|
|
1365
|
-
#
|
|
1366
|
-
#
|
|
1367
|
-
#
|
|
1491
|
+
# An Interrupt, a Timeout or Thread#kill is not an ordinary failure: the
|
|
1492
|
+
# rescue below catches StandardError only, so those still leave through
|
|
1493
|
+
# here (rescuing Exception would hide a Ctrl-C). They leave the rest of
|
|
1494
|
+
# the stream on the socket, so the ensure marks the connection broken on
|
|
1495
|
+
# EVERY exit that did not reach the end of the stream. Left unmarked,
|
|
1496
|
+
# the next statements on the connection read the leftover chunks as
|
|
1497
|
+
# their replies, and after those the replies of the statements before
|
|
1498
|
+
# them.
|
|
1368
1499
|
#
|
|
1369
|
-
# There is
|
|
1370
|
-
#
|
|
1371
|
-
#
|
|
1372
|
-
#
|
|
1373
|
-
#
|
|
1500
|
+
# There is no read DEADLINE. A peer that is alive but silent blocks the
|
|
1501
|
+
# drain forever, holding @mutex — the same as every other read in this
|
|
1502
|
+
# driver, and `close` deliberately does not take @mutex so another
|
|
1503
|
+
# thread can break it. A genuinely dead socket (EOF, RST) exits
|
|
1504
|
+
# correctly.
|
|
1374
1505
|
def drain_stream!
|
|
1506
|
+
drained = false
|
|
1375
1507
|
loop do
|
|
1376
1508
|
tag = Reader.new(read_frame).u8
|
|
1377
|
-
|
|
1378
|
-
next if tag == 6 # RowsChunk: more to come
|
|
1509
|
+
next if tag == 6 # RowsChunk: more to come
|
|
1379
1510
|
|
|
1380
|
-
|
|
1511
|
+
# RowsEnd, or an Error that ended the stream: at a request boundary.
|
|
1512
|
+
# Anything else: where the reply ends is no longer known.
|
|
1513
|
+
drained = [7, 3].include?(tag)
|
|
1381
1514
|
break
|
|
1382
1515
|
end
|
|
1383
1516
|
rescue StandardError
|
|
1384
|
-
# An I/O failure
|
|
1385
|
-
|
|
1386
|
-
|
|
1517
|
+
# An I/O failure, or a frame too short to carry a tag: not drained.
|
|
1518
|
+
nil
|
|
1519
|
+
ensure
|
|
1520
|
+
mark_broken! unless drained
|
|
1387
1521
|
end
|
|
1388
1522
|
|
|
1389
1523
|
def read_exact(n)
|
|
@@ -1394,11 +1528,11 @@ module Skaidb
|
|
|
1394
1528
|
chunk = begin
|
|
1395
1529
|
@sock.read(n - buf.bytesize)
|
|
1396
1530
|
rescue StandardError => e
|
|
1397
|
-
|
|
1531
|
+
mark_broken!
|
|
1398
1532
|
raise ConnectionError, "read failed: #{e.message}"
|
|
1399
1533
|
end
|
|
1400
1534
|
if chunk.nil? || chunk.empty?
|
|
1401
|
-
|
|
1535
|
+
mark_broken!
|
|
1402
1536
|
raise ConnectionError, "connection closed by server"
|
|
1403
1537
|
end
|
|
1404
1538
|
|
|
@@ -1454,6 +1588,83 @@ module Skaidb
|
|
|
1454
1588
|
|
|
1455
1589
|
# -- query --
|
|
1456
1590
|
|
|
1591
|
+
# prepare_server's work, and how exec_params and exec_batch prepare: [id,
|
|
1592
|
+
# nparams] for +sql+, from this socket's statements or prepared now; nil
|
|
1593
|
+
# when the server declines the statement kind, so the caller falls back
|
|
1594
|
+
# to text binding.
|
|
1595
|
+
#
|
|
1596
|
+
# Statements prepared here on the driver's own account are a cache, least
|
|
1597
|
+
# recently used first. Before a new statement would take the count to
|
|
1598
|
+
# PREPARED_LIMIT the oldest of them is closed on the server (OP_CLOSE),
|
|
1599
|
+
# so the server's table (256 per connection) never fills however many
|
|
1600
|
+
# distinct statements a connection runs. Statements handed to the
|
|
1601
|
+
# application (+hold+) are never closed: it still holds their ids.
|
|
1602
|
+
#
|
|
1603
|
+
# A server that refuses because its table is full all the same — the
|
|
1604
|
+
# application holds that many — is a statement error, QueryError. It is
|
|
1605
|
+
# NOT answered with the text fallback: that would send the values as
|
|
1606
|
+
# literals, a Time as a number and bytes as a hex string, without a word.
|
|
1607
|
+
def prepare_cached(sql, hold: false)
|
|
1608
|
+
raise ConnectionError, "connection is closed" if @closed
|
|
1609
|
+
# Before the cache is read: a re-dial empties it, and a cached id must
|
|
1610
|
+
# not send a statement down a socket that is broken.
|
|
1611
|
+
ensure_live!
|
|
1612
|
+
hit = @held[sql]
|
|
1613
|
+
return hit if hit
|
|
1614
|
+
|
|
1615
|
+
if (hit = @prepared.delete(sql))
|
|
1616
|
+
(hold ? @held : @prepared)[sql] = hit # held from now on, or most recently used
|
|
1617
|
+
@handles[hit[0]] = sql if hold
|
|
1618
|
+
return hit
|
|
1619
|
+
end
|
|
1620
|
+
|
|
1621
|
+
close_prepared(@prepared.shift[1][0]) while @held.length + @prepared.length >= PREPARED_LIMIT && !@prepared.empty?
|
|
1622
|
+
sql_bytes = sql.dup.force_encoding("UTF-8").b
|
|
1623
|
+
req = [2].pack("C") + [sql_bytes.bytesize].pack("V") + sql_bytes
|
|
1624
|
+
reader = request(req)
|
|
1625
|
+
tag = reader.u8
|
|
1626
|
+
case tag
|
|
1627
|
+
when 4
|
|
1628
|
+
id = (@serial << 32) | reader.u32
|
|
1629
|
+
nparams = reader.u16
|
|
1630
|
+
@handles[id] = sql if hold
|
|
1631
|
+
(hold ? @held : @prepared)[sql] = [id, nparams]
|
|
1632
|
+
when 3
|
|
1633
|
+
msg = reader.text
|
|
1634
|
+
raise Skaidb.statement_error(msg) if msg.start_with?("too many prepared statements")
|
|
1635
|
+
|
|
1636
|
+
# Refused: DDL/session statements cannot be prepared, and an old
|
|
1637
|
+
# server answers "unknown opcode". Kept for the caller's error message.
|
|
1638
|
+
@last_prepare_error = msg
|
|
1639
|
+
nil
|
|
1640
|
+
else
|
|
1641
|
+
raise QueryError, "unexpected prepare response tag #{tag}"
|
|
1642
|
+
end
|
|
1643
|
+
end
|
|
1644
|
+
|
|
1645
|
+
# Free a prepared statement's slot on the server (OP_CLOSE). What it
|
|
1646
|
+
# answers does not matter: the id is forgotten here either way.
|
|
1647
|
+
def close_prepared(id)
|
|
1648
|
+
request([4].pack("C") + [id & WIRE_ID].pack("V"))
|
|
1649
|
+
nil
|
|
1650
|
+
end
|
|
1651
|
+
|
|
1652
|
+
# exec_prepared's id as the current socket knows it: the id itself when
|
|
1653
|
+
# this socket issued it; otherwise the id of the same statement prepared
|
|
1654
|
+
# here again — never a number another socket gave out.
|
|
1655
|
+
def current_id(id)
|
|
1656
|
+
ensure_live!
|
|
1657
|
+
return id if id >> 32 == @serial
|
|
1658
|
+
|
|
1659
|
+
sql = @handles[id]
|
|
1660
|
+
raise QueryError, "prepared statement id #{id} was not issued by this connection" if sql.nil?
|
|
1661
|
+
|
|
1662
|
+
entry = prepare_cached(sql, hold: true)
|
|
1663
|
+
raise QueryError, "the statement could not be prepared again: #{@last_prepare_error}" if entry.nil?
|
|
1664
|
+
|
|
1665
|
+
entry[0]
|
|
1666
|
+
end
|
|
1667
|
+
|
|
1457
1668
|
# The transaction helper's clean-up after a block that did not run to its
|
|
1458
1669
|
# end. It runs while something else is unwinding, so it never raises: a
|
|
1459
1670
|
# ROLLBACK the server refuses (one before 0.347 answers "no transaction in
|
|
@@ -1467,6 +1678,48 @@ module Skaidb
|
|
|
1467
1678
|
nil
|
|
1468
1679
|
end
|
|
1469
1680
|
|
|
1681
|
+
# Internal: raised into the Fiber of a stream the transaction helper's
|
|
1682
|
+
# block left open, to unwind it. Not a StandardError, so nothing between
|
|
1683
|
+
# the stream and the top of that Fiber takes it for an ordinary failure.
|
|
1684
|
+
class StreamAbandoned < Exception; end # rubocop:disable Lint/InheritException
|
|
1685
|
+
private_constant :StreamAbandoned
|
|
1686
|
+
|
|
1687
|
+
# The transaction helper's check once its block is done: is a stream the
|
|
1688
|
+
# block opened still in flight? That is an Enumerator from +stream+
|
|
1689
|
+
# advanced with +next+ and not read to its end: its Fiber holds the
|
|
1690
|
+
# socket in the middle of the reply (and the connection's lock), so
|
|
1691
|
+
# neither COMMIT nor ROLLBACK can be sent, and the transaction would stay
|
|
1692
|
+
# open on a live session — where a statement sent after the stream was
|
|
1693
|
+
# read to its end would run inside it, uncommitted.
|
|
1694
|
+
#
|
|
1695
|
+
# So the session goes: the connection is marked broken, which closes the
|
|
1696
|
+
# socket, and the server drops the transaction with it. Then the stream
|
|
1697
|
+
# is unwound — an exception raised into its Fiber runs the stream's
|
|
1698
|
+
# ensure, which finds nothing to drain, clears the busy flag and releases
|
|
1699
|
+
# the lock — so the connection is what it is after any lost connection:
|
|
1700
|
+
# the next statement re-dials. The Fiber must not be left where it is:
|
|
1701
|
+
# it owns the lock, and an Enumerator dropped in that state takes the
|
|
1702
|
+
# lock's owner with it when it is collected (which Ruby 3.3 does not
|
|
1703
|
+
# survive).
|
|
1704
|
+
#
|
|
1705
|
+
# A stream whose Fiber cannot be reached (it runs in another thread)
|
|
1706
|
+
# leaves the connection closed for good instead: a re-dial would hand
|
|
1707
|
+
# that Fiber, when it is resumed, a new socket to read its old reply
|
|
1708
|
+
# from. Returns whether a stream was in flight.
|
|
1709
|
+
def abandon_open_stream
|
|
1710
|
+
return false unless @streaming
|
|
1711
|
+
|
|
1712
|
+
fiber = @stream_fiber
|
|
1713
|
+
mark_broken!
|
|
1714
|
+
begin
|
|
1715
|
+
fiber.raise(StreamAbandoned) if fiber&.alive? && !fiber.equal?(Fiber.current)
|
|
1716
|
+
rescue StreamAbandoned, StandardError
|
|
1717
|
+
nil # unwound; or not reachable (FiberError), which the next line sees
|
|
1718
|
+
end
|
|
1719
|
+
close if @streaming
|
|
1720
|
+
true
|
|
1721
|
+
end
|
|
1722
|
+
|
|
1470
1723
|
def run(sql, consistency)
|
|
1471
1724
|
raise ConnectionError, "connection is closed" if @closed
|
|
1472
1725
|
ensure_live!
|
|
@@ -1476,9 +1729,34 @@ module Skaidb
|
|
|
1476
1729
|
reader = request(req)
|
|
1477
1730
|
res = parse_response(reader)
|
|
1478
1731
|
note_statement(sql)
|
|
1732
|
+
note_use(sql)
|
|
1479
1733
|
res
|
|
1480
1734
|
end
|
|
1481
1735
|
|
|
1736
|
+
# +USE [DATABASE] <name>+ with an optional +;+ — the name bare, or in
|
|
1737
|
+
# double quotes with "" for a quote inside.
|
|
1738
|
+
USE_STATEMENT = /\A\s*USE\s+(?:DATABASE\s+)?("(?:[^"]|"")+"|[[:alpha:]_][[:alnum:]_]*)\s*;?\s*\z/i.freeze
|
|
1739
|
+
# A +--+ comment, which the server skips to the end of its line — or a
|
|
1740
|
+
# double-quoted name or single-quoted string, inside which two dashes are
|
|
1741
|
+
# not a comment (an unclosed quote runs to the end of the text).
|
|
1742
|
+
COMMENT_OR_QUOTED = /"[^"]*(?:"|\z)|'[^']*(?:'|\z)|--[^\n]*/.freeze
|
|
1743
|
+
private_constant :USE_STATEMENT, :COMMENT_OR_QUOTED
|
|
1744
|
+
|
|
1745
|
+
# A statement the server answered with success: when it is a USE, the
|
|
1746
|
+
# session is in that database now, and a re-dial must put it back there
|
|
1747
|
+
# (a new session starts in the default database, where a table of the
|
|
1748
|
+
# same name is another table). Comments are skipped as the server skips
|
|
1749
|
+
# them (+USE app -- the app database+). The name is kept as it was
|
|
1750
|
+
# written and sent again the same way, in a USE of its own. Text that is
|
|
1751
|
+
# not exactly one such statement is left alone.
|
|
1752
|
+
def note_use(sql)
|
|
1753
|
+
text = sql.gsub(COMMENT_OR_QUOTED) { |part| part.start_with?("--") ? "" : part }
|
|
1754
|
+
m = USE_STATEMENT.match(text)
|
|
1755
|
+
@session_use = m[1] if m
|
|
1756
|
+
rescue ArgumentError, EncodingError
|
|
1757
|
+
nil # bytes the pattern cannot read
|
|
1758
|
+
end
|
|
1759
|
+
|
|
1482
1760
|
# The request +stream+ sends. Without parameters: OP_QUERY_STREAM with
|
|
1483
1761
|
# the SQL as given, byte for byte what every server understands. With
|
|
1484
1762
|
# them: OP_QUERY_STREAM_PARAMS (server >= 0.330), $N rewritten to the
|
|
@@ -1523,8 +1801,8 @@ module Skaidb
|
|
|
1523
1801
|
# Interrupt, a Timeout or Thread#kill, which no rescue here sees — means
|
|
1524
1802
|
# the reply is still on its way: the socket no longer sits at a request
|
|
1525
1803
|
# boundary, and the next statement would read this one's reply as its
|
|
1526
|
-
# own. So the connection is marked broken
|
|
1527
|
-
# drops it, and the next statement re-dials.
|
|
1804
|
+
# own. So the connection is marked broken and its socket closed: usable?
|
|
1805
|
+
# turns false, a pool drops it, and the next statement re-dials.
|
|
1528
1806
|
def round_trip(req)
|
|
1529
1807
|
answered = false
|
|
1530
1808
|
write_frame(req)
|
|
@@ -1532,7 +1810,7 @@ module Skaidb
|
|
|
1532
1810
|
answered = true
|
|
1533
1811
|
frame
|
|
1534
1812
|
ensure
|
|
1535
|
-
|
|
1813
|
+
mark_broken! unless answered
|
|
1536
1814
|
end
|
|
1537
1815
|
|
|
1538
1816
|
def observe_failure
|
|
@@ -1847,6 +2125,9 @@ module Skaidb
|
|
|
1847
2125
|
# fine locally, so check before handing it out.
|
|
1848
2126
|
close_quietly(conn)
|
|
1849
2127
|
next
|
|
2128
|
+
elsif !in_pool_database?(conn)
|
|
2129
|
+
close_quietly(conn)
|
|
2130
|
+
next
|
|
1850
2131
|
end
|
|
1851
2132
|
@mutex.synchronize do
|
|
1852
2133
|
m = @members[conn.pool_member]
|
|
@@ -2044,6 +2325,17 @@ module Skaidb
|
|
|
2044
2325
|
nil
|
|
2045
2326
|
end
|
|
2046
2327
|
|
|
2328
|
+
# An idle connection its last borrower moved with a USE statement goes
|
|
2329
|
+
# back to the pool's +database:+ before it is handed out again; false
|
|
2330
|
+
# when that fails (the connection is then dropped). A pool without
|
|
2331
|
+
# +database:+ has no database to put it back in.
|
|
2332
|
+
def in_pool_database?(conn)
|
|
2333
|
+
conn.reenter_dial_database
|
|
2334
|
+
true
|
|
2335
|
+
rescue Error
|
|
2336
|
+
false
|
|
2337
|
+
end
|
|
2338
|
+
|
|
2047
2339
|
def log(message)
|
|
2048
2340
|
@logger&.warn("skaidb pool: #{message}")
|
|
2049
2341
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: skaidb
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.5.
|
|
4
|
+
version: 1.5.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- skaidb contributors
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-11-
|
|
11
|
+
date: 2026-11-16 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bigdecimal
|