diff --git a/README.md b/README.md index 6843ce4..28fd595 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ Note that if you just want to relay traffic (not publish), you don't need one. ./register_rln.sh ``` -### 💽 2. Select storage size +### 💽 2. Select DB Parameters Waku runs a PostgreSQL Database to store messages from the network and serve them to other peers. To prevent the database to grow indefinitely, you need to select how much disk space to allocate. @@ -63,6 +63,18 @@ Or select your own value. For example, `50GB`: echo "STORAGE_SIZE=50GB" >> .env ``` +Depending on your machine's memory, it may be worth allocating more memory to the Postgres container to ensure heavy queries are served: + +```shell +./set_postgres_shm.sh +``` + +Or select your own value manually, for example, `4g`: + +```shell +echo "POSTGRES_SHM=4g" >> .env +``` + ### 🖥️ 3. Start your node Start all processes: nwaku node, database and grafana for metrics. Your [RLN](https://rate-limiting-nullifier.github.io/rln-docs/what_is_rln.html) membership is loaded into nwaku under the hood. diff --git a/docker-compose.yml b/docker-compose.yml index c7e0b73..f095cd3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -112,7 +112,7 @@ services: # and the store-message-db-url is set to use Postgres image: postgres:15.4-alpine3.18 restart: on-failure:5 - shm_size: '1g' # Set shared memory size to 1 GB + shm_size: "${POSTGRES_SHM:-1g}" # Set default shared memory size to 1 GB environment: <<: *pg_env volumes: diff --git a/set_postgres_shm.sh b/set_postgres_shm.sh new file mode 100755 index 0000000..cf7297a --- /dev/null +++ b/set_postgres_shm.sh @@ -0,0 +1,28 @@ +#!/bin/sh + +# check if POSTGRES_SHM is already specified +if [ -f "./.env" ]; then + . ./.env +fi + +if [ -n "$POSTGRES_SHM" ]; then + >&2 echo "POSTGRES_SHM is specified in .env file, doing nothing" + exit 0 +fi + +# Set PostgreSQL container Shared Memory value +TOTAL_MEM_MB=$(free -m|grep Mem| awk '{ print $2 }') +if [ "${TOTAL_MEM_MB}" -ge 4096 ]; then + # Allocate 2GB of Shared Memory for Postgres if machine has more than 4GB RAM + POSTGRES_SHM='2g' +else + # Allocate 1GB of Shared Memory for Postgres + POSTGRES_SHM='1g' +fi + +>&2 echo "Setting PostgreSQL container SHM to ${POSTGRES_SHM}" +if [ "$1" = "echo-value" ]; then + echo ${POSTGRES_SHM} +else + echo "POSTGRES_SHM=${POSTGRES_SHM}" >> .env +fi diff --git a/set_storage_retention.sh b/set_storage_retention.sh index 164b0ce..02a0705 100755 --- a/set_storage_retention.sh +++ b/set_storage_retention.sh @@ -54,10 +54,11 @@ fi # check if STORAGE_SIZE is already specified if [ -f "./.env" ]; then . ./.env - if [ -n "$STORAGE_SIZE" ]; then - >&2 echo "STORAGE_SIZE is specified in .env file, doing nothing" - exit 0 - fi +fi + +if [ -n "$STORAGE_SIZE" ]; then + >&2 echo "STORAGE_SIZE is specified in .env file, doing nothing" + exit 0 fi SUDO="" diff --git a/setup_wizard.sh b/setup_wizard.sh index 257fe2e..3bc7b6d 100755 --- a/setup_wizard.sh +++ b/setup_wizard.sh @@ -66,18 +66,29 @@ if [ -z "$STORAGE_SIZE" ]; then exit 1 fi +echocol "...." +echocol "Selecting a SHM for Postgres..." +POSTGRES_SHM=$(./set_postgres_shm.sh echo-value) + +if [ -z "$POSTGRES_SHM" ]; then + echo "Error encountered when estimating postgres container shm, exiting" + exit 1 +fi + echocol "...." echocol "The following parameters will be saved to your .env file. Press ENTER to confirm or quit with CONTROL-C to abort:" echo "RLN_RELAY_ETH_CLIENT_ADDRESS='$RLN_RELAY_ETH_CLIENT_ADDRESS' ETH_TESTNET_KEY=$ETH_TESTNET_KEY RLN_RELAY_CRED_PASSWORD='$RLN_RELAY_CRED_PASSWORD' -STORAGE_SIZE=$STORAGE_SIZE" +STORAGE_SIZE=$STORAGE_SIZE +POSTGRES_SHM=$POSTGRES_SHM" read foo echo "RLN_RELAY_ETH_CLIENT_ADDRESS='$RLN_RELAY_ETH_CLIENT_ADDRESS' ETH_TESTNET_KEY=$ETH_TESTNET_KEY RLN_RELAY_CRED_PASSWORD='$RLN_RELAY_CRED_PASSWORD' -STORAGE_SIZE=$STORAGE_SIZE" > ./.env +STORAGE_SIZE=$STORAGE_SIZE +POSTGRES_SHM=$POSTGRES_SHM" > ./.env SUDO="" if ! docker info > /dev/null 2>&1; then