fix tests

This commit is contained in:
John Smith
2023-04-23 14:10:17 -04:00
parent cb4abaefd7
commit 75c16b8c51
16 changed files with 160 additions and 115 deletions

View File

@@ -1,7 +1,8 @@
use crate::crypto::Crypto;
use crate::network_manager::*;
use crate::routing_table::*;
use crate::*;
use crypto::Crypto;
use network_manager::*;
use routing_table::*;
use storage_manager::*;
pub struct AttachmentManagerInner {
last_attachment_state: AttachmentState,
@@ -26,6 +27,7 @@ pub struct AttachmentManager {
impl AttachmentManager {
fn new_unlocked_inner(
config: VeilidConfig,
storage_manager: StorageManager,
protected_store: ProtectedStore,
table_store: TableStore,
block_store: BlockStore,
@@ -35,6 +37,7 @@ impl AttachmentManager {
config: config.clone(),
network_manager: NetworkManager::new(
config,
storage_manager,
protected_store,
table_store,
block_store,
@@ -54,6 +57,7 @@ impl AttachmentManager {
}
pub fn new(
config: VeilidConfig,
storage_manager: StorageManager,
protected_store: ProtectedStore,
table_store: TableStore,
block_store: BlockStore,
@@ -63,6 +67,7 @@ impl AttachmentManager {
inner: Arc::new(Mutex::new(Self::new_inner())),
unlocked_inner: Arc::new(Self::new_unlocked_inner(
config,
storage_manager,
protected_store,
table_store,
block_store,

View File

@@ -106,11 +106,27 @@ impl ServicesContext {
}
self.block_store = Some(block_store.clone());
// Set up storage manager
trace!("init storage manager");
let storage_manager = StorageManager::new(
self.config.clone(),
self.crypto.clone().unwrap(),
self.protected_store.clone().unwrap(),
self.table_store.clone().unwrap(),
self.block_store.clone().unwrap(),
);
if let Err(e) = storage_manager.init().await {
self.shutdown().await;
return Err(e);
}
self.storage_manager = Some(storage_manager.clone());
// Set up attachment manager
trace!("init attachment manager");
let update_callback = self.update_callback.clone();
let attachment_manager = AttachmentManager::new(
self.config.clone(),
storage_manager,
protected_store,
table_store,
block_store,
@@ -122,26 +138,6 @@ impl ServicesContext {
}
self.attachment_manager = Some(attachment_manager);
// Set up storage manager
trace!("init storage manager");
let storage_manager = StorageManager::new(
self.config.clone(),
self.crypto.clone().unwrap(),
self.protected_store.clone().unwrap(),
self.table_store.clone().unwrap(),
self.block_store.clone().unwrap(),
self.attachment_manager
.clone()
.unwrap()
.network_manager()
.rpc_processor(),
);
if let Err(e) = storage_manager.init().await {
self.shutdown().await;
return Err(e);
}
self.storage_manager = Some(storage_manager.clone());
info!("Veilid API startup complete");
Ok(())
}
@@ -150,14 +146,14 @@ impl ServicesContext {
pub async fn shutdown(&mut self) {
info!("Veilid API shutting down");
if let Some(storage_manager) = &mut self.storage_manager {
trace!("terminate storage manager");
storage_manager.terminate().await;
}
if let Some(attachment_manager) = &mut self.attachment_manager {
trace!("terminate attachment manager");
attachment_manager.terminate().await;
}
if let Some(storage_manager) = &mut self.storage_manager {
trace!("terminate storage manager");
storage_manager.terminate().await;
}
if let Some(block_store) = &mut self.block_store {
trace!("terminate block store");
block_store.terminate().await;
@@ -191,12 +187,12 @@ pub struct VeilidCoreContext {
pub config: VeilidConfig,
pub update_callback: UpdateCallback,
// Services
pub storage_manager: StorageManager,
pub protected_store: ProtectedStore,
pub table_store: TableStore,
pub block_store: BlockStore,
pub crypto: Crypto,
pub attachment_manager: AttachmentManager,
pub storage_manager: StorageManager,
}
impl VeilidCoreContext {
@@ -242,14 +238,14 @@ impl VeilidCoreContext {
sc.startup().await.map_err(VeilidAPIError::generic)?;
Ok(VeilidCoreContext {
update_callback: sc.update_callback,
config: sc.config,
update_callback: sc.update_callback,
storage_manager: sc.storage_manager.unwrap(),
protected_store: sc.protected_store.unwrap(),
table_store: sc.table_store.unwrap(),
block_store: sc.block_store.unwrap(),
crypto: sc.crypto.unwrap(),
attachment_manager: sc.attachment_manager.unwrap(),
storage_manager: sc.storage_manager.unwrap(),
})
}

View File

@@ -33,6 +33,7 @@ use native::*;
use receipt_manager::*;
use routing_table::*;
use rpc_processor::*;
use storage_manager::*;
#[cfg(target_arch = "wasm32")]
use wasm::*;
@@ -146,6 +147,7 @@ struct NetworkManagerInner {
struct NetworkManagerUnlockedInner {
// Handles
config: VeilidConfig,
storage_manager: StorageManager,
protected_store: ProtectedStore,
table_store: TableStore,
block_store: BlockStore,
@@ -176,6 +178,7 @@ impl NetworkManager {
}
fn new_unlocked_inner(
config: VeilidConfig,
storage_manager: StorageManager,
protected_store: ProtectedStore,
table_store: TableStore,
block_store: BlockStore,
@@ -183,6 +186,7 @@ impl NetworkManager {
) -> NetworkManagerUnlockedInner {
NetworkManagerUnlockedInner {
config,
storage_manager,
protected_store,
table_store,
block_store,
@@ -197,6 +201,7 @@ impl NetworkManager {
pub fn new(
config: VeilidConfig,
storage_manager: StorageManager,
protected_store: ProtectedStore,
table_store: TableStore,
block_store: BlockStore,
@@ -206,6 +211,7 @@ impl NetworkManager {
inner: Arc::new(Mutex::new(Self::new_inner())),
unlocked_inner: Arc::new(Self::new_unlocked_inner(
config,
storage_manager,
protected_store,
table_store,
block_store,
@@ -226,6 +232,9 @@ impl NetworkManager {
{
f(&*self.unlocked_inner.config.get())
}
pub fn storage_manager(&self) -> StorageManager {
self.unlocked_inner.storage_manager.clone()
}
pub fn protected_store(&self) -> ProtectedStore {
self.unlocked_inner.protected_store.clone()
}

View File

@@ -34,7 +34,7 @@ use network_manager::*;
use receipt_manager::*;
use routing_table::*;
use stop_token::future::FutureExt;
use storage_manager::StorageManager;
use storage_manager::*;
/////////////////////////////////////////////////////////////////////
@@ -227,7 +227,6 @@ pub struct RPCProcessorInner {
send_channel: Option<flume::Sender<(Option<Id>, RPCMessageEncoded)>>,
stop_source: Option<StopSource>,
worker_join_handles: Vec<MustJoinHandle<()>>,
opt_storage_manager: Option<StorageManager>,
}
pub struct RPCProcessorUnlockedInner {
@@ -246,6 +245,7 @@ pub struct RPCProcessor {
crypto: Crypto,
config: VeilidConfig,
network_manager: NetworkManager,
storage_manager: StorageManager,
routing_table: RoutingTable,
inner: Arc<Mutex<RPCProcessorInner>>,
unlocked_inner: Arc<RPCProcessorUnlockedInner>,
@@ -257,7 +257,6 @@ impl RPCProcessor {
send_channel: None,
stop_source: None,
worker_join_handles: Vec::new(),
opt_storage_manager: None,
}
}
fn new_unlocked_inner(
@@ -298,6 +297,7 @@ impl RPCProcessor {
config: config.clone(),
network_manager: network_manager.clone(),
routing_table: network_manager.routing_table(),
storage_manager: network_manager.storage_manager(),
inner: Arc::new(Mutex::new(Self::new_inner())),
unlocked_inner: Arc::new(Self::new_unlocked_inner(config, update_callback)),
}
@@ -311,14 +311,8 @@ impl RPCProcessor {
self.routing_table.clone()
}
pub fn set_storage_manager(&self, opt_storage_manager: Option<StorageManager>) {
let mut inner = self.inner.lock();
inner.opt_storage_manager = opt_storage_manager
}
pub fn storage_manager(&self) -> Option<StorageManager> {
let inner = self.inner.lock();
inner.opt_storage_manager.clone()
pub fn storage_manager(&self) -> StorageManager {
self.storage_manager.clone()
}
//////////////////////////////////////////////////////////////////////
@@ -326,28 +320,35 @@ impl RPCProcessor {
#[instrument(level = "debug", skip_all, err)]
pub async fn startup(&self) -> EyreResult<()> {
debug!("startup rpc processor");
let mut inner = self.inner.lock();
{
let mut inner = self.inner.lock();
let channel = flume::bounded(self.unlocked_inner.queue_size as usize);
inner.send_channel = Some(channel.0.clone());
inner.stop_source = Some(StopSource::new());
let channel = flume::bounded(self.unlocked_inner.queue_size as usize);
inner.send_channel = Some(channel.0.clone());
inner.stop_source = Some(StopSource::new());
// spin up N workers
trace!(
"Spinning up {} RPC workers",
self.unlocked_inner.concurrency
);
for _ in 0..self.unlocked_inner.concurrency {
let this = self.clone();
let receiver = channel.1.clone();
let jh = spawn(Self::rpc_worker(
this,
inner.stop_source.as_ref().unwrap().token(),
receiver,
));
inner.worker_join_handles.push(jh);
// spin up N workers
trace!(
"Spinning up {} RPC workers",
self.unlocked_inner.concurrency
);
for _ in 0..self.unlocked_inner.concurrency {
let this = self.clone();
let receiver = channel.1.clone();
let jh = spawn(Self::rpc_worker(
this,
inner.stop_source.as_ref().unwrap().token(),
receiver,
));
inner.worker_join_handles.push(jh);
}
}
// Inform storage manager we are up
self.storage_manager
.set_rpc_processor(Some(self.clone()))
.await;
Ok(())
}
@@ -355,6 +356,9 @@ impl RPCProcessor {
pub async fn shutdown(&self) {
debug!("starting rpc processor shutdown");
// Stop storage manager from using us
self.storage_manager.set_rpc_processor(None).await;
// Stop the rpc workers
let mut unord = FuturesUnordered::new();
{

View File

@@ -29,6 +29,8 @@ struct StorageManagerInner {
local_record_store: Option<RecordStore>,
/// Records that have been pushed to this node for distribution by other nodes
remote_record_store: Option<RecordStore>,
/// RPC processor if it is available
rpc_processor: Option<RPCProcessor>,
}
struct StorageManagerUnlockedInner {
@@ -37,7 +39,6 @@ struct StorageManagerUnlockedInner {
protected_store: ProtectedStore,
table_store: TableStore,
block_store: BlockStore,
rpc_processor: RPCProcessor,
}
#[derive(Clone)]
@@ -53,7 +54,6 @@ impl StorageManager {
protected_store: ProtectedStore,
table_store: TableStore,
block_store: BlockStore,
rpc_processor: RPCProcessor,
) -> StorageManagerUnlockedInner {
StorageManagerUnlockedInner {
config,
@@ -61,7 +61,6 @@ impl StorageManager {
protected_store,
table_store,
block_store,
rpc_processor,
}
}
fn new_inner() -> StorageManagerInner {
@@ -69,6 +68,7 @@ impl StorageManager {
initialized: false,
local_record_store: None,
remote_record_store: None,
rpc_processor: None,
}
}
@@ -106,7 +106,6 @@ impl StorageManager {
protected_store: ProtectedStore,
table_store: TableStore,
block_store: BlockStore,
rpc_processor: RPCProcessor,
) -> StorageManager {
StorageManager {
unlocked_inner: Arc::new(Self::new_unlocked_inner(
@@ -115,7 +114,6 @@ impl StorageManager {
protected_store,
table_store,
block_store,
rpc_processor,
)),
inner: Arc::new(AsyncMutex::new(Self::new_inner())),
}
@@ -148,11 +146,6 @@ impl StorageManager {
inner.initialized = true;
// Let rpc processor access storage manager
self.unlocked_inner
.rpc_processor
.set_storage_manager(Some(self.clone()));
Ok(())
}
@@ -163,12 +156,14 @@ impl StorageManager {
// Release the storage manager
*inner = Self::new_inner();
// Remove storage manager from rpc processor
self.unlocked_inner.rpc_processor.set_storage_manager(None);
debug!("finished storage manager shutdown");
}
pub async fn set_rpc_processor(&self, opt_rpc_processor: Option<RPCProcessor>) {
let mut inner = self.inner.lock().await;
inner.rpc_processor = opt_rpc_processor
}
/// # DHT Key = Hash(ownerKeyKind) of: [ ownerKeyValue, schema ]
fn get_key(&self, vcrypto: CryptoSystemVersion, record: &Record) -> TypedKey {
let compiled = record.descriptor().schema_data();

View File

@@ -135,9 +135,9 @@ impl RecordStore {
async fn purge_dead_records(&mut self, lazy: bool) {
let purge_dead_records_mutex = self.purge_dead_records_mutex.clone();
let lock = if lazy {
match purge_dead_records_mutex.try_lock() {
Ok(v) => v,
Err(_) => {
match mutex_try_lock!(purge_dead_records_mutex) {
Some(v) => v,
None => {
// If not ready now, just skip it if we're lazy
return;
}

View File

@@ -204,22 +204,28 @@ fn config_callback(key: String) -> ConfigCallbackReturn {
"network.rpc.queue_size" => Ok(Box::new(1024u32)),
"network.rpc.max_timestamp_behind_ms" => Ok(Box::new(Some(10_000u32))),
"network.rpc.max_timestamp_ahead_ms" => Ok(Box::new(Some(10_000u32))),
"network.rpc.timeout_ms" => Ok(Box::new(10_000u32)),
"network.rpc.timeout_ms" => Ok(Box::new(5_000u32)),
"network.rpc.max_route_hop_count" => Ok(Box::new(4u8)),
"network.rpc.default_route_hop_count" => Ok(Box::new(1u8)),
"network.dht.resolve_node_timeout_ms" => Ok(Box::new(Option::<u32>::None)),
"network.dht.resolve_node_timeout_ms" => Ok(Box::new(10_000u32)),
"network.dht.resolve_node_count" => Ok(Box::new(20u32)),
"network.dht.resolve_node_fanout" => Ok(Box::new(3u32)),
"network.dht.max_find_node_count" => Ok(Box::new(20u32)),
"network.dht.get_value_timeout_ms" => Ok(Box::new(10u32)),
"network.dht.get_value_timeout_ms" => Ok(Box::new(10_000u32)),
"network.dht.get_value_count" => Ok(Box::new(20u32)),
"network.dht.get_value_fanout" => Ok(Box::new(3u32)),
"network.dht.set_value_timeout_ms" => Ok(Box::new(10u32)),
"network.dht.set_value_timeout_ms" => Ok(Box::new(10_000u32)),
"network.dht.set_value_count" => Ok(Box::new(20u32)),
"network.dht.set_value_fanout" => Ok(Box::new(5u32)),
"network.dht.min_peer_count" => Ok(Box::new(20u32)),
"network.dht.min_peer_refresh_time_ms" => Ok(Box::new(2_000u32)),
"network.dht.validate_dial_info_receipt_time_ms" => Ok(Box::new(5_000u32)),
"network.dht.local_subkey_cache_size" => Ok(Box::new(128u32)),
"network.dht.local_max_subkey_cache_memory_mb" => Ok(Box::new(256u32)),
"network.dht.remote_subkey_cache_size" => Ok(Box::new(1024u32)),
"network.dht.remote_max_records" => Ok(Box::new(4096u32)),
"network.dht.remote_max_subkey_cache_memory_mb" => Ok(Box::new(64u32)),
"network.dht.remote_max_storage_space_mb" => Ok(Box::new(64u32)),
"network.upnp" => Ok(Box::new(false)),
"network.detect_address_changes" => Ok(Box::new(true)),
"network.restricted_nat_retries" => Ok(Box::new(3u32)),