feat: dynamically set shm (#135)

This commit is contained in:
fryorcraken 2025-02-17 20:17:34 +11:00 committed by GitHub
parent 6fc66099f1
commit 387354c977
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 60 additions and 8 deletions

View File

@ -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.

View File

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

28
set_postgres_shm.sh Executable file
View File

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

View File

@ -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=""

View File

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