|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +############################################################################### |
| 4 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 5 | +# or more contributor license agreements. See the NOTICE file |
| 6 | +# distributed with this work for additional information |
| 7 | +# regarding copyright ownership. The ASF licenses this file |
| 8 | +# to you under the Apache License, Version 2.0 (the |
| 9 | +# "License"); you may not use this file except in compliance |
| 10 | +# with the License. You may obtain a copy of the License at |
| 11 | +# |
| 12 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +# |
| 14 | +# Unless required by applicable law or agreed to in writing, software |
| 15 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | +# See the License for the specific language governing permissions and |
| 18 | +# limitations under the License. |
| 19 | +############################################################################### |
| 20 | + |
| 21 | +COMMAND_STANDALONE="standalone-job" |
| 22 | +COMMAND_HISTORY_SERVER="history-server" |
| 23 | + |
| 24 | +# If unspecified, the hostname of the container is taken as the JobManager address |
| 25 | +JOB_MANAGER_RPC_ADDRESS=${JOB_MANAGER_RPC_ADDRESS:-$(hostname -f)} |
| 26 | +CONF_FILE="${FLINK_HOME}/conf/flink-conf.yaml" |
| 27 | + |
| 28 | +drop_privs_cmd() { |
| 29 | + if [ $(id -u) != 0 ]; then |
| 30 | + # Don't need to drop privs if EUID != 0 |
| 31 | + return |
| 32 | + elif [ -x /sbin/su-exec ]; then |
| 33 | + # Alpine |
| 34 | + echo su-exec flink |
| 35 | + else |
| 36 | + # Others |
| 37 | + echo gosu flink |
| 38 | + fi |
| 39 | +} |
| 40 | + |
| 41 | +copy_plugins_if_required() { |
| 42 | + if [ -z "$ENABLE_BUILT_IN_PLUGINS" ]; then |
| 43 | + return 0 |
| 44 | + fi |
| 45 | + |
| 46 | + echo "Enabling required built-in plugins" |
| 47 | + for target_plugin in $(echo "$ENABLE_BUILT_IN_PLUGINS" | tr ';' ' '); do |
| 48 | + echo "Linking ${target_plugin} to plugin directory" |
| 49 | + plugin_name=${target_plugin%.jar} |
| 50 | + |
| 51 | + mkdir -p "${FLINK_HOME}/plugins/${plugin_name}" |
| 52 | + if [ ! -e "${FLINK_HOME}/opt/${target_plugin}" ]; then |
| 53 | + echo "Plugin ${target_plugin} does not exist. Exiting." |
| 54 | + exit 1 |
| 55 | + else |
| 56 | + ln -fs "${FLINK_HOME}/opt/${target_plugin}" "${FLINK_HOME}/plugins/${plugin_name}" |
| 57 | + echo "Successfully enabled ${target_plugin}" |
| 58 | + fi |
| 59 | + done |
| 60 | +} |
| 61 | + |
| 62 | +set_config_option() { |
| 63 | + local option=$1 |
| 64 | + local value=$2 |
| 65 | + |
| 66 | + # escape periods for usage in regular expressions |
| 67 | + local escaped_option=$(echo ${option} | sed -e "s/\./\\\./g") |
| 68 | + |
| 69 | + # either override an existing entry, or append a new one |
| 70 | + if grep -E "^${escaped_option}:.*" "${CONF_FILE}" > /dev/null; then |
| 71 | + sed -i -e "s/${escaped_option}:.*/$option: $value/g" "${CONF_FILE}" |
| 72 | + else |
| 73 | + echo "${option}: ${value}" >> "${CONF_FILE}" |
| 74 | + fi |
| 75 | +} |
| 76 | + |
| 77 | +prepare_configuration() { |
| 78 | + set_config_option jobmanager.rpc.address ${JOB_MANAGER_RPC_ADDRESS} |
| 79 | + set_config_option blob.server.port 6124 |
| 80 | + set_config_option query.server.port 6125 |
| 81 | + |
| 82 | + if [ -n "${TASK_MANAGER_NUMBER_OF_TASK_SLOTS}" ]; then |
| 83 | + set_config_option taskmanager.numberOfTaskSlots ${TASK_MANAGER_NUMBER_OF_TASK_SLOTS} |
| 84 | + fi |
| 85 | + |
| 86 | + if [ -n "${FLINK_PROPERTIES}" ]; then |
| 87 | + echo "${FLINK_PROPERTIES}" >> "${CONF_FILE}" |
| 88 | + fi |
| 89 | + envsubst < "${CONF_FILE}" > "${CONF_FILE}.tmp" && mv "${CONF_FILE}.tmp" "${CONF_FILE}" |
| 90 | +} |
| 91 | + |
| 92 | +maybe_enable_jemalloc() { |
| 93 | + if [ "${DISABLE_JEMALLOC:-false}" == "false" ]; then |
| 94 | + JEMALLOC_PATH="/usr/lib/$(uname -m)-linux-gnu/libjemalloc.so" |
| 95 | + JEMALLOC_FALLBACK="/usr/lib/x86_64-linux-gnu/libjemalloc.so" |
| 96 | + if [ -f "$JEMALLOC_PATH" ]; then |
| 97 | + export LD_PRELOAD=$LD_PRELOAD:$JEMALLOC_PATH |
| 98 | + elif [ -f "$JEMALLOC_FALLBACK" ]; then |
| 99 | + export LD_PRELOAD=$LD_PRELOAD:$JEMALLOC_FALLBACK |
| 100 | + else |
| 101 | + if [ "$JEMALLOC_PATH" = "$JEMALLOC_FALLBACK" ]; then |
| 102 | + MSG_PATH=$JEMALLOC_PATH |
| 103 | + else |
| 104 | + MSG_PATH="$JEMALLOC_PATH and $JEMALLOC_FALLBACK" |
| 105 | + fi |
| 106 | + echo "WARNING: attempted to load jemalloc from $MSG_PATH but the library couldn't be found. glibc will be used instead." |
| 107 | + fi |
| 108 | + fi |
| 109 | +} |
| 110 | + |
| 111 | +maybe_enable_jemalloc |
| 112 | + |
| 113 | +copy_plugins_if_required |
| 114 | + |
| 115 | +prepare_configuration |
| 116 | + |
| 117 | +args=("$@") |
| 118 | +if [ "$1" = "help" ]; then |
| 119 | + printf "Usage: $(basename "$0") (jobmanager|${COMMAND_STANDALONE}|taskmanager|${COMMAND_HISTORY_SERVER})\n" |
| 120 | + printf " Or $(basename "$0") help\n\n" |
| 121 | + printf "By default, Flink image adopts jemalloc as default memory allocator. This behavior can be disabled by setting the 'DISABLE_JEMALLOC' environment variable to 'true'.\n" |
| 122 | + exit 0 |
| 123 | +elif [ "$1" = "jobmanager" ]; then |
| 124 | + args=("${args[@]:1}") |
| 125 | + |
| 126 | + echo "Starting Job Manager" |
| 127 | + |
| 128 | + exec $(drop_privs_cmd) "$FLINK_HOME/bin/jobmanager.sh" start-foreground "${args[@]}" |
| 129 | +elif [ "$1" = ${COMMAND_STANDALONE} ]; then |
| 130 | + args=("${args[@]:1}") |
| 131 | + |
| 132 | + echo "Starting Job Manager" |
| 133 | + |
| 134 | + exec $(drop_privs_cmd) "$FLINK_HOME/bin/standalone-job.sh" start-foreground "${args[@]}" |
| 135 | +elif [ "$1" = ${COMMAND_HISTORY_SERVER} ]; then |
| 136 | + args=("${args[@]:1}") |
| 137 | + |
| 138 | + echo "Starting History Server" |
| 139 | + |
| 140 | + exec $(drop_privs_cmd) "$FLINK_HOME/bin/historyserver.sh" start-foreground "${args[@]}" |
| 141 | +elif [ "$1" = "taskmanager" ]; then |
| 142 | + args=("${args[@]:1}") |
| 143 | + |
| 144 | + echo "Starting Task Manager" |
| 145 | + |
| 146 | + exec $(drop_privs_cmd) "$FLINK_HOME/bin/taskmanager.sh" start-foreground "${args[@]}" |
| 147 | +fi |
| 148 | + |
| 149 | +args=("${args[@]}") |
| 150 | + |
| 151 | +# Running command in pass-through mode |
| 152 | +exec $(drop_privs_cmd) "${args[@]}" |
0 commit comments