Repository navigation
Add script for calculating DB memory settings #3541
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
base: main
Are you sure you want to change the base?
Changes from 10 commits
e5e80f2
6990605
3547a22
675902e
f4c77ed
992aa94
eb3f359
b04ba10
2992485
2362ba5
6826cbb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -219,6 +219,14 @@ if [ -f "temp/deployment.tar.gz" ]; then | |||||||||||||||
| docker load < temp/deployment.tar.gz | ||||||||||||||||
| fi | ||||||||||||||||
|
|
||||||||||||||||
| # Calculate DB memory settings | ||||||||||||||||
| . ./temp/host_init/calculate_db_memory.sh | ||||||||||||||||
|
|
||||||||||||||||
| # Calculate host RAM | ||||||||||||||||
| export OR_RAM="$(( $(free -m | awk '/^Mem:/{print $2}') * 40 / 100 ))" | ||||||||||||||||
| export OR_KC_RAM="$(( $(free -m | awk '/^Mem:/{print $2}') * 20 / 100 ))" | ||||||||||||||||
| export OR_DB_RAM="$(( $(free -m | awk '/^Mem:/{print $2}') * 25 / 100 ))" | ||||||||||||||||
|
Comment on lines
+226
to
+228
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The heredoc at line 191 is unquoted, so these No compose file in this repo reads
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Should |
||||||||||||||||
|
|
||||||||||||||||
| # Login to Private ECR Registry to download restricted images | ||||||||||||||||
| if [ -n "$OR_AWS_ECR_ACCOUNT_ID" ] && [ -n "$OR_AWS_ECR_REGION" ]; then | ||||||||||||||||
| aws ecr get-login-password --region $OR_AWS_ECR_REGION | docker login --username AWS --password-stdin $OR_AWS_ECR_ACCOUNT_ID.dkr.ecr.$OR_AWS_ECR_REGION.amazonaws.com &>/dev/null | ||||||||||||||||
|
|
||||||||||||||||
| Original file line number | Diff line number | Diff line change | |||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,30 @@ | |||||||||||||||||||||||
| #!/bin/bash | |||||||||||||||||||||||
| set -eo pipefail | |||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The
Suggested change
|
|||||||||||||||||||||||
|
|
|||||||||||||||||||||||
| # Retrieve largest uncompressed chunk size from query-exporter service | |||||||||||||||||||||||
| CHUNK_SIZE="$(curl -s http://localhost:8406/metrics | grep '^pg_datapoint_largest_uncompressed_chunk' | awk '{printf "%.0f\n", $NF}' || true)" | |||||||||||||||||||||||
| if [ -z "$CHUNK_SIZE" ]; then | |||||||||||||||||||||||
| echo 'Could not retrieve chunk size from query-exporter service, using defaults from Docker Compose' | |||||||||||||||||||||||
| else | |||||||||||||||||||||||
| # Calculate minimum RAM size and round up to the next whole GB (1.2 GB becomes 2 GB) | |||||||||||||||||||||||
| MINIMUM_RAM_SIZE="$((((4 * CHUNK_SIZE + 1024 - 1) / 1024) * 1024))" | |||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A metric value of 0 passes the if [ "${CHUNK_SIZE:-0}" -eq 0 ]; then |
|||||||||||||||||||||||
|
|
|||||||||||||||||||||||
| # Set shared buffers to 25% of minimum RAM size. | |||||||||||||||||||||||
| export OR_DB_SHARED_BUFFERS="$((MINIMUM_RAM_SIZE / 4))" | |||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exporting bare MB values works when compose adds the unit, as in
shm_size: ${OR_DB_SHM:-156}MB
deploy:
resources:
limits:
memory: ${OR_DB_RAM:-512}MB
command:
- postgres
- -c
- shared_buffers=${OR_DB_SHARED_BUFFERS:-128}MB
- -c
- effective_cache_size=${OR_DB_CACHE_SIZE:-384}MB
- -c
- maintenance_work_mem=${OR_DB_MAINTENANCE_WORK_MEM:-56}MB
- -c
- work_mem=${OR_DB_WORK_MEM:-4}MBThis also affects custom projects. A compose file that follows the guide's current example gets the misread values above, and in the new form a value such as |
|||||||||||||||||||||||
| # Calculate SHM size by adding 20% to the shared buffers and round up to the next whole GB (1.2 GB becomes 2 GB) | |||||||||||||||||||||||
| export OR_DB_SHM="$((((OR_DB_SHARED_BUFFERS * 120 / 100 + 1024 - 1) / 1024) * 1024))" | |||||||||||||||||||||||
| # Set maintenance work mem to 12.5% of minimum RAM size and round up to the next multiple of 128 MB. | |||||||||||||||||||||||
| export OR_DB_MAINTENANCE_WORK_MEM="$((((MINIMUM_RAM_SIZE / 8 + 128 - 1) / 128) * 128))" | |||||||||||||||||||||||
| # Set worker mem to 1% of minimum RAM size and round up to the next multiple of 16 MB. | |||||||||||||||||||||||
| export OR_DB_WORK_MEM="$((((MINIMUM_RAM_SIZE / 100 + 16 - 1) / 16) * 16))" | |||||||||||||||||||||||
| # Calculate effective cache size | |||||||||||||||||||||||
| export OR_DB_CACHE_SIZE="$((MINIMUM_RAM_SIZE - OR_DB_SHARED_BUFFERS))" | |||||||||||||||||||||||
|
|
|||||||||||||||||||||||
| echo 'Chunk size:' "$CHUNK_SIZE MB" | |||||||||||||||||||||||
| echo 'Minimum RAM size:' "$MINIMUM_RAM_SIZE MB" | |||||||||||||||||||||||
| echo 'Shared buffers:' "$OR_DB_SHARED_BUFFERS MB" | |||||||||||||||||||||||
| echo 'SHM Size:' "$OR_DB_SHM MB" | |||||||||||||||||||||||
| echo 'Maintenance worker mem:' "$OR_DB_MAINTENANCE_WORK_MEM MB" | |||||||||||||||||||||||
| echo 'Worker mem:' "$OR_DB_WORK_MEM MB" | |||||||||||||||||||||||
| echo 'Effective cache size:' "$OR_DB_CACHE_SIZE MB" | |||||||||||||||||||||||
| fi | |||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
temp/envis loaded just above, so this script and theOR_DB_RAMexport below overwrite any value a project set in.ci_cd/envor a secret, which.ci_cd/README.mddescribes as the way to configure compose variables. A host tuned by hand would change on its next deployment. Calculating only the values that are unset would keep those settings, and the README's variables section should mention the new behavior.