Files
ai-security-xdr/haobang-security-dm/syslog-consumer-rule/src/main/java/com/config/DirectStatsConfigurationVerifier.java
T
王绍宏 29445c6825 feat(syslog): 完成高吞吐可靠链路与统计存储优化
- 新增接入 WAL、检查点、背压恢复及可靠分发机制,完善 Kafka 幂等发布、分区、重试、DLT 和补偿流程
- 引入规则预编译、批内快照及 JetCache/Caffeine 两级缓存,支持缓存失效广播、影子对账和执行指标
- 升级 InfluxDB V2 原始日志模型,统一接收时间与 Point 时间,支持按日 measurement、UUID 分桶及 V1/V2 双读
- 重构达梦标准化批量写入与统计链路,支持事务内直写、增量投影、幂等控制、窗口水位和日期切换
- 完善告警处理、配置校验、运行监控及测试覆盖
2026-08-20 00:18:33 +08:00

99 lines
4.8 KiB
Java

package com.config;
import com.common.service.StatsWriteModeProvider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.time.DateTimeException;
import java.time.ZoneId;
/** 启动时拒绝会造成双计数、统计缺失或影子表误写正式表的配置。 */
@Component
public class DirectStatsConfigurationVerifier {
private final StatsWriteModeProvider.Mode mode;
private final boolean legacyCaptureEnabled;
private final boolean readEnabled;
private final boolean factOnlyConfirmed;
private final boolean todayStatsDateEnabled;
private final String directDeviceTable;
private final String directRuleTable;
private final String shadowDeviceTable;
private final String shadowRuleTable;
private final String businessZone;
public DirectStatsConfigurationVerifier(
@Value("${stats.write-mode:LEGACY_CHANGE_LOG}") String configuredMode,
@Value("${stats.incremental.capture-enabled:false}") boolean legacyCaptureEnabled,
@Value("${stats.incremental.read-enabled:false}") boolean readEnabled,
@Value("${stats.fact-only.confirm-rebuild-required:false}") boolean factOnlyConfirmed,
@Value("${stats.direct.today-stats-date-enabled:true}") boolean todayStatsDateEnabled,
@Value("${stats.direct.device-table:device_daily_stats}") String directDeviceTable,
@Value("${stats.direct.rule-table:normalize_rule_daily_stats}") String directRuleTable,
@Value("${stats.direct.shadow-device-table:device_daily_stats_direct_shadow}") String shadowDeviceTable,
@Value("${stats.direct.shadow-rule-table:normalize_rule_daily_stats_direct_shadow}") String shadowRuleTable,
@Value("${stats.business-zone:Asia/Shanghai}") String businessZone) {
this.mode = parse(configuredMode);
this.legacyCaptureEnabled = legacyCaptureEnabled;
this.readEnabled = readEnabled;
this.factOnlyConfirmed = factOnlyConfirmed;
this.todayStatsDateEnabled = todayStatsDateEnabled;
this.directDeviceTable = directDeviceTable;
this.directRuleTable = directRuleTable;
this.shadowDeviceTable = shadowDeviceTable;
this.shadowRuleTable = shadowRuleTable;
this.businessZone = businessZone;
}
@PostConstruct
public void validate() {
try {
ZoneId.of(businessZone);
} catch (DateTimeException invalidZone) {
throw new IllegalStateException("非法 stats.business-zone: " + businessZone, invalidZone);
}
if (mode == StatsWriteModeProvider.Mode.LEGACY_CHANGE_LOG && !legacyCaptureEnabled) {
throw new IllegalStateException("LEGACY_CHANGE_LOG 模式必须开启 stats.incremental.capture-enabled");
}
if (mode == StatsWriteModeProvider.Mode.DIRECT_SHADOW) {
if (!legacyCaptureEnabled) {
throw new IllegalStateException("DIRECT_SHADOW 模式必须保留旧 change log 捕获用于对账");
}
if (same(directDeviceTable, shadowDeviceTable) || same(directRuleTable, shadowRuleTable)) {
throw new IllegalStateException("DIRECT_SHADOW 的影子表不能与正式聚合表相同,否则会双计数");
}
}
if (mode == StatsWriteModeProvider.Mode.DIRECT) {
if (legacyCaptureEnabled) {
throw new IllegalStateException("DIRECT 模式禁止继续写 syslog_stats_change_log/pending");
}
if (!readEnabled) {
throw new IllegalStateException("DIRECT 正式模式必须开启 stats.incremental.read-enabled");
}
if (!todayStatsDateEnabled) {
throw new IllegalStateException("DIRECT 正式模式必须启用 today_stats_date 日切保护字段");
}
}
if (mode == StatsWriteModeProvider.Mode.FACT_ONLY_REBUILD_REQUIRED) {
if (legacyCaptureEnabled) {
throw new IllegalStateException("FACT_ONLY_REBUILD_REQUIRED 模式禁止同时写旧变更日志");
}
if (!factOnlyConfirmed) {
throw new IllegalStateException("事实单写降级必须显式确认 stats.fact-only.confirm-rebuild-required=true");
}
}
}
private static boolean same(String first, String second) {
return first != null && second != null && first.equalsIgnoreCase(second);
}
private static StatsWriteModeProvider.Mode parse(String value) {
try {
return StatsWriteModeProvider.Mode.valueOf(value.trim().toUpperCase(java.util.Locale.ROOT));
} catch (Exception invalid) {
throw new IllegalStateException("不支持的 stats.write-mode: " + value, invalid);
}
}
}