root / lib / mcpu.py @ 0d5a0b96
History | View | Annotate | Download (18.1 kB)
1 | 2f31098c | Iustin Pop | #
|
---|---|---|---|
2 | a8083063 | Iustin Pop | #
|
3 | a8083063 | Iustin Pop | |
4 | a1a7bc78 | Iustin Pop | # Copyright (C) 2006, 2007, 2011 Google Inc.
|
5 | a8083063 | Iustin Pop | #
|
6 | a8083063 | Iustin Pop | # This program is free software; you can redistribute it and/or modify
|
7 | a8083063 | Iustin Pop | # it under the terms of the GNU General Public License as published by
|
8 | a8083063 | Iustin Pop | # the Free Software Foundation; either version 2 of the License, or
|
9 | a8083063 | Iustin Pop | # (at your option) any later version.
|
10 | a8083063 | Iustin Pop | #
|
11 | a8083063 | Iustin Pop | # This program is distributed in the hope that it will be useful, but
|
12 | a8083063 | Iustin Pop | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | a8083063 | Iustin Pop | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | a8083063 | Iustin Pop | # General Public License for more details.
|
15 | a8083063 | Iustin Pop | #
|
16 | a8083063 | Iustin Pop | # You should have received a copy of the GNU General Public License
|
17 | a8083063 | Iustin Pop | # along with this program; if not, write to the Free Software
|
18 | a8083063 | Iustin Pop | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | a8083063 | Iustin Pop | # 02110-1301, USA.
|
20 | a8083063 | Iustin Pop | |
21 | a8083063 | Iustin Pop | |
22 | a8083063 | Iustin Pop | """Module implementing the logic behind the cluster operations
|
23 | a8083063 | Iustin Pop |
|
24 | a8083063 | Iustin Pop | This module implements the logic for doing operations in the cluster. There
|
25 | a8083063 | Iustin Pop | are two kinds of classes defined:
|
26 | a8083063 | Iustin Pop | - logical units, which know how to deal with their specific opcode only
|
27 | a8083063 | Iustin Pop | - the processor, which dispatches the opcodes to their logical units
|
28 | a8083063 | Iustin Pop |
|
29 | a8083063 | Iustin Pop | """
|
30 | a8083063 | Iustin Pop | |
31 | a5eb7789 | Iustin Pop | import logging |
32 | 407339d0 | Michael Hanselmann | import random |
33 | 407339d0 | Michael Hanselmann | import time |
34 | a8083063 | Iustin Pop | |
35 | a8083063 | Iustin Pop | from ganeti import opcodes |
36 | a8083063 | Iustin Pop | from ganeti import constants |
37 | a8083063 | Iustin Pop | from ganeti import errors |
38 | a8083063 | Iustin Pop | from ganeti import rpc |
39 | a8083063 | Iustin Pop | from ganeti import cmdlib |
40 | 04864530 | Guido Trotter | from ganeti import locking |
41 | 557838c1 | René Nussbaumer | from ganeti import utils |
42 | ebc75510 | Michael Hanselmann | from ganeti import compat |
43 | a8083063 | Iustin Pop | |
44 | 7c0d6283 | Michael Hanselmann | |
45 | a1a7bc78 | Iustin Pop | _OP_PREFIX = "Op"
|
46 | a1a7bc78 | Iustin Pop | _LU_PREFIX = "LU"
|
47 | a1a7bc78 | Iustin Pop | |
48 | a1a7bc78 | Iustin Pop | |
49 | 831bbbc1 | Michael Hanselmann | class LockAcquireTimeout(Exception): |
50 | 831bbbc1 | Michael Hanselmann | """Exception to report timeouts on acquiring locks.
|
51 | 407339d0 | Michael Hanselmann |
|
52 | 407339d0 | Michael Hanselmann | """
|
53 | 407339d0 | Michael Hanselmann | |
54 | 407339d0 | Michael Hanselmann | |
55 | e3200b18 | Michael Hanselmann | def _CalculateLockAttemptTimeouts(): |
56 | e3200b18 | Michael Hanselmann | """Calculate timeouts for lock attempts.
|
57 | e3200b18 | Michael Hanselmann |
|
58 | e3200b18 | Michael Hanselmann | """
|
59 | d385a174 | Iustin Pop | result = [constants.LOCK_ATTEMPTS_MINWAIT] |
60 | d385a174 | Iustin Pop | running_sum = result[0]
|
61 | e3200b18 | Michael Hanselmann | |
62 | d385a174 | Iustin Pop | # Wait for a total of at least LOCK_ATTEMPTS_TIMEOUT before doing a
|
63 | d385a174 | Iustin Pop | # blocking acquire
|
64 | d385a174 | Iustin Pop | while running_sum < constants.LOCK_ATTEMPTS_TIMEOUT:
|
65 | e3200b18 | Michael Hanselmann | timeout = (result[-1] * 1.05) ** 1.25 |
66 | e3200b18 | Michael Hanselmann | |
67 | d385a174 | Iustin Pop | # Cap max timeout. This gives other jobs a chance to run even if
|
68 | d385a174 | Iustin Pop | # we're still trying to get our locks, before finally moving to a
|
69 | d385a174 | Iustin Pop | # blocking acquire.
|
70 | d385a174 | Iustin Pop | timeout = min(timeout, constants.LOCK_ATTEMPTS_MAXWAIT)
|
71 | d385a174 | Iustin Pop | # And also cap the lower boundary for safety
|
72 | d385a174 | Iustin Pop | timeout = max(timeout, constants.LOCK_ATTEMPTS_MINWAIT)
|
73 | e3200b18 | Michael Hanselmann | |
74 | e3200b18 | Michael Hanselmann | result.append(timeout) |
75 | d385a174 | Iustin Pop | running_sum += timeout |
76 | e3200b18 | Michael Hanselmann | |
77 | e3200b18 | Michael Hanselmann | return result
|
78 | e3200b18 | Michael Hanselmann | |
79 | e3200b18 | Michael Hanselmann | |
80 | a7770f03 | Michael Hanselmann | class LockAttemptTimeoutStrategy(object): |
81 | 407339d0 | Michael Hanselmann | """Class with lock acquire timeout strategy.
|
82 | 407339d0 | Michael Hanselmann |
|
83 | 407339d0 | Michael Hanselmann | """
|
84 | 407339d0 | Michael Hanselmann | __slots__ = [ |
85 | a7770f03 | Michael Hanselmann | "_timeouts",
|
86 | 407339d0 | Michael Hanselmann | "_random_fn",
|
87 | e3200b18 | Michael Hanselmann | "_time_fn",
|
88 | 407339d0 | Michael Hanselmann | ] |
89 | 407339d0 | Michael Hanselmann | |
90 | e3200b18 | Michael Hanselmann | _TIMEOUT_PER_ATTEMPT = _CalculateLockAttemptTimeouts() |
91 | 407339d0 | Michael Hanselmann | |
92 | a7770f03 | Michael Hanselmann | def __init__(self, _time_fn=time.time, _random_fn=random.random): |
93 | 407339d0 | Michael Hanselmann | """Initializes this class.
|
94 | 407339d0 | Michael Hanselmann |
|
95 | e3200b18 | Michael Hanselmann | @param _time_fn: Time function for unittests
|
96 | 407339d0 | Michael Hanselmann | @param _random_fn: Random number generator for unittests
|
97 | 407339d0 | Michael Hanselmann |
|
98 | 407339d0 | Michael Hanselmann | """
|
99 | 407339d0 | Michael Hanselmann | object.__init__(self) |
100 | 407339d0 | Michael Hanselmann | |
101 | a7770f03 | Michael Hanselmann | self._timeouts = iter(self._TIMEOUT_PER_ATTEMPT) |
102 | e3200b18 | Michael Hanselmann | self._time_fn = _time_fn
|
103 | e3200b18 | Michael Hanselmann | self._random_fn = _random_fn
|
104 | e3200b18 | Michael Hanselmann | |
105 | 407339d0 | Michael Hanselmann | def NextAttempt(self): |
106 | a7770f03 | Michael Hanselmann | """Returns the timeout for the next attempt.
|
107 | 407339d0 | Michael Hanselmann |
|
108 | 407339d0 | Michael Hanselmann | """
|
109 | a7770f03 | Michael Hanselmann | try:
|
110 | a7770f03 | Michael Hanselmann | timeout = self._timeouts.next()
|
111 | a7770f03 | Michael Hanselmann | except StopIteration: |
112 | a7770f03 | Michael Hanselmann | # No more timeouts, do blocking acquire
|
113 | a7770f03 | Michael Hanselmann | timeout = None
|
114 | 407339d0 | Michael Hanselmann | |
115 | a6db1af2 | Michael Hanselmann | if timeout is not None: |
116 | a6db1af2 | Michael Hanselmann | # Add a small variation (-/+ 5%) to timeout. This helps in situations
|
117 | a6db1af2 | Michael Hanselmann | # where two or more jobs are fighting for the same lock(s).
|
118 | a6db1af2 | Michael Hanselmann | variation_range = timeout * 0.1
|
119 | a6db1af2 | Michael Hanselmann | timeout += ((self._random_fn() * variation_range) -
|
120 | a6db1af2 | Michael Hanselmann | (variation_range * 0.5))
|
121 | 407339d0 | Michael Hanselmann | |
122 | a6db1af2 | Michael Hanselmann | return timeout
|
123 | 407339d0 | Michael Hanselmann | |
124 | 407339d0 | Michael Hanselmann | |
125 | 7260cfbe | Iustin Pop | class OpExecCbBase: # pylint: disable-msg=W0232 |
126 | 031a3e57 | Michael Hanselmann | """Base class for OpCode execution callbacks.
|
127 | 031a3e57 | Michael Hanselmann |
|
128 | 031a3e57 | Michael Hanselmann | """
|
129 | 031a3e57 | Michael Hanselmann | def NotifyStart(self): |
130 | 031a3e57 | Michael Hanselmann | """Called when we are about to execute the LU.
|
131 | 031a3e57 | Michael Hanselmann |
|
132 | 031a3e57 | Michael Hanselmann | This function is called when we're about to start the lu's Exec() method,
|
133 | 031a3e57 | Michael Hanselmann | that is, after we have acquired all locks.
|
134 | 031a3e57 | Michael Hanselmann |
|
135 | 031a3e57 | Michael Hanselmann | """
|
136 | 031a3e57 | Michael Hanselmann | |
137 | 031a3e57 | Michael Hanselmann | def Feedback(self, *args): |
138 | 031a3e57 | Michael Hanselmann | """Sends feedback from the LU code to the end-user.
|
139 | 031a3e57 | Michael Hanselmann |
|
140 | 031a3e57 | Michael Hanselmann | """
|
141 | 031a3e57 | Michael Hanselmann | |
142 | acf931b7 | Michael Hanselmann | def CheckCancel(self): |
143 | acf931b7 | Michael Hanselmann | """Check whether job has been cancelled.
|
144 | ef2df7d3 | Michael Hanselmann |
|
145 | ef2df7d3 | Michael Hanselmann | """
|
146 | ef2df7d3 | Michael Hanselmann | |
147 | 6a373640 | Michael Hanselmann | def SubmitManyJobs(self, jobs): |
148 | 6a373640 | Michael Hanselmann | """Submits jobs for processing.
|
149 | 6a373640 | Michael Hanselmann |
|
150 | 6a373640 | Michael Hanselmann | See L{jqueue.JobQueue.SubmitManyJobs}.
|
151 | 6a373640 | Michael Hanselmann |
|
152 | 6a373640 | Michael Hanselmann | """
|
153 | 6a373640 | Michael Hanselmann | raise NotImplementedError |
154 | 6a373640 | Michael Hanselmann | |
155 | 031a3e57 | Michael Hanselmann | |
156 | a1a7bc78 | Iustin Pop | def _LUNameForOpName(opname): |
157 | a1a7bc78 | Iustin Pop | """Computes the LU name for a given OpCode name.
|
158 | a1a7bc78 | Iustin Pop |
|
159 | a1a7bc78 | Iustin Pop | """
|
160 | a1a7bc78 | Iustin Pop | assert opname.startswith(_OP_PREFIX), \
|
161 | a1a7bc78 | Iustin Pop | "Invalid OpCode name, doesn't start with %s: %s" % (_OP_PREFIX, opname)
|
162 | a1a7bc78 | Iustin Pop | |
163 | a1a7bc78 | Iustin Pop | return _LU_PREFIX + opname[len(_OP_PREFIX):] |
164 | a1a7bc78 | Iustin Pop | |
165 | a1a7bc78 | Iustin Pop | |
166 | a1a7bc78 | Iustin Pop | def _ComputeDispatchTable(): |
167 | a1a7bc78 | Iustin Pop | """Computes the opcode-to-lu dispatch table.
|
168 | a1a7bc78 | Iustin Pop |
|
169 | a1a7bc78 | Iustin Pop | """
|
170 | a1a7bc78 | Iustin Pop | return dict((op, getattr(cmdlib, _LUNameForOpName(op.__name__))) |
171 | a1a7bc78 | Iustin Pop | for op in opcodes.OP_MAPPING.values() |
172 | a1a7bc78 | Iustin Pop | if op.WITH_LU)
|
173 | a1a7bc78 | Iustin Pop | |
174 | a1a7bc78 | Iustin Pop | |
175 | a8083063 | Iustin Pop | class Processor(object): |
176 | a8083063 | Iustin Pop | """Object which runs OpCodes"""
|
177 | a1a7bc78 | Iustin Pop | DISPATCH_TABLE = _ComputeDispatchTable() |
178 | a8083063 | Iustin Pop | |
179 | adfa97e3 | Guido Trotter | def __init__(self, context, ec_id): |
180 | a8083063 | Iustin Pop | """Constructor for Processor
|
181 | a8083063 | Iustin Pop |
|
182 | adfa97e3 | Guido Trotter | @type context: GanetiContext
|
183 | adfa97e3 | Guido Trotter | @param context: global Ganeti context
|
184 | adfa97e3 | Guido Trotter | @type ec_id: string
|
185 | adfa97e3 | Guido Trotter | @param ec_id: execution context identifier
|
186 | adfa97e3 | Guido Trotter |
|
187 | a8083063 | Iustin Pop | """
|
188 | 1c901d13 | Guido Trotter | self.context = context
|
189 | adfa97e3 | Guido Trotter | self._ec_id = ec_id
|
190 | 031a3e57 | Michael Hanselmann | self._cbs = None |
191 | 72737a7f | Iustin Pop | self.rpc = rpc.RpcRunner(context.cfg)
|
192 | cd46f3b4 | Luca Bigliardi | self.hmclass = HooksMaster
|
193 | a8083063 | Iustin Pop | |
194 | f879a9c7 | Michael Hanselmann | def _AcquireLocks(self, level, names, shared, timeout, priority): |
195 | 211b6132 | Michael Hanselmann | """Acquires locks via the Ganeti lock manager.
|
196 | 211b6132 | Michael Hanselmann |
|
197 | 211b6132 | Michael Hanselmann | @type level: int
|
198 | 211b6132 | Michael Hanselmann | @param level: Lock level
|
199 | 211b6132 | Michael Hanselmann | @type names: list or string
|
200 | 211b6132 | Michael Hanselmann | @param names: Lock names
|
201 | 211b6132 | Michael Hanselmann | @type shared: bool
|
202 | 211b6132 | Michael Hanselmann | @param shared: Whether the locks should be acquired in shared mode
|
203 | 211b6132 | Michael Hanselmann | @type timeout: None or float
|
204 | 211b6132 | Michael Hanselmann | @param timeout: Timeout for acquiring the locks
|
205 | 900df6cd | Michael Hanselmann | @raise LockAcquireTimeout: In case locks couldn't be acquired in specified
|
206 | 900df6cd | Michael Hanselmann | amount of time
|
207 | 211b6132 | Michael Hanselmann |
|
208 | 211b6132 | Michael Hanselmann | """
|
209 | acf931b7 | Michael Hanselmann | if self._cbs: |
210 | acf931b7 | Michael Hanselmann | self._cbs.CheckCancel()
|
211 | 211b6132 | Michael Hanselmann | |
212 | 211b6132 | Michael Hanselmann | acquired = self.context.glm.acquire(level, names, shared=shared,
|
213 | f879a9c7 | Michael Hanselmann | timeout=timeout, priority=priority) |
214 | 211b6132 | Michael Hanselmann | |
215 | 900df6cd | Michael Hanselmann | if acquired is None: |
216 | 900df6cd | Michael Hanselmann | raise LockAcquireTimeout()
|
217 | 900df6cd | Michael Hanselmann | |
218 | 211b6132 | Michael Hanselmann | return acquired
|
219 | 211b6132 | Michael Hanselmann | |
220 | 6a373640 | Michael Hanselmann | def _ProcessResult(self, result): |
221 | 6a373640 | Michael Hanselmann | """
|
222 | 6a373640 | Michael Hanselmann |
|
223 | 6a373640 | Michael Hanselmann | """
|
224 | 6a373640 | Michael Hanselmann | if isinstance(result, cmdlib.ResultWithJobs): |
225 | 6a373640 | Michael Hanselmann | # Submit jobs
|
226 | 6a373640 | Michael Hanselmann | job_submission = self._cbs.SubmitManyJobs(result.jobs)
|
227 | 6a373640 | Michael Hanselmann | |
228 | 6a373640 | Michael Hanselmann | # Build dictionary
|
229 | 6a373640 | Michael Hanselmann | result = result.other |
230 | 6a373640 | Michael Hanselmann | |
231 | 6a373640 | Michael Hanselmann | assert constants.JOB_IDS_KEY not in result, \ |
232 | 6a373640 | Michael Hanselmann | "Key '%s' found in additional return values" % constants.JOB_IDS_KEY
|
233 | 6a373640 | Michael Hanselmann | |
234 | 6a373640 | Michael Hanselmann | result[constants.JOB_IDS_KEY] = job_submission |
235 | 6a373640 | Michael Hanselmann | |
236 | 6a373640 | Michael Hanselmann | return result
|
237 | 6a373640 | Michael Hanselmann | |
238 | 36c381d7 | Guido Trotter | def _ExecLU(self, lu): |
239 | 36c381d7 | Guido Trotter | """Logical Unit execution sequence.
|
240 | 36c381d7 | Guido Trotter |
|
241 | 36c381d7 | Guido Trotter | """
|
242 | 36c381d7 | Guido Trotter | write_count = self.context.cfg.write_count
|
243 | 36c381d7 | Guido Trotter | lu.CheckPrereq() |
244 | 4b5e8271 | Iustin Pop | hm = HooksMaster(self.rpc.call_hooks_runner, lu)
|
245 | 36c381d7 | Guido Trotter | h_results = hm.RunPhase(constants.HOOKS_PHASE_PRE) |
246 | 36c381d7 | Guido Trotter | lu.HooksCallBack(constants.HOOKS_PHASE_PRE, h_results, |
247 | 7b4c1cb9 | Michael Hanselmann | self.Log, None) |
248 | 20777413 | Iustin Pop | |
249 | 20777413 | Iustin Pop | if getattr(lu.op, "dry_run", False): |
250 | 20777413 | Iustin Pop | # in this mode, no post-hooks are run, and the config is not
|
251 | 20777413 | Iustin Pop | # written (as it might have been modified by another LU, and we
|
252 | 20777413 | Iustin Pop | # shouldn't do writeout on behalf of other threads
|
253 | 20777413 | Iustin Pop | self.LogInfo("dry-run mode requested, not actually executing" |
254 | 20777413 | Iustin Pop | " the operation")
|
255 | 20777413 | Iustin Pop | return lu.dry_run_result
|
256 | 20777413 | Iustin Pop | |
257 | 36c381d7 | Guido Trotter | try:
|
258 | 6a373640 | Michael Hanselmann | result = self._ProcessResult(lu.Exec(self.Log)) |
259 | 36c381d7 | Guido Trotter | h_results = hm.RunPhase(constants.HOOKS_PHASE_POST) |
260 | 36c381d7 | Guido Trotter | result = lu.HooksCallBack(constants.HOOKS_PHASE_POST, h_results, |
261 | 7b4c1cb9 | Michael Hanselmann | self.Log, result)
|
262 | 36c381d7 | Guido Trotter | finally:
|
263 | 36c381d7 | Guido Trotter | # FIXME: This needs locks if not lu_class.REQ_BGL
|
264 | 36c381d7 | Guido Trotter | if write_count != self.context.cfg.write_count: |
265 | 36c381d7 | Guido Trotter | hm.RunConfigUpdate() |
266 | 36c381d7 | Guido Trotter | |
267 | 36c381d7 | Guido Trotter | return result
|
268 | 36c381d7 | Guido Trotter | |
269 | f879a9c7 | Michael Hanselmann | def _LockAndExecLU(self, lu, level, calc_timeout, priority): |
270 | 68adfdb2 | Guido Trotter | """Execute a Logical Unit, with the needed locks.
|
271 | 68adfdb2 | Guido Trotter |
|
272 | 68adfdb2 | Guido Trotter | This is a recursive function that starts locking the given level, and
|
273 | 68adfdb2 | Guido Trotter | proceeds up, till there are no more locks to acquire. Then it executes the
|
274 | 68adfdb2 | Guido Trotter | given LU and its opcodes.
|
275 | 68adfdb2 | Guido Trotter |
|
276 | 68adfdb2 | Guido Trotter | """
|
277 | ca2a79e1 | Guido Trotter | adding_locks = level in lu.add_locks
|
278 | ca2a79e1 | Guido Trotter | acquiring_locks = level in lu.needed_locks
|
279 | 8a2941c4 | Guido Trotter | if level not in locking.LEVELS: |
280 | 031a3e57 | Michael Hanselmann | if self._cbs: |
281 | 031a3e57 | Michael Hanselmann | self._cbs.NotifyStart()
|
282 | 031a3e57 | Michael Hanselmann | |
283 | 8a2941c4 | Guido Trotter | result = self._ExecLU(lu)
|
284 | 407339d0 | Michael Hanselmann | |
285 | ca2a79e1 | Guido Trotter | elif adding_locks and acquiring_locks: |
286 | ca2a79e1 | Guido Trotter | # We could both acquire and add locks at the same level, but for now we
|
287 | ca2a79e1 | Guido Trotter | # don't need this, so we'll avoid the complicated code needed.
|
288 | 407339d0 | Michael Hanselmann | raise NotImplementedError("Can't declare locks to acquire when adding" |
289 | 407339d0 | Michael Hanselmann | " others")
|
290 | 407339d0 | Michael Hanselmann | |
291 | ca2a79e1 | Guido Trotter | elif adding_locks or acquiring_locks: |
292 | fb8dcb62 | Guido Trotter | lu.DeclareLocks(level) |
293 | 3977a4c1 | Guido Trotter | share = lu.share_locks[level] |
294 | 407339d0 | Michael Hanselmann | |
295 | 68adfdb2 | Guido Trotter | try:
|
296 | 407339d0 | Michael Hanselmann | assert adding_locks ^ acquiring_locks, \
|
297 | 407339d0 | Michael Hanselmann | "Locks must be either added or acquired"
|
298 | 407339d0 | Michael Hanselmann | |
299 | 407339d0 | Michael Hanselmann | if acquiring_locks:
|
300 | 407339d0 | Michael Hanselmann | # Acquiring locks
|
301 | 407339d0 | Michael Hanselmann | needed_locks = lu.needed_locks[level] |
302 | 407339d0 | Michael Hanselmann | |
303 | 0d5a0b96 | Michael Hanselmann | self._AcquireLocks(level, needed_locks, share,
|
304 | 0d5a0b96 | Michael Hanselmann | calc_timeout(), priority) |
305 | 407339d0 | Michael Hanselmann | else:
|
306 | 407339d0 | Michael Hanselmann | # Adding locks
|
307 | 407339d0 | Michael Hanselmann | add_locks = lu.add_locks[level] |
308 | 407339d0 | Michael Hanselmann | lu.remove_locks[level] = add_locks |
309 | 407339d0 | Michael Hanselmann | |
310 | 407339d0 | Michael Hanselmann | try:
|
311 | 407339d0 | Michael Hanselmann | self.context.glm.add(level, add_locks, acquired=1, shared=share) |
312 | 407339d0 | Michael Hanselmann | except errors.LockError:
|
313 | 407339d0 | Michael Hanselmann | raise errors.OpPrereqError(
|
314 | 407339d0 | Michael Hanselmann | "Couldn't add locks (%s), probably because of a race condition"
|
315 | debac808 | Iustin Pop | " with another job, who added them first" % add_locks,
|
316 | debac808 | Iustin Pop | errors.ECODE_FAULT) |
317 | 407339d0 | Michael Hanselmann | |
318 | ca2a79e1 | Guido Trotter | try:
|
319 | f879a9c7 | Michael Hanselmann | result = self._LockAndExecLU(lu, level + 1, calc_timeout, priority) |
320 | ca2a79e1 | Guido Trotter | finally:
|
321 | ca2a79e1 | Guido Trotter | if level in lu.remove_locks: |
322 | ca2a79e1 | Guido Trotter | self.context.glm.remove(level, lu.remove_locks[level])
|
323 | 68adfdb2 | Guido Trotter | finally:
|
324 | 80ee04a4 | Guido Trotter | if self.context.glm.is_owned(level): |
325 | 68adfdb2 | Guido Trotter | self.context.glm.release(level)
|
326 | 407339d0 | Michael Hanselmann | |
327 | 68adfdb2 | Guido Trotter | else:
|
328 | f879a9c7 | Michael Hanselmann | result = self._LockAndExecLU(lu, level + 1, calc_timeout, priority) |
329 | 68adfdb2 | Guido Trotter | |
330 | 68adfdb2 | Guido Trotter | return result
|
331 | 68adfdb2 | Guido Trotter | |
332 | f879a9c7 | Michael Hanselmann | def ExecOpCode(self, op, cbs, timeout=None, priority=None): |
333 | a8083063 | Iustin Pop | """Execute an opcode.
|
334 | a8083063 | Iustin Pop |
|
335 | e92376d7 | Iustin Pop | @type op: an OpCode instance
|
336 | e92376d7 | Iustin Pop | @param op: the opcode to be executed
|
337 | 031a3e57 | Michael Hanselmann | @type cbs: L{OpExecCbBase}
|
338 | 031a3e57 | Michael Hanselmann | @param cbs: Runtime callbacks
|
339 | 831bbbc1 | Michael Hanselmann | @type timeout: float or None
|
340 | 831bbbc1 | Michael Hanselmann | @param timeout: Maximum time to acquire all locks, None for no timeout
|
341 | f879a9c7 | Michael Hanselmann | @type priority: number or None
|
342 | f879a9c7 | Michael Hanselmann | @param priority: Priority for acquiring lock(s)
|
343 | 831bbbc1 | Michael Hanselmann | @raise LockAcquireTimeout: In case locks couldn't be acquired in specified
|
344 | 831bbbc1 | Michael Hanselmann | amount of time
|
345 | a8083063 | Iustin Pop |
|
346 | a8083063 | Iustin Pop | """
|
347 | a8083063 | Iustin Pop | if not isinstance(op, opcodes.OpCode): |
348 | 3ecf6786 | Iustin Pop | raise errors.ProgrammerError("Non-opcode instance passed" |
349 | 3ecf6786 | Iustin Pop | " to ExecOpcode")
|
350 | a8083063 | Iustin Pop | |
351 | 831bbbc1 | Michael Hanselmann | lu_class = self.DISPATCH_TABLE.get(op.__class__, None) |
352 | 831bbbc1 | Michael Hanselmann | if lu_class is None: |
353 | 831bbbc1 | Michael Hanselmann | raise errors.OpCodeUnknown("Unknown opcode") |
354 | 831bbbc1 | Michael Hanselmann | |
355 | 831bbbc1 | Michael Hanselmann | if timeout is None: |
356 | 831bbbc1 | Michael Hanselmann | calc_timeout = lambda: None |
357 | 831bbbc1 | Michael Hanselmann | else:
|
358 | 557838c1 | René Nussbaumer | calc_timeout = utils.RunningTimeout(timeout, False).Remaining
|
359 | 831bbbc1 | Michael Hanselmann | |
360 | 031a3e57 | Michael Hanselmann | self._cbs = cbs
|
361 | fe482621 | Iustin Pop | try:
|
362 | 831bbbc1 | Michael Hanselmann | # Acquire the Big Ganeti Lock exclusively if this LU requires it,
|
363 | 831bbbc1 | Michael Hanselmann | # and in a shared fashion otherwise (to prevent concurrent run with
|
364 | 831bbbc1 | Michael Hanselmann | # an exclusive LU.
|
365 | 900df6cd | Michael Hanselmann | self._AcquireLocks(locking.LEVEL_CLUSTER, locking.BGL,
|
366 | 900df6cd | Michael Hanselmann | not lu_class.REQ_BGL, calc_timeout(),
|
367 | 900df6cd | Michael Hanselmann | priority) |
368 | 831bbbc1 | Michael Hanselmann | try:
|
369 | 831bbbc1 | Michael Hanselmann | lu = lu_class(self, op, self.context, self.rpc) |
370 | 831bbbc1 | Michael Hanselmann | lu.ExpandNames() |
371 | 831bbbc1 | Michael Hanselmann | assert lu.needed_locks is not None, "needed_locks not set by LU" |
372 | 407339d0 | Michael Hanselmann | |
373 | 407339d0 | Michael Hanselmann | try:
|
374 | f879a9c7 | Michael Hanselmann | return self._LockAndExecLU(lu, locking.LEVEL_INSTANCE, calc_timeout, |
375 | f879a9c7 | Michael Hanselmann | priority) |
376 | 831bbbc1 | Michael Hanselmann | finally:
|
377 | 831bbbc1 | Michael Hanselmann | if self._ec_id: |
378 | 831bbbc1 | Michael Hanselmann | self.context.cfg.DropECReservations(self._ec_id) |
379 | 831bbbc1 | Michael Hanselmann | finally:
|
380 | 831bbbc1 | Michael Hanselmann | self.context.glm.release(locking.LEVEL_CLUSTER)
|
381 | 04864530 | Guido Trotter | finally:
|
382 | 031a3e57 | Michael Hanselmann | self._cbs = None |
383 | 6a4aa7c1 | Iustin Pop | |
384 | 7b4c1cb9 | Michael Hanselmann | def Log(self, *args): |
385 | 031a3e57 | Michael Hanselmann | """Forward call to feedback callback function.
|
386 | 031a3e57 | Michael Hanselmann |
|
387 | 031a3e57 | Michael Hanselmann | """
|
388 | 031a3e57 | Michael Hanselmann | if self._cbs: |
389 | 031a3e57 | Michael Hanselmann | self._cbs.Feedback(*args)
|
390 | 031a3e57 | Michael Hanselmann | |
391 | 0fbbf897 | Iustin Pop | def LogStep(self, current, total, message): |
392 | 0fbbf897 | Iustin Pop | """Log a change in LU execution progress.
|
393 | 0fbbf897 | Iustin Pop |
|
394 | 0fbbf897 | Iustin Pop | """
|
395 | a5eb7789 | Iustin Pop | logging.debug("Step %d/%d %s", current, total, message)
|
396 | 7b4c1cb9 | Michael Hanselmann | self.Log("STEP %d/%d %s" % (current, total, message)) |
397 | 0fbbf897 | Iustin Pop | |
398 | c0088fb9 | Iustin Pop | def LogWarning(self, message, *args, **kwargs): |
399 | 0fbbf897 | Iustin Pop | """Log a warning to the logs and the user.
|
400 | 0fbbf897 | Iustin Pop |
|
401 | c0088fb9 | Iustin Pop | The optional keyword argument is 'hint' and can be used to show a
|
402 | c0088fb9 | Iustin Pop | hint to the user (presumably related to the warning). If the
|
403 | c0088fb9 | Iustin Pop | message is empty, it will not be printed at all, allowing one to
|
404 | c0088fb9 | Iustin Pop | show only a hint.
|
405 | 0fbbf897 | Iustin Pop |
|
406 | c0088fb9 | Iustin Pop | """
|
407 | c0088fb9 | Iustin Pop | assert not kwargs or (len(kwargs) == 1 and "hint" in kwargs), \ |
408 | c0088fb9 | Iustin Pop | "Invalid keyword arguments for LogWarning (%s)" % str(kwargs) |
409 | c0088fb9 | Iustin Pop | if args:
|
410 | c0088fb9 | Iustin Pop | message = message % tuple(args)
|
411 | c0088fb9 | Iustin Pop | if message:
|
412 | c0088fb9 | Iustin Pop | logging.warning(message) |
413 | 7b4c1cb9 | Michael Hanselmann | self.Log(" - WARNING: %s" % message) |
414 | c0088fb9 | Iustin Pop | if "hint" in kwargs: |
415 | 7b4c1cb9 | Michael Hanselmann | self.Log(" Hint: %s" % kwargs["hint"]) |
416 | c0088fb9 | Iustin Pop | |
417 | c0088fb9 | Iustin Pop | def LogInfo(self, message, *args): |
418 | 0fbbf897 | Iustin Pop | """Log an informational message to the logs and the user.
|
419 | 0fbbf897 | Iustin Pop |
|
420 | 0fbbf897 | Iustin Pop | """
|
421 | c0088fb9 | Iustin Pop | if args:
|
422 | c0088fb9 | Iustin Pop | message = message % tuple(args)
|
423 | a5eb7789 | Iustin Pop | logging.info(message) |
424 | 7b4c1cb9 | Michael Hanselmann | self.Log(" - INFO: %s" % message) |
425 | 0fbbf897 | Iustin Pop | |
426 | adfa97e3 | Guido Trotter | def GetECId(self): |
427 | 3ae70d76 | Michael Hanselmann | """Returns the current execution context ID.
|
428 | 3ae70d76 | Michael Hanselmann |
|
429 | 3ae70d76 | Michael Hanselmann | """
|
430 | adfa97e3 | Guido Trotter | if not self._ec_id: |
431 | 3ae70d76 | Michael Hanselmann | raise errors.ProgrammerError("Tried to use execution context id when" |
432 | 3ae70d76 | Michael Hanselmann | " not set")
|
433 | adfa97e3 | Guido Trotter | return self._ec_id |
434 | adfa97e3 | Guido Trotter | |
435 | a8083063 | Iustin Pop | |
436 | a8083063 | Iustin Pop | class HooksMaster(object): |
437 | a8083063 | Iustin Pop | """Hooks master.
|
438 | a8083063 | Iustin Pop |
|
439 | a8083063 | Iustin Pop | This class distributes the run commands to the nodes based on the
|
440 | a8083063 | Iustin Pop | specific LU class.
|
441 | a8083063 | Iustin Pop |
|
442 | a8083063 | Iustin Pop | In order to remove the direct dependency on the rpc module, the
|
443 | a8083063 | Iustin Pop | constructor needs a function which actually does the remote
|
444 | a8083063 | Iustin Pop | call. This will usually be rpc.call_hooks_runner, but any function
|
445 | a8083063 | Iustin Pop | which behaves the same works.
|
446 | a8083063 | Iustin Pop |
|
447 | a8083063 | Iustin Pop | """
|
448 | 4b5e8271 | Iustin Pop | def __init__(self, callfn, lu): |
449 | a8083063 | Iustin Pop | self.callfn = callfn
|
450 | a8083063 | Iustin Pop | self.lu = lu
|
451 | a8083063 | Iustin Pop | self.op = lu.op
|
452 | 07e0896f | Michael Hanselmann | self.pre_env = self._BuildEnv(constants.HOOKS_PHASE_PRE) |
453 | 07e0896f | Michael Hanselmann | |
454 | 07e0896f | Michael Hanselmann | if self.lu.HPATH is None: |
455 | 07e0896f | Michael Hanselmann | nodes = (None, None) |
456 | 07e0896f | Michael Hanselmann | else:
|
457 | 07e0896f | Michael Hanselmann | nodes = map(frozenset, self.lu.BuildHooksNodes()) |
458 | 07e0896f | Michael Hanselmann | |
459 | 07e0896f | Michael Hanselmann | (self.pre_nodes, self.post_nodes) = nodes |
460 | a8083063 | Iustin Pop | |
461 | dd7f6776 | Michael Hanselmann | def _BuildEnv(self, phase): |
462 | a8083063 | Iustin Pop | """Compute the environment and the target nodes.
|
463 | a8083063 | Iustin Pop |
|
464 | a8083063 | Iustin Pop | Based on the opcode and the current node list, this builds the
|
465 | a8083063 | Iustin Pop | environment for the hooks and the target node list for the run.
|
466 | a8083063 | Iustin Pop |
|
467 | a8083063 | Iustin Pop | """
|
468 | dd7f6776 | Michael Hanselmann | if phase == constants.HOOKS_PHASE_PRE:
|
469 | dd7f6776 | Michael Hanselmann | prefix = "GANETI_"
|
470 | dd7f6776 | Michael Hanselmann | elif phase == constants.HOOKS_PHASE_POST:
|
471 | dd7f6776 | Michael Hanselmann | prefix = "GANETI_POST_"
|
472 | dd7f6776 | Michael Hanselmann | else:
|
473 | dd7f6776 | Michael Hanselmann | raise AssertionError("Unknown phase '%s'" % phase) |
474 | dd7f6776 | Michael Hanselmann | |
475 | dd7f6776 | Michael Hanselmann | env = {} |
476 | a8083063 | Iustin Pop | |
477 | 9a395a76 | Iustin Pop | if self.lu.HPATH is not None: |
478 | 07e0896f | Michael Hanselmann | lu_env = self.lu.BuildHooksEnv()
|
479 | 9a395a76 | Iustin Pop | if lu_env:
|
480 | 07e0896f | Michael Hanselmann | assert not compat.any(key.upper().startswith(prefix) for key in lu_env) |
481 | dd7f6776 | Michael Hanselmann | env.update(("%s%s" % (prefix, key), value)
|
482 | dd7f6776 | Michael Hanselmann | for (key, value) in lu_env.items()) |
483 | a8083063 | Iustin Pop | |
484 | dd7f6776 | Michael Hanselmann | if phase == constants.HOOKS_PHASE_PRE:
|
485 | dd7f6776 | Michael Hanselmann | assert compat.all((key.startswith("GANETI_") and |
486 | dd7f6776 | Michael Hanselmann | not key.startswith("GANETI_POST_")) |
487 | dd7f6776 | Michael Hanselmann | for key in env) |
488 | dd7f6776 | Michael Hanselmann | |
489 | dd7f6776 | Michael Hanselmann | elif phase == constants.HOOKS_PHASE_POST:
|
490 | dd7f6776 | Michael Hanselmann | assert compat.all(key.startswith("GANETI_POST_") for key in env) |
491 | 07e0896f | Michael Hanselmann | assert isinstance(self.pre_env, dict) |
492 | dd7f6776 | Michael Hanselmann | |
493 | 07e0896f | Michael Hanselmann | # Merge with pre-phase environment
|
494 | 07e0896f | Michael Hanselmann | assert not compat.any(key.startswith("GANETI_POST_") |
495 | 07e0896f | Michael Hanselmann | for key in self.pre_env) |
496 | 07e0896f | Michael Hanselmann | env.update(self.pre_env)
|
497 | dd7f6776 | Michael Hanselmann | else:
|
498 | dd7f6776 | Michael Hanselmann | raise AssertionError("Unknown phase '%s'" % phase) |
499 | dd7f6776 | Michael Hanselmann | |
500 | 07e0896f | Michael Hanselmann | return env
|
501 | 4167825b | Iustin Pop | |
502 | dd7f6776 | Michael Hanselmann | def _RunWrapper(self, node_list, hpath, phase, phase_env): |
503 | 4167825b | Iustin Pop | """Simple wrapper over self.callfn.
|
504 | 4167825b | Iustin Pop |
|
505 | 4167825b | Iustin Pop | This method fixes the environment before doing the rpc call.
|
506 | 4167825b | Iustin Pop |
|
507 | 4167825b | Iustin Pop | """
|
508 | dd7f6776 | Michael Hanselmann | cfg = self.lu.cfg
|
509 | dd7f6776 | Michael Hanselmann | |
510 | dd7f6776 | Michael Hanselmann | env = { |
511 | dd7f6776 | Michael Hanselmann | "PATH": "/sbin:/bin:/usr/sbin:/usr/bin", |
512 | dd7f6776 | Michael Hanselmann | "GANETI_HOOKS_VERSION": constants.HOOKS_VERSION,
|
513 | dd7f6776 | Michael Hanselmann | "GANETI_OP_CODE": self.op.OP_ID, |
514 | dd7f6776 | Michael Hanselmann | "GANETI_DATA_DIR": constants.DATA_DIR,
|
515 | dd7f6776 | Michael Hanselmann | "GANETI_HOOKS_PHASE": phase,
|
516 | dd7f6776 | Michael Hanselmann | "GANETI_HOOKS_PATH": hpath,
|
517 | dd7f6776 | Michael Hanselmann | } |
518 | dd7f6776 | Michael Hanselmann | |
519 | 07e0896f | Michael Hanselmann | if self.lu.HTYPE: |
520 | 07e0896f | Michael Hanselmann | env["GANETI_OBJECT_TYPE"] = self.lu.HTYPE |
521 | 07e0896f | Michael Hanselmann | |
522 | dd7f6776 | Michael Hanselmann | if cfg is not None: |
523 | dd7f6776 | Michael Hanselmann | env["GANETI_CLUSTER"] = cfg.GetClusterName()
|
524 | dd7f6776 | Michael Hanselmann | env["GANETI_MASTER"] = cfg.GetMasterNode()
|
525 | dd7f6776 | Michael Hanselmann | |
526 | dd7f6776 | Michael Hanselmann | if phase_env:
|
527 | dd7f6776 | Michael Hanselmann | assert not (set(env) & set(phase_env)), "Environment variables conflict" |
528 | dd7f6776 | Michael Hanselmann | env.update(phase_env) |
529 | a8083063 | Iustin Pop | |
530 | dd7f6776 | Michael Hanselmann | # Convert everything to strings
|
531 | 4167825b | Iustin Pop | env = dict([(str(key), str(val)) for key, val in env.iteritems()]) |
532 | a8083063 | Iustin Pop | |
533 | dd7f6776 | Michael Hanselmann | assert compat.all(key == "PATH" or key.startswith("GANETI_") |
534 | ebc75510 | Michael Hanselmann | for key in env) |
535 | ebc75510 | Michael Hanselmann | |
536 | 4167825b | Iustin Pop | return self.callfn(node_list, hpath, phase, env) |
537 | a8083063 | Iustin Pop | |
538 | 17e82923 | Luca Bigliardi | def RunPhase(self, phase, nodes=None): |
539 | a8083063 | Iustin Pop | """Run all the scripts for a phase.
|
540 | a8083063 | Iustin Pop |
|
541 | a8083063 | Iustin Pop | This is the main function of the HookMaster.
|
542 | a8083063 | Iustin Pop |
|
543 | 8dca23a3 | Iustin Pop | @param phase: one of L{constants.HOOKS_PHASE_POST} or
|
544 | 8dca23a3 | Iustin Pop | L{constants.HOOKS_PHASE_PRE}; it denotes the hooks phase
|
545 | 17e82923 | Luca Bigliardi | @param nodes: overrides the predefined list of nodes for the given phase
|
546 | 8dca23a3 | Iustin Pop | @return: the processed results of the hooks multi-node rpc call
|
547 | 8dca23a3 | Iustin Pop | @raise errors.HooksFailure: on communication failure to the nodes
|
548 | 6ef2dc74 | Luca Bigliardi | @raise errors.HooksAbort: on failure of one of the hooks
|
549 | b07a6922 | Guido Trotter |
|
550 | a8083063 | Iustin Pop | """
|
551 | 07e0896f | Michael Hanselmann | if phase == constants.HOOKS_PHASE_PRE:
|
552 | 07e0896f | Michael Hanselmann | if nodes is None: |
553 | 07e0896f | Michael Hanselmann | nodes = self.pre_nodes
|
554 | 07e0896f | Michael Hanselmann | env = self.pre_env
|
555 | 07e0896f | Michael Hanselmann | elif phase == constants.HOOKS_PHASE_POST:
|
556 | 07e0896f | Michael Hanselmann | if nodes is None: |
557 | b423c513 | Michael Hanselmann | nodes = self.post_nodes
|
558 | 07e0896f | Michael Hanselmann | env = self._BuildEnv(phase)
|
559 | 07e0896f | Michael Hanselmann | else:
|
560 | 07e0896f | Michael Hanselmann | raise AssertionError("Unknown phase '%s'" % phase) |
561 | 0306ff62 | Michael Hanselmann | |
562 | 0306ff62 | Michael Hanselmann | if not nodes: |
563 | 9a395a76 | Iustin Pop | # empty node list, we should not attempt to run this as either
|
564 | 9a395a76 | Iustin Pop | # we're in the cluster init phase and the rpc client part can't
|
565 | 9a395a76 | Iustin Pop | # even attempt to run, or this LU doesn't do hooks at all
|
566 | a8083063 | Iustin Pop | return
|
567 | 0306ff62 | Michael Hanselmann | |
568 | dd7f6776 | Michael Hanselmann | results = self._RunWrapper(nodes, self.lu.HPATH, phase, env) |
569 | 8c4b9364 | Luca Bigliardi | if not results: |
570 | 8c4b9364 | Luca Bigliardi | msg = "Communication Failure"
|
571 | 8c4b9364 | Luca Bigliardi | if phase == constants.HOOKS_PHASE_PRE:
|
572 | 8c4b9364 | Luca Bigliardi | raise errors.HooksFailure(msg)
|
573 | 8c4b9364 | Luca Bigliardi | else:
|
574 | 8c4b9364 | Luca Bigliardi | self.lu.LogWarning(msg)
|
575 | 640b961e | Luca Bigliardi | return results
|
576 | 0306ff62 | Michael Hanselmann | |
577 | 0306ff62 | Michael Hanselmann | errs = [] |
578 | 8c4b9364 | Luca Bigliardi | for node_name in results: |
579 | 8c4b9364 | Luca Bigliardi | res = results[node_name] |
580 | 8c4b9364 | Luca Bigliardi | if res.offline:
|
581 | 8c4b9364 | Luca Bigliardi | continue
|
582 | 0306ff62 | Michael Hanselmann | |
583 | 3cebe102 | Michael Hanselmann | msg = res.fail_msg |
584 | 8c4b9364 | Luca Bigliardi | if msg:
|
585 | 8c4b9364 | Luca Bigliardi | self.lu.LogWarning("Communication failure to node %s: %s", |
586 | 8c4b9364 | Luca Bigliardi | node_name, msg) |
587 | 8c4b9364 | Luca Bigliardi | continue
|
588 | 0306ff62 | Michael Hanselmann | |
589 | 8c4b9364 | Luca Bigliardi | for script, hkr, output in res.payload: |
590 | 8c4b9364 | Luca Bigliardi | if hkr == constants.HKR_FAIL:
|
591 | 8c4b9364 | Luca Bigliardi | if phase == constants.HOOKS_PHASE_PRE:
|
592 | a8083063 | Iustin Pop | errs.append((node_name, script, output)) |
593 | 8c4b9364 | Luca Bigliardi | else:
|
594 | 8c4b9364 | Luca Bigliardi | if not output: |
595 | 640b961e | Luca Bigliardi | output = "(no output)"
|
596 | 8c4b9364 | Luca Bigliardi | self.lu.LogWarning("On %s script %s failed, output: %s" % |
597 | 8c4b9364 | Luca Bigliardi | (node_name, script, output)) |
598 | 0306ff62 | Michael Hanselmann | |
599 | 8c4b9364 | Luca Bigliardi | if errs and phase == constants.HOOKS_PHASE_PRE: |
600 | 8c4b9364 | Luca Bigliardi | raise errors.HooksAbort(errs)
|
601 | 0306ff62 | Michael Hanselmann | |
602 | b07a6922 | Guido Trotter | return results
|
603 | 6a4aa7c1 | Iustin Pop | |
604 | 6a4aa7c1 | Iustin Pop | def RunConfigUpdate(self): |
605 | 6a4aa7c1 | Iustin Pop | """Run the special configuration update hook
|
606 | 6a4aa7c1 | Iustin Pop |
|
607 | 6a4aa7c1 | Iustin Pop | This is a special hook that runs only on the master after each
|
608 | 6a4aa7c1 | Iustin Pop | top-level LI if the configuration has been updated.
|
609 | 6a4aa7c1 | Iustin Pop |
|
610 | 6a4aa7c1 | Iustin Pop | """
|
611 | 6a4aa7c1 | Iustin Pop | phase = constants.HOOKS_PHASE_POST |
612 | 6a4aa7c1 | Iustin Pop | hpath = constants.HOOKS_NAME_CFGUPDATE |
613 | 437138c9 | Michael Hanselmann | nodes = [self.lu.cfg.GetMasterNode()]
|
614 | dd7f6776 | Michael Hanselmann | self._RunWrapper(nodes, hpath, phase, self.pre_env) |