namenode.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. setup_ranger_hdfs(env)
  44. def prepare_rolling_upgrade(self, env):
  45. namenode_upgrade.prepare_rolling_upgrade()
  46. def finalize_rolling_upgrade(self, env):
  47. namenode_upgrade.finalize_rolling_upgrade()
  48. def pre_rolling_restart(self, env):
  49. Logger.info("Executing Rolling Upgrade pre-restart")
  50. import params
  51. env.set_params(params)
  52. if params.version and compare_versions(format_hdp_stack_version(params.version), '2.2.0.0') >= 0:
  53. Execute(format("hdp-select set hadoop-hdfs-namenode {version}"))
  54. def start(self, env, rolling_restart=False):
  55. import params
  56. env.set_params(params)
  57. self.configure(env)
  58. setup_ranger_hdfs(env)
  59. namenode(action="start", rolling_restart=rolling_restart, env=env)
  60. self.save_component_version_to_structured_out(params.stack_name)
  61. def post_rolling_restart(self, env):
  62. Logger.info("Executing Rolling Upgrade post-restart")
  63. import params
  64. env.set_params(params)
  65. Execute("hdfs dfsadmin -report -live",
  66. user=params.hdfs_principal_name if params.security_enabled else params.hdfs_user
  67. )
  68. def stop(self, env, rolling_restart=False):
  69. import params
  70. env.set_params(params)
  71. if rolling_restart and params.dfs_ha_enabled:
  72. if params.dfs_ha_automatic_failover_enabled:
  73. failover_namenode()
  74. else:
  75. raise Fail("Rolling Upgrade - dfs.ha.automatic-failover.enabled must be enabled to perform a rolling restart")
  76. namenode(action="stop", rolling_restart=rolling_restart, env=env)
  77. def configure(self, env):
  78. import params
  79. env.set_params(params)
  80. hdfs()
  81. namenode(action="configure", env=env)
  82. pass
  83. def status(self, env):
  84. import status_params
  85. env.set_params(status_params)
  86. check_process_status(status_params.namenode_pid_file)
  87. pass
  88. def security_status(self, env):
  89. import status_params
  90. env.set_params(status_params)
  91. props_value_check = {"hadoop.security.authentication": "kerberos",
  92. "hadoop.security.authorization": "true"}
  93. props_empty_check = ["hadoop.security.auth_to_local"]
  94. props_read_check = None
  95. core_site_expectations = build_expectations('core-site', props_value_check, props_empty_check,
  96. props_read_check)
  97. props_value_check = None
  98. props_empty_check = ['dfs.namenode.kerberos.internal.spnego.principal',
  99. 'dfs.namenode.keytab.file',
  100. 'dfs.namenode.kerberos.principal']
  101. props_read_check = ['dfs.namenode.keytab.file']
  102. hdfs_site_expectations = build_expectations('hdfs-site', props_value_check, props_empty_check,
  103. props_read_check)
  104. hdfs_expectations = {}
  105. hdfs_expectations.update(core_site_expectations)
  106. hdfs_expectations.update(hdfs_site_expectations)
  107. security_params = get_params_from_filesystem(status_params.hadoop_conf_dir,
  108. {'core-site.xml': FILE_TYPE_XML,
  109. 'hdfs-site.xml': FILE_TYPE_XML})
  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. def decommission(self, env):
  138. import params
  139. env.set_params(params)
  140. namenode(action="decommission")
  141. pass
  142. def rebalancehdfs(self, env):
  143. import params
  144. env.set_params(params)
  145. name_node_parameters = json.loads( params.name_node_params )
  146. threshold = name_node_parameters['threshold']
  147. _print("Starting balancer with threshold = %s\n" % threshold)
  148. def calculateCompletePercent(first, current):
  149. return 1.0 - current.bytesLeftToMove/first.bytesLeftToMove
  150. def startRebalancingProcess(threshold):
  151. rebalanceCommand = format('hdfs --config {hadoop_conf_dir} balancer -threshold {threshold}')
  152. return as_user(rebalanceCommand, params.hdfs_user, env={'PATH': params.hadoop_bin_dir})
  153. command = startRebalancingProcess(threshold)
  154. basedir = os.path.join(env.config.basedir, 'scripts')
  155. if(threshold == 'DEBUG'): #FIXME TODO remove this on PROD
  156. basedir = os.path.join(env.config.basedir, 'scripts', 'balancer-emulator')
  157. command = ['python','hdfs-command.py']
  158. _print("Executing command %s\n" % command)
  159. parser = hdfs_rebalance.HdfsParser()
  160. def handle_new_line(line):
  161. _print('[balancer] %s' % (line))
  162. pl = parser.parseLine(line)
  163. if pl:
  164. res = pl.toJson()
  165. res['completePercent'] = calculateCompletePercent(parser.initialLine, pl)
  166. self.put_structured_out(res)
  167. elif parser.state == 'PROCESS_FINISED' :
  168. _print('[balancer] %s' % ('Process is finished' ))
  169. self.put_structured_out({'completePercent' : 1})
  170. return
  171. Execute(command,
  172. on_new_line = handle_new_line,
  173. logoutput = False,
  174. )
  175. def _print(line):
  176. sys.stdout.write(line)
  177. sys.stdout.flush()
  178. if __name__ == "__main__":
  179. NameNode().execute()