实现七月迭代公共技术基础
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 9m20s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 9m20s
This commit is contained in:
178
internal/application/systemconfig/update.go
Normal file
178
internal/application/systemconfig/update.go
Normal file
@@ -0,0 +1,178 @@
|
||||
// Package systemconfig 提供受控系统配置的简单写事务脚本。
|
||||
package systemconfig
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
stderrors "errors"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
|
||||
configinfra "github.com/break/junhong_cmp_fiber/internal/infrastructure/systemconfig"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model/dto"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/middleware"
|
||||
)
|
||||
|
||||
// ChangeAudit 是系统配置更新交给统一审计 Port 的事实。
|
||||
type ChangeAudit struct {
|
||||
OperatorID uint
|
||||
OperationType string
|
||||
Description string
|
||||
ConfigKey string
|
||||
BeforeData map[string]any
|
||||
AfterData map[string]any
|
||||
RequestID string
|
||||
CorrelationID string
|
||||
}
|
||||
|
||||
// AuditWriter 由 tech-global-audit 提供事务内实现。
|
||||
type AuditWriter interface {
|
||||
WriteConfigChange(ctx context.Context, tx *gorm.DB, audit ChangeAudit) error
|
||||
}
|
||||
|
||||
// UnavailableAuditWriter 是统一审计尚未注入时的失败关闭策略。
|
||||
type UnavailableAuditWriter struct{}
|
||||
|
||||
// WriteConfigChange 拒绝在缺少统一审计时修改敏感配置。
|
||||
func (UnavailableAuditWriter) WriteConfigChange(_ context.Context, _ *gorm.DB, _ ChangeAudit) error {
|
||||
return stderrors.New("统一审计接缝尚未配置")
|
||||
}
|
||||
|
||||
// UpdateService 执行单 Key 校验、事务更新、审计和提交后缓存失效。
|
||||
type UpdateService struct {
|
||||
db *gorm.DB
|
||||
registry *configinfra.Registry
|
||||
cache configinfra.Cache
|
||||
audit AuditWriter
|
||||
alerts configinfra.AlertSink
|
||||
now func() time.Time
|
||||
}
|
||||
|
||||
// NewUpdateService 创建系统配置更新事务脚本。
|
||||
func NewUpdateService(
|
||||
db *gorm.DB,
|
||||
registry *configinfra.Registry,
|
||||
cache configinfra.Cache,
|
||||
audit AuditWriter,
|
||||
alerts configinfra.AlertSink,
|
||||
now func() time.Time,
|
||||
) *UpdateService {
|
||||
if audit == nil {
|
||||
audit = UnavailableAuditWriter{}
|
||||
}
|
||||
if now == nil {
|
||||
now = time.Now
|
||||
}
|
||||
return &UpdateService{db: db, registry: registry, cache: cache, audit: audit, alerts: alerts, now: now}
|
||||
}
|
||||
|
||||
// Execute 更新一个已注册且非只读的配置 Key。
|
||||
func (s *UpdateService) Execute(ctx context.Context, key string, request dto.UpdateSystemConfigRequest) (*dto.SystemConfigItem, error) {
|
||||
if middleware.GetUserTypeFromContext(ctx) != constants.UserTypeSuperAdmin {
|
||||
return nil, errors.New(errors.CodeForbidden)
|
||||
}
|
||||
operatorID := middleware.GetUserIDFromContext(ctx)
|
||||
if operatorID == 0 || key == "" {
|
||||
return nil, errors.New(errors.CodeInvalidParam)
|
||||
}
|
||||
definition, registered := s.registry.Get(key)
|
||||
if !registered {
|
||||
return nil, errors.New(errors.CodeInvalidParam, "系统配置 Key 未注册")
|
||||
}
|
||||
if definition.Readonly {
|
||||
return nil, errors.New(errors.CodeInvalidStatus, "系统配置为只读,不能更新")
|
||||
}
|
||||
if err := configinfra.ValidateValue(definition, request.Value); err != nil {
|
||||
return nil, errors.New(errors.CodeInvalidParam, "系统配置值不符合注册规则")
|
||||
}
|
||||
now := s.now().UTC()
|
||||
var saved model.SystemConfig
|
||||
err := s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
// 同一 Key 的首次创建和后续更新都由 PostgreSQL 事务级咨询锁串行裁决。
|
||||
if err := tx.Exec("SELECT pg_advisory_xact_lock(hashtext(?))", key).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
var existing model.SystemConfig
|
||||
findErr := tx.Clauses(clause.Locking{Strength: "UPDATE"}).Where("config_key = ?", key).First(&existing).Error
|
||||
beforeValue := definition.DefaultValue
|
||||
if findErr == nil {
|
||||
beforeValue = existing.ConfigValue
|
||||
} else if findErr != gorm.ErrRecordNotFound {
|
||||
return findErr
|
||||
}
|
||||
if findErr == gorm.ErrRecordNotFound {
|
||||
existing = model.SystemConfig{
|
||||
ConfigKey: key, ConfigValue: request.Value, ValueType: definition.ValueType,
|
||||
Module: definition.Module, Description: definition.Description,
|
||||
IsReadonly: definition.Readonly, IsSensitive: definition.Sensitive,
|
||||
Creator: operatorID, Updater: operatorID, CreatedAt: now, UpdatedAt: now,
|
||||
}
|
||||
if err := tx.Create(&existing).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err := tx.Model(&model.SystemConfig{}).Where("id = ?", existing.ID).Updates(map[string]any{
|
||||
"config_value": request.Value, "value_type": definition.ValueType,
|
||||
"module": definition.Module, "description": definition.Description,
|
||||
"is_readonly": definition.Readonly, "is_sensitive": definition.Sensitive,
|
||||
"updater": operatorID, "updated_at": now,
|
||||
}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
existing.ConfigValue = request.Value
|
||||
existing.Updater = operatorID
|
||||
existing.UpdatedAt = now
|
||||
}
|
||||
requestID := ""
|
||||
if value := middleware.GetRequestIDFromContext(ctx); value != nil {
|
||||
requestID = *value
|
||||
}
|
||||
if err := s.audit.WriteConfigChange(ctx, tx, ChangeAudit{
|
||||
OperatorID: operatorID, OperationType: "system_config_update", Description: "更新受控系统配置",
|
||||
ConfigKey: key,
|
||||
BeforeData: map[string]any{"config_key": key, "value": auditValue(definition, beforeValue)},
|
||||
AfterData: map[string]any{"config_key": key, "value": auditValue(definition, request.Value)},
|
||||
RequestID: requestID, CorrelationID: requestID,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
saved = existing
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(errors.CodeDatabaseError, err, "更新系统配置失败")
|
||||
}
|
||||
s.registry.Remember(key, request.Value)
|
||||
if s.cache != nil {
|
||||
if err := s.cache.Delete(ctx, constants.RedisSystemConfigKey(key)); err != nil {
|
||||
if s.alerts != nil {
|
||||
s.alerts.Warn(ctx, "SYSTEM_CONFIG_CACHE_INVALIDATE_FAILED", "system_config", key, "系统配置缓存失效失败,数据库事实已提交")
|
||||
}
|
||||
}
|
||||
}
|
||||
value := saved.ConfigValue
|
||||
if definition.Sensitive && value != "" {
|
||||
value = "[已配置]"
|
||||
}
|
||||
updatedAt := saved.UpdatedAt
|
||||
return &dto.SystemConfigItem{
|
||||
ConfigKey: key, Value: value, ValueType: definition.ValueType, Module: definition.Module,
|
||||
Description: definition.Description, Readonly: definition.Readonly, Sensitive: definition.Sensitive,
|
||||
Registered: true, Control: definition.Control, EnumValues: definition.EnumValues,
|
||||
Min: definition.Min, Max: definition.Max, UpdatedAt: &updatedAt,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func auditValue(definition configinfra.Definition, value string) string {
|
||||
if !definition.Sensitive {
|
||||
return value
|
||||
}
|
||||
sum := sha256.Sum256([]byte(value))
|
||||
return "[敏感值 sha256:" + hex.EncodeToString(sum[:8]) + "]"
|
||||
}
|
||||
141
internal/application/systemconfig/update_integration_test.go
Normal file
141
internal/application/systemconfig/update_integration_test.go
Normal file
@@ -0,0 +1,141 @@
|
||||
package systemconfig_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/bytedance/sonic"
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
|
||||
systemconfigapp "github.com/break/junhong_cmp_fiber/internal/application/systemconfig"
|
||||
systemconfiginfra "github.com/break/junhong_cmp_fiber/internal/infrastructure/systemconfig"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model/dto"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/config"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/middleware"
|
||||
)
|
||||
|
||||
type transactionAuditWriter struct{}
|
||||
|
||||
func (transactionAuditWriter) WriteConfigChange(_ context.Context, tx *gorm.DB, audit systemconfigapp.ChangeAudit) error {
|
||||
before, _ := sonic.Marshal(audit.BeforeData)
|
||||
after, _ := sonic.Marshal(audit.AfterData)
|
||||
return tx.Exec(`INSERT INTO test_system_config_audit
|
||||
(config_key, operator_id, before_data, after_data) VALUES (?, ?, ?::jsonb, ?::jsonb)`,
|
||||
audit.ConfigKey, audit.OperatorID, string(before), string(after)).Error
|
||||
}
|
||||
|
||||
func TestConcurrentFirstSystemConfigUpdatesAreSerializedByPostgres(t *testing.T) {
|
||||
if os.Getenv("JUNHONG_DATABASE_HOST") == "" {
|
||||
t.Skip("未加载 .env.local,跳过依赖真实 PostgreSQL 的并发集成测试")
|
||||
}
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
t.Fatalf("加载测试配置失败:%v", err)
|
||||
}
|
||||
schema := "test_system_config_" + strings.ReplaceAll(uuid.NewString(), "-", "")
|
||||
adminDB := openSchemaDatabase(t, &cfg.Database, "public")
|
||||
if err := adminDB.Exec("CREATE SCHEMA " + schema).Error; err != nil {
|
||||
t.Fatalf("创建隔离 schema 失败:%v", err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
_ = adminDB.Exec("DROP SCHEMA " + schema + " CASCADE").Error
|
||||
closeDatabase(adminDB)
|
||||
})
|
||||
|
||||
firstDB := openSchemaDatabase(t, &cfg.Database, schema)
|
||||
secondDB := openSchemaDatabase(t, &cfg.Database, schema)
|
||||
t.Cleanup(func() {
|
||||
closeDatabase(firstDB)
|
||||
closeDatabase(secondDB)
|
||||
})
|
||||
createConcurrentConfigTables(t, firstDB)
|
||||
registry := systemconfiginfra.NewRegistry()
|
||||
definition := systemconfiginfra.Definition{
|
||||
Key: "foundation.concurrent.value", Module: "foundation", ValueType: constants.SystemConfigTypeString,
|
||||
DefaultValue: "default", Description: "并发配置测试值",
|
||||
}
|
||||
if err := registry.Register(definition); err != nil {
|
||||
t.Fatalf("注册并发测试配置失败:%v", err)
|
||||
}
|
||||
ctx := middleware.SetUserContext(context.Background(), &middleware.UserContextInfo{UserID: 7, UserType: constants.UserTypeSuperAdmin})
|
||||
services := []*systemconfigapp.UpdateService{
|
||||
systemconfigapp.NewUpdateService(firstDB, registry, nil, transactionAuditWriter{}, nil, nil),
|
||||
systemconfigapp.NewUpdateService(secondDB, registry, nil, transactionAuditWriter{}, nil, nil),
|
||||
}
|
||||
values := []string{"first", "second"}
|
||||
start := make(chan struct{})
|
||||
errorsFound := make(chan error, len(services))
|
||||
var waitGroup sync.WaitGroup
|
||||
for index := range services {
|
||||
waitGroup.Add(1)
|
||||
go func(index int) {
|
||||
defer waitGroup.Done()
|
||||
<-start
|
||||
_, executeErr := services[index].Execute(ctx, definition.Key, dto.UpdateSystemConfigRequest{Value: values[index]})
|
||||
errorsFound <- executeErr
|
||||
}(index)
|
||||
}
|
||||
close(start)
|
||||
waitGroup.Wait()
|
||||
close(errorsFound)
|
||||
for executeErr := range errorsFound {
|
||||
if executeErr != nil {
|
||||
t.Fatalf("并发更新不应产生唯一键冲突:%v", executeErr)
|
||||
}
|
||||
}
|
||||
var stored model.SystemConfig
|
||||
if err := firstDB.Where("config_key = ?", definition.Key).First(&stored).Error; err != nil {
|
||||
t.Fatalf("读取并发更新结果失败:%v", err)
|
||||
}
|
||||
if stored.ConfigValue != values[0] && stored.ConfigValue != values[1] {
|
||||
t.Fatalf("并发更新保存了非法结果:%q", stored.ConfigValue)
|
||||
}
|
||||
var auditCount int64
|
||||
if err := firstDB.Table("test_system_config_audit").Where("config_key = ?", definition.Key).Count(&auditCount).Error; err != nil || auditCount != 2 {
|
||||
t.Fatalf("并发更新审计事实不完整:错误=%v,数量=%d", err, auditCount)
|
||||
}
|
||||
}
|
||||
|
||||
func openSchemaDatabase(t *testing.T, cfg *config.DatabaseConfig, schema string) *gorm.DB {
|
||||
t.Helper()
|
||||
dsn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=%s search_path=%s TimeZone=Asia/Shanghai",
|
||||
cfg.Host, cfg.Port, cfg.User, cfg.Password, cfg.DBName, cfg.SSLMode, schema)
|
||||
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{SkipDefaultTransaction: true})
|
||||
if err != nil {
|
||||
t.Fatalf("连接隔离 PostgreSQL 失败:%v", err)
|
||||
}
|
||||
return db
|
||||
}
|
||||
|
||||
func closeDatabase(db *gorm.DB) {
|
||||
if sqlDB, err := db.DB(); err == nil {
|
||||
_ = sqlDB.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func createConcurrentConfigTables(t *testing.T, db *gorm.DB) {
|
||||
t.Helper()
|
||||
if err := db.Exec(`CREATE TABLE tb_system_config (
|
||||
id bigserial PRIMARY KEY, config_key varchar(150) NOT NULL UNIQUE, config_value text NOT NULL,
|
||||
value_type varchar(20) NOT NULL, module varchar(100) NOT NULL, description varchar(500) NOT NULL,
|
||||
is_readonly boolean NOT NULL DEFAULT false, is_sensitive boolean NOT NULL DEFAULT false,
|
||||
creator bigint NOT NULL DEFAULT 0, updater bigint NOT NULL DEFAULT 0,
|
||||
created_at timestamptz NOT NULL DEFAULT NOW(), updated_at timestamptz NOT NULL DEFAULT NOW()
|
||||
)`).Error; err != nil {
|
||||
t.Fatalf("创建隔离配置表失败:%v", err)
|
||||
}
|
||||
if err := db.Exec(`CREATE TABLE test_system_config_audit (
|
||||
id bigserial PRIMARY KEY, config_key varchar(150) NOT NULL, operator_id bigint NOT NULL,
|
||||
before_data jsonb NOT NULL, after_data jsonb NOT NULL
|
||||
)`).Error; err != nil {
|
||||
t.Fatalf("创建隔离审计表失败:%v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user