client.rs 8.02 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
Ryan Olson's avatar
Ryan Olson committed
15
16
17
18

use std::sync::Arc;

use futures::{SinkExt, StreamExt};
19
use tokio::io::{ReadHalf, WriteHalf};
Ryan Olson's avatar
Ryan Olson committed
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
use tokio::{io::AsyncWriteExt, net::TcpStream};
use tokio_util::codec::{FramedRead, FramedWrite};

use super::{CallHomeHandshake, ControlMessage, TcpStreamConnectionInfo};
use crate::engine::AsyncEngineContext;
use crate::pipeline::network::{
    codec::{TwoPartCodec, TwoPartMessage},
    tcp::StreamType,
    ConnectionInfo, ResponseStreamPrologue, StreamSender,
}; // Import SinkExt to use the `send` method

#[allow(dead_code)]
pub struct TcpClient {
    worker_id: String,
}

impl Default for TcpClient {
    fn default() -> Self {
        TcpClient {
            worker_id: uuid::Uuid::new_v4().to_string(),
        }
    }
}

impl TcpClient {
    pub fn new(worker_id: String) -> Self {
        TcpClient { worker_id }
    }

    async fn connect(address: &str) -> Result<TcpStream, String> {
        let socket = TcpStream::connect(address)
            .await
            .map_err(|e| format!("failed to connect: {:?}", e))?;

        socket
            .set_nodelay(true)
            .map_err(|e| format!("failed to set nodelay: {:?}", e))?;

        Ok(socket)
    }

    pub async fn create_response_steam(
        context: Arc<dyn AsyncEngineContext>,
        info: ConnectionInfo,
    ) -> Result<StreamSender, String> {
        let info = TcpStreamConnectionInfo::try_from(info)?;
        tracing::trace!("Creating response stream for {:?}", info);

        if info.stream_type != StreamType::Response {
            return Err(format!(
                "Invalid stream type; TcpClient requires the stream type to be `response`; however {:?} was passed",
                info.stream_type
            ));
        }

        if info.context != context.id() {
            return Err(format!(
                "Invalid context; TcpClient requires the context to be {:?}; however {:?} was passed",
                context.id(),
                info.context
            ));
        }

        let stream = TcpClient::connect(&info.address).await?;
        let (read_half, write_half) = tokio::io::split(stream);

86
        let framed_reader = FramedRead::new(read_half, TwoPartCodec::default());
Ryan Olson's avatar
Ryan Olson committed
87
88
89
90
91
92
93
        let mut framed_writer = FramedWrite::new(write_half, TwoPartCodec::default());

        // this is a oneshot channel that will be used to signal when the stream is closed
        // when the stream sender is dropped, the bytes_rx will be closed and the forwarder task will exit
        // the forwarder task will capture the alive_rx half of the oneshot channel; this will close the alive channel
        // so the holder of the alive_tx half will be notified that the stream is closed; the alive_tx channel will be
        // captured by the monitor task
94
95
96
        let (alive_tx, alive_rx) = tokio::sync::oneshot::channel::<()>();

        tokio::spawn(control_handler(framed_reader, context, alive_tx));
Ryan Olson's avatar
Ryan Olson committed
97
98
99
100
101
102
103

        // transport specific handshake message
        let handshake = CallHomeHandshake {
            subject: info.subject,
            stream_type: StreamType::Response,
        };

104
105
106
107
108
109
110
111
        let handshake_bytes = match serde_json::to_vec(&handshake) {
            Ok(hb) => hb,
            Err(err) => {
                return Err(format!(
                    "create_response_steam: Error converting CallHomeHandshake to JSON array: {err:#}"
                ));
            }
        };
Ryan Olson's avatar
Ryan Olson committed
112
113
114
115
116
117
118
119
120
        let msg = TwoPartMessage::from_header(handshake_bytes.into());

        // issue the the first tcp handshake message
        framed_writer
            .send(msg)
            .await
            .map_err(|e| format!("failed to send handshake: {:?}", e))?;

        // set up the channel to send bytes to the transport layer
121
        let (bytes_tx, bytes_rx) = tokio::sync::mpsc::channel(16);
Ryan Olson's avatar
Ryan Olson committed
122
123

        // forwards the bytes send from this stream to the transport layer; hold the alive_rx half of the oneshot channel
124
        tokio::spawn(forward_handler(bytes_rx, framed_writer, alive_rx));
Ryan Olson's avatar
Ryan Olson committed
125
126
127
128
129
130
131
132
133
134
135
136
137
138

        // set up the prologue for the stream
        // this might have transport specific metadata in the future
        let prologue = Some(ResponseStreamPrologue { error: None });

        // create the stream sender
        let stream_sender = StreamSender {
            tx: bytes_tx,
            prologue,
        };

        Ok(stream_sender)
    }
}
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215

/// monitors the channel for a cancellation signal
/// this task exits when the alive_rx half of the oneshot channel is closed or a stop/kill signal is received
async fn control_handler(
    mut framed_reader: FramedRead<ReadHalf<TcpStream>, TwoPartCodec>,
    context: Arc<dyn AsyncEngineContext>,
    mut alive_tx: tokio::sync::oneshot::Sender<()>,
) {
    loop {
        tokio::select! {
            msg = framed_reader.next() => {
                match msg {
                    Some(Ok(two_part_msg)) => {
                        match two_part_msg.optional_parts() {
                           (Some(bytes), None) => {
                                let msg: ControlMessage = match serde_json::from_slice(bytes) {
                                    Ok(msg) => msg,
                                    Err(err) => {
                                        let json_str = String::from_utf8_lossy(bytes);
                                        tracing::error!(%err, %json_str, "control_handler fatal error deserializing JSON to ControlMessage");
                                        context.kill();
                                        break;
                                    }
                                };
                                match msg {
                                    ControlMessage::Stop => {
                                        context.stop();
                                        break;
                                    }
                                    ControlMessage::Kill => {
                                        context.kill();
                                        break;
                                    }
                                }
                           }
                           _ => {
                               // we should not receive this
                           }
                        }
                    }
                    Some(Err(e)) => {
                        panic!("failed to decode message from stream: {:?}", e);
                        // break;
                    }
                    None => {
                        // the stream was closed, we should stop the stream
                        return;
                    }
                }
            }
            _ = alive_tx.closed() => {
                // the channel was closed, we should stop the stream
                break;
            }
        }
    }
    // framed_writer.get_mut().shutdown().await.unwrap();
}

async fn forward_handler(
    mut bytes_rx: tokio::sync::mpsc::Receiver<TwoPartMessage>,
    mut framed_writer: FramedWrite<WriteHalf<TcpStream>, TwoPartCodec>,
    alive_rx: tokio::sync::oneshot::Receiver<()>,
) {
    while let Some(msg) = bytes_rx.recv().await {
        if let Err(e) = framed_writer.send(msg).await {
            tracing::trace!(%e, "failed to send message to stream; possible disconnect");

            // TODO - possibly propagate the error upstream
            break;
        }
    }
    drop(alive_rx);
    if let Err(e) = framed_writer.get_mut().shutdown().await {
        tracing::trace!("failed to shutdown writer: {:?}", e);
    }
}