use of com.pamirs.pradar.pressurement.datasource.DbMediatorDataSource in project LinkAgent by shulieTech.
the class DataSourceWrapUtil method init.
// static AtomicBoolean inited = new AtomicBoolean(false);
public static void init(DataSourceMeta<HikariDataSource> dataSourceMeta) {
if (pressureDataSources.get(dataSourceMeta) != null) {
return;
}
HikariDataSource target = dataSourceMeta.getDataSource();
if (isPerformanceDataSource(target)) {
return;
}
if (!validate(target)) {
// 没有配置对应的影子表或影子库
ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0002").setMessage("没有配置对应的影子表或影子库!").setDetail("业务库配置:::url: " + target.getJdbcUrl() + "; username:" + dataSourceMeta.getUsername() + "; 中间件类型:hikari").report();
HikariMediaDataSource dbMediatorDataSource = new HikariMediaDataSource();
dbMediatorDataSource.setDataSourceBusiness(target);
DbMediatorDataSource old = pressureDataSources.put(dataSourceMeta, dbMediatorDataSource);
if (old != null) {
if (logger.isInfoEnabled()) {
logger.info("[hikariCP] destroyed shadow table datasource success. url:{} ,username:{}", target.getJdbcUrl(), target.getUsername());
}
old.close();
}
return;
}
if (shadowTable(target)) {
// 影子表
try {
HikariMediaDataSource dbMediatorDataSource = new HikariMediaDataSource();
dbMediatorDataSource.setDataSourceBusiness(target);
DbMediatorDataSource old = pressureDataSources.put(dataSourceMeta, dbMediatorDataSource);
if (old != null) {
if (logger.isInfoEnabled()) {
logger.info("[hikariCP] destroyed shadow table datasource success. url:{} ,username:{}", target.getJdbcUrl(), target.getUsername());
}
old.close();
}
} catch (Throwable e) {
ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0002").setMessage("没有配置对应的影子表或影子库!").setDetail("业务库配置:::url: " + target.getJdbcUrl() + "; username:" + dataSourceMeta.getUsername() + "; 中间件类型:hikari" + Throwables.getStackTraceAsString(e)).report();
logger.error("[hikariCP] init datasource err!", e);
}
} else {
// 影子库
try {
HikariMediaDataSource dataSource = new HikariMediaDataSource();
HikariDataSource ptDataSource = copy(target);
dataSource.setDataSourcePerformanceTest(ptDataSource);
dataSource.setDataSourceBusiness(target);
DbMediatorDataSource old = pressureDataSources.put(dataSourceMeta, dataSource);
if (old != null) {
if (logger.isInfoEnabled()) {
logger.info("[hikariCP] destroyed shadow table datasource success. url:{} ,username:{}", target.getJdbcUrl(), target.getUsername());
}
old.close();
}
if (logger.isInfoEnabled()) {
logger.info("[hikariCP] create shadow datasource success. target:{} url:{} ,username:{} shadow-url:{},shadow-username:{}", target.hashCode(), target.getJdbcUrl(), target.getUsername(), ptDataSource.getJdbcUrl(), ptDataSource.getUsername());
}
} catch (Throwable t) {
logger.error("[hikariCP] init datasource err!", t);
ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0003").setMessage("影子库配置异常,无法由配置正确生成影子库!").setDetail("url: " + target.getJdbcUrl() + Throwables.getStackTraceAsString(t)).closePradar(ConfigNames.SHADOW_DATABASE_CONFIGS).report();
}
}
}
use of com.pamirs.pradar.pressurement.datasource.DbMediatorDataSource in project LinkAgent by shulieTech.
the class DataSourceWrapUtil method init.
// static AtomicBoolean inited = new AtomicBoolean(false);
public static void init(DataSourceMeta<BasicDataSource> dataSourceMeta) {
if (pressureDataSources.containsKey(dataSourceMeta) && pressureDataSources.get(dataSourceMeta) != null) {
return;
}
BasicDataSource target = dataSourceMeta.getDataSource();
if (isPerformanceDataSource(target)) {
return;
}
if (!validate(target)) {
// 没有配置对应的影子表或影子库
ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0002").setMessage("没有配置对应的影子表或影子库!").setDetail("dbcp2:DataSourceWrapUtil:业务库配置:::url: " + target.getUrl() + "; username:" + dataSourceMeta.getUsername() + "; 中间件类型:dbcp2").closePradar(ConfigNames.SHADOW_DATABASE_CONFIGS).report();
DbcpMediaDataSource dbMediatorDataSource = new DbcpMediaDataSource();
dbMediatorDataSource.setDataSourceBusiness(target);
DbMediatorDataSource old = pressureDataSources.put(dataSourceMeta, dbMediatorDataSource);
if (old != null) {
if (logger.isInfoEnabled()) {
logger.info("[dbcp2] destroyed shadow table datasource successful. url:{} ,username:{}", target.getUrl(), target.getUsername());
}
old.close();
}
return;
}
if (shadowTable(target)) {
// 影子表
try {
DbcpMediaDataSource dbMediatorDataSource = new DbcpMediaDataSource();
dbMediatorDataSource.setDataSourceBusiness(target);
DbMediatorDataSource old = pressureDataSources.put(dataSourceMeta, dbMediatorDataSource);
if (old != null) {
if (logger.isInfoEnabled()) {
logger.info("[dbcp2] destroyed shadow table datasource successful. url:{} ,username:{}", target.getUrl(), target.getUsername());
}
old.close();
}
} catch (Throwable e) {
ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0003").setMessage("影子表设置初始化异常!").setDetail("dbcp2:DataSourceWrapUtil:业务库配置:::url: " + target.getUrl() + "|||" + Throwables.getStackTraceAsString(e)).closePradar(ConfigNames.SHADOW_DATABASE_CONFIGS).report();
logger.error("[dbcp2] init datasource err!", e);
}
} else {
// 影子库
try {
DbcpMediaDataSource dataSource = new DbcpMediaDataSource();
BasicDataSource ptDataSource = copy(target);
dataSource.setDataSourcePerformanceTest(ptDataSource);
dataSource.setDataSourceBusiness(target);
DbMediatorDataSource old = pressureDataSources.put(dataSourceMeta, dataSource);
if (old != null) {
if (logger.isInfoEnabled()) {
logger.info("[dbcp2] destroyed shadow table datasource successful. url:{} ,username:{}", target.getUrl(), target.getUsername());
}
old.close();
}
if (logger.isInfoEnabled()) {
logger.info("[dbcp2] create shadow datasource successful. target:{} url:{} ,username:{} shadow-url:{},shadow-username:{}", target.hashCode(), target.getUrl(), target.getUsername(), ptDataSource.getUrl(), ptDataSource.getUsername());
}
} catch (Throwable t) {
logger.error("[dbcp2] init datasource err!", t);
ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0003").setMessage("影子库设置初始化异常!").setDetail("dbcp2:DataSourceWrapUtil:业务库配置:::url: " + target.getUrl() + "|||" + Throwables.getStackTraceAsString(t)).closePradar(ConfigNames.SHADOW_DATABASE_CONFIGS).report();
}
}
}
use of com.pamirs.pradar.pressurement.datasource.DbMediatorDataSource in project LinkAgent by shulieTech.
the class DataSourceWrapUtil method init.
// static AtomicBoolean inited = new AtomicBoolean(false);
public static void init(DataSourceMeta<ComboPooledDataSource> dataSourceMeta) {
if (pressureDataSources.containsKey(dataSourceMeta) && pressureDataSources.get(dataSourceMeta) != null) {
return;
}
ComboPooledDataSource target = dataSourceMeta.getDataSource();
if (isPerformanceDataSource(target)) {
return;
}
if (!validate(target)) {
logger.error("[c3p0] No configuration found for datasource, url: " + target.getJdbcUrl());
// 没有配置对应的影子表或影子库
ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0002").setMessage("没有配置对应的影子表或影子库!").setDetail("c3p0:DataSourceWrapUtil:业务库配置:::url: " + target.getJdbcUrl() + "; username:" + dataSourceMeta.getUsername() + "; 中间件类型:c3p0").report();
C3p0MediaDataSource dbMediatorDataSource = new C3p0MediaDataSource();
dbMediatorDataSource.setDataSourceBusiness(target);
DbMediatorDataSource old = pressureDataSources.put(dataSourceMeta, dbMediatorDataSource);
if (old != null) {
if (logger.isInfoEnabled()) {
logger.info("[c3p0] destroyed shadow table datasource successful. url:{} ,username:{}", target.getJdbcUrl(), target.getUser());
}
old.close();
}
return;
}
if (shadowTable(target)) {
// 影子表
try {
C3p0MediaDataSource dbMediatorDataSource = new C3p0MediaDataSource();
dbMediatorDataSource.setDataSourceBusiness(target);
DbMediatorDataSource old = pressureDataSources.put(dataSourceMeta, dbMediatorDataSource);
if (old != null) {
if (logger.isInfoEnabled()) {
logger.info("[c3p0] destroyed shadow table datasource successful. url:{} ,username:{}", target.getJdbcUrl(), target.getUser());
}
old.close();
}
} catch (Throwable e) {
ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0002").setMessage("没有配置对应的影子表或影子库!").setDetail("c3p0:DataSourceWrapUtil:业务库配置:::url: " + target.getJdbcUrl() + "; username:" + dataSourceMeta.getUsername() + "; 中间件类型:c3p0" + Throwables.getStackTraceAsString(e)).closePradar(ConfigNames.SHADOW_DATABASE_CONFIGS).report();
logger.error("[c3p0] init datasource err!", e);
}
} else {
// 影子库
try {
C3p0MediaDataSource dataSource = new C3p0MediaDataSource();
/**
* 如果没有配置则为null
*/
ComboPooledDataSource ptDataSource = copy(target);
dataSource.setDataSourcePerformanceTest(ptDataSource);
dataSource.setDataSourceBusiness(target);
DbMediatorDataSource old = pressureDataSources.put(dataSourceMeta, dataSource);
if (old != null) {
if (logger.isInfoEnabled()) {
logger.info("[c3p0] destroyed shadow table datasource successful. url:{} ,username:{}", target.getJdbcUrl(), target.getUser());
}
old.close();
}
if (logger.isInfoEnabled()) {
logger.info("[c3p0] create shadow datasource successful. target:{} url:{} ,username:{} shadow-url:{},shadow-username:{}", target.hashCode(), target.getJdbcUrl(), target.getUser(), ptDataSource.getJdbcUrl(), ptDataSource.getUser());
}
} catch (Throwable t) {
logger.error("[c3p0] init datasource err!", t);
ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0003").setMessage("影子库初始化失败!").setDetail("c3p0:DataSourceWrapUtil:业务库配置:::url: " + target.getJdbcUrl() + Throwables.getStackTraceAsString(t)).closePradar(ConfigNames.SHADOW_DATABASE_CONFIGS).report();
}
}
}
use of com.pamirs.pradar.pressurement.datasource.DbMediatorDataSource in project LinkAgent by shulieTech.
the class DataSourceWrapUtil method doWrap.
public static DbDruidMediatorDataSource doWrap(DataSourceMeta<DruidDataSource> dataSourceMeta) {
DbDruidMediatorDataSource cacheValue = pressureDataSources.get(dataSourceMeta);
if (cacheValue != null) {
return cacheValue;
}
DruidDataSource target = dataSourceMeta.getDataSource();
if (isPerformanceDataSource(target)) {
LOGGER.warn("[druid] current datasource is performance datasource. ignore it. url={}, username={}", target.getUrl(), target.getUsername());
return null;
}
boolean infoEnabled = LOGGER.isInfoEnabled();
if (!DruidDatasourceUtils.configured(target)) {
// 没有配置对应的影子表或影子库
LOGGER.error("[druid] No configuration found for datasource, url:{} username:{}", target.getUrl(), target.getUsername());
ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0002").setMessage("没有配置对应的影子表或影子库!").setDetail("业务库配置:::url: " + target.getUrl() + " ; username: " + target.getUsername() + "; 中间件类型:druid").report();
/**
* 如果未配置,则返回包装的数据源,类似于影子表
*/
DbDruidMediatorDataSource dbMediatorDataSource = new DbDruidMediatorDataSource();
dbMediatorDataSource.setDataSourceBusiness(target);
DbMediatorDataSource old = pressureDataSources.put(dataSourceMeta, dbMediatorDataSource);
if (old != null) {
if (infoEnabled) {
LOGGER.info("[druid] destroyed shadow table datasource success. url:{} ,username:{}", target.getUrl(), target.getUsername());
}
old.close();
}
return dbMediatorDataSource;
}
if (DruidDatasourceUtils.shadowTable(target)) {
// 影子表
DbDruidMediatorDataSource dbMediatorDataSource = new DbDruidMediatorDataSource();
dbMediatorDataSource.setDataSourceBusiness(target);
DbMediatorDataSource old = pressureDataSources.put(dataSourceMeta, dbMediatorDataSource);
if (old != null) {
if (infoEnabled) {
LOGGER.info("[druid] destroyed shadow table datasource success. url:{} ,username:{}", target.getUrl(), target.getUsername());
}
old.close();
}
return dbMediatorDataSource;
} else {
// 影子库
// 初始化影子数据源配置
DbDruidMediatorDataSource dbMediatorDataSource = new DbDruidMediatorDataSource();
dbMediatorDataSource.setDataSourceBusiness(target);
if (infoEnabled) {
LOGGER.info("[druid] use db shadow config:{}", GlobalConfig.getInstance().getShadowDatasourceConfigs());
}
DruidDataSource ptDataSource = DruidDatasourceUtils.generateDatasourceFromConfiguration(target, GlobalConfig.getInstance().getShadowDatasourceConfigs());
if (ptDataSource == null) {
LOGGER.error("[druid] create shadow datasource error. maybe datasource config is not correct, url: {} username:{} configurations:{}", target.getUrl(), target.getUsername(), GlobalConfig.getInstance().getShadowDatasourceConfigs());
ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0003").setMessage("影子库配置异常,无法由配置正确生成影子库!").setDetail("url: " + target.getUrl() + " username: " + target.getUsername()).closePradar(ConfigNames.SHADOW_DATABASE_CONFIGS).report();
return null;
}
dbMediatorDataSource.setDataSourcePerformanceTest(ptDataSource);
DbMediatorDataSource old = pressureDataSources.put(dataSourceMeta, dbMediatorDataSource);
if (old != null) {
if (infoEnabled) {
LOGGER.info("[druid] destroyed shadow table datasource success. url:{} ,username:{}", target.getUrl(), target.getUsername());
}
old.close();
}
if (infoEnabled) {
LOGGER.info("[druid] create shadow datasource success. target:{} url:{} ,username:{} shadow-url:{},shadow-username:{}", target.hashCode(), target.getUrl(), target.getUsername(), ptDataSource.getUrl(), ptDataSource.getUsername());
}
pressureDatasourceSet.add(ptDataSource);
return dbMediatorDataSource;
}
}
Aggregations