Parallelize LUReplaceDisks
[ganeti-local] / lib / mcpu.py
index ff2c80c..b6c9ffd 100644 (file)
@@ -34,9 +34,9 @@ from ganeti import constants
 from ganeti import errors
 from ganeti import rpc
 from ganeti import cmdlib
-from ganeti import config
 from ganeti import ssconf
 from ganeti import logger
+from ganeti import locking
 
 
 class Processor(object):
@@ -46,7 +46,6 @@ class Processor(object):
     opcodes.OpDestroyCluster: cmdlib.LUDestroyCluster,
     opcodes.OpQueryClusterInfo: cmdlib.LUQueryClusterInfo,
     opcodes.OpVerifyCluster: cmdlib.LUVerifyCluster,
-    opcodes.OpMasterFailover: cmdlib.LUMasterFailover,
     opcodes.OpDumpClusterConfig: cmdlib.LUDumpClusterConfig,
     opcodes.OpRenameCluster: cmdlib.LURenameCluster,
     opcodes.OpVerifyDisks: cmdlib.LUVerifyDisks,
@@ -89,96 +88,107 @@ class Processor(object):
     opcodes.OpTestAllocator: cmdlib.LUTestAllocator,
     }
 
-  def __init__(self, feedback=None):
+  def __init__(self, context):
     """Constructor for Processor
 
     Args:
      - feedback_fn: the feedback function (taking one string) to be run when
                     interesting events are happening
     """
-    self.cfg = None
-    self.sstore = None
-    self._feedback_fn = feedback
+    self.context = context
+    self._feedback_fn = None
+    self.exclusive_BGL = False
 
-  def ExecOpCode(self, op):
-    """Execute an opcode.
-
-    Args:
-      op: the opcode to be executed
+  def _ExecLU(self, lu):
+    """Logical Unit execution sequence.
 
     """
-    if not isinstance(op, opcodes.OpCode):
-      raise errors.ProgrammerError("Non-opcode instance passed"
-                                   " to ExecOpcode")
-
-    lu_class = self.DISPATCH_TABLE.get(op.__class__, None)
-    if lu_class is None:
-      raise errors.OpCodeUnknown("Unknown opcode")
-
-    if self.cfg is None:
-      self.cfg = config.ConfigWriter()
-      if lu_class.REQ_WSSTORE:
-        self.sstore = ssconf.WritableSimpleStore()
-      else:
-        self.sstore = ssconf.SimpleStore()
-    if self.cfg is not None:
-      write_count = self.cfg.write_count
-    else:
-      write_count = 0
-    lu = lu_class(self, op, self.cfg, self.sstore)
+    write_count = self.context.cfg.write_count
     lu.CheckPrereq()
     hm = HooksMaster(rpc.call_hooks_runner, self, lu)
     h_results = hm.RunPhase(constants.HOOKS_PHASE_PRE)
-    lu.HooksCallBack(constants.HOOKS_PHASE_PRE,
-                     h_results, self._feedback_fn, None)
+    lu.HooksCallBack(constants.HOOKS_PHASE_PRE, h_results,
+                     self._feedback_fn, None)
     try:
       result = lu.Exec(self._feedback_fn)
       h_results = hm.RunPhase(constants.HOOKS_PHASE_POST)
-      result = lu.HooksCallBack(constants.HOOKS_PHASE_POST,
-                       h_results, self._feedback_fn, result)
+      result = lu.HooksCallBack(constants.HOOKS_PHASE_POST, h_results,
+                                self._feedback_fn, result)
     finally:
-      if lu.cfg is not None:
-        # we use lu.cfg and not self.cfg as for init cluster, self.cfg
-        # is None but lu.cfg has been recently initialized in the
-        # lu.Exec method
-        if write_count != lu.cfg.write_count:
-          hm.RunConfigUpdate()
+      # FIXME: This needs locks if not lu_class.REQ_BGL
+      if write_count != self.context.cfg.write_count:
+        hm.RunConfigUpdate()
 
     return result
 
-  def ChainOpCode(self, op):
-    """Chain and execute an opcode.
+  def _LockAndExecLU(self, lu, level):
+    """Execute a Logical Unit, with the needed locks.
 
-    This is used by LUs when they need to execute a child LU.
+    This is a recursive function that starts locking the given level, and
+    proceeds up, till there are no more locks to acquire. Then it executes the
+    given LU and its opcodes.
+
+    """
+    if level not in locking.LEVELS:
+      result = self._ExecLU(lu)
+    elif level in lu.needed_locks:
+      # This gives a chance to LUs to make last-minute changes after acquiring
+      # locks at any preceding level.
+      lu.DeclareLocks(level)
+      needed_locks = lu.needed_locks[level]
+      share = lu.share_locks[level]
+      # This is always safe to do, as we can't acquire more/less locks than
+      # what was requested.
+      lu.acquired_locks[level] = self.context.glm.acquire(level,
+                                                          needed_locks,
+                                                          shared=share)
+      try:
+        result = self._LockAndExecLU(lu, level + 1)
+      finally:
+        # We need to release the current level if we acquired any lock, or if
+        # we acquired the set-lock (needed_locks is None)
+        if lu.needed_locks[level] is None or lu.acquired_locks[level]:
+          self.context.glm.release(level)
+    else:
+      result = self._LockAndExecLU(lu, level + 1)
+
+    return result
+
+  def ExecOpCode(self, op, feedback_fn):
+    """Execute an opcode.
 
     Args:
-     - opcode: the opcode to be executed
+      op: the opcode to be executed
 
     """
     if not isinstance(op, opcodes.OpCode):
       raise errors.ProgrammerError("Non-opcode instance passed"
                                    " to ExecOpcode")
 
+    self._feedback_fn = feedback_fn
     lu_class = self.DISPATCH_TABLE.get(op.__class__, None)
     if lu_class is None:
       raise errors.OpCodeUnknown("Unknown opcode")
 
-    if self.cfg is None:
-      self.cfg = config.ConfigWriter()
-      self.sstore = ssconf.SimpleStore()
-    #do_hooks = lu_class.HPATH is not None
-    lu = lu_class(self, op, self.cfg, self.sstore)
-    lu.CheckPrereq()
-    #if do_hooks:
-    #  hm = HooksMaster(rpc.call_hooks_runner, self, lu)
-    #  h_results = hm.RunPhase(constants.HOOKS_PHASE_PRE)
-    #  lu.HooksCallBack(constants.HOOKS_PHASE_PRE,
-    #                   h_results, self._feedback_fn, None)
-    result = lu.Exec(self._feedback_fn)
-    #if do_hooks:
-    #  h_results = hm.RunPhase(constants.HOOKS_PHASE_POST)
-    #  result = lu.HooksCallBack(constants.HOOKS_PHASE_POST,
-    #                   h_results, self._feedback_fn, result)
+    if lu_class.REQ_WSSTORE:
+      sstore = ssconf.WritableSimpleStore()
+    else:
+      sstore = ssconf.SimpleStore()
+
+    # Acquire the Big Ganeti Lock exclusively if this LU requires it, and in a
+    # shared fashion otherwise (to prevent concurrent run with an exclusive LU.
+    self.context.glm.acquire(locking.LEVEL_CLUSTER, [locking.BGL],
+                             shared=not lu_class.REQ_BGL)
+    try:
+      self.exclusive_BGL = lu_class.REQ_BGL
+      lu = lu_class(self, op, self.context, sstore)
+      lu.ExpandNames()
+      assert lu.needed_locks is not None, "needed_locks not set by LU"
+      result = self._LockAndExecLU(lu, locking.LEVEL_INSTANCE)
+    finally:
+      self.context.glm.release(locking.LEVEL_CLUSTER)
+      self.exclusive_BGL = False
+
     return result
 
   def LogStep(self, current, total, message):