#
#
-# Copyright (C) 2006, 2007 Google Inc.
+# Copyright (C) 2006, 2007, 2011 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
from ganeti import rpc
from ganeti import cmdlib
from ganeti import locking
+from ganeti import utils
+
+
+_OP_PREFIX = "Op"
+_LU_PREFIX = "LU"
class LockAcquireTimeout(Exception):
"""
+def _LUNameForOpName(opname):
+ """Computes the LU name for a given OpCode name.
+
+ """
+ assert opname.startswith(_OP_PREFIX), \
+ "Invalid OpCode name, doesn't start with %s: %s" % (_OP_PREFIX, opname)
+
+ return _LU_PREFIX + opname[len(_OP_PREFIX):]
+
+
+def _ComputeDispatchTable():
+ """Computes the opcode-to-lu dispatch table.
+
+ """
+ return dict((op, getattr(cmdlib, _LUNameForOpName(op.__name__)))
+ for op in opcodes.OP_MAPPING.values()
+ if op.WITH_LU)
+
+
class Processor(object):
"""Object which runs OpCodes"""
- DISPATCH_TABLE = {
- # Cluster
- opcodes.OpPostInitCluster: cmdlib.LUPostInitCluster,
- opcodes.OpDestroyCluster: cmdlib.LUDestroyCluster,
- opcodes.OpQueryClusterInfo: cmdlib.LUQueryClusterInfo,
- opcodes.OpVerifyCluster: cmdlib.LUVerifyCluster,
- opcodes.OpQueryConfigValues: cmdlib.LUQueryConfigValues,
- opcodes.OpRenameCluster: cmdlib.LURenameCluster,
- opcodes.OpVerifyDisks: cmdlib.LUVerifyDisks,
- opcodes.OpSetClusterParams: cmdlib.LUSetClusterParams,
- opcodes.OpRedistributeConfig: cmdlib.LURedistributeConfig,
- opcodes.OpRepairDiskSizes: cmdlib.LURepairDiskSizes,
- # node lu
- opcodes.OpAddNode: cmdlib.LUAddNode,
- opcodes.OpQueryNodes: cmdlib.LUQueryNodes,
- opcodes.OpQueryNodeVolumes: cmdlib.LUQueryNodeVolumes,
- opcodes.OpQueryNodeStorage: cmdlib.LUQueryNodeStorage,
- opcodes.OpModifyNodeStorage: cmdlib.LUModifyNodeStorage,
- opcodes.OpRepairNodeStorage: cmdlib.LURepairNodeStorage,
- opcodes.OpRemoveNode: cmdlib.LURemoveNode,
- opcodes.OpSetNodeParams: cmdlib.LUSetNodeParams,
- opcodes.OpPowercycleNode: cmdlib.LUPowercycleNode,
- opcodes.OpMigrateNode: cmdlib.LUMigrateNode,
- opcodes.OpNodeEvacuationStrategy: cmdlib.LUNodeEvacuationStrategy,
- # instance lu
- opcodes.OpCreateInstance: cmdlib.LUCreateInstance,
- opcodes.OpReinstallInstance: cmdlib.LUReinstallInstance,
- opcodes.OpRemoveInstance: cmdlib.LURemoveInstance,
- opcodes.OpRenameInstance: cmdlib.LURenameInstance,
- opcodes.OpActivateInstanceDisks: cmdlib.LUActivateInstanceDisks,
- opcodes.OpShutdownInstance: cmdlib.LUShutdownInstance,
- opcodes.OpStartupInstance: cmdlib.LUStartupInstance,
- opcodes.OpRebootInstance: cmdlib.LURebootInstance,
- opcodes.OpDeactivateInstanceDisks: cmdlib.LUDeactivateInstanceDisks,
- opcodes.OpReplaceDisks: cmdlib.LUReplaceDisks,
- opcodes.OpRecreateInstanceDisks: cmdlib.LURecreateInstanceDisks,
- opcodes.OpFailoverInstance: cmdlib.LUFailoverInstance,
- opcodes.OpMigrateInstance: cmdlib.LUMigrateInstance,
- opcodes.OpMoveInstance: cmdlib.LUMoveInstance,
- opcodes.OpConnectConsole: cmdlib.LUConnectConsole,
- opcodes.OpQueryInstances: cmdlib.LUQueryInstances,
- opcodes.OpQueryInstanceData: cmdlib.LUQueryInstanceData,
- opcodes.OpSetInstanceParams: cmdlib.LUSetInstanceParams,
- opcodes.OpGrowDisk: cmdlib.LUGrowDisk,
- # os lu
- opcodes.OpDiagnoseOS: cmdlib.LUDiagnoseOS,
- # exports lu
- opcodes.OpQueryExports: cmdlib.LUQueryExports,
- opcodes.OpPrepareExport: cmdlib.LUPrepareExport,
- opcodes.OpExportInstance: cmdlib.LUExportInstance,
- opcodes.OpRemoveExport: cmdlib.LURemoveExport,
- # tags lu
- opcodes.OpGetTags: cmdlib.LUGetTags,
- opcodes.OpSearchTags: cmdlib.LUSearchTags,
- opcodes.OpAddTags: cmdlib.LUAddTags,
- opcodes.OpDelTags: cmdlib.LUDelTags,
- # test lu
- opcodes.OpTestDelay: cmdlib.LUTestDelay,
- opcodes.OpTestAllocator: cmdlib.LUTestAllocator,
- opcodes.OpTestJobqueue: cmdlib.LUTestJobqueue,
- }
+ DISPATCH_TABLE = _ComputeDispatchTable()
def __init__(self, context, ec_id):
"""Constructor for Processor
@param shared: Whether the locks should be acquired in shared mode
@type timeout: None or float
@param timeout: Timeout for acquiring the locks
+ @raise LockAcquireTimeout: In case locks couldn't be acquired in specified
+ amount of time
"""
if self._cbs:
acquired = self.context.glm.acquire(level, names, shared=shared,
timeout=timeout, priority=priority)
+ if acquired is None:
+ raise LockAcquireTimeout()
+
return acquired
def _ExecLU(self, lu):
acquired = self._AcquireLocks(level, needed_locks, share,
calc_timeout(), priority)
-
- if acquired is None:
- raise LockAcquireTimeout()
-
else:
# Adding locks
add_locks = lu.add_locks[level]
if timeout is None:
calc_timeout = lambda: None
else:
- calc_timeout = locking.RunningTimeout(timeout, False).Remaining
+ calc_timeout = utils.RunningTimeout(timeout, False).Remaining
self._cbs = cbs
try:
# 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.
- if self._AcquireLocks(locking.LEVEL_CLUSTER, locking.BGL,
- not lu_class.REQ_BGL, calc_timeout(),
- priority) is None:
- raise LockAcquireTimeout()
-
+ self._AcquireLocks(locking.LEVEL_CLUSTER, locking.BGL,
+ not lu_class.REQ_BGL, calc_timeout(),
+ priority)
try:
lu = lu_class(self, op, self.context, self.rpc)
lu.ExpandNames()