Search in sources :

Example 1 with RemotingException

use of io.dingodb.raft.error.RemotingException in project dingo by dingodb.

the class AbstractClientService method connect.

@Override
public boolean connect(final Endpoint endpoint) {
    final RpcClient rc = this.rpcClient;
    if (rc == null) {
        throw new IllegalStateException("Client service is uninitialized.");
    }
    if (isConnected(rc, endpoint)) {
        return true;
    }
    try {
        final RpcRequests.PingRequest req = // 
        RpcRequests.PingRequest.newBuilder().setSendTimestamp(// 
        System.currentTimeMillis()).build();
        final RpcRequests.ErrorResponse resp = (RpcRequests.ErrorResponse) rc.invokeSync(endpoint, req, this.rpcOptions.getRpcConnectTimeoutMs());
        return resp.getErrorCode() == 0;
    } catch (final InterruptedException e) {
        Thread.currentThread().interrupt();
        return false;
    } catch (final RemotingException e) {
        LOG.error("Fail to connect {}, remoting exception: {}.", endpoint, e.getMessage());
        return false;
    }
}
Also used : RemotingException(io.dingodb.raft.error.RemotingException) RpcRequests(io.dingodb.raft.rpc.RpcRequests) RpcClient(io.dingodb.raft.rpc.RpcClient)

Example 2 with RemotingException

use of io.dingodb.raft.error.RemotingException in project dingo by dingodb.

the class CoordinatorStateMachine method getLeaderLocation.

private void getLeaderLocation(Channel channel) {
    try {
        GetLocationResponse res = (GetLocationResponse) rpcClient.invokeSync(context.node().getLeaderId().getEndpoint(), GetLocationRequest.INSTANCE, 3000);
        channel.send(SimpleMessage.builder().content(encodeHostPort(res.getHost(), res.getPort())).build());
    } catch (IOException e) {
        log.error("Serialize location error", e);
        channel.send(EmptyMessage.INSTANCE);
    } catch (RemotingException e) {
        log.error("Get leader peer location error", e);
        channel.send(EmptyMessage.INSTANCE);
    } catch (InterruptedException e) {
        log.error("Get leader location interrupt.", e);
        channel.send(EmptyMessage.INSTANCE);
    } catch (Exception e) {
        log.error("Get leader location error.", e);
        channel.send(EmptyMessage.INSTANCE);
    }
}
Also used : RemotingException(io.dingodb.raft.error.RemotingException) IOException(java.io.IOException) GetLocationResponse(io.dingodb.server.coordinator.handler.GetLocationHandler.GetLocationResponse) StoreCodecException(io.dingodb.store.row.errors.StoreCodecException) RemotingException(io.dingodb.raft.error.RemotingException) IOException(java.io.IOException) IllegalRowStoreOperationException(io.dingodb.store.row.errors.IllegalRowStoreOperationException)

Example 3 with RemotingException

use of io.dingodb.raft.error.RemotingException in project dingo by dingodb.

the class AbstractClientService method invokeWithDone.

public <T extends Message> Future<Message> invokeWithDone(final Endpoint endpoint, final Message request, final InvokeContext ctx, final RpcResponseClosure<T> done, final int timeoutMs, final Executor rpcExecutor) {
    final RpcClient rc = this.rpcClient;
    final FutureImpl<Message> future = new FutureImpl<>();
    final Executor currExecutor = rpcExecutor != null ? rpcExecutor : this.rpcExecutor;
    try {
        if (rc == null) {
            future.failure(new IllegalStateException("Client service is uninitialized."));
            // should be in another thread to avoid dead locking.
            RpcUtils.runClosureInExecutor(currExecutor, done, new Status(RaftError.EINTERNAL, "Client service is uninitialized."));
            return future;
        }
        rc.invokeAsync(endpoint, request, ctx, new InvokeCallback() {

            @SuppressWarnings({ "unchecked", "ConstantConditions" })
            @Override
            public void complete(final Object result, final Throwable err) {
                if (future.isCancelled()) {
                    onCanceled(request, done);
                    return;
                }
                if (err == null) {
                    Status status = Status.OK();
                    Message msg;
                    if (result instanceof RpcRequests.ErrorResponse) {
                        status = handleErrorResponse((RpcRequests.ErrorResponse) result);
                        msg = (Message) result;
                    } else if (result instanceof Message) {
                        final Descriptors.FieldDescriptor fd = // 
                        ((Message) result).getDescriptorForType().findFieldByNumber(RpcResponseFactory.ERROR_RESPONSE_NUM);
                        if (fd != null && ((Message) result).hasField(fd)) {
                            final RpcRequests.ErrorResponse eResp = (RpcRequests.ErrorResponse) ((Message) result).getField(fd);
                            status = handleErrorResponse(eResp);
                            msg = eResp;
                        } else {
                            msg = (T) result;
                        }
                    } else {
                        msg = (T) result;
                    }
                    if (done != null) {
                        try {
                            if (status.isOk()) {
                                done.setResponse((T) msg);
                            }
                            done.run(status);
                        } catch (final Throwable t) {
                            LOG.error("Fail to run RpcResponseClosure, the request is {}.", request, t);
                        }
                    }
                    if (!future.isDone()) {
                        future.setResult(msg);
                    }
                } else {
                    if (done != null) {
                        try {
                            done.run(new Status(err instanceof InvokeTimeoutException ? RaftError.ETIMEDOUT : RaftError.EINTERNAL, "RPC exception:" + err.getMessage()));
                        } catch (final Throwable t) {
                            LOG.error("Fail to run RpcResponseClosure, the request is {}.", request, t);
                        }
                    }
                    if (!future.isDone()) {
                        future.failure(err);
                    }
                }
            }

            @Override
            public Executor executor() {
                return currExecutor;
            }
        }, timeoutMs <= 0 ? this.rpcOptions.getRpcDefaultTimeout() : timeoutMs);
    } catch (final InterruptedException e) {
        Thread.currentThread().interrupt();
        future.failure(e);
        // should be in another thread to avoid dead locking.
        RpcUtils.runClosureInExecutor(currExecutor, done, new Status(RaftError.EINTR, "Sending rpc was interrupted"));
    } catch (final RemotingException e) {
        future.failure(e);
        // should be in another thread to avoid dead locking.
        RpcUtils.runClosureInExecutor(currExecutor, done, new Status(RaftError.EINTERNAL, "Fail to send a RPC request:" + e.getMessage()));
    }
    return future;
}
Also used : Status(io.dingodb.raft.Status) InvokeCallback(io.dingodb.raft.rpc.InvokeCallback) InvokeTimeoutException(io.dingodb.raft.error.InvokeTimeoutException) Message(com.google.protobuf.Message) RpcRequests(io.dingodb.raft.rpc.RpcRequests) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Executor(java.util.concurrent.Executor) RemotingException(io.dingodb.raft.error.RemotingException) Descriptors(com.google.protobuf.Descriptors) RpcClient(io.dingodb.raft.rpc.RpcClient)

Example 4 with RemotingException

use of io.dingodb.raft.error.RemotingException in project dingo by dingodb.

the class DefaultRaftClientService method onConnectionFail.

// fail-fast when no connection
private Future<Message> onConnectionFail(final Endpoint endpoint, final Message request, Closure done, final Executor executor) {
    final FutureImpl<Message> future = new FutureImpl<>();
    executor.execute(() -> {
        final String fmt = "Check connection[%s] fail and try to create new one";
        if (done != null) {
            try {
                done.run(new Status(RaftError.EINTERNAL, fmt, endpoint));
            } catch (final Throwable t) {
                LOG.error("Fail to run RpcResponseClosure, the request is {}.", request, t);
            }
        }
        if (!future.isDone()) {
            future.failure(new RemotingException(String.format(fmt, endpoint)));
        }
    });
    return future;
}
Also used : Status(io.dingodb.raft.Status) Message(com.google.protobuf.Message) FutureImpl(io.dingodb.raft.rpc.impl.FutureImpl) RemotingException(io.dingodb.raft.error.RemotingException)

Example 5 with RemotingException

use of io.dingodb.raft.error.RemotingException in project dingo by dingodb.

the class CoordinatorStateMachine method getAllLocation.

private void getAllLocation(Channel channel) {
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        List<PeerId> peerIds = context.node().listPeers();
        baos.write(PrimitiveCodec.encodeVarInt(peerIds.size()));
        for (PeerId peerId : peerIds) {
            GetLocationResponse res = (GetLocationResponse) rpcClient.invokeSync(peerId.getEndpoint(), GetLocationRequest.INSTANCE, 3000);
            baos.write(encodeHostPort(res.getHost(), res.getPort()));
        }
        baos.flush();
        channel.send(new SimpleMessage(null, baos.toByteArray()));
    } catch (IOException e) {
        log.error("Serialize leader location error", e);
        channel.send(EmptyMessage.INSTANCE);
    } catch (RemotingException e) {
        log.error("Get peer location error", e);
        channel.send(EmptyMessage.INSTANCE);
    } catch (InterruptedException e) {
        log.error("Get all location interrupt.", e);
        channel.send(EmptyMessage.INSTANCE);
    } catch (Exception e) {
        log.error("Get leader location error.", e);
        channel.send(EmptyMessage.INSTANCE);
    }
}
Also used : SimpleMessage(io.dingodb.net.SimpleMessage) RemotingException(io.dingodb.raft.error.RemotingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) GetLocationResponse(io.dingodb.server.coordinator.handler.GetLocationHandler.GetLocationResponse) StoreCodecException(io.dingodb.store.row.errors.StoreCodecException) RemotingException(io.dingodb.raft.error.RemotingException) IOException(java.io.IOException) IllegalRowStoreOperationException(io.dingodb.store.row.errors.IllegalRowStoreOperationException) PeerId(io.dingodb.raft.entity.PeerId)

Aggregations

RemotingException (io.dingodb.raft.error.RemotingException)5 Message (com.google.protobuf.Message)2 Status (io.dingodb.raft.Status)2 RpcClient (io.dingodb.raft.rpc.RpcClient)2 RpcRequests (io.dingodb.raft.rpc.RpcRequests)2 GetLocationResponse (io.dingodb.server.coordinator.handler.GetLocationHandler.GetLocationResponse)2 IllegalRowStoreOperationException (io.dingodb.store.row.errors.IllegalRowStoreOperationException)2 StoreCodecException (io.dingodb.store.row.errors.StoreCodecException)2 IOException (java.io.IOException)2 Descriptors (com.google.protobuf.Descriptors)1 SimpleMessage (io.dingodb.net.SimpleMessage)1 PeerId (io.dingodb.raft.entity.PeerId)1 InvokeTimeoutException (io.dingodb.raft.error.InvokeTimeoutException)1 InvokeCallback (io.dingodb.raft.rpc.InvokeCallback)1 FutureImpl (io.dingodb.raft.rpc.impl.FutureImpl)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Executor (java.util.concurrent.Executor)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1