Skip to content

Commit

Permalink
[ISSUE apache#4670] Support Java 11 StartUp and Docker deployment (ap…
Browse files Browse the repository at this point in the history
…ache#4675)

* Switch jdk8 to jre11

* Locate Java executable, check version and apply distinguished vm options
https://docs.oracle.com/javase/9/tools/java.htm#GUID-BE93ABDC-999C-4CB5-A88B-1994AAAC74D5__CONVERTGCLOGGINGFLAGSTOXLOG-A5046BD1

* Streamline further

* minor: Adjust port seq

* minor: Streamline a little

* Redirect GC logs to log file instead of stdout

* Streamline EVENTMESH_LOG_HOME & XLOG_PARAM

* Use multi-stage compiled JRE8 env by default

* fix: get pid correctly in docker

* Generate pid file exactly under /bin

* exec builder_8 exactly after builder_11

* Use JDK instead of JRE

* Rename dockerfile
  • Loading branch information
Pil0tXia authored Dec 19, 2023
1 parent 03aa825 commit 2b2bf71
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ jobs:
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
file: ./docker/Dockerfile
file: ./docker/Dockerfile_jdk8
context: ./
9 changes: 4 additions & 5 deletions docker/Dockerfile → docker/Dockerfile_jdk11
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,19 @@
# specific language governing permissions and limitations
# under the License.
#
FROM openjdk:8-jdk as builder
FROM openjdk:11-jdk as builder
WORKDIR /build
COPY . .
RUN ./gradlew clean build jar dist --parallel --daemon
RUN ./gradlew installPlugin

FROM openjdk:8-jdk
RUN apt-get update && apt-get install -y locales
FROM openjdk:11-jdk
RUN apt-get update && apt-get install -y locales procps
RUN localedef -i en_US -f UTF-8 en_US.UTF-8 --quiet
WORKDIR /data/app/eventmesh
COPY --from=builder /build/dist ./

EXPOSE 10106
EXPOSE 10205 10105 10000
EXPOSE 10000 10105 10106 10205

ENV DOCKER true
ENV EVENTMESH_HOME /data/app/eventmesh
Expand Down
43 changes: 43 additions & 0 deletions docker/Dockerfile_jdk8
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
FROM openjdk:11-jdk as builder_11
WORKDIR /build
COPY . .
RUN ./gradlew clean generateGrammarSource --parallel --daemon

FROM openjdk:8-jdk as builder_8
WORKDIR /build
COPY --from=builder_11 /build ./
RUN ./gradlew clean build jar dist -x spotlessJava -x generateGrammarSource --parallel --daemon
RUN ./gradlew installPlugin

FROM openjdk:8-jdk
RUN apt-get update && apt-get install -y locales procps
RUN localedef -i en_US -f UTF-8 en_US.UTF-8 --quiet
WORKDIR /data/app/eventmesh
COPY --from=builder_8 /build/dist ./

EXPOSE 10000 10105 10106 10205

ENV DOCKER true
ENV EVENTMESH_HOME /data/app/eventmesh
ENV EVENTMESH_LOG_HOME /data/app/eventmesh/logs
ENV CONFPATH /data/app/eventmesh/conf

CMD ["bash", "bin/start.sh"]
78 changes: 49 additions & 29 deletions eventmesh-runtime/bin/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,30 @@
# Java Environment Setting
#===========================================================================================
set -e
#Server configuration may be inconsistent, add these configurations to avoid garbled code problems
# Server configuration may be inconsistent, add these configurations to avoid garbled code problems
export LANG=en_US.UTF-8
export LC_CTYPE=en_US.UTF-8
export LC_ALL=en_US.UTF-8

TMP_JAVA_HOME="/nemo/jdk1.8.0_152"
TMP_JAVA_HOME="/customize/your/java/home/here"

#detect operating system.
# Detect operating system.
OS=$(uname)

function is_java8 {
function is_java8_or_11 {
local _java="$1"
[[ -x "$_java" ]] || return 1
[[ "$("$_java" -version 2>&1)" =~ 'java version "1.8' || "$("$_java" -version 2>&1)" =~ 'openjdk version "1.8' ]] || return 2
[[ "$("$_java" -version 2>&1)" =~ 'java version "1.8' || "$("$_java" -version 2>&1)" =~ 'openjdk version "1.8' || "$("$_java" -version 2>&1)" =~ 'java version "11' || "$("$_java" -version 2>&1)" =~ 'openjdk version "11' ]] || return 2
return 0
}

#0(not running), 1(is running)
function extract_java_version {
local _java="$1"
local version=$("$_java" -version 2>&1 | awk -F '"' '/version/ {print $2}' | awk -F '.' '{if ($1 == 1 && $2 == 8) print "8"; else if ($1 == 11) print "11"; else print "unknown"}')
echo "$version"
}

# 0(not running), 1(is running)
#function is_proxyRunning {
# local _pid="$1"
# local pid=`ps ax | grep -i 'org.apache.eventmesh.runtime.boot.EventMeshStartup' |grep java | grep -v grep | awk '{print $1}'|grep $_pid`
Expand All @@ -55,7 +61,7 @@ function get_pid {
ppid=$(cat ${EVENTMESH_HOME}/bin/pid.file)
# If the process does not exist, it indicates that the previous process terminated abnormally.
if [ ! -d /proc/$ppid ]; then
# Remove the residual file
# Remove the residual file.
rm ${EVENTMESH_HOME}/bin/pid.file
echo -e "ERROR\t EventMesh process had already terminated unexpectedly before, please check log output."
ppid=""
Expand All @@ -68,32 +74,37 @@ function get_pid {
# Known problem: grep Java may not be able to accurately identify Java processes
ppid=$(/bin/ps -o user,pid,command | grep "java" | grep -i "org.apache.eventmesh.runtime.boot.EventMeshStartup" | grep -Ev "^root" |awk -F ' ' {'print $2'})
else
# It is required to identify the process as accurately as possible on Linux
ppid=$(ps -C java -o user,pid,command --cols 99999 | grep -w $EVENTMESH_HOME | grep -i "org.apache.eventmesh.runtime.boot.EventMeshStartup" | grep -Ev "^root" |awk -F ' ' {'print $2'})
if [ $DOCKER ]; then
# No need to exclude root user in Docker containers.
ppid=$(ps -C java -o user,pid,command --cols 99999 | grep -w $EVENTMESH_HOME | grep -i "org.apache.eventmesh.runtime.boot.EventMeshStartup" | awk -F ' ' {'print $2'})
else
# It is required to identify the process as accurately as possible on Linux.
ppid=$(ps -C java -o user,pid,command --cols 99999 | grep -w $EVENTMESH_HOME | grep -i "org.apache.eventmesh.runtime.boot.EventMeshStartup" | grep -Ev "^root" | awk -F ' ' {'print $2'})
fi
fi
fi
echo "$ppid";
}

#===========================================================================================
# Locate Java Executable
#===========================================================================================

if [[ -d "$TMP_JAVA_HOME" ]] && is_java8 "$TMP_JAVA_HOME/bin/java"; then
if [[ -d "$TMP_JAVA_HOME" ]] && is_java8_or_11 "$TMP_JAVA_HOME/bin/java"; then
JAVA="$TMP_JAVA_HOME/bin/java"
elif [[ -d "$JAVA_HOME" ]] && is_java8 "$JAVA_HOME/bin/java"; then
JAVA_VERSION=$(extract_java_version "$TMP_JAVA_HOME/bin/java")
elif [[ -d "$JAVA_HOME" ]] && is_java8_or_11 "$JAVA_HOME/bin/java"; then
JAVA="$JAVA_HOME/bin/java"
elif is_java8 "/nemo/jdk8/bin/java"; then
JAVA="/nemo/jdk8/bin/java";
elif is_java8 "/nemo/jdk1.8/bin/java"; then
JAVA="/nemo/jdk1.8/bin/java";
elif is_java8 "/nemo/jdk/bin/java"; then
JAVA="/nemo/jdk/bin/java";
elif is_java8 "$(which java)"; then
JAVA_VERSION=$(extract_java_version "$JAVA_HOME/bin/java")
elif is_java8_or_11 "$(which java)"; then
JAVA="$(which java)"
JAVA_VERSION=$(extract_java_version "$(which java)")
else
echo -e "ERROR\t Java 8 not found, operation abort."
echo -e "ERROR\t Java 8 or 11 not found, operation abort."
exit 9;
fi

echo "EventMesh use Java location: $JAVA"
echo "EventMesh using Java version: $JAVA_VERSION, path: $JAVA"

EVENTMESH_HOME=$(cd "$(dirname "$0")/.." && pwd)
export EVENTMESH_HOME
Expand Down Expand Up @@ -123,12 +134,23 @@ export JAVA_HOME
#elif [ $1 = "dev" ]; then JAVA_OPT="${JAVA_OPT} -server -Xms128M -Xmx256M -Xmn128m -XX:SurvivorRatio=4"
#fi

GC_LOG_FILE="${EVENTMESH_LOG_HOME}/eventmesh_gc_%p.log"

#JAVA_OPT="${JAVA_OPT} -server -Xms2048M -Xmx4096M -Xmn2048m -XX:SurvivorRatio=4"
JAVA_OPT=`cat ${EVENTMESH_HOME}/conf/server.env | grep APP_START_JVM_OPTION::: | awk -F ':::' {'print $2'}`
JAVA_OPT="${JAVA_OPT} -XX:+UseG1GC -XX:G1HeapRegionSize=16m -XX:G1ReservePercent=25 -XX:InitiatingHeapOccupancyPercent=30 -XX:SoftRefLRUPolicyMSPerMB=0 -XX:SurvivorRatio=8 -XX:MaxGCPauseMillis=50"
JAVA_OPT="${JAVA_OPT} -verbose:gc -Xloggc:${EVENTMESH_HOME}/logs/eventmesh_gc_%p.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCApplicationStoppedTime -XX:+PrintAdaptiveSizePolicy"
JAVA_OPT="${JAVA_OPT} -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${EVENTMESH_HOME}/logs -XX:ErrorFile=${EVENTMESH_HOME}/logs/hs_err_%p.log"
JAVA_OPT="${JAVA_OPT} -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=30m"
JAVA_OPT="${JAVA_OPT} -verbose:gc"
if [[ "$JAVA_VERSION" == "8" ]]; then
# Set JAVA_OPT for Java 8
JAVA_OPT="${JAVA_OPT} -Xloggc:${GC_LOG_FILE} -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=30m"
JAVA_OPT="${JAVA_OPT} -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCApplicationStoppedTime -XX:+PrintAdaptiveSizePolicy"
elif [[ "$JAVA_VERSION" == "11" ]]; then
# Set JAVA_OPT for Java 11
XLOG_PARAM="time,level,tags:filecount=5,filesize=30m"
JAVA_OPT="${JAVA_OPT} -Xlog:gc*:${GC_LOG_FILE}:${XLOG_PARAM}"
JAVA_OPT="${JAVA_OPT} -Xlog:safepoint:${GC_LOG_FILE}:${XLOG_PARAM} -Xlog:ergo*=debug:${GC_LOG_FILE}:${XLOG_PARAM}"
fi
JAVA_OPT="${JAVA_OPT} -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${EVENTMESH_LOG_HOME} -XX:ErrorFile=${EVENTMESH_LOG_HOME}/hs_err_%p.log"
JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow"
JAVA_OPT="${JAVA_OPT} -XX:+AlwaysPreTouch"
JAVA_OPT="${JAVA_OPT} -XX:MaxDirectMemorySize=8G"
Expand Down Expand Up @@ -159,22 +181,20 @@ if [[ $pid == "ERROR"* ]]; then
echo -e "${pid}"
exit 9
fi
if [ -n "$pid" ];then
if [ -n "$pid" ]; then
echo -e "ERROR\t The server is already running (pid=$pid), there is no need to execute start.sh again."
exit 9
fi

make_logs_dir

echo "Using JDK[$JAVA]" >> ${EVENTMESH_LOG_HOME}/eventmesh.out

echo "Using Java version: $JAVA_VERSION, path: $JAVA" >> ${EVENTMESH_LOG_HOME}/eventmesh.out

EVENTMESH_MAIN=org.apache.eventmesh.runtime.boot.EventMeshStartup
if [ $DOCKER ]
then
if [ $DOCKER ]; then
$JAVA $JAVA_OPT -classpath ${EVENTMESH_HOME}/conf:${EVENTMESH_HOME}/apps/*:${EVENTMESH_HOME}/lib/* $EVENTMESH_MAIN >> ${EVENTMESH_LOG_HOME}/eventmesh.out
else
$JAVA $JAVA_OPT -classpath ${EVENTMESH_HOME}/conf:${EVENTMESH_HOME}/apps/*:${EVENTMESH_HOME}/lib/* $EVENTMESH_MAIN >> ${EVENTMESH_LOG_HOME}/eventmesh.out 2>&1 &
echo $!>pid.file
echo $!>${EVENTMESH_HOME}/bin/pid.file
fi
exit 0

0 comments on commit 2b2bf71

Please sign in to comment.