component.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. # Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. #
  3. # Permission to use, copy, modify, and distribute this software for any
  4. # purpose with or without fee is hereby granted, provided that the above
  5. # copyright notice and this permission notice appear in all copies.
  6. #
  7. # THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
  8. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  9. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  10. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  12. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  14. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. from isc.log_messages.bind10_messages import *
  16. import time
  17. logger = isc.log.Logger("boss")
  18. """
  19. Module for managing components (abstraction of process). It allows starting
  20. them in given order, handling when they crash (what happens depends on kind
  21. of component) and shutting down. It also handles the configuration of this.
  22. Dependencies between them are not yet handled. It might turn out they are
  23. needed, in that case they will be added sometime in future.
  24. """
  25. class Component:
  26. """
  27. This represents a single component. It has some defaults of behaviour,
  28. which should be reasonable for majority of ordinary components, but
  29. it might be inherited and modified for special-purpose components,
  30. like the core modules with different ways of starting up.
  31. """
  32. def __init__(self, process, boss, kind):
  33. """
  34. Creates the component in not running mode.
  35. The parameters are:
  36. - `process` is the name of the process to start.
  37. - `boss` the boss object to plug into. The component needs to plug
  38. into it to know when it failed, etc.
  39. - `kind` is the kind of component. It may be one of:
  40. * 'core' means the system can't run without it and it can't be
  41. safely restarted. If it does not start, the system is brought
  42. down. If it crashes, the system is turned off as well (with
  43. non-zero exit status).
  44. * 'needed' means the system is able to restart the component,
  45. but it is vital part of the service (like auth server). If
  46. it fails to start or crashes in less than 10s after the first
  47. startup, the system is brought down. If it crashes later on,
  48. it is restarted.
  49. * 'dispensable' means the component should be running, but if it
  50. doesn't start or crashes for some reason, the system simply tries
  51. to restart it and keeps running.
  52. """
  53. if kind not in ['core', 'needed', 'dispensable']:
  54. raise ValueError('Component kind can not be ' + kind)
  55. self.__running = False
  56. # Dead like really dead. No resurrection possible.
  57. self.__dead = False
  58. self.__kind = kind
  59. self.__boss = boss
  60. def start(self):
  61. """
  62. Start the component for the first time or restart it. If you need to
  63. modify the way a component is started, do not replace this method,
  64. but start_internal. This one does some more bookkeeping around.
  65. If you try to start an already running component, it raises ValueError.
  66. """
  67. if self.__dead:
  68. raise ValueError("Can't resurrect already dead component")
  69. if self.running():
  70. raise ValueError("Can't start already running component")
  71. self.__running = True
  72. self.__start_time = time.time()
  73. try:
  74. self.start_internal()
  75. except:
  76. self.failed()
  77. raise
  78. def start_internal(self):
  79. """
  80. This method does the actual starting of a process. If you need to
  81. change the way the component is started, replace this method.
  82. """
  83. pass
  84. def stop(self):
  85. """
  86. Stop the component. If you need to modify the way a component is
  87. stopped, do not replace this method, but stop_internal. This one
  88. does some more bookkeeping.
  89. If you try to stop a component that is not running, it raises
  90. ValueError.
  91. """
  92. if not self.running():
  93. raise ValueError("Can't stop a component which is not running")
  94. self.stop_internal()
  95. self.__running = False
  96. def stop_internal(self):
  97. """
  98. This is the method that does the actual stopping of a component.
  99. You can replace this method if you want a different way to do it.
  100. """
  101. pass
  102. def failed(self):
  103. """
  104. Notify the component it crashed. This will be called from boss object.
  105. If you try to call failed on a component that is not running,
  106. a ValueError is raised.
  107. """
  108. if not self.running():
  109. raise ValueError("Can't fail component that isn't running")
  110. self.failed_internal()
  111. self.__running = False
  112. # If it is a core component or the needed component failed to start
  113. # (including it stopped really soon)
  114. if self.__kind == 'core' or \
  115. (self.__kind == 'needed' and time.time() - 10 < self.__start_time):
  116. self.__dead = True
  117. self.__boss.shutdown(1)
  118. # This means we want to restart
  119. else:
  120. self.start()
  121. def failed_internal(self):
  122. """
  123. This method is called from failed. You can replace it if you need
  124. some specific behaviour when the component crashes. The default
  125. implementation is empty.
  126. Do not raise exceptions from here, please. The propper shutdown
  127. would have not happened.
  128. """
  129. pass
  130. def running(self):
  131. """
  132. Informs if the component is currently running. It assumes the failed
  133. is called whenever the component really fails and there might be some
  134. time in between actual failure and the call.
  135. """
  136. return self.__running
  137. specials = {}
  138. """
  139. List of specially started components. Each one should be the class than can
  140. be created for that component.
  141. """
  142. class Configurator:
  143. """
  144. This thing keeps track of configuration changes and starts and stops
  145. components as it goes. It also handles the inital startup and final
  146. shutdown.
  147. Note that this will allow you to stop (by invoking reconfigure) a core
  148. component. There should be some kind of layer protecting users from ever
  149. doing so (users must not stop the config manager, message queue and stuff
  150. like that or the system won't start again).
  151. """
  152. def __init__(self, boss):
  153. """
  154. Initializes the configurator, but nothing is started yet.
  155. The boss parameter is the boss object used to start and stop processes.
  156. """
  157. self.__boss = boss
  158. # These could be __private, but as we access them from within unittest,
  159. # it's more comfortable to have them just _protected.
  160. self._components = {}
  161. self._old_config = {}
  162. self._running = False
  163. def startup(self, configuration):
  164. """
  165. Starts the first set of processes. This configuration is expected
  166. to be hardcoded from the boss itself to start the configuration
  167. manager and other similar things.
  168. """
  169. if self._running:
  170. raise ValueError("Trying to start the component configurator " +
  171. "twice")
  172. self._run_plan(self._build_plan({}, configuration))
  173. self._old_config = configuration
  174. self._running = True
  175. def shutdown(self):
  176. """
  177. Shuts everything down.
  178. """
  179. pass
  180. def reconfigure(configuration):
  181. """
  182. Changes configuration from the current one to the provided. It
  183. starts and stops all the components as needed.
  184. """
  185. pass
  186. def _build_plan(self, old, new):
  187. """
  188. Builds a plan how to transfer from the old configuration to the new
  189. one. It'll be sorted by priority and it will contain the components
  190. (already created, but not started). Each command in the plan is a dict,
  191. so it can be extended any time in future to include whatever
  192. parameters each operation might need.
  193. Any configuration problems are expected to be handled here, so the
  194. plan is not yet run.
  195. """
  196. plan = []
  197. # Handle removals of old components
  198. for cname in old.keys():
  199. if cname not in new:
  200. component = self._components[cname]
  201. if component.running():
  202. plan.append({
  203. 'command': 'stop',
  204. 'component': component
  205. })
  206. # Handle transitions of configuration of what is here
  207. for cname in new.keys():
  208. if cname in old:
  209. for option in ['special', 'process', 'kind']:
  210. if new[cname].get(option) != old[cname].get(option):
  211. raise NotImplementedError('Changing configuration of' +
  212. ' a running component is ' +
  213. 'not yet supported. Remove' +
  214. ' and re-add ' + cname +
  215. 'to get the same effect')
  216. # Handle introduction of new components
  217. plan_add = []
  218. for cname in new.keys():
  219. if cname not in old:
  220. params = new[cname]
  221. creator = Component
  222. if 'special' in params:
  223. # TODO: Better error handling
  224. creator = specials[params['special']]
  225. component = creator(params['process'], self.__boss,
  226. params['kind'])
  227. priority = params.get('priority', 0)
  228. # We store tuples, priority first, so we can easily sort
  229. plan_add.append((priority, {
  230. 'component': component,
  231. 'command': 'start',
  232. 'name': cname
  233. }))
  234. # Push the starts there sorted by priority
  235. plan.extend([command for (_, command) in sorted(plan_add,
  236. reverse=True)])
  237. return plan
  238. def _run_plan(self, plan):
  239. """
  240. Run a plan, created beforehead by _build_plan.
  241. With the start and stop commands, it also adds and removes components
  242. in _components.
  243. Currently implemented commands are:
  244. * start
  245. * stop
  246. """
  247. for task in plan:
  248. component = task['component']
  249. command = task['command']
  250. if command == 'start':
  251. component.start()
  252. self._components[task['name']] = component
  253. elif command == 'stop':
  254. component.stop()
  255. del self._components[task['name']]
  256. else:
  257. # Can Not Happen (as the plans are generated by ourself).
  258. # Therefore not tested.
  259. raise NotImplementedError("Command unknown: " + command)