Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[develop] add request type field at the head of task data, exchange parse order between Log and GetRequest #7871

Merged
merged 2 commits into from
Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@
*/
public class ProtoMessageUtil {

/**
* should be different from field tags of ReadRequest or WriteQuest.
*/
public static final int REQUEST_TYPE_FIELD_TAG = 7 << 3;

public static final int REQUEST_TYPE_READ = 1;

public static final int REQUEST_TYPE_WRITE = 2;

/**
* Converts the byte array to a specific Protobuf object.
* Internally, the protobuf new and old objects are compatible.
Expand All @@ -40,26 +49,27 @@ public class ProtoMessageUtil {
public static Message parse(byte[] bytes) {
Message result;
try {
result = ReadRequest.parseFrom(bytes);
return result;
} catch (Throwable ignore) {
}
try {
result = WriteRequest.parseFrom(bytes);
return result;
if (bytes[0] == REQUEST_TYPE_FIELD_TAG) {
if (bytes[1] == REQUEST_TYPE_READ) {
result = ReadRequest.parseFrom(bytes);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bytes中添加了2个标示位, parse之前应该移除掉这两个标示位,才能正确解析吧?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不用删除,只是解析的结果多了个unknown fields,field的名称为编号7,grpc服务端和客户端可以通过unknown fields配置自己特有的字段,目的就是为了相互兼容老版本

} else {
result = WriteRequest.parseFrom(bytes);
}
return result;
}
} catch (Throwable ignore) {
}

// old consistency entity, will be @Deprecated in future
try {
Log log = Log.parseFrom(bytes);
return convertToWriteRequest(log);
GetRequest request = GetRequest.parseFrom(bytes);
return convertToReadRequest(request);
} catch (Throwable ignore) {
}

try {
GetRequest request = GetRequest.parseFrom(bytes);
return convertToReadRequest(request);
Log log = Log.parseFrom(bytes);
return convertToWriteRequest(log);
} catch (Throwable ignore) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.alibaba.nacos.common.utils.LoggerUtils;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.common.utils.ThreadUtils;
import com.alibaba.nacos.consistency.ProtoMessageUtil;
import com.alibaba.nacos.consistency.RequestProcessor;
import com.alibaba.nacos.consistency.SerializeFactory;
import com.alibaba.nacos.consistency.Serializer;
Expand Down Expand Up @@ -410,7 +411,19 @@ public void applyOperation(Node node, Message data, FailoverClosure closure) {
closure.setResponse(nacosStatus.getResponse());
closure.run(nacosStatus);
}));
task.setData(ByteBuffer.wrap(data.toByteArray()));

// add request type field at the head of task data.
byte[] requestTypeFieldBytes = new byte[2];
requestTypeFieldBytes[0] = ProtoMessageUtil.REQUEST_TYPE_FIELD_TAG;
if (data instanceof ReadRequest) {
requestTypeFieldBytes[1] = ProtoMessageUtil.REQUEST_TYPE_READ;
} else {
requestTypeFieldBytes[1] = ProtoMessageUtil.REQUEST_TYPE_WRITE;
}

byte[] dataBytes = data.toByteArray();
task.setData((ByteBuffer) ByteBuffer.allocate(requestTypeFieldBytes.length + dataBytes.length)
.put(requestTypeFieldBytes).put(dataBytes).position(0));
node.apply(task);
}

Expand Down