MCPcopy Create free account
hub / github.com/clockworklabs/SpacetimeDB / try_init

Method try_init

crates/core/src/host/host_controller.rs:904–1133  ·  view source on GitHub ↗
(host_controller: &HostController, database: Database, replica_id: u64)

Source from the content-addressed store, hash-verified

902 /// create on-disk artifacts if the host / database did not exist.
903 #[tracing::instrument(level = "debug", skip_all)]
904 async fn try_init(host_controller: &HostController, database: Database, replica_id: u64) -> anyhow::Result<Self> {
905 let HostController {
906 data_dir,
907 default_config: config,
908 program_storage,
909 energy_monitor,
910 runtimes,
911 persistence,
912 page_pool,
913 bsatn_rlb_pool,
914 ..
915 } = host_controller;
916 let replica_dir = data_dir.replica(replica_id);
917 let runtime = spacetimedb_runtime::Handle::tokio_current();
918 let (tx_metrics_queue, tx_metrics_recorder_task) = spawn_tx_metrics_recorder(&runtime);
919
920 let (db, connected_clients) = match config.storage {
921 db::Storage::Memory => RelationalDB::open(
922 database.database_identity,
923 database.owner_identity,
924 EmptyHistory::new(),
925 None,
926 Some(tx_metrics_queue),
927 page_pool.clone(),
928 )?,
929 db::Storage::Disk => {
930 // Replay from the local state.
931 let history = relational_db::local_history(&replica_dir, &runtime).await?;
932 let persistence_db = db::persistence::Database {
933 id: database.id,
934 database_identity: database.database_identity,
935 owner_identity: database.owner_identity,
936 };
937 let persistence = persistence.persistence(&persistence_db, replica_id).await?;
938 // Loading a database from persistent storage involves heavy
939 // blocking I/O. `asyncify` to avoid blocking the async worker.
940 let (db, clients) = asyncify({
941 let database_identity = database.database_identity;
942 let owner_identity = database.owner_identity;
943 let page_pool = page_pool.clone();
944 move || {
945 RelationalDB::open(
946 database_identity,
947 owner_identity,
948 history,
949 Some(persistence),
950 Some(tx_metrics_queue),
951 page_pool,
952 )
953 }
954 })
955 .await
956 // Make sure we log the source chain of the error
957 // as a single line, with the help of `anyhow`.
958 .map_err(anyhow::Error::from)
959 .inspect_err(|e| {
960 tracing::error!(
961 database = %database.database_identity,

Callers 1

enable_loggingFunction · 0.45

Calls 15

local_historyFunction · 0.85
load_programFunction · 0.85
metric_reporterFunction · 0.85
spawn_view_cleanup_loopFunction · 0.85
replicaMethod · 0.80
persistenceMethod · 0.80
launch_moduleMethod · 0.80
unregister_fnMethod · 0.80
module_logsMethod · 0.80
with_auto_commitMethod · 0.80

Tested by 1

enable_loggingFunction · 0.36