From d5528bc42f6dc8232115d0834274e2bf1b7339e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?tianxiang=20bao=20=EF=BC=88=E5=8C=85=E5=A4=A9=E7=A5=A5?= =?UTF-8?q?=EF=BC=89?= Date: Mon, 28 Jul 2025 11:21:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E5=91=8A=E8=AD=A6=E4=BD=93=E7=B3=BB?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../adapter/common/DtpAdapterListener.java | 4 +- .../common/constant/DynamicTpConst.java | 39 +++-- .../dynamictp/common/em/NotifyItemEnum.java | 148 +++++++++--------- .../common/em/NotifyItemTypeGroupEnum.java | 32 ++++ .../dynamictp/common/entity/AlarmInfo.java | 4 +- .../common/entity/CustomNotifyParam.java | 49 ++++++ .../dynamictp/common/entity/NotifyItem.java | 70 +++++---- .../common/entity/TpExecutorProps.java | 4 +- .../type/AbstractCustomAlarmType.java | 34 ++++ .../notifier/type/CommonNotifyItemType.java | 68 ++++++++ .../notifier/type/NotifyItemTypeRegistry.java | 109 +++++++++++++ .../dynamictp/core/aware/TaskRejectAware.java | 2 +- .../dynamictp/core/executor/DtpExecutor.java | 4 +- .../core/handler/NotifierHandler.java | 20 ++- .../dynamictp/core/monitor/DtpMonitor.java | 4 +- .../core/notifier/AbstractDtpNotifier.java | 30 ++-- .../dynamictp/core/notifier/DtpNotifier.java | 13 +- .../core/notifier/alarm/AlarmCounter.java | 8 +- .../core/notifier/alarm/AlarmLimiter.java | 5 +- .../notifier/capture/CapturedExecutor.java | 4 +- .../notifier/chain/invoker/AlarmInvoker.java | 4 +- .../core/notifier/context/BaseNotifyCtx.java | 7 +- .../core/notifier/context/CustomAlarmCtx.java | 39 +++++ .../core/notifier/manager/AlarmManager.java | 45 ++++-- .../core/notifier/manager/NoticeManager.java | 2 +- .../core/notifier/manager/NotifyHelper.java | 30 ++-- .../core/support/ExecutorWrapper.java | 4 +- .../core/support/ThreadPoolBuilder.java | 4 +- .../core/timer/QueueTimeoutTimerTask.java | 2 +- .../core/timer/RunTimeoutTimerTask.java | 2 +- .../dynamictp/extension/agent/AgentAware.java | 7 + .../RedisRateLimiterNotifyFilter.java | 2 +- .../notify/email/DtpEmailNotifier.java | 8 +- .../DtpBeanDefinitionRegistrar.java | 22 +-- .../core/notify/AbstractDtpNotifierTest.java | 19 ++- .../test/core/notify/AlarmManagerTest.java | 39 +++++ .../notify/customalarmtype/MyAlarmType.java | 10 ++ ...mmon.notifier.type.AbstractCustomAlarmType | 1 + .../src/test/resources/dynamic-tp-alarm.yml | 44 ++++++ 39 files changed, 723 insertions(+), 219 deletions(-) create mode 100644 common/src/main/java/org/dromara/dynamictp/common/em/NotifyItemTypeGroupEnum.java create mode 100644 common/src/main/java/org/dromara/dynamictp/common/entity/CustomNotifyParam.java create mode 100644 common/src/main/java/org/dromara/dynamictp/common/notifier/type/AbstractCustomAlarmType.java create mode 100644 common/src/main/java/org/dromara/dynamictp/common/notifier/type/CommonNotifyItemType.java create mode 100644 common/src/main/java/org/dromara/dynamictp/common/notifier/type/NotifyItemTypeRegistry.java create mode 100644 core/src/main/java/org/dromara/dynamictp/core/notifier/context/CustomAlarmCtx.java create mode 100644 test/test-core/src/test/java/org/dromara/dynamictp/test/core/notify/AlarmManagerTest.java create mode 100644 test/test-core/src/test/java/org/dromara/dynamictp/test/core/notify/customalarmtype/MyAlarmType.java create mode 100644 test/test-core/src/test/resources/META-INF/services/org.dromara.dynamictp.common.notifier.type.AbstractCustomAlarmType create mode 100644 test/test-core/src/test/resources/dynamic-tp-alarm.yml diff --git a/adapter/adapter-common/src/main/java/org/dromara/dynamictp/adapter/common/DtpAdapterListener.java b/adapter/adapter-common/src/main/java/org/dromara/dynamictp/adapter/common/DtpAdapterListener.java index 1590b9e0..d50fc968 100644 --- a/adapter/adapter-common/src/main/java/org/dromara/dynamictp/adapter/common/DtpAdapterListener.java +++ b/adapter/adapter-common/src/main/java/org/dromara/dynamictp/adapter/common/DtpAdapterListener.java @@ -26,13 +26,13 @@ import org.dromara.dynamictp.common.event.CollectEvent; import org.dromara.dynamictp.common.event.RefreshEvent; import org.dromara.dynamictp.common.manager.ContextManagerHelper; import org.dromara.dynamictp.common.manager.EventBusManager; +import org.dromara.dynamictp.common.notifier.type.NotifyItemTypeRegistry; import org.dromara.dynamictp.common.properties.DtpProperties; import org.dromara.dynamictp.core.handler.CollectorHandler; import org.dromara.dynamictp.core.notifier.manager.AlarmManager; import java.util.EventObject; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.SCHEDULE_NOTIFY_ITEMS; /** * DtpAdapterListener related @@ -101,7 +101,7 @@ public class DtpAdapterListener { } handlerMap.forEach((k, v) -> { val executorWrapper = v.getExecutorWrappers(); - executorWrapper.forEach((kk, vv) -> AlarmManager.checkAndTryAlarmAsync(vv, SCHEDULE_NOTIFY_ITEMS)); + executorWrapper.forEach((kk, vv) -> AlarmManager.checkAndTryAlarmAsync(vv, NotifyItemTypeRegistry.getScheduleNotifyTypes())); }); } } diff --git a/common/src/main/java/org/dromara/dynamictp/common/constant/DynamicTpConst.java b/common/src/main/java/org/dromara/dynamictp/common/constant/DynamicTpConst.java index 847c529f..aee9db61 100644 --- a/common/src/main/java/org/dromara/dynamictp/common/constant/DynamicTpConst.java +++ b/common/src/main/java/org/dromara/dynamictp/common/constant/DynamicTpConst.java @@ -17,11 +17,6 @@ package org.dromara.dynamictp.common.constant; -import com.google.common.collect.ImmutableList; -import org.dromara.dynamictp.common.em.NotifyItemEnum; - -import java.util.List; - /** * DynamicTpConst related * @@ -81,11 +76,11 @@ public final class DynamicTpConst { public static final String REJECT_HANDLER_TYPE = "rejectHandlerType"; - public static final String RUN_TIMEOUT = "runTimeout"; + public static final String RUN_TIMEOUT_PROP = "runTimeout"; public static final String TRY_INTERRUPT_WHEN_TIMEOUT = "tryInterrupt"; - public static final String QUEUE_TIMEOUT = "queueTimeout"; + public static final String QUEUE_TIMEOUT_PROP = "queueTimeout"; public static final String TASK_WRAPPERS = "taskWrappers"; @@ -102,8 +97,34 @@ public final class DynamicTpConst { public static final String ARR_RIGHT_BRACKET = "]"; - public static final List SCHEDULE_NOTIFY_ITEMS = ImmutableList.of(NotifyItemEnum.LIVENESS, - NotifyItemEnum.CAPACITY); +// public static final List SCHEDULE_NOTIFY_ITEMS = ImmutableList.of(NotifyItemEnum.LIVENESS, +// NotifyItemEnum.CAPACITY); + + /** + * Config change notify. + */ + public static final String CHANGE = "change"; + /** + * ThreadPool liveness notify. + * liveness = activeCount / maximumPoolSize + */ + public static final String LIVENESS = "liveness"; + /** + * Capacity threshold notify. + */ + public static final String CAPACITY = "capacity"; + /** + * Reject notify. + */ + public static final String REJECT = "reject"; + /** + * Task run timeout alarm. + */ + public static final String RUN_TIMEOUT = "run_timeout"; + /** + * Task queue wait timeout alarm. + */ + public static final String QUEUE_TIMEOUT = "queue_timeout"; /** * unit diff --git a/common/src/main/java/org/dromara/dynamictp/common/em/NotifyItemEnum.java b/common/src/main/java/org/dromara/dynamictp/common/em/NotifyItemEnum.java index 39bdc46d..805e92ed 100644 --- a/common/src/main/java/org/dromara/dynamictp/common/em/NotifyItemEnum.java +++ b/common/src/main/java/org/dromara/dynamictp/common/em/NotifyItemEnum.java @@ -1,74 +1,74 @@ -/* - * 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.common.em; - -import lombok.AllArgsConstructor; -import lombok.Getter; - -/** - * NotifyItemEnum related - * - * @author yanhom - * @since 1.0.0 - **/ -@Getter -@AllArgsConstructor -public enum NotifyItemEnum { - - /** - * Config change notify. - */ - CHANGE("change"), - - /** - * ThreadPool liveness notify. - * liveness = activeCount / maximumPoolSize - */ - LIVENESS("liveness"), - - /** - * Capacity threshold notify. - */ - CAPACITY("capacity"), - - /** - * Reject notify. - */ - REJECT("reject"), - - /** - * Task run timeout alarm. - */ - RUN_TIMEOUT("run_timeout"), - - /** - * Task queue wait timeout alarm. - */ - QUEUE_TIMEOUT("queue_timeout"); - - private final String value; - - public static NotifyItemEnum of(String value) { - for (NotifyItemEnum notifyItem : NotifyItemEnum.values()) { - if (notifyItem.value.equals(value)) { - return notifyItem; - } - } - return null; - } -} +///* +// * 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.common.em; +// +//import lombok.AllArgsConstructor; +//import lombok.Getter; +// +///** +// * NotifyItemEnum related +// * +// * @author yanhom +// * @since 1.0.0 +// **/ +//@Getter +//@AllArgsConstructor +//public enum NotifyItemEnum { +// +// /** +// * Config change notify. +// */ +// CHANGE("change"), +// +// /** +// * ThreadPool liveness notify. +// * liveness = activeCount / maximumPoolSize +// */ +// LIVENESS("liveness"), +// +// /** +// * Capacity threshold notify. +// */ +// CAPACITY("capacity"), +// +// /** +// * Reject notify. +// */ +// REJECT("reject"), +// +// /** +// * Task run timeout alarm. +// */ +// RUN_TIMEOUT("run_timeout"), +// +// /** +// * Task queue wait timeout alarm. +// */ +// QUEUE_TIMEOUT("queue_timeout"); +// +// private final String value; +// +// public static NotifyItemEnum of(String value) { +// for (NotifyItemEnum notifyItem : NotifyItemEnum.values()) { +// if (notifyItem.value.equals(value)) { +// return notifyItem; +// } +// } +// return null; +// } +//} diff --git a/common/src/main/java/org/dromara/dynamictp/common/em/NotifyItemTypeGroupEnum.java b/common/src/main/java/org/dromara/dynamictp/common/em/NotifyItemTypeGroupEnum.java new file mode 100644 index 00000000..65641f0b --- /dev/null +++ b/common/src/main/java/org/dromara/dynamictp/common/em/NotifyItemTypeGroupEnum.java @@ -0,0 +1,32 @@ +/* + * 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.common.em; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public enum NotifyItemTypeGroupEnum { + + SCHEDULE("schedule"), + SIMPLE("simple"), + FRAMEWORK_WITHOUT_SIMPLE("frameworkWithoutSimple"), + CUSTOM("custom"); + + private final String value; +} diff --git a/common/src/main/java/org/dromara/dynamictp/common/entity/AlarmInfo.java b/common/src/main/java/org/dromara/dynamictp/common/entity/AlarmInfo.java index 438cb4d1..f42e8a75 100644 --- a/common/src/main/java/org/dromara/dynamictp/common/entity/AlarmInfo.java +++ b/common/src/main/java/org/dromara/dynamictp/common/entity/AlarmInfo.java @@ -19,7 +19,7 @@ package org.dromara.dynamictp.common.entity; import lombok.Data; import lombok.experimental.Accessors; -import org.dromara.dynamictp.common.em.NotifyItemEnum; +import org.dromara.dynamictp.common.notifier.type.CommonNotifyItemType; import java.util.concurrent.atomic.AtomicInteger; @@ -33,7 +33,7 @@ import java.util.concurrent.atomic.AtomicInteger; @Accessors(chain = true) public class AlarmInfo { - private NotifyItemEnum notifyItem; + private CommonNotifyItemType notifyItem; private final AtomicInteger counter = new AtomicInteger(0); diff --git a/common/src/main/java/org/dromara/dynamictp/common/entity/CustomNotifyParam.java b/common/src/main/java/org/dromara/dynamictp/common/entity/CustomNotifyParam.java new file mode 100644 index 00000000..1e704d1d --- /dev/null +++ b/common/src/main/java/org/dromara/dynamictp/common/entity/CustomNotifyParam.java @@ -0,0 +1,49 @@ +/* + * 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.common.entity; + +import cn.hutool.core.map.MapUtil; + +import java.util.HashMap; +import java.util.Map; + +public class CustomNotifyParam { + + private final Map customAlarmParams; + + public CustomNotifyParam() { + customAlarmParams = new HashMap<>(); + } + + public CustomNotifyParam(int size) { + this.customAlarmParams = MapUtil.newHashMap(size); + } + + public CustomNotifyParam put(String key, Object value) { + customAlarmParams.put(key, value); + return this; + } + + @SuppressWarnings("unchecked") + public T get(String key) { + return (T) customAlarmParams.get(key); + } + + public Map getCustomAlarmParams() { + return customAlarmParams; + } +} diff --git a/common/src/main/java/org/dromara/dynamictp/common/entity/NotifyItem.java b/common/src/main/java/org/dromara/dynamictp/common/entity/NotifyItem.java index be911dd3..ebf5be72 100644 --- a/common/src/main/java/org/dromara/dynamictp/common/entity/NotifyItem.java +++ b/common/src/main/java/org/dromara/dynamictp/common/entity/NotifyItem.java @@ -17,18 +17,20 @@ package org.dromara.dynamictp.common.entity; +import cn.hutool.core.collection.CollUtil; import lombok.Data; import lombok.val; import org.apache.commons.collections4.CollectionUtils; -import org.dromara.dynamictp.common.em.NotifyItemEnum; +import org.dromara.dynamictp.common.notifier.type.CommonNotifyItemType; +import org.dromara.dynamictp.common.notifier.type.NotifyItemTypeRegistry; import org.dromara.dynamictp.common.util.StringUtil; import java.util.ArrayList; import java.util.List; -import java.util.Objects; import java.util.stream.Collectors; import static java.util.stream.Collectors.toList; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.*; import static org.dromara.dynamictp.common.util.DefaultValueUtil.setIfZero; /** @@ -46,7 +48,7 @@ public class NotifyItem { private boolean enabled = true; /** - * Notify item, see {@link NotifyItemEnum} + * Notify item, see {@link CommonNotifyItemType} */ private String type; @@ -101,41 +103,46 @@ public class NotifyItem { } public static List getAllNotifyItems() { - NotifyItem rejectNotify = new NotifyItem(); - rejectNotify.setType(NotifyItemEnum.REJECT.getValue()); - - NotifyItem runTimeoutNotify = new NotifyItem(); - runTimeoutNotify.setType(NotifyItemEnum.RUN_TIMEOUT.getValue()); - - NotifyItem queueTimeoutNotify = new NotifyItem(); - queueTimeoutNotify.setType(NotifyItemEnum.QUEUE_TIMEOUT.getValue()); - List notifyItems = new ArrayList<>(6); notifyItems.addAll(getSimpleNotifyItems()); - notifyItems.add(rejectNotify); - notifyItems.add(runTimeoutNotify); - notifyItems.add(queueTimeoutNotify); - + notifyItems.addAll(getFrameworkWithoutSimpleNotifyTypes()); + List customNotifyItems = getCustomNotifyItems(); + if (CollUtil.isNotEmpty(customNotifyItems)) { + notifyItems.addAll(customNotifyItems); + } populateDefaultValues(notifyItems); return notifyItems; } - public static List getSimpleNotifyItems() { - NotifyItem changeNotify = new NotifyItem(); - changeNotify.setType(NotifyItemEnum.CHANGE.getValue()); - changeNotify.setSilencePeriod(1); - - NotifyItem livenessNotify = new NotifyItem(); - livenessNotify.setType(NotifyItemEnum.LIVENESS.getValue()); - - NotifyItem capacityNotify = new NotifyItem(); - capacityNotify.setType(NotifyItemEnum.CAPACITY.getValue()); + public static List getCustomNotifyItems() { + return NotifyItemTypeRegistry.getCustomNotifyTypes().stream() + .map(ele -> { + NotifyItem notifyItem = new NotifyItem(); + notifyItem.setType(ele.getNotifyType()); + return notifyItem; + }).collect(toList()); + } + public static List getSimpleNotifyItems() { List notifyItems = new ArrayList<>(3); - notifyItems.add(livenessNotify); - notifyItems.add(changeNotify); - notifyItems.add(capacityNotify); + NotifyItemTypeRegistry.getSimpleNotifyTypes().forEach(item -> { + NotifyItem notifyItem = new NotifyItem(); + notifyItem.setType(item.getType()); + if (item.getType().equals(CHANGE)) { + notifyItem.setSilencePeriod(1); + } + notifyItems.add(notifyItem); + }); + return notifyItems; + } + public static List getFrameworkWithoutSimpleNotifyTypes() { + List notifyItems = new ArrayList<>(3); + NotifyItemTypeRegistry.getFrameworkNotifyWithoutSimpleTypes().forEach(item -> { + NotifyItem notifyItem = new NotifyItem(); + notifyItem.setType(item.getType()); + notifyItems.add(notifyItem); + }); return notifyItems; } @@ -144,8 +151,9 @@ public class NotifyItem { return; } for (NotifyItem item : source) { - NotifyItemEnum itemEnum = NotifyItemEnum.of(item.getType()); - switch (Objects.requireNonNull(itemEnum)) { + CommonNotifyItemType notifyItemType = NotifyItemTypeRegistry.getNotifyItemType(item.getType()); + assert notifyItemType != null; + switch (notifyItemType.getType()) { case REJECT: setIfZero(item::getCount, item::setCount, 1); break; diff --git a/common/src/main/java/org/dromara/dynamictp/common/entity/TpExecutorProps.java b/common/src/main/java/org/dromara/dynamictp/common/entity/TpExecutorProps.java index 39bfdfbd..5815bb49 100644 --- a/common/src/main/java/org/dromara/dynamictp/common/entity/TpExecutorProps.java +++ b/common/src/main/java/org/dromara/dynamictp/common/entity/TpExecutorProps.java @@ -19,8 +19,8 @@ package org.dromara.dynamictp.common.entity; import lombok.Data; import org.dromara.dynamictp.common.constant.DynamicTpConst; -import org.dromara.dynamictp.common.em.NotifyItemEnum; import org.dromara.dynamictp.common.em.RejectedTypeEnum; +import org.dromara.dynamictp.common.notifier.type.NotifyItemTypeRegistry; import java.util.List; import java.util.Set; @@ -98,7 +98,7 @@ public class TpExecutorProps { private boolean allowCoreThreadTimeOut = false; /** - * Notify items, see {@link NotifyItemEnum} + * Notify items, see {@link NotifyItemTypeRegistry} */ private List notifyItems; diff --git a/common/src/main/java/org/dromara/dynamictp/common/notifier/type/AbstractCustomAlarmType.java b/common/src/main/java/org/dromara/dynamictp/common/notifier/type/AbstractCustomAlarmType.java new file mode 100644 index 00000000..c51efbfe --- /dev/null +++ b/common/src/main/java/org/dromara/dynamictp/common/notifier/type/AbstractCustomAlarmType.java @@ -0,0 +1,34 @@ +/* + * 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.common.notifier.type; + + +import com.google.common.collect.Lists; +import org.dromara.dynamictp.common.em.NotifyItemTypeGroupEnum; + +public abstract class AbstractCustomAlarmType extends CommonNotifyItemType { + + public AbstractCustomAlarmType() { + super(Lists.newArrayList(NotifyItemTypeGroupEnum.CUSTOM)); + } + + /** + * custom business notify type + */ + public abstract String getNotifyType(); +} diff --git a/common/src/main/java/org/dromara/dynamictp/common/notifier/type/CommonNotifyItemType.java b/common/src/main/java/org/dromara/dynamictp/common/notifier/type/CommonNotifyItemType.java new file mode 100644 index 00000000..41428151 --- /dev/null +++ b/common/src/main/java/org/dromara/dynamictp/common/notifier/type/CommonNotifyItemType.java @@ -0,0 +1,68 @@ +/* + * 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.common.notifier.type; + +import cn.hutool.core.collection.CollUtil; +import lombok.Getter; +import org.dromara.dynamictp.common.em.NotifyItemTypeGroupEnum; + +import java.util.HashSet; +import java.util.List; +import java.util.Objects; +import java.util.Set; + +@Getter +public class CommonNotifyItemType { + private String type; + private final Set groupEnums = new HashSet<>(); + + public CommonNotifyItemType(List groupEnums) { + new CommonNotifyItemType("", groupEnums); + } + + public CommonNotifyItemType(String type, List groupEnums) { + this.type = type; + if (CollUtil.isNotEmpty(groupEnums)) { + this.groupEnums.addAll(groupEnums); + } + } + + protected void setType(String type) { + this.type = type; + } + + public Set getGroupEnums() { + return groupEnums; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + CommonNotifyItemType that = (CommonNotifyItemType) o; + return Objects.equals(type, that.type); + } + + @Override + public int hashCode() { + return Objects.hashCode(type); + } +} diff --git a/common/src/main/java/org/dromara/dynamictp/common/notifier/type/NotifyItemTypeRegistry.java b/common/src/main/java/org/dromara/dynamictp/common/notifier/type/NotifyItemTypeRegistry.java new file mode 100644 index 00000000..35b04799 --- /dev/null +++ b/common/src/main/java/org/dromara/dynamictp/common/notifier/type/NotifyItemTypeRegistry.java @@ -0,0 +1,109 @@ +/* + * 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.common.notifier.type; + +import cn.hutool.core.collection.CollUtil; +import com.google.common.collect.Lists; +import org.dromara.dynamictp.common.em.NotifyItemTypeGroupEnum; +import org.dromara.dynamictp.common.util.ExtensionServiceLoader; + +import java.util.List; +import java.util.stream.Collectors; + +import static org.dromara.dynamictp.common.constant.DynamicTpConst.*; + +public class NotifyItemTypeRegistry { + + // notify by schedule + private static final List SCHEDULE_NOTIFY_TYPES; + private static final List SIMPLE_NOTIFY_TYPES; + private static final List FRAMEWORK_NOTIFY_WITHOUT_SIMPLE_TYPES; + // dynamic tp core notify + private static final List FRAMEWORK_NOTIFY_TYPES; + // custom extension notify + private static final List CUSTOM_NOTIFY_TYPES = Lists.newArrayListWithCapacity(0); + + static { + SCHEDULE_NOTIFY_TYPES = Lists.newArrayList(LIVENESS, CAPACITY) + .stream() + .map(ele -> new CommonNotifyItemType(ele, Lists.newArrayList(NotifyItemTypeGroupEnum.SCHEDULE))) + .collect(Collectors.toList()); + + SIMPLE_NOTIFY_TYPES = Lists.newArrayList(CHANGE) + .stream() + .map(ele -> new CommonNotifyItemType(ele, Lists.newArrayList(NotifyItemTypeGroupEnum.SIMPLE))) + .collect(Collectors.toList()); + SCHEDULE_NOTIFY_TYPES.forEach(ele -> { + ele.getGroupEnums().add(NotifyItemTypeGroupEnum.SIMPLE); + SIMPLE_NOTIFY_TYPES.add(ele); + }); + + FRAMEWORK_NOTIFY_WITHOUT_SIMPLE_TYPES = Lists.newArrayList(REJECT, RUN_TIMEOUT, QUEUE_TIMEOUT) + .stream() + .map(ele -> new CommonNotifyItemType(ele, Lists.newArrayList(NotifyItemTypeGroupEnum.FRAMEWORK_WITHOUT_SIMPLE))) + .collect(Collectors.toList()); + + FRAMEWORK_NOTIFY_TYPES = Lists.newArrayListWithCapacity(SIMPLE_NOTIFY_TYPES.size() + FRAMEWORK_NOTIFY_WITHOUT_SIMPLE_TYPES.size()); + FRAMEWORK_NOTIFY_TYPES.addAll(SIMPLE_NOTIFY_TYPES); + FRAMEWORK_NOTIFY_TYPES.addAll(FRAMEWORK_NOTIFY_WITHOUT_SIMPLE_TYPES); + + List customAlamType = ExtensionServiceLoader.get(AbstractCustomAlarmType.class); + if (CollUtil.isNotEmpty(customAlamType)) { + customAlamType.forEach(ele -> { + ele.setType(ele.getNotifyType()); + ele.getGroupEnums().add(NotifyItemTypeGroupEnum.CUSTOM); + CUSTOM_NOTIFY_TYPES.add(ele); + }); + } + } + + public static List getScheduleNotifyTypes() { + return SCHEDULE_NOTIFY_TYPES; + } + + public static List getSimpleNotifyTypes() { + return SIMPLE_NOTIFY_TYPES; + } + + public static List getFrameworkNotifyWithoutSimpleTypes() { + return FRAMEWORK_NOTIFY_WITHOUT_SIMPLE_TYPES; + } + + public static List getFrameworkNotifyTypes() { + return FRAMEWORK_NOTIFY_TYPES; + } + + public static List getCustomNotifyTypes() { + return CUSTOM_NOTIFY_TYPES; + } + + public static CommonNotifyItemType getNotifyItemType(String type) { + CommonNotifyItemType commonNotifyItemType = FRAMEWORK_NOTIFY_TYPES.stream() + .filter(ele -> ele.getType().equals(type)) + .findFirst() + .orElse(null); + if (commonNotifyItemType == null) { + // search from custom + commonNotifyItemType = CUSTOM_NOTIFY_TYPES.stream() + .filter(ele -> ele.getType().equals(type)) + .findFirst() + .orElse(null); + } + return commonNotifyItemType; + } + +} diff --git a/core/src/main/java/org/dromara/dynamictp/core/aware/TaskRejectAware.java b/core/src/main/java/org/dromara/dynamictp/core/aware/TaskRejectAware.java index ffb0af7b..915d7889 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/aware/TaskRejectAware.java +++ b/core/src/main/java/org/dromara/dynamictp/core/aware/TaskRejectAware.java @@ -27,8 +27,8 @@ import org.slf4j.MDC; import java.util.Objects; import java.util.concurrent.Executor; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.REJECT; import static org.dromara.dynamictp.common.constant.DynamicTpConst.TRACE_ID; -import static org.dromara.dynamictp.common.em.NotifyItemEnum.REJECT; /** * TaskRejectAware related diff --git a/core/src/main/java/org/dromara/dynamictp/core/executor/DtpExecutor.java b/core/src/main/java/org/dromara/dynamictp/core/executor/DtpExecutor.java index 25be905f..71b084b3 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/executor/DtpExecutor.java +++ b/core/src/main/java/org/dromara/dynamictp/core/executor/DtpExecutor.java @@ -21,8 +21,8 @@ import com.google.common.collect.Lists; import com.google.common.collect.Sets; import lombok.extern.slf4j.Slf4j; import lombok.val; -import org.dromara.dynamictp.common.em.NotifyItemEnum; import org.dromara.dynamictp.common.entity.NotifyItem; +import org.dromara.dynamictp.common.notifier.type.NotifyItemTypeRegistry; import org.dromara.dynamictp.common.util.ExecutorUtil; import org.dromara.dynamictp.core.aware.AwareManager; import org.dromara.dynamictp.core.aware.TaskEnhanceAware; @@ -65,7 +65,7 @@ public class DtpExecutor extends ThreadPoolExecutor implements TaskEnhanceAware, private boolean notifyEnabled = true; /** - * Notify items, see {@link NotifyItemEnum}. + * Notify items, see {@link NotifyItemTypeRegistry}. */ private List notifyItems; diff --git a/core/src/main/java/org/dromara/dynamictp/core/handler/NotifierHandler.java b/core/src/main/java/org/dromara/dynamictp/core/handler/NotifierHandler.java index 0a92d825..e0d0318d 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/handler/NotifierHandler.java +++ b/core/src/main/java/org/dromara/dynamictp/core/handler/NotifierHandler.java @@ -17,10 +17,12 @@ package org.dromara.dynamictp.core.handler; +import cn.hutool.core.collection.CollUtil; import lombok.extern.slf4j.Slf4j; -import org.dromara.dynamictp.common.em.NotifyItemEnum; import org.dromara.dynamictp.common.entity.NotifyItem; +import org.dromara.dynamictp.common.entity.NotifyPlatform; import org.dromara.dynamictp.common.entity.TpMainFields; +import org.dromara.dynamictp.common.notifier.type.CommonNotifyItemType; import org.dromara.dynamictp.common.util.ExtensionServiceLoader; import org.dromara.dynamictp.core.notifier.DtpDingNotifier; import org.dromara.dynamictp.core.notifier.DtpLarkNotifier; @@ -29,6 +31,7 @@ import org.dromara.dynamictp.core.notifier.DtpWechatNotifier; import org.dromara.dynamictp.common.notifier.DingNotifier; import org.dromara.dynamictp.common.notifier.LarkNotifier; import org.dromara.dynamictp.common.notifier.WechatNotifier; +import org.dromara.dynamictp.core.notifier.context.CustomAlarmCtx; import org.dromara.dynamictp.core.notifier.context.DtpNotifyCtxHolder; import org.dromara.dynamictp.core.notifier.manager.NotifyHelper; @@ -36,6 +39,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import static org.dromara.dynamictp.common.em.NotifyItemTypeGroupEnum.CUSTOM; + /** * NotifierHandler related * @@ -71,17 +76,26 @@ public final class NotifierHandler { } } - public void sendAlarm(NotifyItemEnum notifyItemEnum) { + public void sendAlarm(CommonNotifyItemType notifyItemType) { NotifyItem notifyItem = DtpNotifyCtxHolder.get().getNotifyItem(); + boolean customFlag = CollUtil.contains(notifyItemType.getGroupEnums(), CUSTOM); for (String platformId : notifyItem.getPlatformIds()) { NotifyHelper.getPlatform(platformId).ifPresent(p -> { DtpNotifier notifier = NOTIFIERS.get(p.getPlatform().toLowerCase()); if (notifier != null) { - notifier.sendAlarmMsg(p, notifyItemEnum); + doSendAlarmMsg(customFlag, notifier, p, notifyItemType); } }); } } + private void doSendAlarmMsg(boolean isCustom, DtpNotifier notifier, NotifyPlatform platform, CommonNotifyItemType notifyItemType) { + if (isCustom) { + CustomAlarmCtx context = (CustomAlarmCtx) DtpNotifyCtxHolder.get(); + notifier.sendCustomAlarmMsg(platform, notifyItemType, context.getCustomAlarmParam()); + } else { + notifier.sendAlarmMsg(platform, notifyItemType); + } + } public static NotifierHandler getInstance() { return NotifierHandlerHolder.INSTANCE; diff --git a/core/src/main/java/org/dromara/dynamictp/core/monitor/DtpMonitor.java b/core/src/main/java/org/dromara/dynamictp/core/monitor/DtpMonitor.java index 5d248bf7..88f7ce80 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/monitor/DtpMonitor.java +++ b/core/src/main/java/org/dromara/dynamictp/core/monitor/DtpMonitor.java @@ -24,6 +24,7 @@ import org.dromara.dynamictp.common.event.AlarmCheckEvent; import org.dromara.dynamictp.common.event.CollectEvent; import org.dromara.dynamictp.common.event.CustomContextRefreshedEvent; import org.dromara.dynamictp.common.manager.EventBusManager; +import org.dromara.dynamictp.common.notifier.type.NotifyItemTypeRegistry; import org.dromara.dynamictp.common.properties.DtpProperties; import org.dromara.dynamictp.core.DtpRegistry; import org.dromara.dynamictp.core.converter.ExecutorConverter; @@ -37,7 +38,6 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.SCHEDULE_NOTIFY_ITEMS; /** * DtpMonitor related @@ -92,7 +92,7 @@ public class DtpMonitor { private void checkAlarm(Set executorNames) { executorNames.forEach(name -> { ExecutorWrapper wrapper = DtpRegistry.getExecutorWrapper(name); - AlarmManager.checkAndTryAlarmAsync(wrapper, SCHEDULE_NOTIFY_ITEMS); + AlarmManager.checkAndTryAlarmAsync(wrapper, NotifyItemTypeRegistry.getScheduleNotifyTypes()); }); publishAlarmCheckEvent(); } diff --git a/core/src/main/java/org/dromara/dynamictp/core/notifier/AbstractDtpNotifier.java b/core/src/main/java/org/dromara/dynamictp/core/notifier/AbstractDtpNotifier.java index aad39bec..c29566bc 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/notifier/AbstractDtpNotifier.java +++ b/core/src/main/java/org/dromara/dynamictp/core/notifier/AbstractDtpNotifier.java @@ -23,11 +23,12 @@ import lombok.extern.slf4j.Slf4j; import lombok.val; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.Pair; -import org.dromara.dynamictp.common.em.NotifyItemEnum; +import org.dromara.dynamictp.common.entity.CustomNotifyParam; import org.dromara.dynamictp.common.entity.NotifyItem; import org.dromara.dynamictp.common.entity.NotifyPlatform; import org.dromara.dynamictp.common.entity.TpMainFields; import org.dromara.dynamictp.common.notifier.Notifier; +import org.dromara.dynamictp.common.notifier.type.CommonNotifyItemType; import org.dromara.dynamictp.common.util.CommonUtil; import org.dromara.dynamictp.common.util.DateUtil; import org.dromara.dynamictp.core.notifier.alarm.AlarmCounter; @@ -45,8 +46,7 @@ import java.util.Optional; import java.util.Set; import java.util.concurrent.TimeUnit; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.TRACE_ID; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.UNKNOWN; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.*; import static org.dromara.dynamictp.core.notifier.manager.NotifyHelper.getAlarmKeys; import static org.dromara.dynamictp.core.notifier.manager.NotifyHelper.getAllAlarmKeys; @@ -78,7 +78,7 @@ public abstract class AbstractDtpNotifier implements DtpNotifier { } @Override - public void sendAlarmMsg(NotifyPlatform notifyPlatform, NotifyItemEnum notifyItemEnum) { + public void sendAlarmMsg(NotifyPlatform notifyPlatform, CommonNotifyItemType notifyItemEnum) { String content = buildAlarmContent(notifyPlatform, notifyItemEnum); if (StringUtils.isBlank(content)) { log.debug("Alarm content is empty, ignore send alarm message."); @@ -87,7 +87,13 @@ public abstract class AbstractDtpNotifier implements DtpNotifier { notifier.send(newTargetPlatform(notifyPlatform), content); } - protected String buildAlarmContent(NotifyPlatform platform, NotifyItemEnum notifyItemEnum) { + @Override + public void sendCustomAlarmMsg(NotifyPlatform notifyPlatform, CommonNotifyItemType notifyItemType, CustomNotifyParam customAlarmParam) { + // give a chance to override alarm content + log.warn("{} Custom alarm content is empty, ignore send custom alarm message.", notifyItemType.getType()); + } + + protected String buildAlarmContent(NotifyPlatform platform, CommonNotifyItemType notifyItemType) { AlarmCtx context = (AlarmCtx) DtpNotifyCtxHolder.get(); ExecutorWrapper executorWrapper = context.getExecutorWrapper(); NotifyItem notifyItem = context.getNotifyItem(); @@ -103,7 +109,7 @@ public abstract class AbstractDtpNotifier implements DtpNotifier { CommonUtil.getInstance().getIp() + ":" + CommonUtil.getInstance().getPort(), CommonUtil.getInstance().getEnv(), populatePoolName(executorWrapper), - populateAlarmItem(notifyItemEnum, notifyItem, executorWrapper), + populateAlarmItem(notifyItemType, notifyItem, executorWrapper), alarmValue, executor.getCorePoolSize(), executor.getMaximumPoolSize(), @@ -129,7 +135,7 @@ public abstract class AbstractDtpNotifier implements DtpNotifier { getTraceInfo(), getExtInfo() ); - return highlightAlarmContent(content, notifyItemEnum); + return highlightAlarmContent(content, notifyItemType); } protected String buildNoticeContent(NotifyPlatform platform, TpMainFields oldFields, List diffs) { @@ -201,9 +207,9 @@ public abstract class AbstractDtpNotifier implements DtpNotifier { return executorWrapper.getThreadPoolName() + " (" + poolAlisaName + ")"; } - protected String populateAlarmItem(NotifyItemEnum notifyType, NotifyItem notifyItem, ExecutorWrapper executorWrapper) { + protected String populateAlarmItem(CommonNotifyItemType notifyType, NotifyItem notifyItem, ExecutorWrapper executorWrapper) { String suffix = StringUtils.EMPTY; - switch (notifyType) { + switch (notifyType.getType()) { case RUN_TIMEOUT: suffix = " (" + executorWrapper.getThreadPoolStatProvider().getRunTimeout() + "ms)"; break; @@ -217,7 +223,7 @@ public abstract class AbstractDtpNotifier implements DtpNotifier { default: break; } - return notifyType.getValue() + suffix; + return notifyType.getType() + suffix; } private String highlightNotifyContent(String content, List diffs) { @@ -235,12 +241,12 @@ public abstract class AbstractDtpNotifier implements DtpNotifier { return content; } - private String highlightAlarmContent(String content, NotifyItemEnum notifyItemEnum) { + private String highlightAlarmContent(String content, CommonNotifyItemType notifyItemType) { if (StringUtils.isBlank(content) || Objects.isNull(getColors())) { return content; } - Set colorKeys = getAlarmKeys(notifyItemEnum); + Set colorKeys = getAlarmKeys(notifyItemType); Pair pair = getColors(); for (String field : colorKeys) { content = content.replace(field, pair.getLeft()); diff --git a/core/src/main/java/org/dromara/dynamictp/core/notifier/DtpNotifier.java b/core/src/main/java/org/dromara/dynamictp/core/notifier/DtpNotifier.java index f1e804e9..43f17d4a 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/notifier/DtpNotifier.java +++ b/core/src/main/java/org/dromara/dynamictp/core/notifier/DtpNotifier.java @@ -17,9 +17,10 @@ package org.dromara.dynamictp.core.notifier; +import org.dromara.dynamictp.common.entity.CustomNotifyParam; import org.dromara.dynamictp.common.entity.TpMainFields; -import org.dromara.dynamictp.common.em.NotifyItemEnum; import org.dromara.dynamictp.common.entity.NotifyPlatform; +import org.dromara.dynamictp.common.notifier.type.CommonNotifyItemType; import java.util.List; @@ -53,5 +54,13 @@ public interface DtpNotifier { * @param notifyPlatform notify platform * @param notifyItemEnum notify item enum */ - void sendAlarmMsg(NotifyPlatform notifyPlatform, NotifyItemEnum notifyItemEnum); + void sendAlarmMsg(NotifyPlatform notifyPlatform, CommonNotifyItemType notifyItemEnum); + + /** + * Send custom alarm message. + * @param notifyPlatform notify platform + * @param notifyItemType notify item type + * @param customAlarmParam custom alarm params + */ + void sendCustomAlarmMsg(NotifyPlatform notifyPlatform, CommonNotifyItemType notifyItemType, CustomNotifyParam customAlarmParam); } diff --git a/core/src/main/java/org/dromara/dynamictp/core/notifier/alarm/AlarmCounter.java b/core/src/main/java/org/dromara/dynamictp/core/notifier/alarm/AlarmCounter.java index bfab77e1..9556b396 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/notifier/alarm/AlarmCounter.java +++ b/core/src/main/java/org/dromara/dynamictp/core/notifier/alarm/AlarmCounter.java @@ -20,10 +20,10 @@ package org.dromara.dynamictp.core.notifier.alarm; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import lombok.val; -import org.dromara.dynamictp.common.em.NotifyItemEnum; import org.dromara.dynamictp.common.entity.AlarmInfo; import org.dromara.dynamictp.common.entity.NotifyItem; import org.dromara.dynamictp.common.ex.DtpException; +import org.dromara.dynamictp.common.notifier.type.NotifyItemTypeRegistry; import org.dromara.dynamictp.common.util.DateUtil; import java.util.Map; @@ -31,6 +31,8 @@ import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.CHANGE; + /** * AlarmCounter related * @@ -46,7 +48,7 @@ public class AlarmCounter { private AlarmCounter() { } public static void initAlarmCounter(String threadPoolName, NotifyItem notifyItem) { - if (NotifyItemEnum.CHANGE.getValue().equalsIgnoreCase(notifyItem.getType())) { + if (CHANGE.equalsIgnoreCase(notifyItem.getType())) { return; } @@ -78,7 +80,7 @@ public class AlarmCounter { AlarmInfo alarmInfo = getAlarmInfo(threadPoolName, notifyType); if (Objects.isNull(alarmInfo)) { String key = buildKey(threadPoolName, notifyType); - alarmInfo = new AlarmInfo().setNotifyItem(NotifyItemEnum.of(notifyType)); + alarmInfo = new AlarmInfo().setNotifyItem(NotifyItemTypeRegistry.getNotifyItemType(notifyType)); ALARM_INFO_CACHE.get(key).put(notifyType, alarmInfo); } alarmInfo.incCounter(); diff --git a/core/src/main/java/org/dromara/dynamictp/core/notifier/alarm/AlarmLimiter.java b/core/src/main/java/org/dromara/dynamictp/core/notifier/alarm/AlarmLimiter.java index fb6274c0..ba8d7e41 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/notifier/alarm/AlarmLimiter.java +++ b/core/src/main/java/org/dromara/dynamictp/core/notifier/alarm/AlarmLimiter.java @@ -21,7 +21,6 @@ import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import lombok.val; import org.apache.commons.lang3.StringUtils; -import org.dromara.dynamictp.common.em.NotifyItemEnum; import org.dromara.dynamictp.common.entity.NotifyItem; import java.util.Map; @@ -29,6 +28,8 @@ import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.CHANGE; + /** * AlarmLimiter related * @@ -42,7 +43,7 @@ public class AlarmLimiter { private AlarmLimiter() { } public static void initAlarmLimiter(String threadPoolName, NotifyItem notifyItem) { - if (NotifyItemEnum.CHANGE.getValue().equalsIgnoreCase(notifyItem.getType())) { + if (CHANGE.equalsIgnoreCase(notifyItem.getType())) { return; } diff --git a/core/src/main/java/org/dromara/dynamictp/core/notifier/capture/CapturedExecutor.java b/core/src/main/java/org/dromara/dynamictp/core/notifier/capture/CapturedExecutor.java index 13a59e1a..796da525 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/notifier/capture/CapturedExecutor.java +++ b/core/src/main/java/org/dromara/dynamictp/core/notifier/capture/CapturedExecutor.java @@ -17,8 +17,8 @@ package org.dromara.dynamictp.core.notifier.capture; -import org.dromara.dynamictp.common.em.NotifyItemEnum; import org.dromara.dynamictp.common.entity.NotifyPlatform; +import org.dromara.dynamictp.common.notifier.type.CommonNotifyItemType; import org.dromara.dynamictp.core.executor.DtpExecutor; import org.dromara.dynamictp.core.notifier.AbstractDtpNotifier; import org.dromara.dynamictp.core.notifier.context.BaseNotifyCtx; @@ -34,7 +34,7 @@ import java.util.concurrent.TimeUnit; * is to capture DtpExecutor's status when construct {@link BaseNotifyCtx} during {@link AlarmManager#doTryAlarm}. *

* In other words, this can ensure that the thread pool status when the alarm threshold is triggered is - * consistent with the content in the {@link AbstractDtpNotifier#buildAlarmContent(NotifyPlatform, NotifyItemEnum)} + * consistent with the content in the {@link AbstractDtpNotifier#buildAlarmContent(NotifyPlatform, CommonNotifyItemType)} * * @author ruoan * @since 1.1.3 diff --git a/core/src/main/java/org/dromara/dynamictp/core/notifier/chain/invoker/AlarmInvoker.java b/core/src/main/java/org/dromara/dynamictp/core/notifier/chain/invoker/AlarmInvoker.java index 8d9a5462..1fc0cca6 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/notifier/chain/invoker/AlarmInvoker.java +++ b/core/src/main/java/org/dromara/dynamictp/core/notifier/chain/invoker/AlarmInvoker.java @@ -18,7 +18,7 @@ package org.dromara.dynamictp.core.notifier.chain.invoker; import lombok.val; -import org.dromara.dynamictp.common.em.NotifyItemEnum; +import org.dromara.dynamictp.common.notifier.type.NotifyItemTypeRegistry; import org.dromara.dynamictp.common.pattern.filter.Invoker; import org.dromara.dynamictp.core.handler.NotifierHandler; import org.dromara.dynamictp.core.notifier.alarm.AlarmCounter; @@ -39,7 +39,7 @@ public class AlarmInvoker implements Invoker { val notifyItem = context.getNotifyItem(); try { DtpNotifyCtxHolder.set(context); - NotifierHandler.getInstance().sendAlarm(NotifyItemEnum.of(notifyItem.getType())); + NotifierHandler.getInstance().sendAlarm(NotifyItemTypeRegistry.getNotifyItemType(notifyItem.getType())); AlarmCounter.reset(executorWrapper.getThreadPoolName(), notifyItem.getType()); } finally { DtpNotifyCtxHolder.remove(); diff --git a/core/src/main/java/org/dromara/dynamictp/core/notifier/context/BaseNotifyCtx.java b/core/src/main/java/org/dromara/dynamictp/core/notifier/context/BaseNotifyCtx.java index 432d42c1..719b4257 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/notifier/context/BaseNotifyCtx.java +++ b/core/src/main/java/org/dromara/dynamictp/core/notifier/context/BaseNotifyCtx.java @@ -17,8 +17,9 @@ package org.dromara.dynamictp.core.notifier.context; -import org.dromara.dynamictp.common.em.NotifyItemEnum; import org.dromara.dynamictp.common.entity.NotifyItem; +import org.dromara.dynamictp.common.notifier.type.CommonNotifyItemType; +import org.dromara.dynamictp.common.notifier.type.NotifyItemTypeRegistry; import org.dromara.dynamictp.core.support.ExecutorWrapper; import lombok.Data; @@ -42,7 +43,7 @@ public class BaseNotifyCtx { this.notifyItem = notifyItem; } - public NotifyItemEnum getNotifyItemEnum() { - return NotifyItemEnum.of(notifyItem.getType()); + public CommonNotifyItemType getNotifyItemType() { + return NotifyItemTypeRegistry.getNotifyItemType(notifyItem.getType()); } } diff --git a/core/src/main/java/org/dromara/dynamictp/core/notifier/context/CustomAlarmCtx.java b/core/src/main/java/org/dromara/dynamictp/core/notifier/context/CustomAlarmCtx.java new file mode 100644 index 00000000..e01031b2 --- /dev/null +++ b/core/src/main/java/org/dromara/dynamictp/core/notifier/context/CustomAlarmCtx.java @@ -0,0 +1,39 @@ +/* + * 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.notifier.context; + +import org.dromara.dynamictp.common.entity.CustomNotifyParam; +import org.dromara.dynamictp.common.entity.NotifyItem; +import org.dromara.dynamictp.core.support.ExecutorWrapper; + + +public class CustomAlarmCtx extends AlarmCtx { + + private CustomNotifyParam customAlarmParam; + + public CustomAlarmCtx(ExecutorWrapper wrapper, NotifyItem notifyItem) { + super(wrapper, notifyItem); + } + + public CustomNotifyParam getCustomAlarmParam() { + return customAlarmParam; + } + + public void setCustomAlarmParam(CustomNotifyParam customAlarmParam) { + this.customAlarmParam = customAlarmParam; + } +} diff --git a/core/src/main/java/org/dromara/dynamictp/core/notifier/manager/AlarmManager.java b/core/src/main/java/org/dromara/dynamictp/core/notifier/manager/AlarmManager.java index c9cc8834..7bf1c27c 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/notifier/manager/AlarmManager.java +++ b/core/src/main/java/org/dromara/dynamictp/core/notifier/manager/AlarmManager.java @@ -21,14 +21,16 @@ import cn.hutool.core.util.NumberUtil; import com.google.common.collect.Sets; import lombok.extern.slf4j.Slf4j; import lombok.val; -import org.dromara.dynamictp.common.em.NotifyItemEnum; import org.dromara.dynamictp.common.em.RejectedTypeEnum; +import org.dromara.dynamictp.common.entity.CustomNotifyParam; import org.dromara.dynamictp.common.entity.NotifyItem; +import org.dromara.dynamictp.common.notifier.type.CommonNotifyItemType; import org.dromara.dynamictp.common.pattern.filter.InvokerChain; import org.dromara.dynamictp.core.notifier.alarm.AlarmCounter; import org.dromara.dynamictp.core.notifier.alarm.AlarmLimiter; import org.dromara.dynamictp.core.notifier.context.AlarmCtx; import org.dromara.dynamictp.core.notifier.context.BaseNotifyCtx; +import org.dromara.dynamictp.core.notifier.context.CustomAlarmCtx; import org.dromara.dynamictp.core.support.ExecutorWrapper; import org.dromara.dynamictp.core.support.ThreadPoolBuilder; import org.dromara.dynamictp.core.support.task.runnable.DtpRunnable; @@ -38,7 +40,7 @@ import org.slf4j.MDC; import java.util.List; import java.util.concurrent.ExecutorService; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.TRACE_ID; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.*; import static org.dromara.dynamictp.common.em.QueueTypeEnum.LINKED_BLOCKING_QUEUE; /** @@ -85,7 +87,7 @@ public class AlarmManager { AlarmCounter.initAlarmCounter(poolName, notifyItem); } - public static void tryAlarmAsync(ExecutorWrapper executorWrapper, NotifyItemEnum notifyType, Runnable runnable) { + public static void tryAlarmAsync(ExecutorWrapper executorWrapper, String notifyType, Runnable runnable) { preAlarm(runnable); try { ALARM_EXECUTOR.execute(() -> doTryAlarm(executorWrapper, notifyType)); @@ -94,23 +96,44 @@ public class AlarmManager { } } - public static void checkAndTryAlarmAsync(ExecutorWrapper executorWrapper, List notifyTypes) { + public static void checkAndTryAlarmAsync(ExecutorWrapper executorWrapper, List notifyTypes) { ALARM_EXECUTOR.execute(() -> notifyTypes.forEach(x -> doCheckAndTryAlarm(executorWrapper, x))); } - public static void doCheckAndTryAlarm(ExecutorWrapper executorWrapper, NotifyItemEnum notifyType) { - NotifyHelper.getNotifyItem(executorWrapper, notifyType).ifPresent(notifyItem -> { + public static void doCheckAndTryAlarm(ExecutorWrapper executorWrapper, CommonNotifyItemType notifyType) { + NotifyHelper.getNotifyItem(executorWrapper, notifyType.getType()).ifPresent(notifyItem -> { if (hasReachedThreshold(executorWrapper, notifyType, notifyItem)) { ALARM_INVOKER_CHAIN.proceed(new AlarmCtx(executorWrapper, notifyItem)); } }); } - public static void tryAlarmAsync(ExecutorWrapper executorWrapper, List notifyTypes) { - ALARM_EXECUTOR.execute(() -> notifyTypes.forEach(x -> doTryAlarm(executorWrapper, x))); + public static void tryAlarmAsync(ExecutorWrapper executorWrapper, List notifyTypes) { + ALARM_EXECUTOR.execute(() -> notifyTypes.forEach(x -> doTryAlarm(executorWrapper, x.getType()))); + } + + /** + * try to send custom alarm + */ + public static void tryCustomAlarmAsync(ExecutorWrapper executorWrapper, String customAlarmType, Runnable runnable, + CustomNotifyParam customNotifyParam) { + preAlarm(runnable); + try { + ALARM_EXECUTOR.execute(() -> doTryCustomAlarm(executorWrapper, customAlarmType, customNotifyParam)); + } finally { + postAlarm(runnable); + } + } + + public static void doTryCustomAlarm(ExecutorWrapper executorWrapper, String customAlarmType, CustomNotifyParam customNotifyParam) { + NotifyHelper.getNotifyItem(executorWrapper, customAlarmType).ifPresent(notifyItem -> { + CustomAlarmCtx customAlarmCtx = new CustomAlarmCtx(executorWrapper, notifyItem); + customAlarmCtx.setCustomAlarmParam(customNotifyParam); + ALARM_INVOKER_CHAIN.proceed(customAlarmCtx); + }); } - public static void doTryAlarm(ExecutorWrapper executorWrapper, NotifyItemEnum notifyType) { + public static void doTryAlarm(ExecutorWrapper executorWrapper, String notifyType) { NotifyHelper.getNotifyItem(executorWrapper, notifyType).ifPresent(notifyItem -> { val alarmCtx = new AlarmCtx(executorWrapper, notifyItem); ALARM_INVOKER_CHAIN.proceed(alarmCtx); @@ -138,8 +161,8 @@ public class AlarmManager { * @param notifyItem the notify item * @return true if the threshold is reached, false otherwise */ - private static boolean hasReachedThreshold(ExecutorWrapper executor, NotifyItemEnum notifyType, NotifyItem notifyItem) { - switch (notifyType) { + private static boolean hasReachedThreshold(ExecutorWrapper executor, CommonNotifyItemType notifyType, NotifyItem notifyItem) { + switch (notifyType.getType()) { case CAPACITY: return checkCapacity(executor, notifyItem); case LIVENESS: diff --git a/core/src/main/java/org/dromara/dynamictp/core/notifier/manager/NoticeManager.java b/core/src/main/java/org/dromara/dynamictp/core/notifier/manager/NoticeManager.java index a833160b..bf6d8541 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/notifier/manager/NoticeManager.java +++ b/core/src/main/java/org/dromara/dynamictp/core/notifier/manager/NoticeManager.java @@ -29,7 +29,7 @@ import org.dromara.dynamictp.core.support.ThreadPoolBuilder; import java.util.List; import java.util.concurrent.ExecutorService; -import static org.dromara.dynamictp.common.em.NotifyItemEnum.CHANGE; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.CHANGE; import static org.dromara.dynamictp.common.em.QueueTypeEnum.LINKED_BLOCKING_QUEUE; /** diff --git a/core/src/main/java/org/dromara/dynamictp/core/notifier/manager/NotifyHelper.java b/core/src/main/java/org/dromara/dynamictp/core/notifier/manager/NotifyHelper.java index 1cd373b4..c483093b 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/notifier/manager/NotifyHelper.java +++ b/core/src/main/java/org/dromara/dynamictp/core/notifier/manager/NotifyHelper.java @@ -23,12 +23,12 @@ import com.google.common.collect.Sets; import lombok.extern.slf4j.Slf4j; import lombok.val; import org.apache.commons.collections4.CollectionUtils; -import org.dromara.dynamictp.common.em.NotifyItemEnum; import org.dromara.dynamictp.common.entity.DtpExecutorProps; import org.dromara.dynamictp.common.entity.NotifyItem; import org.dromara.dynamictp.common.entity.NotifyPlatform; import org.dromara.dynamictp.common.entity.TpExecutorProps; import org.dromara.dynamictp.common.manager.ContextManagerHelper; +import org.dromara.dynamictp.common.notifier.type.CommonNotifyItemType; import org.dromara.dynamictp.common.properties.DtpProperties; import org.dromara.dynamictp.common.util.StreamUtil; import org.dromara.dynamictp.core.executor.DtpExecutor; @@ -43,11 +43,11 @@ import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; -import static org.dromara.dynamictp.common.em.NotifyItemEnum.CAPACITY; -import static org.dromara.dynamictp.common.em.NotifyItemEnum.LIVENESS; -import static org.dromara.dynamictp.common.em.NotifyItemEnum.QUEUE_TIMEOUT; -import static org.dromara.dynamictp.common.em.NotifyItemEnum.REJECT; -import static org.dromara.dynamictp.common.em.NotifyItemEnum.RUN_TIMEOUT; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.CAPACITY; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.LIVENESS; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.QUEUE_TIMEOUT; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.REJECT; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.RUN_TIMEOUT; import static org.dromara.dynamictp.common.entity.NotifyItem.mergeAllNotifyItems; /** @@ -78,11 +78,11 @@ public class NotifyHelper { private static final Map> ALARM_KEYS = Maps.newHashMap(); static { - ALARM_KEYS.put(LIVENESS.name(), LIVENESS_ALARM_KEYS); - ALARM_KEYS.put(CAPACITY.name(), CAPACITY_ALARM_KEYS); - ALARM_KEYS.put(REJECT.name(), REJECT_ALARM_KEYS); - ALARM_KEYS.put(RUN_TIMEOUT.name(), RUN_TIMEOUT_ALARM_KEYS); - ALARM_KEYS.put(QUEUE_TIMEOUT.name(), QUEUE_TIMEOUT_ALARM_KEYS); + ALARM_KEYS.put(LIVENESS, LIVENESS_ALARM_KEYS); + ALARM_KEYS.put(CAPACITY, CAPACITY_ALARM_KEYS); + ALARM_KEYS.put(REJECT, REJECT_ALARM_KEYS); + ALARM_KEYS.put(RUN_TIMEOUT, RUN_TIMEOUT_ALARM_KEYS); + ALARM_KEYS.put(QUEUE_TIMEOUT, QUEUE_TIMEOUT_ALARM_KEYS); ALL_ALARM_KEYS = ALARM_KEYS.values().stream().flatMap(Collection::stream).collect(Collectors.toSet()); ALL_ALARM_KEYS.addAll(COMMON_ALARM_KEYS); @@ -95,18 +95,18 @@ public class NotifyHelper { return ALL_ALARM_KEYS; } - public static Set getAlarmKeys(NotifyItemEnum notifyItemEnum) { - val keys = ALARM_KEYS.get(notifyItemEnum.name()); + public static Set getAlarmKeys(CommonNotifyItemType notifyItemType) { + val keys = ALARM_KEYS.get(notifyItemType.getType()); keys.addAll(COMMON_ALARM_KEYS); return keys; } - public static Optional getNotifyItem(ExecutorWrapper executor, NotifyItemEnum notifyType) { + public static Optional getNotifyItem(ExecutorWrapper executor, String notifyType) { if (CollectionUtils.isEmpty(executor.getNotifyItems())) { return Optional.empty(); } return executor.getNotifyItems().stream() - .filter(x -> notifyType.getValue().equalsIgnoreCase(x.getType())) + .filter(x -> notifyType.equalsIgnoreCase(x.getType())) .findFirst(); } diff --git a/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java b/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java index 4145f0d6..a2d5f21a 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java +++ b/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java @@ -19,8 +19,8 @@ package org.dromara.dynamictp.core.support; import cn.hutool.core.bean.BeanUtil; import lombok.Data; -import org.dromara.dynamictp.common.em.NotifyItemEnum; import org.dromara.dynamictp.common.entity.NotifyItem; +import org.dromara.dynamictp.common.notifier.type.NotifyItemTypeRegistry; import org.dromara.dynamictp.core.aware.AwareManager; import org.dromara.dynamictp.core.aware.RejectHandlerAware; import org.dromara.dynamictp.core.aware.TaskEnhanceAware; @@ -65,7 +65,7 @@ public class ExecutorWrapper { private ExecutorAdapter executor; /** - * Notify items, see {@link NotifyItemEnum}. + * Notify items, see {@link NotifyItemTypeRegistry}. */ private List notifyItems; diff --git a/core/src/main/java/org/dromara/dynamictp/core/support/ThreadPoolBuilder.java b/core/src/main/java/org/dromara/dynamictp/core/support/ThreadPoolBuilder.java index c128ad87..574a31bd 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/support/ThreadPoolBuilder.java +++ b/core/src/main/java/org/dromara/dynamictp/core/support/ThreadPoolBuilder.java @@ -24,10 +24,10 @@ import com.google.common.collect.Lists; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.dromara.dynamictp.common.constant.DynamicTpConst; -import org.dromara.dynamictp.common.em.NotifyItemEnum; import org.dromara.dynamictp.common.em.QueueTypeEnum; import org.dromara.dynamictp.common.em.RejectedTypeEnum; import org.dromara.dynamictp.common.entity.NotifyItem; +import org.dromara.dynamictp.common.notifier.type.NotifyItemTypeRegistry; import org.dromara.dynamictp.common.queue.VariableLinkedBlockingQueue; import org.dromara.dynamictp.core.executor.DtpExecutor; import org.dromara.dynamictp.core.executor.NamedThreadFactory; @@ -193,7 +193,7 @@ public class ThreadPoolBuilder { private final List taskWrappers = Lists.newArrayList(); /** - * Notify items, see {@link NotifyItemEnum} + * Notify items, see {@link NotifyItemTypeRegistry} */ private List notifyItems = NotifyItem.getAllNotifyItems(); diff --git a/core/src/main/java/org/dromara/dynamictp/core/timer/QueueTimeoutTimerTask.java b/core/src/main/java/org/dromara/dynamictp/core/timer/QueueTimeoutTimerTask.java index 2bbb82a3..16c4bb0c 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/timer/QueueTimeoutTimerTask.java +++ b/core/src/main/java/org/dromara/dynamictp/core/timer/QueueTimeoutTimerTask.java @@ -24,7 +24,7 @@ import org.dromara.dynamictp.core.notifier.manager.AlarmManager; import org.dromara.dynamictp.core.support.adapter.ExecutorAdapter; import org.dromara.dynamictp.core.support.ExecutorWrapper; -import static org.dromara.dynamictp.common.em.NotifyItemEnum.QUEUE_TIMEOUT; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.QUEUE_TIMEOUT; /** * A timer task used to handle queued timeout. diff --git a/core/src/main/java/org/dromara/dynamictp/core/timer/RunTimeoutTimerTask.java b/core/src/main/java/org/dromara/dynamictp/core/timer/RunTimeoutTimerTask.java index 3bf958a3..401f41a8 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/timer/RunTimeoutTimerTask.java +++ b/core/src/main/java/org/dromara/dynamictp/core/timer/RunTimeoutTimerTask.java @@ -24,7 +24,7 @@ import org.dromara.dynamictp.core.notifier.manager.AlarmManager; import org.dromara.dynamictp.core.support.adapter.ExecutorAdapter; import org.dromara.dynamictp.core.support.ExecutorWrapper; -import static org.dromara.dynamictp.common.em.NotifyItemEnum.RUN_TIMEOUT; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.RUN_TIMEOUT; /** * A timer task used to handle run timeout. diff --git a/extension/extension-agent/src/main/java/org/dromara/dynamictp/extension/agent/AgentAware.java b/extension/extension-agent/src/main/java/org/dromara/dynamictp/extension/agent/AgentAware.java index 94cd455c..87a236de 100644 --- a/extension/extension-agent/src/main/java/org/dromara/dynamictp/extension/agent/AgentAware.java +++ b/extension/extension-agent/src/main/java/org/dromara/dynamictp/extension/agent/AgentAware.java @@ -136,6 +136,13 @@ public class AgentAware extends TaskStatAware { } else { // 被封装的wrapper没有找到DtpRunnable对象,那么就关闭某些监控指标,防止内存溢出 System.setProperty(DTP_EXECUTE_ENHANCED, FALSE_STR); + if (log.isDebugEnabled()) { + if (runnableWrap == null) { + log.warn("DynamicTp aware, can not find Runnable."); + } else { + log.warn("DynamicTp aware, can not find DtpRunnable, runnable: {}", runnableWrap.getClass().getName()); + } + } } return runnableWrap; } diff --git a/extension/extension-limiter-redis/src/main/java/org/dromara/dynamictp/extension/limiter/redis/ratelimiter/RedisRateLimiterNotifyFilter.java b/extension/extension-limiter-redis/src/main/java/org/dromara/dynamictp/extension/limiter/redis/ratelimiter/RedisRateLimiterNotifyFilter.java index b468ff45..7da7a81d 100644 --- a/extension/extension-limiter-redis/src/main/java/org/dromara/dynamictp/extension/limiter/redis/ratelimiter/RedisRateLimiterNotifyFilter.java +++ b/extension/extension-limiter-redis/src/main/java/org/dromara/dynamictp/extension/limiter/redis/ratelimiter/RedisRateLimiterNotifyFilter.java @@ -56,7 +56,7 @@ public class RedisRateLimiterNotifyFilter implements NotifyFilter { if (notifyItem.getSilencePeriod() <= 0) { return true; } - String notifyName = context.getExecutorWrapper().getThreadPoolName() + "#" + context.getNotifyItemEnum().getValue(); + String notifyName = context.getExecutorWrapper().getThreadPoolName() + "#" + context.getNotifyItemType().getType(); int silencePeriod = notifyItem.getSilencePeriod(); int clusterLimit = notifyItem.getClusterLimit(); return redisScriptRateLimiter.tryPass(notifyName, silencePeriod, clusterLimit); diff --git a/extension/extension-notify-email/src/main/java/org/dromara/dynamictp/extension/notify/email/DtpEmailNotifier.java b/extension/extension-notify-email/src/main/java/org/dromara/dynamictp/extension/notify/email/DtpEmailNotifier.java index 35da8919..1dd447ad 100644 --- a/extension/extension-notify-email/src/main/java/org/dromara/dynamictp/extension/notify/email/DtpEmailNotifier.java +++ b/extension/extension-notify-email/src/main/java/org/dromara/dynamictp/extension/notify/email/DtpEmailNotifier.java @@ -20,12 +20,12 @@ package org.dromara.dynamictp.extension.notify.email; import lombok.extern.slf4j.Slf4j; import lombok.val; import org.apache.commons.lang3.tuple.Pair; -import org.dromara.dynamictp.common.em.NotifyItemEnum; import org.dromara.dynamictp.common.em.NotifyPlatformEnum; import org.dromara.dynamictp.common.entity.NotifyItem; import org.dromara.dynamictp.common.entity.NotifyPlatform; import org.dromara.dynamictp.common.entity.TpMainFields; import org.dromara.dynamictp.common.manager.ContextManagerHelper; +import org.dromara.dynamictp.common.notifier.type.CommonNotifyItemType; import org.dromara.dynamictp.common.util.CommonUtil; import org.dromara.dynamictp.common.util.DateUtil; import org.dromara.dynamictp.core.notifier.AbstractDtpNotifier; @@ -78,7 +78,7 @@ public class DtpEmailNotifier extends AbstractDtpNotifier { } @Override - protected String buildAlarmContent(NotifyPlatform platform, NotifyItemEnum notifyItemEnum) { + protected String buildAlarmContent(NotifyPlatform platform, CommonNotifyItemType notifyItemType) { AlarmCtx alarmCtx = (AlarmCtx) DtpNotifyCtxHolder.get(); NotifyItem notifyItem = alarmCtx.getNotifyItem(); ExecutorWrapper executorWrapper = alarmCtx.getExecutorWrapper(); @@ -89,7 +89,7 @@ public class DtpEmailNotifier extends AbstractDtpNotifier { val executor = executorWrapper.getExecutor(); val statProvider = executorWrapper.getThreadPoolStatProvider(); Context context = newContext(executorWrapper); - context.setVariable("alarmType", populateAlarmItem(notifyItemEnum, notifyItem, executorWrapper)); + context.setVariable("alarmType", populateAlarmItem(notifyItemType, notifyItem, executorWrapper)); context.setVariable("alarmValue", alarmValue); context.setVariable("corePoolSize", executor.getCorePoolSize()); context.setVariable("maximumPoolSize", executor.getMaximumPoolSize()); @@ -111,7 +111,7 @@ public class DtpEmailNotifier extends AbstractDtpNotifier { context.setVariable("alarmTime", DateUtil.now()); context.setVariable("alarmPeriod", notifyItem.getPeriod()); context.setVariable("alarmSilencePeriod", notifyItem.getSilencePeriod()); - context.setVariable("highlightVariables", getAlarmKeys(notifyItemEnum)); + context.setVariable("highlightVariables", getAlarmKeys(notifyItemType)); context.setVariable("trace", getTraceInfo()); context.setVariable("ext", getExtInfo()); return ((EmailNotifier) notifier).processTemplateContent("alarm", context); diff --git a/spring/src/main/java/org/dromara/dynamictp/spring/annotation/DtpBeanDefinitionRegistrar.java b/spring/src/main/java/org/dromara/dynamictp/spring/annotation/DtpBeanDefinitionRegistrar.java index d0d7c8c8..bcd09b84 100644 --- a/spring/src/main/java/org/dromara/dynamictp/spring/annotation/DtpBeanDefinitionRegistrar.java +++ b/spring/src/main/java/org/dromara/dynamictp/spring/annotation/DtpBeanDefinitionRegistrar.java @@ -42,23 +42,7 @@ import java.util.Map; import java.util.concurrent.BlockingQueue; import java.util.concurrent.PriorityBlockingQueue; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.ALLOW_CORE_THREAD_TIMEOUT; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.AWAIT_TERMINATION_SECONDS; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.AWARE_NAMES; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.NOTIFY_ENABLED; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.NOTIFY_ITEMS; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.PLATFORM_IDS; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.PLUGIN_NAMES; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.PRE_START_ALL_CORE_THREADS; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.QUEUE_TIMEOUT; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.REJECT_ENHANCED; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.REJECT_HANDLER_TYPE; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.RUN_TIMEOUT; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.TASK_WRAPPERS; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.THREAD_POOL_ALIAS_NAME; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.THREAD_POOL_NAME; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.TRY_INTERRUPT_WHEN_TIMEOUT; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.WAIT_FOR_TASKS_TO_COMPLETE_ON_SHUTDOWN; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.*; import static org.dromara.dynamictp.common.em.QueueTypeEnum.buildLbq; import static org.dromara.dynamictp.common.entity.NotifyItem.mergeAllNotifyItems; @@ -109,9 +93,9 @@ public class DtpBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar propertyValues.put(PRE_START_ALL_CORE_THREADS, props.isPreStartAllCoreThreads()); propertyValues.put(REJECT_HANDLER_TYPE, props.getRejectedHandlerType()); propertyValues.put(REJECT_ENHANCED, props.isRejectEnhanced()); - propertyValues.put(RUN_TIMEOUT, props.getRunTimeout()); + propertyValues.put(RUN_TIMEOUT_PROP, props.getRunTimeout()); propertyValues.put(TRY_INTERRUPT_WHEN_TIMEOUT, props.isTryInterrupt()); - propertyValues.put(QUEUE_TIMEOUT, props.getQueueTimeout()); + propertyValues.put(QUEUE_TIMEOUT_PROP, props.getQueueTimeout()); val notifyItems = mergeAllNotifyItems(props.getNotifyItems()); propertyValues.put(NOTIFY_ITEMS, notifyItems); propertyValues.put(PLATFORM_IDS, props.getPlatformIds()); diff --git a/test/test-core/src/test/java/org/dromara/dynamictp/test/core/notify/AbstractDtpNotifierTest.java b/test/test-core/src/test/java/org/dromara/dynamictp/test/core/notify/AbstractDtpNotifierTest.java index 31402d14..1d6a0885 100644 --- a/test/test-core/src/test/java/org/dromara/dynamictp/test/core/notify/AbstractDtpNotifierTest.java +++ b/test/test-core/src/test/java/org/dromara/dynamictp/test/core/notify/AbstractDtpNotifierTest.java @@ -18,12 +18,10 @@ package org.dromara.dynamictp.test.core.notify; import com.google.common.collect.Lists; +import org.dromara.dynamictp.common.entity.*; import org.dromara.dynamictp.common.notifier.LarkNotifier; -import org.dromara.dynamictp.common.em.NotifyItemEnum; -import org.dromara.dynamictp.common.entity.NotifyItem; -import org.dromara.dynamictp.common.entity.NotifyPlatform; -import org.dromara.dynamictp.common.entity.ServiceInstance; -import org.dromara.dynamictp.common.entity.TpMainFields; +import org.dromara.dynamictp.common.notifier.type.CommonNotifyItemType; +import org.dromara.dynamictp.common.notifier.type.NotifyItemTypeRegistry; import org.dromara.dynamictp.common.util.CommonUtil; import org.dromara.dynamictp.core.notifier.AbstractDtpNotifier; import org.dromara.dynamictp.core.notifier.DtpDingNotifier; @@ -52,6 +50,7 @@ import org.springframework.core.env.Environment; import java.util.List; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.LIVENESS; import static org.dromara.dynamictp.common.em.QueueTypeEnum.VARIABLE_LINKED_BLOCKING_QUEUE; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; @@ -110,9 +109,13 @@ public class AbstractDtpNotifierTest { public void testSendAlarmMsg() { AbstractDtpNotifier notifier = new DtpDingNotifier(this.notifier); NotifyPlatform notifyPlatform = new NotifyPlatform(); - NotifyItemEnum notifyItemEnum = NotifyItemEnum.LIVENESS; - DtpNotifyCtxHolder.set(new AlarmCtx(ExecutorWrapper.of(dtpExecutor), new NotifyItem())); - notifier.sendAlarmMsg(notifyPlatform, notifyItemEnum); + CommonNotifyItemType notifyItemType = NotifyItemTypeRegistry.getNotifyItemType(LIVENESS); + AlarmCtx alarmCtx = new AlarmCtx(ExecutorWrapper.of(dtpExecutor), new NotifyItem()); + AlarmInfo alarmInfo = new AlarmInfo(); + alarmInfo.setNotifyItem(notifyItemType); + alarmCtx.setAlarmInfo(alarmInfo); + DtpNotifyCtxHolder.set(alarmCtx); + notifier.sendAlarmMsg(notifyPlatform, notifyItemType); Mockito.verify(this.notifier, Mockito.times(1)).send(any(), anyString()); } diff --git a/test/test-core/src/test/java/org/dromara/dynamictp/test/core/notify/AlarmManagerTest.java b/test/test-core/src/test/java/org/dromara/dynamictp/test/core/notify/AlarmManagerTest.java new file mode 100644 index 00000000..72d0b3fc --- /dev/null +++ b/test/test-core/src/test/java/org/dromara/dynamictp/test/core/notify/AlarmManagerTest.java @@ -0,0 +1,39 @@ +package org.dromara.dynamictp.test.core.notify; + +import lombok.extern.slf4j.Slf4j; +import org.dromara.dynamictp.common.entity.CustomNotifyParam; +import org.dromara.dynamictp.common.notifier.Notifier; +import org.dromara.dynamictp.core.DtpRegistry; +import org.dromara.dynamictp.core.notifier.manager.AlarmManager; +import org.dromara.dynamictp.core.support.ExecutorWrapper; +import org.dromara.dynamictp.spring.annotation.EnableDynamicTp; +import org.dromara.dynamictp.spring.support.YamlPropertySourceFactory; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mockito; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.PropertySource; +import org.springframework.test.context.junit.jupiter.SpringExtension; + + +@Slf4j +@EnableDynamicTp +@EnableAutoConfiguration +@ExtendWith(SpringExtension.class) +@SpringBootTest(classes = AlarmManagerTest.class) +@PropertySource(value = "classpath:/dynamic-tp-alarm.yml", factory = YamlPropertySourceFactory.class) +public class AlarmManagerTest { + + + @Test + public void testCustomNotify() throws InterruptedException { + ExecutorWrapper executorWrapper = DtpRegistry.getExecutorWrapper("alarmDtpThreadPoolExecutor"); + CustomNotifyParam customAlarmParams = new CustomNotifyParam(); + customAlarmParams.put("mytestKey", "mytestKeyValue"); + AlarmManager.tryCustomAlarmAsync(executorWrapper, "myAlarmType", () -> log.info("custom alarm test"), customAlarmParams); + AlarmManager.tryCustomAlarmAsync(executorWrapper, "myAlarmType", () -> log.info("custom alarm test"), customAlarmParams); + + Thread.sleep(1000); + } +} diff --git a/test/test-core/src/test/java/org/dromara/dynamictp/test/core/notify/customalarmtype/MyAlarmType.java b/test/test-core/src/test/java/org/dromara/dynamictp/test/core/notify/customalarmtype/MyAlarmType.java new file mode 100644 index 00000000..fccbac48 --- /dev/null +++ b/test/test-core/src/test/java/org/dromara/dynamictp/test/core/notify/customalarmtype/MyAlarmType.java @@ -0,0 +1,10 @@ +package org.dromara.dynamictp.test.core.notify.customalarmtype; + +import org.dromara.dynamictp.common.notifier.type.AbstractCustomAlarmType; + +public class MyAlarmType extends AbstractCustomAlarmType { + @Override + public String getNotifyType() { + return "myAlarmType"; + } +} diff --git a/test/test-core/src/test/resources/META-INF/services/org.dromara.dynamictp.common.notifier.type.AbstractCustomAlarmType b/test/test-core/src/test/resources/META-INF/services/org.dromara.dynamictp.common.notifier.type.AbstractCustomAlarmType new file mode 100644 index 00000000..47bec92a --- /dev/null +++ b/test/test-core/src/test/resources/META-INF/services/org.dromara.dynamictp.common.notifier.type.AbstractCustomAlarmType @@ -0,0 +1 @@ +org.dromara.dynamictp.test.core.notify.customalarmtype.MyAlarmType \ No newline at end of file diff --git a/test/test-core/src/test/resources/dynamic-tp-alarm.yml b/test/test-core/src/test/resources/dynamic-tp-alarm.yml new file mode 100644 index 00000000..3fd78ad0 --- /dev/null +++ b/test/test-core/src/test/resources/dynamic-tp-alarm.yml @@ -0,0 +1,44 @@ +# 动态线程池配置文件,建议单独开一个文件放到配置中心,字段详解看readme介绍 +dynamictp: + enabled: true + enabledBanner: true # 是否开启banner打印,默认true + enabledCollect: true # 是否开启监控指标采集,默认false + collectorTypes: jmx,micrometer,logging # 监控数据采集器类型(logging | micrometer | internal_logging),默认micrometer + logPath: /home/logs # 监控日志数据路径,默认 ${user.home}/logs + monitorInterval: 5 # 监控时间间隔(报警判断、指标采集),默认5s + configType: yml # 配置文件类型 + platforms: # 通知报警平台配置 + - platform: wechat + platformId: "1" + urlKey: 3a7500-1287-4bd-a798-c5c3d8b69c # 替换 + receivers: test1,test2 # 接受人企微名称 + - platform: ding + platformId: "2" + urlKey: f80dad441fcd655438f4a08dcd6a # 替换 + secret: SECb5441fa6f375d5b9d21 # 替换,非sign模式可以没有此值 + receivers: 15810119805 # 钉钉账号手机号 + - platform: lark + platformId: "3" + urlKey: 0d944ae7-b24a-40 # 替换 + receivers: test1,test2 # 接受人飞书名称/openid + executors: # 动态线程池配置,都有默认值,采用默认值的可以不配置该项,减少配置量 + - threadPoolName: alarmDtpThreadPoolExecutor + executorType: eager # 线程池类型common、eager:适用于io密集型 + corePoolSize: 1 + maximumPoolSize: 10 + queueCapacity: 5000 + rejectedHandlerType: CallerRunsPolicy # 拒绝策略,查看RejectedTypeEnum枚举类 + keepAliveTime: 50 + allowCoreThreadTimeOut: false # 是否允许核心线程池超时 + threadNamePrefix: eagerDtp # 线程名前缀 + waitForTasksToCompleteOnShutdown: false # 参考spring线程池设计,优雅关闭线程池 + awaitTerminationSeconds: 5 # 单位(s) + preStartAllCoreThreads: false # 是否预热所有核心线程,默认false + runTimeout: 10000 # 任务执行超时阈值,目前只做告警用,单位(ms) + queueTimeout: 10000 # 任务在队列等待超时阈值,目前只做告警用,单位(ms) + taskWrapperNames: [ "ttl" ] # 任务包装器名称,集成TaskWrapper接口 + notifyItems: + - type: "myAlarmType" + enabled: true + count: 2 + platformIds: ["2"] -- Gitee