From 2b2bf71efcbcac3d0166ee1cd7f96b55e4818be3 Mon Sep 17 00:00:00 2001 From: Pil0tXia <41445332+Pil0tXia@users.noreply.github.com> Date: Tue, 19 Dec 2023 15:26:29 +0800 Subject: [PATCH] [ISSUE #4670] Support Java 11 StartUp and Docker deployment (#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 --- .github/workflows/build.yaml | 2 +- docker/{Dockerfile => Dockerfile_jdk11} | 9 ++- docker/Dockerfile_jdk8 | 43 ++++++++++++++ eventmesh-runtime/bin/start.sh | 78 ++++++++++++++++--------- 4 files changed, 97 insertions(+), 35 deletions(-) rename docker/{Dockerfile => Dockerfile_jdk11} (89%) create mode 100644 docker/Dockerfile_jdk8 diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index a5a81f6dbb..68ca1799cc 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -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: ./ diff --git a/docker/Dockerfile b/docker/Dockerfile_jdk11 similarity index 89% rename from docker/Dockerfile rename to docker/Dockerfile_jdk11 index efe587a2fb..7245e26046 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile_jdk11 @@ -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 diff --git a/docker/Dockerfile_jdk8 b/docker/Dockerfile_jdk8 new file mode 100644 index 0000000000..3e98459da7 --- /dev/null +++ b/docker/Dockerfile_jdk8 @@ -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"] diff --git a/eventmesh-runtime/bin/start.sh b/eventmesh-runtime/bin/start.sh index 1e2099db96..96fac20989 100644 --- a/eventmesh-runtime/bin/start.sh +++ b/eventmesh-runtime/bin/start.sh @@ -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` @@ -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="" @@ -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 @@ -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" @@ -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