Search in sources :

Example 11 with PhysicalDatasource

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

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

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

the class RouterUtil method getAliveRandomDataNode.

/**
 * 解决getRandomDataNode方法获取错误节点的问题.
 * @param tc
 * @return
 */
private static String getAliveRandomDataNode(TableConfig tc) {
    List<String> randomDns = tc.getDataNodes();
    MycatConfig mycatConfig = MycatServer.getInstance().getConfig();
    if (mycatConfig != null) {
        for (String randomDn : randomDns) {
            PhysicalDBNode physicalDBNode = mycatConfig.getDataNodes().get(randomDn);
            if (physicalDBNode != null) {
                if (physicalDBNode.getDbPool().getSource().isAlive()) {
                    for (PhysicalDBPool pool : MycatServer.getInstance().getConfig().getDataHosts().values()) {
                        PhysicalDatasource source = pool.getSource();
                        if (source.getHostConfig().containDataNode(randomDn) && pool.getSource().isAlive()) {
                            return randomDn;
                        }
                    }
                }
            }
        }
    }
    // all fail return default
    return tc.getRandomDataNode();
}
Also used : PhysicalDBNode(io.mycat.backend.datasource.PhysicalDBNode) PhysicalDatasource(io.mycat.backend.datasource.PhysicalDatasource) PhysicalDBPool(io.mycat.backend.datasource.PhysicalDBPool) MycatConfig(io.mycat.config.MycatConfig)

Example 13 with PhysicalDatasource

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

the class GlobalTableUtil method consistencyCheck.

public static void consistencyCheck() {
    MycatConfig config = MycatServer.getInstance().getConfig();
    for (String key : globalTableMap.keySet()) {
        TableConfig table = globalTableMap.get(key);
        // <table name="travelrecord" dataNode="dn1,dn2,dn3"
        List<String> dataNodeList = table.getDataNodes();
        // 记录本次已经执行的datanode
        // 多个 datanode 对应到同一个 PhysicalDatasource 只执行一次
        Map<String, String> executedMap = new HashMap<>();
        for (String nodeName : dataNodeList) {
            Map<String, PhysicalDBNode> map = config.getDataNodes();
            for (String k2 : map.keySet()) {
                // <dataNode name="dn1" dataHost="localhost1" database="db1" />
                PhysicalDBNode dBnode = map.get(k2);
                if (nodeName.equals(dBnode.getName())) {
                    // dn1,dn2,dn3
                    PhysicalDBPool pool = dBnode.getDbPool();
                    Collection<PhysicalDatasource> allDS = pool.genAllDataSources();
                    for (PhysicalDatasource pds : allDS) {
                        if (pds instanceof MySQLDataSource) {
                            MySQLDataSource mds = (MySQLDataSource) pds;
                            if (executedMap.get(pds.getName()) == null) {
                                MySQLConsistencyChecker checker = new MySQLConsistencyChecker(mds, table.getName());
                                isInnerColumnCheckFinished = 0;
                                checker.checkInnerColumnExist();
                                while (isInnerColumnCheckFinished <= 0) {
                                    LOGGER.debug("isInnerColumnCheckFinished:" + isInnerColumnCheckFinished);
                                    try {
                                        TimeUnit.SECONDS.sleep(1);
                                    } catch (InterruptedException e) {
                                        LOGGER.warn(e.getMessage());
                                    }
                                }
                                LOGGER.debug("isInnerColumnCheckFinished:" + isInnerColumnCheckFinished);
                                // 一种 check 完成之后,再进行另一种 check
                                checker = new MySQLConsistencyChecker(mds, table.getName());
                                isColumnCountCheckFinished = 0;
                                checker.checkRecordCout();
                                while (isColumnCountCheckFinished <= 0) {
                                    LOGGER.debug("isColumnCountCheckFinished:" + isColumnCountCheckFinished);
                                    try {
                                        TimeUnit.SECONDS.sleep(1);
                                    } catch (InterruptedException e) {
                                        LOGGER.warn(e.getMessage());
                                    }
                                }
                                LOGGER.debug("isColumnCountCheckFinished:" + isColumnCountCheckFinished);
                                checker = new MySQLConsistencyChecker(mds, table.getName());
                                checker.checkMaxTimeStamp();
                                executedMap.put(pds.getName(), nodeName);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : PhysicalDBNode(io.mycat.backend.datasource.PhysicalDBNode) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PhysicalDBPool(io.mycat.backend.datasource.PhysicalDBPool) MycatConfig(io.mycat.config.MycatConfig) MySQLConsistencyChecker(io.mycat.backend.heartbeat.MySQLConsistencyChecker) PhysicalDatasource(io.mycat.backend.datasource.PhysicalDatasource) MySQLDataSource(io.mycat.backend.mysql.nio.MySQLDataSource) TableConfig(io.mycat.config.model.TableConfig)

Example 14 with PhysicalDatasource

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

the class ConfigInitializer 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++) {
            // 设置最大idle时间,默认为30分钟
            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 if ("postgresql".equalsIgnoreCase(dbType) && dbDriver.equalsIgnoreCase("native")) {
        for (int i = 0; i < nodes.length; i++) {
            nodes[i].setIdleTimeout(system.getIdleTimeout());
            PostgreSQLDataSource ds = new PostgreSQLDataSource(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) PostgreSQLDataSource(io.mycat.backend.postgresql.PostgreSQLDataSource) MySQLDataSource(io.mycat.backend.mysql.nio.MySQLDataSource) JDBCDatasource(io.mycat.backend.jdbc.JDBCDatasource) ConfigException(io.mycat.config.util.ConfigException)

Example 15 with PhysicalDatasource

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

the class ConfigInitializer method testConnection.

public void testConnection() {
    // 实际链路的连接测试
    if (this.dataNodes != null && this.dataHosts != null) {
        Map<String, Boolean> map = new HashMap<String, Boolean>();
        for (PhysicalDBNode dataNode : dataNodes.values()) {
            String database = dataNode.getDatabase();
            PhysicalDBPool pool = dataNode.getDbPool();
            for (PhysicalDatasource ds : pool.getAllDataSources()) {
                String key = ds.getName() + "_" + database;
                if (map.get(key) == null) {
                    map.put(key, false);
                    boolean isConnected = false;
                    try {
                        isConnected = ds.testConnection(database);
                        map.put(key, isConnected);
                    } catch (IOException e) {
                        LOGGER.warn("test conn error:", e);
                    }
                }
            }
        }
        // 
        boolean isConnectivity = true;
        for (Map.Entry<String, Boolean> entry : map.entrySet()) {
            String key = entry.getKey();
            Boolean value = entry.getValue();
            if (!value && isConnectivity) {
                LOGGER.warn("SelfCheck### test " + key + " database connection failed ");
                isConnectivity = false;
            } else {
                LOGGER.info("SelfCheck### test " + key + " database connection success ");
            }
        }
        if (!isConnectivity) {
            throw new ConfigException("SelfCheck### there are some datasource connection failed, pls check!");
        }
    }
}
Also used : PhysicalDBNode(io.mycat.backend.datasource.PhysicalDBNode) PhysicalDatasource(io.mycat.backend.datasource.PhysicalDatasource) HashMap(java.util.HashMap) PhysicalDBPool(io.mycat.backend.datasource.PhysicalDBPool) ConfigException(io.mycat.config.util.ConfigException) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map)

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