Skip to content

Commit

Permalink
【enhancement】add policy event, report to backend
Browse files Browse the repository at this point in the history
  • Loading branch information
robotLJW committed Apr 3, 2023
1 parent e6167c0 commit 1d6306d
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 6 deletions.
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.router.common.event;

/**
* Policy事件包含四件:
* 1. 关闭policy规则
* 2.1 相同tag规则匹配生效: 全部可用实例数小于全部实例最小可用阈值,则同TAG优先
* 2.2 相同tag规则匹配生效:
* 未设置全部实例最小可用阈值,超过同TAG比例阈值,则同TAG优先;
* 设置了全部实例最小可用阈值,但其小于全部TAG可用实例,超过同TAG比例阈值,则同TAG优先
* 3. 相同tag规则匹配不生效
*
* @author robotLJW
* @since 2023-04-03
*
*/
public enum PolicyEvent {

/**
* 关闭policy规则
*/
CLOSE("Close policy rule"),

/**
* 符合相同Tag规则匹配:全部可用实例数小于全部实例最小可用阈值,则同TAG优先
*/
SAME_TAG_MATCH_LESS_MIN_ALL_INSTANCES("Same tag rule match: less than the minimum available threshold for "
+ "all instances"),

/**
* 符合相同Tag规则匹配:超过同TAG比例阈值,则同TAG优先
*/
SAME_TAG_MATCH_EXCEEDED_TRIGGER_THRESHOLD("Same tag rule match: exceeded trigger threshold"),

/**
* 相同Tag规则匹配没匹配上
*/
SAME_TAG_MISMATCH("Same tag rule mismatch");

/**
* 事件描述
*/
private String desc;

PolicyEvent(String desc) {
this.desc = desc;
}

public String getDesc() {
return this.desc;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,22 @@ public void collectGlobalRouteRuleEvent(String rule) {
RouterEventDefinition.ROUTER_RULE_TAKE_EFFECT.getEventType(),
new EventInfo(RouterEventDefinition.ROUTER_RULE_TAKE_EFFECT.getName(), eventDescription)));
}

/**
* 采集选取serviceName下游匹配policy规则事件
*
* @param serviceName 服务名
* @param event 事件
*/
public void collectPolicyEvent(String serviceName, String event) {
if (!eventConfig.isEnable()) {
return;
}
String eventDescription = "According to the policy in the rule, " + serviceName + " " + event;
offerEvent(new Event(RouterEventDefinition.ROUTER_RULE_POLICY_TAKE_EFFECT.getScope(),
RouterEventDefinition.ROUTER_RULE_POLICY_TAKE_EFFECT.getEventLevel(),
RouterEventDefinition.ROUTER_RULE_POLICY_TAKE_EFFECT.getEventType(),
new EventInfo(RouterEventDefinition.ROUTER_RULE_POLICY_TAKE_EFFECT.getName(), eventDescription)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ public enum RouterEventDefinition {
* 路由插件规则生效事件
*/

ROUTER_RULE_TAKE_EFFECT("ROUTER_RULE_TAKE_EFFECT", EventType.GOVERNANCE, EventLevel.NORMAL);
ROUTER_RULE_TAKE_EFFECT("ROUTER_RULE_TAKE_EFFECT", EventType.GOVERNANCE, EventLevel.NORMAL),

/**
* 路由插件中policy规则生效事件
*/
ROUTER_RULE_POLICY_TAKE_EFFECT("ROUTER_RULE_POLICY_TAKE_EFFECT", EventType.GOVERNANCE, EventLevel.NORMAL);

private final String name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.huaweicloud.sermant.router.config.strategy;

import com.huaweicloud.sermant.core.common.LoggerFactory;
import com.huaweicloud.sermant.router.common.event.PolicyEvent;
import com.huaweicloud.sermant.router.common.event.RouterEventCollector;
import com.huaweicloud.sermant.router.common.utils.CollectionUtils;
import com.huaweicloud.sermant.router.config.entity.Match;
import com.huaweicloud.sermant.router.config.entity.Policy;
Expand Down Expand Up @@ -54,6 +56,21 @@ public abstract class AbstractRuleStrategy<I> implements RuleStrategy<I> {

private final String source;

private volatile PolicyEvent state;

/**
* 给backend上报policy相关生效事件
*
* @param newState 目前policy规则状态
* @param serviceName 服务名
*/
public synchronized void notify(PolicyEvent newState, String serviceName) {
if (!newState.equals(this.state)) {
this.state = newState;
RouterEventCollector.getInstance().collectPolicyEvent(serviceName, newState.getDesc());
}
}

/**
* 构造方法
*
Expand All @@ -69,6 +86,7 @@ public AbstractRuleStrategy(String source, InstanceStrategy<I, Map<String, Strin
this.matchInstanceStrategy = matchInstanceStrategy;
this.mismatchInstanceStrategy = mismatchInstanceStrategy;
this.mapper = mapper;
this.state = PolicyEvent.CLOSE;
}

@Override
Expand Down Expand Up @@ -96,14 +114,28 @@ public List<I> getMatchInstances(String serviceName, List<I> instances, Rule rul
}
Policy policy = match.getPolicy();

// 1.全部可用实例数小于全部实例最小可用阈值,则同AZ优先
// 2.未设置全部实例最小可用阈值,未超过同AZ比例阈值,则同AZ优先
// 3.设置了全部实例最小可用阈值,但其小于全部AZ可用实例,未超过同AZ比例阈值,则同AZ优先
if (policy == null || policy.getMinAllInstances() > instances.size()
|| instances.size() * policy.getTriggerThreshold() < matchInstances.size()) {
// 未定义policy规则
if (policy == null) {
notify(PolicyEvent.CLOSE, serviceName);
return matchInstances;
}

// 情况一:全部可用实例数小于全部实例最小可用阈值,则同TAG优先
if (policy.getMinAllInstances() > instances.size()) {
notify(PolicyEvent.SAME_TAG_MATCH_LESS_MIN_ALL_INSTANCES, serviceName);
return matchInstances;
}

// 情况二:未设置全部实例最小可用阈值,超过同TAG比例阈值,则同TAG优先
// 情况三:设置了全部实例最小可用阈值,但其小于全部TAG可用实例,超过同TAG比例阈值,则同TAG优先
if (instances.size() * policy.getTriggerThreshold() < matchInstances.size()) {
notify(PolicyEvent.SAME_TAG_MATCH_EXCEEDED_TRIGGER_THRESHOLD, serviceName);
return matchInstances;
}
LOGGER.warning("not matched, return all instances");

// 情况四:未匹配上
notify(PolicyEvent.SAME_TAG_MISMATCH, serviceName);
return instances;
}

Expand Down

0 comments on commit 1d6306d

Please sign in to comment.