Search in sources :

Example 11 with CobarConfig

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

the class ShowHeartbeat method getRows.

private static List<RowDataPacket> getRows() {
    List<RowDataPacket> list = new LinkedList<RowDataPacket>();
    CobarConfig conf = CobarServer.getInstance().getConfig();
    // cobar nodes
    Map<String, CobarNode> cobarNodes = conf.getCluster().getNodes();
    List<String> cobarNodeKeys = new ArrayList<String>(cobarNodes.size());
    cobarNodeKeys.addAll(cobarNodes.keySet());
    Collections.sort(cobarNodeKeys);
    for (String key : cobarNodeKeys) {
        CobarNode node = cobarNodes.get(key);
        if (node != null) {
            CobarHeartbeat hb = node.getHeartbeat();
            RowDataPacket row = new RowDataPacket(FIELD_COUNT);
            row.add(node.getName().getBytes());
            row.add("COBAR".getBytes());
            row.add(node.getConfig().getHost().getBytes());
            row.add(IntegerUtil.toBytes(node.getConfig().getPort()));
            row.add(IntegerUtil.toBytes(hb.getStatus()));
            row.add(IntegerUtil.toBytes(hb.getErrorCount()));
            row.add(hb.isChecking() ? "checking".getBytes() : "idle".getBytes());
            row.add(LongUtil.toBytes(hb.getTimeout()));
            row.add(hb.getRecorder().get().getBytes());
            String at = hb.lastActiveTime();
            row.add(at == null ? null : at.getBytes());
            row.add(hb.isStop() ? "true".getBytes() : "false".getBytes());
            list.add(row);
        }
    }
    // data nodes
    Map<String, MySQLDataNode> dataNodes = conf.getDataNodes();
    List<String> dataNodeKeys = new ArrayList<String>(dataNodes.size());
    dataNodeKeys.addAll(dataNodes.keySet());
    Collections.sort(dataNodeKeys, new Comparators<String>());
    for (String key : dataNodeKeys) {
        MySQLDataNode node = dataNodes.get(key);
        if (node != null) {
            MySQLHeartbeat hb = node.getHeartbeat();
            RowDataPacket row = new RowDataPacket(FIELD_COUNT);
            row.add(node.getName().getBytes());
            row.add("MYSQL".getBytes());
            if (hb != null) {
                row.add(hb.getSource().getConfig().getHost().getBytes());
                row.add(IntegerUtil.toBytes(hb.getSource().getConfig().getPort()));
                row.add(IntegerUtil.toBytes(hb.getStatus()));
                row.add(IntegerUtil.toBytes(hb.getErrorCount()));
                row.add(hb.isChecking() ? "checking".getBytes() : "idle".getBytes());
                row.add(LongUtil.toBytes(hb.getTimeout()));
                row.add(hb.getRecorder().get().getBytes());
                String lat = hb.getLastActiveTime();
                row.add(lat == null ? null : lat.getBytes());
                row.add(hb.isStop() ? "true".getBytes() : "false".getBytes());
            } else {
                row.add(null);
                row.add(null);
                row.add(null);
                row.add(null);
                row.add(null);
                row.add(null);
                row.add(null);
                row.add(null);
                row.add(null);
            }
            list.add(row);
        }
    }
    return list;
}
Also used : MySQLDataNode(com.alibaba.cobar.mysql.MySQLDataNode) RowDataPacket(com.alibaba.cobar.net.mysql.RowDataPacket) ArrayList(java.util.ArrayList) MySQLHeartbeat(com.alibaba.cobar.heartbeat.MySQLHeartbeat) CobarConfig(com.alibaba.cobar.CobarConfig) LinkedList(java.util.LinkedList) CobarHeartbeat(com.alibaba.cobar.heartbeat.CobarHeartbeat) CobarNode(com.alibaba.cobar.CobarNode)

Example 12 with CobarConfig

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

the class ShowDataNode method execute.

public static void execute(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();
    Map<String, MySQLDataNode> dataNodes = conf.getDataNodes();
    List<String> keys = new ArrayList<String>();
    if (StringUtil.isEmpty(name)) {
        keys.addAll(dataNodes.keySet());
    } else {
        SchemaConfig sc = conf.getSchemas().get(name);
        if (null != sc) {
            keys.addAll(sc.getAllDataNodes());
        }
    }
    Collections.sort(keys, new Comparators<String>());
    for (String key : keys) {
        RowDataPacket row = getRow(dataNodes.get(key), 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);
    // post write
    c.write(buffer);
}
Also used : MySQLDataNode(com.alibaba.cobar.mysql.MySQLDataNode) SchemaConfig(com.alibaba.cobar.config.model.SchemaConfig) RowDataPacket(com.alibaba.cobar.net.mysql.RowDataPacket) ArrayList(java.util.ArrayList) EOFPacket(com.alibaba.cobar.net.mysql.EOFPacket) CobarConfig(com.alibaba.cobar.CobarConfig) ByteBuffer(java.nio.ByteBuffer) FieldPacket(com.alibaba.cobar.net.mysql.FieldPacket)

Example 13 with CobarConfig

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

the class ShowDataSource method execute.

public static void execute(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();
    Map<String, DataSourceConfig> dataSources = conf.getDataSources();
    List<String> keys = new ArrayList<String>();
    if (null != name) {
        MySQLDataNode dn = conf.getDataNodes().get(name);
        if (dn != null)
            for (MySQLDataSource ds : dn.getSources()) {
                if (ds != null) {
                    keys.add(ds.getName());
                }
            }
    } else {
        keys.addAll(dataSources.keySet());
    }
    Collections.sort(keys, new Comparators<String>());
    for (String key : keys) {
        RowDataPacket row = getRow(dataSources.get(key), 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);
    // post write
    c.write(buffer);
}
Also used : MySQLDataNode(com.alibaba.cobar.mysql.MySQLDataNode) DataSourceConfig(com.alibaba.cobar.config.model.DataSourceConfig) RowDataPacket(com.alibaba.cobar.net.mysql.RowDataPacket) ArrayList(java.util.ArrayList) EOFPacket(com.alibaba.cobar.net.mysql.EOFPacket) CobarConfig(com.alibaba.cobar.CobarConfig) ByteBuffer(java.nio.ByteBuffer) MySQLDataSource(com.alibaba.cobar.mysql.MySQLDataSource) FieldPacket(com.alibaba.cobar.net.mysql.FieldPacket)

Example 14 with CobarConfig

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

the class ShowDatabases method response.

public static void response(ServerConnection c) {
    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();
    Map<String, UserConfig> users = conf.getUsers();
    UserConfig user = users == null ? null : users.get(c.getUser());
    if (user != null) {
        TreeSet<String> schemaSet = new TreeSet<String>();
        Set<String> schemaList = user.getSchemas();
        if (schemaList == null || schemaList.size() == 0) {
            schemaSet.addAll(conf.getSchemas().keySet());
        } else {
            for (String schema : schemaList) {
                schemaSet.add(schema);
            }
        }
        for (String name : schemaSet) {
            RowDataPacket row = new RowDataPacket(FIELD_COUNT);
            row.add(StringUtil.encode(name, 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);
    // post write
    c.write(buffer);
}
Also used : TreeSet(java.util.TreeSet) RowDataPacket(com.alibaba.cobar.net.mysql.RowDataPacket) EOFPacket(com.alibaba.cobar.net.mysql.EOFPacket) CobarConfig(com.alibaba.cobar.CobarConfig) UserConfig(com.alibaba.cobar.config.model.UserConfig) ByteBuffer(java.nio.ByteBuffer) FieldPacket(com.alibaba.cobar.net.mysql.FieldPacket)

Example 15 with CobarConfig

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

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