Fix config update and health check problem

This commit is contained in:
DullJZ
2025-09-29 22:22:26 +08:00
parent c92b874bba
commit 579aeb2632
+24 -5
View File
@@ -35,6 +35,7 @@ type Manager struct {
metrics *metrics.Metrics metrics *metrics.Metrics
healthMonitor *health.Monitor healthMonitor *health.Monitor
statsMonitor *health.StatsMonitor statsMonitor *health.StatsMonitor
monitorCtx context.Context
} }
// NewManager 创建新的存储桶管理器 // NewManager 创建新的存储桶管理器
@@ -161,6 +162,16 @@ func (m *Manager) initHealthMonitoring() {
// Start 启动管理器(健康检查和统计更新) // Start 启动管理器(健康检查和统计更新)
func (m *Manager) Start(ctx context.Context) { func (m *Manager) Start(ctx context.Context) {
m.monitorCtx = ctx
m.startMonitors()
}
func (m *Manager) startMonitors() {
ctx := m.monitorCtx
if ctx == nil {
return
}
// 启动健康监控 // 启动健康监控
if m.healthMonitor != nil { if m.healthMonitor != nil {
m.healthMonitor.Start(ctx) m.healthMonitor.Start(ctx)
@@ -293,17 +304,16 @@ func (m *Manager) GetRealBuckets() []*BucketInfo {
// UpdateConfig 更新配置(支持热更新) // UpdateConfig 更新配置(支持热更新)
func (m *Manager) UpdateConfig(newConfig *config.Config) error { func (m *Manager) UpdateConfig(newConfig *config.Config) error {
m.mu.Lock()
defer m.mu.Unlock()
log.Println("Updating bucket manager configuration...") log.Println("Updating bucket manager configuration...")
// 更新配置引用 m.mu.Lock()
oldConfig := m.config oldConfig := m.config
m.config = newConfig m.config = newConfig
// 检查是否需要重新创建存储桶 // 检查是否需要重新创建存储桶
needsRestart := m.checkIfRestartNeeded(oldConfig, newConfig) needsRestart := m.checkIfRestartNeeded(oldConfig, newConfig)
restartMonitors := false
if needsRestart { if needsRestart {
log.Println("Bucket configuration changed significantly, recreating buckets...") log.Println("Bucket configuration changed significantly, recreating buckets...")
@@ -326,8 +336,9 @@ func (m *Manager) UpdateConfig(newConfig *config.Config) error {
client, err := createS3Client(bucketCfg) client, err := createS3Client(bucketCfg)
if err != nil { if err != nil {
// 如果创建失败,恢复旧配置 // 如果创建失败,恢复旧配置并回滚
m.config = oldConfig m.config = oldConfig
m.mu.Unlock()
return fmt.Errorf("failed to create S3 client for bucket %s: %v", bucketCfg.Name, err) return fmt.Errorf("failed to create S3 client for bucket %s: %v", bucketCfg.Name, err)
} }
@@ -343,6 +354,7 @@ func (m *Manager) UpdateConfig(newConfig *config.Config) error {
// 重新初始化监控 // 重新初始化监控
m.initHealthMonitoring() m.initHealthMonitoring()
restartMonitors = true
} else { } else {
// 只更新监控间隔(需要重启监控来改变间隔) // 只更新监控间隔(需要重启监控来改变间隔)
log.Println("Updating monitoring intervals...") log.Println("Updating monitoring intervals...")
@@ -354,6 +366,13 @@ func (m *Manager) UpdateConfig(newConfig *config.Config) error {
} }
// 重新初始化监控以应用新的间隔 // 重新初始化监控以应用新的间隔
m.initHealthMonitoring() m.initHealthMonitoring()
restartMonitors = true
}
m.mu.Unlock()
if restartMonitors {
m.startMonitors()
} }
log.Println("Bucket manager configuration updated successfully") log.Println("Bucket manager configuration updated successfully")