bash-java-utils.sh 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #!/usr/bin/env bash
  2. ################################################################################
  3. # Licensed to the Apache Software Foundation (ASF) under one
  4. # or more contributor license agreements. See the NOTICE file
  5. # distributed with this work for additional information
  6. # regarding copyright ownership. The ASF licenses this file
  7. # to you under the Apache License, Version 2.0 (the
  8. # "License"); you may not use this file except in compliance
  9. # with the License. You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. ################################################################################
  19. readFromConfigFile() {
  20. local key=$1
  21. local defaultValue=$2
  22. local configFile=$3
  23. # first extract the value with the given key (1st sed), then trim the result (2nd sed)
  24. # if a key exists multiple times, take the "last" one (tail)
  25. local value=`sed -n "s/^[ ]*${key}[ ]*: \([^#]*\).*$/\1/p" "${configFile}" | sed "s/^ *//;s/ *$//" | tail -n 1`
  26. [ -z "$value" ] && echo "$defaultValue" || echo "$value"
  27. }
  28. setJavaHome() {
  29. # read JAVA_HOME from config with no default value
  30. # NOTE: we need to obtain JAVA_HOME before using BashJavaUtils, so the value for env.java.home must
  31. # be in a flattened format, rather than nested, allowing us to retrieve the corresponding value via
  32. # shell script.
  33. CONF_FILE="$1/flink-conf.yaml"
  34. if [ ! -e "$1/flink-conf.yaml" ]; then
  35. CONF_FILE="$1/config.yaml"
  36. fi;
  37. KEY_ENV_JAVA_HOME="env.java.home"
  38. MY_JAVA_HOME=$(readFromConfigFile ${KEY_ENV_JAVA_HOME} "" "${CONF_FILE}")
  39. # check if config specified JAVA_HOME
  40. if [ -z "${MY_JAVA_HOME}" ]; then
  41. # config did not specify JAVA_HOME. Use system JAVA_HOME
  42. MY_JAVA_HOME="${JAVA_HOME}"
  43. fi
  44. # check if we have a valid JAVA_HOME and if java is not available
  45. if [ -z "${MY_JAVA_HOME}" ] && ! type java > /dev/null 2> /dev/null; then
  46. echo "Please specify JAVA_HOME. Either in Flink config ./conf/config.yaml or as system-wide JAVA_HOME."
  47. exit 1
  48. else
  49. export JAVA_HOME="${MY_JAVA_HOME}"
  50. fi
  51. }
  52. setJavaRun() {
  53. setJavaHome "$1"
  54. UNAME=$(uname -s)
  55. if [ "${UNAME:0:6}" == "CYGWIN" ]; then
  56. JAVA_RUN=java
  57. else
  58. if [[ -d "$JAVA_HOME" ]]; then
  59. JAVA_RUN="$JAVA_HOME"/bin/java
  60. else
  61. JAVA_RUN=java
  62. fi
  63. fi
  64. export JAVA_RUN
  65. }
  66. manglePathList() {
  67. UNAME=$(uname -s)
  68. # a path list, for example a java classpath
  69. if [ "${UNAME:0:6}" == "CYGWIN" ]; then
  70. echo `cygpath -wp "$1"`
  71. else
  72. echo $1
  73. fi
  74. }
  75. findFlinkDistJar() {
  76. local FLINK_DIST
  77. local LIB_DIR
  78. if [[ -n "$1" ]]; then
  79. LIB_DIR="$1"
  80. else
  81. LIB_DIR="$FLINK_LIB_DIR"
  82. fi
  83. FLINK_DIST="$(find "$LIB_DIR" -name 'flink-dist*.jar')"
  84. local FLINK_DIST_COUNT
  85. FLINK_DIST_COUNT="$(echo "$FLINK_DIST" | wc -l)"
  86. # If flink-dist*.jar cannot be resolved write error messages to stderr since stdout is stored
  87. # as the classpath and exit function with empty classpath to force process failure
  88. if [[ "$FLINK_DIST" == "" ]]; then
  89. (>&2 echo "[ERROR] Flink distribution jar not found in $FLINK_LIB_DIR.")
  90. exit 1
  91. elif [[ "$FLINK_DIST_COUNT" -gt 1 ]]; then
  92. (>&2 echo "[ERROR] Multiple flink-dist*.jar found in $FLINK_LIB_DIR. Please resolve.")
  93. exit 1
  94. fi
  95. echo "$FLINK_DIST"
  96. }
  97. runBashJavaUtilsCmd() {
  98. local cmd=$1
  99. local conf_dir=$2
  100. local class_path=$3
  101. local dynamic_args=${@:4}
  102. class_path=`manglePathList "${class_path}"`
  103. local output=`"${JAVA_RUN}" -classpath "${class_path}" org.apache.flink.runtime.util.bash.BashJavaUtils ${cmd} --configDir "${conf_dir}" $dynamic_args 2>&1 | tail -n 1000`
  104. if [[ $? -ne 0 ]]; then
  105. echo "[ERROR] Cannot run BashJavaUtils to execute command ${cmd}." 1>&2
  106. # Print the output in case the user redirect the log to console.
  107. echo "$output" 1>&2
  108. exit 1
  109. fi
  110. echo "$output"
  111. }
  112. updateAndGetFlinkConfiguration() {
  113. local FLINK_CONF_DIR="$1"
  114. local FLINK_BIN_DIR="$2"
  115. local FLINK_LIB_DIR="$3"
  116. local command_result
  117. command_result=$(parseConfigurationAndExportLogs "$FLINK_CONF_DIR" "$FLINK_BIN_DIR" "$FLINK_LIB_DIR" "UPDATE_AND_GET_FLINK_CONFIGURATION" "${@:4}")
  118. echo "$command_result"
  119. }
  120. migrateLegacyFlinkConfigToStandardYaml() {
  121. local FLINK_CONF_DIR="$1"
  122. local FLINK_BIN_DIR="$2"
  123. local FLINK_LIB_DIR="$3"
  124. local command_result
  125. command_result=$(parseConfigurationAndExportLogs "$FLINK_CONF_DIR" "$FLINK_BIN_DIR" "$FLINK_LIB_DIR" "MIGRATE_LEGACY_FLINK_CONFIGURATION_TO_STANDARD_YAML")
  126. echo "$command_result"
  127. }
  128. parseConfigurationAndExportLogs() {
  129. local FLINK_CONF_DIR="$1"
  130. local FLINK_BIN_DIR="$2"
  131. local FLINK_LIB_DIR="$3"
  132. local COMMAND="$4"
  133. local EXECUTION_PREFIX="BASH_JAVA_UTILS_EXEC_RESULT:"
  134. java_utils_output=$(runBashJavaUtilsCmd "${COMMAND}" "${FLINK_CONF_DIR}" "${FLINK_BIN_DIR}/bash-java-utils.jar:$(findFlinkDistJar ${FLINK_LIB_DIR})" "${@:5}")
  135. logging_output=$(extractLoggingOutputs "${java_utils_output}")
  136. execution_results=$(echo "${java_utils_output}" | grep ${EXECUTION_PREFIX})
  137. if [[ $? -ne 0 ]]; then
  138. echo "[ERROR] Could not parse configurations properly."
  139. echo "[ERROR] Raw output from BashJavaUtils:"
  140. echo "$java_utils_output"
  141. exit 1
  142. fi
  143. echo "${execution_results//${EXECUTION_PREFIX}/}"
  144. }
  145. extractLoggingOutputs() {
  146. local output="$1"
  147. local EXECUTION_PREFIX="BASH_JAVA_UTILS_EXEC_RESULT:"
  148. echo "${output}" | grep -v ${EXECUTION_PREFIX}
  149. }
  150. extractExecutionResults() {
  151. local output="$1"
  152. local expected_lines="$2"
  153. local EXECUTION_PREFIX="BASH_JAVA_UTILS_EXEC_RESULT:"
  154. local execution_results
  155. local num_lines
  156. execution_results=$(echo "${output}" | grep ${EXECUTION_PREFIX})
  157. num_lines=$(echo "${execution_results}" | wc -l)
  158. # explicit check for empty result, because if execution_results is empty, then wc returns 1
  159. if [[ -z ${execution_results} ]]; then
  160. echo "[ERROR] The execution result is empty." 1>&2
  161. exit 1
  162. fi
  163. if [[ ${num_lines} -ne ${expected_lines} ]]; then
  164. echo "[ERROR] The execution results has unexpected number of lines, expected: ${expected_lines}, actual: ${num_lines}." 1>&2
  165. echo "[ERROR] An execution result line is expected following the prefix '${EXECUTION_PREFIX}'" 1>&2
  166. echo "$output" 1>&2
  167. exit 1
  168. fi
  169. echo "${execution_results//${EXECUTION_PREFIX}/}"
  170. }
  171. parseResourceParamsAndExportLogs() {
  172. local cmd=$1
  173. java_utils_output=$(runBashJavaUtilsCmd ${cmd} "${FLINK_CONF_DIR}" "${FLINK_BIN_DIR}/bash-java-utils.jar:$(findFlinkDistJar)" "${@:2}")
  174. logging_output=$(extractLoggingOutputs "${java_utils_output}")
  175. params_output=$(extractExecutionResults "${java_utils_output}" 2)
  176. if [[ $? -ne 0 ]]; then
  177. echo "[ERROR] Could not get JVM parameters and dynamic configurations properly."
  178. echo "[ERROR] Raw output from BashJavaUtils:"
  179. echo "$java_utils_output"
  180. exit 1
  181. fi
  182. jvm_params=$(echo "${params_output}" | head -n1)
  183. export JVM_ARGS="${JVM_ARGS} ${jvm_params}"
  184. export DYNAMIC_PARAMETERS=$(IFS=" " echo "${params_output}" | tail -n1)
  185. export FLINK_INHERITED_LOGS="
  186. $FLINK_INHERITED_LOGS
  187. RESOURCE_PARAMS extraction logs:
  188. jvm_params: $jvm_params
  189. dynamic_configs: $DYNAMIC_PARAMETERS
  190. logs: $logging_output
  191. "
  192. }