forked from tursodatabase/libsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
514 lines (442 loc) · 14 KB
/
mod.rs
File metadata and controls
514 lines (442 loc) · 14 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
use libsql_replication::rpc::replication::NAMESPACE_METADATA_KEY;
use std::ops::Deref;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use tokio::time::{Duration, Instant};
use futures::Future;
use tokio::{sync::Semaphore, time::timeout};
use tonic::metadata::BinaryMetadataValue;
use crate::auth::Authenticated;
use crate::error::Error;
use crate::metrics::{
CONCURRENT_CONNECTIONS_COUNT, CONNECTION_ALIVE_DURATION, CONNECTION_CREATE_TIME,
};
use crate::namespace::meta_store::MetaStore;
use crate::namespace::NamespaceName;
use crate::query::{Params, Query};
use crate::query_analysis::Statement;
use crate::query_result_builder::{IgnoreResult, QueryResultBuilder};
use crate::replication::FrameNo;
use crate::Result;
use self::program::{Cond, DescribeResponse, Program, Step};
pub mod config;
pub mod connection_manager;
pub mod dump;
pub mod libsql;
pub mod program;
pub mod write_proxy;
#[cfg(not(test))]
const TXN_TIMEOUT: Duration = Duration::from_secs(500);
#[cfg(test)]
const TXN_TIMEOUT: Duration = Duration::from_millis(100);
#[derive(Clone)]
pub struct RequestContext {
/// Authentication for this request
auth: Authenticated,
/// current namespace
namespace: NamespaceName,
meta_store: MetaStore,
}
impl RequestContext {
pub fn new(auth: Authenticated, namespace: NamespaceName, meta_store: MetaStore) -> Self {
Self {
auth,
namespace,
meta_store,
}
}
pub fn upgrade_grpc_request<T>(&self, req: &mut tonic::Request<T>) {
let namespace = BinaryMetadataValue::from_bytes(self.namespace.as_slice());
req.metadata_mut()
.insert_bin(NAMESPACE_METADATA_KEY, namespace);
self.auth.upgrade_grpc_request(req);
}
pub fn namespace(&self) -> &NamespaceName {
&self.namespace
}
pub fn auth(&self) -> &Authenticated {
&self.auth
}
}
#[async_trait::async_trait]
pub trait Connection: Send + Sync + 'static {
/// Executes a query program
async fn execute_program<B: QueryResultBuilder>(
&self,
pgm: Program,
ctx: RequestContext,
response_builder: B,
replication_index: Option<FrameNo>,
) -> Result<B>;
/// Execute all the queries in the batch sequentially.
/// If an query in the batch fails, the remaining queries are ignores, and the batch current
/// transaction (if any) is rolledback.
async fn execute_batch_or_rollback<B: QueryResultBuilder>(
&self,
batch: Vec<Query>,
ctx: RequestContext,
result_builder: B,
replication_index: Option<FrameNo>,
) -> Result<B> {
let batch_len = batch.len();
let mut steps = make_batch_program(batch);
if !steps.is_empty() {
// We add a conditional rollback step if the last step was not successful.
steps.push(Step {
query: Query {
stmt: Statement::parse("ROLLBACK").next().unwrap().unwrap(),
params: Params::empty(),
want_rows: false,
},
cond: Some(Cond::Not {
cond: Box::new(Cond::Ok {
step: steps.len() - 1,
}),
}),
})
}
let pgm = Program::new(steps);
// ignore the rollback result
let builder = result_builder.take(batch_len);
let builder = self
.execute_program(pgm, ctx, builder, replication_index)
.await?;
Ok(builder.into_inner())
}
/// Execute all the queries in the batch sequentially.
/// If an query in the batch fails, the remaining queries are ignored
async fn execute_batch<B: QueryResultBuilder>(
&self,
batch: Vec<Query>,
ctx: RequestContext,
result_builder: B,
replication_index: Option<FrameNo>,
) -> Result<B> {
let steps = make_batch_program(batch);
let pgm = Program::new(steps);
self.execute_program(pgm, ctx, result_builder, replication_index)
.await
}
async fn rollback(&self, ctx: RequestContext) -> Result<()> {
self.execute_batch(
vec![Query {
stmt: Statement::parse("ROLLBACK").next().unwrap().unwrap(),
params: Params::empty(),
want_rows: false,
}],
ctx,
IgnoreResult,
None,
)
.await?;
Ok(())
}
/// Parse the SQL statement and return information about it.
async fn describe(
&self,
sql: String,
ctx: RequestContext,
replication_index: Option<FrameNo>,
) -> Result<Result<DescribeResponse>>;
/// Check whether the connection is in autocommit mode.
async fn is_autocommit(&self) -> Result<bool>;
/// Calls for database checkpoint (if supported).
async fn checkpoint(&self) -> Result<()>;
// Calls for database vacuum (if supported).
async fn vacuum_if_needed(&self) -> Result<()>;
fn diagnostics(&self) -> String;
}
fn make_batch_program(batch: Vec<Query>) -> Vec<Step> {
let mut steps = Vec::with_capacity(batch.len());
for (i, query) in batch.into_iter().enumerate() {
let cond = if i > 0 {
// only execute if the previous step was a success
Some(Cond::Ok { step: i - 1 })
} else {
None
};
let step = Step { cond, query };
steps.push(step);
}
steps
}
#[async_trait::async_trait]
pub trait MakeConnection: Send + Sync + 'static {
type Connection: Connection;
/// Create a new connection of type Self::Connection
async fn create(&self) -> Result<Self::Connection, Error>;
fn throttled(
self,
semaphore: Arc<Semaphore>,
timeout: Option<Duration>,
max_total_response_size: u64,
max_concurrent_requests: u64,
) -> MakeThrottledConnection<Self>
where
Self: Sized,
{
MakeThrottledConnection::new(
semaphore,
self,
timeout,
max_total_response_size,
max_concurrent_requests,
)
}
fn map<F, T>(self, f: F) -> Map<Self, F>
where
F: Fn(Self::Connection) -> T + Send + Sync + 'static,
Self: Sized,
{
Map { inner: self, f }
}
}
pub struct Map<T, F> {
inner: T,
f: F,
}
#[async_trait::async_trait]
impl<F, T, O> MakeConnection for Map<T, F>
where
F: Fn(T::Connection) -> O + Send + Sync + 'static,
T: MakeConnection,
O: Connection,
{
type Connection = O;
async fn create(&self) -> Result<Self::Connection, Error> {
let conn = self.inner.create().await?;
Ok((self.f)(conn))
}
}
#[async_trait::async_trait]
impl<T: MakeConnection> MakeConnection for Arc<T> {
type Connection = T::Connection;
async fn create(&self) -> Result<Self::Connection, Error> {
self.as_ref().create().await
}
}
#[async_trait::async_trait]
impl<F, C, Fut> MakeConnection for F
where
F: Fn() -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<C, Error>> + Send,
C: Connection + Sync + Send + 'static,
{
type Connection = C;
async fn create(&self) -> Result<Self::Connection, Error> {
let db = (self)().await?;
Ok(db)
}
}
pub struct MakeThrottledConnection<F> {
semaphore: Arc<Semaphore>,
connection_maker: F,
timeout: Option<Duration>,
// Max memory available for responses. High memory pressure
// will result in reducing concurrency to prevent out-of-memory errors.
max_total_response_size: u64,
waiters: AtomicUsize,
max_concurrent_requests: u64,
}
impl<F> MakeThrottledConnection<F> {
fn new(
semaphore: Arc<Semaphore>,
connection_maker: F,
timeout: Option<Duration>,
max_total_response_size: u64,
max_concurrent_requests: u64,
) -> Self {
Self {
semaphore,
connection_maker,
timeout,
max_total_response_size,
waiters: AtomicUsize::new(0),
max_concurrent_requests,
}
}
// How many units should be acquired from the semaphore,
// depending on current memory pressure.
fn units_to_take(&self) -> u32 {
let total_response_size = crate::query_result_builder::TOTAL_RESPONSE_SIZE
.load(std::sync::atomic::Ordering::Relaxed) as u64;
if total_response_size * 2 > self.max_total_response_size {
tracing::trace!("High memory pressure, reducing concurrency");
16
} else if total_response_size * 4 > self.max_total_response_size {
tracing::trace!("Medium memory pressure, reducing concurrency");
4
} else {
1
}
}
}
struct WaitersGuard<'a> {
pub waiters: &'a AtomicUsize,
}
impl<'a> WaitersGuard<'a> {
fn new(waiters: &'a AtomicUsize) -> Self {
waiters.fetch_add(1, Ordering::Relaxed);
Self { waiters }
}
}
impl Drop for WaitersGuard<'_> {
fn drop(&mut self) {
self.waiters.fetch_sub(1, Ordering::Relaxed);
}
}
#[async_trait::async_trait]
impl<F: MakeConnection> MakeConnection for MakeThrottledConnection<F> {
type Connection = TrackedConnection<F::Connection>;
async fn create(&self) -> Result<Self::Connection, Error> {
let before_create = Instant::now();
// If the memory pressure is high, request more units to reduce concurrency.
tracing::trace!(
"Available semaphore units: {}",
self.semaphore.available_permits()
);
let units = self.units_to_take();
let waiters_guard = WaitersGuard::new(&self.waiters);
if (waiters_guard.waiters.load(Ordering::Relaxed) as u64) >= self.max_concurrent_requests {
return Err(Error::TooManyRequests);
}
let fut = self.semaphore.clone().acquire_many_owned(units);
let mut permit = match self.timeout {
Some(t) => timeout(t, fut).await.map_err(|_| Error::DbCreateTimeout)?,
None => fut.await,
}
.expect("semaphore closed");
let units = self.units_to_take();
if units > 1 {
tracing::debug!("Reacquiring {units} units due to high memory pressure");
let fut = self.semaphore.clone().acquire_many_owned(units);
let mem_permit = match self.timeout {
Some(t) => timeout(t, fut).await.map_err(|_| Error::DbCreateTimeout)?,
None => fut.await,
}
.expect("semaphore closed");
permit.merge(mem_permit);
}
let inner = self.connection_maker.create().await?;
CONCURRENT_CONNECTIONS_COUNT.increment(1.0);
CONNECTION_CREATE_TIME.record(before_create.elapsed());
Ok(TrackedConnection {
permit,
inner,
created_at: Instant::now(),
})
}
}
#[derive(Debug)]
pub struct TrackedConnection<DB> {
inner: DB,
#[allow(dead_code)] // just hold on to it
permit: tokio::sync::OwnedSemaphorePermit,
created_at: Instant,
}
impl<T> Drop for TrackedConnection<T> {
fn drop(&mut self) {
CONCURRENT_CONNECTIONS_COUNT.decrement(1.0);
CONNECTION_ALIVE_DURATION.record(self.created_at.elapsed());
}
}
impl<T> Deref for TrackedConnection<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
#[async_trait::async_trait]
impl<DB: Connection> Connection for TrackedConnection<DB> {
#[inline]
async fn execute_program<B: QueryResultBuilder>(
&self,
pgm: Program,
ctx: RequestContext,
builder: B,
replication_index: Option<FrameNo>,
) -> crate::Result<B> {
self.inner
.execute_program(pgm, ctx, builder, replication_index)
.await
}
#[inline]
async fn describe(
&self,
sql: String,
ctx: RequestContext,
replication_index: Option<FrameNo>,
) -> crate::Result<crate::Result<DescribeResponse>> {
self.inner.describe(sql, ctx, replication_index).await
}
#[inline]
async fn is_autocommit(&self) -> crate::Result<bool> {
self.inner.is_autocommit().await
}
#[inline]
async fn checkpoint(&self) -> Result<()> {
self.inner.checkpoint().await
}
#[inline]
async fn vacuum_if_needed(&self) -> Result<()> {
self.inner.vacuum_if_needed().await
}
#[inline]
fn diagnostics(&self) -> String {
self.inner.diagnostics()
}
}
#[cfg(test)]
pub mod test {
use super::*;
#[derive(Debug)]
struct DummyDb;
#[async_trait::async_trait]
impl Connection for DummyDb {
async fn execute_program<B: QueryResultBuilder>(
&self,
_pgm: Program,
_ctx: RequestContext,
_builder: B,
_replication_index: Option<FrameNo>,
) -> crate::Result<B> {
unreachable!()
}
async fn describe(
&self,
_sql: String,
_ctx: RequestContext,
_replication_index: Option<FrameNo>,
) -> crate::Result<crate::Result<DescribeResponse>> {
unreachable!()
}
async fn is_autocommit(&self) -> crate::Result<bool> {
unreachable!()
}
async fn checkpoint(&self) -> Result<()> {
unreachable!()
}
async fn vacuum_if_needed(&self) -> Result<()> {
unreachable!()
}
fn diagnostics(&self) -> String {
"dummy".into()
}
}
#[tokio::test]
async fn throttle_db_creation() {
let factory = (|| async { Ok(DummyDb) }).throttled(
Arc::new(Semaphore::new(10)),
Some(Duration::from_millis(100)),
u64::MAX,
u64::MAX,
);
let mut conns = Vec::with_capacity(10);
for _ in 0..10 {
conns.push(factory.create().await.unwrap())
}
assert!(factory.create().await.is_err());
drop(conns);
assert!(factory.create().await.is_ok());
}
}