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

feat: 优化代码[修改虚拟线程乱用或不合理的地方] #3242

Merged
merged 1 commit into from
Jan 1, 2025

Conversation

KouShenhai
Copy link
Owner

@KouShenhai KouShenhai commented Jan 1, 2025

Summary by Sourcery

增强功能:

  • 通过优化虚拟线程使用,提升批量操作和页面查询的性能。
Original summary in English

Summary by Sourcery

Enhancements:

  • Improved performance of batch operations and page queries by optimizing virtual thread usage.

Summary by CodeRabbit

Based on the comprehensive summary of changes, here are the release notes:

  • Code Refactoring

    • Simplified asynchronous query methods across multiple services to synchronous implementations
    • Removed complex timeout and executor management in pagination queries
    • Reorganized package structure for Thing Model components
  • Performance Optimization

    • Introduced static ExecutorService instances with virtual thread support
    • Streamlined database query execution in various mapper interfaces
  • Infrastructure Changes

    • Removed selectObjectPage and selectCountByCondition methods from multiple mapper interfaces
    • Updated import statements and package declarations
    • Modified error handling in query execution methods
  • Logging Improvements

    • Added @Slf4j annotation to enable logging in domain services
  • Concurrency Model

    • Transitioned from CompletableFuture-based asynchronous processing to direct synchronous method calls
    • Simplified thread management and error handling

Copy link

coderabbitai bot commented Jan 1, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

This pull request introduces a series of significant architectural and code refactoring changes across multiple modules of the Laokou platform. The primary modifications include:

  1. Simplification of asynchronous query methods across various services
  2. Removal of selectObjectPage methods from multiple mapper interfaces
  3. Restructuring of package organization for thing model-related components
  4. Introduction of static ExecutorService instances in several utility and consumer classes

Changes

File/Module Change Summary
Common Core/Mybatis/Tenant Utilities Added static EXECUTOR using ThreadUtil.newVirtualTaskExecutor()
Admin/IoT Services Removed asynchronous processing in pagination query methods
Mapper Interfaces Deleted selectObjectPage and selectCountByCondition methods
Thing Model Package Restructured from model to thingModel package

Possibly related issues

Poem

🐰 Refactoring Rabbit's Rhyme

In code's vast warren, changes bloom
Async tasks fade, simplicity looms
Virtual threads, once scattered wide
Now neatly tucked, with logic's pride

Hop, hop, refactor! Clean and bright! 🌟


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 103adaa and e592aff.

📒 Files selected for processing (66)
  • laokou-common/laokou-common-core/src/main/java/org/laokou/common/core/utils/FileUtil.java (3 hunks)
  • laokou-common/laokou-common-mybatis-plus/src/main/java/org/laokou/common/mybatisplus/mapper/CrudMapper.java (2 hunks)
  • laokou-common/laokou-common-mybatis-plus/src/main/java/org/laokou/common/mybatisplus/utils/MybatisUtil.java (2 hunks)
  • laokou-common/laokou-common-tenant/src/main/java/org/laokou/common/tenant/mapper/SourceMapper.java (0 hunks)
  • laokou-common/laokou-common-tenant/src/main/java/org/laokou/common/tenant/mapper/TenantMapper.java (1 hunks)
  • laokou-common/laokou-common-tenant/src/main/resources/mapper/tenant/TenantMapper.xml (1 hunks)
  • laokou-sample/laokou-sample-websocket/src/main/java/org/laokou/adapter/consumer/SubscribeMessageConsumer.java (2 hunks)
  • laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/cluster/command/query/ClusterPageQryExe.java (1 hunks)
  • laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/dept/command/query/DeptPageQryExe.java (1 hunks)
  • laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/dict/command/query/DictPageQryExe.java (1 hunks)
  • laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/dictItem/command/query/DictItemPageQryExe.java (1 hunks)
  • laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/i18nMessage/command/query/I18nMessagePageQryExe.java (1 hunks)
  • laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/ip/command/query/IpPageQryExe.java (1 hunks)
  • laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/loginLog/command/query/LoginLogPageQryExe.java (1 hunks)
  • laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/menu/command/query/MenuPageQryExe.java (1 hunks)
  • laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/noticeLog/command/query/NoticeLogPageQryExe.java (2 hunks)
  • laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/operateLog/command/query/OperateLogPageQryExe.java (2 hunks)
  • laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/oss/command/query/OssPageQryExe.java (1 hunks)
  • laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/ossLog/command/query/OssLogPageQryExe.java (2 hunks)
  • laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/role/command/query/RolePageQryExe.java (1 hunks)
  • laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/source/command/query/SourcePageQryExe.java (2 hunks)
  • laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/tenant/command/query/TenantPageQryExe.java (2 hunks)
  • laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/user/command/query/UserPageQryExe.java (1 hunks)
  • laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/cluster/gatewayimpl/database/ClusterMapper.java (1 hunks)
  • laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/dept/gatewayimpl/database/DeptMapper.java (0 hunks)
  • laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/dict/gatewayimpl/database/DictMapper.java (0 hunks)
  • laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/dictItem/gatewayimpl/database/DictItemMapper.java (0 hunks)
  • laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/i18nMessage/gatewayimpl/database/I18nMessageMapper.java (0 hunks)
  • laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/ip/gatewayimpl/database/IpMapper.java (0 hunks)
  • laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/loginLog/gatewayimpl/database/LoginLogMapper.java (0 hunks)
  • laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/menu/gatewayimpl/database/MenuMapper.java (0 hunks)
  • laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/noticeLog/gatewayimpl/database/NoticeLogMapper.java (0 hunks)
  • laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/operateLog/gatewayimpl/database/OperateLogMapper.java (1 hunks)
  • laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/ossLog/gatewayimpl/database/OssLogMapper.java (1 hunks)
  • laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/role/gatewayimpl/database/RoleMapper.java (1 hunks)
  • laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/user/gatewayimpl/database/UserMapper.java (0 hunks)
  • laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/resources/mapper/noticeLog/NoticeLogMapper.xml (2 hunks)
  • laokou-service/laokou-auth/laokou-auth-infrastructure/src/main/java/org/laokou/auth/gatewayimpl/TenantGatewayImpl.java (1 hunks)
  • laokou-service/laokou-generator/laokou-generator-app/src/main/java/org/laokou/generator/column/command/query/ColumnPageQryExe.java (2 hunks)
  • laokou-service/laokou-generator/laokou-generator-app/src/main/java/org/laokou/generator/info/command/query/InfoPageQryExe.java (2 hunks)
  • laokou-service/laokou-generator/laokou-generator-app/src/main/java/org/laokou/generator/template/command/query/TemplatePageQryExe.java (2 hunks)
  • laokou-service/laokou-generator/laokou-generator-domain/src/main/java/org/laokou/generator/ability/GeneratorDomainService.java (3 hunks)
  • laokou-service/laokou-generator/laokou-generator-infrastructure/src/main/java/org/laokou/generator/info/gatewayimpl/database/InfoMapper.java (0 hunks)
  • laokou-service/laokou-generator/laokou-generator-start/src/main/resources/templates/infrastructure/mapper.ftl (0 hunks)
  • laokou-service/laokou-iot/laokou-iot-app/src/main/java/org/laokou/iot/communicationProtocol/command/query/CommunicationProtocolPageQryExe.java (2 hunks)
  • laokou-service/laokou-iot/laokou-iot-app/src/main/java/org/laokou/iot/device/command/query/DevicePageQryExe.java (2 hunks)
  • laokou-service/laokou-iot/laokou-iot-app/src/main/java/org/laokou/iot/product/command/query/ProductPageQryExe.java (2 hunks)
  • laokou-service/laokou-iot/laokou-iot-app/src/main/java/org/laokou/iot/productCategory/command/query/ProductCategoryPageQryExe.java (2 hunks)
  • laokou-service/laokou-iot/laokou-iot-app/src/main/java/org/laokou/iot/productModel/command/query/ProductModelPageQryExe.java (2 hunks)
  • laokou-service/laokou-iot/laokou-iot-app/src/main/java/org/laokou/iot/thingModel/command/ThingModelModifyCmdExe.java (1 hunks)
  • laokou-service/laokou-iot/laokou-iot-app/src/main/java/org/laokou/iot/thingModel/command/ThingModelSaveCmdExe.java (1 hunks)
  • laokou-service/laokou-iot/laokou-iot-app/src/main/java/org/laokou/iot/thingModel/command/query/ThingModelGetQryExe.java (1 hunks)
  • laokou-service/laokou-iot/laokou-iot-app/src/main/java/org/laokou/iot/thingModel/command/query/ThingModelPageQryExe.java (2 hunks)
  • laokou-service/laokou-iot/laokou-iot-app/src/main/java/org/laokou/iot/transportProtocol/command/query/TransportProtocolPageQryExe.java (2 hunks)
  • laokou-service/laokou-iot/laokou-iot-infrastructure/src/main/java/org/laokou/iot/communicationProtocol/gatewayimpl/database/CommunicationProtocolMapper.java (0 hunks)
  • laokou-service/laokou-iot/laokou-iot-infrastructure/src/main/java/org/laokou/iot/device/gatewayimpl/database/DeviceMapper.java (0 hunks)
  • laokou-service/laokou-iot/laokou-iot-infrastructure/src/main/java/org/laokou/iot/product/gatewayimpl/database/ProductMapper.java (0 hunks)
  • laokou-service/laokou-iot/laokou-iot-infrastructure/src/main/java/org/laokou/iot/productCategory/gatewayimpl/database/ProductCategoryMapper.java (0 hunks)
  • laokou-service/laokou-iot/laokou-iot-infrastructure/src/main/java/org/laokou/iot/productModel/gatewayimpl/database/ProductModelMapper.java (0 hunks)
  • laokou-service/laokou-iot/laokou-iot-infrastructure/src/main/java/org/laokou/iot/thingModel/convertor/ModelConvertor.java (1 hunks)
  • laokou-service/laokou-iot/laokou-iot-infrastructure/src/main/java/org/laokou/iot/thingModel/gatewayimpl/ThingModelGatewayImpl.java (1 hunks)
  • laokou-service/laokou-iot/laokou-iot-infrastructure/src/main/java/org/laokou/iot/thingModel/gatewayimpl/database/ThingModelMapper.java (1 hunks)
  • laokou-service/laokou-iot/laokou-iot-infrastructure/src/main/java/org/laokou/iot/thingModel/gatewayimpl/database/dataobject/ThingModelDO.java (1 hunks)
  • laokou-service/laokou-iot/laokou-iot-infrastructure/src/main/java/org/laokou/iot/transportProtocol/gatewayimpl/database/TransportProtocolMapper.java (0 hunks)
  • laokou-service/laokou-iot/laokou-iot-infrastructure/src/main/resources/mapper/thingModel/ThingModelMapper.xml (1 hunks)
  • laokou-service/laokou-logstash/laokou-logstash-adapter/src/main/java/org/laokou/logstash/consumer/TraceLogConsumer.java (1 hunks)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@KouShenhai KouShenhai merged commit 5b59fb0 into master Jan 1, 2025
8 of 13 checks passed
Copy link

sourcery-ai bot commented Jan 1, 2025

审阅者指南 by Sourcery

这个拉取请求通过删除不必要或误用的虚拟线程来优化代码库。它修改了多个文件以实现这一目标,主要是将为单个任务创建的虚拟线程池替换为共享的虚拟线程池。

使用共享执行器的批处理优化序列图

sequenceDiagram
    participant Client
    participant BatchProcessor
    participant SharedExecutor
    participant Database

    Client->>BatchProcessor: batch(dataList)
    activate BatchProcessor
    Note over BatchProcessor: 将数据分区为块
    BatchProcessor->>SharedExecutor: invokeAll(futures)
    activate SharedExecutor
    loop 对于每个块
        SharedExecutor->>Database: 处理块
        Database-->>SharedExecutor: 结果
    end
    SharedExecutor-->>BatchProcessor: 所有结果
    deactivate SharedExecutor
    BatchProcessor-->>Client: 完成
    deactivate BatchProcessor
Loading

显示更新的映射器层次结构的类图

classDiagram
    class CrudMapper {
        <<interface>>
        +selectObjectCount(PageQuery)
        +selectObjectPage(PageQuery)
    }

    class BaseMapper {
        <<interface>>
    }

    class CustomMapper {
        <<interface>>
    }

    CrudMapper --|> BaseMapper
    CustomMapper --|> CrudMapper

    note for CrudMapper "标准化的页面查询方法"
Loading

文件级别变更

变更 详情 文件
将批处理操作的虚拟线程池替换为共享的虚拟线程池。
  • 初始化一个静态共享的虚拟线程池,并在批处理操作中重复使用。
  • 删除了为每个批处理操作创建新虚拟线程池的 try-with-resources 块。
  • 批处理操作现在使用共享的虚拟线程池,减少了重复创建和销毁线程池的开销。
laokou-common/laokou-common-mybatis-plus/src/main/java/org/laokou/common/mybatisplus/utils/MybatisUtil.java
删除页面查询执行中不必要的虚拟线程使用。
  • 删除了数据库查询中的 CompletableFuture 和 ExecutorService。
  • 通过直接在现有的 try 块中调用映射器方法来简化查询执行。
  • 删除了与虚拟线程超时相关的不必要的 try-catch 块。
laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/loginLog/command/query/LoginLogPageQryExe.java
laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/ossLog/command/query/OssLogPageQryExe.java
laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/noticeLog/command/query/NoticeLogPageQryExe.java
laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/operateLog/command/query/OperateLogPageQryExe.java
laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/user/command/query/UserPageQryExe.java
laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/cluster/command/query/ClusterPageQryExe.java
laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/dept/command/query/DeptPageQryExe.java
laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/dict/command/query/DictPageQryExe.java
laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/dictItem/command/query/DictItemPageQryExe.java
laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/i18nMessage/command/query/I18nMessagePageQryExe.java
laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/menu/command/query/MenuPageQryExe.java
laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/oss/command/query/OssPageQryExe.java
laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/role/command/query/RolePageQryExe.java
laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/source/command/query/SourcePageQryExe.java
laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/tenant/command/query/TenantPageQryExe.java
laokou-service/laokou-generator/laokou-generator-app/src/main/java/org/laokou/generator/column/command/query/ColumnPageQryExe.java
laokou-service/laokou-generator/laokou-generator-app/src/main/java/org/laokou/generator/info/command/query/InfoPageQryExe.java
laokou-service/laokou-generator/laokou-generator-app/src/main/java/org/laokou/generator/template/command/query/TemplatePageQryExe.java
laokou-service/laokou-iot/laokou-iot-app/src/main/java/org/laokou/iot/device/command/query/DevicePageQryExe.java
laokou-service/laokou-iot/laokou-iot-app/src/main/java/org/laokou/iot/product/command/query/ProductPageQryExe.java
laokou-service/laokou-iot/laokou-iot-app/src/main/java/org/laokou/iot/productCategory/command/query/ProductCategoryPageQryExe.java
laokou-service/laokou-iot/laokou-iot-app/src/main/java/org/laokou/iot/productModel/command/query/ProductModelPageQryExe.java
laokou-service/laokou-iot/laokou-iot-app/src/main/java/org/laokou/iot/thingModel/command/query/ThingModelPageQryExe.java
laokou-service/laokou-iot/laokou-iot-app/src/main/java/org/laokou/iot/transportProtocol/command/query/TransportProtocolPageQryExe.java
laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/ip/command/query/IpPageQryExe.java
将 WebSocket 消息发送的虚拟线程池替换为共享的虚拟线程池。
  • 初始化一个静态共享的虚拟线程池。
  • 删除了为每个消息创建新虚拟线程池的 try-with-resources 块。
  • 使用共享的虚拟线程池发送 WebSocket 消息。
laokou-sample/laokou-sample-websocket/src/main/java/org/laokou/adapter/consumer/SubscribeMessageConsumer.java
为代码生成使用共享的虚拟线程池。
  • 初始化一个静态共享的虚拟线程池。
  • 替换了为每个代码生成任务创建新虚拟线程池的 try-with-resources 块。
  • 使用共享的虚拟线程池并行执行代码生成任务。
laokou-service/laokou-generator/laokou-generator-domain/src/main/java/org/laokou/generator/ability/GeneratorDomainService.java
为文件写入操作使用共享的虚拟线程池。
  • 初始化一个静态共享的虚拟线程池。
  • 删除了为每个文件写入操作创建新虚拟线程池的 try-with-resources 块。
  • 使用共享的虚拟线程池并发写入文件块。
laokou-common/laokou-common-core/src/main/java/org/laokou/common/core/utils/FileUtil.java
删除不必要的映射器方法并更新相关实现。
  • 从各种映射器接口中删除 selectPageByCondition 和 selectCountByCondition 方法。
  • 从映射器 XML 文件中删除相应的 SQL 选择语句。
  • 更新 CrudMapper 接口以包含 selectObjectPage 方法。
  • 更新先前使用 selectPageByCondition 和 selectCountByCondition 的实现,以使用 CrudMapper 的 selectObjectPage 和 selectObjectCount 方法。
  • 在 TenantMapper 中将 selectIdTenantCode 重命名为 selectIdByCode 并更新其使用。
  • 将包 org.laokou.iot.model 重命名为 org.laokou.iot.thingModel 并更新相关类和引用。
laokou-common/laokou-common-tenant/src/main/java/org/laokou/common/tenant/mapper/TenantMapper.java
laokou-common/laokou-common-tenant/src/main/resources/mapper/tenant/TenantMapper.xml
laokou-common/laokou-common-tenant/src/main/java/org/laokou/common/tenant/mapper/SourceMapper.java
laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/dept/gatewayimpl/database/DeptMapper.java
laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/dict/gatewayimpl/database/DictMapper.java
laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/dictItem/gatewayimpl/database/DictItemMapper.java
laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/i18nMessage/gatewayimpl/database/I18nMessageMapper.java
laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/ip/gatewayimpl/database/IpMapper.java
laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/menu/gatewayimpl/database/MenuMapper.java
laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/operateLog/gatewayimpl/database/OperateLogMapper.java
laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/ossLog/gatewayimpl/database/OssLogMapper.java
laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/role/gatewayimpl/database/RoleMapper.java
laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/user/gatewayimpl/database/UserMapper.java
laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/resources/mapper/noticeLog/NoticeLogMapper.xml
laokou-service/laokou-iot/laokou-iot-infrastructure/src/main/resources/mapper/thingModel/ThingModelMapper.xml
laokou-common/laokou-common-mybatis-plus/src/main/java/org/laokou/common/mybatisplus/mapper/CrudMapper.java
laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/cluster/gatewayimpl/database/ClusterMapper.java
laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/loginLog/gatewayimpl/database/LoginLogMapper.java
laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/noticeLog/gatewayimpl/database/NoticeLogMapper.java
laokou-service/laokou-auth/laokou-auth-infrastructure/src/main/java/org/laokou/auth/gatewayimpl/TenantGatewayImpl.java
laokou-service/laokou-generator/laokou-generator-infrastructure/src/main/java/org/laokou/generator/info/gatewayimpl/database/InfoMapper.java
laokou-service/laokou-generator/laokou-generator-start/src/main/resources/templates/infrastructure/mapper.ftl
laokou-service/laokou-iot/laokou-iot-infrastructure/src/main/java/org/laokou/iot/communicationProtocol/gatewayimpl/database/CommunicationProtocolMapper.java
`l
Original review guide in English

Reviewer's Guide by Sourcery

This pull request optimizes the codebase by removing unnecessary or misused virtual threads. It modifies several files to achieve this, primarily by replacing virtual thread pools created for single tasks with a shared virtual thread pool.

Sequence diagram for optimized batch processing with shared executor

sequenceDiagram
    participant Client
    participant BatchProcessor
    participant SharedExecutor
    participant Database

    Client->>BatchProcessor: batch(dataList)
    activate BatchProcessor
    Note over BatchProcessor: Partition data into chunks
    BatchProcessor->>SharedExecutor: invokeAll(futures)
    activate SharedExecutor
    loop For each chunk
        SharedExecutor->>Database: Process chunk
        Database-->>SharedExecutor: Result
    end
    SharedExecutor-->>BatchProcessor: All results
    deactivate SharedExecutor
    BatchProcessor-->>Client: Complete
    deactivate BatchProcessor
Loading

Class diagram showing updated mapper hierarchy

classDiagram
    class CrudMapper {
        <<interface>>
        +selectObjectCount(PageQuery)
        +selectObjectPage(PageQuery)
    }

    class BaseMapper {
        <<interface>>
    }

    class CustomMapper {
        <<interface>>
    }

    CrudMapper --|> BaseMapper
    CustomMapper --|> CrudMapper

    note for CrudMapper "Standardized page query methods"
Loading

File-Level Changes

Change Details Files
Replaced virtual thread pools for batch operations with a shared virtual thread pool.
  • A static shared virtual thread pool is initialized once and reused across batch operations.
  • The try-with-resources block for creating a new virtual thread pool for each batch operation is removed.
  • Batch operations now use the shared virtual thread pool, reducing overhead from repeated thread pool creation and destruction.
laokou-common/laokou-common-mybatis-plus/src/main/java/org/laokou/common/mybatisplus/utils/MybatisUtil.java
Removed unnecessary virtual thread usage in page query executions.
  • Removed the use of CompletableFuture and ExecutorService for database queries.
  • Simplified the query execution by directly calling the mapper methods within the existing try block.
  • Removed unnecessary try-catch blocks related to virtual thread timeouts.
laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/loginLog/command/query/LoginLogPageQryExe.java
laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/ossLog/command/query/OssLogPageQryExe.java
laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/noticeLog/command/query/NoticeLogPageQryExe.java
laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/operateLog/command/query/OperateLogPageQryExe.java
laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/user/command/query/UserPageQryExe.java
laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/cluster/command/query/ClusterPageQryExe.java
laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/dept/command/query/DeptPageQryExe.java
laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/dict/command/query/DictPageQryExe.java
laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/dictItem/command/query/DictItemPageQryExe.java
laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/i18nMessage/command/query/I18nMessagePageQryExe.java
laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/menu/command/query/MenuPageQryExe.java
laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/oss/command/query/OssPageQryExe.java
laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/role/command/query/RolePageQryExe.java
laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/source/command/query/SourcePageQryExe.java
laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/tenant/command/query/TenantPageQryExe.java
laokou-service/laokou-generator/laokou-generator-app/src/main/java/org/laokou/generator/column/command/query/ColumnPageQryExe.java
laokou-service/laokou-generator/laokou-generator-app/src/main/java/org/laokou/generator/info/command/query/InfoPageQryExe.java
laokou-service/laokou-generator/laokou-generator-app/src/main/java/org/laokou/generator/template/command/query/TemplatePageQryExe.java
laokou-service/laokou-iot/laokou-iot-app/src/main/java/org/laokou/iot/device/command/query/DevicePageQryExe.java
laokou-service/laokou-iot/laokou-iot-app/src/main/java/org/laokou/iot/product/command/query/ProductPageQryExe.java
laokou-service/laokou-iot/laokou-iot-app/src/main/java/org/laokou/iot/productCategory/command/query/ProductCategoryPageQryExe.java
laokou-service/laokou-iot/laokou-iot-app/src/main/java/org/laokou/iot/productModel/command/query/ProductModelPageQryExe.java
laokou-service/laokou-iot/laokou-iot-app/src/main/java/org/laokou/iot/thingModel/command/query/ThingModelPageQryExe.java
laokou-service/laokou-iot/laokou-iot-app/src/main/java/org/laokou/iot/transportProtocol/command/query/TransportProtocolPageQryExe.java
laokou-service/laokou-admin/laokou-admin-app/src/main/java/org/laokou/admin/ip/command/query/IpPageQryExe.java
Replaced virtual thread pool for WebSocket message sending with a shared virtual thread pool.
  • Initialized a static shared virtual thread pool.
  • Removed the try-with-resources block that created a new virtual thread pool for each message.
  • Used the shared virtual thread pool for sending WebSocket messages.
laokou-sample/laokou-sample-websocket/src/main/java/org/laokou/adapter/consumer/SubscribeMessageConsumer.java
Used a shared virtual thread pool for code generation.
  • Initialized a static shared virtual thread pool.
  • Replaced the try-with-resources block that created a new virtual thread pool for each code generation task.
  • Used the shared virtual thread pool for executing code generation tasks in parallel.
laokou-service/laokou-generator/laokou-generator-domain/src/main/java/org/laokou/generator/ability/GeneratorDomainService.java
Used a shared virtual thread pool for file writing operations.
  • Initialized a static shared virtual thread pool.
  • Removed the try-with-resources block that created a new virtual thread pool for each file write operation.
  • Used the shared virtual thread pool for writing files in chunks concurrently.
laokou-common/laokou-common-core/src/main/java/org/laokou/common/core/utils/FileUtil.java
Removed unnecessary mapper methods and updated related implementations.
  • Removed selectPageByCondition and selectCountByCondition methods from various mapper interfaces.
  • Removed corresponding SQL select statements from mapper XML files.
  • Updated CrudMapper interface to include a selectObjectPage method.
  • Updated implementations that previously used selectPageByCondition and selectCountByCondition to use selectObjectPage and selectObjectCount methods from CrudMapper.
  • Renamed selectIdTenantCode to selectIdByCode in TenantMapper and updated its usage.
  • Renamed package org.laokou.iot.model to org.laokou.iot.thingModel and updated related classes and references.
laokou-common/laokou-common-tenant/src/main/java/org/laokou/common/tenant/mapper/TenantMapper.java
laokou-common/laokou-common-tenant/src/main/resources/mapper/tenant/TenantMapper.xml
laokou-common/laokou-common-tenant/src/main/java/org/laokou/common/tenant/mapper/SourceMapper.java
laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/dept/gatewayimpl/database/DeptMapper.java
laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/dict/gatewayimpl/database/DictMapper.java
laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/dictItem/gatewayimpl/database/DictItemMapper.java
laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/i18nMessage/gatewayimpl/database/I18nMessageMapper.java
laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/ip/gatewayimpl/database/IpMapper.java
laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/menu/gatewayimpl/database/MenuMapper.java
laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/operateLog/gatewayimpl/database/OperateLogMapper.java
laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/ossLog/gatewayimpl/database/OssLogMapper.java
laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/role/gatewayimpl/database/RoleMapper.java
laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/user/gatewayimpl/database/UserMapper.java
laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/resources/mapper/noticeLog/NoticeLogMapper.xml
laokou-service/laokou-iot/laokou-iot-infrastructure/src/main/resources/mapper/thingModel/ThingModelMapper.xml
laokou-common/laokou-common-mybatis-plus/src/main/java/org/laokou/common/mybatisplus/mapper/CrudMapper.java
laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/cluster/gatewayimpl/database/ClusterMapper.java
laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/loginLog/gatewayimpl/database/LoginLogMapper.java
laokou-service/laokou-admin/laokou-admin-infrastructure/src/main/java/org/laokou/admin/noticeLog/gatewayimpl/database/NoticeLogMapper.java
laokou-service/laokou-auth/laokou-auth-infrastructure/src/main/java/org/laokou/auth/gatewayimpl/TenantGatewayImpl.java
laokou-service/laokou-generator/laokou-generator-infrastructure/src/main/java/org/laokou/generator/info/gatewayimpl/database/InfoMapper.java
laokou-service/laokou-generator/laokou-generator-start/src/main/resources/templates/infrastructure/mapper.ftl
laokou-service/laokou-iot/laokou-iot-infrastructure/src/main/java/org/laokou/iot/communicationProtocol/gatewayimpl/database/CommunicationProtocolMapper.java
laokou-service/laokou-iot/laokou-iot-infrastructure/src/main/java/org/laokou/iot/device/gatewayimpl/database/DeviceMapper.java
laokou-service/laokou-iot/laokou-iot-infrastructure/src/main/java/org/laokou/iot/product/gatewayimpl/database/ProductMapper.java
laokou-service/laokou-iot/laokou-iot-infrastructure/src/main/java/org/laokou/iot/productCategory/gatewayimpl/database/ProductCategoryMapper.java
laokou-service/laokou-iot/laokou-iot-infrastructure/src/main/java/org/laokou/iot/productModel/gatewayimpl/database/ProductModelMapper.java
laokou-service/laokou-iot/laokou-iot-infrastructure/src/main/java/org/laokou/iot/model/gatewayimpl/database/ThingModelMapper.java
laokou-service/laokou-iot/laokou-iot-infrastructure/src/main/java/org/laokou/iot/transportProtocol/gatewayimpl/database/TransportProtocolMapper.java
laokou-service/laokou-iot/laokou-iot-app/src/main/java/org/laokou/iot/thingModel/command/ThingModelModifyCmdExe.java
laokou-service/laokou-iot/laokou-iot-app/src/main/java/org/laokou/iot/thingModel/command/ThingModelSaveCmdExe.java
laokou-service/laokou-iot/laokou-iot-app/src/main/java/org/laokou/iot/thingModel/command/query/ThingModelGetQryExe.java
laokou-service/laokou-iot/laokou-iot-infrastructure/src/main/java/org/laokou/iot/model/convertor/ModelConvertor.java
laokou-service/laokou-iot/laokou-iot-infrastructure/src/main/java/org/laokou/iot/model/gatewayimpl/ThingModelGatewayImpl.java
laokou-service/laokou-iot/laokou-iot-infrastructure/src/main/java/org/laokou/iot/model/gatewayimpl/database/dataobject/ThingModelDO.java
Improved trace log processing efficiency.
  • Changed messages.stream() to messages.parallelStream() to process trace logs in parallel, potentially improving performance.
laokou-service/laokou-logstash/laokou-logstash-adapter/src/main/java/org/laokou/logstash/consumer/TraceLogConsumer.java

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KouShenhai - 我已经审查了你的更改 - 以下是一些反馈:

整体评论

  • 对于简单的数据库分页查询,考虑移除虚拟线程的使用 - 异步复杂性对于快速操作(仅仅获取列表和计数)是不必要的。保留虚拟线程用于IO密集型操作,如文件处理和WebSocket。
以下是我在审查期间查看的内容
  • 🟡 一般性问题:发现3个问题
  • 🟢 安全性:一切看起来都很好
  • 🟢 测试:一切看起来都很好
  • 🟢 复杂性:一切看起来都很好
  • 🟢 文档:一切看起来都很好

Sourcery 对开源项目是免费的 - 如果你喜欢我们的评论,请考虑分享 ✨
帮助我变得更有用!请在每条评论上点击 👍 或 👎,我将使用反馈来改进你的评论。
Original comment in English

Hey @KouShenhai - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider removing virtual thread usage for simple database pagination queries - the async complexity isn't needed for quick operations that just fetch a list and count. Keep virtual threads for IO-heavy operations like file handling and websockets.
Here's what I looked at during the review
  • 🟡 General issues: 3 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@@ -50,6 +50,8 @@ public class MybatisUtil {

private static final int DEFAULT_BATCH_NUM = 100000;

private static final ExecutorService EXECUTOR = ThreadUtil.newVirtualTaskExecutor();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议(性能): 考虑添加适当的执行器关闭处理以防止资源泄漏

静态执行器需要仔细的生命周期管理。考虑实现关闭钩子或应用程序生命周期监听器,以确保执行器服务的正确清理。

Original comment in English

suggestion (performance): Consider adding proper executor shutdown handling to prevent resource leaks

Static executors need careful lifecycle management. Consider implementing a shutdown hook or application lifecycle listener to ensure proper cleanup of the executor service.

c2.get(30, TimeUnit.SECONDS)));
try {
DynamicDataSourceContextHolder.push("domain");
List<LoginLogDO> list = loginLogMapper.selectObjectPage(qry);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议(性能): 考虑保留数据库查询的超时保护

虽然代码现在更简单,但移除超时处理可能导致长时间运行的查询影响系统性能。考虑添加数据库查询超时配置。

建议的实现:

		try {
			DynamicDataSourceContextHolder.push("domain");

			CompletableFuture<List<LoginLogDO>> listFuture = CompletableFuture.supplyAsync(() -> 
				loginLogMapper.selectObjectPage(qry));
			CompletableFuture<Long> countFuture = CompletableFuture.supplyAsync(() -> 
				loginLogMapper.selectObjectCount(qry));

			List<LoginLogDO> list = listFuture.get(30, TimeUnit.SECONDS);
			long total = countFuture.get(30, TimeUnit.SECONDS);

			return Result.ok(Page.create(list.stream().map(LoginLogConvertor::toClientObject).toList(), total));

你需要在文件顶部添加这些导入:

  • import java.util.concurrent.CompletableFuture;
  • import java.util.concurrent.TimeUnit;
  • import java.util.concurrent.TimeoutException;

你可能还想为TimeoutException添加错误处理,并将其包装在适当的特定于应用程序的异常中。

考虑通过应用程序属性使超时持续时间(30秒)可配置。

Original comment in English

suggestion (performance): Consider retaining timeout protection for database queries

While the code is simpler now, removing the timeout handling could lead to long-running queries impacting system performance. Consider adding a database query timeout configuration.

Suggested implementation:

		try {
			DynamicDataSourceContextHolder.push("domain");

			CompletableFuture<List<LoginLogDO>> listFuture = CompletableFuture.supplyAsync(() -> 
				loginLogMapper.selectObjectPage(qry));
			CompletableFuture<Long> countFuture = CompletableFuture.supplyAsync(() -> 
				loginLogMapper.selectObjectCount(qry));

			List<LoginLogDO> list = listFuture.get(30, TimeUnit.SECONDS);
			long total = countFuture.get(30, TimeUnit.SECONDS);

			return Result.ok(Page.create(list.stream().map(LoginLogConvertor::toClientObject).toList(), total));

You'll need to add these imports at the top of the file:

  • import java.util.concurrent.CompletableFuture;
  • import java.util.concurrent.TimeUnit;
  • import java.util.concurrent.TimeoutException;

You may also want to add error handling for TimeoutException and wrap it in an appropriate application-specific exception.

Consider making the timeout duration (30 seconds) configurable via application properties.

@@ -48,7 +48,7 @@ public class TraceLogConsumer {
@KafkaListener(topics = "laokou_trace_topic", groupId = "laokou_trace_consumer_group")
public CompletableFuture<Void> kafkaConsumer(List<String> messages, Acknowledgment ack) {
try {
Map<String, Object> dataMap = messages.stream()
Map<String, Object> dataMap = messages.parallelStream()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

问题(性能): 并行流处理需要背压处理

在没有背压机制的情况下使用parallelStream()可能在高负载下导致资源耗尽。考虑使用有界队列或速率限制器来控制处理。

Original comment in English

issue (performance): Parallel stream processing needs backpressure handling

Using parallelStream() without backpressure mechanisms could lead to resource exhaustion under high load. Consider using a bounded queue or rate limiter to control processing.

Copy link

codecov bot commented Jan 1, 2025

Codecov Report

Attention: Patch coverage is 0% with 3 lines in your changes missing coverage. Please review.

Project coverage is 24.40%. Comparing base (103adaa) to head (e592aff).
Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
...in/java/org/laokou/common/core/utils/FileUtil.java 0.00% 3 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3242      +/-   ##
============================================
+ Coverage     24.29%   24.40%   +0.10%     
- Complexity      182      183       +1     
============================================
  Files           140      140              
  Lines          1922     1922              
  Branches        123      123              
============================================
+ Hits            467      469       +2     
+ Misses         1399     1397       -2     
  Partials         56       56              

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

Copy link

sonarqubecloud bot commented Jan 1, 2025

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