Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add AgentAware #426

Merged
merged 15 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public class ThreadPoolStats extends Metrics {
*/
private int maximumPoolSize;

/**
* 空闲时间 (ms)
*/
private long keepAliveTime;

/**
* 队列类型
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static void execute(Executor executor, Runnable r) {
public static void beforeExecute(Executor executor, Thread t, Runnable r) {
for (ExecutorAware aware : EXECUTOR_AWARE_LIST) {
try {
aware.beforeExecute(executor, t, r);
r = aware.beforeExecuteWrap(executor, t, r);
} catch (Exception e) {
log.error("DynamicTp aware [{}], enhance beforeExecute error.", aware.getName(), e);
}
Expand All @@ -106,7 +106,7 @@ public static void beforeExecute(Executor executor, Thread t, Runnable r) {
public static void afterExecute(Executor executor, Runnable r, Throwable t) {
for (ExecutorAware aware : EXECUTOR_AWARE_LIST) {
try {
aware.afterExecute(executor, r, t);
r = aware.afterExecuteWrap(executor, r, t);
} catch (Exception e) {
log.error("DynamicTp aware [{}], enhance afterExecute error.", aware.getName(), e);
}
Expand Down Expand Up @@ -146,7 +146,7 @@ public static void terminated(Executor executor) {
public static void beforeReject(Runnable r, Executor executor) {
for (ExecutorAware aware : EXECUTOR_AWARE_LIST) {
try {
aware.beforeReject(r, executor);
r = aware.beforeRejectWrap(r, executor);
} catch (Exception e) {
log.error("DynamicTp aware [{}], enhance beforeReject error.", aware.getName(), e);
}
Expand All @@ -156,7 +156,7 @@ public static void beforeReject(Runnable r, Executor executor) {
public static void afterReject(Runnable r, Executor executor) {
for (ExecutorAware aware : EXECUTOR_AWARE_LIST) {
try {
aware.afterReject(r, executor);
r = aware.afterRejectWrap(r, executor);
} catch (Exception e) {
log.error("DynamicTp aware [{}], enhance afterReject error.", aware.getName(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ default void beforeExecute(Executor executor, Thread t, Runnable r) {
// default no Operation
}

default Runnable beforeExecuteWrap(Executor executor, Thread t, Runnable r) {
beforeExecute(executor, t, r);
return r;
}

/**
* enhance afterExecute
*
Expand All @@ -102,6 +107,11 @@ default void afterExecute(Executor executor, Runnable r, Throwable t) {
// default no Operation
}

default Runnable afterExecuteWrap(Executor executor, Runnable r, Throwable t) {
afterExecute(executor, r, t);
return r;
}

/**
* enhance shutdown
*
Expand Down Expand Up @@ -139,6 +149,11 @@ default void beforeReject(Runnable r, Executor executor) {
// default no Operation
}

default Runnable beforeRejectWrap(Runnable r, Executor executor) {
beforeReject(r, executor);
return r;
}

/**
* enhance after reject
* @param r runnable
Expand All @@ -147,4 +162,9 @@ default void beforeReject(Runnable r, Executor executor) {
default void afterReject(Runnable r, Executor executor) {
// default no Operation
}

default Runnable afterRejectWrap(Runnable r, Executor executor) {
afterReject(r, executor);
return r;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ private static ThreadPoolStats convertCommon(ExecutorAdapter<?> executor) {
poolStats.setCompletedTaskCount(executor.getCompletedTaskCount());
poolStats.setWaitTaskCount(executor.getQueueSize());
poolStats.setRejectHandlerName(executor.getRejectHandlerType());
poolStats.setKeepAliveTime(executor.getKeepAliveTime(TimeUnit.MILLISECONDS));
return poolStats;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,11 @@
import org.dromara.dynamictp.common.em.CollectorTypeEnum;
import org.dromara.dynamictp.common.entity.ThreadPoolStats;
import org.dromara.dynamictp.core.monitor.collector.AbstractCollector;
import org.springframework.beans.BeanUtils;

import javax.management.JMException;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.lang.management.ManagementFactory;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* ThreadPoolStatsInfo related
Expand All @@ -40,26 +37,15 @@ public class JMXCollector extends AbstractCollector {

public static final String DTP_METRIC_NAME_PREFIX = "dtp.thread.pool";

/**
* thread pool stats map
*/
private static final Map<String, ThreadPoolStats> GAUGE_CACHE = new ConcurrentHashMap<>();

@Override
public void collect(ThreadPoolStats threadPoolStats) {
if (GAUGE_CACHE.containsKey(threadPoolStats.getPoolName())) {
ThreadPoolStats poolStats = GAUGE_CACHE.get(threadPoolStats.getPoolName());
BeanUtils.copyProperties(threadPoolStats, poolStats);
} else {
try {
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
ObjectName name = new ObjectName(DTP_METRIC_NAME_PREFIX + ":name=" + threadPoolStats.getPoolName());
ThreadPoolStatsJMX stats = new ThreadPoolStatsJMX(threadPoolStats);
server.registerMBean(stats, name);
} catch (JMException e) {
log.error("collect thread pool stats error", e);
}
GAUGE_CACHE.put(threadPoolStats.getPoolName(), threadPoolStats);
try {
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
ObjectName name = new ObjectName(DTP_METRIC_NAME_PREFIX + ":name=" + threadPoolStats.getPoolName());
ThreadPoolStatsJMX stats = new ThreadPoolStatsJMX(threadPoolStats);
server.registerMBean(stats, name);
} catch (JMException e) {
log.error("collect thread pool stats error", e);
}
}

Expand Down
8 changes: 7 additions & 1 deletion dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<url>https://github.com/yanhom1314/dynamic-tp</url>

<properties>
<revision>1.1.7</revision>
<revision>1.1.8-beta</revision>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<lombok.version>1.18.24</lombok.version>
Expand Down Expand Up @@ -426,6 +426,12 @@
<version>${revision}</version>
</dependency>

<dependency>
<groupId>org.dromara.dynamictp</groupId>
<artifactId>dynamic-tp-extension-agent</artifactId>
<version>${revision}</version>
</dependency>

<dependency>
<groupId>org.dromara.dynamictp</groupId>
<artifactId>dynamic-tp-extension-notify-email</artifactId>
Expand Down
14 changes: 14 additions & 0 deletions extension/extension-agent/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.dromara.dynamictp</groupId>
<artifactId>dynamic-tp-extension</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>dynamic-tp-extension-agent</artifactId>

<dependencies>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.dromara.dynamictp.core.aware;

import cn.hutool.core.util.ArrayUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.dromara.dynamictp.core.support.task.runnable.DtpRunnable;

import java.lang.ref.SoftReference;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.stream.Collectors;

import static org.dromara.dynamictp.common.constant.DynamicTpConst.DTP_EXECUTE_ENHANCED;
import static org.dromara.dynamictp.common.constant.DynamicTpConst.FALSE_STR;

/**
* deal agent wrapper
* @author txbao
*/
@Slf4j
@SuppressWarnings("all")
public class AgentAware extends TaskStatAware {

/**
* dtpRunnableCache key -> Runnable value -> DtpRunnable
*/
private final Map<Runnable, SoftReference<DtpRunnable>> dtpRunnableCache = new ConcurrentHashMap<>();

@Override
public int getOrder() {
return Integer.MIN_VALUE;
}

@Override
public String getName() {
return "agent";
}

private DtpRunnable determineDtpRunnable(List<Field> conditionalFields, Runnable r) throws IllegalAccessException {
for (Field field : conditionalFields) {
if (Objects.isNull(field)) {
continue;
}
field.setAccessible(true);
Runnable o = (Runnable) field.get(r);
if (o instanceof DtpRunnable) {
return (DtpRunnable) o;
}
// 纵向查找
DtpRunnable dtpRunnable = getDtpRunnable(o.getClass(), o);
if (dtpRunnable != null) {
return dtpRunnable;
}
}
return null;
}

private DtpRunnable getDtpRunnable(Class<? extends Runnable> rClass, Runnable r) throws IllegalAccessException {
while (Runnable.class.isAssignableFrom(rClass)) {
Field[] declaredFields = rClass.getDeclaredFields();
if (ArrayUtil.isNotEmpty(declaredFields)) {
List<Field> conditionFields = Arrays.stream(declaredFields)
.filter(ele -> Runnable.class.isAssignableFrom(ele.getType()))
.collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(conditionFields)) {
DtpRunnable dtpRunnable = determineDtpRunnable(conditionFields, r);
if (Objects.nonNull(dtpRunnable)) {
return dtpRunnable;
}
}
}
if (!Runnable.class.isAssignableFrom(rClass.getSuperclass())) {
break;
}
rClass = (Class<? extends Runnable>) rClass.getSuperclass();
}
return null;
}

private Runnable getDtpRunnableInstance(Runnable r) {
if (r instanceof DtpRunnable) {
return r;
}
DtpRunnable dtpRunnable = null;
Class<? extends Runnable> rClass = r.getClass();
try {
dtpRunnable = getDtpRunnable(rClass, r);
} catch (IllegalAccessException e) {
log.error("getDtpRunnable Error", e);
}
if (dtpRunnable == null) {
if (log.isWarnEnabled()) {
log.warn("DynamicTp aware [{}], can not find DtpRunnable.", getName());
}
return r;
}
return dtpRunnable;
}

@Override
public Runnable beforeExecuteWrap(Executor executor, Thread t, Runnable r) {
Runnable runnableWrap = getDtpRunnableInstance(r);
if (runnableWrap instanceof DtpRunnable) {
dtpRunnableCache.put(r, new SoftReference<>((DtpRunnable) runnableWrap));
} else {
// 被封装的wrapper没有找到DtpRunnable对象,那么就关闭某些监控指标,防止内存溢出
System.setProperty(DTP_EXECUTE_ENHANCED, FALSE_STR);
}
return runnableWrap;
}

@Override
public Runnable afterExecuteWrap(Executor executor, Runnable r, Throwable t) {
SoftReference<DtpRunnable> remove = dtpRunnableCache.remove(r);
if (remove != null) {
return remove.get();
}
return getDtpRunnableInstance(r);
}

@Override
public Runnable beforeRejectWrap(Runnable r, Executor executor) {
SoftReference<DtpRunnable> remove = dtpRunnableCache.remove(r);
if (remove != null) {
return remove.get();
}
return getDtpRunnableInstance(r);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.dromara.dynamictp.core.aware.AgentAware
5 changes: 0 additions & 5 deletions extension/extension-skywalking/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>

<dependency>
<groupId>org.dromara.dynamictp</groupId>
<artifactId>dynamic-tp-jvmti-runtime</artifactId>
</dependency>
</dependencies>

</project>
Loading
Loading