Skip to content

fix: filter DB connect additional params by component contract - #678

Open
LordofAvernus wants to merge 2 commits into
mainfrom
dms/fix-3183
Open

LordofAvernus wants to merge 2 commits into
mainfrom
dms/fix-3183

Conversation

@LordofAvernus

@LordofAvernus LordofAvernus commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

User description

关联的 issue

https://github.com/actiontech/sqle-ee/issues/3183

描述你的变更

  • DMS 连通性检查按组件契约裁剪 additional_params,避免把 Provision 独有的 database_name 广播给 SQLE
  • 未知/拼错参数仍保留下发,由目标组件显式失败
  • 增加单测覆盖过滤语义

确认项(pr提交后操作)

Tip

请在指定复审人之前,确认并完成以下事项,完成后✅


  • 我已完成自测
  • 我已记录完整日志方便进行诊断
  • 我已在关联的issue里补充了实现方案
  • 我已在关联的issue里补充了测试影响面
  • 我已确认了变更的兼容性,如果不兼容则在issue里标记 not_compatible
  • 我已确认了是否要更新文档,如果要更新则在issue里标记 need_update_doc


Description

  • 新增过滤函数 filterAdditionalParamsByComponentContract

  • IsConnectable 请求中集成参数过滤逻辑

  • 增加单元测试验证过滤逻辑

  • 扩展 PluginUsecase 参数管理及刷新机制


Diagram Walkthrough

flowchart LR
  A["新增函数 filterAdditionalParamsByComponentContract"] --> B["DBServiceUsecase 中调用过滤"]
  B --> C["IsConnectable 请求添加过滤逻辑"]
  C --> D["单元测试验证过滤功能"]
  D --> E["PluginUsecase 更新组件参数管理"]
Loading

File Walkthrough

Relevant files
Bug fix
db_service.go
更新 DBService 中参数过滤逻辑                                                                         

internal/dms/biz/db_service.go

  • 添加过滤逻辑函数实现
  • 修改 IsConnectable 调用过滤函数
  • 处理跨组件参数广播问题
+46/-1   
Tests
db_service_additional_params_test.go
添加 DBService 过滤逻辑单测                                                                           

internal/dms/biz/db_service_additional_params_test.go

  • 新增多场景单元测试
  • 覆盖各类参数过滤组合
+86/-0   
Enhancement
plugin.go
扩展 PluginUsecase 参数管理功能                                                                   

internal/dms/biz/plugin.go

  • 新增刷新组件参数集合函数
  • 添加获取组件和联合参数函数
  • 清理缓存逻辑更新
+67/-0   

@github-actions

Copy link
Copy Markdown

PR Reviewer Guide 🔍

⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

并发共享风险

在 IsConnectable 方法中,针对每个 goroutine 都直接执行了 "reqParams := params" 操作,这里仅做了浅拷贝。如果 params 中的 AdditionalParams 底层切片在多个 goroutine 中共享,可能会在极端并发场景下引发数据竞争风险,尽管目前仅做了赋值操作。建议在修改 AdditionalParams 前进行深拷贝,确保各个 goroutine 拥有独立的数据副本。

reqParams := params
if d.pluginUsecase != nil && len(unionAllowlist) > 0 {
	componentAllowlist, cErr := d.pluginUsecase.GetComponentParamNamesForDBType(ctx, target.Name, params.DBType)
	if cErr != nil {
		d.log.Errorf("get component %s additional params for db_type %s failed: %v; fallback to broadcast", target.Name, params.DBType, cErr)
	} else {
		reqParams.AdditionalParams = filterAdditionalParamsByComponentContract(params.AdditionalParams, componentAllowlist, unionAllowlist)
	}
}

@github-actions

Copy link
Copy Markdown

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
Possible issue
添加并发锁控制

建议引入并使用互斥锁保护全局变量 componentDBTypeParams 的读写操作,防止在并发调用时触发 “concurrent map access” 的
panic 问题。可通过添加 sync.RWMutex 来保护相关方法中对该变量的访问。

internal/dms/biz/plugin.go [292-373]

-var componentDBTypeParams = map[string]map[string]map[string]struct{}{}
+import (
+	"context"
+	"fmt"
+	"sync"
+
+	v1 "github.com/actiontech/dms/pkg/dms-common/api/dms/v1"
+)
+
+var (
+	databaseDriverOptions    []*v1.DatabaseDriverOption
+	componentDBTypeParams    = map[string]map[string]map[string]struct{}{}
+	componentDBTypeParamsMutex sync.RWMutex
+)
 
 func (p *PluginUsecase) ClearDatabaseDriverOptionsCache() {
+	componentDBTypeParamsMutex.Lock()
 	databaseDriverOptions = []*v1.DatabaseDriverOption{}
 	componentDBTypeParams = map[string]map[string]map[string]struct{}{}
+	componentDBTypeParamsMutex.Unlock()
 }
 
 func (p *PluginUsecase) refreshComponentDBTypeParams(optionRes []struct {
 	options []*v1.DatabaseDriverOption
 	source  string
 }) {
 	next := make(map[string]map[string]map[string]struct{}, len(optionRes))
 	for _, res := range optionRes {
 		for _, opt := range res.options {
 			if opt == nil {
 				continue
 			}
 			if next[res.source] == nil {
 				next[res.source] = map[string]map[string]struct{}{}
 			}
 			set := make(map[string]struct{}, len(opt.Params))
 			for _, param := range opt.Params {
 				if param != nil && param.Name != "" {
 					set[param.Name] = struct{}{}
 				}
 			}
 			next[res.source][opt.DBType] = set
 		}
 	}
+	componentDBTypeParamsMutex.Lock()
 	componentDBTypeParams = next
+	componentDBTypeParamsMutex.Unlock()
 }
 
 func (p *PluginUsecase) GetComponentParamNamesForDBType(ctx context.Context, componentName, dbType string) (map[string]struct{}, error) {
 	if p == nil {
 		return map[string]struct{}{}, nil
 	}
+	componentDBTypeParamsMutex.RLock()
 	if len(componentDBTypeParams) == 0 {
+		componentDBTypeParamsMutex.RUnlock()
 		if _, err := p.GetDatabaseDriverOptionsCache(ctx); err != nil {
 			return nil, err
 		}
+		componentDBTypeParamsMutex.RLock()
 	}
+	defer componentDBTypeParamsMutex.RUnlock()
 	if byType, ok := componentDBTypeParams[componentName]; ok {
 		if set, ok := byType[dbType]; ok {
 			return set, nil
 		}
 	}
 	return map[string]struct{}{}, nil
 }
 
 func (p *PluginUsecase) GetUnionParamNamesForDBType(ctx context.Context, dbType string) (map[string]struct{}, error) {
 	if p == nil {
 		return map[string]struct{}{}, nil
 	}
+	componentDBTypeParamsMutex.RLock()
 	if len(componentDBTypeParams) == 0 {
+		componentDBTypeParamsMutex.RUnlock()
 		if _, err := p.GetDatabaseDriverOptionsCache(ctx); err != nil {
 			return nil, err
 		}
+		componentDBTypeParamsMutex.RLock()
 	}
+	defer componentDBTypeParamsMutex.RUnlock()
 	union := map[string]struct{}{}
 	for _, byType := range componentDBTypeParams {
 		for name := range byType[dbType] {
 			union[name] = struct{}{}
 		}
 	}
 	return union, nil
 }
Suggestion importance[1-10]: 9

__

Why: 此建议为全局变量 componentDBTypeParams 引入并发锁保护,防止在多 goroutine 访问时出现 "concurrent map access" 的 panic 问题,从而提高代码在并发场景下的稳定性和安全性。

High

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant