Search in sources :

Example 11 with OkPacket

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

the class ReloadConfig method execute.

public static void execute(ManagerConnection c) {
    final ReentrantLock lock = CobarServer.getInstance().getConfig().getLock();
    lock.lock();
    try {
        if (reload()) {
            StringBuilder s = new StringBuilder();
            s.append(c).append("Reload config success by manager");
            LOGGER.warn(s.toString());
            OkPacket ok = new OkPacket();
            ok.packetId = 1;
            ok.affectedRows = 1;
            ok.serverStatus = 2;
            ok.message = "Reload config success".getBytes();
            ok.write(c);
        } else {
            c.writeErrMessage(ErrorCode.ER_YES, "Reload config failure");
        }
    } finally {
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) OkPacket(com.alibaba.cobar.net.mysql.OkPacket)

Example 12 with OkPacket

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

the class MultiNodeExecutor method execute0.

/**
 * 执行
 */
private void execute0(RouteResultsetNode rrn, Channel c, boolean autocommit, BlockingSession ss, int flag) {
    ServerConnection sc = ss.getSource();
    if (isFail.get() || sc.isClosed()) {
        c.setRunning(false);
        handleFailure(ss, rrn, null);
        return;
    }
    try {
        // 执行并等待返回
        BinaryPacket bin = ((MySQLChannel) c).execute(rrn, sc, autocommit);
        // 接收和处理数据
        final ReentrantLock lock = MultiNodeExecutor.this.lock;
        lock.lock();
        try {
            switch(bin.data[0]) {
                case ErrorPacket.FIELD_COUNT:
                    c.setRunning(false);
                    handleFailure(ss, rrn, new BinaryErrInfo((MySQLChannel) c, bin, sc, rrn));
                    break;
                case OkPacket.FIELD_COUNT:
                    OkPacket ok = new OkPacket();
                    ok.read(bin);
                    affectedRows += ok.affectedRows;
                    // set lastInsertId
                    if (ok.insertId > 0) {
                        insertId = (insertId == 0) ? ok.insertId : Math.min(insertId, ok.insertId);
                    }
                    c.setRunning(false);
                    handleSuccessOK(ss, rrn, autocommit, ok);
                    break;
                default:
                    // HEADER|FIELDS|FIELD_EOF|ROWS|LAST_EOF
                    final MySQLChannel mc = (MySQLChannel) c;
                    if (fieldEOF) {
                        for (; ; ) {
                            bin = mc.receive();
                            switch(bin.data[0]) {
                                case ErrorPacket.FIELD_COUNT:
                                    c.setRunning(false);
                                    handleFailure(ss, rrn, new BinaryErrInfo(mc, bin, sc, rrn));
                                    return;
                                case EOFPacket.FIELD_COUNT:
                                    handleRowData(rrn, c, ss);
                                    return;
                                default:
                                    continue;
                            }
                        }
                    } else {
                        // HEADER
                        bin.packetId = ++packetId;
                        List<MySQLPacket> headerList = new LinkedList<MySQLPacket>();
                        headerList.add(bin);
                        for (; ; ) {
                            bin = mc.receive();
                            switch(bin.data[0]) {
                                case ErrorPacket.FIELD_COUNT:
                                    c.setRunning(false);
                                    handleFailure(ss, rrn, new BinaryErrInfo(mc, bin, sc, rrn));
                                    return;
                                case EOFPacket.FIELD_COUNT:
                                    // FIELD_EOF
                                    bin.packetId = ++packetId;
                                    for (MySQLPacket packet : headerList) {
                                        buffer = packet.write(buffer, sc);
                                    }
                                    headerList = null;
                                    buffer = bin.write(buffer, sc);
                                    fieldEOF = true;
                                    handleRowData(rrn, c, ss);
                                    return;
                                default:
                                    // FIELDS
                                    bin.packetId = ++packetId;
                                    switch(flag) {
                                        case RouteResultset.REWRITE_FIELD:
                                            StringBuilder fieldName = new StringBuilder();
                                            fieldName.append("Tables_in_").append(ss.getSource().getSchema());
                                            FieldPacket field = PacketUtil.getField(bin, fieldName.toString());
                                            headerList.add(field);
                                            break;
                                        default:
                                            headerList.add(bin);
                                    }
                            }
                        }
                    }
            }
        } finally {
            lock.unlock();
        }
    } catch (final IOException e) {
        c.close();
        handleFailure(ss, rrn, new SimpleErrInfo(e, ErrorCode.ER_YES, sc, rrn));
    } catch (final RuntimeException e) {
        c.close();
        handleFailure(ss, rrn, new SimpleErrInfo(e, ErrorCode.ER_YES, sc, rrn));
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) OkPacket(com.alibaba.cobar.net.mysql.OkPacket) ServerConnection(com.alibaba.cobar.server.ServerConnection) IOException(java.io.IOException) LinkedList(java.util.LinkedList) MySQLPacket(com.alibaba.cobar.net.mysql.MySQLPacket) MySQLChannel(com.alibaba.cobar.mysql.bio.MySQLChannel) FieldPacket(com.alibaba.cobar.net.mysql.FieldPacket) BinaryPacket(com.alibaba.cobar.net.mysql.BinaryPacket)

Example 13 with OkPacket

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

the class MultiNodeQueryHandler method okResponse.

@Override
public void okResponse(byte[] data, MySQLConnection conn) {
    boolean executeResponse = false;
    try {
        executeResponse = conn.syncAndExcute();
    } catch (UnsupportedEncodingException e) {
        connectionError(e, conn);
    }
    if (executeResponse) {
        ServerConnection source = session.getSource();
        conn.setRunning(false);
        Object attachment = conn.getAttachment();
        if (attachment instanceof RouteResultsetNode) {
            RouteResultsetNode node = (RouteResultsetNode) attachment;
            conn.recordSql(source.getHost(), source.getSchema(), node.getStatement());
        } else {
            LOGGER.warn(new StringBuilder().append("back-end conn: ").append(conn).append(" has wrong attachment: ").append(attachment).append(", for front-end conn: ").append(source));
        }
        OkPacket ok = new OkPacket();
        ok.read(data);
        lock.lock();
        try {
            affectedRows += ok.affectedRows;
            if (ok.insertId > 0) {
                insertId = (insertId == 0) ? ok.insertId : Math.min(insertId, ok.insertId);
            }
        } finally {
            lock.unlock();
        }
        if (decrementCountBy(1)) {
            if (isFail.get()) {
                notifyError();
                return;
            }
            try {
                recycleResources();
                // OK_PACKET
                ok.packetId = ++packetId;
                ok.affectedRows = affectedRows;
                if (insertId > 0) {
                    ok.insertId = insertId;
                    source.setLastInsertId(insertId);
                }
                if (source.isAutocommit()) {
                    if (!autocommit) {
                        // 前端非事务模式,后端事务模式,则需要自动递交后端事务。
                        icHandler.commit();
                    } else {
                        session.releaseConnections();
                        ok.write(source);
                    }
                } else {
                    ok.write(source);
                }
            } catch (Exception e) {
                LOGGER.warn("exception happens in success notification: " + session.getSource(), e);
            }
        }
    }
}
Also used : OkPacket(com.alibaba.cobar.net.mysql.OkPacket) RouteResultsetNode(com.alibaba.cobar.route.RouteResultsetNode) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ServerConnection(com.alibaba.cobar.server.ServerConnection) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 14 with OkPacket

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

the class Heartbeat method response.

public static void response(ServerConnection c, byte[] data) {
    HeartbeatPacket hp = new HeartbeatPacket();
    hp.read(data);
    if (CobarServer.getInstance().isOnline()) {
        OkPacket ok = new OkPacket();
        ok.packetId = 1;
        ok.affectedRows = hp.id;
        ok.serverStatus = 2;
        ok.write(c);
        if (HEARTBEAT.isInfoEnabled()) {
            HEARTBEAT.info(responseMessage("OK", c, hp.id));
        }
    } else {
        ErrorPacket error = new ErrorPacket();
        error.packetId = 1;
        error.errno = ErrorCode.ER_SERVER_SHUTDOWN;
        error.message = String.valueOf(hp.id).getBytes();
        error.write(c);
        if (HEARTBEAT.isInfoEnabled()) {
            HEARTBEAT.info(responseMessage("ERROR", c, hp.id));
        }
    }
}
Also used : OkPacket(com.alibaba.cobar.net.mysql.OkPacket) ErrorPacket(com.alibaba.cobar.net.mysql.ErrorPacket) HeartbeatPacket(com.alibaba.cobar.net.mysql.HeartbeatPacket)

Example 15 with OkPacket

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

the class CobarHeartbeat method requestMessage.

/**
 * 心跳日志信息
 */
public String requestMessage(int type, byte[] message) {
    String action = null;
    String id = null;
    switch(type) {
        case OK_STATUS:
            action = "OK";
            OkPacket ok = new OkPacket();
            ok.read(message);
            id = String.valueOf(ok.affectedRows);
            break;
        case OFF_STATUS:
            action = "OFFLINE";
            if (message != null) {
                id = new String(message);
            }
            break;
        case ERROR_STATUS:
            action = "ERROR";
            if (message != null) {
                id = new String(message);
            }
            break;
        case TIMEOUT_STATUS:
            action = "TIMEOUT";
            if (message != null) {
                id = new String(message);
            }
            break;
        case SEND:
            action = "SEND";
            if (message != null) {
                id = new String(message);
            }
            break;
        default:
            action = "UNKNOWN";
            if (message != null) {
                id = new String(message);
            }
    }
    // 如果取不到从服务端返回的id,则从本地取得。
    if (id == null) {
        id = "$" + detectCount.get();
    }
    return new StringBuilder().append("REQUEST:").append(action).append(", id=").append(id).append(", host=").append(node.getConfig().getHost()).append(", port=").append(node.getConfig().getPort()).append(", time=").append(TimeUtil.currentTimeMillis()).toString();
}
Also used : OkPacket(com.alibaba.cobar.net.mysql.OkPacket)

Aggregations

OkPacket (com.alibaba.cobar.net.mysql.OkPacket)15 ServerConnection (com.alibaba.cobar.server.ServerConnection)3 ReentrantLock (java.util.concurrent.locks.ReentrantLock)3 MySQLDataNode (com.alibaba.cobar.mysql.MySQLDataNode)2 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 MySQLChannel (com.alibaba.cobar.mysql.bio.MySQLChannel)1 FrontendConnection (com.alibaba.cobar.net.FrontendConnection)1 NIOConnection (com.alibaba.cobar.net.NIOConnection)1 BinaryPacket (com.alibaba.cobar.net.mysql.BinaryPacket)1 ErrorPacket (com.alibaba.cobar.net.mysql.ErrorPacket)1 FieldPacket (com.alibaba.cobar.net.mysql.FieldPacket)1 HeartbeatPacket (com.alibaba.cobar.net.mysql.HeartbeatPacket)1 MySQLPacket (com.alibaba.cobar.net.mysql.MySQLPacket)1 RouteResultsetNode (com.alibaba.cobar.route.RouteResultsetNode)1 LinkedList (java.util.LinkedList)1