From 39d88c1d17c15698f2825ff54f664dfba07469cf Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Mon, 25 Dec 2023 18:21:34 +0800 Subject: [PATCH 01/26] 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 901b886a75396705203464a1d282fd7cf1376254 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Tue, 26 Dec 2023 14:49:09 +0800 Subject: [PATCH 02/26] 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 b51800497d07e9761c91ef52dc24fe5fdd1b08af Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Tue, 26 Dec 2023 14:57:56 +0800 Subject: [PATCH 03/26] 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 04/26] 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 05/26] 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 d8c19a42d129895637460f29fda6984d7f861061 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Fri, 29 Dec 2023 10:55:35 +0800 Subject: [PATCH 06/26] 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 07/26] 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 08/26] 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 227464b864bd3be048c4d14f2f21cf58856ae881 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Fri, 29 Dec 2023 11:18:29 +0800 Subject: [PATCH 09/26] 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 0afa8458b1c228f915dc62ccef582c10daa19ab3 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Fri, 29 Dec 2023 12:30:47 +0800 Subject: [PATCH 10/26] 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 11/26] 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 dfd9bf22a2257d56f193491fd237df02fef814e9 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Fri, 29 Dec 2023 17:20:41 +0800 Subject: [PATCH 12/26] 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 13/26] 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 181a421ef32ababcb3fca0546d726323f7faf7cd Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Thu, 4 Jan 2024 14:51:27 +0800 Subject: [PATCH 14/26] 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 15/26] 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 342d306e27b3bea647c800e95cc8b204ee307b9d Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Mon, 22 Jan 2024 18:13:56 +0800 Subject: [PATCH 16/26] 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 17/26] 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 18/26] 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 704669a4b39dd17059a508c8f6b54d20ee605403 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Wed, 28 Feb 2024 11:46:34 +0800 Subject: [PATCH 19/26] 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 2f5feb0552dc5eebdcea8d55db83b4664f33fa76 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Tue, 30 Apr 2024 10:03:23 +0800 Subject: [PATCH 20/26] 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 89c0674fbc17c679c5ca50d42fdb59703bdea320 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Tue, 7 May 2024 14:21:28 +0800 Subject: [PATCH 21/26] double lock check --- .../apache/seata/serializer/seata/SeataSerializer.java | 8 ++++++-- 1 file changed, 6 insertions(+), 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 53b36da424b..12cf8e3834b 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 @@ -66,7 +66,9 @@ private SeataSerializerV1() { public static SeataSerializerV1 getInstance() { if (instance == null) { synchronized (SeataSerializerV1.class) { - instance = new SeataSerializerV1(); + if(instance == null){ + instance = new SeataSerializerV1(); + } } } return instance; @@ -117,7 +119,9 @@ private SeataSerializerV0() { public static SeataSerializerV0 getInstance() { if (instance == null) { synchronized (SeataSerializerV0.class) { - instance = new SeataSerializerV0(); + if(instance == null){ + instance = new SeataSerializerV0(); + } } } return instance; From b6498528bc703575665b4c269a9a5f25352dca94 Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Tue, 7 May 2024 14:25:55 +0800 Subject: [PATCH 22/26] double lock check --- .../org/apache/seata/serializer/seata/SeataSerializer.java | 4 ++-- 1 file changed, 2 insertions(+), 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 12cf8e3834b..17525c77a4c 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 @@ -66,7 +66,7 @@ private SeataSerializerV1() { public static SeataSerializerV1 getInstance() { if (instance == null) { synchronized (SeataSerializerV1.class) { - if(instance == null){ + if (instance == null) { instance = new SeataSerializerV1(); } } @@ -119,7 +119,7 @@ private SeataSerializerV0() { public static SeataSerializerV0 getInstance() { if (instance == null) { synchronized (SeataSerializerV0.class) { - if(instance == null){ + if (instance == null) { instance = new SeataSerializerV0(); } } From 3cc6ca3c0a86467add26f2e96f567bf1fbeed9bc Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Tue, 7 May 2024 14:30:28 +0800 Subject: [PATCH 23/26] double lock check --- .../org/apache/seata/serializer/seata/SeataSerializer.java | 4 ++-- 1 file changed, 2 insertions(+), 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 17525c77a4c..0e5bdb74c8e 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 @@ -58,7 +58,7 @@ public T deserialize(byte[] bytes) { static class SeataSerializerV1 implements Serializer { - private static SeataSerializerV1 instance; + private static volatile SeataSerializerV1 instance; private SeataSerializerV1() { } @@ -111,7 +111,7 @@ public T deserialize(byte[] bytes) { } static class SeataSerializerV0 implements Serializer { - private static SeataSerializerV0 instance; + private static volatile SeataSerializerV0 instance; private SeataSerializerV0() { } From 9d199a1125f679c81f5b40edd38d781a60a8d4cd Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Fri, 21 Jun 2024 09:58:55 +0800 Subject: [PATCH 24/26] style --- .../apache/seata/core/serializer/SerializerServiceLoader.java | 4 +--- 1 file changed, 1 insertion(+), 3 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 e13d46efc83..f3d40b049db 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 @@ -46,7 +46,7 @@ public final class SerializerServiceLoader { private static final Logger LOGGER = LoggerFactory.getLogger(SerializerServiceLoader.class); private static final Configuration CONFIG = ConfigurationFactory.getInstance(); - private static final SerializerType[] DEFAULT_SERIALIZER_TYPE = new SerializerType[] {SEATA, PROTOBUF, KRYO, HESSIAN}; + private static final SerializerType[] DEFAULT_SERIALIZER_TYPE = new SerializerType[]{SEATA, PROTOBUF, KRYO, HESSIAN}; private static final String SPLIT_CHAR = ","; @@ -115,6 +115,4 @@ public static SerializerType getDefaultSerializerType() { return getSupportedSerializers().get(0); } -} - } \ No newline at end of file From dd9d808e99d6f0d4458b0685e2b1bc00f36afd0b Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Fri, 21 Jun 2024 10:55:20 +0800 Subject: [PATCH 25/26] style --- changes/en-us/2.x.md | 1 + changes/zh-cn/2.x.md | 2 ++ .../apache/seata/core/serializer/SerializerServiceLoader.java | 4 ++-- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md index ac8d07da8e7..1ee39f50af5 100644 --- a/changes/en-us/2.x.md +++ b/changes/en-us/2.x.md @@ -130,6 +130,7 @@ Add changes here for all PR submitted to the 2.x branch. - [[#6442](https://github.com/apache/incubator-seata/pull/6442)] clarify if conditions - [[#6487](https://github.com/apache/incubator-seata/pull/6487)] fix typo and package name - [[#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 ### refactor: - [[#6269](https://github.com/apache/incubator-seata/pull/6269)] standardize Seata Exception diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md index 7859e61dea4..361d279888e 100644 --- a/changes/zh-cn/2.x.md +++ b/changes/zh-cn/2.x.md @@ -130,6 +130,8 @@ - [[#6442](https://github.com/apache/incubator-seata/pull/6442)] 阐明 if - [[#6487](https://github.com/apache/incubator-seata/pull/6487)] 修复错误包名以及单词 - [[#6499](https://github.com/apache/incubator-seata/pull/6499)] 拆分 committing 和 rollbacking 状态的任务线程池 +- [[#6208](https://github.com/apache/incubator-seata/pull/6208)] 支持多版本的Seata序列化 + ### refactor: - [[#6269](https://github.com/apache/incubator-seata/pull/6269)] 统一Seata异常规范 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 f3d40b049db..0503fa8f731 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 @@ -48,13 +48,13 @@ public final class SerializerServiceLoader { private static final SerializerType[] DEFAULT_SERIALIZER_TYPE = new SerializerType[]{SEATA, PROTOBUF, KRYO, HESSIAN}; + private final static Map SERIALIZER_MAP = new HashMap<>(); + private static final String SPLIT_CHAR = ","; private SerializerServiceLoader() { } - private final static Map SERIALIZER_MAP = new HashMap<>(); - private static final String PROTOBUF_SERIALIZER_CLASS_NAME = "org.apache.seata.serializer.protobuf.ProtobufSerializer"; /** From ac90469b52b0d00386bf86ffb5513b6ba36e28bc Mon Sep 17 00:00:00 2001 From: "minghua.xie" Date: Fri, 21 Jun 2024 11:28:05 +0800 Subject: [PATCH 26/26] style --- changes/en-us/2.x.md | 1 + changes/zh-cn/2.x.md | 1 + 2 files changed, 2 insertions(+) diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md index 46cd7eef64c..a015a2ec220 100644 --- a/changes/en-us/2.x.md +++ b/changes/en-us/2.x.md @@ -32,5 +32,6 @@ Thanks to these contributors for their code commits. Please report an unintended - [tuwenlin](https://github.com/tuwenlin) - [YeonCheolGit](https://github.com/YeonCheolGit) - [liuqiufeng](https://github.com/liuqiufeng) +- [Bughue](https://github.com/Bughue) 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 7ba7967a3d1..7e8a0ba21c4 100644 --- a/changes/zh-cn/2.x.md +++ b/changes/zh-cn/2.x.md @@ -29,5 +29,6 @@ - [tuwenlin](https://github.com/tuwenlin) - [YeonCheolGit](https://github.com/YeonCheolGit) - [liuqiufeng](https://github.com/liuqiufeng) +- [Bughue](https://github.com/Bughue) 同时,我们收到了社区反馈的很多有价值的issue和建议,非常感谢大家。