Search in sources :

Example 1 with OkPacket

use of io.mycat.net.mysql.OkPacket 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 OkPacket

use of io.mycat.net.mysql.OkPacket in project Mycat-Server by MyCATApache.

the class UnLockTablesHandler method execute.

public void execute() {
    Map<RouteResultsetNode, BackendConnection> lockedConns = session.getTargetMap();
    Set<RouteResultsetNode> dnSet = lockedConns.keySet();
    this.reset(lockedConns.size());
    // 客户端直接发送unlock tables命令,由于之前未发送lock tables语句,无法获取后端绑定的连接,此时直接返回OK包
    if (lockedConns.size() == 0) {
        LOGGER.warn("find no locked backend connection!" + session.getSource());
        OkPacket ok = new OkPacket();
        ok.packetId = ++packetId;
        // unlock table 命令返回MySQL协议包长度为7
        ok.packetLength = 7;
        ok.serverStatus = session.getSource().isAutocommit() ? 2 : 1;
        ok.write(session.getSource());
        return;
    }
    for (RouteResultsetNode dataNode : dnSet) {
        RouteResultsetNode node = new RouteResultsetNode(dataNode.getName(), ServerParse.UNLOCK, srcStatement);
        BackendConnection conn = lockedConns.get(dataNode);
        if (clearIfSessionClosed(session)) {
            return;
        }
        conn.setResponseHandler(this);
        try {
            conn.execute(node, session.getSource(), autocommit);
        } catch (Exception e) {
            connectionError(e, conn);
        }
    }
}
Also used : BackendConnection(io.mycat.backend.BackendConnection) OkPacket(io.mycat.net.mysql.OkPacket) RouteResultsetNode(io.mycat.route.RouteResultsetNode)

Example 3 with OkPacket

use of io.mycat.net.mysql.OkPacket in project Mycat-Server by MyCATApache.

the class LockTablesHandler method okResponse.

@Override
public void okResponse(byte[] data, BackendConnection conn) {
    boolean executeResponse = conn.syncAndExcute();
    if (executeResponse) {
        if (clearIfSessionClosed(session)) {
            return;
        }
        boolean isEndPack = decrementCountBy(1);
        if (isEndPack) {
            if (this.isFail() || session.closed()) {
                tryErrorFinished(true);
                return;
            }
            OkPacket ok = new OkPacket();
            ok.read(data);
            lock.lock();
            try {
                ok.packetId = ++packetId;
                ok.serverStatus = session.getSource().isAutocommit() ? 2 : 1;
            } finally {
                lock.unlock();
            }
            ok.write(session.getSource());
        }
    }
}
Also used : OkPacket(io.mycat.net.mysql.OkPacket)

Example 4 with OkPacket

use of io.mycat.net.mysql.OkPacket in project Mycat-Server by MyCATApache.

the class ReloadQueryCf method execute.

public static void execute(ManagerConnection c, String cf) {
    if (cf == null) {
        cf = "NULL";
    }
    QueryConditionAnalyzer.getInstance().setCf(cf);
    StringBuilder s = new StringBuilder();
    s.append(c).append("Reset show  @@sql.condition=" + cf + " success by manager");
    logger.warn(s.toString());
    OkPacket ok = new OkPacket();
    ok.packetId = 1;
    ok.affectedRows = 1;
    ok.serverStatus = 2;
    ok.message = "Reset show  @@sql.condition success".getBytes();
    ok.write(c);
    System.out.println(s.toString());
}
Also used : OkPacket(io.mycat.net.mysql.OkPacket)

Example 5 with OkPacket

use of io.mycat.net.mysql.OkPacket in project Mycat-Server by MyCATApache.

the class ReloadSqlSlowTime method execute.

public static void execute(ManagerConnection c, long time) {
    Map<String, UserStat> statMap = UserStatAnalyzer.getInstance().getUserStatMap();
    for (UserStat userStat : statMap.values()) {
        userStat.setSlowTime(time);
    }
    StringBuilder s = new StringBuilder();
    s.append(c).append("Reset show  @@sql.slow=" + time + " time success by manager");
    logger.warn(s.toString());
    OkPacket ok = new OkPacket();
    ok.packetId = 1;
    ok.affectedRows = 1;
    ok.serverStatus = 2;
    ok.message = "Reset show  @@sql.slow time success".getBytes();
    ok.write(c);
    System.out.println(s.toString());
}
Also used : OkPacket(io.mycat.net.mysql.OkPacket) UserStat(io.mycat.statistic.stat.UserStat)

Aggregations

OkPacket (io.mycat.net.mysql.OkPacket)18 PhysicalDBPool (io.mycat.backend.datasource.PhysicalDBPool)2 UserStat (io.mycat.statistic.stat.UserStat)2 BackendConnection (io.mycat.backend.BackendConnection)1 UserConfig (io.mycat.config.model.UserConfig)1 ConfigException (io.mycat.config.util.ConfigException)1 FrontendConnection (io.mycat.net.FrontendConnection)1 NIOConnection (io.mycat.net.NIOConnection)1 ErrorPacket (io.mycat.net.mysql.ErrorPacket)1 HeartbeatPacket (io.mycat.net.mysql.HeartbeatPacket)1 RouteResultsetNode (io.mycat.route.RouteResultsetNode)1 ServerConnection (io.mycat.server.ServerConnection)1 QueryResult (io.mycat.statistic.stat.QueryResult)1 ArrayList (java.util.ArrayList)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1