4 # Copyright (C) 2006, 2007 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 """Module implementing the logic behind the cluster operations
24 This module implements the logic for doing operations in the cluster. There
25 are two kinds of classes defined:
26 - logical units, which know how to deal with their specific opcode only
27 - the processor, which dispatches the opcodes to their logical units
35 from ganeti import opcodes
36 from ganeti import constants
37 from ganeti import errors
38 from ganeti import rpc
39 from ganeti import cmdlib
40 from ganeti import locking
43 class _LockAcquireTimeout(Exception):
44 """Internal exception to report timeouts on acquiring locks.
49 def _CalculateLockAttemptTimeouts():
50 """Calculate timeouts for lock attempts.
56 # Wait for a total of at least 150s before doing a blocking acquire
57 while sum(result) < 150.0:
58 timeout = (result[-1] * 1.05) ** 1.25
60 # Cap timeout at 10 seconds. This gives other jobs a chance to run
61 # even if we're still trying to get our locks, before finally moving
62 # to a blocking acquire.
67 # Lower boundary for safety
70 result.append(timeout)
75 class _LockAttemptTimeoutStrategy(object):
76 """Class with lock acquire timeout strategy.
86 _TIMEOUT_PER_ATTEMPT = _CalculateLockAttemptTimeouts()
88 def __init__(self, attempt=0, _time_fn=time.time, _random_fn=random.random):
89 """Initializes this class.
92 @param attempt: Current attempt number
93 @param _time_fn: Time function for unittests
94 @param _random_fn: Random number generator for unittests
100 raise ValueError("Attempt must be zero or positive")
102 self._attempt = attempt
103 self._time_fn = _time_fn
104 self._random_fn = _random_fn
106 self._start_time = None
108 def NextAttempt(self):
109 """Returns the strategy for the next attempt.
112 return _LockAttemptTimeoutStrategy(attempt=self._attempt + 1,
113 _time_fn=self._time_fn,
114 _random_fn=self._random_fn)
116 def CalcRemainingTimeout(self):
117 """Returns the remaining timeout.
121 timeout = self._TIMEOUT_PER_ATTEMPT[self._attempt]
123 # No more timeouts, do blocking acquire
126 # Get start time on first calculation
127 if self._start_time is None:
128 self._start_time = self._time_fn()
130 # Calculate remaining time for this attempt
131 remaining_timeout = self._start_time + timeout - self._time_fn()
133 # Add a small variation (-/+ 5%) to timeouts. This helps in situations
134 # where two or more jobs are fighting for the same lock(s).
135 variation_range = remaining_timeout * 0.1
136 remaining_timeout += ((self._random_fn() * variation_range) -
137 (variation_range * 0.5))
139 assert remaining_timeout >= 0.0, "Timeout must be positive"
141 return remaining_timeout
145 """Base class for OpCode execution callbacks.
148 def NotifyStart(self):
149 """Called when we are about to execute the LU.
151 This function is called when we're about to start the lu's Exec() method,
152 that is, after we have acquired all locks.
156 def Feedback(self, *args):
157 """Sends feedback from the LU code to the end-user.
161 def ReportLocks(self, msg):
162 """Report lock operations.
167 class Processor(object):
168 """Object which runs OpCodes"""
171 opcodes.OpPostInitCluster: cmdlib.LUPostInitCluster,
172 opcodes.OpDestroyCluster: cmdlib.LUDestroyCluster,
173 opcodes.OpQueryClusterInfo: cmdlib.LUQueryClusterInfo,
174 opcodes.OpVerifyCluster: cmdlib.LUVerifyCluster,
175 opcodes.OpQueryConfigValues: cmdlib.LUQueryConfigValues,
176 opcodes.OpRenameCluster: cmdlib.LURenameCluster,
177 opcodes.OpVerifyDisks: cmdlib.LUVerifyDisks,
178 opcodes.OpSetClusterParams: cmdlib.LUSetClusterParams,
179 opcodes.OpRedistributeConfig: cmdlib.LURedistributeConfig,
180 opcodes.OpRepairDiskSizes: cmdlib.LURepairDiskSizes,
182 opcodes.OpAddNode: cmdlib.LUAddNode,
183 opcodes.OpQueryNodes: cmdlib.LUQueryNodes,
184 opcodes.OpQueryNodeVolumes: cmdlib.LUQueryNodeVolumes,
185 opcodes.OpQueryNodeStorage: cmdlib.LUQueryNodeStorage,
186 opcodes.OpModifyNodeStorage: cmdlib.LUModifyNodeStorage,
187 opcodes.OpRepairNodeStorage: cmdlib.LURepairNodeStorage,
188 opcodes.OpRemoveNode: cmdlib.LURemoveNode,
189 opcodes.OpSetNodeParams: cmdlib.LUSetNodeParams,
190 opcodes.OpPowercycleNode: cmdlib.LUPowercycleNode,
191 opcodes.OpEvacuateNode: cmdlib.LUEvacuateNode,
192 opcodes.OpMigrateNode: cmdlib.LUMigrateNode,
194 opcodes.OpCreateInstance: cmdlib.LUCreateInstance,
195 opcodes.OpReinstallInstance: cmdlib.LUReinstallInstance,
196 opcodes.OpRemoveInstance: cmdlib.LURemoveInstance,
197 opcodes.OpRenameInstance: cmdlib.LURenameInstance,
198 opcodes.OpActivateInstanceDisks: cmdlib.LUActivateInstanceDisks,
199 opcodes.OpShutdownInstance: cmdlib.LUShutdownInstance,
200 opcodes.OpStartupInstance: cmdlib.LUStartupInstance,
201 opcodes.OpRebootInstance: cmdlib.LURebootInstance,
202 opcodes.OpDeactivateInstanceDisks: cmdlib.LUDeactivateInstanceDisks,
203 opcodes.OpReplaceDisks: cmdlib.LUReplaceDisks,
204 opcodes.OpRecreateInstanceDisks: cmdlib.LURecreateInstanceDisks,
205 opcodes.OpFailoverInstance: cmdlib.LUFailoverInstance,
206 opcodes.OpMigrateInstance: cmdlib.LUMigrateInstance,
207 opcodes.OpMoveInstance: cmdlib.LUMoveInstance,
208 opcodes.OpConnectConsole: cmdlib.LUConnectConsole,
209 opcodes.OpQueryInstances: cmdlib.LUQueryInstances,
210 opcodes.OpQueryInstanceData: cmdlib.LUQueryInstanceData,
211 opcodes.OpSetInstanceParams: cmdlib.LUSetInstanceParams,
212 opcodes.OpGrowDisk: cmdlib.LUGrowDisk,
214 opcodes.OpDiagnoseOS: cmdlib.LUDiagnoseOS,
216 opcodes.OpQueryExports: cmdlib.LUQueryExports,
217 opcodes.OpExportInstance: cmdlib.LUExportInstance,
218 opcodes.OpRemoveExport: cmdlib.LURemoveExport,
220 opcodes.OpGetTags: cmdlib.LUGetTags,
221 opcodes.OpSearchTags: cmdlib.LUSearchTags,
222 opcodes.OpAddTags: cmdlib.LUAddTags,
223 opcodes.OpDelTags: cmdlib.LUDelTags,
225 opcodes.OpTestDelay: cmdlib.LUTestDelay,
226 opcodes.OpTestAllocator: cmdlib.LUTestAllocator,
229 def __init__(self, context):
230 """Constructor for Processor
233 self.context = context
235 self.rpc = rpc.RpcRunner(context.cfg)
236 self.hmclass = HooksMaster
238 def _ReportLocks(self, level, names, shared, timeout, acquired, result):
239 """Reports lock operations.
242 @param level: Lock level
243 @type names: list or string
244 @param names: Lock names
246 @param shared: Whether the locks should be acquired in shared mode
247 @type timeout: None or float
248 @param timeout: Timeout for acquiring the locks
250 @param acquired: Whether the locks have already been acquired
251 @type result: None or set
252 @param result: Result from L{locking.GanetiLockManager.acquire}
260 parts.append("timeout")
262 parts.append("acquired")
264 parts.append("waiting")
266 parts.append("blocking")
268 parts.append("timeout=%0.6fs" % timeout)
270 parts.append(locking.LEVEL_NAMES[level])
272 if names == locking.ALL_SET:
274 elif isinstance(names, basestring):
277 parts.append(",".join(names))
280 parts.append("shared")
282 parts.append("exclusive")
284 msg = "/".join(parts)
286 logging.debug("LU locks %s", msg)
289 self._cbs.ReportLocks(msg)
291 def _AcquireLocks(self, level, names, shared, timeout):
292 """Acquires locks via the Ganeti lock manager.
295 @param level: Lock level
296 @type names: list or string
297 @param names: Lock names
299 @param shared: Whether the locks should be acquired in shared mode
300 @type timeout: None or float
301 @param timeout: Timeout for acquiring the locks
304 self._ReportLocks(level, names, shared, timeout, False, None)
306 acquired = self.context.glm.acquire(level, names, shared=shared,
309 self._ReportLocks(level, names, shared, timeout, True, acquired)
313 def _ExecLU(self, lu):
314 """Logical Unit execution sequence.
317 write_count = self.context.cfg.write_count
319 hm = HooksMaster(self.rpc.call_hooks_runner, lu)
320 h_results = hm.RunPhase(constants.HOOKS_PHASE_PRE)
321 lu.HooksCallBack(constants.HOOKS_PHASE_PRE, h_results,
322 self._Feedback, None)
324 if getattr(lu.op, "dry_run", False):
325 # in this mode, no post-hooks are run, and the config is not
326 # written (as it might have been modified by another LU, and we
327 # shouldn't do writeout on behalf of other threads
328 self.LogInfo("dry-run mode requested, not actually executing"
330 return lu.dry_run_result
333 result = lu.Exec(self._Feedback)
334 h_results = hm.RunPhase(constants.HOOKS_PHASE_POST)
335 result = lu.HooksCallBack(constants.HOOKS_PHASE_POST, h_results,
336 self._Feedback, result)
338 # FIXME: This needs locks if not lu_class.REQ_BGL
339 if write_count != self.context.cfg.write_count:
344 def _LockAndExecLU(self, lu, level, calc_timeout):
345 """Execute a Logical Unit, with the needed locks.
347 This is a recursive function that starts locking the given level, and
348 proceeds up, till there are no more locks to acquire. Then it executes the
349 given LU and its opcodes.
352 adding_locks = level in lu.add_locks
353 acquiring_locks = level in lu.needed_locks
354 if level not in locking.LEVELS:
356 self._cbs.NotifyStart()
358 result = self._ExecLU(lu)
360 elif adding_locks and acquiring_locks:
361 # We could both acquire and add locks at the same level, but for now we
362 # don't need this, so we'll avoid the complicated code needed.
363 raise NotImplementedError("Can't declare locks to acquire when adding"
366 elif adding_locks or acquiring_locks:
367 lu.DeclareLocks(level)
368 share = lu.share_locks[level]
371 assert adding_locks ^ acquiring_locks, \
372 "Locks must be either added or acquired"
376 needed_locks = lu.needed_locks[level]
378 acquired = self._AcquireLocks(level, needed_locks, share,
382 raise _LockAcquireTimeout()
386 add_locks = lu.add_locks[level]
387 lu.remove_locks[level] = add_locks
390 self.context.glm.add(level, add_locks, acquired=1, shared=share)
391 except errors.LockError:
392 raise errors.OpPrereqError(
393 "Couldn't add locks (%s), probably because of a race condition"
394 " with another job, who added them first" % add_locks)
399 lu.acquired_locks[level] = acquired
401 result = self._LockAndExecLU(lu, level + 1, calc_timeout)
403 if level in lu.remove_locks:
404 self.context.glm.remove(level, lu.remove_locks[level])
406 if self.context.glm.is_owned(level):
407 self.context.glm.release(level)
410 result = self._LockAndExecLU(lu, level + 1, calc_timeout)
414 def ExecOpCode(self, op, cbs):
415 """Execute an opcode.
417 @type op: an OpCode instance
418 @param op: the opcode to be executed
419 @type cbs: L{OpExecCbBase}
420 @param cbs: Runtime callbacks
423 if not isinstance(op, opcodes.OpCode):
424 raise errors.ProgrammerError("Non-opcode instance passed"
429 lu_class = self.DISPATCH_TABLE.get(op.__class__, None)
431 raise errors.OpCodeUnknown("Unknown opcode")
433 timeout_strategy = _LockAttemptTimeoutStrategy()
437 acquire_timeout = timeout_strategy.CalcRemainingTimeout()
439 # Acquire the Big Ganeti Lock exclusively if this LU requires it,
440 # and in a shared fashion otherwise (to prevent concurrent run with
442 if self._AcquireLocks(locking.LEVEL_CLUSTER, locking.BGL,
443 not lu_class.REQ_BGL, acquire_timeout) is None:
444 raise _LockAcquireTimeout()
447 lu = lu_class(self, op, self.context, self.rpc)
449 assert lu.needed_locks is not None, "needed_locks not set by LU"
451 return self._LockAndExecLU(lu, locking.LEVEL_INSTANCE,
452 timeout_strategy.CalcRemainingTimeout)
454 self.context.glm.release(locking.LEVEL_CLUSTER)
456 except _LockAcquireTimeout:
457 # Timeout while waiting for lock, try again
460 timeout_strategy = timeout_strategy.NextAttempt()
465 def _Feedback(self, *args):
466 """Forward call to feedback callback function.
470 self._cbs.Feedback(*args)
472 def LogStep(self, current, total, message):
473 """Log a change in LU execution progress.
476 logging.debug("Step %d/%d %s", current, total, message)
477 self._Feedback("STEP %d/%d %s" % (current, total, message))
479 def LogWarning(self, message, *args, **kwargs):
480 """Log a warning to the logs and the user.
482 The optional keyword argument is 'hint' and can be used to show a
483 hint to the user (presumably related to the warning). If the
484 message is empty, it will not be printed at all, allowing one to
488 assert not kwargs or (len(kwargs) == 1 and "hint" in kwargs), \
489 "Invalid keyword arguments for LogWarning (%s)" % str(kwargs)
491 message = message % tuple(args)
493 logging.warning(message)
494 self._Feedback(" - WARNING: %s" % message)
496 self._Feedback(" Hint: %s" % kwargs["hint"])
498 def LogInfo(self, message, *args):
499 """Log an informational message to the logs and the user.
503 message = message % tuple(args)
504 logging.info(message)
505 self._Feedback(" - INFO: %s" % message)
508 class HooksMaster(object):
511 This class distributes the run commands to the nodes based on the
514 In order to remove the direct dependency on the rpc module, the
515 constructor needs a function which actually does the remote
516 call. This will usually be rpc.call_hooks_runner, but any function
517 which behaves the same works.
520 def __init__(self, callfn, lu):
524 self.env, node_list_pre, node_list_post = self._BuildEnv()
525 self.node_list = {constants.HOOKS_PHASE_PRE: node_list_pre,
526 constants.HOOKS_PHASE_POST: node_list_post}
529 """Compute the environment and the target nodes.
531 Based on the opcode and the current node list, this builds the
532 environment for the hooks and the target node list for the run.
536 "PATH": "/sbin:/bin:/usr/sbin:/usr/bin",
537 "GANETI_HOOKS_VERSION": constants.HOOKS_VERSION,
538 "GANETI_OP_CODE": self.op.OP_ID,
539 "GANETI_OBJECT_TYPE": self.lu.HTYPE,
540 "GANETI_DATA_DIR": constants.DATA_DIR,
543 if self.lu.HPATH is not None:
544 lu_env, lu_nodes_pre, lu_nodes_post = self.lu.BuildHooksEnv()
547 env["GANETI_" + key] = lu_env[key]
549 lu_nodes_pre = lu_nodes_post = []
551 return env, frozenset(lu_nodes_pre), frozenset(lu_nodes_post)
553 def _RunWrapper(self, node_list, hpath, phase):
554 """Simple wrapper over self.callfn.
556 This method fixes the environment before doing the rpc call.
559 env = self.env.copy()
560 env["GANETI_HOOKS_PHASE"] = phase
561 env["GANETI_HOOKS_PATH"] = hpath
562 if self.lu.cfg is not None:
563 env["GANETI_CLUSTER"] = self.lu.cfg.GetClusterName()
564 env["GANETI_MASTER"] = self.lu.cfg.GetMasterNode()
566 env = dict([(str(key), str(val)) for key, val in env.iteritems()])
568 return self.callfn(node_list, hpath, phase, env)
570 def RunPhase(self, phase, nodes=None):
571 """Run all the scripts for a phase.
573 This is the main function of the HookMaster.
575 @param phase: one of L{constants.HOOKS_PHASE_POST} or
576 L{constants.HOOKS_PHASE_PRE}; it denotes the hooks phase
577 @param nodes: overrides the predefined list of nodes for the given phase
578 @return: the processed results of the hooks multi-node rpc call
579 @raise errors.HooksFailure: on communication failure to the nodes
580 @raise errors.HooksAbort: on failure of one of the hooks
583 if not self.node_list[phase] and not nodes:
584 # empty node list, we should not attempt to run this as either
585 # we're in the cluster init phase and the rpc client part can't
586 # even attempt to run, or this LU doesn't do hooks at all
588 hpath = self.lu.HPATH
589 if nodes is not None:
590 results = self._RunWrapper(nodes, hpath, phase)
592 results = self._RunWrapper(self.node_list[phase], hpath, phase)
595 msg = "Communication Failure"
596 if phase == constants.HOOKS_PHASE_PRE:
597 raise errors.HooksFailure(msg)
599 self.lu.LogWarning(msg)
601 for node_name in results:
602 res = results[node_name]
607 self.lu.LogWarning("Communication failure to node %s: %s",
610 for script, hkr, output in res.payload:
611 if hkr == constants.HKR_FAIL:
612 if phase == constants.HOOKS_PHASE_PRE:
613 errs.append((node_name, script, output))
616 output = "(no output)"
617 self.lu.LogWarning("On %s script %s failed, output: %s" %
618 (node_name, script, output))
619 if errs and phase == constants.HOOKS_PHASE_PRE:
620 raise errors.HooksAbort(errs)
623 def RunConfigUpdate(self):
624 """Run the special configuration update hook
626 This is a special hook that runs only on the master after each
627 top-level LI if the configuration has been updated.
630 phase = constants.HOOKS_PHASE_POST
631 hpath = constants.HOOKS_NAME_CFGUPDATE
632 nodes = [self.lu.cfg.GetMasterNode()]
633 self._RunWrapper(nodes, hpath, phase)