Control service for IPC (#2013)

* hypervisor extension

* sorted with shutdown-wait

* hypervisor lifecycle alter
This commit is contained in:
Nikolay Volf
2016-08-30 16:05:02 +04:00
committed by Arkadiy Paronyan
parent 6f321d9849
commit efc846bb3e
6 changed files with 123 additions and 33 deletions

View File

@@ -62,10 +62,10 @@ pub fn payload<B: ipc::BinaryConvertable>() -> Result<B, BootError> {
.map_err(|binary_error| BootError::DecodeArgs(binary_error))
}
pub fn register(hv_url: &str, module_id: IpcModuleId) -> GuardedSocket<HypervisorServiceClient<NanoSocket>>{
pub fn register(hv_url: &str, control_url: &str, module_id: IpcModuleId) -> GuardedSocket<HypervisorServiceClient<NanoSocket>>{
let hypervisor_client = nanoipc::init_client::<HypervisorServiceClient<_>>(hv_url).unwrap();
hypervisor_client.handshake().unwrap();
hypervisor_client.module_ready(module_id);
hypervisor_client.module_ready(module_id, control_url.to_owned());
hypervisor_client
}

View File

@@ -32,6 +32,7 @@ pub mod service_urls {
pub const SYNC: &'static str = "parity-sync.ipc";
pub const SYNC_NOTIFY: &'static str = "parity-sync-notify.ipc";
pub const NETWORK_MANAGER: &'static str = "parity-manage-net.ipc";
pub const SYNC_CONTROL: &'static str = "parity-sync-control.ipc";
#[cfg(feature="stratum")]
pub const STRATUM: &'static str = "parity-stratum.ipc";
#[cfg(feature="stratum")]

View File

@@ -260,6 +260,10 @@ pub fn execute(cmd: RunCmd) -> Result<(), String> {
// Handle exit
wait_for_exit(panic_handler, http_server, ipc_server, dapps_server, signer_server);
// hypervisor should be shutdown first while everything still works and can be
// terminated gracefully
drop(hypervisor);
Ok(())
}

View File

@@ -16,14 +16,26 @@
//! Parity sync service
use std;
use std::sync::Arc;
use hypervisor::{SYNC_MODULE_ID, HYPERVISOR_IPC_URL};
use std::sync::atomic::AtomicBool;
use hypervisor::{SYNC_MODULE_ID, HYPERVISOR_IPC_URL, ControlService};
use ethcore::client::{RemoteClient, ChainNotify};
use ethsync::{SyncProvider, EthSync, ManageNetwork, ServiceConfiguration};
use std::thread;
use modules::service_urls;
use boot;
use nanoipc;
#[derive(Default)]
struct SyncControlService {
pub stop: Arc<AtomicBool>,
}
impl ControlService for SyncControlService {
fn shutdown(&self) {
trace!(target: "hypervisor", "Received shutdown from control service");
self.stop.store(true, ::std::sync::atomic::Ordering::Relaxed);
}
}
pub fn main() {
boot::setup_cli_logger("sync");
@@ -33,31 +45,45 @@ pub fn main() {
let remote_client = dependency!(RemoteClient, &service_urls::with_base(&service_config.io_path, service_urls::CLIENT));
let stop = boot::main_thread();
let sync = EthSync::new(service_config.sync, remote_client.service().clone(), service_config.net).unwrap();
let _ = boot::register(
let _ = boot::main_thread();
let service_stop = Arc::new(AtomicBool::new(false));
let hypervisor = boot::register(
&service_urls::with_base(&service_config.io_path, HYPERVISOR_IPC_URL),
&service_urls::with_base(&service_config.io_path, service_urls::SYNC_CONTROL),
SYNC_MODULE_ID
);
boot::host_service(
&service_urls::with_base(&service_config.io_path, service_urls::SYNC),
stop.clone(),
service_stop.clone(),
sync.clone() as Arc<SyncProvider>
);
boot::host_service(
&service_urls::with_base(&service_config.io_path, service_urls::NETWORK_MANAGER),
stop.clone(),
service_stop.clone(),
sync.clone() as Arc<ManageNetwork>
);
boot::host_service(
&service_urls::with_base(&service_config.io_path, service_urls::SYNC_NOTIFY),
stop.clone(),
service_stop.clone(),
sync.clone() as Arc<ChainNotify>
);
while !stop.load(::std::sync::atomic::Ordering::Relaxed) {
thread::park_timeout(std::time::Duration::from_millis(1000));
let control_service = Arc::new(SyncControlService::default());
let as_control = control_service.clone() as Arc<ControlService>;
let mut worker = nanoipc::Worker::<ControlService>::new(&as_control);
worker.add_reqrep(
&service_urls::with_base(&service_config.io_path, service_urls::SYNC_CONTROL)
).unwrap();
while !control_service.stop.load(::std::sync::atomic::Ordering::Relaxed) {
worker.poll();
}
service_stop.store(true, ::std::sync::atomic::Ordering::Relaxed);
hypervisor.module_shutdown(SYNC_MODULE_ID);
trace!(target: "hypervisor", "Sync process terminated gracefully");
}