vcon-server 0.4.15

Stores vCons encrypted at rest, on local disk or in an S3 bucket (as objects or Parquet files), and returns them to authorized readers.
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
//! The advisory locks a process holds on the catalog database for as long
//! as it runs, in place of the data directory's lock files: a server, a
//! command that runs beside a server, and a command that runs alone. The
//! live tests need a PostgreSQL server.

use super::super::schema::{COMMANDS_LOCK, Holds, SERVERS_LOCK, lock_refusal, lock_text};
use crate::datadir::Sharing;

// What must work.

#[test]
fn a_server_tries_the_servers_lock_alone_and_not_shared() {
    assert_eq!(
        lock_text(Holds::Serve),
        format!("SELECT pg_try_advisory_lock({SERVERS_LOCK}), true"),
        "one instance serves a database in this release"
    );
}

#[test]
fn a_command_beside_a_server_shares_the_commands_lock() {
    assert_eq!(
        lock_text(Holds::Beside),
        format!("SELECT true, pg_try_advisory_lock_shared({COMMANDS_LOCK})"),
        "with other such commands, and takes nothing a server holds"
    );
}

#[test]
fn a_command_alone_tries_both_locks_and_shares_neither() {
    assert_eq!(
        lock_text(Holds::Alone),
        format!(
            "SELECT pg_try_advisory_lock({SERVERS_LOCK}), pg_try_advisory_lock({COMMANDS_LOCK})"
        ),
        "no server and no other command"
    );
}

#[test]
fn locks_that_were_given_are_no_refusal() {
    for holds in [Holds::Serve, Holds::Beside, Holds::Alone] {
        assert_eq!(lock_refusal(holds, true, true), None, "{holds:?}");
    }
}

#[test]
fn the_kind_of_work_on_the_data_directory_is_the_kind_on_the_database() {
    assert_eq!(Holds::from(Sharing::Serve), Holds::Serve, "a server");
    assert_eq!(Holds::from(Sharing::Beside), Holds::Beside, "beside one");
    assert_eq!(Holds::from(Sharing::Alone), Holds::Alone, "alone");
}

// What must be refused.

#[test]
fn a_server_is_told_what_may_hold_the_database() {
    let why = lock_refusal(Holds::Serve, false, true).expect("refused");
    assert_eq!(
        why,
        "another server, a command that runs alone, or a migration holds this database's \
         lock; one server serves a database, and those commands run while none does",
        "who it may be, and the rule"
    );
}

#[test]
fn a_command_beside_a_server_is_told_to_wait_for_the_one_that_runs_alone() {
    let why = lock_refusal(Holds::Beside, true, false).expect("refused");
    assert_eq!(
        why,
        "a command that runs alone, or a migration, holds this database's lock; run this \
         command again when it has ended",
        "who it may be, and what to do"
    );
}

#[test]
fn a_command_alone_is_told_to_stop_the_servers_and_wait_for_the_commands() {
    for (servers, commands) in [(false, true), (true, false), (false, false)] {
        let why = lock_refusal(Holds::Alone, servers, commands).expect("refused");
        assert_eq!(
            why,
            "a server or another command holds this database's locks; this command runs \
             alone: stop every server, wait for the other commands to end, and run it again",
            "servers {servers}, commands {commands}"
        );
    }
}

// The edges.

#[test]
fn no_lock_is_waited_for() {
    for holds in [Holds::Serve, Holds::Beside, Holds::Alone] {
        let text = lock_text(holds);
        assert_eq!(
            text.matches("pg_try_advisory_lock").count(),
            text.matches("advisory").count(),
            "{holds:?} tries, and is refused at once: {text}"
        );
    }
}

#[test]
fn a_lock_that_was_not_asked_for_is_not_a_refusal() {
    // The statement answers `true` in the column of a lock it does not
    // take; a `false` there cannot come from the server.
    assert_eq!(
        lock_refusal(Holds::Serve, true, false),
        None,
        "a server takes nothing on the commands' lock"
    );
    assert_eq!(
        lock_refusal(Holds::Beside, false, true),
        None,
        "a command beside a server takes nothing on the servers' lock"
    );
}

#[cfg(feature = "backend-tests")]
mod on_a_server {
    use std::sync::Arc;

    use super::super::super::catalog::Database;
    use super::super::super::schema::{
        COMMANDS_LOCK, Holds, MIGRATIONS, SERVERS_LOCK, take_locks_for,
    };
    use super::super::live::value;
    use super::super::schema_tests::live::Db;
    use crate::config::StorageError;

    /// Runs `call` on a runtime of its own, for a test's own connections.
    fn wait<T>(call: impl Future<Output = T>) -> T {
        tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .expect("a runtime")
            .block_on(call)
    }

    /// A migrated database of the test's own.
    fn migrated() -> Db {
        wait(Db::migrated())
    }

    /// Connects to `db` as the application role for the work `holds`.
    fn connect(db: &Db, holds: Holds) -> Result<Arc<Database>, String> {
        let (_dir, settings) = db.settings("vcon_app");
        Database::connect_as(&settings, holds).map_err(|StorageError(why)| why)
    }

    /// The advisory locks held in the database, as `key:mode` in order.
    fn held(db: &Db) -> String {
        wait(async {
            value(
                &db.admin().await,
                "SELECT coalesce(string_agg(((l.classid::bigint << 32) | l.objid::bigint) \
                 || ':' || l.mode, ',' ORDER BY l.objid DESC, l.mode), '') \
                 FROM pg_locks l WHERE l.locktype = 'advisory' AND l.granted AND l.database = \
                 (SELECT oid FROM pg_database WHERE datname = current_database())",
            )
            .await
        })
    }

    const SERVER_REFUSED: &str = "catalog.postgres: another server, a command that runs alone, \
                                  or a migration holds this database's lock";
    const BESIDE_REFUSED: &str =
        "catalog.postgres: a command that runs alone, or a migration, holds this database's lock";
    const ALONE_REFUSED: &str =
        "catalog.postgres: a server or another command holds this database's locks";

    // What must work.

    #[test]
    fn a_command_runs_beside_a_server() {
        let db = migrated();
        let _server = connect(&db, Holds::Serve).expect("the server");
        let _command = connect(&db, Holds::Beside).expect("a command beside it");
        assert_eq!(
            held(&db),
            format!("{SERVERS_LOCK}:ExclusiveLock,{COMMANDS_LOCK}:ShareLock"),
            "each holds its own lock"
        );
    }

    #[test]
    fn a_server_starts_while_a_command_runs_beside() {
        let db = migrated();
        let _command = connect(&db, Holds::Beside).expect("a command");
        connect(&db, Holds::Serve).expect("the server starts");
    }

    #[test]
    fn two_commands_run_beside_each_other() {
        let db = migrated();
        let _one = connect(&db, Holds::Beside).expect("one");
        let _two = connect(&db, Holds::Beside).expect("another");
    }

    #[test]
    fn a_command_runs_alone_when_nothing_else_does_and_holds_both_locks() {
        let db = migrated();
        let _command = connect(&db, Holds::Alone).expect("alone");
        assert_eq!(
            held(&db),
            format!("{SERVERS_LOCK}:ExclusiveLock,{COMMANDS_LOCK}:ExclusiveLock"),
            "the servers' lock and the commands' lock"
        );
    }

    #[test]
    fn a_command_alone_runs_once_the_server_has_stopped() {
        let db = migrated();
        let server = connect(&db, Holds::Serve).expect("the server");
        server.give_up_lock();
        connect(&db, Holds::Alone).expect("alone, after the server");
    }

    #[test]
    fn a_server_starts_once_the_command_alone_has_ended() {
        let db = migrated();
        let command = connect(&db, Holds::Alone).expect("alone");
        command.give_up_lock();
        assert_eq!(held(&db), "", "both locks are given up");
        connect(&db, Holds::Serve).expect("the server, after the command");
    }

    // What must be refused.

    #[test]
    fn a_command_alone_is_refused_while_a_server_runs() {
        let db = migrated();
        let _server = connect(&db, Holds::Serve).expect("the server");
        let why = connect(&db, Holds::Alone).expect_err("refused");
        assert!(why.starts_with(ALONE_REFUSED), "says who holds it: {why}");
    }

    #[test]
    fn a_command_alone_is_refused_while_a_command_runs_beside() {
        let db = migrated();
        let _command = connect(&db, Holds::Beside).expect("a command");
        let why = connect(&db, Holds::Alone).expect_err("refused");
        assert!(why.starts_with(ALONE_REFUSED), "says who holds it: {why}");
    }

    #[test]
    fn a_second_command_alone_is_refused() {
        let db = migrated();
        let _command = connect(&db, Holds::Alone).expect("alone");
        let why = connect(&db, Holds::Alone).expect_err("refused");
        assert!(why.starts_with(ALONE_REFUSED), "says who holds it: {why}");
    }

    #[test]
    fn a_server_is_refused_while_a_command_runs_alone() {
        let db = migrated();
        let _command = connect(&db, Holds::Alone).expect("alone");
        let why = connect(&db, Holds::Serve).expect_err("refused");
        assert!(why.starts_with(SERVER_REFUSED), "says who holds it: {why}");
    }

    #[test]
    fn a_command_beside_is_refused_while_a_command_runs_alone() {
        let db = migrated();
        let _command = connect(&db, Holds::Alone).expect("alone");
        let why = connect(&db, Holds::Beside).expect_err("refused");
        assert!(why.starts_with(BESIDE_REFUSED), "says who holds it: {why}");
    }

    #[test]
    fn migrate_is_refused_while_a_command_runs_beside() {
        let db = migrated();
        let _command = connect(&db, Holds::Beside).expect("a command");
        let why = wait(db.migrate_with(MIGRATIONS)).expect_err("refused");
        assert!(
            why.contains("another server or command holds the database's locks"),
            "says so: {why}"
        );
    }

    #[test]
    fn a_command_beside_is_refused_while_migrate_holds_the_locks() {
        let runtime = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .expect("a runtime");
        let db = runtime.block_on(Db::migrated());
        let _holder = runtime.block_on(async {
            let holder = db.admin().await;
            holder
                .batch_execute(&format!(
                    "SELECT pg_advisory_lock({SERVERS_LOCK}), pg_advisory_lock({COMMANDS_LOCK})"
                ))
                .await
                .expect("holds the locks migrate takes");
            holder
        });
        let why = connect(&db, Holds::Beside).expect_err("refused");
        assert!(why.starts_with(BESIDE_REFUSED), "says who holds it: {why}");
    }

    // The edges.

    #[tokio::test]
    async fn a_refused_work_gives_up_the_lock_it_got_while_its_session_is_open() {
        // A session that ends frees its locks, and that is seen some time
        // after: the refused work gives up what it got before it ends.
        let db = Db::migrated().await;
        let holder = db.admin().await;
        holder
            .batch_execute(&format!("SELECT pg_advisory_lock_shared({COMMANDS_LOCK})"))
            .await
            .expect("holds the commands' lock, as a command beside does");
        let client = db.app().await;
        let why = take_locks_for(&client, Holds::Alone)
            .await
            .expect_err("refused the commands' lock");
        assert!(why.starts_with("a server or another command"), "{why}");
        assert_eq!(
            value(
                &client,
                "SELECT count(*) FROM pg_locks WHERE locktype = 'advisory' AND granted \
                 AND pid = pg_backend_pid()"
            )
            .await,
            "0",
            "the session that was refused holds no lock, and is still open"
        );
    }

    #[test]
    fn a_command_alone_that_is_refused_one_lock_keeps_neither() {
        let db = migrated();
        let _command = connect(&db, Holds::Beside).expect("a command beside");
        connect(&db, Holds::Alone).expect_err("refused the commands' lock");
        assert_eq!(
            held(&db),
            format!("{COMMANDS_LOCK}:ShareLock"),
            "the servers' lock it was given is given up"
        );
        connect(&db, Holds::Serve).expect("so a server starts");
    }

    #[test]
    fn a_server_takes_nothing_on_the_commands_lock() {
        let db = migrated();
        let _server = connect(&db, Holds::Serve).expect("the server");
        assert_eq!(
            held(&db),
            format!("{SERVERS_LOCK}:ExclusiveLock"),
            "the servers' lock alone"
        );
    }

    #[test]
    fn a_command_beside_takes_nothing_on_the_servers_lock() {
        let db = migrated();
        let _command = connect(&db, Holds::Beside).expect("a command");
        assert_eq!(
            held(&db),
            format!("{COMMANDS_LOCK}:ShareLock"),
            "the commands' lock alone, shared"
        );
    }

    #[test]
    fn a_command_beside_that_ends_gives_its_lock_up() {
        let db = migrated();
        let command = connect(&db, Holds::Beside).expect("a command");
        command.give_up_lock();
        assert_eq!(held(&db), "", "nothing is held");
        connect(&db, Holds::Alone).expect("so a command runs alone");
    }

    #[test]
    fn a_process_that_ends_without_a_word_frees_its_locks() {
        let db = migrated();
        // Dropped with its lock held, as a process that is killed leaves
        // it: PostgreSQL frees the locks when it sees the session closed.
        drop(connect(&db, Holds::Alone).expect("alone"));
        let mut freed = false;
        for _ in 0..100 {
            if held(&db).is_empty() {
                freed = true;
                break;
            }
            std::thread::sleep(std::time::Duration::from_millis(50));
        }
        assert!(freed, "within five seconds of the session's end");
        connect(&db, Holds::Alone).expect("the next command runs");
    }

    #[test]
    fn the_connection_that_serves_is_the_one_that_connect_makes() {
        let db = migrated();
        let (_dir, settings) = db.settings("vcon_app");
        let _server = Database::connect(&settings).expect("a server");
        assert_eq!(
            held(&db),
            format!("{SERVERS_LOCK}:ExclusiveLock"),
            "connect is a server's connect"
        );
    }
}