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;
}
}
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));
}
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));
}
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"));
}
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));
}
Aggregations