laburnum 1.17.0

An LSP framework for building language servers and compilers, powered by an incremental query tree with content-addressed storage, task-based dataflow, and parallel queries.
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
// Copyright Two Neutron Stars Incorporated and contributors
// SPDX-License-Identifier: BlueOak-1.0.0

use {
  super::common::*,
  crate::{
    Ident,
    database::{
      chunk::RecordWriter,
      PartitionKey,
    },
    scheduler::lanes::*,
  },
  macro_rules_attribute::apply,
  std::{
    sync::{
      Arc,
      atomic::{
        AtomicBool,
        Ordering,
      },
    },
    time::Duration,
  },
};

fn partition_key() -> Ident {
  TestPartition::KEY
}

#[apply(smol_macros::test!)]
#[test_log::test]
async fn test_task_writes_single_record() {
  let (scheduler, _conn) = test_scheduler();
  let task_executed = Arc::new(AtomicBool::new(false));
  let task_exec = task_executed.clone();

  let sort_key = "record_1".to_string();
  let test_value = "hello world".to_string();
  let test_count = 42;

  scheduler.queue(
    move |_ctx| {
      let sk = sort_key.clone();
      let val = test_value.clone();
      let cnt = test_count;
      let exec = task_exec.clone();

      async move {
        exec.store(true, Ordering::SeqCst);
        let mut builder = RecordWriter::new(Ident::new("task_1"));
        builder.insert::<TestPartition, _>(sk, TestRecordData::new(val, cnt));
        Some(builder)
      }
    },
    DEFAULT_LANE,
  );
  scheduler.spawn_workers();

  assert!(
    wait_for(
      || task_executed.load(Ordering::SeqCst),
      Duration::from_secs(5)
    )
    .await,
    "Task did not execute"
  );

  let verify_executed = Arc::new(AtomicBool::new(false));
  let verify_success = Arc::new(AtomicBool::new(false));
  let verify_exec = verify_executed.clone();
  let verify_ok = verify_success.clone();

  scheduler.queue(
    move |mut ctx| {
      let exec = verify_exec.clone();
      let success = verify_ok.clone();
      async move {
        let query_client = ctx.query_client();
        exec.store(true, Ordering::SeqCst);
        let results = query_client
          .get_record(partition_key(), "record_1".to_string())
          .await;

        if !results.is_empty()
          && results.len() == 1
          && let Some(TestRecord::Test(record)) = results.get(&results.records()[0])
          && record.value == "hello world"
          && record.count == 42
        {
          success.store(true, Ordering::SeqCst);
        }

        None
      }
    },
    DEFAULT_LANE,
  );

  assert!(
    wait_for(
      || verify_success.load(Ordering::SeqCst),
      Duration::from_secs(5)
    )
    .await,
    "Verify task did not find expected record"
  );
}

#[apply(smol_macros::test!)]
#[test_log::test]
async fn test_task_writes_multiple_records() {
  let (scheduler, _conn) = test_scheduler();
  let task_executed = Arc::new(AtomicBool::new(false));
  let task_exec = task_executed.clone();

  let num_records = 5;

  scheduler.queue(
    move |_ctx| {
      let exec = task_exec.clone();

      async move {
        exec.store(true, Ordering::SeqCst);
        let mut builder =
          RecordWriter::new(Ident::new("task_multi"));

        for i in 0..num_records {
          builder.insert::<TestPartition, _>(
            format!("record_{}", i),
            TestRecordData::new(format!("value_{}", i), i),
          );
        }

        Some(builder)
      }
    },
    DEFAULT_LANE,
  );
  scheduler.spawn_workers();

  assert!(
    wait_for(
      || task_executed.load(Ordering::SeqCst),
      Duration::from_secs(5)
    )
    .await,
    "Task did not execute"
  );

  let verify_executed = Arc::new(AtomicBool::new(false));
  let verify_success = Arc::new(AtomicBool::new(false));
  let verify_exec = verify_executed.clone();
  let verify_ok = verify_success.clone();

  scheduler.queue(
    move |mut ctx| {
      let exec = verify_exec.clone();
      let success = verify_ok.clone();
      async move {
        let query_client = ctx.query_client();
        exec.store(true, Ordering::SeqCst);
        let mut all_correct = true;

        for i in 0..num_records {
          let results = query_client
            .get_record(partition_key(), format!("record_{}", i))
            .await;

          if results.is_empty() {
            all_correct = false;
            break;
          }

          if let Some(TestRecord::Test(record)) = results.get(&results.records()[0]) {
            if record.value != format!("value_{}", i) || record.count != i {
              all_correct = false;
              break;
            }
          } else {
            all_correct = false;
            break;
          }
        }

        if all_correct {
          success.store(true, Ordering::SeqCst);
        }
        None
      }
    },
    DEFAULT_LANE,
  );

  assert!(
    wait_for(
      || verify_success.load(Ordering::SeqCst),
      Duration::from_secs(5)
    )
    .await,
    "Verify task did not find all expected records"
  );
}

#[apply(smol_macros::test!)]
#[test_log::test]
async fn test_parent_and_child_write_different_records() {
  let (scheduler, _conn) = test_scheduler();
  let parent_executed = Arc::new(AtomicBool::new(false));
  let child_executed = Arc::new(AtomicBool::new(false));

  let parent_exec = parent_executed.clone();
  let child_exec = child_executed.clone();

  scheduler.queue(
    move |ctx| {
      let exec = parent_exec.clone();
      let child_exec_inner = child_exec.clone();

      ctx.spawn_task(
        move |_child_ctx| {
          async move {
            child_exec_inner.store(true, Ordering::SeqCst);
            let mut builder =
              RecordWriter::new(Ident::new("child_task"));
            builder.insert::<TestPartition, _>(
              "child_record".to_string(),
              TestRecordData::new("child_value".to_string(), 100),
            );
            Some(builder)
          }
        },
        DEFAULT_LANE,
      );

      async move {
        exec.store(true, Ordering::SeqCst);
        let mut builder =
          RecordWriter::new(Ident::new("parent_task"));
        builder.insert::<TestPartition, _>(
          "parent_record".to_string(),
          TestRecordData::new("parent_value".to_string(), 200),
        );
        Some(builder)
      }
    },
    DEFAULT_LANE,
  );
  scheduler.spawn_workers();

  assert!(
    wait_for(
      || parent_executed.load(Ordering::SeqCst),
      Duration::from_secs(5)
    )
    .await,
    "Parent task did not execute"
  );

  assert!(
    wait_for(
      || child_executed.load(Ordering::SeqCst),
      Duration::from_secs(5)
    )
    .await,
    "Child task did not execute"
  );

  let verify_executed = Arc::new(AtomicBool::new(false));
  let verify_success = Arc::new(AtomicBool::new(false));
  let verify_exec = verify_executed.clone();
  let verify_ok = verify_success.clone();

  scheduler.queue(
    move |mut ctx| {
      let exec = verify_exec.clone();
      let success = verify_ok.clone();
      async move {
        let query_client = ctx.query_client();
        exec.store(true, Ordering::SeqCst);

        let parent_results = query_client
          .get_record(
            partition_key(),
            "parent_record".to_string(),
          )
          .await;

        let child_results = query_client
          .get_record(partition_key(), "child_record".to_string())
          .await;

        let mut both_correct = false;
        if !parent_results.is_empty()
          && !child_results.is_empty()
          && let (Some(TestRecord::Test(parent)), Some(TestRecord::Test(child))) = (
            parent_results.get(&parent_results.records()[0]),
            child_results.get(&child_results.records()[0]),
          )
          && parent.value == "parent_value"
          && parent.count == 200
          && child.value == "child_value"
          && child.count == 100
        {
          both_correct = true;
        }

        if both_correct {
          success.store(true, Ordering::SeqCst);
        }
        None
      }
    },
    DEFAULT_LANE,
  );

  assert!(
    wait_for(
      || verify_success.load(Ordering::SeqCst),
      Duration::from_secs(5)
    )
    .await,
    "Verify task did not find expected records"
  );
}

#[apply(smol_macros::test!)]
#[test_log::test]
async fn test_query_within_task() {
  use crate::database::query::QueryClient;

  let (scheduler, _conn) = test_scheduler();
  let first_task_executed = Arc::new(AtomicBool::new(false));
  let second_task_found_record = Arc::new(AtomicBool::new(false));

  let first_exec = first_task_executed.clone();
  let second_found = second_task_found_record.clone();

  scheduler.queue(
    move |_ctx| {
      let exec = first_exec.clone();
      async move {
        exec.store(true, Ordering::SeqCst);
        let mut builder =
          RecordWriter::new(Ident::new("first_task"));
        builder.insert::<TestPartition, _>(
          "shared_record".to_string(),
          TestRecordData::new("shared_value".to_string(), 777),
        );
        Some(builder)
      }
    },
    DEFAULT_LANE,
  );
  scheduler.spawn_workers();

  assert!(
    wait_for(
      || first_task_executed.load(Ordering::SeqCst),
      Duration::from_secs(5)
    )
    .await,
    "First task did not execute"
  );

  let scheduler_clone = scheduler.clone();
  scheduler.queue(
    move |_ctx| {
      let found = second_found.clone();
      let db = scheduler_clone.db.clone();
      async move {
        let mut query_client = QueryClient::new(db);
        let results = query_client
          .get_record(
            partition_key(),
            "shared_record".to_string(),
          )
          .await;

        if !results.records.is_empty() {
          let record_meta = &results.records[0];
          if let Some(TestRecord::Test(record)) = results.get(record_meta)
            && record.value == "shared_value"
            && record.count == 777
          {
            found.store(true, Ordering::SeqCst);
          }
        }

        None
      }
    },
    DEFAULT_LANE,
  );

  assert!(
    wait_for(
      || second_task_found_record.load(Ordering::SeqCst),
      Duration::from_secs(5)
    )
    .await,
    "Second task did not find the record written by first task"
  );
}

#[apply(smol_macros::test!)]
#[test_log::test]
async fn test_snapshot_isolation() {
  let (scheduler, _conn) = test_scheduler();
  let first_executed = Arc::new(AtomicBool::new(false));
  let second_executed = Arc::new(AtomicBool::new(false));

  let first_exec = first_executed.clone();
  let second_exec = second_executed.clone();

  scheduler.queue(
    move |_ctx| {
      let exec = first_exec.clone();
      async move {
        exec.store(true, Ordering::SeqCst);
        let mut builder =
          RecordWriter::new(Ident::new("initial_task"));
        builder.insert::<TestPartition, _>(
          "initial_record".to_string(),
          TestRecordData::new("initial_value".to_string(), 111),
        );
        Some(builder)
      }
    },
    DEFAULT_LANE,
  );
  scheduler.spawn_workers();

  assert!(
    wait_for(
      || first_executed.load(Ordering::SeqCst),
      Duration::from_secs(5)
    )
    .await,
    "First task did not execute"
  );

  scheduler.queue(
    move |_ctx| {
      let exec = second_exec.clone();
      async move {
        exec.store(true, Ordering::SeqCst);
        let mut builder =
          RecordWriter::new(Ident::new("snapshot_task"));
        builder.insert::<TestPartition, _>(
          "new_record".to_string(),
          TestRecordData::new("new_value".to_string(), 999),
        );
        Some(builder)
      }
    },
    DEFAULT_LANE,
  );

  assert!(
    wait_for(
      || second_executed.load(Ordering::SeqCst),
      Duration::from_secs(5)
    )
    .await,
    "Second task did not execute"
  );

  let verify_success = Arc::new(AtomicBool::new(false));
  let verify_ok = verify_success.clone();

  scheduler.queue(
    move |mut ctx| {
      let success = verify_ok.clone();
      async move {
        let query_client = ctx.query_client();
        let initial_results = query_client
          .get_record(partition_key(), "initial_record".to_string())
          .await;
        let new_results = query_client
          .get_record(partition_key(), "new_record".to_string())
          .await;

        if !initial_results.is_empty() && !new_results.is_empty() {
          success.store(true, Ordering::SeqCst);
        }
        None
      }
    },
    DEFAULT_LANE,
  );

  assert!(
    wait_for(
      || verify_success.load(Ordering::SeqCst),
      Duration::from_secs(5)
    )
    .await,
    "Should have both records committed to database"
  );
}