mirror of
https://github.com/DullJZ/s3-balance.git
synced 2026-09-08 17:36:39 +08:00
Fix config update and health check problem
This commit is contained in:
+30
-11
@@ -35,6 +35,7 @@ type Manager struct {
|
||||
metrics *metrics.Metrics
|
||||
healthMonitor *health.Monitor
|
||||
statsMonitor *health.StatsMonitor
|
||||
monitorCtx context.Context
|
||||
}
|
||||
|
||||
// NewManager 创建新的存储桶管理器
|
||||
@@ -161,6 +162,16 @@ func (m *Manager) initHealthMonitoring() {
|
||||
|
||||
// Start 启动管理器(健康检查和统计更新)
|
||||
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 {
|
||||
m.healthMonitor.Start(ctx)
|
||||
@@ -293,17 +304,16 @@ func (m *Manager) GetRealBuckets() []*BucketInfo {
|
||||
|
||||
// UpdateConfig 更新配置(支持热更新)
|
||||
func (m *Manager) UpdateConfig(newConfig *config.Config) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
log.Println("Updating bucket manager configuration...")
|
||||
|
||||
// 更新配置引用
|
||||
m.mu.Lock()
|
||||
oldConfig := m.config
|
||||
m.config = newConfig
|
||||
|
||||
// 检查是否需要重新创建存储桶
|
||||
needsRestart := m.checkIfRestartNeeded(oldConfig, newConfig)
|
||||
restartMonitors := false
|
||||
|
||||
if needsRestart {
|
||||
log.Println("Bucket configuration changed significantly, recreating buckets...")
|
||||
|
||||
@@ -326,8 +336,9 @@ func (m *Manager) UpdateConfig(newConfig *config.Config) error {
|
||||
|
||||
client, err := createS3Client(bucketCfg)
|
||||
if err != nil {
|
||||
// 如果创建失败,恢复旧配置
|
||||
// 如果创建失败,恢复旧配置并回滚
|
||||
m.config = oldConfig
|
||||
m.mu.Unlock()
|
||||
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()
|
||||
restartMonitors = true
|
||||
} else {
|
||||
// 只更新监控间隔(需要重启监控来改变间隔)
|
||||
log.Println("Updating monitoring intervals...")
|
||||
@@ -354,6 +366,13 @@ func (m *Manager) UpdateConfig(newConfig *config.Config) error {
|
||||
}
|
||||
// 重新初始化监控以应用新的间隔
|
||||
m.initHealthMonitoring()
|
||||
restartMonitors = true
|
||||
}
|
||||
|
||||
m.mu.Unlock()
|
||||
|
||||
if restartMonitors {
|
||||
m.startMonitors()
|
||||
}
|
||||
|
||||
log.Println("Bucket manager configuration updated successfully")
|
||||
@@ -376,12 +395,12 @@ func (m *Manager) checkIfRestartNeeded(oldConfig, newConfig *config.Config) bool
|
||||
|
||||
// 检查关键配置项
|
||||
if oldBucket.Name != newBucket.Name ||
|
||||
oldBucket.Endpoint != newBucket.Endpoint ||
|
||||
oldBucket.AccessKeyID != newBucket.AccessKeyID ||
|
||||
oldBucket.SecretAccessKey != newBucket.SecretAccessKey ||
|
||||
oldBucket.Region != newBucket.Region ||
|
||||
oldBucket.Enabled != newBucket.Enabled ||
|
||||
oldBucket.Virtual != newBucket.Virtual {
|
||||
oldBucket.Endpoint != newBucket.Endpoint ||
|
||||
oldBucket.AccessKeyID != newBucket.AccessKeyID ||
|
||||
oldBucket.SecretAccessKey != newBucket.SecretAccessKey ||
|
||||
oldBucket.Region != newBucket.Region ||
|
||||
oldBucket.Enabled != newBucket.Enabled ||
|
||||
oldBucket.Virtual != newBucket.Virtual {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user