From c1515acbe33193304c814aeadfcf630d19a22710 Mon Sep 17 00:00:00 2001 From: wt_better Date: Tue, 6 Aug 2024 11:17:58 +0800 Subject: [PATCH] optimize: deserialize performance optimize (#6727) --- changes/en-us/2.x.md | 1 + changes/zh-cn/2.x.md | 1 + .../serializer/seata/MessageCodecFactory.java | 208 +++++++----------- .../serializer/seata/SeataSerializer.java | 7 +- 4 files changed, 86 insertions(+), 131 deletions(-) diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md index 18addd09085..2e6f4907f87 100644 --- a/changes/en-us/2.x.md +++ b/changes/en-us/2.x.md @@ -37,6 +37,7 @@ Add changes here for all PR submitted to the 2.x branch. - [[#6667](https://github.com/apache/incubator-seata/pull/6667)] optimize Namingserver log output - [[#6687](https://github.com/apache/incubator-seata/pull/6687)] delete static code built on the frontend - [[#6700](https://github.com/apache/incubator-seata/pull/6700)] remove sdk version checking +- [[#6727](https://github.com/apache/incubator-seata/pull/6727)] deserialize performance optimize ### refactor: diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md index 66ef670815c..d88e714cae7 100644 --- a/changes/zh-cn/2.x.md +++ b/changes/zh-cn/2.x.md @@ -37,6 +37,7 @@ - [[#6667](https://github.com/apache/incubator-seata/pull/6667)] 优化Namingserver日志输出 - [[#6687](https://github.com/apache/incubator-seata/pull/6687)] 删除前端构建的静态代码 - [[#6700](https://github.com/apache/incubator-seata/pull/6700)] 去掉sdk版本检查 +- [[#6727](https://github.com/apache/incubator-seata/pull/6727)] 反序列化性能优化 ### refactor: diff --git a/serializer/seata-serializer-seata/src/main/java/org/apache/seata/serializer/seata/MessageCodecFactory.java b/serializer/seata-serializer-seata/src/main/java/org/apache/seata/serializer/seata/MessageCodecFactory.java index 2675751cd28..6231c72bbde 100644 --- a/serializer/seata-serializer-seata/src/main/java/org/apache/seata/serializer/seata/MessageCodecFactory.java +++ b/serializer/seata-serializer-seata/src/main/java/org/apache/seata/serializer/seata/MessageCodecFactory.java @@ -137,90 +137,69 @@ public static MessageSeataCodec getMessageCodec(short typeCode, byte version) { case MessageType.TYPE_BATCH_RESULT_MSG: msgCodec = new BatchResultMessageCodec(version); break; - default: - break; - } - - if (msgCodec != null) { - return msgCodec; - } - - try { - msgCodec = getMergeRequestMessageSeataCodec(typeCode, version); - } catch (Exception exx) { - } - - if (msgCodec != null) { - return msgCodec; - } - - msgCodec = getMergeResponseMessageSeataCodec(typeCode, version); - - return msgCodec; - } - - /** - * Gets merge request instance by code. - * - * @param typeCode the type code - * @return the merge request instance by code - */ - protected static MessageSeataCodec getMergeRequestMessageSeataCodec(int typeCode, byte version) { - switch (typeCode) { case MessageType.TYPE_GLOBAL_BEGIN: - return new GlobalBeginRequestCodec(); + msgCodec = new GlobalBeginRequestCodec(); + break; case MessageType.TYPE_GLOBAL_COMMIT: - return new GlobalCommitRequestCodec(); + msgCodec = new GlobalCommitRequestCodec(); + break; case MessageType.TYPE_GLOBAL_ROLLBACK: - return new GlobalRollbackRequestCodec(); + msgCodec = new GlobalRollbackRequestCodec(); + break; case MessageType.TYPE_GLOBAL_STATUS: - return new GlobalStatusRequestCodec(); + msgCodec = new GlobalStatusRequestCodec(); + break; case MessageType.TYPE_GLOBAL_LOCK_QUERY: - return new GlobalLockQueryRequestCodec(); + msgCodec = new GlobalLockQueryRequestCodec(); + break; case MessageType.TYPE_BRANCH_REGISTER: - return new BranchRegisterRequestCodec(); + msgCodec = new BranchRegisterRequestCodec(); + break; case MessageType.TYPE_BRANCH_STATUS_REPORT: - return new BranchReportRequestCodec(); - case MessageType.TYPE_GLOBAL_REPORT: - return new GlobalReportRequestCodec(); - default: - throw new IllegalArgumentException("not support typeCode," + typeCode); - } - } - - /** - * Gets merge response instance by code. - * - * @param typeCode the type code - * @return the merge response instance by code - */ - protected static MessageSeataCodec getMergeResponseMessageSeataCodec(int typeCode, byte version) { - switch (typeCode) { + msgCodec = new BranchReportRequestCodec(); + break; case MessageType.TYPE_GLOBAL_BEGIN_RESULT: - return new GlobalBeginResponseCodec(); + msgCodec = new GlobalBeginResponseCodec(); + break; case MessageType.TYPE_GLOBAL_COMMIT_RESULT: - return new GlobalCommitResponseCodec(); + msgCodec = new GlobalCommitResponseCodec(); + break; case MessageType.TYPE_GLOBAL_ROLLBACK_RESULT: - return new GlobalRollbackResponseCodec(); + msgCodec = new GlobalRollbackResponseCodec(); + break; case MessageType.TYPE_GLOBAL_STATUS_RESULT: - return new GlobalStatusResponseCodec(); + msgCodec = new GlobalStatusResponseCodec(); + break; case MessageType.TYPE_GLOBAL_LOCK_QUERY_RESULT: - return new GlobalLockQueryResponseCodec(); + msgCodec = new GlobalLockQueryResponseCodec(); + break; case MessageType.TYPE_BRANCH_REGISTER_RESULT: - return new BranchRegisterResponseCodec(); + msgCodec = new BranchRegisterResponseCodec(); + break; case MessageType.TYPE_BRANCH_STATUS_REPORT_RESULT: - return new BranchReportResponseCodec(); + msgCodec = new BranchReportResponseCodec(); + break; case MessageType.TYPE_BRANCH_COMMIT_RESULT: - return new BranchCommitResponseCodec(); + msgCodec = new BranchCommitResponseCodec(); + break; case MessageType.TYPE_BRANCH_ROLLBACK_RESULT: - return new BranchRollbackResponseCodec(); + msgCodec = new BranchRollbackResponseCodec(); + break; case MessageType.TYPE_RM_DELETE_UNDOLOG: - return new UndoLogDeleteRequestCodec(); + msgCodec = new UndoLogDeleteRequestCodec(); + break; case MessageType.TYPE_GLOBAL_REPORT_RESULT: - return new GlobalReportResponseCodec(); + msgCodec = new GlobalReportResponseCodec(); + break; default: - throw new IllegalArgumentException("not support typeCode," + typeCode); + break; + } + + if (msgCodec != null) { + return msgCodec; } + + throw new IllegalArgumentException("not support typeCode," + typeCode); } /** @@ -268,86 +247,63 @@ public static AbstractMessage getMessage(short typeCode) { case MessageType.TYPE_BATCH_RESULT_MSG: abstractMessage = new BatchResultMessage(); break; - default: - break; - } - - if (abstractMessage != null) { - return abstractMessage; - } - - try { - abstractMessage = getMergeRequestInstanceByCode(typeCode); - } catch (Exception exx) { - } - - if (abstractMessage != null) { - return abstractMessage; - } - - return getMergeResponseInstanceByCode(typeCode); - } - - /** - * Gets merge request instance by code. - * - * @param typeCode the type code - * @return the merge request instance by code - */ - protected static AbstractMessage getMergeRequestInstanceByCode(int typeCode) { - switch (typeCode) { case MessageType.TYPE_GLOBAL_BEGIN: - return new GlobalBeginRequest(); + abstractMessage = new GlobalBeginRequest(); + break; case MessageType.TYPE_GLOBAL_COMMIT: - return new GlobalCommitRequest(); + abstractMessage = new GlobalCommitRequest(); + break; case MessageType.TYPE_GLOBAL_ROLLBACK: - return new GlobalRollbackRequest(); + abstractMessage = new GlobalRollbackRequest(); + break; case MessageType.TYPE_GLOBAL_STATUS: - return new GlobalStatusRequest(); + abstractMessage = new GlobalStatusRequest(); + break; case MessageType.TYPE_GLOBAL_LOCK_QUERY: - return new GlobalLockQueryRequest(); + abstractMessage = new GlobalLockQueryRequest(); + break; case MessageType.TYPE_BRANCH_REGISTER: - return new BranchRegisterRequest(); + abstractMessage = new BranchRegisterRequest(); + break; case MessageType.TYPE_BRANCH_STATUS_REPORT: - return new BranchReportRequest(); - case MessageType.TYPE_GLOBAL_REPORT: - return new GlobalReportRequest(); - default: - throw new IllegalArgumentException("not support typeCode," + typeCode); - } - } - - /** - * Gets merge response instance by code. - * - * @param typeCode the type code - * @return the merge response instance by code - */ - protected static AbstractMessage getMergeResponseInstanceByCode(int typeCode) { - switch (typeCode) { + abstractMessage = new BranchReportRequest(); + break; case MessageType.TYPE_GLOBAL_BEGIN_RESULT: - return new GlobalBeginResponse(); + abstractMessage = new GlobalBeginResponse(); + break; case MessageType.TYPE_GLOBAL_COMMIT_RESULT: - return new GlobalCommitResponse(); + abstractMessage = new GlobalCommitResponse(); + break; case MessageType.TYPE_GLOBAL_ROLLBACK_RESULT: - return new GlobalRollbackResponse(); + abstractMessage = new GlobalRollbackResponse(); + break; case MessageType.TYPE_GLOBAL_STATUS_RESULT: - return new GlobalStatusResponse(); + abstractMessage = new GlobalStatusResponse(); + break; case MessageType.TYPE_GLOBAL_LOCK_QUERY_RESULT: - return new GlobalLockQueryResponse(); + abstractMessage = new GlobalLockQueryResponse(); + break; case MessageType.TYPE_BRANCH_REGISTER_RESULT: - return new BranchRegisterResponse(); + abstractMessage = new BranchRegisterResponse(); + break; case MessageType.TYPE_BRANCH_STATUS_REPORT_RESULT: - return new BranchReportResponse(); + abstractMessage = new BranchReportResponse(); + break; case MessageType.TYPE_BRANCH_COMMIT_RESULT: - return new BranchCommitResponse(); + abstractMessage = new BranchCommitResponse(); + break; case MessageType.TYPE_BRANCH_ROLLBACK_RESULT: - return new BranchRollbackResponse(); - case MessageType.TYPE_GLOBAL_REPORT_RESULT: - return new GlobalReportResponse(); + abstractMessage = new BranchRollbackResponse(); + break; default: - throw new IllegalArgumentException("not support typeCode," + typeCode); + break; + } + + if (abstractMessage != null) { + return abstractMessage; } + + throw new IllegalArgumentException("not support typeCode," + typeCode); } } 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 0e5bdb74c8e..81730db3d24 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 @@ -172,11 +172,8 @@ private static T deserializeByVersion(byte[] bytes, byte version) { 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 + ByteBuffer in = byteBuffer.slice(); + //new message AbstractMessage abstractMessage = MessageCodecFactory.getMessage(typecode); //get messageCodec MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typecode, version);