forked from tursodatabase/libsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.rs
More file actions
534 lines (472 loc) · 17.5 KB
/
store.rs
File metadata and controls
534 lines (472 loc) · 17.5 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
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use async_lock::RwLock;
use chrono::NaiveDateTime;
use futures::TryFutureExt;
use moka::future::Cache;
use once_cell::sync::OnceCell;
use tokio::task::JoinSet;
use tokio::time::{Duration, Instant};
use tokio_stream::wrappers::BroadcastStream;
use crate::auth::Authenticated;
use crate::broadcaster::BroadcastMsg;
use crate::connection::config::DatabaseConfig;
use crate::database::DatabaseKind;
use crate::error::Error;
use crate::metrics::NAMESPACE_LOAD_LATENCY;
use crate::namespace::{NamespaceBottomlessDbId, NamespaceBottomlessDbIdInit, NamespaceName};
use crate::stats::Stats;
use super::broadcasters::{BroadcasterHandle, BroadcasterRegistry};
use super::configurator::{DynConfigurator, NamespaceConfigurators};
use super::meta_store::{MetaStore, MetaStoreHandle};
use super::schema_lock::SchemaLocksRegistry;
use super::{Namespace, ResetCb, ResetOp, ResolveNamespacePathFn, RestoreOption};
type NamespaceEntry = Arc<RwLock<Option<Namespace>>>;
/// Stores and manage a set of namespaces.
pub struct NamespaceStore {
pub inner: Arc<NamespaceStoreInner>,
}
impl Clone for NamespaceStore {
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
}
}
}
pub struct NamespaceStoreInner {
store: Cache<NamespaceName, NamespaceEntry>,
metadata: MetaStore,
allow_lazy_creation: bool,
has_shutdown: AtomicBool,
snapshot_at_shutdown: bool,
schema_locks: SchemaLocksRegistry,
broadcasters: BroadcasterRegistry,
configurators: NamespaceConfigurators,
db_kind: DatabaseKind,
}
impl NamespaceStore {
pub(crate) async fn new(
allow_lazy_creation: bool,
snapshot_at_shutdown: bool,
max_active_namespaces: usize,
metadata: MetaStore,
configurators: NamespaceConfigurators,
db_kind: DatabaseKind,
) -> crate::Result<Self> {
tracing::trace!("Max active namespaces: {max_active_namespaces}");
let store = Cache::<NamespaceName, NamespaceEntry>::builder()
.async_eviction_listener(move |name, ns, cause| {
tracing::debug!("evicting namespace `{name}` asynchronously: {cause:?}");
// TODO(sarna): not clear if we should snapshot-on-evict...
// On the one hand, better to do so, because we have no idea
// for how long we're evicting a namespace.
// On the other, if there's lots of cache pressure, snapshotting
// very often will kill the machine's I/O.
Box::pin(async move {
tracing::info!("namespace `{name}` deallocated");
// shutdown namespace
if let Some(ns) = ns.write().await.take() {
if let Err(e) = ns.shutdown(snapshot_at_shutdown).await {
tracing::error!("error deallocating `{name}`: {e}")
}
}
})
})
.max_capacity(max_active_namespaces as u64)
.time_to_idle(Duration::from_secs(86400))
.build();
Ok(Self {
inner: Arc::new(NamespaceStoreInner {
store,
metadata,
allow_lazy_creation,
has_shutdown: AtomicBool::new(false),
snapshot_at_shutdown,
schema_locks: Default::default(),
broadcasters: Default::default(),
configurators,
db_kind,
}),
})
}
pub async fn exists(&self, namespace: &NamespaceName) -> bool {
self.inner.metadata.exists(namespace).await
}
pub async fn destroy(&self, namespace: NamespaceName, prune_all: bool) -> crate::Result<()> {
if self.inner.has_shutdown.load(Ordering::Relaxed) {
return Err(Error::NamespaceStoreShutdown);
}
// destroy on-disk database and backups
let db_config = tokio::task::spawn_blocking({
let inner = self.inner.clone();
let namespace = namespace.clone();
move || {
inner
.metadata
.remove(namespace.clone())?
.ok_or_else(|| crate::Error::NamespaceDoesntExist(namespace.to_string()))
}
})
.await??;
let mut bottomless_db_id_init = NamespaceBottomlessDbIdInit::FetchFromConfig;
if let Some(ns) = self.inner.store.remove(&namespace).await {
// deallocate in-memory resources
if let Some(ns) = ns.write().await.take() {
bottomless_db_id_init = NamespaceBottomlessDbIdInit::Provided(
NamespaceBottomlessDbId::from_config(&ns.db_config_store.get()),
);
ns.destroy().await?;
}
}
self.cleanup(&namespace, &db_config, prune_all, bottomless_db_id_init)
.await?;
tracing::info!("destroyed namespace: {namespace}");
Ok(())
}
pub async fn checkpoint(&self, namespace: NamespaceName) -> crate::Result<()> {
let entry = self
.inner
.store
.get_with(namespace.clone(), async { Default::default() })
.await;
let lock = entry.read().await;
if let Some(ns) = &*lock {
ns.checkpoint().await?;
}
Ok(())
}
pub async fn reset(
&self,
namespace: NamespaceName,
restore_option: RestoreOption,
) -> anyhow::Result<()> {
// The process for reseting is as follow:
// - get a lock on the namespace entry, if the entry exists, then it's a lock on the entry,
// if it doesn't exist, insert an empty entry and take a lock on it
// - destroy the old namespace
// - create a new namespace and insert it in the held lock
let entry = self
.inner
.store
.get_with(namespace.clone(), async { Default::default() })
.await;
let mut lock = entry.write().await;
if let Some(ns) = lock.take() {
ns.destroy().await?;
}
let db_config = self.inner.metadata.handle(namespace.clone()).await;
// destroy on-disk database
self.cleanup(
&namespace,
&db_config.get(),
false,
NamespaceBottomlessDbIdInit::FetchFromConfig,
)
.await?;
let ns = self
.make_namespace(&namespace, db_config, restore_option)
.await?;
lock.replace(ns);
Ok(())
}
// This is only called on replica
fn make_reset_cb(&self) -> ResetCb {
let this = self.clone();
Box::new(move |op| {
let this = this.clone();
tokio::spawn(async move {
match op {
ResetOp::Reset(ns) => {
tracing::info!("received reset signal for: {ns}");
if let Err(e) = this.reset(ns.clone(), RestoreOption::Latest).await {
tracing::error!("error resetting namespace `{ns}`: {e}");
}
}
}
});
})
}
pub async fn fork(
&self,
from: NamespaceName,
to: NamespaceName,
to_config: DatabaseConfig,
timestamp: Option<NaiveDateTime>,
) -> crate::Result<()> {
if self.inner.has_shutdown.load(Ordering::Relaxed) {
return Err(Error::NamespaceStoreShutdown);
}
// check that the source namespace exists
if !self.inner.metadata.exists(&from).await {
return Err(crate::error::Error::NamespaceDoesntExist(from.to_string()));
}
let to_entry = self
.inner
.store
.get_with(to.clone(), async { Default::default() })
.await;
let mut to_lock = to_entry.write().await;
if to_lock.is_some() {
return Err(crate::error::Error::NamespaceAlreadyExist(to.to_string()));
}
// FIXME: we could potentially delete the namespace while trying to fork it
if !self.inner.metadata.exists(&from).await {
return Err(crate::Error::NamespaceDoesntExist(from.to_string()));
}
let from_config = self.inner.metadata.handle(from.clone()).await;
let from_entry = self
.load_namespace(&from, from_config.clone(), RestoreOption::Latest)
.await?;
let from_lock = from_entry.read().await;
let Some(from_ns) = &*from_lock else {
return Err(crate::error::Error::NamespaceDoesntExist(from.to_string()));
};
struct Bomb {
store: MetaStore,
ns: NamespaceName,
should_delete: bool,
}
impl Drop for Bomb {
fn drop(&mut self) {
if self.should_delete {
// we need to block in place because the inner connection may blocking, or
// unsing tokio's blocking methods (bottomless), which would cause a panic.
if let Err(e) =
tokio::task::block_in_place(|| self.store.remove(self.ns.clone()))
{
tracing::error!("failed to clean handle while forking: {e}");
}
}
}
}
let mut bomb = Bomb {
store: self.inner.metadata.clone(),
ns: to.clone(),
should_delete: true,
};
let handle = self.inner.metadata.handle(to.clone()).await;
handle
.store_and_maybe_flush(Some(to_config.into()), false)
.await?;
let to_ns = self
.get_configurator(&from_config.get())
.fork(
from_ns,
from_config,
to.clone(),
handle.clone(),
timestamp,
self.clone(),
)
.await?;
to_lock.replace(to_ns);
handle.flush().await?;
// defuse
bomb.should_delete = false;
Ok(())
}
pub async fn with_authenticated<Fun, R>(
&self,
namespace: NamespaceName,
auth: Authenticated,
f: Fun,
) -> crate::Result<R>
where
Fun: FnOnce(&Namespace) -> R + 'static,
{
if self.inner.has_shutdown.load(Ordering::Relaxed) {
return Err(Error::NamespaceStoreShutdown);
}
if !auth.is_namespace_authorized(&namespace) {
return Err(Error::NamespaceDoesntExist(namespace.to_string()));
}
self.with(namespace, f).await
}
pub async fn with<Fun, R>(&self, namespace: NamespaceName, f: Fun) -> crate::Result<R>
where
Fun: FnOnce(&Namespace) -> R,
{
if namespace != NamespaceName::default()
&& !self.inner.metadata.exists(&namespace).await
&& !self.inner.allow_lazy_creation
{
return Err(Error::NamespaceDoesntExist(namespace.to_string()));
}
let f = {
let name = namespace.clone();
move |ns: NamespaceEntry| async move {
let lock = ns.read().await;
match &*lock {
Some(ns) => Ok(f(ns)),
// the namespace was taken out of the entry
None => Err(Error::NamespaceDoesntExist(name.to_string())),
}
}
};
let handle = self.inner.metadata.handle(namespace.to_owned()).await;
f(self
.load_namespace(&namespace, handle, RestoreOption::Latest)
.await?)
.await
}
fn resolve_attach_fn(&self) -> ResolveNamespacePathFn {
static FN: OnceCell<ResolveNamespacePathFn> = OnceCell::new();
FN.get_or_init(|| {
Arc::new({
let store = self.clone();
move |ns: &NamespaceName| {
tokio::runtime::Handle::current()
.block_on(store.with(ns.clone(), |ns| ns.path.clone()))
}
})
})
.clone()
}
pub(crate) async fn make_namespace(
&self,
namespace: &NamespaceName,
config: MetaStoreHandle,
restore_option: RestoreOption,
) -> crate::Result<Namespace> {
let ns = self
.get_configurator(&config.get())
.setup(
config,
restore_option,
namespace,
self.make_reset_cb(),
self.resolve_attach_fn(),
self.clone(),
self.broadcaster(namespace.clone()),
)
.await?;
Ok(ns)
}
async fn load_namespace(
&self,
namespace: &NamespaceName,
db_config: MetaStoreHandle,
restore_option: RestoreOption,
) -> crate::Result<NamespaceEntry> {
let init = async {
let ns = self
.make_namespace(namespace, db_config, restore_option)
.await?;
Ok(Some(ns))
};
let before_load = Instant::now();
let ns = self
.inner
.store
.try_get_with(
namespace.clone(),
init.map_ok(|ns| Arc::new(RwLock::new(ns))),
)
.await?;
NAMESPACE_LOAD_LATENCY.record(before_load.elapsed());
Ok(ns)
}
#[tracing::instrument(skip_all, fields(namespace))]
pub async fn create(
&self,
namespace: NamespaceName,
restore_option: RestoreOption,
db_config: DatabaseConfig,
) -> crate::Result<()> {
if let Some(shared_schema_name) = &db_config.shared_schema_name {
// we hold a lock for the duration of the namespace creation
let _lock = self
.inner
.schema_locks
.acquire_shared(shared_schema_name.clone())
.await;
return self
.fork(shared_schema_name.clone(), namespace, db_config, None)
.await;
};
// With namespaces disabled, the default namespace can be auto-created,
// otherwise it's an error.
// FIXME: move the default namespace check out of this function.
if self.inner.allow_lazy_creation || namespace == NamespaceName::default() {
tracing::trace!("auto-creating the namespace");
} else if self.inner.metadata.exists(&namespace).await {
return Err(Error::NamespaceAlreadyExist(namespace.to_string()));
}
let db_config = Arc::new(db_config);
let handle = self.inner.metadata.handle(namespace.clone()).await;
tracing::debug!("storing db config");
handle.store(db_config).await?;
tracing::debug!("completed storing db config, loading namespace");
self.load_namespace(&namespace, handle, restore_option)
.await?;
tracing::debug!("completed loading namespace");
Ok(())
}
pub async fn shutdown(self) -> crate::Result<()> {
let mut set = JoinSet::new();
self.inner.has_shutdown.store(true, Ordering::Relaxed);
for (_name, entry) in self.inner.store.iter() {
let snapshow_at_shutdown = self.inner.snapshot_at_shutdown;
let mut lock = entry.write().await;
if let Some(ns) = lock.take() {
set.spawn(async move {
ns.shutdown(snapshow_at_shutdown).await?;
Ok::<_, anyhow::Error>(())
});
}
}
while let Some(_) = set.join_next().await.transpose()?.transpose()? {}
self.inner.metadata.shutdown().await?;
self.inner.store.invalidate_all();
self.inner.store.run_pending_tasks().await;
Ok(())
}
pub(crate) async fn stats(&self, namespace: NamespaceName) -> crate::Result<Arc<Stats>> {
self.with(namespace, |ns| ns.stats.clone()).await
}
pub(crate) fn broadcaster(&self, namespace: NamespaceName) -> BroadcasterHandle {
self.inner.broadcasters.handle(namespace)
}
pub(crate) fn subscribe(
&self,
namespace: NamespaceName,
table: String,
) -> BroadcastStream<BroadcastMsg> {
self.inner.broadcasters.subscribe(namespace, table)
}
pub(crate) fn unsubscribe(&self, namespace: NamespaceName, table: &String) {
self.inner.broadcasters.unsubscribe(namespace, table);
}
pub(crate) async fn config_store(
&self,
namespace: NamespaceName,
) -> crate::Result<MetaStoreHandle> {
self.with(namespace, |ns| ns.db_config_store.clone()).await
}
pub fn meta_store(&self) -> &MetaStore {
&self.inner.metadata
}
pub(crate) fn schema_locks(&self) -> &SchemaLocksRegistry {
&self.inner.schema_locks
}
fn get_configurator(&self, db_config: &DatabaseConfig) -> &DynConfigurator {
match self.inner.db_kind {
DatabaseKind::Primary if db_config.is_shared_schema => {
self.inner.configurators.configure_schema().unwrap()
}
DatabaseKind::Primary => self.inner.configurators.configure_primary().unwrap(),
DatabaseKind::Replica => self.inner.configurators.configure_replica().unwrap(),
}
}
async fn cleanup(
&self,
namespace: &NamespaceName,
db_config: &DatabaseConfig,
prune_all: bool,
bottomless_db_id_init: NamespaceBottomlessDbIdInit,
) -> crate::Result<()> {
self.get_configurator(db_config)
.cleanup(namespace, db_config, prune_all, bottomless_db_id_init)
.await
}
}