flink-console.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. # Start a Flink service as a console application. Must be stopped with Ctrl-C
  20. # or with SIGTERM by kill or the controlling process.
  21. USAGE="Usage: flink-console.sh (taskexecutor|zookeeper|historyserver|standalonesession|standalonejob|kubernetes-session|kubernetes-application|kubernetes-taskmanager|sql-gateway) [args]"
  22. SERVICE=$1
  23. ARGS=("${@:2}") # get remaining arguments as array
  24. bin=`dirname "$0"`
  25. bin=`cd "$bin"; pwd`
  26. . "$bin"/config.sh
  27. case $SERVICE in
  28. (taskexecutor)
  29. CLASS_TO_RUN=org.apache.flink.runtime.taskexecutor.TaskManagerRunner
  30. ;;
  31. (historyserver)
  32. CLASS_TO_RUN=org.apache.flink.runtime.webmonitor.history.HistoryServer
  33. ;;
  34. (zookeeper)
  35. CLASS_TO_RUN=org.apache.flink.runtime.zookeeper.FlinkZooKeeperQuorumPeer
  36. ;;
  37. (standalonesession)
  38. CLASS_TO_RUN=org.apache.flink.runtime.entrypoint.StandaloneSessionClusterEntrypoint
  39. ;;
  40. (standalonejob)
  41. CLASS_TO_RUN=org.apache.flink.container.entrypoint.StandaloneApplicationClusterEntryPoint
  42. ;;
  43. (kubernetes-session)
  44. CLASS_TO_RUN=org.apache.flink.kubernetes.entrypoint.KubernetesSessionClusterEntrypoint
  45. ;;
  46. (kubernetes-application)
  47. CLASS_TO_RUN=org.apache.flink.kubernetes.entrypoint.KubernetesApplicationClusterEntrypoint
  48. ;;
  49. (kubernetes-taskmanager)
  50. CLASS_TO_RUN=org.apache.flink.kubernetes.taskmanager.KubernetesTaskExecutorRunner
  51. ;;
  52. (sql-gateway)
  53. CLASS_TO_RUN=org.apache.flink.table.gateway.SqlGateway
  54. SQL_GATEWAY_CLASSPATH="`findSqlGatewayJar`":"`findFlinkPythonJar`"
  55. ;;
  56. (*)
  57. echo "Unknown service '${SERVICE}'. $USAGE."
  58. exit 1
  59. ;;
  60. esac
  61. FLINK_TM_CLASSPATH=`constructFlinkClassPath`
  62. if [ "$FLINK_IDENT_STRING" = "" ]; then
  63. FLINK_IDENT_STRING="$USER"
  64. fi
  65. pid=$FLINK_PID_DIR/flink-$FLINK_IDENT_STRING-$SERVICE.pid
  66. mkdir -p "$FLINK_PID_DIR"
  67. # The lock needs to be released after use because this script is started foreground
  68. command -v flock >/dev/null 2>&1
  69. flock_exist=$?
  70. if [[ ${flock_exist} -eq 0 ]]; then
  71. exec 200<"$FLINK_PID_DIR"
  72. flock 200
  73. fi
  74. # Remove the pid file when all the processes are dead
  75. if [ -f "$pid" ]; then
  76. all_dead=0
  77. while read each_pid; do
  78. # Check whether the process is still running
  79. kill -0 $each_pid > /dev/null 2>&1
  80. [[ $? -eq 0 ]] && all_dead=1
  81. done < "$pid"
  82. [ ${all_dead} -eq 0 ] && rm $pid
  83. fi
  84. id=$([ -f "$pid" ] && echo $(wc -l < "$pid") || echo "0")
  85. FLINK_LOG_PREFIX="${FLINK_LOG_DIR}/flink-${FLINK_IDENT_STRING}-${SERVICE}-${id}-${HOSTNAME}"
  86. log="${FLINK_LOG_PREFIX}.log"
  87. out="${FLINK_LOG_PREFIX}.out"
  88. err="${FLINK_LOG_PREFIX}.err"
  89. log_setting=("-Dlog.file=${log}" "-Dlog4j.configuration=file:${FLINK_CONF_DIR}/log4j-console.properties" "-Dlog4j.configurationFile=file:${FLINK_CONF_DIR}/log4j-console.properties" "-Dlogback.configurationFile=file:${FLINK_CONF_DIR}/logback-console.xml")
  90. echo "Starting $SERVICE as a console application on host $HOSTNAME."
  91. # Add the current process id to pid file
  92. echo $$ >> "$pid" 2>/dev/null
  93. # Release the lock because the java process runs in the foreground and would block other processes from modifying the pid file
  94. [[ ${flock_exist} -eq 0 ]] && flock -u 200
  95. # Evaluate user options for local variable expansion
  96. FLINK_ENV_JAVA_OPTS=$(eval echo ${FLINK_ENV_JAVA_OPTS})
  97. if [ "${STD_REDIRECT_TO_FILE}" == "true" ]; then
  98. # disable console appender to avoid redundant logs in out file
  99. log_setting=("-Dconsole.log.level=OFF" "${log_setting[@]}")
  100. exec 1>"${out}"
  101. exec 2>"${err}"
  102. fi
  103. exec "$JAVA_RUN" $JVM_ARGS ${FLINK_ENV_JAVA_OPTS} "${log_setting[@]}" -classpath "`manglePathList "$FLINK_TM_CLASSPATH:$SQL_GATEWAY_CLASSPATH:$INTERNAL_HADOOP_CLASSPATHS"`" ${CLASS_TO_RUN} "${ARGS[@]}"