-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move NodeSvcHandle into core networking
- Loading branch information
1 parent
ed92d66
commit babed1a
Showing
7 changed files
with
161 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (c) 2023 - 2025 Restate Software, Inc., Restate GmbH. | ||
// All rights reserved. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the LICENSE file. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0. | ||
|
||
use futures::Stream; | ||
use tokio_stream::StreamExt; | ||
|
||
use restate_types::GenerationalNodeId; | ||
use restate_types::config::NetworkingOptions; | ||
use restate_types::nodes_config::NodesConfiguration; | ||
use restate_types::protobuf::node::Message; | ||
use tracing::trace; | ||
|
||
use super::MAX_MESSAGE_SIZE; | ||
use crate::network::net_util::create_tonic_channel; | ||
use crate::network::protobuf::core_node_svc::core_node_svc_client::CoreNodeSvcClient; | ||
use crate::network::{NetworkError, ProtocolError, TransportConnect}; | ||
|
||
pub struct GrpcConnector { | ||
networking_options: NetworkingOptions, | ||
} | ||
|
||
impl GrpcConnector { | ||
pub fn new(networking_options: NetworkingOptions) -> Self { | ||
Self { networking_options } | ||
} | ||
} | ||
|
||
impl TransportConnect for GrpcConnector { | ||
async fn connect( | ||
&self, | ||
node_id: GenerationalNodeId, | ||
nodes_config: &NodesConfiguration, | ||
output_stream: impl Stream<Item = Message> + Send + Unpin + 'static, | ||
) -> Result< | ||
impl Stream<Item = Result<Message, ProtocolError>> + Send + Unpin + 'static, | ||
NetworkError, | ||
> { | ||
let address = nodes_config.find_node_by_id(node_id)?.address.clone(); | ||
|
||
trace!("Attempting to connect to node {} at {}", node_id, address); | ||
let channel = create_tonic_channel(address, &self.networking_options); | ||
|
||
// Establish the connection | ||
let mut client = CoreNodeSvcClient::new(channel) | ||
.max_decoding_message_size(MAX_MESSAGE_SIZE) | ||
.max_decoding_message_size(MAX_MESSAGE_SIZE); | ||
let incoming = client.create_connection(output_stream).await?.into_inner(); | ||
Ok(incoming.map(|x| x.map_err(ProtocolError::from))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright (c) 2023 - 2025 Restate Software, Inc., Restate GmbH. | ||
// All rights reserved. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the LICENSE file. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0. | ||
|
||
mod connector; | ||
mod svc_handler; | ||
|
||
pub use connector::GrpcConnector; | ||
pub use svc_handler::CoreNodeSvcHandler; | ||
|
||
/// The maximum size for a grpc message for core networking service. | ||
/// This impacts the buffer limit for prost codec. | ||
const MAX_MESSAGE_SIZE: usize = 32 * 1024 * 1024; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright (c) 2023 - 2025 Restate Software, Inc., Restate GmbH. | ||
// All rights reserved. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the LICENSE file. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0. | ||
|
||
use futures::stream::BoxStream; | ||
use tokio_stream::StreamExt; | ||
use tonic::codec::CompressionEncoding; | ||
use tonic::{Request, Response, Status, Streaming}; | ||
|
||
use crate::network::protobuf::core_node_svc::core_node_svc_server::{ | ||
CoreNodeSvc, CoreNodeSvcServer, | ||
}; | ||
use crate::network::{ConnectionManager, ProtocolError, TransportConnect}; | ||
use restate_types::protobuf::node::Message; | ||
|
||
use super::MAX_MESSAGE_SIZE; | ||
|
||
pub struct CoreNodeSvcHandler<T> { | ||
connections: ConnectionManager<T>, | ||
} | ||
|
||
impl<T> CoreNodeSvcHandler<T> { | ||
pub fn new(connections: ConnectionManager<T>) -> Self { | ||
Self { connections } | ||
} | ||
|
||
pub fn into_server(self) -> CoreNodeSvcServer<Self> { | ||
CoreNodeSvcServer::new(self) | ||
.max_decoding_message_size(MAX_MESSAGE_SIZE) | ||
.max_encoding_message_size(MAX_MESSAGE_SIZE) | ||
.accept_compressed(CompressionEncoding::Gzip) | ||
.send_compressed(CompressionEncoding::Gzip) | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl<T> CoreNodeSvc for CoreNodeSvcHandler<T> | ||
where | ||
T: TransportConnect, | ||
{ | ||
type CreateConnectionStream = BoxStream<'static, Result<Message, Status>>; | ||
|
||
// Status codes returned in different scenarios: | ||
// - DeadlineExceeded: No hello received within deadline | ||
// - InvalidArgument: Header should always be set or any other missing required part of the | ||
// handshake. This also happens if the client sent wrong message on handshake. | ||
// - AlreadyExists: A node with a newer generation has been observed already | ||
// - Cancelled: received an error from the client, or the client has dropped the stream during | ||
// handshake. | ||
// - Unavailable: This node is shutting down | ||
async fn create_connection( | ||
&self, | ||
request: Request<Streaming<Message>>, | ||
) -> Result<Response<Self::CreateConnectionStream>, Status> { | ||
let incoming = request.into_inner(); | ||
let transformed = incoming.map(|x| x.map_err(ProtocolError::from)); | ||
let output_stream = self | ||
.connections | ||
.accept_incoming_connection(transformed) | ||
.await?; | ||
|
||
// For uniformity with outbound connections, we map all responses to Ok, we never rely on | ||
// sending tonic::Status errors explicitly. We use ConnectionControl frames to communicate | ||
// errors and/or drop the stream when necessary. | ||
Ok(Response::new(Box::pin(output_stream.map(Ok)))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters