Search in sources :

Example 26 with ServerConnection

use of com.alibaba.cobar.server.ServerConnection 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 27 with ServerConnection

use of com.alibaba.cobar.server.ServerConnection in project cobar by alibaba.

the class MultiNodeQueryHandler method fieldEofResponse.

@Override
public void fieldEofResponse(byte[] header, List<byte[]> fields, byte[] eof, MySQLConnection conn) {
    lock.lock();
    try {
        if (fieldsReturned) {
            return;
        }
        fieldsReturned = true;
        header[3] = ++packetId;
        ServerConnection source = session.getSource();
        buffer = source.writeToBuffer(header, buffer);
        for (int i = 0, len = fields.size(); i < len; ++i) {
            byte[] field = fields.get(i);
            field[3] = ++packetId;
            buffer = source.writeToBuffer(field, buffer);
        }
        eof[3] = ++packetId;
        buffer = source.writeToBuffer(eof, buffer);
    } finally {
        lock.unlock();
    }
}
Also used : ServerConnection(com.alibaba.cobar.server.ServerConnection)

Example 28 with ServerConnection

use of com.alibaba.cobar.server.ServerConnection in project cobar by alibaba.

the class RollbackExecutor method handleException.

private void handleException(Channel mc, BlockingSession session, Exception e) {
    isFail.set(true);
    if (decrementCountBy(1)) {
        try {
            session.clear();
            ServerConnection source = session.getSource();
            LOGGER.warn(new StringBuilder().append(source).append(mc).append("rollback").toString(), e);
            String msg = e.getMessage();
            source.writeErrMessage(ErrorCode.ER_YES, msg == null ? e.getClass().getSimpleName() : msg);
        } catch (Exception e2) {
            LOGGER.warn("exception happens in failure notification: " + session.getSource(), e2);
        }
    }
}
Also used : ServerConnection(com.alibaba.cobar.server.ServerConnection) IOException(java.io.IOException) UnknownPacketException(com.alibaba.cobar.exception.UnknownPacketException)

Example 29 with ServerConnection

use of com.alibaba.cobar.server.ServerConnection in project cobar by alibaba.

the class KillQueryHandler method handle.

public static void handle(String stmt, int offset, final ServerConnection c) {
    String id = stmt.substring(offset).trim();
    if (StringUtil.isEmpty(id)) {
        c.writeErrMessage(ErrorCode.ER_NO_SUCH_THREAD, "NULL connection id");
    } else {
        // get value
        long value = 0;
        try {
            value = Long.parseLong(id);
        } catch (NumberFormatException e) {
            c.writeErrMessage(ErrorCode.ER_NO_SUCH_THREAD, "Invalid connection id:" + id);
            return;
        }
        // kill query itself
        if (value == c.getId()) {
            c.cancel(null);
            return;
        }
        // get the connection and kill query
        FrontendConnection fc = null;
        NIOProcessor[] processors = CobarServer.getInstance().getProcessors();
        for (NIOProcessor p : processors) {
            if ((fc = p.getFrontends().get(value)) != null) {
                break;
            }
        }
        if (fc != null) {
            if (fc instanceof ServerConnection) {
                ((ServerConnection) fc).cancel(c);
            } else {
                c.writeErrMessage(ErrorCode.ER_UNKNOWN_COM_ERROR, "Unknown command");
            }
        } else {
            c.writeErrMessage(ErrorCode.ER_NO_SUCH_THREAD, "Unknown connection id:" + id);
        }
    }
}
Also used : FrontendConnection(com.alibaba.cobar.net.FrontendConnection) ServerConnection(com.alibaba.cobar.server.ServerConnection) NIOProcessor(com.alibaba.cobar.net.NIOProcessor)

Aggregations

ServerConnection (com.alibaba.cobar.server.ServerConnection)29 IOException (java.io.IOException)16 BinaryPacket (com.alibaba.cobar.net.mysql.BinaryPacket)6 UnknownDataNodeException (com.alibaba.cobar.exception.UnknownDataNodeException)5 MySQLChannel (com.alibaba.cobar.mysql.bio.MySQLChannel)5 ByteBuffer (java.nio.ByteBuffer)5 UnknownPacketException (com.alibaba.cobar.exception.UnknownPacketException)4 ErrorPacket (com.alibaba.cobar.net.mysql.ErrorPacket)4 Channel (com.alibaba.cobar.mysql.bio.Channel)3 OkPacket (com.alibaba.cobar.net.mysql.OkPacket)3 RouteResultsetNode (com.alibaba.cobar.route.RouteResultsetNode)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 ReentrantLock (java.util.concurrent.locks.ReentrantLock)3 CobarConfig (com.alibaba.cobar.CobarConfig)2 MySQLDataNode (com.alibaba.cobar.mysql.MySQLDataNode)2 FieldPacket (com.alibaba.cobar.net.mysql.FieldPacket)2 MySQLPacket (com.alibaba.cobar.net.mysql.MySQLPacket)2 LinkedList (java.util.LinkedList)2 FrontendConnection (com.alibaba.cobar.net.FrontendConnection)1 NIOProcessor (com.alibaba.cobar.net.NIOProcessor)1