Search in sources :

Example 16 with Invocation

use of org.apache.dubbo.rpc.Invocation in project dubbo by alibaba.

the class DubboCodec method decodeBody.

@Override
protected Object decodeBody(Channel channel, InputStream is, byte[] header) throws IOException {
    byte flag = header[2], proto = (byte) (flag & SERIALIZATION_MASK);
    // get request id.
    long id = Bytes.bytes2long(header, 4);
    if ((flag & FLAG_REQUEST) == 0) {
        // decode response.
        Response res = new Response(id);
        if ((flag & FLAG_EVENT) != 0) {
            res.setEvent(true);
        }
        // get status.
        byte status = header[3];
        res.setStatus(status);
        try {
            if (status == Response.OK) {
                Object data;
                if (res.isEvent()) {
                    byte[] eventPayload = CodecSupport.getPayload(is);
                    if (CodecSupport.isHeartBeat(eventPayload, proto)) {
                        // heart beat response data is always null;
                        data = null;
                    } else {
                        ObjectInput in = CodecSupport.deserialize(channel.getUrl(), new ByteArrayInputStream(eventPayload), proto);
                        data = decodeEventData(channel, in, eventPayload);
                    }
                } else {
                    DecodeableRpcResult result;
                    if (channel.getUrl().getParameter(DECODE_IN_IO_THREAD_KEY, DEFAULT_DECODE_IN_IO_THREAD)) {
                        result = new DecodeableRpcResult(channel, res, is, (Invocation) getRequestData(id), proto);
                        result.decode();
                    } else {
                        result = new DecodeableRpcResult(channel, res, new UnsafeByteArrayInputStream(readMessageData(is)), (Invocation) getRequestData(id), proto);
                    }
                    data = result;
                }
                res.setResult(data);
            } else {
                ObjectInput in = CodecSupport.deserialize(channel.getUrl(), is, proto);
                res.setErrorMessage(in.readUTF());
            }
        } catch (Throwable t) {
            if (log.isWarnEnabled()) {
                log.warn("Decode response failed: " + t.getMessage(), t);
            }
            res.setStatus(Response.CLIENT_ERROR);
            res.setErrorMessage(StringUtils.toString(t));
        }
        return res;
    } else {
        // decode request.
        Request req = new Request(id);
        req.setVersion(Version.getProtocolVersion());
        req.setTwoWay((flag & FLAG_TWOWAY) != 0);
        if ((flag & FLAG_EVENT) != 0) {
            req.setEvent(true);
        }
        try {
            Object data;
            if (req.isEvent()) {
                byte[] eventPayload = CodecSupport.getPayload(is);
                if (CodecSupport.isHeartBeat(eventPayload, proto)) {
                    // heart beat response data is always null;
                    data = null;
                } else {
                    ObjectInput in = CodecSupport.deserialize(channel.getUrl(), new ByteArrayInputStream(eventPayload), proto);
                    data = decodeEventData(channel, in, eventPayload);
                }
            } else {
                DecodeableRpcInvocation inv;
                if (channel.getUrl().getParameter(DECODE_IN_IO_THREAD_KEY, DEFAULT_DECODE_IN_IO_THREAD)) {
                    inv = new DecodeableRpcInvocation(channel, req, is, proto);
                    inv.decode();
                } else {
                    inv = new DecodeableRpcInvocation(channel, req, new UnsafeByteArrayInputStream(readMessageData(is)), proto);
                }
                data = inv;
            }
            req.setData(data);
        } catch (Throwable t) {
            if (log.isWarnEnabled()) {
                log.warn("Decode request failed: " + t.getMessage(), t);
            }
            // bad request
            req.setBroken(true);
            req.setData(t);
        }
        return req;
    }
}
Also used : Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Request(org.apache.dubbo.remoting.exchange.Request) UnsafeByteArrayInputStream(org.apache.dubbo.common.io.UnsafeByteArrayInputStream) AppResponse(org.apache.dubbo.rpc.AppResponse) Response(org.apache.dubbo.remoting.exchange.Response) ByteArrayInputStream(java.io.ByteArrayInputStream) UnsafeByteArrayInputStream(org.apache.dubbo.common.io.UnsafeByteArrayInputStream) ObjectInput(org.apache.dubbo.common.serialize.ObjectInput)

Example 17 with Invocation

use of org.apache.dubbo.rpc.Invocation in project dubbo by alibaba.

the class RpcUtilsTest method testAttachInvocationIdIfAsync_forceAttache.

/**
 * scenario: explicitly configure to add attachment
 * verify: id attribute added in attachment
 */
@Test
public void testAttachInvocationIdIfAsync_forceAttache() {
    URL url = URL.valueOf("dubbo://localhost/?" + AUTO_ATTACH_INVOCATIONID_KEY + "=true");
    Invocation inv = new RpcInvocation("test", "DemoService", "", new Class[] {}, new String[] {});
    RpcUtils.attachInvocationIdIfAsync(url, inv);
    assertNotNull(RpcUtils.getInvocationId(inv));
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 18 with Invocation

use of org.apache.dubbo.rpc.Invocation in project dubbo by alibaba.

the class RpcUtilsTest method testIsOneway.

@Test
public void testIsOneway() {
    URL url1 = URL.valueOf("dubbo://localhost/?test.return=false");
    Invocation inv1 = new RpcInvocation("test", "DemoService", "", new Class[] {}, new String[] {});
    inv1.setAttachment("test." + RETURN_KEY, Boolean.TRUE.toString());
    Assertions.assertFalse(RpcUtils.isOneway(url1, inv1));
    URL url2 = URL.valueOf("dubbo://localhost/?test.return=false");
    Invocation inv2 = new RpcInvocation("test", "DemoService", "", new Class[] {}, new String[] {});
    inv2.setAttachment(RETURN_KEY, Boolean.TRUE.toString());
    Assertions.assertFalse(RpcUtils.isOneway(url2, inv2));
    URL url3 = URL.valueOf("dubbo://localhost/?test");
    Invocation inv3 = new RpcInvocation("test", "DemoService", "", new Class[] {}, new String[] {});
    inv3.setAttachment(RETURN_KEY, Boolean.FALSE.toString());
    Assertions.assertTrue(RpcUtils.isOneway(url3, inv3));
    URL url4 = URL.valueOf("dubbo://localhost/?test.return=false");
    Invocation inv4 = new RpcInvocation("test", "DemoService", "", new Class[] {}, new String[] {});
    Assertions.assertTrue(RpcUtils.isOneway(url4, inv4));
    URL url5 = URL.valueOf("dubbo://localhost/?test");
    Invocation inv5 = new RpcInvocation("test", "DemoService", "", new Class[] {}, new String[] {});
    Assertions.assertFalse(RpcUtils.isOneway(url5, inv5));
    URL url6 = URL.valueOf("dubbo://localhost/?test");
    Invocation inv6 = new RpcInvocation("test", "DemoService", "", new Class[] {}, new String[] {});
    inv6.setAttachment("test." + RETURN_KEY, Boolean.FALSE.toString());
    Assertions.assertTrue(RpcUtils.isOneway(url6, inv6));
    URL url7 = URL.valueOf("dubbo://localhost/?test");
    Invocation inv7 = new RpcInvocation("test", "DemoService", "", new Class[] {}, new String[] {});
    inv7.setAttachment("testB." + RETURN_KEY, Boolean.FALSE.toString());
    Assertions.assertFalse(RpcUtils.isOneway(url7, inv7));
    URL url8 = URL.valueOf("dubbo://localhost/?test");
    Invocation inv8 = new RpcInvocation("test", "DemoService", "", new Class[] {}, new String[] {});
    inv8.setAttachment(RETURN_KEY, Boolean.FALSE.toString());
    inv8.setAttachment("test." + RETURN_KEY, Boolean.TRUE.toString());
    Assertions.assertFalse(RpcUtils.isOneway(url8, inv8));
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 19 with Invocation

use of org.apache.dubbo.rpc.Invocation in project dubbo by alibaba.

the class RpcUtilsTest method testAttachInvocationIdIfAsync_normal.

/**
 * regular scenario: async invocation in URL
 * verify: 1. whether invocationId is set correctly, 2. idempotent or not
 */
@Test
public void testAttachInvocationIdIfAsync_normal() {
    URL url = URL.valueOf("dubbo://localhost/?test.async=true");
    Map<String, Object> attachments = new HashMap<>();
    attachments.put("aa", "bb");
    Invocation inv = new RpcInvocation("test", "DemoService", "", new Class[] {}, new String[] {}, attachments);
    RpcUtils.attachInvocationIdIfAsync(url, inv);
    long id1 = RpcUtils.getInvocationId(inv);
    RpcUtils.attachInvocationIdIfAsync(url, inv);
    long id2 = RpcUtils.getInvocationId(inv);
    // verify if it's idempotent
    assertEquals(id1, id2);
    assertTrue(id1 >= 0);
    assertEquals("bb", attachments.get("aa"));
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) HashMap(java.util.HashMap) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 20 with Invocation

use of org.apache.dubbo.rpc.Invocation in project dubbo by alibaba.

the class RpcUtilsTest method testAttachInvocationIdIfAsync_forceNotAttache.

/**
 * scenario: explicitly configure to not add attachment
 * verify: no id attribute added in attachment
 */
@Test
public void testAttachInvocationIdIfAsync_forceNotAttache() {
    URL url = URL.valueOf("dubbo://localhost/?test.async=true&" + AUTO_ATTACH_INVOCATIONID_KEY + "=false");
    Invocation inv = new RpcInvocation("test", "DemoService", "", new Class[] {}, new String[] {});
    RpcUtils.attachInvocationIdIfAsync(url, inv);
    assertNull(RpcUtils.getInvocationId(inv));
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Invocation (org.apache.dubbo.rpc.Invocation)98 Test (org.junit.jupiter.api.Test)78 URL (org.apache.dubbo.common.URL)77 Invoker (org.apache.dubbo.rpc.Invoker)44 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)44 Result (org.apache.dubbo.rpc.Result)33 AppResponse (org.apache.dubbo.rpc.AppResponse)29 RpcException (org.apache.dubbo.rpc.RpcException)18 MockInvocation (org.apache.dubbo.rpc.support.MockInvocation)17 HashMap (java.util.HashMap)16 AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)13 BlockMyInvoker (org.apache.dubbo.rpc.support.BlockMyInvoker)11 MyInvoker (org.apache.dubbo.rpc.support.MyInvoker)10 ArrayList (java.util.ArrayList)8 Person (org.apache.dubbo.rpc.support.Person)7 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 IMetricManager (com.alibaba.metrics.IMetricManager)5 Method (java.lang.reflect.Method)5 DemoService (org.apache.dubbo.monitor.dubbo.service.DemoService)5 FastCompass (com.alibaba.metrics.FastCompass)4