Search in sources :

Example 1 with MySQLConnection

use of com.alibaba.cobar.mysql.nio.MySQLConnection in project cobar by alibaba.

the class SingleNodeHandler method execute.

public void execute() throws Exception {
    lock.lock();
    try {
        this.isRunning = true;
        this.packetId = 0;
        this.buffer = session.getSource().allocate();
    } finally {
        lock.unlock();
    }
    final MySQLConnection conn = session.getTarget(route);
    if (conn == null) {
        CobarConfig conf = CobarServer.getInstance().getConfig();
        MySQLDataNode dn = conf.getDataNodes().get(route.getName());
        dn.getConnection(this, null);
    } else {
        conn.setRunning(true);
        session.getSource().getProcessor().getExecutor().execute(new Runnable() {

            @Override
            public void run() {
                _execute(conn);
            }
        });
    }
}
Also used : MySQLDataNode(com.alibaba.cobar.mysql.MySQLDataNode) CobarConfig(com.alibaba.cobar.CobarConfig) MySQLConnection(com.alibaba.cobar.mysql.nio.MySQLConnection)

Example 2 with MySQLConnection

use of com.alibaba.cobar.mysql.nio.MySQLConnection 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 MySQLConnection

use of com.alibaba.cobar.mysql.nio.MySQLConnection 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 4 with MySQLConnection

use of com.alibaba.cobar.mysql.nio.MySQLConnection 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 5 with MySQLConnection

use of com.alibaba.cobar.mysql.nio.MySQLConnection in project cobar by alibaba.

the class MultiNodeQueryHandler method execute.

public void execute() throws Exception {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        this.reset(route.length);
        this.fieldsReturned = false;
        this.affectedRows = 0L;
        this.insertId = 0L;
        this.buffer = session.getSource().allocate();
    } finally {
        lock.unlock();
    }
    if (session.closed()) {
        decrementCountToZero();
        recycleResources();
        return;
    }
    session.setConnectionRunning(route);
    ThreadPoolExecutor executor = session.getSource().getProcessor().getExecutor();
    for (final RouteResultsetNode node : route) {
        final MySQLConnection conn = session.getTarget(node);
        if (conn != null) {
            conn.setAttachment(node);
            executor.execute(new Runnable() {

                @Override
                public void run() {
                    _execute(conn, node);
                }
            });
        } else {
            CobarConfig conf = CobarServer.getInstance().getConfig();
            MySQLDataNode dn = conf.getDataNodes().get(node.getName());
            dn.getConnection(this, node);
        }
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) MySQLDataNode(com.alibaba.cobar.mysql.MySQLDataNode) RouteResultsetNode(com.alibaba.cobar.route.RouteResultsetNode) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) CobarConfig(com.alibaba.cobar.CobarConfig) MySQLConnection(com.alibaba.cobar.mysql.nio.MySQLConnection)

Aggregations

MySQLConnection (com.alibaba.cobar.mysql.nio.MySQLConnection)6 RouteResultsetNode (com.alibaba.cobar.route.RouteResultsetNode)5 CobarConfig (com.alibaba.cobar.CobarConfig)3 MySQLDataNode (com.alibaba.cobar.mysql.MySQLDataNode)3 Executor (java.util.concurrent.Executor)2 KillConnectionHandler (com.alibaba.cobar.mysql.nio.handler.KillConnectionHandler)1 RollbackReleaseHandler (com.alibaba.cobar.mysql.nio.handler.RollbackReleaseHandler)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1