1
0

hadoop-functions.sh 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. #!/bin/bash
  2. # Licensed to the Apache Software Foundation (ASF) under one or more
  3. # contributor license agreements. See the NOTICE file distributed with
  4. # this work for additional information regarding copyright ownership.
  5. # The ASF licenses this file to You under the Apache License, Version 2.0
  6. # (the "License"); you may not use this file except in compliance with
  7. # the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. function hadoop_error
  17. {
  18. # NOTE: This function is not user replaceable.
  19. echo "$*" 1>&2
  20. }
  21. function hadoop_bootstrap_init
  22. {
  23. # NOTE: This function is not user replaceable.
  24. # the root of the Hadoop installation
  25. # See HADOOP-6255 for the expected directory structure layout
  26. # By now, HADOOP_LIBEXEC_DIR should have been defined upstream
  27. # We can piggyback off of that to figure out where the default
  28. # HADOOP_FREFIX should be. This allows us to run without
  29. # HADOOP_PREFIX ever being defined by a human! As a consequence
  30. # HADOOP_LIBEXEC_DIR now becomes perhaps the single most powerful
  31. # env var within Hadoop.
  32. if [[ -z "${HADOOP_LIBEXEC_DIR}" ]]; then
  33. hadoop_error "HADOOP_LIBEXEC_DIR is not defined. Exiting."
  34. exit 1
  35. fi
  36. HADOOP_DEFAULT_PREFIX=$(cd -P -- "${HADOOP_LIBEXEC_DIR}/.." >/dev/null && pwd -P)
  37. HADOOP_PREFIX=${HADOOP_PREFIX:-$HADOOP_DEFAULT_PREFIX}
  38. export HADOOP_PREFIX
  39. #
  40. # short-cuts. vendors may redefine these as well, preferably
  41. # in hadoop-layouts.sh
  42. #
  43. HADOOP_COMMON_DIR=${HADOOP_COMMON_DIR:-"share/hadoop/common"}
  44. HADOOP_COMMON_LIB_JARS_DIR=${HADOOP_COMMON_LIB_JARS_DIR:-"share/hadoop/common/lib"}
  45. HADOOP_COMMON_LIB_NATIVE_DIR=${HADOOP_COMMON_LIB_NATIVE_DIR:-"lib/native"}
  46. HDFS_DIR=${HDFS_DIR:-"share/hadoop/hdfs"}
  47. HDFS_LIB_JARS_DIR=${HDFS_LIB_JARS_DIR:-"share/hadoop/hdfs/lib"}
  48. YARN_DIR=${YARN_DIR:-"share/hadoop/yarn"}
  49. YARN_LIB_JARS_DIR=${YARN_LIB_JARS_DIR:-"share/hadoop/yarn/lib"}
  50. MAPRED_DIR=${MAPRED_DIR:-"share/hadoop/mapreduce"}
  51. MAPRED_LIB_JARS_DIR=${MAPRED_LIB_JARS_DIR:-"share/hadoop/mapreduce/lib"}
  52. # setup a default TOOL_PATH
  53. TOOL_PATH=${TOOL_PATH:-${HADOOP_PREFIX}/share/hadoop/tools/lib/*}
  54. export HADOOP_OS_TYPE=${HADOOP_OS_TYPE:-$(uname -s)}
  55. # defaults
  56. export HADOOP_OPTS=${HADOOP_OPTS:-"-Djava.net.preferIPv4Stack=true"}
  57. }
  58. function hadoop_find_confdir
  59. {
  60. # NOTE: This function is not user replaceable.
  61. local conf_dir
  62. # Look for the basic hadoop configuration area.
  63. #
  64. #
  65. # An attempt at compatibility with some Hadoop 1.x
  66. # installs.
  67. if [[ -e "${HADOOP_PREFIX}/conf/hadoop-env.sh" ]]; then
  68. conf_dir="conf"
  69. else
  70. conf_dir="etc/hadoop"
  71. fi
  72. export HADOOP_CONF_DIR="${HADOOP_CONF_DIR:-${HADOOP_PREFIX}/${conf_dir}}"
  73. }
  74. function hadoop_exec_hadoopenv
  75. {
  76. # NOTE: This function is not user replaceable.
  77. if [[ -z "${HADOOP_ENV_PROCESSED}" ]]; then
  78. if [[ -f "${HADOOP_CONF_DIR}/hadoop-env.sh" ]]; then
  79. export HADOOP_ENV_PROCESSED=true
  80. . "${HADOOP_CONF_DIR}/hadoop-env.sh"
  81. fi
  82. fi
  83. }
  84. function hadoop_basic_init
  85. {
  86. # Some of these are also set in hadoop-env.sh.
  87. # we still set them here just in case hadoop-env.sh is
  88. # broken in some way, set up defaults, etc.
  89. #
  90. # but it is important to note that if you update these
  91. # you also need to update hadoop-env.sh as well!!!
  92. # CLASSPATH initially contains $HADOOP_CONF_DIR
  93. CLASSPATH="${HADOOP_CONF_DIR}"
  94. if [[ -z "${HADOOP_COMMON_HOME}" ]] &&
  95. [[ -d "${HADOOP_PREFIX}/${HADOOP_COMMON_DIR}" ]]; then
  96. export HADOOP_COMMON_HOME="${HADOOP_PREFIX}"
  97. fi
  98. # default policy file for service-level authorization
  99. HADOOP_POLICYFILE=${HADOOP_POLICYFILE:-"hadoop-policy.xml"}
  100. # define HADOOP_HDFS_HOME
  101. if [[ -z "${HADOOP_HDFS_HOME}" ]] &&
  102. [[ -d "${HADOOP_PREFIX}/${HDFS_DIR}" ]]; then
  103. export HADOOP_HDFS_HOME="${HADOOP_PREFIX}"
  104. fi
  105. # define HADOOP_YARN_HOME
  106. if [[ -z "${HADOOP_YARN_HOME}" ]] &&
  107. [[ -d "${HADOOP_PREFIX}/${YARN_DIR}" ]]; then
  108. export HADOOP_YARN_HOME="${HADOOP_PREFIX}"
  109. fi
  110. # define HADOOP_MAPRED_HOME
  111. if [[ -z "${HADOOP_MAPRED_HOME}" ]] &&
  112. [[ -d "${HADOOP_PREFIX}/${MAPRED_DIR}" ]]; then
  113. export HADOOP_MAPRED_HOME="${HADOOP_PREFIX}"
  114. fi
  115. HADOOP_IDENT_STRING=${HADOP_IDENT_STRING:-$USER}
  116. HADOOP_LOG_DIR=${HADOOP_LOG_DIR:-"${HADOOP_PREFIX}/logs"}
  117. HADOOP_LOGFILE=${HADOOP_LOGFILE:-hadoop.log}
  118. HADOOP_NICENESS=${HADOOP_NICENESS:-0}
  119. HADOOP_STOP_TIMEOUT=${HADOOP_STOP_TIMEOUT:-5}
  120. HADOOP_PID_DIR=${HADOOP_PID_DIR:-/tmp}
  121. HADOOP_ROOT_LOGGER=${HADOOP_ROOT_LOGGER:-INFO,console}
  122. HADOOP_DAEMON_ROOT_LOGGER=${HADOOP_DAEMON_ROOT_LOGGER:-INFO,RFA}
  123. HADOOP_SECURITY_LOGGER=${HADOOP_SECURITY_LOGGER:-INFO,NullAppender}
  124. HADOOP_HEAPSIZE=${HADOOP_HEAPSIZE:-1024}
  125. HADOOP_SSH_OPTS=${HADOOP_SSH_OPTS:-"-o BatchMode=yes -o StrictHostKeyChecking=no -o ConnectTimeout=10s"}
  126. HADOOP_SECURE_LOG_DIR=${HADOOP_SECURE_LOG_DIR:-${HADOOP_LOG_DIR}}
  127. HADOOP_SECURE_PID_DIR=${HADOOP_SECURE_PID_DIR:-${HADOOP_PID_DIR}}
  128. HADOOP_SSH_PARALLEL=${HADOOP_SSH_PARALLEL:-10}
  129. }
  130. function hadoop_populate_slaves_file()
  131. {
  132. # NOTE: This function is not user replaceable.
  133. local slavesfile=$1
  134. shift
  135. if [[ -f "${slavesfile}" ]]; then
  136. # shellcheck disable=2034
  137. HADOOP_SLAVES="${slavesfile}"
  138. elif [[ -f "${HADOOP_CONF_DIR}/${slavesfile}" ]]; then
  139. # shellcheck disable=2034
  140. HADOOP_SLAVES="${HADOOP_CONF_DIR}/${slavesfile}"
  141. # shellcheck disable=2034
  142. YARN_SLAVES="${HADOOP_CONF_DIR}/${slavesfile}"
  143. else
  144. hadoop_error "ERROR: Cannot find hosts file \"${slavesfile}\""
  145. hadoop_exit_with_usage 1
  146. fi
  147. }
  148. function hadoop_rotate_log
  149. {
  150. #
  151. # log rotation (mainly used for .out files)
  152. # Users are likely to replace this one for something
  153. # that gzips or uses dates or who knows what.
  154. #
  155. # be aware that &1 and &2 might go through here
  156. # so don't do anything too crazy...
  157. #
  158. local log=$1;
  159. local num=${2:-5};
  160. if [[ -f "${log}" ]]; then # rotate logs
  161. while [[ ${num} -gt 1 ]]; do
  162. #shellcheck disable=SC2086
  163. let prev=${num}-1
  164. if [[ -f "${log}.${prev}" ]]; then
  165. mv "${log}.${prev}" "${log}.${num}"
  166. fi
  167. num=${prev}
  168. done
  169. mv "${log}" "${log}.${num}"
  170. fi
  171. }
  172. function hadoop_actual_ssh
  173. {
  174. # we are passing this function to xargs
  175. # should get hostname followed by rest of command line
  176. local slave=$1
  177. shift
  178. # shellcheck disable=SC2086
  179. ssh ${HADOOP_SSH_OPTS} ${slave} $"${@// /\\ }" 2>&1 | sed "s/^/$slave: /"
  180. }
  181. function hadoop_connect_to_hosts
  182. {
  183. # shellcheck disable=SC2124
  184. local params="$@"
  185. #
  186. # ssh (or whatever) to a host
  187. #
  188. # User can specify hostnames or a file where the hostnames are (not both)
  189. if [[ -n "${HADOOP_SLAVES}" && -n "${HADOOP_SLAVE_NAMES}" ]] ; then
  190. hadoop_error "ERROR: Both HADOOP_SLAVES and HADOOP_SLAVE_NAME were defined. Aborting."
  191. exit 1
  192. fi
  193. if [[ -n "${HADOOP_SLAVE_NAMES}" ]] ; then
  194. SLAVE_NAMES=${HADOOP_SLAVE_NAMES}
  195. else
  196. SLAVE_FILE=${HADOOP_SLAVES:-${HADOOP_CONF_DIR}/slaves}
  197. fi
  198. # if pdsh is available, let's use it. otherwise default
  199. # to a loop around ssh. (ugh)
  200. if [[ -e '/usr/bin/pdsh' ]]; then
  201. if [[ -z "${HADOOP_SLAVE_NAMES}" ]] ; then
  202. # if we were given a file, just let pdsh deal with it.
  203. # shellcheck disable=SC2086
  204. PDSH_SSH_ARGS_APPEND="${HADOOP_SSH_OPTS}" pdsh \
  205. -f "${HADOOP_SSH_PARALLEL}" -w ^"${SLAVE_FILE}" $"${@// /\\ }" 2>&1
  206. else
  207. # no spaces allowed in the pdsh arg host list
  208. # shellcheck disable=SC2086
  209. SLAVE_NAMES=$(echo ${SLAVE_NAMES} | tr -s ' ' ,)
  210. PDSH_SSH_ARGS_APPEND="${HADOOP_SSH_OPTS}" pdsh \
  211. -f "${HADOOP_SSH_PARALLEL}" -w "${SLAVE_NAMES}" $"${@// /\\ }" 2>&1
  212. fi
  213. else
  214. if [[ -z "${SLAVE_NAMES}" ]]; then
  215. SLAVE_NAMES=$(sed 's/#.*$//;/^$/d' "${SLAVE_FILE}")
  216. fi
  217. # quoting here gets tricky. it's easier to push it into a function
  218. # so that we don't have to deal with it. However...
  219. # xargs can't use a function so instead we'll export it out
  220. # and force it into a subshell
  221. # moral of the story: just use pdsh.
  222. export -f hadoop_actual_ssh
  223. export HADOOP_SSH_OPTS
  224. echo "${SLAVE_NAMES}" | \
  225. xargs -n 1 -P"${HADOOP_SSH_PARALLEL}" \
  226. -I {} bash -c -- "hadoop_actual_ssh {} ${params}"
  227. wait
  228. fi
  229. }
  230. function hadoop_add_param
  231. {
  232. #
  233. # general param dedupe..
  234. # $1 is what we are adding to
  235. # $2 is the name of what we want to add (key)
  236. # $3 is the key+value of what we're adding
  237. #
  238. # doing it this way allows us to support all sorts of
  239. # different syntaxes, just so long as they are space
  240. # delimited
  241. #
  242. if [[ ! ${!1} =~ $2 ]] ; then
  243. # shellcheck disable=SC2086
  244. eval $1="'${!1} $3'"
  245. fi
  246. }
  247. function hadoop_add_classpath
  248. {
  249. # two params:
  250. # $1 = directory, file, wildcard, whatever to add
  251. # $2 = before or after, which determines where in the
  252. # classpath this object should go. default is after
  253. # return 0 = success
  254. # return 1 = failure (duplicate, doesn't exist, whatever)
  255. # However, with classpath (& JLP), we can do dedupe
  256. # along with some sanity checking (e.g., missing directories)
  257. # since we have a better idea of what is legal
  258. #
  259. # for wildcard at end, we can
  260. # at least check the dir exists
  261. if [[ $1 =~ ^.*\*$ ]]; then
  262. local mp=$(dirname "$1")
  263. if [[ ! -d "${mp}" ]]; then
  264. return 1
  265. fi
  266. # no wildcard in the middle, so check existence
  267. # (doesn't matter *what* it is)
  268. elif [[ ! $1 =~ ^.*\*.*$ ]] && [[ ! -e "$1" ]]; then
  269. return 1
  270. fi
  271. if [[ -z "${CLASSPATH}" ]]; then
  272. CLASSPATH=$1
  273. elif [[ ":${CLASSPATH}:" != *":$1:"* ]]; then
  274. if [[ "$2" = "before" ]]; then
  275. CLASSPATH="$1:${CLASSPATH}"
  276. else
  277. CLASSPATH+=:$1
  278. fi
  279. fi
  280. return 0
  281. }
  282. function hadoop_add_colonpath
  283. {
  284. # two params:
  285. # $1 = directory, file, wildcard, whatever to add
  286. # $2 = before or after, which determines where in the
  287. # classpath this object should go
  288. # return 0 = success
  289. # return 1 = failure (duplicate)
  290. # this is CLASSPATH, JLP, etc but with dedupe but no
  291. # other checking
  292. if [[ -d "${2}" ]] && [[ ":${!1}:" != *":$2:"* ]]; then
  293. if [[ -z "${!1}" ]]; then
  294. # shellcheck disable=SC2086
  295. eval $1="'$2'"
  296. elif [[ "$3" = "before" ]]; then
  297. # shellcheck disable=SC2086
  298. eval $1="'$2:${!1}'"
  299. else
  300. # shellcheck disable=SC2086
  301. eval $1+="'$2'"
  302. fi
  303. fi
  304. }
  305. function hadoop_add_javalibpath
  306. {
  307. # specialized function for a common use case
  308. hadoop_add_colonpath JAVA_LIBRARY_PATH "$1" "$2"
  309. }
  310. function hadoop_add_ldlibpath
  311. {
  312. # specialized function for a common use case
  313. hadoop_add_colonpath LD_LIBRARY_PATH "$1" "$2"
  314. # note that we export this
  315. export LD_LIBRARY_PATH
  316. }
  317. function hadoop_add_to_classpath_common
  318. {
  319. #
  320. # get all of the common jars+config in the path
  321. #
  322. # developers
  323. if [[ -n "${HADOOP_ENABLE_BUILD_PATHS}" ]]; then
  324. hadoop_add_classpath "${HADOOP_COMMON_HOME}/hadoop-common/target/classes"
  325. fi
  326. if [[ -d "${HADOOP_COMMON_HOME}/${HADOOP_COMMON_DIR}/webapps" ]]; then
  327. hadoop_add_classpath "${HADOOP_COMMON_HOME}/${HADOOP_COMMON_DIR}"
  328. fi
  329. hadoop_add_classpath "${HADOOP_COMMON_HOME}/${HADOOP_COMMON_LIB_JARS_DIR}"'/*'
  330. hadoop_add_classpath "${HADOOP_COMMON_HOME}/${HADOOP_COMMON_DIR}"'/*'
  331. }
  332. function hadoop_add_to_classpath_hdfs
  333. {
  334. #
  335. # get all of the hdfs jars+config in the path
  336. #
  337. # developers
  338. if [[ -n "${HADOOP_ENABLE_BUILD_PATHS}" ]]; then
  339. hadoop_add_classpath "${HADOOP_HDFS_HOME}/hadoop-hdfs/target/classes"
  340. fi
  341. # put hdfs in classpath if present
  342. if [[ -d "${HADOOP_HDFS_HOME}/${HDFS_DIR}/webapps" ]]; then
  343. hadoop_add_classpath "${HADOOP_HDFS_HOME}/${HDFS_DIR}"
  344. fi
  345. hadoop_add_classpath "${HADOOP_HDFS_HOME}/${HDFS_LIB_JARS_DIR}"'/*'
  346. hadoop_add_classpath "${HADOOP_HDFS_HOME}/${HDFS_DIR}"'/*'
  347. }
  348. function hadoop_add_to_classpath_yarn
  349. {
  350. #
  351. # get all of the yarn jars+config in the path
  352. #
  353. # developers
  354. if [[ -n "${HADOOP_ENABLE_BUILD_PATHS}" ]]; then
  355. for i in yarn-api yarn-common yarn-mapreduce yarn-master-worker \
  356. yarn-server/yarn-server-nodemanager \
  357. yarn-server/yarn-server-common \
  358. yarn-server/yarn-server-resourcemanager; do
  359. hadoop_add_classpath "${HADOOP_YARN_HOME}/$i/target/classes"
  360. done
  361. hadoop_add_classpath "${HADOOP_YARN_HOME}/build/test/classes"
  362. hadoop_add_classpath "${HADOOP_YARN_HOME}/build/tools"
  363. fi
  364. if [[ -d "${HADOOP_YARN_HOME}/${YARN_DIR}/webapps" ]]; then
  365. hadoop_add_classpath "${HADOOP_YARN_HOME}/${YARN_DIR}"
  366. fi
  367. hadoop_add_classpath "${HADOOP_YARN_HOME}/${YARN_LIB_JARS_DIR}"'/*'
  368. hadoop_add_classpath "${HADOOP_YARN_HOME}/${YARN_DIR}"'/*'
  369. }
  370. function hadoop_add_to_classpath_mapred
  371. {
  372. #
  373. # get all of the mapreduce jars+config in the path
  374. #
  375. # developers
  376. if [[ -n "${HADOOP_ENABLE_BUILD_PATHS}" ]]; then
  377. hadoop_add_classpath "${HADOOP_MAPRED_HOME}/hadoop-mapreduce-client-shuffle/target/classes"
  378. hadoop_add_classpath "${HADOOP_MAPRED_HOME}/hadoop-mapreduce-client-common/target/classes"
  379. hadoop_add_classpath "${HADOOP_MAPRED_HOME}/hadoop-mapreduce-client-hs/target/classes"
  380. hadoop_add_classpath "${HADOOP_MAPRED_HOME}/hadoop-mapreduce-client-hs-plugins/target/classes"
  381. hadoop_add_classpath "${HADOOP_MAPRED_HOME}/hadoop-mapreduce-client-app/target/classes"
  382. hadoop_add_classpath "${HADOOP_MAPRED_HOME}/hadoop-mapreduce-client-jobclient/target/classes"
  383. hadoop_add_classpath "${HADOOP_MAPRED_HOME}/hadoop-mapreduce-client-core/target/classes"
  384. fi
  385. if [[ -d "${HADOOP_MAPRED_HOME}/${MAPRED_DIR}/webapps" ]]; then
  386. hadoop_add_classpath "${HADOOP_MAPRED_HOME}/${MAPRED_DIR}"
  387. fi
  388. hadoop_add_classpath "${HADOOP_MAPRED_HOME}/${MAPRED_LIB_JARS_DIR}"'/*'
  389. hadoop_add_classpath "${HADOOP_MAPRED_HOME}/${MAPRED_DIR}"'/*'
  390. }
  391. function hadoop_add_to_classpath_userpath
  392. {
  393. # Add the user-specified HADOOP_CLASSPATH to the
  394. # official CLASSPATH env var if HADOOP_USE_CLIENT_CLASSLOADER
  395. # is not set.
  396. # Add it first or last depending on if user has
  397. # set env-var HADOOP_USER_CLASSPATH_FIRST
  398. # we'll also dedupe it, because we're cool like that.
  399. #
  400. local c
  401. local array
  402. local i
  403. local j
  404. let c=0
  405. if [[ -n "${HADOOP_CLASSPATH}" ]]; then
  406. # I wonder if Java runs on VMS.
  407. for i in $(echo "${HADOOP_CLASSPATH}" | tr : '\n'); do
  408. array[$c]=$i
  409. let c+=1
  410. done
  411. let j=c-1
  412. if [[ -z "${HADOOP_USE_CLIENT_CLASSLOADER}" ]]; then
  413. if [[ -z "${HADOOP_USER_CLASSPATH_FIRST}" ]]; then
  414. for ((i=j; i>=0; i--)); do
  415. hadoop_add_classpath "${array[$i]}" before
  416. done
  417. else
  418. for ((i=0; i<=j; i++)); do
  419. hadoop_add_classpath "${array[$i]}" after
  420. done
  421. fi
  422. fi
  423. fi
  424. }
  425. function hadoop_os_tricks
  426. {
  427. local bindv6only
  428. # some OSes have special needs. here's some out of the box
  429. # examples for OS X and Linux. Vendors, replace this with your special sauce.
  430. case ${HADOOP_OS_TYPE} in
  431. Darwin)
  432. if [[ -x /usr/libexec/java_home ]]; then
  433. export JAVA_HOME="$(/usr/libexec/java_home)"
  434. else
  435. export JAVA_HOME=/Library/Java/Home
  436. fi
  437. ;;
  438. Linux)
  439. bindv6only=$(/sbin/sysctl -n net.ipv6.bindv6only 2> /dev/null)
  440. # NOTE! HADOOP_ALLOW_IPV6 is a developer hook. We leave it
  441. # undocumented in hadoop-env.sh because we don't want users to
  442. # shoot themselves in the foot while devs make IPv6 work.
  443. if [[ -n "${bindv6only}" ]] &&
  444. [[ "${bindv6only}" -eq "1" ]] &&
  445. [[ "${HADOOP_ALLOW_IPV6}" != "yes" ]]; then
  446. hadoop_error "ERROR: \"net.ipv6.bindv6only\" is set to 1 "
  447. hadoop_error "ERROR: Hadoop networking could be broken. Aborting."
  448. hadoop_error "ERROR: For more info: http://wiki.apache.org/hadoop/HadoopIPv6"
  449. exit 1
  450. fi
  451. # Newer versions of glibc use an arena memory allocator that
  452. # causes virtual # memory usage to explode. This interacts badly
  453. # with the many threads that we use in Hadoop. Tune the variable
  454. # down to prevent vmem explosion.
  455. export MALLOC_ARENA_MAX=${MALLOC_ARENA_MAX:-4}
  456. ;;
  457. esac
  458. }
  459. function hadoop_java_setup
  460. {
  461. # Bail if we did not detect it
  462. if [[ -z "${JAVA_HOME}" ]]; then
  463. hadoop_error "ERROR: JAVA_HOME is not set and could not be found."
  464. exit 1
  465. fi
  466. if [[ ! -d "${JAVA_HOME}" ]]; then
  467. hadoop_error "ERROR: JAVA_HOME ${JAVA_HOME} does not exist."
  468. exit 1
  469. fi
  470. JAVA="${JAVA_HOME}/bin/java"
  471. if [[ ! -x "$JAVA" ]]; then
  472. hadoop_error "ERROR: $JAVA is not executable."
  473. exit 1
  474. fi
  475. # shellcheck disable=SC2034
  476. JAVA_HEAP_MAX=-Xmx1g
  477. HADOOP_HEAPSIZE=${HADOOP_HEAPSIZE:-1024}
  478. # check envvars which might override default args
  479. if [[ -n "$HADOOP_HEAPSIZE" ]]; then
  480. # shellcheck disable=SC2034
  481. JAVA_HEAP_MAX="-Xmx${HADOOP_HEAPSIZE}m"
  482. fi
  483. }
  484. function hadoop_finalize_libpaths
  485. {
  486. if [[ -n "${JAVA_LIBRARY_PATH}" ]]; then
  487. hadoop_add_param HADOOP_OPTS java.library.path \
  488. "-Djava.library.path=${JAVA_LIBRARY_PATH}"
  489. export LD_LIBRARY_PATH
  490. fi
  491. }
  492. #
  493. # fill in any last minute options that might not have been defined yet
  494. #
  495. function hadoop_finalize_hadoop_opts
  496. {
  497. hadoop_add_param HADOOP_OPTS hadoop.log.dir "-Dhadoop.log.dir=${HADOOP_LOG_DIR}"
  498. hadoop_add_param HADOOP_OPTS hadoop.log.file "-Dhadoop.log.file=${HADOOP_LOGFILE}"
  499. hadoop_add_param HADOOP_OPTS hadoop.home.dir "-Dhadoop.home.dir=${HADOOP_PREFIX}"
  500. hadoop_add_param HADOOP_OPTS hadoop.id.str "-Dhadoop.id.str=${HADOOP_IDENT_STRING}"
  501. hadoop_add_param HADOOP_OPTS hadoop.root.logger "-Dhadoop.root.logger=${HADOOP_ROOT_LOGGER}"
  502. hadoop_add_param HADOOP_OPTS hadoop.policy.file "-Dhadoop.policy.file=${HADOOP_POLICYFILE}"
  503. hadoop_add_param HADOOP_OPTS hadoop.security.logger "-Dhadoop.security.logger=${HADOOP_SECURITY_LOGGER}"
  504. }
  505. function hadoop_finalize_classpath
  506. {
  507. hadoop_add_classpath "${HADOOP_CONF_DIR}" before
  508. # user classpath gets added at the last minute. this allows
  509. # override of CONF dirs and more
  510. hadoop_add_to_classpath_userpath
  511. }
  512. function hadoop_finalize
  513. {
  514. # user classpath gets added at the last minute. this allows
  515. # override of CONF dirs and more
  516. hadoop_finalize_classpath
  517. hadoop_finalize_libpaths
  518. hadoop_finalize_hadoop_opts
  519. }
  520. function hadoop_exit_with_usage
  521. {
  522. # NOTE: This function is not user replaceable.
  523. local exitcode=$1
  524. if [[ -z $exitcode ]]; then
  525. exitcode=1
  526. fi
  527. if declare -F hadoop_usage >/dev/null ; then
  528. hadoop_usage
  529. elif [[ -x /usr/bin/cowsay ]]; then
  530. /usr/bin/cowsay -f elephant "Sorry, no help available."
  531. else
  532. hadoop_error "Sorry, no help available."
  533. fi
  534. exit $exitcode
  535. }
  536. function hadoop_verify_secure_prereq
  537. {
  538. # if you are on an OS like Illumos that has functional roles
  539. # and you are using pfexec, you'll probably want to change
  540. # this.
  541. # ${EUID} comes from the shell itself!
  542. if [[ "${EUID}" -ne 0 ]] || [[ -n "${HADOOP_SECURE_COMMAND}" ]]; then
  543. hadoop_error "ERROR: You must be a privileged in order to run a secure serice."
  544. return 1
  545. else
  546. return 0
  547. fi
  548. }
  549. function hadoop_setup_secure_service
  550. {
  551. # need a more complicated setup? replace me!
  552. HADOOP_PID_DIR=${HADOOP_SECURE_PID_DIR}
  553. HADOOP_LOG_DIR=${HADOOP_SECURE_LOG_DIR}
  554. }
  555. function hadoop_verify_piddir
  556. {
  557. if [[ -z "${HADOOP_PID_DIR}" ]]; then
  558. hadoop_error "No pid directory defined."
  559. exit 1
  560. fi
  561. if [[ ! -w "${HADOOP_PID_DIR}" ]] && [[ ! -d "${HADOOP_PID_DIR}" ]]; then
  562. hadoop_error "WARNING: ${HADOOP_PID_DIR} does not exist. Creating."
  563. mkdir -p "${HADOOP_PID_DIR}" > /dev/null 2>&1
  564. if [[ $? -gt 0 ]]; then
  565. hadoop_error "ERROR: Unable to create ${HADOOP_PID_DIR}. Aborting."
  566. exit 1
  567. fi
  568. fi
  569. touch "${HADOOP_PID_DIR}/$$" >/dev/null 2>&1
  570. if [[ $? -gt 0 ]]; then
  571. hadoop_error "ERROR: Unable to write in ${HADOOP_PID_DIR}. Aborting."
  572. exit 1
  573. fi
  574. rm "${HADOOP_PID_DIR}/$$" >/dev/null 2>&1
  575. }
  576. function hadoop_verify_logdir
  577. {
  578. if [[ -z "${HADOOP_LOG_DIR}" ]]; then
  579. hadoop_error "No log directory defined."
  580. exit 1
  581. fi
  582. if [[ ! -w "${HADOOP_LOG_DIR}" ]] && [[ ! -d "${HADOOP_LOG_DIR}" ]]; then
  583. hadoop_error "WARNING: ${HADOOP_LOG_DIR} does not exist. Creating."
  584. mkdir -p "${HADOOP_LOG_DIR}" > /dev/null 2>&1
  585. if [[ $? -gt 0 ]]; then
  586. hadoop_error "ERROR: Unable to create ${HADOOP_LOG_DIR}. Aborting."
  587. exit 1
  588. fi
  589. fi
  590. touch "${HADOOP_LOG_DIR}/$$" >/dev/null 2>&1
  591. if [[ $? -gt 0 ]]; then
  592. hadoop_error "ERROR: Unable to write in ${HADOOP_LOG_DIR}. Aborting."
  593. exit 1
  594. fi
  595. rm "${HADOOP_LOG_DIR}/$$" >/dev/null 2>&1
  596. }
  597. function hadoop_status_daemon() {
  598. #
  599. # LSB 4.1.0 compatible status command (1)
  600. #
  601. # 0 = program is running
  602. # 1 = dead, but still a pid (2)
  603. # 2 = (not used by us)
  604. # 3 = not running
  605. #
  606. # 1 - this is not an endorsement of the LSB
  607. #
  608. # 2 - technically, the specification says /var/run/pid, so
  609. # we should never return this value, but we're giving
  610. # them the benefit of a doubt and returning 1 even if
  611. # our pid is not in in /var/run .
  612. #
  613. local pidfile=$1
  614. shift
  615. local pid
  616. if [[ -f "${pidfile}" ]]; then
  617. pid=$(cat "${pidfile}")
  618. if ps -p "${pid}" > /dev/null 2>&1; then
  619. return 0
  620. fi
  621. return 1
  622. fi
  623. return 3
  624. }
  625. function hadoop_java_exec
  626. {
  627. # run a java command. this is used for
  628. # non-daemons
  629. local command=$1
  630. local class=$2
  631. shift 2
  632. #shellcheck disable=SC2086
  633. exec "${JAVA}" "-Dproc_${command}" ${HADOOP_OPTS} "${class}" "$@"
  634. }
  635. function hadoop_start_daemon
  636. {
  637. # this is our non-privileged daemon starter
  638. # that fires up a daemon in the *foreground*
  639. # so complex! so wow! much java!
  640. local command=$1
  641. local class=$2
  642. shift 2
  643. #shellcheck disable=SC2086
  644. exec "${JAVA}" "-Dproc_${command}" ${HADOOP_OPTS} "${class}" "$@"
  645. }
  646. function hadoop_start_daemon_wrapper
  647. {
  648. # this is our non-privileged daemon start
  649. # that fires up a daemon in the *background*
  650. local daemonname=$1
  651. local class=$2
  652. local pidfile=$3
  653. local outfile=$4
  654. shift 4
  655. hadoop_rotate_log "${outfile}"
  656. hadoop_start_daemon "${daemonname}" \
  657. "$class" "$@" >> "${outfile}" 2>&1 < /dev/null &
  658. #shellcheck disable=SC2086
  659. echo $! > "${pidfile}" 2>/dev/null
  660. if [[ $? -gt 0 ]]; then
  661. hadoop_error "ERROR: Cannot write pid ${pidfile}."
  662. fi
  663. # shellcheck disable=SC2086
  664. renice "${HADOOP_NICENESS}" $! >/dev/null 2>&1
  665. if [[ $? -gt 0 ]]; then
  666. hadoop_error "ERROR: Cannot set priority of process $!"
  667. fi
  668. # shellcheck disable=SC2086
  669. disown $! 2>&1
  670. if [[ $? -gt 0 ]]; then
  671. hadoop_error "ERROR: Cannot disconnect process $!"
  672. fi
  673. sleep 1
  674. # capture the ulimit output
  675. ulimit -a >> "${outfile}" 2>&1
  676. # shellcheck disable=SC2086
  677. if ! ps -p $! >/dev/null 2>&1; then
  678. return 1
  679. fi
  680. return 0
  681. }
  682. function hadoop_start_secure_daemon
  683. {
  684. # this is used to launch a secure daemon in the *foreground*
  685. #
  686. local daemonname=$1
  687. local class=$2
  688. # pid file to create for our deamon
  689. local daemonpidfile=$3
  690. # where to send stdout. jsvc has bad habits so this *may* be &1
  691. # which means you send it to stdout!
  692. local daemonoutfile=$4
  693. # where to send stderr. same thing, except &2 = stderr
  694. local daemonerrfile=$5
  695. shift 5
  696. hadoop_rotate_log "${daemonoutfile}"
  697. hadoop_rotate_log "${daemonerrfile}"
  698. jsvc="${JSVC_HOME}/jsvc"
  699. if [[ ! -f "${jsvc}" ]]; then
  700. hadoop_error "JSVC_HOME is not set or set incorrectly. jsvc is required to run secure"
  701. hadoop_error "or privileged daemons. Please download and install jsvc from "
  702. hadoop_error "http://archive.apache.org/dist/commons/daemon/binaries/ "
  703. hadoop_error "and set JSVC_HOME to the directory containing the jsvc binary."
  704. exit 1
  705. fi
  706. # note that shellcheck will throw a
  707. # bogus for-our-use-case 2086 here.
  708. # it doesn't properly support multi-line situations
  709. exec "${jsvc}" \
  710. "-Dproc_${daemonname}" \
  711. -outfile "${daemonoutfile}" \
  712. -errfile "${daemonerrfile}" \
  713. -pidfile "${daemonpidfile}" \
  714. -nodetach \
  715. -user "${HADOOP_SECURE_USER}" \
  716. -cp "${CLASSPATH}" \
  717. ${HADOOP_OPTS} \
  718. "${class}" "$@"
  719. }
  720. function hadoop_start_secure_daemon_wrapper
  721. {
  722. # this wraps hadoop_start_secure_daemon to take care
  723. # of the dirty work to launch a daemon in the background!
  724. local daemonname=$1
  725. local class=$2
  726. # same rules as hadoop_start_secure_daemon except we
  727. # have some additional parameters
  728. local daemonpidfile=$3
  729. local daemonoutfile=$4
  730. # the pid file of the subprocess that spawned our
  731. # secure launcher
  732. local jsvcpidfile=$5
  733. # the output of the subprocess that spawned our secure
  734. # launcher
  735. local jsvcoutfile=$6
  736. local daemonerrfile=$7
  737. shift 7
  738. hadoop_rotate_log "${jsvcoutfile}"
  739. hadoop_start_secure_daemon \
  740. "${daemonname}" \
  741. "${class}" \
  742. "${daemonpidfile}" \
  743. "${daemonoutfile}" \
  744. "${daemonerrfile}" "$@" >> "${jsvcoutfile}" 2>&1 < /dev/null &
  745. # This wrapper should only have one child. Unlike Shawty Lo.
  746. #shellcheck disable=SC2086
  747. echo $! > "${jsvcpidfile}" 2>/dev/null
  748. if [[ $? -gt 0 ]]; then
  749. hadoop_error "ERROR: Cannot write pid ${pidfile}."
  750. fi
  751. sleep 1
  752. #shellcheck disable=SC2086
  753. renice "${HADOOP_NICENESS}" $! >/dev/null 2>&1
  754. if [[ $? -gt 0 ]]; then
  755. hadoop_error "ERROR: Cannot set priority of process $!"
  756. fi
  757. if [[ -f "${daemonpidfile}" ]]; then
  758. #shellcheck disable=SC2046
  759. renice "${HADOOP_NICENESS}" $(cat "${daemonpidfile}") >/dev/null 2>&1
  760. if [[ $? -gt 0 ]]; then
  761. hadoop_error "ERROR: Cannot set priority of process $(cat "${daemonpidfile}")"
  762. fi
  763. fi
  764. #shellcheck disable=SC2086
  765. disown $! 2>&1
  766. if [[ $? -gt 0 ]]; then
  767. hadoop_error "ERROR: Cannot disconnect process $!"
  768. fi
  769. # capture the ulimit output
  770. su "${HADOOP_SECURE_USER}" -c 'bash -c "ulimit -a"' >> "${jsvcoutfile}" 2>&1
  771. #shellcheck disable=SC2086
  772. if ! ps -p $! >/dev/null 2>&1; then
  773. return 1
  774. fi
  775. return 0
  776. }
  777. function hadoop_stop_daemon
  778. {
  779. local cmd=$1
  780. local pidfile=$2
  781. shift 2
  782. local pid
  783. if [[ -f "${pidfile}" ]]; then
  784. pid=$(cat "$pidfile")
  785. kill "${pid}" >/dev/null 2>&1
  786. sleep "${HADOOP_STOP_TIMEOUT}"
  787. if kill -0 "${pid}" > /dev/null 2>&1; then
  788. hadoop_error "WARNING: ${cmd} did not stop gracefully after ${HADOOP_STOP_TIMEOUT} seconds: Trying to kill with kill -9"
  789. kill -9 "${pid}" >/dev/null 2>&1
  790. fi
  791. if ps -p "${pid}" > /dev/null 2>&1; then
  792. hadoop_error "ERROR: Unable to kill ${pid}"
  793. else
  794. rm -f "${pidfile}" >/dev/null 2>&1
  795. fi
  796. fi
  797. }
  798. function hadoop_stop_secure_daemon
  799. {
  800. local command=$1
  801. local daemonpidfile=$2
  802. local privpidfile=$3
  803. shift 3
  804. local ret
  805. hadoop_stop_daemon "${command}" "${daemonpidfile}"
  806. ret=$?
  807. rm -f "${daemonpidfile}" "${privpidfile}" 2>/dev/null
  808. return ${ret}
  809. }
  810. function hadoop_daemon_handler
  811. {
  812. local daemonmode=$1
  813. local daemonname=$2
  814. local class=$3
  815. local pidfile=$4
  816. local outfile=$5
  817. shift 5
  818. case ${daemonmode} in
  819. status)
  820. hadoop_status_daemon "${daemon_pidfile}"
  821. exit $?
  822. ;;
  823. stop)
  824. hadoop_stop_daemon "${daemonname}" "${daemon_pidfile}"
  825. exit $?
  826. ;;
  827. ##COMPAT -- older hadoops would also start daemons by default
  828. start|default)
  829. hadoop_verify_piddir
  830. hadoop_verify_logdir
  831. hadoop_status_daemon "${daemon_pidfile}"
  832. if [[ $? == 0 ]]; then
  833. hadoop_error "${daemonname} running as process $(cat "${daemon_pidfile}"). Stop it first."
  834. exit 1
  835. else
  836. # stale pid file, so just remove it and continue on
  837. rm -f "${daemon_pidfile}" >/dev/null 2>&1
  838. fi
  839. ##COMPAT - differenticate between --daemon start and nothing
  840. # "nothing" shouldn't detach
  841. if [[ "$daemonmode" = "default" ]]; then
  842. hadoop_start_daemon "${daemonname}" "${class}" "$@"
  843. else
  844. hadoop_start_daemon_wrapper "${daemonname}" \
  845. "${class}" "${daemon_pidfile}" "${daemon_outfile}" "$@"
  846. fi
  847. ;;
  848. esac
  849. }
  850. function hadoop_secure_daemon_handler
  851. {
  852. local daemonmode=$1
  853. local daemonname=$2
  854. local classname=$3
  855. local daemon_pidfile=$4
  856. local daemon_outfile=$5
  857. local priv_pidfile=$6
  858. local priv_outfile=$7
  859. local priv_errfile=$8
  860. shift 8
  861. case ${daemonmode} in
  862. status)
  863. hadoop_status_daemon "${daemon_pidfile}"
  864. exit $?
  865. ;;
  866. stop)
  867. hadoop_stop_secure_daemon "${daemonname}" \
  868. "${daemon_pidfile}" "${priv_pidfile}"
  869. exit $?
  870. ;;
  871. ##COMPAT -- older hadoops would also start daemons by default
  872. start|default)
  873. hadoop_verify_piddir
  874. hadoop_verify_logdir
  875. hadoop_status_daemon "${daemon_pidfile}"
  876. if [[ $? == 0 ]]; then
  877. hadoop_error "${daemonname} running as process $(cat "${daemon_pidfile}"). Stop it first."
  878. exit 1
  879. else
  880. # stale pid file, so just remove it and continue on
  881. rm -f "${daemon_pidfile}" >/dev/null 2>&1
  882. fi
  883. ##COMPAT - differenticate between --daemon start and nothing
  884. # "nothing" shouldn't detach
  885. if [[ "${daemonmode}" = "default" ]]; then
  886. hadoop_start_secure_daemon "${daemonname}" "${classname}" \
  887. "${daemon_pidfile}" "${daemon_outfile}" \
  888. "${priv_errfile}" "$@"
  889. else
  890. hadoop_start_secure_daemon_wrapper "${daemonname}" "${classname}" \
  891. "${daemon_pidfile}" "${daemon_outfile}" \
  892. "${priv_pidfile}" "${priv_outfile}" "${priv_errfile}" "$@"
  893. fi
  894. ;;
  895. esac
  896. }