Search in sources :

Example 11 with RouteResultsetNode

use of com.alibaba.cobar.route.RouteResultsetNode 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)

Example 12 with RouteResultsetNode

use of com.alibaba.cobar.route.RouteResultsetNode 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 13 with RouteResultsetNode

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

the class NonBlockingSession method execute.

@Override
public void execute(RouteResultset rrs, int type) {
    if (LOGGER.isDebugEnabled()) {
        StringBuilder s = new StringBuilder();
        LOGGER.debug(s.append(source).append(rrs).toString());
    }
    // 检查路由结果是否为空
    RouteResultsetNode[] nodes = rrs.getNodes();
    if (nodes == null || nodes.length == 0) {
        source.writeErrMessage(ErrorCode.ER_NO_DB_ERROR, "No dataNode selected");
        return;
    }
    if (nodes.length == 1) {
        singleNodeHandler = new SingleNodeHandler(nodes[0], this);
    // singleNodeHandler.execute();
    } else {
        boolean autocommit = source.isAutocommit();
        if (autocommit && isModifySQL(type)) {
            autocommit = false;
        }
        multiNodeHandler = new MultiNodeQueryHandler(nodes, autocommit, this);
    // multiNodeHandler.execute();
    }
}
Also used : SingleNodeHandler(com.alibaba.cobar.mysql.nio.handler.SingleNodeHandler) MultiNodeQueryHandler(com.alibaba.cobar.mysql.nio.handler.MultiNodeQueryHandler) RouteResultsetNode(com.alibaba.cobar.route.RouteResultsetNode)

Example 14 with RouteResultsetNode

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

the class NonBlockingSession method kill.

private void kill(Runnable run) {
    boolean hooked = false;
    AtomicInteger count = null;
    Map<RouteResultsetNode, MySQLConnection> killees = null;
    for (RouteResultsetNode node : target.keySet()) {
        MySQLConnection c = target.get(node);
        if (c != null && c.isRunning()) {
            if (!hooked) {
                hooked = true;
                killees = new HashMap<RouteResultsetNode, MySQLConnection>();
                count = new AtomicInteger(0);
            }
            killees.put(node, c);
            count.incrementAndGet();
        }
    }
    if (hooked) {
        for (Entry<RouteResultsetNode, MySQLConnection> en : killees.entrySet()) {
            KillConnectionHandler kill = new KillConnectionHandler(en.getValue(), this, run, count);
            CobarConfig conf = CobarServer.getInstance().getConfig();
            MySQLDataNode dn = conf.getDataNodes().get(en.getKey().getName());
            try {
                dn.getConnection(kill, en.getKey());
            } catch (Exception e) {
                LOGGER.error("get killer connection failed for " + en.getKey(), e);
                kill.connectionError(e, null);
            }
        }
    } else {
        run.run();
    }
}
Also used : MySQLDataNode(com.alibaba.cobar.mysql.MySQLDataNode) KillConnectionHandler(com.alibaba.cobar.mysql.nio.handler.KillConnectionHandler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RouteResultsetNode(com.alibaba.cobar.route.RouteResultsetNode) CobarConfig(com.alibaba.cobar.CobarConfig) MySQLConnection(com.alibaba.cobar.mysql.nio.MySQLConnection)

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