Search in sources :

Example 1 with CommandPacket

use of com.alibaba.cobar.net.mysql.CommandPacket in project cobar by alibaba.

the class MySQLDetector method heartbeat.

public void heartbeat() {
    if (isAuthenticated) {
        String sql = heartbeat.getSource().getNode().getHeartbeatSQL();
        if (sql != null) {
            CommandPacket packet = new CommandPacket();
            packet.packetId = 0;
            packet.command = MySQLPacket.COM_QUERY;
            packet.arg = sql.getBytes();
            packet.write(this);
        }
    } else {
        authenticate();
    }
}
Also used : CommandPacket(com.alibaba.cobar.net.mysql.CommandPacket)

Example 2 with CommandPacket

use of com.alibaba.cobar.net.mysql.CommandPacket in project cobar by alibaba.

the class MySQLChannel method sendSqlMode.

/**
     * 发送SQL_MODE设置
     */
private void sendSqlMode() throws IOException {
    CommandPacket cmd = getSqlModeCommand();
    cmd.write(out);
    out.flush();
    BinaryPacket bin = receive();
    switch(bin.data[0]) {
        case OkPacket.FIELD_COUNT:
            break;
        case ErrorPacket.FIELD_COUNT:
            ErrorPacket err = new ErrorPacket();
            err.read(bin);
            throw new ErrorPacketException(new String(err.message, charset));
        default:
            throw new UnknownPacketException(bin.toString());
    }
}
Also used : ErrorPacket(com.alibaba.cobar.net.mysql.ErrorPacket) ErrorPacketException(com.alibaba.cobar.exception.ErrorPacketException) UnknownPacketException(com.alibaba.cobar.exception.UnknownPacketException) CommandPacket(com.alibaba.cobar.net.mysql.CommandPacket) BinaryPacket(com.alibaba.cobar.net.mysql.BinaryPacket)

Example 3 with CommandPacket

use of com.alibaba.cobar.net.mysql.CommandPacket in project cobar by alibaba.

the class MySQLChannel method execute.

public BinaryPacket execute(RouteResultsetNode rrn, ServerConnection sc, boolean autocommit) throws IOException {
    // 状态一致性检查
    if (this.charsetIndex != sc.getCharsetIndex()) {
        /*如果后端MySQL服务器配置了character_set_server=utf8mb4,
            并且数据库客户端要求使用uft8,
            我们将不发送客户端的字符集要求。
            这种情况在我们使用mysql官方的Connector/J编程的时候遇见,
            详细见:http://dev.mysql.com/doc/connector-j/en/connector-j-usagenotes-troubleshooting.html#qandaitem-15-1-15
                    https://dev.mysql.com/doc/relnotes/connector-j/en/news-5-1-13.html
            */
        if (this.charsetIndex != 45 && sc.getCharsetIndex() != 33)
            sendCharset(sc.getCharsetIndex());
    }
    if (this.txIsolation != sc.getTxIsolation()) {
        sendTxIsolation(sc.getTxIsolation());
    }
    if (this.autocommit != autocommit) {
        sendAutocommit(autocommit);
    }
    // 生成执行数据包
    CommandPacket packet = new CommandPacket();
    packet.packetId = 0;
    packet.command = MySQLPacket.COM_QUERY;
    packet.arg = rrn.getStatement().getBytes(charset);
    // 记录执行开始时间
    lastActiveTime = TimeUtil.currentTimeMillis();
    // 递交执行数据包并等待执行返回
    packet.write(out);
    out.flush();
    BinaryPacket bin = receive();
    // SQL执行时间统计
    long now = TimeUtil.currentTimeMillis();
    if (now > lastActiveTime) {
        recordSql(sc.getHost(), sc.getSchema(), rrn.getStatement());
    }
    // 记录执行结束时间
    lastActiveTime = now;
    return bin;
}
Also used : CommandPacket(com.alibaba.cobar.net.mysql.CommandPacket) BinaryPacket(com.alibaba.cobar.net.mysql.BinaryPacket)

Example 4 with CommandPacket

use of com.alibaba.cobar.net.mysql.CommandPacket in project cobar by alibaba.

the class MySQLChannel method sendTxIsolation.

/**
     * 发送事务级别设置
     */
private void sendTxIsolation(int txIsolation) throws IOException {
    CommandPacket cmd = getTxIsolationCommand(txIsolation);
    cmd.write(out);
    out.flush();
    BinaryPacket bin = receive();
    switch(bin.data[0]) {
        case OkPacket.FIELD_COUNT:
            this.txIsolation = txIsolation;
            break;
        case ErrorPacket.FIELD_COUNT:
            ErrorPacket err = new ErrorPacket();
            err.read(bin);
            throw new ErrorPacketException(new String(err.message, charset));
        default:
            throw new UnknownPacketException(bin.toString());
    }
}
Also used : ErrorPacket(com.alibaba.cobar.net.mysql.ErrorPacket) ErrorPacketException(com.alibaba.cobar.exception.ErrorPacketException) UnknownPacketException(com.alibaba.cobar.exception.UnknownPacketException) CommandPacket(com.alibaba.cobar.net.mysql.CommandPacket) BinaryPacket(com.alibaba.cobar.net.mysql.BinaryPacket)

Example 5 with CommandPacket

use of com.alibaba.cobar.net.mysql.CommandPacket in project cobar by alibaba.

the class MySQLChannel method killChannel.

private void killChannel() {
    MySQLChannel killChannel = null;
    try {
        killChannel = (MySQLChannel) dataSource.getChannel();
    } catch (Exception e) {
        LOGGER.error("killProcess failure for getting channel", e);
        return;
    }
    CommandPacket killPacket = new CommandPacket();
    killPacket.packetId = 0;
    killPacket.command = MySQLPacket.COM_QUERY;
    killPacket.arg = new StringBuilder("KILL ").append(threadId).toString().getBytes();
    try {
        killPacket.write(killChannel.out);
        killChannel.out.flush();
        BinaryPacket bin = new BinaryPacket();
        bin.read(killChannel.in);
        switch(bin.data[0]) {
            case OkPacket.FIELD_COUNT:
                killChannel.release();
                break;
            case ErrorPacket.FIELD_COUNT:
                LOGGER.error("kill error! id:" + threadId + ", err=" + bin);
                killChannel.release();
                break;
            default:
                LOGGER.error("kill unknown response, id:" + threadId + "packet=" + bin);
                killChannel.close();
        }
    } catch (IOException e) {
        killChannel.close();
        LOGGER.error("kill IOException, id:" + threadId, e);
    }
}
Also used : IOException(java.io.IOException) CommandPacket(com.alibaba.cobar.net.mysql.CommandPacket) TimeoutException(java.util.concurrent.TimeoutException) UnknownPacketException(com.alibaba.cobar.exception.UnknownPacketException) ErrorPacketException(com.alibaba.cobar.exception.ErrorPacketException) UnknownTxIsolationException(com.alibaba.cobar.exception.UnknownTxIsolationException) IOException(java.io.IOException) UnknownCharsetException(com.alibaba.cobar.exception.UnknownCharsetException) ExecutionException(java.util.concurrent.ExecutionException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BinaryPacket(com.alibaba.cobar.net.mysql.BinaryPacket)

Aggregations

CommandPacket (com.alibaba.cobar.net.mysql.CommandPacket)10 BinaryPacket (com.alibaba.cobar.net.mysql.BinaryPacket)6 ErrorPacketException (com.alibaba.cobar.exception.ErrorPacketException)5 UnknownPacketException (com.alibaba.cobar.exception.UnknownPacketException)5 ErrorPacket (com.alibaba.cobar.net.mysql.ErrorPacket)4 UnknownCharsetException (com.alibaba.cobar.exception.UnknownCharsetException)1 UnknownTxIsolationException (com.alibaba.cobar.exception.UnknownTxIsolationException)1 IOException (java.io.IOException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1