Search in sources :

Example 11 with Response

use of org.apache.dubbo.remoting.exchange.Response in project dubbo by alibaba.

the class ThriftCodec method decode.

private Object decode(TProtocol protocol) throws IOException {
    // version
    String serviceName;
    String path;
    long id;
    TMessage message;
    try {
        protocol.readI16();
        protocol.readByte();
        serviceName = protocol.readString();
        path = protocol.readString();
        id = protocol.readI64();
        message = protocol.readMessageBegin();
    } catch (TException e) {
        throw new IOException(e.getMessage(), e);
    }
    if (message.type == TMessageType.CALL) {
        RpcInvocation result = new RpcInvocation();
        result.setAttachment(INTERFACE_KEY, serviceName);
        result.setAttachment(PATH_KEY, path);
        result.setMethodName(message.name);
        String argsClassName = ExtensionLoader.getExtensionLoader(ClassNameGenerator.class).getExtension(ThriftClassNameGenerator.NAME).generateArgsClassName(serviceName, message.name);
        if (StringUtils.isEmpty(argsClassName)) {
            throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, "The specified interface name incorrect.");
        }
        Class clazz = CACHED_CLASS.get(argsClassName);
        if (clazz == null) {
            try {
                clazz = ClassUtils.forNameWithThreadContextClassLoader(argsClassName);
                CACHED_CLASS.putIfAbsent(argsClassName, clazz);
            } catch (ClassNotFoundException e) {
                throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e);
            }
        }
        TBase args;
        try {
            args = (TBase) clazz.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e);
        }
        try {
            args.read(protocol);
            protocol.readMessageEnd();
        } catch (TException e) {
            throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e);
        }
        List<Object> parameters = new ArrayList<>();
        List<Class<?>> parameterTypes = new ArrayList<>();
        int index = 1;
        while (true) {
            TFieldIdEnum fieldIdEnum = args.fieldForId(index++);
            if (fieldIdEnum == null) {
                break;
            }
            String fieldName = fieldIdEnum.getFieldName();
            String getMethodName = ThriftUtils.generateGetMethodName(fieldName);
            Method getMethod;
            try {
                getMethod = clazz.getMethod(getMethodName);
            } catch (NoSuchMethodException e) {
                throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e);
            }
            parameterTypes.add(getMethod.getReturnType());
            try {
                parameters.add(getMethod.invoke(args));
            } catch (IllegalAccessException | InvocationTargetException e) {
                throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e);
            }
        }
        result.setArguments(parameters.toArray());
        result.setParameterTypes(parameterTypes.toArray(new Class[0]));
        Request request = new Request(id);
        request.setData(result);
        CACHED_REQUEST.putIfAbsent(id, RequestData.create(message.seqid, serviceName, message.name));
        return request;
    } else if (message.type == TMessageType.EXCEPTION) {
        TApplicationException exception;
        try {
            exception = TApplicationException.readFrom(protocol);
            protocol.readMessageEnd();
        } catch (TException e) {
            throw new IOException(e.getMessage(), e);
        }
        AppResponse result = new AppResponse();
        result.setException(new RpcException(exception.getMessage()));
        Response response = new Response();
        response.setResult(result);
        response.setId(id);
        return response;
    } else if (message.type == TMessageType.REPLY) {
        String resultClassName = ExtensionLoader.getExtensionLoader(ClassNameGenerator.class).getExtension(ThriftClassNameGenerator.NAME).generateResultClassName(serviceName, message.name);
        if (StringUtils.isEmpty(resultClassName)) {
            throw new IllegalArgumentException("Could not infer service result class name from service name " + serviceName + ", the service name you specified may not generated by thrift idl compiler");
        }
        Class<?> clazz = CACHED_CLASS.get(resultClassName);
        if (clazz == null) {
            try {
                clazz = ClassUtils.forNameWithThreadContextClassLoader(resultClassName);
                CACHED_CLASS.putIfAbsent(resultClassName, clazz);
            } catch (ClassNotFoundException e) {
                throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e);
            }
        }
        TBase<?, ? extends TFieldIdEnum> result;
        try {
            result = (TBase<?, ?>) clazz.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e);
        }
        try {
            result.read(protocol);
            protocol.readMessageEnd();
        } catch (TException e) {
            throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e);
        }
        Object realResult = null;
        int index = 0;
        while (true) {
            TFieldIdEnum fieldIdEnum = result.fieldForId(index++);
            if (fieldIdEnum == null) {
                break;
            }
            Field field;
            try {
                field = clazz.getDeclaredField(fieldIdEnum.getFieldName());
                ReflectUtils.makeAccessible(field);
            } catch (NoSuchFieldException e) {
                throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e);
            }
            try {
                realResult = field.get(result);
            } catch (IllegalAccessException e) {
                throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e);
            }
            if (realResult != null) {
                break;
            }
        }
        Response response = new Response();
        response.setId(id);
        AppResponse appResponse = new AppResponse();
        if (realResult instanceof Throwable) {
            appResponse.setException((Throwable) realResult);
        } else {
            appResponse.setValue(realResult);
        }
        response.setResult(appResponse);
        return response;
    } else {
        // Impossible
        throw new IOException();
    }
}
Also used : TException(org.apache.thrift.TException) ArrayList(java.util.ArrayList) Field(java.lang.reflect.Field) TMessage(org.apache.thrift.protocol.TMessage) AppResponse(org.apache.dubbo.rpc.AppResponse) RpcException(org.apache.dubbo.rpc.RpcException) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Request(org.apache.dubbo.remoting.exchange.Request) IOException(java.io.IOException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) TApplicationException(org.apache.thrift.TApplicationException) AppResponse(org.apache.dubbo.rpc.AppResponse) Response(org.apache.dubbo.remoting.exchange.Response) TFieldIdEnum(org.apache.thrift.TFieldIdEnum) TBase(org.apache.thrift.TBase)

Example 12 with Response

use of org.apache.dubbo.remoting.exchange.Response in project dubbo by alibaba.

the class PayloadDropper method getRequestWithoutData.

/**
 * only log body in debugger mode for size & security consideration.
 *
 * @param message
 * @return
 */
public static Object getRequestWithoutData(Object message) {
    if (logger.isDebugEnabled()) {
        return message;
    }
    if (message instanceof Request) {
        Request request = (Request) message;
        request.setData(null);
        return request;
    } else if (message instanceof Response) {
        Response response = (Response) message;
        response.setResult(null);
        return response;
    }
    return message;
}
Also used : Response(org.apache.dubbo.remoting.exchange.Response) Request(org.apache.dubbo.remoting.exchange.Request)

Example 13 with Response

use of org.apache.dubbo.remoting.exchange.Response in project dubbo by alibaba.

the class DefaultFuture method cancel.

@Override
public boolean cancel(boolean mayInterruptIfRunning) {
    Response errorResult = new Response(id);
    errorResult.setStatus(Response.CLIENT_ERROR);
    errorResult.setErrorMessage("request future has been canceled.");
    this.doReceived(errorResult);
    FUTURES.remove(id);
    CHANNELS.remove(id);
    return true;
}
Also used : Response(org.apache.dubbo.remoting.exchange.Response)

Example 14 with Response

use of org.apache.dubbo.remoting.exchange.Response in project dubbo by alibaba.

the class DefaultFuture method closeChannel.

/**
 * close a channel when a channel is inactive
 * directly return the unfinished requests.
 *
 * @param channel channel to close
 */
public static void closeChannel(Channel channel) {
    for (Map.Entry<Long, Channel> entry : CHANNELS.entrySet()) {
        if (channel.equals(entry.getValue())) {
            DefaultFuture future = getFuture(entry.getKey());
            if (future != null && !future.isDone()) {
                Response disconnectResponse = new Response(future.getId());
                disconnectResponse.setStatus(Response.CHANNEL_INACTIVE);
                disconnectResponse.setErrorMessage("Channel " + channel + " is inactive. Directly return the unFinished request : " + (logger.isDebugEnabled() ? future.getRequest() : future.getRequest().copyWithoutData()));
                DefaultFuture.received(channel, disconnectResponse);
            }
        }
    }
}
Also used : Response(org.apache.dubbo.remoting.exchange.Response) Channel(org.apache.dubbo.remoting.Channel) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map)

Example 15 with Response

use of org.apache.dubbo.remoting.exchange.Response in project dubbo by alibaba.

the class WrappedChannelHandler method sendFeedback.

protected void sendFeedback(Channel channel, Request request, Throwable t) throws RemotingException {
    if (request.isTwoWay()) {
        String msg = "Server side(" + url.getIp() + "," + url.getPort() + ") thread pool is exhausted, detail msg:" + t.getMessage();
        Response response = new Response(request.getId(), request.getVersion());
        response.setStatus(Response.SERVER_THREADPOOL_EXHAUSTED_ERROR);
        response.setErrorMessage(msg);
        channel.send(response);
        return;
    }
}
Also used : Response(org.apache.dubbo.remoting.exchange.Response)

Aggregations

Response (org.apache.dubbo.remoting.exchange.Response)39 Request (org.apache.dubbo.remoting.exchange.Request)21 Test (org.junit.jupiter.api.Test)20 Channel (org.apache.dubbo.remoting.Channel)12 ChannelBuffer (org.apache.dubbo.remoting.buffer.ChannelBuffer)9 IOException (java.io.IOException)6 RemotingException (org.apache.dubbo.remoting.RemotingException)6 AppResponse (org.apache.dubbo.rpc.AppResponse)6 ExchangeChannel (org.apache.dubbo.remoting.exchange.ExchangeChannel)5 TMessage (org.apache.thrift.protocol.TMessage)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 URL (org.apache.dubbo.common.URL)4 Demo (org.apache.dubbo.rpc.gen.thrift.Demo)4 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)4 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)4 DefaultFuture (org.apache.dubbo.remoting.exchange.support.DefaultFuture)3 HeaderExchangeHandler (org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler)3 TApplicationException (org.apache.thrift.TApplicationException)3 ObjectInput (org.apache.dubbo.common.serialize.ObjectInput)2