skaidb 1.5.0 → 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.
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 (and after every re-dial). |
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`. |
@@ -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. `consistency:` overrides the connection default for this
46
- statement.
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
- The id is valid on the socket that prepared it: on a broken connection
56
- this raises `ConnectionError` instead of re-dialing, and `prepare_server`
57
- hands out a new id.
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 refuses. Cached per
60
- connection (240 entries); the cache is cleared on re-dial.
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,22 +125,75 @@ 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 the next statement after
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
+ - 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.
137
+ - A stream the block leaves open (an Enumerator from `stream` advanced with
138
+ `next`/`peek` and not read to its end) sits in the middle of its reply, so
139
+ neither `COMMIT` nor `ROLLBACK` can be sent. The connection is marked
140
+ broken — its socket closed, so the server drops the transaction — and the
141
+ stream is ended, which frees the connection: its next statement re-dials.
142
+ `transaction` raises `ConnectionError` (`a stream opened inside the
143
+ transaction was left open, …`) when the block ran to its end; a block
144
+ that was leaving with an exception, `break` or `throw` leaves with that.
145
+ The Enumerator, advanced again, starts its statement again on the
146
+ re-dialled connection, outside any transaction. (A stream opened by
147
+ another thread cannot be ended from here: the connection is closed for
148
+ good instead, `finished?` is `true`.)
118
149
  - Raises `ArgumentError`, before anything is sent, when `attempts` is not an
119
150
  Integer ≥ 1 or no block is given.
120
151
 
121
152
  The whole transaction runs on this one connection, so no other thread may
122
153
  use it meanwhile.
123
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
+
124
184
  ### State
125
185
 
126
186
  - `consistency` / `consistency=` — the default level (`0`/`1`/`2`; the setter
127
187
  accepts anything `Consistency.resolve` does).
128
- - `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
129
190
  or a round trip was cut short with its reply outstanding, or while a
130
191
  stream is in flight (including one abandoned through external iteration).
131
- 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.
132
197
  - `endpoint` — the member the connection reached, as the `"host:port"` it
133
198
  dialed (it changes when a re-dial lands on another member).
134
199
  - `closed`, `finished?` — `true` after `close`.
@@ -137,18 +202,73 @@ use it meanwhile.
137
202
 
138
203
  ### Reconnect
139
204
 
140
- A transport failure raises `Skaidb::ConnectionError` and marks the
141
- connection broken. A statement cut short between its request and its reply
142
- by anything else (an `Interrupt`, `Timeout.timeout`, `Thread#kill`) marks it
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
143
213
  broken too, since that reply is still on its way; the exception propagates
144
- as it is. The next `exec`/`exec_params`/`exec_batch`/`stream`
145
- re-dials across the original seeds (a pooled connection: in the pool's
146
- order, usable members first and the one it lost last), re-authenticates,
147
- re-sends `Hello`, re-runs `USE`, and starts with an empty prepared-statement
148
- cache. If the re-dial fails, that statement raises `ConnectionError` and the
149
- one after it tries again. Inside `transaction` there is no re-dial: the
150
- transaction ended with its socket, so every further statement raises
151
- `ConnectionError` until the helper returns.
214
+ as it is. A connection marked broken closes its socket there and then, so
215
+ the server drops the session — and a transaction left open on it — without
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)).
152
272
 
153
273
  ## `Skaidb::Result`
154
274
 
@@ -187,9 +307,18 @@ A thread-safe pool balanced across the cluster's members — see
187
307
  arguments are checked before a connection is checked out.
188
308
  - `checkout(affinity: nil) → Connection` — an idle connection to the member
189
309
  the policy picks, or a new one. Never blocks. `affinity` (any Hash key;
190
- `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.
191
314
  - `checkin(conn)` — return it; closed instead when broken, when its member
192
- 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).
193
322
  - `members → Array<Hash>` — per member: `:endpoint`, `:canonical`, `:state`
194
323
  (`:ready`, `:ejected`, `:resyncing`, `:unreachable`, `:unlisted`),
195
324
  `:latency_ms` (`nil` until measured), `:in_use`, `:idle`, `:seed`.
@@ -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| … }`. Details:
111
- [api.md](api.md#transactions).
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
@@ -59,6 +59,29 @@ pooled connection that re-dials after a transport error does so in the
59
59
  pool's order: usable members first, the one it lost last.
60
60
  `Connection#endpoint` names the member a connection reached.
61
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
+
62
85
  ## Balancing across members
63
86
 
64
87
  With the default `load_balancing: :random` each new connection dials the
@@ -157,11 +180,15 @@ the same in every skaidb driver
157
180
  is closed instead of kept.
158
181
  - `checkout` and `checkin` both validate with `usable?`: a connection broken
159
182
  by a transport error, or left mid-stream by an abandoned Enumerator, is
160
- closed and dropped rather than handed on. `usable?` is local knowledge
161
- only — a connection the server closed while it sat idle still looks fine
162
- and will fail its first statement with `Skaidb::ConnectionError`; that
163
- failure marks it broken, so `checkin` discards it and the next checkout
164
- gets a fresh one. Retry the statement in that case if it is safe to.
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.
165
192
  - A transaction belongs to one connection. `pool.transaction { |conn| … }`
166
193
  checks one out, runs `Connection#transaction` on it (`begin`, the block,
167
194
  `COMMIT`, the whole of it again when the `COMMIT` is refused with
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
- Two things defeat the drain:
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 the next statement
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