uqa-execution 0.5.2

Volcano physical operators with row-batch pipelines
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
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//

//! Exact relation identities leased across holders and waiters. The native pin prevents slot reuse until the last process using that identity releases it.

use super::super::super::{relation_byte_claims, RelationLockMode, RELATION_BASE, RELATION_SPAN};
use super::super::{CoordinatorState, FileLockCoordinator};
use super::{Admission, RELATION_ADMISSION_BYTE};
use rusqlite::OptionalExtension;
use std::collections::HashMap;
use uqa_sql::SQLError;

const ATTACHMENT_BYTE: u64 = 15;

struct Pin {
    slot: u64,
    users: usize,
    poisoned: bool,
}

#[derive(Default)]
pub(in crate::row_locks::cross_process::file) struct Identities {
    connection: Option<rusqlite::Connection>,
    pins: HashMap<Vec<u8>, Pin>,
    slots: HashMap<u64, u64>,
}

pub(in crate::row_locks) struct RelationIdentityLease<'a> {
    coordinator: &'a FileLockCoordinator,
    relation: &'a [u8],
    slot: u64,
    generation: u64,
    retained: bool,
}

impl RelationIdentityLease<'_> {
    pub(in crate::row_locks) fn generation(&self) -> u64 {
        self.generation
    }

    pub(in crate::row_locks) fn slot(&self) -> u64 {
        self.slot
    }
    pub(in crate::row_locks) fn retain(&mut self) {
        self.retained = true;
    }
}

impl Drop for RelationIdentityLease<'_> {
    fn drop(&mut self) {
        if !self.retained {
            self.coordinator
                .unpin_relation(&mut self.coordinator.state.lock(), self.relation);
        }
    }
}

fn sql(error: rusqlite::Error) -> String {
    format!("relation identity registry: {error}")
}

impl FileLockCoordinator {
    pub(in crate::row_locks::cross_process::file) fn attach_relation_registry(
        &self,
    ) -> Result<(), String> {
        let mut state = self.state.lock();
        uqa_storage::native_file::lock_byte(&self.file, RELATION_ADMISSION_BYTE, true)
            .map_err(|error| format!("admit relation registry: {error}"))?;
        let mut admission = Admission {
            coordinator: self,
            active: true,
        };
        self.open_relation_identities(&mut state)?;
        admission.release()
    }

    pub(in crate::row_locks) fn pin_relation<'a>(
        &'a self,
        relation: &'a [u8],
        cancel: &uqa_core::CancellationToken,
    ) -> Result<RelationIdentityLease<'a>, SQLError> {
        loop {
            cancel.check()?;
            if let Some(slot) = self
                .try_pin_relation(relation)
                .map_err(SQLError::Internal)?
            {
                return Ok(RelationIdentityLease {
                    coordinator: self,
                    relation,
                    slot,
                    generation: self.state.lock().relation_identities.slots[&slot],
                    retained: false,
                });
            }
            std::thread::sleep(std::time::Duration::from_millis(1));
        }
    }

    pub(super) fn try_pin_relation(&self, relation: &[u8]) -> Result<Option<u64>, String> {
        let mut state = self.state.lock();
        if let Some(pin) = state.relation_identities.pins.get_mut(relation) {
            pin.users += 1;
            return Ok(Some(pin.slot));
        }
        if let Err(error) = self.apply_byte_mode(RELATION_ADMISSION_BYTE, None, Some(true)) {
            return if super::lock_would_block(&error) {
                Ok(None)
            } else {
                Err(format!("admit relation identity: {error}"))
            };
        }
        let mut admission = Admission {
            coordinator: self,
            active: true,
        };
        let slot = self.pin_admitted_relation(&mut state, relation)?;
        if let Err(error) = admission.release() {
            self.unpin_relation(&mut state, relation);
            return Err(error);
        }
        Ok(Some(slot))
    }

    fn pin_admitted_relation(
        &self,
        state: &mut CoordinatorState,
        relation: &[u8],
    ) -> Result<u64, String> {
        self.open_relation_identities(state)?;
        let identities = &mut state.relation_identities;
        let connection = identities
            .connection
            .as_ref()
            .expect("identity registry opened");
        let existing = connection
            .query_row(
                "SELECT slot,generation FROM identities WHERE identity=?1",
                [relation],
                |row| Ok((u64::from(row.get::<_, u32>(0)?), row.get::<_, i64>(1)?)),
            )
            .optional()
            .map_err(sql)?;
        let (slot, generation) = match existing {
            Some(identity) => identity,
            None => {
                let mut statement = connection
                    .prepare("SELECT slot FROM identities ORDER BY slot")
                    .map_err(sql)?;
                let mut rows = statement.query([]).map_err(sql)?;
                let mut available = None;
                let mut next = 0;
                while let Some(row) = rows.next().map_err(sql)? {
                    let slot = u64::from(row.get::<_, u32>(0).map_err(sql)?);
                    if slot >= RELATION_SPAN {
                        return Err("invalid relation identity slot".into());
                    }
                    next = slot + 1;
                    if identities.slots.contains_key(&slot) {
                        continue;
                    }
                    match self.apply_byte_mode(RELATION_BASE + slot, None, Some(true)) {
                        Ok(()) => {
                            self.apply_byte_mode(RELATION_BASE + slot, Some(true), None)
                                .map_err(|error| {
                                    format!("release relation identity probe: {error}")
                                })?;
                            available = Some(slot);
                            break;
                        }
                        Err(error) if super::lock_would_block(&error) => {}
                        Err(error) => return Err(format!("probe relation identity: {error}")),
                    }
                }
                drop(rows);
                drop(statement);
                let slot = available.unwrap_or(next);
                if slot >= RELATION_SPAN {
                    return Err("too many simultaneously retained relation identities".into());
                }
                let transaction = connection.unchecked_transaction().map_err(sql)?;
                let generation: i64 = transaction.query_row("UPDATE generation SET value=value+1 WHERE value<9223372036854775807 RETURNING value", [], |row| row.get(0)).map_err(sql)?;
                transaction.execute("INSERT INTO identities(slot,identity,generation) VALUES(?1,?2,?3) ON CONFLICT(slot) DO UPDATE SET identity=excluded.identity,generation=excluded.generation", rusqlite::params![u32::try_from(slot).expect("bounded relation slot"),relation,generation]).map_err(sql)?;
                transaction.commit().map_err(sql)?;
                (slot, generation)
            }
        };
        if slot >= RELATION_SPAN {
            return Err("invalid relation identity slot".into());
        }
        let generation = u64::try_from(generation)
            .ok()
            .filter(|value| *value != 0)
            .ok_or("invalid relation identity generation")?;
        self.apply_byte_mode(RELATION_BASE + slot, None, Some(false))
            .map_err(|error| format!("pin relation identity: {error}"))?;
        identities.pins.insert(
            relation.to_vec(),
            Pin {
                slot,
                users: 1,
                poisoned: false,
            },
        );
        identities.slots.insert(slot, generation);
        Ok(slot)
    }

    pub(super) fn unpin_relation(&self, state: &mut CoordinatorState, relation: &[u8]) {
        let identities = &mut state.relation_identities;
        let Some(pin) = identities.pins.get_mut(relation) else {
            return;
        };
        pin.users -= 1;
        if pin.users == 0 && !pin.poisoned {
            // An unlock error must leave the mapping pinned locally, so another identity can never reuse a possibly held native byte.
            if self
                .apply_byte_mode(RELATION_BASE + pin.slot, Some(false), None)
                .is_ok()
            {
                identities.slots.remove(&pin.slot);
                identities.pins.remove(relation);
            }
        }
    }

    pub(in crate::row_locks) fn release_relation(
        &self,
        session: u64,
        relation: &[u8],
        mode: RelationLockMode,
    ) {
        let mut state = self.state.lock();
        if let Some(pin) = state.relation_identities.pins.get(relation) {
            for claim in relation_byte_claims(pin.slot, mode) {
                self.release_one(&mut state, session, claim);
            }
            self.unpin_relation(&mut state, relation);
        }
    }

    pub(in crate::row_locks) fn retained_row_identity(
        &self,
        relation: &[u8],
        doc_id: u64,
    ) -> Option<super::super::super::RowIdentity> {
        use super::super::super::RowIdentity;
        if let Some(identity) = RowIdentity::key(relation, doc_id) {
            return Some(identity);
        }
        let state = self.state.lock();
        let identities = &state.relation_identities;
        let slot = identities.pins.get(relation)?.slot;
        Some(RowIdentity::Relation {
            generation: *identities.slots.get(&slot)?,
            doc_id,
        })
    }

    pub(in crate::row_locks) fn release_row_identity(&self, relation: &[u8]) {
        if super::super::super::RowIdentity::key(relation, 0).is_none() {
            self.unpin_relation(&mut self.state.lock(), relation);
        }
    }

    fn open_relation_identities(&self, state: &mut CoordinatorState) -> Result<(), String> {
        if state.relation_identities.connection.is_some() {
            return Ok(());
        }
        let first = match self.apply_byte_mode(ATTACHMENT_BYTE, None, Some(true)) {
            Ok(()) => true,
            Err(error) if super::lock_would_block(&error) => false,
            Err(error) => return Err(format!("attach relation identities: {error}")),
        };
        let opened = self.open_relation_registry(first);
        let connection = match opened {
            Ok(connection) => connection,
            Err(error) => {
                if first {
                    let _ = self.apply_byte_mode(ATTACHMENT_BYTE, Some(true), None);
                }
                return Err(error);
            }
        };
        if let Err(error) =
            self.apply_byte_mode(ATTACHMENT_BYTE, first.then_some(true), Some(false))
        {
            drop(connection);
            if first {
                let _ = self.apply_byte_mode(ATTACHMENT_BYTE, Some(true), None);
            }
            return Err(format!("retain relation registry attachment: {error}"));
        }
        state.relation_identities.connection = Some(connection);
        Ok(())
    }

    fn open_relation_registry(&self, first: bool) -> Result<rusqlite::Connection, String> {
        if first {
            self.clear_relation_epoch_metadata()?;
            // No process retains a holder or waiter from the old epoch. This sidecar has no committed data; rebuilding also recovers it after a host failure with synchronous=OFF.
            for suffix in ["", "-journal"] {
                let mut path = self.relation_path.as_os_str().to_owned();
                path.push(suffix);
                match std::fs::remove_file(std::path::Path::new(&path)) {
                    Ok(()) => {}
                    Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
                    Err(error) => return Err(format!("reset relation registry epoch: {error}")),
                }
            }
        }
        let connection = rusqlite::Connection::open(&self.relation_path).map_err(sql)?;
        if let Some(key) = &self.relation_key {
            connection
                .pragma_update(None, "key", key.expose_secret())
                .map_err(sql)?;
            let cipher: String = connection
                .pragma_query_value(None, "cipher_version", |row| row.get(0))
                .map_err(sql)?;
            if cipher.is_empty() {
                return Err("encrypted relation registry requires SQLCipher".into());
            }
        }
        // Read before configuring or writing: an incorrect credential must not mutate another live process's registry.
        let version: u32 = connection
            .pragma_query_value(None, "user_version", |row| row.get(0))
            .map_err(sql)?;
        if (!first && version != 2) || (first && version != 0) {
            return Err("invalid relation registry version".into());
        }
        connection.execute_batch("PRAGMA synchronous=OFF; PRAGMA temp_store=MEMORY; PRAGMA cache_size=-64; PRAGMA secure_delete=ON;").map_err(sql)?;
        if first {
            connection.execute_batch("CREATE TABLE generation(value INTEGER NOT NULL); INSERT INTO generation VALUES(0); CREATE TABLE identities(slot INTEGER PRIMARY KEY CHECK(slot>=0 AND slot<1048576), identity BLOB NOT NULL UNIQUE, generation INTEGER NOT NULL CHECK(generation>0)); PRAGMA user_version=2;").map_err(sql)?;
        }
        Ok(connection)
    }

    pub(in crate::row_locks::cross_process::file) fn close_relation_identities(&self) {
        // Close SQLite before the native descriptor releases the attachment and every pin together. A new epoch cannot start while this connection is closing.
        self.state.lock().relation_identities.connection.take();
    }

    pub(in crate::row_locks::cross_process::file) fn local_relation_generation(
        state: &CoordinatorState,
        offset: u64,
    ) -> u64 {
        super::super::super::relation_slot_of_claim(offset)
            .and_then(|slot| state.relation_identities.slots.get(&slot).copied())
            .unwrap_or(0)
    }

    pub(in crate::row_locks::cross_process::file) fn current_relation_generation(
        &self,
        offset: u64,
    ) -> Option<u64> {
        let Some(slot) = super::super::super::relation_slot_of_claim(offset) else {
            return Some(0);
        };
        let mut state = self.state.lock();
        if let Some(generation) = state.relation_identities.slots.get(&slot) {
            return Some(*generation);
        }
        if state.relation_identities.connection.is_none() {
            // A row-only waiter may follow a foreign relation edge. Attach lazily so its graph traversal uses the same registry even before it acquires any relation of its own.
            self.apply_byte_mode(RELATION_ADMISSION_BYTE, None, Some(true))
                .ok()?;
            let mut admission = Admission {
                coordinator: self,
                active: true,
            };
            self.open_relation_identities(&mut state).ok()?;
            admission.release().ok()?;
        }
        let generation: i64 = state
            .relation_identities
            .connection
            .as_ref()?
            .query_row(
                "SELECT generation FROM identities WHERE slot=?1",
                [u32::try_from(slot).ok()?],
                |row| row.get(0),
            )
            .ok()?;
        u64::try_from(generation)
            .ok()
            .filter(|generation| *generation != 0)
    }

    pub(in crate::row_locks::cross_process::file) fn poison_relation_slot(
        state: &mut CoordinatorState,
        offset: u64,
    ) {
        if let Some(slot) = super::super::super::relation_slot_of_claim(offset) {
            for pin in state.relation_identities.pins.values_mut() {
                if pin.slot == slot {
                    pin.poisoned = true;
                }
            }
        }
    }

    #[cfg(test)]
    pub(in crate::row_locks) fn relation_registry_counts(&self) -> (usize, i64) {
        let state = self.state.lock();
        let identities = &state.relation_identities;
        let count = identities
            .connection
            .as_ref()
            .unwrap()
            .query_row("SELECT count(*) FROM identities", [], |row| row.get(0))
            .unwrap();
        (identities.pins.len(), count)
    }

    #[cfg(test)]
    pub(in crate::row_locks) fn relation_slot(&self, relation: &[u8]) -> u64 {
        self.pin_relation(relation, &uqa_core::CancellationToken::new())
            .unwrap()
            .slot()
    }
}