Search in sources :

Example 1 with RouteResultsetNode

use of com.alibaba.cobar.route.RouteResultsetNode in project cobar by alibaba.

the class MultiNodeQueryHandler method rowEofResponse.

@Override
public void rowEofResponse(byte[] eof, MySQLConnection conn) {
    conn.setRunning(false);
    ServerConnection source = session.getSource();
    RouteResultsetNode node = null;
    Object attachment = conn.getAttachment();
    if (attachment instanceof 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));
    }
    if (source.isAutocommit()) {
        if (node != null) {
            conn = session.removeTarget(node);
            if (conn != null) {
                if (isFail.get() || session.closed()) {
                    conn.quit();
                } else {
                    conn.release();
                }
            }
        }
    }
    if (decrementCountBy(1)) {
        if (isFail.get()) {
            notifyError();
            recycleResources();
            return;
        }
        try {
            if (source.isAutocommit()) {
                session.releaseConnections();
            }
            eof[3] = ++packetId;
            source.write(source.writeToBuffer(eof, buffer));
        } catch (Exception e) {
            LOGGER.warn("exception happens in success notification: " + session.getSource(), e);
        }
    }
}
Also used : RouteResultsetNode(com.alibaba.cobar.route.RouteResultsetNode) ServerConnection(com.alibaba.cobar.server.ServerConnection) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 2 with RouteResultsetNode

use of com.alibaba.cobar.route.RouteResultsetNode in project cobar by alibaba.

the class RollbackNodeHandler method rollback.

public void rollback() {
    final int initCount = session.getTargetCount();
    lock.lock();
    try {
        reset(initCount);
    } finally {
        lock.unlock();
    }
    if (session.closed()) {
        decrementCountToZero();
        return;
    }
    // 执行
    Executor executor = session.getSource().getProcessor().getExecutor();
    int started = 0;
    for (final RouteResultsetNode node : session.getTargetKeys()) {
        if (node == null) {
            try {
                logger.error("null is contained in RoutResultsetNodes, source = " + session.getSource());
            } catch (Exception e) {
            }
            continue;
        }
        final MySQLConnection conn = session.getTarget(node);
        if (conn != null) {
            conn.setRunning(true);
            executor.execute(new Runnable() {

                @Override
                public void run() {
                    if (isFail.get() || session.closed()) {
                        backendConnError(conn, "cancelled by other thread");
                        return;
                    }
                    conn.setResponseHandler(RollbackNodeHandler.this);
                    conn.rollback();
                }
            });
            ++started;
        }
    }
    if (started < initCount && decrementCountBy(initCount - started)) {
        /**
             * assumption: only caused by front-end connection close. <br/>
             * Otherwise, packet must be returned to front-end
             */
        session.clearConnections();
    }
}
Also used : Executor(java.util.concurrent.Executor) RouteResultsetNode(com.alibaba.cobar.route.RouteResultsetNode) MySQLConnection(com.alibaba.cobar.mysql.nio.MySQLConnection)

Example 3 with RouteResultsetNode

use of com.alibaba.cobar.route.RouteResultsetNode in project cobar by alibaba.

the class ExplainHandler method handle.

public static void handle(String stmt, ServerConnection c, int offset) {
    stmt = stmt.substring(offset);
    RouteResultset rrs = getRouteResultset(c, stmt);
    if (rrs == null)
        return;
    ByteBuffer buffer = c.allocate();
    // write header
    ResultSetHeaderPacket header = PacketUtil.getHeader(FIELD_COUNT);
    byte packetId = header.packetId;
    buffer = header.write(buffer, c);
    // write fields
    for (FieldPacket field : fields) {
        field.packetId = ++packetId;
        buffer = field.write(buffer, c);
    }
    // write eof
    EOFPacket eof = new EOFPacket();
    eof.packetId = ++packetId;
    buffer = eof.write(buffer, c);
    // write rows
    RouteResultsetNode[] rrsn = (rrs != null) ? rrs.getNodes() : EMPTY_ARRAY;
    for (RouteResultsetNode node : rrsn) {
        RowDataPacket row = getRow(node, c.getCharset());
        row.packetId = ++packetId;
        buffer = row.write(buffer, c);
    }
    // write last eof
    EOFPacket lastEof = new EOFPacket();
    lastEof.packetId = ++packetId;
    buffer = lastEof.write(buffer, c);
    // post write
    c.write(buffer);
}
Also used : ResultSetHeaderPacket(com.alibaba.cobar.net.mysql.ResultSetHeaderPacket) RouteResultsetNode(com.alibaba.cobar.route.RouteResultsetNode) RowDataPacket(com.alibaba.cobar.net.mysql.RowDataPacket) EOFPacket(com.alibaba.cobar.net.mysql.EOFPacket) ByteBuffer(java.nio.ByteBuffer) FieldPacket(com.alibaba.cobar.net.mysql.FieldPacket) RouteResultset(com.alibaba.cobar.route.RouteResultset)

Example 4 with RouteResultsetNode

use of com.alibaba.cobar.route.RouteResultsetNode in project cobar by alibaba.

the class MultiNodeExecutor method execute.

/**
     * 多数据节点执行
     * 
     * @param nodes never null
     */
public void execute(RouteResultsetNode[] nodes, final boolean autocommit, final BlockingSession ss, final int flag) {
    // 初始化
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        this.isFail.set(false);
        this.unfinishedNodeCount = nodes.length;
        this.errno = 0;
        this.errMessage = null;
        this.fieldEOF = false;
        this.packetId = 0;
        this.affectedRows = 0L;
        this.insertId = 0L;
        this.buffer = ss.getSource().allocate();
    } finally {
        lock.unlock();
    }
    if (ss.getSource().isClosed()) {
        decrementCountToZero();
        ss.getSource().recycle(this.buffer);
        return;
    }
    // 多节点处理
    ConcurrentMap<RouteResultsetNode, Channel> target = ss.getTarget();
    for (RouteResultsetNode rrn : nodes) {
        Channel c = target.get(rrn);
        if (c != null) {
            c.setRunning(true);
        }
    }
    ThreadPoolExecutor exec = ss.getSource().getProcessor().getExecutor();
    for (final RouteResultsetNode rrn : nodes) {
        final Channel c = target.get(rrn);
        if (c != null) {
            exec.execute(new Runnable() {

                @Override
                public void run() {
                    execute0(rrn, c, autocommit, ss, flag);
                }
            });
        } else {
            newExecute(rrn, autocommit, ss, flag);
        }
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) RouteResultsetNode(com.alibaba.cobar.route.RouteResultsetNode) MySQLChannel(com.alibaba.cobar.mysql.bio.MySQLChannel) Channel(com.alibaba.cobar.mysql.bio.Channel) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 5 with RouteResultsetNode

use of com.alibaba.cobar.route.RouteResultsetNode in project cobar by alibaba.

the class RollbackExecutor method rollback.

/**
     * 事务回滚
     */
public void rollback(final BlockingSession session) {
    final ServerConnection source = session.getSource();
    final ConcurrentMap<RouteResultsetNode, Channel> target = session.getTarget();
    final int initNodeCount = target.size();
    if (initNodeCount <= 0) {
        ByteBuffer buffer = source.allocate();
        source.write(source.writeToBuffer(OkPacket.OK, buffer));
        return;
    }
    // 初始化
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        this.isFail.set(false);
        this.nodeCount = initNodeCount;
    } finally {
        lock.unlock();
    }
    if (source.isClosed()) {
        decrementCountToZero();
        return;
    }
    // 执行
    Executor committer = source.getProcessor().getCommitter();
    int started = 0;
    for (RouteResultsetNode rrn : target.keySet()) {
        final MySQLChannel mc = (MySQLChannel) target.get(rrn);
        if (mc != null) {
            mc.setRunning(true);
            committer.execute(new Runnable() {

                @Override
                public void run() {
                    _rollback(mc, session);
                }
            });
            ++started;
        }
    }
    if (started < initNodeCount) {
        decrementCountBy(initNodeCount - started);
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Executor(java.util.concurrent.Executor) RouteResultsetNode(com.alibaba.cobar.route.RouteResultsetNode) MySQLChannel(com.alibaba.cobar.mysql.bio.MySQLChannel) Channel(com.alibaba.cobar.mysql.bio.Channel) ServerConnection(com.alibaba.cobar.server.ServerConnection) MySQLChannel(com.alibaba.cobar.mysql.bio.MySQLChannel) ByteBuffer(java.nio.ByteBuffer)

Aggregations

RouteResultsetNode (com.alibaba.cobar.route.RouteResultsetNode)14 MySQLConnection (com.alibaba.cobar.mysql.nio.MySQLConnection)5 Channel (com.alibaba.cobar.mysql.bio.Channel)4 MySQLChannel (com.alibaba.cobar.mysql.bio.MySQLChannel)4 IOException (java.io.IOException)4 Executor (java.util.concurrent.Executor)4 ReentrantLock (java.util.concurrent.locks.ReentrantLock)4 ServerConnection (com.alibaba.cobar.server.ServerConnection)3 CobarConfig (com.alibaba.cobar.CobarConfig)2 UnknownPacketException (com.alibaba.cobar.exception.UnknownPacketException)2 MySQLDataNode (com.alibaba.cobar.mysql.MySQLDataNode)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ByteBuffer (java.nio.ByteBuffer)2 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)2 KillConnectionHandler (com.alibaba.cobar.mysql.nio.handler.KillConnectionHandler)1 MultiNodeQueryHandler (com.alibaba.cobar.mysql.nio.handler.MultiNodeQueryHandler)1 RollbackReleaseHandler (com.alibaba.cobar.mysql.nio.handler.RollbackReleaseHandler)1 SingleNodeHandler (com.alibaba.cobar.mysql.nio.handler.SingleNodeHandler)1 BinaryPacket (com.alibaba.cobar.net.mysql.BinaryPacket)1 EOFPacket (com.alibaba.cobar.net.mysql.EOFPacket)1