|
| 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_DIR="${FLINK_HOME}/conf" |
| 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_options() { |
| 63 | + local config_parser_script="$FLINK_HOME/bin/config-parser-utils.sh" |
| 64 | + local config_dir="$FLINK_HOME/conf" |
| 65 | + local bin_dir="$FLINK_HOME/bin" |
| 66 | + local lib_dir="$FLINK_HOME/lib" |
| 67 | + |
| 68 | + local config_params=() |
| 69 | + |
| 70 | + while [ $# -gt 0 ]; do |
| 71 | + local key="$1" |
| 72 | + local value="$2" |
| 73 | + |
| 74 | + config_params+=("-D${key}=${value}") |
| 75 | + |
| 76 | + shift 2 |
| 77 | + done |
| 78 | + |
| 79 | + if [ "${#config_params[@]}" -gt 0 ]; then |
| 80 | + "${config_parser_script}" "${config_dir}" "${bin_dir}" "${lib_dir}" "${config_params[@]}" |
| 81 | + fi |
| 82 | +} |
| 83 | + |
| 84 | +prepare_configuration() { |
| 85 | + local config_options=() |
| 86 | + |
| 87 | + config_options+=("jobmanager.rpc.address" "${JOB_MANAGER_RPC_ADDRESS}") |
| 88 | + config_options+=("blob.server.port" "6124") |
| 89 | + config_options+=("query.server.port" "6125") |
| 90 | + |
| 91 | + if [ -n "${TASK_MANAGER_NUMBER_OF_TASK_SLOTS}" ]; then |
| 92 | + config_options+=("taskmanager.numberOfTaskSlots" "${TASK_MANAGER_NUMBER_OF_TASK_SLOTS}") |
| 93 | + fi |
| 94 | + |
| 95 | + if [ ${#config_options[@]} -ne 0 ]; then |
| 96 | + set_config_options "${config_options[@]}" |
| 97 | + fi |
| 98 | + |
| 99 | + if [ -n "${FLINK_PROPERTIES}" ]; then |
| 100 | + process_flink_properties "${FLINK_PROPERTIES}" |
| 101 | + fi |
| 102 | +} |
| 103 | + |
| 104 | +process_flink_properties() { |
| 105 | + local flink_properties_content=$1 |
| 106 | + local config_options=() |
| 107 | + |
| 108 | + local OLD_IFS="$IFS" |
| 109 | + IFS=$'\n' |
| 110 | + for prop in $flink_properties_content; do |
| 111 | + prop=$(echo $prop | tr -d '[:space:]') |
| 112 | + |
| 113 | + if [ -z "$prop" ]; then |
| 114 | + continue |
| 115 | + fi |
| 116 | + |
| 117 | + IFS=':' read -r key value <<< "$prop" |
| 118 | + |
| 119 | + value=$(echo $value | envsubst) |
| 120 | + |
| 121 | + config_options+=("$key" "$value") |
| 122 | + done |
| 123 | + IFS="$OLD_IFS" |
| 124 | + |
| 125 | + if [ ${#config_options[@]} -ne 0 ]; then |
| 126 | + set_config_options "${config_options[@]}" |
| 127 | + fi |
| 128 | +} |
| 129 | + |
| 130 | +maybe_enable_jemalloc() { |
| 131 | + if [ "${DISABLE_JEMALLOC:-false}" == "false" ]; then |
| 132 | + JEMALLOC_PATH="/usr/lib/$(uname -m)-linux-gnu/libjemalloc.so" |
| 133 | + JEMALLOC_FALLBACK="/usr/lib/x86_64-linux-gnu/libjemalloc.so" |
| 134 | + if [ -f "$JEMALLOC_PATH" ]; then |
| 135 | + export LD_PRELOAD=$LD_PRELOAD:$JEMALLOC_PATH |
| 136 | + elif [ -f "$JEMALLOC_FALLBACK" ]; then |
| 137 | + export LD_PRELOAD=$LD_PRELOAD:$JEMALLOC_FALLBACK |
| 138 | + else |
| 139 | + if [ "$JEMALLOC_PATH" = "$JEMALLOC_FALLBACK" ]; then |
| 140 | + MSG_PATH=$JEMALLOC_PATH |
| 141 | + else |
| 142 | + MSG_PATH="$JEMALLOC_PATH and $JEMALLOC_FALLBACK" |
| 143 | + fi |
| 144 | + echo "WARNING: attempted to load jemalloc from $MSG_PATH but the library couldn't be found. glibc will be used instead." |
| 145 | + fi |
| 146 | + fi |
| 147 | +} |
| 148 | + |
| 149 | +maybe_enable_jemalloc |
| 150 | + |
| 151 | +copy_plugins_if_required |
| 152 | + |
| 153 | +prepare_configuration |
| 154 | + |
| 155 | +args=("$@") |
| 156 | +if [ "$1" = "help" ]; then |
| 157 | + printf "Usage: $(basename "$0") (jobmanager|${COMMAND_STANDALONE}|taskmanager|${COMMAND_HISTORY_SERVER})\n" |
| 158 | + printf " Or $(basename "$0") help\n\n" |
| 159 | + 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" |
| 160 | + exit 0 |
| 161 | +elif [ "$1" = "jobmanager" ]; then |
| 162 | + args=("${args[@]:1}") |
| 163 | + |
| 164 | + echo "Starting Job Manager" |
| 165 | + |
| 166 | + exec $(drop_privs_cmd) "$FLINK_HOME/bin/jobmanager.sh" start-foreground "${args[@]}" |
| 167 | +elif [ "$1" = ${COMMAND_STANDALONE} ]; then |
| 168 | + args=("${args[@]:1}") |
| 169 | + |
| 170 | + echo "Starting Job Manager" |
| 171 | + |
| 172 | + exec $(drop_privs_cmd) "$FLINK_HOME/bin/standalone-job.sh" start-foreground "${args[@]}" |
| 173 | +elif [ "$1" = ${COMMAND_HISTORY_SERVER} ]; then |
| 174 | + args=("${args[@]:1}") |
| 175 | + |
| 176 | + echo "Starting History Server" |
| 177 | + |
| 178 | + exec $(drop_privs_cmd) "$FLINK_HOME/bin/historyserver.sh" start-foreground "${args[@]}" |
| 179 | +elif [ "$1" = "taskmanager" ]; then |
| 180 | + args=("${args[@]:1}") |
| 181 | + |
| 182 | + echo "Starting Task Manager" |
| 183 | + |
| 184 | + exec $(drop_privs_cmd) "$FLINK_HOME/bin/taskmanager.sh" start-foreground "${args[@]}" |
| 185 | +fi |
| 186 | + |
| 187 | +args=("${args[@]}") |
| 188 | + |
| 189 | +# Running command in pass-through mode |
| 190 | +exec $(drop_privs_cmd) "${args[@]}" |
0 commit comments