Skip to content

Commit

Permalink
修改功能测试出现的问题
Browse files Browse the repository at this point in the history
Signed-off-by: hanbingleixue <[email protected]>
  • Loading branch information
hanbingleixue committed Jan 27, 2024
1 parent 177a697 commit 345c04e
Show file tree
Hide file tree
Showing 35 changed files with 899 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@

package com.huaweicloud.sermant.core.plugin.classloader;

import com.huaweicloud.sermant.core.common.LoggerFactory;
import com.huaweicloud.sermant.core.config.ConfigManager;
import com.huaweicloud.sermant.core.plugin.agent.config.AgentConfig;

import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* 加载插件主模块的类加载器
Expand All @@ -30,8 +33,18 @@
* @since 2023-04-27
*/
public class PluginClassLoader extends URLClassLoader {
/**
* 日志
*/
private static final Logger LOGGER = LoggerFactory.getLogger();

private final HashMap<Long, ClassLoader> localLoader = new HashMap<>();

/**
* 对ClassLoader内部已加载的类进行管理
*/
private final HashMap<String, Class<?>> pluginClassMap = new HashMap<>();

/**
* 是否允许使用线程上下文类加载器
*/
Expand All @@ -57,49 +70,68 @@ public void appendUrl(URL url) {
this.addURL(url);
}

private Class<?> loadPluginClass(String name) {
if (!pluginClassMap.containsKey(name)) {
try {
pluginClassMap.put(name, findClass(name));
} catch (ClassNotFoundException classNotFoundException) {
pluginClassMap.put(name, null);
}
}
return pluginClassMap.get(name);
}

@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
return this.loadClass(name, false);
}

@Override
public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
Class<?> clazz = null;
synchronized (getClassLoadingLock(name)) {
Class<?> clazz = loadPluginClass(name);

try {
clazz = super.loadClass(name, resolve);
} catch (ClassNotFoundException ignored) {
// ignored
}
// 自身无法加载类,则通过Sermant搜索路径中加载
if (clazz == null) {
try {
clazz = super.loadClass(name, resolve);
} catch (ClassNotFoundException e) {
// 捕获类找不到的异常,下一步会进入localLoader中去加载类
// ignored
LOGGER.log(Level.FINE, "Load class failed, msg is {0}", e.getMessage());
}
}

// 无法从Sermant搜索路径中找到类,则尝试通过线程绑定的局部类加载器加载
if (clazz == null) {
ClassLoader loader = localLoader.get(Thread.currentThread().getId());
// 无法从Sermant搜索路径中找到类,则尝试通过线程绑定的局部类加载器加载
if (clazz == null) {
ClassLoader loader = localLoader.get(Thread.currentThread().getId());

if (loader == null && useContextLoader) {
loader = Thread.currentThread().getContextClassLoader();
}
if (loader == null && useContextLoader) {
loader = Thread.currentThread().getContextClassLoader();
}

// 确保局部类加载器不是当前类加载器,否则会stackoverflow
if (loader != null && !this.equals(loader)) {
try {
clazz = loader.loadClass(name);
} catch (ClassNotFoundException e) {
// 无法找到类,忽略,后续抛出异常
// 确保局部类加载器不是当前类加载器,否则会stackoverflow
if (loader != null && !this.equals(loader)) {
try {
clazz = loader.loadClass(name);
} catch (ClassNotFoundException e) {
// 无法找到类,忽略,后续抛出异常
LOGGER.log(Level.FINE, "Load class failed, msg is {0}", e.getMessage());
}
}
}
}

// 如果无法找到类,则抛出异常
if (clazz == null) {
throw new ClassNotFoundException("Sermant pluginClassLoader can not load class: " + name);
}
// 如果无法找到类,则抛出异常
if (clazz == null) {
throw new ClassNotFoundException("Sermant pluginClassLoader can not load class: " + name);
}

// 如果有需要则解析该类
if (resolve) {
resolveClass(clazz);
// 如果有需要则解析该类
if (resolve) {
resolveClass(clazz);
}
return clazz;
}
return clazz;
}

/**
Expand All @@ -110,19 +142,24 @@ public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundExce
* @throws ClassNotFoundException 无法通过类加载
*/
public Class<?> loadSermantClass(String name) throws ClassNotFoundException {
Class<?> clazz = null;
synchronized (getClassLoadingLock(name)) {
Class<?> clazz = loadPluginClass(name);

try {
clazz = super.loadClass(name, false);
} catch (ClassNotFoundException ignored) {
// 无法找到类,忽略,后续抛出异常
}
if (clazz == null) {
try {
clazz = super.loadClass(name, false);
} catch (ClassNotFoundException e) {
// 无法找到类,忽略,后续抛出异常
LOGGER.log(Level.WARNING, "load sermant class failed, msg is {0}", e.getMessage());
}
}

// 如果无法找到类,则抛出异常
if (clazz == null) {
throw new ClassNotFoundException("Sermant pluginClassLoader can not load class: " + name);
// 如果无法找到类,则抛出异常
if (clazz == null) {
throw new ClassNotFoundException("Sermant pluginClassLoader can not load class: " + name);
}
return clazz;
}
return clazz;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -55,7 +56,7 @@ public ServiceClassLoader(URL[] urls, ClassLoader parent) {
* @param name 全限定名
* @return Class对象
*/
private Class<?> loadPluginClass(String name) {
private Class<?> loadServiceClass(String name) {
if (!serviceClassMap.containsKey(name)) {
try {
serviceClassMap.put(name, findClass(name));
Expand All @@ -74,7 +75,7 @@ public Class<?> loadClass(String name) throws ClassNotFoundException {
@Override
public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
synchronized (getClassLoadingLock(name)) {
Class<?> clazz = loadPluginClass(name);
Class<?> clazz = loadServiceClass(name);
if (clazz == null) {
clazz = super.loadClass(name, resolve);

Expand Down
2 changes: 1 addition & 1 deletion sermant-plugins/sermant-metrics/metrics-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<jsqlparser.version>4.4</jsqlparser.version>
<jsqlparser.version>4.8</jsqlparser.version>
</properties>
<dependencies>
<!--provided依赖-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ public class Constants {
*/
public static final int MAX_SUCCESS_CODE = 399;

/**
* 默认的HTTP失败编码
*/
public static final int HTTP_DEFAULT_FAILURE_CODE = 9999;

/**
* 最大客户端失败编码
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
import com.huawei.metrics.util.ResultJudgmentUtil;
import com.huawei.metrics.util.SqlParseUtil;

import com.huaweicloud.sermant.core.common.LoggerFactory;
import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext;
import com.huaweicloud.sermant.core.plugin.agent.interceptor.Interceptor;

import java.sql.SQLException;
import java.util.logging.Logger;

/**
* MYSQL拦截器父类
Expand All @@ -35,6 +37,11 @@
* @since 2024-01-15
*/
public abstract class AbstractMysqlInterceptor implements Interceptor {
/**
* 日志
*/
public static final Logger LOGGER = LoggerFactory.getLogger();

@Override
public ExecuteContext after(ExecuteContext context) {
return collectMetrics(context);
Expand All @@ -58,16 +65,16 @@ public ExecuteContext onThrow(ExecuteContext context) {
*
* @param context 上下文信息
* @param enableSsl 是否开启SSL
* @param serverIp 服务端IP
* @param host 服务端域名或者IP
* @param serverPort 服务端端口
* @param sql 执行的SQL
* @return 指标信息
*/
public MetricsRpcInfo initMetricsRpcInfo(ExecuteContext context, boolean enableSsl, String serverIp,
public MetricsRpcInfo initMetricsRpcInfo(ExecuteContext context, boolean enableSsl, String host,
int serverPort, String sql) {
MetricsRpcInfo metricsRpcInfo = new MetricsRpcInfo();
metricsRpcInfo.setClientIp(InetAddressUtil.getHostAddress());
metricsRpcInfo.setServerIp(serverIp);
metricsRpcInfo.setServerIp(InetAddressUtil.getHostAddress(host));
metricsRpcInfo.setServerPort(serverPort);
metricsRpcInfo.setProtocol(Constants.MYSQL_PROTOCOL);
metricsRpcInfo.setEnableSsl(enableSsl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.huaweicloud.sermant.core.common.LoggerFactory;
import com.huaweicloud.sermant.core.utils.StringUtils;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
Expand Down Expand Up @@ -61,7 +62,8 @@ public static String getHostAddress() {
for (Enumeration<InetAddress> inetAdd = ni.getInetAddresses();
inetAdd.hasMoreElements(); ) {
InetAddress inetAddress = inetAdd.nextElement();
if (!inetAddress.isLoopbackAddress() && inetAddress.isSiteLocalAddress()) {
if (!inetAddress.isLoopbackAddress() && !inetAddress.isSiteLocalAddress()
&& inetAddress instanceof Inet4Address) {
hostAddress = inetAddress.getHostAddress();
return hostAddress;
}
Expand All @@ -75,4 +77,24 @@ public static String getHostAddress() {
}
return hostAddress;
}

/**
* 获取host对应的IP地址
*
* @param host 域名
* @return IP地址
*/
public static String getHostAddress(String host) {
try {
InetAddress inetAddress = InetAddress.getByName(host);
String address = inetAddress.getHostAddress();
if (StringUtils.equals(address, "127.0.0.1")) {
return getHostAddress();
}
return address;
} catch (UnknownHostException e) {
LOGGER.log(Level.SEVERE, "Unable to resolve domain name to IP.");
return StringUtils.EMPTY;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.huawei.metrics.util;

import com.huaweicloud.sermant.core.utils.StringUtils;

import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.alter.Alter;
import net.sf.jsqlparser.statement.create.index.CreateIndex;
import net.sf.jsqlparser.statement.drop.Drop;
import net.sf.jsqlparser.util.TablesNamesFinder;

/**
* 表名获取工具类
*
* @author zhp
* @since 2024-01-15
*/
public class MysqlTablesNameFinder extends TablesNamesFinder {
private static final String INDEX_TYPE = "INDEX";

@Override
public void visit(Drop drop) {
Table table = drop.getName();
if (StringUtils.equals(drop.getType(), INDEX_TYPE) || table == null
|| StringUtils.isEmpty(table.getName())) {
return;
}
visit(drop.getName());
}

@Override
public void visit(CreateIndex createIndex) {
visit(createIndex.getTable());
}

@Override
public void visit(Alter alter) {
visit(alter.getTable());
}
}
Loading

0 comments on commit 345c04e

Please sign in to comment.