hdfs.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. workers_per_ring = 1):
  64. MasterSlave.__init__(self, serviceDesc, nodePool, required_node)
  65. self.masterNode = None
  66. self.masterAddr = None
  67. self.runAdminCommands = True
  68. self.infoAddr = None
  69. self._isLost = False
  70. self.format = format
  71. self.upgrade = upgrade
  72. self.workers = []
  73. self.version = version
  74. self.workers_per_ring = workers_per_ring
  75. def getMasterRequest(self):
  76. req = NodeRequest(1, [], False)
  77. return req
  78. def getMasterCommands(self, serviceDict):
  79. masterCommands = []
  80. if self.format:
  81. masterCommands.append(self._getNameNodeCommand(True))
  82. if self.upgrade:
  83. masterCommands.append(self._getNameNodeCommand(False, True))
  84. else:
  85. masterCommands.append(self._getNameNodeCommand(False))
  86. return masterCommands
  87. def getAdminCommands(self, serviceDict):
  88. adminCommands = []
  89. if self.upgrade and self.runAdminCommands:
  90. adminCommands.append(self._getNameNodeAdminCommand('-safemode wait'))
  91. adminCommands.append(self._getNameNodeAdminCommand('-finalizeUpgrade',
  92. True, True))
  93. self.runAdminCommands = False
  94. return adminCommands
  95. def getWorkerCommands(self, serviceDict):
  96. workerCmds = []
  97. for id in range(1, self.workers_per_ring + 1):
  98. workerCmds.append(self._getDataNodeCommand(str(id)))
  99. return workerCmds
  100. def setMasterNodes(self, list):
  101. node = list[0]
  102. self.masterNode = node
  103. def getMasterAddrs(self):
  104. return [self.masterAddr]
  105. def getInfoAddrs(self):
  106. return [self.infoAddr]
  107. def getWorkers(self):
  108. return self.workers
  109. def setMasterParams(self, list):
  110. dict = self._parseEquals(list)
  111. self.masterAddr = dict['fs.default.name']
  112. k,v = self.masterAddr.split( ":")
  113. self.masterNode = k
  114. if self.version < 16:
  115. self.infoAddr = self.masterNode + ':' + dict['dfs.info.port']
  116. else:
  117. # After Hadoop-2185
  118. self.infoAddr = dict['dfs.http.address']
  119. def _parseEquals(self, list):
  120. return parseEquals(list)
  121. def _setWorkDirs(self, workDirs, envs, attrs, parentDirs, subDir):
  122. namedir = None
  123. hadooptmpdir = None
  124. datadir = []
  125. for p in parentDirs:
  126. workDirs.append(p)
  127. workDirs.append(os.path.join(p, subDir))
  128. dir = os.path.join(p, subDir, 'dfs-data')
  129. datadir.append(dir)
  130. if not hadooptmpdir:
  131. # Not used currently, generating hadooptmpdir just in case
  132. hadooptmpdir = os.path.join(p, subDir, 'hadoop-tmp')
  133. if not namedir:
  134. namedir = os.path.join(p, subDir, 'dfs-name')
  135. workDirs.append(namedir)
  136. workDirs.extend(datadir)
  137. # FIXME!! use csv
  138. attrs['dfs.name.dir'] = namedir
  139. attrs['hadoop.tmp.dir'] = hadooptmpdir
  140. attrs['dfs.data.dir'] = ','.join(datadir)
  141. # FIXME -- change dfs.client.buffer.dir
  142. envs['HADOOP_ROOT_LOGGER'] = "INFO,DRFA"
  143. def _getNameNodeCommand(self, format=False, upgrade=False):
  144. sd = self.serviceDesc
  145. parentDirs = self.workDirs
  146. workDirs = []
  147. attrs = sd.getfinalAttrs().copy()
  148. envs = sd.getEnvs().copy()
  149. if 'fs.default.name' not in attrs:
  150. attrs['fs.default.name'] = 'fillinhostport'
  151. if self.version < 16:
  152. if 'dfs.info.port' not in attrs:
  153. attrs['dfs.info.port'] = 'fillinport'
  154. else:
  155. # Addressing Hadoop-2185, added the following. Earlier versions don't
  156. # care about this
  157. if 'dfs.http.address' not in attrs:
  158. attrs['dfs.http.address'] = 'fillinhostport'
  159. self._setWorkDirs(workDirs, envs, attrs, parentDirs, 'hdfs-nn')
  160. dict = { 'name' : 'namenode' }
  161. dict['program'] = os.path.join('bin', 'hadoop')
  162. argv = ['namenode']
  163. if format:
  164. argv.append('-format')
  165. elif upgrade:
  166. argv.append('-upgrade')
  167. dict['argv'] = argv
  168. dict['envs'] = envs
  169. dict['pkgdirs'] = sd.getPkgDirs()
  170. dict['workdirs'] = workDirs
  171. dict['final-attrs'] = attrs
  172. dict['attrs'] = sd.getAttrs()
  173. if format:
  174. dict['fg'] = 'true'
  175. dict['stdin'] = 'Y'
  176. cmd = CommandDesc(dict)
  177. return cmd
  178. def _getNameNodeAdminCommand(self, adminCommand, wait=True, ignoreFailures=False):
  179. sd = self.serviceDesc
  180. parentDirs = self.workDirs
  181. workDirs = []
  182. attrs = sd.getfinalAttrs().copy()
  183. envs = sd.getEnvs().copy()
  184. nn = self.masterAddr
  185. if nn == None:
  186. raise ValueError, "Can't get namenode address"
  187. attrs['fs.default.name'] = nn
  188. self._setWorkDirs(workDirs, envs, attrs, parentDirs, 'hdfs-nn')
  189. dict = { 'name' : 'dfsadmin' }
  190. dict['program'] = os.path.join('bin', 'hadoop')
  191. argv = ['dfsadmin']
  192. argv.append(adminCommand)
  193. dict['argv'] = argv
  194. dict['envs'] = envs
  195. dict['pkgdirs'] = sd.getPkgDirs()
  196. dict['workdirs'] = workDirs
  197. dict['final-attrs'] = attrs
  198. dict['attrs'] = sd.getAttrs()
  199. if wait:
  200. dict['fg'] = 'true'
  201. dict['stdin'] = 'Y'
  202. if ignoreFailures:
  203. dict['ignorefailures'] = 'Y'
  204. cmd = CommandDesc(dict)
  205. return cmd
  206. def _getDataNodeCommand(self, id):
  207. sd = self.serviceDesc
  208. parentDirs = self.workDirs
  209. workDirs = []
  210. attrs = sd.getfinalAttrs().copy()
  211. envs = sd.getEnvs().copy()
  212. nn = self.masterAddr
  213. if nn == None:
  214. raise ValueError, "Can't get namenode address"
  215. attrs['fs.default.name'] = nn
  216. if self.version < 16:
  217. if 'dfs.datanode.port' not in attrs:
  218. attrs['dfs.datanode.port'] = 'fillinport'
  219. if 'dfs.datanode.info.port' not in attrs:
  220. attrs['dfs.datanode.info.port'] = 'fillinport'
  221. else:
  222. # Adding the following. Hadoop-2185
  223. if 'dfs.datanode.address' not in attrs:
  224. attrs['dfs.datanode.address'] = 'fillinhostport'
  225. if 'dfs.datanode.http.address' not in attrs:
  226. attrs['dfs.datanode.http.address'] = 'fillinhostport'
  227. if self.version >= 18:
  228. # After HADOOP-3283
  229. # TODO: check for major as well as minor versions
  230. attrs['dfs.datanode.ipc.address'] = 'fillinhostport'
  231. # unique workdirs in case of multiple datanodes per hodring
  232. pd = []
  233. for dir in parentDirs:
  234. dir = dir + "-" + id
  235. pd.append(dir)
  236. parentDirs = pd
  237. # end of unique workdirs
  238. self._setWorkDirs(workDirs, envs, attrs, parentDirs, 'hdfs-dn')
  239. dict = { 'name' : 'datanode' }
  240. dict['program'] = os.path.join('bin', 'hadoop')
  241. dict['argv'] = ['datanode']
  242. dict['envs'] = envs
  243. dict['pkgdirs'] = sd.getPkgDirs()
  244. dict['workdirs'] = workDirs
  245. dict['final-attrs'] = attrs
  246. dict['attrs'] = sd.getAttrs()
  247. cmd = CommandDesc(dict)
  248. return cmd