Search in sources :

Example 11 with PhysicalDBNode

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

the class GlobalTableUtil method reGetColumnsForTable.

/**
	 * 重新获得table 的列list
	 * @param tableName
	 */
private static void reGetColumnsForTable(String tableName) {
    MycatConfig config = MycatServer.getInstance().getConfig();
    if (globalTableMap != null && globalTableMap.get(tableName.toUpperCase()) != null) {
        TableConfig tableConfig = globalTableMap.get(tableName.toUpperCase());
        if (// consistencyCheck 在运行中
        tableConfig == null || isInnerColumnCheckFinished != 1)
            return;
        String nodeName = tableConfig.getDataNodes().get(0);
        Map<String, PhysicalDBNode> map = config.getDataNodes();
        for (String k2 : map.keySet()) {
            PhysicalDBNode dBnode = map.get(k2);
            if (nodeName.equals(dBnode.getName())) {
                PhysicalDBPool pool = dBnode.getDbPool();
                List<PhysicalDatasource> dsList = (List<PhysicalDatasource>) pool.genAllDataSources();
                for (PhysicalDatasource ds : dsList) {
                    if (ds instanceof MySQLDataSource) {
                        MySQLDataSource mds = (MySQLDataSource) dsList.get(0);
                        MySQLConsistencyChecker checker = new MySQLConsistencyChecker(mds, tableConfig.getName());
                        checker.checkInnerColumnExist();
                        // 运行一次就行了,不需要像consistencyCheck那样每个db都运行一次
                        return;
                    }
                }
            }
        }
    }
}
Also used : PhysicalDBNode(io.mycat.backend.datasource.PhysicalDBNode) 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) PhysicalDBPool(io.mycat.backend.datasource.PhysicalDBPool) ArrayList(java.util.ArrayList) List(java.util.List) MycatConfig(io.mycat.config.MycatConfig)

Example 12 with PhysicalDBNode

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

the class FetchStoreNodeOfChildTableHandler method execute.

public String execute(String schema, String sql, ArrayList<String> dataNodes) {
    String key = schema + ":" + sql;
    CachePool cache = MycatServer.getInstance().getCacheService().getCachePool("ER_SQL2PARENTID");
    String result = (String) cache.get(key);
    if (result != null) {
        return result;
    }
    this.sql = sql;
    int totalCount = dataNodes.size();
    long startTime = System.currentTimeMillis();
    long endTime = startTime + 5 * 60 * 1000L;
    MycatConfig conf = MycatServer.getInstance().getConfig();
    LOGGER.debug("find child node with sql:" + sql);
    for (String dn : dataNodes) {
        if (dataNode != null) {
            return dataNode;
        }
        PhysicalDBNode mysqlDN = conf.getDataNodes().get(dn);
        try {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("execute in datanode " + dn);
            }
            RouteResultsetNode node = new RouteResultsetNode(dn, ServerParse.SELECT, sql);
            // 获取 子表节点,最好走master为好
            node.setRunOnSlave(false);
            mysqlDN.getConnection(mysqlDN.getDatabase(), true, node, this, node);
        //				mysqlDN.getConnection(mysqlDN.getDatabase(), true,
        //						new RouteResultsetNode(dn, ServerParse.SELECT, sql),
        //						this, dn);
        } catch (Exception e) {
            LOGGER.warn("get connection err " + e);
        }
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
        }
    }
    while (dataNode == null && System.currentTimeMillis() < endTime) {
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            break;
        }
        if (dataNode != null || finished.get() >= totalCount) {
            break;
        }
    }
    if (dataNode != null) {
        cache.putIfAbsent(key, dataNode);
    }
    return dataNode;
}
Also used : PhysicalDBNode(io.mycat.backend.datasource.PhysicalDBNode) RouteResultsetNode(io.mycat.route.RouteResultsetNode) MycatConfig(io.mycat.config.MycatConfig) IOException(java.io.IOException) CachePool(io.mycat.cache.CachePool)

Example 13 with PhysicalDBNode

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

the class MycatServer method performXARecoveryLog.

//XA recovery log check
private void performXARecoveryLog() {
    //fetch the recovery log
    CoordinatorLogEntry[] coordinatorLogEntries = getCoordinatorLogEntries();
    for (int i = 0; i < coordinatorLogEntries.length; i++) {
        CoordinatorLogEntry coordinatorLogEntry = coordinatorLogEntries[i];
        boolean needRollback = false;
        for (int j = 0; j < coordinatorLogEntry.participants.length; j++) {
            ParticipantLogEntry participantLogEntry = coordinatorLogEntry.participants[j];
            if (participantLogEntry.txState == TxState.TX_PREPARED_STATE) {
                needRollback = true;
                break;
            }
        }
        if (needRollback) {
            for (int j = 0; j < coordinatorLogEntry.participants.length; j++) {
                ParticipantLogEntry participantLogEntry = coordinatorLogEntry.participants[j];
                //XA rollback
                String xacmd = "XA ROLLBACK " + coordinatorLogEntry.id + ';';
                OneRawSQLQueryResultHandler resultHandler = new OneRawSQLQueryResultHandler(new String[0], new XARollbackCallback());
                outloop: for (SchemaConfig schema : MycatServer.getInstance().getConfig().getSchemas().values()) {
                    for (TableConfig table : schema.getTables().values()) {
                        for (String dataNode : table.getDataNodes()) {
                            PhysicalDBNode dn = MycatServer.getInstance().getConfig().getDataNodes().get(dataNode);
                            if (dn.getDbPool().getSource().getConfig().getIp().equals(participantLogEntry.uri) && dn.getDatabase().equals(participantLogEntry.resourceName)) {
                                //XA STATE ROLLBACK
                                participantLogEntry.txState = TxState.TX_ROLLBACKED_STATE;
                                SQLJob sqlJob = new SQLJob(xacmd, dn.getDatabase(), resultHandler, dn.getDbPool().getSource());
                                sqlJob.run();
                                LOGGER.debug(String.format("[XA ROLLBACK] [%s] Host:[%s] schema:[%s]", xacmd, dn.getName(), dn.getDatabase()));
                                break outloop;
                            }
                        }
                    }
                }
            }
        }
    }
    //init into in memory cached
    for (int i = 0; i < coordinatorLogEntries.length; i++) {
        MultiNodeCoordinator.inMemoryRepository.put(coordinatorLogEntries[i].id, coordinatorLogEntries[i]);
    }
    //discard the recovery log
    MultiNodeCoordinator.fileRepository.writeCheckpoint(MultiNodeCoordinator.inMemoryRepository.getAllCoordinatorLogEntries());
}
Also used : OneRawSQLQueryResultHandler(io.mycat.sqlengine.OneRawSQLQueryResultHandler) PhysicalDBNode(io.mycat.backend.datasource.PhysicalDBNode) SchemaConfig(io.mycat.config.model.SchemaConfig) ParticipantLogEntry(io.mycat.backend.mysql.xa.ParticipantLogEntry) SQLJob(io.mycat.sqlengine.SQLJob) XARollbackCallback(io.mycat.backend.mysql.xa.XARollbackCallback) TableConfig(io.mycat.config.model.TableConfig) CoordinatorLogEntry(io.mycat.backend.mysql.xa.CoordinatorLogEntry)

Example 14 with PhysicalDBNode

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

Example 15 with PhysicalDBNode

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

the class ClearSlow method dataNode.

public static void dataNode(ManagerConnection c, String name) {
    PhysicalDBNode dn = MycatServer.getInstance().getConfig().getDataNodes().get(name);
    PhysicalDBPool ds = null;
    if (dn != null && ((ds = dn.getDbPool()) != null)) {
        // ds.getSqlRecorder().clear();
        c.write(c.writeToBuffer(OkPacket.OK, c.allocate()));
    } else {
        c.writeErrMessage(ErrorCode.ER_YES, "Invalid DataNode:" + name);
    }
}
Also used : PhysicalDBNode(io.mycat.backend.datasource.PhysicalDBNode) PhysicalDBPool(io.mycat.backend.datasource.PhysicalDBPool)

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