Search in sources :

Example 21 with Request

use of com.weibo.api.motan.rpc.Request in project motan by weibocom.

the class MotanV2Codec method encode.

@Override
public byte[] encode(Channel channel, Object message) throws IOException {
    try {
        if (DefaultRpcHeartbeatFactory.isHeartbeatRequest(message)) {
            return encodeHeartbeat(((Request) message).getRequestId(), true);
        }
        if (DefaultRpcHeartbeatFactory.isHeartbeatResponse(message)) {
            return encodeHeartbeat(((Response) message).getRequestId(), false);
        }
        MotanV2Header header = new MotanV2Header();
        byte[] body = null;
        Serialization serialization;
        GrowableByteBuffer buf = new GrowableByteBuffer(4096);
        // meta
        int index = HEADER_SIZE;
        buf.position(index);
        // metasize
        buf.putInt(0);
        if (message instanceof Request) {
            String serialName = channel.getUrl().getParameter(URLParamType.serialize.getName(), URLParamType.serialize.getValue());
            serialization = ExtensionLoader.getExtensionLoader(Serialization.class).getExtension(serialName);
            if (serialization == null) {
                throw new MotanServiceException("can not found serialization " + serialName);
            }
            header.setSerialize(serialization.getSerializationNumber());
            Request request = (Request) message;
            putString(buf, M2_PATH);
            putString(buf, request.getInterfaceName());
            putString(buf, M2_METHOD);
            putString(buf, request.getMethodName());
            if (request.getParamtersDesc() != null) {
                putString(buf, M2_METHOD_DESC);
                putString(buf, request.getParamtersDesc());
            }
            if (request.getAttachments() != null && request.getAttachments().get(URLParamType.group.getName()) != null) {
                request.setAttachment(M2_GROUP, request.getAttachments().get(URLParamType.group.getName()));
            }
            putMap(buf, request.getAttachments());
            header.setRequestId(request.getRequestId());
            if (request.getArguments() != null) {
                body = serialization.serializeMulti(request.getArguments());
            }
        } else if (message instanceof Response) {
            Response response = (Response) message;
            serialization = getSerializationByNum(response.getSerializeNumber());
            header.setSerialize(serialization.getSerializationNumber());
            putString(buf, M2_PROCESS_TIME);
            putString(buf, String.valueOf(response.getProcessTime()));
            if (response.getException() != null) {
                putString(buf, M2_ERROR);
                putString(buf, ExceptionUtil.toMessage(response.getException()));
                header.setStatus(MotanV2Header.MessageStatus.EXCEPTION.getStatus());
            }
            putMap(buf, response.getAttachments());
            header.setRequestId(response.getRequestId());
            header.setRequest(false);
            if (response.getException() == null) {
                body = serialization.serialize(response.getValue());
            }
        }
        buf.position(buf.position() - 1);
        int metalength = buf.position() - index - 4;
        buf.putInt(index, metalength);
        // body
        if (body != null && body.length > 0) {
            if (channel.getUrl().getBooleanParameter(URLParamType.usegz.getName(), URLParamType.usegz.getBooleanValue()) && body.length > channel.getUrl().getIntParameter(URLParamType.mingzSize.getName(), URLParamType.mingzSize.getIntValue())) {
                try {
                    body = ByteUtil.gzip(body);
                    header.setGzip(true);
                } catch (IOException e) {
                    LoggerUtil.warn("MotanV2Codec encode gzip fail. so not gzip body.", e);
                }
            }
            buf.putInt(body.length);
            buf.put(body);
        } else {
            buf.putInt(0);
        }
        // header
        int position = buf.position();
        buf.position(0);
        buf.put(header.toBytes());
        buf.position(position);
        buf.flip();
        byte[] result = new byte[buf.remaining()];
        buf.get(result);
        return result;
    } catch (Exception e) {
        String errmsg = "";
        if (message != null) {
            if (message instanceof Request) {
                errmsg = "type:request, " + message.toString();
            } else {
                errmsg = "type:response, " + message.toString();
            }
        }
        LoggerUtil.warn("motan2 encode error." + errmsg, e);
        if (ExceptionUtil.isMotanException(e)) {
            throw (RuntimeException) e;
        } else {
            throw new MotanFrameworkException("encode error!" + errmsg + ", origin errmsg:" + e.getMessage(), e, MotanErrorMsgConstant.FRAMEWORK_ENCODE_ERROR);
        }
    }
}
Also used : Serialization(com.weibo.api.motan.codec.Serialization) DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) Response(com.weibo.api.motan.rpc.Response) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) Request(com.weibo.api.motan.rpc.Request) IOException(java.io.IOException) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 22 with Request

use of com.weibo.api.motan.rpc.Request in project motan by weibocom.

the class RoundRobinLoadBalanceTest method testSelect.

public void testSelect() {
    Request request = mockery.mock(Request.class);
    mockery.checking(new Expectations() {

        {
            for (int i = 0; i < referers.size(); i++) {
                if (i % 2 == 0) {
                    atLeast(0).of(referers.get(i)).isAvailable();
                    will(returnValue(true));
                } else {
                    atLeast(0).of(referers.get(i)).isAvailable();
                    will(returnValue(false));
                }
            }
        }
    });
    Referer<IHello> ref = roundRobinLoadBalance.select(request);
    for (int i = 0; i < referers.size(); i++) {
        if (i % 2 == 1) {
            assertNotSame(ref, referers.get(i));
        }
    }
    List<Referer<IHello>> refHolder = new ArrayList<Referer<IHello>>();
    roundRobinLoadBalance.selectToHolder(request, refHolder);
    assertEquals(refHolder.size(), referers.size() / 2);
}
Also used : Expectations(org.jmock.Expectations) Request(com.weibo.api.motan.rpc.Request) Referer(com.weibo.api.motan.rpc.Referer) ArrayList(java.util.ArrayList) IHello(com.weibo.api.motan.protocol.example.IHello)

Example 23 with Request

use of com.weibo.api.motan.rpc.Request in project motan by weibocom.

the class CompressRpcCodecTest method testCodecRequest.

public void testCodecRequest(Request request) throws Exception {
    byte[] bytes = rpcCodec.encode(channel, request);
    assertTrue(isCompressVersion(bytes));
    Request result = (Request) rpcCodec.decode(channel, "", bytes);
    Assert.assertTrue(equals(request, result));
}
Also used : Request(com.weibo.api.motan.rpc.Request) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest)

Example 24 with Request

use of com.weibo.api.motan.rpc.Request in project motan by weibocom.

the class CompressRpcCodecTest method testCompatibility.

// 测试server端对旧版本的兼容性
@Test
public void testCompatibility() throws IOException {
    DefaultRequest request = getRequest("int[]", new Object[] { new int[] { 1, 2 } });
    Codec v1Codec = new DefaultRpcCodec();
    byte[] bytes = v1Codec.encode(channel, request);
    assertTrue(isV1Version(bytes));
    Request result = (Request) rpcCodec.decode(channel, "", bytes);
    Assert.assertTrue(equals(request, result));
}
Also used : Codec(com.weibo.api.motan.codec.Codec) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) Request(com.weibo.api.motan.rpc.Request) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) Test(org.junit.Test)

Example 25 with Request

use of com.weibo.api.motan.rpc.Request in project motan by weibocom.

the class FailfastHaStrategyTest method testCall.

@Test
@SuppressWarnings("unchecked")
public void testCall() {
    final LoadBalance<IWorld> loadBalance = mockery.mock(LoadBalance.class);
    final Referer<IWorld> referer = mockery.mock(Referer.class);
    final Request request = mockery.mock(Request.class);
    final Response response = mockery.mock(Response.class);
    mockery.checking(new Expectations() {

        {
            one(loadBalance).select(request);
            will(returnValue(referer));
            one(referer).call(request);
            will(returnValue(response));
        }
    });
    assertEquals(response, failfastHaStrategy.call(request, loadBalance));
}
Also used : Response(com.weibo.api.motan.rpc.Response) Expectations(org.jmock.Expectations) IWorld(com.weibo.api.motan.protocol.example.IWorld) Request(com.weibo.api.motan.rpc.Request) Test(org.junit.Test)

Aggregations

Request (com.weibo.api.motan.rpc.Request)31 DefaultRequest (com.weibo.api.motan.rpc.DefaultRequest)12 Expectations (org.jmock.Expectations)12 Response (com.weibo.api.motan.rpc.Response)10 IHello (com.weibo.api.motan.protocol.example.IHello)9 URL (com.weibo.api.motan.rpc.URL)9 Test (org.junit.Test)9 DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)7 MotanServiceException (com.weibo.api.motan.exception.MotanServiceException)6 Referer (com.weibo.api.motan.rpc.Referer)5 HashMap (java.util.HashMap)5 RegistryService (com.weibo.api.motan.registry.RegistryService)4 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)3 IWorld (com.weibo.api.motan.protocol.example.IWorld)3 Channel (com.weibo.api.motan.transport.Channel)3 MessageHandler (com.weibo.api.motan.transport.MessageHandler)3 YarRequest (com.weibo.yar.YarRequest)3 ArrayList (java.util.ArrayList)3 Serialization (com.weibo.api.motan.codec.Serialization)2 IOException (java.io.IOException)2