Search in sources :

Example 11 with DBHeartbeat

use of io.mycat.backend.heartbeat.DBHeartbeat in project Mycat-Server by MyCATApache.

the class PostgreSQLHeartbeat method switchSourceIfNeed.

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) DBHeartbeat(io.mycat.backend.heartbeat.DBHeartbeat) PhysicalDBPool(io.mycat.backend.datasource.PhysicalDBPool)

Aggregations

PhysicalDBPool (io.mycat.backend.datasource.PhysicalDBPool)11 PhysicalDatasource (io.mycat.backend.datasource.PhysicalDatasource)11 DBHeartbeat (io.mycat.backend.heartbeat.DBHeartbeat)11 MycatConfig (io.mycat.config.MycatConfig)10 RowDataPacket (io.mycat.net.mysql.RowDataPacket)10 LinkedList (java.util.LinkedList)10 DataSourceSyncRecorder (io.mycat.statistic.DataSourceSyncRecorder)6 SimpleDateFormat (java.text.SimpleDateFormat)4 Date (java.util.Date)4 Record (io.mycat.statistic.DataSourceSyncRecorder.Record)2 HeartbeatRecorder (io.mycat.statistic.HeartbeatRecorder)2