Wrap lines over 80 characters
[ganeti-local] / daemons / ganeti-confd
index 2f2e95b..8cf896a 100755 (executable)
@@ -28,23 +28,18 @@ It uses UDP+HMAC for authentication with a global cluster key.
 
 import os
 import sys
-import traceback
-import errno
 import logging
-import time
 import asyncore
 import socket
 import pyinotify
+import time
 
 from optparse import OptionParser
 
 from ganeti import constants
-from ganeti import objects
 from ganeti import errors
 from ganeti import daemon
-from ganeti import utils
 from ganeti import ssconf
-from ganeti import serializer
 from ganeti.asyncnotifier import AsyncNotifier
 from ganeti.confd.server import ConfdProcessor
 
@@ -101,45 +96,45 @@ class ConfdAsyncUDPServer(asyncore.dispatcher):
 
 class ConfdInotifyEventHandler(pyinotify.ProcessEvent):
 
-  def __init__(self, watch_manager, reader,
+  def __init__(self, watch_manager, callback,
                file=constants.CLUSTER_CONF_FILE):
     """Constructor for ConfdInotifyEventHandler
 
     @type watch_manager: L{pyinotify.WatchManager}
     @param watch_manager: ganeti-confd inotify watch manager
-    @type reader: L{ssconf.SimpleConfigReader}
-    @param reader: ganeti-confd SimpleConfigReader
+    @type callback: function accepting a boolean
+    @param callback: function to call when an inotify event happens
     @type file: string
     @param file: config file to watch
 
     """
     # no need to call the parent's constructor
     self.watch_manager = watch_manager
-    self.reader = reader
+    self.callback = callback
     self.mask = pyinotify.EventsCodes.IN_IGNORED | \
                 pyinotify.EventsCodes.IN_MODIFY
     self.file = file
-    self.add_config_watch()
+    self.watch_handle = None
 
-  def add_config_watch(self):
-    """Add a watcher for the ganeti config file
+  def enable(self):
+    """Watch the given file
 
     """
-    result = self.watch_manager.add_watch(self.file, self.mask)
-    if not result[self.file] > 0:
-      raise errors.ConfdFatalError("Could not add inotify watcher")
-
-  def reload_config(self):
-    try:
-      reloaded = self.reader.Reload()
-      if reloaded:
-        logging.info("Reloaded ganeti config")
+    if self.watch_handle is None:
+      result = self.watch_manager.add_watch(self.file, self.mask)
+      if not self.file in result or result[self.file] <= 0:
+        raise errors.InotifyError("Could not add inotify watcher")
       else:
-        logging.debug("Skipped double config reload")
-    except errors.ConfigurationError:
-      # transform a ConfigurationError in a fatal error, that will cause confd
-      # to quit.
-      raise errors.ConfdFatalError(err)
+        self.watch_handle = result[self.file]
+
+  def disable(self):
+    """Stop watching the given file
+
+    """
+    if self.watch_handle is not None:
+      result = self.watch_manager.rm_watch(self.watch_handle)
+      if result[self.watch_handle]:
+        self.watch_handle = None
 
   def process_IN_IGNORED(self, event):
     # Due to the fact that we monitor just for the cluster config file (rather
@@ -149,14 +144,14 @@ class ConfdInotifyEventHandler(pyinotify.ProcessEvent):
     # contextual with the replacement). In such a case we need to create
     # another watcher for the "new" file.
     logging.debug("Received 'ignored' inotify event for %s" % event.path)
+    self.watch_handle = None
 
     try:
       # Since the kernel believes the file we were interested in is gone, it's
       # not going to notify us of any other events, until we set up, here, the
       # new watch. This is not a race condition, though, since we're anyway
       # going to realod the file after setting up the new watch.
-      self.add_config_watch()
-      self.reload_config()
+      self.callback(False)
     except errors.ConfdFatalError, err:
       logging.critical("Critical error, shutting down: %s" % err)
       sys.exit(constants.EXIT_FAILURE)
@@ -174,7 +169,7 @@ class ConfdInotifyEventHandler(pyinotify.ProcessEvent):
     logging.debug("Received 'modify' inotify event for %s" % event.path)
 
     try:
-      self.reload_config()
+      self.callback(True)
     except errors.ConfdFatalError, err:
       logging.critical("Critical error, shutting down: %s" % err)
       sys.exit(constants.EXIT_FAILURE)
@@ -188,8 +183,153 @@ class ConfdInotifyEventHandler(pyinotify.ProcessEvent):
     logging.error("Received unhandled inotify event: %s" % event)
 
 
-def CheckCONFD(options, args):
-  """Initial checks whether to run exit with a failure
+class ConfdConfigurationReloader(object):
+  """Logic to control when to reload the ganeti configuration
+
+  This class is able to alter between inotify and polling, to rate-limit the
+  number of reloads. When using inotify it also supports a fallback timed
+  check, to verify that the reload hasn't failed.
+
+  """
+  def __init__(self, processor, mainloop):
+    """Constructor for ConfdConfigurationReloader
+
+    @type processor: L{confd.server.ConfdProcessor}
+    @param processor: ganeti-confd ConfdProcessor
+    @type mainloop: L{daemon.Mainloop}
+    @param mainloop: ganeti-confd mainloop
+
+    """
+    self.processor = processor
+    self.mainloop = mainloop
+
+    self.polling = True
+    self.last_notification = 0
+
+    # Asyncronous inotify handler for config changes
+    self.wm = pyinotify.WatchManager()
+    self.inotify_handler = ConfdInotifyEventHandler(self.wm, self.OnInotify)
+    self.notifier = AsyncNotifier(self.wm, self.inotify_handler)
+
+    self.timer_handle = None
+    self._EnableTimer()
+
+  def OnInotify(self, notifier_enabled):
+    """Receive an inotify notification.
+
+    @type notifier_enabled: boolean
+    @param notifier_enabled: whether the notifier is still enabled
+
+    """
+    current_time = time.time()
+    time_delta = current_time - self.last_notification
+    self.last_notification = current_time
+
+    if time_delta < constants.CONFD_CONFIG_RELOAD_RATELIMIT:
+      logging.debug("Moving from inotify mode to polling mode")
+      self.polling = True
+      if notifier_enabled:
+        self.inotify_handler.disable()
+
+    if not self.polling and not notifier_enabled:
+      try:
+        self.inotify_handler.enable()
+      except errors.InotifyError:
+        self.polling = True
+
+    try:
+      reloaded = self.processor.reader.Reload()
+      if reloaded:
+        logging.info("Reloaded ganeti config")
+      else:
+        logging.debug("Skipped double config reload")
+    except errors.ConfigurationError:
+      self.DisableConfd()
+      self.inotify_handler.disable()
+      return
+
+    # Reset the timer. If we're polling it will go to the polling rate, if
+    # we're not it will delay it again to its base safe timeout.
+    self._ResetTimer()
+
+  def _DisableTimer(self):
+    if self.timer_handle is not None:
+      self.mainloop.scheduler.cancel(self.timer_handle)
+      self.timer_handle = None
+
+  def _EnableTimer(self):
+    if self.polling:
+      timeout = constants.CONFD_CONFIG_RELOAD_RATELIMIT
+    else:
+      timeout = constants.CONFD_CONFIG_RELOAD_TIMEOUT
+
+    if self.timer_handle is None:
+      self.timer_handle = self.mainloop.scheduler.enter(
+        timeout, 1, self.OnTimer, [])
+
+  def _ResetTimer(self):
+    self._DisableTimer()
+    self._EnableTimer()
+
+  def OnTimer(self):
+    """Function called when the timer fires
+
+    """
+    self.timer_handle = None
+    reloaded = False
+    was_disabled = False
+    try:
+      if self.processor.reader is None:
+        was_disabled = True
+        self.EnableConfd()
+        reloaded = True
+      else:
+        reloaded = self.processor.reader.Reload()
+    except errors.ConfigurationError:
+      self.DisableConfd(silent=was_disabled)
+      return
+
+    if self.polling and reloaded:
+      logging.info("Reloaded ganeti config")
+    elif reloaded:
+      # We have reloaded the config files, but received no inotify event.  If
+      # an event is pending though, we just happen to have timed out before
+      # receiving it, so this is not a problem, and we shouldn't alert
+      if not self.notifier.check_events() and not was_disabled:
+        logging.warning("Config file reload at timeout (inotify failure)")
+    elif self.polling:
+      # We're polling, but we haven't reloaded the config:
+      # Going back to inotify mode
+      logging.debug("Moving from polling mode to inotify mode")
+      self.polling = False
+      try:
+        self.inotify_handler.enable()
+      except errors.InotifyError:
+        self.polling = True
+    else:
+      logging.debug("Performed configuration check")
+
+    self._EnableTimer()
+
+  def DisableConfd(self, silent=False):
+    """Puts confd in non-serving mode
+
+    """
+    if not silent:
+      logging.warning("Confd is being disabled")
+    self.processor.Disable()
+    self.polling = False
+    self._ResetTimer()
+
+  def EnableConfd(self):
+    self.processor.Enable()
+    logging.warning("Confd is being enabled")
+    self.polling = True
+    self._ResetTimer()
+
+
+def CheckConfd(options, args):
+  """Initial checks whether to run exit with a failure.
 
   """
   # TODO: collapse HMAC daemons handling in daemons GenericMain, when we'll
@@ -198,25 +338,28 @@ def CheckCONFD(options, args):
     print >> sys.stderr, "Need HMAC key %s to run" % constants.HMAC_CLUSTER_KEY
     sys.exit(constants.EXIT_FAILURE)
 
-  ssconf.CheckMasterCandidate(options.debug)
 
-
-def ExecCONFD(options, args):
-  """Main CONFD function, executed with pidfile held
+def ExecConfd(options, args):
+  """Main confd function, executed with PID file held
 
   """
-  # confd-level SimpleConfigReader
-  reader = ssconf.SimpleConfigReader()
+  mainloop = daemon.Mainloop()
 
   # Asyncronous confd UDP server
-  processor = ConfdProcessor(reader)
+  processor = ConfdProcessor()
+  try:
+    processor.Enable()
+  except errors.ConfigurationError:
+    # If enabling the processor has failed, we can still go on, but confd will
+    # be disabled
+    logging.warning("Confd is starting in disabled mode")
+    pass
   server = ConfdAsyncUDPServer(options.bind_address, options.port, processor)
 
-  # Asyncronous inotify handler for config changes
-  wm = pyinotify.WatchManager()
-  confd_event_handler = ConfdInotifyEventHandler(wm, reader)
-  notifier = AsyncNotifier(wm, confd_event_handler)
-  asyncore.loop()
+  # Configuration reloader
+  reloader = ConfdConfigurationReloader(processor, mainloop)
+
+  mainloop.Run()
 
 
 def main():
@@ -231,8 +374,8 @@ def main():
   dirs = [(val, constants.RUN_DIRS_MODE) for val in constants.SUB_RUN_DIRS]
   dirs.append((constants.LOG_OS_DIR, 0750))
   dirs.append((constants.LOCK_DIR, 1777))
-  daemon.GenericMain(constants.CONFD, parser, dirs, CheckCONFD, ExecCONFD)
+  daemon.GenericMain(constants.CONFD, parser, dirs, CheckConfd, ExecConfd)
 
 
-if __name__ == '__main__':
+if __name__ == "__main__":
   main()