Search in sources :

Example 26 with MotanFrameworkException

use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.

the class ZookeeperRegistry method doRegister.

@Override
protected void doRegister(URL url) {
    try {
        serverLock.lock();
        // 防止旧节点未正常注销
        removeNode(url, ZkNodeType.AVAILABLE_SERVER);
        removeNode(url, ZkNodeType.UNAVAILABLE_SERVER);
        createNode(url, ZkNodeType.UNAVAILABLE_SERVER);
    } catch (Throwable e) {
        throw new MotanFrameworkException(String.format("Failed to register %s to zookeeper(%s), cause: %s", url, getUrl(), e.getMessage()), e);
    } finally {
        serverLock.unlock();
    }
}
Also used : MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException)

Example 27 with MotanFrameworkException

use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.

the class CompressRpcCodec method putMethodSign.

public static void putMethodSign(Provider<?> provider, List<Method> methods) {
    String group = provider.getUrl().getGroup();
    String interfaceName = provider.getInterface().getName();
    String version = provider.getUrl().getVersion();
    for (Method method : methods) {
        MethodInfo temp = new MethodInfo(group, interfaceName, method.getName(), ReflectUtil.getMethodParamDesc(method), version);
        String sign = temp.getSign();
        MethodInfo priInfo = SIGN_METHOD_MAP.putIfAbsent(sign, temp);
        if (priInfo != null && !temp.equals(priInfo)) {
            // 方法签名冲突
            throw new MotanFrameworkException("add method sign conflict! " + temp.toString() + " with " + priInfo.toString(), MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
        } else {
            LoggerUtil.info("add method sign:" + sign + ", methodinfo:" + temp.toString());
        }
    }
}
Also used : MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) Method(java.lang.reflect.Method)

Example 28 with MotanFrameworkException

use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.

the class CompressRpcCodec method decodeRequest.

private Object decodeRequest(byte[] body, long requestId, String remoteIp, Serialization serialization) throws IOException, ClassNotFoundException {
    ObjectInput input = createInput(getInputStream(body));
    String interfaceName = null;
    String methodName = null;
    String paramtersDesc = null;
    String group = null;
    String version = null;
    String flag = input.readUTF();
    if (SIGN_FLAG.equals(flag)) {
        // 方法签名方式
        String sign = input.readUTF();
        MethodInfo mInfo = SIGN_METHOD_MAP.get(sign);
        if (mInfo == null) {
            throw new MotanFrameworkException("decode error: invalid method sign: " + sign, MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
        }
        interfaceName = mInfo.getInterfaceName();
        methodName = mInfo.getMethodName();
        paramtersDesc = mInfo.getParamtersDesc();
        group = mInfo.getGroup();
        version = mInfo.getVersion();
    } else {
        interfaceName = flag;
        methodName = input.readUTF();
        paramtersDesc = input.readUTF();
    }
    DefaultRequest rpcRequest = new DefaultRequest();
    rpcRequest.setRequestId(requestId);
    rpcRequest.setInterfaceName(interfaceName);
    rpcRequest.setMethodName(methodName);
    rpcRequest.setParamtersDesc(paramtersDesc);
    rpcRequest.setArguments(decodeRequestParameter(input, paramtersDesc, serialization));
    rpcRequest.setAttachments(decodeRequestAttachments(input));
    rpcRequest.setRpcProtocolVersion(RpcProtocolVersion.VERSION_2.getVersion());
    input.close();
    Map<String, String> attachments = rpcRequest.getAttachments();
    // 根据签名添加client固定参数。
    putSignedAttachment(attachments, remoteIp);
    if (attachments.get(URLParamType.group.name()) == null) {
        // 如果attachment sign失效时,需要使用methodsign中的group信息。
        attachments.put(URLParamType.group.name(), group);
        attachments.put(URLParamType.version.name(), version);
    }
    return rpcRequest;
}
Also used : MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) ObjectInput(java.io.ObjectInput)

Example 29 with MotanFrameworkException

use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.

the class DefaultRpcCodec method decode.

/**
     * decode data
     * 
     * <pre>
	 * 		对于client端:主要是来自server端的response or exception
	 * 		对于server端: 主要是来自client端的request
	 * </pre>
     * 
     * @param data
     * @return
     * @throws IOException
     */
@Override
public Object decode(Channel channel, String remoteIp, byte[] data) throws IOException {
    if (data.length <= RpcProtocolVersion.VERSION_1.getHeaderLength()) {
        throw new MotanFrameworkException("decode error: format problem", MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
    }
    short type = ByteUtil.bytes2short(data, 0);
    if (type != MAGIC) {
        throw new MotanFrameworkException("decode error: magic error", MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
    }
    if (data[2] != RpcProtocolVersion.VERSION_1.getVersion()) {
        throw new MotanFrameworkException("decode error: version error", MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
    }
    int bodyLength = ByteUtil.bytes2int(data, 12);
    if (RpcProtocolVersion.VERSION_1.getHeaderLength() + bodyLength != data.length) {
        throw new MotanFrameworkException("decode error: content length error", MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
    }
    byte flag = data[3];
    byte dataType = (byte) (flag & MASK);
    boolean isResponse = (dataType != MotanConstants.FLAG_REQUEST);
    byte[] body = new byte[bodyLength];
    System.arraycopy(data, RpcProtocolVersion.VERSION_1.getHeaderLength(), body, 0, bodyLength);
    long requestId = ByteUtil.bytes2long(data, 4);
    Serialization serialization = ExtensionLoader.getExtensionLoader(Serialization.class).getExtension(channel.getUrl().getParameter(URLParamType.serialize.getName(), URLParamType.serialize.getValue()));
    try {
        if (isResponse) {
            // response
            return decodeResponse(body, dataType, requestId, serialization);
        } else {
            return decodeRequest(body, requestId, serialization);
        }
    } catch (ClassNotFoundException e) {
        throw new MotanFrameworkException("decode " + (isResponse ? "response" : "request") + " error: class not found", e, MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
    } catch (Exception e) {
        if (ExceptionUtil.isMotanException(e)) {
            throw (RuntimeException) e;
        } else {
            throw new MotanFrameworkException("decode error: isResponse=" + isResponse, e, MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
        }
    }
}
Also used : Serialization(com.weibo.api.motan.codec.Serialization) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) IOException(java.io.IOException) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException)

Example 30 with MotanFrameworkException

use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.

the class ServiceConfig method doExport.

@SuppressWarnings("unchecked")
private void doExport(ProtocolConfig protocolConfig, int port, List<URL> registryURLs) {
    String protocolName = protocolConfig.getName();
    if (protocolName == null || protocolName.length() == 0) {
        protocolName = URLParamType.protocol.getValue();
    }
    String hostAddress = host;
    if (StringUtils.isBlank(hostAddress) && basicServiceConfig != null) {
        hostAddress = basicServiceConfig.getHost();
    }
    if (NetUtils.isInvalidLocalHost(hostAddress)) {
        hostAddress = getLocalHostAddress(registryURLs);
    }
    Map<String, String> map = new HashMap<String, String>();
    map.put(URLParamType.nodeType.getName(), MotanConstants.NODE_TYPE_SERVICE);
    map.put(URLParamType.refreshTimestamp.getName(), String.valueOf(System.currentTimeMillis()));
    collectConfigParams(map, protocolConfig, basicServiceConfig, extConfig, this);
    collectMethodConfigParams(map, this.getMethods());
    URL serviceUrl = new URL(protocolName, hostAddress, port, interfaceClass.getName(), map);
    if (serviceExists(serviceUrl)) {
        LoggerUtil.warn(String.format("%s configService is malformed, for same service (%s) already exists ", interfaceClass.getName(), serviceUrl.getIdentity()));
        throw new MotanFrameworkException(String.format("%s configService is malformed, for same service (%s) already exists ", interfaceClass.getName(), serviceUrl.getIdentity()), MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR);
    }
    List<URL> urls = new ArrayList<URL>();
    // injvm 协议只支持注册到本地,其他协议可以注册到local、remote
    if (MotanConstants.PROTOCOL_INJVM.equals(protocolConfig.getId())) {
        URL localRegistryUrl = null;
        for (URL ru : registryURLs) {
            if (MotanConstants.REGISTRY_PROTOCOL_LOCAL.equals(ru.getProtocol())) {
                localRegistryUrl = ru.createCopy();
                break;
            }
        }
        if (localRegistryUrl == null) {
            localRegistryUrl = new URL(MotanConstants.REGISTRY_PROTOCOL_LOCAL, hostAddress, MotanConstants.DEFAULT_INT_VALUE, RegistryService.class.getName());
        }
        urls.add(localRegistryUrl);
    } else {
        for (URL ru : registryURLs) {
            urls.add(ru.createCopy());
        }
    }
    for (URL u : urls) {
        u.addParameter(URLParamType.embed.getName(), StringTools.urlEncode(serviceUrl.toFullStr()));
        registereUrls.add(u.createCopy());
    }
    ConfigHandler configHandler = ExtensionLoader.getExtensionLoader(ConfigHandler.class).getExtension(MotanConstants.DEFAULT_VALUE);
    exporters.add(configHandler.export(interfaceClass, ref, urls));
    initLocalAppInfo(serviceUrl);
}
Also used : MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) URL(com.weibo.api.motan.rpc.URL) ConfigHandler(com.weibo.api.motan.config.handler.ConfigHandler)

Aggregations

MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)41 URL (com.weibo.api.motan.rpc.URL)7 DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)6 ArrayList (java.util.ArrayList)6 MotanServiceException (com.weibo.api.motan.exception.MotanServiceException)5 IOException (java.io.IOException)5 Method (java.lang.reflect.Method)4 Registry (com.weibo.api.motan.registry.Registry)3 Response (com.weibo.api.motan.rpc.Response)3 HeartbeatFactory (com.weibo.api.motan.transport.HeartbeatFactory)3 ObjectInput (java.io.ObjectInput)3 HashMap (java.util.HashMap)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 Serialization (com.weibo.api.motan.codec.Serialization)2 ConfigHandler (com.weibo.api.motan.config.handler.ConfigHandler)2 CommandListener (com.weibo.api.motan.registry.support.command.CommandListener)2 ServiceListener (com.weibo.api.motan.registry.support.command.ServiceListener)2 TransportException (com.weibo.api.motan.transport.TransportException)2 Map (java.util.Map)2 IZkChildListener (org.I0Itec.zkclient.IZkChildListener)2