use of com.pamirs.pradar.internal.config.ShadowDatabaseConfig in project LinkAgent by shulieTech.
the class DatabaseUtils method isTestTable.
public static boolean isTestTable(String url, String username) throws SQLException {
String key = DbUrlUtils.getKey(url, username);
ShadowDatabaseConfig shadowDatabaseConfig = GlobalConfig.getInstance().getShadowDatabaseConfig(key);
if (shadowDatabaseConfig == null) {
/**
* 解决现在影子表配置没有username的问题,再尝试使用非用户名的判断一次
*/
key = DbUrlUtils.getKey(url, null);
}
shadowDatabaseConfig = GlobalConfig.getInstance().getShadowDatabaseConfig(key);
if (shadowDatabaseConfig == null) {
return false;
}
return shadowDatabaseConfig.isShadowTable();
}
use of com.pamirs.pradar.internal.config.ShadowDatabaseConfig in project LinkAgent by shulieTech.
the class DelegateOperationExecutorInterceptor method getShadowDatabaseConfig.
private ShadowDatabaseConfig getShadowDatabaseConfig(Object mongoClientDelegate) {
ClusterSettings clusterSettings = ((Cluster) (ReflectionUtils.getFieldValue(mongoClientDelegate, "cluster"))).getSettings();
List<ServerAddress> serverAddresses = clusterSettings.getHosts();
ShadowDatabaseConfig shadowDatabaseConfig = null;
for (ShadowDatabaseConfig config : GlobalConfig.getInstance().getShadowDatasourceConfigs().values()) {
for (ServerAddress serverAddress : serverAddresses) {
if (config.getUrl().contains(serverAddress.toString())) {
shadowDatabaseConfig = config;
break;
}
}
}
return shadowDatabaseConfig;
}
use of com.pamirs.pradar.internal.config.ShadowDatabaseConfig in project LinkAgent by shulieTech.
the class DelegateOperationExecutorInterceptor method cutoff0.
@Override
public CutOffResult cutoff0(Advice advice) throws Throwable {
if (!Pradar.isClusterTest()) {
return CutOffResult.passed();
}
// 影子库重入
if (manager.getDynamicField(advice.getTarget(), "isCluster") != null) {
return CutOffResult.passed();
}
Object[] args = advice.getParameterArray();
Class operationClass = args[0].getClass();
Object operation = args[0];
OperationAccessor operationAccessor = OperationAccessorFactory.getOperationAccessor(operationClass);
if (operationAccessor == null) {
LOGGER.error("not support operation class is {} ", args[0].getClass().getName());
throw new PressureMeasureError("mongo not support pressure operation class is " + args[0].getClass().getName());
}
Object mongoClientDelegate = Caches.getMongoClientDelegate(advice.getTarget());
ShadowDatabaseConfig shadowDatabaseConfig = getShadowDatabaseConfig(mongoClientDelegate);
if (shadowDatabaseConfig == null) {
if (operationAccessor.isRead()) {
return CutOffResult.passed();
} else {
MongoNamespace busMongoNamespace = operationAccessor.getMongoNamespace(operation);
ErrorReporter.Error error = ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0005").setMessage("mongodb 不存在对应影子表或影子库:" + busMongoNamespace.getFullName()).setDetail("mongodb " + StringUtils.join(((Cluster) (ReflectionUtils.getFieldValue(mongoClientDelegate, "cluster"))).getSettings().getHosts(), ",") + "不存在对应影子表或影子库:" + busMongoNamespace.getFullName());
error.closePradar(ConfigNames.SHADOW_DATABASE_CONFIGS);
error.report();
throw new PressureMeasureError("mongo 对应影子表或影子库:" + busMongoNamespace.getFullName());
}
}
if (shadowDatabaseConfig.isShadowTable()) {
doShadowTable(operation, operationAccessor, shadowDatabaseConfig);
return CutOffResult.passed();
} else {
return CutOffResult.cutoff(doShadowDb(args, operationAccessor, shadowDatabaseConfig));
}
}
use of com.pamirs.pradar.internal.config.ShadowDatabaseConfig in project LinkAgent by shulieTech.
the class DataSourceWrapUtil method wrap.
public static void wrap(DataSourceMeta<Neo4jSession> neo4jSessionDataSourceMeta) {
if (DataSourceWrapUtil.pressureDataSources.containsKey(neo4jSessionDataSourceMeta)) {
return;
}
// 生成影子库配置
// 初始化影子库配置
DriverConfiguration sourceConfig = Components.driver().getConfiguration();
Configuration shadowConfig = new Configuration();
DriverConfiguration configuration = new DriverConfiguration(shadowConfig);
configuration.setDriverClassName(sourceConfig.getDriverClassName());
configuration.setConnectionPoolSize(sourceConfig.getConnectionPoolSize());
configuration.setEncryptionLevel(sourceConfig.getEncryptionLevel());
configuration.setTrustCertFile(sourceConfig.getTrustCertFile());
configuration.setTrustStrategy(sourceConfig.getTrustStrategy());
String key = DbUrlUtils.getKey(neo4jSessionDataSourceMeta.getUrl(), neo4jSessionDataSourceMeta.getUsername());
ShadowDatabaseConfig shadowDatabaseConfig = GlobalConfig.getInstance().getShadowDatabaseConfig(key);
if (null == shadowDatabaseConfig) {
ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0002").setMessage("没有配置对应的影子表或影子库!").setDetail("业务库配置:::url: " + neo4jSessionDataSourceMeta.getUrl()).closePradar(ConfigNames.SHADOW_DATABASE_CONFIGS).report();
return;
}
configuration.setURI(shadowDatabaseConfig.getShadowUrl());
configuration.setCredentials(shadowDatabaseConfig.getShadowUsername(), shadowDatabaseConfig.getShadowPassword());
Driver shadowDriver = DriverService.load(configuration);
// 生成影子库session
Neo4jSession sourceSession = neo4jSessionDataSourceMeta.getDataSource();
Neo4JSessionExt shadowSession = new Neo4JSessionExt(metaDataMap.get(sourceSession), shadowDriver);
// 放入本地
DbMediatorDataSource<Neo4jSession> driverConfigurationDbMediatorDataSource = new DriverConfig();
driverConfigurationDbMediatorDataSource.setDataSourceBusiness(sourceSession);
driverConfigurationDbMediatorDataSource.setDataSourcePerformanceTest(shadowSession);
DataSourceWrapUtil.pressureDataSources.put(neo4jSessionDataSourceMeta, driverConfigurationDbMediatorDataSource);
}
use of com.pamirs.pradar.internal.config.ShadowDatabaseConfig in project LinkAgent by shulieTech.
the class DataSourceWrapUtil method selectMatchPtDataSourceConfiguration.
@SuppressWarnings("unchecked")
private static ShadowDatabaseConfig selectMatchPtDataSourceConfiguration(BasicDataSource source, Map<String, ShadowDatabaseConfig> shadowDbConfigurations) {
BasicDataSource dataSource = source;
String key = DbUrlUtils.getKey(dataSource.getUrl(), dataSource.getUsername());
ShadowDatabaseConfig shadowDatabaseConfig = shadowDbConfigurations.get(key);
if (shadowDatabaseConfig == null) {
key = DbUrlUtils.getKey(dataSource.getUrl(), null);
shadowDatabaseConfig = shadowDbConfigurations.get(key);
}
return shadowDatabaseConfig;
}
Aggregations