spark 2.0 RpcHandler

本文介绍了RPC回调机制的核心组件RpcResponseCallback及其实现OneWayRpcCallback。详细解析了onSuccess和onFailure方法的作用,以及如何处理从服务器返回的成功结果或异常信息。同时涵盖了RpcHandler的基本职责,包括接收RPC消息、处理异常及维护流状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PpcHandler用到RpcResponseCallback,RpcResponseCallback是RPC结果的回调函数,有onSuccess和onFailure两个方法。

onSuccess方法当成功从服务器序列化结果时调用。当onSuccess调用返回时,参数ByteBuffer response会被回收,它的内容会变得不再有效。如果在onSucess方法返回之后还要使用ByteBuffer的内容,请把ByteBuffer的内容拷贝一份。

onFailure传递从服务端传过来的异常信息,或者客户端抛出的异常信息。

/**
 * Callback for the result of a single RPC. This will be invoked once with either success or
 * failure.
 */
public interface RpcResponseCallback {
  /**
   * Successful serialized result from server.
   *
   * After `onSuccess` returns, `response` will be recycled and its content will become invalid.
   * Please copy the content of `response` if you want to use it after `onSuccess` returns.
   */
  void onSuccess(ByteBuffer response);

  /** Exception either propagated from server or raised on client side. */
  void onFailure(Throwable e);
}


OneWayRpcCallBack实现了OneWayRpcCallback,在onSuccess和onFailure方法中,在log中打印一条警告日志。


private static class OneWayRpcCallback implements RpcResponseCallback {

    private final Logger logger = LoggerFactory.getLogger(OneWayRpcCallback.class);

    @Override
    public void onSuccess(ByteBuffer response) {
      logger.warn("Response provided for one-way RPC.");
    }

    @Override
    public void onFailure(Throwable e) {
      logger.error("Error response provided for one-way RPC.", e);
    }

  }

RpcResponseCallback 处理从TransportClient 调用sendRPC()发送过来的信息。

onReceive方法:接收一个RPC信息。当发生异常时,这个方法中的异常信息会以一个字符串的格式被发送回客户端。一个TransportClient对象中,这个方法不能被多线程调用。

getStreamManager方法:返回StreamMaager,这个StreamManager包含哪个流正在被TransportClient读的状态信息。

channelActive():当客户端所在的通道活动时调用。

channelInactive(): 当客户端所在的通道不活动时调用。

excpetionCautht(): 异常信息

/**
 * Handler for sendRPC() messages sent by {@link org.apache.spark.network.client.TransportClient}s.
 */
public abstract class RpcHandler {

  private static final RpcResponseCallback ONE_WAY_CALLBACK = new OneWayRpcCallback();

  /**
   * Receive a single RPC message. Any exception thrown while in this method will be sent back to
   * the client in string form as a standard RPC failure.
   *
   * This method will not be called in parallel for a single TransportClient (i.e., channel).
   *
   * @param client A channel client which enables the handler to make requests back to the sender
   *               of this RPC. This will always be the exact same object for a particular channel.
   * @param message The serialized bytes of the RPC.
   * @param callback Callback which should be invoked exactly once upon success or failure of the
   *                 RPC.
   */
  public abstract void receive(
      TransportClient client,
      ByteBuffer message,
      RpcResponseCallback callback);

  /**
   * Returns the StreamManager which contains the state about which streams are currently being
   * fetched by a TransportClient.
   */
  public abstract StreamManager getStreamManager();

  /**
   * Receives an RPC message that does not expect a reply. The default implementation will
   * call "{@link #receive(TransportClient, ByteBuffer, RpcResponseCallback)}" and log a warning if
   * any of the callback methods are called.
   *
   * @param client A channel client which enables the handler to make requests back to the sender
   *               of this RPC. This will always be the exact same object for a particular channel.
   * @param message The serialized bytes of the RPC.
   */
  public void receive(TransportClient client, ByteBuffer message) {
    receive(client, message, ONE_WAY_CALLBACK);
  }

  /**
   * Invoked when the channel associated with the given client is active.
   */
  public void channelActive(TransportClient client) { }

  /**
   * Invoked when the channel associated with the given client is inactive.
   * No further requests will come from this client.
   */
  public void channelInactive(TransportClient client) { }

  public void exceptionCaught(Throwable cause, TransportClient client) { }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值