forked from zstackio/zstack
-
Notifications
You must be signed in to change notification settings - Fork 0
<feature>[kvm]: add kvmagent auto-restart window config #3863
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zstack-robot-2
wants to merge
2
commits into
5.5.16
Choose a base branch
from
sync/jin.ma/feat/kvmagent-autorestart-window
base: 5.5.16
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -99,6 +99,7 @@ | |
| import java.nio.channels.Selector; | ||
| import java.nio.channels.ServerSocketChannel; | ||
| import java.nio.channels.SocketChannel; | ||
| import java.time.LocalTime; | ||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
|
|
@@ -459,6 +460,14 @@ private void processKvmagentPhysicalMemUsageAbnormal(HostProcessPhysicalMemoryUs | |
| return; | ||
| } | ||
|
|
||
| String window = KVMGlobalConfig.KVMAGENT_AUTO_RESTART_WINDOW.value(); | ||
| if (!isNowInAutoRestartWindow(window, LocalTime.now())) { | ||
| logger.info(String.format("zstack-kvmagent on host %s exceeded physical memory hard limit, " + | ||
| "but current time is outside auto-restart window [%s]; skip auto-restart, will retry on next alarm", | ||
| cmd.getHostUuid(), window)); | ||
| return; | ||
| } | ||
|
|
||
| logger.debug("The zstack-kvmagent service has exceeded the hard limit for physical memory usage, " + | ||
| "and we will try restart it later"); | ||
| RestartKvmAgentMsg restartKvmAgentMsg = new RestartKvmAgentMsg(); | ||
|
|
@@ -467,6 +476,19 @@ private void processKvmagentPhysicalMemUsageAbnormal(HostProcessPhysicalMemoryUs | |
| bus.send(restartKvmAgentMsg); | ||
| } | ||
|
|
||
| static boolean isNowInAutoRestartWindow(String configValue, LocalTime now) { | ||
| if (configValue == null || configValue.trim().isEmpty()) { | ||
| return true; | ||
| } | ||
| String[] parts = configValue.trim().split("-"); | ||
| LocalTime start = LocalTime.parse(parts[0]); | ||
| LocalTime end = LocalTime.parse(parts[1]); | ||
| if (start.isBefore(end)) { | ||
| return !now.isBefore(start) && now.isBefore(end); | ||
| } | ||
| return !now.isBefore(start) || now.isBefore(end); | ||
| } | ||
|
Comment on lines
+479
to
+490
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
set -euo pipefail
echo "== 检查窗口解析实现是否存在无兜底解析 =="
rg -n -A20 -B5 'static boolean isNowInAutoRestartWindow|LocalTime\.parse|split\("-"\)' plugin/kvm/src/main/java/org/zstack/kvm/KVMHostFactory.java
echo
echo "== 检查测试是否覆盖非法配置输入场景 =="
rg -n 'inWindow\(|assert(True|False)|Exception' plugin/kvm/src/test/java/org/zstack/kvm/TestKVMAutoRestartWindow.java
rg -n '25:00|aa:bb|invalid|malformed|02:00-02:00|02:00-' plugin/kvm/src/test/java/org/zstack/kvm/TestKVMAutoRestartWindow.java || trueRepository: MatheMatrix/zstack Length of output: 5207 添加错误处理防止脏配置导致告警流程中断。 当前代码直接解析时间字符串而无任何错误处理;虽然全局配置验证器可阻止新的非法值写入数据库,但无法防护:
若这些脏数据在生产环境触发 建议修复 static boolean isNowInAutoRestartWindow(String configValue, LocalTime now) {
if (configValue == null || configValue.trim().isEmpty()) {
return true;
}
- String[] parts = configValue.trim().split("-");
- LocalTime start = LocalTime.parse(parts[0]);
- LocalTime end = LocalTime.parse(parts[1]);
+ String[] parts = configValue.trim().split("-");
+ if (parts.length != 2) {
+ logger.warn(String.format("invalid auto-restart window config[%s], treat as out-of-window", configValue));
+ return false;
+ }
+
+ final LocalTime start;
+ final LocalTime end;
+ try {
+ start = LocalTime.parse(parts[0].trim());
+ end = LocalTime.parse(parts[1].trim());
+ } catch (Exception e) {
+ logger.warn(String.format("invalid auto-restart window config[%s], treat as out-of-window", configValue), e);
+ return false;
+ }
+
if (start.isBefore(end)) {
return !now.isBefore(start) && now.isBefore(end);
}
return !now.isBefore(start) || now.isBefore(end);
}🤖 Prompt for AI Agents |
||
|
|
||
| private void initLibvirtTlsCA() { | ||
| if (CoreGlobalProperty.UNIT_TEST_ON) { | ||
| return; | ||
|
|
@@ -558,6 +580,31 @@ public void validateGlobalConfig(String category, String name, String oldValue, | |
| } | ||
| } | ||
| }); | ||
| KVMGlobalConfig.KVMAGENT_AUTO_RESTART_WINDOW.installValidateExtension(new GlobalConfigValidatorExtensionPoint() { | ||
| @Override | ||
| public void validateGlobalConfig(String category, String name, String oldValue, String value) throws GlobalConfigException { | ||
| if (value == null || value.trim().isEmpty()) { | ||
| return; | ||
| } | ||
| String[] parts = value.trim().split("-"); | ||
| if (parts.length != 2 || !parts[0].matches("\\d{2}:\\d{2}") || !parts[1].matches("\\d{2}:\\d{2}")) { | ||
| throw new GlobalConfigException(String.format("%s must be in format HH:MM-HH:MM, but got %s", | ||
| KVMGlobalConfig.KVMAGENT_AUTO_RESTART_WINDOW.getCanonicalName(), value)); | ||
| } | ||
| int sh = Integer.parseInt(parts[0].substring(0, 2)); | ||
| int sm = Integer.parseInt(parts[0].substring(3, 5)); | ||
| int eh = Integer.parseInt(parts[1].substring(0, 2)); | ||
| int em = Integer.parseInt(parts[1].substring(3, 5)); | ||
| if (sh > 23 || eh > 23 || sm > 59 || em > 59) { | ||
| throw new GlobalConfigException(String.format("%s has out-of-range hour/minute, but got %s", | ||
| KVMGlobalConfig.KVMAGENT_AUTO_RESTART_WINDOW.getCanonicalName(), value)); | ||
| } | ||
| if (sh == eh && sm == em) { | ||
| throw new GlobalConfigException(String.format("%s start equals end, but got %s", | ||
| KVMGlobalConfig.KVMAGENT_AUTO_RESTART_WINDOW.getCanonicalName(), value)); | ||
| } | ||
| } | ||
| }); | ||
| ResourceConfig resourceConfig = rcf.getResourceConfig(KVMGlobalConfig.VM_CPU_HYPERVISOR_FEATURE.getIdentity()); | ||
| resourceConfig.installValidatorExtension((resourceUuid, oldValue, newValue) -> { | ||
| if (Boolean.TRUE.toString().equals(newValue)) { | ||
|
|
||
67 changes: 67 additions & 0 deletions
67
plugin/kvm/src/test/java/org/zstack/kvm/TestKVMAutoRestartWindow.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package org.zstack.kvm; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| import java.time.LocalTime; | ||
|
|
||
| import static org.junit.Assert.assertFalse; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| public class TestKVMAutoRestartWindow { | ||
|
|
||
| private boolean inWindow(String configValue, String now) { | ||
| return KVMHostFactory.isNowInAutoRestartWindow(configValue, LocalTime.parse(now)); | ||
| } | ||
|
|
||
| @Test | ||
| public void emptyOrNullValueAlwaysAllowed() { | ||
| assertTrue(inWindow("", "00:00")); | ||
| assertTrue(inWindow("", "12:00")); | ||
| assertTrue(inWindow("", "23:59")); | ||
| assertTrue(inWindow(null, "12:00")); | ||
| assertTrue(inWindow(" ", "12:00")); | ||
| } | ||
|
|
||
| @Test | ||
| public void normalWindowMembership() { | ||
| assertTrue(inWindow("02:00-04:00", "02:30")); | ||
| assertTrue(inWindow("02:00-04:00", "03:59")); | ||
| assertFalse(inWindow("02:00-04:00", "00:00")); | ||
| assertFalse(inWindow("02:00-04:00", "14:00")); | ||
| } | ||
|
|
||
| @Test | ||
| public void normalWindowBoundary() { | ||
| // start is inclusive | ||
| assertTrue(inWindow("02:00-04:00", "02:00")); | ||
| // end is exclusive | ||
| assertFalse(inWindow("02:00-04:00", "04:00")); | ||
| // just before end is included | ||
| assertTrue(inWindow("02:00-04:00", "03:59")); | ||
| } | ||
|
|
||
| @Test | ||
| public void crossMidnightWindowMembership() { | ||
| // after start, before midnight | ||
| assertTrue(inWindow("22:00-02:00", "23:30")); | ||
| assertTrue(inWindow("22:00-02:00", "22:00")); | ||
| // after midnight, before end | ||
| assertTrue(inWindow("22:00-02:00", "00:30")); | ||
| assertTrue(inWindow("22:00-02:00", "01:59")); | ||
| // outside (afternoon) | ||
| assertFalse(inWindow("22:00-02:00", "14:00")); | ||
| // boundary: end is exclusive | ||
| assertFalse(inWindow("22:00-02:00", "02:00")); | ||
| // just before start is excluded | ||
| assertFalse(inWindow("22:00-02:00", "21:59")); | ||
| } | ||
|
|
||
| @Test | ||
| public void wholeDayMinusOneMinute() { | ||
| // 00:00-23:59 covers everything except 23:59 itself | ||
| assertTrue(inWindow("00:00-23:59", "00:00")); | ||
| assertTrue(inWindow("00:00-23:59", "12:00")); | ||
| assertTrue(inWindow("00:00-23:59", "23:58")); | ||
| assertFalse(inWindow("00:00-23:59", "23:59")); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
空值语义与当前注解默认校验存在冲突。
这里使用裸
@GlobalConfigValidation会沿用默认notNull=true/notEmpty=true,与“空值表示不限时段”的需求冲突;用户也无法把已配置窗口恢复为“始终允许”。💡 建议修复
🤖 Prompt for AI Agents