apache / rocketmq

Apache RocketMQ is a cloud native messaging and streaming platform, making it simple to build event-driven applications.
https://rocketmq.apache.org/
Apache License 2.0
21.25k stars 11.7k forks source link

[Feature] Can JVM parameters be passed in when starting the broker? #8697

Open asjfoajs opened 2 months ago

asjfoajs commented 2 months ago

Is Your Feature Request Related to a Problem?

At present, when starting the broker, passing JVM parameters is not supported. For example, setting the JVM heap size is directly set in the runbroker. sh script through JAVA-POT="${JAVA-POT} - server - Xms8g - Xmx8g", which is not suitable for containerized deployment. When using containerized deployment, you want to pass JVM parameters yourself.

Describe the Solution You'd Like

  1. Add DockerFile to receive JVM parameters
    ENV JVM_OPT_XMX="4g"
    ENV JVM_OPT_XMS="4g"
    ENV JVM_OPT_XMN="2g"

2.Modify runbroker.sh

if [ "$JVM_OPT_XMX" = "" ]; then
   export JVM_OPT_XMX="4g"
fi
if [ "$JVM_OPT_XMS" = "" ]; then
    export JVM_OPT_XMS="4g"
fi

if [ "$JVM_OPT_XMN" = "" ]; then
    export JVM_OPT_XMN="2g"
fi

JAVA_OPT="${JAVA_OPT} -server -Xms${JVM_OPT_XMX} -Xmx${JVM_OPT_XMS} -Xmn${JVM_OPT_XMN}"

Describe Alternatives You've Considered

Since it is not possible to impose restrictions on the image, how did RocketMQ operator do it? View the images/broker/apine directory, which is the memory size obtained through cgroup, then generate a script and rename it to runbroker.sh.

calculate_heap_sizes()
{
    case "`uname`" in
        Linux)
            system_memory_in_mb=`free -m| sed -n '2p' | awk '{print $2}'`
            if [ -f /sys/fs/cgroup/memory/memory.limit_in_bytes ]; then
                system_memory_in_mb_in_docker=$(($(cat /sys/fs/cgroup/memory/memory.limit_in_bytes)/1024/1024))
            elif [ -f /sys/fs/cgroup/memory.max ]; then
                system_memory_in_mb_in_docker=$(($(cat /sys/fs/cgroup/memory.max)/1024/1024))
            else
                error_exit "Can not get memory, please check cgroup"
            fi
            if [ $system_memory_in_mb_in_docker -lt $system_memory_in_mb ];then
              system_memory_in_mb=$system_memory_in_mb_in_docker
            fi

            system_cpu_cores=`egrep -c 'processor([[:space:]]+):.*' /proc/cpuinfo`
            if [ -f /sys/fs/cgroup/cpu/cpu.cfs_quota_us ]; then
                system_cpu_cores_in_docker=$(($(cat /sys/fs/cgroup/cpu/cpu.cfs_quota_us)/$(cat /sys/fs/cgroup/cpu/cpu.cfs_period_us)))
            elif [ -f /sys/fs/cgroup/cpu.max ]; then
                QUOTA=$(cut -d ' ' -f 1 /sys/fs/cgroup/cpu.max)
                PERIOD=$(cut -d ' ' -f 2 /sys/fs/cgroup/cpu.max)
                if [ "$QUOTA" == "max" ]; then # no limit, see https://docs.kernel.org/admin-guide/cgroup-v2.html#cgroup-v2-cpu
                  system_cpu_cores_in_docker=$system_cpu_cores
                else
                  system_cpu_cores_in_docker=$(($QUOTA/$PERIOD))
                fi
            else
                error_exit "Can not get cpu, please check cgroup"
            fi
            if [ $system_cpu_cores_in_docker -lt $system_cpu_cores -a $system_cpu_cores_in_docker -ne 0 ];then
              system_cpu_cores=$system_cpu_cores_in_docker
            fi
        ;;
        FreeBSD)
            system_memory_in_bytes=`sysctl hw.physmem | awk '{print $2}'`
            system_memory_in_mb=`expr $system_memory_in_bytes / 1024 / 1024`
            system_cpu_cores=`sysctl hw.ncpu | awk '{print $2}'`
        ;;
        SunOS)
            system_memory_in_mb=`prtconf | awk '/Memory size:/ {print $3}'`
            system_cpu_cores=`psrinfo | wc -l`
        ;;
        Darwin)
            system_memory_in_bytes=`sysctl hw.memsize | awk '{print $2}'`
            system_memory_in_mb=`expr $system_memory_in_bytes / 1024 / 1024`
            system_cpu_cores=`sysctl hw.ncpu | awk '{print $2}'`
        ;;
        *)
            # assume reasonable defaults for e.g. a modern desktop or
            # cheap server
            system_memory_in_mb="2048"
            system_cpu_cores="2"
        ;;
    esac

    # some systems like the raspberry pi don't report cores, use at least 1
    if [ "$system_cpu_cores" -lt "1" ]
    then
        system_cpu_cores="1"
    fi

    # set max heap size based on the following
    # max(min(1/2 ram, 1024MB), min(1/4 ram, 8GB))
    # calculate 1/2 ram and cap to 1024MB
    # calculate 1/4 ram and cap to 8192MB
    # pick the max
    half_system_memory_in_mb=`expr $system_memory_in_mb / 2`
    quarter_system_memory_in_mb=`expr $half_system_memory_in_mb / 2`
    if [ "$half_system_memory_in_mb" -gt "1024" ]
    then
        half_system_memory_in_mb="1024"
    fi
    if [ "$quarter_system_memory_in_mb" -gt "8192" ]
    then
        quarter_system_memory_in_mb="8192"
    fi
    if [ "$half_system_memory_in_mb" -gt "$quarter_system_memory_in_mb" ]
    then
        max_heap_size_in_mb="$half_system_memory_in_mb"
    else
        max_heap_size_in_mb="$quarter_system_memory_in_mb"
    fi
    MAX_HEAP_SIZE="${max_heap_size_in_mb}M"

    # Young gen: min(max_sensible_per_modern_cpu_core * num_cores, 1/4 * heap size)
    max_sensible_yg_per_core_in_mb="100"
    max_sensible_yg_in_mb=`expr $max_sensible_yg_per_core_in_mb "*" $system_cpu_cores`

    desired_yg_in_mb=`expr $max_heap_size_in_mb / 4`

    if [ "$desired_yg_in_mb" -gt "$max_sensible_yg_in_mb" ]
    then
        HEAP_NEWSIZE="${max_sensible_yg_in_mb}M"
    else
        HEAP_NEWSIZE="${desired_yg_in_mb}M"
    fi
}
# Dynamically calculate parameters, for reference.
Xms=$MAX_HEAP_SIZE
Xmx=$MAX_HEAP_SIZE
Xmn=$HEAP_NEWSIZE
# Set for `JAVA_OPT`.
JAVA_OPT="${JAVA_OPT} -server -Xms${Xms} -Xmx${Xmx} -Xmn${Xmn}"
JAVA_OPT="${JAVA_OPT} -XX:+UseConcMarkSweepGC -XX:+UseCMSCompactAtFullCollection -XX:CMSInitiatingOccupancyFraction=70 -XX:+CMSParallelRemarkEnabled -XX:SoftRefLRUPolicyMSPerMB=0 -XX:+CMSClassUnloadingEnabled -XX:SurvivorRatio=8  -XX:-UseParNewGC"
JAVA_OPT="${JAVA_OPT} -verbose:gc -Xloggc:/dev/shm/rmq_srv_gc.log -XX:+PrintGCDetails"
JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow"
JAVA_OPT="${JAVA_OPT}  -XX:-UseLargePages"
JAVA_OPT="${JAVA_OPT} -Djava.ext.dirs=${JAVA_HOME}/jre/lib/ext:${BASE_DIR}/lib"
#JAVA_OPT="${JAVA_OPT} -Xdebug -Xrunjdwp:transport=dt_socket,address=9555,server=y,suspend=n"
JAVA_OPT="${JAVA_OPT} ${JAVA_OPT_EXT}"
JAVA_OPT="${JAVA_OPT} -cp ${CLASSPATH}"

Additional Context

No response

asjfoajs commented 2 months ago

If possible, can I make some modifications