hadoop_common/ipc/
rpc.rs

1use super::{AlignmentContext, Invoker, ProtobufRpcEngine2, ProtocolInfo, RpcEngine};
2use crate::{conf::Configuration, io::retry::RetryPolicy, security::UserGroupInformation};
3use atomic::Atomic;
4use std::{net::SocketAddr, rc::Rc, sync::Arc};
5
6#[derive(Clone)]
7#[repr(u8)]
8pub enum RpcKind {
9    // Used for built in calls by tests
10    RpcBuiltin,
11    // Use WritableRpcEngine
12    RpcWritable,
13    // Use ProtobufRpcEngine
14    RpcProtocolBuffer,
15}
16
17pub trait RpcProtocol<T: RpcProtocol = Self> {
18    fn get_protocol_info() -> &'static ProtocolInfo;
19
20    fn from(invoker: Invoker<T>) -> Self;
21}
22
23/// A simple RPC mechanism.
24pub struct RPC;
25
26impl RPC {
27    pub const RPC_SERVICE_CLASS_DEFAULT: u8 = 0;
28
29    /// Get the protocol name.
30    pub fn get_protocol_name<T: RpcProtocol>() -> &'static str {
31        T::get_protocol_info().protocol_name
32    }
33
34    /// Get the protocol version from protocol class.
35    pub fn get_protocol_version<T: RpcProtocol>() -> u64 {
36        T::get_protocol_info().protocol_version
37    }
38
39    /// return the RpcEngine configured to handle a protocol
40    fn get_protocol_engine<T: RpcProtocol>(_conf: &Configuration) -> impl RpcEngine {
41        // TODO: get or create an RPC Engine in cache for Protocol `T`
42
43        ProtobufRpcEngine2
44    }
45
46    /// Get a protocol proxy that contains a proxy connection to a remote server
47    /// and a set of methods that are supported by the server.
48    pub fn get_protocol_proxy<T: RpcProtocol>(
49        addr: &SocketAddr,
50        ticket: &UserGroupInformation,
51        conf: &Configuration,
52        rpc_timeout: i32,
53        connection_retry_policy: Option<Rc<dyn RetryPolicy>>,
54        fallback_to_simple_auth: Option<Arc<Atomic<bool>>>,
55        alignment_context: Option<Rc<dyn AlignmentContext>>,
56    ) -> anyhow::Result<T> {
57        // TODO: init SaslRpcServer if needed
58
59        Self::get_protocol_engine::<T>(conf).get_proxy(
60            addr,
61            ticket,
62            conf,
63            rpc_timeout,
64            connection_retry_policy,
65            fallback_to_simple_auth,
66            alignment_context,
67        )
68    }
69}