Search in sources :

Example 46 with PhysicalDBPool

use of io.mycat.backend.datasource.PhysicalDBPool in project Mycat_plus by coderczp.

the class ShowHeartbeatDetail method getRows.

private static List<RowDataPacket> getRows(String name, String charset) {
    List<RowDataPacket> list = new LinkedList<RowDataPacket>();
    MycatConfig conf = MycatServer.getInstance().getConfig();
    // host nodes
    String type = "";
    String ip = "";
    int port = 0;
    DBHeartbeat hb = null;
    Map<String, PhysicalDBPool> dataHosts = conf.getDataHosts();
    for (PhysicalDBPool pool : dataHosts.values()) {
        for (PhysicalDatasource ds : pool.getAllDataSources()) {
            if (name.equals(ds.getName())) {
                hb = ds.getHeartbeat();
                type = ds.getConfig().getDbType();
                ip = ds.getConfig().getIp();
                port = ds.getConfig().getPort();
                break;
            }
        }
    }
    if (hb != null) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Queue<HeartbeatRecorder.Record> heatbeartRecorders = hb.getRecorder().getRecordsAll();
        for (HeartbeatRecorder.Record record : heatbeartRecorders) {
            RowDataPacket row = new RowDataPacket(FIELD_COUNT);
            row.add(StringUtil.encode(name, charset));
            row.add(StringUtil.encode(type, charset));
            row.add(StringUtil.encode(ip, charset));
            row.add(IntegerUtil.toBytes(port));
            long time = record.getTime();
            String timeStr = sdf.format(new Date(time));
            row.add(StringUtil.encode(timeStr, charset));
            row.add(LongUtil.toBytes(record.getValue()));
            list.add(row);
        }
    } else {
        RowDataPacket row = new RowDataPacket(FIELD_COUNT);
        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 : DBHeartbeat(io.mycat.backend.heartbeat.DBHeartbeat) RowDataPacket(io.mycat.net.mysql.RowDataPacket) PhysicalDBPool(io.mycat.backend.datasource.PhysicalDBPool) MycatConfig(io.mycat.config.MycatConfig) LinkedList(java.util.LinkedList) Date(java.util.Date) PhysicalDatasource(io.mycat.backend.datasource.PhysicalDatasource) HeartbeatRecorder(io.mycat.statistic.HeartbeatRecorder) SimpleDateFormat(java.text.SimpleDateFormat)

Example 47 with PhysicalDBPool

use of io.mycat.backend.datasource.PhysicalDBPool in project Mycat_plus by coderczp.

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, PhysicalDBPool> dns = MycatServer.getInstance().getConfig().getDataHosts();
        for (String key : keys.getKey()) {
            PhysicalDBPool dn = dns.get(key);
            if (dn != null) {
                dn.getSource().setHeartbeatRecoveryTime(TimeUtil.currentTimeMillis() + time);
                ++count;
                StringBuilder s = new StringBuilder();
                s.append(dn.getHostName()).append(" stop heartbeat '");
                logger.warn(s.append(FormatUtil.formatTime(time, 3)).append("' by manager.").toString());
            }
        }
    }
    OkPacket packet = new OkPacket();
    packet.packetId = 1;
    packet.affectedRows = count;
    packet.serverStatus = 2;
    packet.write(c);
}
Also used : OkPacket(io.mycat.net.mysql.OkPacket) PhysicalDBPool(io.mycat.backend.datasource.PhysicalDBPool)

Example 48 with PhysicalDBPool

use of io.mycat.backend.datasource.PhysicalDBPool in project Mycat-Server by MyCATApache.

the class MycatConfig method apply.

private void apply(Map<String, UserConfig> newUsers, Map<String, SchemaConfig> newSchemas, Map<String, PhysicalDBNode> newDataNodes, Map<String, PhysicalDBPool> newDataHosts, MycatCluster newCluster, FirewallConfig newFirewall, boolean isLoadAll) {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        // --------------------------------------------
        if (isLoadAll) {
            Map<String, PhysicalDBPool> oldDataHosts = this.dataHosts;
            if (oldDataHosts != null) {
                for (PhysicalDBPool oldDbPool : oldDataHosts.values()) {
                    if (oldDbPool != null) {
                        oldDbPool.stopHeartbeat();
                    }
                }
            }
            this._dataNodes = this.dataNodes;
            this._dataHosts = this.dataHosts;
        }
        this._users = this.users;
        this._schemas = this.schemas;
        this._cluster = this.cluster;
        this._firewall = this.firewall;
        // ---------------------------------------------------
        if (isLoadAll) {
            if (newDataHosts != null) {
                for (PhysicalDBPool newDbPool : newDataHosts.values()) {
                    if (newDbPool != null) {
                        newDbPool.startHeartbeat();
                    }
                }
            }
            this.dataNodes = newDataNodes;
            this.dataHosts = newDataHosts;
        }
        this.users = newUsers;
        this.schemas = newSchemas;
        this.cluster = newCluster;
        this.firewall = newFirewall;
    } finally {
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) PhysicalDBPool(io.mycat.backend.datasource.PhysicalDBPool)

Example 49 with PhysicalDBPool

use of io.mycat.backend.datasource.PhysicalDBPool in project Mycat-Server by MyCATApache.

the class ConfigInitializer method getPhysicalDBPool.

private PhysicalDBPool getPhysicalDBPool(DataHostConfig conf, ConfigLoader configLoader) {
    String name = conf.getName();
    // 数据库类型,我们这里只讨论MySQL
    String dbType = conf.getDbType();
    // 连接数据库驱动,我们这里只讨论MyCat自己实现的native
    String dbDriver = conf.getDbDriver();
    // 针对所有写节点创建PhysicalDatasource
    PhysicalDatasource[] writeSources = createDataSource(conf, name, dbType, dbDriver, conf.getWriteHosts(), false);
    Map<Integer, DBHostConfig[]> readHostsMap = conf.getReadHosts();
    Map<Integer, PhysicalDatasource[]> readSourcesMap = new HashMap<Integer, PhysicalDatasource[]>(readHostsMap.size());
    // 对于每个读节点建立key为writeHost下标value为readHost的PhysicalDatasource[]的哈希表
    for (Map.Entry<Integer, DBHostConfig[]> entry : readHostsMap.entrySet()) {
        PhysicalDatasource[] readSources = createDataSource(conf, name, dbType, dbDriver, entry.getValue(), true);
        readSourcesMap.put(entry.getKey(), readSources);
    }
    PhysicalDBPool pool = new PhysicalDBPool(conf.getName(), conf, writeSources, readSourcesMap, conf.getBalance(), conf.getWriteType());
    pool.setSlaveIDs(conf.getSlaveIDs());
    return pool;
}
Also used : PhysicalDatasource(io.mycat.backend.datasource.PhysicalDatasource) HashMap(java.util.HashMap) PhysicalDBPool(io.mycat.backend.datasource.PhysicalDBPool) HashMap(java.util.HashMap) Map(java.util.Map)

Example 50 with PhysicalDBPool

use of io.mycat.backend.datasource.PhysicalDBPool in project Mycat-Server by MyCATApache.

the class ConfigInitializer method initDataNodes.

private Map<String, PhysicalDBNode> initDataNodes(ConfigLoader configLoader) {
    Map<String, DataNodeConfig> nodeConfs = configLoader.getDataNodes();
    Map<String, PhysicalDBNode> nodes = new HashMap<String, PhysicalDBNode>(nodeConfs.size());
    for (DataNodeConfig conf : nodeConfs.values()) {
        PhysicalDBPool pool = this.dataHosts.get(conf.getDataHost());
        if (pool == null) {
            throw new ConfigException("dataHost not exists " + conf.getDataHost());
        }
        PhysicalDBNode dataNode = new PhysicalDBNode(conf.getName(), conf.getDatabase(), pool);
        nodes.put(dataNode.getName(), dataNode);
    }
    return nodes;
}
Also used : PhysicalDBNode(io.mycat.backend.datasource.PhysicalDBNode) HashMap(java.util.HashMap) PhysicalDBPool(io.mycat.backend.datasource.PhysicalDBPool) ConfigException(io.mycat.config.util.ConfigException) DataNodeConfig(io.mycat.config.model.DataNodeConfig)

Aggregations

PhysicalDBPool (io.mycat.backend.datasource.PhysicalDBPool)69 PhysicalDatasource (io.mycat.backend.datasource.PhysicalDatasource)43 PhysicalDBNode (io.mycat.backend.datasource.PhysicalDBNode)28 MycatConfig (io.mycat.config.MycatConfig)22 HashMap (java.util.HashMap)14 DBHostConfig (io.mycat.config.model.DBHostConfig)12 RowDataPacket (io.mycat.net.mysql.RowDataPacket)12 DBHeartbeat (io.mycat.backend.heartbeat.DBHeartbeat)11 LinkedList (java.util.LinkedList)10 Connection (java.sql.Connection)8 MycatCluster (io.mycat.config.MycatCluster)6 DataHostConfig (io.mycat.config.model.DataHostConfig)6 FirewallConfig (io.mycat.config.model.FirewallConfig)6 SchemaConfig (io.mycat.config.model.SchemaConfig)6 UserConfig (io.mycat.config.model.UserConfig)6 DataSourceSyncRecorder (io.mycat.statistic.DataSourceSyncRecorder)6 IOException (java.io.IOException)6 Map (java.util.Map)6 ArrayList (java.util.ArrayList)5 MySQLConsistencyChecker (io.mycat.backend.heartbeat.MySQLConsistencyChecker)4