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);
}
}
}
Aggregations