use of com.pamirs.pradar.internal.config.ShadowDatabaseConfig in project LinkAgent by shulieTech.
the class DataSourceGetConnectionCutoffInterceptor method addListener.
private void addListener() {
EventRouter.router().addListener(new PradarEventListener() {
@Override
public EventResult onEvent(IEvent event) {
if (!(event instanceof ClusterTestSwitchOffEvent)) {
return EventResult.IGNORE;
}
// 关闭压测数据源
AtomikosDataSourceBeanWrapUtil.destroy();
AtomikosNonXADataSourceBeanWrapUtil.destroy();
return EventResult.success("tomcat-jdbc-plugin");
}
@Override
public int order() {
return 8;
}
}).addListener(new PradarEventListener() {
@Override
public EventResult onEvent(IEvent event) {
if (!(event instanceof ShadowDataSourceConfigModifyEvent)) {
return EventResult.IGNORE;
}
ShadowDataSourceConfigModifyEvent shadowDataSourceConfigModifyEvent = (ShadowDataSourceConfigModifyEvent) event;
Set<ShadowDatabaseConfig> target = shadowDataSourceConfigModifyEvent.getTarget();
if (null == target || target.size() == 0) {
return EventResult.IGNORE;
}
for (ShadowDatabaseConfig config : target) {
Iterator<Map.Entry<DataSourceMeta, AtomikosDataSourceBeanMediaDataSource>> it = AtomikosDataSourceBeanWrapUtil.pressureDataSources.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<DataSourceMeta, AtomikosDataSourceBeanMediaDataSource> entry = it.next();
if (StringUtils.equalsIgnoreCase(DbUrlUtils.getKey(config.getUrl(), config.getUsername()), DbUrlUtils.getKey(entry.getKey().getUrl(), entry.getKey().getUsername()))) {
AtomikosDataSourceBeanMediaDataSource value = entry.getValue();
it.remove();
try {
value.close();
if (logger.isInfoEnabled()) {
logger.info("module-atomikos-datasource: destroyed shadow table datasource success. url:{} ,username:{}", entry.getKey().getUrl(), entry.getKey().getUsername());
}
} catch (Throwable e) {
logger.error("module-atomikos-datasource: closed datasource err! target:{}, url:{} username:{}", entry.getKey().getDataSource().hashCode(), entry.getKey().getUrl(), entry.getKey().getUsername(), e);
}
break;
}
}
Iterator<Map.Entry<DataSourceMeta, AtomikosNonXADataSourceBeanMediaDataSource>> nonIt = AtomikosNonXADataSourceBeanWrapUtil.pressureDataSources.entrySet().iterator();
while (nonIt.hasNext()) {
Map.Entry<DataSourceMeta, AtomikosNonXADataSourceBeanMediaDataSource> entry = nonIt.next();
if (StringUtils.equalsIgnoreCase(config.getUrl(), entry.getKey().getUrl()) && StringUtils.equalsIgnoreCase(config.getUsername(), entry.getKey().getUsername())) {
AtomikosNonXADataSourceBeanMediaDataSource value = entry.getValue();
nonIt.remove();
try {
value.close();
if (logger.isInfoEnabled()) {
logger.info("module-atomikos-datasource: destroyed shadow table datasource success. url:{} ,username:{}", entry.getKey().getUrl(), entry.getKey().getUsername());
}
} catch (Throwable e) {
logger.error("module-atomikos-datasource: closed datasource err! target:{}, url:{} username:{}", entry.getKey().getDataSource().hashCode(), entry.getKey().getUrl(), entry.getKey().getUsername(), e);
}
break;
}
}
}
return EventResult.success("module-atomikos-datasource: destroyed shadow table datasource success.");
}
@Override
public int order() {
return 2;
}
});
}
use of com.pamirs.pradar.internal.config.ShadowDatabaseConfig in project LinkAgent by shulieTech.
the class DelegateOperationExecutorInterceptor method getShadowDatabaseConfig.
private ShadowDatabaseConfig getShadowDatabaseConfig(MongoClientDelegate mongoClientDelegate) {
ClusterSettings clusterSettings = mongoClientDelegate.getCluster().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 MongoDBMongoClientDelegateConstructorInterceptor method check.
/**
* 判断是否影子库
*/
private void check(Object baseCluster, Mongo businessMongo) {
List<ServerAddress> addressList;
if (baseCluster instanceof SingleServerCluster) {
addressList = ((SingleServerCluster) baseCluster).getSettings().getHosts();
} else {
addressList = ((MultiServerCluster) baseCluster).getSettings().getHosts();
}
for (Map.Entry<String, ShadowDatabaseConfig> entry : GlobalConfig.getInstance().getShadowDatasourceConfigs().entrySet()) {
String businessUrl = entry.getValue().getUrl();
for (ServerAddress address : addressList) {
if (businessUrl != null && businessUrl.contains(address.toString())) {
if (!MongoClientHolder.mongoClientMap.containsKey(address.toString())) {
MongoClientHolder.mongoIntegerMap.put(businessMongo, 2);
break;
} else {
throw new PressureMeasureError("配置了相同地址的的影子库!");
}
}
}
}
for (ServerAddress address : addressList) {
ShadowDatabaseConfig shadowDatabaseConfig = GlobalConfig.getInstance().getShadowDatabaseConfig(address.toString());
if (shadowDatabaseConfig == null) {
continue;
}
if (!shadowDatabaseConfig.isShadowTable()) {
continue;
}
Map<String, String> map = shadowDatabaseConfig.getBusinessShadowTables();
if (null != map && map.size() > 0) {
MongoClientHolder.mongoIntegerMap.put(businessMongo, 1);
MongoClientHolder.mongoTableMappingMap.put(businessMongo, map);
break;
}
}
}
use of com.pamirs.pradar.internal.config.ShadowDatabaseConfig in project LinkAgent by shulieTech.
the class MongoExecuteCutoffInterceptor method cutoff0.
@Override
public CutOffResult cutoff0(Advice advice) throws Throwable {
if (!Pradar.isClusterTest()) {
return CutOffResult.passed();
}
if (ClientManagerUtils.getBusClient2ptClientMapping().containsValue(advice.getTarget())) {
return CutOffResult.passed();
}
List<ServerAddress> serverAddresses = ((MongoClient) advice.getTarget()).getAllAddress();
ShadowDatabaseConfig shadowDatabaseConfig = getShadowDatabaseConfig(serverAddresses);
if (shadowDatabaseConfig == null) {
ErrorReporter.Error error = ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0005").setMessage("mongo 未配置对应影子表").setDetail("mongo 未配置对应影子表");
error.closePradar(ConfigNames.SHADOW_DATABASE_CONFIGS);
error.report();
throw new PressureMeasureError("mongo 未配置对应影子表");
}
if (!shadowDatabaseConfig.isShadowDatabase()) {
return CutOffResult.passed();
}
Mongo ptMongo = ClientManagerUtils.getBusClient2ptClientMapping().get(advice.getTarget());
if (ptMongo == null) {
return CutOffResult.passed();
}
if (advice.getParameterArray().length == 1) {
return CutOffResult.cutoff(writeMethod.invoke(ptMongo, advice.getParameterArray()));
} else {
return CutOffResult.cutoff(readMethod.invoke(ptMongo, advice.getParameterArray()));
}
}
use of com.pamirs.pradar.internal.config.ShadowDatabaseConfig in project LinkAgent by shulieTech.
the class ProxoolDriverConnectInterceptor method getParameter0.
@Override
public Object[] getParameter0(Advice advice) {
if (!Pradar.isClusterTest()) {
return advice.getParameterArray();
}
// proxool.ds
String connectUrl = (String) advice.getParameterArray()[0];
if (!connectUrl.startsWith("proxool")) {
return advice.getParameterArray();
}
String alias = getAlias(connectUrl);
// 说明url有问题
if (alias == null || alias.length() == 0) {
return advice.getParameterArray();
}
Object[] args = advice.getParameterArray();
if (ConnectionPoolUtils.getConnectionPoolMap().containsKey(Pradar.CLUSTER_TEST_PREFIX + alias)) {
args[0] = "proxool." + Pradar.CLUSTER_TEST_PREFIX + alias;
return args;
}
String username = null;
String url = null;
Properties definitionProperties = null;
try {
if (getDefinitionMethod == null) {
getDefinitionMethod = ConnectionPoolUtils.getConnectionPoolMap().get(alias).getClass().getDeclaredMethod(ProxoolConst.REFLECT_GET_DEFINITION_METHOD);
getDefinitionMethod.setAccessible(true);
}
Object definition = getDefinitionMethod.invoke(ConnectionPoolUtils.getConnectionPoolMap().get(alias));
getUrlMethod = definition.getClass().getDeclaredMethod(ProxoolConst.REFLECT_GET_URL_METHOD);
getUserMethod = definition.getClass().getDeclaredMethod(ProxoolConst.REFLECT_GET_USER_METHOD);
Method getCompleteInfo = definition.getClass().getDeclaredMethod(ProxoolConst.REFLECT_GET_COMPLETE_INFO_METHOD);
getUrlMethod.setAccessible(true);
getUserMethod.setAccessible(true);
getCompleteInfo.setAccessible(true);
url = (String) getUrlMethod.invoke(definition);
username = (String) getUserMethod.invoke(definition);
definitionProperties = (Properties) getCompleteInfo.invoke(definition);
} catch (Exception e) {
logger.error("proxool init getDefinitionMethod and definition error.", e);
}
ShadowDatabaseConfig shadowDatabaseConfig = getShadowDatabaseConfig(url, username);
if (shadowDatabaseConfig == null) {
// 未配置影子库表
throw new PressureMeasureError("未配置影子库表: " + url + " " + username);
}
// 影子表 直接返回
if (shadowDatabaseConfig.isShadowTable()) {
return args;
}
StringBuffer ptUrl = new StringBuffer();
ptUrl.append("proxool");
ptUrl.append(ProxoolConstants.ALIAS_DELIMITER);
ptUrl.append(Pradar.CLUSTER_TEST_PREFIX + alias);
ptUrl.append(ProxoolConstants.URL_DELIMITER);
ptUrl.append(shadowDatabaseConfig.getShadowDriverClassName());
ptUrl.append(ProxoolConstants.URL_DELIMITER);
ptUrl.append(shadowDatabaseConfig.getShadowUrl());
try {
Class connectionPoolDefinitionClass = Thread.currentThread().getContextClassLoader().loadClass("org.logicalcobwebs.proxool.ConnectionPoolDefinition");
Constructor connectionPoolDefinitionOfConstructor = connectionPoolDefinitionClass.getDeclaredConstructor(String.class, Properties.class, boolean.class);
connectionPoolDefinitionOfConstructor.setAccessible(true);
Object ptConnectionPoolDefinitionOfInstance = connectionPoolDefinitionOfConstructor.newInstance(ptUrl.toString(), buildPtProperties(definitionProperties, shadowDatabaseConfig), true);
Class connectionPoolClass = Thread.currentThread().getContextClassLoader().loadClass("org.logicalcobwebs.proxool.ConnectionPool");
Constructor connectionPoolOfConstructor = connectionPoolClass.getDeclaredConstructors()[0];
connectionPoolOfConstructor.setAccessible(true);
Object connectionPoolOfInstance = connectionPoolOfConstructor.newInstance(ptConnectionPoolDefinitionOfInstance);
Method startMethod = connectionPoolClass.getDeclaredMethod("start");
startMethod.setAccessible(true);
startMethod.invoke(connectionPoolOfInstance);
ConnectionPoolUtils.getConnectionPoolMap().put(Pradar.CLUSTER_TEST_PREFIX + alias, connectionPoolOfInstance);
ConnectionPoolUtils.getConnectionPools().add(connectionPoolOfInstance);
} catch (Exception e) {
logger.error("proxool init ConnectionPool error.", e);
}
args[0] = "proxool." + Pradar.CLUSTER_TEST_PREFIX + alias;
return args;
}
Aggregations