Skip to content

Commit

Permalink
增加Agent通知逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
hanbingleixue committed Jun 29, 2023
1 parent 925b052 commit 34d7396
Show file tree
Hide file tree
Showing 16 changed files with 565 additions and 6 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,6 +22,9 @@
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.SermantNotificationType;
import com.huaweicloud.sermant.core.operation.OperationManager;
import com.huaweicloud.sermant.core.plugin.PluginSystemEntrance;
import com.huaweicloud.sermant.core.service.ServiceManager;
Expand Down Expand Up @@ -74,5 +77,9 @@ public static void run(Map<String, Object> argsMap, Instrumentation instrumentat

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

if (NotificationManager.isEnable()) {
NotificationManager.doNotify(new NotificationInfo(SermantNotificationType.LOAD_COMPLETE, null));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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 NettyNotificationType implements NotificationType {
/**
* netty已链接
*/
CONNECTED("CONNECTED", "notification of netty connected"),

/**
* netty链接断开
*/
DISCONNECTED("DISCONNECTED", "notification of the netty connection has been disconnected");

private final String name;

private final String description;

NettyNotificationType(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,68 @@
/*
* 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 class NotificationInfo {
/**
* 通知类型
*/
private NotificationType notificationType;

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

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

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

public NotificationType getNotificationType() {
return notificationType;
}

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

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 process(NotificationInfo notificationInfo);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* 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.common.LoggerFactory;
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;
import java.util.logging.Logger;

/**
* 通知管理器
*
* @author zhp
* @since 2023-06-16
*/
public class NotificationManager {
private static final Logger LOGGER = LoggerFactory.getLogger();

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 typeClass 通知类型
*/
public static void registry(NotificationListener notificationListener,
Class<? extends NotificationType> typeClass) {
if (!isEnable()) {
return;
}
List<NotificationListener> listenerList = NOTIFICATION_LISTENER_MAP.computeIfAbsent(
typeClass.getCanonicalName(), s -> new ArrayList<>());
listenerList.add(notificationListener);
}

/**
* 通知监听器取消注册。监听该场景下的所有通知
*
* @param notificationListener 通知监听器
* @param typeClass 通知类型
*/
public static void unRegistry(NotificationListener notificationListener,
Class<? extends NotificationType> typeClass) {
if (!isEnable()) {
return;
}
List<NotificationListener> listenerList = NOTIFICATION_LISTENER_MAP.get(typeClass.getCanonicalName());
if (listenerList != null) {
listenerList.remove(notificationListener);
}
}

/**
* 事件通知
*
* @param notificationInfo 通知信息
*/
public static void doNotify(NotificationInfo notificationInfo) {
if (!isEnable()) {
return;
}
if (notificationInfo == null || notificationInfo.getNotificationType() == null) {
LOGGER.fine("notificationInfo is null or notificationType is null");
return;
}
List<NotificationListener> notificationListeners =
NOTIFICATION_LISTENER_MAP.get(notificationInfo.getNotificationType().getClass().getCanonicalName());
if (notificationListeners == null || notificationListeners.isEmpty()) {
return;
}
notificationListeners.forEach(notificationListener ->
CompletableFuture.runAsync(() -> notificationListener.process(notificationInfo)));
}

/**
* 通知开关是否开启
*
* @return 通知开关状态
*/
public static boolean isEnable() {
if (notificationConfig != null && notificationConfig.isEnable()) {
return true;
}
LOGGER.fine("the notification switch is not turned on");
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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 NotificationType {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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 SermantNotificationType implements NotificationType {
/**
* sermant启动完成通知
*/
LOAD_COMPLETE("LOAD_COMPLETE", "sermant startup completed");

private final String name;

private final String description;

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

public String getName() {
return name;
}

public String getDescription() {
return description;
}
}
Loading

0 comments on commit 34d7396

Please sign in to comment.