Search in sources :

Example 6 with RouteResultsetNode

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

the class BlockingSession method clear.

/**
     * MUST be called at the end of {@link NodeExecutor}
     * 
     * @param pessimisticRelease true if this method might be invoked
     *            concurrently with {@link #kill()}
     */
private void clear(boolean pessimisticRelease) {
    for (RouteResultsetNode rrn : target.keySet()) {
        Channel c = target.remove(rrn);
        // 通道不存在或者已被关闭
        if (c == null || c.isClosed()) {
            continue;
        }
        // 如果通道正在运行中,则关闭当前通道。
        if (c.isRunning() || (pessimisticRelease && source.isClosed())) {
            c.close();
            continue;
        }
        // 非事务中的通道,直接释放资源。
        if (c.isAutocommit()) {
            c.release();
            continue;
        }
        // 事务中的通道,需要先回滚后再释放资源。
        MySQLChannel mc = (MySQLChannel) c;
        try {
            BinaryPacket bin = mc.rollback();
            switch(bin.data[0]) {
                case OkPacket.FIELD_COUNT:
                    mc.release();
                    break;
                case ErrorPacket.FIELD_COUNT:
                    mc.close();
                    break;
                default:
                    throw new UnknownPacketException(bin.toString());
            }
        } catch (IOException e) {
            StringBuilder s = new StringBuilder();
            LOGGER.warn(s.append(mc).append("rollback").toString(), e);
            mc.close();
        } catch (RuntimeException e) {
            StringBuilder s = new StringBuilder();
            LOGGER.warn(s.append(mc).append("rollback").toString(), e);
            mc.close();
        }
    }
}
Also used : UnknownPacketException(com.alibaba.cobar.exception.UnknownPacketException) RouteResultsetNode(com.alibaba.cobar.route.RouteResultsetNode) MySQLChannel(com.alibaba.cobar.mysql.bio.MySQLChannel) Channel(com.alibaba.cobar.mysql.bio.Channel) MySQLChannel(com.alibaba.cobar.mysql.bio.MySQLChannel) IOException(java.io.IOException) BinaryPacket(com.alibaba.cobar.net.mysql.BinaryPacket)

Example 7 with RouteResultsetNode

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

the class NonBlockingSession method clearConnections.

private void clearConnections(boolean pessimisticRelease) {
    for (RouteResultsetNode node : target.keySet()) {
        MySQLConnection c = target.remove(node);
        if (c == null || c.isClosedOrQuit()) {
            continue;
        }
        // 如果通道正在运行中,则关闭当前通道。
        if (c.isRunning() || (pessimisticRelease && source.isClosed())) {
            c.close();
            continue;
        }
        // 非事务中的通道,直接释放资源。
        if (c.isAutocommit()) {
            c.release();
            continue;
        }
        c.setResponseHandler(new RollbackReleaseHandler());
        c.rollback();
    }
}
Also used : RouteResultsetNode(com.alibaba.cobar.route.RouteResultsetNode) MySQLConnection(com.alibaba.cobar.mysql.nio.MySQLConnection) RollbackReleaseHandler(com.alibaba.cobar.mysql.nio.handler.RollbackReleaseHandler)

Example 8 with RouteResultsetNode

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

the class DefaultCommitExecutor method commit.

/**
     * 提交事务
     */
public void commit(final OkPacket packet, final BlockingSession session, final int initCount) {
    // 初始化
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        this.isFail.set(false);
        this.nodeCount = initCount;
        this.indicatedOK = packet;
    } finally {
        lock.unlock();
    }
    if (session.getSource().isClosed()) {
        decrementCountToZero();
        return;
    }
    // 执行
    final ConcurrentMap<RouteResultsetNode, Channel> target = session.getTarget();
    Executor committer = session.getSource().getProcessor().getCommitter();
    int started = 0;
    for (RouteResultsetNode rrn : target.keySet()) {
        if (rrn == null) {
            try {
                getLogger().error("null is contained in RoutResultsetNodes, source = " + session.getSource() + ", bindChannel = " + target);
            } catch (Exception e) {
            }
            continue;
        }
        final MySQLChannel mc = (MySQLChannel) target.get(rrn);
        if (mc != null) {
            mc.setRunning(true);
            committer.execute(new Runnable() {

                @Override
                public void run() {
                    _commit(mc, session);
                }
            });
            ++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.clear();
    }
}
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) MySQLChannel(com.alibaba.cobar.mysql.bio.MySQLChannel) IOException(java.io.IOException) UnknownPacketException(com.alibaba.cobar.exception.UnknownPacketException)

Example 9 with RouteResultsetNode

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

the class CommitNodeHandler method commit.

public void commit(OkPacket packet) {
    final int initCount = session.getTargetCount();
    lock.lock();
    try {
        reset(initCount);
        okPacket = packet;
    } finally {
        lock.unlock();
    }
    if (session.closed()) {
        decrementCountToZero();
        return;
    }
    // 执行
    Executor executor = session.getSource().getProcessor().getExecutor();
    int started = 0;
    for (RouteResultsetNode rrn : session.getTargetKeys()) {
        if (rrn == null) {
            try {
                logger.error("null is contained in RoutResultsetNodes, source = " + session.getSource());
            } catch (Exception e) {
            }
            continue;
        }
        final MySQLConnection conn = session.getTarget(rrn);
        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(CommitNodeHandler.this);
                    conn.commit();
                }
            });
            ++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 10 with RouteResultsetNode

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

the class MultiNodeQueryHandler method connectionAcquired.

@Override
public void connectionAcquired(final MySQLConnection conn) {
    Object attachment = conn.getAttachment();
    if (!(attachment instanceof RouteResultsetNode)) {
        backendConnError(conn, new StringBuilder().append("wrong attachement from connection build: ").append(conn).append(" bound by ").append(session.getSource()).toString());
        conn.release();
        return;
    }
    final RouteResultsetNode node = (RouteResultsetNode) attachment;
    conn.setRunning(true);
    session.bindConnection(node, conn);
    session.getSource().getProcessor().getExecutor().execute(new Runnable() {

        @Override
        public void run() {
            _execute(conn, node);
        }
    });
}
Also used : RouteResultsetNode(com.alibaba.cobar.route.RouteResultsetNode)

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