Search in sources :

Example 6 with PhysicalDatasource

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

the class ShowDatasourceSyn method getRows.

private static List<RowDataPacket> getRows(String charset) {
    List<RowDataPacket> list = new LinkedList<RowDataPacket>();
    MycatConfig conf = MycatServer.getInstance().getConfig();
    // host nodes
    Map<String, PhysicalDBPool> dataHosts = conf.getDataHosts();
    for (PhysicalDBPool pool : dataHosts.values()) {
        for (PhysicalDatasource ds : pool.getAllDataSources()) {
            DBHeartbeat hb = ds.getHeartbeat();
            DataSourceSyncRecorder record = hb.getAsynRecorder();
            Map<String, String> states = record.getRecords();
            RowDataPacket row = new RowDataPacket(FIELD_COUNT);
            if (!states.isEmpty()) {
                row.add(StringUtil.encode(ds.getName(), charset));
                row.add(StringUtil.encode(ds.getConfig().getIp(), charset));
                row.add(LongUtil.toBytes(ds.getConfig().getPort()));
                row.add(StringUtil.encode(states.get("Master_Host"), charset));
                row.add(LongUtil.toBytes(Long.valueOf(states.get("Master_Port"))));
                row.add(StringUtil.encode(states.get("Master_Use"), charset));
                String secords = states.get("Seconds_Behind_Master");
                row.add(secords == null ? null : LongUtil.toBytes(Long.valueOf(secords)));
                row.add(StringUtil.encode(states.get("Slave_IO_Running"), charset));
                row.add(StringUtil.encode(states.get("Slave_SQL_Running"), charset));
                row.add(StringUtil.encode(states.get("Slave_IO_State"), charset));
                row.add(LongUtil.toBytes(Long.valueOf(states.get("Connect_Retry"))));
                row.add(StringUtil.encode(states.get("Last_IO_Error"), charset));
                list.add(row);
            }
        }
    }
    return list;
}
Also used : PhysicalDatasource(io.mycat.backend.datasource.PhysicalDatasource) DBHeartbeat(io.mycat.backend.heartbeat.DBHeartbeat) RowDataPacket(io.mycat.net.mysql.RowDataPacket) DataSourceSyncRecorder(io.mycat.statistic.DataSourceSyncRecorder) PhysicalDBPool(io.mycat.backend.datasource.PhysicalDBPool) MycatConfig(io.mycat.config.MycatConfig) LinkedList(java.util.LinkedList)

Example 7 with PhysicalDatasource

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

the class ShowHeartbeat method getRows.

private static List<RowDataPacket> getRows() {
    List<RowDataPacket> list = new LinkedList<RowDataPacket>();
    MycatConfig conf = MycatServer.getInstance().getConfig();
    // host nodes
    Map<String, PhysicalDBPool> dataHosts = conf.getDataHosts();
    for (PhysicalDBPool pool : dataHosts.values()) {
        for (PhysicalDatasource ds : pool.getAllDataSources()) {
            DBHeartbeat hb = ds.getHeartbeat();
            RowDataPacket row = new RowDataPacket(FIELD_COUNT);
            row.add(ds.getName().getBytes());
            row.add(ds.getConfig().getDbType().getBytes());
            if (hb != null) {
                row.add(ds.getConfig().getIp().getBytes());
                row.add(IntegerUtil.toBytes(ds.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 : PhysicalDatasource(io.mycat.backend.datasource.PhysicalDatasource) 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)

Example 8 with PhysicalDatasource

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

the class ShowDataNode method getRow.

private static RowDataPacket getRow(PhysicalDBNode node, String charset) {
    RowDataPacket row = new RowDataPacket(FIELD_COUNT);
    row.add(StringUtil.encode(node.getName(), charset));
    row.add(StringUtil.encode(node.getDbPool().getHostName() + '/' + node.getDatabase(), charset));
    PhysicalDBPool pool = node.getDbPool();
    PhysicalDatasource ds = pool.getSource();
    if (ds != null) {
        int active = ds.getActiveCountForSchema(node.getDatabase());
        int idle = ds.getIdleCountForSchema(node.getDatabase());
        row.add(IntegerUtil.toBytes(pool.getActivedIndex()));
        row.add(StringUtil.encode(ds.getConfig().getDbType(), charset));
        row.add(IntegerUtil.toBytes(active));
        row.add(IntegerUtil.toBytes(idle));
        row.add(IntegerUtil.toBytes(ds.getSize()));
    } else {
        row.add(null);
        row.add(null);
        row.add(null);
        row.add(null);
        row.add(null);
    }
    row.add(LongUtil.toBytes(ds.getExecuteCountForSchema(node.getDatabase())));
    row.add(StringUtil.encode(nf.format(0), charset));
    row.add(StringUtil.encode(nf.format(0), charset));
    row.add(LongUtil.toBytes(0));
    long recoveryTime = pool.getSource().getHeartbeatRecoveryTime() - TimeUtil.currentTimeMillis();
    row.add(LongUtil.toBytes(recoveryTime > 0 ? recoveryTime / 1000L : -1L));
    return row;
}
Also used : PhysicalDatasource(io.mycat.backend.datasource.PhysicalDatasource) RowDataPacket(io.mycat.net.mysql.RowDataPacket) PhysicalDBPool(io.mycat.backend.datasource.PhysicalDBPool)

Example 9 with PhysicalDatasource

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

the class ConfigTest method createDataSource.

private PhysicalDatasource[] createDataSource(DataHostConfig conf, String hostName, String dbType, String dbDriver, DBHostConfig[] nodes, boolean isRead) {
    PhysicalDatasource[] dataSources = new PhysicalDatasource[nodes.length];
    if (dbType.equals("mysql") && dbDriver.equals("native")) {
        for (int i = 0; i < nodes.length; i++) {
            nodes[i].setIdleTimeout(system.getIdleTimeout());
            MySQLDataSource ds = new MySQLDataSource(nodes[i], conf, isRead);
            dataSources[i] = ds;
        }
    } else if (dbDriver.equals("jdbc")) {
        for (int i = 0; i < nodes.length; i++) {
            nodes[i].setIdleTimeout(system.getIdleTimeout());
            JDBCDatasource ds = new JDBCDatasource(nodes[i], conf, isRead);
            dataSources[i] = ds;
        }
    } else {
        throw new ConfigException("not supported yet !" + hostName);
    }
    return dataSources;
}
Also used : PhysicalDatasource(io.mycat.backend.datasource.PhysicalDatasource) MySQLDataSource(io.mycat.backend.mysql.nio.MySQLDataSource) JDBCDatasource(io.mycat.backend.jdbc.JDBCDatasource) ConfigException(io.mycat.config.util.ConfigException)

Example 10 with PhysicalDatasource

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

the class MySQLDetector method onResult.

@Override
public void onResult(SQLQueryResult<Map<String, String>> result) {
    if (result.isSuccess()) {
        int balance = heartbeat.getSource().getDbPool().getBalance();
        PhysicalDatasource source = heartbeat.getSource();
        int switchType = source.getHostConfig().getSwitchType();
        Map<String, String> resultResult = result.getResult();
        if (resultResult != null && !resultResult.isEmpty() && switchType == DataHostConfig.SYN_STATUS_SWITCH_DS && source.getHostConfig().isShowSlaveSql()) {
            String Slave_IO_Running = resultResult != null ? resultResult.get("Slave_IO_Running") : null;
            String Slave_SQL_Running = resultResult != null ? resultResult.get("Slave_SQL_Running") : null;
            if (Slave_IO_Running != null && Slave_IO_Running.equals(Slave_SQL_Running) && Slave_SQL_Running.equals("Yes")) {
                heartbeat.setDbSynStatus(DBHeartbeat.DB_SYN_NORMAL);
                String Seconds_Behind_Master = resultResult.get("Seconds_Behind_Master");
                if (null != Seconds_Behind_Master && !"".equals(Seconds_Behind_Master)) {
                    int Behind_Master = Integer.parseInt(Seconds_Behind_Master);
                    if (Behind_Master > source.getHostConfig().getSlaveThreshold()) {
                        MySQLHeartbeat.LOGGER.warn("found MySQL master/slave Replication delay !!! " + heartbeat.getSource().getConfig() + ", binlog sync time delay: " + Behind_Master + "s");
                    }
                    heartbeat.setSlaveBehindMaster(Behind_Master);
                }
            } else if (source.isSalveOrRead()) {
                // String Last_IO_Error = resultResult != null ? resultResult.get("Last_IO_Error") : null;
                MySQLHeartbeat.LOGGER.warn("found MySQL master/slave Replication err !!! " + heartbeat.getSource().getConfig() + ", " + resultResult);
                heartbeat.setDbSynStatus(DBHeartbeat.DB_SYN_ERROR);
            }
            heartbeat.getAsynRecorder().set(resultResult, switchType);
            heartbeat.setResult(MySQLHeartbeat.OK_STATUS, this, null);
        } else if (resultResult != null && !resultResult.isEmpty() && switchType == DataHostConfig.CLUSTER_STATUS_SWITCH_DS && source.getHostConfig().isShowClusterSql()) {
            // String Variable_name = resultResult != null ? resultResult.get("Variable_name") : null;
            // Primary
            String wsrep_cluster_status = resultResult != null ? resultResult.get("wsrep_cluster_status") : null;
            // ON
            String wsrep_connected = resultResult != null ? resultResult.get("wsrep_connected") : null;
            // ON
            String wsrep_ready = resultResult != null ? resultResult.get("wsrep_ready") : null;
            if ("ON".equals(wsrep_connected) && "ON".equals(wsrep_ready) && "Primary".equals(wsrep_cluster_status)) {
                heartbeat.setDbSynStatus(DBHeartbeat.DB_SYN_NORMAL);
                heartbeat.setResult(MySQLHeartbeat.OK_STATUS, this, null);
            } else {
                MySQLHeartbeat.LOGGER.warn("found MySQL  cluster status err !!! " + heartbeat.getSource().getConfig() + " wsrep_cluster_status: " + wsrep_cluster_status + " wsrep_connected: " + wsrep_connected + " wsrep_ready: " + wsrep_ready);
                heartbeat.setDbSynStatus(DBHeartbeat.DB_SYN_ERROR);
                heartbeat.setResult(MySQLHeartbeat.ERROR_STATUS, this, null);
            }
            heartbeat.getAsynRecorder().set(resultResult, switchType);
        } else {
            heartbeat.setResult(MySQLHeartbeat.OK_STATUS, this, null);
        }
        // 监测数据库同步状态,在 switchType=-1或者1的情况下,也需要收集主从同步状态
        heartbeat.getAsynRecorder().set(resultResult, switchType);
    } else {
        heartbeat.setResult(MySQLHeartbeat.ERROR_STATUS, this, null);
    }
    lasstReveivedQryTime = System.currentTimeMillis();
    heartbeat.getRecorder().set((lasstReveivedQryTime - lastSendQryTime));
}
Also used : PhysicalDatasource(io.mycat.backend.datasource.PhysicalDatasource)

Aggregations

PhysicalDatasource (io.mycat.backend.datasource.PhysicalDatasource)52 PhysicalDBPool (io.mycat.backend.datasource.PhysicalDBPool)43 PhysicalDBNode (io.mycat.backend.datasource.PhysicalDBNode)20 MycatConfig (io.mycat.config.MycatConfig)20 RowDataPacket (io.mycat.net.mysql.RowDataPacket)14 DBHostConfig (io.mycat.config.model.DBHostConfig)12 LinkedList (java.util.LinkedList)12 DBHeartbeat (io.mycat.backend.heartbeat.DBHeartbeat)11 HashMap (java.util.HashMap)10 MySQLDataSource (io.mycat.backend.mysql.nio.MySQLDataSource)8 Connection (java.sql.Connection)8 Map (java.util.Map)8 ConfigException (io.mycat.config.util.ConfigException)6 DataSourceSyncRecorder (io.mycat.statistic.DataSourceSyncRecorder)6 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 MySQLConsistencyChecker (io.mycat.backend.heartbeat.MySQLConsistencyChecker)4 JDBCDatasource (io.mycat.backend.jdbc.JDBCDatasource)4 TableConfig (io.mycat.config.model.TableConfig)4 SimpleDateFormat (java.text.SimpleDateFormat)4