Search in sources :

Example 1 with MySQLDataNode

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

use of com.alibaba.cobar.mysql.MySQLDataNode 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 3 with MySQLDataNode

use of com.alibaba.cobar.mysql.MySQLDataNode in project cobar by alibaba.

the class ShowSlow method dataNode.

public static void dataNode(ManagerConnection c, String name) {
    ByteBuffer buffer = c.allocate();
    // write header
    buffer = header.write(buffer, c);
    // write fields
    for (FieldPacket field : fields) {
        buffer = field.write(buffer, c);
    }
    // write eof
    buffer = eof.write(buffer, c);
    // write rows
    byte packetId = eof.packetId;
    CobarConfig conf = CobarServer.getInstance().getConfig();
    MySQLDataNode dn = conf.getDataNodes().get(name);
    MySQLDataSource ds = null;
    if (dn != null && (ds = dn.getSource()) != null) {
        SQLRecord[] records = ds.getSqlRecorder().getRecords();
        for (int i = records.length - 1; i >= 0; i--) {
            if (records[i] != null) {
                RowDataPacket row = getRow(records[i], 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);
    // write buffer
    c.write(buffer);
}
Also used : MySQLDataNode(com.alibaba.cobar.mysql.MySQLDataNode) MySQLDataSource(com.alibaba.cobar.mysql.MySQLDataSource) RowDataPacket(com.alibaba.cobar.net.mysql.RowDataPacket) EOFPacket(com.alibaba.cobar.net.mysql.EOFPacket) CobarConfig(com.alibaba.cobar.CobarConfig) SQLRecord(com.alibaba.cobar.statistic.SQLRecord) ByteBuffer(java.nio.ByteBuffer) FieldPacket(com.alibaba.cobar.net.mysql.FieldPacket)

Example 4 with MySQLDataNode

use of com.alibaba.cobar.mysql.MySQLDataNode in project cobar by alibaba.

the class ShowSlow method schema.

public static void schema(ManagerConnection c, String name) {
    ByteBuffer buffer = c.allocate();
    // write header
    buffer = header.write(buffer, c);
    // write fields
    for (FieldPacket field : fields) {
        buffer = field.write(buffer, c);
    }
    // write eof
    buffer = eof.write(buffer, c);
    // write rows
    byte packetId = eof.packetId;
    CobarConfig conf = CobarServer.getInstance().getConfig();
    SchemaConfig schema = conf.getSchemas().get(name);
    if (schema != null) {
        SQLRecorder recorder = new SQLRecorder(conf.getSystem().getSqlRecordCount());
        Map<String, MySQLDataNode> dataNodes = conf.getDataNodes();
        for (String n : schema.getAllDataNodes()) {
            MySQLDataNode dn = dataNodes.get(n);
            MySQLDataSource ds = null;
            if (dn != null && (ds = dn.getSource()) != null) {
                for (SQLRecord r : ds.getSqlRecorder().getRecords()) {
                    if (r != null && recorder.check(r.executeTime)) {
                        recorder.add(r);
                    }
                }
            }
        }
        SQLRecord[] records = recorder.getRecords();
        for (int i = records.length - 1; i >= 0; i--) {
            if (records[i] != null) {
                RowDataPacket row = getRow(records[i], 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);
    // write buffer
    c.write(buffer);
}
Also used : MySQLDataNode(com.alibaba.cobar.mysql.MySQLDataNode) SchemaConfig(com.alibaba.cobar.config.model.SchemaConfig) SQLRecorder(com.alibaba.cobar.statistic.SQLRecorder) RowDataPacket(com.alibaba.cobar.net.mysql.RowDataPacket) EOFPacket(com.alibaba.cobar.net.mysql.EOFPacket) CobarConfig(com.alibaba.cobar.CobarConfig) SQLRecord(com.alibaba.cobar.statistic.SQLRecord) ByteBuffer(java.nio.ByteBuffer) MySQLDataSource(com.alibaba.cobar.mysql.MySQLDataSource) FieldPacket(com.alibaba.cobar.net.mysql.FieldPacket)

Example 5 with MySQLDataNode

use of com.alibaba.cobar.mysql.MySQLDataNode in project cobar by alibaba.

the class StopHeartbeat method execute.

public static void execute(String stmt, ManagerConnection c) {
    int count = 0;
    Pair<String[], Integer> keys = ManagerParseStop.getPair(stmt);
    if (keys.getKey() != null && keys.getValue() != null) {
        long time = keys.getValue().intValue() * 1000L;
        Map<String, MySQLDataNode> dns = CobarServer.getInstance().getConfig().getDataNodes();
        for (String key : keys.getKey()) {
            MySQLDataNode dn = dns.get(key);
            if (dn != null) {
                dn.setHeartbeatRecoveryTime(TimeUtil.currentTimeMillis() + time);
                ++count;
                StringBuilder s = new StringBuilder();
                s.append(dn.getName()).append(" stop heartbeat '");
                logger.warn(s.append(FormatUtil.formatTime(time, 3)).append("' by manager."));
            }
        }
    }
    OkPacket packet = new OkPacket();
    packet.packetId = 1;
    packet.affectedRows = count;
    packet.serverStatus = 2;
    packet.write(c);
}
Also used : MySQLDataNode(com.alibaba.cobar.mysql.MySQLDataNode) OkPacket(com.alibaba.cobar.net.mysql.OkPacket)

Aggregations

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