Skip to content

Commit

Permalink
add short names for simulated nodes (#9)
Browse files Browse the repository at this point in the history
* introduce short names for logging

* add names_map

* fix name

* f

* change names
  • Loading branch information
rjwalters authored and Robb Walters committed Sep 11, 2020
1 parent 8eb8d17 commit 7c56ba8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
1 change: 1 addition & 0 deletions consensus/scp/tests/mock_network/cyclic_topology.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub fn directed_cycle(num_nodes: usize) -> mock_network::Network {
.collect::<Vec<NodeID>>();

nodes.push(mock_network::NodeOptions::new(
format!("c{}", node_index),
test_utils::test_node_id(node_index as u32),
peers_vector.iter().cloned().collect::<HashSet<NodeID>>(),
QuorumSet::new_with_node_ids(1, vec![next_node_id]),
Expand Down
1 change: 1 addition & 0 deletions consensus/scp/tests/mock_network/mesh_topology.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub fn dense_mesh(num_nodes: usize, k: usize) -> mock_network::Network {
.collect::<Vec<NodeID>>();

nodes.push(mock_network::NodeOptions::new(
format!("m{}", node_index),
test_utils::test_node_id(node_index as u32),
peers_vector.iter().cloned().collect::<HashSet<NodeID>>(),
QuorumSet::new_with_node_ids(k as u32, peers_vector),
Expand Down
54 changes: 32 additions & 22 deletions consensus/scp/tests/mock_network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#![allow(dead_code)]

use mc_common::{
logger::{log, o, Logger},
logger::{log, Logger},
HashMap, HashSet, NodeID,
};
use mc_consensus_scp::{
Expand Down Expand Up @@ -86,6 +86,9 @@ impl TestOptions {
// Describes one simulated node
#[derive(Clone)]
pub struct NodeOptions {
/// This node's short name
name: String,

/// This node's id
id: NodeID,

Expand All @@ -97,8 +100,9 @@ pub struct NodeOptions {
}

impl NodeOptions {
pub fn new(id: NodeID, peers: HashSet<NodeID>, quorum_set: QuorumSet) -> Self {
pub fn new(name: String, id: NodeID, peers: HashSet<NodeID>, quorum_set: QuorumSet) -> Self {
Self {
name,
id,
peers,
quorum_set,
Expand All @@ -121,6 +125,7 @@ impl Network {

pub struct SimulatedNetwork {
handle_map: HashMap<NodeID, JoinHandle<()>>,
names_map: HashMap<NodeID, String>,
nodes_map: Arc<Mutex<HashMap<NodeID, SimulatedNode>>>,
shared_data_map: HashMap<NodeID, Arc<Mutex<SimulatedNodeSharedData>>>,
logger: Logger,
Expand All @@ -131,6 +136,7 @@ impl SimulatedNetwork {
pub fn new(network: &Network, test_options: &TestOptions, logger: Logger) -> Self {
let mut simulation = SimulatedNetwork {
handle_map: HashMap::default(),
names_map: HashMap::default(),
nodes_map: Arc::new(Mutex::new(HashMap::default())),
shared_data_map: HashMap::default(),
logger: logger.clone(),
Expand All @@ -143,19 +149,21 @@ impl SimulatedNetwork {
let peers_clone = node_options.peers.clone();

let (node, join_handle_option) = SimulatedNode::new(
format!("{}-{}", network.name, node_options.id.clone()),
node_options.id.clone(),
node_options.quorum_set.clone(),
node_options.clone(),
test_options,
Arc::new(move |logger, msg| {
SimulatedNetwork::broadcast_msg(logger, &nodes_map_clone, &peers_clone, msg)
}),
logger.new(o!("mc.local_node_id" => node_options.id.to_string())),
logger.clone(),
);
simulation.handle_map.insert(
node_options.id.clone(),
join_handle_option.expect("thread failed to spawn"),
);
simulation.names_map.insert(
node_options.id.clone(),
node_options.name.clone(),
);
simulation
.shared_data_map
.insert(node_options.id.clone(), node.shared_data.clone());
Expand All @@ -176,15 +184,19 @@ impl SimulatedNetwork {
.expect("lock failed on nodes_map in stop_all");
let mut node_ids: Vec<NodeID> = Vec::new();
for (node_id, node) in nodes_map.iter_mut() {
log::trace!(self.logger, "sending stop to {}", node_id);
log::trace!(
self.logger,
"sending stop to {}",
self.names_map
.get(node_id)
.expect("could not find node_id in nodes_map"),
);
node.send_stop();
node_ids.push(node_id.clone());
}
drop(nodes_map);

for node_id in node_ids {
log::trace!(self.logger, "joining {}", node_id);

self.handle_map
.remove(&node_id)
.expect("thread handle is missing")
Expand Down Expand Up @@ -276,9 +288,7 @@ struct SimulatedNode {

impl SimulatedNode {
fn new(
thread_name: String,
node_id: NodeID,
quorum_set: QuorumSet,
node_options: NodeOptions,
test_options: &TestOptions,
broadcast_msg_fn: Arc<dyn Fn(Logger, Msg<String>) + Sync + Send>,
logger: Logger,
Expand All @@ -291,8 +301,8 @@ impl SimulatedNode {
};

let mut thread_local_node = Node::new(
node_id.clone(),
quorum_set,
node_options.id.clone(),
node_options.quorum_set.clone(),
test_options.validity_fn.clone(),
test_options.combine_fn.clone(),
logger.clone(),
Expand All @@ -310,7 +320,7 @@ impl SimulatedNode {

let join_handle_option = Some(
thread::Builder::new()
.name(thread_name)
.name(node_options.id.to_string())
.spawn(move || {
// All values that have not yet been externalized.
let mut pending_values: HashSet<String> = HashSet::default();
Expand Down Expand Up @@ -423,7 +433,7 @@ impl SimulatedNode {
log::trace!(
logger,
"( ledger ) node {} slot {} : {} new, {} total, {} pending",
node_id,
node_options.name,
current_slot as SlotIndex,
new_block_length,
ledger_size,
Expand All @@ -437,7 +447,7 @@ impl SimulatedNode {
log::info!(
logger,
"thread results: {},{},{}",
node_id,
node_options.name,
total_broadcasts,
current_slot,
);
Expand Down Expand Up @@ -594,7 +604,7 @@ pub fn build_and_test(network: &Network, test_options: &TestOptions, logger: Log
simulation.logger,
"( testing ) failed to externalize all values within {} sec at node {}!",
test_options.allowed_test_time.as_secs(),
node_id,
simulation.names_map.get(node_id).expect("could not find node_id"),
);
// panic
panic!("test failed due to timeout");
Expand All @@ -610,14 +620,14 @@ pub fn build_and_test(network: &Network, test_options: &TestOptions, logger: Log
"( testing ) externalized {}/{} values at node {}",
num_externalized_values,
test_options.values_to_submit,
node_id
simulation.names_map.get(node_id).expect("could not find node_id"),
);

if num_externalized_values > test_options.values_to_submit {
log::warn!(
simulation.logger,
"( testing ) externalized extra values at node {}",
node_id
simulation.names_map.get(node_id).expect("could not find node_id"),
);
}

Expand All @@ -630,7 +640,7 @@ pub fn build_and_test(network: &Network, test_options: &TestOptions, logger: Log
"( testing ) externalized {}/{} values at node {}",
num_externalized_values,
test_options.values_to_submit,
node_id
simulation.names_map.get(node_id).expect("could not find node_id"),
);
last_log = Instant::now();
}
Expand Down Expand Up @@ -661,7 +671,7 @@ pub fn build_and_test(network: &Network, test_options: &TestOptions, logger: Log
log::error!(
simulation.logger,
"node {} externalized wrong values! missing: {:?}, unexpected: {:?}",
node_id,
simulation.names_map.get(node_id).expect("could not find node_id"),
missing_values,
unexpected_values,
);
Expand Down

0 comments on commit 7c56ba8

Please sign in to comment.