@@ -26,10 +26,16 @@ pub struct DriaComputeNode {
26
26
pub p2p : DriaP2PCommander ,
27
27
pub available_nodes : AvailableNodes ,
28
28
pub cancellation : CancellationToken ,
29
- // channels
29
+ /// Gossipsub message receiver.
30
30
message_rx : mpsc:: Receiver < ( PeerId , MessageId , Message ) > ,
31
- worklow_tx : mpsc:: Sender < WorkflowsWorkerInput > ,
32
- publish_rx : mpsc:: Receiver < WorkflowsWorkerOutput > ,
31
+ /// Workflow transmitter to send batchable tasks.
32
+ workflow_batch_tx : mpsc:: Sender < WorkflowsWorkerInput > ,
33
+ /// Publish receiver to receive messages to be published.
34
+ publish_batch_rx : mpsc:: Receiver < WorkflowsWorkerOutput > ,
35
+ /// Workflow transmitter to send single tasks.
36
+ workflow_single_tx : mpsc:: Sender < WorkflowsWorkerInput > ,
37
+ /// Publish receiver to receive messages to be published.
38
+ publish_single_rx : mpsc:: Receiver < WorkflowsWorkerOutput > ,
33
39
}
34
40
35
41
impl DriaComputeNode {
@@ -39,7 +45,12 @@ impl DriaComputeNode {
39
45
pub async fn new (
40
46
config : DriaComputeNodeConfig ,
41
47
cancellation : CancellationToken ,
42
- ) -> Result < ( DriaComputeNode , DriaP2PClient , WorkflowsWorker ) > {
48
+ ) -> Result < (
49
+ DriaComputeNode ,
50
+ DriaP2PClient ,
51
+ WorkflowsWorker ,
52
+ WorkflowsWorker ,
53
+ ) > {
43
54
// create the keypair from secret key
44
55
let keypair = secret_to_keypair ( & config. secret_key ) ;
45
56
@@ -66,10 +77,10 @@ impl DriaComputeNode {
66
77
protocol,
67
78
) ?;
68
79
69
- // create workflow worker
70
- let ( worklow_tx , workflow_rx ) = mpsc :: channel ( 256 ) ;
71
- let ( publish_tx , publish_rx ) = mpsc :: channel ( 256 ) ;
72
- let workflows_worker = WorkflowsWorker :: new ( workflow_rx , publish_tx ) ;
80
+ // create workflow workers
81
+ let ( workflows_batch_worker , workflow_batch_tx , publish_batch_rx ) = WorkflowsWorker :: new ( ) ;
82
+ let ( workflows_single_worker , workflow_single_tx , publish_single_rx ) =
83
+ WorkflowsWorker :: new ( ) ;
73
84
74
85
Ok ( (
75
86
DriaComputeNode {
@@ -78,11 +89,14 @@ impl DriaComputeNode {
78
89
cancellation,
79
90
available_nodes,
80
91
message_rx,
81
- worklow_tx,
82
- publish_rx,
92
+ workflow_batch_tx,
93
+ publish_batch_rx,
94
+ workflow_single_tx,
95
+ publish_single_rx,
83
96
} ,
84
97
p2p_client,
85
- workflows_worker,
98
+ workflows_batch_worker,
99
+ workflows_single_worker,
86
100
) )
87
101
}
88
102
@@ -164,7 +178,7 @@ impl DriaComputeNode {
164
178
return MessageAcceptance :: Ignore ;
165
179
}
166
180
167
- // first, parse the raw gossipsub message to a prepared message
181
+ // parse the raw gossipsub message to a prepared DKN message
168
182
let message = match DKNMessage :: try_from_gossipsub_message (
169
183
& message,
170
184
& self . config . admin_public_key ,
@@ -177,19 +191,25 @@ impl DriaComputeNode {
177
191
}
178
192
} ;
179
193
180
- // then handle the prepared message
194
+ // handle the DKN message with respect to the topic
181
195
let handler_result = match message. topic . as_str ( ) {
182
196
WorkflowHandler :: LISTEN_TOPIC => {
183
197
match WorkflowHandler :: handle_compute ( self , & message) . await {
198
+ // we got acceptance, so something was not right about the workflow and we can ignore it
184
199
Ok ( Either :: Left ( acceptance) ) => Ok ( acceptance) ,
185
- Ok ( Either :: Right ( workflow_message) ) => {
186
- if let Err ( e) = self . worklow_tx . send ( workflow_message) . await {
200
+ // we got the parsed workflow itself, send to a worker thread w.r.t batchable
201
+ Ok ( Either :: Right ( ( workflow_message, batchable) ) ) => {
202
+ if let Err ( e) = match batchable {
203
+ true => self . workflow_batch_tx . send ( workflow_message) . await ,
204
+ false => self . workflow_single_tx . send ( workflow_message) . await ,
205
+ } {
187
206
log:: error!( "Error sending workflow message: {:?}" , e) ;
188
207
} ;
189
208
190
209
// accept the message in case others may be included in the filter as well
191
210
Ok ( MessageAcceptance :: Accept )
192
211
}
212
+ // something went wrong, handle this outside
193
213
Err ( err) => Err ( err) ,
194
214
}
195
215
}
@@ -241,7 +261,16 @@ impl DriaComputeNode {
241
261
_ = tokio:: time:: sleep( available_node_refresh_duration) => self . handle_available_nodes_refresh( ) . await ,
242
262
// a Workflow message to be published is received from the channel
243
263
// this is expected to be sent by the workflow worker
244
- publish_msg = self . publish_rx. recv( ) => {
264
+ publish_msg = self . publish_batch_rx. recv( ) => {
265
+ if let Some ( result) = publish_msg {
266
+ WorkflowHandler :: handle_publish( self , result) . await ?;
267
+ } else {
268
+ log:: error!( "Publish channel closed unexpectedly." ) ;
269
+ break ;
270
+ } ;
271
+ } ,
272
+ // TODO: make the both receivers handled together somehow
273
+ publish_msg = self . publish_single_rx. recv( ) => {
245
274
if let Some ( result) = publish_msg {
246
275
WorkflowHandler :: handle_publish( self , result) . await ?;
247
276
} else {
@@ -293,7 +322,7 @@ impl DriaComputeNode {
293
322
self . message_rx . close ( ) ;
294
323
295
324
log:: debug!( "Closing publish channel." ) ;
296
- self . publish_rx . close ( ) ;
325
+ self . publish_batch_rx . close ( ) ;
297
326
298
327
Ok ( ( ) )
299
328
}
@@ -339,7 +368,7 @@ mod tests {
339
368
340
369
// create node
341
370
let cancellation = CancellationToken :: new ( ) ;
342
- let ( mut node, p2p, _) =
371
+ let ( mut node, p2p, _, _ ) =
343
372
DriaComputeNode :: new ( DriaComputeNodeConfig :: default ( ) , cancellation. clone ( ) )
344
373
. await
345
374
. expect ( "should create node" ) ;
0 commit comments