entrypoint.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/bin/bash
  2. #
  3. # Licensed to the Apache Software Foundation (ASF) under one or more
  4. # contributor license agreements. See the NOTICE file distributed with
  5. # this work for additional information regarding copyright ownership.
  6. # The ASF licenses this file to You under the Apache License, Version 2.0
  7. # (the "License"); you may not use this file except in compliance with
  8. # the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. # echo commands to the terminal output
  19. set -ex
  20. # Check whether there is a passwd entry for the container UID
  21. myuid=$(id -u)
  22. mygid=$(id -g)
  23. # turn off -e for getent because it will return error code in anonymous uid case
  24. set +e
  25. uidentry=$(getent passwd $myuid)
  26. set -e
  27. # If there is no passwd entry for the container UID, attempt to create one
  28. if [ -z "$uidentry" ] ; then
  29. if [ -w /etc/passwd ] ; then
  30. echo "$myuid:x:$myuid:$mygid:anonymous uid:$SPARK_HOME:/bin/false" >> /etc/passwd
  31. else
  32. echo "Container ENTRYPOINT failed to add passwd entry for anonymous UID"
  33. fi
  34. fi
  35. SPARK_K8S_CMD="$1"
  36. case "$SPARK_K8S_CMD" in
  37. driver | driver-py | driver-r | executor)
  38. shift 1
  39. ;;
  40. "")
  41. ;;
  42. *)
  43. echo "Non-spark-on-k8s command provided, proceeding in pass-through mode..."
  44. exec /usr/bin/tini -s -- "$@"
  45. ;;
  46. esac
  47. SPARK_CLASSPATH="$SPARK_CLASSPATH:${SPARK_HOME}/jars/*"
  48. env | grep SPARK_JAVA_OPT_ | sort -t_ -k4 -n | sed 's/[^=]*=\(.*\)/\1/g' > /tmp/java_opts.txt
  49. readarray -t SPARK_EXECUTOR_JAVA_OPTS < /tmp/java_opts.txt
  50. if [ -n "$SPARK_EXTRA_CLASSPATH" ]; then
  51. SPARK_CLASSPATH="$SPARK_CLASSPATH:$SPARK_EXTRA_CLASSPATH"
  52. fi
  53. if [ -n "$PYSPARK_FILES" ]; then
  54. PYTHONPATH="$PYTHONPATH:$PYSPARK_FILES"
  55. fi
  56. PYSPARK_ARGS=""
  57. if [ -n "$PYSPARK_APP_ARGS" ]; then
  58. PYSPARK_ARGS="$PYSPARK_APP_ARGS"
  59. fi
  60. R_ARGS=""
  61. if [ -n "$R_APP_ARGS" ]; then
  62. R_ARGS="$R_APP_ARGS"
  63. fi
  64. if [ "$PYSPARK_MAJOR_PYTHON_VERSION" == "2" ]; then
  65. pyv="$(python -V 2>&1)"
  66. export PYTHON_VERSION="${pyv:7}"
  67. export PYSPARK_PYTHON="python"
  68. export PYSPARK_DRIVER_PYTHON="python"
  69. elif [ "$PYSPARK_MAJOR_PYTHON_VERSION" == "3" ]; then
  70. pyv3="$(python3 -V 2>&1)"
  71. export PYTHON_VERSION="${pyv3:7}"
  72. export PYSPARK_PYTHON="python3"
  73. export PYSPARK_DRIVER_PYTHON="python3"
  74. fi
  75. case "$SPARK_K8S_CMD" in
  76. driver)
  77. CMD=(
  78. "$SPARK_HOME/bin/spark-submit"
  79. --conf "spark.driver.bindAddress=$SPARK_DRIVER_BIND_ADDRESS"
  80. --deploy-mode client
  81. "$@"
  82. )
  83. ;;
  84. driver-py)
  85. CMD=(
  86. "$SPARK_HOME/bin/spark-submit"
  87. --conf "spark.driver.bindAddress=$SPARK_DRIVER_BIND_ADDRESS"
  88. --deploy-mode client
  89. "$@" $PYSPARK_PRIMARY $PYSPARK_ARGS
  90. )
  91. ;;
  92. driver-r)
  93. CMD=(
  94. "$SPARK_HOME/bin/spark-submit"
  95. --conf "spark.driver.bindAddress=$SPARK_DRIVER_BIND_ADDRESS"
  96. --deploy-mode client
  97. "$@" $R_PRIMARY $R_ARGS
  98. )
  99. ;;
  100. executor)
  101. CMD=(
  102. ${JAVA_HOME}/bin/java
  103. "${SPARK_EXECUTOR_JAVA_OPTS[@]}"
  104. -Xms$SPARK_EXECUTOR_MEMORY
  105. -Xmx$SPARK_EXECUTOR_MEMORY
  106. -cp "$SPARK_CLASSPATH"
  107. org.apache.spark.executor.CoarseGrainedExecutorBackend
  108. --driver-url $SPARK_DRIVER_URL
  109. --executor-id $SPARK_EXECUTOR_ID
  110. --cores $SPARK_EXECUTOR_CORES
  111. --app-id $SPARK_APPLICATION_ID
  112. --hostname $SPARK_EXECUTOR_POD_IP
  113. )
  114. ;;
  115. *)
  116. echo "Unknown command: $SPARK_K8S_CMD" 1>&2
  117. exit 1
  118. esac
  119. # Execute the container CMD under tini for better hygiene
  120. BENCH=$(hostname | awk -F'-' '{print $1}')
  121. EXEC=$(hostname | awk -F'-' '{print $3"-"$4}')
  122. mkdir -p /results/$BENCH/$SCENARIO/$EXEC
  123. /pcm/pcm.x -r -csv=/results/$BENCH/$SCENARIO/$EXEC/pcm.csv 1>&- 2>&- &
  124. exec /usr/bin/tini -s -- "${CMD[@]}"