完善配置快照失败恢复提示与重试测试
Security Checks / security (push) Waiting to run

This commit is contained in:
xuans committed 2026-08-30 22:55:41 +08:00
1 parent 476c85ac56
commit e603295df3
7 files changed
+118 -5

No files matched your search

+1 -1
View File
@@ -77,7 +77,7 @@ func (m *ConfigManager) SwitchDatabase(ctx context.Context, expectedGeneration u
if err := m.transactions.Commit(current, candidate); err != nil {
return err
}
if err := m.snapshots.Publish(candidate); err != nil {
if err := m.publishSnapshot(candidate); err != nil {
return err
}
now := time.Now().UTC()
+23 -4
View File
@@ -217,7 +217,7 @@ func (m *ConfigManager) Load(ctx context.Context) error {
if err := contextError(ctx); err != nil {
return err
}
if err := m.snapshots.Publish(runtime); err != nil {
if err := m.publishSnapshot(runtime); err != nil {
return err
}
now := runtime.LoadedAt
@@ -395,7 +395,7 @@ func (m *ConfigManager) apply(ctx context.Context, generation uint64, actor Audi
if err := m.transactions.Commit(current, candidate); err != nil {
return nil, err
}
if err := m.snapshots.Publish(candidate); err != nil {
if err := m.publishSnapshot(candidate); err != nil {
return nil, err
}
now := time.Now().UTC()
@@ -554,6 +554,25 @@ func rollbackTicketRetryable(err error) bool {
errors.Is(err, ErrRuntimeSnapshotUnavailable)
}
func (m *ConfigManager) publishSnapshot(candidate RuntimeConfig) error {
if m == nil || m.snapshots == nil {
return ErrRuntimeSnapshotUnavailable
}
if err := m.snapshots.Publish(candidate); err != nil {
if errors.Is(err, ErrRuntimeSnapshotUnavailable) {
m.mu.Lock()
m.status.SetupState = SetupStateDegraded
m.status.DegradedReasons = []PublicIssue{{
Code: "RUNTIME_SNAPSHOT_UNAVAILABLE",
Message: "配置已保存,但运行时配置尚未切换,请重试应用",
}}
m.mu.Unlock()
}
return err
}
return nil
}
func (m *ConfigManager) rollback(ctx context.Context, generation uint64, actor AuditActor, confirmation StepUpConfirmation) (*ApplyResult, error) {
result, _, err := m.rollbackWithState(ctx, generation, actor, confirmation)
return result, err
@@ -619,7 +638,7 @@ func (m *ConfigManager) rollbackWithState(ctx context.Context, generation uint64
if err := m.transactions.Commit(current, candidate); err != nil {
return nil, false, err
}
if err := m.snapshots.Publish(candidate); err != nil {
if err := m.publishSnapshot(candidate); err != nil {
return nil, false, err
}
now := time.Now().UTC()
@@ -942,7 +961,7 @@ func (m *ConfigManager) MigrateLegacyPluginSecrets(ctx context.Context, importer
if err := m.transactions.Commit(current, candidate); err != nil {
return false, err
}
if err := m.snapshots.Publish(candidate); err != nil {
if err := m.publishSnapshot(candidate); err != nil {
return false, err
}
now := time.Now().UTC()
+52
View File
@@ -1505,6 +1505,58 @@ func (s *flakyRuntimeSnapshotStore) Snapshot() (RuntimeConfig, error) {
return cloneRuntimeConfig(s.current), nil
}
func TestConfigManagerApplyCanRetryAfterSnapshotFailure(t *testing.T) {
root := t.TempDir()
writeRuntimeFixture(t, root, validRuntimeConfig())
options := ConfigLoaderOptions{Validation: RuntimeValidationOptions{HasSuperAdmin: true, Production: true}}
manager, err := NewConfigManager(root, options)
if err != nil {
t.Fatal(err)
}
updatedEmail := manager.Snapshot().Email
updatedEmail.Policy.MaxMessageBytes = 8 << 20
payload, err := json.Marshal(updatedEmail)
if err != nil {
t.Fatal(err)
}
updated, err := manager.Update(context.Background(), UpdateRequest{
Section: SectionEmail, ExpectedGeneration: 1, Payload: payload, Actor: authorizedConfigActor(),
})
if err != nil {
t.Fatal(err)
}
manager.snapshots = &flakyRuntimeSnapshotStore{current: manager.Snapshot(), failures: 1}
if _, err := manager.Apply(context.Background(), updated.Generation); !errors.Is(err, ErrRuntimeSnapshotUnavailable) {
t.Fatalf("first apply error = %v, want snapshot unavailable", err)
}
status := manager.Status()
if status.SetupState != SetupStateDegraded || len(status.DegradedReasons) != 1 || status.DegradedReasons[0].Code != "RUNTIME_SNAPSHOT_UNAVAILABLE" {
t.Fatalf("degraded status after snapshot failure = %+v", status)
}
if manager.Snapshot().Generation != 1 {
t.Fatalf("runtime snapshot generation after failed publish = %d, want 1", manager.Snapshot().Generation)
}
persisted, err := NewConfigLoader(root, options).Load()
if err != nil {
t.Fatal(err)
}
if persisted.Generation != 2 || persisted.Email.Policy.MaxMessageBytes != 8<<20 {
t.Fatalf("persisted candidate after snapshot failure = %+v", persisted)
}
applied, err := manager.Apply(context.Background(), updated.Generation)
if err != nil {
t.Fatal(err)
}
if applied.Generation != 2 || manager.Snapshot().Generation != 2 || manager.Snapshot().Email.Policy.MaxMessageBytes != 8<<20 {
t.Fatalf("retried apply result = %+v, runtime = %+v", applied, manager.Snapshot())
}
if manager.Status().SetupState == SetupStateDegraded || len(manager.Status().DegradedReasons) != 0 {
t.Fatalf("status was not recovered after retry: %+v", manager.Status())
}
}
func TestConfigManagerRollbackTicketCanRetryAfterSnapshotFailure(t *testing.T) {
root := t.TempDir()
writeRuntimeFixture(t, root, validRuntimeConfig())