use of com.ctrip.platform.dal.dao.log.Callback in project dal by ctripcorp.
the class ForceSwitchableDataSource method forceSwitch.
public SwitchableDataSourceStatus forceSwitch(FirstAidKit configure, final String ip, final Integer port) {
synchronized (lock) {
final SwitchableDataSourceStatus oldStatus = getStatus();
final String name;
final DataSourceConfigure usedConfigure;
forceSwitchedStatus = ForceSwitchedStatus.ForceSwitching;
try {
if (isNullDataSource) {
if (configure instanceof IDataSourceConfigure) {
usedConfigure = dataSourceConfigureConvert.desDecrypt(DataSourceConfigure.valueOf((IDataSourceConfigure) configure));
name = ((IDataSourceConfigure) configure).getConnectionUrl();
} else {
throw new DalRuntimeException("Force Switch Error: datasource configure is invalid");
}
} else {
usedConfigure = getSingleDataSource().getDataSourceConfigure().clone();
name = getSingleDataSource().getName();
}
final String logName = String.format(FORCE_SWITCH, name);
LOGGER.logTransaction(DalLogTypes.DAL_CONFIGURE, logName, String.format("isNullDataSource: %s; newIp: %s, newPort: %s", isNullDataSource, ip, port), new Callback() {
@Override
public void execute() throws Exception {
LOGGER.logEvent(DalLogTypes.DAL_CONFIGURE, logName, String.format("old connection url: %s", usedConfigure.getConnectionUrl()));
LOGGER.logEvent(DalLogTypes.DAL_CONFIGURE, logName, String.format("old isForceSwitched before force switch: %s, old poolCreated before force switch: %s", oldStatus.isForceSwitched(), oldStatus.isPoolCreated()));
usedConfigure.replaceURL(ip, port);
LOGGER.logEvent(DalLogTypes.DAL_CONFIGURE, logName, String.format("new connection url: %s", usedConfigure.getConnectionUrl()));
asyncRefreshDataSource(name, usedConfigure, new ForceSwitchListener() {
public void onCreatePoolSuccess() {
LOGGER.logEvent(DalLogTypes.DAL_DATASOURCE, String.format("onCreatePoolSuccess: %s", name), usedConfigure.getConnectionUrl());
poolCreated = true;
forceSwitchedStatus = ForceSwitchedStatus.ForceSwitched;
oldForceSwitchedStatus = ForceSwitchedStatus.ForceSwitched;
isNullDataSource = false;
final SwitchableDataSourceStatus status = getStatus();
LOGGER.logEvent(DalLogTypes.DAL_DATASOURCE, String.format("onCreatePoolSuccess::notifyListeners: %s", name), "notify listeners' onForceSwitchSuccess");
for (final SwitchListener listener : listeners) executor.submit(new Runnable() {
@Override
public void run() {
try {
listener.onForceSwitchSuccess(status);
} catch (Exception e) {
LOGGER.error("call listener.onForceSwitchSuccess() error ", e);
}
}
});
}
public void onCreatePoolFail(final Throwable e) {
LOGGER.logEvent(DalLogTypes.DAL_DATASOURCE, String.format("onCreatePoolFail: %s", name), e.getMessage());
forceSwitchedStatus = oldForceSwitchedStatus;
final SwitchableDataSourceStatus status = getStatus();
LOGGER.logEvent(DalLogTypes.DAL_DATASOURCE, String.format("onCreatePoolFail::notifyListeners: %s", name), "notify listeners' onForceSwitchFail");
for (final SwitchListener listener : listeners) executor.submit(new Runnable() {
@Override
public void run() {
try {
listener.onForceSwitchFail(status, e);
} catch (Exception e1) {
LOGGER.error("call listener.onForceSwitchFail() error ", e1);
}
}
});
}
});
}
});
} catch (Throwable t) {
forceSwitchedStatus = oldForceSwitchedStatus;
LOGGER.error("Force switch error", t);
throw new DalRuntimeException("Force switch error", t);
}
return oldStatus;
}
}
use of com.ctrip.platform.dal.dao.log.Callback in project dal by ctripcorp.
the class DalClientFactory method internalInitClientFactory.
private static void internalInitClientFactory(final String path) throws Exception {
if (configureRef.get() != null) {
iLogger.warn(ALREADY_INITIALIZED);
iLogger.logEvent(DalLogTypes.DAL, INIT_DAL_JAVA_CLIENT, ALREADY_INITIALIZED);
return;
}
synchronized (DalClientFactory.class) {
if (configureRef.get() != null) {
iLogger.warn(ALREADY_INITIALIZED);
iLogger.logEvent(DalLogTypes.DAL, INIT_DAL_JAVA_CLIENT, ALREADY_INITIALIZED);
return;
}
iLogger.logTransaction(DalLogTypes.DAL, INIT_DAL_JAVA_CLIENT, "Initialize Dal Java Client", new Callback() {
@Override
public void execute() throws Exception {
DalConfigure config = null;
if (path == null) {
DalConfigLoader loader = ServiceLoaderHelper.getInstance(DalConfigLoader.class);
if (loader == null)
config = DalConfigureFactory.load();
else
config = loader.load();
iLogger.info("Successfully initialized Dal Java Client Factory");
} else {
config = DalConfigureFactory.load(path);
iLogger.info("Successfully initialized Dal Java Client Factory with " + path);
}
config.validate();
LogEntry.init();
DalRequestExecutor.init(config);
DalStatusManager.initialize(config);
configureRef.set(config);
}
});
}
}
use of com.ctrip.platform.dal.dao.log.Callback in project dal by ctripcorp.
the class ForceSwitchableDataSource method getStatus.
public SwitchableDataSourceStatus getStatus() {
synchronized (lock) {
org.apache.tomcat.jdbc.pool.DataSource ds = (org.apache.tomcat.jdbc.pool.DataSource) getSingleDataSource().getDataSource();
final String url = ds.getUrl();
final String name = getSingleDataSource().getName();
final String logName = String.format(GET_STATUS, name);
try {
LOGGER.logTransaction(DalLogTypes.DAL_CONFIGURE, logName, url, new Callback() {
@Override
public void execute() throws Exception {
if (!currentHostAndPort.isValid() || isUrlChanged(url)) {
currentHostAndPort.setValid(false);
LOGGER.logEvent(DalLogTypes.DAL_CONFIGURE, logName, "url changed, we will get url from connection");
try {
final CountDownLatch latch = new CountDownLatch(1);
executor.submit(new Runnable() {
public void run() {
try (Connection conn = getConnection()) {
HostAndPort hostAndPort = ConnectionStringParser.parseHostPortFromURL(conn.getMetaData().getURL());
hostAndPort.setValid(true);
setIpPortCache(hostAndPort);
setPoolCreated(true);
} catch (Exception e) {
setIpPortCache(ConnectionStringParser.parseHostPortFromURL(url));
} finally {
latch.countDown();
}
}
});
latch.await(1, TimeUnit.SECONDS);
} catch (Exception e) {
LOGGER.error("get connection error", e);
}
}
}
});
} catch (Throwable t) {
LOGGER.error("Get status error", t);
throw new DalRuntimeException("Get status error", t);
}
boolean isForceSwitched;
if (ForceSwitchedStatus.ForceSwitched.equals(forceSwitchedStatus)) {
isForceSwitched = true;
} else {
isForceSwitched = false;
}
if (!currentHostAndPort.isValid()) {
setIpPortCache(ConnectionStringParser.parseHostPortFromURL(url));
return buildSwitchableDataSourceStatus(isForceSwitched, currentHostAndPort.getHost(), url, currentHostAndPort.getPort(), false);
}
return buildSwitchableDataSourceStatus(isForceSwitched, currentHostAndPort.getHost(), url, currentHostAndPort.getPort(), poolCreated);
}
}
use of com.ctrip.platform.dal.dao.log.Callback in project dal by ctripcorp.
the class ForceSwitchableDataSource method restore.
public SwitchableDataSourceStatus restore() {
synchronized (lock) {
final SwitchableDataSourceStatus oldStatus = getStatus();
final String name = getSingleDataSource().getName();
final String logName = String.format(RESTORE, name);
try {
LOGGER.logTransaction(DalLogTypes.DAL_CONFIGURE, logName, "restore", new Callback() {
@Override
public void execute() throws Exception {
DataSourceConfigure configure = getSingleDataSource().getDataSourceConfigure().clone();
LOGGER.logEvent(DalLogTypes.DAL_CONFIGURE, logName, String.format("old connection url: %s", configure.getConnectionUrl()));
LOGGER.logEvent(DalLogTypes.DAL_CONFIGURE, logName, String.format("old isForceSwitched before restore: %s, old poolCreated before restore: %s", oldStatus.isForceSwitched(), oldStatus.isPoolCreated()));
if (!ForceSwitchedStatus.ForceSwitched.equals(forceSwitchedStatus)) {
LOGGER.logEvent(DalLogTypes.DAL_CONFIGURE, logName, String.format("%s is not force switched, return", name));
return;
}
final DataSourceConfigure newConfigure = DataSourceConfigure.valueOf(provider.forceLoadDataSourceConfigure());
LOGGER.logEvent(DalLogTypes.DAL_CONFIGURE, logName, String.format("new connection url: %s", newConfigure.getConnectionUrl()));
asyncRefreshDataSource(name, newConfigure, new RestoreListener() {
public void onCreatePoolSuccess() {
LOGGER.logEvent(DalLogTypes.DAL_DATASOURCE, String.format("onCreatePoolSuccess: %s", name), newConfigure.getConnectionUrl());
poolCreated = true;
forceSwitchedStatus = ForceSwitchedStatus.UnForceSwitched;
oldForceSwitchedStatus = ForceSwitchedStatus.UnForceSwitched;
final SwitchableDataSourceStatus status = getStatus();
LOGGER.logEvent(DalLogTypes.DAL_DATASOURCE, String.format("onCreatePoolSuccess::notifyListeners: %s", name), "notify listeners' onRestoreSuccess");
for (final SwitchListener listener : listeners) executor.submit(new Runnable() {
@Override
public void run() {
try {
listener.onRestoreSuccess(status);
} catch (Exception e1) {
LOGGER.error("call listener.onRestoreSuccess() error ", e1);
}
}
});
}
public void onCreatePoolFail(final Throwable e) {
LOGGER.logEvent(DalLogTypes.DAL_DATASOURCE, String.format("onCreatePoolFail: %s", name), e.getMessage());
final SwitchableDataSourceStatus status = getStatus();
LOGGER.logEvent(DalLogTypes.DAL_DATASOURCE, String.format("onCreatePoolFail::notifyListeners: %s", name), "notify listeners' onRestoreFail");
for (final SwitchListener listener : listeners) executor.submit(new Runnable() {
@Override
public void run() {
try {
listener.onRestoreFail(status, e);
} catch (Exception e1) {
LOGGER.error("call listener.onRestoreFail() error ", e1);
}
}
});
}
});
}
});
} catch (Throwable t) {
LOGGER.error("Restore error", t);
throw new DalRuntimeException("Restore error", t);
}
return oldStatus;
}
}
use of com.ctrip.platform.dal.dao.log.Callback in project dal by ctripcorp.
the class DalTransaction method rollbackOnly.
private void rollbackOnly(int level) throws Exception {
final String name = String.format(DAL_TRANSACTION_EXECUTE_ROLLBACK_ONLY, logicDbName == null ? "" : logicDbName);
final String msg = getRollbackOnlyMessage(level);
iLogger.logTransaction(SQL_Transaction, name, msg, new Callback() {
@Override
public void execute() {
rollback();
iLogger.logEvent(SQL_Transaction, name, msg);
}
}, msg);
}
Aggregations