Search in sources :

Example 6 with CobarConfig

use of com.alibaba.cobar.CobarConfig in project cobar by alibaba.

the class SingleNodeExecutor method newExecute.

/**
     * 新数据通道的执行
     */
private void newExecute(final RouteResultsetNode rrn, final BlockingSession ss, final int flag) {
    final ServerConnection sc = ss.getSource();
    // 检查数据节点是否存在
    CobarConfig conf = CobarServer.getInstance().getConfig();
    final MySQLDataNode dn = conf.getDataNodes().get(rrn.getName());
    if (dn == null) {
        LOGGER.warn(new StringBuilder().append(sc).append(rrn).toString(), new UnknownDataNodeException());
        handleError(ErrorCode.ER_BAD_DB_ERROR, "Unknown dataNode '" + rrn.getName() + "'", ss);
        return;
    }
    // 提交执行任务
    sc.getProcessor().getExecutor().execute(new Runnable() {

        @Override
        public void run() {
            // 取得数据通道
            int i = rrn.getReplicaIndex();
            Channel c = null;
            try {
                c = (i == DEFAULT_REPLICA_INDEX) ? dn.getChannel() : dn.getChannel(i);
            } catch (Exception e) {
                LOGGER.warn(new StringBuilder().append(sc).append(rrn).toString(), e);
                String msg = e.getMessage();
                handleError(ErrorCode.ER_BAD_DB_ERROR, msg == null ? e.getClass().getSimpleName() : msg, ss);
                return;
            }
            // 检查连接是否已关闭。
            if (sc.isClosed()) {
                c.release();
                endRunning();
                return;
            }
            // 绑定数据通道
            c.setRunning(true);
            Channel old = ss.getTarget().put(rrn, c);
            if (old != null && old != c) {
                old.close();
            }
            // 执行
            execute0(rrn, ss, c, flag);
        }
    });
}
Also used : MySQLDataNode(com.alibaba.cobar.mysql.MySQLDataNode) UnknownDataNodeException(com.alibaba.cobar.exception.UnknownDataNodeException) MySQLChannel(com.alibaba.cobar.mysql.bio.MySQLChannel) Channel(com.alibaba.cobar.mysql.bio.Channel) ServerConnection(com.alibaba.cobar.server.ServerConnection) CobarConfig(com.alibaba.cobar.CobarConfig) UnknownDataNodeException(com.alibaba.cobar.exception.UnknownDataNodeException) IOException(java.io.IOException)

Example 7 with CobarConfig

use of com.alibaba.cobar.CobarConfig 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 8 with CobarConfig

use of com.alibaba.cobar.CobarConfig in project cobar by alibaba.

the class ShowCobarCluster method getRows.

private static List<RowDataPacket> getRows(ServerConnection c) {
    List<RowDataPacket> rows = new LinkedList<RowDataPacket>();
    CobarConfig config = CobarServer.getInstance().getConfig();
    CobarCluster cluster = config.getCluster();
    Map<String, SchemaConfig> schemas = config.getSchemas();
    SchemaConfig schema = (c.getSchema() == null) ? null : schemas.get(c.getSchema());
    // 如果没有指定schema或者schema为null,则使用全部集群。
    if (schema == null) {
        Map<String, CobarNode> nodes = cluster.getNodes();
        for (CobarNode n : nodes.values()) {
            if (n != null && n.isOnline()) {
                rows.add(getRow(n, c.getCharset()));
            }
        }
    } else {
        String group = (schema.getGroup() == null) ? "default" : schema.getGroup();
        List<String> nodeList = cluster.getGroups().get(group);
        if (nodeList != null && nodeList.size() > 0) {
            Map<String, CobarNode> nodes = cluster.getNodes();
            for (String id : nodeList) {
                CobarNode n = nodes.get(id);
                if (n != null && n.isOnline()) {
                    rows.add(getRow(n, c.getCharset()));
                }
            }
        }
        // 如果schema对应的group或者默认group都没有有效的节点,则使用全部集群。
        if (rows.size() == 0) {
            Map<String, CobarNode> nodes = cluster.getNodes();
            for (CobarNode n : nodes.values()) {
                if (n != null && n.isOnline()) {
                    rows.add(getRow(n, c.getCharset()));
                }
            }
        }
    }
    if (rows.size() == 0) {
        alarm.error(Alarms.CLUSTER_EMPTY + c.toString());
    }
    return rows;
}
Also used : SchemaConfig(com.alibaba.cobar.config.model.SchemaConfig) CobarNode(com.alibaba.cobar.CobarNode) RowDataPacket(com.alibaba.cobar.net.mysql.RowDataPacket) CobarConfig(com.alibaba.cobar.CobarConfig) LinkedList(java.util.LinkedList) CobarCluster(com.alibaba.cobar.CobarCluster)

Example 9 with CobarConfig

use of com.alibaba.cobar.CobarConfig in project cobar by alibaba.

the class MultiNodeExecutor method newExecute.

/**
     * 新通道的执行
     */
private void newExecute(final RouteResultsetNode rrn, final boolean autocommit, final BlockingSession ss, final int flag) {
    final ServerConnection sc = ss.getSource();
    // 检查数据节点是否存在
    CobarConfig conf = CobarServer.getInstance().getConfig();
    final MySQLDataNode dn = conf.getDataNodes().get(rrn.getName());
    if (dn == null) {
        handleFailure(ss, rrn, new SimpleErrInfo(new UnknownDataNodeException("Unknown dataNode '" + rrn.getName() + "'"), ErrorCode.ER_BAD_DB_ERROR, sc, rrn));
        return;
    }
    // 提交执行任务
    sc.getProcessor().getExecutor().execute(new Runnable() {

        @Override
        public void run() {
            // 取得数据通道
            int i = rrn.getReplicaIndex();
            Channel c = null;
            try {
                c = (i == DEFAULT_REPLICA_INDEX) ? dn.getChannel() : dn.getChannel(i);
            } catch (final Exception e) {
                handleFailure(ss, rrn, new SimpleErrInfo(e, ErrorCode.ER_BAD_DB_ERROR, sc, rrn));
                return;
            }
            c.setRunning(true);
            Channel old = ss.getTarget().put(rrn, c);
            if (old != null && c != old) {
                old.close();
            }
            // 执行
            execute0(rrn, c, autocommit, ss, flag);
        }
    });
}
Also used : MySQLDataNode(com.alibaba.cobar.mysql.MySQLDataNode) UnknownDataNodeException(com.alibaba.cobar.exception.UnknownDataNodeException) MySQLChannel(com.alibaba.cobar.mysql.bio.MySQLChannel) Channel(com.alibaba.cobar.mysql.bio.Channel) ServerConnection(com.alibaba.cobar.server.ServerConnection) CobarConfig(com.alibaba.cobar.CobarConfig) UnknownDataNodeException(com.alibaba.cobar.exception.UnknownDataNodeException) IOException(java.io.IOException)

Example 10 with CobarConfig

use of com.alibaba.cobar.CobarConfig 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

CobarConfig (com.alibaba.cobar.CobarConfig)15 MySQLDataNode (com.alibaba.cobar.mysql.MySQLDataNode)13 RowDataPacket (com.alibaba.cobar.net.mysql.RowDataPacket)7 SchemaConfig (com.alibaba.cobar.config.model.SchemaConfig)6 MySQLDataSource (com.alibaba.cobar.mysql.MySQLDataSource)6 EOFPacket (com.alibaba.cobar.net.mysql.EOFPacket)5 FieldPacket (com.alibaba.cobar.net.mysql.FieldPacket)5 ByteBuffer (java.nio.ByteBuffer)5 CobarCluster (com.alibaba.cobar.CobarCluster)3 DataSourceConfig (com.alibaba.cobar.config.model.DataSourceConfig)3 UserConfig (com.alibaba.cobar.config.model.UserConfig)3 MySQLConnection (com.alibaba.cobar.mysql.nio.MySQLConnection)3 ArrayList (java.util.ArrayList)3 CobarNode (com.alibaba.cobar.CobarNode)2 QuarantineConfig (com.alibaba.cobar.config.model.QuarantineConfig)2 UnknownDataNodeException (com.alibaba.cobar.exception.UnknownDataNodeException)2 Channel (com.alibaba.cobar.mysql.bio.Channel)2 MySQLChannel (com.alibaba.cobar.mysql.bio.MySQLChannel)2 RouteResultsetNode (com.alibaba.cobar.route.RouteResultsetNode)2 ServerConnection (com.alibaba.cobar.server.ServerConnection)2