This is an automated email from the ASF dual-hosted git repository.

panjuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 0d00b55  Reduce buffer allocation in MySQL Codec (#13086)
0d00b55 is described below

commit 0d00b55076104b9bae7045158c1ae9311ffd92c9
Author: 吴伟杰 <[email protected]>
AuthorDate: Mon Oct 18 14:01:40 2021 +0800

    Reduce buffer allocation in MySQL Codec (#13086)
---
 .../mysql/codec/MySQLPacketCodecEngine.java        | 18 ++++++----
 .../mysql/codec/MySQLPacketCodecEngineTest.java    | 40 ++++++++++++++++------
 2 files changed, 41 insertions(+), 17 deletions(-)

diff --git 
a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/codec/MySQLPacketCodecEngine.java
 
b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/codec/MySQLPacketCodecEngine.java
index f99cea5..bf5f38f 100644
--- 
a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/codec/MySQLPacketCodecEngine.java
+++ 
b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/codec/MySQLPacketCodecEngine.java
@@ -54,22 +54,28 @@ public final class MySQLPacketCodecEngine implements 
DatabasePacketCodecEngine<M
     
     @Override
     public void encode(final ChannelHandlerContext context, final MySQLPacket 
message, final ByteBuf out) {
-        MySQLPacketPayload payload = new 
MySQLPacketPayload(context.alloc().buffer());
+        MySQLPacketPayload payload = new 
MySQLPacketPayload(prepareMessageHeader(out).markWriterIndex());
         try {
             message.write(payload);
             // CHECKSTYLE:OFF
         } catch (final Exception ex) {
             // CHECKSTYLE:ON
-            payload.getByteBuf().resetWriterIndex();
+            out.resetWriterIndex();
             new MySQLErrPacket(1, CommonErrorCode.UNKNOWN_EXCEPTION, 
ex.getMessage()).write(payload);
         } finally {
-            out.writeMediumLE(payload.getByteBuf().readableBytes());
-            out.writeByte(message.getSequenceId());
-            out.writeBytes(payload.getByteBuf());
-            payload.close();
+            updateMessageHeader(out, message.getSequenceId());
         }
     }
     
+    private ByteBuf prepareMessageHeader(final ByteBuf out) {
+        return out.writeInt(0);
+    }
+    
+    private void updateMessageHeader(final ByteBuf byteBuf, final int 
sequenceId) {
+        byteBuf.setMediumLE(0, byteBuf.readableBytes() - PAYLOAD_LENGTH - 
SEQUENCE_LENGTH);
+        byteBuf.setByte(3, sequenceId);
+    }
+    
     @Override
     public MySQLPacketPayload createPacketPayload(final ByteBuf message) {
         return new MySQLPacketPayload(message);
diff --git 
a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/codec/MySQLPacketCodecEngineTest.java
 
b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/codec/MySQLPacketCodecEngineTest.java
index 7017596..865480a 100644
--- 
a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/codec/MySQLPacketCodecEngineTest.java
+++ 
b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/codec/MySQLPacketCodecEngineTest.java
@@ -18,12 +18,11 @@
 package org.apache.shardingsphere.db.protocol.mysql.codec;
 
 import io.netty.buffer.ByteBuf;
-import io.netty.buffer.ByteBufAllocator;
 import io.netty.channel.ChannelHandlerContext;
 import org.apache.shardingsphere.db.protocol.mysql.packet.MySQLPacket;
+import org.apache.shardingsphere.db.protocol.mysql.payload.MySQLPacketPayload;
 import org.junit.Test;
 import org.junit.runner.RunWith;
-import org.mockito.ArgumentMatchers;
 import org.mockito.Mock;
 import org.mockito.junit.MockitoJUnitRunner;
 
@@ -34,6 +33,9 @@ import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
@@ -89,18 +91,34 @@ public final class MySQLPacketCodecEngineTest {
     
     @Test
     public void assertEncode() {
-        ByteBufAllocator byteBufAllocator = mock(ByteBufAllocator.class);
-        when(context.alloc()).thenReturn(byteBufAllocator);
-        ByteBuf payloadByteBuf = mock(ByteBuf.class);
-        when(byteBufAllocator.buffer()).thenReturn(payloadByteBuf);
-        when(payloadByteBuf.readableBytes()).thenReturn(50);
+        when(byteBuf.writeInt(anyInt())).thenReturn(byteBuf);
+        when(byteBuf.markWriterIndex()).thenReturn(byteBuf);
+        when(byteBuf.readableBytes()).thenReturn(8);
         MySQLPacket actualMessage = mock(MySQLPacket.class);
         when(actualMessage.getSequenceId()).thenReturn(1);
         new MySQLPacketCodecEngine().encode(context, actualMessage, byteBuf);
-        verify(actualMessage).write(ArgumentMatchers.any());
-        verify(byteBuf).writeMediumLE(50);
-        verify(byteBuf).writeByte(1);
-        verify(byteBuf).writeBytes(payloadByteBuf);
+        verify(byteBuf).writeInt(0);
+        verify(byteBuf).markWriterIndex();
+        verify(actualMessage).write(any(MySQLPacketPayload.class));
+        verify(byteBuf).setMediumLE(0, 4);
+        verify(byteBuf).setByte(3, 1);
+    }
+    
+    @Test
+    public void assertEncodeOccursException() {
+        when(byteBuf.writeInt(anyInt())).thenReturn(byteBuf);
+        when(byteBuf.markWriterIndex()).thenReturn(byteBuf);
+        when(byteBuf.readableBytes()).thenReturn(12);
+        RuntimeException ex = mock(RuntimeException.class);
+        MySQLPacket actualMessage = mock(MySQLPacket.class);
+        doThrow(ex).when(actualMessage).write(any(MySQLPacketPayload.class));
+        when(actualMessage.getSequenceId()).thenReturn(2);
+        new MySQLPacketCodecEngine().encode(context, actualMessage, byteBuf);
+        verify(byteBuf).writeInt(0);
+        verify(byteBuf).markWriterIndex();
+        verify(byteBuf).resetWriterIndex();
+        verify(byteBuf).setMediumLE(0, 8);
+        verify(byteBuf).setByte(3, 2);
     }
     
     @Test

Reply via email to