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