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

chore: Add postgres dependency for server to startup #36585

Merged
merged 9 commits into from
Sep 30, 2024

Conversation

abhvsn
Copy link
Contributor

@abhvsn abhvsn commented Sep 27, 2024

Description

As in the past we have seen the corruption of postgres DB which is being used for temporal we want to make sure we have a retry mechanism in place if:

  1. APPSMITH_DB_URL is pointing to postgres url
  2. Postgres is waiting in recovery mode

As per local testing when the docker container is abruptly shutdown via docker rm -f {container_name} or docker kill {container_name} or even via docker desktop we end up in state where postgres goes into recovery state.
logs:

2024-09-27 08:02:49 backend stdout | SQL State  : 57P03
2024-09-27 08:02:49 backend stdout | Error Code : 0
2024-09-27 08:02:49 backend stdout | Message    : FATAL: the database system is starting up

Currently we have implemented polling mechanism, but we will keep looking for better alternative here if we can opt for.

Note:

  1. With release dump this is taking ~300sec to get out of that state and start accepting the connections.
  2. With the existing implementation without retries server dies down within 60sec.
INFO exited: backend (exit status 1; not expected)
INFO gave up: backend entered FATAL state, too many start retries too quickly

Reference doc: https://www.notion.so/appsmith/Postgres-critical-scenarios-668f49c96aef40308e24c2a8d6b1137c

/test Sanity

🔍 Cypress test results

Tip

🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
Workflow run: https://github.com/appsmithorg/appsmith/actions/runs/11100944184
Commit: 9dbbe4b
Cypress dashboard.
Tags: @tag.Sanity
Spec:


Mon, 30 Sep 2024 07:26:41 UTC

Communication

Should the DevRel and Marketing teams inform users about this change?

  • Yes
  • No

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features
    • Introduced new functions for enhanced handling of PostgreSQL database connections, including availability checks and parameter extraction.
    • Added a new utility script for managing PostgreSQL connections.
  • Bug Fixes
    • Implemented a retry mechanism for PostgreSQL server availability checks to ensure more reliable connections.
  • Tests
    • Added unit tests to validate the functionality of the PostgreSQL parameter extraction logic.

Copy link
Contributor

coderabbitai bot commented Sep 27, 2024

Warning

Rate limit exceeded

@abhvsn has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 11 minutes and 9 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Files that changed from the base of the PR and between 69e64cf and 9dbbe4b.

Walkthrough

The changes introduce two new functions in the run-java.sh script to enhance PostgreSQL connection handling. The waitForPostgresAvailability function checks the PostgreSQL server's availability with a retry mechanism, while the extract_postgres_db_params function parses a connection string to extract database parameters. These functions are conditionally invoked when the mode variable is set to "pg", ensuring that the necessary database parameters are available before executing the script.

Changes

Files Change Summary
deploy/docker/fs/opt/appsmith/run-java.sh Added waitForPostgresAvailability() for checking PostgreSQL availability and extract_postgres_db_params(conn_string) for parsing connection strings. Conditional logic added to call these functions when mode is "pg".
deploy/docker/fs/opt/appsmith/pg-utils.sh Introduced waitForPostgresAvailability() and extract_postgres_db_params(conn_string) for managing PostgreSQL connections, including examples and logging for connection attempts.
deploy/docker/tests/test_pg-utils.sh Created a test suite for extract_postgres_db_params with various scenarios to validate parameter extraction from connection strings.

Possibly related PRs

Suggested labels

Task, Move to Postgres, DB Infrastructure Pod

Suggested reviewers

  • sharat87
  • pratapaprasanna
  • AnaghHegde

In the script where databases dwell,
Functions arise to handle them well.
With waits and extracts, they pave the way,
For PostgreSQL's dance, come what may.
A robust approach, so clever and bright,
Ensuring connections are ready and right! 🎉


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.

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 the skip-changelog Adding this label to a PR prevents it from being listed in the changelog label Sep 27, 2024
Copy link
Contributor

@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

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 6368215 and 598ef61.

📒 Files selected for processing (1)
  • deploy/docker/fs/opt/appsmith/run-java.sh (2 hunks)
🧰 Additional context used
🪛 Shellcheck
deploy/docker/fs/opt/appsmith/run-java.sh

[warning] 81-96: Quote this to prevent word splitting.

(SC2046)

🔇 Additional comments (2)
deploy/docker/fs/opt/appsmith/run-java.sh (2)

19-64: Well done implementing the PostgreSQL availability check function

You've done a great job with the waitForPostgresAvailability function. It effectively checks the database status and includes proper error handling with retries. This is essential for ensuring the server starts reliably, even when the PostgreSQL database is recovering.


120-123: Great job integrating PostgreSQL parameter extraction and availability checking

By adding this conditional block, you're ensuring that the script properly handles PostgreSQL configurations. Extracting the database parameters and verifying the database's availability before proceeding demonstrates good attention to detail and improves the robustness of the startup process.

deploy/docker/fs/opt/appsmith/run-java.sh Outdated Show resolved Hide resolved
@abhvsn abhvsn added the ok-to-test Required label for CI label Sep 27, 2024
@abhvsn abhvsn marked this pull request as draft September 27, 2024 09:04
@abhvsn abhvsn self-assigned this Sep 27, 2024
@abhvsn abhvsn marked this pull request as ready for review September 27, 2024 09:20
Copy link
Contributor

@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

🧹 Outside diff range and nitpick comments (1)
deploy/docker/fs/opt/appsmith/pg-utils.sh (1)

61-88: Implement Validation for Extracted Parameters

While your function extracts the database parameters, it's prudent to validate these parameters before proceeding. This helps in catching any issues with the connection string early and prevents potential runtime errors.

Consider adding validation checks like:

if [ -z "$PG_DB_HOST" ] || [ -z "$PG_DB_NAME" ]; then
  tlog "Error: Missing essential database parameters. Please verify the connection string."
  exit 1
fi

This ensures that the script only continues if all necessary parameters are present.

🧰 Tools
🪛 Shellcheck

[warning] 65-80: Quote this to prevent word splitting.

(SC2046)

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 62fb7d2 and 76e95c3.

📒 Files selected for processing (1)
  • deploy/docker/fs/opt/appsmith/pg-utils.sh (1 hunks)
🧰 Additional context used
🪛 Shellcheck
deploy/docker/fs/opt/appsmith/pg-utils.sh

[warning] 65-80: Quote this to prevent word splitting.

(SC2046)

🔇 Additional comments (3)
deploy/docker/fs/opt/appsmith/pg-utils.sh (3)

3-48: Good Job on Implementing PostgreSQL Availability Check

You've done well to create the waitForPostgresAvailability function. This ensures that the PostgreSQL server is ready before the application proceeds, which is essential for maintaining reliability and preventing connection issues. The use of a retry mechanism with proper logging demonstrates sound scripting practices.


13-13: Verify Permissions for su postgres Command

When using su postgres -c "pg_isready...", it's important to consider the script's execution environment. The su command requires the script to run with sufficient privileges; otherwise, it may fail due to permission issues.

I recommend checking whether the script will always have the necessary permissions in your deployment scenarios. If not, you might explore alternatives like adjusting user permissions or using sudo with appropriate configurations.


90-92: ⚠️ Potential issue

Comment Out Example Usage to Prevent Unintended Execution

Including examples is a great way to document how to use your functions. However, it's important to ensure these examples don't execute unintentionally.

Please modify the example usage as follows:

-# Example usage of the functions
-# waitForPostgresAvailability
-# extract_postgres_db_params "postgresql://user:password@localhost:5432/dbname"
+# Example usage of the functions
+# waitForPostgresAvailability
+# extract_postgres_db_params "postgresql://user:password@localhost:5432/dbname"

By commenting out the lines fully, you prevent the sample code from running if the script is executed, while still providing valuable examples to other developers.

Likely invalid or redundant comment.

Comment on lines 65 to 80
IFS=' ' read -r USER PASSWORD HOST PORT DB <<<$(node -e "
const connectionString = process.argv[1];
const pgUri = connectionString.startsWith(\"postgresql://\")
? connectionString
: 'http://' + connectionString; //Prepend a fake scheme for URL parsing
const url = require('url');
const parsedUrl = new url.URL(pgUri);

// Extract the pathname and remove the leading '/'
const db = parsedUrl.pathname.substring(1);

// Default the port to 5432 if it's empty
const port = parsedUrl.port || '5432';

console.log(\`\${parsedUrl.username || '-'} \${parsedUrl.password || '-'} \${parsedUrl.hostname} \${port} \${db}\`);
" "$conn_string")
Copy link
Contributor

@coderabbitai coderabbitai bot Sep 27, 2024

Choose a reason for hiding this comment

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

⚠️ Potential issue

Quote Command Substitution to Prevent Word Splitting

In the extract_postgres_db_params function, the output of the node command should be enclosed in quotes to prevent word splitting. Without quotes, parameters containing spaces could lead to incorrect parsing of database credentials.

Here's how you can fix it:

-    IFS=' ' read -r USER PASSWORD HOST PORT DB <<<$(node -e "
+    IFS=' ' read -r USER PASSWORD HOST PORT DB <<<"$(node -e "
      const connectionString = process.argv[1];
      const pgUri = connectionString.startsWith(\"postgresql://\")
        ? connectionString
        : 'http://' + connectionString; // Prepend a fake scheme for URL parsing
      const url = require('url');
      const parsedUrl = new url.URL(pgUri);

      // Extract the pathname and remove the leading '/'
      const db = parsedUrl.pathname.substring(1);

      // Default the port to 5432 if it's empty
      const port = parsedUrl.port || '5432';

      console.log(\`\${parsedUrl.username || '-'} \${parsedUrl.password || '-'} \${parsedUrl.hostname} \${port} \${db}\`);
-    " "$conn_string")
+    " "$conn_string")"

By quoting the command substitution, you ensure that all the variables are read correctly, even if they contain spaces.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
IFS=' ' read -r USER PASSWORD HOST PORT DB <<<$(node -e "
const connectionString = process.argv[1];
const pgUri = connectionString.startsWith(\"postgresql://\")
? connectionString
: 'http://' + connectionString; //Prepend a fake scheme for URL parsing
const url = require('url');
const parsedUrl = new url.URL(pgUri);
// Extract the pathname and remove the leading '/'
const db = parsedUrl.pathname.substring(1);
// Default the port to 5432 if it's empty
const port = parsedUrl.port || '5432';
console.log(\`\${parsedUrl.username || '-'} \${parsedUrl.password || '-'} \${parsedUrl.hostname} \${port} \${db}\`);
" "$conn_string")
IFS=' ' read -r USER PASSWORD HOST PORT DB <<<"$(node -e "
const connectionString = process.argv[1];
const pgUri = connectionString.startsWith(\"postgresql://\")
? connectionString
: 'http://' + connectionString; //Prepend a fake scheme for URL parsing
const url = require('url');
const parsedUrl = new url.URL(pgUri);
// Extract the pathname and remove the leading '/'
const db = parsedUrl.pathname.substring(1);
// Default the port to 5432 if it's empty
const port = parsedUrl.port || '5432';
console.log(\`\${parsedUrl.username || '-'} \${parsedUrl.password || '-'} \${parsedUrl.hostname} \${port} \${db}\`);
" "$conn_string")"
🧰 Tools
🪛 Shellcheck

[warning] 65-80: Quote this to prevent word splitting.

(SC2046)

Copy link
Member

Choose a reason for hiding this comment

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

@abhvsn Can you please modify the code to resolve this comment?

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

Copy link
Contributor Author

@abhvsn abhvsn Sep 30, 2024

Choose a reason for hiding this comment

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

@mohanarpit I am not able to understand the advantage for this. I have written the tests to see the effect of this change but couldn't find anything, tests are written for following APPSMITH_DB_URL input for which the response was exactly same with or without quotes:

  1. postgresql://user:password@localhost:5432/dbname
  2. postgresql://user:p a s s w o r d@localhost:5432/dbname
  3. postgresql://user:password@localhost:5432/db name

Copy link
Contributor Author

Choose a reason for hiding this comment

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

https://github.com/coderabbitai can you please check the above comment, if I'm missing something?

@abhvsn
Copy link
Contributor Author

abhvsn commented Sep 27, 2024

/build-deploy-preview skip-tests=true

Copy link

Deploying Your Preview: https://github.com/appsmithorg/appsmith/actions/runs/11070194243.
Workflow: On demand build Docker image and deploy preview.
skip-tests: true.
env: ``.
PR: 36585.
recreate: .

Copy link

Deploy-Preview-URL: https://ce-36585.dp.appsmith.com

Copy link
Contributor

@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: 4

🧹 Outside diff range and nitpick comments (1)
deploy/docker/tests/test_pg-utils.sh (1)

48-54: Let's enhance our debugging function for even more clarity.

The echo_params function is a great addition for debugging! It's always important to have tools that help us understand what's happening when things don't go as expected.

To make this even more helpful, let's add a little more context. How about we modify it like this:

echo_params() {
  echo "Extracted PostgreSQL parameters:"
  echo "--------------------------------"
  echo "PG_DB_USER: $PG_DB_USER"
  echo "PG_DB_PASSWORD: $PG_DB_PASSWORD"
  echo "PG_DB_HOST: $PG_DB_HOST"
  echo "PG_DB_PORT: $PG_DB_PORT"
  echo "PG_DB_NAME: $PG_DB_NAME"
  echo "--------------------------------"
}

Can anyone tell me why adding this extra context might be beneficial when we're debugging our tests?

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 76e95c3 and 69e64cf.

📒 Files selected for processing (1)
  • deploy/docker/tests/test_pg-utils.sh (1 hunks)

deploy/docker/tests/test_pg-utils.sh Outdated Show resolved Hide resolved
deploy/docker/tests/test_pg-utils.sh Outdated Show resolved Hide resolved
deploy/docker/tests/test_pg-utils.sh Outdated Show resolved Hide resolved
deploy/docker/tests/test_pg-utils.sh Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ok-to-test Required label for CI skip-changelog Adding this label to a PR prevents it from being listed in the changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants