Search in sources :

Example 1 with DataSourceConfig

use of com.alibaba.cobar.config.model.DataSourceConfig in project cobar by alibaba.

the class MySQLChannelMain method getChannel.

public Channel getChannel() throws Exception {
    DataSourceConfig config = new DataSourceConfig();
    config.setHost("10.20.153.177");
    config.setPort(3306);
    config.setUser("offer");
    config.setPassword("offer");
    config.setDatabase("offer1");
    MySQLDataSource ds = new MySQLDataSource(null, 0, config, 1);
    return ds.getChannel();
}
Also used : DataSourceConfig(com.alibaba.cobar.config.model.DataSourceConfig)

Example 2 with DataSourceConfig

use of com.alibaba.cobar.config.model.DataSourceConfig in project cobar by alibaba.

the class MySQLDetectorFactory method make.

public MySQLDetector make(MySQLHeartbeat heartbeat) throws IOException {
    SocketChannel channel = openSocketChannel();
    DataSourceConfig dsc = heartbeat.getSource().getConfig();
    DataNodeConfig dnc = heartbeat.getSource().getNode().getConfig();
    MySQLDetector detector = new MySQLDetector(channel);
    detector.setHost(dsc.getHost());
    detector.setPort(dsc.getPort());
    detector.setUser(dsc.getUser());
    detector.setPassword(dsc.getPassword());
    detector.setSchema(dsc.getDatabase());
    detector.setHeartbeatTimeout(dnc.getHeartbeatTimeout());
    detector.setHeartbeat(heartbeat);
    postConnect(detector, CobarServer.getInstance().getConnector());
    return detector;
}
Also used : SocketChannel(java.nio.channels.SocketChannel) DataSourceConfig(com.alibaba.cobar.config.model.DataSourceConfig) DataNodeConfig(com.alibaba.cobar.config.model.DataNodeConfig)

Example 3 with DataSourceConfig

use of com.alibaba.cobar.config.model.DataSourceConfig in project cobar by alibaba.

the class ReloadConfig method reload.

private static boolean reload() {
    // 载入新的配置
    ConfigInitializer loader = new ConfigInitializer();
    Map<String, UserConfig> users = loader.getUsers();
    Map<String, SchemaConfig> schemas = loader.getSchemas();
    Map<String, MySQLDataNode> dataNodes = loader.getDataNodes();
    Map<String, DataSourceConfig> dataSources = loader.getDataSources();
    CobarCluster cluster = loader.getCluster();
    QuarantineConfig quarantine = loader.getQuarantine();
    // 应用新配置
    CobarConfig conf = CobarServer.getInstance().getConfig();
    // 如果重载已经存在的数据节点,初始化连接数参考空闲连接数,否则为1。
    boolean reloadStatus = true;
    Map<String, MySQLDataNode> cNodes = conf.getDataNodes();
    for (MySQLDataNode dn : dataNodes.values()) {
        MySQLDataNode cdn = cNodes.get(dn.getName());
        if (cdn != null && cdn.getSource() != null) {
            int size = Math.min(cdn.getSource().getIdleCount(), dn.getConfig().getPoolSize());
            dn.init(size > 0 ? size : 1, 0);
        } else {
            dn.init(1, 0);
        }
        if (!dn.isInitSuccess()) {
            reloadStatus = false;
            break;
        }
    }
    // 如果重载不成功,则清理已初始化的资源。
    if (!reloadStatus) {
        for (MySQLDataNode dn : dataNodes.values()) {
            MySQLDataSource ds = dn.getSource();
            if (ds != null) {
                ds.clear();
            }
        }
        return false;
    }
    // 应用重载
    conf.reload(users, schemas, dataNodes, dataSources, cluster, quarantine);
    // 处理旧的资源
    for (MySQLDataNode dn : cNodes.values()) {
        MySQLDataSource ds = dn.getSource();
        if (ds != null) {
            ds.clear();
        }
    }
    return true;
}
Also used : MySQLDataNode(com.alibaba.cobar.mysql.MySQLDataNode) SchemaConfig(com.alibaba.cobar.config.model.SchemaConfig) DataSourceConfig(com.alibaba.cobar.config.model.DataSourceConfig) ConfigInitializer(com.alibaba.cobar.ConfigInitializer) QuarantineConfig(com.alibaba.cobar.config.model.QuarantineConfig) CobarConfig(com.alibaba.cobar.CobarConfig) UserConfig(com.alibaba.cobar.config.model.UserConfig) MySQLDataSource(com.alibaba.cobar.mysql.MySQLDataSource) CobarCluster(com.alibaba.cobar.CobarCluster)

Example 4 with DataSourceConfig

use of com.alibaba.cobar.config.model.DataSourceConfig in project cobar by alibaba.

the class RollbackConfig method rollback.

private static boolean rollback() {
    CobarConfig conf = CobarServer.getInstance().getConfig();
    Map<String, UserConfig> users = conf.getBackupUsers();
    Map<String, SchemaConfig> schemas = conf.getBackupSchemas();
    Map<String, MySQLDataNode> dataNodes = conf.getBackupDataNodes();
    Map<String, DataSourceConfig> dataSources = conf.getBackupDataSources();
    CobarCluster cluster = conf.getBackupCluster();
    QuarantineConfig quarantine = conf.getBackupQuarantine();
    // 检查可回滚状态
    if (!conf.canRollback()) {
        return false;
    }
    // 如果回滚已经存在的pool,初始化连接数参考空闲连接数,否则为1。
    boolean rollbackStatus = true;
    Map<String, MySQLDataNode> cNodes = conf.getDataNodes();
    for (MySQLDataNode dn : dataNodes.values()) {
        MySQLDataNode cdn = cNodes.get(dn.getName());
        if (cdn != null && cdn.getSource() != null) {
            int size = Math.min(cdn.getSource().getIdleCount(), dn.getConfig().getPoolSize());
            dn.init(size > 0 ? size : 1, dn.getActivedIndex());
        } else {
            dn.init(1, dn.getActivedIndex());
        }
        if (!dn.isInitSuccess()) {
            rollbackStatus = false;
            break;
        }
    }
    // 如果回滚不成功,则清理已初始化的资源。
    if (!rollbackStatus) {
        for (MySQLDataNode dn : dataNodes.values()) {
            MySQLDataSource ds = dn.getSource();
            if (ds != null) {
                ds.clear();
            }
        }
        return false;
    }
    // 应用回滚
    conf.rollback(users, schemas, dataNodes, dataSources, cluster, quarantine);
    // 处理旧的资源
    for (MySQLDataNode dn : cNodes.values()) {
        MySQLDataSource ds = dn.getSource();
        if (ds != null) {
            ds.clear();
        }
    }
    return true;
}
Also used : MySQLDataNode(com.alibaba.cobar.mysql.MySQLDataNode) SchemaConfig(com.alibaba.cobar.config.model.SchemaConfig) DataSourceConfig(com.alibaba.cobar.config.model.DataSourceConfig) QuarantineConfig(com.alibaba.cobar.config.model.QuarantineConfig) CobarConfig(com.alibaba.cobar.CobarConfig) UserConfig(com.alibaba.cobar.config.model.UserConfig) MySQLDataSource(com.alibaba.cobar.mysql.MySQLDataSource) CobarCluster(com.alibaba.cobar.CobarCluster)

Example 5 with DataSourceConfig

use of com.alibaba.cobar.config.model.DataSourceConfig 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)

Aggregations

DataSourceConfig (com.alibaba.cobar.config.model.DataSourceConfig)9 MySQLDataSource (com.alibaba.cobar.mysql.MySQLDataSource)5 MySQLDataNode (com.alibaba.cobar.mysql.MySQLDataNode)4 CobarConfig (com.alibaba.cobar.CobarConfig)3 CobarCluster (com.alibaba.cobar.CobarCluster)2 QuarantineConfig (com.alibaba.cobar.config.model.QuarantineConfig)2 SchemaConfig (com.alibaba.cobar.config.model.SchemaConfig)2 UserConfig (com.alibaba.cobar.config.model.UserConfig)2 RowDataPacket (com.alibaba.cobar.net.mysql.RowDataPacket)2 SocketChannel (java.nio.channels.SocketChannel)2 ArrayList (java.util.ArrayList)2 ConfigInitializer (com.alibaba.cobar.ConfigInitializer)1 DataNodeConfig (com.alibaba.cobar.config.model.DataNodeConfig)1 ConfigException (com.alibaba.cobar.config.util.ConfigException)1 EOFPacket (com.alibaba.cobar.net.mysql.EOFPacket)1 FieldPacket (com.alibaba.cobar.net.mysql.FieldPacket)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 Element (org.w3c.dom.Element)1 NodeList (org.w3c.dom.NodeList)1