Skip to content

Commit

Permalink
add JVM HEAP calculation + docker-compose installation to Sakuli UI D…
Browse files Browse the repository at this point in the history
…ocker Image #308, ConSol/sakuli-ui#14
  • Loading branch information
Tobias Schneck committed Mar 2, 2018
1 parent ef3ab06 commit 975585c
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 4 deletions.
9 changes: 5 additions & 4 deletions docker/Dockerfile.sakuli.ui
Expand Up @@ -25,22 +25,23 @@ ENV HTTP_PORT=8080 \
SAKULI_UI_DOCKER_USER_ID=1000 \
SAKULI_ROOT_DIR=/opt/sakuli-ui-root

# $INST_SCRIPTS is already set in FROM image
ADD ./sakuli-ui/src/common/install/ $INST_SCRIPTS/
RUN find $INST_SCRIPTS -name '*.sh' -exec chmod a+x {} +

### Intsall basics for jvm & docker
RUN $INST_SCRIPTS/add_jvm_options.sh
RUN $INST_SCRIPTS/docker.sh
#TODO TS add docker-compose
#TODO TS add JAVA HEAP options
RUN $INST_SCRIPTS/docker-compose.sh

### Install Sakuli UI
ARG SAKULI_VERSION=1.2.0-SNAPSHOT-308-sakuli-ui
RUN $INST_SCRIPTS/sakuli_ui.sh

#### configure startup
ADD ./sakuli-ui/src/common/scripts/ $STARTUPDIR
RUN $INST_SCRIPTS/set_user_permission.sh $STARTUPDIR $HOME

## Connection ports for Web Application:
## Connection port for Web Application:
EXPOSE $HTTP_PORT
USER $SAKULI_UI_USER_ID

Expand Down
9 changes: 9 additions & 0 deletions docker/sakuli-ui/src/common/install/add_jvm_options.sh
@@ -0,0 +1,9 @@
#!/usr/bin/env bash
### every exit != 0 fails the script
set -e
set -u

echo "Add JVM options"

# add source jvm_options.sh script to set correct java JVM options on startup
echo 'source $STARTUPDIR/jvm_options.sh' >> $HOME/.bashrc
12 changes: 12 additions & 0 deletions docker/sakuli-ui/src/common/install/docker-compose.sh
@@ -0,0 +1,12 @@
#!/usr/bin/env bash
set -e

version=1.19.0
echo "install latest docker-compose $version"
curl -L https://github.com/docker/compose/releases/download/$version/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

echo "------------ docker-compose version -----------"
docker-compose version

echo ".... done!"
2 changes: 2 additions & 0 deletions docker/sakuli-ui/src/common/install/docker.sh
Expand Up @@ -7,3 +7,5 @@ echo "Install Docker CLI from Docker Inc. repositories."
curl -sSL https://get.docker.com/ | sh \
&& useradd $SAKULI_UI_DOCKER_USER_ID -m -s /bin/bash \
&& usermod -aG docker $SAKULI_UI_DOCKER_USER_ID

apt-get clean -y
61 changes: 61 additions & 0 deletions docker/sakuli-ui/src/common/scripts/jvm_options.sh
@@ -0,0 +1,61 @@
#!/usr/bin/env bash
# Return reasonable JVM options to run inside a Docker container where memory and
# CPU can be limited with cgroups.
# https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/parallel.html
#
# The script can be used in a custom CMD or ENTRYPOINT
#
# source jvm_options

# Options:
# JVM_HEAP_RATIO=0.5 Ratio of heap size to available memory

# If Xmx is not set the JVM will use by default 1/4th (in most cases) of the host memory
# This can cause the Kernel to kill the container if the JVM memory grows over the cgroups limit
# because the JVM is not aware of that limit and doesn't invoke the GC
# Setting it by default to 0.5 times the memory limited by cgroups, customizable with JVM_HEAP_RATIO

echo "---------------------- Resolve _JAVA_OPTIONS -----------------------------------"
if [ -z "$JVM_HEAP_XMX" ] ; then
CGROUPS_MEM=$(cat /sys/fs/cgroup/memory/memory.limit_in_bytes)
echo CGROUPS_MEM: $CGROUPS_MEM bytes, $(($CGROUPS_MEM/1024/1024)) MB

MEMINFO_MEM=$(($(awk '/MemTotal/ {print $2}' /proc/meminfo)*1024))
echo MEMINFO_MEM: $MEMINFO_MEM bytes, $(($MEMINFO_MEM/1024/1024)) MB

MEM_USABLE=$(($MEMINFO_MEM>$CGROUPS_MEM?$CGROUPS_MEM:$MEMINFO_MEM))
# prevent float issue if $CGROUPS_MEM > bash max value, see https://github.com/ConSol/sakuli/issues/280
MEM_USABLE=$(($CGROUPS_MEM>$MEMINFO_MEM?$MEMINFO_MEM:$MEMINFO_MEM))
echo ""
echo MEM_USABLE: $MEM_USABLE bytes, $(($MEM_USABLE/1024/1024)) MB

JVM_HEAP_RATIO=${JVM_HEAP_RATIO:-0.5}
echo "JVM_HEAP_RATIO $JVM_HEAP_RATIO of MEM $(($MEM_USABLE/1024/1024)) MB"
JVM_HEAP_XMX=$(awk '{printf("%d",$1*$2/1024^2)}' <<<" ${MEM_USABLE} ${JVM_HEAP_RATIO} ")

JVM_HEAP_XMX_MAX=${JVM_HEAP_XMX_MAX:-300}
JVM_HEAP_XMX_MIN=${JVM_HEAP_XMX_MIN:-96}
if [ $JVM_HEAP_XMX -gt $JVM_HEAP_XMX_MAX ]; then
echo "calculated JVM_HEAP_XMX $JVM_HEAP_XMX MB is too high, choose JVM_HEAP_XMX_MAX: $JVM_HEAP_XMX_MAX MB"
JVM_HEAP_XMX=$JVM_HEAP_XMX_MAX
fi
if [ $JVM_HEAP_XMX -lt $JVM_HEAP_XMX_MIN ]; then
echo "calculated JVM_HEAP_XMX $JVM_HEAP_XMX MB is too small, try to use at the JVM_HEAP_XMX_MIN ($JVM_HEAP_XMX_MIN MB)"
if [ $MEM_USABLE -lt $JVM_HEAP_XMX_MIN ]; then
echo "Container need at least $JVM_HEAP_XMX_MIN MB memory for the JVM! Stop container starup!"
exit -1
fi
JVM_HEAP_XMX=$JVM_HEAP_XMX_MIN
fi

fi
echo ""
echo "JVM_HEAP_XMX: $JVM_HEAP_XMX MB"

# set correct java startup
export _JAVA_OPTIONS="-Duser.home=$HOME -Xmx${JVM_HEAP_XMX}m"
# add docker jvm flags, can maybe removed with JDK9, see https://github.com/ConSol/sakuli/issues/291
export _JAVA_OPTIONS="$_JAVA_OPTIONS -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap"

echo "set _JAVA_OPTIONS: $_JAVA_OPTIONS"
echo "--------------------------------------------------------------------------------"

0 comments on commit 975585c

Please sign in to comment.