skaidb 1.5.1 → 1.5.2
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 +93 -0
- data/README.md +81 -18
- data/docs/api.md +118 -25
- data/docs/getting-started.md +4 -2
- data/docs/pooling.md +33 -13
- data/lib/skaidb.rb +595 -185
- 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: 818a2b6adaefda4048711f0eed371dd8ab73a05ec59edbd46cef8e390e2cc909
|
|
4
|
+
data.tar.gz: 7dbffebfc11f59072c55ae18f8a350a5ae0e4e21446449d40db074fc891b5d12
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9a97060e9ff4ae284d56eeeaf8f06ab1c817726696ada1dda2ef2eb5137fddbd11385d6cdb92493612740b79a87bebe1c9e285102aa63f9b053b9a681793c3eb
|
|
7
|
+
data.tar.gz: 2a992604955c5ceba123fe3b5ea4b9a054b3d7360d8caf1c5de839a8c9bda79e5129b4152d30e74ccf754bd230a7f574c699f0672dd79ba49200d9772a1911d1
|
data/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,98 @@ 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.2] - 2026-11-16
|
|
8
|
+
|
|
9
|
+
### Fixed
|
|
10
|
+
- **A connection the server closed while it sat idle costs no statement.**
|
|
11
|
+
A member restart, a failover or an idle timeout closes idle connections,
|
|
12
|
+
and the first statement on each of them raised `ConnectionError` — once
|
|
13
|
+
per idle connection, after every restart; only the statement after it
|
|
14
|
+
re-dialled. The driver now looks at the socket before it writes a request
|
|
15
|
+
(one peek that does not wait, no round trip): an idle connection has
|
|
16
|
+
nothing to read, so an end-of-file, a reset or bytes nobody asked for
|
|
17
|
+
mean it is gone, and the statement runs on a new connection with the
|
|
18
|
+
session restored on it. A pool does not hand out an idle connection that
|
|
19
|
+
was lost. Under TLS the TCP socket is looked at, and bytes the TLS layer
|
|
20
|
+
has already decrypted count too. A connection lost *under* a statement —
|
|
21
|
+
request written, no reply — still raises `ConnectionError`, since the
|
|
22
|
+
statement may have run.
|
|
23
|
+
- **A pool hands every borrower its own session state.** A pooled
|
|
24
|
+
connection kept what its last borrower had done to the session: a `USE`
|
|
25
|
+
(on a pool without `database:`), a `SET CONSISTENCY`, a `SET SCAN BUDGET`,
|
|
26
|
+
a transaction left open. The next borrower read another database's
|
|
27
|
+
tables, was refused for someone else's scan budget, or had its writes
|
|
28
|
+
swallowed by a transaction it knew nothing of. A connection now reaches
|
|
29
|
+
each borrower in the pool's database (`database:`, or `default` when the
|
|
30
|
+
pool names none) with no transaction open and neither setting: what a
|
|
31
|
+
borrower changed is put back when the connection is returned (`ROLLBACK`,
|
|
32
|
+
`USE`, `SET SCAN BUDGET DEFAULT`, `SET CONSISTENCY DEFAULT`, only the
|
|
33
|
+
ones needed), and a connection that cannot be put back is closed.
|
|
34
|
+
- **`SET CONSISTENCY` and `SET SCAN BUDGET` survive a re-dial.** Both are
|
|
35
|
+
state of the server's session, and a re-dialled connection came back
|
|
36
|
+
without them: a scan the application had limited ran unlimited, a read
|
|
37
|
+
asked for at `ALL` ran at the request's own level. The last one of each
|
|
38
|
+
the server accepted is now sent again on every dial, after the database
|
|
39
|
+
and before the statement; `… DEFAULT` clears it. A setting the new
|
|
40
|
+
session refuses fails the statement instead of letting it run without.
|
|
41
|
+
- **A transaction written by hand is a transaction to the driver too.**
|
|
42
|
+
Only `transaction` kept a lost connection from being replaced. After
|
|
43
|
+
`exec("BEGIN")` a lost connection was re-dialled, and the rest of the
|
|
44
|
+
statements — and the `COMMIT` — ran on a new session, each committed on
|
|
45
|
+
its own. A `BEGIN` the server accepted is now followed until `COMMIT` or
|
|
46
|
+
`ROLLBACK` ends it: while it is open a lost connection is not replaced,
|
|
47
|
+
every statement raises `ConnectionError`, `ROLLBACK` succeeds without
|
|
48
|
+
being sent and `COMMIT` raises (`COMMIT was not sent and nothing was
|
|
49
|
+
committed`). A connection lost under the `COMMIT` itself says that the
|
|
50
|
+
outcome is not known, from `transaction` as well.
|
|
51
|
+
- **A `USE` gets a connection out of a database that is gone.** When the
|
|
52
|
+
session's database had been dropped and the connection was lost, every
|
|
53
|
+
statement failed at the re-dial's `USE` — and so did a `USE` of another
|
|
54
|
+
database, because the old one was entered first. A `USE` is now sent to
|
|
55
|
+
the new session without that.
|
|
56
|
+
- **`transaction` cut short between two statements leaves no transaction
|
|
57
|
+
open.** A `Timeout` or an `Interrupt` landing just after the opening
|
|
58
|
+
statement's reply, or with the block done and `COMMIT` not yet written,
|
|
59
|
+
left the transaction open on a connection that went on being used. The
|
|
60
|
+
socket is now closed unless the server has answered a `ROLLBACK` or the
|
|
61
|
+
`COMMIT`.
|
|
62
|
+
- **A reply that cannot be read drops the connection.** An unknown
|
|
63
|
+
response tag or a message shorter than its own fields say raised
|
|
64
|
+
`ConnectionError` and left the connection usable, with whatever followed
|
|
65
|
+
on the socket for the next statement to read as its reply.
|
|
66
|
+
- A stream cut short between its header frame and its first row (a
|
|
67
|
+
`Timeout`, an `Interrupt`) left the rest of the stream on a connection
|
|
68
|
+
that looked idle. It is now marked broken.
|
|
69
|
+
- A dial that moved on from an endpoint whose handshake failed to one that
|
|
70
|
+
answered left the connection marked broken: `usable?` was false on a
|
|
71
|
+
healthy socket, the next statement dialled again, and a pool threw the
|
|
72
|
+
connection away and dialled once more.
|
|
73
|
+
- A connect that failed after its handshake (a `database:` the server
|
|
74
|
+
refuses, a connect cut short) left its socket open until the collector
|
|
75
|
+
closed it. It is closed at once.
|
|
76
|
+
- Under `ruby -w` on Ruby 2.7, a dial whose first endpoint refused the
|
|
77
|
+
connection printed `instance variable @sock not initialized`.
|
|
78
|
+
|
|
79
|
+
### Changed
|
|
80
|
+
- On a pool without a `database:` option a borrower's `USE` no longer
|
|
81
|
+
carries over to later borrowers: the connection goes back to `default`
|
|
82
|
+
when it is returned. Send `USE` after each checkout, or give the
|
|
83
|
+
database a pool of its own (`database:`).
|
|
84
|
+
- A pool puts a connection back in its own state when the connection is
|
|
85
|
+
returned, not when it is next handed out; a borrower that left a
|
|
86
|
+
transaction open has it rolled back there.
|
|
87
|
+
- The statement that finds its connection closed by a restart no longer
|
|
88
|
+
raises `ConnectionError`: code that caught it and ran the statement again
|
|
89
|
+
keeps working, and no longer needs to.
|
|
90
|
+
- After a connection is lost inside a hand-written `BEGIN` … `COMMIT`, every
|
|
91
|
+
statement raises `ConnectionError` until `ROLLBACK` or `COMMIT` ends the
|
|
92
|
+
transaction; they used to run on a new session.
|
|
93
|
+
|
|
94
|
+
### Added
|
|
95
|
+
- `Connection#lost_while_idle?` — the look at the socket on its own.
|
|
96
|
+
- `examples/transaction.rb`; the opt-in live test covers transactions and
|
|
97
|
+
session state.
|
|
98
|
+
|
|
7
99
|
## [1.5.1] - 2026-11-16
|
|
8
100
|
|
|
9
101
|
### Fixed
|
|
@@ -353,6 +445,7 @@ batches, a 2 500-row stream, error handling, and the `drivers` table showing
|
|
|
353
445
|
because it is a bundled gem since Ruby 3.4.
|
|
354
446
|
- The example moved to `examples/basic.rb`.
|
|
355
447
|
|
|
448
|
+
[1.5.2]: https://github.com/porcupin26/skaidb-ruby/compare/v1.5.1...v1.5.2
|
|
356
449
|
[1.5.1]: https://github.com/porcupin26/skaidb-ruby/compare/v1.5.0...v1.5.1
|
|
357
450
|
[1.5.0]: https://github.com/porcupin26/skaidb-ruby/compare/v1.4.1...v1.5.0
|
|
358
451
|
[1.4.1]: https://github.com/porcupin26/skaidb-ruby/compare/v1.4.0...v1.4.1
|
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.2/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.2.gem`.
|
|
65
65
|
|
|
66
66
|
## Quick start
|
|
67
67
|
|
|
@@ -147,14 +147,32 @@ 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
149
|
`Hello`, re-enters the session's database, and drops the connection's prepared
|
|
150
|
-
statements.
|
|
151
|
-
|
|
150
|
+
statements.
|
|
151
|
+
|
|
152
|
+
A connection the server closed **while it sat idle** — a member restart, a
|
|
153
|
+
failover, an idle timeout — costs no statement. Before a request is written
|
|
154
|
+
the driver looks at the socket (one peek that does not wait, no round trip): an idle connection
|
|
155
|
+
has nothing to read, so an end-of-file, a reset or stray bytes there mean it
|
|
156
|
+
is gone, and the statement goes out on a new connection instead. A
|
|
157
|
+
connection lost **under a statement** — the request was written and the
|
|
158
|
+
reply did not come — raises `Skaidb::ConnectionError`: the statement may
|
|
159
|
+
have run, so the driver does not send it again. The one after it re-dials.
|
|
152
160
|
|
|
153
161
|
The database re-entered is the one the session was last in: `database:`, or
|
|
154
162
|
the one a later `USE <name>` statement sent with `exec` moved it to. It is
|
|
155
163
|
entered before anything else is sent on the new socket, and if the server
|
|
156
164
|
refuses it the statement raises that error instead of running in another
|
|
157
|
-
database.
|
|
165
|
+
database. The one statement that does not wait for it is a `USE`: when the
|
|
166
|
+
session's database is gone — dropped while the connection was down — a
|
|
167
|
+
`USE` of another database is sent to the new session without the old one
|
|
168
|
+
being entered first, so the connection can always be moved out of it.
|
|
169
|
+
|
|
170
|
+
Session settings come back with it. The last `SET CONSISTENCY …` and the
|
|
171
|
+
last `SET SCAN BUDGET …` the server accepted are sent again on the new
|
|
172
|
+
socket, after the database and before the statement; one cleared with
|
|
173
|
+
`… DEFAULT` is not. If the server refuses a setting there, the statement
|
|
174
|
+
raises that error: it never runs under other settings than the ones you
|
|
175
|
+
set.
|
|
158
176
|
|
|
159
177
|
### Timeout
|
|
160
178
|
|
|
@@ -210,11 +228,19 @@ conn.stream("SELECT …", consistency: :one) { |row| … }
|
|
|
210
228
|
integer and raises `Skaidb::Error` otherwise. DDL is always run at quorum by
|
|
211
229
|
the server regardless of this setting.
|
|
212
230
|
|
|
231
|
+
These are the level each request carries. A `SET CONSISTENCY { ONE | QUORUM
|
|
232
|
+
| ALL }` statement is something else: it makes the server run every
|
|
233
|
+
statement of the session at that level whatever the request says, until
|
|
234
|
+
`SET CONSISTENCY DEFAULT` (server ≥ 0.349). The driver follows it, and
|
|
235
|
+
`SET SCAN BUDGET`, across re-dials — see
|
|
236
|
+
[Seeds and failover](#seeds-and-failover).
|
|
237
|
+
|
|
213
238
|
### Health
|
|
214
239
|
|
|
215
240
|
- `conn.usable?` — no round-trip. `true` unless the connection is closed, was
|
|
216
|
-
left out of sync by a transport error, a
|
|
217
|
-
undrainable abandoned stream, or is currently
|
|
241
|
+
left out of sync by a transport error, a reply that could not be read, a
|
|
242
|
+
round trip cut short or an undrainable abandoned stream, or is currently
|
|
243
|
+
mid-stream.
|
|
218
244
|
- `conn.closed` / `conn.finished?` — `true` after `close`.
|
|
219
245
|
|
|
220
246
|
## Statements and parameters
|
|
@@ -353,7 +379,16 @@ pool.close # closes idle connections
|
|
|
353
379
|
blocks — when no idle connection is available a new one is dialed, and a
|
|
354
380
|
returned connection beyond `maxsize` is closed. Connections are validated with
|
|
355
381
|
`usable?` on checkout and check-in, so one broken by a transport error or an
|
|
356
|
-
undrained stream is closed and replaced
|
|
382
|
+
undrained stream is closed and replaced; and an idle connection the server
|
|
383
|
+
closed while it sat in the pool is found by a look at its socket (no round
|
|
384
|
+
trip) and replaced before it is handed out.
|
|
385
|
+
|
|
386
|
+
Every borrower gets the pool's own session state: the pool's database
|
|
387
|
+
(`database:`, or `default` when it names none), no transaction open, and no
|
|
388
|
+
`SET CONSISTENCY` or `SET SCAN BUDGET` of an earlier borrower. Whatever a
|
|
389
|
+
borrower changed is put back when the connection is returned — a `ROLLBACK`,
|
|
390
|
+
a `USE`, a `SET … DEFAULT`, only the ones needed — and a connection that
|
|
391
|
+
cannot be put back is closed.
|
|
357
392
|
|
|
358
393
|
The pool spreads its connections across the members by the rules every skaidb
|
|
359
394
|
driver shares
|
|
@@ -455,8 +490,10 @@ StandardError
|
|
|
455
490
|
[Transactions](#transactions).
|
|
456
491
|
- A transport failure (`ConnectionError`) marks the connection broken and
|
|
457
492
|
closes its socket; the next statement re-dials, or a pool replaces it. So
|
|
458
|
-
does
|
|
459
|
-
|
|
493
|
+
does a reply that cannot be read (a tag the protocol does not have, a
|
|
494
|
+
message shorter than its own fields say), and so does anything else that
|
|
495
|
+
cuts a statement short between its request and its reply (an `Interrupt`,
|
|
496
|
+
a `Timeout`); that exception propagates as it is.
|
|
460
497
|
- `ArgumentError` for `Pool.new(maxsize: 0)` or an unknown
|
|
461
498
|
`load_balancing`/`idle_order`, for `Skaidb::Uuid.new` of a string that
|
|
462
499
|
is not a UUID, for `stream` parameters that are not an Array, and for
|
|
@@ -516,6 +553,12 @@ What else `transaction` does:
|
|
|
516
553
|
still on its way, so nothing more is written to that socket. Either way
|
|
517
554
|
the socket is closed at once, and the server drops the transaction with
|
|
518
555
|
it.
|
|
556
|
+
- Such an exception can also land between two statements — just after the
|
|
557
|
+
opening statement's reply, or with the block done and `COMMIT` not yet
|
|
558
|
+
written. `transaction` never leaves a transaction open on a connection
|
|
559
|
+
that goes on being used: unless the server has answered a `ROLLBACK` or
|
|
560
|
+
the `COMMIT`, the socket is closed, the server drops the transaction with
|
|
561
|
+
it, and the connection's next statement re-dials.
|
|
519
562
|
- A stream the block leaves open — an Enumerator from `stream` advanced with
|
|
520
563
|
`next` or `peek` and not read to its end — sits in the middle of its
|
|
521
564
|
reply, so neither `COMMIT` nor `ROLLBACK` can be sent. The socket is
|
|
@@ -537,7 +580,7 @@ begin
|
|
|
537
580
|
conn.exec_params("UPDATE accounts SET balance = balance + $1 WHERE id = $2", [10, 2])
|
|
538
581
|
rescue StandardError
|
|
539
582
|
begin
|
|
540
|
-
conn.exec("ROLLBACK")
|
|
583
|
+
conn.exec("ROLLBACK")
|
|
541
584
|
rescue Skaidb::Error
|
|
542
585
|
nil # the clean-up never replaces the error it follows
|
|
543
586
|
end
|
|
@@ -546,15 +589,34 @@ end
|
|
|
546
589
|
conn.exec("COMMIT") # Skaidb::TransactionConflict: nothing was applied; run it again from BEGIN
|
|
547
590
|
```
|
|
548
591
|
|
|
592
|
+
The driver follows a transaction written this way: a statement the server
|
|
593
|
+
accepted whose first word is `BEGIN` opens it, and `COMMIT [TRANSACTION]` or
|
|
594
|
+
`ROLLBACK [TRANSACTION]` ends it once the server has answered, with success
|
|
595
|
+
or with an error (sent with `exec`, `query`, `exec_params` or `stream`; `--`
|
|
596
|
+
comments are skipped). While it is open **a lost connection is not
|
|
597
|
+
replaced**, exactly as inside `transaction`: a new session has no
|
|
598
|
+
transaction open, and the rest of the statements, and the `COMMIT`, would
|
|
599
|
+
each be committed on their own there.
|
|
600
|
+
|
|
601
|
+
- The statement under which the connection is lost raises
|
|
602
|
+
`Skaidb::ConnectionError`, and so does every statement after it; none is
|
|
603
|
+
sent.
|
|
604
|
+
- `ROLLBACK` then succeeds without being sent — the server discarded the
|
|
605
|
+
transaction with the connection — and `COMMIT` raises `ConnectionError`
|
|
606
|
+
(`connection lost inside a transaction: COMMIT was not sent and nothing
|
|
607
|
+
was committed`). Either one ends the transaction for the driver; the next
|
|
608
|
+
statement re-dials as after any lost connection.
|
|
609
|
+
- A connection lost under the `COMMIT` itself leaves the outcome unknown —
|
|
610
|
+
the server may have applied it before its reply went missing — and the
|
|
611
|
+
`ConnectionError` says so (`connection lost under COMMIT: whether the
|
|
612
|
+
transaction was committed is not known`).
|
|
613
|
+
|
|
549
614
|
Two rules hold however the statements are sent:
|
|
550
615
|
|
|
551
616
|
- **One connection for the whole transaction.** Statements sent on another
|
|
552
617
|
connection are not part of it, so with a pool check one connection out
|
|
553
618
|
(`pool.with`) and keep it until the `COMMIT`; a statement another thread
|
|
554
|
-
sends on that connection meanwhile is part of it.
|
|
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?`.
|
|
619
|
+
sends on that connection meanwhile is part of it.
|
|
558
620
|
- **Stop if `BEGIN` fails.** No transaction is open, so every statement sent
|
|
559
621
|
after it would be committed on its own.
|
|
560
622
|
|
|
@@ -579,7 +641,7 @@ frame after every handshake. It shows up as `client_name = 'ruby'` /
|
|
|
579
641
|
|
|
580
642
|
```sql
|
|
581
643
|
SELECT client_name, client_version FROM drivers;
|
|
582
|
-
-- ruby | 1.5.
|
|
644
|
+
-- ruby | 1.5.2
|
|
583
645
|
```
|
|
584
646
|
|
|
585
647
|
The server records the row asynchronously, so a `SELECT` immediately after
|
|
@@ -629,11 +691,12 @@ fails when the vendored `vectors.json` differs from the published
|
|
|
629
691
|
|
|
630
692
|
[`examples/`](https://github.com/porcupin26/skaidb-ruby/tree/main/examples)
|
|
631
693
|
contains runnable scripts: `basic.rb`, `prepared_batch.rb`, `stream.rb`,
|
|
632
|
-
`pool.rb`, `tls.rb`, `subscribe.rb`. Each takes
|
|
694
|
+
`transaction.rb`, `pool.rb`, `tls.rb`, `subscribe.rb`. Each takes
|
|
633
695
|
`host port user password` on the command line and defaults to
|
|
634
696
|
`localhost:7000`; the arguments after those differ per script:
|
|
635
697
|
|
|
636
|
-
- `basic.rb`, `prepared_batch.rb`, `stream.rb`, `pool.rb`:
|
|
698
|
+
- `basic.rb`, `prepared_batch.rb`, `stream.rb`, `transaction.rb`, `pool.rb`:
|
|
699
|
+
`[database]`
|
|
637
700
|
- `subscribe.rb`: `[database] [stream]` (the stream defaults to
|
|
638
701
|
`orders_stream`)
|
|
639
702
|
- `tls.rb`: `[ca.crt]`, a CA file to trust; without it the script uses the
|
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. Every re-dial re-enters the session's database: this one, or the one a later `USE` statement moved the session to. |
|
|
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 — and sends the session's `SET CONSISTENCY` / `SET SCAN BUDGET` again (see [Reconnect](#reconnect)). |
|
|
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`. |
|
|
@@ -128,6 +128,12 @@ yields the connection, then sends `COMMIT`.
|
|
|
128
128
|
transaction ends with that socket, which is closed at once — the server
|
|
129
129
|
drops the transaction with it — and which the next statement after
|
|
130
130
|
`transaction` replaces.
|
|
131
|
+
- The same exception landing between two statements — just after the
|
|
132
|
+
opening statement's reply, or with the block done and `COMMIT` not yet
|
|
133
|
+
written — finds no round trip under way. Unless the server has answered a
|
|
134
|
+
`ROLLBACK` or the `COMMIT`, `transaction` closes the socket on its way
|
|
135
|
+
out, so no transaction stays open on a connection that is still
|
|
136
|
+
`usable?`; the exception propagates as it is.
|
|
131
137
|
- A stream the block leaves open (an Enumerator from `stream` advanced with
|
|
132
138
|
`next`/`peek` and not read to its end) sits in the middle of its reply, so
|
|
133
139
|
neither `COMMIT` nor `ROLLBACK` can be sent. The connection is marked
|
|
@@ -146,14 +152,48 @@ yields the connection, then sends `COMMIT`.
|
|
|
146
152
|
The whole transaction runs on this one connection, so no other thread may
|
|
147
153
|
use it meanwhile.
|
|
148
154
|
|
|
155
|
+
**A transaction written by hand** — `exec("BEGIN")`, statements,
|
|
156
|
+
`exec("COMMIT")` — is followed too. A statement the server accepted whose
|
|
157
|
+
first word is `BEGIN` (`BEGIN`, `BEGIN TRANSACTION`, `BEGIN ATOMIC …`) opens
|
|
158
|
+
it; a statement that is exactly `COMMIT [TRANSACTION]` or
|
|
159
|
+
`ROLLBACK [TRANSACTION]` ends it once the server has answered, with success
|
|
160
|
+
or with an error. They are read from `exec`, `query`, `exec_params` and
|
|
161
|
+
`stream`, with `--` comments skipped and an optional trailing `;`. While
|
|
162
|
+
the transaction is open:
|
|
163
|
+
|
|
164
|
+
- a lost connection is not replaced. The statement under which it is lost
|
|
165
|
+
— or before which it is found lost, see [Reconnect](#reconnect) — raises
|
|
166
|
+
`ConnectionError`, and so does every further statement, by whichever call
|
|
167
|
+
(`connection lost inside a transaction: …`); none is sent on a new
|
|
168
|
+
session, where it would be committed on its own;
|
|
169
|
+
- `ROLLBACK` returns its usual `:ddl` Result without being sent (the server
|
|
170
|
+
discarded the transaction with the connection), and `COMMIT` raises
|
|
171
|
+
`ConnectionError` (`… COMMIT was not sent and nothing was committed`).
|
|
172
|
+
Either one ends the transaction; the next statement re-dials;
|
|
173
|
+
- a connection lost under the `COMMIT` itself raises `ConnectionError`
|
|
174
|
+
`connection lost under COMMIT: whether the transaction was committed is
|
|
175
|
+
not known (…)` — from `transaction` as well — and the transaction is over
|
|
176
|
+
for the driver;
|
|
177
|
+
- a `COMMIT` or `ROLLBACK` cut short before the server answered it (an
|
|
178
|
+
`Interrupt`, a `Timeout`) closes the socket, so the transaction does not
|
|
179
|
+
stay open behind a statement the caller was told had failed.
|
|
180
|
+
|
|
181
|
+
`transaction` notes its own `BEGIN` and `COMMIT` the same way and leaves
|
|
182
|
+
nothing noted when it returns.
|
|
183
|
+
|
|
149
184
|
### State
|
|
150
185
|
|
|
151
186
|
- `consistency` / `consistency=` — the default level (`0`/`1`/`2`; the setter
|
|
152
187
|
accepts anything `Consistency.resolve` does).
|
|
153
|
-
- `usable?` — `false` once closed, once a transport error broke the socket
|
|
188
|
+
- `usable?` — `false` once closed, once a transport error broke the socket,
|
|
189
|
+
a reply could not be read
|
|
154
190
|
or a round trip was cut short with its reply outstanding, or while a
|
|
155
191
|
stream is in flight (including one abandoned through external iteration).
|
|
156
|
-
No round-trip.
|
|
192
|
+
No round-trip, and no look at the socket: what the driver already knows.
|
|
193
|
+
- `lost_while_idle?` — the look at the socket that every statement starts
|
|
194
|
+
with, on its own: `true` when the peer has closed or reset the idle
|
|
195
|
+
connection, or written to it unasked. No round-trip. `false` for a
|
|
196
|
+
connection that is closed, already broken or in the middle of a stream.
|
|
157
197
|
- `endpoint` — the member the connection reached, as the `"host:port"` it
|
|
158
198
|
dialed (it changes when a re-dial lands on another member).
|
|
159
199
|
- `closed`, `finished?` — `true` after `close`.
|
|
@@ -162,29 +202,73 @@ use it meanwhile.
|
|
|
162
202
|
|
|
163
203
|
### Reconnect
|
|
164
204
|
|
|
165
|
-
A transport failure raises
|
|
166
|
-
|
|
167
|
-
|
|
205
|
+
**What breaks a connection.** A transport failure raises
|
|
206
|
+
`Skaidb::ConnectionError` and marks the connection broken. So does a reply
|
|
207
|
+
that cannot be read — a tag the protocol does not have, a message shorter
|
|
208
|
+
than its own fields say — whichever call it answers: what follows it on the
|
|
209
|
+
socket cannot be placed. (A reply of the wrong kind to a prepare or to a
|
|
210
|
+
stream request is raised as `QueryError`; the connection is marked broken
|
|
211
|
+
all the same.) A statement cut short between its request and its reply by
|
|
212
|
+
anything else (an `Interrupt`, `Timeout.timeout`, `Thread#kill`) marks it
|
|
168
213
|
broken too, since that reply is still on its way; the exception propagates
|
|
169
214
|
as it is. A connection marked broken closes its socket there and then, so
|
|
170
215
|
the server drops the session — and a transaction left open on it — without
|
|
171
|
-
waiting for the connection's next statement or its `close`.
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
one
|
|
186
|
-
|
|
187
|
-
|
|
216
|
+
waiting for the connection's next statement or its `close`.
|
|
217
|
+
|
|
218
|
+
**A connection lost while idle.** Before each request is written the
|
|
219
|
+
connection is looked at (one peek at the TCP socket that does not wait,
|
|
220
|
+
never a round trip): an idle connection has nothing to read, so an
|
|
221
|
+
end-of-file, a reset or bytes nobody asked for mean the server closed it
|
|
222
|
+
while it sat idle. It is then marked broken like any other lost connection
|
|
223
|
+
— but nothing was sent on it, so nothing is raised: the statement re-dials
|
|
224
|
+
and runs. A connection in the middle of a stream is not idle and is not
|
|
225
|
+
looked at.
|
|
226
|
+
|
|
227
|
+
**The re-dial.** A broken connection's next `exec`, `exec_params`,
|
|
228
|
+
`exec_batch`, `exec_prepared` or `stream` re-dials across the original
|
|
229
|
+
seeds (a pooled connection: in the pool's order, usable members first and
|
|
230
|
+
the one it lost last), re-authenticates, re-sends `Hello`, puts the session
|
|
231
|
+
back as it was — its database, then its settings — and starts with an
|
|
232
|
+
empty prepared-statement cache. If the re-dial fails, that statement raises
|
|
233
|
+
`ConnectionError` and the one after it tries again.
|
|
234
|
+
|
|
235
|
+
**The database** is the one the session was last in: `database:`, or the
|
|
236
|
+
name of the last `USE [DATABASE] <name>` statement the server accepted from
|
|
237
|
+
`exec`, `query`, `exec_params` or `stream` (the name bare or double-quoted,
|
|
238
|
+
an optional trailing `;`, `--` comments skipped as the server skips them;
|
|
239
|
+
the name is kept as it was written and sent again in a `USE` of its own).
|
|
240
|
+
It is entered before anything else is sent, and a refusal is raised to the
|
|
241
|
+
statement that caused the re-dial — which is not sent — so no statement
|
|
242
|
+
runs in another database.
|
|
243
|
+
|
|
244
|
+
When that statement is itself a `USE` the driver can read, the database the
|
|
245
|
+
session is leaving is not entered first: the settings are put back and the
|
|
246
|
+
`USE` is sent, and its answer decides the session database as usual. So a
|
|
247
|
+
session whose database was dropped while its connection was down fails
|
|
248
|
+
every statement with the `USE`'s error (`database "app" does not exist`),
|
|
249
|
+
and is moved out of it with a `USE` of another database. A `USE` that this
|
|
250
|
+
new session refuses leaves it in no database the application chose, so it
|
|
251
|
+
is closed: the next statement dials again and fails as before, rather than
|
|
252
|
+
running in the default database.
|
|
253
|
+
|
|
254
|
+
**The settings** are the last `SET CONSISTENCY …` and the last
|
|
255
|
+
`SET SCAN BUDGET …` statement the server accepted (from the same four
|
|
256
|
+
calls; one note per kind, the statement kept as written without its `--`
|
|
257
|
+
comments). They are sent after the database and before the statement, in
|
|
258
|
+
that order, each only when one is in force: `SET CONSISTENCY DEFAULT` and
|
|
259
|
+
`SET SCAN BUDGET DEFAULT` clear the note, and nothing is sent for a setting
|
|
260
|
+
that was never made. A setting the new session refuses is raised to the
|
|
261
|
+
statement that caused the re-dial, which is not sent: no statement runs at
|
|
262
|
+
another consistency, or without the scan limit the application set. Text
|
|
263
|
+
that is not exactly one such statement is not followed. (`consistency=` is
|
|
264
|
+
not a session setting: it is the level each request carries, and needs no
|
|
265
|
+
restoring.)
|
|
266
|
+
|
|
267
|
+
**Inside a transaction** — the helper's, or one opened with a `BEGIN`
|
|
268
|
+
statement — there is no re-dial: the transaction ended with its socket, so
|
|
269
|
+
every further statement raises `ConnectionError` until the helper returns,
|
|
270
|
+
or until a hand-written transaction's `ROLLBACK` or `COMMIT` ends it (see
|
|
271
|
+
[Transactions](#transactions)).
|
|
188
272
|
|
|
189
273
|
## `Skaidb::Result`
|
|
190
274
|
|
|
@@ -223,9 +307,18 @@ A thread-safe pool balanced across the cluster's members — see
|
|
|
223
307
|
arguments are checked before a connection is checked out.
|
|
224
308
|
- `checkout(affinity: nil) → Connection` — an idle connection to the member
|
|
225
309
|
the policy picks, or a new one. Never blocks. `affinity` (any Hash key;
|
|
226
|
-
`nil` = pool-wide) scopes `read_your_writes`.
|
|
310
|
+
`nil` = pool-wide) scopes `read_your_writes`. An idle connection the
|
|
311
|
+
server closed while it sat in the pool is not handed out: it is found by
|
|
312
|
+
a look at its socket (no round trip) and closed, and the checkout goes on
|
|
313
|
+
to the next idle connection or dials.
|
|
227
314
|
- `checkin(conn)` — return it; closed instead when broken, when its member
|
|
228
|
-
is ejected or gone, or when `maxsize` are already idle.
|
|
315
|
+
is ejected or gone, or when `maxsize` are already idle. A connection the
|
|
316
|
+
pool keeps is first put back in the pool's session state — `ROLLBACK` for
|
|
317
|
+
a transaction its borrower left open, `USE` of the pool's database
|
|
318
|
+
(`database:`, or `default`), `SET SCAN BUDGET DEFAULT`,
|
|
319
|
+
`SET CONSISTENCY DEFAULT`, each only if the borrower's statements made it
|
|
320
|
+
necessary — and closed if one of them fails. See
|
|
321
|
+
[pooling.md](pooling.md).
|
|
229
322
|
- `members → Array<Hash>` — per member: `:endpoint`, `:canonical`, `:state`
|
|
230
323
|
(`:ready`, `:ejected`, `:resyncing`, `:unreachable`, `:unlisted`),
|
|
231
324
|
`:latency_ms` (`nil` until measured), `:in_use`, `:idle`, `:seed`.
|
data/docs/getting-started.md
CHANGED
|
@@ -107,8 +107,10 @@ another writer changed a row the transaction writes raises
|
|
|
107
107
|
`Skaidb::TransactionConflict` with nothing applied, and `transaction` then
|
|
108
108
|
runs the block again (three runs in all by default), so the block must be
|
|
109
109
|
safe to run more than once. On a cluster pass the opening statement:
|
|
110
|
-
`conn.transaction(begin: "BEGIN ATOMIC") { |c| … }`.
|
|
111
|
-
|
|
110
|
+
`conn.transaction(begin: "BEGIN ATOMIC") { |c| … }`. A transaction you
|
|
111
|
+
write yourself (`exec("BEGIN")` … `exec("COMMIT")`) is followed by the
|
|
112
|
+
driver too: if its connection is lost, nothing more is sent until your
|
|
113
|
+
`ROLLBACK` or `COMMIT` ends it. Details: [api.md](api.md#transactions).
|
|
112
114
|
|
|
113
115
|
## Errors
|
|
114
116
|
|
data/docs/pooling.md
CHANGED
|
@@ -56,16 +56,32 @@ 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.
|
|
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.
|
|
59
|
+
pool's order: usable members first, the one it lost last.
|
|
67
60
|
`Connection#endpoint` names the member a connection reached.
|
|
68
61
|
|
|
62
|
+
**Every borrower gets the pool's own session state.** A pooled connection
|
|
63
|
+
reaches each borrower
|
|
64
|
+
|
|
65
|
+
- in the pool's database: its `database:` option, or `default` — the
|
|
66
|
+
database every new connection starts in — when the pool names none;
|
|
67
|
+
- with no transaction open;
|
|
68
|
+
- with no `SET CONSISTENCY` override and no `SET SCAN BUDGET` of an earlier
|
|
69
|
+
borrower.
|
|
70
|
+
|
|
71
|
+
What a borrower changed is put back when the connection is returned, before
|
|
72
|
+
anyone else can get it: `ROLLBACK` for a transaction left open (a `BEGIN`
|
|
73
|
+
with no `COMMIT` or `ROLLBACK` after it), `USE <the pool's database>`,
|
|
74
|
+
`SET SCAN BUDGET DEFAULT`, `SET CONSISTENCY DEFAULT` — only the ones the
|
|
75
|
+
borrower's statements made necessary, so a borrower that changed nothing
|
|
76
|
+
costs no round trip. A connection that cannot be put back is closed instead
|
|
77
|
+
of kept: one of the statements was refused (a server before 0.349 has no
|
|
78
|
+
`SET CONSISTENCY DEFAULT`), or the connection is gone. While a borrower
|
|
79
|
+
holds a connection, its own `USE` and settings follow it across re-dials as
|
|
80
|
+
on any connection; once it is returned they are forgotten, so a re-dial
|
|
81
|
+
under the next borrower restores the pool's state. To work in another
|
|
82
|
+
database, send `USE` after the checkout, or give that database a pool of
|
|
83
|
+
its own.
|
|
84
|
+
|
|
69
85
|
## Balancing across members
|
|
70
86
|
|
|
71
87
|
With the default `load_balancing: :random` each new connection dials the
|
|
@@ -164,11 +180,15 @@ the same in every skaidb driver
|
|
|
164
180
|
is closed instead of kept.
|
|
165
181
|
- `checkout` and `checkin` both validate with `usable?`: a connection broken
|
|
166
182
|
by a transport error, or left mid-stream by an abandoned Enumerator, is
|
|
167
|
-
closed and dropped rather than handed on. `
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
183
|
+
closed and dropped rather than handed on. `checkout` also looks at the
|
|
184
|
+
socket of the idle connection it is about to hand out (a peek that does not wait, no round
|
|
185
|
+
trip): one the server closed while it sat idle — a member restart, an
|
|
186
|
+
idle timeout — is closed and the checkout takes the next idle connection
|
|
187
|
+
or dials, so a restart costs the borrowers nothing. A connection lost
|
|
188
|
+
while its borrower holds it idle is replaced under the borrower the same
|
|
189
|
+
way, at its next statement. Only a connection lost under a statement —
|
|
190
|
+
request written, no reply — raises `Skaidb::ConnectionError`; `checkin`
|
|
191
|
+
then discards it. Send that statement again if it is safe to.
|
|
172
192
|
- A transaction belongs to one connection. `pool.transaction { |conn| … }`
|
|
173
193
|
checks one out, runs `Connection#transaction` on it (`begin`, the block,
|
|
174
194
|
`COMMIT`, the whole of it again when the `COMMIT` is refused with
|