Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(plugin): use standard dfs instead of topological for stable sort #262

Merged
merged 2 commits into from
Aug 29, 2023

Conversation

noahziheng
Copy link
Member

@noahziheng noahziheng commented Aug 24, 2023

Background

之前插件的按依赖排序使用了拓扑排序算法;

  • 优点是有向无环图理论复杂度低,可以较容易检查循环依赖;
  • 缺点是不稳定排序,可能造成 config 内锚定的无关依赖顺序漂移;

而插件排序场景中对于不能靠 dependencies 声明来实现的,较普遍使用 pluginConfig 中的顺序做依赖顺序保障,而拓扑排序使顺序漂移问题变得不易发现和处理。

Example

例如,当我们有这段配置时:

[
  { name: 'a', deps: [ 'b', 'c' ] },
  { name: 'b', deps: [ 'c' ] },
  { name: 'c', deps: [] },
  { name: 'd', deps: [ 'c' ] }
]

依赖链路为:

a -> b -> c
a -> c
b -> c
d -> c

期待的顺序为 c, b, a, d,即对于 a / d 作为无关项保持配置中的顺序;

然而,因为拓扑排序不稳定,最终顺序与依赖链长度有关(indegree 出队顺序),所以结果会是 c, b, d, a,如果 a / d 间有相互影响,将产生不可用现象

Solution

综上,有必要对该部分做出改变

该 PR 通过 DFS 重新实现 PluginFactory.createFromConfig 方法,在递归过程中进行 optional 和循环依赖检查;

同时优化了 Plugin 类的实现代码,移除了无用方法;

调整了 plugin 相关单测,顺序保证增加到 4 个,并使用上述的依赖链 case 做验证

@noahziheng noahziheng self-assigned this Aug 24, 2023
@codecov-commenter
Copy link

codecov-commenter commented Aug 24, 2023

Codecov Report

Patch coverage: 95.00% and project coverage change: -0.05% ⚠️

Comparison is base (6e591b4) 91.68% compared to head (993f8f0) 91.63%.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #262      +/-   ##
==========================================
- Coverage   91.68%   91.63%   -0.05%     
==========================================
  Files          49       49              
  Lines        1203     1184      -19     
  Branches      213      212       -1     
==========================================
- Hits         1103     1085      -18     
+ Misses        100       99       -1     
Files Changed Coverage Δ
src/plugin/types.ts 100.00% <ø> (ø)
src/plugin/impl.ts 80.00% <66.66%> (-6.89%) ⬇️
src/plugin/common.ts 98.00% <97.05%> (+0.38%) ⬆️
src/plugin/factory.ts 100.00% <100.00%> (+8.33%) ⬆️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@noahziheng noahziheng force-pushed the refactor/plugin-sort branch from 43f61ca to 993f8f0 Compare August 28, 2023 03:28
@noahziheng noahziheng merged commit bf4ac0f into master Aug 29, 2023
@noahziheng noahziheng deleted the refactor/plugin-sort branch August 29, 2023 02:47
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.

3 participants