Skip to content

Commit 0eb1df8

Browse files
Refactor: Fix RootModel generation and owner config issues
Co-authored-by: yourton.ma <yourton.ma@gmail.com>
1 parent fc841ed commit 0eb1df8

File tree

6 files changed

+841
-417
lines changed

6 files changed

+841
-417
lines changed

FINAL_FIX_SUMMARY.md

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
# OpenMetadata Owner Config - 完整修复总结
2+
3+
## ✅ 已完成的修复
4+
5+
### 1. 多线程竞态条件修复(已完成)
6+
7+
**问题**: Worker线程复制context时,database_owner还未存储,导致继承失效
8+
9+
**修复文件**:
10+
-`ingestion/src/metadata/ingestion/source/database/common_db_source.py` (第220-238行, 279-302行)
11+
-`ingestion/src/metadata/ingestion/source/database/database_service.py` (第652行, 第695行)
12+
13+
**关键改动**:
14+
```python
15+
# 修复前:先yield,后存储context(错误顺序)
16+
database_request = CreateDatabaseRequest(owners=...)
17+
yield Either(right=database_request) # ← Worker线程可能在这里启动
18+
context.upsert("database_owner", ...) # ← 太晚了!
19+
20+
# 修复后:先存储context,后yield(正确顺序)
21+
database_owner_ref = self.get_database_owner_ref(database_name)
22+
context.upsert("database_owner", database_owner_name) # ← 先存储
23+
database_request = CreateDatabaseRequest(owners=database_owner_ref)
24+
yield Either(right=database_request) # ← 然后yield
25+
```
26+
27+
### 2. RootModel 自动修复(已完成)
28+
29+
**问题**: datamodel-code-generator 生成的 RootModel 包含不支持的 model_config
30+
31+
**修复文件**:
32+
-`scripts/datamodel_generation.py` (添加自动修复逻辑)
33+
34+
**修复逻辑**:
35+
```python
36+
# 在代码生成后自动扫描并修复所有 RootModel
37+
# 移除: model_config = ConfigDict(extra="forbid")
38+
# 保留: class XXX(RootModel[...]): 和 root: Type
39+
```
40+
41+
### 3. 文档更新(已完成)
42+
43+
**创建的文档**:
44+
-`ROOT_MODEL_PERMANENT_FIX.md` - RootModel 根本解决方案
45+
-`fix_rootmodel_generation.py` - 独立修复脚本
46+
-`ingestion/tests/.../TROUBLESHOOTING.md` - 故障排查指南
47+
-`ingestion/tests/.../run-all-tests.sh` - 路径修复
48+
49+
## 🚀 使用新的修复方案
50+
51+
### 方案 A: 自动修复(推荐)⭐
52+
53+
现在每次运行 `mvn clean install` 都会**自动修复** RootModel 问题:
54+
55+
```bash
56+
cd ~/workspaces/OpenMetadata
57+
58+
# 1. 重新生成所有模型(会自动修复RootModel)
59+
cd openmetadata-spec
60+
mvn clean install
61+
62+
# 2. 重新安装 ingestion
63+
cd ../ingestion
64+
pip install -e . --force-reinstall --no-deps
65+
66+
# 3. 验证修复
67+
python3 -c "from metadata.generated.schema.type import ownerConfig; print('✅ Success')"
68+
69+
# 4. 运行测试
70+
metadata ingest -c tests/unit/metadata/ingestion/owner_config_tests/test-01-basic-configuration.yaml
71+
```
72+
73+
**输出示例**:
74+
```
75+
...
76+
# Fixing RootModel model_config issues...
77+
✓ Fixed RootModel in: ingestion/src/metadata/generated/schema/type/ownerConfig.py
78+
✓ Fixed RootModel in: ingestion/src/metadata/generated/schema/type/someOther.py
79+
# Fixed 2 file(s) with RootModel issues
80+
```
81+
82+
### 方案 B: 手动修复(临时)
83+
84+
如果不想重新生成,可以使用独立脚本:
85+
86+
```bash
87+
cd ~/workspaces/OpenMetadata
88+
89+
# 运行独立修复脚本
90+
python3 fix_rootmodel_generation.py
91+
92+
# 验证
93+
python3 -c "from metadata.generated.schema.type import ownerConfig; print('✅ Success')"
94+
```
95+
96+
## ⚠️ 当前限制
97+
98+
### Pydantic 数组支持
99+
100+
**问题**: 当前 Pydantic 模型不支持 `List[str]` 形式的 owner 配置
101+
102+
**影响**: Test 3, 4, 7, 8 需要修改配置
103+
104+
**临时解决**: 将数组改为单个字符串
105+
106+
```yaml
107+
# 从:
108+
database:
109+
"finance_db": ["alice", "bob"] # ❌ 数组不支持
110+
111+
# 改为:
112+
database:
113+
"finance_db": "alice" # ✅ 单个字符串
114+
```
115+
116+
**永久解决**: 需要修改 JSON Schema 或 datamodel-code-generator 配置(详见 `ROOT_MODEL_PERMANENT_FIX.md`)
117+
118+
## 📋 测试验证
119+
120+
### 关键测试
121+
122+
**Test 1-2**: 基础配置(应该可以运行)✅
123+
```bash
124+
metadata ingest -c ingestion/tests/unit/metadata/ingestion/owner_config_tests/test-01-basic-configuration.yaml
125+
metadata ingest -c ingestion/tests/unit/metadata/ingestion/owner_config_tests/test-02-fqn-matching.yaml
126+
```
127+
128+
**Test 5-6**: 继承测试(验证多线程修复)✅ **最重要!**
129+
```bash
130+
metadata ingest -c ingestion/tests/unit/metadata/ingestion/owner_config_tests/test-05-inheritance-enabled.yaml
131+
metadata ingest -c ingestion/tests/unit/metadata/ingestion/owner_config_tests/test-06-inheritance-disabled.yaml
132+
```
133+
134+
**Test 3, 4, 7, 8**: 需要修改配置后运行
135+
136+
### 预期结果(Test 5)
137+
138+
验证多线程修复是否成功:
139+
140+
| 实体 | 配置 | 期望Owner | 验证点 |
141+
|------|------|-----------|--------|
142+
| finance_db | ✓ | finance-team | 配置明确 |
143+
| accounting schema | ✗ | **finance-team** | ⭐ 继承(不是default) |
144+
| revenue table | ✗ | **finance-team** | ⭐ 继承(不是default) |
145+
| treasury schema | ✓ | treasury-team | 配置明确 |
146+
| expenses table | ✓ | expense-team | 配置明确 |
147+
148+
如果 accounting 和 revenue 的 owner 是 `finance-team`(而不是 `data-platform-team`),说明**多线程竞态条件修复成功**!🎉
149+
150+
## 🔧 如果遇到问题
151+
152+
### 问题 1: RootModel 错误仍然存在
153+
154+
```bash
155+
# 检查 datamodel_generation.py 是否包含修复代码
156+
grep -A 5 "Fix RootModel" scripts/datamodel_generation.py
157+
158+
# 如果没有,手动运行修复脚本
159+
python3 fix_rootmodel_generation.py
160+
161+
# 或者重新应用 datamodel_generation.py 的修改
162+
git diff scripts/datamodel_generation.py
163+
```
164+
165+
### 问题 2: 数组配置报错
166+
167+
**错误信息**:
168+
```
169+
ValidationError: Input should be a valid string [type=string_type, input_value=['alice', 'bob'], input_type=list]
170+
```
171+
172+
**解决**: 将测试配置中的数组改为单个字符串(见上文"当前限制")
173+
174+
### 问题 3: 继承仍然失效
175+
176+
**检查步骤**:
177+
1. 确认运行的是修复后的代码(检查 git diff)
178+
2. 确认 teams 存在(运行 `./setup-test-entities.sh`)
179+
3. 查看 DEBUG 日志:
180+
```bash
181+
metadata ingest -c test-05-inheritance-enabled.yaml --log-level DEBUG 2>&1 | grep -i "parent_owner\|inherited"
182+
```
183+
4. 应该看到:
184+
```
185+
DEBUG: Resolving owner for databaseSchema 'finance_db.accounting', parent_owner: finance-team
186+
DEBUG: Using inherited owner for 'finance_db.accounting': finance-team
187+
```
188+
189+
## 📊 文件清单
190+
191+
### 修改的代码文件
192+
-`ingestion/src/metadata/ingestion/source/database/common_db_source.py`
193+
-`ingestion/src/metadata/ingestion/source/database/database_service.py`
194+
-`scripts/datamodel_generation.py`
195+
196+
### 修复的测试文件
197+
-`ingestion/tests/.../owner_config_tests/run-all-tests.sh` (路径修复)
198+
-`ingestion/tests/.../owner_config_tests/QUICK-START.md` (路径统一)
199+
200+
### 新增的工具和文档
201+
-`fix_rootmodel_generation.py` - 独立 RootModel 修复脚本
202+
-`ROOT_MODEL_PERMANENT_FIX.md` - 完整技术文档
203+
-`TROUBLESHOOTING.md` - 故障排查指南
204+
205+
## 🎯 下一步建议
206+
207+
### 立即执行
208+
1. ✅ 重新生成模型:`cd openmetadata-spec && mvn clean install`
209+
2. ✅ 重新安装 ingestion:`cd ../ingestion && pip install -e . --force-reinstall`
210+
3. ✅ 运行 Test 5 验证继承修复
211+
212+
### 短期优化
213+
1. 修改 Test 3, 4, 7, 8 的配置(数组→字符串)
214+
2. 运行完整测试套件
215+
3. 验证 OpenMetadata UI 中的 owner 显示
216+
217+
### 长期改进
218+
1. 修改 JSON Schema 支持数组(详见 `ROOT_MODEL_PERMANENT_FIX.md` 方案2)
219+
2. 或者更新 datamodel-code-generator 配置
220+
3. 添加自动化测试验证 RootModel 修复
221+
222+
## 🎉 总结
223+
224+
**三个问题,三个解决方案**
225+
226+
1.**多线程竞态条件** → 调整代码顺序(已修复)
227+
2.**RootModel 错误** → 自动后处理修复(已集成)
228+
3. ⚠️ **数组支持** → 临时修改配置,长期优化 Schema(详见文档)
229+
230+
**现在您可以**
231+
- ✅ 正常生成代码(自动修复 RootModel)
232+
- ✅ 测试继承功能(Test 5-6)
233+
- ✅ 使用单个 owner 配置(Test 1-2, 3-8 修改后)
234+
235+
**最重要的验证**:运行 Test 5,检查 `accounting` schema 和 `revenue` table 的 owner 是否为 `finance-team`(不是 `data-platform-team`),这证明多线程修复成功!

0 commit comments

Comments
 (0)