From 640e36287e8a3d4b560ac7a998062980e2047e7b Mon Sep 17 00:00:00 2001 From: Michael Vines Date: Mon, 1 Mar 2021 13:20:04 -0800 Subject: [PATCH] Move ValidatorExit into ValidatorConfig, making it accessible from the solana-validator crate --- core/src/rpc.rs | 12 +++++------- core/src/rpc_service.rs | 7 +++---- core/src/validator.rs | 35 +++++++++++++++++++++++------------ 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/core/src/rpc.rs b/core/src/rpc.rs index 8849786288..289b7b3be7 100644 --- a/core/src/rpc.rs +++ b/core/src/rpc.rs @@ -130,7 +130,7 @@ pub struct JsonRpcRequestProcessor { blockstore: Arc, config: JsonRpcConfig, snapshot_config: Option, - validator_exit: Arc>>, + validator_exit: Arc>, health: Arc, cluster_info: Arc, genesis_hash: Hash, @@ -215,7 +215,7 @@ impl JsonRpcRequestProcessor { bank_forks: Arc>, block_commitment_cache: Arc>, blockstore: Arc, - validator_exit: Arc>>, + validator_exit: Arc>, health: Arc, cluster_info: Arc, genesis_hash: Hash, @@ -661,9 +661,7 @@ impl JsonRpcRequestProcessor { pub fn validator_exit(&self) -> bool { if self.config.enable_validator_exit { warn!("validator_exit request..."); - if let Some(x) = self.validator_exit.write().unwrap().take() { - x.exit() - } + self.validator_exit.write().unwrap().exit(); true } else { debug!("validator_exit ignored"); @@ -3049,11 +3047,11 @@ fn deserialize_transaction( .map(|transaction| (wire_transaction, transaction)) } -pub(crate) fn create_validator_exit(exit: &Arc) -> Arc>> { +pub(crate) fn create_validator_exit(exit: &Arc) -> Arc> { let mut validator_exit = ValidatorExit::default(); let exit_ = exit.clone(); validator_exit.register_exit(Box::new(move || exit_.store(true, Ordering::Relaxed))); - Arc::new(RwLock::new(Some(validator_exit))) + Arc::new(RwLock::new(validator_exit)) } #[cfg(test)] diff --git a/core/src/rpc_service.rs b/core/src/rpc_service.rs index 0b20a013e8..30513633c5 100644 --- a/core/src/rpc_service.rs +++ b/core/src/rpc_service.rs @@ -267,7 +267,7 @@ impl JsonRpcService { poh_recorder: Option>>, genesis_hash: Hash, ledger_path: &Path, - validator_exit: Arc>>, + validator_exit: Arc>, trusted_validators: Option>, override_health_check: Arc, optimistically_confirmed_bank: Arc>, @@ -434,9 +434,8 @@ impl JsonRpcService { let close_handle = close_handle_receiver.recv().unwrap(); let close_handle_ = close_handle.clone(); - let mut validator_exit_write = validator_exit.write().unwrap(); - validator_exit_write - .as_mut() + validator_exit + .write() .unwrap() .register_exit(Box::new(move || close_handle_.close())); Self { diff --git a/core/src/validator.rs b/core/src/validator.rs index b38428de00..d02dca6584 100644 --- a/core/src/validator.rs +++ b/core/src/validator.rs @@ -66,6 +66,7 @@ use solana_vote_program::vote_state::VoteState; use std::time::Instant; use std::{ collections::HashSet, + fmt, net::SocketAddr, ops::Deref, path::{Path, PathBuf}, @@ -127,6 +128,7 @@ pub struct ValidatorConfig { pub accounts_db_test_hash_calculation: bool, pub accounts_db_use_index_hash_calculation: bool, pub tpu_coalesce_ms: u64, + pub validator_exit: Arc>, } impl Default for ValidatorConfig { @@ -179,6 +181,7 @@ impl Default for ValidatorConfig { accounts_db_test_hash_calculation: false, accounts_db_use_index_hash_calculation: true, tpu_coalesce_ms: DEFAULT_TPU_COALESCE_MS, + validator_exit: Arc::new(RwLock::new(ValidatorExit::default())), } } } @@ -193,13 +196,19 @@ impl ValidatorExit { self.exits.push(exit); } - pub fn exit(self) { - for exit in self.exits { + pub fn exit(&mut self) { + for exit in self.exits.drain(..) { exit(); } } } +impl fmt::Debug for ValidatorExit { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{} exits", self.exits.len()) + } +} + #[derive(Default)] struct TransactionHistoryServices { transaction_status_sender: Option, @@ -218,7 +227,7 @@ struct RpcServices { pub struct Validator { pub id: Pubkey, - validator_exit: Arc>>, + validator_exit: Arc>, rpc_service: Option, transaction_status_service: Option, rewards_recorder_service: Option, @@ -319,11 +328,15 @@ impl Validator { start.stop(); info!("done. {}", start); - let mut validator_exit = ValidatorExit::default(); let exit = Arc::new(AtomicBool::new(false)); - let exit_ = exit.clone(); - validator_exit.register_exit(Box::new(move || exit_.store(true, Ordering::Relaxed))); - let validator_exit = Arc::new(RwLock::new(Some(validator_exit))); + { + let exit = exit.clone(); + config + .validator_exit + .write() + .unwrap() + .register_exit(Box::new(move || exit.store(true, Ordering::Relaxed))); + } let (replay_vote_sender, replay_vote_receiver) = unbounded(); let ( @@ -484,7 +497,7 @@ impl Validator { Some(poh_recorder.clone()), genesis_config.hash(), ledger_path, - validator_exit.clone(), + config.validator_exit.clone(), config.trusted_validators.clone(), rpc_override_health_check.clone(), optimistically_confirmed_bank.clone(), @@ -703,15 +716,13 @@ impl Validator { poh_service, poh_recorder, ip_echo_server, - validator_exit, + validator_exit: config.validator_exit.clone(), } } // Used for notifying many nodes in parallel to exit pub fn exit(&mut self) { - if let Some(x) = self.validator_exit.write().unwrap().take() { - x.exit() - } + self.validator_exit.write().unwrap().exit(); } pub fn close(mut self) {