funky-eyes commented on code in PR #6208:
URL: https://github.com/apache/incubator-seata/pull/6208#discussion_r1438064621


##########
serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java:
##########
@@ -16,72 +16,43 @@
  */
 package io.seata.serializer.seata;
 
-import java.nio.ByteBuffer;
-
-import io.netty.buffer.ByteBuf;
-import io.netty.buffer.Unpooled;
+import com.google.common.collect.ImmutableMap;
 import io.seata.common.loader.LoadLevel;
-import io.seata.common.util.BufferUtils;
-import io.seata.core.protocol.AbstractMessage;
+import io.seata.common.loader.Scope;
+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.util.Map;
 
 /**
  * The Seata codec.
- *
  */
-@LoadLevel(name = "SEATA")
+@LoadLevel(name = "SEATA", scope = Scope.PROTOTYPE)
 public class SeataSerializer implements Serializer {
 
-    @Override
-    public <T> byte[] serialize(T t) {
-        if (!(t instanceof AbstractMessage)) {
-            throw new IllegalArgumentException("AbstractMessage isn't 
available.");
-        }
-        AbstractMessage abstractMessage = (AbstractMessage)t;
-        //typecode
-        short typecode = abstractMessage.getTypeCode();
-        //msg codec
-        MessageSeataCodec messageCodec = 
MessageCodecFactory.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);
+    Serializer versionSeataSerializer;
 
-        //typecode + body
-        ByteBuffer byteBuffer = ByteBuffer.allocate(2 + body.length);
-        byteBuffer.putShort(typecode);
-        byteBuffer.put(body);
+    static Map<Byte, Serializer> VERSION_MAP = ImmutableMap.<Byte, 
Serializer>builder()
+            .put(ProtocolConstants.VERSION_0, new SeataSerializerV0())
+            .put(ProtocolConstants.VERSION_1, new SeataSerializerV1())
+            .build();
 
-        BufferUtils.flip(byteBuffer);
-        byte[] content = new byte[byteBuffer.limit()];
-        byteBuffer.get(content);
-        return content;
+    public SeataSerializer(Byte version){
+        versionSeataSerializer =  VERSION_MAP.get(version);

Review Comment:
   为什么需要一个map,我认为使用if else构建对应版本的序列化器即可,因为本身spi加载到时已经对SeataSerializer进行了缓存
   Why do you need a map? I think it is enough to use if else to build the 
corresponding version of the serializer, because the SeataSerializer has 
already been cached when the spi is loaded.



##########
serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/protocol/MergeResultMessageCodec.java:
##########
@@ -24,13 +24,16 @@
 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.
  *
  */
 public class MergeResultMessageCodec extends AbstractMessageCodec {
 
+    protected MessageCodecFactory factory = new MessageCodecFactoryV1();

Review Comment:
   为什么MessageCodecFactory不通过spi形式加载,我认为它只需要一个单例然后复用即可
   Why doesn't MessageCodecFactory load as spi, I think it just needs a 
singleton and then reuse it



##########
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);

Review Comment:
   除了序列化,v0的protocolencoder和decoder在哪?因为v0是没有header这些内容的
   Except for serialization, where are the protocolencoder and decoder of v0? 
Because v0 has no headers



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to