提交 be5992a2 编写于 作者: W william.liangf

DUBBO-146 当Response序列化失败时发送序列化异常信息,避免出现超时异常,误导排错。

git-svn-id: http://code.alibabatech.com/svn/dubbo/trunk@695 1a56cb94-b969-4eaa-88fa-be21384802f2
上级 5cd3f375
......@@ -22,6 +22,7 @@ package com.alibaba.dubbo.remoting.exchange;
* @author william.liangf
*/
public class Response {
public static final String HEARTBEAT_EVENT = null;
public static final String READONLY_EVENT = "R";
......@@ -45,6 +46,11 @@ public class Response {
* request format error.
*/
public static final byte BAD_REQUEST = 40;
/**
* response format error.
*/
public static final byte BAD_RESPONSE = 50;
/**
* service not found.
......
......@@ -35,6 +35,7 @@ import com.alibaba.dubbo.common.serialize.ObjectOutput;
import com.alibaba.dubbo.common.serialize.Serialization;
import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.remoting.Channel;
import com.alibaba.dubbo.remoting.RemotingException;
import com.alibaba.dubbo.remoting.exchange.Request;
import com.alibaba.dubbo.remoting.exchange.Response;
import com.alibaba.dubbo.remoting.exchange.support.DefaultFuture;
......@@ -218,7 +219,7 @@ public class ExchangeCodec extends TelnetCodec {
return req.getData();
}
protected void encodeRequest(Channel channel, OutputStream os, Request req) throws IOException {
protected void encodeRequest(Channel channel, OutputStream os, Request req) throws IOException {
Serialization serialization = getSerialization(channel);
// header.
byte[] header = new byte[HEADER_LENGTH];
......@@ -250,46 +251,69 @@ public class ExchangeCodec extends TelnetCodec {
// write
os.write(header); // write header.
os.write(data); // write data.
os.write(data); // write data.
}
protected void encodeResponse(Channel channel, OutputStream os, Response res) throws IOException {
Serialization serialization = getSerialization(channel);
// header.
byte[] header = new byte[HEADER_LENGTH];
// set magic number.
Bytes.short2bytes(MAGIC, header);
// set request and serialization flag.
header[2] = serialization.getContentTypeId();
if (res.isHeartbeat()) header[2] |= FLAG_EVENT;
// set response status.
byte status = res.getStatus();
header[3] = status;
// set request id.
Bytes.long2bytes(res.getId(), header, 4);
UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream(1024);
ObjectOutput out = serialization.serialize(channel.getUrl(), bos);
// encode response data or error message.
if (status == Response.OK) {
if (res.isHeartbeat()) {
encodeHeartbeatData(channel, out, res.getResult());
} else {
encodeResponseData(channel, out, res.getResult());
protected void encodeResponse(Channel channel, OutputStream os, Response res) throws IOException {
try {
Serialization serialization = getSerialization(channel);
// header.
byte[] header = new byte[HEADER_LENGTH];
// set magic number.
Bytes.short2bytes(MAGIC, header);
// set request and serialization flag.
header[2] = serialization.getContentTypeId();
if (res.isHeartbeat()) header[2] |= FLAG_EVENT;
// set response status.
byte status = res.getStatus();
header[3] = status;
// set request id.
Bytes.long2bytes(res.getId(), header, 4);
UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream(1024);
ObjectOutput out = serialization.serialize(channel.getUrl(), bos);
// encode response data or error message.
if (status == Response.OK) {
if (res.isHeartbeat()) {
encodeHeartbeatData(channel, out, res.getResult());
} else {
encodeResponseData(channel, out, res.getResult());
}
}
}
else out.writeUTF(res.getErrorMessage());
out.flushBuffer();
bos.flush();
bos.close();
byte[] data = bos.toByteArray();
Bytes.int2bytes(data.length, header, 12);
// write
os.write(header); // write header.
os.write(data); // write data.
}
else out.writeUTF(res.getErrorMessage());
out.flushBuffer();
bos.flush();
bos.close();
byte[] data = bos.toByteArray();
Bytes.int2bytes(data.length, header, 12);
// write
os.write(header); // write header.
os.write(data); // write data.
} catch (Throwable t) {
if (! res.isEvent() && res.getStatus() != Response.BAD_RESPONSE) {
try {
Response r = new Response(res.getId(), res.getVersion());
r.setStatus(Response.BAD_RESPONSE);
r.setErrorMessage("Failed to send response : " + res + ", cause: " + StringUtils.toString(t));
channel.send(r);
return;
} catch (RemotingException e) {
logger.warn("Failed to send error response: " + res + ", cause: " + e.getMessage(), e);
}
}
if (t instanceof IOException) {
throw (IOException) t;
} else if (t instanceof RuntimeException) {
throw (RuntimeException) t;
} else if (t instanceof Error) {
throw (Error) t;
} else {
throw new RuntimeException(t.getMessage(), t);
}
}
}
private static final Serialization getSerializationById(Byte id) {
return ID_SERIALIZATION_MAP.get(id);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册