1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use std::sync::Arc;
use creep::Context;
use futures::channel::mpsc::{unbounded, UnboundedReceiver, UnboundedSender};
use parking_lot::RwLock;
use crate::error::ConsensusError;
use crate::state::process::State;
use crate::types::{Address, Node, OverlordMsg};
use crate::DurationConfig;
use crate::{smr::SMR, timer::Timer};
use crate::{Codec, Consensus, ConsensusResult, Crypto, Wal};
type Pile<T> = RwLock<Option<T>>;
pub struct Overlord<T: Codec, F: Consensus<T>, C: Crypto, W: Wal> {
sender: Pile<UnboundedSender<(Context, OverlordMsg<T>)>>,
state_rx: Pile<UnboundedReceiver<(Context, OverlordMsg<T>)>>,
address: Pile<Address>,
consensus: Pile<Arc<F>>,
crypto: Pile<Arc<C>>,
wal: Pile<Arc<W>>,
}
impl<T, F, C, W> Overlord<T, F, C, W>
where
T: Codec + Send + Sync + 'static,
F: Consensus<T> + 'static,
C: Crypto + Send + Sync + 'static,
W: Wal + 'static,
{
pub fn new(address: Address, consensus: Arc<F>, crypto: Arc<C>, wal: Arc<W>) -> Self {
let (tx, rx) = unbounded();
Overlord {
sender: RwLock::new(Some(tx)),
state_rx: RwLock::new(Some(rx)),
address: RwLock::new(Some(address)),
consensus: RwLock::new(Some(consensus)),
crypto: RwLock::new(Some(crypto)),
wal: RwLock::new(Some(wal)),
}
}
pub fn get_handler(&self) -> OverlordHandler<T> {
let sender = self.sender.write();
assert!(sender.is_some());
let tx = sender.clone().unwrap();
OverlordHandler::new(tx)
}
pub async fn run(
&self,
init_height: u64,
interval: u64,
authority_list: Vec<Node>,
timer_config: Option<DurationConfig>,
) -> ConsensusResult<()> {
let (mut smr_provider, evt_state, evt_timer) = SMR::new();
let smr_handler = smr_provider.take_smr();
let timer = Timer::new(evt_timer, smr_handler.clone(), interval, timer_config);
let (verify_sig_tx, verify_sig_rx) = unbounded();
let (rx, mut state, resp) = {
let mut state_rx = self.state_rx.write();
let mut address = self.address.write();
let mut consensus = self.consensus.write();
let mut crypto = self.crypto.write();
let mut wal = self.wal.write();
let tmp_rx = state_rx.take().unwrap();
let (tmp_state, tmp_resp) = State::new(
smr_handler,
address.take().unwrap(),
init_height,
interval,
authority_list,
verify_sig_tx,
consensus.take().unwrap(),
crypto.take().unwrap(),
wal.take().unwrap(),
);
assert!(address.is_none());
assert!(consensus.is_none());
assert!(crypto.is_none());
assert!(state_rx.is_none());
assert!(wal.is_none());
(tmp_rx, tmp_state, tmp_resp)
};
log::info!("Overlord start running");
smr_provider.run();
timer.run();
state.run(rx, evt_state, resp, verify_sig_rx).await;
Ok(())
}
}
#[derive(Clone, Debug)]
pub struct OverlordHandler<T: Codec>(UnboundedSender<(Context, OverlordMsg<T>)>);
impl<T: Codec> OverlordHandler<T> {
fn new(tx: UnboundedSender<(Context, OverlordMsg<T>)>) -> Self {
OverlordHandler(tx)
}
pub fn send_msg(&self, ctx: Context, msg: OverlordMsg<T>) -> ConsensusResult<()> {
let ctx = match muta_apm::MUTA_TRACER.span(
"overlord.send_msg_to_inner",
vec![muta_apm::rustracing::tag::Tag::new("kind", "overlord")],
) {
Some(mut span) => {
span.log(|log| {
log.time(std::time::SystemTime::now());
});
ctx.with_value("parent_span_ctx", span.context().cloned())
}
None => ctx,
};
if self.0.is_closed() {
Err(ConsensusError::ChannelErr(
"[OverlordHandler]: channel closed".to_string(),
))
} else {
self.0
.unbounded_send((ctx, msg))
.map_err(|e| ConsensusError::Other(format!("Send message error {:?}", e)))
}
}
}