-
Notifications
You must be signed in to change notification settings - Fork 486
Expand file tree
/
Copy pathconnection_core.rs
More file actions
799 lines (716 loc) · 25.8 KB
/
connection_core.rs
File metadata and controls
799 lines (716 loc) · 25.8 KB
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
use libsql_sys::wal::{Wal, WalManager};
use metrics::histogram;
use parking_lot::Mutex;
use crate::connection::legacy::open_conn_active_checkpoint;
use crate::error::Error;
use crate::metrics::{PROGRAM_EXEC_COUNT, QUERY_CANCELED, VACUUM_COUNT, WAL_CHECKPOINT_COUNT};
use crate::namespace::broadcasters::BroadcasterHandle;
use crate::namespace::meta_store::MetaStoreHandle;
use crate::namespace::ResolveNamespacePathFn;
use crate::query_analysis::StmtKind;
use crate::query_result_builder::{QueryBuilderConfig, QueryResultBuilder};
use crate::replication::FrameNo;
use crate::stats::{Stats, StatsUpdateMessage};
use crate::{Result, BLOCKING_RT};
use super::config::DatabaseConfig;
use super::program::{DescribeCol, DescribeParam, DescribeResponse, Program, Vm};
pub type GetCurrentFrameNo = Arc<dyn Fn() -> Option<FrameNo> + Send + Sync + 'static>;
/// The base connection type, shared between legacy and libsql-wal implementations
pub(super) struct CoreConnection<W> {
conn: libsql_sys::Connection<W>,
stats: Arc<Stats>,
config_store: MetaStoreHandle,
builder_config: QueryBuilderConfig,
get_current_frame_no: GetCurrentFrameNo,
block_writes: Arc<AtomicBool>,
resolve_attach_path: ResolveNamespacePathFn,
forced_rollback: bool,
broadcaster: BroadcasterHandle,
hooked: bool,
canceled: Arc<AtomicBool>,
}
fn update_stats(
stats: &Stats,
sql: String,
rows_read: u64,
rows_written: u64,
mem_used: u64,
elapsed: Duration,
) {
stats.send(StatsUpdateMessage {
sql,
elapsed,
rows_read,
rows_written,
mem_used,
});
}
impl<W: Wal + Send + 'static> CoreConnection<W> {
pub(super) fn new<T: WalManager<Wal = W>>(
path: &Path,
extensions: Arc<[PathBuf]>,
wal_manager: T,
stats: Arc<Stats>,
broadcaster: BroadcasterHandle,
config_store: MetaStoreHandle,
builder_config: QueryBuilderConfig,
get_current_frame_no: GetCurrentFrameNo,
block_writes: Arc<AtomicBool>,
resolve_attach_path: ResolveNamespacePathFn,
) -> Result<Self> {
let conn = open_conn_active_checkpoint(
path,
wal_manager,
None,
builder_config.auto_checkpoint,
builder_config.encryption_config.clone(),
)?;
let config = config_store.get();
conn.pragma_update(None, "max_page_count", config.max_db_pages)?;
tracing::debug!("setting PRAGMA synchronous to {}", config.durability_mode);
conn.pragma_update(None, "synchronous", config.durability_mode)?;
conn.set_limit(
rusqlite::limits::Limit::SQLITE_LIMIT_LENGTH,
config.max_row_size as i32,
);
let canceled = Arc::new(AtomicBool::new(false));
conn.progress_handler(100, {
let canceled = canceled.clone();
Some(move || {
let canceled = canceled.load(Ordering::Relaxed);
if canceled {
QUERY_CANCELED.increment(1);
tracing::trace!("request canceled");
}
canceled
})
});
let this = Self {
conn,
stats,
config_store,
builder_config,
block_writes,
resolve_attach_path,
forced_rollback: false,
broadcaster,
hooked: false,
canceled,
get_current_frame_no,
};
for ext in extensions.iter() {
unsafe {
let _guard = rusqlite::LoadExtensionGuard::new(&this.conn).unwrap();
if let Err(e) = this.conn.load_extension(ext, None) {
tracing::error!("failed to load extension: {}", ext.display());
Err(e)?;
}
tracing::trace!("Loaded extension {}", ext.display());
}
}
Ok(this)
}
pub(super) fn raw_mut(&mut self) -> &mut libsql_sys::Connection<W> {
&mut self.conn
}
pub(super) fn raw(&self) -> &libsql_sys::Connection<W> {
&self.conn
}
pub(super) fn config(&self) -> Arc<DatabaseConfig> {
self.config_store.get()
}
pub(super) async fn run_async<B: QueryResultBuilder>(
this: Arc<Mutex<Self>>,
pgm: Program,
builder: B,
) -> Result<B> {
struct Bomb {
canceled: Arc<AtomicBool>,
defused: bool,
}
impl Drop for Bomb {
fn drop(&mut self) {
if !self.defused {
tracing::trace!("cancelling request");
self.canceled.store(true, Ordering::Relaxed);
}
}
}
let canceled = {
let cancelled = this.lock().canceled.clone();
cancelled.store(false, Ordering::Relaxed);
cancelled
};
PROGRAM_EXEC_COUNT.increment(1);
// create the bomb right before spawning the blocking task.
let mut bomb = Bomb {
canceled,
defused: false,
};
let ret = BLOCKING_RT
.spawn_blocking(move || CoreConnection::run(this, pgm, builder))
.await
.unwrap();
bomb.defused = true;
ret
}
pub(super) fn run<B: QueryResultBuilder>(
this: Arc<Mutex<Self>>,
pgm: Program,
mut builder: B,
) -> Result<B> {
let (config, stats, block_writes, resolve_attach_path) = {
let mut lock = this.lock();
let config = lock.config_store.get();
let stats = lock.stats.clone();
let block_writes = lock.block_writes.clone();
let resolve_attach_path = lock.resolve_attach_path.clone();
lock.update_hooks();
(config, stats, block_writes, resolve_attach_path)
};
builder.init(&this.lock().builder_config)?;
let mut vm = Vm::new(
builder,
&pgm,
move |stmt_kind| {
let should_block = match stmt_kind {
StmtKind::Read | StmtKind::TxnBegin => config.block_reads,
StmtKind::Write => {
config.block_reads
|| config.block_writes
|| block_writes.load(Ordering::SeqCst)
}
StmtKind::DDL => config.block_reads || config.block_writes,
StmtKind::TxnEnd
| StmtKind::Release
| StmtKind::Savepoint
| StmtKind::Detach
| StmtKind::Attach(_) => false,
};
(
should_block,
should_block.then(|| config.block_reason.clone()).flatten(),
)
},
move |sql, rows_read, rows_written, mem_used, elapsed| {
update_stats(&stats, sql, rows_read, rows_written, mem_used, elapsed)
},
resolve_attach_path,
);
let mut has_timeout = false;
while !vm.finished() {
let mut conn = this.lock();
if conn.forced_rollback {
has_timeout = true;
conn.forced_rollback = false;
}
// once there was a timeout, invalidate all the program steps
if has_timeout {
vm.builder().begin_step()?;
vm.builder().step_error(Error::LibSqlTxTimeout)?;
vm.builder().finish_step(0, None)?;
vm.advance();
continue;
}
vm.step(&conn.raw())?;
}
{
let lock = this.lock();
let is_autocommit = lock.conn.is_autocommit();
let current_fno = (lock.get_current_frame_no)();
vm.builder().finish(current_fno, is_autocommit)?;
}
Ok(vm.into_builder())
}
fn rollback(&self) {
if let Err(e) = self.conn.execute("ROLLBACK", ()) {
tracing::error!("failed to rollback: {e}");
}
}
pub(super) fn force_rollback(&mut self) {
if !self.forced_rollback {
self.rollback();
self.forced_rollback = true;
}
}
pub(super) fn checkpoint(&self) -> Result<()> {
let start = Instant::now();
self.conn
.query_row("PRAGMA wal_checkpoint(TRUNCATE)", (), |row| {
let status: i32 = row.get(0)?;
let wal_frames: i32 = row.get(1)?;
let moved_frames: i32 = row.get(2)?;
tracing::info!(
"WAL checkpoint successful, status: {}, WAL frames: {}, moved frames: {}",
status,
wal_frames,
moved_frames
);
Ok(())
})?;
WAL_CHECKPOINT_COUNT.increment(1);
histogram!("libsql_server_wal_checkpoint_time", start.elapsed());
Ok(())
}
pub(super) fn vacuum_if_needed(&self) -> Result<()> {
let page_count = self
.conn
.query_row("PRAGMA page_count", (), |row| row.get::<_, i64>(0))?;
let freelist_count = self
.conn
.query_row("PRAGMA freelist_count", (), |row| row.get::<_, i64>(0))?;
// NOTICE: don't bother vacuuming if we don't have at least 256MiB of data
if page_count >= 65536 && freelist_count * 2 > page_count {
tracing::info!("Vacuuming: pages={page_count} freelist={freelist_count}");
self.conn.execute("VACUUM", ())?;
} else {
tracing::trace!("Not vacuuming: pages={page_count} freelist={freelist_count}");
}
VACUUM_COUNT.increment(1);
Ok(())
}
pub(super) fn describe(&self, sql: &str) -> crate::Result<DescribeResponse> {
let stmt = self.conn.prepare(sql)?;
let params = (1..=stmt.parameter_count())
.map(|param_i| {
let name = stmt.parameter_name(param_i).map(|n| n.into());
DescribeParam { name }
})
.collect();
let cols = stmt
.columns()
.into_iter()
.map(|col| {
let name = col.name().into();
let decltype = col.decl_type().map(|t| t.into());
DescribeCol { name, decltype }
})
.collect();
let is_explain = stmt.is_explain() != 0;
let is_readonly = stmt.readonly();
Ok(DescribeResponse {
params,
cols,
is_explain,
is_readonly,
})
}
pub(super) fn is_autocommit(&self) -> bool {
self.conn.is_autocommit()
}
fn update_hooks(&mut self) {
let (update_fn, commit_fn, rollback_fn) = if self.hooked {
if self.broadcaster.active() {
return;
}
self.hooked = false;
(None, None, None)
} else {
let Some(broadcaster) = self.broadcaster.get() else {
return;
};
let update = broadcaster.clone();
let update_fn = Some(move |action: _, _: &_, table: &_, _| {
update.notify(table, action);
});
let commit = broadcaster.clone();
let commit_fn = Some(move || {
commit.commit();
false // allow commit to go through
});
let rollback = broadcaster;
let rollback_fn = Some(move || rollback.rollback());
(update_fn, commit_fn, rollback_fn)
};
self.conn.update_hook(update_fn);
self.conn.commit_hook(commit_fn);
self.conn.rollback_hook(rollback_fn);
}
}
#[cfg(test)]
mod test {
use itertools::Itertools;
use libsql_sys::wal::wrapper::PassthroughWalWrapper;
use libsql_sys::wal::{Sqlite3Wal, Sqlite3WalManager};
use rand::Rng;
use tempfile::tempdir;
use tokio::task::JoinSet;
use tokio::time::Instant;
use crate::auth::Authenticated;
use crate::connection::legacy::MakeLegacyConnection;
use crate::connection::{Connection as _, RequestContext, TXN_TIMEOUT};
use crate::namespace::meta_store::{metastore_connection_maker, MetaStore};
use crate::namespace::NamespaceName;
use crate::query_result_builder::test::{test_driver, TestBuilder};
use crate::query_result_builder::QueryResultBuilder;
use crate::DEFAULT_AUTO_CHECKPOINT;
use super::*;
fn setup_test_conn() -> Arc<Mutex<CoreConnection<Sqlite3Wal>>> {
let conn = CoreConnection {
conn: libsql_sys::Connection::test(),
stats: Arc::new(Stats::default()),
config_store: MetaStoreHandle::new_test(),
builder_config: QueryBuilderConfig::default(),
block_writes: Default::default(),
resolve_attach_path: Arc::new(|_| unreachable!()),
forced_rollback: false,
broadcaster: Default::default(),
hooked: false,
canceled: Arc::new(false.into()),
get_current_frame_no: Arc::new(|| None),
};
let conn = Arc::new(Mutex::new(conn));
let stmts = std::iter::once("create table test (x)")
.chain(std::iter::repeat("insert into test values ('hello world')").take(100))
.collect_vec();
CoreConnection::run(conn.clone(), Program::seq(&stmts), TestBuilder::default()).unwrap();
conn
}
#[test]
fn test_libsql_conn_builder_driver() {
test_driver(1000, |b| {
let conn = setup_test_conn();
CoreConnection::run(conn, Program::seq(&["select * from test"]), b)
})
}
#[ignore = "the new implementation doesn't steal if nobody is trying to acquire a write lock"]
#[tokio::test]
async fn txn_timeout_no_stealing() {
let tmp = tempdir().unwrap();
let make_conn = MakeLegacyConnection::new(
tmp.path().into(),
PassthroughWalWrapper,
Default::default(),
Default::default(),
MetaStoreHandle::load(tmp.path()).unwrap(),
Arc::new([]),
100000000,
100000000,
DEFAULT_AUTO_CHECKPOINT,
Arc::new(|| None),
None,
Default::default(),
Arc::new(|_| unreachable!()),
Arc::new(|| Sqlite3WalManager::default()),
)
.await
.unwrap();
tokio::time::pause();
let conn = make_conn.make_connection().await.unwrap();
let _builder = CoreConnection::run(
conn.inner.clone(),
Program::seq(&["BEGIN IMMEDIATE"]),
TestBuilder::default(),
)
.unwrap();
assert!(!conn.inner.lock().conn.is_autocommit());
tokio::time::sleep(Duration::from_secs(1)).await;
let builder = CoreConnection::run(
conn.inner.clone(),
Program::seq(&["create table test (c)"]),
TestBuilder::default(),
)
.unwrap();
assert!(!conn.is_autocommit().await.unwrap());
assert!(matches!(builder.into_ret()[0], Err(Error::LibSqlTxTimeout)));
}
#[tokio::test]
/// A bunch of txn try to acquire the lock, and never release it. They will try to steal the
/// lock one after the other. All txn should eventually acquire the write lock
async fn serialized_txn_timeouts() {
let tmp = tempdir().unwrap();
let make_conn = MakeLegacyConnection::new(
tmp.path().into(),
PassthroughWalWrapper,
Default::default(),
Default::default(),
MetaStoreHandle::load(tmp.path()).unwrap(),
Arc::new([]),
100000000,
100000000,
DEFAULT_AUTO_CHECKPOINT,
Arc::new(|| None),
None,
Default::default(),
Arc::new(|_| unreachable!()),
Arc::new(|| Sqlite3WalManager::default()),
)
.await
.unwrap();
let mut set = JoinSet::new();
for _ in 0..10 {
let conn = make_conn.make_connection().await.unwrap();
set.spawn_blocking(move || {
let builder = CoreConnection::run(
conn.inner.clone(),
Program::seq(&["BEGIN IMMEDIATE"]),
TestBuilder::default(),
)
.unwrap();
let ret = &builder.into_ret()[0];
assert!(
(ret.is_ok() && !conn.inner.lock().conn.is_autocommit())
|| (matches!(ret, Err(Error::RusqliteErrorExtended(_, 5)))
&& conn.inner.lock().conn.is_autocommit())
);
});
}
tokio::time::pause();
while let Some(ret) = set.join_next().await {
assert!(ret.is_ok());
// advance time by a bit more than the txn timeout
tokio::time::advance(TXN_TIMEOUT + Duration::from_millis(100)).await;
}
}
#[tokio::test]
/// verify that releasing a txn before the timeout
async fn release_before_timeout() {
let tmp = tempdir().unwrap();
let make_conn = MakeLegacyConnection::new(
tmp.path().into(),
PassthroughWalWrapper,
Default::default(),
Default::default(),
MetaStoreHandle::load(tmp.path()).unwrap(),
Arc::new([]),
100000000,
100000000,
DEFAULT_AUTO_CHECKPOINT,
Arc::new(|| None),
None,
Default::default(),
Arc::new(|_| unreachable!()),
Arc::new(|| Sqlite3WalManager::default()),
)
.await
.unwrap();
let conn1 = make_conn.make_connection().await.unwrap();
tokio::task::spawn_blocking({
let conn = conn1.clone();
move || {
let builder = CoreConnection::run(
conn.inner.clone(),
Program::seq(&["BEGIN IMMEDIATE"]),
TestBuilder::default(),
)
.unwrap();
assert!(!conn.inner.lock().is_autocommit());
assert!(builder.into_ret()[0].is_ok());
}
})
.await
.unwrap();
let conn2 = make_conn.make_connection().await.unwrap();
let handle = tokio::task::spawn_blocking({
let conn = conn2.clone();
move || {
let before = Instant::now();
let builder = CoreConnection::run(
conn.inner.clone(),
Program::seq(&["BEGIN IMMEDIATE"]),
TestBuilder::default(),
)
.unwrap();
assert!(!conn.inner.lock().is_autocommit());
assert!(builder.into_ret()[0].is_ok());
before.elapsed()
}
});
let wait_time = TXN_TIMEOUT / 10;
tokio::time::sleep(wait_time).await;
tokio::task::spawn_blocking({
let conn = conn1.clone();
move || {
let builder = CoreConnection::run(
conn.inner.clone(),
Program::seq(&["COMMIT"]),
TestBuilder::default(),
)
.unwrap();
assert!(conn.inner.lock().is_autocommit());
assert!(builder.into_ret()[0].is_ok());
}
})
.await
.unwrap();
let elapsed = handle.await.unwrap();
let epsilon = Duration::from_millis(100);
assert!((wait_time..wait_time + epsilon).contains(&elapsed));
}
/// The goal of this test is to run many concurrent transaction and hopefully catch a bug in
/// the lock stealing code. If this test becomes flaky check out the lock stealing code.
#[tokio::test]
async fn test_many_concurrent() {
let tmp = tempdir().unwrap();
let make_conn = MakeLegacyConnection::new(
tmp.path().into(),
PassthroughWalWrapper,
Default::default(),
Default::default(),
MetaStoreHandle::load(tmp.path()).unwrap(),
Arc::new([]),
100000000,
100000000,
DEFAULT_AUTO_CHECKPOINT,
Arc::new(|| None),
None,
Default::default(),
Arc::new(|_| unreachable!()),
Arc::new(|| Sqlite3WalManager::default()),
)
.await
.unwrap();
let conn = make_conn.make_connection().await.unwrap();
let (maker, manager) = metastore_connection_maker(None, tmp.path()).await.unwrap();
let ctx = RequestContext::new(
Authenticated::FullAccess,
NamespaceName::default(),
MetaStore::new(
Default::default(),
tmp.path(),
maker().unwrap(),
manager,
crate::database::DatabaseKind::Primary,
)
.await
.unwrap(),
);
conn.execute_program(
Program::seq(&["CREATE TABLE test (x)"]),
ctx.clone(),
TestBuilder::default(),
None,
)
.await
.unwrap();
let run_conn = |maker: Arc<MakeLegacyConnection<PassthroughWalWrapper>>| {
let ctx = ctx.clone();
async move {
for _ in 0..1000 {
let conn = maker.make_connection().await.unwrap();
let pgm = Program::seq(&["BEGIN IMMEDIATE", "INSERT INTO test VALUES (42)"]);
let res = conn
.execute_program(pgm, ctx.clone(), TestBuilder::default(), None)
.await
.unwrap()
.into_ret();
for result in res {
result.unwrap();
}
// with 99% change, commit the txn
if rand::thread_rng().gen_range(0..100) > 1 {
let pgm = Program::seq(&["INSERT INTO test VALUES (43)", "COMMIT"]);
let res = conn
.execute_program(pgm, ctx.clone(), TestBuilder::default(), None)
.await
.unwrap()
.into_ret();
for result in res {
result.unwrap();
}
}
}
}
};
let maker = Arc::new(make_conn);
let mut join_set = JoinSet::new();
for _ in 0..3 {
join_set.spawn(run_conn(maker.clone()));
}
let join_all = async move {
while let Some(next) = join_set.join_next().await {
next.unwrap();
}
};
tokio::time::timeout(Duration::from_secs(60), join_all)
.await
.expect("timed out running connections");
}
#[tokio::test]
/// verify that releasing a txn before the timeout
async fn force_rollback_reset() {
let tmp = tempdir().unwrap();
let make_conn = MakeLegacyConnection::new(
tmp.path().into(),
PassthroughWalWrapper,
Default::default(),
Default::default(),
MetaStoreHandle::load(tmp.path()).unwrap(),
Arc::new([]),
100000000,
100000000,
DEFAULT_AUTO_CHECKPOINT,
Arc::new(|| None),
None,
Default::default(),
Arc::new(|_| unreachable!()),
Arc::new(|| Sqlite3WalManager::default()),
)
.await
.unwrap();
let conn1 = make_conn.make_connection().await.unwrap();
tokio::task::spawn_blocking({
let conn = conn1.clone();
move || {
let builder = CoreConnection::run(
conn.inner.clone(),
Program::seq(&["BEGIN IMMEDIATE"]),
TestBuilder::default(),
)
.unwrap();
assert!(!conn.inner.lock().is_autocommit());
assert!(builder.into_ret()[0].is_ok());
}
})
.await
.unwrap();
let conn2 = make_conn.make_connection().await.unwrap();
tokio::task::spawn_blocking({
let conn = conn2.clone();
move || {
let before = Instant::now();
let builder = CoreConnection::run(
conn.inner.clone(),
Program::seq(&["BEGIN IMMEDIATE"]),
TestBuilder::default(),
)
.unwrap();
assert!(!conn.inner.lock().is_autocommit());
assert!(builder.into_ret()[0].is_ok());
before.elapsed()
}
})
.await
.unwrap();
tokio::time::sleep(TXN_TIMEOUT * 2).await;
tokio::task::spawn_blocking({
let conn = conn1.clone();
move || {
let builder = CoreConnection::run(
conn.inner.clone(),
Program::seq(&["SELECT 1;"]),
TestBuilder::default(),
)
.unwrap();
assert!(conn.inner.lock().is_autocommit());
// timeout
assert!(builder.into_ret()[0].is_err());
let builder = CoreConnection::run(
conn.inner.clone(),
Program::seq(&["SELECT 1;"]),
TestBuilder::default(),
)
.unwrap();
assert!(conn.inner.lock().is_autocommit());
// state reset
assert!(builder.into_ret()[0].is_ok());
}
})
.await
.unwrap();
}
}