Search in sources :

Example 11 with MySQLDataNode

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

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

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

the class CobarConfig method apply.

private void apply(Map<String, UserConfig> users, Map<String, SchemaConfig> schemas, Map<String, MySQLDataNode> dataNodes, Map<String, DataSourceConfig> dataSources, CobarCluster cluster, QuarantineConfig quarantine) {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        // stop mysql heartbeat
        Map<String, MySQLDataNode> oldDataNodes = this.dataNodes;
        if (oldDataNodes != null) {
            for (MySQLDataNode n : oldDataNodes.values()) {
                if (n != null) {
                    n.stopHeartbeat();
                }
            }
        }
        // stop cobar heartbeat
        CobarCluster oldCluster = this.cluster;
        if (oldCluster != null) {
            Map<String, CobarNode> nodes = oldCluster.getNodes();
            for (CobarNode n : nodes.values()) {
                if (n != null) {
                    n.stopHeartbeat();
                }
            }
        }
        this._users = this.users;
        this._schemas = this.schemas;
        this._dataNodes = this.dataNodes;
        this._dataSources = this.dataSources;
        this._cluster = this.cluster;
        this._quarantine = this.quarantine;
        // start mysql heartbeat
        if (dataNodes != null) {
            for (MySQLDataNode n : dataNodes.values()) {
                if (n != null) {
                    n.startHeartbeat();
                }
            }
        }
        // start cobar heartbeat
        if (cluster != null) {
            Map<String, CobarNode> nodes = cluster.getNodes();
            for (CobarNode n : nodes.values()) {
                if (n != null) {
                    n.startHeartbeat();
                }
            }
        }
        this.users = users;
        this.schemas = schemas;
        this.dataNodes = dataNodes;
        this.dataSources = dataSources;
        this.cluster = cluster;
        this.quarantine = quarantine;
    } finally {
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) MySQLDataNode(com.alibaba.cobar.mysql.MySQLDataNode)

Example 14 with MySQLDataNode

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

the class CobarServer method startup.

public void startup() throws IOException {
    // server startup
    LOGGER.info("===============================================");
    LOGGER.info(NAME + " is ready to startup ...");
    SystemConfig system = config.getSystem();
    timer.schedule(updateTime(), 0L, TIME_UPDATE_PERIOD);
    // startup processors
    LOGGER.info("Startup processors ...");
    int handler = system.getProcessorHandler();
    int executor = system.getProcessorExecutor();
    int committer = system.getProcessorCommitter();
    processors = new NIOProcessor[system.getProcessors()];
    for (int i = 0; i < processors.length; i++) {
        processors[i] = new NIOProcessor("Processor" + i, handler, executor, committer);
        processors[i].startup();
    }
    timer.schedule(processorCheck(), 0L, system.getProcessorCheckPeriod());
    // startup connector
    LOGGER.info("Startup connector ...");
    connector = new NIOConnector(NAME + "Connector");
    connector.setProcessors(processors);
    connector.start();
    // init dataNodes
    Map<String, MySQLDataNode> dataNodes = config.getDataNodes();
    LOGGER.info("Initialize dataNodes ...");
    for (MySQLDataNode node : dataNodes.values()) {
        node.init(1, 0);
    }
    timer.schedule(dataNodeIdleCheck(), 0L, system.getDataNodeIdleCheckPeriod());
    timer.schedule(dataNodeHeartbeat(), 0L, system.getDataNodeHeartbeatPeriod());
    // startup manager
    ManagerConnectionFactory mf = new ManagerConnectionFactory();
    mf.setCharset(system.getCharset());
    mf.setIdleTimeout(system.getIdleTimeout());
    manager = new NIOAcceptor(NAME + "Manager", system.getManagerPort(), mf);
    manager.setProcessors(processors);
    manager.start();
    LOGGER.info(manager.getName() + " is started and listening on " + manager.getPort());
    // startup server
    ServerConnectionFactory sf = new ServerConnectionFactory();
    sf.setCharset(system.getCharset());
    sf.setIdleTimeout(system.getIdleTimeout());
    server = new NIOAcceptor(NAME + "Server", system.getServerPort(), sf);
    server.setProcessors(processors);
    server.start();
    timer.schedule(clusterHeartbeat(), 0L, system.getClusterHeartbeatPeriod());
    // server started
    LOGGER.info(server.getName() + " is started and listening on " + server.getPort());
    LOGGER.info("===============================================");
}
Also used : MySQLDataNode(com.alibaba.cobar.mysql.MySQLDataNode) SystemConfig(com.alibaba.cobar.config.model.SystemConfig) ServerConnectionFactory(com.alibaba.cobar.server.ServerConnectionFactory) NIOConnector(com.alibaba.cobar.net.NIOConnector) ManagerConnectionFactory(com.alibaba.cobar.manager.ManagerConnectionFactory) NIOAcceptor(com.alibaba.cobar.net.NIOAcceptor) NIOProcessor(com.alibaba.cobar.net.NIOProcessor)

Example 15 with MySQLDataNode

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

the class ConfigInitializer method initDataNodes.

private Map<String, MySQLDataNode> initDataNodes(ConfigLoader configLoader) {
    Map<String, DataNodeConfig> nodeConfs = configLoader.getDataNodes();
    Map<String, MySQLDataNode> nodes = new HashMap<String, MySQLDataNode>(nodeConfs.size());
    for (DataNodeConfig conf : nodeConfs.values()) {
        MySQLDataNode dataNode = getDataNode(conf, configLoader);
        if (nodes.containsKey(dataNode.getName())) {
            throw new ConfigException("dataNode " + dataNode.getName() + " duplicated!");
        }
        nodes.put(dataNode.getName(), dataNode);
    }
    return nodes;
}
Also used : MySQLDataNode(com.alibaba.cobar.mysql.MySQLDataNode) HashMap(java.util.HashMap) ConfigException(com.alibaba.cobar.config.util.ConfigException) DataNodeConfig(com.alibaba.cobar.config.model.DataNodeConfig)

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