Search in sources :

Example 1 with ForceSwitchedStatus

use of com.ctrip.platform.dal.common.enums.ForceSwitchedStatus in project dal by ctripcorp.

the class ClusterDynamicDataSource method forceSwitch.

@Override
public SwitchableDataSourceStatus forceSwitch(FirstAidKit configure, final String ip, final Integer port) {
    synchronized (lock) {
        SwitchableDataSourceStatus currentStatus = getStatus();
        ForceSwitchedStatus prevStatus = status.getAndSet(ForceSwitchedStatus.ForceSwitching);
        try {
            String logName = String.format(FORCE_SWITCH, clusterInfo.toString());
            LOGGER.logTransaction(DalLogTypes.DAL_CONFIGURE, logName, String.format("newIp: %s, newPort: %s", ip, port), () -> {
                LOGGER.logEvent(DalLogTypes.DAL_CONFIGURE, logName, String.format("old isForceSwitched before force switch: %s, old poolCreated before force switch: %s", currentStatus.isForceSwitched(), currentStatus.isPoolCreated()));
                getExecutor().submit(() -> {
                    try {
                        DataSource newDataSource;
                        if (DatabaseCategory.CUSTOM == cluster.getDatabaseCategory()) {
                            newDataSource = createCustomDataSource();
                        } else {
                            DataSourceConfigure dataSourceConfig = getSingleDataSource().getDataSourceConfigure().clone();
                            LOGGER.logEvent(DalLogTypes.DAL_CONFIGURE, logName, String.format("previous host(s): %s:%s", currentStatus.getHostName(), currentStatus.getPort()));
                            dataSourceConfig.replaceURL(ip, port);
                            LOGGER.logEvent(DalLogTypes.DAL_CONFIGURE, logName, String.format("new host(s): %s:%s", ip, port));
                            newDataSource = new RefreshableDataSource(dataSourceId, dataSourceConfig);
                        }
                        switchDataSource(newDataSource);
                        status.set(ForceSwitchedStatus.ForceSwitched);
                        currentHost.set(new HostAndPort(null, ip, port));
                    } catch (Throwable t) {
                        LOGGER.error("DataSource creation failed", t);
                        // TODO: handle pool creation failure
                        status.set(prevStatus);
                    }
                });
            });
            return currentStatus;
        } catch (Throwable t) {
            status.set(prevStatus);
            LOGGER.error("Force switch error", t);
            throw new DalRuntimeException("Force switch error", t);
        }
    }
}
Also used : ForceSwitchedStatus(com.ctrip.platform.dal.common.enums.ForceSwitchedStatus) DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) ConnectionString(com.ctrip.framework.dal.cluster.client.database.ConnectionString) ClusterDataSource(com.ctrip.platform.dal.dao.datasource.cluster.ClusterDataSource) DataSource(javax.sql.DataSource)

Aggregations

ConnectionString (com.ctrip.framework.dal.cluster.client.database.ConnectionString)1 ForceSwitchedStatus (com.ctrip.platform.dal.common.enums.ForceSwitchedStatus)1 ClusterDataSource (com.ctrip.platform.dal.dao.datasource.cluster.ClusterDataSource)1 DalRuntimeException (com.ctrip.platform.dal.exceptions.DalRuntimeException)1 DataSource (javax.sql.DataSource)1