Skip to content

Commit 71fb20b

Browse files
committed
Update.
1 parent 9560ddd commit 71fb20b

File tree

3 files changed

+31
-29
lines changed

3 files changed

+31
-29
lines changed

db/badger.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,20 @@ func getOptimalCacheConfig() CacheConfig {
3535
currentHeapMB := int(m.HeapAlloc / (1024 * 1024))
3636

3737
// 根据内存使用情况动态调整
38-
// 目标:BadgerDB缓存控制在50-100MB之间
38+
// 目标:BadgerDB缓存控制在30-60MB之间,减少内存压力
3939
var totalCacheMB int
40-
if currentHeapMB < 100 {
41-
totalCacheMB = 80 // 系统内存充足时
42-
} else if currentHeapMB < 200 {
43-
totalCacheMB = 60 // 内存适中时
40+
if currentHeapMB < 80 {
41+
totalCacheMB = 50 // 系统内存充足时
42+
} else if currentHeapMB < 150 {
43+
totalCacheMB = 35 // 内存适中时
4444
} else {
45-
totalCacheMB = 40 // 内存紧张时
45+
totalCacheMB = 25 // 内存紧张时,进一步减少缓存
4646
}
4747

48-
// 智能分配缓存
48+
// 智能分配缓存,优化性能和内存使用平衡
4949
return CacheConfig{
50-
BlockCache: totalCacheMB * 50 / 100, // 50%给块缓存(最重要)
51-
IndexCache: totalCacheMB * 30 / 100, // 30%给索引缓存
50+
BlockCache: totalCacheMB * 45 / 100, // 45%给块缓存
51+
IndexCache: totalCacheMB * 35 / 100, // 35%给索引缓存
5252
MemTable: totalCacheMB * 20 / 100, // 20%给内存表
5353
}
5454
}

service/singleton/servicesentinel.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,35 +1255,37 @@ func (ss *ServiceSentinel) getTaskPriority(taskType string) int {
12551255
func (ss *ServiceSentinel) getTaskTimeout(taskType string) time.Duration {
12561256
switch taskType {
12571257
case "badger_save_tcp_ping", "badger_save_service":
1258-
return 30 * time.Second // 增加数据库操作超时时间
1258+
return 60 * time.Second // 数据库操作超时时间
12591259
case "traffic_save":
1260-
return 3 * time.Minute // 流量保存任务需要更长时间,增加到3分钟
1260+
return 5 * time.Minute // 流量保存任务超时时间
12611261
case "full_data_save":
1262-
return 5 * time.Minute // 全量数据保存需要更长时间,增加到5分钟
1263-
case "scheduled_cleanup", "hourly_cleanup":
1264-
return 10 * time.Minute // 清理任务可能需要很长时间
1262+
return 8 * time.Minute // 全量数据保存超时时间
1263+
case "scheduled_cleanup":
1264+
return 15 * time.Minute // 定期清理任务超时时间
1265+
case "hourly_cleanup":
1266+
return 12 * time.Minute // 每小时清理任务超时时间
12651267
case "notification":
1266-
return 30 * time.Second // 通知可能较慢
1268+
return 60 * time.Second // 通知超时时间
12671269
default:
1268-
return 30 * time.Second // 增加默认超时时间
1270+
return 60 * time.Second // 默认超时时间
12691271
}
12701272
}
12711273

12721274
// getMaxRetries 获取最大重试次数
12731275
func (ss *ServiceSentinel) getMaxRetries(taskType string) int {
12741276
switch taskType {
12751277
case "badger_save_tcp_ping", "badger_save_service":
1276-
return 2 // 数据保存减少重试次数,避免重复超时
1278+
return 1 // 数据保存减少重试次数,避免重复超时
12771279
case "traffic_save":
1278-
return 3 // 流量保存任务重试3次
1280+
return 2 // 流量保存任务重试2次
12791281
case "full_data_save":
1280-
return 2 // 全量保存减少重试次数
1282+
return 1 // 全量保存减少重试次数
12811283
case "scheduled_cleanup", "hourly_cleanup":
1282-
return 1 // 清理任务只重试1次
1284+
return 0 // 清理任务不重试,避免重复执行
12831285
case "notification":
12841286
return 1 // 通知重试1次
12851287
default:
1286-
return 2
1288+
return 1
12871289
}
12881290
}
12891291

service/singleton/singleton.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ var (
3535

3636
// 服务器状态缓存,减少数据库访问
3737
serverStatusCache *cache.Cache
38-
38+
3939
// BadgerDB 数据保存任务互斥锁,防止并发冲突
4040
badgerDBSaveMutex sync.Mutex
4141
)
@@ -355,8 +355,8 @@ func LoadSingleton() {
355355
log.Printf("添加监控历史记录一致性检查任务失败: %v", err)
356356
}
357357

358-
// 添加定时清理任务,每6小时执行一次 - 通过ServiceSentinel Worker池执行,减少频率以避免超时
359-
Cron.AddFunc("0 0 */6 * * *", func() {
358+
// 添加定时清理任务,每12小时执行一次 - 进一步减少频率以避免超时
359+
Cron.AddFunc("0 0 */12 * * *", func() {
360360
if ServiceSentinelShared != nil {
361361
ServiceSentinelShared.SubmitOptimizedTask("scheduled_cleanup", nil, func() error {
362362
CleanMonitorHistory()
@@ -403,8 +403,8 @@ func LoadSingleton() {
403403
}
404404
})
405405

406-
// 定期清理任务,每小时执行一次 - 通过ServiceSentinel Worker池执行
407-
Cron.AddFunc("0 0 * * * *", func() {
406+
// 定期清理任务,每2小时执行一次 - 减少频率以避免超时
407+
Cron.AddFunc("0 0 */2 * * *", func() {
408408
if ServiceSentinelShared != nil {
409409
ServiceSentinelShared.SubmitOptimizedTask("hourly_cleanup", nil, func() error {
410410
performHourlyCleanup()
@@ -1267,7 +1267,7 @@ func SaveAllTrafficToBadgerDB() {
12671267
// 使用互斥锁防止多个数据保存任务同时执行
12681268
badgerDBSaveMutex.Lock()
12691269
defer badgerDBSaveMutex.Unlock()
1270-
1270+
12711271
// 使用读锁获取服务器列表
12721272
ServerLock.RLock()
12731273
serverData := make(map[uint64]*model.Server)
@@ -1297,7 +1297,7 @@ func SaveAllTrafficToBadgerDB() {
12971297
start := time.Now()
12981298
err := serverOps.UpdateServerTraffic(serverID, server.CumulativeNetInTransfer, server.CumulativeNetOutTransfer)
12991299
elapsed := time.Since(start)
1300-
1300+
13011301
if err != nil {
13021302
log.Printf("保存服务器 %s (ID:%d) 累计流量失败: %v (耗时: %v)", server.Name, serverID, err, elapsed)
13031303
} else {
@@ -2086,7 +2086,7 @@ func OptimizeDatabase() {
20862086
if err := DB.WithContext(vacCtx).Exec("VACUUM").Error; err != nil {
20872087
log.Printf("VACUUM操作失败: %v", err)
20882088
} else {
2089-
log.Println("VACUUM操作完成")
2089+
log.Println("VACUUM操作完成")
20902090
}
20912091
}
20922092
return nil

0 commit comments

Comments
 (0)