Search in sources :

Example 41 with PhysicalDatasource

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

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 42 with PhysicalDatasource

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

the class MySQLHeartbeat method switchSourceIfNeed.

/**
 * switch data source
 */
private void switchSourceIfNeed(String reason) {
    int switchType = source.getHostConfig().getSwitchType();
    if (switchType == DataHostConfig.NOT_SWITCH_DS) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("not switch datasource ,for switchType is " + DataHostConfig.NOT_SWITCH_DS);
            return;
        }
        return;
    }
    PhysicalDBPool pool = this.source.getDbPool();
    int curDatasourceHB = pool.getSource().getHeartbeat().getStatus();
    // read node can't switch ,only write node can switch
    if (pool.getWriteType() == PhysicalDBPool.WRITE_ONLYONE_NODE && !source.isReadNode() && curDatasourceHB != DBHeartbeat.OK_STATUS && pool.getSources().length > 1) {
        synchronized (pool) {
            // try to see if need switch datasource
            curDatasourceHB = pool.getSource().getHeartbeat().getStatus();
            if (curDatasourceHB != DBHeartbeat.INIT_STATUS && curDatasourceHB != DBHeartbeat.OK_STATUS) {
                int curIndex = pool.getActivedIndex();
                int nextId = pool.next(curIndex);
                PhysicalDatasource[] allWriteNodes = pool.getSources();
                while (true) {
                    if (nextId == curIndex) {
                        break;
                    }
                    PhysicalDatasource theSource = allWriteNodes[nextId];
                    DBHeartbeat theSourceHB = theSource.getHeartbeat();
                    int theSourceHBStatus = theSourceHB.getStatus();
                    if (theSourceHBStatus == DBHeartbeat.OK_STATUS) {
                        if (switchType == DataHostConfig.SYN_STATUS_SWITCH_DS) {
                            if (Integer.valueOf(0).equals(theSourceHB.getSlaveBehindMaster())) {
                                LOGGER.info("try to switch datasource ,slave is synchronized to master " + theSource.getConfig());
                                pool.switchSource(nextId, true, reason);
                                break;
                            } else {
                                LOGGER.warn("ignored  datasource ,slave is not  synchronized to master , slave behind master :" + theSourceHB.getSlaveBehindMaster() + " " + theSource.getConfig());
                            }
                        } else {
                            // normal switch
                            LOGGER.info("try to switch datasource ,not checked slave synchronize status " + theSource.getConfig());
                            pool.switchSource(nextId, true, reason);
                            break;
                        }
                    }
                    nextId = pool.next(nextId);
                }
            }
        }
    }
}
Also used : PhysicalDatasource(io.mycat.backend.datasource.PhysicalDatasource) PhysicalDBPool(io.mycat.backend.datasource.PhysicalDBPool)

Example 43 with PhysicalDatasource

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

the class ConfigTest method testReadHostWeight.

/**
 * 测试 读服务的 权重
 *
 * @throws Exception
 */
@Test
public void testReadHostWeight() throws Exception {
    ArrayList<PhysicalDatasource> okSources = new ArrayList<PhysicalDatasource>();
    PhysicalDBPool pool = this.dataHosts.get("localhost2");
    okSources.addAll(pool.getAllDataSources());
    PhysicalDatasource source = pool.randomSelect(okSources);
    Assert.assertTrue(source != null);
}
Also used : PhysicalDatasource(io.mycat.backend.datasource.PhysicalDatasource) ArrayList(java.util.ArrayList) PhysicalDBPool(io.mycat.backend.datasource.PhysicalDBPool) Test(org.junit.Test)

Example 44 with PhysicalDatasource

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

the class MigrateUtils method execulteCount.

public static long execulteCount(String sql, String toDn) throws SQLException, IOException {
    PhysicalDBNode dbNode = MycatServer.getInstance().getConfig().getDataNodes().get(toDn);
    PhysicalDBPool dbPool = dbNode.getDbPool();
    PhysicalDatasource datasource = dbPool.getSources()[dbPool.getActivedIndex()];
    DBHostConfig config = datasource.getConfig();
    Connection con = null;
    try {
        con = DriverManager.getConnection("jdbc:mysql://" + config.getUrl() + "/" + dbNode.getDatabase(), config.getUser(), config.getPassword());
        List<Map<String, Object>> result = JdbcUtils.executeQuery(con, sql, new ArrayList<>());
        if (result.size() == 1) {
            return (long) result.get(0).get("count");
        }
    } finally {
        JdbcUtils.close(con);
    }
    return 0;
}
Also used : PhysicalDBNode(io.mycat.backend.datasource.PhysicalDBNode) DBHostConfig(io.mycat.config.model.DBHostConfig) PhysicalDatasource(io.mycat.backend.datasource.PhysicalDatasource) Connection(java.sql.Connection) PhysicalDBPool(io.mycat.backend.datasource.PhysicalDBPool)

Example 45 with PhysicalDatasource

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

the class MigrateUtils method execulteSql.

public static void execulteSql(String sql, String toDn) throws SQLException, IOException {
    PhysicalDBNode dbNode = MycatServer.getInstance().getConfig().getDataNodes().get(toDn);
    PhysicalDBPool dbPool = dbNode.getDbPool();
    PhysicalDatasource datasource = dbPool.getSources()[dbPool.getActivedIndex()];
    DBHostConfig config = datasource.getConfig();
    Connection con = null;
    try {
        con = DriverManager.getConnection("jdbc:mysql://" + config.getUrl() + "/" + dbNode.getDatabase(), config.getUser(), config.getPassword());
        JdbcUtils.execute(con, sql, new ArrayList<>());
    } finally {
        JdbcUtils.close(con);
    }
}
Also used : PhysicalDBNode(io.mycat.backend.datasource.PhysicalDBNode) DBHostConfig(io.mycat.config.model.DBHostConfig) PhysicalDatasource(io.mycat.backend.datasource.PhysicalDatasource) Connection(java.sql.Connection) PhysicalDBPool(io.mycat.backend.datasource.PhysicalDBPool)

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