feat(settings,analysis): 日志保留时间可配(发送历史保留 7 天)+ 分析页手动刷新;修复我引入的 RWMutex 死锁
=== 0. 【严重】修复上一版引入的死锁(平台写路径全卡死)===
上一版把保留期读取写成 `GetRetentionSettings()`(内部 db.RLock()),
却在 `db.Lock()` 已持有的情况下调用(recordDelivery / RecordAudit 的裁剪路径)。
Go 的 sync.RWMutex **不可重入** → 第一次写投递历史/审计日志就永久死锁。
现象:登录 28 秒返回空(登录也要写审计)、所有写接口挂住。
go build / go vet / 之前的单测(不触发写路径)全都查不出来。
修法:
- 拆出 retentionFromDBLocked(db) / alertDeliveryRetentionLocked(db) /
auditLogRetentionLocked(db),调用方已持锁时用无锁版本
- upsertSettingLocked 里**不能**用 db.NextID()(内部 d.mu.Lock(),同样死锁),
改为锁内自算 max(id)+1
- 新增 retention_test.go:持写锁调用裁剪函数,2 秒超时未返回即判死锁
实测修复后:登录 103ms(原 28000ms)、panic/deadlock 命中 0。
=== 1. 平台数据保留期可配(系统设置 → 通用配置 → 日志保留时间)===
新增 GET/PUT /api/v1/settings/retention:
alertDeliveryDays 告警发送历史保留天数,默认 7(原写死"最近 50 条")
auditLogDays 审计日志保留天数,默认 90(原写死常量)
保存后立即按新策略裁剪一次,不必等下次触发。
校验:发送历史 1~365 天、审计 1~3650 天,超出返回明确中文原因(不是 500)。
注意与「Loki 日志保留期」区分:那个是部署级配置(组件部署 → 保留策略)。
=== 2. 发送历史:50 条硬上限 → 保留 7 天 ===
后端 Deliveries 接口原来写死 `len(out) < 50`,现在按保留期取(响应上限 500 可调),
标题改为「发送历史(保留 N 天)」,N 从设置里读。
=== 3. 菜单「日志服务」→「组件部署」===
=== 4. 分析页:去掉「全部命名空间」+ 改手动刷新 ===
- 移除「全部命名空间」选项:{namespace=~".+"} 要扫全集群(约 370GB/天),
一次分析几十秒,得不偿失;历史值 __all__ 也会被忽略并回退到策略/第一个 ns
- 筛选改为「选择值 / 已生效值」分离:切换命名空间或时间范围**不再自动查询**,
只把刷新按钮点亮并提示「筛选条件已改,点刷新生效」;
点一次才应用并取数。
原因:一次分析要跑多条 count_over_time,切一下下拉就重查一轮,
快速连点会把 Loki 打满(并发护栏 429),还拖慢别人。
=== 实测(9739c7c092e7)===
登录 103ms / token 273 字符
GET retention → {alertDeliveryDays:7, auditLogDays:90}
PUT 0 天 → 400「告警发送历史保留天数需在 1~365 之间」
平台日志 panic/deadlock 命中 0
go test ./... 全绿(含 2 个新死锁回归用例)
This commit is contained in:
1 parent
7369a17598
commit
5e59347c40
27 files changed
+491
-54
No files matched your search
@@ -21,6 +21,7 @@ import { EmptyState } from '@/components/EmptyState'
|
||||
import { ConfirmDialog } from '@/components/ConfirmDialog'
|
||||
import { alertsApi, type AlertRule, type AlertDelivery, type NotifyChannel } from '@/api/alerts'
|
||||
import { logsApi, type LogSource } from '@/api/logs'
|
||||
import { systemApi } from '@/api/system'
|
||||
import { toast } from 'sonner'
|
||||
import { useForm } from 'react-hook-form'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
@@ -85,6 +86,8 @@ const AlertsPage = memo(function AlertsPage() {
|
||||
const [sources, setSources] = useState<LogSource[]>([])
|
||||
const [channels, setChannels] = useState<NotifyChannel[]>([])
|
||||
const [deliveries, setDeliveries] = useState<AlertDelivery[]>([])
|
||||
/** 发送历史保留天数(系统设置 → 通用配置 → 日志保留时间),标题里展示 */
|
||||
const [historyDays, setHistoryDays] = useState(7)
|
||||
const [open, setOpen] = useState(false)
|
||||
const [editing, setEditing] = useState<AlertRule | null>(null)
|
||||
const [deleteTarget, setDeleteTarget] = useState<AlertRule | null>(null)
|
||||
@@ -99,9 +102,11 @@ const AlertsPage = memo(function AlertsPage() {
|
||||
|
||||
const refresh = useCallback(async () => {
|
||||
try {
|
||||
const [r, s, d, ch] = await Promise.all([
|
||||
const [r, s, d, ch, ret] = await Promise.all([
|
||||
alertsApi.listRules(), logsApi.sources(), alertsApi.deliveries(), alertsApi.listChannels(),
|
||||
systemApi.retention().catch(() => null),
|
||||
])
|
||||
if (ret?.alertDeliveryDays) setHistoryDays(ret.alertDeliveryDays)
|
||||
setRules(Array.isArray(r) ? r : (r as unknown as { list: AlertRule[] }).list ?? [])
|
||||
setSources(s.list)
|
||||
setDeliveries(Array.isArray(d) ? d : [])
|
||||
@@ -308,7 +313,7 @@ const AlertsPage = memo(function AlertsPage() {
|
||||
) : (
|
||||
<Card className="border-border">
|
||||
<CardContent className="p-4">
|
||||
<h2 className="text-sm font-semibold mb-3">{t('historyTitle')}</h2>
|
||||
<h2 className="text-sm font-semibold mb-3">{t('historyTitle', { days: historyDays })}</h2>
|
||||
<div className="divide-y divide-border text-sm">
|
||||
{deliveries.map((d) => (
|
||||
<div key={d.id} className="py-2 flex items-center gap-3 flex-wrap">
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { memo, useEffect, useState } from 'react'
|
||||
import { memo, useCallback, useEffect, useState } from 'react'
|
||||
import { useNavigate } from 'react-router'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import '@/i18n/domains'
|
||||
@@ -43,8 +43,16 @@ const AnalysisPage = memo(function AnalysisPage() {
|
||||
const [sourceId, setSourceId] = useState(0)
|
||||
// 默认 15 分钟:分析页一次性要跑 5 条 count_over_time,
|
||||
// 1 小时窗口在日志量大的命名空间要十几秒;15 分钟既能看出趋势又能快速出结果。
|
||||
// ── "选择值" 与 "已生效值" 分离 ──────────────────────────────────
|
||||
// start/end/namespace 是**已生效**值,查询只认它们;
|
||||
// sel* 是筛选栏里的**选择**值。
|
||||
// 这样切换命名空间/时间只改变选择,不触发查询;点「刷新」才生效。
|
||||
// 原因:一次分析要跑多条 count_over_time,切一下下拉就重查一轮很浪费,
|
||||
// 用户快速连点几下会把 Loki 打满(并发护栏 429)。
|
||||
const [start, setStart] = useState(() => toLocal(new Date(Date.now() - 15 * 60 * 1000)))
|
||||
const [end, setEnd] = useState(() => toLocal(new Date()))
|
||||
const [selStart, setSelStart] = useState(() => toLocal(new Date(Date.now() - 15 * 60 * 1000)))
|
||||
const [selEnd, setSelEnd] = useState(() => toLocal(new Date()))
|
||||
|
||||
/**
|
||||
* 命名空间筛选。
|
||||
@@ -56,6 +64,8 @@ const AnalysisPage = memo(function AnalysisPage() {
|
||||
// 初始为空,等策略返回后再定(不要用 localStorage 覆盖策略:
|
||||
// 策略是服务端下发的权威值,本地记忆会把管理员改过的策略"顶掉")。
|
||||
const [namespace, setNamespace] = useState<string>('')
|
||||
/** 命名空间下拉里的选择值(未点刷新前不参与查询) */
|
||||
const [selNamespace, setSelNamespace] = useState<string>('')
|
||||
const [namespaceReady, setNamespaceReady] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
@@ -89,17 +99,24 @@ const AnalysisPage = memo(function AnalysisPage() {
|
||||
const values = (list || []).filter(Boolean).sort()
|
||||
const scopeNs = scope?.labels?.namespace ?? ''
|
||||
setNamespaces(values)
|
||||
setNamespace(current => {
|
||||
const resolve = (current: string) => {
|
||||
// 优先级:用户当前已选(同一数据源内不跳变)→ 授权策略 → default → 第一个
|
||||
// 注意:已移除「全部命名空间」选项 —— {namespace=~".+"} 要扫全集群
|
||||
// (实测约 370GB/天),一次分析跑几十秒,得不偿失。
|
||||
const candidates = [current, scopeNs, 'default', values[0]]
|
||||
for (const candidate of candidates) {
|
||||
if (!candidate) continue
|
||||
if (candidate === ALL_NAMESPACES) return candidate
|
||||
if (!candidate || candidate === ALL_NAMESPACES) continue
|
||||
if (values.includes(candidate)) return candidate
|
||||
}
|
||||
// 策略里配了但 Loki 里暂时没数据时,仍按策略显示(用户能看到"为什么没数据")
|
||||
if (scopeNs) return scopeNs
|
||||
return ''
|
||||
}
|
||||
// 首次解析结果同时作为"选择值"和"已生效值",保证进页面就有一屏数据
|
||||
setNamespace(current => {
|
||||
const next = resolve(current)
|
||||
setSelNamespace(next)
|
||||
return next
|
||||
})
|
||||
})
|
||||
.catch(() => { if (!cancelled) setNamespaces([]) })
|
||||
@@ -133,6 +150,20 @@ const AnalysisPage = memo(function AnalysisPage() {
|
||||
end,
|
||||
namespace: scopedNamespace,
|
||||
})
|
||||
|
||||
// 选择值与已生效值不一致 → 提示"点刷新生效"
|
||||
const pendingApply =
|
||||
namespaceReady && (selNamespace !== namespace || selStart !== start || selEnd !== end)
|
||||
|
||||
/** 把筛选栏里的选择值应用到查询:点一次「刷新」才生效 */
|
||||
const applySelection = useCallback(() => {
|
||||
setNamespace(selNamespace)
|
||||
setStart(selStart)
|
||||
setEnd(selEnd)
|
||||
// 参数没变(只是点刷新)时也要重新取数
|
||||
refresh()
|
||||
}, [selNamespace, selStart, selEnd, refresh])
|
||||
|
||||
// 每日统计固定近 7 天,不随上方时间范围变化;错误就地展示,不影响主区
|
||||
// 每日量可看范围:7 / 30 / 90 / 365 天(数据来自平台留存历史)
|
||||
const [dailyDays, setDailyDays] = useState(7)
|
||||
@@ -156,7 +187,7 @@ const AnalysisPage = memo(function AnalysisPage() {
|
||||
title={t('title')}
|
||||
description={t('subtitle')}
|
||||
action={
|
||||
<Button variant="outline" onClick={refresh} disabled={!sourceId}>
|
||||
<Button variant={pendingApply ? 'default' : 'outline'} onClick={applySelection} disabled={!sourceId}>
|
||||
{loading ? <Loader2 className="mr-1 size-4 animate-spin" /> : <RefreshCw className="mr-1 size-4" />}
|
||||
{t('filter.refresh')}
|
||||
</Button>
|
||||
@@ -179,9 +210,10 @@ const AnalysisPage = memo(function AnalysisPage() {
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<Select
|
||||
value={namespace || undefined}
|
||||
value={selNamespace || undefined}
|
||||
onValueChange={value => {
|
||||
setNamespace(value)
|
||||
// 只改选择值,不触发查询 —— 统一由「刷新」按钮生效
|
||||
setSelNamespace(value)
|
||||
try { localStorage.setItem('analysisNamespace', value) } catch { /* 隐私模式忽略 */ }
|
||||
}}
|
||||
>
|
||||
@@ -190,10 +222,12 @@ const AnalysisPage = memo(function AnalysisPage() {
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{namespaces.map(item => <SelectItem key={item} value={item}>{item}</SelectItem>)}
|
||||
<SelectItem value={ALL_NAMESPACES}>{t('filter.namespaceAll')}</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<TimeRangePicker start={start} end={end} onChange={(nextStart, nextEnd) => { setStart(nextStart); setEnd(nextEnd) }} />
|
||||
<TimeRangePicker start={selStart} end={selEnd} onChange={(nextStart, nextEnd) => { setSelStart(nextStart); setSelEnd(nextEnd) }} />
|
||||
{pendingApply && (
|
||||
<span className="text-xs text-warning">{t('filter.pendingApply')}</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{!namespaceReady && sourceId ? (
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
import { memo, useCallback, useEffect, useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { systemApi, type RetentionSettings } from '@/api/system'
|
||||
import { toast } from 'sonner'
|
||||
|
||||
/**
|
||||
* 数据保留 — 平台自身产生的小数据保留多久。
|
||||
*
|
||||
* 注意与「Loki 日志保留期」区分:那个是部署级配置(组件部署 → 保留策略),
|
||||
* 管的是日志本身;这里管的是平台库里的投递历史与审计日志。
|
||||
*/
|
||||
export const RetentionSection = memo(function RetentionSection() {
|
||||
const { t } = useTranslation('settings')
|
||||
const [settings, setSettings] = useState<RetentionSettings>({ alertDeliveryDays: 7, auditLogDays: 90 })
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [saving, setSaving] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false
|
||||
systemApi
|
||||
.retention()
|
||||
.then(value => {
|
||||
if (!cancelled && value) setSettings(value)
|
||||
})
|
||||
.catch(() => undefined)
|
||||
.finally(() => {
|
||||
if (!cancelled) setLoading(false)
|
||||
})
|
||||
return () => {
|
||||
cancelled = true
|
||||
}
|
||||
}, [])
|
||||
|
||||
const save = useCallback(async () => {
|
||||
setSaving(true)
|
||||
try {
|
||||
const value = await systemApi.saveRetention(settings)
|
||||
if (value) setSettings(value)
|
||||
toast.success(t('retention.saved'))
|
||||
} catch (err: unknown) {
|
||||
toast.error(err instanceof Error ? err.message : t('retention.saveFailed'))
|
||||
} finally {
|
||||
setSaving(false)
|
||||
}
|
||||
}, [settings, t])
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader className="pb-3">
|
||||
<CardTitle className="text-base">{t('retention.title')}</CardTitle>
|
||||
<p className="text-xs text-muted-foreground">{t('retention.desc')}</p>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
|
||||
<div className="flex flex-col">
|
||||
<label className="text-sm font-medium" htmlFor="retention-alert">
|
||||
{t('retention.alertLabel')}
|
||||
</label>
|
||||
<Input
|
||||
id="retention-alert"
|
||||
type="number"
|
||||
min={1}
|
||||
max={365}
|
||||
className="mt-1"
|
||||
disabled={loading}
|
||||
value={settings.alertDeliveryDays}
|
||||
onChange={e => setSettings(s => ({ ...s, alertDeliveryDays: Number(e.target.value) || 0 }))}
|
||||
/>
|
||||
<p className="mt-1 min-h-4 text-xs text-muted-foreground">{t('retention.alertHint')}</p>
|
||||
</div>
|
||||
<div className="flex flex-col">
|
||||
<label className="text-sm font-medium" htmlFor="retention-audit">
|
||||
{t('retention.auditLabel')}
|
||||
</label>
|
||||
<Input
|
||||
id="retention-audit"
|
||||
type="number"
|
||||
min={1}
|
||||
max={3650}
|
||||
className="mt-1"
|
||||
disabled={loading}
|
||||
value={settings.auditLogDays}
|
||||
onChange={e => setSettings(s => ({ ...s, auditLogDays: Number(e.target.value) || 0 }))}
|
||||
/>
|
||||
<p className="mt-1 min-h-4 text-xs text-muted-foreground">{t('retention.auditHint')}</p>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex justify-end">
|
||||
<Button onClick={() => void save()} disabled={saving || loading}>
|
||||
{saving ? t('retention.saving') : t('retention.save')}
|
||||
</Button>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
})
|
||||
@@ -11,6 +11,7 @@ import { clearAuth } from '@/lib/auth-storage'
|
||||
import { toast } from 'sonner'
|
||||
import { PageContainer } from '@/components/PageContainer'
|
||||
import { ThemeSection } from './components/ThemeSection'
|
||||
import { RetentionSection } from './components/RetentionSection'
|
||||
import { LanguageSection } from '@/i18n/LanguageSection'
|
||||
import { Palette } from 'lucide-react'
|
||||
|
||||
@@ -80,6 +81,7 @@ const SettingsPage = memo(function SettingsPage() {
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-3">
|
||||
<RetentionSection />
|
||||
<ThemeSection />
|
||||
<LanguageSection />
|
||||
</CardContent>
|
||||
|
||||
Reference in new issue
Block a user