Search in sources :

Example 6 with TApplicationException

use of org.apache.thrift.TApplicationException in project accumulo by apache.

the class TServiceClientWrapper method receiveBase.

@Override
protected void receiveBase(TBase<?, ?> result, String methodName) throws TException {
    TMessage msg = iprot_.readMessageBegin();
    if (msg.type == TMessageType.EXCEPTION) {
        TApplicationException x = new TApplicationException();
        x.read(iprot_);
        iprot_.readMessageEnd();
        throw x;
    }
    if (msg.seqid != seqid_) {
        throw new TApplicationException(TApplicationException.BAD_SEQUENCE_ID, String.format("%s failed: out of sequence response: expected %d but got %d", methodName, seqid_, msg.seqid));
    }
    result.read(iprot_);
    iprot_.readMessageEnd();
}
Also used : TMessage(org.apache.thrift.protocol.TMessage) TApplicationException(org.apache.thrift.TApplicationException)

Example 7 with TApplicationException

use of org.apache.thrift.TApplicationException in project flink by apache.

the class HiveShimV210 method getPrimaryKey.

@Override
public Optional<UniqueConstraint> getPrimaryKey(IMetaStoreClient client, String dbName, String tableName, byte requiredTrait) {
    try {
        Class requestClz = Class.forName("org.apache.hadoop.hive.metastore.api.PrimaryKeysRequest");
        Object request = requestClz.getDeclaredConstructor(String.class, String.class).newInstance(dbName, tableName);
        List<?> constraints = (List<?>) HiveReflectionUtils.invokeMethod(client.getClass(), client, "getPrimaryKeys", new Class[] { requestClz }, new Object[] { request });
        if (constraints.isEmpty()) {
            return Optional.empty();
        }
        Class constraintClz = Class.forName("org.apache.hadoop.hive.metastore.api.SQLPrimaryKey");
        Method colNameMethod = constraintClz.getDeclaredMethod("getColumn_name");
        Method isEnableMethod = constraintClz.getDeclaredMethod("isEnable_cstr");
        Method isValidateMethod = constraintClz.getDeclaredMethod("isValidate_cstr");
        Method isRelyMethod = constraintClz.getDeclaredMethod("isRely_cstr");
        List<String> colNames = new ArrayList<>();
        for (Object constraint : constraints) {
            // check whether a constraint satisfies all the traits the caller specified
            boolean satisfy = !HiveTableUtil.requireEnableConstraint(requiredTrait) || (boolean) isEnableMethod.invoke(constraint);
            if (satisfy) {
                satisfy = !HiveTableUtil.requireValidateConstraint(requiredTrait) || (boolean) isValidateMethod.invoke(constraint);
            }
            if (satisfy) {
                satisfy = !HiveTableUtil.requireRelyConstraint(requiredTrait) || (boolean) isRelyMethod.invoke(constraint);
            }
            if (satisfy) {
                colNames.add((String) colNameMethod.invoke(constraint));
            } else {
                return Optional.empty();
            }
        }
        // all pk constraints should have the same name, so let's use the name of the first one
        String pkName = (String) HiveReflectionUtils.invokeMethod(constraintClz, constraints.get(0), "getPk_name", null, null);
        return Optional.of(UniqueConstraint.primaryKey(pkName, colNames));
    } catch (Throwable t) {
        if (t instanceof InvocationTargetException) {
            t = t.getCause();
        }
        if (t instanceof TApplicationException && t.getMessage() != null && t.getMessage().contains("Invalid method name")) {
            return Optional.empty();
        }
        throw new CatalogException("Failed to get PrimaryKey constraints", t);
    }
}
Also used : ArrayList(java.util.ArrayList) CatalogException(org.apache.flink.table.catalog.exceptions.CatalogException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) TApplicationException(org.apache.thrift.TApplicationException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 8 with TApplicationException

use of org.apache.thrift.TApplicationException in project distributedlog by twitter.

the class DistributedLogClientImpl method handleRequestException.

void handleRequestException(SocketAddress addr, ProxyClient sc, Optional<StreamOp> op, Throwable cause) {
    boolean resendOp = false;
    boolean removeOwnerFromStream = false;
    SocketAddress previousAddr = addr;
    String reason = cause.getMessage();
    if (cause instanceof ConnectionFailedException || cause instanceof java.net.ConnectException) {
        routingService.removeHost(addr, cause);
        onServerLeft(addr, sc);
        removeOwnerFromStream = true;
        // redirect the request to other host.
        resendOp = true;
    } else if (cause instanceof ChannelException) {
        // no process listening on remote address/port.
        if (cause.getCause() instanceof java.net.ConnectException) {
            routingService.removeHost(addr, cause.getCause());
            onServerLeft(addr);
            reason = cause.getCause().getMessage();
        } else {
            routingService.removeHost(addr, cause);
            reason = cause.getMessage();
        }
        removeOwnerFromStream = true;
        // redirect the request to other host.
        resendOp = true;
    } else if (cause instanceof ServiceTimeoutException) {
        // redirect the request to itself again, which will backoff for a while
        resendOp = true;
        previousAddr = null;
    } else if (cause instanceof WriteException) {
        // redirect the request to other host.
        resendOp = true;
    } else if (cause instanceof ServiceException) {
        // redirect the request to other host.
        clientManager.removeClient(addr, sc);
        resendOp = true;
    } else if (cause instanceof TApplicationException) {
        handleTApplicationException(cause, op, addr, sc);
    } else if (cause instanceof Failure) {
        handleFinagleFailure((Failure) cause, op, addr);
    } else {
        // Default handler
        handleException(cause, op, addr);
    }
    if (op.isPresent()) {
        if (removeOwnerFromStream) {
            ownershipCache.removeOwnerFromStream(op.get().stream, addr, reason);
        }
        if (resendOp) {
            doSend(op.get(), previousAddr);
        }
    }
}
Also used : WriteException(com.twitter.finagle.WriteException) ServiceException(com.twitter.finagle.ServiceException) SocketAddress(java.net.SocketAddress) DLSocketAddress(com.twitter.distributedlog.service.DLSocketAddress) ServiceTimeoutException(com.twitter.finagle.ServiceTimeoutException) ConnectionFailedException(com.twitter.finagle.ConnectionFailedException) Failure(com.twitter.finagle.Failure) ChannelException(org.jboss.netty.channel.ChannelException) TApplicationException(org.apache.thrift.TApplicationException)

Example 9 with TApplicationException

use of org.apache.thrift.TApplicationException 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 10 with TApplicationException

use of org.apache.thrift.TApplicationException in project dubbo by alibaba.

the class ThriftCodec method encodeResponse.

private void encodeResponse(Channel channel, ChannelBuffer buffer, Response response) throws IOException {
    AppResponse result = (AppResponse) response.getResult();
    RequestData rd = CACHED_REQUEST.get(response.getId());
    String resultClassName = ExtensionLoader.getExtensionLoader(ClassNameGenerator.class).getExtension(channel.getUrl().getParameter(ThriftConstants.CLASS_NAME_GENERATOR_KEY, ThriftClassNameGenerator.NAME)).generateResultClassName(rd.serviceName, rd.methodName);
    if (StringUtils.isEmpty(resultClassName)) {
        throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, "Could not encode response, the specified interface may be incorrect.");
    }
    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 resultObj;
    try {
        resultObj = (TBase) clazz.newInstance();
    } catch (InstantiationException | IllegalAccessException e) {
        throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e);
    }
    TApplicationException applicationException = null;
    TMessage message;
    if (result.hasException()) {
        Throwable throwable = result.getException();
        int index = 1;
        boolean found = false;
        while (true) {
            TFieldIdEnum fieldIdEnum = resultObj.fieldForId(index++);
            if (fieldIdEnum == null) {
                break;
            }
            String fieldName = fieldIdEnum.getFieldName();
            String getMethodName = ThriftUtils.generateGetMethodName(fieldName);
            String setMethodName = ThriftUtils.generateSetMethodName(fieldName);
            Method getMethod;
            Method setMethod;
            try {
                getMethod = clazz.getMethod(getMethodName);
                if (getMethod.getReturnType().equals(throwable.getClass())) {
                    found = true;
                    setMethod = clazz.getMethod(setMethodName, throwable.getClass());
                    setMethod.invoke(resultObj, throwable);
                }
            } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
                throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e);
            }
        }
        if (!found) {
            applicationException = new TApplicationException(throwable.getMessage());
        }
    } else {
        Object realResult = result.getValue();
        // result field id is 0
        String fieldName = resultObj.fieldForId(0).getFieldName();
        String setMethodName = ThriftUtils.generateSetMethodName(fieldName);
        String getMethodName = ThriftUtils.generateGetMethodName(fieldName);
        Method getMethod;
        Method setMethod;
        try {
            getMethod = clazz.getMethod(getMethodName);
            setMethod = clazz.getMethod(setMethodName, getMethod.getReturnType());
            setMethod.invoke(resultObj, realResult);
        } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
            throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e);
        }
    }
    if (applicationException != null) {
        message = new TMessage(rd.methodName, TMessageType.EXCEPTION, rd.id);
    } else {
        message = new TMessage(rd.methodName, TMessageType.REPLY, rd.id);
    }
    RandomAccessByteArrayOutputStream bos = new RandomAccessByteArrayOutputStream(1024);
    TIOStreamTransport transport = new TIOStreamTransport(bos);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    int messageLength;
    int headerLength;
    byte[] bytes = new byte[4];
    try {
        // magic
        protocol.writeI16(MAGIC);
        // message length
        protocol.writeI32(Integer.MAX_VALUE);
        // message header length
        protocol.writeI16(Short.MAX_VALUE);
        // version
        protocol.writeByte(VERSION);
        // service name
        protocol.writeString(rd.serviceName);
        // id
        protocol.writeI64(response.getId());
        protocol.getTransport().flush();
        headerLength = bos.size();
        // message
        protocol.writeMessageBegin(message);
        switch(message.type) {
            case TMessageType.EXCEPTION:
                applicationException.write(protocol);
                break;
            case TMessageType.REPLY:
                resultObj.write(protocol);
                break;
            default:
        }
        protocol.writeMessageEnd();
        protocol.getTransport().flush();
        int oldIndex = messageLength = bos.size();
        try {
            TFramedTransport.encodeFrameSize(messageLength, bytes);
            bos.setWriteIndex(MESSAGE_LENGTH_INDEX);
            protocol.writeI32(messageLength);
            bos.setWriteIndex(MESSAGE_HEADER_LENGTH_INDEX);
            protocol.writeI16((short) (0xffff & headerLength));
        } finally {
            bos.setWriteIndex(oldIndex);
        }
    } catch (TException e) {
        throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e);
    }
    buffer.writeBytes(bytes);
    buffer.writeBytes(bos.toByteArray());
}
Also used : TException(org.apache.thrift.TException) TMessage(org.apache.thrift.protocol.TMessage) AppResponse(org.apache.dubbo.rpc.AppResponse) RpcException(org.apache.dubbo.rpc.RpcException) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) TApplicationException(org.apache.thrift.TApplicationException) RandomAccessByteArrayOutputStream(org.apache.dubbo.rpc.protocol.thrift.io.RandomAccessByteArrayOutputStream) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TFieldIdEnum(org.apache.thrift.TFieldIdEnum) TBase(org.apache.thrift.TBase)

Aggregations

TApplicationException (org.apache.thrift.TApplicationException)38 TException (org.apache.thrift.TException)16 Test (org.junit.Test)14 MetastoreCheckinTest (org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)12 TMessage (org.apache.thrift.protocol.TMessage)12 InvalidObjectException (org.apache.hadoop.hive.metastore.api.InvalidObjectException)11 Table (org.apache.hadoop.hive.metastore.api.Table)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)6 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)6 Method (java.lang.reflect.Method)5 AccumuloSecurityException (org.apache.accumulo.core.client.AccumuloSecurityException)5 SQLPrimaryKey (org.apache.hadoop.hive.metastore.api.SQLPrimaryKey)5 SQLPrimaryKeyBuilder (org.apache.hadoop.hive.metastore.client.builder.SQLPrimaryKeyBuilder)5 SQLForeignKey (org.apache.hadoop.hive.metastore.api.SQLForeignKey)4 SQLForeignKeyBuilder (org.apache.hadoop.hive.metastore.client.builder.SQLForeignKeyBuilder)4 TBase (org.apache.thrift.TBase)4 TFieldIdEnum (org.apache.thrift.TFieldIdEnum)4