Search in sources :

Example 16 with MycatException

use of io.mycat.MycatException in project Mycat2 by MyCATApache.

the class BackendConCreateHandler method handle.

public void handle(MySQLClientSession mysql) throws Exception {
    ProxyBuffer proxyBuffer = mysql.currentProxyBuffer().newBufferIfNeed();
    int totalPacketEndIndex = proxyBuffer.channelReadEndIndex();
    if (!mysql.readProxyPayloadFully()) {
        return;
    }
    MySQLPayloadType payloadType = mysql.getPayloadType();
    if (!welcomePkgReceived) {
        writeClientAuth(mysql);
        return;
    }
    // 收到切换登陆插件的包
    if (mysql.getPayloadType() == MySQLPayloadType.FIRST_EOF && mysql.getBackendPacketResolver().getState() == ComQueryState.AUTH_SWITCH_PLUGIN_RESPONSE) {
        // 重新发送密码验证
        MySQLPacket mySQLPacket = mysql.currentProxyPayload();
        AuthSwitchRequestPacket authSwitchRequestPacket = new AuthSwitchRequestPacket();
        authSwitchRequestPacket.readPayload(mySQLPacket);
        byte[] password = generatePassword(authSwitchRequestPacket);
        mySQLPacket = mysql.newCurrentProxyPacket(1024);
        mySQLPacket.writeBytes(password);
        mysql.writeCurrentProxyPacket(mySQLPacket, mysql.getPacketId() + 1);
        mysql.getBackendPacketResolver().setIsClientLoginRequest(true);
        return;
    }
    // 验证成功
    if (payloadType == MySQLPayloadType.FIRST_OK) {
        mysql.switchNioHandler(null);
        mysql.resetPacket();
        mysql.getBackendPacketResolver().setIsClientLoginRequest(false);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("successful create mysql backend connection:{}", mysql.sessionId());
        }
        callback.complete(mysql);
        return;
    }
    MySQLPacket mySQLPacket = mysql.getBackendPacketResolver().currentPayload();
    // 用公钥进行密码加密
    if (STR_CACHING_AUTH_STAGE.equals(stage) && authPluginName.equals(CachingSha2PasswordPlugin.PROTOCOL_PLUGIN_NAME)) {
        LOGGER.info("authPluginName:{} ", authPluginName);
        String publicKeyString = mySQLPacket.readEOFString();
        byte[] payload = CachingSha2PasswordPlugin.encrypt(mysqlVersion, publicKeyString, datasource.getPassword(), seed, CharsetUtil.getCharset(charsetIndex));
        mySQLPacket = mysql.newCurrentProxyPacket(1024);
        mySQLPacket.writeBytes(payload);
        mysql.writeCurrentProxyPacket(mySQLPacket, mysql.getPacketId() + 1);
        mysql.getBackendPacketResolver().setIsClientLoginRequest(true);
        stage = null;
        return;
    }
    int status = mySQLPacket.getByte(4) & 0xff;
    int fastAuthResult = mySQLPacket.getByte(5) & 0xff;
    if (status == 1 && fastAuthResult == 3) {
        // 验证成功继续 读取认证的ok包
        mysql.resetCurrentProxyPayload();
        proxyBuffer.channelReadEndIndex(totalPacketEndIndex);
        MySQLPacketResolver packetResolver = mysql.getBackendPacketResolver();
        mySQLPacket.packetReadStartIndex(packetResolver.getEndPos());
        mysql.getBackendPacketResolver().setIsClientLoginRequest(true);
        handle(mysql);
        return;
    }
    if (status == 1 && fastAuthResult == 4) {
        // 发送payload为02的包 然后读取公钥加密密码
        byte[] payload = { (byte) 2 };
        mySQLPacket = mysql.newCurrentProxyPacket(1024);
        mySQLPacket.writeBytes(payload);
        mysql.writeCurrentProxyPacket(mySQLPacket, 3);
        stage = STR_CACHING_AUTH_STAGE;
        mysql.getBackendPacketResolver().setIsClientLoginRequest(true);
        return;
    }
    // 连接不上
    ErrorPacketImpl errorPacket = new ErrorPacketImpl();
    errorPacket.readPayload(mySQLPacket);
    onException(mysql, new MycatException(errorPacket.getErrorCode(), errorPacket.getErrorMessageString()));
}
Also used : MycatException(io.mycat.MycatException) MySQLPayloadType(io.mycat.proxy.packet.MySQLPayloadType) MySQLPacketResolver(io.mycat.proxy.packet.MySQLPacketResolver)

Example 17 with MycatException

use of io.mycat.MycatException in project Mycat2 by MyCATApache.

the class ResultSetHandler method request.

/**
 * COM_QUERY 命令请求报文调用此方法 head是payload的第一个字节 data根据实际报文构造 该方法自动构造请求报文,生成报文序列号以及长度,
 * 但是被限制整个报文长度不超过proxybuffer的chunk大小,大小也不应该超过mysql拆分报文大小 如需构造大的报文,可以自行替换proxbuffer即可
 */
default void request(MySQLClientSession mysql, int head, byte[] data, ResultSetCallBack<MySQLClientSession> callBack) {
    if (data == null) {
        data = EMPTY;
    }
    assert (mysql.currentProxyBuffer() == null);
    int chunkSize = mysql.getIOThread().getBufPool().chunkSize();
    if (data.length > (chunkSize - 5) || data.length > MySQLPacketSplitter.MAX_PACKET_SIZE) {
        throw new MycatException("ResultSetHandler unsupport request length more than 1024 bytes");
    }
    mysql.setCurrentProxyBuffer(new ProxyBufferImpl(mysql.getIOThread().getBufPool()));
    MySQLPacket mySQLPacket = mysql.newCurrentProxyPacket(data.length + 5);
    mySQLPacket.writeByte((byte) head);
    mySQLPacket.writeBytes(data);
    request(mysql, mySQLPacket, mysql.setPacketId(0), callBack);
}
Also used : ProxyBufferImpl(io.mycat.proxy.buffer.ProxyBufferImpl) MycatException(io.mycat.MycatException) MySQLPacket(io.mycat.beans.mysql.packet.MySQLPacket)

Example 18 with MycatException

use of io.mycat.MycatException in project Mycat2 by MyCATApache.

the class ProxyBufferImpl method readFromChannel.

/**
 * 从读通道获取数据
 */
@Override
public boolean readFromChannel(SocketChannel channel) throws IOException {
    buffer.limit(buffer.capacity());
    int readEndIndex = this.readEndIndex;
    buffer.position(readEndIndex);
    int readed = channel.read(buffer);
    if (readed == -1) {
        throw new ClosedChannelException();
    } else if (readed == 0) {
        throw new MycatException("readed zero bytes ,Maybe a bug ,please fix it !!!!");
    }
    this.channelReadEndIndex(buffer.position());
    return readed > 0;
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) MycatException(io.mycat.MycatException)

Example 19 with MycatException

use of io.mycat.MycatException in project Mycat2 by MyCATApache.

the class ShowTriggersSQLHandler method onExecute.

@Override
protected Future<Void> onExecute(SQLRequest<MySqlShowTriggersStatement> request, MycatDataContext dataContext, Response response) {
    MySqlShowTriggersStatement ast = request.getAst();
    if (ast.getDatabase() == null && dataContext.getDefaultSchema() != null) {
        ast.setDatabase(new SQLIdentifierExpr(dataContext.getDefaultSchema()));
    }
    String database = ast.getDatabase().getSimpleName();
    SQLExpr like = ast.getLike();
    SQLExpr where = ast.getWhere();
    if (database == null) {
        return response.sendError(new MycatException("NO DATABASES SELECTED"));
    }
    String sql = toNormalSQL(request.getAst());
    return DrdsRunnerHelper.runOnDrds(dataContext, DrdsRunnerHelper.preParse(sql, dataContext.getDefaultSchema()), response);
}
Also used : MycatException(io.mycat.MycatException) MySqlShowTriggersStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTriggersStatement) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 20 with MycatException

use of io.mycat.MycatException in project Mycat2 by MyCATApache.

the class MycatCalciteMySqlNodeVisitor method visit.

@Override
public boolean visit(SQLExprTableSource x) {
    SqlNode table;
    SQLExpr expr = x.getExpr();
    SqlNodeList sqlNodes = convertHints((List) x.getHints(), x.computeAlias());
    if (expr instanceof SQLIdentifierExpr) {
        table = new SqlTableRef(SqlParserPos.ZERO, (SqlIdentifier) buildIdentifier((SQLIdentifierExpr) expr), sqlNodes);
    } else if (expr instanceof SQLPropertyExpr) {
        table = new SqlTableRef(SqlParserPos.ZERO, (SqlIdentifier) buildIdentifier((SQLPropertyExpr) expr), sqlNodes);
    } else {
        throw new MycatException("not support : " + expr);
    }
    if (x.getAlias() != null) {
        SqlIdentifier alias = new SqlIdentifier(SQLUtils.normalize(x.computeAlias()), SqlParserPos.ZERO);
        SqlBasicCall as = new SqlBasicCall(SqlStdOperatorTable.AS, new SqlNode[] { table, alias }, SqlParserPos.ZERO);
        sqlNode = as;
    } else {
        sqlNode = table;
    }
    return false;
}
Also used : MycatException(io.mycat.MycatException) CalciteSqlBasicCall(com.alibaba.druid.support.calcite.CalciteSqlBasicCall)

Aggregations

MycatException (io.mycat.MycatException)31 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)3 SQLName (com.alibaba.druid.sql.ast.SQLName)3 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)3 MySQLClientSession (io.mycat.proxy.session.MySQLClientSession)3 SQLException (java.sql.SQLException)3 CalciteSqlBasicCall (com.alibaba.druid.support.calcite.CalciteSqlBasicCall)2 ColumnDefPacket (io.mycat.beans.mysql.packet.ColumnDefPacket)2 DatasourceConfig (io.mycat.config.DatasourceConfig)2 ProxyBufferImpl (io.mycat.proxy.buffer.ProxyBufferImpl)2 ReactorEnvThread (io.mycat.proxy.reactor.ReactorEnvThread)2 Future (io.vertx.core.Future)2 PromiseInternal (io.vertx.core.impl.future.PromiseInternal)2 Statement (java.sql.Statement)2 DateString (org.apache.calcite.util.DateString)2 TimeString (org.apache.calcite.util.TimeString)2 TimestampString (org.apache.calcite.util.TimestampString)2 SQLBooleanExpr (com.alibaba.druid.sql.ast.expr.SQLBooleanExpr)1 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)1 SQLShowTablesStatement (com.alibaba.druid.sql.ast.statement.SQLShowTablesStatement)1