namenode.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. """
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. """
  16. import sys
  17. import os
  18. import json
  19. from resource_management import *
  20. from resource_management.libraries.functions.security_commons import build_expectations, \
  21. cached_kinit_executor, get_params_from_filesystem, validate_security_config_properties, \
  22. FILE_TYPE_XML
  23. from resource_management.libraries.functions.version import compare_versions, \
  24. format_hdp_stack_version
  25. from resource_management.libraries.functions.format import format
  26. from resource_management.libraries.functions.check_process_status import check_process_status
  27. from resource_management.core.exceptions import Fail
  28. import namenode_upgrade
  29. from hdfs_namenode import namenode
  30. from hdfs import hdfs
  31. import hdfs_rebalance
  32. from utils import failover_namenode
  33. from setup_ranger_hdfs import setup_ranger_hdfs
  34. class NameNode(Script):
  35. def get_stack_to_component(self):
  36. return {"HDP": "hadoop-hdfs-namenode"}
  37. def install(self, env):
  38. import params
  39. self.install_packages(env, params.exclude_packages)
  40. env.set_params(params)
  41. #TODO we need this for HA because of manual steps
  42. self.configure(env)
  43. def prepare_rolling_upgrade(self, env):
  44. namenode_upgrade.prepare_rolling_upgrade()
  45. def finalize_rolling_upgrade(self, env):
  46. namenode_upgrade.finalize_rolling_upgrade()
  47. def pre_rolling_restart(self, env):
  48. Logger.info("Executing Rolling Upgrade pre-restart")
  49. import params
  50. env.set_params(params)
  51. if params.version and compare_versions(format_hdp_stack_version(params.version), '2.2.0.0') >= 0:
  52. Execute(format("hdp-select set hadoop-hdfs-namenode {version}"))
  53. def start(self, env, rolling_restart=False):
  54. import params
  55. env.set_params(params)
  56. self.configure(env)
  57. setup_ranger_hdfs()
  58. namenode(action="start", rolling_restart=rolling_restart, env=env)
  59. def post_rolling_restart(self, env):
  60. Logger.info("Executing Rolling Upgrade post-restart")
  61. import params
  62. env.set_params(params)
  63. Execute("hdfs dfsadmin -report -live",
  64. user=params.hdfs_user
  65. )
  66. def stop(self, env, rolling_restart=False):
  67. import params
  68. env.set_params(params)
  69. if rolling_restart and params.dfs_ha_enabled:
  70. if params.dfs_ha_automatic_failover_enabled:
  71. failover_namenode()
  72. else:
  73. raise Fail("Rolling Upgrade - dfs.ha.automatic-failover.enabled must be enabled to perform a rolling restart")
  74. namenode(action="stop", rolling_restart=rolling_restart, env=env)
  75. def configure(self, env):
  76. import params
  77. env.set_params(params)
  78. hdfs()
  79. namenode(action="configure", env=env)
  80. pass
  81. def status(self, env):
  82. import status_params
  83. env.set_params(status_params)
  84. check_process_status(status_params.namenode_pid_file)
  85. pass
  86. def security_status(self, env):
  87. import status_params
  88. env.set_params(status_params)
  89. props_value_check = {"hadoop.security.authentication": "kerberos",
  90. "hadoop.security.authorization": "true"}
  91. props_empty_check = ["hadoop.security.auth_to_local"]
  92. props_read_check = None
  93. core_site_expectations = build_expectations('core-site', props_value_check, props_empty_check,
  94. props_read_check)
  95. props_value_check = None
  96. props_empty_check = ['dfs.namenode.kerberos.internal.spnego.principal',
  97. 'dfs.namenode.keytab.file',
  98. 'dfs.namenode.kerberos.principal']
  99. props_read_check = ['dfs.namenode.keytab.file']
  100. hdfs_site_expectations = build_expectations('hdfs-site', props_value_check, props_empty_check,
  101. props_read_check)
  102. hdfs_expectations = {}
  103. hdfs_expectations.update(core_site_expectations)
  104. hdfs_expectations.update(hdfs_site_expectations)
  105. security_params = get_params_from_filesystem(status_params.hadoop_conf_dir,
  106. {'core-site.xml': FILE_TYPE_XML,
  107. 'hdfs-site.xml': FILE_TYPE_XML})
  108. if 'core-site' in security_params and 'hadoop.security.authentication' in security_params['core-site'] and \
  109. security_params['core-site']['hadoop.security.authentication'].lower() == 'kerberos':
  110. result_issues = validate_security_config_properties(security_params, hdfs_expectations)
  111. if not result_issues: # If all validations passed successfully
  112. try:
  113. # Double check the dict before calling execute
  114. if ( 'hdfs-site' not in security_params
  115. or 'dfs.namenode.keytab.file' not in security_params['hdfs-site']
  116. or 'dfs.namenode.kerberos.principal' not in security_params['hdfs-site']):
  117. self.put_structured_out({"securityState": "UNSECURED"})
  118. self.put_structured_out(
  119. {"securityIssuesFound": "Keytab file or principal are not set property."})
  120. return
  121. cached_kinit_executor(status_params.kinit_path_local,
  122. status_params.hdfs_user,
  123. security_params['hdfs-site']['dfs.namenode.keytab.file'],
  124. security_params['hdfs-site']['dfs.namenode.kerberos.principal'],
  125. status_params.hostname,
  126. status_params.tmp_dir)
  127. self.put_structured_out({"securityState": "SECURED_KERBEROS"})
  128. except Exception as e:
  129. self.put_structured_out({"securityState": "ERROR"})
  130. self.put_structured_out({"securityStateErrorInfo": str(e)})
  131. else:
  132. issues = []
  133. for cf in result_issues:
  134. issues.append("Configuration file %s did not pass the validation. Reason: %s" % (cf, result_issues[cf]))
  135. self.put_structured_out({"securityIssuesFound": ". ".join(issues)})
  136. self.put_structured_out({"securityState": "UNSECURED"})
  137. else:
  138. self.put_structured_out({"securityState": "UNSECURED"})
  139. def decommission(self, env):
  140. import params
  141. env.set_params(params)
  142. namenode(action="decommission")
  143. pass
  144. def rebalancehdfs(self, env):
  145. import params
  146. env.set_params(params)
  147. name_node_parameters = json.loads( params.name_node_params )
  148. threshold = name_node_parameters['threshold']
  149. _print("Starting balancer with threshold = %s\n" % threshold)
  150. if params.security_enabled:
  151. Execute(format("{kinit_path_local} -kt {hdfs_user_keytab} {hdfs_principal_name}"),
  152. user = params.hdfs_user)
  153. def calculateCompletePercent(first, current):
  154. return 1.0 - current.bytesLeftToMove/first.bytesLeftToMove
  155. def startRebalancingProcess(threshold):
  156. rebalanceCommand = format('hdfs --config {hadoop_conf_dir} balancer -threshold {threshold}')
  157. return as_user(rebalanceCommand, params.hdfs_user, env={'PATH': params.hadoop_bin_dir})
  158. command = startRebalancingProcess(threshold)
  159. basedir = os.path.join(env.config.basedir, 'scripts')
  160. if(threshold == 'DEBUG'): #FIXME TODO remove this on PROD
  161. basedir = os.path.join(env.config.basedir, 'scripts', 'balancer-emulator')
  162. command = ['python','hdfs-command.py']
  163. _print("Executing command %s\n" % command)
  164. parser = hdfs_rebalance.HdfsParser()
  165. def handle_new_line(line):
  166. _print('[balancer] %s' % (line))
  167. pl = parser.parseLine(line)
  168. if pl:
  169. res = pl.toJson()
  170. res['completePercent'] = calculateCompletePercent(parser.initialLine, pl)
  171. self.put_structured_out(res)
  172. elif parser.state == 'PROCESS_FINISED' :
  173. _print('[balancer] %s' % ('Process is finished' ))
  174. self.put_structured_out({'completePercent' : 1})
  175. return
  176. Execute(command,
  177. on_new_line = handle_new_line,
  178. logoutput = False,
  179. )
  180. def _print(line):
  181. sys.stdout.write(line)
  182. sys.stdout.flush()
  183. if __name__ == "__main__":
  184. NameNode().execute()