cartel-pg 0.9.0

Async PostgreSQL driver for the dope runtime
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
use cartel_core::{Arena, ArenaConfig, ArenaLane, Limits, QueueArena, QueueLane, Registrable};
use dope::driver::ready::ReadyKey;
use dope::driver::token::Token;
use dope::manifold::connector::session::Connector;
use dope::manifold::connector::source::Dialer;
use dope::manifold::env::Env;
use dope::runtime::executor::StorageFactory;
use dope::{DriverContext, DriverRef};
use dope_net::Transport;
use dope_net::link::egress::storage::Storage as EgressStorage;
use dope_net::wire::Wire;
use o3::buffer::{Lease, Pool, PoolLayout};
use o3::cell::RegionToken;
use std::cell::Cell;
use std::marker::PhantomData;

use crate::protocol::{RowItem, Session, Shared as PoolState};
use crate::query::QuerySet;
use crate::wire::Sink;
use crate::{Config as DatabaseConfig, Error};

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Config {
    capacities: Capacities,
    request_pool: PoolLayout,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Capacities {
    pub connections: usize,
    pub request_entries: usize,
    pub request_bytes: usize,
    pub response_entries: usize,
    pub response_bytes: usize,
    pub inflight: usize,
    pub waiters: usize,
    pub notifications: usize,
}

impl Config {
    pub fn new(capacities: Capacities) -> Result<Self, ConfigError> {
        let Capacities {
            connections: connection_capacity,
            request_entries: request_capacity,
            request_bytes: request_byte_capacity,
            response_entries: response_capacity,
            response_bytes: response_byte_capacity,
            inflight: inflight_capacity,
            waiters: waiter_capacity,
            notifications: notification_capacity,
        } = capacities;
        if connection_capacity == 0 || u32::try_from(connection_capacity).is_err() {
            return Err(ConfigError::Connections);
        }
        if request_capacity < connection_capacity || request_capacity > u32::MAX as usize / 2 {
            return Err(ConfigError::RequestEntries);
        }
        if request_byte_capacity == 0 || u32::try_from(request_byte_capacity).is_err() {
            return Err(ConfigError::RequestBytes);
        }
        let request_pool = PoolLayout::new(request_capacity as u32, request_byte_capacity as u32)
            .map_err(|_| ConfigError::RequestBytes)?;
        if response_capacity < connection_capacity || u32::try_from(response_capacity).is_err() {
            return Err(ConfigError::ResponseEntries);
        }
        let max_response_bytes = (u32::MAX as usize)
            .saturating_sub(4)
            .min(usize::MAX.saturating_sub(5));
        if response_byte_capacity < connection_capacity
            || response_byte_capacity > max_response_bytes
        {
            return Err(ConfigError::ResponseBytes);
        }
        if inflight_capacity < connection_capacity || inflight_capacity > request_capacity {
            return Err(ConfigError::Inflight);
        }
        if waiter_capacity == 0
            || waiter_capacity
                .checked_mul(2)
                .and_then(usize::checked_next_power_of_two)
                .is_none()
        {
            return Err(ConfigError::Waiters);
        }
        if notification_capacity == 0 || notification_capacity.checked_next_power_of_two().is_none()
        {
            return Err(ConfigError::Notifications);
        }
        Ok(Self {
            capacities,
            request_pool,
        })
    }

    pub const fn connection_capacity(self) -> usize {
        self.capacities.connections
    }

    pub const fn request_capacity(self) -> usize {
        self.capacities.request_entries
    }

    pub const fn request_byte_capacity(self) -> usize {
        self.capacities.request_bytes
    }

    pub const fn request_pool(self) -> PoolLayout {
        self.request_pool
    }

    pub const fn response_capacity(self) -> usize {
        self.capacities.response_entries
    }

    pub const fn response_byte_capacity(self) -> usize {
        self.capacities.response_bytes
    }

    pub const fn inflight_capacity(self) -> usize {
        self.capacities.inflight
    }

    pub const fn waiter_capacity(self) -> usize {
        self.capacities.waiters
    }

    pub const fn notification_capacity(self) -> usize {
        self.capacities.notifications
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ConfigError {
    Connections,
    RequestEntries,
    RequestBytes,
    ResponseEntries,
    ResponseBytes,
    Inflight,
    Waiters,
    Notifications,
}

impl std::fmt::Display for ConfigError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(match self {
            Self::Connections => "connection capacity must be positive and fit in u32",
            Self::RequestEntries => {
                "request entries must cover all connections and fit their backing pools"
            }
            Self::RequestBytes => "request byte capacity must be positive and fit in u32",
            Self::ResponseEntries => "response entries must cover all connections and fit in u32",
            Self::ResponseBytes => {
                "response byte capacity must be positive and fit a PostgreSQL frame"
            }
            Self::Inflight => "inflight capacity must be positive and not exceed request entries",
            Self::Waiters => "waiter capacity must be positive and fit its backing set",
            Self::Notifications => {
                "notification capacity must be positive and fit its backing queue"
            }
        })
    }
}

impl std::error::Error for ConfigError {}

pub struct Frame<'d> {
    buffer: Lease<'d>,
    overflowed: bool,
}

impl Frame<'_> {
    pub(super) fn overflowed(&self) -> bool {
        self.overflowed
    }
}

impl AsRef<[u8]> for Frame<'_> {
    fn as_ref(&self) -> &[u8] {
        self.buffer.as_ref()
    }
}

impl Sink for Frame<'_> {
    fn push(&mut self, byte: u8) {
        self.overflowed |= self.buffer.try_push(byte).is_err();
    }

    fn extend_from_slice(&mut self, src: &[u8]) {
        self.overflowed |= self.buffer.try_extend_from_slice(src).is_err();
    }

    fn len(&self) -> usize {
        self.buffer.len()
    }

    fn as_mut_slice(&mut self) -> &mut [u8] {
        self.buffer.as_mut_slice()
    }
}

#[derive(Clone, Copy)]
pub(super) enum Boundary {
    Close,
    Open,
    External,
}

struct Conn<'d> {
    driver: DriverRef<'d>,
    token: Cell<Option<Token>>,
    ready: Cell<Option<ReadyKey<'d>>>,
    close: Cell<bool>,
    unsynced: Cell<u32>,
    batch_open: Cell<bool>,
}

impl<'d> Conn<'d> {
    fn new(driver: DriverRef<'d>) -> Self {
        Self {
            driver,
            token: Cell::new(None),
            ready: Cell::new(None),
            close: Cell::new(false),
            unsynced: Cell::new(0),
            batch_open: Cell::new(false),
        }
    }

    fn matches(&self, token: Token) -> bool {
        self.token.get() == Some(token)
    }

    fn wake(&self) {
        if let Some(ready) = self.ready.get() {
            self.driver.activate_ready(ready);
        }
    }
}

pub struct Port<'d, I: QuerySet> {
    pub(super) shared: PoolState,
    conns: Box<[Conn<'d>]>,
    request_queue: QueueArena<'d, Frame<'d>>,
    requests: Pool,
    responses: Arena<'d, RowItem>,
    egress: EgressStorage,
    _instance: PhantomData<fn() -> I>,
}

impl<'d, I: QuerySet> AsRef<Self> for Port<'d, I> {
    fn as_ref(&self) -> &Self {
        self
    }
}

pub struct PortFactory<I> {
    database: DatabaseConfig,
    config: Config,
    instance: PhantomData<fn() -> I>,
}

impl<I> PortFactory<I> {
    pub fn config(&self) -> Config {
        self.config
    }
}

impl<'d, I: QuerySet> Port<'d, I> {
    pub fn factory(database: DatabaseConfig, config: Config) -> PortFactory<I> {
        PortFactory {
            database,
            config,
            instance: PhantomData,
        }
    }

    pub fn new(database: DatabaseConfig, config: Config, driver: DriverRef<'d>) -> Self {
        let connections = config.connection_capacity();
        let request_entries = config.request_capacity();
        let limits = Limits::new(
            config.response_capacity(),
            config.response_byte_capacity(),
            config.response_capacity(),
        );
        let request_queue = QueueArena::with_capacity(request_entries, connections);
        let responses = Arena::new(ArenaConfig::new(
            connections,
            config.inflight_capacity(),
            config.response_capacity(),
            config.response_byte_capacity(),
            config.response_capacity(),
            limits,
        ));
        Self {
            shared: PoolState::new(database, config),
            conns: (0..connections).map(|_| Conn::new(driver)).collect(),
            request_queue,
            requests: Pool::from_layout(config.request_pool()),
            responses,
            egress: EgressStorage::default(),
            _instance: PhantomData,
        }
    }

    pub fn capacity(&self) -> usize {
        self.shared.config.connection_capacity()
    }

    pub fn config(&self) -> Config {
        self.shared.config
    }

    pub(super) fn backpressure(&self, queued: usize, region: &mut RegionToken<'d>) -> Error {
        Error::Backpressure {
            inflight: self.responses.inflight(region),
            queued,
            cap: self.shared.config.inflight_capacity(),
        }
    }

    pub fn connect<const ID: u8, S, E>(
        &'d self,
        upstreams: S,
        driver: &mut DriverContext<'_, 'd>,
    ) -> std::io::Result<Connector<'d, ID, Session<'d, I>, S, E>>
    where
        S: Dialer<E::Transport> + 'd,
        E: Env + 'd,
        E::Transport: Transport,
        <E::Wire as Wire>::InitConfig<'d>: Default,
    {
        Connector::new(
            Session::new(self),
            upstreams,
            self.config().connection_capacity(),
            &self.egress,
            driver,
        )
    }

    /// Creates a connector whose wire runtime is local to this core.
    ///
    /// The configuration is consumed once when the connector's wire runtime is
    /// initialized, rather than cloned into individual connections.
    pub fn connect_configured<const ID: u8, S, E>(
        &'d self,
        upstreams: S,
        wire: <E::Wire as Wire>::InitConfig<'d>,
        driver: &mut DriverContext<'_, 'd>,
    ) -> std::io::Result<Connector<'d, ID, Session<'d, I>, S, E>>
    where
        S: Dialer<E::Transport> + 'd,
        E: Env + 'd,
        E::Transport: Transport,
    {
        Connector::new_with_wire_config(
            Session::new(self),
            upstreams,
            self.config().connection_capacity(),
            wire,
            &self.egress,
            driver,
        )
    }

    fn conn(&self, token: Token) -> Option<&Conn<'d>> {
        let conn = self.conns.get(token.slot().raw() as usize)?;
        conn.matches(token).then_some(conn)
    }

    pub(super) fn activate(&self, token: Token, ready: ReadyKey<'d>) {
        let conn = &self.conns[token.slot().raw() as usize];
        conn.token.set(Some(token));
        conn.ready.set(Some(ready));
        conn.close.set(false);
        conn.unsynced.set(0);
        conn.batch_open.set(false);
    }

    pub(super) fn deactivate(&'d self, token: Token, region: &mut RegionToken<'d>) {
        let Some(conn) = self.conn(token) else {
            return;
        };
        let lane = token.slot().raw() as usize;
        conn.token.set(None);
        conn.ready.set(None);
        conn.close.set(false);
        conn.unsynced.set(0);
        conn.batch_open.set(false);
        self.request_queue.lane(lane).clear(region);
    }

    pub(super) fn frame(&'d self) -> Result<Frame<'d>, Error> {
        self.requests
            .try_acquire()
            .map(|buffer| Frame {
                buffer,
                overflowed: false,
            })
            .ok_or(Error::RequestCapacity)
    }

    pub(super) fn encode(&'d self, f: impl FnOnce(&mut Frame<'_>)) -> Result<Frame<'d>, Error> {
        let mut frame = self.frame()?;
        f(&mut frame);
        if frame.overflowed() {
            return Err(Error::RequestTooLarge);
        }
        Ok(frame)
    }

    fn request_lane(&self, token: Token) -> QueueLane<'_, 'd, Frame<'d>> {
        self.request_queue.lane(token.slot().raw() as usize)
    }

    fn enqueue_request(
        &'d self,
        token: Token,
        frame: Frame<'d>,
        region: &mut RegionToken<'d>,
    ) -> Result<(), Frame<'d>> {
        let len = frame.as_ref().len();
        self.request_lane(token).try_push(region, frame, len)
    }

    fn try_enqueue_conn(
        &'d self,
        token: Token,
        bytes: Frame<'d>,
        region: &mut RegionToken<'d>,
    ) -> Result<(), (Error, Frame<'d>)> {
        let Some(conn) = self.conn(token) else {
            return Err((Error::Closed, bytes));
        };
        let requests = self.request_lane(token);
        let queued = requests.weight(region);
        if !requests.has_capacity(region) {
            return Err((self.backpressure(queued, region), bytes));
        }
        self.enqueue_request(token, bytes, region)
            .map_err(|bytes| (self.backpressure(queued, region), bytes))?;
        conn.wake();
        Ok(())
    }

    pub(super) fn try_enqueue_reply(
        &'d self,
        token: Token,
        bytes: Frame<'d>,
        reply: &mut impl Registrable<'d, RowItem>,
        boundary: Boundary,
        region: &mut RegionToken<'d>,
    ) -> Result<(), (Error, Frame<'d>)> {
        let Some(conn) = self.conn(token) else {
            return Err((Error::Closed, bytes));
        };
        let requests = self.request_lane(token);
        let queued = requests.weight(region);
        if !requests.has_capacity(region) {
            return Err((self.backpressure(queued, region), bytes));
        }
        let lane = token.slot().raw() as usize;
        if !reply.try_attach_with_boundary(
            region,
            self.responses.lane(lane),
            matches!(boundary, Boundary::Close),
        ) {
            return Err((self.backpressure(queued, region), bytes));
        }
        self.enqueue_request(token, bytes, region)
            .map_err(|bytes| (self.backpressure(queued, region), bytes))?;
        conn.unsynced.set(conn.unsynced.get().saturating_add(1));
        match boundary {
            Boundary::Close => {
                conn.unsynced.set(0);
                conn.batch_open.set(false);
            }
            Boundary::Open => conn.batch_open.set(true),
            Boundary::External => {}
        }
        self.shared.inc_inflight(token);
        conn.wake();
        Ok(())
    }

    pub(super) fn try_enqueue(
        &'d self,
        token: Token,
        bytes: Frame<'d>,
        region: &mut RegionToken<'d>,
    ) -> Result<(), (Error, Frame<'d>)> {
        self.try_enqueue_conn(token, bytes, region)
    }

    pub(super) fn drain_requests(
        &'d self,
        token: Token,
        push: impl FnMut(Frame<'d>) -> Result<(), Frame<'d>>,
        region: &mut RegionToken<'d>,
    ) -> dope::manifold::connector::app::Requests {
        let Some(conn) = self.conn(token) else {
            return dope::manifold::connector::app::Requests::default();
        };
        self.request_lane(token).drain(region, push);
        dope::manifold::connector::app::Requests {
            close: conn
                .close
                .take()
                .then_some(dope::manifold::connector::app::CloseKind::Reconnect),
        }
    }

    pub(super) fn close(&self, token: Token) {
        let Some(conn) = self.conn(token) else {
            return;
        };
        conn.close.set(true);
        conn.wake();
    }

    pub(super) fn unsynced(&self, token: Token) -> u32 {
        self.conn(token).map_or(0, |conn| conn.unsynced.get())
    }

    pub(super) fn batch_open(&self, token: Token) -> bool {
        self.conn(token).is_some_and(|conn| conn.batch_open.get())
    }

    pub(super) fn set_batch_open(&self, token: Token, open: bool) {
        if let Some(conn) = self.conn(token) {
            conn.batch_open.set(open);
        }
    }

    pub(super) fn can_push_boundary(&'d self, token: Token, region: &mut RegionToken<'d>) -> bool {
        self.conn(token).is_some()
            && self
                .responses
                .lane(token.slot().raw() as usize)
                .can_mark_boundary(region)
    }

    pub(super) fn push_boundary(&'d self, token: Token, region: &mut RegionToken<'d>) -> bool {
        let Some(conn) = self.conn(token) else {
            return false;
        };
        if !self
            .responses
            .lane(token.slot().raw() as usize)
            .mark_boundary(region)
        {
            return false;
        }
        conn.unsynced.set(0);
        conn.batch_open.set(false);
        true
    }

    pub(super) fn responses(&'d self, token: Token) -> Option<ArenaLane<'d, RowItem>> {
        self.conn(token)?;
        Some(self.responses.lane(token.slot().raw() as usize))
    }

    pub(super) fn response_len(&'d self, token: Token, region: &mut RegionToken<'d>) -> usize {
        self.responses(token)
            .map_or(0, |responses| responses.len(region))
    }

    pub(super) fn responses_empty(&'d self, token: Token, region: &mut RegionToken<'d>) -> bool {
        self.responses(token)
            .is_none_or(|responses| responses.is_empty(region))
    }
}

impl<I: QuerySet + 'static> StorageFactory for PortFactory<I> {
    type Output<'d> = Port<'d, I>;

    fn build<'d>(self, driver: &mut DriverContext<'_, 'd>) -> Self::Output<'d> {
        Port::new(self.database, self.config, driver.driver_ref())
    }
}