From 39d88c1d17c15698f2825ff54f664dfba07469cf Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Mon, 25 Dec 2023 18:21:34 +0800 Subject: [PATCH 01/62] load SeataSerializer by version --- .../core/protocol/ProtocolConstants.java | 10 +++ .../core/rpc/netty/v1/ProtocolV1Decoder.java | 2 +- .../core/rpc/netty/v1/ProtocolV1Encoder.java | 2 +- .../serializer/SerializerServiceLoader.java | 27 +++++- .../serializer/seata/MessageCodecFactory.java | 16 ++-- .../serializer/seata/SeataSerializer.java | 36 ++++++-- .../protocol/BatchResultMessageCodec.java | 9 +- .../protocol/MergeResultMessageCodec.java | 9 +- .../protocol/MergedWarpMessageCodec.java | 9 +- .../protocol/v0/MessageCodecFactoryV0.java | 82 +++++++++++++++++++ .../protocol/v1/MessageCodecFactoryV1.java | 29 +++++++ 11 files changed, 202 insertions(+), 29 deletions(-) create mode 100644 serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/MessageCodecFactoryV0.java create mode 100644 serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v1/MessageCodecFactoryV1.java diff --git a/core/src/main/java/io/seata/core/protocol/ProtocolConstants.java b/core/src/main/java/io/seata/core/protocol/ProtocolConstants.java index 7b851ebc2a5..c62438ea805 100644 --- a/core/src/main/java/io/seata/core/protocol/ProtocolConstants.java +++ b/core/src/main/java/io/seata/core/protocol/ProtocolConstants.java @@ -31,6 +31,16 @@ public interface ProtocolConstants { */ byte[] MAGIC_CODE_BYTES = {(byte) 0xda, (byte) 0xda}; + /** + * Old protocol version + */ + byte VERSION_0 = 0; + + /** + * Protocol version + */ + byte VERSION_1 = 1; + /** * Protocol version */ diff --git a/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Decoder.java b/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Decoder.java index 867e27d9457..5584acec700 100644 --- a/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Decoder.java +++ b/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Decoder.java @@ -142,7 +142,7 @@ public Object decodeFrame(ByteBuf frame) { frame.readBytes(bs); Compressor compressor = CompressorFactory.getCompressor(compressorType); bs = compressor.decompress(bs); - Serializer serializer = SerializerServiceLoader.load(SerializerType.getByCode(rpcMessage.getCodec())); + Serializer serializer = SerializerServiceLoader.load(SerializerType.getByCode(rpcMessage.getCodec()), ProtocolConstants.VERSION_1); rpcMessage.setBody(serializer.deserialize(bs)); } } diff --git a/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Encoder.java b/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Encoder.java index 0f35eff8492..ff66ef56997 100644 --- a/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Encoder.java +++ b/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Encoder.java @@ -92,7 +92,7 @@ public void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) { if (messageType != ProtocolConstants.MSGTYPE_HEARTBEAT_REQUEST && messageType != ProtocolConstants.MSGTYPE_HEARTBEAT_RESPONSE) { // heartbeat has no body - Serializer serializer = SerializerServiceLoader.load(SerializerType.getByCode(rpcMessage.getCodec())); + Serializer serializer = SerializerServiceLoader.load(SerializerType.getByCode(rpcMessage.getCodec()), ProtocolConstants.VERSION_1); bodyBytes = serializer.serialize(rpcMessage.getBody()); Compressor compressor = CompressorFactory.getCompressor(rpcMessage.getCompressor()); bodyBytes = compressor.compress(bodyBytes); diff --git a/core/src/main/java/io/seata/core/serializer/SerializerServiceLoader.java b/core/src/main/java/io/seata/core/serializer/SerializerServiceLoader.java index 67c23c29127..fea9ab46567 100644 --- a/core/src/main/java/io/seata/core/serializer/SerializerServiceLoader.java +++ b/core/src/main/java/io/seata/core/serializer/SerializerServiceLoader.java @@ -20,6 +20,9 @@ import io.seata.common.loader.EnhancedServiceNotFoundException; import io.seata.common.util.ReflectionUtil; +import java.util.HashMap; +import java.util.Map; + /** * The Service Loader for the interface {@link Serializer} * @@ -29,6 +32,7 @@ public final class SerializerServiceLoader { private SerializerServiceLoader() { } + private static Map serializerMap = new HashMap<>(); private static final String PROTOBUF_SERIALIZER_CLASS_NAME = "io.seata.serializer.protobuf.ProtobufSerializer"; @@ -39,7 +43,7 @@ private SerializerServiceLoader() { * @return the service of {@link Serializer} * @throws EnhancedServiceNotFoundException the enhanced service not found exception */ - public static Serializer load(SerializerType type) throws EnhancedServiceNotFoundException { + public static Serializer load(SerializerType type, byte version) throws EnhancedServiceNotFoundException { if (type == SerializerType.PROTOBUF) { try { ReflectionUtil.getClassByName(PROTOBUF_SERIALIZER_CLASS_NAME); @@ -48,6 +52,25 @@ public static Serializer load(SerializerType type) throws EnhancedServiceNotFoun "Please manually reference 'io.seata:seata-serializer-protobuf' dependency ", e); } } - return EnhancedServiceLoader.load(Serializer.class, type.name()); + + String key = serialzerKey(type, version); + Serializer serializer = serializerMap.get(key); + if (serializer == null) { + if (type == SerializerType.SEATA) { + serializer = EnhancedServiceLoader.load(Serializer.class, type.name(), new Object[]{version}); + } else { + serializer = EnhancedServiceLoader.load(Serializer.class, type.name()); + } + serializerMap.put(key,serializer); + } + return serializer; + + } + + private static String serialzerKey(SerializerType type, byte version){ + if (type == SerializerType.SEATA) { + return type.name() + version; + } + return type.name(); } } diff --git a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/MessageCodecFactory.java b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/MessageCodecFactory.java index 6aeaa904899..8b6a9a21f84 100644 --- a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/MessageCodecFactory.java +++ b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/MessageCodecFactory.java @@ -82,7 +82,7 @@ * The type Message codec factory. * */ -public class MessageCodecFactory { +public abstract class MessageCodecFactory { /** * The constant UTF8. @@ -95,7 +95,7 @@ public class MessageCodecFactory { * @param abstractMessage the abstract message * @return the message codec */ - public static MessageSeataCodec getMessageCodec(AbstractMessage abstractMessage) { + public MessageSeataCodec getMessageCodec(AbstractMessage abstractMessage) { return getMessageCodec(abstractMessage.getTypeCode()); } @@ -105,7 +105,7 @@ public static MessageSeataCodec getMessageCodec(AbstractMessage abstractMessage) * @param typeCode the type code * @return the msg instance by code */ - public static MessageSeataCodec getMessageCodec(short typeCode) { + public MessageSeataCodec getMessageCodec(short typeCode) { MessageSeataCodec msgCodec = null; switch (typeCode) { case MessageType.TYPE_SEATA_MERGE: @@ -166,7 +166,7 @@ public static MessageSeataCodec getMessageCodec(short typeCode) { * @param typeCode the type code * @return the merge request instance by code */ - protected static MessageSeataCodec getMergeRequestMessageSeataCodec(int typeCode) { + protected MessageSeataCodec getMergeRequestMessageSeataCodec(int typeCode) { switch (typeCode) { case MessageType.TYPE_GLOBAL_BEGIN: return new GlobalBeginRequestCodec(); @@ -195,7 +195,7 @@ protected static MessageSeataCodec getMergeRequestMessageSeataCodec(int typeCode * @param typeCode the type code * @return the merge response instance by code */ - protected static MessageSeataCodec getMergeResponseMessageSeataCodec(int typeCode) { + protected MessageSeataCodec getMergeResponseMessageSeataCodec(int typeCode) { switch (typeCode) { case MessageType.TYPE_GLOBAL_BEGIN_RESULT: return new GlobalBeginResponseCodec(); @@ -230,7 +230,7 @@ protected static MessageSeataCodec getMergeResponseMessageSeataCodec(int typeCod * @param typeCode the type code * @return the message */ - public static AbstractMessage getMessage(short typeCode) { + public AbstractMessage getMessage(short typeCode) { AbstractMessage abstractMessage = null; switch (typeCode) { case MessageType.TYPE_SEATA_MERGE: @@ -295,7 +295,7 @@ public static AbstractMessage getMessage(short typeCode) { * @param typeCode the type code * @return the merge request instance by code */ - protected static AbstractMessage getMergeRequestInstanceByCode(int typeCode) { + protected AbstractMessage getMergeRequestInstanceByCode(int typeCode) { switch (typeCode) { case MessageType.TYPE_GLOBAL_BEGIN: return new GlobalBeginRequest(); @@ -324,7 +324,7 @@ protected static AbstractMessage getMergeRequestInstanceByCode(int typeCode) { * @param typeCode the type code * @return the merge response instance by code */ - protected static AbstractMessage getMergeResponseInstanceByCode(int typeCode) { + protected AbstractMessage getMergeResponseInstanceByCode(int typeCode) { switch (typeCode) { case MessageType.TYPE_GLOBAL_BEGIN_RESULT: return new GlobalBeginResponse(); diff --git a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java index 3e94cbf9749..ef34992eb5d 100644 --- a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java +++ b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java @@ -20,28 +20,43 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; +import io.seata.common.exception.NotSupportYetException; import io.seata.common.loader.LoadLevel; +import io.seata.common.loader.Scope; import io.seata.common.util.BufferUtils; import io.seata.core.protocol.AbstractMessage; +import io.seata.core.protocol.ProtocolConstants; import io.seata.core.serializer.Serializer; +import io.seata.serializer.seata.protocol.v1.MessageCodecFactoryV1; /** * The Seata codec. * */ -@LoadLevel(name = "SEATA") +@LoadLevel(name = "SEATA", scope = Scope.PROTOTYPE) public class SeataSerializer implements Serializer { + MessageCodecFactory factory; + byte protocolVersion ; + + public SeataSerializer(Byte version){ + if (version == ProtocolConstants.VERSION_1) { + factory = new MessageCodecFactoryV1(); + }else { + throw new NotSupportYetException("not support version" + version); + } + protocolVersion = version; + } @Override public byte[] serialize(T t) { if (!(t instanceof AbstractMessage)) { throw new IllegalArgumentException("AbstractMessage isn't available."); } AbstractMessage abstractMessage = (AbstractMessage)t; - //typecode + //type code short typecode = abstractMessage.getTypeCode(); //msg codec - MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typecode); + MessageSeataCodec messageCodec = factory.getMessageCodec(typecode); //get empty ByteBuffer ByteBuf out = Unpooled.buffer(1024); //msg encode @@ -49,9 +64,14 @@ public byte[] serialize(T t) { byte[] body = new byte[out.readableBytes()]; out.readBytes(body); - //typecode + body - ByteBuffer byteBuffer = ByteBuffer.allocate(2 + body.length); - byteBuffer.putShort(typecode); + ByteBuffer byteBuffer; + if (protocolVersion == ProtocolConstants.VERSION_0) { + byteBuffer = ByteBuffer.allocate(body.length); + } else { + //typecode + body + byteBuffer = ByteBuffer.allocate(2 + body.length); + byteBuffer.putShort(typecode); + } byteBuffer.put(body); BufferUtils.flip(byteBuffer); @@ -76,9 +96,9 @@ public T deserialize(byte[] bytes) { byteBuffer.get(body); ByteBuffer in = ByteBuffer.wrap(body); //new Messgae - AbstractMessage abstractMessage = MessageCodecFactory.getMessage(typecode); + AbstractMessage abstractMessage = factory.getMessage(typecode); //get messageCodec - MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typecode); + MessageSeataCodec messageCodec = factory.getMessageCodec(typecode); //decode messageCodec.decode(abstractMessage, in); return (T)abstractMessage; diff --git a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/BatchResultMessageCodec.java b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/BatchResultMessageCodec.java index 9a280d10262..a81ad1a874e 100644 --- a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/BatchResultMessageCodec.java +++ b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/BatchResultMessageCodec.java @@ -27,6 +27,7 @@ import io.seata.core.protocol.BatchResultMessage; import io.seata.serializer.seata.MessageCodecFactory; import io.seata.serializer.seata.MessageSeataCodec; +import io.seata.serializer.seata.protocol.v1.MessageCodecFactoryV1; /** * the type batch result message codec @@ -35,6 +36,8 @@ */ public class BatchResultMessageCodec extends AbstractMessageCodec { + protected MessageCodecFactory factory = new MessageCodecFactoryV1(); + @Override public Class getMessageClassType() { return BatchResultMessage.class; @@ -53,7 +56,7 @@ public void encode(T t, ByteBuf out) { for (final AbstractMessage msg : msgs) { final ByteBuf subBuffer = Unpooled.buffer(1024); short typeCode = msg.getTypeCode(); - MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typeCode); + MessageSeataCodec messageCodec = factory.getMessageCodec(typeCode); messageCodec.encode(msg, subBuffer); buffer.writeShort(msg.getTypeCode()); buffer.writeBytes(subBuffer); @@ -106,8 +109,8 @@ protected void decode(BatchResultMessage batchResultMessage, ByteBuffer byteBuff List msgIds = new ArrayList<>(); for (int idx = 0; idx < msgNum; idx++) { short typeCode = byteBuffer.getShort(); - AbstractMessage abstractResultMessage = MessageCodecFactory.getMessage(typeCode); - MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typeCode); + AbstractMessage abstractResultMessage = factory.getMessage(typeCode); + MessageSeataCodec messageCodec = factory.getMessageCodec(typeCode); messageCodec.decode(abstractResultMessage, byteBuffer); msgs.add((AbstractResultMessage) abstractResultMessage); } diff --git a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/MergeResultMessageCodec.java b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/MergeResultMessageCodec.java index 17eeb3b64b9..100ae839963 100644 --- a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/MergeResultMessageCodec.java +++ b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/MergeResultMessageCodec.java @@ -24,6 +24,7 @@ import io.seata.core.protocol.AbstractMessage; import io.seata.core.protocol.AbstractResultMessage; import io.seata.core.protocol.MergeResultMessage; +import io.seata.serializer.seata.protocol.v1.MessageCodecFactoryV1; /** * The type Merge result message codec. @@ -31,6 +32,8 @@ */ public class MergeResultMessageCodec extends AbstractMessageCodec { + protected MessageCodecFactory factory = new MessageCodecFactoryV1(); + @Override public Class getMessageClassType() { return MergeResultMessage.class; @@ -48,7 +51,7 @@ public void encode(T t, ByteBuf out) { short typeCode = msg.getTypeCode(); //put typeCode out.writeShort(typeCode); - MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typeCode); + MessageSeataCodec messageCodec = factory.getMessageCodec(typeCode); messageCodec.encode(msg, out); } @@ -90,8 +93,8 @@ protected void decode(MergeResultMessage mergeResultMessage, ByteBuffer byteBuff AbstractResultMessage[] msgs = new AbstractResultMessage[msgNum]; for (int idx = 0; idx < msgNum; idx++) { short typeCode = byteBuffer.getShort(); - AbstractMessage abstractResultMessage = MessageCodecFactory.getMessage(typeCode); - MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typeCode); + AbstractMessage abstractResultMessage = factory.getMessage(typeCode); + MessageSeataCodec messageCodec = factory.getMessageCodec(typeCode); messageCodec.decode(abstractResultMessage, byteBuffer); msgs[idx] = (AbstractResultMessage)abstractResultMessage; } diff --git a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/MergedWarpMessageCodec.java b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/MergedWarpMessageCodec.java index 11e02adc003..436a82b7d26 100644 --- a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/MergedWarpMessageCodec.java +++ b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/MergedWarpMessageCodec.java @@ -26,6 +26,7 @@ import io.seata.serializer.seata.MessageSeataCodec; import io.seata.core.protocol.AbstractMessage; import io.seata.core.protocol.MergedWarpMessage; +import io.seata.serializer.seata.protocol.v1.MessageCodecFactoryV1; /** * The type Merged warp message codec. @@ -33,6 +34,8 @@ */ public class MergedWarpMessageCodec extends AbstractMessageCodec { + protected MessageCodecFactory factory = new MessageCodecFactoryV1(); + @Override public Class getMessageClassType() { return MergedWarpMessage.class; @@ -51,7 +54,7 @@ public void encode(T t, ByteBuf out) { for (final AbstractMessage msg : msgs) { final ByteBuf subBuffer = Unpooled.buffer(1024); short typeCode = msg.getTypeCode(); - MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typeCode); + MessageSeataCodec messageCodec = factory.getMessageCodec(typeCode); messageCodec.encode(msg, subBuffer); buffer.writeShort(msg.getTypeCode()); buffer.writeBytes(subBuffer); @@ -96,8 +99,8 @@ private void doDecode(MergedWarpMessage mergedWarpMessage, ByteBuffer byteBuffer List msgs = new ArrayList(); for (int idx = 0; idx < msgNum; idx++) { short typeCode = byteBuffer.getShort(); - AbstractMessage abstractMessage = MessageCodecFactory.getMessage(typeCode); - MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typeCode); + AbstractMessage abstractMessage = factory.getMessage(typeCode); + MessageSeataCodec messageCodec = factory.getMessageCodec(typeCode); messageCodec.decode(abstractMessage, byteBuffer); msgs.add(abstractMessage); } diff --git a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/MessageCodecFactoryV0.java b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/MessageCodecFactoryV0.java new file mode 100644 index 00000000000..746cc30884c --- /dev/null +++ b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/MessageCodecFactoryV0.java @@ -0,0 +1,82 @@ +/* + * Copyright 1999-2019 Seata.io Group. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.seata.serializer.seata.protocol.v0; + +import io.seata.core.protocol.MessageType; +import io.seata.serializer.seata.MessageCodecFactory; +import io.seata.serializer.seata.MessageSeataCodec; +import io.seata.serializer.seata.protocol.BatchResultMessageCodec; +import io.seata.serializer.seata.protocol.MergeResultMessageCodec; +import io.seata.serializer.seata.protocol.MergedWarpMessageCodec; +import io.seata.serializer.seata.protocol.RegisterRMRequestCodec; +import io.seata.serializer.seata.protocol.RegisterRMResponseCodec; +import io.seata.serializer.seata.protocol.RegisterTMRequestCodec; +import io.seata.serializer.seata.protocol.RegisterTMResponseCodec; +import io.seata.serializer.seata.protocol.transaction.BranchCommitRequestCodec; +import io.seata.serializer.seata.protocol.transaction.BranchRollbackRequestCodec; +import io.seata.serializer.seata.protocol.transaction.GlobalReportRequestCodec; + +/** + * The type Message codec factory v0. + */ +public class MessageCodecFactoryV0 extends MessageCodecFactory { + + /** + * Gets msg instance by code. + * + * @param typeCode the type code + * @return the msg instance by code + */ + public MessageSeataCodec getMessageCodec(short typeCode) { + MessageSeataCodec msgCodec = null; + switch (typeCode) { + case MessageType.TYPE_SEATA_MERGE: + msgCodec = new MergedWarpMessageCodec(); + break; + case MessageType.TYPE_SEATA_MERGE_RESULT: + msgCodec = new MergeResultMessageCodec(); + break; + case MessageType.TYPE_REG_CLT: + msgCodec = new RegisterTMRequestCodec(); + break; + case MessageType.TYPE_REG_CLT_RESULT: + msgCodec = new RegisterTMResponseCodec(); + break; + case MessageType.TYPE_REG_RM: + msgCodec = new RegisterRMRequestCodec(); + break; + case MessageType.TYPE_REG_RM_RESULT: + msgCodec = new RegisterRMResponseCodec(); + break; + case MessageType.TYPE_BRANCH_COMMIT: + msgCodec = new BranchCommitRequestCodec(); + break; + case MessageType.TYPE_BRANCH_ROLLBACK: + msgCodec = new BranchRollbackRequestCodec(); + break; + case MessageType.TYPE_GLOBAL_REPORT: + msgCodec = new GlobalReportRequestCodec(); + break; + case MessageType.TYPE_BATCH_RESULT_MSG: + msgCodec = new BatchResultMessageCodec(); + break; + default: + break; + } + + return msgCodec; + } +} diff --git a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v1/MessageCodecFactoryV1.java b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v1/MessageCodecFactoryV1.java new file mode 100644 index 00000000000..6957bec200f --- /dev/null +++ b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v1/MessageCodecFactoryV1.java @@ -0,0 +1,29 @@ +/* + * Copyright 1999-2019 Seata.io Group. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.seata.serializer.seata.protocol.v1; + +import io.seata.serializer.seata.MessageCodecFactory; + +/** + * The type Message codec factory. + * + * @author zhangsen + */ +public class MessageCodecFactoryV1 extends MessageCodecFactory { + + + +} From f1bacdd20c98c7a2aabcdb9a21f465cc4795a8c8 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Mon, 25 Dec 2023 18:58:19 +0800 Subject: [PATCH 02/62] Unwinding RpcMessage and Encoder/Decoder dependencies --- .../io/seata/core/protocol/RpcMessage.java | 10 + .../netty/AbstractNettyRemotingClient.java | 9 +- .../netty/AbstractNettyRemotingServer.java | 9 +- .../core/rpc/netty/ProtocolRpcMessage.java | 43 ++++ .../rpc/netty/v1/ProtocolRpcMessageV1.java | 196 ++++++++++++++++++ .../core/rpc/netty/v1/ProtocolV1Decoder.java | 4 +- .../core/rpc/netty/v1/ProtocolV1Encoder.java | 4 +- 7 files changed, 266 insertions(+), 9 deletions(-) create mode 100644 core/src/main/java/io/seata/core/rpc/netty/ProtocolRpcMessage.java create mode 100644 core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolRpcMessageV1.java diff --git a/core/src/main/java/io/seata/core/protocol/RpcMessage.java b/core/src/main/java/io/seata/core/protocol/RpcMessage.java index 844ea87fc4c..f96ead91ad5 100644 --- a/core/src/main/java/io/seata/core/protocol/RpcMessage.java +++ b/core/src/main/java/io/seata/core/protocol/RpcMessage.java @@ -35,6 +35,8 @@ public class RpcMessage implements Serializable { private Map headMap = new HashMap<>(); private Object body; + private String version; + /** * Gets id. * @@ -169,6 +171,14 @@ public void setMessageType(byte messageType) { this.messageType = messageType; } + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + @Override public String toString() { return StringUtils.toString(this); diff --git a/core/src/main/java/io/seata/core/rpc/netty/AbstractNettyRemotingClient.java b/core/src/main/java/io/seata/core/rpc/netty/AbstractNettyRemotingClient.java index 2a9248828ca..31594159f67 100644 --- a/core/src/main/java/io/seata/core/rpc/netty/AbstractNettyRemotingClient.java +++ b/core/src/main/java/io/seata/core/rpc/netty/AbstractNettyRemotingClient.java @@ -403,10 +403,13 @@ class ClientHandler extends ChannelDuplexHandler { @Override public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception { - if (!(msg instanceof RpcMessage)) { - return; + RpcMessage rpcMessage = null; + if (msg instanceof ProtocolRpcMessage) { + rpcMessage = ((ProtocolRpcMessage) msg).protocolMsg2RpcMsg(); + processMessage(ctx, rpcMessage); + } else { + LOGGER.error("rpcMessage type error"); } - processMessage(ctx, (RpcMessage) msg); } @Override diff --git a/core/src/main/java/io/seata/core/rpc/netty/AbstractNettyRemotingServer.java b/core/src/main/java/io/seata/core/rpc/netty/AbstractNettyRemotingServer.java index eb9b4adcde4..97e43923bf5 100644 --- a/core/src/main/java/io/seata/core/rpc/netty/AbstractNettyRemotingServer.java +++ b/core/src/main/java/io/seata/core/rpc/netty/AbstractNettyRemotingServer.java @@ -163,10 +163,13 @@ class ServerHandler extends ChannelDuplexHandler { */ @Override public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception { - if (!(msg instanceof RpcMessage)) { - return; + RpcMessage rpcMessage = null; + if (msg instanceof ProtocolRpcMessage) { + rpcMessage = ((ProtocolRpcMessage) msg).protocolMsg2RpcMsg(); + processMessage(ctx, rpcMessage); + } else { + LOGGER.error("rpcMessage type error"); } - processMessage(ctx, (RpcMessage) msg); } @Override diff --git a/core/src/main/java/io/seata/core/rpc/netty/ProtocolRpcMessage.java b/core/src/main/java/io/seata/core/rpc/netty/ProtocolRpcMessage.java new file mode 100644 index 00000000000..b538e04e604 --- /dev/null +++ b/core/src/main/java/io/seata/core/rpc/netty/ProtocolRpcMessage.java @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.seata.core.rpc.netty; + +import io.seata.core.protocol.AbstractIdentifyRequest; +import io.seata.core.protocol.RpcMessage; + +/** + * The protocol RPC message. + */ +public interface ProtocolRpcMessage { + RpcMessage protocolMsg2RpcMsg(); + + void rpcMsg2ProtocolMsg(RpcMessage rpcMessage); + + static String getVersion(Object body) { + if (body instanceof AbstractIdentifyRequest) { + return ((AbstractIdentifyRequest) body).getVersion(); + } else { + return null; + } + } + + static void setVersion(Object body, String version) { + if (body instanceof AbstractIdentifyRequest) { + ((AbstractIdentifyRequest) body).setVersion(version); + } + } +} diff --git a/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolRpcMessageV1.java b/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolRpcMessageV1.java new file mode 100644 index 00000000000..5f2639a2fe1 --- /dev/null +++ b/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolRpcMessageV1.java @@ -0,0 +1,196 @@ +/* + * Copyright 1999-2019 Seata.io Group. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.seata.core.rpc.netty.v1; + +import io.seata.common.util.StringUtils; +import io.seata.core.protocol.RpcMessage; +import io.seata.core.rpc.netty.ProtocolRpcMessage; + +import java.util.HashMap; +import java.util.Map; + +/** + * protocol v1 rpc message + **/ +public class ProtocolRpcMessageV1 implements ProtocolRpcMessage { + private int id; + private byte messageType; + private byte codec; + private byte compressor; + private Map headMap = new HashMap<>(); + private Object body; + + /** + * Gets id. + * + * @return the id + */ + public int getId() { + return id; + } + + /** + * Sets id. + * + * @param id the id + */ + public void setId(int id) { + this.id = id; + } + + /** + * Gets body. + * + * @return the body + */ + public Object getBody() { + return body; + } + + /** + * Sets body. + * + * @param body the body + */ + public void setBody(Object body) { + this.body = body; + } + + /** + * Gets codec. + * + * @return the codec + */ + public byte getCodec() { + return codec; + } + + /** + * Sets codec. + * + * @param codec the codec + * @return the codec + */ + public void setCodec(byte codec) { + this.codec = codec; + } + + /** + * Gets compressor. + * + * @return the compressor + */ + public byte getCompressor() { + return compressor; + } + + /** + * Sets compressor. + * + * @param compressor the compressor + * @return the compressor + */ + public void setCompressor(byte compressor) { + this.compressor = compressor; + } + + /** + * Gets head map. + * + * @return the head map + */ + public Map getHeadMap() { + return headMap; + } + + /** + * Sets head map. + * + * @param headMap the head map + * @return the head map + */ + public void setHeadMap(Map headMap) { + this.headMap = headMap; + } + + /** + * Gets head. + * + * @param headKey the head key + * @return the head + */ + public String getHead(String headKey) { + return headMap.get(headKey); + } + + /** + * Put head. + * + * @param headKey the head key + * @param headValue the head value + */ + public void putHead(String headKey, String headValue) { + headMap.put(headKey, headValue); + } + + /** + * Gets message type. + * + * @return the message type + */ + public byte getMessageType() { + return messageType; + } + + /** + * Sets message type. + * + * @param messageType the message type + */ + public void setMessageType(byte messageType) { + this.messageType = messageType; + } + + @Override + public String toString() { + return StringUtils.toString(this); + } + + @Override + public RpcMessage protocolMsg2RpcMsg() { + RpcMessage rpcMessage = new RpcMessage(); + rpcMessage.setId(this.id); + rpcMessage.setMessageType(this.messageType); + rpcMessage.setCodec(this.codec); + rpcMessage.setCompressor(this.compressor); + rpcMessage.setHeadMap(this.headMap); + rpcMessage.setBody(this.body); + rpcMessage.setVersion(ProtocolRpcMessage.getVersion(this.body)); + return rpcMessage; + } + + + @Override + public void rpcMsg2ProtocolMsg(RpcMessage rpcMessage) { + this.body = rpcMessage.getBody(); + ProtocolRpcMessage.setVersion(this.body, rpcMessage.getVersion()); + this.headMap = rpcMessage.getHeadMap(); + this.id = rpcMessage.getId(); + this.messageType = rpcMessage.getMessageType(); + this.codec = rpcMessage.getCodec(); + this.compressor = rpcMessage.getCompressor(); + } +} diff --git a/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Decoder.java b/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Decoder.java index 867e27d9457..1f4a13a75da 100644 --- a/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Decoder.java +++ b/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Decoder.java @@ -70,7 +70,7 @@ public ProtocolV1Decoder() { public ProtocolV1Decoder(int maxFrameLength) { /* - int maxFrameLength, + int maxFrameLength, int lengthFieldOffset, magic code is 2B, and version is 1B, and then FullLength. so value is 3 int lengthFieldLength, FullLength is int(4B). so values is 4 int lengthAdjustment, FullLength include all data and read 7 bytes before, so the left length is (FullLength-7). so values is -7 @@ -117,7 +117,7 @@ public Object decodeFrame(ByteBuf frame) { byte compressorType = frame.readByte(); int requestId = frame.readInt(); - RpcMessage rpcMessage = new RpcMessage(); + ProtocolRpcMessageV1 rpcMessage = new ProtocolRpcMessageV1(); rpcMessage.setCodec(codecType); rpcMessage.setId(requestId); rpcMessage.setCompressor(compressorType); diff --git a/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Encoder.java b/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Encoder.java index 0f35eff8492..1af01f711c8 100644 --- a/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Encoder.java +++ b/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Encoder.java @@ -65,8 +65,10 @@ public class ProtocolV1Encoder extends MessageToByteEncoder { public void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) { try { if (msg instanceof RpcMessage) { - RpcMessage rpcMessage = (RpcMessage) msg; + RpcMessage rpcMsg = (RpcMessage) msg; + ProtocolRpcMessageV1 rpcMessage = new ProtocolRpcMessageV1(); + rpcMessage.rpcMsg2ProtocolMsg(rpcMsg); int fullLength = ProtocolConstants.V1_HEAD_LENGTH; int headLength = ProtocolConstants.V1_HEAD_LENGTH; From 901b886a75396705203464a1d282fd7cf1376254 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Tue, 26 Dec 2023 14:49:09 +0800 Subject: [PATCH 03/62] license --- .../protocol/v0/MessageCodecFactoryV0.java | 23 +++++++++-------- .../protocol/v1/MessageCodecFactoryV1.java | 25 +++++++++---------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/MessageCodecFactoryV0.java b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/MessageCodecFactoryV0.java index 746cc30884c..fca6998976f 100644 --- a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/MessageCodecFactoryV0.java +++ b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/MessageCodecFactoryV0.java @@ -1,17 +1,18 @@ /* - * Copyright 1999-2019 Seata.io Group. + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package io.seata.serializer.seata.protocol.v0; diff --git a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v1/MessageCodecFactoryV1.java b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v1/MessageCodecFactoryV1.java index 6957bec200f..7843336bfe9 100644 --- a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v1/MessageCodecFactoryV1.java +++ b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v1/MessageCodecFactoryV1.java @@ -1,17 +1,18 @@ /* - * Copyright 1999-2019 Seata.io Group. + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package io.seata.serializer.seata.protocol.v1; @@ -19,8 +20,6 @@ /** * The type Message codec factory. - * - * @author zhangsen */ public class MessageCodecFactoryV1 extends MessageCodecFactory { From ee3c3aa344be5ea72c064869e230fb6e7aae3207 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Tue, 26 Dec 2023 14:51:39 +0800 Subject: [PATCH 04/62] license --- .../rpc/netty/v1/ProtocolRpcMessageV1.java | 23 ++++++++++--------- .../core/rpc/netty/v1/ProtocolV1Decoder.java | 1 - 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolRpcMessageV1.java b/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolRpcMessageV1.java index 5f2639a2fe1..1a3a76e1f27 100644 --- a/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolRpcMessageV1.java +++ b/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolRpcMessageV1.java @@ -1,17 +1,18 @@ /* - * Copyright 1999-2019 Seata.io Group. + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package io.seata.core.rpc.netty.v1; diff --git a/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Decoder.java b/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Decoder.java index 1f4a13a75da..2e5453d9da9 100644 --- a/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Decoder.java +++ b/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Decoder.java @@ -25,7 +25,6 @@ import io.seata.core.compressor.CompressorFactory; import io.seata.core.protocol.HeartbeatMessage; import io.seata.core.protocol.ProtocolConstants; -import io.seata.core.protocol.RpcMessage; import io.seata.core.serializer.SerializerServiceLoader; import io.seata.core.serializer.SerializerType; import org.slf4j.Logger; From b51800497d07e9761c91ef52dc24fe5fdd1b08af Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Tue, 26 Dec 2023 14:57:56 +0800 Subject: [PATCH 05/62] style --- .../io/seata/core/serializer/SerializerServiceLoader.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/io/seata/core/serializer/SerializerServiceLoader.java b/core/src/main/java/io/seata/core/serializer/SerializerServiceLoader.java index fea9ab46567..b5b7340356e 100644 --- a/core/src/main/java/io/seata/core/serializer/SerializerServiceLoader.java +++ b/core/src/main/java/io/seata/core/serializer/SerializerServiceLoader.java @@ -25,7 +25,6 @@ /** * The Service Loader for the interface {@link Serializer} - * */ public final class SerializerServiceLoader { @@ -61,16 +60,15 @@ public static Serializer load(SerializerType type, byte version) throws Enhanced } else { serializer = EnhancedServiceLoader.load(Serializer.class, type.name()); } - serializerMap.put(key,serializer); + serializerMap.put(key, serializer); } return serializer; - } - private static String serialzerKey(SerializerType type, byte version){ + private static String serialzerKey(SerializerType type, byte version) { if (type == SerializerType.SEATA) { return type.name() + version; } return type.name(); } -} +} \ No newline at end of file From 883adfbff67f31d6b28d4967617d22a380c0f663 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Tue, 26 Dec 2023 15:03:33 +0800 Subject: [PATCH 06/62] test ProtocolConstants.VERSION --- .../main/java/io/seata/core/protocol/ProtocolConstants.java | 2 +- .../seata/protocol/BatchResultMessageSerializerTest.java | 3 ++- .../seata/protocol/MergeResultMessageSerializerTest.java | 3 ++- .../seata/protocol/MergedWarpMessageSerializerTest.java | 3 ++- .../seata/protocol/RegisterRMRequestSerializerTest.java | 3 ++- .../seata/protocol/RegisterRMResponseSerializerTest.java | 3 ++- .../seata/protocol/RegisterTMRequestSerializerTest.java | 3 ++- .../seata/protocol/RegisterTMResponseSerializerTest.java | 3 ++- .../transaction/BranchCommitRequestSerializerTest.java | 3 ++- .../transaction/BranchCommitResponseSerializerTest.java | 3 ++- .../transaction/BranchRegisterRequestSerializerTest.java | 3 ++- .../transaction/BranchRegisterResponseSerializerTest.java | 3 ++- .../transaction/BranchReportRequestSerializerTest.java | 3 ++- .../transaction/BranchReportResponseSerializerTest.java | 3 ++- .../transaction/BranchRollbackRequestSerializerTest.java | 3 ++- .../transaction/BranchRollbackResponseSerializerTest.java | 3 ++- .../protocol/transaction/GlobalBeginRequestSerializerTest.java | 3 ++- .../transaction/GlobalBeginResponseSerializerTest.java | 3 ++- .../protocol/transaction/GlobalCommitRequestCodecTest.java | 3 ++- .../transaction/GlobalCommitResponseSerializerTest.java | 3 ++- .../transaction/GlobalLockQueryRequestSerializerTest.java | 3 ++- .../transaction/GlobalLockQueryResponseSerializerTest.java | 3 ++- .../protocol/transaction/GlobalRollbackRequestCodecTest.java | 3 ++- .../transaction/GlobalRollbackResponseSerializerTest.java | 3 ++- .../protocol/transaction/GlobalStatusRequestCodecTest.java | 3 ++- .../transaction/GlobalStatusResponseSerializerTest.java | 3 ++- .../transaction/UndoLogDeleteRequestSerializerTest.java | 3 ++- 27 files changed, 53 insertions(+), 27 deletions(-) diff --git a/core/src/main/java/io/seata/core/protocol/ProtocolConstants.java b/core/src/main/java/io/seata/core/protocol/ProtocolConstants.java index c62438ea805..edf1163d5a3 100644 --- a/core/src/main/java/io/seata/core/protocol/ProtocolConstants.java +++ b/core/src/main/java/io/seata/core/protocol/ProtocolConstants.java @@ -44,7 +44,7 @@ public interface ProtocolConstants { /** * Protocol version */ - byte VERSION = 1; + byte VERSION = VERSION_1; /** * Max frame length diff --git a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/BatchResultMessageSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/BatchResultMessageSerializerTest.java index b8d8cf21744..e9775751d25 100644 --- a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/BatchResultMessageSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/BatchResultMessageSerializerTest.java @@ -22,6 +22,7 @@ import io.seata.core.model.BranchStatus; import io.seata.core.protocol.AbstractResultMessage; import io.seata.core.protocol.BatchResultMessage; +import io.seata.core.protocol.ProtocolConstants; import io.seata.core.protocol.ResultCode; import io.seata.core.protocol.transaction.BranchCommitResponse; import io.seata.serializer.seata.SeataSerializer; @@ -39,7 +40,7 @@ public class BatchResultMessageSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); @Test diff --git a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/MergeResultMessageSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/MergeResultMessageSerializerTest.java index ccc692a3a90..d172d2691d5 100644 --- a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/MergeResultMessageSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/MergeResultMessageSerializerTest.java @@ -16,6 +16,7 @@ */ package io.seata.serializer.seata.protocol; +import io.seata.core.protocol.ProtocolConstants; import io.seata.serializer.seata.SeataSerializer; import io.seata.core.exception.TransactionExceptionCode; import io.seata.core.protocol.AbstractResultMessage; @@ -35,7 +36,7 @@ public class MergeResultMessageSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/MergedWarpMessageSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/MergedWarpMessageSerializerTest.java index 854656b963b..51d2640028c 100644 --- a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/MergedWarpMessageSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/MergedWarpMessageSerializerTest.java @@ -21,6 +21,7 @@ import io.seata.core.protocol.AbstractMessage; import io.seata.core.protocol.MergedWarpMessage; +import io.seata.core.protocol.ProtocolConstants; import io.seata.core.protocol.transaction.GlobalBeginRequest; import io.seata.serializer.seata.SeataSerializer; import org.junit.jupiter.api.Test; @@ -37,7 +38,7 @@ public class MergedWarpMessageSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/RegisterRMRequestSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/RegisterRMRequestSerializerTest.java index ea8fbaad0cf..b402252e6d3 100644 --- a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/RegisterRMRequestSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/RegisterRMRequestSerializerTest.java @@ -16,6 +16,7 @@ */ package io.seata.serializer.seata.protocol; +import io.seata.core.protocol.ProtocolConstants; import io.seata.serializer.seata.SeataSerializer; import io.seata.core.protocol.RegisterRMRequest; import org.junit.jupiter.api.Test; @@ -31,7 +32,7 @@ public class RegisterRMRequestSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/RegisterRMResponseSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/RegisterRMResponseSerializerTest.java index 1fac2b015c5..7e7ba6b0822 100644 --- a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/RegisterRMResponseSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/RegisterRMResponseSerializerTest.java @@ -16,6 +16,7 @@ */ package io.seata.serializer.seata.protocol; +import io.seata.core.protocol.ProtocolConstants; import io.seata.serializer.seata.SeataSerializer; import io.seata.core.protocol.RegisterRMResponse; import io.seata.core.protocol.ResultCode; @@ -33,7 +34,7 @@ public class RegisterRMResponseSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/RegisterTMRequestSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/RegisterTMRequestSerializerTest.java index 13a05463deb..1b288ccafdb 100644 --- a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/RegisterTMRequestSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/RegisterTMRequestSerializerTest.java @@ -17,6 +17,7 @@ package io.seata.serializer.seata.protocol; import io.netty.buffer.ByteBuf; +import io.seata.core.protocol.ProtocolConstants; import io.seata.serializer.seata.SeataSerializer; import io.seata.core.protocol.AbstractIdentifyRequest; import io.seata.core.protocol.RegisterTMRequest; @@ -37,7 +38,7 @@ public class RegisterTMRequestSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); private static RegisterTMRequest registerTMRequest; private static AbstractIdentifyRequest air; diff --git a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/RegisterTMResponseSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/RegisterTMResponseSerializerTest.java index 785a075344b..b67eedcdc21 100644 --- a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/RegisterTMResponseSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/RegisterTMResponseSerializerTest.java @@ -16,6 +16,7 @@ */ package io.seata.serializer.seata.protocol; +import io.seata.core.protocol.ProtocolConstants; import io.seata.serializer.seata.SeataSerializer; import io.seata.core.protocol.RegisterTMResponse; import io.seata.core.protocol.ResultCode; @@ -32,7 +33,7 @@ public class RegisterTMResponseSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/BranchCommitRequestSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/BranchCommitRequestSerializerTest.java index 03caa1bbecd..ec496a74d91 100644 --- a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/BranchCommitRequestSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/BranchCommitRequestSerializerTest.java @@ -16,6 +16,7 @@ */ package io.seata.serializer.seata.protocol.transaction; +import io.seata.core.protocol.ProtocolConstants; import io.seata.serializer.seata.SeataSerializer; import io.seata.core.model.BranchType; import io.seata.core.protocol.transaction.BranchCommitRequest; @@ -32,7 +33,7 @@ public class BranchCommitRequestSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/BranchCommitResponseSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/BranchCommitResponseSerializerTest.java index 9fda8b1f8a6..c2401ec8735 100644 --- a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/BranchCommitResponseSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/BranchCommitResponseSerializerTest.java @@ -16,6 +16,7 @@ */ package io.seata.serializer.seata.protocol.transaction; +import io.seata.core.protocol.ProtocolConstants; import io.seata.serializer.seata.SeataSerializer; import io.seata.core.exception.TransactionExceptionCode; import io.seata.core.model.BranchStatus; @@ -34,7 +35,7 @@ public class BranchCommitResponseSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/BranchRegisterRequestSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/BranchRegisterRequestSerializerTest.java index 4ee59378f0b..de5f1a436c8 100644 --- a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/BranchRegisterRequestSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/BranchRegisterRequestSerializerTest.java @@ -16,6 +16,7 @@ */ package io.seata.serializer.seata.protocol.transaction; +import io.seata.core.protocol.ProtocolConstants; import io.seata.serializer.seata.SeataSerializer; import io.seata.core.model.BranchType; import io.seata.core.protocol.transaction.BranchRegisterRequest; @@ -32,7 +33,7 @@ public class BranchRegisterRequestSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/BranchRegisterResponseSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/BranchRegisterResponseSerializerTest.java index 670f863a68c..24d99f14e1a 100644 --- a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/BranchRegisterResponseSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/BranchRegisterResponseSerializerTest.java @@ -16,6 +16,7 @@ */ package io.seata.serializer.seata.protocol.transaction; +import io.seata.core.protocol.ProtocolConstants; import io.seata.serializer.seata.SeataSerializer; import io.seata.core.exception.TransactionExceptionCode; import io.seata.core.protocol.ResultCode; @@ -33,7 +34,7 @@ public class BranchRegisterResponseSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/BranchReportRequestSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/BranchReportRequestSerializerTest.java index a7ec699e47b..7e181c35963 100644 --- a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/BranchReportRequestSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/BranchReportRequestSerializerTest.java @@ -16,6 +16,7 @@ */ package io.seata.serializer.seata.protocol.transaction; +import io.seata.core.protocol.ProtocolConstants; import io.seata.serializer.seata.SeataSerializer; import io.seata.core.model.BranchStatus; import io.seata.core.model.BranchType; @@ -33,7 +34,7 @@ public class BranchReportRequestSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/BranchReportResponseSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/BranchReportResponseSerializerTest.java index 36c885e8746..64f16995d40 100644 --- a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/BranchReportResponseSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/BranchReportResponseSerializerTest.java @@ -16,6 +16,7 @@ */ package io.seata.serializer.seata.protocol.transaction; +import io.seata.core.protocol.ProtocolConstants; import io.seata.serializer.seata.SeataSerializer; import io.seata.core.exception.TransactionExceptionCode; import io.seata.core.protocol.ResultCode; @@ -33,7 +34,7 @@ public class BranchReportResponseSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/BranchRollbackRequestSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/BranchRollbackRequestSerializerTest.java index b339ea16317..2d397dca05c 100644 --- a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/BranchRollbackRequestSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/BranchRollbackRequestSerializerTest.java @@ -16,6 +16,7 @@ */ package io.seata.serializer.seata.protocol.transaction; +import io.seata.core.protocol.ProtocolConstants; import io.seata.serializer.seata.SeataSerializer; import io.seata.core.model.BranchType; import io.seata.core.protocol.transaction.BranchRollbackRequest; @@ -32,7 +33,7 @@ public class BranchRollbackRequestSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/BranchRollbackResponseSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/BranchRollbackResponseSerializerTest.java index 0d47d07de7a..fd13628d5a3 100644 --- a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/BranchRollbackResponseSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/BranchRollbackResponseSerializerTest.java @@ -16,6 +16,7 @@ */ package io.seata.serializer.seata.protocol.transaction; +import io.seata.core.protocol.ProtocolConstants; import io.seata.serializer.seata.SeataSerializer; import io.seata.core.exception.TransactionExceptionCode; import io.seata.core.model.BranchStatus; @@ -34,7 +35,7 @@ public class BranchRollbackResponseSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalBeginRequestSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalBeginRequestSerializerTest.java index 47eb946502c..81452e9e985 100644 --- a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalBeginRequestSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalBeginRequestSerializerTest.java @@ -16,6 +16,7 @@ */ package io.seata.serializer.seata.protocol.transaction; +import io.seata.core.protocol.ProtocolConstants; import io.seata.serializer.seata.SeataSerializer; import io.seata.core.protocol.transaction.GlobalBeginRequest; import org.junit.jupiter.api.Test; @@ -31,7 +32,7 @@ public class GlobalBeginRequestSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalBeginResponseSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalBeginResponseSerializerTest.java index 547688c54a7..bb8ca561412 100644 --- a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalBeginResponseSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalBeginResponseSerializerTest.java @@ -16,6 +16,7 @@ */ package io.seata.serializer.seata.protocol.transaction; +import io.seata.core.protocol.ProtocolConstants; import io.seata.serializer.seata.SeataSerializer; import io.seata.core.exception.TransactionExceptionCode; import io.seata.core.protocol.ResultCode; @@ -32,7 +33,7 @@ public class GlobalBeginResponseSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalCommitRequestCodecTest.java b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalCommitRequestCodecTest.java index 000fdd75886..4a6b1c19d96 100644 --- a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalCommitRequestCodecTest.java +++ b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalCommitRequestCodecTest.java @@ -16,6 +16,7 @@ */ package io.seata.serializer.seata.protocol.transaction; +import io.seata.core.protocol.ProtocolConstants; import io.seata.serializer.seata.SeataSerializer; import io.seata.core.protocol.transaction.GlobalCommitRequest; import org.junit.jupiter.api.Test; @@ -31,7 +32,7 @@ public class GlobalCommitRequestCodecTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalCommitResponseSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalCommitResponseSerializerTest.java index e297937958d..292dc36d3c5 100644 --- a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalCommitResponseSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalCommitResponseSerializerTest.java @@ -16,6 +16,7 @@ */ package io.seata.serializer.seata.protocol.transaction; +import io.seata.core.protocol.ProtocolConstants; import io.seata.serializer.seata.SeataSerializer; import io.seata.core.exception.TransactionExceptionCode; import io.seata.core.model.GlobalStatus; @@ -33,7 +34,7 @@ public class GlobalCommitResponseSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalLockQueryRequestSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalLockQueryRequestSerializerTest.java index 9346bc0ef04..099d64e6af3 100644 --- a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalLockQueryRequestSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalLockQueryRequestSerializerTest.java @@ -16,6 +16,7 @@ */ package io.seata.serializer.seata.protocol.transaction; +import io.seata.core.protocol.ProtocolConstants; import io.seata.serializer.seata.SeataSerializer; import io.seata.core.model.BranchType; import io.seata.core.protocol.transaction.GlobalLockQueryRequest; @@ -31,7 +32,7 @@ public class GlobalLockQueryRequestSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalLockQueryResponseSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalLockQueryResponseSerializerTest.java index 5140308361b..f28af2acdc5 100644 --- a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalLockQueryResponseSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalLockQueryResponseSerializerTest.java @@ -16,6 +16,7 @@ */ package io.seata.serializer.seata.protocol.transaction; +import io.seata.core.protocol.ProtocolConstants; import io.seata.serializer.seata.SeataSerializer; import io.seata.core.exception.TransactionExceptionCode; import io.seata.core.protocol.ResultCode; @@ -33,7 +34,7 @@ public class GlobalLockQueryResponseSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalRollbackRequestCodecTest.java b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalRollbackRequestCodecTest.java index 5e6c059afb6..5746d70d93d 100644 --- a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalRollbackRequestCodecTest.java +++ b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalRollbackRequestCodecTest.java @@ -16,6 +16,7 @@ */ package io.seata.serializer.seata.protocol.transaction; +import io.seata.core.protocol.ProtocolConstants; import io.seata.serializer.seata.SeataSerializer; import io.seata.core.protocol.transaction.GlobalRollbackRequest; import org.junit.jupiter.api.Test; @@ -31,7 +32,7 @@ public class GlobalRollbackRequestCodecTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalRollbackResponseSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalRollbackResponseSerializerTest.java index 8049625b295..a3d099fb3dd 100644 --- a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalRollbackResponseSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalRollbackResponseSerializerTest.java @@ -16,6 +16,7 @@ */ package io.seata.serializer.seata.protocol.transaction; +import io.seata.core.protocol.ProtocolConstants; import io.seata.serializer.seata.SeataSerializer; import io.seata.core.exception.TransactionExceptionCode; import io.seata.core.model.GlobalStatus; @@ -34,7 +35,7 @@ public class GlobalRollbackResponseSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalStatusRequestCodecTest.java b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalStatusRequestCodecTest.java index 0d88d5e64e9..66a3d91d100 100644 --- a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalStatusRequestCodecTest.java +++ b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalStatusRequestCodecTest.java @@ -16,6 +16,7 @@ */ package io.seata.serializer.seata.protocol.transaction; +import io.seata.core.protocol.ProtocolConstants; import io.seata.serializer.seata.SeataSerializer; import io.seata.core.protocol.transaction.GlobalStatusRequest; import org.junit.jupiter.api.Test; @@ -31,7 +32,7 @@ public class GlobalStatusRequestCodecTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalStatusResponseSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalStatusResponseSerializerTest.java index 4f3e818aaae..bca5aff8201 100644 --- a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalStatusResponseSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/GlobalStatusResponseSerializerTest.java @@ -16,6 +16,7 @@ */ package io.seata.serializer.seata.protocol.transaction; +import io.seata.core.protocol.ProtocolConstants; import io.seata.serializer.seata.SeataSerializer; import io.seata.core.exception.TransactionExceptionCode; import io.seata.core.model.GlobalStatus; @@ -33,7 +34,7 @@ public class GlobalStatusResponseSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/UndoLogDeleteRequestSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/UndoLogDeleteRequestSerializerTest.java index d2c3c8ea004..806d3bb638c 100644 --- a/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/UndoLogDeleteRequestSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/io/seata/serializer/seata/protocol/transaction/UndoLogDeleteRequestSerializerTest.java @@ -16,6 +16,7 @@ */ package io.seata.serializer.seata.protocol.transaction; +import io.seata.core.protocol.ProtocolConstants; import io.seata.serializer.seata.SeataSerializer; import io.seata.core.model.BranchType; import io.seata.core.protocol.transaction.UndoLogDeleteRequest; @@ -32,7 +33,7 @@ public class UndoLogDeleteRequestSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. From 492672c6578f1eb7fb69483ede323194e318c900 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Wed, 27 Dec 2023 15:42:32 +0800 Subject: [PATCH 07/62] version serialize --- .../serializer/seata/SeataSerializer.java | 62 +----------- .../seata/protocol/v0/SeataSerializerV0.java | 93 ++++++++++++++++++ .../seata/protocol/v1/SeataSerializerV1.java | 98 +++++++++++++++++++ 3 files changed, 196 insertions(+), 57 deletions(-) create mode 100644 serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/SeataSerializerV0.java create mode 100644 serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v1/SeataSerializerV1.java diff --git a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java index ef34992eb5d..e23c436ce59 100644 --- a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java +++ b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java @@ -28,6 +28,7 @@ import io.seata.core.protocol.ProtocolConstants; import io.seata.core.serializer.Serializer; import io.seata.serializer.seata.protocol.v1.MessageCodecFactoryV1; +import io.seata.serializer.seata.protocol.v1.SeataSerializerV1; /** * The Seata codec. @@ -36,72 +37,19 @@ @LoadLevel(name = "SEATA", scope = Scope.PROTOTYPE) public class SeataSerializer implements Serializer { - MessageCodecFactory factory; - byte protocolVersion ; + Serializer versionSeataSerializer; public SeataSerializer(Byte version){ - if (version == ProtocolConstants.VERSION_1) { - factory = new MessageCodecFactoryV1(); - }else { - throw new NotSupportYetException("not support version" + version); - } - protocolVersion = version; + versionSeataSerializer = new SeataSerializerV1(); } @Override public byte[] serialize(T t) { - if (!(t instanceof AbstractMessage)) { - throw new IllegalArgumentException("AbstractMessage isn't available."); - } - AbstractMessage abstractMessage = (AbstractMessage)t; - //type code - short typecode = abstractMessage.getTypeCode(); - //msg codec - MessageSeataCodec messageCodec = factory.getMessageCodec(typecode); - //get empty ByteBuffer - ByteBuf out = Unpooled.buffer(1024); - //msg encode - messageCodec.encode(t, out); - byte[] body = new byte[out.readableBytes()]; - out.readBytes(body); - - ByteBuffer byteBuffer; - if (protocolVersion == ProtocolConstants.VERSION_0) { - byteBuffer = ByteBuffer.allocate(body.length); - } else { - //typecode + body - byteBuffer = ByteBuffer.allocate(2 + body.length); - byteBuffer.putShort(typecode); - } - byteBuffer.put(body); - - BufferUtils.flip(byteBuffer); - byte[] content = new byte[byteBuffer.limit()]; - byteBuffer.get(content); - return content; + return versionSeataSerializer.serialize(t); } @Override public T deserialize(byte[] bytes) { - if (bytes == null || bytes.length == 0) { - throw new IllegalArgumentException("Nothing to decode."); - } - if (bytes.length < 2) { - throw new IllegalArgumentException("The byte[] isn't available for decode."); - } - ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); - //typecode - short typecode = byteBuffer.getShort(); - //msg body - byte[] body = new byte[byteBuffer.remaining()]; - byteBuffer.get(body); - ByteBuffer in = ByteBuffer.wrap(body); - //new Messgae - AbstractMessage abstractMessage = factory.getMessage(typecode); - //get messageCodec - MessageSeataCodec messageCodec = factory.getMessageCodec(typecode); - //decode - messageCodec.decode(abstractMessage, in); - return (T)abstractMessage; + return versionSeataSerializer.deserialize(bytes); } } diff --git a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/SeataSerializerV0.java b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/SeataSerializerV0.java new file mode 100644 index 00000000000..a917b5ff263 --- /dev/null +++ b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/SeataSerializerV0.java @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.seata.serializer.seata.protocol.v0; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.seata.common.util.BufferUtils; +import io.seata.core.protocol.AbstractMessage; +import io.seata.core.protocol.ProtocolConstants; +import io.seata.core.serializer.Serializer; +import io.seata.serializer.seata.MessageCodecFactory; +import io.seata.serializer.seata.MessageSeataCodec; + +import java.nio.ByteBuffer; + +/** + * The Seata codec v0. + * + */ +public class SeataSerializerV0 implements Serializer { + + MessageCodecFactory factory; + + public SeataSerializerV0(Byte version){ + factory = new MessageCodecFactoryV0(); + } + @Override + public byte[] serialize(T t) { + if (!(t instanceof AbstractMessage)) { + throw new IllegalArgumentException("AbstractMessage isn't available."); + } + AbstractMessage abstractMessage = (AbstractMessage)t; + //type code + short typecode = abstractMessage.getTypeCode(); + //msg codec + MessageSeataCodec messageCodec = factory.getMessageCodec(typecode); + //get empty ByteBuffer + ByteBuf out = Unpooled.buffer(1024); + //msg encode + messageCodec.encode(t, out); + byte[] body = new byte[out.readableBytes()]; + out.readBytes(body); + + ByteBuffer byteBuffer; + byteBuffer = ByteBuffer.allocate(body.length); + + byteBuffer.put(body); + + BufferUtils.flip(byteBuffer); + byte[] content = new byte[byteBuffer.limit()]; + byteBuffer.get(content); + return content; + } + + @Override + public T deserialize(byte[] bytes) { + if (bytes == null || bytes.length == 0) { + throw new IllegalArgumentException("Nothing to decode."); + } + if (bytes.length < 2) { + throw new IllegalArgumentException("The byte[] isn't available for decode."); + } + ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); + //typecode + short typecode = byteBuffer.getShort(); + //msg body + byte[] body = new byte[byteBuffer.remaining()]; + byteBuffer.get(body); + ByteBuffer in = ByteBuffer.wrap(body); + //new Messgae + AbstractMessage abstractMessage = factory.getMessage(typecode); + //get messageCodec + MessageSeataCodec messageCodec = factory.getMessageCodec(typecode); + //decode + messageCodec.decode(abstractMessage, in); + return (T)abstractMessage; + } + +} diff --git a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v1/SeataSerializerV1.java b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v1/SeataSerializerV1.java new file mode 100644 index 00000000000..b637dd81f7f --- /dev/null +++ b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v1/SeataSerializerV1.java @@ -0,0 +1,98 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.seata.serializer.seata.protocol.v1; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.seata.common.exception.NotSupportYetException; +import io.seata.common.loader.LoadLevel; +import io.seata.common.loader.Scope; +import io.seata.common.util.BufferUtils; +import io.seata.core.protocol.AbstractMessage; +import io.seata.core.protocol.ProtocolConstants; +import io.seata.core.serializer.Serializer; +import io.seata.serializer.seata.MessageCodecFactory; +import io.seata.serializer.seata.MessageSeataCodec; + +import java.nio.ByteBuffer; + +/** + * The Seata codec v1. + * + */ +public class SeataSerializerV1 implements Serializer { + + MessageCodecFactory factory; + + public SeataSerializerV1(){ + factory = new MessageCodecFactoryV1(); + } + @Override + public byte[] serialize(T t) { + if (!(t instanceof AbstractMessage)) { + throw new IllegalArgumentException("AbstractMessage isn't available."); + } + AbstractMessage abstractMessage = (AbstractMessage)t; + //type code + short typecode = abstractMessage.getTypeCode(); + //msg codec + MessageSeataCodec messageCodec = factory.getMessageCodec(typecode); + //get empty ByteBuffer + ByteBuf out = Unpooled.buffer(1024); + //msg encode + messageCodec.encode(t, out); + byte[] body = new byte[out.readableBytes()]; + out.readBytes(body); + + ByteBuffer byteBuffer; + + //typecode + body + byteBuffer = ByteBuffer.allocate(2 + body.length); + byteBuffer.putShort(typecode); + byteBuffer.put(body); + + BufferUtils.flip(byteBuffer); + byte[] content = new byte[byteBuffer.limit()]; + byteBuffer.get(content); + return content; + } + + @Override + public T deserialize(byte[] bytes) { + if (bytes == null || bytes.length == 0) { + throw new IllegalArgumentException("Nothing to decode."); + } + if (bytes.length < 2) { + throw new IllegalArgumentException("The byte[] isn't available for decode."); + } + ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); + //typecode + short typecode = byteBuffer.getShort(); + //msg body + byte[] body = new byte[byteBuffer.remaining()]; + byteBuffer.get(body); + ByteBuffer in = ByteBuffer.wrap(body); + //new Messgae + AbstractMessage abstractMessage = factory.getMessage(typecode); + //get messageCodec + MessageSeataCodec messageCodec = factory.getMessageCodec(typecode); + //decode + messageCodec.decode(abstractMessage, in); + return (T)abstractMessage; + } + +} From 94cb0cfd712bed15ddf87bb0687385dfb1b61d2b Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Thu, 28 Dec 2023 19:49:12 +0800 Subject: [PATCH 08/62] v0 --- .../rpc/netty/v0/ProtocolRpcMessageV0.java | 205 ++++++++++++++++++ 1 file changed, 205 insertions(+) create mode 100644 core/src/main/java/io/seata/core/rpc/netty/v0/ProtocolRpcMessageV0.java diff --git a/core/src/main/java/io/seata/core/rpc/netty/v0/ProtocolRpcMessageV0.java b/core/src/main/java/io/seata/core/rpc/netty/v0/ProtocolRpcMessageV0.java new file mode 100644 index 00000000000..d6a4bfa54f4 --- /dev/null +++ b/core/src/main/java/io/seata/core/rpc/netty/v0/ProtocolRpcMessageV0.java @@ -0,0 +1,205 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.seata.core.rpc.netty.v0; + +import io.seata.core.compressor.CompressorType; +import io.seata.core.protocol.ProtocolConstants; +import io.seata.core.protocol.RpcMessage; +import io.seata.core.rpc.netty.ProtocolRpcMessage; +import io.seata.core.serializer.SerializerType; + +import java.util.concurrent.atomic.AtomicLong; + +/** + * the protocol v0 rpc message + **/ +public class ProtocolRpcMessageV0 implements ProtocolRpcMessage { + + private static AtomicLong NEXT_ID = new AtomicLong(0); + + /** + * Gets next message id. + * + * @return the next message id + */ + public static long getNextMessageId() { + return NEXT_ID.incrementAndGet(); + } + + private long id; + private boolean isAsync; + private boolean isRequest; + private boolean isHeartbeat; + private Object body; + private byte messageType; + private boolean isSeataCodec; + + /** + * Gets id. + * + * @return the id + */ + public long getId() { + return id; + } + + /** + * Sets id. + * + * @param id the id + */ + public void setId(long id) { + this.id = id; + } + + /** + * Is async boolean. + * + * @return the boolean + */ + public boolean isAsync() { + return isAsync; + } + + /** + * Sets async. + * + * @param async the async + */ + public void setAsync(boolean async) { + isAsync = async; + } + + /** + * Is request boolean. + * + * @return the boolean + */ + public boolean isRequest() { + return isRequest; + } + + /** + * Sets request. + * + * @param request the request + */ + public void setRequest(boolean request) { + isRequest = request; + } + + /** + * Is heartbeat boolean. + * + * @return the boolean + */ + public boolean isHeartbeat() { + return isHeartbeat; + } + + /** + * Sets heartbeat. + * + * @param heartbeat the heartbeat + */ + public void setHeartbeat(boolean heartbeat) { + isHeartbeat = heartbeat; + } + + /** + * Gets body. + * + * @return the body + */ + public Object getBody() { + return body; + } + + /** + * Sets body. + * + * @param body the body + */ + public void setBody(Object body) { + this.body = body; + } + + public boolean isSeataCodec() { + return isSeataCodec; + } + + public void setSeataCodec(boolean seataCodec) { + isSeataCodec = seataCodec; + } + + public byte getMessageType() { + return messageType; + } + + public void setMessageType(byte messageType) { + this.messageType = messageType; + } + + @Override + public RpcMessage protocolMsg2RpcMsg() { + RpcMessage rpcMessage = new RpcMessage(); + rpcMessage.setMessageType(this.messageType); + rpcMessage.setCompressor(CompressorType.NONE.getCode()); + + byte codecType = this.isSeataCodec ? SerializerType.SEATA.getCode() : SerializerType.HESSIAN.getCode(); + rpcMessage.setCodec(codecType); + + if (this.isHeartbeat) { + if (this.isRequest) { + rpcMessage.setMessageType(ProtocolConstants.MSGTYPE_HEARTBEAT_REQUEST); + } else { + rpcMessage.setMessageType(ProtocolConstants.MSGTYPE_HEARTBEAT_RESPONSE); + } + } else { + if (this.isRequest) { + rpcMessage.setMessageType(ProtocolConstants.MSGTYPE_RESQUEST_ONEWAY); + } else { + rpcMessage.setMessageType(ProtocolConstants.MSGTYPE_RESPONSE); + } + } + rpcMessage.setBody(this.body); + rpcMessage.setId((int) this.id); + rpcMessage.setVersion(ProtocolRpcMessage.getVersion(this.body)); + return rpcMessage; + } + + @Override + public void rpcMsg2ProtocolMsg(RpcMessage rpcMessage) { + this.body = rpcMessage.getBody(); + ProtocolRpcMessage.setVersion(this.body, rpcMessage.getVersion()); + this.id = rpcMessage.getId(); + this.isRequest = isRequest(rpcMessage.getMessageType()); + this.isHeartbeat = isHeartbeat(rpcMessage.getMessageType()); + this.isSeataCodec = rpcMessage.getCodec() == SerializerType.SEATA.getCode(); + this.messageType = rpcMessage.getMessageType(); + } + + private boolean isHeartbeat(byte msgType) { + return msgType == ProtocolConstants.MSGTYPE_HEARTBEAT_REQUEST + || msgType == ProtocolConstants.MSGTYPE_HEARTBEAT_RESPONSE; + } + + private boolean isRequest(byte msgType) { + return msgType == ProtocolConstants.MSGTYPE_RESQUEST_ONEWAY + || msgType == ProtocolConstants.MSGTYPE_RESQUEST_SYNC; + } +} From a151ebab6482af8baa0ad5519d82f800cb60e0b0 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Fri, 29 Dec 2023 10:38:11 +0800 Subject: [PATCH 09/62] v0 --- .../core/protocol/ProtocolConstants.java | 9 +- .../java/io/seata/core/protocol/Version.java | 10 ++ .../core/rpc/netty/AbstractNettyRemoting.java | 19 ++- .../netty/AbstractNettyRemotingServer.java | 14 +- .../rpc/netty/CompatibleProtocolDecoder.java | 155 ++++++++++++++++++ .../rpc/netty/CompatibleProtocolEncoder.java | 77 +++++++++ .../core/rpc/netty/NettyClientBootstrap.java | 6 +- .../core/rpc/netty/NettyServerBootstrap.java | 6 +- .../seata/core/rpc/netty/ProtocolDecoder.java | 29 ++++ .../seata/core/rpc/netty/ProtocolEncoder.java | 28 ++++ .../core/rpc/netty/v0/MessageCodecV0.java | 44 +++++ .../rpc/netty/v0/ProtocolConstantsV0.java | 31 ++++ .../core/rpc/netty/v0/ProtocolDecoderV0.java | 132 +++++++++++++++ .../core/rpc/netty/v0/ProtocolEncoderV0.java | 106 ++++++++++++ ...lV1Decoder.java => ProtocolDecoderV1.java} | 53 +----- .../core/rpc/netty/v1/ProtocolEncoderV1.java | 121 ++++++++++++++ .../core/rpc/netty/v1/ProtocolV1Encoder.java | 122 -------------- .../rpc/processor/server/RegTmProcessor.java | 4 +- .../core/rpc/netty/v1/ProtocolV1Client.java | 6 +- .../core/rpc/netty/v1/ProtocolV1Server.java | 6 +- 20 files changed, 789 insertions(+), 189 deletions(-) create mode 100644 core/src/main/java/io/seata/core/rpc/netty/CompatibleProtocolDecoder.java create mode 100644 core/src/main/java/io/seata/core/rpc/netty/CompatibleProtocolEncoder.java create mode 100644 core/src/main/java/io/seata/core/rpc/netty/ProtocolDecoder.java create mode 100644 core/src/main/java/io/seata/core/rpc/netty/ProtocolEncoder.java create mode 100644 core/src/main/java/io/seata/core/rpc/netty/v0/MessageCodecV0.java create mode 100644 core/src/main/java/io/seata/core/rpc/netty/v0/ProtocolConstantsV0.java create mode 100644 core/src/main/java/io/seata/core/rpc/netty/v0/ProtocolDecoderV0.java create mode 100644 core/src/main/java/io/seata/core/rpc/netty/v0/ProtocolEncoderV0.java rename core/src/main/java/io/seata/core/rpc/netty/v1/{ProtocolV1Decoder.java => ProtocolDecoderV1.java} (74%) create mode 100644 core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolEncoderV1.java delete mode 100644 core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Encoder.java diff --git a/core/src/main/java/io/seata/core/protocol/ProtocolConstants.java b/core/src/main/java/io/seata/core/protocol/ProtocolConstants.java index 7b851ebc2a5..c6bae36e6fb 100644 --- a/core/src/main/java/io/seata/core/protocol/ProtocolConstants.java +++ b/core/src/main/java/io/seata/core/protocol/ProtocolConstants.java @@ -31,10 +31,17 @@ public interface ProtocolConstants { */ byte[] MAGIC_CODE_BYTES = {(byte) 0xda, (byte) 0xda}; + /** + * Old protocol version + */ + byte VERSION_0 = 0; + /** * Protocol version */ - byte VERSION = 1; + byte VERSION_1 = 1; + + byte VERSION_CURRENT = VERSION_1; /** * Max frame length diff --git a/core/src/main/java/io/seata/core/protocol/Version.java b/core/src/main/java/io/seata/core/protocol/Version.java index 164ac9ee82c..13eed45bd20 100644 --- a/core/src/main/java/io/seata/core/protocol/Version.java +++ b/core/src/main/java/io/seata/core/protocol/Version.java @@ -150,6 +150,16 @@ public static long convertVersionNotThrowException(String version) { return -1; } + public static byte calcProtocolVersion(String sdkVersion) throws IncompatibleVersionException { + long version = convertVersion(sdkVersion); + long v0 = convertVersion(VERSION_0_7_1); + if (version <= v0) { + return ProtocolConstants.VERSION_0; + } else { + return ProtocolConstants.VERSION_1; + } + } + private static long calculatePartValue(String partNumeric, int size, int index) { return Long.parseLong(partNumeric) * Double.valueOf(Math.pow(100, size - index)).longValue(); } diff --git a/core/src/main/java/io/seata/core/rpc/netty/AbstractNettyRemoting.java b/core/src/main/java/io/seata/core/rpc/netty/AbstractNettyRemoting.java index cad610c6ed2..22ca93122cc 100644 --- a/core/src/main/java/io/seata/core/rpc/netty/AbstractNettyRemoting.java +++ b/core/src/main/java/io/seata/core/rpc/netty/AbstractNettyRemoting.java @@ -43,6 +43,7 @@ import io.seata.core.protocol.MessageTypeAware; import io.seata.core.protocol.ProtocolConstants; import io.seata.core.protocol.RpcMessage; +import io.seata.core.protocol.Version; import io.seata.core.rpc.Disposable; import io.seata.core.rpc.hook.RpcHook; import io.seata.core.rpc.processor.Pair; @@ -62,7 +63,7 @@ public abstract class AbstractNettyRemoting implements Disposable { * The Timer executor. */ protected final ScheduledExecutorService timerExecutor = new ScheduledThreadPoolExecutor(1, - new NamedThreadFactory("timeoutChecker", 1, true)); + new NamedThreadFactory("timeoutChecker", 1, true)); /** * The Message executor. */ @@ -112,7 +113,7 @@ public void run() { futures.remove(entry.getKey()); RpcMessage rpcMessage = future.getRequestMessage(); future.setResultMessage(new TimeoutException(String - .format("msgId: %s ,msgType: %s ,msg: %s ,request timeout", rpcMessage.getId(), String.valueOf(rpcMessage.getMessageType()), rpcMessage.getBody().toString()))); + .format("msgId: %s ,msgType: %s ,msg: %s ,request timeout", rpcMessage.getId(), String.valueOf(rpcMessage.getMessageType()), rpcMessage.getBody().toString()))); if (LOGGER.isDebugEnabled()) { LOGGER.debug("timeout clear future: {}", entry.getValue().getRequestMessage().getBody()); } @@ -199,7 +200,7 @@ protected Object sendSync(Channel channel, RpcMessage rpcMessage, long timeoutMi return result; } catch (Exception exx) { LOGGER.error("wait response error:{},ip:{},request:{}", exx.getMessage(), channel.remoteAddress(), - rpcMessage.getBody()); + rpcMessage.getBody()); if (exx instanceof TimeoutException) { throw (TimeoutException) exx; } else { @@ -218,7 +219,7 @@ protected void sendAsync(Channel channel, RpcMessage rpcMessage) { channelWritableCheck(channel, rpcMessage.getBody()); if (LOGGER.isDebugEnabled()) { LOGGER.debug("write message:" + rpcMessage.getBody() + ", channel:" + channel + ",active?" - + channel.isActive() + ",writable?" + channel.isWritable() + ",isopen?" + channel.isOpen()); + + channel.isActive() + ",writable?" + channel.isWritable() + ",isopen?" + channel.isOpen()); } doBeforeRpcHooks(ChannelUtil.getAddressFromChannel(channel), rpcMessage); @@ -231,22 +232,32 @@ protected void sendAsync(Channel channel, RpcMessage rpcMessage) { } protected RpcMessage buildRequestMessage(Object msg, byte messageType) { + return buildRequestMessage(msg, messageType, Version.getCurrent()); + } + + protected RpcMessage buildRequestMessage(Object msg, byte messageType, String version) { RpcMessage rpcMessage = new RpcMessage(); rpcMessage.setId(getNextMessageId()); rpcMessage.setMessageType(messageType); rpcMessage.setCodec(ProtocolConstants.CONFIGURED_CODEC); rpcMessage.setCompressor(ProtocolConstants.CONFIGURED_COMPRESSOR); rpcMessage.setBody(msg); + rpcMessage.setVersion(version); return rpcMessage; } protected RpcMessage buildResponseMessage(RpcMessage rpcMessage, Object msg, byte messageType) { + return buildResponseMessage(rpcMessage, msg, messageType, Version.getCurrent()); + } + + protected RpcMessage buildResponseMessage(RpcMessage rpcMessage, Object msg, byte messageType, String version) { RpcMessage rpcMsg = new RpcMessage(); rpcMsg.setMessageType(messageType); rpcMsg.setCodec(rpcMessage.getCodec()); // same with request rpcMsg.setCompressor(rpcMessage.getCompressor()); rpcMsg.setBody(msg); rpcMsg.setId(rpcMessage.getId()); + rpcMsg.setVersion(version); return rpcMsg; } diff --git a/core/src/main/java/io/seata/core/rpc/netty/AbstractNettyRemotingServer.java b/core/src/main/java/io/seata/core/rpc/netty/AbstractNettyRemotingServer.java index 97e43923bf5..a6f947a34ed 100644 --- a/core/src/main/java/io/seata/core/rpc/netty/AbstractNettyRemotingServer.java +++ b/core/src/main/java/io/seata/core/rpc/netty/AbstractNettyRemotingServer.java @@ -69,16 +69,20 @@ public Object sendSyncRequest(String resourceId, String clientId, Object msg, bo if (channel == null) { throw new RuntimeException("rm client is not connected. dbkey:" + resourceId + ",clientId:" + clientId); } - RpcMessage rpcMessage = buildRequestMessage(msg, ProtocolConstants.MSGTYPE_RESQUEST_SYNC); + RpcContext rpcContext = ChannelManager.getContextFromIdentified(channel); + RpcMessage rpcMessage = buildRequestMessage(msg, ProtocolConstants.MSGTYPE_RESQUEST_SYNC, rpcContext.getVersion()); return super.sendSync(channel, rpcMessage, NettyServerConfig.getRpcRequestTimeout()); } + + @Override public Object sendSyncRequest(Channel channel, Object msg) throws TimeoutException { if (channel == null) { throw new RuntimeException("client is not connected"); } - RpcMessage rpcMessage = buildRequestMessage(msg, ProtocolConstants.MSGTYPE_RESQUEST_SYNC); + RpcContext rpcContext = ChannelManager.getContextFromIdentified(channel); + RpcMessage rpcMessage = buildRequestMessage(msg, ProtocolConstants.MSGTYPE_RESQUEST_SYNC, rpcContext.getVersion()); return super.sendSync(channel, rpcMessage, NettyServerConfig.getRpcRequestTimeout()); } @@ -87,7 +91,8 @@ public void sendAsyncRequest(Channel channel, Object msg) { if (channel == null) { throw new RuntimeException("client is not connected"); } - RpcMessage rpcMessage = buildRequestMessage(msg, ProtocolConstants.MSGTYPE_RESQUEST_ONEWAY); + RpcContext rpcContext = ChannelManager.getContextFromIdentified(channel); + RpcMessage rpcMessage = buildRequestMessage(msg, ProtocolConstants.MSGTYPE_RESQUEST_ONEWAY, rpcContext.getVersion()); super.sendAsync(channel, rpcMessage); } @@ -98,9 +103,10 @@ public void sendAsyncResponse(RpcMessage rpcMessage, Channel channel, Object msg clientChannel = ChannelManager.getSameClientChannel(channel); } if (clientChannel != null) { + RpcContext rpcContext = ChannelManager.getContextFromIdentified(channel); RpcMessage rpcMsg = buildResponseMessage(rpcMessage, msg, msg instanceof HeartbeatMessage ? ProtocolConstants.MSGTYPE_HEARTBEAT_RESPONSE - : ProtocolConstants.MSGTYPE_RESPONSE); + : ProtocolConstants.MSGTYPE_RESPONSE, rpcContext.getVersion()); super.sendAsync(clientChannel, rpcMsg); } else { throw new RuntimeException("channel is error."); diff --git a/core/src/main/java/io/seata/core/rpc/netty/CompatibleProtocolDecoder.java b/core/src/main/java/io/seata/core/rpc/netty/CompatibleProtocolDecoder.java new file mode 100644 index 00000000000..11e429cd208 --- /dev/null +++ b/core/src/main/java/io/seata/core/rpc/netty/CompatibleProtocolDecoder.java @@ -0,0 +1,155 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.seata.core.rpc.netty; + +import com.google.common.collect.ImmutableMap; +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.LengthFieldBasedFrameDecoder; +import io.seata.core.exception.DecodeException; +import io.seata.core.protocol.ProtocolConstants; +import io.seata.core.rpc.netty.v0.ProtocolDecoderV0; +import io.seata.core.rpc.netty.v1.ProtocolDecoderV1; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Map; + +/** + *
+ * (> 0.7.0)
+ * 0     1     2     3     4     5     6     7     8     9    10     11    12    13    14    15    16
+ * +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
+ * |   magic   |Proto|     Full length       |    Head   | Msg |Seria|Compr|     RequestId         |
+ * |   code    |colVer|    (head+body)      |   Length  |Type |lizer|ess  |                       |
+ * +-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+
+ *
+ * (<= 0.7.0)
+ * 0     1     2     3     4           6           8          10           12          14
+ * +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
+ * |   0xdada  |   flag    | typecode/ |                 requestid                     |
+ * |           |           | bodylength|                                               |
+ * +-----------+-----------+-----------+-----------+-----------+-----------+-----------+
+ *
+ * 
+ *

+ *

  • Full Length: include all data
  • + *
  • Head Length: include head data from magic code to head map.
  • + *
  • Body Length: Full Length - Head Length
  • + *

    + * https://github.com/seata/seata/issues/893 + * + */ +public class CompatibleProtocolDecoder extends LengthFieldBasedFrameDecoder { + + private static final Logger LOGGER = LoggerFactory.getLogger(CompatibleProtocolDecoder.class); + private static Map protocolDecoderMap; + + public CompatibleProtocolDecoder() { + // default is 8M + this(ProtocolConstants.MAX_FRAME_LENGTH); + } + + public CompatibleProtocolDecoder(int maxFrameLength) { + /* + int maxFrameLength, + int lengthFieldOffset, magic code is 2B, and version is 1B, and then FullLength. so value is 3 + int lengthFieldLength, FullLength is int(4B). so values is 4 + int lengthAdjustment, FullLength include all data and read 7 bytes before, so the left length is (FullLength-7). so values is -7 + int initialBytesToStrip we will check magic code and version self, so do not strip any bytes. so values is 0 + */ + super(maxFrameLength, 3, 4, -7, 0); + protocolDecoderMap = ImmutableMap.builder() + .put(ProtocolConstants.VERSION_0, new ProtocolDecoderV0()) + .put(ProtocolConstants.VERSION_1, new ProtocolDecoderV1()) + .build(); + } + + @Override + protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { + ByteBuf frame; + Object decoded; + byte version; + try { + if (isV0(in)) { + decoded = in; + version = ProtocolConstants.VERSION_0; + } else { + decoded = super.decode(ctx, in); + version = decideVersion(decoded); + } + + if (decoded instanceof ByteBuf) { + frame = (ByteBuf) decoded; + try { + ProtocolDecoder decoder = protocolDecoderMap.get(version); + if (decoder == null) { + throw new UnsupportedOperationException("Unsupported version: " + version); + } + return decoder.decodeFrame(frame); + } finally { + if (!isV0(version)) { + frame.release(); + } + } + } + } catch (Exception exx) { + LOGGER.error("Decode frame error, cause: {}", exx.getMessage()); + throw new DecodeException(exx); + } + return decoded; + } + + protected byte decideVersion(Object in) { + if (in instanceof ByteBuf) { + ByteBuf frame = (ByteBuf) in; + frame.markReaderIndex(); + byte b0 = frame.readByte(); + byte b1 = frame.readByte(); + if (ProtocolConstants.MAGIC_CODE_BYTES[0] != b0 + || ProtocolConstants.MAGIC_CODE_BYTES[1] != b1) { + throw new IllegalArgumentException("Unknown magic code: " + b0 + ", " + b1); + } + + byte version = frame.readByte(); + frame.resetReaderIndex(); + return version; + } + return -1; + } + + + protected boolean isV0(ByteBuf in) { + boolean isV0 = false; + in.markReaderIndex(); + byte b0 = in.readByte(); + byte b1 = in.readByte(); + byte version = in.readByte(); + if (ProtocolConstants.MAGIC_CODE_BYTES[0] == b0 + && ProtocolConstants.MAGIC_CODE_BYTES[1] == b1 + && isV0(version)) { + isV0 = true; + } + + in.resetReaderIndex(); + return isV0; + } + + protected boolean isV0(byte version) { + return version == ProtocolConstants.VERSION_0; + } +} \ No newline at end of file diff --git a/core/src/main/java/io/seata/core/rpc/netty/CompatibleProtocolEncoder.java b/core/src/main/java/io/seata/core/rpc/netty/CompatibleProtocolEncoder.java new file mode 100644 index 00000000000..a1a76254b8b --- /dev/null +++ b/core/src/main/java/io/seata/core/rpc/netty/CompatibleProtocolEncoder.java @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.seata.core.rpc.netty; + +import com.google.common.collect.ImmutableMap; +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToByteEncoder; +import io.seata.core.protocol.ProtocolConstants; +import io.seata.core.protocol.RpcMessage; +import io.seata.core.protocol.Version; +import io.seata.core.rpc.netty.v0.ProtocolEncoderV0; +import io.seata.core.rpc.netty.v1.ProtocolEncoderV1; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Map; + +/** + *
    + * 
    + *

    + *

  • Full Length: include all data
  • + *
  • Head Length: include head data from magic code to head map.
  • + *
  • Body Length: Full Length - Head Length
  • + *

    + * https://github.com/seata/seata/issues/893 + * + */ +public class CompatibleProtocolEncoder extends MessageToByteEncoder { + + private static final Logger LOGGER = LoggerFactory.getLogger(CompatibleProtocolEncoder.class); + + private static Map protocolEncoderMap; + + public CompatibleProtocolEncoder() { + super(); + protocolEncoderMap = ImmutableMap.builder() + .put(ProtocolConstants.VERSION_0, new ProtocolEncoderV0()) + .put(ProtocolConstants.VERSION_1, new ProtocolEncoderV1()) + .build(); + } + + @Override + public void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) { + try { + if (msg instanceof RpcMessage) { + RpcMessage rpcMessage = (RpcMessage) msg; + byte version = Version.calcProtocolVersion(rpcMessage.getVersion()); + ProtocolEncoder encoder = protocolEncoderMap.get(version); + if (encoder == null) { + throw new UnsupportedOperationException("Unsupported version: " + version); + } + + encoder.encode(rpcMessage, out); + } else { + throw new UnsupportedOperationException("Not support this class:" + msg.getClass()); + } + } catch (Throwable e) { + LOGGER.error("Encode request error!", e); + } + } +} diff --git a/core/src/main/java/io/seata/core/rpc/netty/NettyClientBootstrap.java b/core/src/main/java/io/seata/core/rpc/netty/NettyClientBootstrap.java index 1e9d6a0590c..3b37394a336 100644 --- a/core/src/main/java/io/seata/core/rpc/netty/NettyClientBootstrap.java +++ b/core/src/main/java/io/seata/core/rpc/netty/NettyClientBootstrap.java @@ -35,8 +35,6 @@ import io.seata.common.exception.FrameworkException; import io.seata.common.thread.NamedThreadFactory; import io.seata.core.rpc.RemotingBootstrap; -import io.seata.core.rpc.netty.v1.ProtocolV1Decoder; -import io.seata.core.rpc.netty.v1.ProtocolV1Encoder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -134,8 +132,8 @@ public void initChannel(SocketChannel ch) { new IdleStateHandler(nettyClientConfig.getChannelMaxReadIdleSeconds(), nettyClientConfig.getChannelMaxWriteIdleSeconds(), nettyClientConfig.getChannelMaxAllIdleSeconds())) - .addLast(new ProtocolV1Decoder()) - .addLast(new ProtocolV1Encoder()); + .addLast(new CompatibleProtocolDecoder()) + .addLast(new CompatibleProtocolEncoder()); if (channelHandlers != null) { addChannelPipelineLast(ch, channelHandlers); } diff --git a/core/src/main/java/io/seata/core/rpc/netty/NettyServerBootstrap.java b/core/src/main/java/io/seata/core/rpc/netty/NettyServerBootstrap.java index 1036f3df62b..0817285c2e7 100644 --- a/core/src/main/java/io/seata/core/rpc/netty/NettyServerBootstrap.java +++ b/core/src/main/java/io/seata/core/rpc/netty/NettyServerBootstrap.java @@ -36,8 +36,6 @@ import io.seata.common.thread.NamedThreadFactory; import io.seata.config.ConfigurationFactory; import io.seata.core.rpc.RemotingBootstrap; -import io.seata.core.rpc.netty.v1.ProtocolV1Decoder; -import io.seata.core.rpc.netty.v1.ProtocolV1Encoder; import io.seata.discovery.registry.MultiRegistryFactory; import io.seata.discovery.registry.RegistryService; import org.slf4j.Logger; @@ -157,8 +155,8 @@ public void start() { @Override public void initChannel(SocketChannel ch) { ch.pipeline().addLast(new IdleStateHandler(nettyServerConfig.getChannelMaxReadIdleSeconds(), 0, 0)) - .addLast(new ProtocolV1Decoder()) - .addLast(new ProtocolV1Encoder()); + .addLast(new CompatibleProtocolDecoder()) + .addLast(new CompatibleProtocolEncoder()); if (channelHandlers != null) { addChannelPipelineLast(ch, channelHandlers); } diff --git a/core/src/main/java/io/seata/core/rpc/netty/ProtocolDecoder.java b/core/src/main/java/io/seata/core/rpc/netty/ProtocolDecoder.java new file mode 100644 index 00000000000..bfef75f0efd --- /dev/null +++ b/core/src/main/java/io/seata/core/rpc/netty/ProtocolDecoder.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.seata.core.rpc.netty; + +import io.netty.buffer.ByteBuf; + +/** + * the protocol decoder + * + **/ +public interface ProtocolDecoder { + + ProtocolRpcMessage decodeFrame(ByteBuf in); + +} diff --git a/core/src/main/java/io/seata/core/rpc/netty/ProtocolEncoder.java b/core/src/main/java/io/seata/core/rpc/netty/ProtocolEncoder.java new file mode 100644 index 00000000000..49b8917c578 --- /dev/null +++ b/core/src/main/java/io/seata/core/rpc/netty/ProtocolEncoder.java @@ -0,0 +1,28 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.seata.core.rpc.netty; + +import io.netty.buffer.ByteBuf; +import io.seata.core.protocol.RpcMessage; + +/** + * the protocol encoder + * + **/ +public interface ProtocolEncoder { + void encode(RpcMessage rpcMessage, ByteBuf out); +} diff --git a/core/src/main/java/io/seata/core/rpc/netty/v0/MessageCodecV0.java b/core/src/main/java/io/seata/core/rpc/netty/v0/MessageCodecV0.java new file mode 100644 index 00000000000..1261601e4ac --- /dev/null +++ b/core/src/main/java/io/seata/core/rpc/netty/v0/MessageCodecV0.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.seata.core.rpc.netty.v0; + +import io.netty.buffer.ByteBuf; +import io.seata.core.protocol.MessageTypeAware; + +/** + * The interface Message codec. + * + */ +public interface MessageCodecV0 extends MessageTypeAware { + + /** + * Encode byte [ ]. + * + * @return the byte [ ] + */ + byte[] encode(); + + /** + * Decode boolean. + * + * @param in the in + * @return the boolean + */ + boolean decode(ByteBuf in); + + boolean decode(ByteBuf in, T req); +} diff --git a/core/src/main/java/io/seata/core/rpc/netty/v0/ProtocolConstantsV0.java b/core/src/main/java/io/seata/core/rpc/netty/v0/ProtocolConstantsV0.java new file mode 100644 index 00000000000..e86bea98e9b --- /dev/null +++ b/core/src/main/java/io/seata/core/rpc/netty/v0/ProtocolConstantsV0.java @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.seata.core.rpc.netty.v0; + +/** + * protocol v0 constants + * + **/ +public class ProtocolConstantsV0 { + public static short MAGIC = (short)0xdada; + + public static int HEAD_LENGTH = 14; + public static final short FLAG_REQUEST = 0x80; + public static final short FLAG_ASYNC = 0x40; + public static final short FLAG_HEARTBEAT = 0x20; + public static final short FLAG_SEATA_CODEC = 0x10; +} diff --git a/core/src/main/java/io/seata/core/rpc/netty/v0/ProtocolDecoderV0.java b/core/src/main/java/io/seata/core/rpc/netty/v0/ProtocolDecoderV0.java new file mode 100644 index 00000000000..91078e1ec11 --- /dev/null +++ b/core/src/main/java/io/seata/core/rpc/netty/v0/ProtocolDecoderV0.java @@ -0,0 +1,132 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.seata.core.rpc.netty.v0; + +import io.netty.buffer.ByteBuf; +import io.seata.core.protocol.HeartbeatMessage; + +import io.seata.core.protocol.ProtocolConstants; +import io.seata.core.rpc.netty.ProtocolDecoder; +import io.seata.core.serializer.Serializer; +import io.seata.core.serializer.SerializerServiceLoader; +import io.seata.core.serializer.SerializerType; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + *
    + *  seata-version < 0.7
    + *  Only used in TC receives a request from RM/TM.
    + * 0     1     2     3     4           6           8          10           12          14         16
    + * +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
    + * |   0xdada  |   flag    | typecode/ |                 requestid                     |           |
    + * |           |           | bodylength|                                               |           |
    + * +-----------+-----------+-----------+-----------+-----------+-----------+-----------+           +
    + * |                                    ... ...                                                    |
    + * +                                                                                               +
    + * |                                     body                                                      |
    + * +                                                                                               +
    + * |                                    ... ...                                                    |
    + * +-----------------------------------------------------------------------------------------------+
    + *
    + * 
    + *

    + *

  • flag: msg type
  • + *
  • typecode: action type code
  • + *
  • bodylength: body Length
  • + *
  • requestid: request id
  • + *

    + * + * @see ProtocolEncoderV0 + */ +public class ProtocolDecoderV0 implements ProtocolDecoder { + + private static final Logger LOGGER = LoggerFactory.getLogger(ProtocolDecoderV0.class); + + + @Override + public ProtocolRpcMessageV0 decodeFrame(ByteBuf in) { + ProtocolRpcMessageV0 rpcMessage = new ProtocolRpcMessageV0(); + if (in.readableBytes() < ProtocolConstantsV0.HEAD_LENGTH) { + throw new IllegalArgumentException("Nothing to decode."); + } + + in.markReaderIndex(); + short protocol = in.readShort(); + int flag = (int) in.readShort(); + + boolean isHeartbeat = (ProtocolConstantsV0.FLAG_HEARTBEAT & flag) > 0; + boolean isRequest = (ProtocolConstantsV0.FLAG_REQUEST & flag) > 0; + boolean isSeataCodec = (ProtocolConstantsV0.FLAG_SEATA_CODEC & flag) > 0; + rpcMessage.setSeataCodec(isSeataCodec); + + short bodyLength = 0; + short typeCode = 0; + if (!isSeataCodec) { + bodyLength = in.readShort(); + } else { + typeCode = in.readShort(); + } + long msgId = in.readLong(); + rpcMessage.setId(msgId); + if (isHeartbeat) { + rpcMessage.setAsync(true); + rpcMessage.setHeartbeat(isHeartbeat); + rpcMessage.setRequest(isRequest); + if (isRequest) { + rpcMessage.setBody(HeartbeatMessage.PING); + } else { + rpcMessage.setBody(HeartbeatMessage.PONG); + } + + return rpcMessage; + } + + if (bodyLength > 0 && in.readableBytes() < bodyLength) { + in.resetReaderIndex(); + throw new IllegalArgumentException("readableBytes < bodyLength"); + } + + rpcMessage.setAsync((ProtocolConstantsV0.FLAG_ASYNC & flag) > 0); + rpcMessage.setHeartbeat(false); + rpcMessage.setRequest(isRequest); + + try { + int length = in.readableBytes(); + byte[] bs = new byte[length]; + in.readBytes(bs); + + // fill messageType in v0 + byte[] bs2 = new byte[2 + length]; + bs2[0] = (byte) (0x00FF & (typeCode >> 8)); + bs2[1] = (byte) (0x00FF & typeCode); + System.arraycopy(bs, 0, bs2, 2, length); + byte codecType = isSeataCodec ? SerializerType.SEATA.getCode() : SerializerType.HESSIAN.getCode(); + Serializer serializer = SerializerServiceLoader.load(SerializerType.getByCode(codecType), ProtocolConstants.VERSION_0); + rpcMessage.setBody(serializer.deserialize(bs2)); + } catch (Exception e) { + LOGGER.error("decode error", e); + throw e; + } + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Receive:" + rpcMessage.getBody() + ", messageId:" + msgId); + } + return rpcMessage; + } + + +} diff --git a/core/src/main/java/io/seata/core/rpc/netty/v0/ProtocolEncoderV0.java b/core/src/main/java/io/seata/core/rpc/netty/v0/ProtocolEncoderV0.java new file mode 100644 index 00000000000..e72f3f9f53a --- /dev/null +++ b/core/src/main/java/io/seata/core/rpc/netty/v0/ProtocolEncoderV0.java @@ -0,0 +1,106 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.seata.core.rpc.netty.v0; + +import io.netty.buffer.ByteBuf; +import io.seata.core.protocol.HeartbeatMessage; +import io.seata.core.protocol.MessageTypeAware; +import io.seata.core.protocol.ProtocolConstants; +import io.seata.core.protocol.RpcMessage; +import io.seata.core.rpc.netty.ProtocolEncoder; +import io.seata.core.serializer.Serializer; +import io.seata.core.serializer.SerializerServiceLoader; +import io.seata.core.serializer.SerializerType; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + *
    + *  seata-version < 0.7
    + *  Only used in TC send a request to RM/TM.
    + * 0     1     2     3     4           6           8          10           12          14         16
    + * +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
    + * |   0xdada  |   flag    | typecode/ |                 requestid                     |           |
    + * |           |           | bodylength|                                               |           |
    + * +-----------+-----------+-----------+-----------+-----------+-----------+-----------+           +
    + * |                                    ... ...                                                    |
    + * +                                                                                               +
    + * |                                     body                                                      |
    + * +                                                                                               +
    + * |                                    ... ...                                                    |
    + * +-----------------------------------------------------------------------------------------------+
    + *
    + * 
    + *

    + *

  • flag: msg type
  • + *
  • typecode: action type code
  • + *
  • bodylength: body Length
  • + *
  • requestid: request id
  • + *

    + * + * @see ProtocolDecoderV0 + */ +public class ProtocolEncoderV0 implements ProtocolEncoder { + + private static final Logger LOGGER = LoggerFactory.getLogger(ProtocolEncoderV0.class); + + @Override + public void encode(RpcMessage message, ByteBuf out) { + try { + byte codec = message.getCodec(); + ProtocolRpcMessageV0 msg = new ProtocolRpcMessageV0(); + msg.rpcMsg2ProtocolMsg(message); + + out.writeShort(ProtocolConstantsV0.MAGIC); + int flag = (msg.isAsync() ? ProtocolConstantsV0.FLAG_ASYNC : 0) + | (msg.isHeartbeat() ? ProtocolConstantsV0.FLAG_HEARTBEAT : 0) + | (msg.isRequest() ? ProtocolConstantsV0.FLAG_REQUEST : 0) + | (msg.isSeataCodec() ? ProtocolConstantsV0.FLAG_SEATA_CODEC : 0); + + out.writeShort((short) flag); + + if (msg.getBody() instanceof HeartbeatMessage) { + out.writeShort((short) 0); + out.writeLong(msg.getId()); + return; + } + + byte[] bodyBytes = null; + Serializer serializer = SerializerServiceLoader.load(SerializerType.getByCode(codec), ProtocolConstants.VERSION_0); + bodyBytes = serializer.serialize(msg.getBody()); + + if (msg.isSeataCodec()) { + if (msg.getBody() instanceof MessageTypeAware) { + short typeCode = ((MessageTypeAware) msg.getBody()).getTypeCode(); + out.writeShort(typeCode); + } + } else { + out.writeShort(bodyBytes.length); + } + out.writeLong(msg.getId()); + if (bodyBytes != null) { + out.writeBytes(bodyBytes); + } + + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Send:" + msg.getBody()); + } + } catch (Throwable e) { + LOGGER.error("Encode request error!", e); + } + } +} diff --git a/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Decoder.java b/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolDecoderV1.java similarity index 74% rename from core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Decoder.java rename to core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolDecoderV1.java index 2e5453d9da9..d6a4de70d61 100644 --- a/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Decoder.java +++ b/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolDecoderV1.java @@ -17,14 +17,13 @@ package io.seata.core.rpc.netty.v1; import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.LengthFieldBasedFrameDecoder; -import io.seata.core.exception.DecodeException; -import io.seata.core.serializer.Serializer; import io.seata.core.compressor.Compressor; import io.seata.core.compressor.CompressorFactory; import io.seata.core.protocol.HeartbeatMessage; import io.seata.core.protocol.ProtocolConstants; +import io.seata.core.rpc.netty.ProtocolDecoder; +import io.seata.core.rpc.netty.ProtocolRpcMessage; +import io.seata.core.serializer.Serializer; import io.seata.core.serializer.SerializerServiceLoader; import io.seata.core.serializer.SerializerType; import org.slf4j.Logger; @@ -56,49 +55,16 @@ * https://github.com/seata/seata/issues/893 * * @see ProtocolV1Encoder + * @author Geng Zhang + * @see ProtocolEncoderV1 * @since 0.7.0 */ -public class ProtocolV1Decoder extends LengthFieldBasedFrameDecoder { - - private static final Logger LOGGER = LoggerFactory.getLogger(ProtocolV1Decoder.class); - - public ProtocolV1Decoder() { - // default is 8M - this(ProtocolConstants.MAX_FRAME_LENGTH); - } +public class ProtocolDecoderV1 implements ProtocolDecoder { - public ProtocolV1Decoder(int maxFrameLength) { - /* - int maxFrameLength, - int lengthFieldOffset, magic code is 2B, and version is 1B, and then FullLength. so value is 3 - int lengthFieldLength, FullLength is int(4B). so values is 4 - int lengthAdjustment, FullLength include all data and read 7 bytes before, so the left length is (FullLength-7). so values is -7 - int initialBytesToStrip we will check magic code and version self, so do not strip any bytes. so values is 0 - */ - super(maxFrameLength, 3, 4, -7, 0); - } + private static final Logger LOGGER = LoggerFactory.getLogger(ProtocolDecoderV1.class); @Override - protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { - Object decoded; - try { - decoded = super.decode(ctx, in); - if (decoded instanceof ByteBuf) { - ByteBuf frame = (ByteBuf)decoded; - try { - return decodeFrame(frame); - } finally { - frame.release(); - } - } - } catch (Exception exx) { - LOGGER.error("Decode frame error, cause: {}", exx.getMessage()); - throw new DecodeException(exx); - } - return decoded; - } - - public Object decodeFrame(ByteBuf frame) { + public ProtocolRpcMessage decodeFrame(ByteBuf frame) { byte b0 = frame.readByte(); byte b1 = frame.readByte(); if (ProtocolConstants.MAGIC_CODE_BYTES[0] != b0 @@ -107,7 +73,6 @@ public Object decodeFrame(ByteBuf frame) { } byte version = frame.readByte(); - // TODO check version compatible here int fullLength = frame.readInt(); short headLength = frame.readShort(); @@ -141,7 +106,7 @@ public Object decodeFrame(ByteBuf frame) { frame.readBytes(bs); Compressor compressor = CompressorFactory.getCompressor(compressorType); bs = compressor.decompress(bs); - Serializer serializer = SerializerServiceLoader.load(SerializerType.getByCode(rpcMessage.getCodec())); + Serializer serializer = SerializerServiceLoader.load(SerializerType.getByCode(rpcMessage.getCodec()), version); rpcMessage.setBody(serializer.deserialize(bs)); } } diff --git a/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolEncoderV1.java b/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolEncoderV1.java new file mode 100644 index 00000000000..91030c82b4f --- /dev/null +++ b/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolEncoderV1.java @@ -0,0 +1,121 @@ +/* + * Copyright 1999-2019 Seata.io Group. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.seata.core.rpc.netty.v1; + +import io.netty.buffer.ByteBuf; +import io.seata.core.rpc.netty.ProtocolEncoder; +import io.seata.core.serializer.Serializer; +import io.seata.core.compressor.Compressor; +import io.seata.core.compressor.CompressorFactory; +import io.seata.core.protocol.ProtocolConstants; +import io.seata.core.protocol.RpcMessage; +import io.seata.core.serializer.SerializerServiceLoader; +import io.seata.core.serializer.SerializerType; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Map; + +/** + *
    + * 0     1     2     3     4     5     6     7     8     9    10     11    12    13    14    15    16
    + * +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
    + * |   magic   |Proto|     Full length       |    Head   | Msg |Seria|Compr|     RequestId         |
    + * |   code    |colVer|    (head+body)      |   Length  |Type |lizer|ess  |                       |
    + * +-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+
    + * |                                                                                               |
    + * |                                   Head Map [Optional]                                         |
    + * +-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+
    + * |                                                                                               |
    + * |                                         body                                                  |
    + * |                                                                                               |
    + * |                                        ... ...                                                |
    + * +-----------------------------------------------------------------------------------------------+
    + * 
    + *

    + *

  • Full Length: include all data
  • + *
  • Head Length: include head data from magic code to head map.
  • + *
  • Body Length: Full Length - Head Length
  • + *

    + * https://github.com/seata/seata/issues/893 + * + * @author Geng Zhang + * @see ProtocolDecoderV1 + * @since 0.7.0 + */ +public class ProtocolEncoderV1 implements ProtocolEncoder { + + private static final Logger LOGGER = LoggerFactory.getLogger(ProtocolEncoderV1.class); + + + public void encode(RpcMessage message, ByteBuf out) { + try { + + ProtocolRpcMessageV1 rpcMessage = new ProtocolRpcMessageV1(); + rpcMessage.rpcMsg2ProtocolMsg(message); + + int fullLength = ProtocolConstants.V1_HEAD_LENGTH; + int headLength = ProtocolConstants.V1_HEAD_LENGTH; + + byte messageType = rpcMessage.getMessageType(); + out.writeBytes(ProtocolConstants.MAGIC_CODE_BYTES); + out.writeByte(ProtocolConstants.VERSION_1); + // full Length(4B) and head length(2B) will fix in the end. + out.writerIndex(out.writerIndex() + 6); + out.writeByte(messageType); + out.writeByte(rpcMessage.getCodec()); + out.writeByte(rpcMessage.getCompressor()); + out.writeInt(rpcMessage.getId()); + + // direct write head with zero-copy + Map headMap = rpcMessage.getHeadMap(); + if (headMap != null && !headMap.isEmpty()) { + int headMapBytesLength = HeadMapSerializer.getInstance().encode(headMap, out); + headLength += headMapBytesLength; + fullLength += headMapBytesLength; + } + + byte[] bodyBytes = null; + if (messageType != ProtocolConstants.MSGTYPE_HEARTBEAT_REQUEST + && messageType != ProtocolConstants.MSGTYPE_HEARTBEAT_RESPONSE) { + // heartbeat has no body + Serializer serializer = SerializerServiceLoader.load(SerializerType.getByCode(rpcMessage.getCodec()), ProtocolConstants.VERSION_1); + bodyBytes = serializer.serialize(rpcMessage.getBody()); + Compressor compressor = CompressorFactory.getCompressor(rpcMessage.getCompressor()); + bodyBytes = compressor.compress(bodyBytes); + fullLength += bodyBytes.length; + } + + if (bodyBytes != null) { + out.writeBytes(bodyBytes); + } + + // fix fullLength and headLength + int writeIndex = out.writerIndex(); + // skip magic code(2B) + version(1B) + out.writerIndex(writeIndex - fullLength + 3); + out.writeInt(fullLength); + out.writeShort(headLength); + out.writerIndex(writeIndex); + + + } catch (Throwable e) { + LOGGER.error("Encode request error!", e); + // todo + throw e; + } + } +} diff --git a/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Encoder.java b/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Encoder.java deleted file mode 100644 index 1af01f711c8..00000000000 --- a/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Encoder.java +++ /dev/null @@ -1,122 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.seata.core.rpc.netty.v1; - -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.MessageToByteEncoder; -import io.seata.core.serializer.Serializer; -import io.seata.core.compressor.Compressor; -import io.seata.core.compressor.CompressorFactory; -import io.seata.core.protocol.ProtocolConstants; -import io.seata.core.protocol.RpcMessage; -import io.seata.core.serializer.SerializerServiceLoader; -import io.seata.core.serializer.SerializerType; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Map; - -/** - *
    - * 0     1     2     3     4     5     6     7     8     9    10     11    12    13    14    15    16
    - * +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
    - * |   magic   |Proto|     Full length       |    Head   | Msg |Seria|Compr|     RequestId         |
    - * |   code    |colVer|    (head+body)      |   Length  |Type |lizer|ess  |                       |
    - * +-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+
    - * |                                                                                               |
    - * |                                   Head Map [Optional]                                         |
    - * +-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+
    - * |                                                                                               |
    - * |                                         body                                                  |
    - * |                                                                                               |
    - * |                                        ... ...                                                |
    - * +-----------------------------------------------------------------------------------------------+
    - * 
    - *

    - *

  • Full Length: include all data
  • - *
  • Head Length: include head data from magic code to head map.
  • - *
  • Body Length: Full Length - Head Length
  • - *

    - * https://github.com/seata/seata/issues/893 - * - * @see ProtocolV1Decoder - * @since 0.7.0 - */ -public class ProtocolV1Encoder extends MessageToByteEncoder { - - private static final Logger LOGGER = LoggerFactory.getLogger(ProtocolV1Encoder.class); - - @Override - public void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) { - try { - if (msg instanceof RpcMessage) { - RpcMessage rpcMsg = (RpcMessage) msg; - - ProtocolRpcMessageV1 rpcMessage = new ProtocolRpcMessageV1(); - rpcMessage.rpcMsg2ProtocolMsg(rpcMsg); - int fullLength = ProtocolConstants.V1_HEAD_LENGTH; - int headLength = ProtocolConstants.V1_HEAD_LENGTH; - - byte messageType = rpcMessage.getMessageType(); - out.writeBytes(ProtocolConstants.MAGIC_CODE_BYTES); - out.writeByte(ProtocolConstants.VERSION); - // full Length(4B) and head length(2B) will fix in the end. - out.writerIndex(out.writerIndex() + 6); - out.writeByte(messageType); - out.writeByte(rpcMessage.getCodec()); - out.writeByte(rpcMessage.getCompressor()); - out.writeInt(rpcMessage.getId()); - - // direct write head with zero-copy - Map headMap = rpcMessage.getHeadMap(); - if (headMap != null && !headMap.isEmpty()) { - int headMapBytesLength = HeadMapSerializer.getInstance().encode(headMap, out); - headLength += headMapBytesLength; - fullLength += headMapBytesLength; - } - - byte[] bodyBytes = null; - if (messageType != ProtocolConstants.MSGTYPE_HEARTBEAT_REQUEST - && messageType != ProtocolConstants.MSGTYPE_HEARTBEAT_RESPONSE) { - // heartbeat has no body - Serializer serializer = SerializerServiceLoader.load(SerializerType.getByCode(rpcMessage.getCodec())); - bodyBytes = serializer.serialize(rpcMessage.getBody()); - Compressor compressor = CompressorFactory.getCompressor(rpcMessage.getCompressor()); - bodyBytes = compressor.compress(bodyBytes); - fullLength += bodyBytes.length; - } - - if (bodyBytes != null) { - out.writeBytes(bodyBytes); - } - - // fix fullLength and headLength - int writeIndex = out.writerIndex(); - // skip magic code(2B) + version(1B) - out.writerIndex(writeIndex - fullLength + 3); - out.writeInt(fullLength); - out.writeShort(headLength); - out.writerIndex(writeIndex); - } else { - throw new UnsupportedOperationException("Not support this class:" + msg.getClass()); - } - } catch (Throwable e) { - LOGGER.error("Encode request error!", e); - } - } -} diff --git a/core/src/main/java/io/seata/core/rpc/processor/server/RegTmProcessor.java b/core/src/main/java/io/seata/core/rpc/processor/server/RegTmProcessor.java index 494d2b8cdee..7aa458cdf57 100644 --- a/core/src/main/java/io/seata/core/rpc/processor/server/RegTmProcessor.java +++ b/core/src/main/java/io/seata/core/rpc/processor/server/RegTmProcessor.java @@ -89,8 +89,8 @@ private void onRegTmMessage(ChannelHandlerContext ctx, RpcMessage rpcMessage) { } remotingServer.sendAsyncResponse(rpcMessage, ctx.channel(), response); if (isSuccess && LOGGER.isInfoEnabled()) { - LOGGER.info("TM register success,message:{},channel:{},client version:{}", message, ctx.channel(), - message.getVersion()); + LOGGER.info("TM register success,message:{},channel:{},client version:{},client protocol-version:{}" + , message, ctx.channel(), message.getVersion(), rpcMessage.getVersion()); } } diff --git a/test/src/test/java/io/seata/core/rpc/netty/v1/ProtocolV1Client.java b/test/src/test/java/io/seata/core/rpc/netty/v1/ProtocolV1Client.java index 0736b9740ac..0eb6f34aa7d 100644 --- a/test/src/test/java/io/seata/core/rpc/netty/v1/ProtocolV1Client.java +++ b/test/src/test/java/io/seata/core/rpc/netty/v1/ProtocolV1Client.java @@ -31,6 +31,8 @@ import io.netty.util.concurrent.DefaultPromise; import io.seata.common.thread.NamedThreadFactory; import io.seata.common.thread.PositiveAtomicCounter; +import io.seata.core.rpc.netty.CompatibleProtocolDecoder; +import io.seata.core.rpc.netty.CompatibleProtocolEncoder; import io.seata.core.serializer.SerializerType; import io.seata.core.model.BranchType; import io.seata.core.protocol.ProtocolConstants; @@ -80,8 +82,8 @@ public void connect(String host, int port, int connectTimeout) { @Override protected void initChannel(Channel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); - pipeline.addLast(new ProtocolV1Encoder()); - pipeline.addLast(new ProtocolV1Decoder(8 * 1024 * 1024)); + pipeline.addLast(new CompatibleProtocolEncoder()); + pipeline.addLast(new CompatibleProtocolDecoder(8 * 1024 * 1024)); pipeline.addLast(new ClientChannelHandler(ProtocolV1Client.this)); } }); diff --git a/test/src/test/java/io/seata/core/rpc/netty/v1/ProtocolV1Server.java b/test/src/test/java/io/seata/core/rpc/netty/v1/ProtocolV1Server.java index 6d655b800e0..5f6a1e3cf98 100644 --- a/test/src/test/java/io/seata/core/rpc/netty/v1/ProtocolV1Server.java +++ b/test/src/test/java/io/seata/core/rpc/netty/v1/ProtocolV1Server.java @@ -32,6 +32,8 @@ import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; import io.seata.common.thread.NamedThreadFactory; +import io.seata.core.rpc.netty.CompatibleProtocolDecoder; +import io.seata.core.rpc.netty.CompatibleProtocolEncoder; import java.net.InetSocketAddress; import java.util.concurrent.TimeUnit; @@ -67,8 +69,8 @@ public void start() { @Override protected void initChannel(Channel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); - pipeline.addLast(new ProtocolV1Decoder(8 * 1024 * 1024)); - pipeline.addLast(new ProtocolV1Encoder()); + pipeline.addLast(new CompatibleProtocolDecoder(8 * 1024 * 1024)); + pipeline.addLast(new CompatibleProtocolEncoder()); pipeline.addLast(new ServerChannelHandler()); } }); From 8c197af15dbf1faee4f6ef09f794339878d0f02b Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Fri, 29 Dec 2023 10:52:13 +0800 Subject: [PATCH 10/62] v0 --- .../core/rpc/netty/AbstractNettyRemoting.java | 19 +++++++++++++++---- .../netty/AbstractNettyRemotingServer.java | 14 ++++++++++---- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/io/seata/core/rpc/netty/AbstractNettyRemoting.java b/core/src/main/java/io/seata/core/rpc/netty/AbstractNettyRemoting.java index cad610c6ed2..22ca93122cc 100644 --- a/core/src/main/java/io/seata/core/rpc/netty/AbstractNettyRemoting.java +++ b/core/src/main/java/io/seata/core/rpc/netty/AbstractNettyRemoting.java @@ -43,6 +43,7 @@ import io.seata.core.protocol.MessageTypeAware; import io.seata.core.protocol.ProtocolConstants; import io.seata.core.protocol.RpcMessage; +import io.seata.core.protocol.Version; import io.seata.core.rpc.Disposable; import io.seata.core.rpc.hook.RpcHook; import io.seata.core.rpc.processor.Pair; @@ -62,7 +63,7 @@ public abstract class AbstractNettyRemoting implements Disposable { * The Timer executor. */ protected final ScheduledExecutorService timerExecutor = new ScheduledThreadPoolExecutor(1, - new NamedThreadFactory("timeoutChecker", 1, true)); + new NamedThreadFactory("timeoutChecker", 1, true)); /** * The Message executor. */ @@ -112,7 +113,7 @@ public void run() { futures.remove(entry.getKey()); RpcMessage rpcMessage = future.getRequestMessage(); future.setResultMessage(new TimeoutException(String - .format("msgId: %s ,msgType: %s ,msg: %s ,request timeout", rpcMessage.getId(), String.valueOf(rpcMessage.getMessageType()), rpcMessage.getBody().toString()))); + .format("msgId: %s ,msgType: %s ,msg: %s ,request timeout", rpcMessage.getId(), String.valueOf(rpcMessage.getMessageType()), rpcMessage.getBody().toString()))); if (LOGGER.isDebugEnabled()) { LOGGER.debug("timeout clear future: {}", entry.getValue().getRequestMessage().getBody()); } @@ -199,7 +200,7 @@ protected Object sendSync(Channel channel, RpcMessage rpcMessage, long timeoutMi return result; } catch (Exception exx) { LOGGER.error("wait response error:{},ip:{},request:{}", exx.getMessage(), channel.remoteAddress(), - rpcMessage.getBody()); + rpcMessage.getBody()); if (exx instanceof TimeoutException) { throw (TimeoutException) exx; } else { @@ -218,7 +219,7 @@ protected void sendAsync(Channel channel, RpcMessage rpcMessage) { channelWritableCheck(channel, rpcMessage.getBody()); if (LOGGER.isDebugEnabled()) { LOGGER.debug("write message:" + rpcMessage.getBody() + ", channel:" + channel + ",active?" - + channel.isActive() + ",writable?" + channel.isWritable() + ",isopen?" + channel.isOpen()); + + channel.isActive() + ",writable?" + channel.isWritable() + ",isopen?" + channel.isOpen()); } doBeforeRpcHooks(ChannelUtil.getAddressFromChannel(channel), rpcMessage); @@ -231,22 +232,32 @@ protected void sendAsync(Channel channel, RpcMessage rpcMessage) { } protected RpcMessage buildRequestMessage(Object msg, byte messageType) { + return buildRequestMessage(msg, messageType, Version.getCurrent()); + } + + protected RpcMessage buildRequestMessage(Object msg, byte messageType, String version) { RpcMessage rpcMessage = new RpcMessage(); rpcMessage.setId(getNextMessageId()); rpcMessage.setMessageType(messageType); rpcMessage.setCodec(ProtocolConstants.CONFIGURED_CODEC); rpcMessage.setCompressor(ProtocolConstants.CONFIGURED_COMPRESSOR); rpcMessage.setBody(msg); + rpcMessage.setVersion(version); return rpcMessage; } protected RpcMessage buildResponseMessage(RpcMessage rpcMessage, Object msg, byte messageType) { + return buildResponseMessage(rpcMessage, msg, messageType, Version.getCurrent()); + } + + protected RpcMessage buildResponseMessage(RpcMessage rpcMessage, Object msg, byte messageType, String version) { RpcMessage rpcMsg = new RpcMessage(); rpcMsg.setMessageType(messageType); rpcMsg.setCodec(rpcMessage.getCodec()); // same with request rpcMsg.setCompressor(rpcMessage.getCompressor()); rpcMsg.setBody(msg); rpcMsg.setId(rpcMessage.getId()); + rpcMsg.setVersion(version); return rpcMsg; } diff --git a/core/src/main/java/io/seata/core/rpc/netty/AbstractNettyRemotingServer.java b/core/src/main/java/io/seata/core/rpc/netty/AbstractNettyRemotingServer.java index 97e43923bf5..a6f947a34ed 100644 --- a/core/src/main/java/io/seata/core/rpc/netty/AbstractNettyRemotingServer.java +++ b/core/src/main/java/io/seata/core/rpc/netty/AbstractNettyRemotingServer.java @@ -69,16 +69,20 @@ public Object sendSyncRequest(String resourceId, String clientId, Object msg, bo if (channel == null) { throw new RuntimeException("rm client is not connected. dbkey:" + resourceId + ",clientId:" + clientId); } - RpcMessage rpcMessage = buildRequestMessage(msg, ProtocolConstants.MSGTYPE_RESQUEST_SYNC); + RpcContext rpcContext = ChannelManager.getContextFromIdentified(channel); + RpcMessage rpcMessage = buildRequestMessage(msg, ProtocolConstants.MSGTYPE_RESQUEST_SYNC, rpcContext.getVersion()); return super.sendSync(channel, rpcMessage, NettyServerConfig.getRpcRequestTimeout()); } + + @Override public Object sendSyncRequest(Channel channel, Object msg) throws TimeoutException { if (channel == null) { throw new RuntimeException("client is not connected"); } - RpcMessage rpcMessage = buildRequestMessage(msg, ProtocolConstants.MSGTYPE_RESQUEST_SYNC); + RpcContext rpcContext = ChannelManager.getContextFromIdentified(channel); + RpcMessage rpcMessage = buildRequestMessage(msg, ProtocolConstants.MSGTYPE_RESQUEST_SYNC, rpcContext.getVersion()); return super.sendSync(channel, rpcMessage, NettyServerConfig.getRpcRequestTimeout()); } @@ -87,7 +91,8 @@ public void sendAsyncRequest(Channel channel, Object msg) { if (channel == null) { throw new RuntimeException("client is not connected"); } - RpcMessage rpcMessage = buildRequestMessage(msg, ProtocolConstants.MSGTYPE_RESQUEST_ONEWAY); + RpcContext rpcContext = ChannelManager.getContextFromIdentified(channel); + RpcMessage rpcMessage = buildRequestMessage(msg, ProtocolConstants.MSGTYPE_RESQUEST_ONEWAY, rpcContext.getVersion()); super.sendAsync(channel, rpcMessage); } @@ -98,9 +103,10 @@ public void sendAsyncResponse(RpcMessage rpcMessage, Channel channel, Object msg clientChannel = ChannelManager.getSameClientChannel(channel); } if (clientChannel != null) { + RpcContext rpcContext = ChannelManager.getContextFromIdentified(channel); RpcMessage rpcMsg = buildResponseMessage(rpcMessage, msg, msg instanceof HeartbeatMessage ? ProtocolConstants.MSGTYPE_HEARTBEAT_RESPONSE - : ProtocolConstants.MSGTYPE_RESPONSE); + : ProtocolConstants.MSGTYPE_RESPONSE, rpcContext.getVersion()); super.sendAsync(clientChannel, rpcMsg); } else { throw new RuntimeException("channel is error."); From d8c19a42d129895637460f29fda6984d7f861061 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Fri, 29 Dec 2023 10:55:35 +0800 Subject: [PATCH 11/62] v0 --- .../serializer/seata/SeataSerializer.java | 19 ++++++++++--------- .../seata/protocol/v0/SeataSerializerV0.java | 2 +- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java index e23c436ce59..5ab4fdf0fda 100644 --- a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java +++ b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java @@ -16,20 +16,16 @@ */ package io.seata.serializer.seata; -import java.nio.ByteBuffer; - -import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; -import io.seata.common.exception.NotSupportYetException; +import com.google.common.collect.ImmutableMap; import io.seata.common.loader.LoadLevel; import io.seata.common.loader.Scope; -import io.seata.common.util.BufferUtils; -import io.seata.core.protocol.AbstractMessage; import io.seata.core.protocol.ProtocolConstants; import io.seata.core.serializer.Serializer; -import io.seata.serializer.seata.protocol.v1.MessageCodecFactoryV1; +import io.seata.serializer.seata.protocol.v0.SeataSerializerV0; import io.seata.serializer.seata.protocol.v1.SeataSerializerV1; +import java.util.Map; + /** * The Seata codec. * @@ -39,8 +35,13 @@ public class SeataSerializer implements Serializer { Serializer versionSeataSerializer; + static Map VERSION_MAP = ImmutableMap.builder() + .put(ProtocolConstants.VERSION_0, new SeataSerializerV0()) + .put(ProtocolConstants.VERSION_1, new SeataSerializerV1()) + .build(); + public SeataSerializer(Byte version){ - versionSeataSerializer = new SeataSerializerV1(); + versionSeataSerializer = VERSION_MAP.get(version); } @Override public byte[] serialize(T t) { diff --git a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/SeataSerializerV0.java b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/SeataSerializerV0.java index a917b5ff263..184cf256389 100644 --- a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/SeataSerializerV0.java +++ b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/SeataSerializerV0.java @@ -35,7 +35,7 @@ public class SeataSerializerV0 implements Serializer { MessageCodecFactory factory; - public SeataSerializerV0(Byte version){ + public SeataSerializerV0(){ factory = new MessageCodecFactoryV0(); } @Override From 958ef77f23802d9e4fe1fd694eabecb3fe20316a Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Fri, 29 Dec 2023 10:58:44 +0800 Subject: [PATCH 12/62] v0 --- .../main/java/io/seata/serializer/seata/SeataSerializer.java | 2 +- .../seata/serializer/seata/protocol/v0/SeataSerializerV0.java | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java index 5ab4fdf0fda..08bc503df44 100644 --- a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java +++ b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java @@ -35,7 +35,7 @@ public class SeataSerializer implements Serializer { Serializer versionSeataSerializer; - static Map VERSION_MAP = ImmutableMap.builder() + static Map VERSION_MAP = ImmutableMap.builder() .put(ProtocolConstants.VERSION_0, new SeataSerializerV0()) .put(ProtocolConstants.VERSION_1, new SeataSerializerV1()) .build(); diff --git a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/SeataSerializerV0.java b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/SeataSerializerV0.java index 184cf256389..dbadf7e667b 100644 --- a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/SeataSerializerV0.java +++ b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/SeataSerializerV0.java @@ -20,7 +20,6 @@ import io.netty.buffer.Unpooled; import io.seata.common.util.BufferUtils; import io.seata.core.protocol.AbstractMessage; -import io.seata.core.protocol.ProtocolConstants; import io.seata.core.serializer.Serializer; import io.seata.serializer.seata.MessageCodecFactory; import io.seata.serializer.seata.MessageSeataCodec; From 2a0d8365cac87031b002088b3ad59f2ad14ccf3b Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Fri, 29 Dec 2023 10:59:36 +0800 Subject: [PATCH 13/62] v0 --- .../main/java/io/seata/serializer/seata/SeataSerializer.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java index 08bc503df44..de2cd4dc793 100644 --- a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java +++ b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java @@ -42,6 +42,9 @@ public class SeataSerializer implements Serializer { public SeataSerializer(Byte version){ versionSeataSerializer = VERSION_MAP.get(version); + if (versionSeataSerializer == null) { + throw new IllegalArgumentException("version is not supported"); + } } @Override public byte[] serialize(T t) { From 59e9f8cf80741c7a3986592f8422c40231ebd9e0 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Fri, 29 Dec 2023 11:12:10 +0800 Subject: [PATCH 14/62] v0 --- .../io/seata/core/rpc/netty/CompatibleProtocolDecoder.java | 2 -- .../io/seata/core/rpc/netty/CompatibleProtocolEncoder.java | 4 +--- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/core/src/main/java/io/seata/core/rpc/netty/CompatibleProtocolDecoder.java b/core/src/main/java/io/seata/core/rpc/netty/CompatibleProtocolDecoder.java index 11e429cd208..31d7602b9af 100644 --- a/core/src/main/java/io/seata/core/rpc/netty/CompatibleProtocolDecoder.java +++ b/core/src/main/java/io/seata/core/rpc/netty/CompatibleProtocolDecoder.java @@ -51,8 +51,6 @@ *
  • Head Length: include head data from magic code to head map.
  • *
  • Body Length: Full Length - Head Length
  • *

    - * https://github.com/seata/seata/issues/893 - * */ public class CompatibleProtocolDecoder extends LengthFieldBasedFrameDecoder { diff --git a/core/src/main/java/io/seata/core/rpc/netty/CompatibleProtocolEncoder.java b/core/src/main/java/io/seata/core/rpc/netty/CompatibleProtocolEncoder.java index a1a76254b8b..299d962f92b 100644 --- a/core/src/main/java/io/seata/core/rpc/netty/CompatibleProtocolEncoder.java +++ b/core/src/main/java/io/seata/core/rpc/netty/CompatibleProtocolEncoder.java @@ -31,14 +31,12 @@ import java.util.Map; /** - *
    - * 
    + * Compatible Protocol Encoder *

    *

  • Full Length: include all data
  • *
  • Head Length: include head data from magic code to head map.
  • *
  • Body Length: Full Length - Head Length
  • *

    - * https://github.com/seata/seata/issues/893 * */ public class CompatibleProtocolEncoder extends MessageToByteEncoder { From 227464b864bd3be048c4d14f2f21cf58856ae881 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Fri, 29 Dec 2023 11:18:29 +0800 Subject: [PATCH 15/62] v0 --- .../io/seata/core/serializer/SerializerServiceLoader.java | 6 +++--- .../serializer/seata/protocol/v1/SeataSerializerV1.java | 4 ---- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/io/seata/core/serializer/SerializerServiceLoader.java b/core/src/main/java/io/seata/core/serializer/SerializerServiceLoader.java index b5b7340356e..f1540d663aa 100644 --- a/core/src/main/java/io/seata/core/serializer/SerializerServiceLoader.java +++ b/core/src/main/java/io/seata/core/serializer/SerializerServiceLoader.java @@ -31,7 +31,7 @@ public final class SerializerServiceLoader { private SerializerServiceLoader() { } - private static Map serializerMap = new HashMap<>(); + private final static Map SERIALIZER_MAP = new HashMap<>(); private static final String PROTOBUF_SERIALIZER_CLASS_NAME = "io.seata.serializer.protobuf.ProtobufSerializer"; @@ -53,14 +53,14 @@ public static Serializer load(SerializerType type, byte version) throws Enhanced } String key = serialzerKey(type, version); - Serializer serializer = serializerMap.get(key); + Serializer serializer = SERIALIZER_MAP.get(key); if (serializer == null) { if (type == SerializerType.SEATA) { serializer = EnhancedServiceLoader.load(Serializer.class, type.name(), new Object[]{version}); } else { serializer = EnhancedServiceLoader.load(Serializer.class, type.name()); } - serializerMap.put(key, serializer); + SERIALIZER_MAP.put(key, serializer); } return serializer; } diff --git a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v1/SeataSerializerV1.java b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v1/SeataSerializerV1.java index b637dd81f7f..40fb0d55a30 100644 --- a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v1/SeataSerializerV1.java +++ b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v1/SeataSerializerV1.java @@ -18,12 +18,8 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; -import io.seata.common.exception.NotSupportYetException; -import io.seata.common.loader.LoadLevel; -import io.seata.common.loader.Scope; import io.seata.common.util.BufferUtils; import io.seata.core.protocol.AbstractMessage; -import io.seata.core.protocol.ProtocolConstants; import io.seata.core.serializer.Serializer; import io.seata.serializer.seata.MessageCodecFactory; import io.seata.serializer.seata.MessageSeataCodec; From c27c63b7c23ee8849d185d9a66136b359e563bcb Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Fri, 29 Dec 2023 12:29:29 +0800 Subject: [PATCH 16/62] license --- .../core/rpc/netty/v1/ProtocolEncoderV1.java | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolEncoderV1.java b/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolEncoderV1.java index 91030c82b4f..f23bab30f3c 100644 --- a/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolEncoderV1.java +++ b/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolEncoderV1.java @@ -1,17 +1,18 @@ /* - * Copyright 1999-2019 Seata.io Group. + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package io.seata.core.rpc.netty.v1; From 0afa8458b1c228f915dc62ccef582c10daa19ab3 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Fri, 29 Dec 2023 12:30:47 +0800 Subject: [PATCH 17/62] license --- .../serializer/seata/protocol/v0/MessageCodecFactoryV0.java | 1 + 1 file changed, 1 insertion(+) diff --git a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/MessageCodecFactoryV0.java b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/MessageCodecFactoryV0.java index fca6998976f..f4ec067ae0a 100644 --- a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/MessageCodecFactoryV0.java +++ b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/MessageCodecFactoryV0.java @@ -41,6 +41,7 @@ public class MessageCodecFactoryV0 extends MessageCodecFactory { * @param typeCode the type code * @return the msg instance by code */ + @Override public MessageSeataCodec getMessageCodec(short typeCode) { MessageSeataCodec msgCodec = null; switch (typeCode) { From 4959cbe7c82b142a270354dc5f01c902680368dd Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Fri, 29 Dec 2023 14:27:42 +0800 Subject: [PATCH 18/62] license --- .../src/main/java/io/seata/serializer/seata/SeataSerializer.java | 1 - 1 file changed, 1 deletion(-) diff --git a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java index de2cd4dc793..afb2484ae4a 100644 --- a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java +++ b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java @@ -28,7 +28,6 @@ /** * The Seata codec. - * */ @LoadLevel(name = "SEATA", scope = Scope.PROTOTYPE) public class SeataSerializer implements Serializer { From dd7c3142e5cf3dd6c7c5720cc17a8bafffae4e5f Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Fri, 29 Dec 2023 16:12:41 +0800 Subject: [PATCH 19/62] style --- .../io/seata/core/rpc/netty/CompatibleProtocolDecoder.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/io/seata/core/rpc/netty/CompatibleProtocolDecoder.java b/core/src/main/java/io/seata/core/rpc/netty/CompatibleProtocolDecoder.java index 31d7602b9af..cd0dd17f84d 100644 --- a/core/src/main/java/io/seata/core/rpc/netty/CompatibleProtocolDecoder.java +++ b/core/src/main/java/io/seata/core/rpc/netty/CompatibleProtocolDecoder.java @@ -100,7 +100,7 @@ protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception } return decoder.decodeFrame(frame); } finally { - if (!isV0(version)) { + if (version != ProtocolConstants.VERSION_0) { frame.release(); } } @@ -136,10 +136,10 @@ protected boolean isV0(ByteBuf in) { in.markReaderIndex(); byte b0 = in.readByte(); byte b1 = in.readByte(); - byte version = in.readByte(); + byte b2 = in.readByte(); if (ProtocolConstants.MAGIC_CODE_BYTES[0] == b0 && ProtocolConstants.MAGIC_CODE_BYTES[1] == b1 - && isV0(version)) { + && 0 == b2) { isV0 = true; } From b7bdb6da8968a93d249ba830a998017f928d0a33 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Fri, 29 Dec 2023 16:19:02 +0800 Subject: [PATCH 20/62] style --- .../java/io/seata/core/rpc/netty/CompatibleProtocolDecoder.java | 2 ++ .../main/java/io/seata/core/rpc/netty/v1/ProtocolDecoderV1.java | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/io/seata/core/rpc/netty/CompatibleProtocolDecoder.java b/core/src/main/java/io/seata/core/rpc/netty/CompatibleProtocolDecoder.java index cd0dd17f84d..ee699da23b5 100644 --- a/core/src/main/java/io/seata/core/rpc/netty/CompatibleProtocolDecoder.java +++ b/core/src/main/java/io/seata/core/rpc/netty/CompatibleProtocolDecoder.java @@ -136,6 +136,8 @@ protected boolean isV0(ByteBuf in) { in.markReaderIndex(); byte b0 = in.readByte(); byte b1 = in.readByte(); + // v1/v2/v3 : b2 = version + // v0 : 1st byte in FLAG(2byte:0x10/0x20/0x40/0x80) byte b2 = in.readByte(); if (ProtocolConstants.MAGIC_CODE_BYTES[0] == b0 && ProtocolConstants.MAGIC_CODE_BYTES[1] == b1 diff --git a/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolDecoderV1.java b/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolDecoderV1.java index d6a4de70d61..0ae92149373 100644 --- a/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolDecoderV1.java +++ b/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolDecoderV1.java @@ -54,7 +54,7 @@ *

    * https://github.com/seata/seata/issues/893 * - * @see ProtocolV1Encoder + * @see ProtocolEncoderV1 * @author Geng Zhang * @see ProtocolEncoderV1 * @since 0.7.0 From dfd9bf22a2257d56f193491fd237df02fef814e9 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Fri, 29 Dec 2023 17:20:41 +0800 Subject: [PATCH 21/62] style --- .../serializer/seata/MessageCodecFactory.java | 29 ++++--- .../serializer/seata/SeataSerializer.java | 16 ++-- .../protocol/BatchResultMessageCodec.java | 12 +-- .../protocol/MergeResultMessageCodec.java | 12 +-- .../protocol/MergedWarpMessageCodec.java | 12 +-- .../protocol/v0/MessageCodecFactoryV0.java | 84 ------------------- .../seata/protocol/v0/SeataSerializerV0.java | 31 +++++-- .../protocol/v1/MessageCodecFactoryV1.java | 28 ------- .../seata/protocol/v1/SeataSerializerV1.java | 24 ++++-- 9 files changed, 79 insertions(+), 169 deletions(-) delete mode 100644 serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/MessageCodecFactoryV0.java delete mode 100644 serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v1/MessageCodecFactoryV1.java diff --git a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/MessageCodecFactory.java b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/MessageCodecFactory.java index 8b6a9a21f84..4431ba828c0 100644 --- a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/MessageCodecFactory.java +++ b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/MessageCodecFactory.java @@ -80,9 +80,8 @@ /** * The type Message codec factory. - * */ -public abstract class MessageCodecFactory { +public class MessageCodecFactory { /** * The constant UTF8. @@ -95,8 +94,8 @@ public abstract class MessageCodecFactory { * @param abstractMessage the abstract message * @return the message codec */ - public MessageSeataCodec getMessageCodec(AbstractMessage abstractMessage) { - return getMessageCodec(abstractMessage.getTypeCode()); + public static MessageSeataCodec getMessageCodec(AbstractMessage abstractMessage, byte version) { + return getMessageCodec(abstractMessage.getTypeCode(), version); } /** @@ -105,14 +104,14 @@ public MessageSeataCodec getMessageCodec(AbstractMessage abstractMessage) { * @param typeCode the type code * @return the msg instance by code */ - public MessageSeataCodec getMessageCodec(short typeCode) { + public static MessageSeataCodec getMessageCodec(short typeCode, byte version) { MessageSeataCodec msgCodec = null; switch (typeCode) { case MessageType.TYPE_SEATA_MERGE: - msgCodec = new MergedWarpMessageCodec(); + msgCodec = new MergedWarpMessageCodec(version); break; case MessageType.TYPE_SEATA_MERGE_RESULT: - msgCodec = new MergeResultMessageCodec(); + msgCodec = new MergeResultMessageCodec(version); break; case MessageType.TYPE_REG_CLT: msgCodec = new RegisterTMRequestCodec(); @@ -136,7 +135,7 @@ public MessageSeataCodec getMessageCodec(short typeCode) { msgCodec = new GlobalReportRequestCodec(); break; case MessageType.TYPE_BATCH_RESULT_MSG: - msgCodec = new BatchResultMessageCodec(); + msgCodec = new BatchResultMessageCodec(version); break; default: break; @@ -147,7 +146,7 @@ public MessageSeataCodec getMessageCodec(short typeCode) { } try { - msgCodec = getMergeRequestMessageSeataCodec(typeCode); + msgCodec = getMergeRequestMessageSeataCodec(typeCode, version); } catch (Exception exx) { } @@ -155,7 +154,7 @@ public MessageSeataCodec getMessageCodec(short typeCode) { return msgCodec; } - msgCodec = getMergeResponseMessageSeataCodec(typeCode); + msgCodec = getMergeResponseMessageSeataCodec(typeCode, version); return msgCodec; } @@ -166,7 +165,7 @@ public MessageSeataCodec getMessageCodec(short typeCode) { * @param typeCode the type code * @return the merge request instance by code */ - protected MessageSeataCodec getMergeRequestMessageSeataCodec(int typeCode) { + protected static MessageSeataCodec getMergeRequestMessageSeataCodec(int typeCode, byte version) { switch (typeCode) { case MessageType.TYPE_GLOBAL_BEGIN: return new GlobalBeginRequestCodec(); @@ -195,7 +194,7 @@ protected MessageSeataCodec getMergeRequestMessageSeataCodec(int typeCode) { * @param typeCode the type code * @return the merge response instance by code */ - protected MessageSeataCodec getMergeResponseMessageSeataCodec(int typeCode) { + protected static MessageSeataCodec getMergeResponseMessageSeataCodec(int typeCode, byte version) { switch (typeCode) { case MessageType.TYPE_GLOBAL_BEGIN_RESULT: return new GlobalBeginResponseCodec(); @@ -230,7 +229,7 @@ protected MessageSeataCodec getMergeResponseMessageSeataCodec(int typeCode) { * @param typeCode the type code * @return the message */ - public AbstractMessage getMessage(short typeCode) { + public static AbstractMessage getMessage(short typeCode) { AbstractMessage abstractMessage = null; switch (typeCode) { case MessageType.TYPE_SEATA_MERGE: @@ -295,7 +294,7 @@ public AbstractMessage getMessage(short typeCode) { * @param typeCode the type code * @return the merge request instance by code */ - protected AbstractMessage getMergeRequestInstanceByCode(int typeCode) { + protected static AbstractMessage getMergeRequestInstanceByCode(int typeCode) { switch (typeCode) { case MessageType.TYPE_GLOBAL_BEGIN: return new GlobalBeginRequest(); @@ -324,7 +323,7 @@ protected AbstractMessage getMergeRequestInstanceByCode(int typeCode) { * @param typeCode the type code * @return the merge response instance by code */ - protected AbstractMessage getMergeResponseInstanceByCode(int typeCode) { + protected static AbstractMessage getMergeResponseInstanceByCode(int typeCode) { switch (typeCode) { case MessageType.TYPE_GLOBAL_BEGIN_RESULT: return new GlobalBeginResponse(); diff --git a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java index afb2484ae4a..2122a0cd2ba 100644 --- a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java +++ b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java @@ -16,7 +16,6 @@ */ package io.seata.serializer.seata; -import com.google.common.collect.ImmutableMap; import io.seata.common.loader.LoadLevel; import io.seata.common.loader.Scope; import io.seata.core.protocol.ProtocolConstants; @@ -24,8 +23,6 @@ import io.seata.serializer.seata.protocol.v0.SeataSerializerV0; import io.seata.serializer.seata.protocol.v1.SeataSerializerV1; -import java.util.Map; - /** * The Seata codec. */ @@ -34,17 +31,18 @@ public class SeataSerializer implements Serializer { Serializer versionSeataSerializer; - static Map VERSION_MAP = ImmutableMap.builder() - .put(ProtocolConstants.VERSION_0, new SeataSerializerV0()) - .put(ProtocolConstants.VERSION_1, new SeataSerializerV1()) - .build(); - public SeataSerializer(Byte version){ - versionSeataSerializer = VERSION_MAP.get(version); + public SeataSerializer(Byte version) { + if (version == ProtocolConstants.VERSION_0) { + versionSeataSerializer = SeataSerializerV0.getInstance(); + } else if (version == ProtocolConstants.VERSION_1) { + versionSeataSerializer = SeataSerializerV1.getInstance(); + } if (versionSeataSerializer == null) { throw new IllegalArgumentException("version is not supported"); } } + @Override public byte[] serialize(T t) { return versionSeataSerializer.serialize(t); diff --git a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/BatchResultMessageCodec.java b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/BatchResultMessageCodec.java index a81ad1a874e..7c1405ceb70 100644 --- a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/BatchResultMessageCodec.java +++ b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/BatchResultMessageCodec.java @@ -27,7 +27,6 @@ import io.seata.core.protocol.BatchResultMessage; import io.seata.serializer.seata.MessageCodecFactory; import io.seata.serializer.seata.MessageSeataCodec; -import io.seata.serializer.seata.protocol.v1.MessageCodecFactoryV1; /** * the type batch result message codec @@ -36,8 +35,11 @@ */ public class BatchResultMessageCodec extends AbstractMessageCodec { - protected MessageCodecFactory factory = new MessageCodecFactoryV1(); + private byte version; + public BatchResultMessageCodec(byte version) { + this.version = version; + } @Override public Class getMessageClassType() { return BatchResultMessage.class; @@ -56,7 +58,7 @@ public void encode(T t, ByteBuf out) { for (final AbstractMessage msg : msgs) { final ByteBuf subBuffer = Unpooled.buffer(1024); short typeCode = msg.getTypeCode(); - MessageSeataCodec messageCodec = factory.getMessageCodec(typeCode); + MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typeCode, version); messageCodec.encode(msg, subBuffer); buffer.writeShort(msg.getTypeCode()); buffer.writeBytes(subBuffer); @@ -109,8 +111,8 @@ protected void decode(BatchResultMessage batchResultMessage, ByteBuffer byteBuff List msgIds = new ArrayList<>(); for (int idx = 0; idx < msgNum; idx++) { short typeCode = byteBuffer.getShort(); - AbstractMessage abstractResultMessage = factory.getMessage(typeCode); - MessageSeataCodec messageCodec = factory.getMessageCodec(typeCode); + AbstractMessage abstractResultMessage = MessageCodecFactory.getMessage(typeCode); + MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typeCode, version); messageCodec.decode(abstractResultMessage, byteBuffer); msgs.add((AbstractResultMessage) abstractResultMessage); } diff --git a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/MergeResultMessageCodec.java b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/MergeResultMessageCodec.java index 100ae839963..c7cc6714be2 100644 --- a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/MergeResultMessageCodec.java +++ b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/MergeResultMessageCodec.java @@ -24,7 +24,6 @@ import io.seata.core.protocol.AbstractMessage; import io.seata.core.protocol.AbstractResultMessage; import io.seata.core.protocol.MergeResultMessage; -import io.seata.serializer.seata.protocol.v1.MessageCodecFactoryV1; /** * The type Merge result message codec. @@ -32,8 +31,11 @@ */ public class MergeResultMessageCodec extends AbstractMessageCodec { - protected MessageCodecFactory factory = new MessageCodecFactoryV1(); + private byte version; + public MergeResultMessageCodec(byte version) { + this.version = version; + } @Override public Class getMessageClassType() { return MergeResultMessage.class; @@ -51,7 +53,7 @@ public void encode(T t, ByteBuf out) { short typeCode = msg.getTypeCode(); //put typeCode out.writeShort(typeCode); - MessageSeataCodec messageCodec = factory.getMessageCodec(typeCode); + MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typeCode, version); messageCodec.encode(msg, out); } @@ -93,8 +95,8 @@ protected void decode(MergeResultMessage mergeResultMessage, ByteBuffer byteBuff AbstractResultMessage[] msgs = new AbstractResultMessage[msgNum]; for (int idx = 0; idx < msgNum; idx++) { short typeCode = byteBuffer.getShort(); - AbstractMessage abstractResultMessage = factory.getMessage(typeCode); - MessageSeataCodec messageCodec = factory.getMessageCodec(typeCode); + AbstractMessage abstractResultMessage = MessageCodecFactory.getMessage(typeCode); + MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typeCode, version); messageCodec.decode(abstractResultMessage, byteBuffer); msgs[idx] = (AbstractResultMessage)abstractResultMessage; } diff --git a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/MergedWarpMessageCodec.java b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/MergedWarpMessageCodec.java index 436a82b7d26..be519f90ecc 100644 --- a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/MergedWarpMessageCodec.java +++ b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/MergedWarpMessageCodec.java @@ -26,7 +26,6 @@ import io.seata.serializer.seata.MessageSeataCodec; import io.seata.core.protocol.AbstractMessage; import io.seata.core.protocol.MergedWarpMessage; -import io.seata.serializer.seata.protocol.v1.MessageCodecFactoryV1; /** * The type Merged warp message codec. @@ -34,8 +33,11 @@ */ public class MergedWarpMessageCodec extends AbstractMessageCodec { - protected MessageCodecFactory factory = new MessageCodecFactoryV1(); + private byte version; + public MergedWarpMessageCodec(byte version) { + this.version = version; + } @Override public Class getMessageClassType() { return MergedWarpMessage.class; @@ -54,7 +56,7 @@ public void encode(T t, ByteBuf out) { for (final AbstractMessage msg : msgs) { final ByteBuf subBuffer = Unpooled.buffer(1024); short typeCode = msg.getTypeCode(); - MessageSeataCodec messageCodec = factory.getMessageCodec(typeCode); + MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typeCode, version); messageCodec.encode(msg, subBuffer); buffer.writeShort(msg.getTypeCode()); buffer.writeBytes(subBuffer); @@ -99,8 +101,8 @@ private void doDecode(MergedWarpMessage mergedWarpMessage, ByteBuffer byteBuffer List msgs = new ArrayList(); for (int idx = 0; idx < msgNum; idx++) { short typeCode = byteBuffer.getShort(); - AbstractMessage abstractMessage = factory.getMessage(typeCode); - MessageSeataCodec messageCodec = factory.getMessageCodec(typeCode); + AbstractMessage abstractMessage = MessageCodecFactory.getMessage(typeCode); + MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typeCode, version); messageCodec.decode(abstractMessage, byteBuffer); msgs.add(abstractMessage); } diff --git a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/MessageCodecFactoryV0.java b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/MessageCodecFactoryV0.java deleted file mode 100644 index f4ec067ae0a..00000000000 --- a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/MessageCodecFactoryV0.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.seata.serializer.seata.protocol.v0; - -import io.seata.core.protocol.MessageType; -import io.seata.serializer.seata.MessageCodecFactory; -import io.seata.serializer.seata.MessageSeataCodec; -import io.seata.serializer.seata.protocol.BatchResultMessageCodec; -import io.seata.serializer.seata.protocol.MergeResultMessageCodec; -import io.seata.serializer.seata.protocol.MergedWarpMessageCodec; -import io.seata.serializer.seata.protocol.RegisterRMRequestCodec; -import io.seata.serializer.seata.protocol.RegisterRMResponseCodec; -import io.seata.serializer.seata.protocol.RegisterTMRequestCodec; -import io.seata.serializer.seata.protocol.RegisterTMResponseCodec; -import io.seata.serializer.seata.protocol.transaction.BranchCommitRequestCodec; -import io.seata.serializer.seata.protocol.transaction.BranchRollbackRequestCodec; -import io.seata.serializer.seata.protocol.transaction.GlobalReportRequestCodec; - -/** - * The type Message codec factory v0. - */ -public class MessageCodecFactoryV0 extends MessageCodecFactory { - - /** - * Gets msg instance by code. - * - * @param typeCode the type code - * @return the msg instance by code - */ - @Override - public MessageSeataCodec getMessageCodec(short typeCode) { - MessageSeataCodec msgCodec = null; - switch (typeCode) { - case MessageType.TYPE_SEATA_MERGE: - msgCodec = new MergedWarpMessageCodec(); - break; - case MessageType.TYPE_SEATA_MERGE_RESULT: - msgCodec = new MergeResultMessageCodec(); - break; - case MessageType.TYPE_REG_CLT: - msgCodec = new RegisterTMRequestCodec(); - break; - case MessageType.TYPE_REG_CLT_RESULT: - msgCodec = new RegisterTMResponseCodec(); - break; - case MessageType.TYPE_REG_RM: - msgCodec = new RegisterRMRequestCodec(); - break; - case MessageType.TYPE_REG_RM_RESULT: - msgCodec = new RegisterRMResponseCodec(); - break; - case MessageType.TYPE_BRANCH_COMMIT: - msgCodec = new BranchCommitRequestCodec(); - break; - case MessageType.TYPE_BRANCH_ROLLBACK: - msgCodec = new BranchRollbackRequestCodec(); - break; - case MessageType.TYPE_GLOBAL_REPORT: - msgCodec = new GlobalReportRequestCodec(); - break; - case MessageType.TYPE_BATCH_RESULT_MSG: - msgCodec = new BatchResultMessageCodec(); - break; - default: - break; - } - - return msgCodec; - } -} diff --git a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/SeataSerializerV0.java b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/SeataSerializerV0.java index dbadf7e667b..dd2305ba461 100644 --- a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/SeataSerializerV0.java +++ b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/SeataSerializerV0.java @@ -20,6 +20,7 @@ import io.netty.buffer.Unpooled; import io.seata.common.util.BufferUtils; import io.seata.core.protocol.AbstractMessage; +import io.seata.core.protocol.ProtocolConstants; import io.seata.core.serializer.Serializer; import io.seata.serializer.seata.MessageCodecFactory; import io.seata.serializer.seata.MessageSeataCodec; @@ -28,25 +29,37 @@ /** * The Seata codec v0. - * */ public class SeataSerializerV0 implements Serializer { - MessageCodecFactory factory; - public SeataSerializerV0(){ - factory = new MessageCodecFactoryV0(); + private SeataSerializerV0() { } + + static Serializer instance; + + public static Serializer getInstance() { + if (instance == null) { + synchronized (SeataSerializerV0.class) { + if (instance == null) { + instance = new SeataSerializerV0(); + } + } + } + return instance; + } + + @Override public byte[] serialize(T t) { if (!(t instanceof AbstractMessage)) { throw new IllegalArgumentException("AbstractMessage isn't available."); } - AbstractMessage abstractMessage = (AbstractMessage)t; + AbstractMessage abstractMessage = (AbstractMessage) t; //type code short typecode = abstractMessage.getTypeCode(); //msg codec - MessageSeataCodec messageCodec = factory.getMessageCodec(typecode); + MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typecode, ProtocolConstants.VERSION_0); //get empty ByteBuffer ByteBuf out = Unpooled.buffer(1024); //msg encode @@ -81,12 +94,12 @@ public T deserialize(byte[] bytes) { byteBuffer.get(body); ByteBuffer in = ByteBuffer.wrap(body); //new Messgae - AbstractMessage abstractMessage = factory.getMessage(typecode); + AbstractMessage abstractMessage = MessageCodecFactory.getMessage(typecode); //get messageCodec - MessageSeataCodec messageCodec = factory.getMessageCodec(typecode); + MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typecode, ProtocolConstants.VERSION_0); //decode messageCodec.decode(abstractMessage, in); - return (T)abstractMessage; + return (T) abstractMessage; } } diff --git a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v1/MessageCodecFactoryV1.java b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v1/MessageCodecFactoryV1.java deleted file mode 100644 index 7843336bfe9..00000000000 --- a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v1/MessageCodecFactoryV1.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.seata.serializer.seata.protocol.v1; - -import io.seata.serializer.seata.MessageCodecFactory; - -/** - * The type Message codec factory. - */ -public class MessageCodecFactoryV1 extends MessageCodecFactory { - - - -} diff --git a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v1/SeataSerializerV1.java b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v1/SeataSerializerV1.java index 40fb0d55a30..5fec4661d0f 100644 --- a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v1/SeataSerializerV1.java +++ b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v1/SeataSerializerV1.java @@ -20,33 +20,39 @@ import io.netty.buffer.Unpooled; import io.seata.common.util.BufferUtils; import io.seata.core.protocol.AbstractMessage; +import io.seata.core.protocol.ProtocolConstants; import io.seata.core.serializer.Serializer; import io.seata.serializer.seata.MessageCodecFactory; import io.seata.serializer.seata.MessageSeataCodec; +import io.seata.serializer.seata.protocol.v0.SeataSerializerV0; import java.nio.ByteBuffer; /** * The Seata codec v1. - * */ public class SeataSerializerV1 implements Serializer { - MessageCodecFactory factory; - public SeataSerializerV1(){ - factory = new MessageCodecFactoryV1(); + static Serializer instance = new SeataSerializerV1(); + + public static Serializer getInstance() { + return instance; } + + private SeataSerializerV1() { + } + @Override public byte[] serialize(T t) { if (!(t instanceof AbstractMessage)) { throw new IllegalArgumentException("AbstractMessage isn't available."); } - AbstractMessage abstractMessage = (AbstractMessage)t; + AbstractMessage abstractMessage = (AbstractMessage) t; //type code short typecode = abstractMessage.getTypeCode(); //msg codec - MessageSeataCodec messageCodec = factory.getMessageCodec(typecode); + MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typecode, ProtocolConstants.VERSION_1); //get empty ByteBuffer ByteBuf out = Unpooled.buffer(1024); //msg encode @@ -83,12 +89,12 @@ public T deserialize(byte[] bytes) { byteBuffer.get(body); ByteBuffer in = ByteBuffer.wrap(body); //new Messgae - AbstractMessage abstractMessage = factory.getMessage(typecode); + AbstractMessage abstractMessage = MessageCodecFactory.getMessage(typecode); //get messageCodec - MessageSeataCodec messageCodec = factory.getMessageCodec(typecode); + MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typecode, ProtocolConstants.VERSION_1); //decode messageCodec.decode(abstractMessage, in); - return (T)abstractMessage; + return (T) abstractMessage; } } From 48a6090d35a9f30a7c04c2cb174d6c5d707aba59 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Tue, 2 Jan 2024 11:10:08 +0800 Subject: [PATCH 22/62] style --- .../io/seata/serializer/seata/protocol/v1/SeataSerializerV1.java | 1 - 1 file changed, 1 deletion(-) diff --git a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v1/SeataSerializerV1.java b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v1/SeataSerializerV1.java index 5fec4661d0f..acbdd74488f 100644 --- a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v1/SeataSerializerV1.java +++ b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v1/SeataSerializerV1.java @@ -24,7 +24,6 @@ import io.seata.core.serializer.Serializer; import io.seata.serializer.seata.MessageCodecFactory; import io.seata.serializer.seata.MessageSeataCodec; -import io.seata.serializer.seata.protocol.v0.SeataSerializerV0; import java.nio.ByteBuffer; From 7d1a6b3c85dfc3d40ee8cdb146a7a15a7821cca3 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Tue, 2 Jan 2024 11:20:29 +0800 Subject: [PATCH 23/62] test --- .../seata/core/rpc/netty/v1/ServerChannelHandler.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/test/src/test/java/io/seata/core/rpc/netty/v1/ServerChannelHandler.java b/test/src/test/java/io/seata/core/rpc/netty/v1/ServerChannelHandler.java index 81d9464a392..6e1f9e3861e 100644 --- a/test/src/test/java/io/seata/core/rpc/netty/v1/ServerChannelHandler.java +++ b/test/src/test/java/io/seata/core/rpc/netty/v1/ServerChannelHandler.java @@ -22,6 +22,7 @@ import io.netty.channel.ChannelInboundHandlerAdapter; import io.seata.core.protocol.ProtocolConstants; import io.seata.core.protocol.RpcMessage; +import io.seata.core.rpc.netty.ProtocolRpcMessage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -39,11 +40,15 @@ public class ServerChannelHandler extends ChannelInboundHandlerAdapter { public void channelRead(ChannelHandlerContext ctx, Object msg) { Channel channel = ctx.channel(); - if (msg instanceof RpcMessage) { - ((RpcMessage) msg).setMessageType(ProtocolConstants.MSGTYPE_RESPONSE); + RpcMessage rpcMessage = null; + if (msg instanceof ProtocolRpcMessage) { + rpcMessage = ((ProtocolRpcMessage) msg).protocolMsg2RpcMsg(); + } else { + LOGGER.error("rpcMessage type error"); + return; } - channel.writeAndFlush(msg); + channel.writeAndFlush(rpcMessage); } @Override From 33e6e4e31255f2f8f1629557e02f9224ddb325af Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Tue, 2 Jan 2024 11:20:49 +0800 Subject: [PATCH 24/62] test --- .../java/io/seata/core/rpc/netty/v1/ServerChannelHandler.java | 1 - 1 file changed, 1 deletion(-) diff --git a/test/src/test/java/io/seata/core/rpc/netty/v1/ServerChannelHandler.java b/test/src/test/java/io/seata/core/rpc/netty/v1/ServerChannelHandler.java index 6e1f9e3861e..692c700858c 100644 --- a/test/src/test/java/io/seata/core/rpc/netty/v1/ServerChannelHandler.java +++ b/test/src/test/java/io/seata/core/rpc/netty/v1/ServerChannelHandler.java @@ -20,7 +20,6 @@ import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; -import io.seata.core.protocol.ProtocolConstants; import io.seata.core.protocol.RpcMessage; import io.seata.core.rpc.netty.ProtocolRpcMessage; import org.slf4j.Logger; From 379ba281056a9cacfddbf0bcb6ea3191af1fd88e Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Tue, 2 Jan 2024 14:48:57 +0800 Subject: [PATCH 25/62] test --- .../java/io/seata/core/rpc/netty/v1/ServerChannelHandler.java | 1 - 1 file changed, 1 deletion(-) diff --git a/test/src/test/java/io/seata/core/rpc/netty/v1/ServerChannelHandler.java b/test/src/test/java/io/seata/core/rpc/netty/v1/ServerChannelHandler.java index 692c700858c..4f2ac538e69 100644 --- a/test/src/test/java/io/seata/core/rpc/netty/v1/ServerChannelHandler.java +++ b/test/src/test/java/io/seata/core/rpc/netty/v1/ServerChannelHandler.java @@ -46,7 +46,6 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) { LOGGER.error("rpcMessage type error"); return; } - channel.writeAndFlush(rpcMessage); } From a9b4d5debad0649d40468416f2ab299dd32ab3f7 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Wed, 3 Jan 2024 15:05:50 +0800 Subject: [PATCH 26/62] test --- .../io/seata/core/rpc/netty/v1/ServerChannelHandler.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/src/test/java/io/seata/core/rpc/netty/v1/ServerChannelHandler.java b/test/src/test/java/io/seata/core/rpc/netty/v1/ServerChannelHandler.java index 4f2ac538e69..173a6c68dda 100644 --- a/test/src/test/java/io/seata/core/rpc/netty/v1/ServerChannelHandler.java +++ b/test/src/test/java/io/seata/core/rpc/netty/v1/ServerChannelHandler.java @@ -39,14 +39,12 @@ public class ServerChannelHandler extends ChannelInboundHandlerAdapter { public void channelRead(ChannelHandlerContext ctx, Object msg) { Channel channel = ctx.channel(); - RpcMessage rpcMessage = null; if (msg instanceof ProtocolRpcMessage) { - rpcMessage = ((ProtocolRpcMessage) msg).protocolMsg2RpcMsg(); + RpcMessage rpcMessage = ((ProtocolRpcMessage) msg).protocolMsg2RpcMsg(); + channel.writeAndFlush(rpcMessage); } else { LOGGER.error("rpcMessage type error"); - return; } - channel.writeAndFlush(rpcMessage); } @Override From 95573d35c2cc5e8bbc90652a2ed8b7a527e30d22 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Thu, 4 Jan 2024 14:21:33 +0800 Subject: [PATCH 27/62] fix test --- .../io/seata/core/rpc/netty/v1/ClientChannelHandler.java | 8 ++++++-- .../seata/core/rpc/netty/v1/ProtocolV1SerializerTest.java | 3 ++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/test/src/test/java/io/seata/core/rpc/netty/v1/ClientChannelHandler.java b/test/src/test/java/io/seata/core/rpc/netty/v1/ClientChannelHandler.java index 0a6a59669f4..51c2254d87a 100644 --- a/test/src/test/java/io/seata/core/rpc/netty/v1/ClientChannelHandler.java +++ b/test/src/test/java/io/seata/core/rpc/netty/v1/ClientChannelHandler.java @@ -20,6 +20,7 @@ import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.util.concurrent.DefaultPromise; import io.seata.core.protocol.RpcMessage; +import io.seata.core.rpc.netty.ProtocolRpcMessage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -49,8 +50,8 @@ public void channelInactive(final ChannelHandlerContext ctx) throws Exception { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - if (msg instanceof RpcMessage) { - RpcMessage rpcMessage = (RpcMessage) msg; + if (msg instanceof ProtocolRpcMessage) { + RpcMessage rpcMessage = ((ProtocolRpcMessage) msg).protocolMsg2RpcMsg(); int msgId = rpcMessage.getId(); DefaultPromise future = (DefaultPromise) client.futureMap.remove(msgId); if (future != null) { @@ -58,7 +59,10 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception } else { LOGGER.warn("miss msg id:{}", msgId); } + }else { + LOGGER.warn("msg is not ProtocolRpcMessage"); } + } @Override diff --git a/test/src/test/java/io/seata/core/rpc/netty/v1/ProtocolV1SerializerTest.java b/test/src/test/java/io/seata/core/rpc/netty/v1/ProtocolV1SerializerTest.java index 1c8a4add3fe..8310219c85a 100644 --- a/test/src/test/java/io/seata/core/rpc/netty/v1/ProtocolV1SerializerTest.java +++ b/test/src/test/java/io/seata/core/rpc/netty/v1/ProtocolV1SerializerTest.java @@ -20,6 +20,7 @@ import io.seata.core.model.BranchType; import io.seata.core.protocol.RpcMessage; import io.seata.core.protocol.transaction.BranchCommitRequest; +import io.seata.core.rpc.netty.ProtocolRpcMessage; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.slf4j.Logger; @@ -80,7 +81,7 @@ public void testAll() { while (tag.getAndIncrement() < runTimes) { try { Future future = client.sendRpc(head, body); - RpcMessage resp = (RpcMessage) future.get(10, TimeUnit.SECONDS); + ProtocolRpcMessage resp = (ProtocolRpcMessage) future.get(10, TimeUnit.SECONDS); if (resp != null) { success.incrementAndGet(); } From 181a421ef32ababcb3fca0546d726323f7faf7cd Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Thu, 4 Jan 2024 14:51:27 +0800 Subject: [PATCH 28/62] inner class --- .../serializer/seata/SeataSerializer.java | 121 +++++++++++++++++- .../seata/protocol/v0/SeataSerializerV0.java | 105 --------------- .../seata/protocol/v1/SeataSerializerV1.java | 99 -------------- 3 files changed, 116 insertions(+), 209 deletions(-) delete mode 100644 serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/SeataSerializerV0.java delete mode 100644 serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v1/SeataSerializerV1.java diff --git a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java index 2122a0cd2ba..a64bbcf0e9f 100644 --- a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java +++ b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java @@ -16,12 +16,16 @@ */ package io.seata.serializer.seata; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; import io.seata.common.loader.LoadLevel; import io.seata.common.loader.Scope; +import io.seata.common.util.BufferUtils; +import io.seata.core.protocol.AbstractMessage; import io.seata.core.protocol.ProtocolConstants; import io.seata.core.serializer.Serializer; -import io.seata.serializer.seata.protocol.v0.SeataSerializerV0; -import io.seata.serializer.seata.protocol.v1.SeataSerializerV1; + +import java.nio.ByteBuffer; /** * The Seata codec. @@ -31,15 +35,17 @@ public class SeataSerializer implements Serializer { Serializer versionSeataSerializer; + Serializer seataSerializerV0 = new SeataSerializerV0(); + Serializer seataSerializerV1 = new SeataSerializerV1(); public SeataSerializer(Byte version) { if (version == ProtocolConstants.VERSION_0) { - versionSeataSerializer = SeataSerializerV0.getInstance(); + versionSeataSerializer = seataSerializerV0; } else if (version == ProtocolConstants.VERSION_1) { - versionSeataSerializer = SeataSerializerV1.getInstance(); + versionSeataSerializer = seataSerializerV1; } if (versionSeataSerializer == null) { - throw new IllegalArgumentException("version is not supported"); + throw new UnsupportedOperationException("version is not supported"); } } @@ -53,4 +59,109 @@ public T deserialize(byte[] bytes) { return versionSeataSerializer.deserialize(bytes); } + + class SeataSerializerV1 implements Serializer { + + private SeataSerializerV1() { + } + + @Override + public byte[] serialize(T t) { + if (!(t instanceof AbstractMessage)) { + throw new IllegalArgumentException("AbstractMessage isn't available."); + } + AbstractMessage abstractMessage = (AbstractMessage) t; + //type code + short typecode = abstractMessage.getTypeCode(); + //msg codec + MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typecode, ProtocolConstants.VERSION_1); + //get empty ByteBuffer + ByteBuf out = Unpooled.buffer(1024); + //msg encode + messageCodec.encode(t, out); + byte[] body = new byte[out.readableBytes()]; + out.readBytes(body); + + ByteBuffer byteBuffer; + + //typecode + body + byteBuffer = ByteBuffer.allocate(2 + body.length); + byteBuffer.putShort(typecode); + byteBuffer.put(body); + + BufferUtils.flip(byteBuffer); + byte[] content = new byte[byteBuffer.limit()]; + byteBuffer.get(content); + return content; + } + + @Override + public T deserialize(byte[] bytes) { + return deserializeByVersion(bytes, ProtocolConstants.VERSION_1); + + } + } + class SeataSerializerV0 implements Serializer { + + + private SeataSerializerV0() { + } + + @Override + public byte[] serialize(T t) { + if (!(t instanceof AbstractMessage)) { + throw new IllegalArgumentException("AbstractMessage isn't available."); + } + AbstractMessage abstractMessage = (AbstractMessage) t; + //type code + short typecode = abstractMessage.getTypeCode(); + //msg codec + MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typecode, ProtocolConstants.VERSION_0); + //get empty ByteBuffer + ByteBuf out = Unpooled.buffer(1024); + //msg encode + messageCodec.encode(t, out); + byte[] body = new byte[out.readableBytes()]; + out.readBytes(body); + + ByteBuffer byteBuffer; + byteBuffer = ByteBuffer.allocate(body.length); + + byteBuffer.put(body); + + BufferUtils.flip(byteBuffer); + byte[] content = new byte[byteBuffer.limit()]; + byteBuffer.get(content); + return content; + } + + @Override + public T deserialize(byte[] bytes) { + return deserializeByVersion(bytes, ProtocolConstants.VERSION_0); + } + + } + + private static T deserializeByVersion(byte[] bytes, byte version) { + if (bytes == null || bytes.length == 0) { + throw new IllegalArgumentException("Nothing to decode."); + } + if (bytes.length < 2) { + throw new IllegalArgumentException("The byte[] isn't available for decode."); + } + ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); + //typecode + short typecode = byteBuffer.getShort(); + //msg body + byte[] body = new byte[byteBuffer.remaining()]; + byteBuffer.get(body); + ByteBuffer in = ByteBuffer.wrap(body); + //new Messgae + AbstractMessage abstractMessage = MessageCodecFactory.getMessage(typecode); + //get messageCodec + MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typecode, version); + //decode + messageCodec.decode(abstractMessage, in); + return (T) abstractMessage; + } } diff --git a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/SeataSerializerV0.java b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/SeataSerializerV0.java deleted file mode 100644 index dd2305ba461..00000000000 --- a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v0/SeataSerializerV0.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.seata.serializer.seata.protocol.v0; - -import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; -import io.seata.common.util.BufferUtils; -import io.seata.core.protocol.AbstractMessage; -import io.seata.core.protocol.ProtocolConstants; -import io.seata.core.serializer.Serializer; -import io.seata.serializer.seata.MessageCodecFactory; -import io.seata.serializer.seata.MessageSeataCodec; - -import java.nio.ByteBuffer; - -/** - * The Seata codec v0. - */ -public class SeataSerializerV0 implements Serializer { - - - private SeataSerializerV0() { - } - - static Serializer instance; - - public static Serializer getInstance() { - if (instance == null) { - synchronized (SeataSerializerV0.class) { - if (instance == null) { - instance = new SeataSerializerV0(); - } - } - } - return instance; - } - - - @Override - public byte[] serialize(T t) { - if (!(t instanceof AbstractMessage)) { - throw new IllegalArgumentException("AbstractMessage isn't available."); - } - AbstractMessage abstractMessage = (AbstractMessage) t; - //type code - short typecode = abstractMessage.getTypeCode(); - //msg codec - MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typecode, ProtocolConstants.VERSION_0); - //get empty ByteBuffer - ByteBuf out = Unpooled.buffer(1024); - //msg encode - messageCodec.encode(t, out); - byte[] body = new byte[out.readableBytes()]; - out.readBytes(body); - - ByteBuffer byteBuffer; - byteBuffer = ByteBuffer.allocate(body.length); - - byteBuffer.put(body); - - BufferUtils.flip(byteBuffer); - byte[] content = new byte[byteBuffer.limit()]; - byteBuffer.get(content); - return content; - } - - @Override - public T deserialize(byte[] bytes) { - if (bytes == null || bytes.length == 0) { - throw new IllegalArgumentException("Nothing to decode."); - } - if (bytes.length < 2) { - throw new IllegalArgumentException("The byte[] isn't available for decode."); - } - ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); - //typecode - short typecode = byteBuffer.getShort(); - //msg body - byte[] body = new byte[byteBuffer.remaining()]; - byteBuffer.get(body); - ByteBuffer in = ByteBuffer.wrap(body); - //new Messgae - AbstractMessage abstractMessage = MessageCodecFactory.getMessage(typecode); - //get messageCodec - MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typecode, ProtocolConstants.VERSION_0); - //decode - messageCodec.decode(abstractMessage, in); - return (T) abstractMessage; - } - -} diff --git a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v1/SeataSerializerV1.java b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v1/SeataSerializerV1.java deleted file mode 100644 index acbdd74488f..00000000000 --- a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/v1/SeataSerializerV1.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.seata.serializer.seata.protocol.v1; - -import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; -import io.seata.common.util.BufferUtils; -import io.seata.core.protocol.AbstractMessage; -import io.seata.core.protocol.ProtocolConstants; -import io.seata.core.serializer.Serializer; -import io.seata.serializer.seata.MessageCodecFactory; -import io.seata.serializer.seata.MessageSeataCodec; - -import java.nio.ByteBuffer; - -/** - * The Seata codec v1. - */ -public class SeataSerializerV1 implements Serializer { - - - static Serializer instance = new SeataSerializerV1(); - - public static Serializer getInstance() { - return instance; - } - - private SeataSerializerV1() { - } - - @Override - public byte[] serialize(T t) { - if (!(t instanceof AbstractMessage)) { - throw new IllegalArgumentException("AbstractMessage isn't available."); - } - AbstractMessage abstractMessage = (AbstractMessage) t; - //type code - short typecode = abstractMessage.getTypeCode(); - //msg codec - MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typecode, ProtocolConstants.VERSION_1); - //get empty ByteBuffer - ByteBuf out = Unpooled.buffer(1024); - //msg encode - messageCodec.encode(t, out); - byte[] body = new byte[out.readableBytes()]; - out.readBytes(body); - - ByteBuffer byteBuffer; - - //typecode + body - byteBuffer = ByteBuffer.allocate(2 + body.length); - byteBuffer.putShort(typecode); - byteBuffer.put(body); - - BufferUtils.flip(byteBuffer); - byte[] content = new byte[byteBuffer.limit()]; - byteBuffer.get(content); - return content; - } - - @Override - public T deserialize(byte[] bytes) { - if (bytes == null || bytes.length == 0) { - throw new IllegalArgumentException("Nothing to decode."); - } - if (bytes.length < 2) { - throw new IllegalArgumentException("The byte[] isn't available for decode."); - } - ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); - //typecode - short typecode = byteBuffer.getShort(); - //msg body - byte[] body = new byte[byteBuffer.remaining()]; - byteBuffer.get(body); - ByteBuffer in = ByteBuffer.wrap(body); - //new Messgae - AbstractMessage abstractMessage = MessageCodecFactory.getMessage(typecode); - //get messageCodec - MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typecode, ProtocolConstants.VERSION_1); - //decode - messageCodec.decode(abstractMessage, in); - return (T) abstractMessage; - } - -} From d2804fb04469b88ace8ecf12e9ccb13274f69329 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Thu, 4 Jan 2024 15:00:34 +0800 Subject: [PATCH 29/62] inner class --- .../main/java/io/seata/serializer/seata/SeataSerializer.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java index a64bbcf0e9f..e8ab45237f3 100644 --- a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java +++ b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java @@ -60,7 +60,7 @@ public T deserialize(byte[] bytes) { } - class SeataSerializerV1 implements Serializer { + static class SeataSerializerV1 implements Serializer { private SeataSerializerV1() { } @@ -101,7 +101,7 @@ public T deserialize(byte[] bytes) { } } - class SeataSerializerV0 implements Serializer { + static class SeataSerializerV0 implements Serializer { private SeataSerializerV0() { From 91c171ddc6b7286c94d111846a2913fce2aa0e0d Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Mon, 22 Jan 2024 17:54:48 +0800 Subject: [PATCH 30/62] style --- .../main/java/io/seata/core/rpc/netty/v1/ProtocolV1Decoder.java | 1 - 1 file changed, 1 deletion(-) diff --git a/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Decoder.java b/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Decoder.java index 37f0b1cb73d..bea58ac5d8e 100644 --- a/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Decoder.java +++ b/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Decoder.java @@ -29,7 +29,6 @@ import io.seata.core.exception.DecodeException; import io.seata.core.protocol.HeartbeatMessage; import io.seata.core.protocol.ProtocolConstants; -import io.seata.core.protocol.RpcMessage; import io.seata.core.serializer.Serializer; import io.seata.core.serializer.SerializerServiceLoader; import io.seata.core.serializer.SerializerType; From 342d306e27b3bea647c800e95cc8b204ee307b9d Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Mon, 22 Jan 2024 18:13:56 +0800 Subject: [PATCH 31/62] single pattern --- .../serializer/seata/SeataSerializer.java | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java index e8ab45237f3..ea3634c5492 100644 --- a/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java +++ b/serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java @@ -35,14 +35,11 @@ public class SeataSerializer implements Serializer { Serializer versionSeataSerializer; - Serializer seataSerializerV0 = new SeataSerializerV0(); - Serializer seataSerializerV1 = new SeataSerializerV1(); - public SeataSerializer(Byte version) { if (version == ProtocolConstants.VERSION_0) { - versionSeataSerializer = seataSerializerV0; + versionSeataSerializer = SeataSerializerV0.getInstance(); } else if (version == ProtocolConstants.VERSION_1) { - versionSeataSerializer = seataSerializerV1; + versionSeataSerializer = SeataSerializerV1.getInstance(); } if (versionSeataSerializer == null) { throw new UnsupportedOperationException("version is not supported"); @@ -62,9 +59,20 @@ public T deserialize(byte[] bytes) { static class SeataSerializerV1 implements Serializer { + private static SeataSerializerV1 instance; + private SeataSerializerV1() { } + public static SeataSerializerV1 getInstance() { + if (instance == null) { + synchronized (SeataSerializerV1.class) { + instance = new SeataSerializerV1(); + } + } + return instance; + } + @Override public byte[] serialize(T t) { if (!(t instanceof AbstractMessage)) { @@ -103,10 +111,20 @@ public T deserialize(byte[] bytes) { } static class SeataSerializerV0 implements Serializer { + private static SeataSerializerV0 instance; private SeataSerializerV0() { } + public static SeataSerializerV0 getInstance() { + if (instance == null) { + synchronized (SeataSerializerV0.class) { + instance = new SeataSerializerV0(); + } + } + return instance; + } + @Override public byte[] serialize(T t) { if (!(t instanceof AbstractMessage)) { From 74c48d5cb32c857eb0488c64a138e5893c3171ef Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Mon, 22 Jan 2024 18:20:50 +0800 Subject: [PATCH 32/62] conflit --- .../main/java/io/seata/core/rpc/netty/v1/ProtocolV1Decoder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Decoder.java b/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Decoder.java index afec56d62e5..47e0db91da4 100644 --- a/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Decoder.java +++ b/core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Decoder.java @@ -151,7 +151,7 @@ public Object decodeFrame(ByteBuf frame) { bs = compressor.decompress(bs); SerializerType protocolType = SerializerType.getByCode(rpcMessage.getCodec()); if (this.serializerType.equals(protocolType)) { - Serializer serializer = SerializerServiceLoader.load(protocolType,, ProtocolConstants.VERSION_1); + Serializer serializer = SerializerServiceLoader.load(protocolType, ProtocolConstants.VERSION_1); rpcMessage.setBody(serializer.deserialize(bs)); } else { throw new IllegalArgumentException("SerializerType not match"); From 0f8c187f7ee19d4ccb310997769f7cb149579986 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Sun, 4 Feb 2024 17:55:34 +0800 Subject: [PATCH 33/62] resolve conflict --- .../apache/seata/serializer/seata/SeataSerializer.java | 8 ++------ .../seata/protocol/BatchResultMessageSerializerTest.java | 3 ++- .../seata/protocol/MergeResultMessageSerializerTest.java | 4 ++-- .../seata/protocol/MergedWarpMessageSerializerTest.java | 4 ++-- .../seata/protocol/RegisterRMRequestSerializerTest.java | 4 ++-- .../seata/protocol/RegisterRMResponseSerializerTest.java | 4 ++-- .../seata/protocol/RegisterTMRequestSerializerTest.java | 4 ++-- .../seata/protocol/RegisterTMResponseSerializerTest.java | 4 ++-- .../transaction/BranchCommitRequestSerializerTest.java | 3 ++- .../transaction/BranchCommitResponseSerializerTest.java | 3 ++- .../transaction/BranchRegisterRequestSerializerTest.java | 3 ++- .../transaction/BranchRegisterResponseSerializerTest.java | 3 ++- .../transaction/BranchReportRequestSerializerTest.java | 3 ++- .../transaction/BranchReportResponseSerializerTest.java | 3 ++- .../transaction/BranchRollbackRequestSerializerTest.java | 3 ++- .../transaction/BranchRollbackResponseSerializerTest.java | 3 ++- .../transaction/GlobalBeginRequestSerializerTest.java | 3 ++- .../transaction/GlobalBeginResponseSerializerTest.java | 3 ++- .../transaction/GlobalCommitRequestCodecTest.java | 3 ++- .../transaction/GlobalCommitResponseSerializerTest.java | 3 ++- .../transaction/GlobalLockQueryRequestSerializerTest.java | 3 ++- .../GlobalLockQueryResponseSerializerTest.java | 3 ++- .../transaction/GlobalRollbackRequestCodecTest.java | 3 ++- .../transaction/GlobalRollbackResponseSerializerTest.java | 3 ++- .../transaction/GlobalStatusRequestCodecTest.java | 3 ++- .../transaction/GlobalStatusResponseSerializerTest.java | 3 ++- .../transaction/UndoLogDeleteRequestSerializerTest.java | 3 ++- 27 files changed, 54 insertions(+), 38 deletions(-) diff --git a/serializer/seata-serializer-seata/src/main/java/org/apache/seata/serializer/seata/SeataSerializer.java b/serializer/seata-serializer-seata/src/main/java/org/apache/seata/serializer/seata/SeataSerializer.java index 671aa9917f8..bf63818556b 100644 --- a/serializer/seata-serializer-seata/src/main/java/org/apache/seata/serializer/seata/SeataSerializer.java +++ b/serializer/seata-serializer-seata/src/main/java/org/apache/seata/serializer/seata/SeataSerializer.java @@ -19,15 +19,11 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import org.apache.seata.common.loader.LoadLevel; +import org.apache.seata.common.loader.Scope; import org.apache.seata.common.util.BufferUtils; import org.apache.seata.core.protocol.AbstractMessage; +import org.apache.seata.core.protocol.ProtocolConstants; import org.apache.seata.core.serializer.Serializer; -import io.seata.common.loader.LoadLevel; -import io.seata.common.loader.Scope; -import io.seata.common.util.BufferUtils; -import io.seata.core.protocol.AbstractMessage; -import io.seata.core.protocol.ProtocolConstants; -import io.seata.core.serializer.Serializer; import java.nio.ByteBuffer; diff --git a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/BatchResultMessageSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/BatchResultMessageSerializerTest.java index 7da24e0954d..ca7ab1a4340 100644 --- a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/BatchResultMessageSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/BatchResultMessageSerializerTest.java @@ -22,6 +22,7 @@ import org.apache.seata.core.model.BranchStatus; import org.apache.seata.core.protocol.AbstractResultMessage; import org.apache.seata.core.protocol.BatchResultMessage; +import org.apache.seata.core.protocol.ProtocolConstants; import org.apache.seata.core.protocol.ResultCode; import org.apache.seata.core.protocol.transaction.BranchCommitResponse; import org.apache.seata.serializer.seata.SeataSerializer; @@ -39,7 +40,7 @@ public class BatchResultMessageSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); @Test diff --git a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/MergeResultMessageSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/MergeResultMessageSerializerTest.java index ee1ae8b83e3..b777710058b 100644 --- a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/MergeResultMessageSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/MergeResultMessageSerializerTest.java @@ -23,7 +23,7 @@ import org.apache.seata.core.protocol.ResultCode; import org.apache.seata.core.protocol.transaction.GlobalBeginResponse; import org.junit.jupiter.api.Test; - +import org.apache.seata.core.protocol.ProtocolConstants; import static org.assertj.core.api.Assertions.assertThat; /** @@ -35,7 +35,7 @@ public class MergeResultMessageSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/MergedWarpMessageSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/MergedWarpMessageSerializerTest.java index 4996210977c..98cce56632f 100644 --- a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/MergedWarpMessageSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/MergedWarpMessageSerializerTest.java @@ -24,7 +24,7 @@ import org.apache.seata.core.protocol.transaction.GlobalBeginRequest; import org.apache.seata.serializer.seata.SeataSerializer; import org.junit.jupiter.api.Test; - +import org.apache.seata.core.protocol.ProtocolConstants; import static org.assertj.core.api.Assertions.assertThat; @@ -37,7 +37,7 @@ public class MergedWarpMessageSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/RegisterRMRequestSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/RegisterRMRequestSerializerTest.java index 46a73801db0..b469bada982 100644 --- a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/RegisterRMRequestSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/RegisterRMRequestSerializerTest.java @@ -19,7 +19,7 @@ import org.apache.seata.serializer.seata.SeataSerializer; import org.apache.seata.core.protocol.RegisterRMRequest; import org.junit.jupiter.api.Test; - +import org.apache.seata.core.protocol.ProtocolConstants; import static org.assertj.core.api.Assertions.assertThat; /** @@ -31,7 +31,7 @@ public class RegisterRMRequestSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/RegisterRMResponseSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/RegisterRMResponseSerializerTest.java index b0115a04817..c766eb203a1 100644 --- a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/RegisterRMResponseSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/RegisterRMResponseSerializerTest.java @@ -20,7 +20,7 @@ import org.apache.seata.core.protocol.RegisterRMResponse; import org.apache.seata.core.protocol.ResultCode; import org.junit.jupiter.api.Test; - +import org.apache.seata.core.protocol.ProtocolConstants; import static org.assertj.core.api.Assertions.assertThat; @@ -33,7 +33,7 @@ public class RegisterRMResponseSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/RegisterTMRequestSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/RegisterTMRequestSerializerTest.java index f3112aa1e27..73c53e89ae2 100644 --- a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/RegisterTMRequestSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/RegisterTMRequestSerializerTest.java @@ -24,7 +24,7 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.mockito.Mockito; - +import org.apache.seata.core.protocol.ProtocolConstants; import static io.netty.buffer.Unpooled.buffer; import static org.assertj.core.api.Assertions.assertThat; @@ -37,7 +37,7 @@ public class RegisterTMRequestSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); private static RegisterTMRequest registerTMRequest; private static AbstractIdentifyRequest air; diff --git a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/RegisterTMResponseSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/RegisterTMResponseSerializerTest.java index 349ef194941..b2cbb88235d 100644 --- a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/RegisterTMResponseSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/RegisterTMResponseSerializerTest.java @@ -20,7 +20,7 @@ import org.apache.seata.core.protocol.RegisterTMResponse; import org.apache.seata.core.protocol.ResultCode; import org.junit.jupiter.api.Test; - +import org.apache.seata.core.protocol.ProtocolConstants; import static org.assertj.core.api.Assertions.assertThat; /** @@ -32,7 +32,7 @@ public class RegisterTMResponseSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/BranchCommitRequestSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/BranchCommitRequestSerializerTest.java index 46f512ad915..fa66b63a35c 100644 --- a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/BranchCommitRequestSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/BranchCommitRequestSerializerTest.java @@ -16,6 +16,7 @@ */ package org.apache.seata.serializer.seata.protocol.transaction; +import org.apache.seata.core.protocol.ProtocolConstants; import org.apache.seata.serializer.seata.SeataSerializer; import org.apache.seata.core.model.BranchType; import org.apache.seata.core.protocol.transaction.BranchCommitRequest; @@ -32,7 +33,7 @@ public class BranchCommitRequestSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/BranchCommitResponseSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/BranchCommitResponseSerializerTest.java index d9e0286130c..d16ccbfa108 100644 --- a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/BranchCommitResponseSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/BranchCommitResponseSerializerTest.java @@ -16,6 +16,7 @@ */ package org.apache.seata.serializer.seata.protocol.transaction; +import org.apache.seata.core.protocol.ProtocolConstants; import org.apache.seata.serializer.seata.SeataSerializer; import org.apache.seata.core.exception.TransactionExceptionCode; import org.apache.seata.core.model.BranchStatus; @@ -34,7 +35,7 @@ public class BranchCommitResponseSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/BranchRegisterRequestSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/BranchRegisterRequestSerializerTest.java index dbd380fa540..dd18665a461 100644 --- a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/BranchRegisterRequestSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/BranchRegisterRequestSerializerTest.java @@ -16,6 +16,7 @@ */ package org.apache.seata.serializer.seata.protocol.transaction; +import org.apache.seata.core.protocol.ProtocolConstants; import org.apache.seata.serializer.seata.SeataSerializer; import org.apache.seata.core.model.BranchType; import org.apache.seata.core.protocol.transaction.BranchRegisterRequest; @@ -32,7 +33,7 @@ public class BranchRegisterRequestSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/BranchRegisterResponseSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/BranchRegisterResponseSerializerTest.java index ef0db9b0bea..d10bf6f09af 100644 --- a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/BranchRegisterResponseSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/BranchRegisterResponseSerializerTest.java @@ -16,6 +16,7 @@ */ package org.apache.seata.serializer.seata.protocol.transaction; +import org.apache.seata.core.protocol.ProtocolConstants; import org.apache.seata.serializer.seata.SeataSerializer; import org.apache.seata.core.exception.TransactionExceptionCode; import org.apache.seata.core.protocol.ResultCode; @@ -33,7 +34,7 @@ public class BranchRegisterResponseSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/BranchReportRequestSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/BranchReportRequestSerializerTest.java index a114c9d49b9..0ea5c0d36f6 100644 --- a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/BranchReportRequestSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/BranchReportRequestSerializerTest.java @@ -16,6 +16,7 @@ */ package org.apache.seata.serializer.seata.protocol.transaction; +import org.apache.seata.core.protocol.ProtocolConstants; import org.apache.seata.serializer.seata.SeataSerializer; import org.apache.seata.core.model.BranchStatus; import org.apache.seata.core.model.BranchType; @@ -33,7 +34,7 @@ public class BranchReportRequestSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/BranchReportResponseSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/BranchReportResponseSerializerTest.java index 0edbc30f00c..2a1fea6cbff 100644 --- a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/BranchReportResponseSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/BranchReportResponseSerializerTest.java @@ -16,6 +16,7 @@ */ package org.apache.seata.serializer.seata.protocol.transaction; +import org.apache.seata.core.protocol.ProtocolConstants; import org.apache.seata.serializer.seata.SeataSerializer; import org.apache.seata.core.exception.TransactionExceptionCode; import org.apache.seata.core.protocol.ResultCode; @@ -33,7 +34,7 @@ public class BranchReportResponseSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/BranchRollbackRequestSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/BranchRollbackRequestSerializerTest.java index 2a7d30a52ec..a73f623a1d3 100644 --- a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/BranchRollbackRequestSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/BranchRollbackRequestSerializerTest.java @@ -16,6 +16,7 @@ */ package org.apache.seata.serializer.seata.protocol.transaction; +import org.apache.seata.core.protocol.ProtocolConstants; import org.apache.seata.serializer.seata.SeataSerializer; import org.apache.seata.core.model.BranchType; import org.apache.seata.core.protocol.transaction.BranchRollbackRequest; @@ -32,7 +33,7 @@ public class BranchRollbackRequestSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/BranchRollbackResponseSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/BranchRollbackResponseSerializerTest.java index 49dca443608..629ec6a4c2e 100644 --- a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/BranchRollbackResponseSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/BranchRollbackResponseSerializerTest.java @@ -16,6 +16,7 @@ */ package org.apache.seata.serializer.seata.protocol.transaction; +import org.apache.seata.core.protocol.ProtocolConstants; import org.apache.seata.serializer.seata.SeataSerializer; import org.apache.seata.core.exception.TransactionExceptionCode; import org.apache.seata.core.model.BranchStatus; @@ -34,7 +35,7 @@ public class BranchRollbackResponseSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalBeginRequestSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalBeginRequestSerializerTest.java index ddc3b82483a..16cd14a8d82 100644 --- a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalBeginRequestSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalBeginRequestSerializerTest.java @@ -16,6 +16,7 @@ */ package org.apache.seata.serializer.seata.protocol.transaction; +import org.apache.seata.core.protocol.ProtocolConstants; import org.apache.seata.serializer.seata.SeataSerializer; import org.apache.seata.core.protocol.transaction.GlobalBeginRequest; import org.junit.jupiter.api.Test; @@ -31,7 +32,7 @@ public class GlobalBeginRequestSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalBeginResponseSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalBeginResponseSerializerTest.java index 8b9cadfccf5..c3bb00f93b3 100644 --- a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalBeginResponseSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalBeginResponseSerializerTest.java @@ -16,6 +16,7 @@ */ package org.apache.seata.serializer.seata.protocol.transaction; +import org.apache.seata.core.protocol.ProtocolConstants; import org.apache.seata.serializer.seata.SeataSerializer; import org.apache.seata.core.exception.TransactionExceptionCode; import org.apache.seata.core.protocol.ResultCode; @@ -32,7 +33,7 @@ public class GlobalBeginResponseSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalCommitRequestCodecTest.java b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalCommitRequestCodecTest.java index 8eea0d3aea5..502d4db7001 100644 --- a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalCommitRequestCodecTest.java +++ b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalCommitRequestCodecTest.java @@ -16,6 +16,7 @@ */ package org.apache.seata.serializer.seata.protocol.transaction; +import org.apache.seata.core.protocol.ProtocolConstants; import org.apache.seata.serializer.seata.SeataSerializer; import org.apache.seata.core.protocol.transaction.GlobalCommitRequest; import org.junit.jupiter.api.Test; @@ -31,7 +32,7 @@ public class GlobalCommitRequestCodecTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalCommitResponseSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalCommitResponseSerializerTest.java index a4418cb29f4..d9f2f2e8d9f 100644 --- a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalCommitResponseSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalCommitResponseSerializerTest.java @@ -16,6 +16,7 @@ */ package org.apache.seata.serializer.seata.protocol.transaction; +import org.apache.seata.core.protocol.ProtocolConstants; import org.apache.seata.serializer.seata.SeataSerializer; import org.apache.seata.core.exception.TransactionExceptionCode; import org.apache.seata.core.model.GlobalStatus; @@ -33,7 +34,7 @@ public class GlobalCommitResponseSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalLockQueryRequestSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalLockQueryRequestSerializerTest.java index 5c2951fedb0..be9a494f790 100644 --- a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalLockQueryRequestSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalLockQueryRequestSerializerTest.java @@ -16,6 +16,7 @@ */ package org.apache.seata.serializer.seata.protocol.transaction; +import org.apache.seata.core.protocol.ProtocolConstants; import org.apache.seata.serializer.seata.SeataSerializer; import org.apache.seata.core.model.BranchType; import org.apache.seata.core.protocol.transaction.GlobalLockQueryRequest; @@ -31,7 +32,7 @@ public class GlobalLockQueryRequestSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalLockQueryResponseSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalLockQueryResponseSerializerTest.java index 2d367ca4daa..1761a11c941 100644 --- a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalLockQueryResponseSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalLockQueryResponseSerializerTest.java @@ -16,6 +16,7 @@ */ package org.apache.seata.serializer.seata.protocol.transaction; +import org.apache.seata.core.protocol.ProtocolConstants; import org.apache.seata.serializer.seata.SeataSerializer; import org.apache.seata.core.exception.TransactionExceptionCode; import org.apache.seata.core.protocol.ResultCode; @@ -33,7 +34,7 @@ public class GlobalLockQueryResponseSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalRollbackRequestCodecTest.java b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalRollbackRequestCodecTest.java index 1b6bf6a128f..204bafa692d 100644 --- a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalRollbackRequestCodecTest.java +++ b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalRollbackRequestCodecTest.java @@ -16,6 +16,7 @@ */ package org.apache.seata.serializer.seata.protocol.transaction; +import org.apache.seata.core.protocol.ProtocolConstants; import org.apache.seata.serializer.seata.SeataSerializer; import org.apache.seata.core.protocol.transaction.GlobalRollbackRequest; import org.junit.jupiter.api.Test; @@ -31,7 +32,7 @@ public class GlobalRollbackRequestCodecTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalRollbackResponseSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalRollbackResponseSerializerTest.java index 32cbd7b3312..084cbc62c56 100644 --- a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalRollbackResponseSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalRollbackResponseSerializerTest.java @@ -16,6 +16,7 @@ */ package org.apache.seata.serializer.seata.protocol.transaction; +import org.apache.seata.core.protocol.ProtocolConstants; import org.apache.seata.serializer.seata.SeataSerializer; import org.apache.seata.core.exception.TransactionExceptionCode; import org.apache.seata.core.model.GlobalStatus; @@ -34,7 +35,7 @@ public class GlobalRollbackResponseSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalStatusRequestCodecTest.java b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalStatusRequestCodecTest.java index 2fe58490c64..0ea3bbba80d 100644 --- a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalStatusRequestCodecTest.java +++ b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalStatusRequestCodecTest.java @@ -16,6 +16,7 @@ */ package org.apache.seata.serializer.seata.protocol.transaction; +import org.apache.seata.core.protocol.ProtocolConstants; import org.apache.seata.serializer.seata.SeataSerializer; import org.apache.seata.core.protocol.transaction.GlobalStatusRequest; import org.junit.jupiter.api.Test; @@ -31,7 +32,7 @@ public class GlobalStatusRequestCodecTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalStatusResponseSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalStatusResponseSerializerTest.java index fc1ebe73545..9122db6d3ee 100644 --- a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalStatusResponseSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/GlobalStatusResponseSerializerTest.java @@ -16,6 +16,7 @@ */ package org.apache.seata.serializer.seata.protocol.transaction; +import org.apache.seata.core.protocol.ProtocolConstants; import org.apache.seata.serializer.seata.SeataSerializer; import org.apache.seata.core.exception.TransactionExceptionCode; import org.apache.seata.core.model.GlobalStatus; @@ -33,7 +34,7 @@ public class GlobalStatusResponseSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. diff --git a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/UndoLogDeleteRequestSerializerTest.java b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/UndoLogDeleteRequestSerializerTest.java index 3c274239049..17ff921034b 100644 --- a/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/UndoLogDeleteRequestSerializerTest.java +++ b/serializer/seata-serializer-seata/src/test/java/org/apache/seata/serializer/seata/protocol/transaction/UndoLogDeleteRequestSerializerTest.java @@ -16,6 +16,7 @@ */ package org.apache.seata.serializer.seata.protocol.transaction; +import org.apache.seata.core.protocol.ProtocolConstants; import org.apache.seata.serializer.seata.SeataSerializer; import org.apache.seata.core.model.BranchType; import org.apache.seata.core.protocol.transaction.UndoLogDeleteRequest; @@ -32,7 +33,7 @@ public class UndoLogDeleteRequestSerializerTest { /** * The Seata codec. */ - SeataSerializer seataSerializer = new SeataSerializer(); + SeataSerializer seataSerializer = new SeataSerializer(ProtocolConstants.VERSION); /** * Test codec. From cf4fe416a52ffb98f4c6d6277aa2f46f4584b088 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Sun, 4 Feb 2024 18:05:55 +0800 Subject: [PATCH 34/62] resolve conflict --- .../seata/core/rpc/netty/ProtocolRpcMessage.java | 6 +++--- .../core/rpc/netty/v0/ProtocolRpcMessageV0.java | 12 ++++++------ .../core/rpc/netty/v1/ProtocolRpcMessageV1.java | 8 ++++---- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/ProtocolRpcMessage.java b/core/src/main/java/org/apache/seata/core/rpc/netty/ProtocolRpcMessage.java index b538e04e604..944d87abc46 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/ProtocolRpcMessage.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/ProtocolRpcMessage.java @@ -14,10 +14,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.seata.core.rpc.netty; +package org.apache.seata.core.rpc.netty; -import io.seata.core.protocol.AbstractIdentifyRequest; -import io.seata.core.protocol.RpcMessage; +import org.apache.seata.core.protocol.AbstractIdentifyRequest; +import org.apache.seata.core.protocol.RpcMessage; /** * The protocol RPC message. diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolRpcMessageV0.java b/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolRpcMessageV0.java index d6a4bfa54f4..087f0e099aa 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolRpcMessageV0.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolRpcMessageV0.java @@ -14,13 +14,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.seata.core.rpc.netty.v0; +package org.apache.seata.core.rpc.netty.v0; -import io.seata.core.compressor.CompressorType; -import io.seata.core.protocol.ProtocolConstants; -import io.seata.core.protocol.RpcMessage; -import io.seata.core.rpc.netty.ProtocolRpcMessage; -import io.seata.core.serializer.SerializerType; +import org.apache.seata.core.compressor.CompressorType; +import org.apache.seata.core.protocol.ProtocolConstants; +import org.apache.seata.core.protocol.RpcMessage; +import org.apache.seata.core.rpc.netty.ProtocolRpcMessage; +import org.apache.seata.core.serializer.SerializerType; import java.util.concurrent.atomic.AtomicLong; diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolRpcMessageV1.java b/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolRpcMessageV1.java index 1a3a76e1f27..04621cfcda9 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolRpcMessageV1.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolRpcMessageV1.java @@ -14,11 +14,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.seata.core.rpc.netty.v1; +package org.apache.seata.core.rpc.netty.v1; -import io.seata.common.util.StringUtils; -import io.seata.core.protocol.RpcMessage; -import io.seata.core.rpc.netty.ProtocolRpcMessage; +import org.apache.seata.common.util.StringUtils; +import org.apache.seata.core.protocol.RpcMessage; +import org.apache.seata.core.rpc.netty.ProtocolRpcMessage; import java.util.HashMap; import java.util.Map; From 9607bcd87aef9fd2c7124b7678fdde43096d5f7e Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Sun, 4 Feb 2024 18:09:54 +0800 Subject: [PATCH 35/62] resolve conflict --- .../seata/core/rpc/netty/v1/ProtocolV1SerializerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1SerializerTest.java b/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1SerializerTest.java index bbc606b35b0..5ee0df7dee2 100644 --- a/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1SerializerTest.java +++ b/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1SerializerTest.java @@ -27,8 +27,8 @@ import org.apache.seata.common.thread.NamedThreadFactory; import org.apache.seata.core.model.BranchType; -import org.apache.seata.core.protocol.RpcMessage; import org.apache.seata.core.protocol.transaction.BranchCommitRequest; +import org.apache.seata.core.rpc.netty.ProtocolRpcMessage; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.slf4j.Logger; From 5b433cd67a3f5ea0507057ddc6e1643088502a42 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Wed, 7 Feb 2024 15:35:56 +0800 Subject: [PATCH 36/62] import --- .../org/apache/seata/core/rpc/netty/v1/ProtocolV1Decoder.java | 1 - 1 file changed, 1 deletion(-) diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1Decoder.java b/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1Decoder.java index 282b6fa7b19..1d603dd4610 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1Decoder.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1Decoder.java @@ -29,7 +29,6 @@ import org.apache.seata.core.exception.DecodeException; import org.apache.seata.core.protocol.HeartbeatMessage; import org.apache.seata.core.protocol.ProtocolConstants; -import org.apache.seata.core.protocol.RpcMessage; import org.apache.seata.core.serializer.Serializer; import org.apache.seata.core.serializer.SerializerServiceLoader; import org.apache.seata.core.serializer.SerializerType; From 704669a4b39dd17059a508c8f6b54d20ee605403 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Wed, 28 Feb 2024 11:46:34 +0800 Subject: [PATCH 37/62] style --- .../java/org/apache/seata/serializer/seata/SeataSerializer.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/serializer/seata-serializer-seata/src/main/java/org/apache/seata/serializer/seata/SeataSerializer.java b/serializer/seata-serializer-seata/src/main/java/org/apache/seata/serializer/seata/SeataSerializer.java index bf63818556b..53b36da424b 100644 --- a/serializer/seata-serializer-seata/src/main/java/org/apache/seata/serializer/seata/SeataSerializer.java +++ b/serializer/seata-serializer-seata/src/main/java/org/apache/seata/serializer/seata/SeataSerializer.java @@ -32,7 +32,6 @@ */ @LoadLevel(name = "SEATA", scope = Scope.PROTOTYPE) public class SeataSerializer implements Serializer { - Serializer versionSeataSerializer; public SeataSerializer(Byte version) { @@ -106,7 +105,6 @@ public byte[] serialize(T t) { @Override public T deserialize(byte[] bytes) { return deserializeByVersion(bytes, ProtocolConstants.VERSION_1); - } } static class SeataSerializerV0 implements Serializer { From 00a4e8e9371a3606f53fd075413421297dd6168e Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Mon, 4 Mar 2024 18:57:31 +0800 Subject: [PATCH 38/62] style --- .../apache/seata/core/rpc/netty/ProtocolRpcMessage.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/ProtocolRpcMessage.java b/core/src/main/java/org/apache/seata/core/rpc/netty/ProtocolRpcMessage.java index 944d87abc46..277b727c8c4 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/ProtocolRpcMessage.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/ProtocolRpcMessage.java @@ -23,8 +23,17 @@ * The protocol RPC message. */ public interface ProtocolRpcMessage { + + /** + * The protocol message to rpc message. + * @return + */ RpcMessage protocolMsg2RpcMsg(); + /** + * The rpc message to protocol message. + * @param rpcMessage + */ void rpcMsg2ProtocolMsg(RpcMessage rpcMessage); static String getVersion(Object body) { From 7396cf96fbae949bc86b517538b794c3a549ca1b Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Tue, 5 Mar 2024 11:16:51 +0800 Subject: [PATCH 39/62] Merge branch '2.x' of https://github.com/seata/seata into dev-mlv-complete-v1 # Conflicts: # core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Decoder.java # core/src/main/java/io/seata/core/rpc/netty/v1/ProtocolV1Encoder.java # core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemoting.java # core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolDecoder.java # core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolEncoder.java # core/src/main/java/org/apache/seata/core/rpc/netty/NettyClientBootstrap.java # core/src/main/java/org/apache/seata/core/rpc/netty/NettyServerBootstrap.java # core/src/main/java/org/apache/seata/core/rpc/netty/ProtocolDecoder.java # core/src/main/java/org/apache/seata/core/rpc/netty/ProtocolEncoder.java # core/src/main/java/org/apache/seata/core/rpc/netty/ProtocolRpcMessage.java # core/src/main/java/org/apache/seata/core/rpc/netty/v0/MessageCodecV0.java # core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolConstantsV0.java # core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolDecoderV0.java # core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolEncoderV0.java # core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolRpcMessageV0.java # core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolDecoderV1.java # core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolEncoderV1.java # core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolRpcMessageV1.java # core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1Decoder.java # core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1Encoder.java # test/src/test/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1Client.java # test/src/test/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1Server.java --- .../rpc/netty/CompatibleProtocolDecoder.java | 10 +++++----- .../rpc/netty/CompatibleProtocolEncoder.java | 12 +++++------ .../seata/core/rpc/netty/ProtocolDecoder.java | 2 +- .../seata/core/rpc/netty/ProtocolEncoder.java | 4 ++-- .../core/rpc/netty/v0/MessageCodecV0.java | 4 ++-- .../rpc/netty/v0/ProtocolConstantsV0.java | 2 +- .../core/rpc/netty/v0/ProtocolDecoderV0.java | 14 ++++++------- .../core/rpc/netty/v0/ProtocolEncoderV0.java | 18 ++++++++--------- .../core/rpc/netty/v1/ProtocolDecoderV1.java | 20 +++++++++---------- .../core/rpc/netty/v1/ProtocolEncoderV1.java | 18 ++++++++--------- 10 files changed, 52 insertions(+), 52 deletions(-) diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolDecoder.java b/core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolDecoder.java index ee699da23b5..d066984c23b 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolDecoder.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolDecoder.java @@ -14,16 +14,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.seata.core.rpc.netty; +package org.apache.seata.core.rpc.netty; import com.google.common.collect.ImmutableMap; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.LengthFieldBasedFrameDecoder; -import io.seata.core.exception.DecodeException; -import io.seata.core.protocol.ProtocolConstants; -import io.seata.core.rpc.netty.v0.ProtocolDecoderV0; -import io.seata.core.rpc.netty.v1.ProtocolDecoderV1; +import org.apache.seata.core.exception.DecodeException; +import org.apache.seata.core.protocol.ProtocolConstants; +import org.apache.seata.core.rpc.netty.v0.ProtocolDecoderV0; +import org.apache.seata.core.rpc.netty.v1.ProtocolDecoderV1; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolEncoder.java b/core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolEncoder.java index 299d962f92b..dfd445abae8 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolEncoder.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolEncoder.java @@ -14,17 +14,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.seata.core.rpc.netty; +package org.apache.seata.core.rpc.netty; import com.google.common.collect.ImmutableMap; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToByteEncoder; -import io.seata.core.protocol.ProtocolConstants; -import io.seata.core.protocol.RpcMessage; -import io.seata.core.protocol.Version; -import io.seata.core.rpc.netty.v0.ProtocolEncoderV0; -import io.seata.core.rpc.netty.v1.ProtocolEncoderV1; +import org.apache.seata.core.protocol.ProtocolConstants; +import org.apache.seata.core.protocol.RpcMessage; +import org.apache.seata.core.protocol.Version; +import org.apache.seata.core.rpc.netty.v0.ProtocolEncoderV0; +import org.apache.seata.core.rpc.netty.v1.ProtocolEncoderV1; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/ProtocolDecoder.java b/core/src/main/java/org/apache/seata/core/rpc/netty/ProtocolDecoder.java index bfef75f0efd..42a7c75c04f 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/ProtocolDecoder.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/ProtocolDecoder.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.seata.core.rpc.netty; +package org.apache.seata.core.rpc.netty; import io.netty.buffer.ByteBuf; diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/ProtocolEncoder.java b/core/src/main/java/org/apache/seata/core/rpc/netty/ProtocolEncoder.java index 49b8917c578..6c91164fff3 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/ProtocolEncoder.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/ProtocolEncoder.java @@ -14,10 +14,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.seata.core.rpc.netty; +package org.apache.seata.core.rpc.netty; import io.netty.buffer.ByteBuf; -import io.seata.core.protocol.RpcMessage; +import org.apache.seata.core.protocol.RpcMessage; /** * the protocol encoder diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/v0/MessageCodecV0.java b/core/src/main/java/org/apache/seata/core/rpc/netty/v0/MessageCodecV0.java index 1261601e4ac..ab1d4f74716 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/v0/MessageCodecV0.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/v0/MessageCodecV0.java @@ -14,10 +14,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.seata.core.rpc.netty.v0; +package org.apache.seata.core.rpc.netty.v0; import io.netty.buffer.ByteBuf; -import io.seata.core.protocol.MessageTypeAware; +import org.apache.seata.core.protocol.MessageTypeAware; /** * The interface Message codec. diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolConstantsV0.java b/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolConstantsV0.java index e86bea98e9b..65b3634059d 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolConstantsV0.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolConstantsV0.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.seata.core.rpc.netty.v0; +package org.apache.seata.core.rpc.netty.v0; /** * protocol v0 constants diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolDecoderV0.java b/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolDecoderV0.java index 91078e1ec11..42e112a2f61 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolDecoderV0.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolDecoderV0.java @@ -14,16 +14,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.seata.core.rpc.netty.v0; +package org.apache.seata.core.rpc.netty.v0; import io.netty.buffer.ByteBuf; -import io.seata.core.protocol.HeartbeatMessage; +import org.apache.seata.core.protocol.HeartbeatMessage; -import io.seata.core.protocol.ProtocolConstants; -import io.seata.core.rpc.netty.ProtocolDecoder; -import io.seata.core.serializer.Serializer; -import io.seata.core.serializer.SerializerServiceLoader; -import io.seata.core.serializer.SerializerType; +import org.apache.seata.core.protocol.ProtocolConstants; +import org.apache.seata.core.rpc.netty.ProtocolDecoder; +import org.apache.seata.core.serializer.Serializer; +import org.apache.seata.core.serializer.SerializerServiceLoader; +import org.apache.seata.core.serializer.SerializerType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolEncoderV0.java b/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolEncoderV0.java index e72f3f9f53a..3fc447b2818 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolEncoderV0.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolEncoderV0.java @@ -14,17 +14,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.seata.core.rpc.netty.v0; +package org.apache.seata.core.rpc.netty.v0; import io.netty.buffer.ByteBuf; -import io.seata.core.protocol.HeartbeatMessage; -import io.seata.core.protocol.MessageTypeAware; -import io.seata.core.protocol.ProtocolConstants; -import io.seata.core.protocol.RpcMessage; -import io.seata.core.rpc.netty.ProtocolEncoder; -import io.seata.core.serializer.Serializer; -import io.seata.core.serializer.SerializerServiceLoader; -import io.seata.core.serializer.SerializerType; +import org.apache.seata.core.protocol.HeartbeatMessage; +import org.apache.seata.core.protocol.MessageTypeAware; +import org.apache.seata.core.protocol.ProtocolConstants; +import org.apache.seata.core.protocol.RpcMessage; +import org.apache.seata.core.rpc.netty.ProtocolEncoder; +import org.apache.seata.core.serializer.Serializer; +import org.apache.seata.core.serializer.SerializerServiceLoader; +import org.apache.seata.core.serializer.SerializerType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolDecoderV1.java b/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolDecoderV1.java index 0ae92149373..4a01b8574e3 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolDecoderV1.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolDecoderV1.java @@ -14,18 +14,18 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.seata.core.rpc.netty.v1; +package org.apache.seata.core.rpc.netty.v1; import io.netty.buffer.ByteBuf; -import io.seata.core.compressor.Compressor; -import io.seata.core.compressor.CompressorFactory; -import io.seata.core.protocol.HeartbeatMessage; -import io.seata.core.protocol.ProtocolConstants; -import io.seata.core.rpc.netty.ProtocolDecoder; -import io.seata.core.rpc.netty.ProtocolRpcMessage; -import io.seata.core.serializer.Serializer; -import io.seata.core.serializer.SerializerServiceLoader; -import io.seata.core.serializer.SerializerType; +import org.apache.seata.core.compressor.Compressor; +import org.apache.seata.core.compressor.CompressorFactory; +import org.apache.seata.core.protocol.HeartbeatMessage; +import org.apache.seata.core.protocol.ProtocolConstants; +import org.apache.seata.core.rpc.netty.ProtocolDecoder; +import org.apache.seata.core.rpc.netty.ProtocolRpcMessage; +import org.apache.seata.core.serializer.Serializer; +import org.apache.seata.core.serializer.SerializerServiceLoader; +import org.apache.seata.core.serializer.SerializerType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolEncoderV1.java b/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolEncoderV1.java index f23bab30f3c..14cbcdb55da 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolEncoderV1.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolEncoderV1.java @@ -14,17 +14,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.seata.core.rpc.netty.v1; +package org.apache.seata.core.rpc.netty.v1; import io.netty.buffer.ByteBuf; -import io.seata.core.rpc.netty.ProtocolEncoder; -import io.seata.core.serializer.Serializer; -import io.seata.core.compressor.Compressor; -import io.seata.core.compressor.CompressorFactory; -import io.seata.core.protocol.ProtocolConstants; -import io.seata.core.protocol.RpcMessage; -import io.seata.core.serializer.SerializerServiceLoader; -import io.seata.core.serializer.SerializerType; +import org.apache.seata.core.rpc.netty.ProtocolEncoder; +import org.apache.seata.core.serializer.Serializer; +import org.apache.seata.core.compressor.Compressor; +import org.apache.seata.core.compressor.CompressorFactory; +import org.apache.seata.core.protocol.ProtocolConstants; +import org.apache.seata.core.protocol.RpcMessage; +import org.apache.seata.core.serializer.SerializerServiceLoader; +import org.apache.seata.core.serializer.SerializerType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; From 3f7a3c873d736b54714b6378a83ae5749a5b97d1 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Tue, 5 Mar 2024 11:22:19 +0800 Subject: [PATCH 40/62] fix test --- .../java/org/apache/seata/core/protocol/ProtocolConstants.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/org/apache/seata/core/protocol/ProtocolConstants.java b/core/src/main/java/org/apache/seata/core/protocol/ProtocolConstants.java index 0d802e2f85b..fc88b09504d 100644 --- a/core/src/main/java/org/apache/seata/core/protocol/ProtocolConstants.java +++ b/core/src/main/java/org/apache/seata/core/protocol/ProtocolConstants.java @@ -44,7 +44,7 @@ public interface ProtocolConstants { /** * Protocol version */ - byte VERSION_CURRENT = VERSION_1; + byte VERSION = VERSION_1; /** * Max frame length From ef146b7dbcaec15713d361bb5377df555ec946f8 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Thu, 7 Mar 2024 11:31:33 +0800 Subject: [PATCH 41/62] optimize version --- .../seata/core/protocol/RpcMessage.java | 10 +-- .../core/rpc/netty/AbstractNettyRemoting.java | 4 +- .../rpc/netty/CompatibleProtocolEncoder.java | 8 ++- .../core/rpc/netty/ProtocolRpcMessage.java | 4 +- .../rpc/netty/v0/ProtocolRpcMessageV0.java | 4 +- .../rpc/netty/v1/ProtocolRpcMessageV1.java | 4 +- .../rpc/processor/server/RegTmProcessor.java | 2 +- .../server/ServerOnRequestProcessor.java | 63 ++++++++++--------- .../rpc/netty/mockserver/RmClientTest.java | 6 -- .../rpc/netty/mockserver/TmClientTest.java | 6 -- .../core/rpc/netty/v1/ProtocolV1Client.java | 1 + 11 files changed, 54 insertions(+), 58 deletions(-) diff --git a/core/src/main/java/org/apache/seata/core/protocol/RpcMessage.java b/core/src/main/java/org/apache/seata/core/protocol/RpcMessage.java index 0424f7ac00c..fa80f814f5d 100644 --- a/core/src/main/java/org/apache/seata/core/protocol/RpcMessage.java +++ b/core/src/main/java/org/apache/seata/core/protocol/RpcMessage.java @@ -35,7 +35,7 @@ public class RpcMessage implements Serializable { private Map headMap = new HashMap<>(); private Object body; - private String version; + private String sdkVersion; /** * Gets id. @@ -171,12 +171,12 @@ public void setMessageType(byte messageType) { this.messageType = messageType; } - public String getVersion() { - return version; + public String getSdkVersion() { + return sdkVersion; } - public void setVersion(String version) { - this.version = version; + public void setSdkVersion(String sdkVersion) { + this.sdkVersion = sdkVersion; } @Override diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemoting.java b/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemoting.java index 332f04b3195..4e96ce4d5ae 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemoting.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemoting.java @@ -243,7 +243,7 @@ protected RpcMessage buildRequestMessage(Object msg, byte messageType, String ve rpcMessage.setCodec(ProtocolConstants.CONFIGURED_CODEC); rpcMessage.setCompressor(ProtocolConstants.CONFIGURED_COMPRESSOR); rpcMessage.setBody(msg); - rpcMessage.setVersion(version); + rpcMessage.setSdkVersion(version); return rpcMessage; } @@ -258,7 +258,7 @@ protected RpcMessage buildResponseMessage(RpcMessage rpcMessage, Object msg, byt rpcMsg.setCompressor(rpcMessage.getCompressor()); rpcMsg.setBody(msg); rpcMsg.setId(rpcMessage.getId()); - rpcMsg.setVersion(version); + rpcMsg.setSdkVersion(version); return rpcMsg; } diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolEncoder.java b/core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolEncoder.java index dfd445abae8..8c9d790bbc6 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolEncoder.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolEncoder.java @@ -58,10 +58,12 @@ public void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) { try { if (msg instanceof RpcMessage) { RpcMessage rpcMessage = (RpcMessage) msg; - byte version = Version.calcProtocolVersion(rpcMessage.getVersion()); - ProtocolEncoder encoder = protocolEncoderMap.get(version); + String sdkVersion = rpcMessage.getSdkVersion(); + //todo null? + byte protocolVersion = Version.calcProtocolVersion(sdkVersion); + ProtocolEncoder encoder = protocolEncoderMap.get(protocolVersion); if (encoder == null) { - throw new UnsupportedOperationException("Unsupported version: " + version); + throw new UnsupportedOperationException("Unsupported protocolVersion: " + protocolVersion); } encoder.encode(rpcMessage, out); diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/ProtocolRpcMessage.java b/core/src/main/java/org/apache/seata/core/rpc/netty/ProtocolRpcMessage.java index 277b727c8c4..1211ad56f10 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/ProtocolRpcMessage.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/ProtocolRpcMessage.java @@ -36,7 +36,7 @@ public interface ProtocolRpcMessage { */ void rpcMsg2ProtocolMsg(RpcMessage rpcMessage); - static String getVersion(Object body) { + static String getSdkVersion(Object body) { if (body instanceof AbstractIdentifyRequest) { return ((AbstractIdentifyRequest) body).getVersion(); } else { @@ -44,7 +44,7 @@ static String getVersion(Object body) { } } - static void setVersion(Object body, String version) { + static void setSdkVersion(Object body, String version) { if (body instanceof AbstractIdentifyRequest) { ((AbstractIdentifyRequest) body).setVersion(version); } diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolRpcMessageV0.java b/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolRpcMessageV0.java index 087f0e099aa..1ada24def10 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolRpcMessageV0.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolRpcMessageV0.java @@ -178,14 +178,14 @@ public RpcMessage protocolMsg2RpcMsg() { } rpcMessage.setBody(this.body); rpcMessage.setId((int) this.id); - rpcMessage.setVersion(ProtocolRpcMessage.getVersion(this.body)); + rpcMessage.setSdkVersion(ProtocolRpcMessage.getSdkVersion(this.body)); return rpcMessage; } @Override public void rpcMsg2ProtocolMsg(RpcMessage rpcMessage) { this.body = rpcMessage.getBody(); - ProtocolRpcMessage.setVersion(this.body, rpcMessage.getVersion()); + ProtocolRpcMessage.setSdkVersion(this.body, rpcMessage.getSdkVersion()); this.id = rpcMessage.getId(); this.isRequest = isRequest(rpcMessage.getMessageType()); this.isHeartbeat = isHeartbeat(rpcMessage.getMessageType()); diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolRpcMessageV1.java b/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolRpcMessageV1.java index 04621cfcda9..7e6668f4a01 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolRpcMessageV1.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolRpcMessageV1.java @@ -179,7 +179,7 @@ public RpcMessage protocolMsg2RpcMsg() { rpcMessage.setCompressor(this.compressor); rpcMessage.setHeadMap(this.headMap); rpcMessage.setBody(this.body); - rpcMessage.setVersion(ProtocolRpcMessage.getVersion(this.body)); + rpcMessage.setSdkVersion(ProtocolRpcMessage.getSdkVersion(this.body)); return rpcMessage; } @@ -187,7 +187,7 @@ public RpcMessage protocolMsg2RpcMsg() { @Override public void rpcMsg2ProtocolMsg(RpcMessage rpcMessage) { this.body = rpcMessage.getBody(); - ProtocolRpcMessage.setVersion(this.body, rpcMessage.getVersion()); + ProtocolRpcMessage.setSdkVersion(this.body, rpcMessage.getSdkVersion()); this.headMap = rpcMessage.getHeadMap(); this.id = rpcMessage.getId(); this.messageType = rpcMessage.getMessageType(); diff --git a/core/src/main/java/org/apache/seata/core/rpc/processor/server/RegTmProcessor.java b/core/src/main/java/org/apache/seata/core/rpc/processor/server/RegTmProcessor.java index 9eef06f82f9..b63ae2d84bc 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/processor/server/RegTmProcessor.java +++ b/core/src/main/java/org/apache/seata/core/rpc/processor/server/RegTmProcessor.java @@ -90,7 +90,7 @@ private void onRegTmMessage(ChannelHandlerContext ctx, RpcMessage rpcMessage) { remotingServer.sendAsyncResponse(rpcMessage, ctx.channel(), response); if (isSuccess && LOGGER.isInfoEnabled()) { LOGGER.info("TM register success,message:{},channel:{},client version:{},client protocol-version:{}" - , message, ctx.channel(), message.getVersion(), rpcMessage.getVersion()); + , message, ctx.channel(), message.getVersion(), rpcMessage.getSdkVersion()); } } diff --git a/core/src/main/java/org/apache/seata/core/rpc/processor/server/ServerOnRequestProcessor.java b/core/src/main/java/org/apache/seata/core/rpc/processor/server/ServerOnRequestProcessor.java index 5431c19e630..aa28a643cb0 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/processor/server/ServerOnRequestProcessor.java +++ b/core/src/main/java/org/apache/seata/core/rpc/processor/server/ServerOnRequestProcessor.java @@ -101,17 +101,17 @@ public class ServerOnRequestProcessor implements RemotingProcessor, Disposable { private static final long KEEP_ALIVE_TIME = Integer.MAX_VALUE; private static final String BATCH_RESPONSE_THREAD_PREFIX = "rpcBatchResponse"; private static final boolean PARALLEL_REQUEST_HANDLE = - ConfigurationFactory.getInstance().getBoolean(ConfigurationKeys.ENABLE_PARALLEL_REQUEST_HANDLE_KEY, true); + ConfigurationFactory.getInstance().getBoolean(ConfigurationKeys.ENABLE_PARALLEL_REQUEST_HANDLE_KEY, true); public ServerOnRequestProcessor(RemotingServer remotingServer, TransactionMessageHandler transactionMessageHandler) { this.remotingServer = remotingServer; this.transactionMessageHandler = transactionMessageHandler; if (NettyServerConfig.isEnableTcServerBatchSendResponse()) { batchResponseExecutorService = new ThreadPoolExecutor(MAX_BATCH_RESPONSE_THREAD, - MAX_BATCH_RESPONSE_THREAD, - KEEP_ALIVE_TIME, TimeUnit.MILLISECONDS, - new LinkedBlockingQueue<>(), - new NamedThreadFactory(BATCH_RESPONSE_THREAD_PREFIX, MAX_BATCH_RESPONSE_THREAD)); + MAX_BATCH_RESPONSE_THREAD, + KEEP_ALIVE_TIME, TimeUnit.MILLISECONDS, + new LinkedBlockingQueue<>(), + new NamedThreadFactory(BATCH_RESPONSE_THREAD_PREFIX, MAX_BATCH_RESPONSE_THREAD)); batchResponseExecutorService.submit(new BatchResponseRunnable()); } } @@ -153,15 +153,15 @@ private void onRequestMessage(ChannelHandlerContext ctx, RpcMessage rpcMessage) // the batch send request message if (message instanceof MergedWarpMessage) { if (NettyServerConfig.isEnableTcServerBatchSendResponse() && StringUtils.isNotBlank(rpcContext.getVersion()) - && Version.isAboveOrEqualVersion150(rpcContext.getVersion())) { - List msgs = ((MergedWarpMessage)message).msgs; - List msgIds = ((MergedWarpMessage)message).msgIds; + && Version.isAboveOrEqualVersion150(rpcContext.getVersion())) { + List msgs = ((MergedWarpMessage) message).msgs; + List msgIds = ((MergedWarpMessage) message).msgIds; for (int i = 0; i < msgs.size(); i++) { AbstractMessage msg = msgs.get(i); int msgId = msgIds.get(i); if (PARALLEL_REQUEST_HANDLE) { CompletableFuture.runAsync( - () -> handleRequestsByMergedWarpMessageBy150(msg, msgId, rpcMessage, ctx, rpcContext)); + () -> handleRequestsByMergedWarpMessageBy150(msg, msgId, rpcMessage, ctx, rpcContext)); } else { handleRequestsByMergedWarpMessageBy150(msg, msgId, rpcMessage, ctx, rpcContext); } @@ -169,17 +169,17 @@ private void onRequestMessage(ChannelHandlerContext ctx, RpcMessage rpcMessage) } else { List results = new ArrayList<>(); List> completableFutures = null; - for (int i = 0; i < ((MergedWarpMessage)message).msgs.size(); i++) { + for (int i = 0; i < ((MergedWarpMessage) message).msgs.size(); i++) { if (PARALLEL_REQUEST_HANDLE) { if (completableFutures == null) { completableFutures = new ArrayList<>(); } int finalI = i; completableFutures.add(CompletableFuture.supplyAsync(() -> handleRequestsByMergedWarpMessage( - ((MergedWarpMessage)message).msgs.get(finalI), rpcContext))); + ((MergedWarpMessage) message).msgs.get(finalI), rpcContext))); } else { results.add(i, - handleRequestsByMergedWarpMessage(((MergedWarpMessage)message).msgs.get(i), rpcContext)); + handleRequestsByMergedWarpMessage(((MergedWarpMessage) message).msgs.get(i), rpcContext)); } } if (CollectionUtils.isNotEmpty(completableFutures)) { @@ -200,14 +200,14 @@ private void onRequestMessage(ChannelHandlerContext ctx, RpcMessage rpcMessage) final AbstractMessage msg = (AbstractMessage) message; if (LOGGER.isInfoEnabled()) { String receiveMsgLog = String.format("receive msg[single]: %s, clientIp: %s, vgroup: %s", message, - NetUtil.toIpAddress(ctx.channel().remoteAddress()), rpcContext.getTransactionServiceGroup()); + NetUtil.toIpAddress(ctx.channel().remoteAddress()), rpcContext.getTransactionServiceGroup()); BatchLogHandler.INSTANCE.writeLog(receiveMsgLog); } AbstractResultMessage result = transactionMessageHandler.onRequest(msg, rpcContext); remotingServer.sendAsyncResponse(rpcMessage, ctx.channel(), result); if (LOGGER.isInfoEnabled()) { String resultMsgLog = String.format("result msg[single]: %s, clientIp: %s, vgroup: %s", result, - NetUtil.toIpAddress(ctx.channel().remoteAddress()), rpcContext.getTransactionServiceGroup()); + NetUtil.toIpAddress(ctx.channel().remoteAddress()), rpcContext.getTransactionServiceGroup()); BatchLogHandler.INSTANCE.writeLog(resultMsgLog); } } @@ -229,7 +229,7 @@ private void offerMsg(BlockingQueue msgQueue, RpcMessage rpcMessage, AbstractResultMessage resultMessage, int msgId, Channel channel) { if (!msgQueue.offer(new QueueItem(resultMessage, msgId, rpcMessage))) { LOGGER.error("put message into basketMap offer failed, channel:{},rpcMessage:{},resultMessage:{}", - channel, rpcMessage, resultMessage); + channel, rpcMessage, resultMessage); } } @@ -261,14 +261,15 @@ public void run() { while (!msgQueue.isEmpty()) { QueueItem item = msgQueue.poll(); BatchResultMessage batchResultMessage = CollectionUtils.computeIfAbsent(batchResultMessageMap, - new ClientRequestRpcInfo(item.getRpcMessage()), - key -> new BatchResultMessage()); + new ClientRequestRpcInfo(item.getRpcMessage()), + key -> new BatchResultMessage()); batchResultMessage.getResultMessages().add(item.getResultMessage()); batchResultMessage.getMsgIds().add(item.getMsgId()); } + RpcContext rpcContext = ChannelManager.getContextFromIdentified(channel); batchResultMessageMap.forEach((clientRequestRpcInfo, batchResultMessage) -> - remotingServer.sendAsyncResponse(buildRpcMessage(clientRequestRpcInfo), - channel, batchResultMessage)); + remotingServer.sendAsyncResponse(buildRpcMessage(clientRequestRpcInfo, rpcContext.getVersion()), + channel, batchResultMessage)); }); isResponding = false; } @@ -277,18 +278,19 @@ public void run() { /** * handle rpc request message + * * @param rpcContext rpcContext */ private AbstractResultMessage handleRequestsByMergedWarpMessage(AbstractMessage subMessage, RpcContext rpcContext) { if (LOGGER.isInfoEnabled()) { String receiveMsgLog = String.format("receive msg[merged]: %s, clientIp: %s, vgroup: %s", subMessage, - NetUtil.toIpAddress(rpcContext.getChannel().remoteAddress()), rpcContext.getTransactionServiceGroup()); + NetUtil.toIpAddress(rpcContext.getChannel().remoteAddress()), rpcContext.getTransactionServiceGroup()); BatchLogHandler.INSTANCE.writeLog(receiveMsgLog); } AbstractResultMessage resultMessage = transactionMessageHandler.onRequest(subMessage, rpcContext); if (LOGGER.isInfoEnabled()) { String resultMsgLog = String.format("result msg[merged]: %s, clientIp: %s, vgroup: %s", resultMessage, - NetUtil.toIpAddress(rpcContext.getChannel().remoteAddress()), rpcContext.getTransactionServiceGroup()); + NetUtil.toIpAddress(rpcContext.getChannel().remoteAddress()), rpcContext.getTransactionServiceGroup()); BatchLogHandler.INSTANCE.writeLog(resultMsgLog); } return resultMessage; @@ -296,17 +298,18 @@ private AbstractResultMessage handleRequestsByMergedWarpMessage(AbstractMessage /** * handle rpc request message - * @param msg msg - * @param msgId msgId + * + * @param msg msg + * @param msgId msgId * @param rpcMessage rpcMessage - * @param ctx ctx + * @param ctx ctx * @param rpcContext rpcContext */ private void handleRequestsByMergedWarpMessageBy150(AbstractMessage msg, int msgId, RpcMessage rpcMessage, - ChannelHandlerContext ctx, RpcContext rpcContext) { + ChannelHandlerContext ctx, RpcContext rpcContext) { if (LOGGER.isInfoEnabled()) { String receiveMsgLog = String.format("receive msg[merged]: %s, clientIp: %s, vgroup: %s", msg, - NetUtil.toIpAddress(ctx.channel().remoteAddress()), rpcContext.getTransactionServiceGroup()); + NetUtil.toIpAddress(ctx.channel().remoteAddress()), rpcContext.getTransactionServiceGroup()); BatchLogHandler.INSTANCE.writeLog(receiveMsgLog); } AbstractResultMessage resultMessage = transactionMessageHandler.onRequest(msg, rpcContext); @@ -315,7 +318,7 @@ private void handleRequestsByMergedWarpMessageBy150(AbstractMessage msg, int msg notifyBatchRespondingThread(); if (LOGGER.isInfoEnabled()) { String resultMsgLog = String.format("result msg[merged]: %s, clientIp: %s, vgroup: %s", resultMessage, - NetUtil.toIpAddress(ctx.channel().remoteAddress()), rpcContext.getTransactionServiceGroup()); + NetUtil.toIpAddress(ctx.channel().remoteAddress()), rpcContext.getTransactionServiceGroup()); BatchLogHandler.INSTANCE.writeLog(resultMsgLog); } } @@ -324,14 +327,16 @@ private void handleRequestsByMergedWarpMessageBy150(AbstractMessage msg, int msg * build RpcMessage * * @param clientRequestRpcInfo For saving client request rpc info + * @param version version * @return rpcMessage */ - private RpcMessage buildRpcMessage(ClientRequestRpcInfo clientRequestRpcInfo) { + private RpcMessage buildRpcMessage(ClientRequestRpcInfo clientRequestRpcInfo, String version) { RpcMessage rpcMessage = new RpcMessage(); rpcMessage.setId(clientRequestRpcInfo.getRpcMessageId()); rpcMessage.setCodec(clientRequestRpcInfo.getCodec()); rpcMessage.setCompressor(clientRequestRpcInfo.getCompressor()); rpcMessage.setHeadMap(clientRequestRpcInfo.getHeadMap()); + rpcMessage.setSdkVersion(version); return rpcMessage; } @@ -413,7 +418,7 @@ public boolean equals(Object o) { } ClientRequestRpcInfo that = (ClientRequestRpcInfo) o; return rpcMessageId == that.rpcMessageId && codec == that.codec - && compressor == that.compressor && headMap.equals(that.headMap); + && compressor == that.compressor && headMap.equals(that.headMap); } @Override diff --git a/test/src/test/java/org/apache/seata/core/rpc/netty/mockserver/RmClientTest.java b/test/src/test/java/org/apache/seata/core/rpc/netty/mockserver/RmClientTest.java index 671f1d44312..c7f100bce74 100644 --- a/test/src/test/java/org/apache/seata/core/rpc/netty/mockserver/RmClientTest.java +++ b/test/src/test/java/org/apache/seata/core/rpc/netty/mockserver/RmClientTest.java @@ -17,8 +17,6 @@ package org.apache.seata.core.rpc.netty.mockserver; import io.netty.channel.Channel; -import org.apache.seata.common.ConfigurationKeys; -import org.apache.seata.common.ConfigurationTestHelper; import org.apache.seata.core.context.RootContext; import org.apache.seata.core.exception.TransactionException; import org.apache.seata.core.model.BranchStatus; @@ -27,13 +25,9 @@ import org.apache.seata.core.rpc.netty.ChannelManagerTestHelper; import org.apache.seata.core.rpc.netty.RmNettyRemotingClient; import org.apache.seata.integration.tx.api.interceptor.parser.DefaultResourceRegisterParser; -import org.apache.seata.mockserver.MockServer; import org.apache.seata.rm.DefaultResourceManager; import org.apache.seata.rm.RMClient; -import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/test/src/test/java/org/apache/seata/core/rpc/netty/mockserver/TmClientTest.java b/test/src/test/java/org/apache/seata/core/rpc/netty/mockserver/TmClientTest.java index b204b0da816..1bd2325a9df 100644 --- a/test/src/test/java/org/apache/seata/core/rpc/netty/mockserver/TmClientTest.java +++ b/test/src/test/java/org/apache/seata/core/rpc/netty/mockserver/TmClientTest.java @@ -17,21 +17,15 @@ package org.apache.seata.core.rpc.netty.mockserver; import io.netty.channel.Channel; -import org.apache.seata.common.ConfigurationKeys; -import org.apache.seata.common.ConfigurationTestHelper; import org.apache.seata.core.model.GlobalStatus; import org.apache.seata.core.model.TransactionManager; import org.apache.seata.core.protocol.ResultCode; import org.apache.seata.core.rpc.netty.ChannelManagerTestHelper; import org.apache.seata.core.rpc.netty.TmNettyRemotingClient; import org.apache.seata.mockserver.MockCoordinator; -import org.apache.seata.mockserver.MockServer; import org.apache.seata.tm.DefaultTransactionManager; import org.jetbrains.annotations.NotNull; -import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1Client.java b/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1Client.java index 048b3f1d0ba..3f52ed63c55 100644 --- a/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1Client.java +++ b/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1Client.java @@ -126,6 +126,7 @@ public Future sendRpc(Map head, Object body) { rpcMessage.setBody(body); rpcMessage.setMessageType(ProtocolConstants.MSGTYPE_RESQUEST_SYNC); + if (channel != null) { DefaultPromise promise = new DefaultPromise(defaultEventExecutor); futureMap.put(msgId, promise); From df9ea50e274d9572ffd164d7c42868d898914b58 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Thu, 7 Mar 2024 11:34:54 +0800 Subject: [PATCH 42/62] optimize version --- .../org/apache/seata/core/protocol/RpcMessage.java | 10 +++++----- .../seata/core/rpc/netty/AbstractNettyRemoting.java | 4 ++-- .../seata/core/rpc/netty/ProtocolRpcMessage.java | 4 ++-- .../seata/core/rpc/netty/v0/ProtocolRpcMessageV0.java | 4 ++-- .../seata/core/rpc/netty/v1/ProtocolRpcMessageV1.java | 4 ++-- .../rpc/processor/server/ServerOnRequestProcessor.java | 8 +++++--- 6 files changed, 18 insertions(+), 16 deletions(-) diff --git a/core/src/main/java/org/apache/seata/core/protocol/RpcMessage.java b/core/src/main/java/org/apache/seata/core/protocol/RpcMessage.java index 0424f7ac00c..fa80f814f5d 100644 --- a/core/src/main/java/org/apache/seata/core/protocol/RpcMessage.java +++ b/core/src/main/java/org/apache/seata/core/protocol/RpcMessage.java @@ -35,7 +35,7 @@ public class RpcMessage implements Serializable { private Map headMap = new HashMap<>(); private Object body; - private String version; + private String sdkVersion; /** * Gets id. @@ -171,12 +171,12 @@ public void setMessageType(byte messageType) { this.messageType = messageType; } - public String getVersion() { - return version; + public String getSdkVersion() { + return sdkVersion; } - public void setVersion(String version) { - this.version = version; + public void setSdkVersion(String sdkVersion) { + this.sdkVersion = sdkVersion; } @Override diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemoting.java b/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemoting.java index 332f04b3195..4e96ce4d5ae 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemoting.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemoting.java @@ -243,7 +243,7 @@ protected RpcMessage buildRequestMessage(Object msg, byte messageType, String ve rpcMessage.setCodec(ProtocolConstants.CONFIGURED_CODEC); rpcMessage.setCompressor(ProtocolConstants.CONFIGURED_COMPRESSOR); rpcMessage.setBody(msg); - rpcMessage.setVersion(version); + rpcMessage.setSdkVersion(version); return rpcMessage; } @@ -258,7 +258,7 @@ protected RpcMessage buildResponseMessage(RpcMessage rpcMessage, Object msg, byt rpcMsg.setCompressor(rpcMessage.getCompressor()); rpcMsg.setBody(msg); rpcMsg.setId(rpcMessage.getId()); - rpcMsg.setVersion(version); + rpcMsg.setSdkVersion(version); return rpcMsg; } diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/ProtocolRpcMessage.java b/core/src/main/java/org/apache/seata/core/rpc/netty/ProtocolRpcMessage.java index 277b727c8c4..1211ad56f10 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/ProtocolRpcMessage.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/ProtocolRpcMessage.java @@ -36,7 +36,7 @@ public interface ProtocolRpcMessage { */ void rpcMsg2ProtocolMsg(RpcMessage rpcMessage); - static String getVersion(Object body) { + static String getSdkVersion(Object body) { if (body instanceof AbstractIdentifyRequest) { return ((AbstractIdentifyRequest) body).getVersion(); } else { @@ -44,7 +44,7 @@ static String getVersion(Object body) { } } - static void setVersion(Object body, String version) { + static void setSdkVersion(Object body, String version) { if (body instanceof AbstractIdentifyRequest) { ((AbstractIdentifyRequest) body).setVersion(version); } diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolRpcMessageV0.java b/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolRpcMessageV0.java index 087f0e099aa..1ada24def10 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolRpcMessageV0.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolRpcMessageV0.java @@ -178,14 +178,14 @@ public RpcMessage protocolMsg2RpcMsg() { } rpcMessage.setBody(this.body); rpcMessage.setId((int) this.id); - rpcMessage.setVersion(ProtocolRpcMessage.getVersion(this.body)); + rpcMessage.setSdkVersion(ProtocolRpcMessage.getSdkVersion(this.body)); return rpcMessage; } @Override public void rpcMsg2ProtocolMsg(RpcMessage rpcMessage) { this.body = rpcMessage.getBody(); - ProtocolRpcMessage.setVersion(this.body, rpcMessage.getVersion()); + ProtocolRpcMessage.setSdkVersion(this.body, rpcMessage.getSdkVersion()); this.id = rpcMessage.getId(); this.isRequest = isRequest(rpcMessage.getMessageType()); this.isHeartbeat = isHeartbeat(rpcMessage.getMessageType()); diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolRpcMessageV1.java b/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolRpcMessageV1.java index 04621cfcda9..7e6668f4a01 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolRpcMessageV1.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolRpcMessageV1.java @@ -179,7 +179,7 @@ public RpcMessage protocolMsg2RpcMsg() { rpcMessage.setCompressor(this.compressor); rpcMessage.setHeadMap(this.headMap); rpcMessage.setBody(this.body); - rpcMessage.setVersion(ProtocolRpcMessage.getVersion(this.body)); + rpcMessage.setSdkVersion(ProtocolRpcMessage.getSdkVersion(this.body)); return rpcMessage; } @@ -187,7 +187,7 @@ public RpcMessage protocolMsg2RpcMsg() { @Override public void rpcMsg2ProtocolMsg(RpcMessage rpcMessage) { this.body = rpcMessage.getBody(); - ProtocolRpcMessage.setVersion(this.body, rpcMessage.getVersion()); + ProtocolRpcMessage.setSdkVersion(this.body, rpcMessage.getSdkVersion()); this.headMap = rpcMessage.getHeadMap(); this.id = rpcMessage.getId(); this.messageType = rpcMessage.getMessageType(); diff --git a/core/src/main/java/org/apache/seata/core/rpc/processor/server/ServerOnRequestProcessor.java b/core/src/main/java/org/apache/seata/core/rpc/processor/server/ServerOnRequestProcessor.java index 5431c19e630..985d8fd598b 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/processor/server/ServerOnRequestProcessor.java +++ b/core/src/main/java/org/apache/seata/core/rpc/processor/server/ServerOnRequestProcessor.java @@ -266,9 +266,10 @@ public void run() { batchResultMessage.getResultMessages().add(item.getResultMessage()); batchResultMessage.getMsgIds().add(item.getMsgId()); } + RpcContext rpcContext = ChannelManager.getContextFromIdentified(channel); batchResultMessageMap.forEach((clientRequestRpcInfo, batchResultMessage) -> - remotingServer.sendAsyncResponse(buildRpcMessage(clientRequestRpcInfo), - channel, batchResultMessage)); + remotingServer.sendAsyncResponse(buildRpcMessage(clientRequestRpcInfo, rpcContext.getVersion()), + channel, batchResultMessage)); }); isResponding = false; } @@ -326,12 +327,13 @@ private void handleRequestsByMergedWarpMessageBy150(AbstractMessage msg, int msg * @param clientRequestRpcInfo For saving client request rpc info * @return rpcMessage */ - private RpcMessage buildRpcMessage(ClientRequestRpcInfo clientRequestRpcInfo) { + private RpcMessage buildRpcMessage(ClientRequestRpcInfo clientRequestRpcInfo, String version) { RpcMessage rpcMessage = new RpcMessage(); rpcMessage.setId(clientRequestRpcInfo.getRpcMessageId()); rpcMessage.setCodec(clientRequestRpcInfo.getCodec()); rpcMessage.setCompressor(clientRequestRpcInfo.getCompressor()); rpcMessage.setHeadMap(clientRequestRpcInfo.getHeadMap()); + rpcMessage.setSdkVersion(version); return rpcMessage; } From af647e7897edcafe0bcc01de82b32d0e36078ca9 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Thu, 7 Mar 2024 11:36:40 +0800 Subject: [PATCH 43/62] optimize version --- .../server/ServerOnRequestProcessor.java | 55 +++++++++---------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/core/src/main/java/org/apache/seata/core/rpc/processor/server/ServerOnRequestProcessor.java b/core/src/main/java/org/apache/seata/core/rpc/processor/server/ServerOnRequestProcessor.java index aa28a643cb0..985d8fd598b 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/processor/server/ServerOnRequestProcessor.java +++ b/core/src/main/java/org/apache/seata/core/rpc/processor/server/ServerOnRequestProcessor.java @@ -101,17 +101,17 @@ public class ServerOnRequestProcessor implements RemotingProcessor, Disposable { private static final long KEEP_ALIVE_TIME = Integer.MAX_VALUE; private static final String BATCH_RESPONSE_THREAD_PREFIX = "rpcBatchResponse"; private static final boolean PARALLEL_REQUEST_HANDLE = - ConfigurationFactory.getInstance().getBoolean(ConfigurationKeys.ENABLE_PARALLEL_REQUEST_HANDLE_KEY, true); + ConfigurationFactory.getInstance().getBoolean(ConfigurationKeys.ENABLE_PARALLEL_REQUEST_HANDLE_KEY, true); public ServerOnRequestProcessor(RemotingServer remotingServer, TransactionMessageHandler transactionMessageHandler) { this.remotingServer = remotingServer; this.transactionMessageHandler = transactionMessageHandler; if (NettyServerConfig.isEnableTcServerBatchSendResponse()) { batchResponseExecutorService = new ThreadPoolExecutor(MAX_BATCH_RESPONSE_THREAD, - MAX_BATCH_RESPONSE_THREAD, - KEEP_ALIVE_TIME, TimeUnit.MILLISECONDS, - new LinkedBlockingQueue<>(), - new NamedThreadFactory(BATCH_RESPONSE_THREAD_PREFIX, MAX_BATCH_RESPONSE_THREAD)); + MAX_BATCH_RESPONSE_THREAD, + KEEP_ALIVE_TIME, TimeUnit.MILLISECONDS, + new LinkedBlockingQueue<>(), + new NamedThreadFactory(BATCH_RESPONSE_THREAD_PREFIX, MAX_BATCH_RESPONSE_THREAD)); batchResponseExecutorService.submit(new BatchResponseRunnable()); } } @@ -153,15 +153,15 @@ private void onRequestMessage(ChannelHandlerContext ctx, RpcMessage rpcMessage) // the batch send request message if (message instanceof MergedWarpMessage) { if (NettyServerConfig.isEnableTcServerBatchSendResponse() && StringUtils.isNotBlank(rpcContext.getVersion()) - && Version.isAboveOrEqualVersion150(rpcContext.getVersion())) { - List msgs = ((MergedWarpMessage) message).msgs; - List msgIds = ((MergedWarpMessage) message).msgIds; + && Version.isAboveOrEqualVersion150(rpcContext.getVersion())) { + List msgs = ((MergedWarpMessage)message).msgs; + List msgIds = ((MergedWarpMessage)message).msgIds; for (int i = 0; i < msgs.size(); i++) { AbstractMessage msg = msgs.get(i); int msgId = msgIds.get(i); if (PARALLEL_REQUEST_HANDLE) { CompletableFuture.runAsync( - () -> handleRequestsByMergedWarpMessageBy150(msg, msgId, rpcMessage, ctx, rpcContext)); + () -> handleRequestsByMergedWarpMessageBy150(msg, msgId, rpcMessage, ctx, rpcContext)); } else { handleRequestsByMergedWarpMessageBy150(msg, msgId, rpcMessage, ctx, rpcContext); } @@ -169,17 +169,17 @@ private void onRequestMessage(ChannelHandlerContext ctx, RpcMessage rpcMessage) } else { List results = new ArrayList<>(); List> completableFutures = null; - for (int i = 0; i < ((MergedWarpMessage) message).msgs.size(); i++) { + for (int i = 0; i < ((MergedWarpMessage)message).msgs.size(); i++) { if (PARALLEL_REQUEST_HANDLE) { if (completableFutures == null) { completableFutures = new ArrayList<>(); } int finalI = i; completableFutures.add(CompletableFuture.supplyAsync(() -> handleRequestsByMergedWarpMessage( - ((MergedWarpMessage) message).msgs.get(finalI), rpcContext))); + ((MergedWarpMessage)message).msgs.get(finalI), rpcContext))); } else { results.add(i, - handleRequestsByMergedWarpMessage(((MergedWarpMessage) message).msgs.get(i), rpcContext)); + handleRequestsByMergedWarpMessage(((MergedWarpMessage)message).msgs.get(i), rpcContext)); } } if (CollectionUtils.isNotEmpty(completableFutures)) { @@ -200,14 +200,14 @@ private void onRequestMessage(ChannelHandlerContext ctx, RpcMessage rpcMessage) final AbstractMessage msg = (AbstractMessage) message; if (LOGGER.isInfoEnabled()) { String receiveMsgLog = String.format("receive msg[single]: %s, clientIp: %s, vgroup: %s", message, - NetUtil.toIpAddress(ctx.channel().remoteAddress()), rpcContext.getTransactionServiceGroup()); + NetUtil.toIpAddress(ctx.channel().remoteAddress()), rpcContext.getTransactionServiceGroup()); BatchLogHandler.INSTANCE.writeLog(receiveMsgLog); } AbstractResultMessage result = transactionMessageHandler.onRequest(msg, rpcContext); remotingServer.sendAsyncResponse(rpcMessage, ctx.channel(), result); if (LOGGER.isInfoEnabled()) { String resultMsgLog = String.format("result msg[single]: %s, clientIp: %s, vgroup: %s", result, - NetUtil.toIpAddress(ctx.channel().remoteAddress()), rpcContext.getTransactionServiceGroup()); + NetUtil.toIpAddress(ctx.channel().remoteAddress()), rpcContext.getTransactionServiceGroup()); BatchLogHandler.INSTANCE.writeLog(resultMsgLog); } } @@ -229,7 +229,7 @@ private void offerMsg(BlockingQueue msgQueue, RpcMessage rpcMessage, AbstractResultMessage resultMessage, int msgId, Channel channel) { if (!msgQueue.offer(new QueueItem(resultMessage, msgId, rpcMessage))) { LOGGER.error("put message into basketMap offer failed, channel:{},rpcMessage:{},resultMessage:{}", - channel, rpcMessage, resultMessage); + channel, rpcMessage, resultMessage); } } @@ -261,8 +261,8 @@ public void run() { while (!msgQueue.isEmpty()) { QueueItem item = msgQueue.poll(); BatchResultMessage batchResultMessage = CollectionUtils.computeIfAbsent(batchResultMessageMap, - new ClientRequestRpcInfo(item.getRpcMessage()), - key -> new BatchResultMessage()); + new ClientRequestRpcInfo(item.getRpcMessage()), + key -> new BatchResultMessage()); batchResultMessage.getResultMessages().add(item.getResultMessage()); batchResultMessage.getMsgIds().add(item.getMsgId()); } @@ -278,19 +278,18 @@ public void run() { /** * handle rpc request message - * * @param rpcContext rpcContext */ private AbstractResultMessage handleRequestsByMergedWarpMessage(AbstractMessage subMessage, RpcContext rpcContext) { if (LOGGER.isInfoEnabled()) { String receiveMsgLog = String.format("receive msg[merged]: %s, clientIp: %s, vgroup: %s", subMessage, - NetUtil.toIpAddress(rpcContext.getChannel().remoteAddress()), rpcContext.getTransactionServiceGroup()); + NetUtil.toIpAddress(rpcContext.getChannel().remoteAddress()), rpcContext.getTransactionServiceGroup()); BatchLogHandler.INSTANCE.writeLog(receiveMsgLog); } AbstractResultMessage resultMessage = transactionMessageHandler.onRequest(subMessage, rpcContext); if (LOGGER.isInfoEnabled()) { String resultMsgLog = String.format("result msg[merged]: %s, clientIp: %s, vgroup: %s", resultMessage, - NetUtil.toIpAddress(rpcContext.getChannel().remoteAddress()), rpcContext.getTransactionServiceGroup()); + NetUtil.toIpAddress(rpcContext.getChannel().remoteAddress()), rpcContext.getTransactionServiceGroup()); BatchLogHandler.INSTANCE.writeLog(resultMsgLog); } return resultMessage; @@ -298,18 +297,17 @@ private AbstractResultMessage handleRequestsByMergedWarpMessage(AbstractMessage /** * handle rpc request message - * - * @param msg msg - * @param msgId msgId + * @param msg msg + * @param msgId msgId * @param rpcMessage rpcMessage - * @param ctx ctx + * @param ctx ctx * @param rpcContext rpcContext */ private void handleRequestsByMergedWarpMessageBy150(AbstractMessage msg, int msgId, RpcMessage rpcMessage, - ChannelHandlerContext ctx, RpcContext rpcContext) { + ChannelHandlerContext ctx, RpcContext rpcContext) { if (LOGGER.isInfoEnabled()) { String receiveMsgLog = String.format("receive msg[merged]: %s, clientIp: %s, vgroup: %s", msg, - NetUtil.toIpAddress(ctx.channel().remoteAddress()), rpcContext.getTransactionServiceGroup()); + NetUtil.toIpAddress(ctx.channel().remoteAddress()), rpcContext.getTransactionServiceGroup()); BatchLogHandler.INSTANCE.writeLog(receiveMsgLog); } AbstractResultMessage resultMessage = transactionMessageHandler.onRequest(msg, rpcContext); @@ -318,7 +316,7 @@ private void handleRequestsByMergedWarpMessageBy150(AbstractMessage msg, int msg notifyBatchRespondingThread(); if (LOGGER.isInfoEnabled()) { String resultMsgLog = String.format("result msg[merged]: %s, clientIp: %s, vgroup: %s", resultMessage, - NetUtil.toIpAddress(ctx.channel().remoteAddress()), rpcContext.getTransactionServiceGroup()); + NetUtil.toIpAddress(ctx.channel().remoteAddress()), rpcContext.getTransactionServiceGroup()); BatchLogHandler.INSTANCE.writeLog(resultMsgLog); } } @@ -327,7 +325,6 @@ private void handleRequestsByMergedWarpMessageBy150(AbstractMessage msg, int msg * build RpcMessage * * @param clientRequestRpcInfo For saving client request rpc info - * @param version version * @return rpcMessage */ private RpcMessage buildRpcMessage(ClientRequestRpcInfo clientRequestRpcInfo, String version) { @@ -418,7 +415,7 @@ public boolean equals(Object o) { } ClientRequestRpcInfo that = (ClientRequestRpcInfo) o; return rpcMessageId == that.rpcMessageId && codec == that.codec - && compressor == that.compressor && headMap.equals(that.headMap); + && compressor == that.compressor && headMap.equals(that.headMap); } @Override From 3841379e2da6ad6dbdb16c30d775a4a6d3e8bf1e Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Fri, 8 Mar 2024 19:20:04 +0800 Subject: [PATCH 44/62] optimize version --- .../apache/seata/core/protocol/RpcMessage.java | 10 ++++------ .../core/rpc/netty/AbstractNettyRemoting.java | 11 ----------- .../rpc/netty/AbstractNettyRemotingServer.java | 14 ++++---------- .../seata/core/rpc/netty/ProtocolRpcMessage.java | 15 --------------- .../core/rpc/netty/v0/ProtocolRpcMessageV0.java | 2 -- .../core/rpc/netty/v1/ProtocolRpcMessageV1.java | 2 -- .../core/rpc/netty/v1/ProtocolV1Decoder.java | 1 + .../server/ServerOnRequestProcessor.java | 8 +++----- 8 files changed, 12 insertions(+), 51 deletions(-) diff --git a/core/src/main/java/org/apache/seata/core/protocol/RpcMessage.java b/core/src/main/java/org/apache/seata/core/protocol/RpcMessage.java index fa80f814f5d..d96822c4bb9 100644 --- a/core/src/main/java/org/apache/seata/core/protocol/RpcMessage.java +++ b/core/src/main/java/org/apache/seata/core/protocol/RpcMessage.java @@ -35,7 +35,6 @@ public class RpcMessage implements Serializable { private Map headMap = new HashMap<>(); private Object body; - private String sdkVersion; /** * Gets id. @@ -172,11 +171,10 @@ public void setMessageType(byte messageType) { } public String getSdkVersion() { - return sdkVersion; - } - - public void setSdkVersion(String sdkVersion) { - this.sdkVersion = sdkVersion; + if (body instanceof AbstractIdentifyRequest) { + return ((AbstractIdentifyRequest) body).getVersion(); + } + return ""; } @Override diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemoting.java b/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemoting.java index 4e96ce4d5ae..3d09108ac43 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemoting.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemoting.java @@ -47,7 +47,6 @@ import org.apache.seata.core.rpc.hook.RpcHook; import org.apache.seata.core.rpc.processor.Pair; import org.apache.seata.core.rpc.processor.RemotingProcessor; -import org.apache.seata.core.protocol.Version; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.MDC; @@ -233,32 +232,22 @@ protected void sendAsync(Channel channel, RpcMessage rpcMessage) { } protected RpcMessage buildRequestMessage(Object msg, byte messageType) { - return buildRequestMessage(msg, messageType, Version.getCurrent()); - } - - protected RpcMessage buildRequestMessage(Object msg, byte messageType, String version) { RpcMessage rpcMessage = new RpcMessage(); rpcMessage.setId(getNextMessageId()); rpcMessage.setMessageType(messageType); rpcMessage.setCodec(ProtocolConstants.CONFIGURED_CODEC); rpcMessage.setCompressor(ProtocolConstants.CONFIGURED_COMPRESSOR); rpcMessage.setBody(msg); - rpcMessage.setSdkVersion(version); return rpcMessage; } protected RpcMessage buildResponseMessage(RpcMessage rpcMessage, Object msg, byte messageType) { - return buildResponseMessage(rpcMessage, msg, messageType, Version.getCurrent()); - } - - protected RpcMessage buildResponseMessage(RpcMessage rpcMessage, Object msg, byte messageType, String version) { RpcMessage rpcMsg = new RpcMessage(); rpcMsg.setMessageType(messageType); rpcMsg.setCodec(rpcMessage.getCodec()); // same with request rpcMsg.setCompressor(rpcMessage.getCompressor()); rpcMsg.setBody(msg); rpcMsg.setId(rpcMessage.getId()); - rpcMsg.setSdkVersion(version); return rpcMsg; } diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingServer.java b/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingServer.java index 528397e9406..0a0cd34562a 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingServer.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingServer.java @@ -69,20 +69,16 @@ public Object sendSyncRequest(String resourceId, String clientId, Object msg, bo if (channel == null) { throw new RuntimeException("rm client is not connected. dbkey:" + resourceId + ",clientId:" + clientId); } - RpcContext rpcContext = ChannelManager.getContextFromIdentified(channel); - RpcMessage rpcMessage = buildRequestMessage(msg, ProtocolConstants.MSGTYPE_RESQUEST_SYNC, rpcContext.getVersion()); + RpcMessage rpcMessage = buildRequestMessage(msg, ProtocolConstants.MSGTYPE_RESQUEST_SYNC); return super.sendSync(channel, rpcMessage, NettyServerConfig.getRpcRequestTimeout()); } - - @Override public Object sendSyncRequest(Channel channel, Object msg) throws TimeoutException { if (channel == null) { throw new RuntimeException("client is not connected"); } - RpcContext rpcContext = ChannelManager.getContextFromIdentified(channel); - RpcMessage rpcMessage = buildRequestMessage(msg, ProtocolConstants.MSGTYPE_RESQUEST_SYNC, rpcContext.getVersion()); + RpcMessage rpcMessage = buildRequestMessage(msg, ProtocolConstants.MSGTYPE_RESQUEST_SYNC); return super.sendSync(channel, rpcMessage, NettyServerConfig.getRpcRequestTimeout()); } @@ -91,8 +87,7 @@ public void sendAsyncRequest(Channel channel, Object msg) { if (channel == null) { throw new RuntimeException("client is not connected"); } - RpcContext rpcContext = ChannelManager.getContextFromIdentified(channel); - RpcMessage rpcMessage = buildRequestMessage(msg, ProtocolConstants.MSGTYPE_RESQUEST_ONEWAY, rpcContext.getVersion()); + RpcMessage rpcMessage = buildRequestMessage(msg, ProtocolConstants.MSGTYPE_RESQUEST_ONEWAY); super.sendAsync(channel, rpcMessage); } @@ -103,10 +98,9 @@ public void sendAsyncResponse(RpcMessage rpcMessage, Channel channel, Object msg clientChannel = ChannelManager.getSameClientChannel(channel); } if (clientChannel != null) { - RpcContext rpcContext = ChannelManager.getContextFromIdentified(channel); RpcMessage rpcMsg = buildResponseMessage(rpcMessage, msg, msg instanceof HeartbeatMessage ? ProtocolConstants.MSGTYPE_HEARTBEAT_RESPONSE - : ProtocolConstants.MSGTYPE_RESPONSE, rpcContext.getVersion()); + : ProtocolConstants.MSGTYPE_RESPONSE); super.sendAsync(clientChannel, rpcMsg); } else { throw new RuntimeException("channel is error."); diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/ProtocolRpcMessage.java b/core/src/main/java/org/apache/seata/core/rpc/netty/ProtocolRpcMessage.java index 1211ad56f10..ba89c508a8c 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/ProtocolRpcMessage.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/ProtocolRpcMessage.java @@ -16,7 +16,6 @@ */ package org.apache.seata.core.rpc.netty; -import org.apache.seata.core.protocol.AbstractIdentifyRequest; import org.apache.seata.core.protocol.RpcMessage; /** @@ -35,18 +34,4 @@ public interface ProtocolRpcMessage { * @param rpcMessage */ void rpcMsg2ProtocolMsg(RpcMessage rpcMessage); - - static String getSdkVersion(Object body) { - if (body instanceof AbstractIdentifyRequest) { - return ((AbstractIdentifyRequest) body).getVersion(); - } else { - return null; - } - } - - static void setSdkVersion(Object body, String version) { - if (body instanceof AbstractIdentifyRequest) { - ((AbstractIdentifyRequest) body).setVersion(version); - } - } } diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolRpcMessageV0.java b/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolRpcMessageV0.java index 1ada24def10..cea2d7e6f71 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolRpcMessageV0.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolRpcMessageV0.java @@ -178,14 +178,12 @@ public RpcMessage protocolMsg2RpcMsg() { } rpcMessage.setBody(this.body); rpcMessage.setId((int) this.id); - rpcMessage.setSdkVersion(ProtocolRpcMessage.getSdkVersion(this.body)); return rpcMessage; } @Override public void rpcMsg2ProtocolMsg(RpcMessage rpcMessage) { this.body = rpcMessage.getBody(); - ProtocolRpcMessage.setSdkVersion(this.body, rpcMessage.getSdkVersion()); this.id = rpcMessage.getId(); this.isRequest = isRequest(rpcMessage.getMessageType()); this.isHeartbeat = isHeartbeat(rpcMessage.getMessageType()); diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolRpcMessageV1.java b/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolRpcMessageV1.java index 7e6668f4a01..10668cbdc65 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolRpcMessageV1.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolRpcMessageV1.java @@ -179,7 +179,6 @@ public RpcMessage protocolMsg2RpcMsg() { rpcMessage.setCompressor(this.compressor); rpcMessage.setHeadMap(this.headMap); rpcMessage.setBody(this.body); - rpcMessage.setSdkVersion(ProtocolRpcMessage.getSdkVersion(this.body)); return rpcMessage; } @@ -187,7 +186,6 @@ public RpcMessage protocolMsg2RpcMsg() { @Override public void rpcMsg2ProtocolMsg(RpcMessage rpcMessage) { this.body = rpcMessage.getBody(); - ProtocolRpcMessage.setSdkVersion(this.body, rpcMessage.getSdkVersion()); this.headMap = rpcMessage.getHeadMap(); this.id = rpcMessage.getId(); this.messageType = rpcMessage.getMessageType(); diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1Decoder.java b/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1Decoder.java index 1d603dd4610..429e0576e3c 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1Decoder.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1Decoder.java @@ -27,6 +27,7 @@ import org.apache.seata.core.compressor.CompressorFactory; import org.apache.seata.core.constants.ConfigurationKeys; import org.apache.seata.core.exception.DecodeException; +import org.apache.seata.core.protocol.AbstractIdentifyRequest; import org.apache.seata.core.protocol.HeartbeatMessage; import org.apache.seata.core.protocol.ProtocolConstants; import org.apache.seata.core.serializer.Serializer; diff --git a/core/src/main/java/org/apache/seata/core/rpc/processor/server/ServerOnRequestProcessor.java b/core/src/main/java/org/apache/seata/core/rpc/processor/server/ServerOnRequestProcessor.java index 985d8fd598b..5431c19e630 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/processor/server/ServerOnRequestProcessor.java +++ b/core/src/main/java/org/apache/seata/core/rpc/processor/server/ServerOnRequestProcessor.java @@ -266,10 +266,9 @@ public void run() { batchResultMessage.getResultMessages().add(item.getResultMessage()); batchResultMessage.getMsgIds().add(item.getMsgId()); } - RpcContext rpcContext = ChannelManager.getContextFromIdentified(channel); batchResultMessageMap.forEach((clientRequestRpcInfo, batchResultMessage) -> - remotingServer.sendAsyncResponse(buildRpcMessage(clientRequestRpcInfo, rpcContext.getVersion()), - channel, batchResultMessage)); + remotingServer.sendAsyncResponse(buildRpcMessage(clientRequestRpcInfo), + channel, batchResultMessage)); }); isResponding = false; } @@ -327,13 +326,12 @@ private void handleRequestsByMergedWarpMessageBy150(AbstractMessage msg, int msg * @param clientRequestRpcInfo For saving client request rpc info * @return rpcMessage */ - private RpcMessage buildRpcMessage(ClientRequestRpcInfo clientRequestRpcInfo, String version) { + private RpcMessage buildRpcMessage(ClientRequestRpcInfo clientRequestRpcInfo) { RpcMessage rpcMessage = new RpcMessage(); rpcMessage.setId(clientRequestRpcInfo.getRpcMessageId()); rpcMessage.setCodec(clientRequestRpcInfo.getCodec()); rpcMessage.setCompressor(clientRequestRpcInfo.getCompressor()); rpcMessage.setHeadMap(clientRequestRpcInfo.getHeadMap()); - rpcMessage.setSdkVersion(version); return rpcMessage; } From d876d1037a2952b7f82a4cb5c4f986605cfcefeb Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Fri, 8 Mar 2024 19:22:07 +0800 Subject: [PATCH 45/62] optimize version --- .../src/main/java/org/apache/seata/core/protocol/RpcMessage.java | 1 - 1 file changed, 1 deletion(-) diff --git a/core/src/main/java/org/apache/seata/core/protocol/RpcMessage.java b/core/src/main/java/org/apache/seata/core/protocol/RpcMessage.java index d96822c4bb9..4ceabf80c2b 100644 --- a/core/src/main/java/org/apache/seata/core/protocol/RpcMessage.java +++ b/core/src/main/java/org/apache/seata/core/protocol/RpcMessage.java @@ -35,7 +35,6 @@ public class RpcMessage implements Serializable { private Map headMap = new HashMap<>(); private Object body; - /** * Gets id. * From 6f8c019625e8998767dd014e5ff25aed15417e14 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Fri, 8 Mar 2024 19:26:22 +0800 Subject: [PATCH 46/62] optimize version --- .../seata/core/rpc/netty/CompatibleProtocolEncoder.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolEncoder.java b/core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolEncoder.java index 8c9d790bbc6..e7bee35e79a 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolEncoder.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolEncoder.java @@ -20,6 +20,7 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToByteEncoder; +import org.apache.seata.common.util.StringUtils; import org.apache.seata.core.protocol.ProtocolConstants; import org.apache.seata.core.protocol.RpcMessage; import org.apache.seata.core.protocol.Version; @@ -59,7 +60,9 @@ public void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) { if (msg instanceof RpcMessage) { RpcMessage rpcMessage = (RpcMessage) msg; String sdkVersion = rpcMessage.getSdkVersion(); - //todo null? + if(StringUtils.isBlank(sdkVersion)){ + sdkVersion = Version.getCurrent(); + } byte protocolVersion = Version.calcProtocolVersion(sdkVersion); ProtocolEncoder encoder = protocolEncoderMap.get(protocolVersion); if (encoder == null) { From c0dfb5172bc9e9e43e0257ca833cd538b192aa40 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Fri, 8 Mar 2024 19:26:59 +0800 Subject: [PATCH 47/62] optimize version --- .../java/org/apache/seata/core/protocol/RpcMessage.java | 7 ------- 1 file changed, 7 deletions(-) diff --git a/core/src/main/java/org/apache/seata/core/protocol/RpcMessage.java b/core/src/main/java/org/apache/seata/core/protocol/RpcMessage.java index 4ceabf80c2b..4f0963b20f7 100644 --- a/core/src/main/java/org/apache/seata/core/protocol/RpcMessage.java +++ b/core/src/main/java/org/apache/seata/core/protocol/RpcMessage.java @@ -169,13 +169,6 @@ public void setMessageType(byte messageType) { this.messageType = messageType; } - public String getSdkVersion() { - if (body instanceof AbstractIdentifyRequest) { - return ((AbstractIdentifyRequest) body).getVersion(); - } - return ""; - } - @Override public String toString() { return StringUtils.toString(this); From b5bb8be5f62333dde593c216c591a63008b0ede4 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Fri, 8 Mar 2024 19:51:33 +0800 Subject: [PATCH 48/62] optimize version --- .../seata/core/protocol/RpcMessage.java | 10 ++++++++ .../netty/AbstractNettyRemotingServer.java | 24 +++++++++++++++---- .../rpc/netty/CompatibleProtocolEncoder.java | 3 ++- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/org/apache/seata/core/protocol/RpcMessage.java b/core/src/main/java/org/apache/seata/core/protocol/RpcMessage.java index 4f0963b20f7..6a7e4d9da50 100644 --- a/core/src/main/java/org/apache/seata/core/protocol/RpcMessage.java +++ b/core/src/main/java/org/apache/seata/core/protocol/RpcMessage.java @@ -35,6 +35,8 @@ public class RpcMessage implements Serializable { private Map headMap = new HashMap<>(); private Object body; + private String otherSideVersion; + /** * Gets id. * @@ -169,6 +171,14 @@ public void setMessageType(byte messageType) { this.messageType = messageType; } + public String getOtherSideVersion() { + return otherSideVersion; + } + + public void setOtherSideVersion(String otherSideVersion) { + this.otherSideVersion = otherSideVersion; + } + @Override public String toString() { return StringUtils.toString(this); diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingServer.java b/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingServer.java index 0a0cd34562a..9be9e79c3b2 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingServer.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingServer.java @@ -69,7 +69,7 @@ public Object sendSyncRequest(String resourceId, String clientId, Object msg, bo if (channel == null) { throw new RuntimeException("rm client is not connected. dbkey:" + resourceId + ",clientId:" + clientId); } - RpcMessage rpcMessage = buildRequestMessage(msg, ProtocolConstants.MSGTYPE_RESQUEST_SYNC); + RpcMessage rpcMessage = buildRequestMessage(channel, msg, ProtocolConstants.MSGTYPE_RESQUEST_SYNC); return super.sendSync(channel, rpcMessage, NettyServerConfig.getRpcRequestTimeout()); } @@ -78,7 +78,7 @@ public Object sendSyncRequest(Channel channel, Object msg) throws TimeoutExcepti if (channel == null) { throw new RuntimeException("client is not connected"); } - RpcMessage rpcMessage = buildRequestMessage(msg, ProtocolConstants.MSGTYPE_RESQUEST_SYNC); + RpcMessage rpcMessage = buildRequestMessage(channel, msg, ProtocolConstants.MSGTYPE_RESQUEST_SYNC); return super.sendSync(channel, rpcMessage, NettyServerConfig.getRpcRequestTimeout()); } @@ -87,7 +87,7 @@ public void sendAsyncRequest(Channel channel, Object msg) { if (channel == null) { throw new RuntimeException("client is not connected"); } - RpcMessage rpcMessage = buildRequestMessage(msg, ProtocolConstants.MSGTYPE_RESQUEST_ONEWAY); + RpcMessage rpcMessage = buildRequestMessage(channel, msg, ProtocolConstants.MSGTYPE_RESQUEST_ONEWAY); super.sendAsync(channel, rpcMessage); } @@ -98,7 +98,7 @@ public void sendAsyncResponse(RpcMessage rpcMessage, Channel channel, Object msg clientChannel = ChannelManager.getSameClientChannel(channel); } if (clientChannel != null) { - RpcMessage rpcMsg = buildResponseMessage(rpcMessage, msg, msg instanceof HeartbeatMessage + RpcMessage rpcMsg = buildResponseMessage(channel, rpcMessage, msg, msg instanceof HeartbeatMessage ? ProtocolConstants.MSGTYPE_HEARTBEAT_RESPONSE : ProtocolConstants.MSGTYPE_RESPONSE); super.sendAsync(clientChannel, rpcMsg); @@ -107,6 +107,22 @@ public void sendAsyncResponse(RpcMessage rpcMessage, Channel channel, Object msg } } + + private RpcMessage buildResponseMessage(Channel channel, RpcMessage fromRpcMessage, Object msg, byte messageType) { + RpcMessage rpcMessage = super.buildResponseMessage(fromRpcMessage, msg, messageType); + RpcContext rpcContext = ChannelManager.getContextFromIdentified(channel); + rpcMessage.setOtherSideVersion(rpcContext.getVersion()); + return rpcMessage; + } + + protected RpcMessage buildRequestMessage(Channel channel, Object msg, byte messageType) { + RpcMessage rpcMessage = super.buildRequestMessage(msg, messageType); + RpcContext rpcContext = ChannelManager.getContextFromIdentified(channel); + rpcMessage.setOtherSideVersion(rpcContext.getVersion()); + return rpcMessage; + } + + @Override public void registerProcessor(int messageType, RemotingProcessor processor, ExecutorService executor) { Pair pair = new Pair<>(processor, executor); diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolEncoder.java b/core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolEncoder.java index e7bee35e79a..eb0a162cd88 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolEncoder.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolEncoder.java @@ -21,6 +21,7 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToByteEncoder; import org.apache.seata.common.util.StringUtils; +import org.apache.seata.core.protocol.AbstractIdentifyRequest; import org.apache.seata.core.protocol.ProtocolConstants; import org.apache.seata.core.protocol.RpcMessage; import org.apache.seata.core.protocol.Version; @@ -59,7 +60,7 @@ public void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) { try { if (msg instanceof RpcMessage) { RpcMessage rpcMessage = (RpcMessage) msg; - String sdkVersion = rpcMessage.getSdkVersion(); + String sdkVersion = rpcMessage.getOtherSideVersion(); if(StringUtils.isBlank(sdkVersion)){ sdkVersion = Version.getCurrent(); } From 2f5feb0552dc5eebdcea8d55db83b4664f33fa76 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Tue, 30 Apr 2024 10:03:23 +0800 Subject: [PATCH 49/62] merge --- .../core/serializer/SerializerServiceLoader.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/org/apache/seata/core/serializer/SerializerServiceLoader.java b/core/src/main/java/org/apache/seata/core/serializer/SerializerServiceLoader.java index 83c74a2053b..0ae7ac8a679 100644 --- a/core/src/main/java/org/apache/seata/core/serializer/SerializerServiceLoader.java +++ b/core/src/main/java/org/apache/seata/core/serializer/SerializerServiceLoader.java @@ -87,6 +87,13 @@ public static Serializer load(SerializerType type, byte version) throws Enhanced return serializer; } + private static String serialzerKey(SerializerType type, byte version) { + if (type == SerializerType.SEATA) { + return type.name() + version; + } + return type.name(); + } + public static Set getSupportedSerializers() { Set supportedSerializers = new HashSet<>(); @@ -104,11 +111,4 @@ public static Set getSupportedSerializers() { return supportedSerializers; } - - private static String serialzerKey(SerializerType type, byte version) { - if (type == SerializerType.SEATA) { - return type.name() + version; - } - return type.name(); - } } \ No newline at end of file From 0ebf2ce4f13afa44a561675eaccb9d9899c8cf16 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Tue, 30 Apr 2024 10:17:04 +0800 Subject: [PATCH 50/62] merge --- .../core/rpc/netty/v1/ProtocolDecoderV1.java | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolDecoderV1.java b/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolDecoderV1.java index 4a01b8574e3..25a8a3a2b05 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolDecoderV1.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolDecoderV1.java @@ -16,6 +16,9 @@ */ package org.apache.seata.core.rpc.netty.v1; +import java.util.Map; +import java.util.Set; + import io.netty.buffer.ByteBuf; import org.apache.seata.core.compressor.Compressor; import org.apache.seata.core.compressor.CompressorFactory; @@ -29,7 +32,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Map; /** *
    @@ -62,6 +64,13 @@
     public class ProtocolDecoderV1 implements ProtocolDecoder {
     
         private static final Logger LOGGER = LoggerFactory.getLogger(ProtocolDecoderV1.class);
    +    private final Set supportDeSerializerTypes;
    +
    +    public ProtocolDecoderV1() {
    +        supportDeSerializerTypes = SerializerServiceLoader.getSupportedSerializers();
    +        if (supportDeSerializerTypes.isEmpty()) {
    +            throw new IllegalArgumentException("No serializer found");
    +        }    }
     
         @Override
         public ProtocolRpcMessage decodeFrame(ByteBuf frame) {
    @@ -106,8 +115,13 @@ public ProtocolRpcMessage decodeFrame(ByteBuf frame) {
                     frame.readBytes(bs);
                     Compressor compressor = CompressorFactory.getCompressor(compressorType);
                     bs = compressor.decompress(bs);
    -                Serializer serializer = SerializerServiceLoader.load(SerializerType.getByCode(rpcMessage.getCodec()), version);
    -                rpcMessage.setBody(serializer.deserialize(bs));
    +                SerializerType protocolType = SerializerType.getByCode(rpcMessage.getCodec());
    +                if (this.supportDeSerializerTypes.contains(protocolType)) {
    +                    Serializer serializer = SerializerServiceLoader.load(protocolType, ProtocolConstants.VERSION_1);
    +                    rpcMessage.setBody(serializer.deserialize(bs));
    +                } else {
    +                    throw new IllegalArgumentException("SerializerType not match");
    +                }
                 }
             }
     
    
    From 7add8d907c1822db31fdd04a84fbe2eb013b3156 Mon Sep 17 00:00:00 2001
    From: "minghua.xie" 
    Date: Tue, 30 Apr 2024 11:42:28 +0800
    Subject: [PATCH 51/62] check style
    
    ---
     .../seata/core/rpc/netty/CompatibleProtocolEncoder.java       | 4 +---
     1 file changed, 1 insertion(+), 3 deletions(-)
    
    diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolEncoder.java b/core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolEncoder.java
    index eb0a162cd88..e588b92b8ea 100644
    --- a/core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolEncoder.java
    +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolEncoder.java
    @@ -21,7 +21,6 @@
     import io.netty.channel.ChannelHandlerContext;
     import io.netty.handler.codec.MessageToByteEncoder;
     import org.apache.seata.common.util.StringUtils;
    -import org.apache.seata.core.protocol.AbstractIdentifyRequest;
     import org.apache.seata.core.protocol.ProtocolConstants;
     import org.apache.seata.core.protocol.RpcMessage;
     import org.apache.seata.core.protocol.Version;
    @@ -39,7 +38,6 @@
      * 
  • Head Length: include head data from magic code to head map.
  • *
  • Body Length: Full Length - Head Length
  • *

    - * */ public class CompatibleProtocolEncoder extends MessageToByteEncoder { @@ -61,7 +59,7 @@ public void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) { if (msg instanceof RpcMessage) { RpcMessage rpcMessage = (RpcMessage) msg; String sdkVersion = rpcMessage.getOtherSideVersion(); - if(StringUtils.isBlank(sdkVersion)){ + if (StringUtils.isBlank(sdkVersion)) { sdkVersion = Version.getCurrent(); } byte protocolVersion = Version.calcProtocolVersion(sdkVersion); From aeb24c8023fa358e717d84420ef5fb31cca37ff0 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Tue, 30 Apr 2024 11:49:41 +0800 Subject: [PATCH 52/62] check style --- .../apache/seata/core/rpc/processor/server/RegTmProcessor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/org/apache/seata/core/rpc/processor/server/RegTmProcessor.java b/core/src/main/java/org/apache/seata/core/rpc/processor/server/RegTmProcessor.java index b63ae2d84bc..6090232c6c4 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/processor/server/RegTmProcessor.java +++ b/core/src/main/java/org/apache/seata/core/rpc/processor/server/RegTmProcessor.java @@ -90,7 +90,7 @@ private void onRegTmMessage(ChannelHandlerContext ctx, RpcMessage rpcMessage) { remotingServer.sendAsyncResponse(rpcMessage, ctx.channel(), response); if (isSuccess && LOGGER.isInfoEnabled()) { LOGGER.info("TM register success,message:{},channel:{},client version:{},client protocol-version:{}" - , message, ctx.channel(), message.getVersion(), rpcMessage.getSdkVersion()); + , message, ctx.channel(), message.getVersion(), rpcMessage.getOtherSideVersion()); } } From 237aeb5713f71d37ff2b77f49af543f6f1273409 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Mon, 24 Jun 2024 12:01:01 +0800 Subject: [PATCH 53/62] style --- .../org/apache/seata/core/rpc/netty/v1/ProtocolDecoderV1.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolDecoderV1.java b/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolDecoderV1.java index 25a8a3a2b05..9ca49779443 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolDecoderV1.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolDecoderV1.java @@ -16,8 +16,8 @@ */ package org.apache.seata.core.rpc.netty.v1; +import java.util.List; import java.util.Map; -import java.util.Set; import io.netty.buffer.ByteBuf; import org.apache.seata.core.compressor.Compressor; @@ -64,7 +64,7 @@ public class ProtocolDecoderV1 implements ProtocolDecoder { private static final Logger LOGGER = LoggerFactory.getLogger(ProtocolDecoderV1.class); - private final Set supportDeSerializerTypes; + private final List supportDeSerializerTypes; public ProtocolDecoderV1() { supportDeSerializerTypes = SerializerServiceLoader.getSupportedSerializers(); From 15dc0d6e67dd77d8e17f70d79250fb84f3139f5a Mon Sep 17 00:00:00 2001 From: jianbin Date: Thu, 27 Jun 2024 10:57:29 +0800 Subject: [PATCH 54/62] optimize: select channel handles based on protocol versions --- .../seata/core/protocol/RpcMessage.java | 10 --- .../netty/AbstractNettyRemotingServer.java | 10 +-- .../rpc/netty/AbstractProtocolDecoder.java | 47 +++++++++++ .../rpc/netty/CompatibleProtocolEncoder.java | 79 ------------------- ...Decoder.java => MultiProtocolDecoder.java} | 42 +++++++--- .../core/rpc/netty/NettyClientBootstrap.java | 6 +- .../core/rpc/netty/NettyServerBootstrap.java | 8 +- .../core/rpc/netty/v0/ProtocolDecoderV0.java | 8 +- .../core/rpc/netty/v0/ProtocolEncoderV0.java | 17 +++- .../core/rpc/netty/v1/ProtocolDecoderV1.java | 37 ++++++++- .../core/rpc/netty/v1/ProtocolEncoderV1.java | 18 ++++- .../rpc/processor/server/RegTmProcessor.java | 4 +- .../core/rpc/netty/v1/ProtocolV1Client.java | 7 +- .../core/rpc/netty/v1/ProtocolV1Server.java | 11 +-- 14 files changed, 168 insertions(+), 136 deletions(-) create mode 100644 core/src/main/java/org/apache/seata/core/rpc/netty/AbstractProtocolDecoder.java delete mode 100644 core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolEncoder.java rename core/src/main/java/org/apache/seata/core/rpc/netty/{CompatibleProtocolDecoder.java => MultiProtocolDecoder.java} (77%) diff --git a/core/src/main/java/org/apache/seata/core/protocol/RpcMessage.java b/core/src/main/java/org/apache/seata/core/protocol/RpcMessage.java index 6a7e4d9da50..4f0963b20f7 100644 --- a/core/src/main/java/org/apache/seata/core/protocol/RpcMessage.java +++ b/core/src/main/java/org/apache/seata/core/protocol/RpcMessage.java @@ -35,8 +35,6 @@ public class RpcMessage implements Serializable { private Map headMap = new HashMap<>(); private Object body; - private String otherSideVersion; - /** * Gets id. * @@ -171,14 +169,6 @@ public void setMessageType(byte messageType) { this.messageType = messageType; } - public String getOtherSideVersion() { - return otherSideVersion; - } - - public void setOtherSideVersion(String otherSideVersion) { - this.otherSideVersion = otherSideVersion; - } - @Override public String toString() { return StringUtils.toString(this); diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingServer.java b/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingServer.java index 9be9e79c3b2..5ca26300c90 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingServer.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingServer.java @@ -109,17 +109,11 @@ public void sendAsyncResponse(RpcMessage rpcMessage, Channel channel, Object msg private RpcMessage buildResponseMessage(Channel channel, RpcMessage fromRpcMessage, Object msg, byte messageType) { - RpcMessage rpcMessage = super.buildResponseMessage(fromRpcMessage, msg, messageType); - RpcContext rpcContext = ChannelManager.getContextFromIdentified(channel); - rpcMessage.setOtherSideVersion(rpcContext.getVersion()); - return rpcMessage; + return super.buildResponseMessage(fromRpcMessage, msg, messageType); } protected RpcMessage buildRequestMessage(Channel channel, Object msg, byte messageType) { - RpcMessage rpcMessage = super.buildRequestMessage(msg, messageType); - RpcContext rpcContext = ChannelManager.getContextFromIdentified(channel); - rpcMessage.setOtherSideVersion(rpcContext.getVersion()); - return rpcMessage; + return super.buildRequestMessage(msg, messageType); } diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractProtocolDecoder.java b/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractProtocolDecoder.java new file mode 100644 index 00000000000..b1c238089e9 --- /dev/null +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractProtocolDecoder.java @@ -0,0 +1,47 @@ +package org.apache.seata.core.rpc.netty; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.LengthFieldBasedFrameDecoder; +import org.apache.seata.core.exception.DecodeException; +import org.apache.seata.core.protocol.ProtocolConstants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public abstract class AbstractProtocolDecoder extends LengthFieldBasedFrameDecoder implements ProtocolDecoder { + + protected Logger logger = LoggerFactory.getLogger(getClass()); + + public AbstractProtocolDecoder() { + /* + int maxFrameLength, + int lengthFieldOffset, magic code is 2B, and version is 1B, and then FullLength. so value is 3 + int lengthFieldLength, FullLength is int(4B). so values is 4 + int lengthAdjustment, FullLength include all data and read 7 bytes before, so the left length is (FullLength-7). so values is -7 + int initialBytesToStrip we will check magic code and version self, so do not strip any bytes. so values is 0 + */ + super(ProtocolConstants.MAX_FRAME_LENGTH, 3, 4); + } + + @Override + protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { + Object decoded; + try { + decoded = super.decode(ctx, in); + if (decoded instanceof ByteBuf) { + ByteBuf frame = (ByteBuf)decoded; + try { + return decodeFrame(frame); + } finally { + frame.release(); + } + } + } catch (Exception exx) { + logger.error("Decode frame error, cause: {}", exx.getMessage()); + throw new DecodeException(exx); + } + return decoded; + } + + +} diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolEncoder.java b/core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolEncoder.java deleted file mode 100644 index e588b92b8ea..00000000000 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolEncoder.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.seata.core.rpc.netty; - -import com.google.common.collect.ImmutableMap; -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.MessageToByteEncoder; -import org.apache.seata.common.util.StringUtils; -import org.apache.seata.core.protocol.ProtocolConstants; -import org.apache.seata.core.protocol.RpcMessage; -import org.apache.seata.core.protocol.Version; -import org.apache.seata.core.rpc.netty.v0.ProtocolEncoderV0; -import org.apache.seata.core.rpc.netty.v1.ProtocolEncoderV1; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Map; - -/** - * Compatible Protocol Encoder - *

    - *

  • Full Length: include all data
  • - *
  • Head Length: include head data from magic code to head map.
  • - *
  • Body Length: Full Length - Head Length
  • - *

    - */ -public class CompatibleProtocolEncoder extends MessageToByteEncoder { - - private static final Logger LOGGER = LoggerFactory.getLogger(CompatibleProtocolEncoder.class); - - private static Map protocolEncoderMap; - - public CompatibleProtocolEncoder() { - super(); - protocolEncoderMap = ImmutableMap.builder() - .put(ProtocolConstants.VERSION_0, new ProtocolEncoderV0()) - .put(ProtocolConstants.VERSION_1, new ProtocolEncoderV1()) - .build(); - } - - @Override - public void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) { - try { - if (msg instanceof RpcMessage) { - RpcMessage rpcMessage = (RpcMessage) msg; - String sdkVersion = rpcMessage.getOtherSideVersion(); - if (StringUtils.isBlank(sdkVersion)) { - sdkVersion = Version.getCurrent(); - } - byte protocolVersion = Version.calcProtocolVersion(sdkVersion); - ProtocolEncoder encoder = protocolEncoderMap.get(protocolVersion); - if (encoder == null) { - throw new UnsupportedOperationException("Unsupported protocolVersion: " + protocolVersion); - } - - encoder.encode(rpcMessage, out); - } else { - throw new UnsupportedOperationException("Not support this class:" + msg.getClass()); - } - } catch (Throwable e) { - LOGGER.error("Encode request error!", e); - } - } -} diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolDecoder.java b/core/src/main/java/org/apache/seata/core/rpc/netty/MultiProtocolDecoder.java similarity index 77% rename from core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolDecoder.java rename to core/src/main/java/org/apache/seata/core/rpc/netty/MultiProtocolDecoder.java index d066984c23b..ab0fb137625 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolDecoder.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/MultiProtocolDecoder.java @@ -18,12 +18,15 @@ import com.google.common.collect.ImmutableMap; import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.LengthFieldBasedFrameDecoder; import org.apache.seata.core.exception.DecodeException; import org.apache.seata.core.protocol.ProtocolConstants; import org.apache.seata.core.rpc.netty.v0.ProtocolDecoderV0; +import org.apache.seata.core.rpc.netty.v0.ProtocolEncoderV0; import org.apache.seata.core.rpc.netty.v1.ProtocolDecoderV1; +import org.apache.seata.core.rpc.netty.v1.ProtocolEncoderV1; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -52,17 +55,26 @@ *
  • Body Length: Full Length - Head Length
  • *

    */ -public class CompatibleProtocolDecoder extends LengthFieldBasedFrameDecoder { +public class MultiProtocolDecoder extends LengthFieldBasedFrameDecoder { - private static final Logger LOGGER = LoggerFactory.getLogger(CompatibleProtocolDecoder.class); - private static Map protocolDecoderMap; + private static final Logger LOGGER = LoggerFactory.getLogger(MultiProtocolDecoder.class); + private final Map protocolDecoderMap; - public CompatibleProtocolDecoder() { + private final Map protocolEncoderMap; + + private final ChannelHandler[] channelHandlers; + + public MultiProtocolDecoder(ChannelHandler... channelHandlers) { // default is 8M - this(ProtocolConstants.MAX_FRAME_LENGTH); + this(ProtocolConstants.MAX_FRAME_LENGTH, channelHandlers); } - public CompatibleProtocolDecoder(int maxFrameLength) { + public MultiProtocolDecoder() { + // default is 8M + this(ProtocolConstants.MAX_FRAME_LENGTH, null); + } + + public MultiProtocolDecoder(int maxFrameLength, ChannelHandler[] channelHandlers) { /* int maxFrameLength, int lengthFieldOffset, magic code is 2B, and version is 1B, and then FullLength. so value is 3 @@ -71,10 +83,15 @@ int lengthFieldLength, FullLength is int(4B). so values is 4 int initialBytesToStrip we will check magic code and version self, so do not strip any bytes. so values is 0 */ super(maxFrameLength, 3, 4, -7, 0); - protocolDecoderMap = ImmutableMap.builder() + this.protocolDecoderMap = ImmutableMap.builder() .put(ProtocolConstants.VERSION_0, new ProtocolDecoderV0()) .put(ProtocolConstants.VERSION_1, new ProtocolDecoderV1()) .build(); + this.protocolEncoderMap = ImmutableMap.builder() + .put(ProtocolConstants.VERSION_0, new ProtocolEncoderV0()) + .put(ProtocolConstants.VERSION_1, new ProtocolEncoderV1()) + .build(); + this.channelHandlers = channelHandlers; } @Override @@ -93,9 +110,10 @@ protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception if (decoded instanceof ByteBuf) { frame = (ByteBuf) decoded; + ProtocolDecoder decoder = protocolDecoderMap.get(version); + ProtocolEncoder encoder = protocolEncoderMap.get(version); try { - ProtocolDecoder decoder = protocolDecoderMap.get(version); - if (decoder == null) { + if (decoder == null || encoder == null) { throw new UnsupportedOperationException("Unsupported version: " + version); } return decoder.decodeFrame(frame); @@ -103,6 +121,12 @@ protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception if (version != ProtocolConstants.VERSION_0) { frame.release(); } + ctx.pipeline().addLast((ChannelHandler)decoder); + ctx.pipeline().addLast((ChannelHandler)encoder); + if (channelHandlers != null) { + ctx.pipeline().addLast(channelHandlers); + } + ctx.pipeline().remove(this); } } } catch (Exception exx) { diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/NettyClientBootstrap.java b/core/src/main/java/org/apache/seata/core/rpc/netty/NettyClientBootstrap.java index 4867f86bcf8..847ba0aa466 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/NettyClientBootstrap.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/NettyClientBootstrap.java @@ -35,6 +35,8 @@ import org.apache.seata.common.exception.FrameworkException; import org.apache.seata.common.thread.NamedThreadFactory; import org.apache.seata.core.rpc.RemotingBootstrap; +import org.apache.seata.core.rpc.netty.v1.ProtocolDecoderV1; +import org.apache.seata.core.rpc.netty.v1.ProtocolEncoderV1; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -132,8 +134,8 @@ public void initChannel(SocketChannel ch) { new IdleStateHandler(nettyClientConfig.getChannelMaxReadIdleSeconds(), nettyClientConfig.getChannelMaxWriteIdleSeconds(), nettyClientConfig.getChannelMaxAllIdleSeconds())) - .addLast(new CompatibleProtocolDecoder()) - .addLast(new CompatibleProtocolEncoder()); + .addLast(new ProtocolDecoderV1()) + .addLast(new ProtocolEncoderV1()); if (channelHandlers != null) { addChannelPipelineLast(ch, channelHandlers); } diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/NettyServerBootstrap.java b/core/src/main/java/org/apache/seata/core/rpc/netty/NettyServerBootstrap.java index b847b2a96dd..35b0bdfc675 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/NettyServerBootstrap.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/NettyServerBootstrap.java @@ -158,12 +158,10 @@ public void start() { .childHandler(new ChannelInitializer() { @Override public void initChannel(SocketChannel ch) { + MultiProtocolDecoder multiProtocolDecoder = + new MultiProtocolDecoder(channelHandlers); ch.pipeline().addLast(new IdleStateHandler(nettyServerConfig.getChannelMaxReadIdleSeconds(), 0, 0)) - .addLast(new CompatibleProtocolDecoder()) - .addLast(new CompatibleProtocolEncoder()); - if (channelHandlers != null) { - addChannelPipelineLast(ch, channelHandlers); - } + .addLast(multiProtocolDecoder); } }); diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolDecoderV0.java b/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolDecoderV0.java index 42e112a2f61..ca0776d044c 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolDecoderV0.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolDecoderV0.java @@ -20,7 +20,7 @@ import org.apache.seata.core.protocol.HeartbeatMessage; import org.apache.seata.core.protocol.ProtocolConstants; -import org.apache.seata.core.rpc.netty.ProtocolDecoder; +import org.apache.seata.core.rpc.netty.AbstractProtocolDecoder; import org.apache.seata.core.serializer.Serializer; import org.apache.seata.core.serializer.SerializerServiceLoader; import org.apache.seata.core.serializer.SerializerType; @@ -53,10 +53,13 @@ * * @see ProtocolEncoderV0 */ -public class ProtocolDecoderV0 implements ProtocolDecoder { +public class ProtocolDecoderV0 extends AbstractProtocolDecoder { private static final Logger LOGGER = LoggerFactory.getLogger(ProtocolDecoderV0.class); + + public ProtocolDecoderV0() { + } @Override public ProtocolRpcMessageV0 decodeFrame(ByteBuf in) { @@ -128,5 +131,4 @@ public ProtocolRpcMessageV0 decodeFrame(ByteBuf in) { return rpcMessage; } - } diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolEncoderV0.java b/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolEncoderV0.java index 3fc447b2818..4f7163a9658 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolEncoderV0.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolEncoderV0.java @@ -17,6 +17,8 @@ package org.apache.seata.core.rpc.netty.v0; import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToByteEncoder; import org.apache.seata.core.protocol.HeartbeatMessage; import org.apache.seata.core.protocol.MessageTypeAware; import org.apache.seata.core.protocol.ProtocolConstants; @@ -54,7 +56,7 @@ * * @see ProtocolDecoderV0 */ -public class ProtocolEncoderV0 implements ProtocolEncoder { +public class ProtocolEncoderV0 extends MessageToByteEncoder implements ProtocolEncoder { private static final Logger LOGGER = LoggerFactory.getLogger(ProtocolEncoderV0.class); @@ -103,4 +105,17 @@ public void encode(RpcMessage message, ByteBuf out) { LOGGER.error("Encode request error!", e); } } + + @Override + protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception { + try { + if (msg instanceof RpcMessage) { + encode((RpcMessage)msg, out); + } else { + throw new UnsupportedOperationException("Not support this class:" + msg.getClass()); + } + } catch (Throwable e) { + LOGGER.error("Encode request error!", e); + } + } } diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolDecoderV1.java b/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolDecoderV1.java index 9ca49779443..8d6eb3abfc6 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolDecoderV1.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolDecoderV1.java @@ -20,8 +20,11 @@ import java.util.Map; import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.LengthFieldBasedFrameDecoder; import org.apache.seata.core.compressor.Compressor; import org.apache.seata.core.compressor.CompressorFactory; +import org.apache.seata.core.exception.DecodeException; import org.apache.seata.core.protocol.HeartbeatMessage; import org.apache.seata.core.protocol.ProtocolConstants; import org.apache.seata.core.rpc.netty.ProtocolDecoder; @@ -61,16 +64,25 @@ * @see ProtocolEncoderV1 * @since 0.7.0 */ -public class ProtocolDecoderV1 implements ProtocolDecoder { +public class ProtocolDecoderV1 extends LengthFieldBasedFrameDecoder implements ProtocolDecoder{ private static final Logger LOGGER = LoggerFactory.getLogger(ProtocolDecoderV1.class); private final List supportDeSerializerTypes; public ProtocolDecoderV1() { + /* + int maxFrameLength, + int lengthFieldOffset, magic code is 2B, and version is 1B, and then FullLength. so value is 3 + int lengthFieldLength, FullLength is int(4B). so values is 4 + int lengthAdjustment, FullLength include all data and read 7 bytes before, so the left length is (FullLength-7). so values is -7 + int initialBytesToStrip we will check magic code and version self, so do not strip any bytes. so values is 0 + */ + super(ProtocolConstants.MAX_FRAME_LENGTH, 3, 4, -7, 0); supportDeSerializerTypes = SerializerServiceLoader.getSupportedSerializers(); if (supportDeSerializerTypes.isEmpty()) { throw new IllegalArgumentException("No serializer found"); - } } + } + } @Override public ProtocolRpcMessage decodeFrame(ByteBuf frame) { @@ -127,4 +139,25 @@ public ProtocolRpcMessage decodeFrame(ByteBuf frame) { return rpcMessage; } + + @Override + protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { + Object decoded; + try { + decoded = super.decode(ctx, in); + if (decoded instanceof ByteBuf) { + ByteBuf frame = (ByteBuf)decoded; + try { + return decodeFrame(frame); + } finally { + frame.release(); + } + } + } catch (Exception exx) { + LOGGER.error("Decode frame error, cause: {}", exx.getMessage()); + throw new DecodeException(exx); + } + return decoded; + } + } diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolEncoderV1.java b/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolEncoderV1.java index 14cbcdb55da..baa6531bbe4 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolEncoderV1.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolEncoderV1.java @@ -17,6 +17,8 @@ package org.apache.seata.core.rpc.netty.v1; import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToByteEncoder; import org.apache.seata.core.rpc.netty.ProtocolEncoder; import org.apache.seata.core.serializer.Serializer; import org.apache.seata.core.compressor.Compressor; @@ -57,7 +59,7 @@ * @see ProtocolDecoderV1 * @since 0.7.0 */ -public class ProtocolEncoderV1 implements ProtocolEncoder { +public class ProtocolEncoderV1 extends MessageToByteEncoder implements ProtocolEncoder { private static final Logger LOGGER = LoggerFactory.getLogger(ProtocolEncoderV1.class); @@ -119,4 +121,18 @@ public void encode(RpcMessage message, ByteBuf out) { throw e; } } + + @Override + protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception { + try { + if (msg instanceof RpcMessage) { + this.encode((RpcMessage)msg, out); + } else { + throw new UnsupportedOperationException("Not support this class:" + msg.getClass()); + } + } catch (Throwable e) { + LOGGER.error("Encode request error!", e); + } + } + } diff --git a/core/src/main/java/org/apache/seata/core/rpc/processor/server/RegTmProcessor.java b/core/src/main/java/org/apache/seata/core/rpc/processor/server/RegTmProcessor.java index 6090232c6c4..0afee867f8b 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/processor/server/RegTmProcessor.java +++ b/core/src/main/java/org/apache/seata/core/rpc/processor/server/RegTmProcessor.java @@ -89,8 +89,8 @@ private void onRegTmMessage(ChannelHandlerContext ctx, RpcMessage rpcMessage) { } remotingServer.sendAsyncResponse(rpcMessage, ctx.channel(), response); if (isSuccess && LOGGER.isInfoEnabled()) { - LOGGER.info("TM register success,message:{},channel:{},client version:{},client protocol-version:{}" - , message, ctx.channel(), message.getVersion(), rpcMessage.getOtherSideVersion()); + LOGGER.info("TM register success,message:{},channel:{},client version:{}", message, ctx.channel(), + message.getVersion()); } } diff --git a/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1Client.java b/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1Client.java index 3f52ed63c55..a052f3718bc 100644 --- a/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1Client.java +++ b/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1Client.java @@ -45,8 +45,7 @@ import org.apache.seata.core.protocol.RpcMessage; import org.apache.seata.core.protocol.transaction.BranchCommitRequest; import org.apache.seata.core.serializer.SerializerType; -import org.apache.seata.core.rpc.netty.CompatibleProtocolDecoder; -import org.apache.seata.core.rpc.netty.CompatibleProtocolEncoder; +import org.apache.seata.core.rpc.netty.MultiProtocolDecoder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -82,9 +81,7 @@ public void connect(String host, int port, int connectTimeout) { @Override protected void initChannel(Channel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); - pipeline.addLast(new CompatibleProtocolEncoder()); - pipeline.addLast(new CompatibleProtocolDecoder(8 * 1024 * 1024)); - pipeline.addLast(new ClientChannelHandler(ProtocolV1Client.this)); + pipeline.addLast(new MultiProtocolDecoder(new ClientChannelHandler(ProtocolV1Client.this))); } }); // Bind and start to accept incoming connections. diff --git a/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1Server.java b/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1Server.java index 20a2fdbcc57..6f997c9fbbd 100644 --- a/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1Server.java +++ b/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1Server.java @@ -35,12 +35,7 @@ import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; import org.apache.seata.common.thread.NamedThreadFactory; -import org.apache.seata.common.thread.NamedThreadFactory; -import org.apache.seata.core.rpc.netty.CompatibleProtocolDecoder; -import org.apache.seata.core.rpc.netty.CompatibleProtocolEncoder; - -import java.net.InetSocketAddress; -import java.util.concurrent.TimeUnit; +import org.apache.seata.core.rpc.netty.MultiProtocolDecoder; /** */ @@ -73,9 +68,7 @@ public void start() { @Override protected void initChannel(Channel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); - pipeline.addLast(new CompatibleProtocolDecoder(8 * 1024 * 1024)); - pipeline.addLast(new CompatibleProtocolEncoder()); - pipeline.addLast(new ServerChannelHandler()); + pipeline.addLast(new MultiProtocolDecoder(new ServerChannelHandler())); } }); From c5652de476b62d058553ef1d82cdbba98d480b3e Mon Sep 17 00:00:00 2001 From: jianbin Date: Thu, 27 Jun 2024 11:06:17 +0800 Subject: [PATCH 55/62] optimize: select channel handles based on protocol versions --- .../rpc/netty/AbstractProtocolDecoder.java | 64 ++++++++++++------- .../core/rpc/netty/MultiProtocolDecoder.java | 16 ++--- .../core/rpc/netty/NettyClientBootstrap.java | 4 +- .../core/rpc/netty/NettyServerBootstrap.java | 7 +- .../core/rpc/netty/v0/MessageCodecV0.java | 44 ------------- .../core/rpc/netty/v1/ProtocolDecoderV1.java | 2 +- 6 files changed, 53 insertions(+), 84 deletions(-) delete mode 100644 core/src/main/java/org/apache/seata/core/rpc/netty/v0/MessageCodecV0.java diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractProtocolDecoder.java b/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractProtocolDecoder.java index b1c238089e9..82735451964 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractProtocolDecoder.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractProtocolDecoder.java @@ -1,3 +1,19 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.apache.seata.core.rpc.netty; import io.netty.buffer.ByteBuf; @@ -10,38 +26,38 @@ public abstract class AbstractProtocolDecoder extends LengthFieldBasedFrameDecoder implements ProtocolDecoder { - protected Logger logger = LoggerFactory.getLogger(getClass()); + protected Logger logger = LoggerFactory.getLogger(getClass()); - public AbstractProtocolDecoder() { - /* + public AbstractProtocolDecoder() { + /* int maxFrameLength, int lengthFieldOffset, magic code is 2B, and version is 1B, and then FullLength. so value is 3 int lengthFieldLength, FullLength is int(4B). so values is 4 int lengthAdjustment, FullLength include all data and read 7 bytes before, so the left length is (FullLength-7). so values is -7 int initialBytesToStrip we will check magic code and version self, so do not strip any bytes. so values is 0 */ - super(ProtocolConstants.MAX_FRAME_LENGTH, 3, 4); - } + super(ProtocolConstants.MAX_FRAME_LENGTH, 3, 4); + } - @Override - protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { - Object decoded; - try { - decoded = super.decode(ctx, in); - if (decoded instanceof ByteBuf) { - ByteBuf frame = (ByteBuf)decoded; - try { - return decodeFrame(frame); - } finally { - frame.release(); - } - } - } catch (Exception exx) { - logger.error("Decode frame error, cause: {}", exx.getMessage()); - throw new DecodeException(exx); - } - return decoded; - } + @Override + protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { + Object decoded; + try { + decoded = super.decode(ctx, in); + if (decoded instanceof ByteBuf) { + ByteBuf frame = (ByteBuf)decoded; + try { + return decodeFrame(frame); + } finally { + frame.release(); + } + } + } catch (Exception exx) { + logger.error("Decode frame error, cause: {}", exx.getMessage()); + throw new DecodeException(exx); + } + return decoded; + } } diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/MultiProtocolDecoder.java b/core/src/main/java/org/apache/seata/core/rpc/netty/MultiProtocolDecoder.java index ab0fb137625..9bd95503697 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/MultiProtocolDecoder.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/MultiProtocolDecoder.java @@ -83,15 +83,13 @@ int lengthFieldLength, FullLength is int(4B). so values is 4 int initialBytesToStrip we will check magic code and version self, so do not strip any bytes. so values is 0 */ super(maxFrameLength, 3, 4, -7, 0); - this.protocolDecoderMap = ImmutableMap.builder() - .put(ProtocolConstants.VERSION_0, new ProtocolDecoderV0()) - .put(ProtocolConstants.VERSION_1, new ProtocolDecoderV1()) - .build(); - this.protocolEncoderMap = ImmutableMap.builder() - .put(ProtocolConstants.VERSION_0, new ProtocolEncoderV0()) - .put(ProtocolConstants.VERSION_1, new ProtocolEncoderV1()) - .build(); - this.channelHandlers = channelHandlers; + this.protocolDecoderMap = + ImmutableMap.builder().put(ProtocolConstants.VERSION_0, new ProtocolDecoderV0()) + .put(ProtocolConstants.VERSION_1, new ProtocolDecoderV1()).build(); + this.protocolEncoderMap = + ImmutableMap.builder().put(ProtocolConstants.VERSION_0, new ProtocolEncoderV0()) + .put(ProtocolConstants.VERSION_1, new ProtocolEncoderV1()).build(); + this.channelHandlers = channelHandlers; } @Override diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/NettyClientBootstrap.java b/core/src/main/java/org/apache/seata/core/rpc/netty/NettyClientBootstrap.java index 847ba0aa466..4aaafc0acb0 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/NettyClientBootstrap.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/NettyClientBootstrap.java @@ -130,8 +130,8 @@ public void start() { @Override public void initChannel(SocketChannel ch) { ChannelPipeline pipeline = ch.pipeline(); - pipeline.addLast( - new IdleStateHandler(nettyClientConfig.getChannelMaxReadIdleSeconds(), + pipeline + .addLast(new IdleStateHandler(nettyClientConfig.getChannelMaxReadIdleSeconds(), nettyClientConfig.getChannelMaxWriteIdleSeconds(), nettyClientConfig.getChannelMaxAllIdleSeconds())) .addLast(new ProtocolDecoderV1()) diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/NettyServerBootstrap.java b/core/src/main/java/org/apache/seata/core/rpc/netty/NettyServerBootstrap.java index 35b0bdfc675..c7b2aa57c21 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/NettyServerBootstrap.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/NettyServerBootstrap.java @@ -158,11 +158,10 @@ public void start() { .childHandler(new ChannelInitializer() { @Override public void initChannel(SocketChannel ch) { - MultiProtocolDecoder multiProtocolDecoder = - new MultiProtocolDecoder(channelHandlers); - ch.pipeline().addLast(new IdleStateHandler(nettyServerConfig.getChannelMaxReadIdleSeconds(), 0, 0)) + MultiProtocolDecoder multiProtocolDecoder = new MultiProtocolDecoder(channelHandlers); + ch.pipeline() + .addLast(new IdleStateHandler(nettyServerConfig.getChannelMaxReadIdleSeconds(), 0, 0)) .addLast(multiProtocolDecoder); - } }); diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/v0/MessageCodecV0.java b/core/src/main/java/org/apache/seata/core/rpc/netty/v0/MessageCodecV0.java deleted file mode 100644 index ab1d4f74716..00000000000 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/v0/MessageCodecV0.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.seata.core.rpc.netty.v0; - -import io.netty.buffer.ByteBuf; -import org.apache.seata.core.protocol.MessageTypeAware; - -/** - * The interface Message codec. - * - */ -public interface MessageCodecV0 extends MessageTypeAware { - - /** - * Encode byte [ ]. - * - * @return the byte [ ] - */ - byte[] encode(); - - /** - * Decode boolean. - * - * @param in the in - * @return the boolean - */ - boolean decode(ByteBuf in); - - boolean decode(ByteBuf in, T req); -} diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolDecoderV1.java b/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolDecoderV1.java index 8d6eb3abfc6..1ef84ea86b0 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolDecoderV1.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolDecoderV1.java @@ -64,7 +64,7 @@ * @see ProtocolEncoderV1 * @since 0.7.0 */ -public class ProtocolDecoderV1 extends LengthFieldBasedFrameDecoder implements ProtocolDecoder{ +public class ProtocolDecoderV1 extends LengthFieldBasedFrameDecoder implements ProtocolDecoder { private static final Logger LOGGER = LoggerFactory.getLogger(ProtocolDecoderV1.class); private final List supportDeSerializerTypes; From 91c65c18cc27c4b5029bb407711193f426534d4e Mon Sep 17 00:00:00 2001 From: jianbin Date: Thu, 27 Jun 2024 11:10:34 +0800 Subject: [PATCH 56/62] optimize: select channel handles based on protocol versions --- .../seata/core/rpc/netty/AbstractNettyRemotingServer.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingServer.java b/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingServer.java index 5ca26300c90..62e2aa26b4b 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingServer.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingServer.java @@ -109,11 +109,11 @@ public void sendAsyncResponse(RpcMessage rpcMessage, Channel channel, Object msg private RpcMessage buildResponseMessage(Channel channel, RpcMessage fromRpcMessage, Object msg, byte messageType) { - return super.buildResponseMessage(fromRpcMessage, msg, messageType); + return super.buildResponseMessage(fromRpcMessage, msg, messageType); } protected RpcMessage buildRequestMessage(Channel channel, Object msg, byte messageType) { - return super.buildRequestMessage(msg, messageType); + return super.buildRequestMessage(msg, messageType); } From e0f3d378af96d963bb802ccd450a0dcfb5a87727 Mon Sep 17 00:00:00 2001 From: jianbin Date: Thu, 27 Jun 2024 13:49:44 +0800 Subject: [PATCH 57/62] optimize: select channel handles based on protocol versions --- .../netty/AbstractNettyRemotingClient.java | 3 +-- .../netty/AbstractNettyRemotingServer.java | 21 +++++-------------- .../seata/core/rpc/netty/ProtocolDecoder.java | 3 ++- .../core/rpc/netty/v0/ProtocolDecoderV0.java | 7 ++++--- .../core/rpc/netty/v1/ProtocolDecoderV1.java | 6 +++--- .../rpc/netty/v1/ClientChannelHandler.java | 4 ++-- .../core/rpc/netty/v1/ProtocolV1Client.java | 6 ++++-- .../netty/v1/ProtocolV1SerializerTest.java | 8 +++---- .../core/rpc/netty/v1/ProtocolV1Server.java | 10 +++------ .../rpc/netty/v1/ServerChannelHandler.java | 6 ++---- 10 files changed, 30 insertions(+), 44 deletions(-) diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingClient.java b/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingClient.java index 2901eb8d3f5..be97b5ade40 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingClient.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingClient.java @@ -410,8 +410,7 @@ class ClientHandler extends ChannelDuplexHandler { @Override public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception { RpcMessage rpcMessage = null; - if (msg instanceof ProtocolRpcMessage) { - rpcMessage = ((ProtocolRpcMessage) msg).protocolMsg2RpcMsg(); + if (msg instanceof RpcMessage) { processMessage(ctx, rpcMessage); } else { LOGGER.error("rpcMessage type error"); diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingServer.java b/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingServer.java index 62e2aa26b4b..39edcaca8ad 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingServer.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingServer.java @@ -69,7 +69,7 @@ public Object sendSyncRequest(String resourceId, String clientId, Object msg, bo if (channel == null) { throw new RuntimeException("rm client is not connected. dbkey:" + resourceId + ",clientId:" + clientId); } - RpcMessage rpcMessage = buildRequestMessage(channel, msg, ProtocolConstants.MSGTYPE_RESQUEST_SYNC); + RpcMessage rpcMessage = buildRequestMessage(msg, ProtocolConstants.MSGTYPE_RESQUEST_SYNC); return super.sendSync(channel, rpcMessage, NettyServerConfig.getRpcRequestTimeout()); } @@ -78,7 +78,7 @@ public Object sendSyncRequest(Channel channel, Object msg) throws TimeoutExcepti if (channel == null) { throw new RuntimeException("client is not connected"); } - RpcMessage rpcMessage = buildRequestMessage(channel, msg, ProtocolConstants.MSGTYPE_RESQUEST_SYNC); + RpcMessage rpcMessage = buildRequestMessage(msg, ProtocolConstants.MSGTYPE_RESQUEST_SYNC); return super.sendSync(channel, rpcMessage, NettyServerConfig.getRpcRequestTimeout()); } @@ -87,7 +87,7 @@ public void sendAsyncRequest(Channel channel, Object msg) { if (channel == null) { throw new RuntimeException("client is not connected"); } - RpcMessage rpcMessage = buildRequestMessage(channel, msg, ProtocolConstants.MSGTYPE_RESQUEST_ONEWAY); + RpcMessage rpcMessage = buildRequestMessage(msg, ProtocolConstants.MSGTYPE_RESQUEST_ONEWAY); super.sendAsync(channel, rpcMessage); } @@ -98,7 +98,7 @@ public void sendAsyncResponse(RpcMessage rpcMessage, Channel channel, Object msg clientChannel = ChannelManager.getSameClientChannel(channel); } if (clientChannel != null) { - RpcMessage rpcMsg = buildResponseMessage(channel, rpcMessage, msg, msg instanceof HeartbeatMessage + RpcMessage rpcMsg = buildResponseMessage(rpcMessage, msg, msg instanceof HeartbeatMessage ? ProtocolConstants.MSGTYPE_HEARTBEAT_RESPONSE : ProtocolConstants.MSGTYPE_RESPONSE); super.sendAsync(clientChannel, rpcMsg); @@ -107,16 +107,6 @@ public void sendAsyncResponse(RpcMessage rpcMessage, Channel channel, Object msg } } - - private RpcMessage buildResponseMessage(Channel channel, RpcMessage fromRpcMessage, Object msg, byte messageType) { - return super.buildResponseMessage(fromRpcMessage, msg, messageType); - } - - protected RpcMessage buildRequestMessage(Channel channel, Object msg, byte messageType) { - return super.buildRequestMessage(msg, messageType); - } - - @Override public void registerProcessor(int messageType, RemotingProcessor processor, ExecutorService executor) { Pair pair = new Pair<>(processor, executor); @@ -174,8 +164,7 @@ class ServerHandler extends ChannelDuplexHandler { @Override public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception { RpcMessage rpcMessage = null; - if (msg instanceof ProtocolRpcMessage) { - rpcMessage = ((ProtocolRpcMessage) msg).protocolMsg2RpcMsg(); + if (msg instanceof RpcMessage) { processMessage(ctx, rpcMessage); } else { LOGGER.error("rpcMessage type error"); diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/ProtocolDecoder.java b/core/src/main/java/org/apache/seata/core/rpc/netty/ProtocolDecoder.java index 42a7c75c04f..d28506fd841 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/ProtocolDecoder.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/ProtocolDecoder.java @@ -17,6 +17,7 @@ package org.apache.seata.core.rpc.netty; import io.netty.buffer.ByteBuf; +import org.apache.seata.core.protocol.RpcMessage; /** * the protocol decoder @@ -24,6 +25,6 @@ **/ public interface ProtocolDecoder { - ProtocolRpcMessage decodeFrame(ByteBuf in); + RpcMessage decodeFrame(ByteBuf in); } diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolDecoderV0.java b/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolDecoderV0.java index ca0776d044c..76e9beb29d6 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolDecoderV0.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolDecoderV0.java @@ -20,6 +20,7 @@ import org.apache.seata.core.protocol.HeartbeatMessage; import org.apache.seata.core.protocol.ProtocolConstants; +import org.apache.seata.core.protocol.RpcMessage; import org.apache.seata.core.rpc.netty.AbstractProtocolDecoder; import org.apache.seata.core.serializer.Serializer; import org.apache.seata.core.serializer.SerializerServiceLoader; @@ -62,7 +63,7 @@ public ProtocolDecoderV0() { } @Override - public ProtocolRpcMessageV0 decodeFrame(ByteBuf in) { + public RpcMessage decodeFrame(ByteBuf in) { ProtocolRpcMessageV0 rpcMessage = new ProtocolRpcMessageV0(); if (in.readableBytes() < ProtocolConstantsV0.HEAD_LENGTH) { throw new IllegalArgumentException("Nothing to decode."); @@ -96,7 +97,7 @@ public ProtocolRpcMessageV0 decodeFrame(ByteBuf in) { rpcMessage.setBody(HeartbeatMessage.PONG); } - return rpcMessage; + return rpcMessage.protocolMsg2RpcMsg(); } if (bodyLength > 0 && in.readableBytes() < bodyLength) { @@ -128,7 +129,7 @@ public ProtocolRpcMessageV0 decodeFrame(ByteBuf in) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Receive:" + rpcMessage.getBody() + ", messageId:" + msgId); } - return rpcMessage; + return rpcMessage.protocolMsg2RpcMsg(); } } diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolDecoderV1.java b/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolDecoderV1.java index 1ef84ea86b0..2d2d01fce55 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolDecoderV1.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolDecoderV1.java @@ -27,8 +27,8 @@ import org.apache.seata.core.exception.DecodeException; import org.apache.seata.core.protocol.HeartbeatMessage; import org.apache.seata.core.protocol.ProtocolConstants; +import org.apache.seata.core.protocol.RpcMessage; import org.apache.seata.core.rpc.netty.ProtocolDecoder; -import org.apache.seata.core.rpc.netty.ProtocolRpcMessage; import org.apache.seata.core.serializer.Serializer; import org.apache.seata.core.serializer.SerializerServiceLoader; import org.apache.seata.core.serializer.SerializerType; @@ -85,7 +85,7 @@ int lengthFieldLength, FullLength is int(4B). so values is 4 } @Override - public ProtocolRpcMessage decodeFrame(ByteBuf frame) { + public RpcMessage decodeFrame(ByteBuf frame) { byte b0 = frame.readByte(); byte b1 = frame.readByte(); if (ProtocolConstants.MAGIC_CODE_BYTES[0] != b0 @@ -137,7 +137,7 @@ public ProtocolRpcMessage decodeFrame(ByteBuf frame) { } } - return rpcMessage; + return rpcMessage.protocolMsg2RpcMsg(); } @Override diff --git a/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ClientChannelHandler.java b/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ClientChannelHandler.java index e35c124e306..14709e5f06c 100644 --- a/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ClientChannelHandler.java +++ b/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ClientChannelHandler.java @@ -50,8 +50,8 @@ public void channelInactive(final ChannelHandlerContext ctx) throws Exception { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - if (msg instanceof ProtocolRpcMessage) { - RpcMessage rpcMessage = ((ProtocolRpcMessage) msg).protocolMsg2RpcMsg(); + if (msg instanceof RpcMessage) { + RpcMessage rpcMessage = (RpcMessage)msg; int msgId = rpcMessage.getId(); DefaultPromise future = (DefaultPromise) client.futureMap.remove(msgId); if (future != null) { diff --git a/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1Client.java b/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1Client.java index a052f3718bc..7b8694e182a 100644 --- a/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1Client.java +++ b/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1Client.java @@ -45,7 +45,6 @@ import org.apache.seata.core.protocol.RpcMessage; import org.apache.seata.core.protocol.transaction.BranchCommitRequest; import org.apache.seata.core.serializer.SerializerType; -import org.apache.seata.core.rpc.netty.MultiProtocolDecoder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -81,7 +80,10 @@ public void connect(String host, int port, int connectTimeout) { @Override protected void initChannel(Channel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); - pipeline.addLast(new MultiProtocolDecoder(new ClientChannelHandler(ProtocolV1Client.this))); + pipeline + .addLast(new ProtocolDecoderV1()) + .addLast(new ProtocolEncoderV1()); + pipeline.addLast(new ClientChannelHandler(ProtocolV1Client.this)); } }); // Bind and start to accept incoming connections. diff --git a/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1SerializerTest.java b/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1SerializerTest.java index 5ee0df7dee2..33a693311ac 100644 --- a/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1SerializerTest.java +++ b/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1SerializerTest.java @@ -27,8 +27,8 @@ import org.apache.seata.common.thread.NamedThreadFactory; import org.apache.seata.core.model.BranchType; +import org.apache.seata.core.protocol.RpcMessage; import org.apache.seata.core.protocol.transaction.BranchCommitRequest; -import org.apache.seata.core.rpc.netty.ProtocolRpcMessage; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.slf4j.Logger; @@ -66,7 +66,7 @@ public void testAll() { body.setXid("xid-1234"); // test run times - int runTimes = 100000; + int runTimes = 100; final int threads = 50; final CountDownLatch cnt = new CountDownLatch(runTimes); @@ -80,7 +80,7 @@ public void testAll() { while (tag.getAndIncrement() < runTimes) { try { Future future = client.sendRpc(head, body); - ProtocolRpcMessage resp = (ProtocolRpcMessage) future.get(10, TimeUnit.SECONDS); + RpcMessage resp = (RpcMessage)future.get(10, TimeUnit.SECONDS); if (resp != null) { success.incrementAndGet(); } @@ -93,7 +93,7 @@ public void testAll() { }); } - cnt.await(); + cnt.await(10,TimeUnit.SECONDS); LOGGER.info("success {}/{}", success.get(), runTimes); Assertions.assertEquals(success.get(), runTimes); } catch (InterruptedException e) { diff --git a/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1Server.java b/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1Server.java index 6f997c9fbbd..264ebd8eaf2 100644 --- a/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1Server.java +++ b/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1Server.java @@ -75,13 +75,9 @@ protected void initChannel(Channel channel) throws Exception { String host = "0.0.0.0"; ChannelFuture future = serverBootstrap.bind(new InetSocketAddress(host, port)); - ChannelFuture channelFuture = future.addListener(new ChannelFutureListener() { - - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (!future.isSuccess()) { - throw new RuntimeException("Server start fail !", future.cause()); - } + ChannelFuture channelFuture = future.addListener((ChannelFutureListener)future1 -> { + if (!future1.isSuccess()) { + throw new RuntimeException("Server start fail !", future1.cause()); } }); diff --git a/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ServerChannelHandler.java b/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ServerChannelHandler.java index 8b468d0e8ff..9a8fff05824 100644 --- a/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ServerChannelHandler.java +++ b/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ServerChannelHandler.java @@ -39,10 +39,8 @@ public class ServerChannelHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) { Channel channel = ctx.channel(); - - if (msg instanceof ProtocolRpcMessage) { - RpcMessage rpcMessage = ((ProtocolRpcMessage) msg).protocolMsg2RpcMsg(); - channel.writeAndFlush(rpcMessage); + if (msg instanceof RpcMessage) { + channel.writeAndFlush(msg); } else { LOGGER.error("rpcMessage type error"); } From 0cf1328de09488f38acea547aab8a37075ebad09 Mon Sep 17 00:00:00 2001 From: jianbin Date: Thu, 27 Jun 2024 14:00:12 +0800 Subject: [PATCH 58/62] optimize: select channel handles based on protocol versions --- changes/en-us/2.x.md | 5 ++++- changes/zh-cn/2.x.md | 3 +++ .../seata/core/rpc/netty/v1/ProtocolV1SerializerTest.java | 3 ++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md index 7941cbbf60a..abd08255b13 100644 --- a/changes/en-us/2.x.md +++ b/changes/en-us/2.x.md @@ -3,7 +3,7 @@ Add changes here for all PR submitted to the 2.x branch. ### feature: - +- [[#6226](https://github.com/apache/incubator-seata/pull/6226)] multi-version seata protocol support ### bugfix: - [[#6592](https://github.com/apache/incubator-seata/pull/6592)] fix @Async annotation not working in ClusterWatcherManager @@ -14,6 +14,8 @@ Add changes here for all PR submitted to the 2.x branch. - [[#6499](https://github.com/apache/incubator-seata/pull/6499)] split the task thread pool for committing and rollbacking statuses - [[#6208](https://github.com/apache/incubator-seata/pull/6208)] optimize : load SeataSerializer by version - [[#6209](https://github.com/apache/incubator-seata/pull/6209)] Eliminate RpcMessage and Encoder/Decoder dependencies +- [[#6634](https://github.com/apache/incubator-seata/pull/6634)] select channel handles based on protocol versions + ### refactor: - [[#6534](https://github.com/apache/incubator-seata/pull/6534)] optimize: send async response @@ -35,5 +37,6 @@ Thanks to these contributors for their code commits. Please report an unintended - [YeonCheolGit](https://github.com/YeonCheolGit) - [liuqiufeng](https://github.com/liuqiufeng) - [Bughue](https://github.com/Bughue) +- [funky-eyes](https://github.com/funky-eyes) Also, we receive many valuable issues, questions and advices from our community. Thanks for you all. diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md index 25ff7a0587c..474d813b106 100644 --- a/changes/zh-cn/2.x.md +++ b/changes/zh-cn/2.x.md @@ -3,6 +3,7 @@ ### feature: +- [[#6226](https://github.com/apache/incubator-seata/pull/6226)] 支持seata私有协议多版本兼容 ### bugfix: - [[#6592](https://github.com/apache/incubator-seata/pull/6592)] fix @Async注解ClusterWatcherManager中不生效的问题 @@ -13,6 +14,7 @@ - [[#6499](https://github.com/apache/incubator-seata/pull/6499)] 拆分 committing 和 rollbacking 状态的任务线程池 - [[#6208](https://github.com/apache/incubator-seata/pull/6208)] 支持多版本的Seata序列化 - [[#6209](https://github.com/apache/incubator-seata/pull/6209)] 解开 RpcMessage 和 Encoder/Decoder 的互相依赖 +- [[#6634](https://github.com/apache/incubator-seata/pull/6634)] 根据协议版本指定channel handle ### refactor: - [[#6534](https://github.com/apache/incubator-seata/pull/6534)] 优化: 发送异步响应 @@ -31,5 +33,6 @@ - [YeonCheolGit](https://github.com/YeonCheolGit) - [liuqiufeng](https://github.com/liuqiufeng) - [Bughue](https://github.com/Bughue) +- [funky-eyes](https://github.com/funky-eyes) 同时,我们收到了社区反馈的很多有价值的issue和建议,非常感谢大家。 diff --git a/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1SerializerTest.java b/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1SerializerTest.java index 33a693311ac..ac75db20c5f 100644 --- a/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1SerializerTest.java +++ b/test/src/test/java/org/apache/seata/core/rpc/netty/v1/ProtocolV1SerializerTest.java @@ -66,7 +66,7 @@ public void testAll() { body.setXid("xid-1234"); // test run times - int runTimes = 100; + int runTimes = 100000; final int threads = 50; final CountDownLatch cnt = new CountDownLatch(runTimes); @@ -96,6 +96,7 @@ public void testAll() { cnt.await(10,TimeUnit.SECONDS); LOGGER.info("success {}/{}", success.get(), runTimes); Assertions.assertEquals(success.get(), runTimes); + service1.shutdown(); } catch (InterruptedException e) { LOGGER.error("Thread interrupted", e); } finally { From 76fc55f5ff2ecc69e04237916bebc358924c4cd0 Mon Sep 17 00:00:00 2001 From: jianbin Date: Thu, 27 Jun 2024 14:08:12 +0800 Subject: [PATCH 59/62] optimize: select channel handles based on protocol versions --- .../seata/core/rpc/netty/AbstractNettyRemotingClient.java | 3 +-- .../seata/core/rpc/netty/AbstractNettyRemotingServer.java | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingClient.java b/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingClient.java index be97b5ade40..248e8f48f6d 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingClient.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingClient.java @@ -409,9 +409,8 @@ class ClientHandler extends ChannelDuplexHandler { @Override public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception { - RpcMessage rpcMessage = null; if (msg instanceof RpcMessage) { - processMessage(ctx, rpcMessage); + processMessage(ctx, (RpcMessage)msg); } else { LOGGER.error("rpcMessage type error"); } diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingServer.java b/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingServer.java index 39edcaca8ad..cf66c0b4562 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingServer.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingServer.java @@ -163,9 +163,8 @@ class ServerHandler extends ChannelDuplexHandler { */ @Override public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception { - RpcMessage rpcMessage = null; if (msg instanceof RpcMessage) { - processMessage(ctx, rpcMessage); + processMessage(ctx, (RpcMessage)msg); } else { LOGGER.error("rpcMessage type error"); } From a9ed6148924e9ca5e57e3689876b35ff4df7251f Mon Sep 17 00:00:00 2001 From: jianbin Date: Thu, 27 Jun 2024 14:38:55 +0800 Subject: [PATCH 60/62] remove --- .../rpc/netty/CompatibleProtocolDecoder.java | 155 ------------------ .../rpc/netty/CompatibleProtocolEncoder.java | 79 --------- 2 files changed, 234 deletions(-) delete mode 100644 core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolDecoder.java delete mode 100644 core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolEncoder.java diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolDecoder.java b/core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolDecoder.java deleted file mode 100644 index d066984c23b..00000000000 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolDecoder.java +++ /dev/null @@ -1,155 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.seata.core.rpc.netty; - -import com.google.common.collect.ImmutableMap; -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.LengthFieldBasedFrameDecoder; -import org.apache.seata.core.exception.DecodeException; -import org.apache.seata.core.protocol.ProtocolConstants; -import org.apache.seata.core.rpc.netty.v0.ProtocolDecoderV0; -import org.apache.seata.core.rpc.netty.v1.ProtocolDecoderV1; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Map; - -/** - *
    - * (> 0.7.0)
    - * 0     1     2     3     4     5     6     7     8     9    10     11    12    13    14    15    16
    - * +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
    - * |   magic   |Proto|     Full length       |    Head   | Msg |Seria|Compr|     RequestId         |
    - * |   code    |colVer|    (head+body)      |   Length  |Type |lizer|ess  |                       |
    - * +-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+
    - *
    - * (<= 0.7.0)
    - * 0     1     2     3     4           6           8          10           12          14
    - * +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
    - * |   0xdada  |   flag    | typecode/ |                 requestid                     |
    - * |           |           | bodylength|                                               |
    - * +-----------+-----------+-----------+-----------+-----------+-----------+-----------+
    - *
    - * 
    - *

    - *

  • Full Length: include all data
  • - *
  • Head Length: include head data from magic code to head map.
  • - *
  • Body Length: Full Length - Head Length
  • - *

    - */ -public class CompatibleProtocolDecoder extends LengthFieldBasedFrameDecoder { - - private static final Logger LOGGER = LoggerFactory.getLogger(CompatibleProtocolDecoder.class); - private static Map protocolDecoderMap; - - public CompatibleProtocolDecoder() { - // default is 8M - this(ProtocolConstants.MAX_FRAME_LENGTH); - } - - public CompatibleProtocolDecoder(int maxFrameLength) { - /* - int maxFrameLength, - int lengthFieldOffset, magic code is 2B, and version is 1B, and then FullLength. so value is 3 - int lengthFieldLength, FullLength is int(4B). so values is 4 - int lengthAdjustment, FullLength include all data and read 7 bytes before, so the left length is (FullLength-7). so values is -7 - int initialBytesToStrip we will check magic code and version self, so do not strip any bytes. so values is 0 - */ - super(maxFrameLength, 3, 4, -7, 0); - protocolDecoderMap = ImmutableMap.builder() - .put(ProtocolConstants.VERSION_0, new ProtocolDecoderV0()) - .put(ProtocolConstants.VERSION_1, new ProtocolDecoderV1()) - .build(); - } - - @Override - protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { - ByteBuf frame; - Object decoded; - byte version; - try { - if (isV0(in)) { - decoded = in; - version = ProtocolConstants.VERSION_0; - } else { - decoded = super.decode(ctx, in); - version = decideVersion(decoded); - } - - if (decoded instanceof ByteBuf) { - frame = (ByteBuf) decoded; - try { - ProtocolDecoder decoder = protocolDecoderMap.get(version); - if (decoder == null) { - throw new UnsupportedOperationException("Unsupported version: " + version); - } - return decoder.decodeFrame(frame); - } finally { - if (version != ProtocolConstants.VERSION_0) { - frame.release(); - } - } - } - } catch (Exception exx) { - LOGGER.error("Decode frame error, cause: {}", exx.getMessage()); - throw new DecodeException(exx); - } - return decoded; - } - - protected byte decideVersion(Object in) { - if (in instanceof ByteBuf) { - ByteBuf frame = (ByteBuf) in; - frame.markReaderIndex(); - byte b0 = frame.readByte(); - byte b1 = frame.readByte(); - if (ProtocolConstants.MAGIC_CODE_BYTES[0] != b0 - || ProtocolConstants.MAGIC_CODE_BYTES[1] != b1) { - throw new IllegalArgumentException("Unknown magic code: " + b0 + ", " + b1); - } - - byte version = frame.readByte(); - frame.resetReaderIndex(); - return version; - } - return -1; - } - - - protected boolean isV0(ByteBuf in) { - boolean isV0 = false; - in.markReaderIndex(); - byte b0 = in.readByte(); - byte b1 = in.readByte(); - // v1/v2/v3 : b2 = version - // v0 : 1st byte in FLAG(2byte:0x10/0x20/0x40/0x80) - byte b2 = in.readByte(); - if (ProtocolConstants.MAGIC_CODE_BYTES[0] == b0 - && ProtocolConstants.MAGIC_CODE_BYTES[1] == b1 - && 0 == b2) { - isV0 = true; - } - - in.resetReaderIndex(); - return isV0; - } - - protected boolean isV0(byte version) { - return version == ProtocolConstants.VERSION_0; - } -} \ No newline at end of file diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolEncoder.java b/core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolEncoder.java deleted file mode 100644 index e588b92b8ea..00000000000 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/CompatibleProtocolEncoder.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.seata.core.rpc.netty; - -import com.google.common.collect.ImmutableMap; -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.MessageToByteEncoder; -import org.apache.seata.common.util.StringUtils; -import org.apache.seata.core.protocol.ProtocolConstants; -import org.apache.seata.core.protocol.RpcMessage; -import org.apache.seata.core.protocol.Version; -import org.apache.seata.core.rpc.netty.v0.ProtocolEncoderV0; -import org.apache.seata.core.rpc.netty.v1.ProtocolEncoderV1; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Map; - -/** - * Compatible Protocol Encoder - *

    - *

  • Full Length: include all data
  • - *
  • Head Length: include head data from magic code to head map.
  • - *
  • Body Length: Full Length - Head Length
  • - *

    - */ -public class CompatibleProtocolEncoder extends MessageToByteEncoder { - - private static final Logger LOGGER = LoggerFactory.getLogger(CompatibleProtocolEncoder.class); - - private static Map protocolEncoderMap; - - public CompatibleProtocolEncoder() { - super(); - protocolEncoderMap = ImmutableMap.builder() - .put(ProtocolConstants.VERSION_0, new ProtocolEncoderV0()) - .put(ProtocolConstants.VERSION_1, new ProtocolEncoderV1()) - .build(); - } - - @Override - public void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) { - try { - if (msg instanceof RpcMessage) { - RpcMessage rpcMessage = (RpcMessage) msg; - String sdkVersion = rpcMessage.getOtherSideVersion(); - if (StringUtils.isBlank(sdkVersion)) { - sdkVersion = Version.getCurrent(); - } - byte protocolVersion = Version.calcProtocolVersion(sdkVersion); - ProtocolEncoder encoder = protocolEncoderMap.get(protocolVersion); - if (encoder == null) { - throw new UnsupportedOperationException("Unsupported protocolVersion: " + protocolVersion); - } - - encoder.encode(rpcMessage, out); - } else { - throw new UnsupportedOperationException("Not support this class:" + msg.getClass()); - } - } catch (Throwable e) { - LOGGER.error("Encode request error!", e); - } - } -} From e23efb6ac210ee63180b973b79e2eb5204396bbe Mon Sep 17 00:00:00 2001 From: jianbin Date: Thu, 27 Jun 2024 16:02:28 +0800 Subject: [PATCH 61/62] optimize: select channel handles based on protocol versions --- .../core/rpc/netty/v0/ProtocolDecoderV0.java | 1 - .../core/rpc/netty/v1/ProtocolDecoderV1.java | 35 ++----------------- 2 files changed, 2 insertions(+), 34 deletions(-) diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolDecoderV0.java b/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolDecoderV0.java index d051317d12f..635dbde3c98 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolDecoderV0.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolDecoderV0.java @@ -59,7 +59,6 @@ public class ProtocolDecoderV0 extends AbstractProtocolDecoder { private static final Logger LOGGER = LoggerFactory.getLogger(ProtocolDecoderV0.class); public ProtocolDecoderV0() { - } @Override diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolDecoderV1.java b/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolDecoderV1.java index ce48b3ce8ca..8d4e8391487 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolDecoderV1.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolDecoderV1.java @@ -20,15 +20,12 @@ import java.util.Map; import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.LengthFieldBasedFrameDecoder; import org.apache.seata.core.compressor.Compressor; import org.apache.seata.core.compressor.CompressorFactory; -import org.apache.seata.core.exception.DecodeException; import org.apache.seata.core.protocol.HeartbeatMessage; import org.apache.seata.core.protocol.ProtocolConstants; import org.apache.seata.core.protocol.RpcMessage; -import org.apache.seata.core.rpc.netty.ProtocolDecoder; +import org.apache.seata.core.rpc.netty.AbstractProtocolDecoder; import org.apache.seata.core.serializer.Serializer; import org.apache.seata.core.serializer.SerializerServiceLoader; import org.apache.seata.core.serializer.SerializerType; @@ -64,20 +61,12 @@ * @see ProtocolEncoderV1 * @since 0.7.0 */ -public class ProtocolDecoderV1 extends LengthFieldBasedFrameDecoder implements ProtocolDecoder { +public class ProtocolDecoderV1 extends AbstractProtocolDecoder { private static final Logger LOGGER = LoggerFactory.getLogger(ProtocolDecoderV1.class); private final List supportDeSerializerTypes; public ProtocolDecoderV1() { - /* - int maxFrameLength, - int lengthFieldOffset, magic code is 2B, and version is 1B, and then FullLength. so value is 3 - int lengthFieldLength, FullLength is int(4B). so values is 4 - int lengthAdjustment, FullLength include all data and read 7 bytes before, so the left length is (FullLength-7). so values is -7 - int initialBytesToStrip we will check magic code and version self, so do not strip any bytes. so values is 0 - */ - super(ProtocolConstants.MAX_FRAME_LENGTH, 3, 4, -7, 0); supportDeSerializerTypes = SerializerServiceLoader.getSupportedSerializers(); if (supportDeSerializerTypes.isEmpty()) { throw new IllegalArgumentException("No serializer found"); @@ -140,24 +129,4 @@ public RpcMessage decodeFrame(ByteBuf frame) { return rpcMessage.protocolMsg2RpcMsg(); } - @Override - protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { - Object decoded; - try { - decoded = super.decode(ctx, in); - if (decoded instanceof ByteBuf) { - ByteBuf frame = (ByteBuf)decoded; - try { - return decodeFrame(frame); - } finally { - frame.release(); - } - } - } catch (Exception exx) { - LOGGER.error("Decode frame error, cause: {}", exx.getMessage()); - throw new DecodeException(exx); - } - return decoded; - } - } From f4b6b72e3fa7dad2f4ae78117780982579f7874d Mon Sep 17 00:00:00 2001 From: jianbin Date: Thu, 27 Jun 2024 16:28:45 +0800 Subject: [PATCH 62/62] optimize: select channel handles based on protocol versions --- .../rpc/netty/AbstractProtocolDecoder.java | 63 ------------------- .../core/rpc/netty/v0/ProtocolDecoderV0.java | 35 ++++++++++- .../core/rpc/netty/v1/ProtocolDecoderV1.java | 35 ++++++++++- 3 files changed, 66 insertions(+), 67 deletions(-) delete mode 100644 core/src/main/java/org/apache/seata/core/rpc/netty/AbstractProtocolDecoder.java diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractProtocolDecoder.java b/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractProtocolDecoder.java deleted file mode 100644 index 82735451964..00000000000 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/AbstractProtocolDecoder.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.seata.core.rpc.netty; - -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.LengthFieldBasedFrameDecoder; -import org.apache.seata.core.exception.DecodeException; -import org.apache.seata.core.protocol.ProtocolConstants; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public abstract class AbstractProtocolDecoder extends LengthFieldBasedFrameDecoder implements ProtocolDecoder { - - protected Logger logger = LoggerFactory.getLogger(getClass()); - - public AbstractProtocolDecoder() { - /* - int maxFrameLength, - int lengthFieldOffset, magic code is 2B, and version is 1B, and then FullLength. so value is 3 - int lengthFieldLength, FullLength is int(4B). so values is 4 - int lengthAdjustment, FullLength include all data and read 7 bytes before, so the left length is (FullLength-7). so values is -7 - int initialBytesToStrip we will check magic code and version self, so do not strip any bytes. so values is 0 - */ - super(ProtocolConstants.MAX_FRAME_LENGTH, 3, 4); - } - - @Override - protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { - Object decoded; - try { - decoded = super.decode(ctx, in); - if (decoded instanceof ByteBuf) { - ByteBuf frame = (ByteBuf)decoded; - try { - return decodeFrame(frame); - } finally { - frame.release(); - } - } - } catch (Exception exx) { - logger.error("Decode frame error, cause: {}", exx.getMessage()); - throw new DecodeException(exx); - } - return decoded; - } - - -} diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolDecoderV0.java b/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolDecoderV0.java index 635dbde3c98..d14ce91ceaf 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolDecoderV0.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/v0/ProtocolDecoderV0.java @@ -17,11 +17,14 @@ package org.apache.seata.core.rpc.netty.v0; import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.LengthFieldBasedFrameDecoder; +import org.apache.seata.core.exception.DecodeException; import org.apache.seata.core.protocol.HeartbeatMessage; import org.apache.seata.core.protocol.ProtocolConstants; import org.apache.seata.core.protocol.RpcMessage; -import org.apache.seata.core.rpc.netty.AbstractProtocolDecoder; +import org.apache.seata.core.rpc.netty.ProtocolDecoder; import org.apache.seata.core.serializer.Serializer; import org.apache.seata.core.serializer.SerializerServiceLoader; import org.apache.seata.core.serializer.SerializerType; @@ -54,11 +57,19 @@ * * @see ProtocolEncoderV0 */ -public class ProtocolDecoderV0 extends AbstractProtocolDecoder { +public class ProtocolDecoderV0 extends LengthFieldBasedFrameDecoder implements ProtocolDecoder { private static final Logger LOGGER = LoggerFactory.getLogger(ProtocolDecoderV0.class); public ProtocolDecoderV0() { + /* + int maxFrameLength, + int lengthFieldOffset, magic code is 2B, and version is 1B, and then FullLength. so value is 3 + int lengthFieldLength, FullLength is int(4B). so values is 4 + int lengthAdjustment, FullLength include all data and read 7 bytes before, so the left length is (FullLength-7). so values is -7 + int initialBytesToStrip we will check magic code and version self, so do not strip any bytes. so values is 0 + */ + super(ProtocolConstants.MAX_FRAME_LENGTH, 3, 4, -7, 0); } @Override @@ -131,4 +142,24 @@ public RpcMessage decodeFrame(ByteBuf in) { return rpcMessage.protocolMsg2RpcMsg(); } + @Override + protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { + Object decoded; + try { + decoded = super.decode(ctx, in); + if (decoded instanceof ByteBuf) { + ByteBuf frame = (ByteBuf)decoded; + try { + return decodeFrame(frame); + } finally { + frame.release(); + } + } + } catch (Exception exx) { + LOGGER.error("Decode frame error, cause: {}", exx.getMessage()); + throw new DecodeException(exx); + } + return decoded; + } + } diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolDecoderV1.java b/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolDecoderV1.java index 8d4e8391487..ce48b3ce8ca 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolDecoderV1.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/v1/ProtocolDecoderV1.java @@ -20,12 +20,15 @@ import java.util.Map; import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.LengthFieldBasedFrameDecoder; import org.apache.seata.core.compressor.Compressor; import org.apache.seata.core.compressor.CompressorFactory; +import org.apache.seata.core.exception.DecodeException; import org.apache.seata.core.protocol.HeartbeatMessage; import org.apache.seata.core.protocol.ProtocolConstants; import org.apache.seata.core.protocol.RpcMessage; -import org.apache.seata.core.rpc.netty.AbstractProtocolDecoder; +import org.apache.seata.core.rpc.netty.ProtocolDecoder; import org.apache.seata.core.serializer.Serializer; import org.apache.seata.core.serializer.SerializerServiceLoader; import org.apache.seata.core.serializer.SerializerType; @@ -61,12 +64,20 @@ * @see ProtocolEncoderV1 * @since 0.7.0 */ -public class ProtocolDecoderV1 extends AbstractProtocolDecoder { +public class ProtocolDecoderV1 extends LengthFieldBasedFrameDecoder implements ProtocolDecoder { private static final Logger LOGGER = LoggerFactory.getLogger(ProtocolDecoderV1.class); private final List supportDeSerializerTypes; public ProtocolDecoderV1() { + /* + int maxFrameLength, + int lengthFieldOffset, magic code is 2B, and version is 1B, and then FullLength. so value is 3 + int lengthFieldLength, FullLength is int(4B). so values is 4 + int lengthAdjustment, FullLength include all data and read 7 bytes before, so the left length is (FullLength-7). so values is -7 + int initialBytesToStrip we will check magic code and version self, so do not strip any bytes. so values is 0 + */ + super(ProtocolConstants.MAX_FRAME_LENGTH, 3, 4, -7, 0); supportDeSerializerTypes = SerializerServiceLoader.getSupportedSerializers(); if (supportDeSerializerTypes.isEmpty()) { throw new IllegalArgumentException("No serializer found"); @@ -129,4 +140,24 @@ public RpcMessage decodeFrame(ByteBuf frame) { return rpcMessage.protocolMsg2RpcMsg(); } + @Override + protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { + Object decoded; + try { + decoded = super.decode(ctx, in); + if (decoded instanceof ByteBuf) { + ByteBuf frame = (ByteBuf)decoded; + try { + return decodeFrame(frame); + } finally { + frame.release(); + } + } + } catch (Exception exx) { + LOGGER.error("Decode frame error, cause: {}", exx.getMessage()); + throw new DecodeException(exx); + } + return decoded; + } + }