-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
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
希望 Halo 提供内存索引功能提高数据的获取速度 #5058
Labels
area/core
Issues or PRs related to the Halo Core
kind/feature
Categorizes issue or PR as related to a new feature.
Milestone
Comments
|
Hi @gengxiaoxiaoxin ,感谢提供思路!
|
f2c-ci-robot bot
pushed a commit
that referenced
this issue
Jan 19, 2024
#### What type of PR is this? /kind feature /area core /milestone 2.12.x #### What this PR does / why we need it: 新增自定义模型索引机制 默认为所有的自定义模型都添加了以下索引: - metadata.name - metadata.labels - metadata.creationTimestamp - metadata.deletionTimestamp **how to test it?** 1. 测试应用的启动和停止 2. 测试 Reconciler 被正确执行,如创建文章发布文章,测试删除文章的某个 label 数据启动后能被 PostReconciler 恢复(即Reconciler 被正确执行) 3. 测试自定义模型自动生成的 list APIs 1. 能根据 labels 正确过滤数据和分页 2. 能根据 creationTimestamp 正确排序 3. 测试插件启用后也能正确使用 list APIs 根据 labels 过滤数据和 creationTimestamp 排序 4. 能正确删除数据(则表示 GcReconciler 使用索引正确) 5. 测试在插件中为自定义模型注册索引 ```java public class DemoPlugin extension BasePlugin { private final SchemeManager schemeManager; public MomentsPlugin(PluginContext pluginContext, SchemeManager schemeManager) { super(pluginContext); this.schemeManager = schemeManager; } @OverRide public void start() { schemeManager.register(Moment.class, indexSpecs -> { indexSpecs.add(new IndexSpec() .setName("spec.tags") .setIndexFunc(multiValueAttribute(Moment.class, moment -> { var tags = moment.getSpec().getTags(); return tags == null ? Set.of() : tags; })) ); indexSpecs.add(new IndexSpec() .setName("spec.owner") .setIndexFunc(simpleAttribute(Moment.class, moment -> moment.getSpec().getOwner()) ) ); indexSpecs.add(new IndexSpec() .setName("spec.releaseTime") .setIndexFunc(simpleAttribute(Moment.class, moment -> { var releaseTime = moment.getSpec().getReleaseTime(); return releaseTime == null ? null : releaseTime.toString(); })) ); indexSpecs.add(new IndexSpec() .setName("spec.visible") .setIndexFunc(simpleAttribute(Moment.class, moment -> { var visible = moment.getSpec().getVisible(); return visible == null ? null : visible.toString(); })) ); }); } @OverRide public void stop() { // unregister scheme 即可,不需要手动删除索引 } } ``` 可以正确在自动生成的 list APIs 使用 fieldSelector 来过滤 `spec.slug` 和排序,可以自己添加其他的 indexSpec 测试 6. 测试唯一索引并添加重复数据,期望无法添加进去 #### Which issue(s) this PR fixes: Fixes #5058 #### Does this PR introduce a user-facing change? ```release-note 新增自定义模型索引机制 ```
Closed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
area/core
Issues or PRs related to the Halo Core
kind/feature
Categorizes issue or PR as related to a new feature.
你当前使用的版本
2.11.0
描述一下此特性
目前 Halo 分页条件查询数据会有大批量无效数据被加载到内存中,在数据量多的情况下获取数据需要的时间和对内存的消耗变的很大
用例验证
创建一个 Moment 自定义模型,有以下这些属性
创建内容大小同为 37kb 的数据 2000 条
查询条件 1
通过 labelSelector 查询条件获取 10 条数据(实际会将该 Moment 自定义模型的所有数据查询到 Halo 然后过滤 10 条)
Heap 使用量在查询时增加了约 1200M
查询耗时:20.120986512 seconds
查询条件 2
耗时:22.709311531 seconds,所以对于没有索引时无论查询条件都会出现相同的内存占用情况
用例 2
在单条数据大小不变的情况下增加到 4000 条,使用以下查询条件
耗时:58.147185776 seconds
可以看出随着数据量的增加,查询耗时和内存占用了都在上涨
在内存中建立索引
而在内存中建立对 metadata.name 与查询条件的索引后在 2k 和 4k 数据的情况下相同的查询条件的内存占用情况基本一致,如下:
![Pasted image 20231206143953](https://private-user-images.githubusercontent.com/38999863/291132989-d81fc701-551a-4726-a7f6-64eebbb9da43.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk2MzY0MzIsIm5iZiI6MTczOTYzNjEzMiwicGF0aCI6Ii8zODk5OTg2My8yOTExMzI5ODktZDgxZmM3MDEtNTUxYS00NzI2LWE3ZjYtNjRlZWJiYjlkYTQzLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjE1VDE2MTUzMlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTkyMzFhNWNlM2ZjOTk4MDFhNDQ0MDY1NmY4MmQ0NDcwMDZlYjg3MmRkMmIzNTUzZTFlMzU3NGNiZDg2YTgyYjUmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.DFo58UwgNO3EhpQw1NrWxYpVjlG57UfgvrynVxlwMG0)
(根据条件从索引筛选出所需的 metadata.name 后通过数据库的类似 where name in() 查询来批量拿取数据)
耗时:0.023571471 seconds
总结
由于索引提供了精准定位数据行主键的能力,有两点优于目前 ReactiveExtensionClient.list 方法:
因此随着目前数据量日益增长的情况下,有必要先提供一个建立内存索引并根据查询条件使用索引来获取数据的功能以减少查询时的内存占用和缩短查询时间
附加信息
/kind feature
/area core
/assign
The text was updated successfully, but these errors were encountered: