upload-fsimage.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env 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. if [ $# -lt 2 ] || [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
  17. echo "Usage: ./upload-fsimage.sh image-txid remote-dir [ name-dir ]"
  18. echo " Takes the steps necessary to place all necessary components for"
  19. echo " the fsimage with the specified transaction ID (leading 0s not"
  20. echo " required) onto HDFS. This includes the fsimage file itself,"
  21. echo " the MD5 hash, the VERSION file, and the XML version of the"
  22. echo " fsimage file, which will be temporarily generated by this script"
  23. echo " (into \$TMPDIR if specified, else /tmp)."
  24. echo " These files will be uploaded to remote_dir (created if it does"
  25. echo " not yet exist). This is the same directory that should be passed"
  26. echo " to the Client as fs_image_dir."
  27. echo " If name-dir is specified, looks for fsimage files under"
  28. echo " \${name-dir}/current. Otherwise, looks in the current directory."
  29. exit 1
  30. fi
  31. image_txid="$1"
  32. remote_dir="$2"
  33. if [[ $# -ge 3 ]]; then
  34. name_dir="$3/current"
  35. else
  36. name_dir="$(pwd)"
  37. fi
  38. image_file_count="$(find -H "${name_dir}" -maxdepth 1 -mindepth 1 -name "fsimage_*$image_txid" -type f | wc -l)"
  39. if [[ "$image_file_count" != 1 ]]; then
  40. echo "Error; found $image_file_count matching fsimage files."
  41. exit 1
  42. fi
  43. image_file="$(find -H "${name_dir}" -maxdepth 1 -mindepth 1 -name "fsimage_*$image_txid" -type f)"
  44. image_file_name="$(basename "${image_file}")"
  45. echo "Using fsimage: $image_file_name"
  46. image_file_md5="${image_file}.md5"
  47. version_file="${name_dir}/VERSION"
  48. if ! [[ -f "$image_file_md5" ]]; then
  49. echo "Unable to find MD5 file for fsimage; looked at $image_file_md5"
  50. exit 1
  51. fi
  52. if ! [[ -f "$version_file" ]]; then
  53. echo "Unable to find VERSION file; looked at $version_file"
  54. exit 1
  55. fi
  56. if ! tmp_dir="$(mktemp -d)"; then
  57. echo "mktemp failed to make a temporary directory; exiting"
  58. exit 1
  59. fi
  60. image_file_xml="${tmp_dir}/${image_file_name}.xml"
  61. echo "Creating temporary XML fsimage file at $image_file_xml"
  62. hdfs oiv -p XML -i "$image_file" -o "$image_file_xml"
  63. echo "Created temporary XML fsimage file"
  64. if ! hdfs dfs -mkdir -p "$remote_dir"; then
  65. echo "Unable to create ${remote_dir}; exiting"
  66. rm -rf "${tmp_dir}"
  67. exit 1
  68. fi
  69. for file in ${image_file} ${image_file_xml} ${image_file_md5} ${version_file}; do
  70. echo "Uploading ${file}..."
  71. if ! hdfs dfs -copyFromLocal -f "$file" "${remote_dir}"; then
  72. echo "Error while uploading ${file}; exiting"
  73. rm -rf "${tmp_dir}"
  74. exit 1
  75. fi
  76. done
  77. rm -rf "${tmp_dir}"
  78. echo "Complete!"