Search in sources :

Example 11 with RemotingException

use of org.apache.dubbo.remoting.RemotingException in project dubbo by alibaba.

the class ThriftProtocol method getServer.

private ProtocolServer getServer(URL url) {
    // enable sending readonly event when server closes by default
    url = url.addParameterIfAbsent(CHANNEL_READONLYEVENT_SENT_KEY, Boolean.TRUE.toString());
    String str = url.getParameter(SERVER_KEY, org.apache.dubbo.rpc.Constants.DEFAULT_REMOTING_SERVER);
    if (str != null && str.length() > 0 && !ExtensionLoader.getExtensionLoader(Transporter.class).hasExtension(str)) {
        throw new RpcException("Unsupported server type: " + str + ", url: " + url);
    }
    ExchangeServer server;
    try {
        server = Exchangers.bind(url, handler);
    } catch (RemotingException e) {
        throw new RpcException("Fail to start server(url: " + url + ") " + e.getMessage(), e);
    }
    str = url.getParameter(CLIENT_KEY);
    if (str != null && str.length() > 0) {
        Set<String> supportedTypes = ExtensionLoader.getExtensionLoader(Transporter.class).getSupportedExtensions();
        if (!supportedTypes.contains(str)) {
            throw new RpcException("Unsupported client type: " + str);
        }
    }
    return new ThriftProtocolServer(server);
}
Also used : RpcException(org.apache.dubbo.rpc.RpcException) RemotingException(org.apache.dubbo.remoting.RemotingException) ExchangeServer(org.apache.dubbo.remoting.exchange.ExchangeServer) Transporter(org.apache.dubbo.remoting.Transporter)

Example 12 with RemotingException

use of org.apache.dubbo.remoting.RemotingException in project dubbo by alibaba.

the class CallbackServiceCodec method decodeInvocationArgument.

public static Object decodeInvocationArgument(Channel channel, RpcInvocation inv, Class<?>[] pts, int paraIndex, Object inObject) throws IOException {
    // if it's a callback, create proxy on client side, callback interface on client side can be invoked through channel
    // need get URL from channel and env when decode
    URL url = null;
    try {
        url = DubboProtocol.getDubboProtocol().getInvoker(channel, inv).getUrl();
    } catch (RemotingException e) {
        if (logger.isInfoEnabled()) {
            logger.info(e.getMessage(), e);
        }
        return inObject;
    }
    byte callbackstatus = isCallBack(url, inv.getProtocolServiceKey(), inv.getMethodName(), paraIndex);
    switch(callbackstatus) {
        case CallbackServiceCodec.CALLBACK_CREATE:
            try {
                return referOrDestroyCallbackService(channel, url, pts[paraIndex], inv, Integer.parseInt(inv.getAttachment(INV_ATT_CALLBACK_KEY + paraIndex)), true);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                throw new IOException(StringUtils.toString(e));
            }
        case CallbackServiceCodec.CALLBACK_DESTROY:
            try {
                return referOrDestroyCallbackService(channel, url, pts[paraIndex], inv, Integer.parseInt(inv.getAttachment(INV_ATT_CALLBACK_KEY + paraIndex)), false);
            } catch (Exception e) {
                throw new IOException(StringUtils.toString(e));
            }
        default:
            return inObject;
    }
}
Also used : RemotingException(org.apache.dubbo.remoting.RemotingException) IOException(java.io.IOException) URL(org.apache.dubbo.common.URL) RemotingException(org.apache.dubbo.remoting.RemotingException) IOException(java.io.IOException)

Example 13 with RemotingException

use of org.apache.dubbo.remoting.RemotingException in project dubbo by alibaba.

the class ExchangeCodec method encodeResponse.

protected void encodeResponse(Channel channel, ChannelBuffer buffer, Response res) throws IOException {
    int savedWriteIndex = buffer.writerIndex();
    try {
        Serialization serialization = getSerialization(channel, res);
        // header.
        byte[] header = new byte[HEADER_LENGTH];
        // set magic number.
        Bytes.short2bytes(MAGIC, header);
        // set request and serialization flag.
        header[2] = serialization.getContentTypeId();
        if (res.isHeartbeat()) {
            header[2] |= FLAG_EVENT;
        }
        // set response status.
        byte status = res.getStatus();
        header[3] = status;
        // set request id.
        Bytes.long2bytes(res.getId(), header, 4);
        buffer.writerIndex(savedWriteIndex + HEADER_LENGTH);
        ChannelBufferOutputStream bos = new ChannelBufferOutputStream(buffer);
        // encode response data or error message.
        if (status == Response.OK) {
            if (res.isHeartbeat()) {
                // heartbeat response data is always null
                bos.write(CodecSupport.getNullBytesOf(serialization));
            } else {
                ObjectOutput out = serialization.serialize(channel.getUrl(), bos);
                if (res.isEvent()) {
                    encodeEventData(channel, out, res.getResult());
                } else {
                    encodeResponseData(channel, out, res.getResult(), res.getVersion());
                }
                out.flushBuffer();
                if (out instanceof Cleanable) {
                    ((Cleanable) out).cleanup();
                }
            }
        } else {
            ObjectOutput out = serialization.serialize(channel.getUrl(), bos);
            out.writeUTF(res.getErrorMessage());
            out.flushBuffer();
            if (out instanceof Cleanable) {
                ((Cleanable) out).cleanup();
            }
        }
        bos.flush();
        bos.close();
        int len = bos.writtenBytes();
        checkPayload(channel, len);
        Bytes.int2bytes(len, header, 12);
        // write
        buffer.writerIndex(savedWriteIndex);
        // write header.
        buffer.writeBytes(header);
        buffer.writerIndex(savedWriteIndex + HEADER_LENGTH + len);
    } catch (Throwable t) {
        // clear buffer
        buffer.writerIndex(savedWriteIndex);
        // send error message to Consumer, otherwise, Consumer will wait till timeout.
        if (!res.isEvent() && res.getStatus() != Response.BAD_RESPONSE) {
            Response r = new Response(res.getId(), res.getVersion());
            r.setStatus(Response.BAD_RESPONSE);
            if (t instanceof ExceedPayloadLimitException) {
                logger.warn(t.getMessage(), t);
                try {
                    r.setErrorMessage(t.getMessage());
                    channel.send(r);
                    return;
                } catch (RemotingException e) {
                    logger.warn("Failed to send bad_response info back: " + t.getMessage() + ", cause: " + e.getMessage(), e);
                }
            } else {
                // FIXME log error message in Codec and handle in caught() of IoHanndler?
                logger.warn("Fail to encode response: " + res + ", send bad_response info instead, cause: " + t.getMessage(), t);
                try {
                    r.setErrorMessage("Failed to send response: " + res + ", cause: " + StringUtils.toString(t));
                    channel.send(r);
                    return;
                } catch (RemotingException e) {
                    logger.warn("Failed to send bad_response info back: " + res + ", cause: " + e.getMessage(), e);
                }
            }
        }
        // Rethrow exception
        if (t instanceof IOException) {
            throw (IOException) t;
        } else if (t instanceof RuntimeException) {
            throw (RuntimeException) t;
        } else if (t instanceof Error) {
            throw (Error) t;
        } else {
            throw new RuntimeException(t.getMessage(), t);
        }
    }
}
Also used : ChannelBufferOutputStream(org.apache.dubbo.remoting.buffer.ChannelBufferOutputStream) ObjectOutput(org.apache.dubbo.common.serialize.ObjectOutput) IOException(java.io.IOException) Serialization(org.apache.dubbo.common.serialize.Serialization) Response(org.apache.dubbo.remoting.exchange.Response) RemotingException(org.apache.dubbo.remoting.RemotingException) ExceedPayloadLimitException(org.apache.dubbo.remoting.transport.ExceedPayloadLimitException) Cleanable(org.apache.dubbo.common.serialize.Cleanable)

Example 14 with RemotingException

use of org.apache.dubbo.remoting.RemotingException in project dubbo by alibaba.

the class HeaderExchangeChannel method request.

@Override
public CompletableFuture<Object> request(Object request, int timeout, ExecutorService executor) throws RemotingException {
    if (closed) {
        throw new RemotingException(this.getLocalAddress(), null, "Failed to send request " + request + ", cause: The channel " + this + " is closed!");
    }
    // create request.
    Request req = new Request();
    req.setVersion(Version.getProtocolVersion());
    req.setTwoWay(true);
    req.setData(request);
    DefaultFuture future = DefaultFuture.newFuture(channel, req, timeout, executor);
    try {
        channel.send(req);
    } catch (RemotingException e) {
        future.cancel();
        throw e;
    }
    return future;
}
Also used : RemotingException(org.apache.dubbo.remoting.RemotingException) Request(org.apache.dubbo.remoting.exchange.Request) DefaultFuture(org.apache.dubbo.remoting.exchange.support.DefaultFuture)

Example 15 with RemotingException

use of org.apache.dubbo.remoting.RemotingException in project dubbo by alibaba.

the class HeaderExchangeHandler method sent.

@Override
public void sent(Channel channel, Object message) throws RemotingException {
    Throwable exception = null;
    try {
        ExchangeChannel exchangeChannel = HeaderExchangeChannel.getOrAddChannel(channel);
        handler.sent(exchangeChannel, message);
    } catch (Throwable t) {
        exception = t;
        HeaderExchangeChannel.removeChannelIfDisconnected(channel);
    }
    if (message instanceof Request) {
        Request request = (Request) message;
        DefaultFuture.sent(channel, request);
    }
    if (exception != null) {
        if (exception instanceof RuntimeException) {
            throw (RuntimeException) exception;
        } else if (exception instanceof RemotingException) {
            throw (RemotingException) exception;
        } else {
            throw new RemotingException(channel.getLocalAddress(), channel.getRemoteAddress(), exception.getMessage(), exception);
        }
    }
}
Also used : RemotingException(org.apache.dubbo.remoting.RemotingException) Request(org.apache.dubbo.remoting.exchange.Request) ExchangeChannel(org.apache.dubbo.remoting.exchange.ExchangeChannel)

Aggregations

RemotingException (org.apache.dubbo.remoting.RemotingException)39 IOException (java.io.IOException)13 Request (org.apache.dubbo.remoting.exchange.Request)7 Response (org.apache.dubbo.remoting.exchange.Response)6 RpcException (org.apache.dubbo.rpc.RpcException)6 Channel (org.apache.dubbo.remoting.Channel)5 ExchangeChannel (org.apache.dubbo.remoting.exchange.ExchangeChannel)5 Test (org.junit.jupiter.api.Test)5 InetSocketAddress (java.net.InetSocketAddress)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 TimeoutException (org.apache.dubbo.remoting.TimeoutException)3 Transporter (org.apache.dubbo.remoting.Transporter)3 ExchangeClient (org.apache.dubbo.remoting.exchange.ExchangeClient)3 AppResponse (org.apache.dubbo.rpc.AppResponse)3 AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)3 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)3 ChannelFuture (io.netty.channel.ChannelFuture)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2