Search in sources :

Example 1 with ServerConnection

use of io.mycat.server.ServerConnection in project Mycat_plus by coderczp.

the class SingleNodeHandler method backConnectionErr.

private void backConnectionErr(ErrorPacket errPkg, BackendConnection conn) {
    endRunning();
    ServerConnection source = session.getSource();
    String errUser = source.getUser();
    String errHost = source.getHost();
    int errPort = source.getLocalPort();
    String errmgs = " errno:" + errPkg.errno + " " + new String(errPkg.message);
    LOGGER.warn("execute  sql err :" + errmgs + " con:" + conn + " frontend host:" + errHost + "/" + errPort + "/" + errUser);
    session.releaseConnectionIfSafe(conn, LOGGER.isDebugEnabled(), false);
    source.setTxInterrupt(errmgs);
    /**
     * TODO: 修复全版本BUG
     *
     * BUG复现:
     * 1、MysqlClient:  SELECT 9223372036854775807 + 1;
     * 2、MyCatServer:  ERROR 1690 (22003): BIGINT value is out of range in '(9223372036854775807 + 1)'
     * 3、MysqlClient: ERROR 2013 (HY000): Lost connection to MySQL server during query
     *
     * Fixed后
     * 1、MysqlClient:  SELECT 9223372036854775807 + 1;
     * 2、MyCatServer:  ERROR 1690 (22003): BIGINT value is out of range in '(9223372036854775807 + 1)'
     * 3、MysqlClient: ERROR 1690 (22003): BIGINT value is out of range in '(9223372036854775807 + 1)'
     */
    // 由于 pakcetId != 1 造成的问题
    errPkg.packetId = 1;
    errPkg.write(source);
    recycleResources();
}
Also used : ServerConnection(io.mycat.server.ServerConnection)

Example 2 with ServerConnection

use of io.mycat.server.ServerConnection in project Mycat_plus by coderczp.

the class SingleNodeHandler method rowEofResponse.

/**
 * select
 *
 * 行结束标志返回时触发,将EOF标志写入缓冲区,最后调用source.write(buffer)将缓冲区放入前端连接的写缓冲队列中,等待NIOSocketWR将其发送给应用
 */
@Override
public void rowEofResponse(byte[] eof, BackendConnection conn) {
    this.netOutBytes += eof.length;
    ServerConnection source = session.getSource();
    conn.recordSql(source.getHost(), source.getSchema(), node.getStatement());
    // 判断是调用存储过程的话不能在这里释放链接
    if (!rrs.isCallStatement() || (rrs.isCallStatement() && rrs.getProcedure().isResultSimpleValue())) {
        session.releaseConnectionIfSafe(conn, LOGGER.isDebugEnabled(), false);
        endRunning();
    }
    eof[3] = ++packetId;
    buffer = source.writeToBuffer(eof, allocBuffer());
    int resultSize = source.getWriteQueue().size() * MycatServer.getInstance().getConfig().getSystem().getBufferPoolPageSize();
    resultSize = resultSize + buffer.position();
    MiddlerResultHandler middlerResultHandler = session.getMiddlerResultHandler();
    if (middlerResultHandler != null) {
        middlerResultHandler.secondEexcute();
    } else {
        source.write(buffer);
    }
    source.setExecuteSql(null);
    // TODO: add by zhuam
    // 查询结果派发
    QueryResult queryResult = new QueryResult(session.getSource().getUser(), rrs.getSqlType(), rrs.getStatement(), affectedRows, netInBytes, netOutBytes, startTime, System.currentTimeMillis(), resultSize);
    QueryResultDispatcher.dispatchQuery(queryResult);
}
Also used : QueryResult(io.mycat.statistic.stat.QueryResult) ServerConnection(io.mycat.server.ServerConnection)

Example 3 with ServerConnection

use of io.mycat.server.ServerConnection in project Mycat_plus by coderczp.

the class SingleNodeHandler method execute.

public void execute() throws Exception {
    startTime = System.currentTimeMillis();
    ServerConnection sc = session.getSource();
    this.isRunning = true;
    this.packetId = 0;
    final BackendConnection conn = session.getTarget(node);
    LOGGER.debug("rrs.getRunOnSlave() " + rrs.getRunOnSlave());
    // 实现 master/slave注解
    node.setRunOnSlave(rrs.getRunOnSlave());
    LOGGER.debug("node.getRunOnSlave() " + node.getRunOnSlave());
    if (session.tryExistsCon(conn, node)) {
        _execute(conn);
    } else {
        // create new connection
        MycatConfig conf = MycatServer.getInstance().getConfig();
        LOGGER.debug("node.getRunOnSlave() " + node.getRunOnSlave());
        // 实现 master/slave注解
        node.setRunOnSlave(rrs.getRunOnSlave());
        LOGGER.debug("node.getRunOnSlave() " + node.getRunOnSlave());
        PhysicalDBNode dn = conf.getDataNodes().get(node.getName());
        dn.getConnection(dn.getDatabase(), sc.isAutocommit(), node, this, node);
    }
}
Also used : PhysicalDBNode(io.mycat.backend.datasource.PhysicalDBNode) BackendConnection(io.mycat.backend.BackendConnection) ServerConnection(io.mycat.server.ServerConnection) MycatConfig(io.mycat.config.MycatConfig)

Example 4 with ServerConnection

use of io.mycat.server.ServerConnection in project Mycat_plus by coderczp.

the class SingleNodeHandler method connectionError.

@Override
public void connectionError(Throwable e, BackendConnection conn) {
    endRunning();
    ErrorPacket err = new ErrorPacket();
    err.packetId = ++packetId;
    err.errno = ErrorCode.ER_NEW_ABORTING_CONNECTION;
    err.message = StringUtil.encode(e.getMessage(), session.getSource().getCharset());
    ServerConnection source = session.getSource();
    source.write(err.write(allocBuffer(), source, true));
}
Also used : ErrorPacket(io.mycat.net.mysql.ErrorPacket) ServerConnection(io.mycat.server.ServerConnection)

Example 5 with ServerConnection

use of io.mycat.server.ServerConnection in project Mycat_plus by coderczp.

the class ShowSession method execute.

public static void execute(ManagerConnection c) {
    ByteBuffer buffer = c.allocate();
    // write header
    buffer = header.write(buffer, c, true);
    // write fields
    for (FieldPacket field : fields) {
        buffer = field.write(buffer, c, true);
    }
    // write eof
    buffer = eof.write(buffer, c, true);
    // write rows
    byte packetId = eof.packetId;
    for (NIOProcessor process : MycatServer.getInstance().getProcessors()) {
        for (FrontendConnection front : process.getFrontends().values()) {
            if (!(front instanceof ServerConnection)) {
                continue;
            }
            ServerConnection sc = (ServerConnection) front;
            RowDataPacket row = getRow(sc, c.getCharset());
            if (row != null) {
                row.packetId = ++packetId;
                buffer = row.write(buffer, c, true);
            }
        }
    }
    // write last eof
    EOFPacket lastEof = new EOFPacket();
    lastEof.packetId = ++packetId;
    buffer = lastEof.write(buffer, c, true);
    // write buffer
    c.write(buffer);
}
Also used : FrontendConnection(io.mycat.net.FrontendConnection) RowDataPacket(io.mycat.net.mysql.RowDataPacket) EOFPacket(io.mycat.net.mysql.EOFPacket) ServerConnection(io.mycat.server.ServerConnection) NIOProcessor(io.mycat.net.NIOProcessor) ByteBuffer(java.nio.ByteBuffer) FieldPacket(io.mycat.net.mysql.FieldPacket)

Aggregations

ServerConnection (io.mycat.server.ServerConnection)36 ByteBuffer (java.nio.ByteBuffer)18 EOFPacket (io.mycat.net.mysql.EOFPacket)12 RowDataPacket (io.mycat.net.mysql.RowDataPacket)10 FieldPacket (io.mycat.net.mysql.FieldPacket)8 QueryResult (io.mycat.statistic.stat.QueryResult)8 IOException (java.io.IOException)8 FrontendConnection (io.mycat.net.FrontendConnection)4 NIOProcessor (io.mycat.net.NIOProcessor)4 OkPacket (io.mycat.net.mysql.OkPacket)4 ResultSetHeaderPacket (io.mycat.net.mysql.ResultSetHeaderPacket)4 BackendConnection (io.mycat.backend.BackendConnection)2 PhysicalDBNode (io.mycat.backend.datasource.PhysicalDBNode)2 MySQLMessage (io.mycat.backend.mysql.MySQLMessage)2 MySQLConnection (io.mycat.backend.mysql.nio.MySQLConnection)2 MycatConfig (io.mycat.config.MycatConfig)2 BufferHolder (io.mycat.memory.unsafe.row.BufferHolder)2 UnsafeRow (io.mycat.memory.unsafe.row.UnsafeRow)2 UnsafeRowWriter (io.mycat.memory.unsafe.row.UnsafeRowWriter)2 BinaryRowDataPacket (io.mycat.net.mysql.BinaryRowDataPacket)2