-
Notifications
You must be signed in to change notification settings - Fork 487
Expand file tree
/
Copy pathlibsql.rs
More file actions
1397 lines (1258 loc) · 46.1 KB
/
libsql.rs
File metadata and controls
1397 lines (1258 loc) · 46.1 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::path::{Path, PathBuf};
use std::sync::Arc;
use libsql_sys::wal::wrapper::{WalWrapper, WrapWal, WrappedWal};
use libsql_sys::wal::{BusyHandler, CheckpointCallback, Wal, WalManager};
use metrics::{histogram, increment_counter};
use once_cell::sync::Lazy;
use parking_lot::{Mutex, RwLock};
use rusqlite::ffi::SQLITE_BUSY;
use rusqlite::{DatabaseName, ErrorCode, OpenFlags, StatementStatus, TransactionState};
use tokio::sync::{watch, Notify};
use tokio::time::{Duration, Instant};
use crate::auth::{Authenticated, Authorized, Permission};
use crate::connection::TXN_TIMEOUT;
use crate::error::Error;
use crate::metrics::{
DESCRIBE_COUNT, PROGRAM_EXEC_COUNT, READ_QUERY_COUNT, VACUUM_COUNT, WAL_CHECKPOINT_COUNT,
WRITE_QUERY_COUNT, WRITE_TXN_DURATION,
};
use crate::namespace::meta_store::MetaStoreHandle;
use crate::query::Query;
use crate::query_analysis::{StmtKind, TxnStatus};
use crate::query_result_builder::{QueryBuilderConfig, QueryResultBuilder};
use crate::replication::FrameNo;
use crate::stats::Stats;
use crate::Result;
use super::program::{Cond, DescribeCol, DescribeParam, DescribeResponse};
use super::{MakeConnection, Program, Step};
pub struct MakeLibSqlConn<T: WalManager> {
db_path: PathBuf,
wal_manager: T,
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>>,
state: Arc<TxnState<T::Wal>>,
/// In wal mode, closing the last database takes time, and causes other databases creation to
/// return sqlite busy. To mitigate that, we hold on to one connection
_db: Option<LibSqlConnection<T::Wal>>,
encryption_key: Option<bytes::Bytes>,
}
impl<T> MakeLibSqlConn<T>
where
T: WalManager + Clone + Send + 'static,
T::Wal: Send + 'static,
{
#[allow(clippy::too_many_arguments)]
pub async fn new(
db_path: PathBuf,
wal_manager: T,
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_key: Option<bytes::Bytes>,
) -> Result<Self> {
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,
state: Default::default(),
wal_manager,
encryption_key,
};
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<T::Wal>> {
// 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)?,
}
}
}
async fn make_connection(&self) -> Result<LibSqlConnection<T::Wal>> {
LibSqlConnection::new(
self.db_path.clone(),
self.extensions.clone(),
self.wal_manager.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_key: self.encryption_key.clone(),
},
self.current_frame_no_receiver.clone(),
self.state.clone(),
)
.await
}
}
#[async_trait::async_trait]
impl<T> MakeConnection for MakeLibSqlConn<T>
where
T: WalManager + Clone + Send + Sync + 'static,
T::Wal: Send,
{
type Connection = LibSqlConnection<T::Wal>;
async fn create(&self) -> Result<Self::Connection, Error> {
self.make_connection().await
}
}
pub struct LibSqlConnection<T> {
inner: Arc<Mutex<Connection<T>>>,
}
impl<T> Clone for LibSqlConnection<T> {
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
}
}
}
impl<T> std::fmt::Debug for LibSqlConnection<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.inner.try_lock() {
Some(conn) => {
write!(f, "{conn:?}")
}
None => write!(f, "<locked>"),
}
}
}
#[derive(Clone, Copy)]
pub struct InhibitCheckpointWalWrapper;
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<()> {
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>>(
&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_key: Option<bytes::Bytes>,
) -> Result<libsql_sys::Connection<InhibitCheckpoint<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,
WalWrapper::new(InhibitCheckpointWalWrapper, wal_manager),
u32::MAX,
encryption_key,
)
}
/// 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_key: Option<bytes::Bytes>,
) -> 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_key,
)
}
impl<W> LibSqlConnection<W>
where
W: Wal + Send + 'static,
{
pub async fn new<T>(
path: impl AsRef<Path> + Send + 'static,
extensions: Arc<[PathBuf]>,
wal_manager: T,
stats: Arc<Stats>,
config_store: MetaStoreHandle,
builder_config: QueryBuilderConfig,
current_frame_no_receiver: watch::Receiver<Option<FrameNo>>,
state: Arc<TxnState<W>>,
) -> crate::Result<Self>
where
T: WalManager<Wal = W> + Send + 'static,
{
let max_db_size = config_store.get().max_db_pages;
let conn = tokio::task::spawn_blocking(move || -> crate::Result<_> {
let conn = Connection::new(
path.as_ref(),
extensions,
wal_manager,
stats,
config_store,
builder_config,
current_frame_no_receiver,
state,
)?;
conn.conn
.pragma_update(None, "max_page_count", max_db_size)?;
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)
})
.await
.unwrap()?;
Ok(Self {
inner: Arc::new(Mutex::new(conn)),
})
}
pub fn txn_status(&self) -> crate::Result<TxnStatus> {
Ok(self
.inner
.lock()
.conn
.transaction_state(Some(DatabaseName::Main))?
.into())
}
}
#[cfg(test)]
impl LibSqlConnection<libsql_sys::wal::Sqlite3Wal> {
pub fn new_test(path: &Path) -> Self {
let (_snd, rcv) = watch::channel(None);
let conn = Connection::new(
path,
Arc::new([]),
libsql_sys::wal::Sqlite3WalManager::new(),
Default::default(),
MetaStoreHandle::new_test(),
QueryBuilderConfig::default(),
rcv,
Default::default(),
)
.unwrap();
Self {
inner: Arc::new(Mutex::new(conn)),
}
}
}
struct Connection<T> {
conn: libsql_sys::Connection<T>,
stats: Arc<Stats>,
config_store: MetaStoreHandle,
builder_config: QueryBuilderConfig,
current_frame_no_receiver: watch::Receiver<Option<FrameNo>>,
// must be dropped after the connection because the connection refers to it
state: Arc<TxnState<T>>,
// current txn slot if any
slot: Option<Arc<TxnSlot<T>>>,
}
impl<T> std::fmt::Debug for Connection<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Connection")
.field("slot", &self.slot)
.finish()
}
}
/// A slot for holding the state of a transaction lock permit
struct TxnSlot<T> {
/// Pointer to the connection holding the lock. Used to rollback the transaction when the lock
/// is stolen.
conn: Arc<Mutex<Connection<T>>>,
/// Time at which the transaction can be stolen
created_at: tokio::time::Instant,
/// The transaction lock was stolen
is_stolen: parking_lot::Mutex<bool>,
txn_timeout: Duration,
}
impl<T> TxnSlot<T> {
#[inline]
fn expires_at(&self) -> Instant {
self.created_at + self.txn_timeout
}
/// abort the connection for that slot.
/// This methods must not be called if a lock on the state's slot is still held.
fn abort(&self)
where
T: Wal,
{
let conn = self.conn.lock();
// we have a lock on the connection, we don't need mode than a
// Relaxed store.
conn.rollback();
WRITE_TXN_DURATION.record(self.created_at.elapsed());
}
}
impl<T> std::fmt::Debug for TxnSlot<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let stolen = self.is_stolen.lock();
let time_left = self.expires_at().duration_since(Instant::now());
write!(
f,
"(conn: {:?}, timeout: {time_left:?}, stolen: {stolen})",
self.conn
)
}
}
/// The transaction state shared among all connections to the same database
#[derive(Debug)]
pub struct TxnState<T> {
/// Slot for the connection currently holding the transaction lock
slot: RwLock<Option<Arc<TxnSlot<T>>>>,
/// Notifier for when the lock gets dropped
notify: Notify,
}
impl<T> Default for TxnState<T> {
fn default() -> Self {
Self {
slot: Default::default(),
notify: Default::default(),
}
}
}
/// The lock-stealing busy handler.
/// Here is a detailed description of the algorithm:
/// - all connections to a database share a `TxnState`, that contains a `TxnSlot`
/// - when a connection acquire a write lock to the database, this is detected by monitoring the state of the
/// connection before and after the call thanks to [sqlite3_txn_state()](https://www.sqlite.org/c3ref/c_txn_none.html)
/// - if the connection acquired a write lock (txn state none/read -> write), a new txn slot is created. A clone of the
/// `TxnSlot` is placed in the `TxnState` shared with other connections to this database, while another clone is kept in
/// the transaction state. The TxnSlot contains: the instant at which the txn should timeout, a `is_stolen` flag, and a
/// pointer to the connection currently holding the lock.
/// - when another connection attempts to acquire the lock, the `busy_handler` callback will be called. The callback is being
/// passed the `TxnState` for the connection. The handler looks at the current slot to determine when the current txn will
/// timeout, and waits for that instant before retrying. The waiting handler can also be notified that the transaction has
/// been finished early.
/// - If the handler waits until the txn timeout and isn't notified of the termination of the txn, it will attempt to steal the lock.
/// This is done by calling rollback on the slot's txn, and marking the slot as stolen.
/// - When a connection notices that it's slot has been stolen, it returns a timedout error to the next request.
const MAX_BUSY_RETRIES: c_int = 512;
unsafe extern "C" fn busy_handler<T: Wal>(state: *mut c_void, retries: c_int) -> c_int {
let state = &*(state as *mut TxnState<T>);
let lock = state.slot.read();
// we take a reference to the slot we will attempt to steal. this is to make sure that we
// actually steal the correct lock.
let slot = match &*lock {
Some(slot) => slot.clone(),
// fast path: there is no slot, try to acquire the lock again
None if retries < 512 => {
std::thread::sleep(std::time::Duration::from_millis(10));
return 1;
}
None => {
tracing::info!("Failed to steal connection lock after {MAX_BUSY_RETRIES} retries.");
return 0;
}
};
tokio::runtime::Handle::current().block_on(async move {
let timeout = {
let slot = lock.as_ref().unwrap();
let timeout_at = slot.expires_at();
drop(lock);
tokio::time::sleep_until(timeout_at)
};
tokio::select! {
// The connection has notified us that it's txn has terminated, try to acquire again
_ = state.notify.notified() => 1,
// the current holder of the transaction has timedout, we will attempt to steal their
// lock.
_ = timeout => {
tracing::info!("transaction has timed-out, stealing lock");
// only a single connection gets to steal the lock, others retry
if let Some(mut lock) = state.slot.try_write() {
if let Some(ref s) = *lock {
// The state contains the same lock as the one we're attempting to steal
if Arc::ptr_eq(s, &slot) {
let can_steal = {
let mut can_steal = false;
let mut is_stolen = slot.is_stolen.lock();
if !*is_stolen {
can_steal = true;
*is_stolen = true;
}
can_steal
};
if can_steal {
// The connection holding the current txn will set itself as stolen when it
// detects a timeout, so if we arrive to this point, then there is
// necessarily a slot, and this slot has to be the one we attempted to
// steal.
assert!(lock.take().is_some());
// we drop the lock here, before aborting, because the connection
// may currently be waiting for the lock to commit/abort itself,
// and we don't need the slot lock past that point.
drop(lock);
slot.abort();
tracing::info!("stole transaction lock");
}
}
}
}
1
}
}
})
}
fn value_size(val: &rusqlite::types::ValueRef) -> usize {
use rusqlite::types::ValueRef;
match val {
ValueRef::Null => 0,
ValueRef::Integer(_) => 8,
ValueRef::Real(_) => 8,
ValueRef::Text(s) => s.len(),
ValueRef::Blob(b) => b.len(),
}
}
impl From<TransactionState> for TxnStatus {
fn from(value: TransactionState) -> Self {
match value {
TransactionState::None => TxnStatus::Init,
TransactionState::Read | TransactionState::Write => TxnStatus::Txn,
_ => unreachable!(),
}
}
}
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>>,
state: Arc<TxnState<W>>,
) -> Result<Self> {
let conn = open_conn_active_checkpoint(
path,
wal_manager,
None,
builder_config.auto_checkpoint,
builder_config.encryption_key.clone(),
)?;
// register the lock-stealing busy handler
unsafe {
let ptr = Arc::as_ptr(&state) as *mut _;
rusqlite::ffi::sqlite3_busy_handler(conn.handle(), Some(busy_handler::<W>), ptr);
}
let this = Self {
conn,
stats,
config_store,
builder_config,
current_frame_no_receiver,
state,
slot: None,
};
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::debug!("Loaded extension {}", ext.display());
}
}
Ok(this)
}
fn run<B: QueryResultBuilder>(
this: Arc<Mutex<Self>>,
pgm: Program,
mut builder: B,
) -> Result<B> {
let txn_timeout = this
.lock()
.config_store
.get()
.txn_timeout
.unwrap_or(TXN_TIMEOUT);
let mut results = Vec::with_capacity(pgm.steps.len());
builder.init(&this.lock().builder_config)?;
let mut previous_state = this
.lock()
.conn
.transaction_state(Some(DatabaseName::Main))?;
let mut has_timeout = false;
for step in pgm.steps() {
let mut lock = this.lock();
if !has_timeout {
if let Some(slot) = &lock.slot {
let mut is_stolen = slot.is_stolen.lock();
if *is_stolen || Instant::now() > slot.expires_at() {
// we mark ourselves as stolen to notify any waiting lock thief.
if !*is_stolen {
lock.rollback();
}
*is_stolen = true;
has_timeout = true;
}
}
}
// once there was a timeout, invalidate all the program steps
if has_timeout {
lock.slot = None;
builder.begin_step()?;
builder.step_error(Error::LibSqlTxTimeout)?;
builder.finish_step(0, None)?;
continue;
}
let ret = lock.execute_step(step, &results, &mut builder);
// /!\ always make sure that the state is updated before returning
previous_state = lock.update_state(this.clone(), previous_state, txn_timeout)?;
let res = ret?;
results.push(res);
}
{
let mut lock = this.lock();
let is_autocommit = lock.conn.is_autocommit();
let current_fno = *lock.current_frame_no_receiver.borrow_and_update();
builder.finish(current_fno, is_autocommit)?;
}
Ok(builder)
}
fn update_state(
&mut self,
arc_this: Arc<Mutex<Self>>,
previous_state: TransactionState,
txn_timeout: Duration,
) -> Result<TransactionState> {
use rusqlite::TransactionState as Tx;
let new_state = self.conn.transaction_state(Some(DatabaseName::Main))?;
match (previous_state, new_state) {
// lock was upgraded, claim the slot
(Tx::None | Tx::Read, Tx::Write) => {
let slot = Arc::new(TxnSlot {
conn: arc_this,
created_at: Instant::now(),
is_stolen: false.into(),
txn_timeout,
});
self.slot.replace(slot.clone());
self.state.slot.write().replace(slot);
}
// lock was downgraded, notify a waiter
(Tx::Write, Tx::None | Tx::Read) => {
let old_slot = self
.slot
.take()
.expect("there should be a slot right after downgrading a txn");
let mut maybe_state_slot = self.state.slot.write();
// We need to make sure that the state slot is our slot before removing it.
if let Some(ref state_slot) = *maybe_state_slot {
if Arc::ptr_eq(state_slot, &old_slot) {
maybe_state_slot.take();
}
}
drop(maybe_state_slot);
self.state.notify.notify_waiters();
}
// nothing to do
(_, _) => (),
}
Ok(new_state)
}
fn execute_step(
&mut self,
step: &Step,
results: &[bool],
builder: &mut impl QueryResultBuilder,
) -> Result<bool> {
builder.begin_step()?;
let mut enabled = match step.cond.as_ref() {
Some(cond) => match eval_cond(cond, results, self.is_autocommit()) {
Ok(enabled) => enabled,
Err(e) => {
builder.step_error(e).unwrap();
false
}
},
None => true,
};
let (affected_row_count, last_insert_rowid) = if enabled {
match self.execute_query(&step.query, builder) {
// builder error interrupt the execution of query. we should exit immediately.
Err(e @ Error::BuilderError(_)) => return Err(e),
Err(mut e) => {
if let Error::RusqliteError(err) = e {
let extended_code =
unsafe { rusqlite::ffi::sqlite3_extended_errcode(self.conn.handle()) };
e = Error::RusqliteErrorExtended(err, extended_code as i32);
};
builder.step_error(e)?;
enabled = false;
(0, None)
}
Ok(x) => x,
}
} else {
(0, None)
};
builder.finish_step(affected_row_count, last_insert_rowid)?;
Ok(enabled)
}
fn prepare_attach_query(&self, attached: &str, attached_alias: &str) -> Result<String> {
let attached = attached.strip_prefix('"').unwrap_or(attached);
let attached = attached.strip_suffix('"').unwrap_or(attached);
if attached.contains('/') {
return Err(Error::Internal(format!(
"Invalid attached database name: {:?}",
attached
)));
}
let path = PathBuf::from(self.conn.path().unwrap_or("."));
let dbs_path = path
.parent()
.unwrap_or_else(|| std::path::Path::new(".."))
.parent()
.unwrap_or_else(|| std::path::Path::new(".."))
.canonicalize()
.unwrap_or_else(|_| std::path::PathBuf::from(".."));
let query = format!(
"ATTACH DATABASE 'file:{}?mode=ro' AS \"{attached_alias}\"",
dbs_path.join(attached).join("data").display()
);
tracing::trace!("ATTACH rewritten to: {query}");
Ok(query)
}
fn execute_query(
&self,
query: &Query,
builder: &mut impl QueryResultBuilder,
) -> Result<(u64, Option<i64>)> {
tracing::trace!("executing query: {}", query.stmt.stmt);
increment_counter!("libsql_server_libsql_query_execute");
let start = Instant::now();
let config = self.config_store.get();
let blocked = match query.stmt.kind {
StmtKind::Read | StmtKind::TxnBegin | StmtKind::Other => config.block_reads,
StmtKind::Write => config.block_reads || config.block_writes,
StmtKind::TxnEnd | StmtKind::Release | StmtKind::Savepoint => false,
StmtKind::Attach | StmtKind::Detach => !config.allow_attach,
};
if blocked {
return Err(Error::Blocked(config.block_reason.clone()));
}
let mut stmt = if matches!(query.stmt.kind, StmtKind::Attach) {
match &query.stmt.attach_info {
Some((attached, attached_alias)) => {
let query = self.prepare_attach_query(attached, attached_alias)?;
self.conn.prepare(&query)?
}
None => {
return Err(Error::Internal(format!(
"Failed to ATTACH: {:?}",
query.stmt.attach_info
)))
}
}
} else {
self.conn.prepare(&query.stmt.stmt)?
};
if stmt.readonly() {
READ_QUERY_COUNT.increment(1);
} else {
WRITE_QUERY_COUNT.increment(1);
}
let cols = stmt.columns();
let cols_count = cols.len();
builder.cols_description(cols.iter())?;
drop(cols);
query
.params
.bind(&mut stmt)
.map_err(Error::LibSqlInvalidQueryParams)?;
let mut qresult = stmt.raw_query();
let mut values_total_bytes = 0;
builder.begin_rows()?;
while let Some(row) = qresult.next()? {
builder.begin_row()?;
for i in 0..cols_count {
let val = row.get_ref(i)?;
values_total_bytes += value_size(&val);
builder.add_row_value(val)?;
}
builder.finish_row()?;
}
histogram!("libsql_server_returned_bytes", values_total_bytes as f64);
builder.finish_rows()?;
// sqlite3_changes() is only modified for INSERT, UPDATE or DELETE; it is not reset for SELECT,
// but we want to return 0 in that case.
let affected_row_count = match query.stmt.is_iud {
true => self.conn.changes(),
false => 0,
};
// sqlite3_last_insert_rowid() only makes sense for INSERTs into a rowid table. we can't detect
// a rowid table, but at least we can detect an INSERT
let last_insert_rowid = match query.stmt.is_insert {
true => Some(self.conn.last_insert_rowid()),
false => None,
};
drop(qresult);
self.update_stats(query.stmt.stmt.clone(), &stmt, Instant::now() - start);
Ok((affected_row_count, last_insert_rowid))
}
fn rollback(&self) {
if let Err(e) = self.conn.execute("ROLLBACK", ()) {
tracing::error!("failed to rollback: {e}");
}
}
fn checkpoint(&self) -> Result<()> {
let start = Instant::now();
self.conn
.query_row("PRAGMA wal_checkpoint(TRUNCATE)", (), |_| 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::debug!("Not vacuuming: pages={page_count} freelist={freelist_count}");
}
VACUUM_COUNT.increment(1);
Ok(())
}
fn update_stats(&self, sql: String, stmt: &rusqlite::Statement, elapsed: Duration) {
histogram!("libsql_server_statement_execution_time", elapsed);
let elapsed = elapsed.as_millis() as u64;
let rows_read = stmt.get_status(StatementStatus::RowsRead) as u64;
let rows_written = stmt.get_status(StatementStatus::RowsWritten) as u64;
let mem_used = stmt.get_status(StatementStatus::MemUsed) as u64;
histogram!("libsql_server_statement_mem_used_bytes", mem_used as f64);
let rows_read = if rows_read == 0 && rows_written == 0 {
1
} else {
rows_read
};
self.stats.inc_rows_read(rows_read);
self.stats.inc_rows_written(rows_written);
let weight = rows_read + rows_written;
if self.stats.qualifies_as_top_query(weight) {
self.stats.add_top_query(crate::stats::TopQuery::new(
sql.clone(),
rows_read,
rows_written,
));
}
if self.stats.qualifies_as_slowest_query(elapsed) {
self.stats
.add_slowest_query(crate::stats::SlowestQuery::new(
sql.clone(),
elapsed,
rows_read,
rows_written,
));
}
self.stats
.update_query_metrics(rows_read, rows_written, mem_used, elapsed)
}
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()
}
}
fn eval_cond(cond: &Cond, results: &[bool], is_autocommit: bool) -> Result<bool> {
let get_step_res = |step: usize| -> Result<bool> {
let res = results.get(step).ok_or(Error::InvalidBatchStep(step))?;
Ok(*res)
};
Ok(match cond {
Cond::Ok { step } => get_step_res(*step)?,
Cond::Err { step } => !get_step_res(*step)?,
Cond::Not { cond } => !eval_cond(cond, results, is_autocommit)?,
Cond::And { conds } => conds.iter().try_fold(true, |x, cond| {
eval_cond(cond, results, is_autocommit).map(|y| x & y)
})?,
Cond::Or { conds } => conds.iter().try_fold(false, |x, cond| {
eval_cond(cond, results, is_autocommit).map(|y| x | y)
})?,
Cond::IsAutocommit => is_autocommit,
})
}
fn check_program_auth(auth: Authenticated, pgm: &Program) -> Result<()> {
for step in pgm.steps() {
let query = &step.query;
match (query.stmt.kind, &auth) {
(_, Authenticated::Anonymous) => {
return Err(Error::NotAuthorized(
"anonymous access not allowed".to_string(),
));