Search in sources :

Example 1 with PhysicalDBPool

use of com.actiontech.dble.backend.datasource.PhysicalDBPool in project dble by actiontech.

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();
    pool.switchSourceIfNeed(this.source, reason);
/*
        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.getActiveIndex();
                    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 : PhysicalDBPool(com.actiontech.dble.backend.datasource.PhysicalDBPool)

Example 2 with PhysicalDBPool

use of com.actiontech.dble.backend.datasource.PhysicalDBPool in project dble by actiontech.

the class ServerConfig method addedDatasource.

private boolean addedDatasource(Map<String, PhysicalDBPool> newDataHosts, DsDiff diff) {
    for (PhysicalDBPool npool : newDataHosts.values()) {
        PhysicalDBPool opool = dataHosts.get(npool.getHostName());
        if (opool == null) {
            LOGGER.warn(AlarmCode.CORE_GENERAL_WARN + "reload -add- failed, use old datasources ");
            return true;
        }
        Map<Integer, PhysicalDatasource[]> ndss = npool.getReadSources();
        Map<Integer, PhysicalDatasource[]> odss = opool.getReadSources();
        Map<Integer, ArrayList<PhysicalDatasource>> iadd = new HashMap<>(2);
        boolean haveOne = false;
        for (Map.Entry<Integer, PhysicalDatasource[]> nentry : ndss.entrySet()) {
            boolean doadd = false;
            ArrayList<PhysicalDatasource> add = new ArrayList<>();
            for (PhysicalDatasource nds : nentry.getValue()) {
                boolean isExist = false;
                for (Map.Entry<Integer, PhysicalDatasource[]> oentry : odss.entrySet()) {
                    for (PhysicalDatasource ods : oentry.getValue()) {
                        if (nds.getName().equals(ods.getName())) {
                            isExist = true;
                            break;
                        }
                    }
                    if (isExist) {
                        break;
                    }
                }
                if (!isExist) {
                    add.add(nds);
                    doadd = true;
                }
            }
            if (doadd) {
                iadd.put(nentry.getKey(), add);
                haveOne = true;
            }
        }
        if (haveOne) {
            diff.added.put(opool, iadd);
        }
    }
    return false;
}
Also used : PhysicalDatasource(com.actiontech.dble.backend.datasource.PhysicalDatasource) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PhysicalDBPool(com.actiontech.dble.backend.datasource.PhysicalDBPool) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with PhysicalDBPool

use of com.actiontech.dble.backend.datasource.PhysicalDBPool in project dble by actiontech.

the class ServerConfig method dsdiff.

private DsDiff dsdiff(Map<String, PhysicalDBPool> newDataHosts) {
    DsDiff diff = new DsDiff();
    // deleted datasource
    for (PhysicalDBPool opool : dataHosts.values()) {
        PhysicalDBPool npool = newDataHosts.get(opool.getHostName());
        if (npool == null) {
            LOGGER.info("reload -delete- failed, use old datasources ");
            return null;
        }
        Map<Integer, PhysicalDatasource[]> odss = opool.getReadSources();
        Map<Integer, PhysicalDatasource[]> ndss = npool.getReadSources();
        Map<Integer, ArrayList<PhysicalDatasource>> idel = new HashMap<>(2);
        boolean haveOne = false;
        for (Map.Entry<Integer, PhysicalDatasource[]> oentry : odss.entrySet()) {
            boolean doadd = false;
            ArrayList<PhysicalDatasource> del = new ArrayList<>();
            for (PhysicalDatasource ods : oentry.getValue()) {
                boolean dodel = true;
                for (Map.Entry<Integer, PhysicalDatasource[]> nentry : ndss.entrySet()) {
                    for (PhysicalDatasource nds : nentry.getValue()) {
                        if (ods.getName().equals(nds.getName())) {
                            dodel = false;
                            break;
                        }
                    }
                    if (!dodel) {
                        break;
                    }
                }
                if (dodel) {
                    del.add(ods);
                    doadd = true;
                }
            }
            if (doadd) {
                idel.put(oentry.getKey(), del);
                haveOne = true;
            }
        }
        if (haveOne) {
            diff.deled.put(opool, idel);
        }
    }
    // added datasource
    if (addedDatasource(newDataHosts, diff))
        return null;
    return diff;
}
Also used : PhysicalDatasource(com.actiontech.dble.backend.datasource.PhysicalDatasource) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PhysicalDBPool(com.actiontech.dble.backend.datasource.PhysicalDBPool) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with PhysicalDBPool

use of com.actiontech.dble.backend.datasource.PhysicalDBPool in project dble by actiontech.

the class ServerConfig method apply.

private void apply(Map<String, UserConfig> newUsers, Map<String, SchemaConfig> newSchemas, Map<String, PhysicalDBNode> newDataNodes, Map<String, PhysicalDBPool> newDataHosts, Map<ERTable, Set<ERTable>> newErRelations, FirewallConfig newFirewall, SystemVariables newSystemVariables, boolean newDataHostWithoutWR, boolean isLoadAll) {
    this.changing = true;
    try {
        // --------------------------------------------
        if (isLoadAll) {
            Map<String, PhysicalDBPool> oldDataHosts = this.dataHosts;
            if (oldDataHosts != null) {
                for (PhysicalDBPool oldDbPool : oldDataHosts.values()) {
                    if (oldDbPool != null) {
                        oldDbPool.stopHeartbeat();
                    }
                }
            }
            this.dataNodes2 = this.dataNodes;
            this.dataHosts2 = this.dataHosts;
        }
        this.users2 = this.users;
        this.schemas2 = this.schemas;
        this.firewall2 = this.firewall;
        this.erRelations2 = this.erRelations;
        this.dataHostWithoutWR2 = this.dataHostWithoutWR;
        // ---------------------------------------------------
        if (isLoadAll) {
            if (newDataHosts != null) {
                for (PhysicalDBPool newDbPool : newDataHosts.values()) {
                    if (newDbPool != null && !newDataHostWithoutWR) {
                        DbleServer.getInstance().saveDataHostIndex(newDbPool.getHostName(), newDbPool.getActiveIndex());
                        newDbPool.startHeartbeat();
                    }
                }
            }
            this.dataNodes = newDataNodes;
            this.dataHosts = newDataHosts;
            this.dataHostWithoutWR = newDataHostWithoutWR;
            DbleServer.getInstance().reloadSystemVariables(newSystemVariables);
            DbleServer.getInstance().getCacheService().reloadCache(newSystemVariables.isLowerCaseTableNames());
            DbleServer.getInstance().getRouterService().loadTableId2DataNodeCache(DbleServer.getInstance().getCacheService());
        }
        this.users = newUsers;
        this.schemas = newSchemas;
        this.firewall = newFirewall;
        this.erRelations = newErRelations;
        DbleServer.getInstance().getCacheService().clearCache();
        if (!newDataHostWithoutWR) {
            DbleServer.getInstance().setMetaChanging(true);
        }
    } finally {
        this.changing = false;
    }
    if (!newDataHostWithoutWR) {
        try {
            DbleServer.getInstance().reloadMetaData(this);
        } finally {
            DbleServer.getInstance().setMetaChanging(false);
        }
    }
}
Also used : PhysicalDBPool(com.actiontech.dble.backend.datasource.PhysicalDBPool)

Example 5 with PhysicalDBPool

use of com.actiontech.dble.backend.datasource.PhysicalDBPool in project dble by actiontech.

the class ConfigInitializer method initDataNodes.

private Map<String, PhysicalDBNode> initDataNodes(SchemaLoader schemaLoader) {
    Map<String, DataNodeConfig> nodeConf = schemaLoader.getDataNodes();
    Map<String, PhysicalDBNode> nodes = new HashMap<>(nodeConf.size());
    for (DataNodeConfig conf : nodeConf.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(com.actiontech.dble.backend.datasource.PhysicalDBNode) PhysicalDBPool(com.actiontech.dble.backend.datasource.PhysicalDBPool) ConfigException(com.actiontech.dble.config.util.ConfigException)

Aggregations

PhysicalDBPool (com.actiontech.dble.backend.datasource.PhysicalDBPool)31 PhysicalDatasource (com.actiontech.dble.backend.datasource.PhysicalDatasource)16 ServerConfig (com.actiontech.dble.config.ServerConfig)9 PhysicalDBNode (com.actiontech.dble.backend.datasource.PhysicalDBNode)8 RowDataPacket (com.actiontech.dble.net.mysql.RowDataPacket)7 DBHeartbeat (com.actiontech.dble.backend.heartbeat.DBHeartbeat)5 Map (java.util.Map)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 LinkedList (java.util.LinkedList)4 ERTable (com.actiontech.dble.config.model.ERTable)3 FirewallConfig (com.actiontech.dble.config.model.FirewallConfig)3 SchemaConfig (com.actiontech.dble.config.model.SchemaConfig)3 UserConfig (com.actiontech.dble.config.model.UserConfig)3 DataSourceSyncRecorder (com.actiontech.dble.statistic.DataSourceSyncRecorder)3 Set (java.util.Set)3 ConfigInitializer (com.actiontech.dble.config.ConfigInitializer)2 ConfigException (com.actiontech.dble.config.util.ConfigException)2 OkPacket (com.actiontech.dble.net.mysql.OkPacket)2 OneRawSQLQueryResultHandler (com.actiontech.dble.sqlengine.OneRawSQLQueryResultHandler)2