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: read replica service #2377

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft

feat: read replica service #2377

wants to merge 1 commit into from

Conversation

liorzam
Copy link
Collaborator

@liorzam liorzam commented May 14, 2024

PR Type

enhancement, bug_fix


Description

  • Introduced ReadPrismaService to handle database operations using a read replica, enhancing performance and scalability.
  • Defined NotFoundEvaluationError for more specific error handling in data analytics services.
  • Added new environment variable and updated Prisma module to conditionally use read replica based on configuration.
  • Updated dependencies to include Prisma read replicas extension.

Changes walkthrough 📝

Relevant files
Enhancement
5 files
data-analytics.service.ts
Enhance error handling and add read replica support in
DataAnalyticsService

services/workflows-service/src/data-analytics/data-analytics.service.ts

  • Added import for node:util and ReadPrismaService.
  • Introduced NotFoundEvaluationError for better error handling.
  • Replaced generic error throwing with specific NotFoundEvaluationError.
  • +9/-6     
    errors.ts
    Define NotFoundEvaluationError for specific rule evaluation errors

    services/workflows-service/src/data-analytics/errors.ts

  • Defined a new error class NotFoundEvaluationError with serialization
    of inlineRule.
  • +10/-0   
    prisma.module.ts
    Update Prisma module to support read replica service         

    services/workflows-service/src/prisma/prisma.module.ts

  • Updated module to conditionally provide ReadPrismaService based on
    environment.
  • Added dynamic provider setup for read replica service.
  • +16/-2   
    prisma.read-replica.service.ts
    Implement ReadPrismaService for handling read replica operations

    services/workflows-service/src/prisma/prisma.read-replica.service.ts

  • Created ReadPrismaService extending PrismaService for read replica
    functionality.
  • Implemented connection and query logging for read replica.
  • +39/-0   
    prisma.service.ts
    Integrate read replica support in PrismaService                   

    services/workflows-service/src/prisma/prisma.service.ts

    • Integrated read replica extension in the base PrismaService.
    +4/-1     
    Configuration changes
    2 files
    env.ts
    Add environment variable for read replica database URL     

    services/workflows-service/src/env.ts

  • Added environment variable READ_REPLICA_DATABASE_URL for configuring
    database replica URL.
  • +1/-0     
    consts.ts
    Define constant for Prisma read replica service                   

    services/workflows-service/src/prisma/consts.ts

  • Added constant PRISMA_READ_SERVICE for read replica service
    identifier.
  • +1/-0     
    Dependencies
    2 files
    pnpm-lock.yaml
    Update dependencies to include Prisma read replicas extension

    pnpm-lock.yaml

    • Added dependency for @prisma/extension-read-replicas.
    +13/-2   
    package.json
    Add Prisma read replicas extension to package dependencies

    services/workflows-service/package.json

    • Added @prisma/extension-read-replicas to package dependencies.
    +1/-0     

    💡 PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    Copy link

    changeset-bot bot commented May 14, 2024

    ⚠️ No Changeset found

    Latest commit: edbc9a9

    Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

    This PR includes no changesets

    When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

    Click here to learn what changesets are, and how to add one.

    Click here if you're a maintainer who wants to add a changeset to this PR

    Copy link
    Contributor

    coderabbitai bot commented May 14, 2024

    Important

    Auto Review Skipped

    Draft detected.

    Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

    You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


    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>.
      • 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 generate interesting stats about this repository and render them as a table.
      • @coderabbitai show all the console.log statements in this repository.
      • @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.

    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 as PR comments)

    • @coderabbitai pause to pause the reviews on a PR.
    • @coderabbitai resume to resume the paused reviews.
    • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
    • @coderabbitai resolve resolve all the CodeRabbit review comments.
    • @coderabbitai help to get help.

    Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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

    @github-actions github-actions bot added enhancement New feature or request bug_fix labels May 14, 2024
    Copy link
    Contributor

    PR Description updated to latest commit (edbc9a9)

    Copy link
    Contributor

    PR Review 🔍

    ⏱️ Estimated effort to review [1-5]

    3, because the PR includes multiple changes across various files including service logic, error handling, and configuration. The introduction of a read replica service and the associated error class requires careful consideration of the new logic and its impact on existing functionalities.

    🧪 Relevant tests

    No

    ⚡ Possible issues

    Possible Bug: The ReadPrismaService extends PrismaService and overrides the onModuleInit and onModuleDestroy methods without calling super.onModuleInit() and super.onModuleDestroy() at the beginning of these methods. This might lead to initialization and cleanup issues if the base class's implementation changes.

    Performance Concern: The ReadPrismaService logs every query made to the read replica. This could potentially lead to performance issues due to excessive logging, especially under high load.

    🔒 Security concerns

    No

    Code feedback:
    relevant fileservices/workflows-service/src/prisma/prisma.read-replica.service.ts
    suggestion      

    Consider calling super.onModuleInit() at the beginning of the onModuleInit method to ensure that any initialization logic in the base class is executed. [important]

    relevant lineasync onModuleInit() {

    relevant fileservices/workflows-service/src/prisma/prisma.read-replica.service.ts
    suggestion      

    To avoid potential performance issues, consider adding a condition to check the log level before logging each query in the read replica. This way, you can avoid the overhead of logging detailed query information in production environments. [important]

    relevant linethis.logger.debug(`Read Replica Query: ${e.query}`);

    Copy link
    Contributor

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Enhancement
    Add error handling for database connection and disconnection to improve robustness

    Consider handling potential exceptions during the connection and disconnection processes
    in onModuleInit and onModuleDestroy methods to prevent the application from crashing due
    to unhandled promise rejections.

    services/workflows-service/src/prisma/prisma.read-replica.service.ts [22-35]

    -await this.readClient.$connect();
    -await this.readClient.$disconnect();
    +try {
    +  await this.readClient.$connect();
    +} catch (error) {
    +  this.logger.error('Failed to connect to read replica:', error);
    +}
    +try {
    +  await this.readClient.$disconnect();
    +} catch (error) {
    +  this.logger.error('Failed to disconnect from read replica:', error);
    +}
     
    Suggestion importance[1-10]: 8

    Why: Adding error handling for connection and disconnection processes is crucial for preventing application crashes due to unhandled promise rejections, making this a valuable suggestion.

    8
    Possible issue
    Correct the conditional service provision to avoid configuration conflicts

    Ensure that the conditional logic for choosing between ReadPrismaService and PrismaService
    based on the READ_REPLICA_DATABASE_URL environment variable is correctly implemented.
    Currently, the useClass and useFactory might conflict, causing unexpected behavior.

    services/workflows-service/src/prisma/prisma.module.ts [13-18]

     {
       provide: PRISMA_READ_SERVICE,
    -  useClass: Boolean(env.READ_REPLICA_DATABASE_URL) ? ReadPrismaService : PrismaService,
    +  useFactory(logger: AppLoggerService): PrismaService {
    +    if (Boolean(env.READ_REPLICA_DATABASE_URL)) {
    +      return new ReadPrismaService(logger);
    +    } else {
    +      return new PrismaService(logger);
    +    }
    +  },
       inject: [AppLoggerService],
    -  useFactory(logger: AppLoggerService): PrismaService {
    -    return new ReadPrismaService(logger);
    -  },
     }
     
    Suggestion importance[1-10]: 7

    Why: The suggestion correctly identifies a potential issue with conflicting useClass and useFactory in the service provider configuration, which could lead to unexpected behavior.

    7
    Verify compatibility of Prisma extensions with the installed Prisma client version

    Ensure that the readReplicas extension is compatible with the current version of
    @prisma/client. The peer dependency listed in the package suggests a higher version than
    what is currently installed.

    services/workflows-service/src/prisma/prisma.read-replica.service.ts [1]

    +// Ensure compatibility or update @prisma/client to a compatible version
     import { readReplicas } from '@prisma/extension-read-replicas';
     
    Suggestion importance[1-10]: 6

    Why: The suggestion is relevant as it addresses potential compatibility issues between @prisma/extension-read-replicas and the installed version of @prisma/client, which is crucial for preventing runtime errors.

    6

    import { env } from '@/env';

    @Injectable()
    export class ReadPrismaService extends PrismaService {
    Copy link
    Collaborator

    Choose a reason for hiding this comment

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

    Can you give an example of how will you use it? Assuming we have a new endpoint that fetches EndUsers, how will you make the query using the read replica?

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    None yet

    3 participants