1
- use std:: cell:: { Cell , RefCell } ;
2
1
use std:: collections:: { BTreeMap , HashSet , VecDeque } ;
3
2
use std:: net:: SocketAddr ;
4
3
@@ -7,11 +6,11 @@ use crate::raft::net::ConnectionId;
7
6
use crate :: raft:: rpc;
8
7
9
8
pub struct NodeInner {
10
- state : Cell < State > ,
9
+ state : State ,
11
10
local_id : u64 ,
12
- peers : RefCell < BTreeMap < u64 , Vec < SocketAddr > > > ,
13
- responded_ids : RefCell < HashSet < u64 > > ,
14
- pending_actions_buffer : RefCell < VecDeque < NodeAction > > ,
11
+ peers : BTreeMap < u64 , Vec < SocketAddr > > ,
12
+ responded_ids : HashSet < u64 > ,
13
+ pending_actions_buffer : VecDeque < NodeAction > ,
15
14
}
16
15
17
16
#[ derive( Debug , Copy , Clone ) ]
@@ -46,29 +45,28 @@ impl NodeInner {
46
45
let mut peers = BTreeMap :: new ( ) ;
47
46
peers. insert ( local_id, local_addrs) ;
48
47
49
- let bootstrap_controller = NodeInner {
50
- state : Cell :: new ( State :: Cold ) ,
48
+ let mut node_inner = NodeInner {
49
+ state : State :: Cold ,
51
50
local_id,
52
- peers : RefCell :: new ( peers ) ,
51
+ peers,
53
52
responded_ids : Default :: default ( ) ,
54
53
pending_actions_buffer : Default :: default ( ) ,
55
54
} ;
56
- bootstrap_controller . poll_seeds ( bootstrap_addrs. into_iter ( ) ) ;
57
- bootstrap_controller
55
+ node_inner . poll_seeds ( bootstrap_addrs. into_iter ( ) ) ;
56
+ node_inner
58
57
}
59
58
60
- pub fn pending_actions ( & self ) -> Vec < NodeAction > {
59
+ pub fn pending_actions ( & mut self ) -> Vec < NodeAction > {
61
60
self . pending_actions_buffer
62
- . borrow_mut ( )
63
61
. drain ( ..)
64
62
. collect :: < Vec < NodeAction > > ( )
65
63
}
66
64
67
- pub fn handle_event ( & self , event : NodeEvent ) {
65
+ pub fn handle_event ( & mut self , event : NodeEvent ) {
68
66
use NodeEvent as E ;
69
67
use State as S ;
70
68
71
- let new_state = match ( self . state . get ( ) , event) {
69
+ let new_state = match ( self . state , event) {
72
70
( S :: Cold , E :: Request ( req) )
73
71
| ( S :: Cold , E :: Response ( req) )
74
72
| ( S :: Offline , E :: Request ( req) ) => {
@@ -78,8 +76,8 @@ impl NodeInner {
78
76
( S :: Warm , E :: Request ( req) ) | ( S :: Warm , E :: Response ( req) ) => {
79
77
self . handle_msg ( req) ;
80
78
81
- let num_peers = self . peers . borrow ( ) . len ( ) ;
82
- let num_responded = self . responded_ids . borrow ( ) . len ( ) ;
79
+ let num_peers = self . peers . len ( ) ;
80
+ let num_responded = self . responded_ids . len ( ) ;
83
81
if num_peers == ( num_responded + 1 ) {
84
82
self . send ( NodeAction :: Completed ) ;
85
83
Some ( S :: Done )
@@ -93,29 +91,28 @@ impl NodeInner {
93
91
} ;
94
92
95
93
if let Some ( new_state) = new_state {
96
- self . state . set ( new_state) ;
94
+ self . state = new_state;
97
95
}
98
96
}
99
97
100
- fn handle_msg ( & self , req : rpc:: BootstrapMsg ) {
98
+ fn handle_msg ( & mut self , req : rpc:: BootstrapMsg ) {
101
99
if req. from_id == self . local_id {
102
100
return ;
103
101
}
104
102
105
- let mut responded_ids = self . responded_ids . borrow_mut ( ) ;
106
- if !responded_ids. contains ( & req. from_id ) {
103
+ if !self . responded_ids . contains ( & req. from_id ) {
107
104
let new_nodes = self . merge_nodes_list ( & req. nodes ) ;
108
105
for ( id, addrs) in new_nodes {
109
106
let id = ConnectionId :: Peer ( id) ;
110
107
self . send ( NodeAction :: Connect ( id. clone ( ) , addrs) ) ;
111
108
self . send_bootstrap_request ( id) ;
112
109
}
113
- responded_ids. insert ( req. from_id ) ;
110
+ self . responded_ids . insert ( req. from_id ) ;
114
111
}
115
112
}
116
113
117
114
#[ inline]
118
- fn poll_seeds ( & self , addrs : impl Iterator < Item = Vec < SocketAddr > > ) {
115
+ fn poll_seeds ( & mut self , addrs : impl Iterator < Item = Vec < SocketAddr > > ) {
119
116
for ( id, seed_addrs) in addrs. enumerate ( ) {
120
117
let id = ConnectionId :: Seed ( id) ;
121
118
self . send ( NodeAction :: Connect ( id. clone ( ) , seed_addrs) ) ;
@@ -124,10 +121,9 @@ impl NodeInner {
124
121
}
125
122
126
123
#[ inline]
127
- fn send_bootstrap_request ( & self , to : ConnectionId ) {
124
+ fn send_bootstrap_request ( & mut self , to : ConnectionId ) {
128
125
let nodes = self
129
126
. peers
130
- . borrow ( )
131
127
. iter ( )
132
128
. map ( |( id, addrs) | ( * id, addrs. clone ( ) ) )
133
129
. collect ( ) ;
@@ -142,21 +138,20 @@ impl NodeInner {
142
138
}
143
139
144
140
#[ inline]
145
- fn send ( & self , action : NodeAction ) {
146
- self . pending_actions_buffer . borrow_mut ( ) . push_back ( action)
141
+ fn send ( & mut self , action : NodeAction ) {
142
+ self . pending_actions_buffer . push_back ( action)
147
143
}
148
144
149
145
/// Merges `other` nodes list to already known. Returns new nodes count
150
146
fn merge_nodes_list (
151
- & self ,
147
+ & mut self ,
152
148
nodes_from : & Vec < ( u64 , Vec < SocketAddr > ) > ,
153
149
) -> Vec < ( u64 , Vec < SocketAddr > ) > {
154
150
let mut new_nodes = Vec :: < ( u64 , Vec < SocketAddr > ) > :: with_capacity ( nodes_from. len ( ) ) ;
155
151
{
156
- let mut nodes_into = self . peers . borrow_mut ( ) ;
157
152
for ( id, addrs) in nodes_from. into_iter ( ) {
158
- if !nodes_into . contains_key ( id) {
159
- nodes_into . insert ( * id, addrs. clone ( ) ) ;
153
+ if !self . peers . contains_key ( id) {
154
+ self . peers . insert ( * id, addrs. clone ( ) ) ;
160
155
new_nodes. push ( ( * id, addrs. clone ( ) ) ) ;
161
156
}
162
157
}
0 commit comments