> lines) throws Exception {
@@ -79,7 +102,7 @@ public static DynamicAnnotationMatch loadClass(DynamicAnnotationRuntime runtime)
}
public static void compileTheJavaSrcFile(File... srcFiles) {
- JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+ final JavaCompiler compiler = javac;
try (StandardJavaFileManager fileMgr = compiler.getStandardFileManager(null, null, null)) {
JavaCompiler.CompilationTask t = compiler.getTask(null, fileMgr, null, null, null, fileMgr.getJavaFileObjects(srcFiles));
t.call();
diff --git a/source/src/main/java/io/mycat/mycat2/sqlparser/byteArrayInterface/mycat/MYCATSQLParser.java b/source/src/main/java/io/mycat/mycat2/sqlparser/byteArrayInterface/mycat/MYCATSQLParser.java
index 8096c4c..cf24029 100644
--- a/source/src/main/java/io/mycat/mycat2/sqlparser/byteArrayInterface/mycat/MYCATSQLParser.java
+++ b/source/src/main/java/io/mycat/mycat2/sqlparser/byteArrayInterface/mycat/MYCATSQLParser.java
@@ -13,32 +13,44 @@
* Created by yanjunli on 2017/9/26.
*/
public class MYCATSQLParser {
-
-
public static int pickMycat(int pos, final int arrayCount, BufferSQLContext context, HashArray hashArray, ByteArrayInterface sql){
long longHash = hashArray.getHash(pos);
if (TokenHash.SWITCH == longHash) {
TokenizerUtil.debug(pos, context);
return pickSwitch(++pos, arrayCount, context, hashArray, sql);
- } else {
+ } else if (TokenHash.SHOW == longHash) {
+ TokenizerUtil.debug(pos, context);
+ return pickShow(++pos, arrayCount, context, hashArray, sql);
+ } else {
throw new InvalidParameterException(" the current mycat command is not support!!");
}
}
- public static int pickSwitch(int pos, final int arrayCount, BufferSQLContext context, HashArray hashArray, ByteArrayInterface sql){
+ private static int pickSwitch(int pos, final int arrayCount, BufferSQLContext context, HashArray hashArray, ByteArrayInterface sql){
long longHash = hashArray.getHash(pos);
- if(TokenHash.REPL == longHash){
+ if (TokenHash.REPL == longHash){
context.setSQLType(BufferSQLContext.MYCAT_SWITCH_REPL);
TokenizerUtil.debug(pos, context);
return pickSwitchRepl(pos, arrayCount, context, hashArray, sql);
- }else{
+ } else {
throw new InvalidParameterException(" the current mycat command is not support!!");
}
}
-
- public static int pickSwitchRepl(int pos, final int arrayCount, BufferSQLContext context, HashArray hashArray, ByteArrayInterface sql){
+
+ private static int pickSwitchRepl(int pos, final int arrayCount, BufferSQLContext context, HashArray hashArray, ByteArrayInterface sql){
context.getMyCmdValue().set(IntTokenHash.REPL_NAME, hashArray.getPos(++pos), hashArray.getSize(pos), hashArray.getHash(pos));
context.getMyCmdValue().set(IntTokenHash.REPL_METABEAN_INDEX, hashArray.getPos(++pos), hashArray.getSize(pos), hashArray.getHash(pos));
return pos;
}
+
+ private static int pickShow(int pos, final int arrayCount, BufferSQLContext context, HashArray hashArray, ByteArrayInterface sql){
+ long longHash = hashArray.getHash(pos);
+ if (TokenHash.CONFIGS == longHash){
+ context.setSQLType(BufferSQLContext.MYCAT_SHOW_CONFIGS);
+ TokenizerUtil.debug(pos, context);
+ return pos;
+ } else {
+ throw new InvalidParameterException(" the current mycat command is not support!!");
+ }
+ }
}
diff --git a/source/src/main/java/io/mycat/mycat2/tasks/BackendConCreateTask.java b/source/src/main/java/io/mycat/mycat2/tasks/BackendConCreateTask.java
index 9be8403..a482bf9 100644
--- a/source/src/main/java/io/mycat/mycat2/tasks/BackendConCreateTask.java
+++ b/source/src/main/java/io/mycat/mycat2/tasks/BackendConCreateTask.java
@@ -20,6 +20,7 @@
import io.mycat.mysql.packet.HandshakePacket;
import io.mycat.mysql.packet.MySQLPacket;
import io.mycat.proxy.BufferPool;
+import io.mycat.util.ErrorCode;
import io.mycat.util.ParseUtil;
import io.mycat.util.SecurityUtil;
@@ -61,6 +62,16 @@ public void onSocketRead(MySQLSession session) throws IOException {
session.curMSQLPackgInf, false)) {// 没有读到数据或者报文不完整
return;
}
+
+ if(MySQLPacket.ERROR_PACKET == session.curMSQLPackgInf.pkgType){
+ errPkg = new ErrorPacket();
+ errPkg.packetId = session.proxyBuffer.getByte(session.curMSQLPackgInf.startPos
+ + ParseUtil.mysql_packetHeader_length);
+ errPkg.read(session.proxyBuffer);
+ logger.warn("backend authed failed. Err No. " + errPkg.errno + "," + errPkg.message);
+ this.finished(false);
+ return;
+ }
if (!welcomePkgReceived) {
handshake = new HandshakePacket();
@@ -100,13 +111,6 @@ public void onSocketRead(MySQLSession session) throws IOException {
if (session.curMSQLPackgInf.pkgType == MySQLPacket.OK_PACKET) {
logger.debug("backend authed suceess ");
this.finished(true);
- } else if (session.curMSQLPackgInf.pkgType == MySQLPacket.ERROR_PACKET) {
- errPkg = new ErrorPacket();
- errPkg.packetId = session.proxyBuffer.getByte(session.curMSQLPackgInf.startPos
- + ParseUtil.mysql_packetHeader_length);
- errPkg.read(session.proxyBuffer);
- logger.warn("backend authed failed. Err No. " + errPkg.errno + "," + errPkg.message);
- this.finished(false);
}
}
}
@@ -125,9 +129,10 @@ public void onConnect(SelectionKey theKey, MySQLSession userSession, boolean suc
} else {
errPkg = new ErrorPacket();
+ errPkg.packetId = 1;
+ errPkg.errno = ErrorCode.ERR_CONNECT_SOCKET;
errPkg.message = logInfo;
finished(false);
-
}
}
diff --git a/source/src/main/java/io/mycat/mycat2/tasks/BackendHeartbeatTask.java b/source/src/main/java/io/mycat/mycat2/tasks/BackendHeartbeatTask.java
index 1591ccf..b59f2ed 100644
--- a/source/src/main/java/io/mycat/mycat2/tasks/BackendHeartbeatTask.java
+++ b/source/src/main/java/io/mycat/mycat2/tasks/BackendHeartbeatTask.java
@@ -134,7 +134,6 @@ void onRsFinish(MySQLSession session,boolean success,String msg) {
reactor.addMySQLSession(metaBean, session);
switch(repBean.getReplicaBean().getRepType()){
- case GROUP_REPLICATION:
case MASTER_SLAVE:
masterSlaveHeartbeat();
break;
diff --git a/source/src/main/java/io/mycat/mysql/packet/FieldPacket.java b/source/src/main/java/io/mycat/mysql/packet/FieldPacket.java
new file mode 100644
index 0000000..3c95109
--- /dev/null
+++ b/source/src/main/java/io/mycat/mysql/packet/FieldPacket.java
@@ -0,0 +1,159 @@
+/*
+ * Copyright (c) 2013, OpenCloudDB/MyCAT and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software;Designed and Developed mainly by many Chinese
+ * opensource volunteers. you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License version 2 only, as published by the
+ * Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Any questions about this component can be directed to it's project Web address
+ * https://code.google.com/p/opencloudb/.
+ *
+ */
+package io.mycat.mysql.packet;
+
+import io.mycat.proxy.ProxyBuffer;
+import io.mycat.util.BufferUtil;
+
+/**
+ * From Server To Client, part of Result Set Packets. One for each column in the
+ * result set. Thus, if the value of field_columns in the Result Set Header
+ * Packet is 3, then the Field Packet occurs 3 times.
+ *
+ *
+ * Bytes Name
+ * ----- ----
+ * n (Length Coded String) catalog
+ * n (Length Coded String) db
+ * n (Length Coded String) table
+ * n (Length Coded String) org_table
+ * n (Length Coded String) name
+ * n (Length Coded String) org_name
+ * 1 (filler)
+ * 2 charsetNumber
+ * 4 length
+ * 1 type
+ * 2 flags
+ * 1 decimals
+ * 2 (filler), always 0x00
+ * n (Length Coded Binary) default
+ *
+ * @see http://forge.mysql.com/wiki/MySQL_Internals_ClientServer_Protocol#Field_Packet
+ *
+ *
+ * @author mycat
+ */
+public class FieldPacket extends MySQLPacket {
+ private static final byte[] DEFAULT_CATALOG = "def".getBytes();
+ private static final byte[] FILLER = new byte[2];
+
+ public byte[] catalog = DEFAULT_CATALOG;
+ public byte[] db;
+ public byte[] table;
+ public byte[] orgTable;
+ public byte[] name;
+ public byte[] orgName;
+ public int charsetIndex;
+ public long length;
+ public int type;
+ public int flags;
+ public byte decimals;
+ public byte[] definition;
+
+ /**
+ * 把字节数组转变成FieldPacket
+ */
+ public void read(byte[] data) {
+ MySQLMessage mm = new MySQLMessage(data);
+ this.packetLength = mm.readUB3();
+ this.packetId = mm.read();
+ readBody(mm);
+ }
+
+ @Override
+ public void write(ProxyBuffer buffer) {
+ buffer.writeFixInt(3, calcPacketSize());
+ buffer.writeByte(packetId);
+ writeBody(buffer);
+ }
+
+ @Override
+ public int calcPacketSize() {
+ int size = (catalog == null ? 1 : BufferUtil.getLength(catalog));
+ size += (db == null ? 1 : BufferUtil.getLength(db));
+ size += (table == null ? 1 : BufferUtil.getLength(table));
+ size += (orgTable == null ? 1 : BufferUtil.getLength(orgTable));
+ size += (name == null ? 1 : BufferUtil.getLength(name));
+ size += (orgName == null ? 1 : BufferUtil.getLength(orgName));
+ size += 13;// 1+2+4+1+2+1+2
+ if (definition != null) {
+ size += BufferUtil.getLength(definition);
+ }
+ return size;
+ }
+
+ @Override
+ protected String getPacketInfo() {
+ return "MySQL Field Packet";
+ }
+
+ private void readBody(MySQLMessage mm) {
+ this.catalog = mm.readBytesWithLength();
+ this.db = mm.readBytesWithLength();
+ this.table = mm.readBytesWithLength();
+ this.orgTable = mm.readBytesWithLength();
+ this.name = mm.readBytesWithLength();
+ this.orgName = mm.readBytesWithLength();
+ mm.move(1);
+ this.charsetIndex = mm.readUB2();
+ this.length = mm.readUB4();
+ this.type = mm.read() & 0xff;
+ this.flags = mm.readUB2();
+ this.decimals = mm.read();
+ mm.move(FILLER.length);
+ if (mm.hasRemaining()) {
+ this.definition = mm.readBytesWithLength();
+ }
+ }
+
+ private void writeBody(ProxyBuffer buffer) {
+ writeBytesWithNullable(buffer, catalog);
+ writeBytesWithNullable(buffer, db);
+ writeBytesWithNullable(buffer, table);
+ writeBytesWithNullable(buffer, orgTable);
+ writeBytesWithNullable(buffer, name);
+ writeBytesWithNullable(buffer, orgName);
+ buffer.writeByte((byte) 0x0C);
+ buffer.writeFixInt(2, charsetIndex);
+ buffer.writeFixInt(4, length);
+ buffer.writeByte((byte) (type & 0xff));
+ buffer.writeFixInt(2, flags);
+ buffer.writeByte(decimals);
+ buffer.writeByte((byte)0x00);
+ buffer.writeByte((byte)0x00);
+ //buffer.position(buffer.position() + FILLER.length);
+ if (definition != null) {
+ writeBytesWithNullable(buffer, definition);
+ }
+ }
+
+ private void writeBytesWithNullable(ProxyBuffer buffer, byte[] bytes) {
+ byte nullVal = 0;
+ if (bytes == null) {
+ buffer.writeByte(nullVal);
+ } else {
+ buffer.writeLenencBytes(bytes);
+ }
+ }
+}
\ No newline at end of file
diff --git a/source/src/main/java/io/mycat/mysql/packet/ResultSetHeaderPacket.java b/source/src/main/java/io/mycat/mysql/packet/ResultSetHeaderPacket.java
new file mode 100644
index 0000000..12ce97f
--- /dev/null
+++ b/source/src/main/java/io/mycat/mysql/packet/ResultSetHeaderPacket.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) 2013, OpenCloudDB/MyCAT and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software;Designed and Developed mainly by many Chinese
+ * opensource volunteers. you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License version 2 only, as published by the
+ * Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Any questions about this component can be directed to it's project Web address
+ * https://code.google.com/p/opencloudb/.
+ *
+ */
+package io.mycat.mysql.packet;
+
+import io.mycat.proxy.ProxyBuffer;
+import io.mycat.util.BufferUtil;
+
+/**
+ * From server to client after command, if no error and result set -- that is,
+ * if the command was a query which returned a result set. The Result Set Header
+ * Packet is the first of several, possibly many, packets that the server sends
+ * for result sets. The order of packets for a result set is:
+ *
+ *
+ * (Result Set Header Packet) the number of columns
+ * (Field Packets) column descriptors
+ * (EOF Packet) marker: end of Field Packets
+ * (Row Data Packets) row contents
+ * (EOF Packet) marker: end of Data Packets
+ *
+ * Bytes Name
+ * ----- ----
+ * 1-9 (Length-Coded-Binary) field_count
+ * 1-9 (Length-Coded-Binary) extra
+ *
+ * @see http://forge.mysql.com/wiki/MySQL_Internals_ClientServer_Protocol#Result_Set_Header_Packet
+ *
+ *
+ * @author mycat
+ */
+public class ResultSetHeaderPacket extends MySQLPacket {
+ public int fieldCount;
+ public long extra;
+
+ public void read(byte[] data) {
+ MySQLMessage mm = new MySQLMessage(data);
+ this.packetLength = mm.readUB3();
+ this.packetId = mm.read();
+ this.fieldCount = (int) mm.readLength();
+ if (mm.hasRemaining()) {
+ this.extra = mm.readLength();
+ }
+ }
+
+ @Override
+ public void write(ProxyBuffer buffer) {
+ buffer.writeFixInt(3, calcPacketSize());
+ buffer.writeByte(packetId);
+ buffer.writeFixInt(1, fieldCount);
+ if (extra > 0) {
+ buffer.writeLenencInt(extra);
+ }
+ }
+
+ @Override
+ public int calcPacketSize() {
+ int size = BufferUtil.getLength(fieldCount);
+ if (extra > 0) {
+ size += BufferUtil.getLength(extra);
+ }
+ return size;
+ }
+
+ @Override
+ protected String getPacketInfo() {
+ return "MySQL ResultSetHeader Packet";
+ }
+}
\ No newline at end of file
diff --git a/source/src/main/java/io/mycat/mysql/packet/RowDataPacket.java b/source/src/main/java/io/mycat/mysql/packet/RowDataPacket.java
new file mode 100644
index 0000000..b1530bf
--- /dev/null
+++ b/source/src/main/java/io/mycat/mysql/packet/RowDataPacket.java
@@ -0,0 +1,113 @@
+/*
+ * Copyright (c) 2013, OpenCloudDB/MyCAT and/or its affiliates. All rights reserved. DO NOT ALTER OR
+ * REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software;Designed and Developed mainly by many Chinese opensource volunteers.
+ * you can redistribute it and/or modify it under the terms of the GNU General Public License
+ * version 2 only, as published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
+ * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version 2 along with this work;
+ * if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA.
+ *
+ * Any questions about this component can be directed to it's project Web address
+ * https://code.google.com/p/opencloudb/.
+ *
+ */
+package io.mycat.mysql.packet;
+
+import io.mycat.proxy.ProxyBuffer;
+import io.mycat.util.BufferUtil;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * From server to client. One packet for each row in the result set.
+ *
+ *
+ *
+ * Bytes Name
+ * ----- ----
+ * n (Length Coded String) (column value)
+ * ...
+ *
+ * (column value): The data in the column, as a character string.
+ * If a column is defined as non-character, the
+ * server converts the value into a character
+ * before sending it. Since the value is a Length
+ * Coded String, a NULL can be represented with a
+ * single byte containing 251(see the description
+ * of Length Coded Strings in section "Elements" above).
+ *
+ * @see http://forge.mysql.com/wiki/MySQL_Internals_ClientServer_Protocol#Row_Data_Packet
+ *
+ *
+ * @author mycat
+ */
+public class RowDataPacket extends MySQLPacket {
+ private static final byte NULL_MARK = (byte) 251;
+ private static final byte EMPTY_MARK = (byte) 0;
+ public int fieldCount;
+ public final List fieldValues;
+
+ public RowDataPacket(int fieldCount) {
+ this.fieldCount = fieldCount;
+ this.fieldValues = new ArrayList(fieldCount);
+ }
+
+ public void add(byte[] value) {
+ // 这里应该修改value
+ fieldValues.add(value);
+ }
+
+ public void addFieldCount(int add) {
+ // 这里应该修改field
+ fieldCount = fieldCount + add;
+ }
+
+ public void read(byte[] data) {
+ MySQLMessage mm = new MySQLMessage(data);
+ packetLength = mm.readUB3();
+ packetId = mm.read();
+ for (int i = 0; i < fieldCount; i++) {
+ fieldValues.add(mm.readBytesWithLength());
+ }
+ }
+
+ @Override
+ public void write(ProxyBuffer buffer) {
+ buffer.writeFixInt(3, calcPacketSize());
+ buffer.writeByte(packetId);
+ for (int i = 0; i < fieldCount; i++) {
+ byte[] fv = fieldValues.get(i);
+ if (fv == null) {
+ buffer.writeByte(RowDataPacket.NULL_MARK);
+ } else if (fv.length == 0) {
+ buffer.writeByte(RowDataPacket.EMPTY_MARK);
+ } else {
+ buffer.writeLenencBytes(fv);
+ }
+ }
+ }
+
+ @Override
+ public int calcPacketSize() {
+ int size = 0;
+ for (int i = 0; i < fieldCount; i++) {
+ byte[] v = fieldValues.get(i);
+ size += (v == null || v.length == 0) ? 1 : BufferUtil.getLength(v);
+ }
+ return size;
+ }
+
+ @Override
+ protected String getPacketInfo() {
+ return "MySQL RowData Packet";
+ }
+}
diff --git a/source/src/main/java/io/mycat/proxy/AbstractSession.java b/source/src/main/java/io/mycat/proxy/AbstractSession.java
index 458bf05..995e5d5 100644
--- a/source/src/main/java/io/mycat/proxy/AbstractSession.java
+++ b/source/src/main/java/io/mycat/proxy/AbstractSession.java
@@ -142,11 +142,6 @@ public boolean readFromChannel() throws IOException {
proxyBuffer.writeIndex = buffer.position();
return readed > 0;
}
-
- private void closeSocket(boolean normal,String msg) throws IOException{
- close(false,msg);
- throw new ClosedChannelException();
- }
protected abstract void doTakeReadOwner();
diff --git a/source/src/main/java/io/mycat/proxy/MycatReactorThread.java b/source/src/main/java/io/mycat/proxy/MycatReactorThread.java
index 2c37215..5e2b04c 100644
--- a/source/src/main/java/io/mycat/proxy/MycatReactorThread.java
+++ b/source/src/main/java/io/mycat/proxy/MycatReactorThread.java
@@ -212,7 +212,7 @@ public void getMysqlSession(MySQLMetaBean mySQLMetaBean,AsynTaskCallBack