Skip to content

Commit

Permalink
增加Agent通知逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
hanbingleixue committed Jun 25, 2023
1 parent 925b052 commit 3061400
Show file tree
Hide file tree
Showing 11 changed files with 437 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ event.offerErrorLog=false
event.sendInterval=30000
event.offerInterval=300000

# notification config
notification.enable=false

# dynamic config
dynamic.config.timeoutValue=30000
dynamic.config.defaultGroup=sermant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@
import com.huaweicloud.sermant.core.config.ConfigManager;
import com.huaweicloud.sermant.core.event.EventManager;
import com.huaweicloud.sermant.core.event.collector.FrameworkEventCollector;
import com.huaweicloud.sermant.core.notification.NotificationInfo;
import com.huaweicloud.sermant.core.notification.NotificationManager;
import com.huaweicloud.sermant.core.notification.NotificationType;
import com.huaweicloud.sermant.core.operation.OperationManager;
import com.huaweicloud.sermant.core.plugin.PluginSystemEntrance;
import com.huaweicloud.sermant.core.service.ServiceManager;

import java.lang.instrument.Instrumentation;
import java.util.Date;
import java.util.Map;

/**
Expand Down Expand Up @@ -74,5 +78,8 @@ public static void run(Map<String, Object> argsMap, Instrumentation instrumentat

// 上报Sermant启动事件
FrameworkEventCollector.getInstance().collectAgentStartEvent();

NotificationManager.sendNotification(new NotificationInfo(NotificationType.SERMANT_START_COMPLETE,
new Date(), null));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (C) 2023-2023 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.huaweicloud.sermant.core.notification;

import java.util.Date;

/**
* 通知信息
*
* @author zhp
* @since 2023-06-16
*/
public class NotificationInfo {
/**
* 通知类型
*/
private NotificationType notificationType;

/**
* 通知开始事件
*/
private Date startDate;

/**
* 通知内容
*/
private Object content;

/**
* 构造方法
*
* @param notificationType 通知类型
* @param startDate 通知开始事件
* @param content 通知内容
*/
public NotificationInfo(NotificationType notificationType, Date startDate, Object content) {
this.notificationType = notificationType;
this.startDate = startDate;
this.content = content;
}

/**
* 无参构造
*/
public NotificationInfo() {
}

public NotificationType getNotificationType() {
return notificationType;
}

public void setNotificationType(NotificationType notificationType) {
this.notificationType = notificationType;
}

public Date getStartDate() {
return startDate;
}

public void setStartDate(Date startDate) {
this.startDate = startDate;
}

public Object getContent() {
return content;
}

public void setContent(Object content) {
this.content = content;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2023-2023 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.huaweicloud.sermant.core.notification;

/**
* 通知监听器
*
* @author zhp
* @since 2023-06-16
*/
public interface NotificationListener {
/**
* 通知
*
* @param notificationInfo 通知信息
*/
void notify(NotificationInfo notificationInfo);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (C) 2023-2023 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.huaweicloud.sermant.core.notification;

import com.huaweicloud.sermant.core.config.ConfigManager;
import com.huaweicloud.sermant.core.notification.config.NotificationConfig;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;

/**
* 通知管理器
*
* @author zhp
* @since 2023-06-16
*/
public class NotificationManager {
private static final Map<String, List<NotificationListener>> NOTIFICATION_LISTENER_MAP = new ConcurrentHashMap<>();

private static NotificationConfig notificationConfig = ConfigManager.getConfig(NotificationConfig.class);

private NotificationManager() {
}

/**
* 通知监听器注册
*
* @param notificationListener 通知监听器
* @param notificationType 通知类型
*/
public static void registry(NotificationListener notificationListener, NotificationType notificationType) {
if (notificationConfig == null || !notificationConfig.isEnable()) {
return;
}
List<NotificationListener> listenerList = NOTIFICATION_LISTENER_MAP.computeIfAbsent(notificationType.getName(),
s -> new ArrayList<>());
listenerList.add(notificationListener);
}

/**
* 通知监听器取消注册
*
* @param notificationListener 通知监听器
* @param notificationType 通知类型
*/
public static void unRegistry(NotificationListener notificationListener, NotificationType notificationType) {
if (notificationConfig == null || !notificationConfig.isEnable()) {
return;
}
List<NotificationListener> listenerList = NOTIFICATION_LISTENER_MAP.get(notificationType.getName());
if (listenerList != null) {
listenerList.remove(notificationListener);
}
}

/**
* 事件通知
*
* @param notificationInfo 通知信息
*/
public static void sendNotification(NotificationInfo notificationInfo) {
if (notificationConfig == null || !notificationConfig.isEnable()) {
return;
}
List<NotificationListener> notificationListeners =
NOTIFICATION_LISTENER_MAP.get(notificationInfo.getNotificationType().getName());
if (notificationListeners == null || notificationListeners.isEmpty()) {
return;
}
notificationListeners.forEach(notificationListener ->
CompletableFuture.runAsync(() -> notificationListener.notify(notificationInfo)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (C) 2023-2023 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.huaweicloud.sermant.core.notification;

/**
* 通知类型
*
* @author zhp
* @since 2023-06-16
*/
public enum NotificationType {
/**
* sermant启动完成
*/
SERMANT_START_COMPLETE("SERMANT_START_COMPLETE", "sermant启动完成"),

/**
* netty已链接
*/
NETTY_CONNECTED("NETTY_CONNECTED", "netty已链接"),

/**
* netty链接断开
*/
NETTY_DISCONNECTED("NETTY_DISCONNECTED", "netty链接断开"),

/**
* zookeeper已链接
*/
ZOOKEEPER_CONNECTED("ZOOKEEPER_CONNECTED", "zookeeper已链接"),

/**
* zookeeper链接断开
*/
ZOOKEEPER_DISCONNECTED("ZOOKEEPER_DISCONNECTED", "zookeeper链接断开");

private final String name;

private final String description;

NotificationType(String name, String description) {
this.name = name;
this.description = description;
}

public String getName() {
return name;
}

public String getDescription() {
return description;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2023-2023 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.huaweicloud.sermant.core.notification.config;

import com.huaweicloud.sermant.core.config.common.BaseConfig;
import com.huaweicloud.sermant.core.config.common.ConfigTypeKey;

/**
* 通知配置
*
* @author zhp
* @since 2023-06-16
*/
@ConfigTypeKey("notification")
public class NotificationConfig implements BaseConfig {
private boolean enable;

public boolean isEnable() {
return enable;
}

public void setEnable(boolean enable) {
this.enable = enable;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ com.huaweicloud.sermant.core.service.dynamicconfig.config.KieDynamicConfig
com.huaweicloud.sermant.core.service.heartbeat.config.HeartbeatConfig
com.huaweicloud.sermant.core.service.send.config.GatewayConfig
com.huaweicloud.sermant.core.plugin.config.ServiceMeta
com.huaweicloud.sermant.core.notification.config.NotificationConfig
Loading

0 comments on commit 3061400

Please sign in to comment.