Search in sources :

Example 1 with QueryResult

use of io.mycat.statistic.stat.QueryResult in project Mycat-Server by MyCATApache.

the class SingleNodeHandler method okResponse.

/**
	 * insert/update/delete
	 * 
	 * okResponse():读取data字节数组,组成一个OKPacket,并调用ok.write(source)将结果写入前端连接FrontendConnection的写缓冲队列writeQueue中,
	 * 真正发送给应用是由对应的NIOSocketWR从写队列中读取ByteBuffer并返回的
	 */
@Override
public void okResponse(byte[] data, BackendConnection conn) {
    //
    this.netOutBytes += data.length;
    boolean executeResponse = conn.syncAndExcute();
    if (executeResponse) {
        ServerConnection source = session.getSource();
        OkPacket ok = new OkPacket();
        ok.read(data);
        boolean isCanClose2Client = (!rrs.isCallStatement()) || (rrs.isCallStatement() && !rrs.getProcedure().isResultSimpleValue());
        if (rrs.isLoadData()) {
            byte lastPackId = source.getLoadDataInfileHandler().getLastPackId();
            // OK_PACKET
            ok.packetId = ++lastPackId;
            source.getLoadDataInfileHandler().clear();
        } else if (isCanClose2Client) {
            // OK_PACKET
            ok.packetId = ++packetId;
        }
        if (isCanClose2Client) {
            session.releaseConnectionIfSafe(conn, LOGGER.isDebugEnabled(), false);
            endRunning();
        }
        ok.serverStatus = source.isAutocommit() ? 2 : 1;
        recycleResources();
        if (isCanClose2Client) {
            source.setLastInsertId(ok.insertId);
            ok.write(source);
        }
        this.affectedRows = ok.affectedRows;
        // add by lian
        // 解决sql统计中写操作永远为0
        QueryResult queryResult = new QueryResult(session.getSource().getUser(), rrs.getSqlType(), rrs.getStatement(), affectedRows, netInBytes, netOutBytes, startTime, System.currentTimeMillis(), 0);
        QueryResultDispatcher.dispatchQuery(queryResult);
    }
}
Also used : QueryResult(io.mycat.statistic.stat.QueryResult) OkPacket(io.mycat.net.mysql.OkPacket) ServerConnection(io.mycat.server.ServerConnection)

Example 2 with QueryResult

use of io.mycat.statistic.stat.QueryResult in project Mycat-Server by MyCATApache.

the class MultiNodeQueryHandler method rowEofResponse.

@Override
public void rowEofResponse(final byte[] eof, BackendConnection conn) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("on row end reseponse " + conn);
    }
    this.netOutBytes += eof.length;
    if (errorRepsponsed.get()) {
        // conn.close(this.error);
        return;
    }
    final ServerConnection source = session.getSource();
    if (!isCallProcedure) {
        if (clearIfSessionClosed(session)) {
            return;
        } else if (canClose(conn, false)) {
            return;
        }
    }
    if (decrementCountBy(1)) {
        if (!rrs.isCallStatement() || (rrs.isCallStatement() && rrs.getProcedure().isResultSimpleValue())) {
            if (this.autocommit && !session.getSource().isLocked()) {
                // clear all connections
                session.releaseConnections(false);
            }
            if (this.isFail() || session.closed()) {
                tryErrorFinished(true);
                return;
            }
        }
        if (dataMergeSvr != null) {
            try {
                dataMergeSvr.outputMergeResult(session, eof);
            } catch (Exception e) {
                handleDataProcessException(e);
            }
        } else {
            try {
                lock.lock();
                eof[3] = ++packetId;
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("last packet id:" + packetId);
                }
                source.write(eof);
            } finally {
                lock.unlock();
            }
        }
    }
    execCount++;
    if (execCount == rrs.getNodes().length) {
        int resultSize = source.getWriteQueue().size() * MycatServer.getInstance().getConfig().getSystem().getBufferPoolPageSize();
        //TODO: add by zhuam
        //查询结果派发
        QueryResult queryResult = new QueryResult(session.getSource().getUser(), rrs.getSqlType(), rrs.getStatement(), selectRows, 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 QueryResult

use of io.mycat.statistic.stat.QueryResult in project Mycat-Server by MyCATApache.

the class MultiNodeQueryHandler method okResponse.

@Override
public void okResponse(byte[] data, BackendConnection conn) {
    this.netOutBytes += data.length;
    boolean executeResponse = conn.syncAndExcute();
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("received ok response ,executeResponse:" + executeResponse + " from " + conn);
    }
    if (executeResponse) {
        ServerConnection source = session.getSource();
        OkPacket ok = new OkPacket();
        ok.read(data);
        //存储过程
        boolean isCanClose2Client = (!rrs.isCallStatement()) || (rrs.isCallStatement() && !rrs.getProcedure().isResultSimpleValue());
        ;
        if (!isCallProcedure) {
            if (clearIfSessionClosed(session)) {
                return;
            } else if (canClose(conn, false)) {
                return;
            }
        }
        lock.lock();
        try {
            // 判断是否是全局表,如果是,执行行数不做累加,以最后一次执行的为准。
            if (!rrs.isGlobalTable()) {
                affectedRows += ok.affectedRows;
            } else {
                affectedRows = ok.affectedRows;
            }
            if (ok.insertId > 0) {
                insertId = (insertId == 0) ? ok.insertId : Math.min(insertId, ok.insertId);
            }
        } finally {
            lock.unlock();
        }
        // 对于存储过程,其比较特殊,查询结果返回EndRow报文以后,还会再返回一个OK报文,才算结束
        boolean isEndPacket = isCallProcedure ? decrementOkCountBy(1) : decrementCountBy(1);
        if (isEndPacket && isCanClose2Client) {
            if (this.autocommit && !session.getSource().isLocked()) {
                // clear all connections
                session.releaseConnections(false);
            }
            if (this.isFail() || session.closed()) {
                tryErrorFinished(true);
                return;
            }
            lock.lock();
            try {
                if (rrs.isLoadData()) {
                    byte lastPackId = source.getLoadDataInfileHandler().getLastPackId();
                    // OK_PACKET
                    ok.packetId = ++lastPackId;
                    ok.message = ("Records: " + affectedRows + "  Deleted: 0  Skipped: 0  Warnings: 0").getBytes();
                    source.getLoadDataInfileHandler().clear();
                } else {
                    // OK_PACKET
                    ok.packetId = ++packetId;
                }
                ok.affectedRows = affectedRows;
                ok.serverStatus = source.isAutocommit() ? 2 : 1;
                if (insertId > 0) {
                    ok.insertId = insertId;
                    source.setLastInsertId(insertId);
                }
                ok.write(source);
            } catch (Exception e) {
                handleDataProcessException(e);
            } finally {
                lock.unlock();
            }
        }
        // add by lian
        // 解决sql统计中写操作永远为0
        execCount++;
        if (execCount == rrs.getNodes().length) {
            QueryResult queryResult = new QueryResult(session.getSource().getUser(), rrs.getSqlType(), rrs.getStatement(), selectRows, netInBytes, netOutBytes, startTime, System.currentTimeMillis(), 0);
            QueryResultDispatcher.dispatchQuery(queryResult);
        }
    }
}
Also used : QueryResult(io.mycat.statistic.stat.QueryResult) ServerConnection(io.mycat.server.ServerConnection)

Example 4 with QueryResult

use of io.mycat.statistic.stat.QueryResult in project Mycat-Server by MyCATApache.

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();
    source.write(buffer);
    //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)

Aggregations

ServerConnection (io.mycat.server.ServerConnection)4 QueryResult (io.mycat.statistic.stat.QueryResult)4 OkPacket (io.mycat.net.mysql.OkPacket)1