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: dal retry when init failed #260

Merged
merged 1 commit into from
Dec 26, 2024
Merged

feat: dal retry when init failed #260

merged 1 commit into from
Dec 26, 2024

Conversation

gxkl
Copy link
Contributor

@gxkl gxkl commented Dec 19, 2024

Checklist
  • npm test passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)
Description of change

Summary by CodeRabbit

  • New Features

    • Enhanced logging capabilities across various components, including MysqlDataSource, DalModuleLoadUnitHook, and Runner.
    • Added retry logic for initialization in MysqlDataSource.
  • Bug Fixes

    • Improved error handling during the initialization process of MysqlDataSource.
  • Tests

    • Restructured test suite for better organization and clarity, focusing on initialization and execution separately.

Copy link

coderabbitai bot commented Dec 19, 2024

Walkthrough

This pull request introduces enhanced logging and initialization capabilities for MySQL data sources across multiple components. The changes primarily focus on adding optional logger support and retry mechanisms during data source initialization. A new development dependency mm is added to the package for testing purposes. The modifications span several files in the core data access layer runtime and plugin modules, aiming to improve error handling and provide more robust logging during database connection and initialization processes.

Changes

File Change Summary
core/dal-runtime/package.json Added development dependency "mm": "^3.2.1"
core/dal-runtime/src/MySqlDataSource.ts - Added initRetryTimes and logger to DataSourceOptions interface
- Introduced retry logic and logging in initialization process
core/dal-runtime/test/DataSource.test.ts Restructured test suite with improved organization
Added error handling and retry scenario tests
plugin/dal/lib/DalModuleLoadUnitHook.ts Added optional logger parameter to constructor
Integrated logger into data source configuration
plugin/dal/lib/MysqlDataSourceManager.ts Updated createDataSource to handle optional logger
standalone/standalone/src/Runner.ts Modified hook instantiations to include logger parameter

Sequence Diagram

sequenceDiagram
    participant Runner
    participant DalModuleLoadUnitHook
    participant MysqlDataSourceManager
    participant MysqlDataSource

    Runner->>DalModuleLoadUnitHook: Create with optional logger
    DalModuleLoadUnitHook->>MysqlDataSourceManager: Configure data source
    MysqlDataSourceManager->>MysqlDataSource: Create with logger and retry options
    MysqlDataSource->>MysqlDataSource: Initialize with retry mechanism
    alt Initialization fails
        MysqlDataSource-->>MysqlDataSource: Retry initialization
    end
Loading

Possibly related PRs

Poem

🐰 In the realm of data's dance,
Loggers hop with retry's chance
MySQL sources now more bright
Resilient connections take flight
A rabbit's code, both smart and light! 🔍


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 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.

Copy link

New dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/@eggjs/[email protected] Transitive: environment, eval, network +10 1.2 MB fengmk2
npm/@types/[email protected] None 0 868 kB types
npm/@types/[email protected] None 0 9.13 kB types
npm/[email protected] Transitive: environment, filesystem, network, shell +27 5.06 MB bitwiseman
npm/[email protected] eval, filesystem Transitive: shell +2 1.89 MB fdintino

View full report↗︎

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (3)
core/dal-runtime/src/MySqlDataSource.ts (2)

11-12: Add validation checks for retry settings
Consider validating that initRetryTimes is a positive integer. This will help avoid unexpected behavior if a negative or zero value is passed in.


28-29: Use descriptive naming or doc comments for private fields
The new private fields #initRetryTimes and #logger are well-introduced. If more context is needed for future maintainers, consider adding doc comments describing their intended behavior.

core/dal-runtime/test/DataSource.test.ts (1)

90-199: Consider breaking down the large test case

While the test is comprehensive, it's testing multiple operations (insert, update, delete, find, paginate) in a single test case. Consider breaking it down into smaller, focused test cases for better maintainability and easier debugging.

Example structure:

describe('execute', () => {
  // ... setup code ...

  describe('CRUD operations', () => {
    it('should successfully insert a record', async () => {
      // Test insert operation
    });

    it('should successfully update a record', async () => {
      // Test update operation
    });

    it('should successfully find a record', async () => {
      // Test find operation
    });

    it('should successfully delete a record', async () => {
      // Test delete operation
    });

    it('should handle pagination correctly', async () => {
      // Test pagination
    });
  });
});
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between b310ec3 and d6ee030.

📒 Files selected for processing (6)
  • core/dal-runtime/package.json (1 hunks)
  • core/dal-runtime/src/MySqlDataSource.ts (3 hunks)
  • core/dal-runtime/test/DataSource.test.ts (2 hunks)
  • plugin/dal/lib/DalModuleLoadUnitHook.ts (2 hunks)
  • plugin/dal/lib/MysqlDataSourceManager.ts (1 hunks)
  • standalone/standalone/src/Runner.ts (1 hunks)
🔇 Additional comments (13)
core/dal-runtime/src/MySqlDataSource.ts (3)

4-4: Ensure consistent logging interface usage
The import of Logger from '@eggjs/tegg-types' looks good. Just verify that any previously used logging mechanism or console statements are properly replaced by this new logger interface for consistency.


33-34: Constructor destructuring is straightforward
The destructuring approach is concise. Just ensure that any required fallback values or validations are handled if initRetryTimes or logger is undefined.

Also applies to: 37-37


46-49: Ensure initialization call is conditional
Line 46 unconditionally calls #doInit(1) if initSql is truthy. If there's a scenario where initSql is configured but initialization should be postponed (e.g. lazy init), consider a way to skip or delay.

core/dal-runtime/package.json (1)

60-60: Ensure test coverage for mocking with 'mm' library
The addition of the "mm" library is typically for mocking in tests. Make sure the test suite is updated to utilize it effectively, and confirm that all newly added logic (like retry) is covered by tests.
[approve]

plugin/dal/lib/DalModuleLoadUnitHook.ts (3)

2-2: Import of Logger
The Logger import is consistent with the rest of the changes. Confirm that other references to logging inside the plugin still align with this new interface.


9-9: Optional logger integration
Allowing an optional logger is a useful flexibility. Ensure that any log invocations gracefully handle a null/undefined logger to avoid runtime errors.

Also applies to: 11-11, 14-14


26-26: Passing logger in dataSourceOptions
This addition ensures that the logger is consistently injected into the DataSource. Confirm that all references to dataSourceOptions in other parts of the code handle the logger property without issues.

plugin/dal/lib/MysqlDataSourceManager.ts (1)

24-26: Configurations properly separated; consider deeper type checks
Extracting logger from config with the destructuring approach ensures code clarity. If you anticipate special usage or type constraints on logger, ensure deeper type checks or fallbacks. Otherwise, this design is flexible and clean.

Also applies to: 32-32

core/dal-runtime/test/DataSource.test.ts (4)

16-24: LGTM: Well-structured test configuration

The MySQL configuration is properly set up with essential parameters including timezone settings and initialization SQL.


31-39: LGTM: Comprehensive error handling test

Good test coverage for initialization failure scenario. The test properly verifies both the error propagation and the execution of initialization SQL.


41-51: LGTM: Thorough retry mechanism test

The test effectively verifies that initialization retries the specified number of times before giving up.


53-66: LGTM: Well-implemented recovery test

Good test coverage for the successful recovery scenario after an initial failure.

standalone/standalone/src/Runner.ts (1)

172-176: LGTM: Proper logger initialization and injection

Good implementation of logger extraction with fallback to console. The logger is correctly passed to all DAL-related hooks for consistent logging behavior.

core/dal-runtime/src/MySqlDataSource.ts Show resolved Hide resolved
Copy link
Contributor

@killagu killagu left a comment

Choose a reason for hiding this comment

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

LGTM

@gxkl gxkl merged commit 74e7c06 into master Dec 26, 2024
12 checks passed
@gxkl gxkl deleted the feat/dal-init-retry branch December 26, 2024 06:56
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.

2 participants