1
- use std:: time:: Duration ;
2
-
3
1
use dkn_p2p:: libp2p:: { request_response:: OutboundRequestId , PeerId } ;
4
- use dkn_workflows:: { Model , ModelProvider } ;
5
2
use eyre:: { eyre, Result } ;
3
+ use serde:: { Deserialize , Serialize } ;
4
+ use std:: time:: Duration ;
6
5
use uuid:: Uuid ;
7
6
8
7
use super :: IsResponder ;
9
- use serde:: { Deserialize , Serialize } ;
10
8
11
- use crate :: DriaComputeNode ;
9
+ use crate :: { utils :: DriaMessage , DriaComputeNode } ;
12
10
13
11
pub struct HeartbeatRequester ;
14
12
@@ -18,8 +16,8 @@ pub struct HeartbeatRequest {
18
16
pub ( crate ) heartbeat_id : Uuid ,
19
17
/// Deadline for the heartbeat request, in nanoseconds.
20
18
pub ( crate ) deadline : chrono:: DateTime < chrono:: Utc > ,
21
- /// Models available in the node.
22
- pub ( crate ) models : Vec < ( ModelProvider , Model ) > ,
19
+ /// Model names available in the node.
20
+ pub ( crate ) models : Vec < String > ,
23
21
/// Number of tasks in the channel currently, `single` and `batch`.
24
22
pub ( crate ) pending_tasks : [ usize ; 2 ] ,
25
23
}
@@ -39,13 +37,16 @@ pub struct HeartbeatResponse {
39
37
}
40
38
41
39
impl IsResponder for HeartbeatRequester {
42
- type Request = HeartbeatRequest ;
43
- type Response = HeartbeatResponse ;
40
+ type Request = DriaMessage ; // TODO: HeartbeatRequest;
41
+ type Response = DriaMessage ; // TODO: HeartbeatResponse;
44
42
}
45
43
46
44
/// Any acknowledged heartbeat that is older than this duration is considered dead.
47
45
const HEARTBEAT_DEADLINE_SECS : Duration = Duration :: from_secs ( 20 ) ;
48
46
47
+ /// Topic for the [`DriaMessage`].
48
+ const HEARTBEAT_TOPIC : & str = "heartbeat" ;
49
+
49
50
impl HeartbeatRequester {
50
51
pub ( crate ) async fn send_heartbeat (
51
52
node : & mut DriaComputeNode ,
@@ -57,16 +58,23 @@ impl HeartbeatRequester {
57
58
let heartbeat_request = HeartbeatRequest {
58
59
heartbeat_id : uuid,
59
60
deadline,
60
- models : node. config . workflows . models . clone ( ) ,
61
+ models : node
62
+ . config
63
+ . workflows
64
+ . models
65
+ . iter ( )
66
+ . map ( |m| m. 1 . to_string ( ) )
67
+ . collect ( ) ,
61
68
pending_tasks : node. get_pending_task_count ( ) ,
62
69
} ;
63
70
71
+ let heartbeat_message = node. new_message (
72
+ serde_json:: to_vec ( & heartbeat_request) . expect ( "should be serializable" ) ,
73
+ HEARTBEAT_TOPIC ,
74
+ ) ;
64
75
let request_id = node
65
76
. p2p
66
- . request (
67
- peer_id,
68
- serde_json:: to_vec ( & heartbeat_request) . expect ( "should be serializable" ) ,
69
- )
77
+ . request ( peer_id, heartbeat_message. to_bytes ( ) ?)
70
78
. await ?;
71
79
72
80
// add it to local heartbeats set
@@ -78,8 +86,10 @@ impl HeartbeatRequester {
78
86
/// Handles the heartbeat request received from the network.
79
87
pub ( crate ) async fn handle_ack (
80
88
node : & mut DriaComputeNode ,
81
- res : HeartbeatResponse ,
89
+ ack_message : DriaMessage ,
82
90
) -> Result < ( ) > {
91
+ let res = ack_message. parse_payload :: < HeartbeatResponse > ( ) ?;
92
+
83
93
if let Some ( deadline) = node. heartbeats . remove ( & res. heartbeat_id ) {
84
94
if let Some ( err) = res. error {
85
95
Err ( eyre ! ( "Heartbeat was not acknowledged: {}" , err) )
0 commit comments