@@ -10,9 +10,11 @@ use rand::random;
10
10
use inner:: { NodeAction , NodeInner } ;
11
11
use net:: { get_local_addrs, ConnectionPool } ;
12
12
13
- use crate :: error:: Error ;
13
+ use crate :: error:: { Error , TarantoolErrorCode } ;
14
+ use crate :: fiber:: Cond ;
14
15
use crate :: net_box:: { Conn , ConnOptions , Options } ;
15
- use crate :: tuple:: { FunctionArgs , FunctionCtx } ;
16
+ use crate :: raft:: inner:: NodeEvent ;
17
+ use crate :: tuple:: { FunctionArgs , FunctionCtx , Tuple } ;
16
18
17
19
mod fsm;
18
20
pub mod inner;
@@ -24,6 +26,9 @@ pub struct Node {
24
26
inner : RefCell < NodeInner > ,
25
27
connections : RefCell < ConnectionPool > ,
26
28
rpc_function : String ,
29
+ events_cond : Cond ,
30
+ events_buffer : RefCell < VecDeque < NodeEvent > > ,
31
+ actions_buffer : RefCell < VecDeque < NodeAction > > ,
27
32
options : NodeOptions ,
28
33
}
29
34
@@ -67,17 +72,26 @@ impl Node {
67
72
inner : RefCell :: new ( NodeInner :: new ( id, local_addrs, bootstrap_addrs_cfg) ) ,
68
73
connections : RefCell :: new ( ConnectionPool :: new ( options. connection_options . clone ( ) ) ) ,
69
74
rpc_function : rpc_function. to_string ( ) ,
75
+ events_cond : Cond :: new ( ) ,
76
+ events_buffer : RefCell :: new ( VecDeque :: with_capacity ( options. recv_queue_size ) ) ,
77
+ actions_buffer : RefCell :: new ( VecDeque :: with_capacity ( options. send_queue_size ) ) ,
70
78
options,
71
79
} )
72
80
}
73
81
74
82
pub fn run ( & self ) -> Result < ( ) , Error > {
75
- let mut events = VecDeque :: new ( ) ;
76
- let mut actions = VecDeque :: new ( ) ;
77
83
loop {
78
- self . inner . borrow_mut ( ) . update ( & mut events, & mut actions) ;
79
- for action in actions. drain ( ..) {
84
+ {
85
+ let mut actions = self . actions_buffer . borrow_mut ( ) ;
86
+ let mut events = self . events_buffer . borrow_mut ( ) ;
87
+ self . inner . borrow_mut ( ) . update ( & mut events, & mut actions) ;
88
+ }
89
+
90
+ for action in self . actions_buffer . borrow_mut ( ) . drain ( ..) {
80
91
match action {
92
+ NodeAction :: Connect ( id, addrs) => {
93
+ self . connections . borrow_mut ( ) . connect ( id, & addrs[ ..] ) ?;
94
+ }
81
95
NodeAction :: Request ( to, msg) => {
82
96
let mut conn_pool = self . connections . borrow_mut ( ) ;
83
97
self . send ( conn_pool. get ( & to) . unwrap ( ) , rpc:: Request :: Bootstrap ( msg) ) ?;
@@ -89,11 +103,31 @@ impl Node {
89
103
_ => { }
90
104
} ;
91
105
}
106
+
107
+ self . events_cond . wait ( ) ;
92
108
}
93
109
}
94
110
95
111
pub fn handle_rpc ( & self , ctx : FunctionCtx , args : FunctionArgs ) -> i32 {
96
- unimplemented ! ( ) ;
112
+ let args: Tuple = args. into ( ) ;
113
+
114
+ match args. into_struct :: < rpc:: Request > ( ) {
115
+ Err ( e) => set_error ! ( TarantoolErrorCode :: Protocol , "{}" , e) ,
116
+ Ok ( request) => {
117
+ match request {
118
+ rpc:: Request :: Bootstrap ( msg) => {
119
+ self . events_buffer
120
+ . borrow_mut ( )
121
+ . push_back ( NodeEvent :: Request ( msg) ) ;
122
+ self . events_cond . wait ( ) ;
123
+ }
124
+ _ => unimplemented ! ( ) ,
125
+ } ;
126
+
127
+ ctx. return_mp ( & rpc:: Response :: Ack )
128
+ . unwrap_or_else ( |e| set_error ! ( TarantoolErrorCode :: ProcC , "{}" , e) )
129
+ }
130
+ }
97
131
}
98
132
99
133
pub fn wait_ready ( & self , timeout : Duration ) -> Result < ( ) , Error > {
0 commit comments