You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by xi...@apache.org on 2021/01/28 12:01:45 UTC
[shardingsphere] branch master updated: handle exceptions when
encode protocol packet (#9207)
This is an automated email from the ASF dual-hosted git repository.
xiaoyu 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 c682a4d handle exceptions when encode protocol packet (#9207)
c682a4d is described below
commit c682a4db77d862d2c9b005b84e4a20d120d4107f
Author: Zhang Yonglun <zh...@apache.org>
AuthorDate: Thu Jan 28 20:01:19 2021 +0800
handle exceptions when encode protocol packet (#9207)
---
.../db/protocol/mysql/codec/MySQLPacketCodecEngine.java | 12 +++++++++++-
.../postgresql/codec/PostgreSQLPacketCodecEngine.java | 13 ++++++++++++-
2 files changed, 23 insertions(+), 2 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 c119c96..a8ce9a0 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
@@ -20,7 +20,9 @@ package org.apache.shardingsphere.db.protocol.mysql.codec;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import org.apache.shardingsphere.db.protocol.codec.DatabasePacketCodecEngine;
+import org.apache.shardingsphere.db.protocol.error.CommonErrorCode;
import org.apache.shardingsphere.db.protocol.mysql.packet.MySQLPacket;
+import org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLErrPacket;
import org.apache.shardingsphere.db.protocol.mysql.payload.MySQLPacketPayload;
import java.util.List;
@@ -48,11 +50,19 @@ public final class MySQLPacketCodecEngine implements DatabasePacketCodecEngine<M
@Override
public void encode(final ChannelHandlerContext context, final MySQLPacket message, final ByteBuf out) {
- try (MySQLPacketPayload payload = new MySQLPacketPayload(context.alloc().buffer())) {
+ MySQLPacketPayload payload = new MySQLPacketPayload(context.alloc().buffer());
+ try {
message.write(payload);
+ // CHECKSTYLE:OFF
+ } catch (final Exception ex) {
+ // CHECKSTYLE:ON
+ payload.getByteBuf().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();
}
}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/codec/PostgreSQLPacketCodecEngine.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/codec/PostgreSQLPacketCodecEngine.java
index 062c468..381e976 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/codec/PostgreSQLPacketCodecEngine.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/codec/PostgreSQLPacketCodecEngine.java
@@ -21,6 +21,7 @@ import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import org.apache.shardingsphere.db.protocol.codec.DatabasePacketCodecEngine;
import org.apache.shardingsphere.db.protocol.postgresql.packet.PostgreSQLPacket;
+import org.apache.shardingsphere.db.protocol.postgresql.packet.generic.PostgreSQLErrorResponsePacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.handshake.PostgreSQLSSLNegativePacket;
import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
@@ -56,13 +57,23 @@ public final class PostgreSQLPacketCodecEngine implements DatabasePacketCodecEng
@Override
public void encode(final ChannelHandlerContext context, final PostgreSQLPacket message, final ByteBuf out) {
- try (PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(context.alloc().buffer())) {
+ PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(context.alloc().buffer());
+ try {
message.write(payload);
+ // CHECKSTYLE:OFF
+ } catch (final Exception ex) {
+ // CHECKSTYLE:ON
+ payload.getByteBuf().resetWriterIndex();
+ PostgreSQLErrorResponsePacket postgreSQLErrorResponsePacket = new PostgreSQLErrorResponsePacket();
+ postgreSQLErrorResponsePacket.addField(PostgreSQLErrorResponsePacket.FIELD_TYPE_MESSAGE, ex.getMessage());
+ postgreSQLErrorResponsePacket.write(payload);
+ } finally {
if (!(message instanceof PostgreSQLSSLNegativePacket)) {
out.writeByte(message.getMessageType());
out.writeInt(payload.getByteBuf().readableBytes() + PostgreSQLPacket.PAYLOAD_LENGTH);
}
out.writeBytes(payload.getByteBuf());
+ payload.close();
}
}