-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathhost_controller.rs
More file actions
1134 lines (1035 loc) · 42.4 KB
/
host_controller.rs
File metadata and controls
1134 lines (1035 loc) · 42.4 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 super::module_host::{EventStatus, ModuleHost, ModuleInfo, NoSuchModule};
use super::scheduler::SchedulerStarter;
use super::wasmtime::WasmtimeRuntime;
use super::{Scheduler, UpdateDatabaseResult};
use crate::database_logger::DatabaseLogger;
use crate::db::datastore::traits::Program;
use crate::db::db_metrics::data_size::DATA_SIZE_METRICS;
use crate::db::db_metrics::DB_METRICS;
use crate::db::relational_db::{self, DiskSizeFn, RelationalDB, Txdata};
use crate::db::{self};
use crate::energy::{EnergyMonitor, EnergyQuanta, NullEnergyMonitor};
use crate::messages::control_db::{Database, HostType};
use crate::module_host_context::ModuleCreationContext;
use crate::replica_context::ReplicaContext;
use crate::subscription::module_subscription_actor::ModuleSubscriptions;
use crate::subscription::module_subscription_manager::{spawn_send_worker, SubscriptionManager};
use crate::util::asyncify;
use crate::util::jobs::{JobCore, JobCores};
use crate::worker_metrics::WORKER_METRICS;
use anyhow::{anyhow, ensure, Context};
use async_trait::async_trait;
use durability::{Durability, EmptyHistory};
use log::{info, trace, warn};
use parking_lot::Mutex;
use spacetimedb_data_structures::map::IntMap;
use spacetimedb_durability::{self as durability, TxOffset};
use spacetimedb_lib::{hash_bytes, Identity};
use spacetimedb_paths::server::{ReplicaDir, ServerDataDir};
use spacetimedb_paths::FromPathUnchecked;
use spacetimedb_sats::hash::Hash;
use spacetimedb_schema::def::ModuleDef;
use spacetimedb_table::page_pool::PagePool;
use std::future::Future;
use std::ops::Deref;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tempfile::TempDir;
use tokio::sync::{watch, OwnedRwLockReadGuard, OwnedRwLockWriteGuard, RwLock as AsyncRwLock};
use tokio::task::AbortHandle;
// TODO:
//
// - [db::Config] should be per-[Database]
/// A shared mutable cell containing a module host and associated database.
type HostCell = Arc<AsyncRwLock<Option<Host>>>;
/// The registry of all running hosts.
type Hosts = Arc<Mutex<IntMap<u64, HostCell>>>;
pub type ExternalDurability = (Arc<dyn Durability<TxData = Txdata>>, DiskSizeFn);
pub type StartSnapshotWatcher = Box<dyn FnOnce(watch::Receiver<TxOffset>)>;
#[async_trait]
pub trait DurabilityProvider: Send + Sync + 'static {
async fn durability(&self, replica_id: u64) -> anyhow::Result<(ExternalDurability, Option<StartSnapshotWatcher>)>;
}
#[async_trait]
pub trait ExternalStorage: Send + Sync + 'static {
async fn lookup(&self, program_hash: Hash) -> anyhow::Result<Option<Box<[u8]>>>;
}
#[async_trait]
impl<F, Fut> ExternalStorage for F
where
F: Fn(Hash) -> Fut + Send + Sync + 'static,
Fut: Future<Output = anyhow::Result<Option<Box<[u8]>>>> + Send,
{
async fn lookup(&self, program_hash: Hash) -> anyhow::Result<Option<Box<[u8]>>> {
self(program_hash).await
}
}
pub type ProgramStorage = Arc<dyn ExternalStorage>;
/// A host controller manages the lifecycle of spacetime databases and their
/// associated modules.
///
/// This type is, and must remain, cheap to clone.
/// All of its fields should either be [`Copy`], enclosed in an [`Arc`],
/// or have some other fast [`Clone`] implementation.
#[derive(Clone)]
pub struct HostController {
/// Map of all hosts managed by this controller,
/// keyed by database instance id.
hosts: Hosts,
/// The root directory for database data.
pub data_dir: Arc<ServerDataDir>,
/// The default configuration to use for databases created by this
/// controller.
default_config: db::Config,
/// The [`ProgramStorage`] to query when instantiating a module.
program_storage: ProgramStorage,
/// The [`EnergyMonitor`] used by this controller.
energy_monitor: Arc<dyn EnergyMonitor>,
/// Provides implementations of [`Durability`] for each replica.
durability: Arc<dyn DurabilityProvider>,
/// The page pool all databases will use by cloning the ref counted pool.
pub page_pool: PagePool,
/// The runtimes for running our modules.
runtimes: Arc<HostRuntimes>,
/// The CPU cores that are reserved for ModuleHost operations to run on.
db_cores: JobCores,
}
struct HostRuntimes {
wasmtime: WasmtimeRuntime,
}
impl HostRuntimes {
fn new(data_dir: Option<&ServerDataDir>) -> Arc<Self> {
let wasmtime = WasmtimeRuntime::new(data_dir);
Arc::new(Self { wasmtime })
}
}
#[derive(Clone, Debug)]
pub struct ReducerCallResult {
pub outcome: ReducerOutcome,
pub energy_used: EnergyQuanta,
pub execution_duration: Duration,
}
impl ReducerCallResult {
pub fn is_err(&self) -> bool {
self.outcome.is_err()
}
pub fn is_ok(&self) -> bool {
!self.is_err()
}
}
impl From<ReducerCallResult> for Result<(), anyhow::Error> {
fn from(value: ReducerCallResult) -> Self {
value.outcome.into_result()
}
}
#[derive(Clone, Debug)]
pub enum ReducerOutcome {
Committed,
Failed(String),
BudgetExceeded,
}
impl ReducerOutcome {
pub fn into_result(self) -> anyhow::Result<()> {
match self {
Self::Committed => Ok(()),
Self::Failed(e) => Err(anyhow::anyhow!(e)),
Self::BudgetExceeded => Err(anyhow::anyhow!("reducer ran out of energy")),
}
}
pub fn is_err(&self) -> bool {
!matches!(self, Self::Committed)
}
}
impl From<&EventStatus> for ReducerOutcome {
fn from(status: &EventStatus) -> Self {
match &status {
EventStatus::Committed(_) => ReducerOutcome::Committed,
EventStatus::Failed(e) => ReducerOutcome::Failed(e.clone()),
EventStatus::OutOfEnergy => ReducerOutcome::BudgetExceeded,
}
}
}
impl HostController {
pub fn new(
data_dir: Arc<ServerDataDir>,
default_config: db::Config,
program_storage: ProgramStorage,
energy_monitor: Arc<impl EnergyMonitor>,
durability: Arc<dyn DurabilityProvider>,
db_cores: JobCores,
) -> Self {
Self {
hosts: <_>::default(),
default_config,
program_storage,
energy_monitor,
durability,
runtimes: HostRuntimes::new(Some(&data_dir)),
data_dir,
page_pool: PagePool::new(default_config.page_pool_max_size),
db_cores,
}
}
/// Replace the [`ProgramStorage`] used by this controller.
pub fn set_program_storage(&mut self, ps: ProgramStorage) {
self.program_storage = ps;
}
/// Get a [`ModuleHost`] managed by this controller, or launch it from
/// persistent state.
///
/// If the host is not running, it is started according to the default
/// [`db::Config`] set for this controller.
/// The underlying database is restored from existing data at its
/// canonical filesystem location _iff_ the default config mandates disk
/// storage.
///
/// The module will be instantiated from the program bytes stored in an
/// existing database.
/// If the database is empty, the `program_bytes_address` of the given
/// [`Database`] will be used to load the program from the controller's
/// [`ProgramStorage`]. The initialization procedure (schema creation,
/// `__init__` reducer) will be invoked on the found module, and the
/// database will be marked as initialized.
///
/// See also: [`Self::get_module_host`]
#[tracing::instrument(level = "trace", skip_all)]
pub async fn get_or_launch_module_host(&self, database: Database, replica_id: u64) -> anyhow::Result<ModuleHost> {
let mut rx = self.watch_maybe_launch_module_host(database, replica_id).await?;
let module = rx.borrow_and_update();
Ok(module.clone())
}
/// Like [`Self::get_or_launch_module_host`], use a [`ModuleHost`] managed
/// by this controller, or launch it if it is not running.
///
/// Instead of a [`ModuleHost`], this returns a [`watch::Receiver`] which
/// gets notified each time the module is updated.
///
/// See also: [`Self::watch_module_host`]
#[tracing::instrument(level = "trace", skip_all)]
pub async fn watch_maybe_launch_module_host(
&self,
database: Database,
replica_id: u64,
) -> anyhow::Result<watch::Receiver<ModuleHost>> {
// Try a read lock first.
{
let guard = self.acquire_read_lock(replica_id).await;
if let Some(host) = &*guard {
trace!("cached host {}/{}", database.database_identity, replica_id);
return Ok(host.module.subscribe());
}
}
// We didn't find a running module, so take a write lock.
// Since [`tokio::sync::RwLock`] doesn't support upgrading of read locks,
// we'll need to check again if a module was added meanwhile.
let mut guard = self.acquire_write_lock(replica_id).await;
if let Some(host) = &*guard {
trace!(
"cached host {}/{} (lock upgrade)",
database.database_identity,
replica_id
);
return Ok(host.module.subscribe());
}
trace!("launch host {}/{}", database.database_identity, replica_id);
// `HostController::clone` is fast,
// as all of its fields are either `Copy` or wrapped in `Arc`.
let this = self.clone();
// `try_init_host` is not cancel safe, as it will spawn other async tasks
// which hold a filesystem lock past when `try_init_host` returns or is cancelled.
// This means that, if `try_init_host` is cancelled, subsequent calls will fail.
//
// This is problematic because Axum will cancel its handler tasks if the client disconnects,
// and this method is called from Axum handlers, e.g. for the subscribe route.
// `tokio::spawn` a task to build the `Host` and install it in the `guard`,
// so that it will run to completion even if the caller goes away.
//
// Note that `tokio::spawn` only cancels its tasks when the runtime shuts down,
// at which point we won't be calling `try_init_host` again anyways.
let rx = tokio::spawn(async move {
let host = this.try_init_host(database, replica_id).await?;
let rx = host.module.subscribe();
*guard = Some(host);
Ok::<_, anyhow::Error>(rx)
})
.await??;
Ok(rx)
}
/// Construct an in-memory instance of `database` running `program`,
/// initialize it, then immediately destroy it.
///
/// This is used during an initial, fresh publish operation
/// in order to check the `program`'s validity as a module,
/// since some validity checks we'd like to do (e.g. typechecking RLS filters)
/// require a fully instantiated database.
///
/// This is not necessary during hotswap publishes,
/// as the automigration planner and executor accomplish the same validity checks.
pub async fn check_module_validity(&self, database: Database, program: Program) -> anyhow::Result<Arc<ModuleInfo>> {
Host::try_init_in_memory_to_check(
&self.runtimes,
self.page_pool.clone(),
database,
program,
// This takes a db core to check validity, and we will later take
// another core to actually run the module. Due to the round-robin
// algorithm that JobCores uses, that will likely just be the same
// core - there's not a concern that we'll only end up using 1/2
// of the actual cores.
self.db_cores.take(),
)
.await
}
/// Run a computation on the [`RelationalDB`] of a [`ModuleHost`] managed by
/// this controller, launching the host if necessary.
///
/// If the computation `F` panics, the host is removed from this controller,
/// releasing its resources.
#[tracing::instrument(level = "trace", skip_all)]
pub async fn using_database<F, T>(&self, database: Database, replica_id: u64, f: F) -> anyhow::Result<T>
where
F: FnOnce(&RelationalDB) -> T + Send + 'static,
T: Send + 'static,
{
trace!("using database {}/{}", database.database_identity, replica_id);
let module = self.get_or_launch_module_host(database, replica_id).await?;
let on_panic = self.unregister_fn(replica_id);
scopeguard::defer_on_unwind!({
warn!("database operation panicked");
on_panic();
});
let result = asyncify(move || f(&module.replica_ctx().relational_db)).await;
Ok(result)
}
/// Update the [`ModuleHost`] identified by `replica_id` to the given
/// program.
///
/// The host may not be running, in which case it is spawned (see
/// [`Self::get_or_launch_module_host`] for details on what this entails).
///
/// If the host was running, and the update fails, the previous version of
/// the host keeps running.
#[tracing::instrument(level = "trace", skip_all, err)]
pub async fn update_module_host(
&self,
database: Database,
host_type: HostType,
replica_id: u64,
program_bytes: Box<[u8]>,
) -> anyhow::Result<UpdateDatabaseResult> {
let program = Program {
hash: hash_bytes(&program_bytes),
bytes: program_bytes,
};
trace!(
"update module host {}/{}: genesis={} update-to={}",
database.database_identity,
replica_id,
database.initial_program,
program.hash
);
let mut guard = self.acquire_write_lock(replica_id).await;
// `HostController::clone` is fast,
// as all of its fields are either `Copy` or wrapped in `Arc`.
let this = self.clone();
// `try_init_host` is not cancel safe, as it will spawn other async tasks
// which hold a filesystem lock past when `try_init_host` returns or is cancelled.
// This means that, if `try_init_host` is cancelled, subsequent calls will fail.
//
// The rest of this future is also not cancel safe, as it will `Option::take` out of the guard
// at the start of the block and then store back into it at the end.
//
// This is problematic because Axum will cancel its handler tasks if the client disconnects,
// and this method is called from Axum handlers, e.g. for the publish route.
// `tokio::spawn` a task to update the contents of `guard`,
// so that it will run to completion even if the caller goes away.
//
// Note that `tokio::spawn` only cancels its tasks when the runtime shuts down,
// at which point we won't be calling `try_init_host` again anyways.
let update_result = tokio::spawn(async move {
let mut host = match guard.take() {
None => {
trace!("host not running, try_init");
this.try_init_host(database, replica_id).await?
}
Some(host) => {
trace!("host found, updating");
host
}
};
let update_result = host
.update_module(
this.runtimes.clone(),
host_type,
program,
this.energy_monitor.clone(),
this.unregister_fn(replica_id),
this.db_cores.take(),
)
.await?;
*guard = Some(host);
Ok::<_, anyhow::Error>(update_result)
})
.await??;
Ok(update_result)
}
/// Start the host `replica_id` and conditionally update it.
///
/// If the host was not initialized before, it is initialized with the
/// program [`Database::initial_program`], which is loaded from the
/// controller's [`ProgramStorage`].
///
/// If it was already initialized and its stored program hash matches
/// [`Database::initial_program`], no further action is taken.
///
/// Otherwise, if `expected_hash` is `Some` and does **not** match the
/// stored hash, an error is returned.
///
/// Otherwise, the host is updated to [`Database::initial_program`], loading
/// the program data from the controller's [`ProgramStorage`].
///
/// > Note that this ascribes different semantics to [`Database::initial_program`]
/// > than elsewhere, where the [`Database`] value is provided by the control
/// > database. The method is mainly useful for bootstrapping the control
/// > database itself.
pub async fn init_maybe_update_module_host(
&self,
database: Database,
replica_id: u64,
expected_hash: Option<Hash>,
) -> anyhow::Result<watch::Receiver<ModuleHost>> {
trace!("custom bootstrap {}/{}", database.database_identity, replica_id);
let db_addr = database.database_identity;
let host_type = database.host_type;
let program_hash = database.initial_program;
let mut guard = self.acquire_write_lock(replica_id).await;
// `HostController::clone` is fast,
// as all of its fields are either `Copy` or wrapped in `Arc`.
let this = self.clone();
// `try_init_host` is not cancel safe, as it will spawn other async tasks
// which hold a filesystem lock past when `try_init_host` returns or is cancelled.
// This means that, if `try_init_host` is cancelled, subsequent calls will fail.
//
// The rest of this future is also not cancel safe, as it will `Option::take` out of the guard
// at the start of the block and then store back into it at the end.
//
// This is problematic because Axum will cancel its handler tasks if the client disconnects,
// and this method is called from Axum handlers, e.g. for the publish route.
// `tokio::spawn` a task to update the contents of `guard`,
// so that it will run to completion even if the caller goes away.
//
// Note that `tokio::spawn` only cancels its tasks when the runtime shuts down,
// at which point we won't be calling `try_init_host` again anyways.
let module = tokio::spawn(async move {
let mut host = match guard.take() {
Some(host) => host,
None => this.try_init_host(database, replica_id).await?,
};
let module = host.module.subscribe();
// The program is now either:
//
// - the desired one from [Database], in which case we do nothing
// - `Some` expected hash, in which case we update to the desired one
// - `None` expected hash, in which case we also update
let stored_hash = stored_program_hash(host.db())?
.with_context(|| format!("[{}] database improperly initialized", db_addr))?;
if stored_hash == program_hash {
info!("[{}] database up-to-date with {}", db_addr, program_hash);
*guard = Some(host);
} else {
if let Some(expected_hash) = expected_hash {
ensure!(
expected_hash == stored_hash,
"[{}] expected program {} found {}",
db_addr,
expected_hash,
stored_hash
);
}
info!(
"[{}] updating database from `{}` to `{}`",
db_addr, stored_hash, program_hash
);
let program = load_program(&this.program_storage, program_hash).await?;
let update_result = host
.update_module(
this.runtimes.clone(),
host_type,
program,
this.energy_monitor.clone(),
this.unregister_fn(replica_id),
this.db_cores.take(),
)
.await?;
match update_result {
UpdateDatabaseResult::NoUpdateNeeded | UpdateDatabaseResult::UpdatePerformed => {
*guard = Some(host);
}
UpdateDatabaseResult::AutoMigrateError(e) => {
return Err(anyhow::anyhow!(e));
}
UpdateDatabaseResult::ErrorExecutingMigration(e) => {
return Err(e);
}
}
}
Ok::<_, anyhow::Error>(module)
})
.await??;
Ok(module)
}
/// Release all resources of the [`ModuleHost`] identified by `replica_id`,
/// and deregister it from the controller.
#[tracing::instrument(level = "trace", skip_all)]
pub async fn exit_module_host(&self, replica_id: u64) -> Result<(), anyhow::Error> {
trace!("exit module host {}", replica_id);
let lock = self.hosts.lock().remove(&replica_id);
if let Some(lock) = lock {
if let Some(host) = lock.write_owned().await.take() {
let module = host.module.borrow().clone();
module.exit().await;
let table_names = module.info().module_def.tables().map(|t| t.name.deref());
remove_database_gauges(&module.info().database_identity, table_names);
}
}
Ok(())
}
/// Get the [`ModuleHost`] identified by `replica_id` or return an error
/// if it is not registered with the controller.
///
/// See [`Self::get_or_launch_module_host`] for a variant which launches
/// the host if it is not running.
#[tracing::instrument(level = "trace", skip_all)]
pub async fn get_module_host(&self, replica_id: u64) -> Result<ModuleHost, NoSuchModule> {
trace!("get module host {}", replica_id);
let guard = self.acquire_read_lock(replica_id).await;
guard
.as_ref()
.map(|Host { module, .. }| module.borrow().clone())
.ok_or(NoSuchModule)
}
/// Subscribe to updates of the [`ModuleHost`] identified by `replica_id`,
/// or return an error if it is not registered with the controller.
///
/// See [`Self::watch_maybe_launch_module_host`] for a variant which
/// launches the host if it is not running.
#[tracing::instrument(level = "trace", skip_all)]
pub async fn watch_module_host(&self, replica_id: u64) -> Result<watch::Receiver<ModuleHost>, NoSuchModule> {
trace!("watch module host {}", replica_id);
let guard = self.acquire_read_lock(replica_id).await;
guard
.as_ref()
.map(|Host { module, .. }| module.subscribe())
.ok_or(NoSuchModule)
}
/// `true` if the module host `replica_id` is currently registered with
/// the controller.
pub async fn has_module_host(&self, replica_id: u64) -> bool {
self.acquire_read_lock(replica_id).await.is_some()
}
/// On-panic callback passed to [`ModuleHost`]s created by this controller.
///
/// Removes the module with the given `replica_id` from this controller.
fn unregister_fn(&self, replica_id: u64) -> impl Fn() + Send + Sync + 'static {
let hosts = Arc::downgrade(&self.hosts);
move || {
if let Some(hosts) = hosts.upgrade() {
hosts.lock().remove(&replica_id);
}
}
}
async fn acquire_write_lock(&self, replica_id: u64) -> OwnedRwLockWriteGuard<Option<Host>> {
let lock = self.hosts.lock().entry(replica_id).or_default().clone();
lock.write_owned().await
}
async fn acquire_read_lock(&self, replica_id: u64) -> OwnedRwLockReadGuard<Option<Host>> {
let lock = self.hosts.lock().entry(replica_id).or_default().clone();
lock.read_owned().await
}
async fn try_init_host(&self, database: Database, replica_id: u64) -> anyhow::Result<Host> {
Host::try_init(self, database, replica_id).await
}
}
fn stored_program_hash(db: &RelationalDB) -> anyhow::Result<Option<Hash>> {
let meta = db.metadata()?;
Ok(meta.map(|meta| meta.program_hash))
}
async fn make_replica_ctx(
path: ReplicaDir,
database: Database,
replica_id: u64,
relational_db: Arc<RelationalDB>,
) -> anyhow::Result<ReplicaContext> {
let logger = tokio::task::block_in_place(move || Arc::new(DatabaseLogger::open_today(path.module_logs())));
let send_worker_queue = spawn_send_worker(Some(database.database_identity));
let subscriptions = Arc::new(parking_lot::RwLock::new(SubscriptionManager::new(
send_worker_queue.clone(),
)));
let downgraded = Arc::downgrade(&subscriptions);
let subscriptions = ModuleSubscriptions::new(
relational_db.clone(),
subscriptions,
send_worker_queue,
database.owner_identity,
);
// If an error occurs when evaluating a subscription,
// we mark each client that was affected,
// and we remove those clients from the manager async.
tokio::spawn(async move {
loop {
tokio::time::sleep(Duration::from_secs(10)).await;
let Some(subscriptions) = downgraded.upgrade() else {
break;
};
asyncify(move || subscriptions.write().remove_dropped_clients()).await
}
});
Ok(ReplicaContext {
database,
replica_id,
logger,
subscriptions,
relational_db,
})
}
/// Initialize a module host for the given program.
/// The passed replica_ctx may not be configured for this version of the program's database schema yet.
#[allow(clippy::too_many_arguments)]
async fn make_module_host(
runtimes: Arc<HostRuntimes>,
host_type: HostType,
replica_ctx: Arc<ReplicaContext>,
scheduler: Scheduler,
program: Program,
energy_monitor: Arc<dyn EnergyMonitor>,
unregister: impl Fn() + Send + Sync + 'static,
core: JobCore,
) -> anyhow::Result<(Program, ModuleHost)> {
// `make_actor` is blocking, as it needs to compile the wasm to native code,
// which may be computationally expensive - sometimes up to 1s for a large module.
// TODO: change back to using `spawn_rayon` here - asyncify runs on tokio blocking
// threads, but those aren't for computation. Also, wasmtime uses rayon
// to run compilation in parallel, so it'll need to run stuff in rayon anyway.
asyncify(move || {
let module_host = match host_type {
HostType::Wasm => {
let mcc = ModuleCreationContext {
replica_ctx,
scheduler,
program: &program,
energy_monitor,
};
let start = Instant::now();
let actor = runtimes.wasmtime.make_actor(mcc)?;
trace!("wasmtime::make_actor blocked for {:?}", start.elapsed());
ModuleHost::new(actor, unregister, core)
}
};
Ok((program, module_host))
})
.await
}
async fn load_program(storage: &ProgramStorage, hash: Hash) -> anyhow::Result<Program> {
let bytes = storage
.lookup(hash)
.await?
.with_context(|| format!("program {} not found", hash))?;
Ok(Program { hash, bytes })
}
struct LaunchedModule {
replica_ctx: Arc<ReplicaContext>,
module_host: ModuleHost,
scheduler: Scheduler,
scheduler_starter: SchedulerStarter,
}
#[allow(clippy::too_many_arguments)]
async fn launch_module(
database: Database,
replica_id: u64,
program: Program,
on_panic: impl Fn() + Send + Sync + 'static,
relational_db: Arc<RelationalDB>,
energy_monitor: Arc<dyn EnergyMonitor>,
replica_dir: ReplicaDir,
runtimes: Arc<HostRuntimes>,
core: JobCore,
) -> anyhow::Result<(Program, LaunchedModule)> {
let db_identity = database.database_identity;
let host_type = database.host_type;
let replica_ctx = make_replica_ctx(replica_dir, database, replica_id, relational_db)
.await
.map(Arc::new)?;
let (scheduler, scheduler_starter) = Scheduler::open(replica_ctx.relational_db.clone());
let (program, module_host) = make_module_host(
runtimes.clone(),
host_type,
replica_ctx.clone(),
scheduler.clone(),
program,
energy_monitor.clone(),
on_panic,
core,
)
.await?;
trace!("launched database {} with program {}", db_identity, program.hash);
Ok((
program,
LaunchedModule {
replica_ctx,
module_host,
scheduler,
scheduler_starter,
},
))
}
/// Update a module.
///
/// If the `db` is not initialized yet (i.e. its program hash is `None`),
/// return an error.
///
/// Otherwise, if `db.program_hash` matches the given `program_hash`, do
/// nothing and return an empty `UpdateDatabaseResult`.
///
/// Otherwise, invoke `module.update_database` and return the result.
async fn update_module(
db: &RelationalDB,
module: &ModuleHost,
program: Program,
old_module_info: Arc<ModuleInfo>,
) -> anyhow::Result<UpdateDatabaseResult> {
let addr = db.database_identity();
match stored_program_hash(db)? {
None => Err(anyhow!("database `{}` not yet initialized", addr)),
Some(stored) => {
let res = if stored == program.hash {
info!("database `{}` up to date with program `{}`", addr, program.hash);
UpdateDatabaseResult::NoUpdateNeeded
} else {
info!("updating `{}` from {} to {}", addr, stored, program.hash);
module.update_database(program, old_module_info).await?
};
Ok(res)
}
}
}
/// Encapsulates a database, associated module, and auxiliary state.
struct Host {
/// The [`ModuleHost`], providing the callable reducer API.
///
/// Modules may be updated via [`Host::update_module`].
/// The module is wrapped in a [`watch::Sender`] to allow for "hot swapping":
/// clients may subscribe to the channel, so they get the most recent
/// [`ModuleHost`] version or an error if the [`Host`] was dropped.
module: watch::Sender<ModuleHost>,
/// Pointer to the `module`'s [`ReplicaContext`].
///
/// The database stays the same if and when the module is updated via
/// [`Host::update_module`].
replica_ctx: Arc<ReplicaContext>,
/// Scheduler for repeating reducers, operating on the current `module`.
scheduler: Scheduler,
/// Handle to the metrics collection task started via [`disk_monitor`].
///
/// The task collects metrics from the `replica_ctx`, and so stays alive as long
/// as the `replica_ctx` is live. The task is aborted when [`Host`] is dropped.
metrics_task: AbortHandle,
}
impl Host {
/// Attempt to instantiate a [`Host`] from persistent storage.
///
/// Note that this does **not** run module initialization routines, but may
/// create on-disk artifacts if the host / database did not exist.
#[tracing::instrument(level = "debug", skip_all)]
async fn try_init(host_controller: &HostController, database: Database, replica_id: u64) -> anyhow::Result<Self> {
let HostController {
data_dir,
default_config: config,
program_storage,
energy_monitor,
runtimes,
durability,
page_pool,
..
} = host_controller;
let on_panic = host_controller.unregister_fn(replica_id);
let replica_dir = data_dir.replica(replica_id);
let (db, connected_clients) = match config.storage {
db::Storage::Memory => RelationalDB::open(
&replica_dir,
database.database_identity,
database.owner_identity,
EmptyHistory::new(),
None,
None,
page_pool.clone(),
)?,
db::Storage::Disk => {
let snapshot_repo =
relational_db::open_snapshot_repo(replica_dir.snapshots(), database.database_identity, replica_id)?;
let (history, _) = relational_db::local_durability(replica_dir.commit_log()).await?;
let (durability, start_snapshot_watcher) = durability.durability(replica_id).await?;
let (db, clients) = RelationalDB::open(
&replica_dir,
database.database_identity,
database.owner_identity,
history,
Some(durability),
Some(snapshot_repo),
page_pool.clone(),
)
// Make sure we log the source chain of the error
// as a single line, with the help of `anyhow`.
.map_err(anyhow::Error::from)
.inspect_err(|e| {
tracing::error!(
database = %database.database_identity,
replica = replica_id,
"Failed to open database: {e:#}"
);
})?;
if let Some(start_snapshot_watcher) = start_snapshot_watcher {
let watcher = db.subscribe_to_snapshots().expect("we passed snapshot_repo");
start_snapshot_watcher(watcher)
}
(db, clients)
}
};
let (program, program_needs_init) = match db.program()? {
// Launch module with program from existing database.
Some(program) => (program, false),
// Database is empty, load program from external storage and run
// initialization.
None => (load_program(program_storage, database.initial_program).await?, true),
};
let (program, launched) = launch_module(
database,
replica_id,
program,
on_panic,
Arc::new(db),
energy_monitor.clone(),
replica_dir,
runtimes.clone(),
host_controller.db_cores.take(),
)
.await?;
if program_needs_init {
let call_result = launched.module_host.init_database(program).await?;
if let Some(call_result) = call_result {
Result::from(call_result)?;
}
} else {
drop(program)
}
let LaunchedModule {
replica_ctx,
module_host,
scheduler,
scheduler_starter,
} = launched;
// Disconnect dangling clients.
for (identity, connection_id) in connected_clients {
module_host
.call_identity_disconnected(identity, connection_id)
.await
.with_context(|| {
format!(
"Error calling disconnect for {} {} on {}",
identity, connection_id, replica_ctx.database_identity
)
})?;
}
scheduler_starter.start(&module_host)?;
let metrics_task = tokio::spawn(metric_reporter(replica_ctx.clone())).abort_handle();
Ok(Host {
module: watch::Sender::new(module_host),
replica_ctx,
scheduler,
metrics_task,
})
}
/// Construct an in-memory instance of `database` running `program`,
/// initialize it, then immediately destroy it.
///
/// This is used during an initial, fresh publish operation
/// in order to check the `program`'s validity as a module,
/// since some validity checks we'd like to do (e.g. typechecking RLS filters)
/// require a fully instantiated database.
///
/// This is not necessary during hotswap publishes,
/// as the automigration planner and executor accomplish the same validity checks.
async fn try_init_in_memory_to_check(
runtimes: &Arc<HostRuntimes>,
page_pool: PagePool,
database: Database,
program: Program,
core: JobCore,
) -> anyhow::Result<Arc<ModuleInfo>> {
// Even in-memory databases acquire a lockfile.
// Grab a tempdir to put that lockfile in.
let phony_replica_dir = TempDir::with_prefix("spacetimedb-publish-in-memory-check")
.context("Error creating temporary directory to house temporary database during publish")?;
// Leave the `TempDir` instance in place, so that its destructor will still run.
let phony_replica_dir = ReplicaDir::from_path_unchecked(phony_replica_dir.path().to_owned());
let (db, _connected_clients) = RelationalDB::open(
&phony_replica_dir,
database.database_identity,
database.owner_identity,
EmptyHistory::new(),
None,
None,
page_pool,
)?;
let (program, launched) = launch_module(
database,
0,
program,
// No need to register a callback here:
// proper publishes use it to unregister a panicked module,
// but this module is not registered in the first place.
|| log::error!("launch_module on_panic called for temporary publish in-memory instance"),
Arc::new(db),
Arc::new(NullEnergyMonitor),
phony_replica_dir,
runtimes.clone(),
core,
)
.await?;
let call_result = launched.module_host.init_database(program).await?;
if let Some(call_result) = call_result {
Result::from(call_result)?;
}
Ok(launched.module_host.info)
}
/// Attempt to replace this [`Host`]'s [`ModuleHost`] with a new one running
/// the program `program_hash`.
///
/// The associated [`ReplicaContext`] stays the same.
///
/// Executes [`ModuleHost::update_database`] on the newly instantiated
/// module, updating the database schema and invoking the `__update__`
/// reducer if it is defined.
/// If this succeeds, the current module is replaced with the new one,
/// otherwise it stays the same.
///
/// Either way, the [`UpdateDatabaseResult`] is returned.