From 87a676e4d538a1533964bfe034f88782ca20cbfb Mon Sep 17 00:00:00 2001 From: Ben Abrams Date: Thu, 14 Jun 2018 18:41:02 -0700 Subject: [PATCH] Do not set vm.max_map_count when unnecessary (#31285) This commit modifies the Sys V init startup scripts to only modify vm.max_map_count if needed. In this case, needed means that the current value is less than our default value of 262144 maps. --- .../packages/src/deb/init.d/elasticsearch | 2 +- .../packages/src/rpm/init.d/elasticsearch | 2 +- .../packaging/tests/70_sysv_initd.bats | 28 +++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/distribution/packages/src/deb/init.d/elasticsearch b/distribution/packages/src/deb/init.d/elasticsearch index 6d3efd99ca6..21ac80a9c22 100755 --- a/distribution/packages/src/deb/init.d/elasticsearch +++ b/distribution/packages/src/deb/init.d/elasticsearch @@ -122,7 +122,7 @@ case "$1" in ulimit -l $MAX_LOCKED_MEMORY fi - if [ -n "$MAX_MAP_COUNT" -a -f /proc/sys/vm/max_map_count ]; then + if [ -n "$MAX_MAP_COUNT" -a -f /proc/sys/vm/max_map_count -a "$MAX_MAP_COUNT" -ge $(cat /proc/sys/vm/max_map_count) ]; then sysctl -q -w vm.max_map_count=$MAX_MAP_COUNT fi diff --git a/distribution/packages/src/rpm/init.d/elasticsearch b/distribution/packages/src/rpm/init.d/elasticsearch index 01dc4e691c0..d0fb4f759d1 100644 --- a/distribution/packages/src/rpm/init.d/elasticsearch +++ b/distribution/packages/src/rpm/init.d/elasticsearch @@ -90,7 +90,7 @@ start() { if [ -n "$MAX_LOCKED_MEMORY" ]; then ulimit -l $MAX_LOCKED_MEMORY fi - if [ -n "$MAX_MAP_COUNT" -a -f /proc/sys/vm/max_map_count ]; then + if [ -n "$MAX_MAP_COUNT" -a -f /proc/sys/vm/max_map_count -a "$MAX_MAP_COUNT" -ge $(cat /proc/sys/vm/max_map_count) ]; then sysctl -q -w vm.max_map_count=$MAX_MAP_COUNT fi diff --git a/qa/vagrant/src/test/resources/packaging/tests/70_sysv_initd.bats b/qa/vagrant/src/test/resources/packaging/tests/70_sysv_initd.bats index 026b46e21bc..51409140933 100644 --- a/qa/vagrant/src/test/resources/packaging/tests/70_sysv_initd.bats +++ b/qa/vagrant/src/test/resources/packaging/tests/70_sysv_initd.bats @@ -163,3 +163,31 @@ setup() { assert_file_exist /var/log/elasticsearch/gc.log.0.current stop_elasticsearch_service } + +# Ensures that if $MAX_MAP_COUNT is less than the set value on the OS +# it will be updated +@test "[INIT.D] sysctl is run when the value set is too small" { + # intentionally a ridiculously low number + sysctl -q -w vm.max_map_count=100 + start_elasticsearch_service + max_map_count=$(sysctl -n vm.max_map_count) + stop_elasticsearch_service + + [ $max_map_count = 262144 ] + +} + +# Ensures that if $MAX_MAP_COUNT is greater than the set vaule on the OS +# we do not attempt to update it this should cover equality as well as I think +# we can trust that equality operators work as intended. +@test "[INIT.D] sysctl is not run when it already has a larger or equal value set" { + # intentionally set to the default +1 + sysctl -q -w vm.max_map_count=262145 + start_elasticsearch_service + max_map_count=$(sysctl -n vm.max_map_count) + stop_elasticsearch_service + + # default value +1 + [ $max_map_count = 262145 ] + +}