Skip to content

Adjust "vm_memory_high_watermark" code to be more forgiving/strict about bad data (such as a memory limit of zero) #149

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

Merged
merged 1 commit into from
Apr 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions 3.6/alpine/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -253,17 +253,23 @@ if [ "$1" = 'rabbitmq-server' ] && [ "$shouldWriteConfig" ]; then
)

# determine whether to set "vm_memory_high_watermark" (based on cgroups)
if [ -r /sys/fs/cgroup/memory/memory.limit_in_bytes ] && [ -r /proc/meminfo ]; then
memLimitB="$(< /sys/fs/cgroup/memory/memory.limit_in_bytes)"
memLimitKb="$(( memLimitB / 1024 ))"

memTotalKb=
if [ -r /proc/meminfo ]; then
memTotalKb="$(awk -F ':? +' '$1 == "MemTotal" { print $2; exit }' /proc/meminfo)"

if [ "$memLimitKb" -gt "$memTotalKb" ]; then
memLimitB=
memLimitKb=
fi

fi
memLimitB=
if [ -r /sys/fs/cgroup/memory/memory.limit_in_bytes ]; then
# "18446744073709551615" is a valid value for "memory.limit_in_bytes", which is too big for Bash math to handle
# "$(( 18446744073709551615 / 1024 ))" = 0; "$(( 18446744073709551615 * 40 / 100 ))" = 0
memLimitB="$(awk -v totKb="$memTotalKb" '{
limB = $0;
limKb = limB / 1024;
if (!totKb || limKb < totKb) {
printf "%.0f\n", limB;
}
}' /sys/fs/cgroup/memory/memory.limit_in_bytes)"
fi
if [ -n "$memTotalKb" ] || [ -n "$memLimitB" ]; then
# https://github.com/docker-library/rabbitmq/pull/105#issuecomment-242165822
vmMemoryHighWatermark=
if [ "${RABBITMQ_VM_MEMORY_HIGH_WATERMARK:-}" ]; then
Expand Down Expand Up @@ -302,18 +308,16 @@ if [ "$1" = 'rabbitmq-server' ] && [ "$shouldWriteConfig" ]; then
)"
elif [ -n "$memLimitB" ]; then
# if there is a cgroup limit, default to 40% of _that_ (as recommended by upstream)
vmMemoryHighWatermark="{ absolute, $(( $memLimitB * 40 / 100 )) }"
vmMemoryHighWatermark="{ absolute, $(awk -v lim="$memLimitB" 'BEGIN { printf "%.0f\n", lim * 0.4; exit }') }"
# otherwise let the default behavior win (40% of the total available)
fi
if [ "$vmMemoryHighWatermark" ]; then
# https://www.rabbitmq.com/memory.html#memsup-usage
rabbitConfig+=( "{ vm_memory_high_watermark, $vmMemoryHighWatermark }" )
fi
elif [ "${RABBITMQ_VM_MEMORY_HIGH_WATERMARK:-}" ]; then
echo >&2 'warning: RABBITMQ_VM_MEMORY_HIGH_WATERMARK was specified, but one of the following is not readable:'
echo >&2 ' - /sys/fs/cgroup/memory/memory.limit_in_bytes'
echo >&2 ' - /proc/meminfo'
echo >&2 '(so "vm_memory_high_watermark" will not be set)'
echo >&2 'warning: RABBITMQ_VM_MEMORY_HIGH_WATERMARK was specified, but current system memory or cgroup memory limit cannot be determined'
echo >&2 ' (so "vm_memory_high_watermark" will not be set)'
fi

if [ "$haveSslConfig" ]; then
Expand Down
34 changes: 19 additions & 15 deletions 3.6/debian/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -253,17 +253,23 @@ if [ "$1" = 'rabbitmq-server' ] && [ "$shouldWriteConfig" ]; then
)

# determine whether to set "vm_memory_high_watermark" (based on cgroups)
if [ -r /sys/fs/cgroup/memory/memory.limit_in_bytes ] && [ -r /proc/meminfo ]; then
memLimitB="$(< /sys/fs/cgroup/memory/memory.limit_in_bytes)"
memLimitKb="$(( memLimitB / 1024 ))"

memTotalKb=
if [ -r /proc/meminfo ]; then
memTotalKb="$(awk -F ':? +' '$1 == "MemTotal" { print $2; exit }' /proc/meminfo)"

if [ "$memLimitKb" -gt "$memTotalKb" ]; then
memLimitB=
memLimitKb=
fi

fi
memLimitB=
if [ -r /sys/fs/cgroup/memory/memory.limit_in_bytes ]; then
# "18446744073709551615" is a valid value for "memory.limit_in_bytes", which is too big for Bash math to handle
# "$(( 18446744073709551615 / 1024 ))" = 0; "$(( 18446744073709551615 * 40 / 100 ))" = 0
memLimitB="$(awk -v totKb="$memTotalKb" '{
limB = $0;
limKb = limB / 1024;
if (!totKb || limKb < totKb) {
printf "%.0f\n", limB;
}
}' /sys/fs/cgroup/memory/memory.limit_in_bytes)"
fi
if [ -n "$memTotalKb" ] || [ -n "$memLimitB" ]; then
# https://github.com/docker-library/rabbitmq/pull/105#issuecomment-242165822
vmMemoryHighWatermark=
if [ "${RABBITMQ_VM_MEMORY_HIGH_WATERMARK:-}" ]; then
Expand Down Expand Up @@ -302,18 +308,16 @@ if [ "$1" = 'rabbitmq-server' ] && [ "$shouldWriteConfig" ]; then
)"
elif [ -n "$memLimitB" ]; then
# if there is a cgroup limit, default to 40% of _that_ (as recommended by upstream)
vmMemoryHighWatermark="{ absolute, $(( $memLimitB * 40 / 100 )) }"
vmMemoryHighWatermark="{ absolute, $(awk -v lim="$memLimitB" 'BEGIN { printf "%.0f\n", lim * 0.4; exit }') }"
# otherwise let the default behavior win (40% of the total available)
fi
if [ "$vmMemoryHighWatermark" ]; then
# https://www.rabbitmq.com/memory.html#memsup-usage
rabbitConfig+=( "{ vm_memory_high_watermark, $vmMemoryHighWatermark }" )
fi
elif [ "${RABBITMQ_VM_MEMORY_HIGH_WATERMARK:-}" ]; then
echo >&2 'warning: RABBITMQ_VM_MEMORY_HIGH_WATERMARK was specified, but one of the following is not readable:'
echo >&2 ' - /sys/fs/cgroup/memory/memory.limit_in_bytes'
echo >&2 ' - /proc/meminfo'
echo >&2 '(so "vm_memory_high_watermark" will not be set)'
echo >&2 'warning: RABBITMQ_VM_MEMORY_HIGH_WATERMARK was specified, but current system memory or cgroup memory limit cannot be determined'
echo >&2 ' (so "vm_memory_high_watermark" will not be set)'
fi

if [ "$haveSslConfig" ]; then
Expand Down