Search in sources :

Example 1 with RpcResponse

use of com.jd.blockchain.consensus.raft.rpc.RpcResponse in project jdchain-core by blockchain-jd-com.

the class RaftMessageService method queryPeersManagerInfo.

private List<MonitorNodeNetwork> queryPeersManagerInfo(List<PeerId> peerIds) throws InterruptedException {
    if (!isReloadMonitorNode) {
        return monitorNodeNetworks;
    }
    synchronized (monitorLock) {
        if (!isReloadMonitorNode) {
            return monitorNodeNetworks;
        }
        final List<MonitorNodeNetwork> nodeNetworkList = Collections.synchronizedList(new ArrayList<>(peerIds.size()));
        CountDownLatch countDownLatch = new CountDownLatch(peerIds.size());
        QueryManagerInfoRequest infoRequest = new QueryManagerInfoRequest();
        for (PeerId peerId : peerIds) {
            try {
                clientService.getRpcClient().invokeAsync(peerId.getEndpoint(), infoRequest, (o, e) -> {
                    try {
                        if (e != null) {
                            LoggerUtils.errorIfEnabled(LOGGER, "queryPeersManagerInfo response error, peer id: {}", peerId, e);
                        } else {
                            RpcResponse response = (RpcResponse) o;
                            QueryManagerInfoRequestProcessor.ManagerInfoResponse managerInfoResponse = QueryManagerInfoRequestProcessor.ManagerInfoResponse.fromBytes(response.getResult());
                            MonitorNodeNetwork monitorNodeNetwork = new MonitorNodeNetwork(managerInfoResponse.getHost(), managerInfoResponse.getConsensusPort(), managerInfoResponse.getManagerPort(), managerInfoResponse.isConsensusSSLEnabled(), managerInfoResponse.isManagerSSLEnabled());
                            nodeNetworkList.add(monitorNodeNetwork);
                        }
                    } catch (Exception exception) {
                        LoggerUtils.errorIfEnabled(LOGGER, "handle queryPeersManagerInfo response error", e);
                    } finally {
                        countDownLatch.countDown();
                    }
                }, this.rpcTimeoutMs);
            } catch (Exception e) {
                LOGGER.error("queryPeersManagerInfo error", e);
                countDownLatch.countDown();
            }
        }
        countDownLatch.await(this.rpcTimeoutMs * peerIds.size(), TimeUnit.MILLISECONDS);
        monitorNodeNetworks.clear();
        monitorNodeNetworks.addAll(nodeNetworkList);
        isReloadMonitorNode = false;
    }
    return monitorNodeNetworks;
}
Also used : QueryManagerInfoRequest(com.jd.blockchain.consensus.raft.rpc.QueryManagerInfoRequest) QueryManagerInfoRequestProcessor(com.jd.blockchain.consensus.raft.rpc.QueryManagerInfoRequestProcessor) RpcResponse(com.jd.blockchain.consensus.raft.rpc.RpcResponse) RemotingException(com.alipay.sofa.jraft.error.RemotingException) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 2 with RpcResponse

use of com.jd.blockchain.consensus.raft.rpc.RpcResponse in project jdchain-core by blockchain-jd-com.

the class RaftMessageService method sendRequest.

private void sendRequest(Endpoint endpoint, SubmitTxRequest txRequest, int retry, CompletableAsyncFuture<byte[]> asyncFuture) throws RemotingException, InterruptedException {
    if (retry >= MAX_RETRY_TIMES) {
        asyncFuture.error(new RuntimeException("raft client send request exceed max retries"));
        return;
    }
    if (endpoint == null) {
        asyncFuture.error(new RuntimeException("raft client send request find leader endpoint is null"));
        return;
    }
    clientService.getRpcClient().invokeAsync(endpoint, txRequest, (o, throwable) -> {
        LoggerUtils.debugIfEnabled(LOGGER, "raft client send request: {} response: {} throwable: {}", txRequest, o, throwable);
        if (throwable != null) {
            LOGGER.error("raft client send request error, request: {}", txRequest, throwable);
            asyncFuture.error(throwable);
            return;
        }
        RpcResponse response = (RpcResponse) o;
        if (response.isRedirect()) {
            LoggerUtils.debugIfEnabled(LOGGER, "request should redirect to leader. current peer: {} , redirect leader: {}", endpoint, response.getLeaderEndpoint());
            try {
                sendRequest(JRaftUtils.getEndPoint(response.getLeaderEndpoint()), txRequest, retry + 1, asyncFuture);
            } catch (Exception e) {
                throw new RaftClientRequestException(e);
            }
            return;
        }
        if (response.isSuccess()) {
            asyncFuture.complete(response.getResult());
        } else {
            asyncFuture.complete(null);
        }
    }, rpcTimeoutMs);
}
Also used : RpcResponse(com.jd.blockchain.consensus.raft.rpc.RpcResponse) RemotingException(com.alipay.sofa.jraft.error.RemotingException)

Example 3 with RpcResponse

use of com.jd.blockchain.consensus.raft.rpc.RpcResponse in project jdchain-core by blockchain-jd-com.

the class BlockSyncService method getConsensusNodeManagerInfo.

private ServiceEndpoint getConsensusNodeManagerInfo(Endpoint remoteEndpoint) {
    try {
        QueryManagerInfoRequest request = new QueryManagerInfoRequest();
        RpcResponse response = (RpcResponse) rpcClient.invokeSync(remoteEndpoint, request, requestTimeoutMs);
        QueryManagerInfoRequestProcessor.ManagerInfoResponse infoResponse = QueryManagerInfoRequestProcessor.ManagerInfoResponse.fromBytes(response.getResult());
        return new ServiceEndpoint(remoteEndpoint.getIp(), infoResponse.getManagerPort(), infoResponse.isManagerSSLEnabled());
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return null;
}
Also used : QueryManagerInfoRequest(com.jd.blockchain.consensus.raft.rpc.QueryManagerInfoRequest) QueryManagerInfoRequestProcessor(com.jd.blockchain.consensus.raft.rpc.QueryManagerInfoRequestProcessor) RpcResponse(com.jd.blockchain.consensus.raft.rpc.RpcResponse) ServiceEndpoint(com.jd.httpservice.agent.ServiceEndpoint) BlockSyncException(com.jd.blockchain.consensus.raft.consensus.BlockSyncException)

Aggregations

RpcResponse (com.jd.blockchain.consensus.raft.rpc.RpcResponse)3 RemotingException (com.alipay.sofa.jraft.error.RemotingException)2 QueryManagerInfoRequest (com.jd.blockchain.consensus.raft.rpc.QueryManagerInfoRequest)2 QueryManagerInfoRequestProcessor (com.jd.blockchain.consensus.raft.rpc.QueryManagerInfoRequestProcessor)2 PeerId (com.alipay.sofa.jraft.entity.PeerId)1 BlockSyncException (com.jd.blockchain.consensus.raft.consensus.BlockSyncException)1 ServiceEndpoint (com.jd.httpservice.agent.ServiceEndpoint)1