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

Fix/default to maindb if read replica errs #1221

Merged
merged 4 commits into from
Jun 5, 2024

Conversation

samika98
Copy link
Contributor

[Replace Me With Meaningful Name] - #[Issue]

Description

Include a summary of the change and which issue it addresses in the title of the PR.

Include relevant motivation, context, brief description and impact of the change(s). List follow-up tasks here.

How Has This Been Tested?

Describe the tests that you ran to verify your changes. Provide instructions for reproduction.

  • Test A (e.g. Test A - New test that ... ran in local, docker, and dev unstable.)
  • Test B

Definition of Done

Before submitting this PR, please make sure:

  • The work addresses the description and outcomes in the issue
  • I have added relevant tests for new or updated functionality
  • My code follows conventions, is well commented, and easy to understand
  • My code builds and tests pass without any errors or warnings
  • I have tagged the relevant reviewers
  • I have updated the READMEs of affected packages
  • I have made corresponding changes to the documentation
  • The changes have been communicated to interested parties

References:

Please list relevant documentation (e.g. tech specs, articles, related work etc.) relevant to this change, and note if the documentation has been updated.

@samika98 samika98 requested review from smrz2001 and 3benbox May 31, 2024 19:56
@codecov-commenter
Copy link

codecov-commenter commented May 31, 2024

Codecov Report

Attention: Patch coverage is 68.42105% with 6 lines in your changes missing coverage. Please review.

Project coverage is 77.95%. Comparing base (e8b6558) to head (c10f1c5).
Report is 1 commits behind head on develop.

Files Patch % Lines
src/services/request-service.ts 57.14% 3 Missing ⚠️
src/repositories/replication-request-repository.ts 50.00% 2 Missing ⚠️
src/db-connection.ts 87.50% 1 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1221      +/-   ##
===========================================
- Coverage    78.18%   77.95%   -0.24%     
===========================================
  Files           48       48              
  Lines         1985     1996      +11     
  Branches       314      315       +1     
===========================================
+ Hits          1552     1556       +4     
- Misses         433      440       +7     

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

let found = await this.replicationRequestRepository.findByCid(cid)
let found
try {
found = await this.replicationRequestRepository.findByCid(cid)
Copy link

Choose a reason for hiding this comment

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

My preference would be to expect the replica to work if it exists, but default the config to main if there is none defined. I think we should expect it to work if it's defined, but it should be optional.

Copy link

Choose a reason for hiding this comment

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

In terms of implementation, I'd expect we either set up a RO connection pool, or we just use the main pool and the app still calls replicationRequestRepository for all reads because it doesn't require the writer, without needing to know anything about the actual DB used.

This also avoids doing double reads for things that don't actually exist and potentially getting hung trying to talk to a non-exists RO instance.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, it all around a better design to make the read replication optional. FYI, currently all our reads do not got to the replica. If the configuration for the read replica is not specified or if any of the config params are missing, we default to the maindb. Will add a commit for this and tag you, let me know if this might fix it or there might be something else I missed

Copy link

Choose a reason for hiding this comment

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

I like the changes you made, do we still need this fallback? if it fails now, it seems like it's failing for a reason so there's not sure we need to try another database.

Copy link
Contributor Author

@samika98 samika98 Jun 5, 2024

Choose a reason for hiding this comment

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

yup, in the case of the read replica not having the request. If sync b/w the db's is slow

Copy link

Choose a reason for hiding this comment

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

Yeah, what I was trying to say is that if the RR doesn't have the request, it's unlikely the main DB does so there's no reason to ask. If for some reason we're far enough behind on replicating (i.e. minutes), we probably don't want to load the main database more and should investigate. Otherwise, we can tolerate the client retrying again in a few seconds and the RR will respond with data.

@@ -71,7 +71,7 @@ export async function createDbConnection(dbConfig: Db = config.db): Promise<Knex

export async function createReplicaDbConnection(
replica_db_config: Replicadb = config.replica_db
): Promise<Knex> {
): Promise<{ connection: Knex; type: string }> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dav1do this change makes the readReplica optional and by introducing a connectionType we have captured the two modes it can work in.
The type is also useful to avoid misleading logs in the connection failure mode giving devs more insight into what is happening in the background

@samika98 samika98 requested a review from stephhuynh18 June 4, 2024 18:45
Copy link

@dav1do dav1do left a comment

Choose a reason for hiding this comment

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

Looks good. One more comment since I'm not sure we need to fallback now but approving.

let found = await this.replicationRequestRepository.findByCid(cid)
let found
try {
found = await this.replicationRequestRepository.findByCid(cid)
Copy link

Choose a reason for hiding this comment

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

I like the changes you made, do we still need this fallback? if it fails now, it seems like it's failing for a reason so there's not sure we need to try another database.

@samika98 samika98 merged commit 7ad08a0 into develop Jun 5, 2024
5 checks passed
@samika98 samika98 deleted the fix/default-to-maindb-if-read-replica-errs branch June 5, 2024 17:16
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