forked from tursodatabase/libsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibsql.rs
More file actions
1123 lines (1021 loc) · 35.6 KB
/
libsql.rs
File metadata and controls
1123 lines (1021 loc) · 35.6 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
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::ffi::{c_int, c_void};
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use libsql_storage::{DurableWalManager, LockManager};
use libsql_sys::wal::wrapper::{WrapWal, WrappedWal};
use libsql_sys::wal::{BusyHandler, CheckpointCallback, Wal, WalManager};
use libsql_sys::EncryptionConfig;
use metrics::histogram;
use parking_lot::Mutex;
use rusqlite::ffi::SQLITE_BUSY;
use rusqlite::{ErrorCode, OpenFlags};
use tokio::sync::watch;
use tokio::time::{Duration, Instant};
use crate::error::Error;
use crate::metrics::{DESCRIBE_COUNT, PROGRAM_EXEC_COUNT, VACUUM_COUNT, WAL_CHECKPOINT_COUNT};
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::connection_manager::{
ConnectionManager, InnerWalManager, ManagedConnectionWal, ManagedConnectionWalWrapper,
};
use super::program::{
check_describe_auth, check_program_auth, DescribeCol, DescribeParam, DescribeResponse, Vm,
};
use super::{MakeConnection, Program, RequestContext, TXN_TIMEOUT};
pub struct MakeLibSqlConn<W> {
db_path: PathBuf,
wal_wrapper: W,
stats: Arc<Stats>,
config_store: MetaStoreHandle,
extensions: Arc<[PathBuf]>,
max_response_size: u64,
max_total_response_size: u64,
auto_checkpoint: u32,
current_frame_no_receiver: watch::Receiver<Option<FrameNo>>,
connection_manager: ConnectionManager,
/// return sqlite busy. To mitigate that, we hold on to one connection
_db: Option<LibSqlConnection<W>>,
encryption_config: Option<EncryptionConfig>,
block_writes: Arc<AtomicBool>,
resolve_attach_path: ResolveNamespacePathFn,
lock_manager: Arc<std::sync::Mutex<LockManager>>,
make_wal_manager: Arc<dyn Fn() -> InnerWalManager + Sync + Send + 'static>,
}
impl<W> MakeLibSqlConn<W>
where
W: WrapWal<ManagedConnectionWal> + Send + 'static + Clone,
{
#[allow(clippy::too_many_arguments)]
pub async fn new(
db_path: PathBuf,
wal_wrapper: W,
stats: Arc<Stats>,
config_store: MetaStoreHandle,
extensions: Arc<[PathBuf]>,
max_response_size: u64,
max_total_response_size: u64,
auto_checkpoint: u32,
current_frame_no_receiver: watch::Receiver<Option<FrameNo>>,
encryption_config: Option<EncryptionConfig>,
block_writes: Arc<AtomicBool>,
resolve_attach_path: ResolveNamespacePathFn,
make_wal_manager: Arc<dyn Fn() -> InnerWalManager + Sync + Send + 'static>,
lock_manager: Arc<std::sync::Mutex<LockManager>>,
) -> Result<Self> {
let txn_timeout = config_store.get().txn_timeout.unwrap_or(TXN_TIMEOUT);
let mut this = Self {
db_path,
stats,
config_store,
extensions,
max_response_size,
max_total_response_size,
auto_checkpoint,
current_frame_no_receiver,
_db: None,
wal_wrapper,
encryption_config,
block_writes,
resolve_attach_path,
connection_manager: ConnectionManager::new(txn_timeout),
lock_manager,
make_wal_manager,
};
let db = this.try_create_db().await?;
this._db = Some(db);
Ok(this)
}
/// Tries to create a database, retrying if the database is busy.
async fn try_create_db(&self) -> Result<LibSqlConnection<W>> {
// try 100 times to acquire initial db connection.
let mut retries = 0;
loop {
match self.make_connection().await {
Ok(conn) => return Ok(conn),
Err(
err @ Error::RusqliteError(rusqlite::Error::SqliteFailure(
rusqlite::ffi::Error {
code: ErrorCode::DatabaseBusy,
..
},
_,
)),
) => {
if retries < 100 {
tracing::warn!("Database file is busy, retrying...");
retries += 1;
tokio::time::sleep(Duration::from_millis(100)).await
} else {
Err(err)?;
}
}
Err(e) => Err(e)?,
}
}
}
#[tracing::instrument(skip(self))]
async fn make_connection(&self) -> Result<LibSqlConnection<W>> {
LibSqlConnection::new(
self.db_path.clone(),
self.extensions.clone(),
self.wal_wrapper.clone(),
self.stats.clone(),
self.config_store.clone(),
QueryBuilderConfig {
max_size: Some(self.max_response_size),
max_total_size: Some(self.max_total_response_size),
auto_checkpoint: self.auto_checkpoint,
encryption_config: self.encryption_config.clone(),
},
self.current_frame_no_receiver.clone(),
self.block_writes.clone(),
self.resolve_attach_path.clone(),
self.connection_manager.clone(),
self.lock_manager.clone(),
self.make_wal_manager.clone(),
)
.await
}
}
#[async_trait::async_trait]
impl<W> MakeConnection for MakeLibSqlConn<W>
where
W: WrapWal<ManagedConnectionWal> + Send + Sync + 'static + Clone,
{
type Connection = LibSqlConnection<W>;
async fn create(&self) -> Result<Self::Connection, Error> {
self.make_connection().await
}
}
pub struct LibSqlConnection<T> {
inner: Arc<Mutex<Connection<WrappedWal<T, ManagedConnectionWal>>>>,
}
#[cfg(test)]
impl LibSqlConnection<libsql_sys::wal::wrapper::PassthroughWalWrapper> {
pub async fn new_test(path: &Path) -> Self {
use libsql_sys::wal::either::Either;
use libsql_sys::wal::Sqlite3WalManager;
Self::new(
path.to_owned(),
Arc::new([]),
libsql_sys::wal::wrapper::PassthroughWalWrapper,
Default::default(),
MetaStoreHandle::new_test(),
QueryBuilderConfig::default(),
tokio::sync::watch::channel(None).1,
Default::default(),
Arc::new(|_| unreachable!()),
ConnectionManager::new(TXN_TIMEOUT),
Arc::new(|| Either::A(Sqlite3WalManager::default())),
)
.await
.unwrap()
}
}
impl<T> Clone for LibSqlConnection<T> {
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
}
}
}
#[derive(Clone, Copy)]
pub struct InhibitCheckpointWalWrapper {
close_only: bool,
}
impl InhibitCheckpointWalWrapper {
pub fn new(close_only: bool) -> Self {
Self { close_only }
}
}
impl<W: Wal> WrapWal<W> for InhibitCheckpointWalWrapper {
fn checkpoint(
&mut self,
wrapped: &mut W,
db: &mut libsql_sys::wal::Sqlite3Db,
mode: libsql_sys::wal::CheckpointMode,
busy_handler: Option<&mut dyn BusyHandler>,
sync_flags: u32,
buf: &mut [u8],
checkpoint_cb: Option<&mut dyn CheckpointCallback>,
in_wal: Option<&mut i32>,
backfilled: Option<&mut i32>,
) -> libsql_sys::wal::Result<()> {
if !self.close_only {
wrapped.checkpoint(
db,
mode,
busy_handler,
sync_flags,
buf,
checkpoint_cb,
in_wal,
backfilled,
)
} else {
tracing::warn!(
"checkpoint inhibited: this connection is not allowed to perform checkpoints"
);
Err(rusqlite::ffi::Error::new(SQLITE_BUSY))
}
}
fn close<M: WalManager<Wal = W>>(
&mut self,
manager: &M,
wrapped: &mut W,
db: &mut libsql_sys::wal::Sqlite3Db,
sync_flags: c_int,
_scratch: Option<&mut [u8]>,
) -> libsql_sys::wal::Result<()> {
// sqlite3 wall will not checkpoint if it's not provided with a scratch buffer. We take
// advantage of that to prevent checpoint on such connections.
manager.close(wrapped, db, sync_flags, None)
}
}
pub type InhibitCheckpoint<T> = WrappedWal<InhibitCheckpointWalWrapper, T>;
// Opens a connection with checkpoint inhibited
pub fn open_conn<T>(
path: &Path,
wal_manager: T,
flags: Option<OpenFlags>,
encryption_config: Option<EncryptionConfig>,
) -> Result<libsql_sys::Connection<InhibitCheckpoint<T::Wal>>, rusqlite::Error>
where
T: WalManager,
{
open_conn_active_checkpoint(
path,
wal_manager.wrap(InhibitCheckpointWalWrapper::new(false)),
flags,
u32::MAX,
encryption_config,
)
}
/// Same as open_conn, but with checkpointing activated.
pub fn open_conn_active_checkpoint<T>(
path: &Path,
wal_manager: T,
flags: Option<OpenFlags>,
auto_checkpoint: u32,
encryption_config: Option<EncryptionConfig>,
) -> Result<libsql_sys::Connection<T::Wal>, rusqlite::Error>
where
T: WalManager,
{
let flags = flags.unwrap_or(
OpenFlags::SQLITE_OPEN_READ_WRITE
| OpenFlags::SQLITE_OPEN_CREATE
| OpenFlags::SQLITE_OPEN_URI
| OpenFlags::SQLITE_OPEN_NO_MUTEX,
);
libsql_sys::Connection::open(
path.join("data"),
flags,
wal_manager,
auto_checkpoint,
encryption_config,
)
}
impl<W> LibSqlConnection<W>
where
W: WrapWal<ManagedConnectionWal> + Send + Clone + 'static,
{
pub async fn new(
path: impl AsRef<Path> + Send + 'static,
extensions: Arc<[PathBuf]>,
wal_wrapper: W,
stats: Arc<Stats>,
config_store: MetaStoreHandle,
builder_config: QueryBuilderConfig,
current_frame_no_receiver: watch::Receiver<Option<FrameNo>>,
block_writes: Arc<AtomicBool>,
resolve_attach_path: ResolveNamespacePathFn,
connection_manager: ConnectionManager,
lock_manager: Arc<std::sync::Mutex<LockManager>>,
make_wal: Arc<dyn Fn() -> InnerWalManager + Sync + Send + 'static>,
) -> crate::Result<Self> {
let (conn, id) = tokio::task::spawn_blocking({
let connection_manager = connection_manager.clone();
move || -> crate::Result<_> {
let manager = ManagedConnectionWalWrapper::new(connection_manager);
let id = manager.id();
let wal = make_wal().wrap(manager).wrap(wal_wrapper);
let conn = Connection::new(
path.as_ref(),
extensions,
wal,
stats,
config_store,
builder_config,
current_frame_no_receiver,
block_writes,
resolve_attach_path,
)?;
let namespace = path
.as_ref()
.file_name()
.unwrap_or_default()
.to_os_string()
.into_string()
.unwrap_or_default();
conn.conn.create_scalar_function(
"libsql_server_database_name",
0,
rusqlite::functions::FunctionFlags::SQLITE_UTF8
| rusqlite::functions::FunctionFlags::SQLITE_DETERMINISTIC,
move |_| Ok(namespace.clone()),
)?;
Ok((conn, id))
}
})
.await
.unwrap()?;
let inner = Arc::new(Mutex::new(conn));
connection_manager.register_connection(&inner, id);
Ok(Self { inner })
}
pub fn with_raw<F, R>(&self, f: F) -> R
where
F: FnOnce(&mut rusqlite::Connection) -> R,
{
let mut inner = self.inner.lock();
f(&mut inner.conn)
}
pub async fn execute<B: QueryResultBuilder>(
&self,
pgm: Program,
ctx: RequestContext,
builder: B,
) -> Result<(B, Program)> {
PROGRAM_EXEC_COUNT.increment(1);
check_program_auth(&ctx, &pgm, &self.inner.lock().config_store.get())?;
let conn = self.inner.clone();
BLOCKING_RT
.spawn_blocking(move || Connection::run(conn, pgm, builder))
.await
.unwrap()
}
}
pub(super) struct Connection<W> {
conn: libsql_sys::Connection<W>,
stats: Arc<Stats>,
config_store: MetaStoreHandle,
builder_config: QueryBuilderConfig,
current_frame_no_receiver: watch::Receiver<Option<FrameNo>>,
block_writes: Arc<AtomicBool>,
resolve_attach_path: ResolveNamespacePathFn,
forced_rollback: bool,
}
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> Connection<W> {
fn new<T: WalManager<Wal = W>>(
path: &Path,
extensions: Arc<[PathBuf]>,
wal_manager: T,
stats: Arc<Stats>,
config_store: MetaStoreHandle,
builder_config: QueryBuilderConfig,
current_frame_no_receiver: watch::Receiver<Option<FrameNo>>,
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)?;
conn.set_limit(
rusqlite::limits::Limit::SQLITE_LIMIT_LENGTH,
config.max_row_size as i32,
);
unsafe {
const MAX_RETRIES: c_int = 8;
extern "C" fn do_nothing(_: *mut c_void, n: c_int) -> c_int {
(n < MAX_RETRIES) as _
}
libsql_sys::ffi::sqlite3_busy_handler(
conn.handle(),
Some(do_nothing),
std::ptr::null_mut(),
);
}
let this = Self {
conn,
stats,
config_store,
builder_config,
current_frame_no_receiver,
block_writes,
resolve_attach_path,
forced_rollback: false,
};
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)
}
fn run<B: QueryResultBuilder>(
this: Arc<Mutex<Self>>,
pgm: Program,
mut builder: B,
) -> Result<(B, Program)> {
let (config, stats, block_writes, resolve_attach_path) = {
let 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();
(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;
}
let conn = conn.conn.deref();
vm.step(conn)?;
}
{
let mut lock = this.lock();
let is_autocommit = lock.conn.is_autocommit();
let current_fno = *lock.current_frame_no_receiver.borrow_and_update();
vm.builder().finish(current_fno, is_autocommit)?;
}
Ok((vm.into_builder(), pgm))
}
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;
}
}
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(())
}
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(())
}
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,
})
}
fn is_autocommit(&self) -> bool {
self.conn.is_autocommit()
}
}
#[async_trait::async_trait]
impl<W> super::Connection for LibSqlConnection<W>
where
W: WrapWal<ManagedConnectionWal> + Clone + Send + 'static,
{
async fn execute_program<B: QueryResultBuilder>(
&self,
pgm: Program,
ctx: RequestContext,
builder: B,
_replication_index: Option<FrameNo>,
) -> Result<B> {
self.execute(pgm, ctx, builder).await.map(|(b, _)| b)
}
async fn describe(
&self,
sql: String,
ctx: RequestContext,
_replication_index: Option<FrameNo>,
) -> Result<crate::Result<DescribeResponse>> {
DESCRIBE_COUNT.increment(1);
check_describe_auth(ctx)?;
let conn = self.inner.clone();
let res = tokio::task::spawn_blocking(move || conn.lock().describe(&sql))
.await
.unwrap();
Ok(res)
}
async fn is_autocommit(&self) -> Result<bool> {
Ok(self.inner.lock().is_autocommit())
}
async fn checkpoint(&self) -> Result<()> {
let conn = self.inner.clone();
tokio::task::spawn_blocking(move || conn.lock().checkpoint())
.await
.unwrap()?;
Ok(())
}
async fn vacuum_if_needed(&self) -> Result<()> {
let conn = self.inner.clone();
tokio::task::spawn_blocking(move || conn.lock().vacuum_if_needed())
.await
.unwrap()?;
Ok(())
}
fn diagnostics(&self) -> String {
String::new()
}
}
#[cfg(test)]
mod test {
use itertools::Itertools;
use libsql_sys::wal::either::Either;
use libsql_sys::wal::wrapper::PassthroughWalWrapper;
use libsql_sys::wal::{Sqlite3Wal, Sqlite3WalManager};
use rand::Rng;
use tempfile::tempdir;
use tokio::task::JoinSet;
use crate::auth::Authenticated;
use crate::connection::{Connection as _, 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<Connection<Sqlite3Wal>>> {
let conn = Connection {
conn: libsql_sys::Connection::test(),
stats: Arc::new(Stats::default()),
config_store: MetaStoreHandle::new_test(),
builder_config: QueryBuilderConfig::default(),
current_frame_no_receiver: watch::channel(None).1,
block_writes: Default::default(),
resolve_attach_path: Arc::new(|_| unreachable!()),
forced_rollback: false,
};
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();
Connection::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();
Connection::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 = MakeLibSqlConn::new(
tmp.path().into(),
PassthroughWalWrapper,
Default::default(),
MetaStoreHandle::load(tmp.path()).unwrap(),
Arc::new([]),
100000000,
100000000,
DEFAULT_AUTO_CHECKPOINT,
watch::channel(None).1,
None,
Default::default(),
Arc::new(|_| unreachable!()),
Arc::new(|| Either::A(Sqlite3WalManager::default())),
)
.await
.unwrap();
tokio::time::pause();
let conn = make_conn.make_connection().await.unwrap();
let _builder = Connection::run(
conn.inner.clone(),
Program::seq(&["BEGIN IMMEDIATE"]),
TestBuilder::default(),
)
.unwrap()
.0;
assert!(!conn.inner.lock().conn.is_autocommit());
tokio::time::sleep(Duration::from_secs(1)).await;
let builder = Connection::run(
conn.inner.clone(),
Program::seq(&["create table test (c)"]),
TestBuilder::default(),
)
.unwrap()
.0;
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 = MakeLibSqlConn::new(
tmp.path().into(),
PassthroughWalWrapper,
Default::default(),
MetaStoreHandle::load(tmp.path()).unwrap(),
Arc::new([]),
100000000,
100000000,
DEFAULT_AUTO_CHECKPOINT,
watch::channel(None).1,
None,
Default::default(),
Arc::new(|_| unreachable!()),
Arc::new(|| Either::A(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 = Connection::run(
conn.inner.clone(),
Program::seq(&["BEGIN IMMEDIATE"]),
TestBuilder::default(),
)
.unwrap()
.0;
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 = MakeLibSqlConn::new(
tmp.path().into(),
PassthroughWalWrapper,
Default::default(),
MetaStoreHandle::load(tmp.path()).unwrap(),
Arc::new([]),
100000000,
100000000,
DEFAULT_AUTO_CHECKPOINT,
watch::channel(None).1,
None,
Default::default(),
Arc::new(|_| unreachable!()),
Arc::new(|| Either::A(Sqlite3WalManager::default())),
)
.await
.unwrap();
let conn1 = make_conn.make_connection().await.unwrap();
tokio::task::spawn_blocking({
let conn = conn1.clone();
move || {
let builder = Connection::run(
conn.inner.clone(),
Program::seq(&["BEGIN IMMEDIATE"]),
TestBuilder::default(),
)
.unwrap()
.0;
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 = Connection::run(
conn.inner.clone(),
Program::seq(&["BEGIN IMMEDIATE"]),
TestBuilder::default(),
)
.unwrap()
.0;
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 = Connection::run(
conn.inner.clone(),
Program::seq(&["COMMIT"]),
TestBuilder::default(),
)
.unwrap()
.0;
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 = MakeLibSqlConn::new(
tmp.path().into(),
PassthroughWalWrapper,
Default::default(),
MetaStoreHandle::load(tmp.path()).unwrap(),
Arc::new([]),
100000000,
100000000,
DEFAULT_AUTO_CHECKPOINT,
watch::channel(None).1,
None,
Default::default(),
Arc::new(|_| unreachable!()),
Arc::new(|| Either::A(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)
.await
.unwrap(),
);
conn.execute_program(
Program::seq(&["CREATE TABLE test (x)"]),
ctx.clone(),
TestBuilder::default(),
None,
)
.await
.unwrap();
let run_conn = |maker: Arc<MakeLibSqlConn<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 {