mirror of
https://github.com/wekan/wekan.git
synced 2026-11-25 04:49:41 -05:00
Symptom: WeKan never started, looping "MongoDB not ready yet, retrying in 5 seconds..."
forever, even though mongod appeared to be running.
Cause: mongodb-control started mongod with a HARDCODED --wiredTigerCacheSizeGB 32. On any
server with less than ~34 GB RAM, mongod was OOM-killed seconds after startup. That
included the temporary mongod started to initialise the replica set: it died before
db-eval could reach it (log showed "child process started successfully" then 30 s of
failed pings), so the replica set was never initiated. The final mongod then ran with
--replSet rs0 but no config, no PRIMARY was elected, and wekan-control (which needs the
replica-set primary for Meteor change streams) waited forever.
Fixes in mongodb-control:
- WiredTiger cache is now RAM-aware: ~50% of (MemTotal - 1 GB), the same formula mongod
uses for its own default, capped at 32 GB, min 1 GB. Override with
MONGODB_WIREDTIGER_CACHE_GB. Applied to both the temp replica-set-check mongod and the
final mongod.
- Temporary-mongod readiness wait raised 30 s -> 90 s so a large database that needs
longer to replay its journal still gets its replica set initialised/reconfigured.
Fix in migration-control:
- The temporary source mongod started to READ the data during a MongoDB -> FerretDB
migration now uses a conservative RAM-aware cache (1/4 of (MemTotal - 1 GB), cap 8 GB,
override MIGRATION_MONGOD_CACHE_GB), leaving room for the target FerretDB and the Node
importer that run alongside it. Applied only to the mongod 7 (WiredTiger-only) read so
the flag can never clash with a legacy MMAPv1 MongoDB 3.x dbpath.
Thanks to xet7.
286 lines
12 KiB
Bash
Executable File
286 lines
12 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
if [[ "$PATH" != *"$SNAP_INSTANCE_NAME"* ]]; then
|
|
export PATH="${PATH/\/snap\/wekan\//\/snap\/$SNAP_INSTANCE_NAME\/}"
|
|
fi
|
|
|
|
# MongoDB Control Script
|
|
# Starts MongoDB 7.x server only
|
|
|
|
# get wekan/mongo settings
|
|
echo "Reading snap settings..."
|
|
source $SNAP/bin/wekan-read-settings
|
|
|
|
# Debug: Show what we got from snap settings
|
|
echo "Snap settings loaded:"
|
|
echo " MONGODB_BIND_IP: '${MONGODB_BIND_IP}'"
|
|
echo " MONGODB_PORT: '${MONGODB_PORT}'"
|
|
echo " MONGODB_BIND_UNIX_SOCKET: '${MONGODB_BIND_UNIX_SOCKET}'"
|
|
|
|
# Debug: Check snap settings directly
|
|
echo "Direct snap settings check:"
|
|
echo " mongodb-port: $(snapctl get mongodb-port 2>/dev/null || echo 'not set')"
|
|
echo " mongodb-bind-ip: $(snapctl get mongodb-bind-ip 2>/dev/null || echo 'not set')"
|
|
echo " mongodb-bind-unix-socket: $(snapctl get mongodb-bind-unix-socket 2>/dev/null || echo 'not set')"
|
|
|
|
if [ "true" == "${DISABLE_MONGODB}" ]; then
|
|
echo "mongodb is disabled. Stop service"
|
|
snapctl stop --disable ${SNAP_NAME}.mongodb
|
|
exit 0
|
|
fi
|
|
|
|
# The migration-success marker is AUTHORITATIVE: once the MongoDB -> FerretDB migration
|
|
# has finished, the data lives in FerretDB (SQLite) and MongoDB must NOT run again (both
|
|
# bind the same port). Even if the `database` setting was never flipped to ferretdb, repair
|
|
# it and disable this service so ferretdb-control takes over. This runs BEFORE the
|
|
# migration-pending check so a completed migration is never re-attempted.
|
|
if [ -f "$SNAP_COMMON/.migration-to-ferretdb-done" ]; then
|
|
echo "MongoDB -> FerretDB migration already finished; disabling mongodb service."
|
|
snapctl set database=ferretdb 2>/dev/null || true
|
|
snapctl stop --disable ${SNAP_NAME}.mongodb 2>/dev/null || true
|
|
exit 0
|
|
fi
|
|
|
|
# First-boot migration: upgrading from an old MongoDB-based WeKan snap. Before
|
|
# starting mongod normally, hand off to migration-control, which reads the
|
|
# existing MongoDB data (mongod 7 for MongoDB 7 data — including the arm64 snaps
|
|
# on newest WeKan; the bundled old mongod for amd64 6.09/MongoDB 3.2 data),
|
|
# migrates text data to FerretDB v1 (SQLite) and CollectionFS/Meteor-Files GridFS
|
|
# attachments+avatars to the filesystem (progress at ROOT_URL, resumable), then
|
|
# switches the snap to database=ferretdb. It is idempotent and only switches on
|
|
# success; if not pending it returns non-zero and mongod starts as usual.
|
|
if bash "$SNAP/bin/migration-pending"; then
|
|
echo "MongoDB -> FerretDB migration pending; running migration-control..."
|
|
exec bash "$SNAP/bin/migration-control"
|
|
fi
|
|
|
|
# If the FerretDB backend is selected, MongoDB must not run (FerretDB binds the
|
|
# same port). Disable this service; ferretdb-control starts FerretDB instead.
|
|
if [ "ferretdb" == "${DATABASE}" ]; then
|
|
echo "database is set to ferretdb. Stopping mongodb service."
|
|
snapctl stop --disable ${SNAP_NAME}.mongodb 2>/dev/null || true
|
|
exit 0
|
|
fi
|
|
|
|
# The replica-set setup below uses db-eval (the bundled Node.js 24 + the `mongodb`
|
|
# driver), not mongosh, so there is no client telemetry to disable. mongod itself
|
|
# has no phone-home telemetry (Cloud Free Monitoring is opt-in and stays off).
|
|
DB_EVAL="$SNAP/bin/db-eval"
|
|
|
|
# make sure we have set minimum env variables for locale
|
|
if [ -z "${LANG}" ]; then
|
|
export LANG=en_US.UTF-8
|
|
fi
|
|
|
|
export LC_ALL=C
|
|
# If CPU does not support required instructions (AVX for x86_64, ARMv8.2-A for arm64),
|
|
# migratemongo/avx wrapper scripts transparently re-run MongoDB commands via QEMU.
|
|
# See https://github.com/wekan/migratemongo
|
|
export PATH=$SNAP/migratemongo/avx:$SNAP/usr/bin:$SNAP/bin:${PATH}
|
|
export LD_LIBRARY_PATH=$SNAP/lib:$SNAP/lib/x86_64-linux-gnu:$SNAP/lib/aarch64-linux-gnu:${LD_LIBRARY_PATH}
|
|
|
|
# If temporary settings log exists, delete it
|
|
if [ -f ${SNAP_COMMON}/settings.log ]; then
|
|
rm ${SNAP_COMMON}/settings.log
|
|
fi
|
|
|
|
# Set MongoDB log destination to snapcommon for log file detection
|
|
export MONGO_LOG_DESTINATION="snapcommon"
|
|
|
|
# Set MongoDB data directory
|
|
export MONGO_DATA_DIR="${SNAP_COMMON}"
|
|
|
|
# Create MongoDB data directory if it doesn't exist
|
|
if [ ! -d "$MONGO_DATA_DIR" ]; then
|
|
mkdir -p "$MONGO_DATA_DIR"
|
|
chmod 755 "$MONGO_DATA_DIR"
|
|
fi
|
|
|
|
# Set MongoDB log file path
|
|
export MONGO_LOG_FILE="${SNAP_COMMON}/mongodb.log"
|
|
|
|
# Build bind options from snap settings
|
|
BIND_OPTIONS=""
|
|
if [ "nill" != "${MONGODB_BIND_UNIX_SOCKET}" ] && [ -n "${MONGODB_BIND_UNIX_SOCKET}" ]; then
|
|
BIND_OPTIONS+=" --unixSocketPrefix ${MONGODB_BIND_UNIX_SOCKET}"
|
|
fi
|
|
if [ -n "${MONGODB_BIND_IP}" ]; then
|
|
BIND_OPTIONS+=" --bind_ip ${MONGODB_BIND_IP}"
|
|
else
|
|
BIND_OPTIONS+=" --bind_ip 127.0.0.1"
|
|
fi
|
|
if [ -n "${MONGODB_PORT}" ]; then
|
|
BIND_OPTIONS+=" --port ${MONGODB_PORT}"
|
|
fi
|
|
|
|
# Debug: Show what settings we're using
|
|
echo "MongoDB settings:"
|
|
echo " MONGODB_BIND_IP: ${MONGODB_BIND_IP:-127.0.0.1}"
|
|
echo " MONGODB_PORT: ${MONGODB_PORT}"
|
|
echo " MONGODB_BIND_UNIX_SOCKET: ${MONGODB_BIND_UNIX_SOCKET:-not set}"
|
|
echo " BIND_OPTIONS: ${BIND_OPTIONS}"
|
|
|
|
# WiredTiger cache size, RAM-aware. This was previously HARDCODED to 32 GB, which on any
|
|
# box with less than ~34 GB RAM made mongod get OOM-killed shortly after startup: the
|
|
# temporary mongod (used to initialise the replica set) died before it could be reached,
|
|
# so the replica set was NEVER initiated, the final mongod ran with --replSet rs0 but no
|
|
# config, no PRIMARY was ever elected, and WeKan looped "MongoDB not ready yet, retrying
|
|
# in 5 seconds..." forever (issue #6454). Compute a safe value from MemTotal instead:
|
|
# ~50% of (RAM - 1 GB) - the same formula mongod uses for its own default - never above
|
|
# RAM, capped at 32 GB (the old value, kept as the ceiling for big servers), min 1 GB.
|
|
# Override with the MONGODB_WIREDTIGER_CACHE_GB env var if you need a specific size.
|
|
if [ -n "${MONGODB_WIREDTIGER_CACHE_GB}" ]; then
|
|
WT_CACHE_GB="${MONGODB_WIREDTIGER_CACHE_GB}"
|
|
else
|
|
_mem_kb=$(awk '/^MemTotal:/{print $2}' /proc/meminfo 2>/dev/null)
|
|
[ -z "${_mem_kb}" ] && _mem_kb=2097152 # assume 2 GB if MemTotal is unreadable
|
|
_mem_gb=$(( _mem_kb / 1024 / 1024 ))
|
|
WT_CACHE_GB=$(( (_mem_gb - 1) / 2 ))
|
|
[ "${WT_CACHE_GB}" -lt 1 ] && WT_CACHE_GB=1
|
|
[ "${WT_CACHE_GB}" -gt 32 ] && WT_CACHE_GB=32
|
|
fi
|
|
echo " WiredTiger cache: ${WT_CACHE_GB} GB (RAM-aware; formerly hardcoded 32 GB - see #6454)"
|
|
|
|
# Check if MongoDB is already running
|
|
check_mongodb_running() {
|
|
local port="${MONGODB_PORT}"
|
|
|
|
# Check if MongoDB is already running on the configured port
|
|
if netstat -tuln 2>/dev/null | grep -q ":${port} "; then
|
|
echo "MongoDB is already running on port ${port}"
|
|
return 0
|
|
fi
|
|
|
|
# Alternative check using lsof if netstat is not available
|
|
if command -v lsof >/dev/null 2>&1; then
|
|
if lsof -i ":${port}" >/dev/null 2>&1; then
|
|
echo "MongoDB is already running on port ${port} (detected via lsof)"
|
|
return 0
|
|
fi
|
|
fi
|
|
|
|
# Check if there's a PID file and the process is still running
|
|
if [ -f "${SNAP_COMMON}/mongodb.pid" ]; then
|
|
local pid=$(cat "${SNAP_COMMON}/mongodb.pid" 2>/dev/null)
|
|
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
|
|
echo "MongoDB is already running (PID: $pid)"
|
|
return 0
|
|
else
|
|
# Remove stale PID file
|
|
rm -f "${SNAP_COMMON}/mongodb.pid"
|
|
fi
|
|
fi
|
|
|
|
return 1
|
|
}
|
|
|
|
# Cleanup function to remove PID file on exit
|
|
cleanup() {
|
|
rm -f "${SNAP_COMMON}/mongodb.pid"
|
|
}
|
|
trap cleanup EXIT
|
|
|
|
# Check if MongoDB is already running
|
|
if check_mongodb_running; then
|
|
echo "MongoDB is already running. Exiting to prevent multiple instances."
|
|
exit 0
|
|
fi
|
|
|
|
# Start MongoDB 7.x server
|
|
echo "Starting MongoDB 7.x server..."
|
|
# Create PID file
|
|
echo $$ > "${SNAP_COMMON}/mongodb.pid"
|
|
|
|
# Determine the expected replica set member host:port.
|
|
# Priority: port extracted from MONGO_URL if it points to localhost,
|
|
# otherwise the mongodb-port snap config (MONGODB_PORT).
|
|
INIT_MARKER="${SNAP_COMMON}/.replica_set_initialized"
|
|
_RS_PORT="${MONGODB_PORT:-27019}"
|
|
if [ -n "$MONGO_URL" ]; then
|
|
_url_port=$(echo "$MONGO_URL" | sed -nE 's#mongodb://([^@]+@)?(127\.0\.0\.1|localhost):([0-9]+)/.*#\3#p')
|
|
[ -n "$_url_port" ] && _RS_PORT="$_url_port"
|
|
fi
|
|
RS_MEMBER_HOST="127.0.0.1:${_RS_PORT}"
|
|
echo "Replica set member host: ${RS_MEMBER_HOST}"
|
|
|
|
# Always start MongoDB temporarily in background to verify/fix replica set
|
|
# config before the final foreground start. This catches first-run,
|
|
# port changes, and corrupt/missing replica set state.
|
|
# Use --pidfilepath so we can kill exactly this process without touching
|
|
# MongoDB instances from other parallel snap installs.
|
|
TEMP_PID_FILE="${SNAP_COMMON}/mongod-temp.pid"
|
|
echo "Starting MongoDB temporarily to check replica set configuration..."
|
|
$SNAP/bin/mongod \
|
|
--dbpath="$MONGO_DATA_DIR" \
|
|
--logpath="$MONGO_LOG_FILE" \
|
|
--logappend $BIND_OPTIONS \
|
|
--storageEngine wiredTiger --wiredTigerCacheSizeGB "${WT_CACHE_GB}" \
|
|
--timeZoneInfo /usr/share/zoneinfo \
|
|
--setParameter logicalSessionRefreshMillis=900000 \
|
|
--setParameter localLogicalSessionTimeoutMinutes=45 \
|
|
--replSet rs0 --oplogSize 20480 --fork \
|
|
--pidfilepath="$TEMP_PID_FILE"
|
|
|
|
# Wait for MongoDB to accept connections (up to 30 s). db-eval uses the bundled
|
|
# Node.js + `mongodb` driver; directConnection reaches this node before the
|
|
# replica set is initiated.
|
|
echo "Waiting for MongoDB to accept connections on port ${_RS_PORT}..."
|
|
_RS_URL="mongodb://127.0.0.1:${_RS_PORT}/?directConnection=true"
|
|
_wait=0
|
|
until "$DB_EVAL" ping "$_RS_URL" >/dev/null 2>&1; do
|
|
_wait=$((_wait + 1))
|
|
# 90 s, not 30 s: a large production database can need more than 30 s to replay its
|
|
# WiredTiger journal on first open. If we gave up at 30 s we would skip the replica-set
|
|
# initialise/reconfig below, and the final mongod would then run with no elected PRIMARY
|
|
# — WeKan would loop "MongoDB not ready yet, retrying..." forever (#6454).
|
|
if [ $_wait -ge 90 ]; then
|
|
echo "WARNING: MongoDB did not respond within 90 seconds"
|
|
break
|
|
fi
|
|
sleep 1
|
|
done
|
|
|
|
# Check replica set status and current member host (prints OK:<host> or ERROR:<msg>)
|
|
_rs_check=$("$DB_EVAL" rs-conf-host "$_RS_URL" 2>/dev/null)
|
|
echo "Replica set check: ${_rs_check}"
|
|
|
|
if echo "$_rs_check" | grep -q "^OK:"; then
|
|
_current_member=$(echo "$_rs_check" | sed 's/^OK://')
|
|
if [ "$_current_member" != "$RS_MEMBER_HOST" ]; then
|
|
echo "Member host mismatch: current='${_current_member}', expected='${RS_MEMBER_HOST}'. Reconfiguring..."
|
|
"$DB_EVAL" rs-reconfig "$_RS_URL" "$RS_MEMBER_HOST" 2>/dev/null || true
|
|
else
|
|
echo "Replica set config is valid."
|
|
fi
|
|
else
|
|
# Not initialized or config is invalid — try initiate first, then reconfig
|
|
echo "Replica set not ready. Initializing with member: ${RS_MEMBER_HOST}..."
|
|
"$DB_EVAL" rs-initiate "$_RS_URL" "$RS_MEMBER_HOST" 2>/dev/null \
|
|
|| "$DB_EVAL" rs-reconfig "$_RS_URL" "$RS_MEMBER_HOST" 2>/dev/null || true
|
|
fi
|
|
|
|
# Ensure init marker exists regardless of path taken above
|
|
[ -f "$INIT_MARKER" ] || touch "$INIT_MARKER"
|
|
|
|
# Shut down the temporary MongoDB process using its PID file so we only
|
|
# kill this instance and never another parallel snap's mongod.
|
|
echo "Stopping temporary MongoDB process..."
|
|
"$DB_EVAL" shutdown "$_RS_URL" 2>/dev/null || true
|
|
sleep 2
|
|
if [ -f "$TEMP_PID_FILE" ]; then
|
|
kill "$(cat "$TEMP_PID_FILE")" 2>/dev/null || true
|
|
rm -f "$TEMP_PID_FILE"
|
|
fi
|
|
sleep 1
|
|
|
|
# Final start of MongoDB in foreground (this becomes the main process)
|
|
exec $SNAP/bin/mongod \
|
|
--dbpath="$MONGO_DATA_DIR" \
|
|
--logpath="$MONGO_LOG_FILE" \
|
|
--logappend $BIND_OPTIONS \
|
|
--storageEngine wiredTiger --wiredTigerCacheSizeGB "${WT_CACHE_GB}" \
|
|
--timeZoneInfo /usr/share/zoneinfo \
|
|
--setParameter logicalSessionRefreshMillis=900000 \
|
|
--setParameter localLogicalSessionTimeoutMinutes=45 \
|
|
--replSet rs0 --oplogSize 20480
|