Search in sources :

Example 21 with PhysicalDBNode

use of io.mycat.backend.datasource.PhysicalDBNode 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 22 with PhysicalDBNode

use of io.mycat.backend.datasource.PhysicalDBNode 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)

Example 23 with PhysicalDBNode

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

the class HintDataNodeHandler method route.

@Override
public RouteResultset route(SystemConfig sysConfig, SchemaConfig schema, int sqlType, String realSQL, String charset, ServerConnection sc, LayerCachePool cachePool, String hintSQLValue, int hintSqlType, Map hintMap) throws SQLNonTransientException {
    String stmt = realSQL;
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("route datanode sql hint from " + stmt);
    }
    RouteResultset rrs = new RouteResultset(stmt, sqlType);
    PhysicalDBNode dataNode = MycatServer.getInstance().getConfig().getDataNodes().get(hintSQLValue);
    if (dataNode != null) {
        rrs = RouterUtil.routeToSingleNode(rrs, dataNode.getName(), stmt);
    } else {
        String msg = "can't find hint datanode:" + hintSQLValue;
        LOGGER.warn(msg);
        throw new SQLNonTransientException(msg);
    }
    return rrs;
}
Also used : PhysicalDBNode(io.mycat.backend.datasource.PhysicalDBNode) SQLNonTransientException(java.sql.SQLNonTransientException) RouteResultset(io.mycat.route.RouteResultset)

Example 24 with PhysicalDBNode

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

the class RouterUtil method getRandomDataNode.

private static String getRandomDataNode(TableConfig tc) {
    //写节点不可用,意味着读节点也不可用。
    //直接使用下一个 dataHost
    String randomDn = tc.getRandomDataNode();
    MycatConfig mycatConfig = MycatServer.getInstance().getConfig();
    if (mycatConfig != null) {
        PhysicalDBNode physicalDBNode = mycatConfig.getDataNodes().get(randomDn);
        if (physicalDBNode != null) {
            if (physicalDBNode.getDbPool().getSource().isAlive()) {
                for (PhysicalDBPool pool : MycatServer.getInstance().getConfig().getDataHosts().values()) {
                    if (pool.getSource().getHostConfig().containDataNode(randomDn)) {
                        continue;
                    }
                    if (pool.getSource().isAlive()) {
                        return pool.getSource().getHostConfig().getRandomDataNode();
                    }
                }
            }
        }
    }
    //all fail return default
    return randomDn;
}
Also used : PhysicalDBNode(io.mycat.backend.datasource.PhysicalDBNode) PhysicalDBPool(io.mycat.backend.datasource.PhysicalDBPool) MycatConfig(io.mycat.config.MycatConfig)

Example 25 with PhysicalDBNode

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

the class MigrateHandler method getSlaveIdFromZKForDataNode.

private static int getSlaveIdFromZKForDataNode(String dataNode) {
    PhysicalDBNode dbNode = MycatServer.getInstance().getConfig().getDataNodes().get(dataNode);
    String slaveIDs = dbNode.getDbPool().getSlaveIDs();
    if (Strings.isNullOrEmpty(slaveIDs))
        throw new RuntimeException("dataHost:" + dbNode.getDbPool().getHostName() + " do not config the salveIDs field");
    List<Integer> allSlaveIDList = parseSlaveIDs(slaveIDs);
    String taskPath = ZKUtils.getZKBasePath() + "slaveIDs/" + dbNode.getDbPool().getHostName();
    try {
        slaveIDsLock.acquire(30, TimeUnit.SECONDS);
        Set<Integer> zkSlaveIdsSet = new HashSet<>();
        if (ZKUtils.getConnection().checkExists().forPath(taskPath) != null) {
            List<String> zkHasSlaveIDs = ZKUtils.getConnection().getChildren().forPath(taskPath);
            for (String zkHasSlaveID : zkHasSlaveIDs) {
                zkSlaveIdsSet.add(Integer.parseInt(zkHasSlaveID));
            }
        }
        for (Integer integer : allSlaveIDList) {
            if (!zkSlaveIdsSet.contains(integer)) {
                ZKUtils.getConnection().create().creatingParentsIfNeeded().forPath(taskPath + "/" + integer);
                return integer;
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        try {
            slaveIDsLock.release();
        } catch (Exception e) {
            LOGGER.error("error:", e);
        }
    }
    throw new RuntimeException("cannot get the slaveID  for dataHost :" + dbNode.getDbPool().getHostName());
}
Also used : PhysicalDBNode(io.mycat.backend.datasource.PhysicalDBNode)

Aggregations

PhysicalDBNode (io.mycat.backend.datasource.PhysicalDBNode)27 MycatConfig (io.mycat.config.MycatConfig)15 PhysicalDBPool (io.mycat.backend.datasource.PhysicalDBPool)13 PhysicalDatasource (io.mycat.backend.datasource.PhysicalDatasource)9 RouteResultsetNode (io.mycat.route.RouteResultsetNode)7 BackendConnection (io.mycat.backend.BackendConnection)6 SchemaConfig (io.mycat.config.model.SchemaConfig)6 DBHostConfig (io.mycat.config.model.DBHostConfig)4 TableConfig (io.mycat.config.model.TableConfig)4 IOException (java.io.IOException)4 Connection (java.sql.Connection)4 HashMap (java.util.HashMap)4 MycatCluster (io.mycat.config.MycatCluster)3 FirewallConfig (io.mycat.config.model.FirewallConfig)3 UserConfig (io.mycat.config.model.UserConfig)3 ConfigException (io.mycat.config.util.ConfigException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 MySQLConsistencyChecker (io.mycat.backend.heartbeat.MySQLConsistencyChecker)2 MySQLDataSource (io.mycat.backend.mysql.nio.MySQLDataSource)2