hdfs.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #Licensed to the Apache Software Foundation (ASF) under one
  2. #or more contributor license agreements. See the NOTICE file
  3. #distributed with this work for additional information
  4. #regarding copyright ownership. The ASF licenses this file
  5. #to you under the Apache License, Version 2.0 (the
  6. #"License"); you may not use this file except in compliance
  7. #with the License. You may obtain a copy of the License at
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #Unless required by applicable law or agreed to in writing, software
  10. #distributed under the License is distributed on an "AS IS" BASIS,
  11. #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. #See the License for the specific language governing permissions and
  13. #limitations under the License.
  14. """define Hdfs as subclass of Service"""
  15. # -*- python -*-
  16. import os
  17. from service import *
  18. from hodlib.Hod.nodePool import *
  19. from hodlib.Common.desc import CommandDesc
  20. from hodlib.Common.util import get_exception_string, parseEquals
  21. class HdfsExternal(MasterSlave):
  22. """dummy proxy to external HDFS instance"""
  23. def __init__(self, serviceDesc, workDirs, version):
  24. MasterSlave.__init__(self, serviceDesc, workDirs,None)
  25. self.launchedMaster = True
  26. self.masterInitialized = True
  27. self.version = version
  28. def getMasterRequest(self):
  29. return None
  30. def getMasterCommands(self, serviceDict):
  31. return []
  32. def getAdminCommands(self, serviceDict):
  33. return []
  34. def getWorkerCommands(self, serviceDict):
  35. return []
  36. def getMasterAddrs(self):
  37. attrs = self.serviceDesc.getfinalAttrs()
  38. addr = attrs['fs.default.name']
  39. return [addr]
  40. def setMasterParams(self, dict):
  41. self.serviceDesc.dict['final-attrs']['fs.default.name'] = "%s:%s" % \
  42. (dict['host'], dict['fs_port'])
  43. if self.version < 16:
  44. self.serviceDesc.dict['final-attrs']['dfs.info.port'] = \
  45. str(self.serviceDesc.dict['info_port'])
  46. else:
  47. # After Hadoop-2185
  48. self.serviceDesc.dict['final-attrs']['dfs.http.address'] = "%s:%s" % \
  49. (dict['host'], dict['info_port'])
  50. def getInfoAddrs(self):
  51. attrs = self.serviceDesc.getfinalAttrs()
  52. if self.version < 16:
  53. addr = attrs['fs.default.name']
  54. k,v = addr.split( ":")
  55. infoaddr = k + ':' + attrs['dfs.info.port']
  56. else:
  57. # After Hadoop-2185
  58. infoaddr = attrs['dfs.http.address']
  59. return [infoaddr]
  60. class Hdfs(MasterSlave):
  61. def __init__(self, serviceDesc, nodePool, required_node, version, \
  62. format=True, upgrade=False):
  63. MasterSlave.__init__(self, serviceDesc, nodePool, required_node)
  64. self.masterNode = None
  65. self.masterAddr = None
  66. self.runAdminCommands = True
  67. self.infoAddr = None
  68. self._isLost = False
  69. self.format = format
  70. self.upgrade = upgrade
  71. self.workers = []
  72. self.version = version
  73. def getMasterRequest(self):
  74. req = NodeRequest(1, [], False)
  75. return req
  76. def getMasterCommands(self, serviceDict):
  77. masterCommands = []
  78. if self.format:
  79. masterCommands.append(self._getNameNodeCommand(True))
  80. if self.upgrade:
  81. masterCommands.append(self._getNameNodeCommand(False, True))
  82. else:
  83. masterCommands.append(self._getNameNodeCommand(False))
  84. return masterCommands
  85. def getAdminCommands(self, serviceDict):
  86. adminCommands = []
  87. if self.upgrade and self.runAdminCommands:
  88. adminCommands.append(self._getNameNodeAdminCommand('-safemode wait'))
  89. adminCommands.append(self._getNameNodeAdminCommand('-finalizeUpgrade',
  90. True, True))
  91. self.runAdminCommands = False
  92. return adminCommands
  93. def getWorkerCommands(self, serviceDict):
  94. cmdDesc = self._getDataNodeCommand()
  95. return [cmdDesc]
  96. def setMasterNodes(self, list):
  97. node = list[0]
  98. self.masterNode = node
  99. def getMasterAddrs(self):
  100. return [self.masterAddr]
  101. def getInfoAddrs(self):
  102. return [self.infoAddr]
  103. def getWorkers(self):
  104. return self.workers
  105. def setMasterParams(self, list):
  106. dict = self._parseEquals(list)
  107. self.masterAddr = dict['fs.default.name']
  108. k,v = self.masterAddr.split( ":")
  109. self.masterNode = k
  110. if self.version < 16:
  111. self.infoAddr = self.masterNode + ':' + dict['dfs.info.port']
  112. else:
  113. # After Hadoop-2185
  114. self.infoAddr = dict['dfs.http.address']
  115. def _parseEquals(self, list):
  116. return parseEquals(list)
  117. def _setWorkDirs(self, workDirs, envs, attrs, parentDirs, subDir):
  118. namedir = None
  119. hadooptmpdir = None
  120. datadir = []
  121. for p in parentDirs:
  122. workDirs.append(p)
  123. workDirs.append(os.path.join(p, subDir))
  124. dir = os.path.join(p, subDir, 'dfs-data')
  125. datadir.append(dir)
  126. if not hadooptmpdir:
  127. # Not used currently, generating hadooptmpdir just in case
  128. hadooptmpdir = os.path.join(p, subDir, 'hadoop-tmp')
  129. if not namedir:
  130. namedir = os.path.join(p, subDir, 'dfs-name')
  131. workDirs.append(namedir)
  132. workDirs.extend(datadir)
  133. # FIXME!! use csv
  134. attrs['dfs.name.dir'] = namedir
  135. attrs['hadoop.tmp.dir'] = hadooptmpdir
  136. attrs['dfs.data.dir'] = ','.join(datadir)
  137. # FIXME -- change dfs.client.buffer.dir
  138. envs['HADOOP_ROOT_LOGGER'] = "INFO,DRFA"
  139. def _getNameNodeCommand(self, format=False, upgrade=False):
  140. sd = self.serviceDesc
  141. parentDirs = self.workDirs
  142. workDirs = []
  143. attrs = sd.getfinalAttrs().copy()
  144. envs = sd.getEnvs().copy()
  145. if 'fs.default.name' not in attrs:
  146. attrs['fs.default.name'] = 'fillinhostport'
  147. if self.version < 16:
  148. if 'dfs.info.port' not in attrs:
  149. attrs['dfs.info.port'] = 'fillinport'
  150. else:
  151. # Addressing Hadoop-2185, added the following. Earlier versions don't
  152. # care about this
  153. if 'dfs.http.address' not in attrs:
  154. attrs['dfs.http.address'] = 'fillinhostport'
  155. self._setWorkDirs(workDirs, envs, attrs, parentDirs, 'hdfs-nn')
  156. dict = { 'name' : 'namenode' }
  157. dict['program'] = os.path.join('bin', 'hadoop')
  158. argv = ['namenode']
  159. if format:
  160. argv.append('-format')
  161. elif upgrade:
  162. argv.append('-upgrade')
  163. dict['argv'] = argv
  164. dict['envs'] = envs
  165. dict['pkgdirs'] = sd.getPkgDirs()
  166. dict['workdirs'] = workDirs
  167. dict['final-attrs'] = attrs
  168. dict['attrs'] = sd.getAttrs()
  169. if format:
  170. dict['fg'] = 'true'
  171. dict['stdin'] = 'Y'
  172. cmd = CommandDesc(dict)
  173. return cmd
  174. def _getNameNodeAdminCommand(self, adminCommand, wait=True, ignoreFailures=False):
  175. sd = self.serviceDesc
  176. parentDirs = self.workDirs
  177. workDirs = []
  178. attrs = sd.getfinalAttrs().copy()
  179. envs = sd.getEnvs().copy()
  180. nn = self.masterAddr
  181. if nn == None:
  182. raise ValueError, "Can't get namenode address"
  183. attrs['fs.default.name'] = nn
  184. self._setWorkDirs(workDirs, envs, attrs, parentDirs, 'hdfs-nn')
  185. dict = { 'name' : 'dfsadmin' }
  186. dict['program'] = os.path.join('bin', 'hadoop')
  187. argv = ['dfsadmin']
  188. argv.append(adminCommand)
  189. dict['argv'] = argv
  190. dict['envs'] = envs
  191. dict['pkgdirs'] = sd.getPkgDirs()
  192. dict['workdirs'] = workDirs
  193. dict['final-attrs'] = attrs
  194. dict['attrs'] = sd.getAttrs()
  195. if wait:
  196. dict['fg'] = 'true'
  197. dict['stdin'] = 'Y'
  198. if ignoreFailures:
  199. dict['ignorefailures'] = 'Y'
  200. cmd = CommandDesc(dict)
  201. return cmd
  202. def _getDataNodeCommand(self):
  203. sd = self.serviceDesc
  204. parentDirs = self.workDirs
  205. workDirs = []
  206. attrs = sd.getfinalAttrs().copy()
  207. envs = sd.getEnvs().copy()
  208. nn = self.masterAddr
  209. if nn == None:
  210. raise ValueError, "Can't get namenode address"
  211. attrs['fs.default.name'] = nn
  212. if self.version < 16:
  213. if 'dfs.datanode.port' not in attrs:
  214. attrs['dfs.datanode.port'] = 'fillinport'
  215. if 'dfs.datanode.info.port' not in attrs:
  216. attrs['dfs.datanode.info.port'] = 'fillinport'
  217. else:
  218. # Adding the following. Hadoop-2185
  219. if 'dfs.datanode.address' not in attrs:
  220. attrs['dfs.datanode.address'] = 'fillinhostport'
  221. if 'dfs.datanode.http.address' not in attrs:
  222. attrs['dfs.datanode.http.address'] = 'fillinhostport'
  223. if self.version >= 18:
  224. # After HADOOP-3283
  225. # TODO: check for major as well as minor versions
  226. attrs['dfs.datanode.ipc.address'] = 'fillinhostport'
  227. self._setWorkDirs(workDirs, envs, attrs, parentDirs, 'hdfs-dn')
  228. dict = { 'name' : 'datanode' }
  229. dict['program'] = os.path.join('bin', 'hadoop')
  230. dict['argv'] = ['datanode']
  231. dict['envs'] = envs
  232. dict['pkgdirs'] = sd.getPkgDirs()
  233. dict['workdirs'] = workDirs
  234. dict['final-attrs'] = attrs
  235. dict['attrs'] = sd.getAttrs()
  236. cmd = CommandDesc(dict)
  237. return cmd