root / lib / jqueue.py @ c3029d0a
History | View | Annotate | Download (40.4 kB)
1 | 498ae1cc | Iustin Pop | #
|
---|---|---|---|
2 | 498ae1cc | Iustin Pop | #
|
3 | 498ae1cc | Iustin Pop | |
4 | 5685c1a5 | Michael Hanselmann | # Copyright (C) 2006, 2007, 2008 Google Inc.
|
5 | 498ae1cc | Iustin Pop | #
|
6 | 498ae1cc | Iustin Pop | # This program is free software; you can redistribute it and/or modify
|
7 | 498ae1cc | Iustin Pop | # it under the terms of the GNU General Public License as published by
|
8 | 498ae1cc | Iustin Pop | # the Free Software Foundation; either version 2 of the License, or
|
9 | 498ae1cc | Iustin Pop | # (at your option) any later version.
|
10 | 498ae1cc | Iustin Pop | #
|
11 | 498ae1cc | Iustin Pop | # This program is distributed in the hope that it will be useful, but
|
12 | 498ae1cc | Iustin Pop | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 498ae1cc | Iustin Pop | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 498ae1cc | Iustin Pop | # General Public License for more details.
|
15 | 498ae1cc | Iustin Pop | #
|
16 | 498ae1cc | Iustin Pop | # You should have received a copy of the GNU General Public License
|
17 | 498ae1cc | Iustin Pop | # along with this program; if not, write to the Free Software
|
18 | 498ae1cc | Iustin Pop | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | 498ae1cc | Iustin Pop | # 02110-1301, USA.
|
20 | 498ae1cc | Iustin Pop | |
21 | 498ae1cc | Iustin Pop | |
22 | 6c5a7090 | Michael Hanselmann | """Module implementing the job queue handling.
|
23 | 6c5a7090 | Michael Hanselmann |
|
24 | ea03467c | Iustin Pop | Locking: there's a single, large lock in the L{JobQueue} class. It's
|
25 | ea03467c | Iustin Pop | used by all other classes in this module.
|
26 | ea03467c | Iustin Pop |
|
27 | ea03467c | Iustin Pop | @var JOBQUEUE_THREADS: the number of worker threads we start for
|
28 | ea03467c | Iustin Pop | processing jobs
|
29 | 6c5a7090 | Michael Hanselmann |
|
30 | 6c5a7090 | Michael Hanselmann | """
|
31 | 498ae1cc | Iustin Pop | |
32 | f1da30e6 | Michael Hanselmann | import os |
33 | e2715f69 | Michael Hanselmann | import logging |
34 | e2715f69 | Michael Hanselmann | import threading |
35 | f1da30e6 | Michael Hanselmann | import errno |
36 | f1da30e6 | Michael Hanselmann | import re |
37 | f1048938 | Iustin Pop | import time |
38 | 5685c1a5 | Michael Hanselmann | import weakref |
39 | 498ae1cc | Iustin Pop | |
40 | e2715f69 | Michael Hanselmann | from ganeti import constants |
41 | f1da30e6 | Michael Hanselmann | from ganeti import serializer |
42 | e2715f69 | Michael Hanselmann | from ganeti import workerpool |
43 | f1da30e6 | Michael Hanselmann | from ganeti import opcodes |
44 | 7a1ecaed | Iustin Pop | from ganeti import errors |
45 | e2715f69 | Michael Hanselmann | from ganeti import mcpu |
46 | 7996a135 | Iustin Pop | from ganeti import utils |
47 | 04ab05ce | Michael Hanselmann | from ganeti import jstore |
48 | c3f0a12f | Iustin Pop | from ganeti import rpc |
49 | e2715f69 | Michael Hanselmann | |
50 | fbf0262f | Michael Hanselmann | |
51 | 1daae384 | Iustin Pop | JOBQUEUE_THREADS = 25
|
52 | 58b22b6e | Michael Hanselmann | JOBS_PER_ARCHIVE_DIRECTORY = 10000
|
53 | e2715f69 | Michael Hanselmann | |
54 | 498ae1cc | Iustin Pop | |
55 | 9728ae5d | Iustin Pop | class CancelJob(Exception): |
56 | fbf0262f | Michael Hanselmann | """Special exception to cancel a job.
|
57 | fbf0262f | Michael Hanselmann |
|
58 | fbf0262f | Michael Hanselmann | """
|
59 | fbf0262f | Michael Hanselmann | |
60 | fbf0262f | Michael Hanselmann | |
61 | 70552c46 | Michael Hanselmann | def TimeStampNow(): |
62 | ea03467c | Iustin Pop | """Returns the current timestamp.
|
63 | ea03467c | Iustin Pop |
|
64 | ea03467c | Iustin Pop | @rtype: tuple
|
65 | ea03467c | Iustin Pop | @return: the current time in the (seconds, microseconds) format
|
66 | ea03467c | Iustin Pop |
|
67 | ea03467c | Iustin Pop | """
|
68 | 70552c46 | Michael Hanselmann | return utils.SplitTime(time.time())
|
69 | 70552c46 | Michael Hanselmann | |
70 | 70552c46 | Michael Hanselmann | |
71 | e2715f69 | Michael Hanselmann | class _QueuedOpCode(object): |
72 | 5bbd3f7f | Michael Hanselmann | """Encapsulates an opcode object.
|
73 | e2715f69 | Michael Hanselmann |
|
74 | ea03467c | Iustin Pop | @ivar log: holds the execution log and consists of tuples
|
75 | ea03467c | Iustin Pop | of the form C{(log_serial, timestamp, level, message)}
|
76 | ea03467c | Iustin Pop | @ivar input: the OpCode we encapsulate
|
77 | ea03467c | Iustin Pop | @ivar status: the current status
|
78 | ea03467c | Iustin Pop | @ivar result: the result of the LU execution
|
79 | ea03467c | Iustin Pop | @ivar start_timestamp: timestamp for the start of the execution
|
80 | ea03467c | Iustin Pop | @ivar stop_timestamp: timestamp for the end of the execution
|
81 | f1048938 | Iustin Pop |
|
82 | e2715f69 | Michael Hanselmann | """
|
83 | 66d895a8 | Iustin Pop | __slots__ = ["input", "status", "result", "log", |
84 | 66d895a8 | Iustin Pop | "start_timestamp", "end_timestamp", |
85 | 66d895a8 | Iustin Pop | "__weakref__"]
|
86 | 66d895a8 | Iustin Pop | |
87 | 85f03e0d | Michael Hanselmann | def __init__(self, op): |
88 | ea03467c | Iustin Pop | """Constructor for the _QuededOpCode.
|
89 | ea03467c | Iustin Pop |
|
90 | ea03467c | Iustin Pop | @type op: L{opcodes.OpCode}
|
91 | ea03467c | Iustin Pop | @param op: the opcode we encapsulate
|
92 | ea03467c | Iustin Pop |
|
93 | ea03467c | Iustin Pop | """
|
94 | 85f03e0d | Michael Hanselmann | self.input = op
|
95 | 85f03e0d | Michael Hanselmann | self.status = constants.OP_STATUS_QUEUED
|
96 | 85f03e0d | Michael Hanselmann | self.result = None |
97 | 85f03e0d | Michael Hanselmann | self.log = []
|
98 | 70552c46 | Michael Hanselmann | self.start_timestamp = None |
99 | 70552c46 | Michael Hanselmann | self.end_timestamp = None |
100 | f1da30e6 | Michael Hanselmann | |
101 | f1da30e6 | Michael Hanselmann | @classmethod
|
102 | f1da30e6 | Michael Hanselmann | def Restore(cls, state): |
103 | ea03467c | Iustin Pop | """Restore the _QueuedOpCode from the serialized form.
|
104 | ea03467c | Iustin Pop |
|
105 | ea03467c | Iustin Pop | @type state: dict
|
106 | ea03467c | Iustin Pop | @param state: the serialized state
|
107 | ea03467c | Iustin Pop | @rtype: _QueuedOpCode
|
108 | ea03467c | Iustin Pop | @return: a new _QueuedOpCode instance
|
109 | ea03467c | Iustin Pop |
|
110 | ea03467c | Iustin Pop | """
|
111 | 85f03e0d | Michael Hanselmann | obj = _QueuedOpCode.__new__(cls) |
112 | 85f03e0d | Michael Hanselmann | obj.input = opcodes.OpCode.LoadOpCode(state["input"])
|
113 | 85f03e0d | Michael Hanselmann | obj.status = state["status"]
|
114 | 85f03e0d | Michael Hanselmann | obj.result = state["result"]
|
115 | 85f03e0d | Michael Hanselmann | obj.log = state["log"]
|
116 | 70552c46 | Michael Hanselmann | obj.start_timestamp = state.get("start_timestamp", None) |
117 | 70552c46 | Michael Hanselmann | obj.end_timestamp = state.get("end_timestamp", None) |
118 | f1da30e6 | Michael Hanselmann | return obj
|
119 | f1da30e6 | Michael Hanselmann | |
120 | f1da30e6 | Michael Hanselmann | def Serialize(self): |
121 | ea03467c | Iustin Pop | """Serializes this _QueuedOpCode.
|
122 | ea03467c | Iustin Pop |
|
123 | ea03467c | Iustin Pop | @rtype: dict
|
124 | ea03467c | Iustin Pop | @return: the dictionary holding the serialized state
|
125 | ea03467c | Iustin Pop |
|
126 | ea03467c | Iustin Pop | """
|
127 | 6c5a7090 | Michael Hanselmann | return {
|
128 | 6c5a7090 | Michael Hanselmann | "input": self.input.__getstate__(), |
129 | 6c5a7090 | Michael Hanselmann | "status": self.status, |
130 | 6c5a7090 | Michael Hanselmann | "result": self.result, |
131 | 6c5a7090 | Michael Hanselmann | "log": self.log, |
132 | 70552c46 | Michael Hanselmann | "start_timestamp": self.start_timestamp, |
133 | 70552c46 | Michael Hanselmann | "end_timestamp": self.end_timestamp, |
134 | 6c5a7090 | Michael Hanselmann | } |
135 | f1048938 | Iustin Pop | |
136 | e2715f69 | Michael Hanselmann | |
137 | e2715f69 | Michael Hanselmann | class _QueuedJob(object): |
138 | e2715f69 | Michael Hanselmann | """In-memory job representation.
|
139 | e2715f69 | Michael Hanselmann |
|
140 | ea03467c | Iustin Pop | This is what we use to track the user-submitted jobs. Locking must
|
141 | ea03467c | Iustin Pop | be taken care of by users of this class.
|
142 | ea03467c | Iustin Pop |
|
143 | ea03467c | Iustin Pop | @type queue: L{JobQueue}
|
144 | ea03467c | Iustin Pop | @ivar queue: the parent queue
|
145 | ea03467c | Iustin Pop | @ivar id: the job ID
|
146 | ea03467c | Iustin Pop | @type ops: list
|
147 | ea03467c | Iustin Pop | @ivar ops: the list of _QueuedOpCode that constitute the job
|
148 | ea03467c | Iustin Pop | @type run_op_index: int
|
149 | ea03467c | Iustin Pop | @ivar run_op_index: the currently executing opcode, or -1 if
|
150 | ea03467c | Iustin Pop | we didn't yet start executing
|
151 | ea03467c | Iustin Pop | @type log_serial: int
|
152 | ea03467c | Iustin Pop | @ivar log_serial: holds the index for the next log entry
|
153 | ea03467c | Iustin Pop | @ivar received_timestamp: the timestamp for when the job was received
|
154 | ea03467c | Iustin Pop | @ivar start_timestmap: the timestamp for start of execution
|
155 | ea03467c | Iustin Pop | @ivar end_timestamp: the timestamp for end of execution
|
156 | ea03467c | Iustin Pop | @ivar change: a Condition variable we use for waiting for job changes
|
157 | e2715f69 | Michael Hanselmann |
|
158 | e2715f69 | Michael Hanselmann | """
|
159 | 66d895a8 | Iustin Pop | __slots__ = ["queue", "id", "ops", "run_op_index", "log_serial", |
160 | 66d895a8 | Iustin Pop | "received_timestamp", "start_timestamp", "end_timestamp", |
161 | 66d895a8 | Iustin Pop | "change",
|
162 | 66d895a8 | Iustin Pop | "__weakref__"]
|
163 | 66d895a8 | Iustin Pop | |
164 | 85f03e0d | Michael Hanselmann | def __init__(self, queue, job_id, ops): |
165 | ea03467c | Iustin Pop | """Constructor for the _QueuedJob.
|
166 | ea03467c | Iustin Pop |
|
167 | ea03467c | Iustin Pop | @type queue: L{JobQueue}
|
168 | ea03467c | Iustin Pop | @param queue: our parent queue
|
169 | ea03467c | Iustin Pop | @type job_id: job_id
|
170 | ea03467c | Iustin Pop | @param job_id: our job id
|
171 | ea03467c | Iustin Pop | @type ops: list
|
172 | ea03467c | Iustin Pop | @param ops: the list of opcodes we hold, which will be encapsulated
|
173 | ea03467c | Iustin Pop | in _QueuedOpCodes
|
174 | ea03467c | Iustin Pop |
|
175 | ea03467c | Iustin Pop | """
|
176 | e2715f69 | Michael Hanselmann | if not ops: |
177 | ea03467c | Iustin Pop | # TODO: use a better exception
|
178 | e2715f69 | Michael Hanselmann | raise Exception("No opcodes") |
179 | e2715f69 | Michael Hanselmann | |
180 | 85f03e0d | Michael Hanselmann | self.queue = queue
|
181 | f1da30e6 | Michael Hanselmann | self.id = job_id
|
182 | 85f03e0d | Michael Hanselmann | self.ops = [_QueuedOpCode(op) for op in ops] |
183 | 85f03e0d | Michael Hanselmann | self.run_op_index = -1 |
184 | 6c5a7090 | Michael Hanselmann | self.log_serial = 0 |
185 | c56ec146 | Iustin Pop | self.received_timestamp = TimeStampNow()
|
186 | c56ec146 | Iustin Pop | self.start_timestamp = None |
187 | c56ec146 | Iustin Pop | self.end_timestamp = None |
188 | 6c5a7090 | Michael Hanselmann | |
189 | 6c5a7090 | Michael Hanselmann | # Condition to wait for changes
|
190 | 6c5a7090 | Michael Hanselmann | self.change = threading.Condition(self.queue._lock) |
191 | f1da30e6 | Michael Hanselmann | |
192 | f1da30e6 | Michael Hanselmann | @classmethod
|
193 | 85f03e0d | Michael Hanselmann | def Restore(cls, queue, state): |
194 | ea03467c | Iustin Pop | """Restore a _QueuedJob from serialized state:
|
195 | ea03467c | Iustin Pop |
|
196 | ea03467c | Iustin Pop | @type queue: L{JobQueue}
|
197 | ea03467c | Iustin Pop | @param queue: to which queue the restored job belongs
|
198 | ea03467c | Iustin Pop | @type state: dict
|
199 | ea03467c | Iustin Pop | @param state: the serialized state
|
200 | ea03467c | Iustin Pop | @rtype: _JobQueue
|
201 | ea03467c | Iustin Pop | @return: the restored _JobQueue instance
|
202 | ea03467c | Iustin Pop |
|
203 | ea03467c | Iustin Pop | """
|
204 | 85f03e0d | Michael Hanselmann | obj = _QueuedJob.__new__(cls) |
205 | 85f03e0d | Michael Hanselmann | obj.queue = queue |
206 | 85f03e0d | Michael Hanselmann | obj.id = state["id"]
|
207 | 85f03e0d | Michael Hanselmann | obj.run_op_index = state["run_op_index"]
|
208 | c56ec146 | Iustin Pop | obj.received_timestamp = state.get("received_timestamp", None) |
209 | c56ec146 | Iustin Pop | obj.start_timestamp = state.get("start_timestamp", None) |
210 | c56ec146 | Iustin Pop | obj.end_timestamp = state.get("end_timestamp", None) |
211 | 6c5a7090 | Michael Hanselmann | |
212 | 6c5a7090 | Michael Hanselmann | obj.ops = [] |
213 | 6c5a7090 | Michael Hanselmann | obj.log_serial = 0
|
214 | 6c5a7090 | Michael Hanselmann | for op_state in state["ops"]: |
215 | 6c5a7090 | Michael Hanselmann | op = _QueuedOpCode.Restore(op_state) |
216 | 6c5a7090 | Michael Hanselmann | for log_entry in op.log: |
217 | 6c5a7090 | Michael Hanselmann | obj.log_serial = max(obj.log_serial, log_entry[0]) |
218 | 6c5a7090 | Michael Hanselmann | obj.ops.append(op) |
219 | 6c5a7090 | Michael Hanselmann | |
220 | 6c5a7090 | Michael Hanselmann | # Condition to wait for changes
|
221 | 6c5a7090 | Michael Hanselmann | obj.change = threading.Condition(obj.queue._lock) |
222 | 6c5a7090 | Michael Hanselmann | |
223 | f1da30e6 | Michael Hanselmann | return obj
|
224 | f1da30e6 | Michael Hanselmann | |
225 | f1da30e6 | Michael Hanselmann | def Serialize(self): |
226 | ea03467c | Iustin Pop | """Serialize the _JobQueue instance.
|
227 | ea03467c | Iustin Pop |
|
228 | ea03467c | Iustin Pop | @rtype: dict
|
229 | ea03467c | Iustin Pop | @return: the serialized state
|
230 | ea03467c | Iustin Pop |
|
231 | ea03467c | Iustin Pop | """
|
232 | f1da30e6 | Michael Hanselmann | return {
|
233 | f1da30e6 | Michael Hanselmann | "id": self.id, |
234 | 85f03e0d | Michael Hanselmann | "ops": [op.Serialize() for op in self.ops], |
235 | f1048938 | Iustin Pop | "run_op_index": self.run_op_index, |
236 | c56ec146 | Iustin Pop | "start_timestamp": self.start_timestamp, |
237 | c56ec146 | Iustin Pop | "end_timestamp": self.end_timestamp, |
238 | c56ec146 | Iustin Pop | "received_timestamp": self.received_timestamp, |
239 | f1da30e6 | Michael Hanselmann | } |
240 | f1da30e6 | Michael Hanselmann | |
241 | 85f03e0d | Michael Hanselmann | def CalcStatus(self): |
242 | ea03467c | Iustin Pop | """Compute the status of this job.
|
243 | ea03467c | Iustin Pop |
|
244 | ea03467c | Iustin Pop | This function iterates over all the _QueuedOpCodes in the job and
|
245 | ea03467c | Iustin Pop | based on their status, computes the job status.
|
246 | ea03467c | Iustin Pop |
|
247 | ea03467c | Iustin Pop | The algorithm is:
|
248 | ea03467c | Iustin Pop | - if we find a cancelled, or finished with error, the job
|
249 | ea03467c | Iustin Pop | status will be the same
|
250 | ea03467c | Iustin Pop | - otherwise, the last opcode with the status one of:
|
251 | ea03467c | Iustin Pop | - waitlock
|
252 | fbf0262f | Michael Hanselmann | - canceling
|
253 | ea03467c | Iustin Pop | - running
|
254 | ea03467c | Iustin Pop |
|
255 | ea03467c | Iustin Pop | will determine the job status
|
256 | ea03467c | Iustin Pop |
|
257 | ea03467c | Iustin Pop | - otherwise, it means either all opcodes are queued, or success,
|
258 | ea03467c | Iustin Pop | and the job status will be the same
|
259 | ea03467c | Iustin Pop |
|
260 | ea03467c | Iustin Pop | @return: the job status
|
261 | ea03467c | Iustin Pop |
|
262 | ea03467c | Iustin Pop | """
|
263 | e2715f69 | Michael Hanselmann | status = constants.JOB_STATUS_QUEUED |
264 | e2715f69 | Michael Hanselmann | |
265 | e2715f69 | Michael Hanselmann | all_success = True
|
266 | 85f03e0d | Michael Hanselmann | for op in self.ops: |
267 | 85f03e0d | Michael Hanselmann | if op.status == constants.OP_STATUS_SUCCESS:
|
268 | e2715f69 | Michael Hanselmann | continue
|
269 | e2715f69 | Michael Hanselmann | |
270 | e2715f69 | Michael Hanselmann | all_success = False
|
271 | e2715f69 | Michael Hanselmann | |
272 | 85f03e0d | Michael Hanselmann | if op.status == constants.OP_STATUS_QUEUED:
|
273 | e2715f69 | Michael Hanselmann | pass
|
274 | e92376d7 | Iustin Pop | elif op.status == constants.OP_STATUS_WAITLOCK:
|
275 | e92376d7 | Iustin Pop | status = constants.JOB_STATUS_WAITLOCK |
276 | 85f03e0d | Michael Hanselmann | elif op.status == constants.OP_STATUS_RUNNING:
|
277 | e2715f69 | Michael Hanselmann | status = constants.JOB_STATUS_RUNNING |
278 | fbf0262f | Michael Hanselmann | elif op.status == constants.OP_STATUS_CANCELING:
|
279 | fbf0262f | Michael Hanselmann | status = constants.JOB_STATUS_CANCELING |
280 | fbf0262f | Michael Hanselmann | break
|
281 | 85f03e0d | Michael Hanselmann | elif op.status == constants.OP_STATUS_ERROR:
|
282 | f1da30e6 | Michael Hanselmann | status = constants.JOB_STATUS_ERROR |
283 | f1da30e6 | Michael Hanselmann | # The whole job fails if one opcode failed
|
284 | f1da30e6 | Michael Hanselmann | break
|
285 | 85f03e0d | Michael Hanselmann | elif op.status == constants.OP_STATUS_CANCELED:
|
286 | 4cb1d919 | Michael Hanselmann | status = constants.OP_STATUS_CANCELED |
287 | 4cb1d919 | Michael Hanselmann | break
|
288 | e2715f69 | Michael Hanselmann | |
289 | e2715f69 | Michael Hanselmann | if all_success:
|
290 | e2715f69 | Michael Hanselmann | status = constants.JOB_STATUS_SUCCESS |
291 | e2715f69 | Michael Hanselmann | |
292 | e2715f69 | Michael Hanselmann | return status
|
293 | e2715f69 | Michael Hanselmann | |
294 | 6c5a7090 | Michael Hanselmann | def GetLogEntries(self, newer_than): |
295 | ea03467c | Iustin Pop | """Selectively returns the log entries.
|
296 | ea03467c | Iustin Pop |
|
297 | ea03467c | Iustin Pop | @type newer_than: None or int
|
298 | 5bbd3f7f | Michael Hanselmann | @param newer_than: if this is None, return all log entries,
|
299 | ea03467c | Iustin Pop | otherwise return only the log entries with serial higher
|
300 | ea03467c | Iustin Pop | than this value
|
301 | ea03467c | Iustin Pop | @rtype: list
|
302 | ea03467c | Iustin Pop | @return: the list of the log entries selected
|
303 | ea03467c | Iustin Pop |
|
304 | ea03467c | Iustin Pop | """
|
305 | 6c5a7090 | Michael Hanselmann | if newer_than is None: |
306 | 6c5a7090 | Michael Hanselmann | serial = -1
|
307 | 6c5a7090 | Michael Hanselmann | else:
|
308 | 6c5a7090 | Michael Hanselmann | serial = newer_than |
309 | 6c5a7090 | Michael Hanselmann | |
310 | 6c5a7090 | Michael Hanselmann | entries = [] |
311 | 6c5a7090 | Michael Hanselmann | for op in self.ops: |
312 | 63712a09 | Iustin Pop | entries.extend(filter(lambda entry: entry[0] > serial, op.log)) |
313 | 6c5a7090 | Michael Hanselmann | |
314 | 6c5a7090 | Michael Hanselmann | return entries
|
315 | 6c5a7090 | Michael Hanselmann | |
316 | 34327f51 | Iustin Pop | def MarkUnfinishedOps(self, status, result): |
317 | 34327f51 | Iustin Pop | """Mark unfinished opcodes with a given status and result.
|
318 | 34327f51 | Iustin Pop |
|
319 | 34327f51 | Iustin Pop | This is an utility function for marking all running or waiting to
|
320 | 34327f51 | Iustin Pop | be run opcodes with a given status. Opcodes which are already
|
321 | 34327f51 | Iustin Pop | finalised are not changed.
|
322 | 34327f51 | Iustin Pop |
|
323 | 34327f51 | Iustin Pop | @param status: a given opcode status
|
324 | 34327f51 | Iustin Pop | @param result: the opcode result
|
325 | 34327f51 | Iustin Pop |
|
326 | 34327f51 | Iustin Pop | """
|
327 | 34327f51 | Iustin Pop | not_marked = True
|
328 | 34327f51 | Iustin Pop | for op in self.ops: |
329 | 34327f51 | Iustin Pop | if op.status in constants.OPS_FINALIZED: |
330 | 34327f51 | Iustin Pop | assert not_marked, "Finalized opcodes found after non-finalized ones" |
331 | 34327f51 | Iustin Pop | continue
|
332 | 34327f51 | Iustin Pop | op.status = status |
333 | 34327f51 | Iustin Pop | op.result = result |
334 | 34327f51 | Iustin Pop | not_marked = False
|
335 | 34327f51 | Iustin Pop | |
336 | f1048938 | Iustin Pop | |
337 | 85f03e0d | Michael Hanselmann | class _JobQueueWorker(workerpool.BaseWorker): |
338 | ea03467c | Iustin Pop | """The actual job workers.
|
339 | ea03467c | Iustin Pop |
|
340 | ea03467c | Iustin Pop | """
|
341 | e92376d7 | Iustin Pop | def _NotifyStart(self): |
342 | e92376d7 | Iustin Pop | """Mark the opcode as running, not lock-waiting.
|
343 | e92376d7 | Iustin Pop |
|
344 | e92376d7 | Iustin Pop | This is called from the mcpu code as a notifier function, when the
|
345 | e92376d7 | Iustin Pop | LU is finally about to start the Exec() method. Of course, to have
|
346 | e92376d7 | Iustin Pop | end-user visible results, the opcode must be initially (before
|
347 | e92376d7 | Iustin Pop | calling into Processor.ExecOpCode) set to OP_STATUS_WAITLOCK.
|
348 | e92376d7 | Iustin Pop |
|
349 | e92376d7 | Iustin Pop | """
|
350 | e92376d7 | Iustin Pop | assert self.queue, "Queue attribute is missing" |
351 | e92376d7 | Iustin Pop | assert self.opcode, "Opcode attribute is missing" |
352 | e92376d7 | Iustin Pop | |
353 | e92376d7 | Iustin Pop | self.queue.acquire()
|
354 | e92376d7 | Iustin Pop | try:
|
355 | fbf0262f | Michael Hanselmann | assert self.opcode.status in (constants.OP_STATUS_WAITLOCK, |
356 | fbf0262f | Michael Hanselmann | constants.OP_STATUS_CANCELING) |
357 | fbf0262f | Michael Hanselmann | |
358 | fbf0262f | Michael Hanselmann | # Cancel here if we were asked to
|
359 | fbf0262f | Michael Hanselmann | if self.opcode.status == constants.OP_STATUS_CANCELING: |
360 | fbf0262f | Michael Hanselmann | raise CancelJob()
|
361 | fbf0262f | Michael Hanselmann | |
362 | e92376d7 | Iustin Pop | self.opcode.status = constants.OP_STATUS_RUNNING
|
363 | e92376d7 | Iustin Pop | finally:
|
364 | e92376d7 | Iustin Pop | self.queue.release()
|
365 | e92376d7 | Iustin Pop | |
366 | 85f03e0d | Michael Hanselmann | def RunTask(self, job): |
367 | e2715f69 | Michael Hanselmann | """Job executor.
|
368 | e2715f69 | Michael Hanselmann |
|
369 | 6c5a7090 | Michael Hanselmann | This functions processes a job. It is closely tied to the _QueuedJob and
|
370 | 6c5a7090 | Michael Hanselmann | _QueuedOpCode classes.
|
371 | e2715f69 | Michael Hanselmann |
|
372 | ea03467c | Iustin Pop | @type job: L{_QueuedJob}
|
373 | ea03467c | Iustin Pop | @param job: the job to be processed
|
374 | ea03467c | Iustin Pop |
|
375 | e2715f69 | Michael Hanselmann | """
|
376 | d21d09d6 | Iustin Pop | logging.info("Worker %s processing job %s",
|
377 | e2715f69 | Michael Hanselmann | self.worker_id, job.id)
|
378 | 5bdce580 | Michael Hanselmann | proc = mcpu.Processor(self.pool.queue.context)
|
379 | e92376d7 | Iustin Pop | self.queue = queue = job.queue
|
380 | e2715f69 | Michael Hanselmann | try:
|
381 | 85f03e0d | Michael Hanselmann | try:
|
382 | 85f03e0d | Michael Hanselmann | count = len(job.ops)
|
383 | 85f03e0d | Michael Hanselmann | for idx, op in enumerate(job.ops): |
384 | d21d09d6 | Iustin Pop | op_summary = op.input.Summary() |
385 | f6424741 | Iustin Pop | if op.status == constants.OP_STATUS_SUCCESS:
|
386 | f6424741 | Iustin Pop | # this is a job that was partially completed before master
|
387 | f6424741 | Iustin Pop | # daemon shutdown, so it can be expected that some opcodes
|
388 | f6424741 | Iustin Pop | # are already completed successfully (if any did error
|
389 | f6424741 | Iustin Pop | # out, then the whole job should have been aborted and not
|
390 | f6424741 | Iustin Pop | # resubmitted for processing)
|
391 | f6424741 | Iustin Pop | logging.info("Op %s/%s: opcode %s already processed, skipping",
|
392 | f6424741 | Iustin Pop | idx + 1, count, op_summary)
|
393 | f6424741 | Iustin Pop | continue
|
394 | 85f03e0d | Michael Hanselmann | try:
|
395 | d21d09d6 | Iustin Pop | logging.info("Op %s/%s: Starting opcode %s", idx + 1, count, |
396 | d21d09d6 | Iustin Pop | op_summary) |
397 | 85f03e0d | Michael Hanselmann | |
398 | 85f03e0d | Michael Hanselmann | queue.acquire() |
399 | 85f03e0d | Michael Hanselmann | try:
|
400 | df0fb067 | Iustin Pop | if op.status == constants.OP_STATUS_CANCELED:
|
401 | df0fb067 | Iustin Pop | raise CancelJob()
|
402 | fbf0262f | Michael Hanselmann | assert op.status == constants.OP_STATUS_QUEUED
|
403 | 85f03e0d | Michael Hanselmann | job.run_op_index = idx |
404 | e92376d7 | Iustin Pop | op.status = constants.OP_STATUS_WAITLOCK |
405 | 85f03e0d | Michael Hanselmann | op.result = None
|
406 | 70552c46 | Michael Hanselmann | op.start_timestamp = TimeStampNow() |
407 | c56ec146 | Iustin Pop | if idx == 0: # first opcode |
408 | c56ec146 | Iustin Pop | job.start_timestamp = op.start_timestamp |
409 | 85f03e0d | Michael Hanselmann | queue.UpdateJobUnlocked(job) |
410 | 85f03e0d | Michael Hanselmann | |
411 | 38206f3c | Iustin Pop | input_opcode = op.input |
412 | 85f03e0d | Michael Hanselmann | finally:
|
413 | 85f03e0d | Michael Hanselmann | queue.release() |
414 | 85f03e0d | Michael Hanselmann | |
415 | dfe57c22 | Michael Hanselmann | def _Log(*args): |
416 | 6c5a7090 | Michael Hanselmann | """Append a log entry.
|
417 | 6c5a7090 | Michael Hanselmann |
|
418 | 6c5a7090 | Michael Hanselmann | """
|
419 | 6c5a7090 | Michael Hanselmann | assert len(args) < 3 |
420 | 6c5a7090 | Michael Hanselmann | |
421 | 6c5a7090 | Michael Hanselmann | if len(args) == 1: |
422 | 6c5a7090 | Michael Hanselmann | log_type = constants.ELOG_MESSAGE |
423 | 6c5a7090 | Michael Hanselmann | log_msg = args[0]
|
424 | 6c5a7090 | Michael Hanselmann | else:
|
425 | 6c5a7090 | Michael Hanselmann | log_type, log_msg = args |
426 | 6c5a7090 | Michael Hanselmann | |
427 | 6c5a7090 | Michael Hanselmann | # The time is split to make serialization easier and not lose
|
428 | 6c5a7090 | Michael Hanselmann | # precision.
|
429 | 6c5a7090 | Michael Hanselmann | timestamp = utils.SplitTime(time.time()) |
430 | dfe57c22 | Michael Hanselmann | |
431 | 6c5a7090 | Michael Hanselmann | queue.acquire() |
432 | dfe57c22 | Michael Hanselmann | try:
|
433 | 6c5a7090 | Michael Hanselmann | job.log_serial += 1
|
434 | 6c5a7090 | Michael Hanselmann | op.log.append((job.log_serial, timestamp, log_type, log_msg)) |
435 | 6c5a7090 | Michael Hanselmann | |
436 | dfe57c22 | Michael Hanselmann | job.change.notifyAll() |
437 | dfe57c22 | Michael Hanselmann | finally:
|
438 | 6c5a7090 | Michael Hanselmann | queue.release() |
439 | dfe57c22 | Michael Hanselmann | |
440 | 6c5a7090 | Michael Hanselmann | # Make sure not to hold lock while _Log is called
|
441 | e92376d7 | Iustin Pop | self.opcode = op
|
442 | e92376d7 | Iustin Pop | result = proc.ExecOpCode(input_opcode, _Log, self._NotifyStart)
|
443 | 85f03e0d | Michael Hanselmann | |
444 | 85f03e0d | Michael Hanselmann | queue.acquire() |
445 | 85f03e0d | Michael Hanselmann | try:
|
446 | 85f03e0d | Michael Hanselmann | op.status = constants.OP_STATUS_SUCCESS |
447 | 85f03e0d | Michael Hanselmann | op.result = result |
448 | 70552c46 | Michael Hanselmann | op.end_timestamp = TimeStampNow() |
449 | 85f03e0d | Michael Hanselmann | queue.UpdateJobUnlocked(job) |
450 | 85f03e0d | Michael Hanselmann | finally:
|
451 | 85f03e0d | Michael Hanselmann | queue.release() |
452 | 85f03e0d | Michael Hanselmann | |
453 | d21d09d6 | Iustin Pop | logging.info("Op %s/%s: Successfully finished opcode %s",
|
454 | d21d09d6 | Iustin Pop | idx + 1, count, op_summary)
|
455 | fbf0262f | Michael Hanselmann | except CancelJob:
|
456 | fbf0262f | Michael Hanselmann | # Will be handled further up
|
457 | fbf0262f | Michael Hanselmann | raise
|
458 | 85f03e0d | Michael Hanselmann | except Exception, err: |
459 | 85f03e0d | Michael Hanselmann | queue.acquire() |
460 | 85f03e0d | Michael Hanselmann | try:
|
461 | 85f03e0d | Michael Hanselmann | try:
|
462 | 85f03e0d | Michael Hanselmann | op.status = constants.OP_STATUS_ERROR |
463 | 85f03e0d | Michael Hanselmann | op.result = str(err)
|
464 | 70552c46 | Michael Hanselmann | op.end_timestamp = TimeStampNow() |
465 | 0f6be82a | Iustin Pop | logging.info("Op %s/%s: Error in opcode %s: %s",
|
466 | 0f6be82a | Iustin Pop | idx + 1, count, op_summary, err)
|
467 | 85f03e0d | Michael Hanselmann | finally:
|
468 | 85f03e0d | Michael Hanselmann | queue.UpdateJobUnlocked(job) |
469 | 85f03e0d | Michael Hanselmann | finally:
|
470 | 85f03e0d | Michael Hanselmann | queue.release() |
471 | 85f03e0d | Michael Hanselmann | raise
|
472 | 85f03e0d | Michael Hanselmann | |
473 | fbf0262f | Michael Hanselmann | except CancelJob:
|
474 | fbf0262f | Michael Hanselmann | queue.acquire() |
475 | fbf0262f | Michael Hanselmann | try:
|
476 | fbf0262f | Michael Hanselmann | queue.CancelJobUnlocked(job) |
477 | fbf0262f | Michael Hanselmann | finally:
|
478 | fbf0262f | Michael Hanselmann | queue.release() |
479 | 85f03e0d | Michael Hanselmann | except errors.GenericError, err:
|
480 | 85f03e0d | Michael Hanselmann | logging.exception("Ganeti exception")
|
481 | 85f03e0d | Michael Hanselmann | except:
|
482 | 85f03e0d | Michael Hanselmann | logging.exception("Unhandled exception")
|
483 | e2715f69 | Michael Hanselmann | finally:
|
484 | 85f03e0d | Michael Hanselmann | queue.acquire() |
485 | 85f03e0d | Michael Hanselmann | try:
|
486 | 65548ed5 | Michael Hanselmann | try:
|
487 | ed21712b | Iustin Pop | job.run_op_index = -1
|
488 | c56ec146 | Iustin Pop | job.end_timestamp = TimeStampNow() |
489 | 65548ed5 | Michael Hanselmann | queue.UpdateJobUnlocked(job) |
490 | 65548ed5 | Michael Hanselmann | finally:
|
491 | 65548ed5 | Michael Hanselmann | job_id = job.id |
492 | 65548ed5 | Michael Hanselmann | status = job.CalcStatus() |
493 | 85f03e0d | Michael Hanselmann | finally:
|
494 | 85f03e0d | Michael Hanselmann | queue.release() |
495 | d21d09d6 | Iustin Pop | logging.info("Worker %s finished job %s, status = %s",
|
496 | d21d09d6 | Iustin Pop | self.worker_id, job_id, status)
|
497 | e2715f69 | Michael Hanselmann | |
498 | e2715f69 | Michael Hanselmann | |
499 | e2715f69 | Michael Hanselmann | class _JobQueueWorkerPool(workerpool.WorkerPool): |
500 | ea03467c | Iustin Pop | """Simple class implementing a job-processing workerpool.
|
501 | ea03467c | Iustin Pop |
|
502 | ea03467c | Iustin Pop | """
|
503 | 5bdce580 | Michael Hanselmann | def __init__(self, queue): |
504 | e2715f69 | Michael Hanselmann | super(_JobQueueWorkerPool, self).__init__(JOBQUEUE_THREADS, |
505 | e2715f69 | Michael Hanselmann | _JobQueueWorker) |
506 | 5bdce580 | Michael Hanselmann | self.queue = queue
|
507 | e2715f69 | Michael Hanselmann | |
508 | e2715f69 | Michael Hanselmann | |
509 | 85f03e0d | Michael Hanselmann | class JobQueue(object): |
510 | 5bbd3f7f | Michael Hanselmann | """Queue used to manage the jobs.
|
511 | ea03467c | Iustin Pop |
|
512 | ea03467c | Iustin Pop | @cvar _RE_JOB_FILE: regex matching the valid job file names
|
513 | ea03467c | Iustin Pop |
|
514 | ea03467c | Iustin Pop | """
|
515 | bac5ffc3 | Oleksiy Mishchenko | _RE_JOB_FILE = re.compile(r"^job-(%s)$" % constants.JOB_ID_TEMPLATE)
|
516 | f1da30e6 | Michael Hanselmann | |
517 | db37da70 | Michael Hanselmann | def _RequireOpenQueue(fn): |
518 | db37da70 | Michael Hanselmann | """Decorator for "public" functions.
|
519 | db37da70 | Michael Hanselmann |
|
520 | ea03467c | Iustin Pop | This function should be used for all 'public' functions. That is,
|
521 | ea03467c | Iustin Pop | functions usually called from other classes.
|
522 | db37da70 | Michael Hanselmann |
|
523 | ea03467c | Iustin Pop | @warning: Use this decorator only after utils.LockedMethod!
|
524 | db37da70 | Michael Hanselmann |
|
525 | ea03467c | Iustin Pop | Example::
|
526 | db37da70 | Michael Hanselmann | @utils.LockedMethod
|
527 | db37da70 | Michael Hanselmann | @_RequireOpenQueue
|
528 | db37da70 | Michael Hanselmann | def Example(self):
|
529 | db37da70 | Michael Hanselmann | pass
|
530 | db37da70 | Michael Hanselmann |
|
531 | db37da70 | Michael Hanselmann | """
|
532 | db37da70 | Michael Hanselmann | def wrapper(self, *args, **kwargs): |
533 | 04ab05ce | Michael Hanselmann | assert self._queue_lock is not None, "Queue should be open" |
534 | db37da70 | Michael Hanselmann | return fn(self, *args, **kwargs) |
535 | db37da70 | Michael Hanselmann | return wrapper
|
536 | db37da70 | Michael Hanselmann | |
537 | 85f03e0d | Michael Hanselmann | def __init__(self, context): |
538 | ea03467c | Iustin Pop | """Constructor for JobQueue.
|
539 | ea03467c | Iustin Pop |
|
540 | ea03467c | Iustin Pop | The constructor will initialize the job queue object and then
|
541 | ea03467c | Iustin Pop | start loading the current jobs from disk, either for starting them
|
542 | ea03467c | Iustin Pop | (if they were queue) or for aborting them (if they were already
|
543 | ea03467c | Iustin Pop | running).
|
544 | ea03467c | Iustin Pop |
|
545 | ea03467c | Iustin Pop | @type context: GanetiContext
|
546 | ea03467c | Iustin Pop | @param context: the context object for access to the configuration
|
547 | ea03467c | Iustin Pop | data and other ganeti objects
|
548 | ea03467c | Iustin Pop |
|
549 | ea03467c | Iustin Pop | """
|
550 | 5bdce580 | Michael Hanselmann | self.context = context
|
551 | 5685c1a5 | Michael Hanselmann | self._memcache = weakref.WeakValueDictionary()
|
552 | c3f0a12f | Iustin Pop | self._my_hostname = utils.HostInfo().name
|
553 | f1da30e6 | Michael Hanselmann | |
554 | 85f03e0d | Michael Hanselmann | # Locking
|
555 | 85f03e0d | Michael Hanselmann | self._lock = threading.Lock()
|
556 | 85f03e0d | Michael Hanselmann | self.acquire = self._lock.acquire |
557 | 85f03e0d | Michael Hanselmann | self.release = self._lock.release |
558 | 85f03e0d | Michael Hanselmann | |
559 | 04ab05ce | Michael Hanselmann | # Initialize
|
560 | 5d6fb8eb | Michael Hanselmann | self._queue_lock = jstore.InitAndVerifyQueue(must_lock=True) |
561 | f1da30e6 | Michael Hanselmann | |
562 | 04ab05ce | Michael Hanselmann | # Read serial file
|
563 | 04ab05ce | Michael Hanselmann | self._last_serial = jstore.ReadSerial()
|
564 | 04ab05ce | Michael Hanselmann | assert self._last_serial is not None, ("Serial file was modified between" |
565 | 04ab05ce | Michael Hanselmann | " check in jstore and here")
|
566 | c4beba1c | Iustin Pop | |
567 | 23752136 | Michael Hanselmann | # Get initial list of nodes
|
568 | 99aabbed | Iustin Pop | self._nodes = dict((n.name, n.primary_ip) |
569 | 59303563 | Iustin Pop | for n in self.context.cfg.GetAllNodesInfo().values() |
570 | 59303563 | Iustin Pop | if n.master_candidate)
|
571 | 8e00939c | Michael Hanselmann | |
572 | 8e00939c | Michael Hanselmann | # Remove master node
|
573 | 8e00939c | Michael Hanselmann | try:
|
574 | 99aabbed | Iustin Pop | del self._nodes[self._my_hostname] |
575 | 33987705 | Iustin Pop | except KeyError: |
576 | 8e00939c | Michael Hanselmann | pass
|
577 | 23752136 | Michael Hanselmann | |
578 | 23752136 | Michael Hanselmann | # TODO: Check consistency across nodes
|
579 | 23752136 | Michael Hanselmann | |
580 | 85f03e0d | Michael Hanselmann | # Setup worker pool
|
581 | 5bdce580 | Michael Hanselmann | self._wpool = _JobQueueWorkerPool(self) |
582 | 85f03e0d | Michael Hanselmann | try:
|
583 | 16714921 | Michael Hanselmann | # We need to lock here because WorkerPool.AddTask() may start a job while
|
584 | 16714921 | Michael Hanselmann | # we're still doing our work.
|
585 | 16714921 | Michael Hanselmann | self.acquire()
|
586 | 16714921 | Michael Hanselmann | try:
|
587 | 711b5124 | Michael Hanselmann | logging.info("Inspecting job queue")
|
588 | 711b5124 | Michael Hanselmann | |
589 | 711b5124 | Michael Hanselmann | all_job_ids = self._GetJobIDsUnlocked()
|
590 | b7cb9024 | Michael Hanselmann | jobs_count = len(all_job_ids)
|
591 | 711b5124 | Michael Hanselmann | lastinfo = time.time() |
592 | 711b5124 | Michael Hanselmann | for idx, job_id in enumerate(all_job_ids): |
593 | 711b5124 | Michael Hanselmann | # Give an update every 1000 jobs or 10 seconds
|
594 | b7cb9024 | Michael Hanselmann | if (idx % 1000 == 0 or time.time() >= (lastinfo + 10.0) or |
595 | b7cb9024 | Michael Hanselmann | idx == (jobs_count - 1)):
|
596 | 711b5124 | Michael Hanselmann | logging.info("Job queue inspection: %d/%d (%0.1f %%)",
|
597 | b7cb9024 | Michael Hanselmann | idx, jobs_count - 1, 100.0 * (idx + 1) / jobs_count) |
598 | 711b5124 | Michael Hanselmann | lastinfo = time.time() |
599 | 711b5124 | Michael Hanselmann | |
600 | 711b5124 | Michael Hanselmann | job = self._LoadJobUnlocked(job_id)
|
601 | 711b5124 | Michael Hanselmann | |
602 | 16714921 | Michael Hanselmann | # a failure in loading the job can cause 'None' to be returned
|
603 | 16714921 | Michael Hanselmann | if job is None: |
604 | 16714921 | Michael Hanselmann | continue
|
605 | 94ed59a5 | Iustin Pop | |
606 | 16714921 | Michael Hanselmann | status = job.CalcStatus() |
607 | 85f03e0d | Michael Hanselmann | |
608 | 16714921 | Michael Hanselmann | if status in (constants.JOB_STATUS_QUEUED, ): |
609 | 16714921 | Michael Hanselmann | self._wpool.AddTask(job)
|
610 | 85f03e0d | Michael Hanselmann | |
611 | 16714921 | Michael Hanselmann | elif status in (constants.JOB_STATUS_RUNNING, |
612 | fbf0262f | Michael Hanselmann | constants.JOB_STATUS_WAITLOCK, |
613 | fbf0262f | Michael Hanselmann | constants.JOB_STATUS_CANCELING): |
614 | 16714921 | Michael Hanselmann | logging.warning("Unfinished job %s found: %s", job.id, job)
|
615 | 16714921 | Michael Hanselmann | try:
|
616 | 34327f51 | Iustin Pop | job.MarkUnfinishedOps(constants.OP_STATUS_ERROR, |
617 | 34327f51 | Iustin Pop | "Unclean master daemon shutdown")
|
618 | 16714921 | Michael Hanselmann | finally:
|
619 | 16714921 | Michael Hanselmann | self.UpdateJobUnlocked(job)
|
620 | 711b5124 | Michael Hanselmann | |
621 | 711b5124 | Michael Hanselmann | logging.info("Job queue inspection finished")
|
622 | 16714921 | Michael Hanselmann | finally:
|
623 | 16714921 | Michael Hanselmann | self.release()
|
624 | 16714921 | Michael Hanselmann | except:
|
625 | 16714921 | Michael Hanselmann | self._wpool.TerminateWorkers()
|
626 | 16714921 | Michael Hanselmann | raise
|
627 | 85f03e0d | Michael Hanselmann | |
628 | d2e03a33 | Michael Hanselmann | @utils.LockedMethod
|
629 | d2e03a33 | Michael Hanselmann | @_RequireOpenQueue
|
630 | 99aabbed | Iustin Pop | def AddNode(self, node): |
631 | 99aabbed | Iustin Pop | """Register a new node with the queue.
|
632 | 99aabbed | Iustin Pop |
|
633 | 99aabbed | Iustin Pop | @type node: L{objects.Node}
|
634 | 99aabbed | Iustin Pop | @param node: the node object to be added
|
635 | 99aabbed | Iustin Pop |
|
636 | 99aabbed | Iustin Pop | """
|
637 | 99aabbed | Iustin Pop | node_name = node.name |
638 | d2e03a33 | Michael Hanselmann | assert node_name != self._my_hostname |
639 | 23752136 | Michael Hanselmann | |
640 | 9f774ee8 | Michael Hanselmann | # Clean queue directory on added node
|
641 | c8457ce7 | Iustin Pop | result = rpc.RpcRunner.call_jobqueue_purge(node_name) |
642 | c8457ce7 | Iustin Pop | msg = result.RemoteFailMsg() |
643 | c8457ce7 | Iustin Pop | if msg:
|
644 | c8457ce7 | Iustin Pop | logging.warning("Cannot cleanup queue directory on node %s: %s",
|
645 | c8457ce7 | Iustin Pop | node_name, msg) |
646 | 23752136 | Michael Hanselmann | |
647 | 59303563 | Iustin Pop | if not node.master_candidate: |
648 | 59303563 | Iustin Pop | # remove if existing, ignoring errors
|
649 | 59303563 | Iustin Pop | self._nodes.pop(node_name, None) |
650 | 59303563 | Iustin Pop | # and skip the replication of the job ids
|
651 | 59303563 | Iustin Pop | return
|
652 | 59303563 | Iustin Pop | |
653 | d2e03a33 | Michael Hanselmann | # Upload the whole queue excluding archived jobs
|
654 | d2e03a33 | Michael Hanselmann | files = [self._GetJobPath(job_id) for job_id in self._GetJobIDsUnlocked()] |
655 | 23752136 | Michael Hanselmann | |
656 | d2e03a33 | Michael Hanselmann | # Upload current serial file
|
657 | d2e03a33 | Michael Hanselmann | files.append(constants.JOB_QUEUE_SERIAL_FILE) |
658 | d2e03a33 | Michael Hanselmann | |
659 | d2e03a33 | Michael Hanselmann | for file_name in files: |
660 | 9f774ee8 | Michael Hanselmann | # Read file content
|
661 | 9f774ee8 | Michael Hanselmann | fd = open(file_name, "r") |
662 | 9f774ee8 | Michael Hanselmann | try:
|
663 | 9f774ee8 | Michael Hanselmann | content = fd.read() |
664 | 9f774ee8 | Michael Hanselmann | finally:
|
665 | 9f774ee8 | Michael Hanselmann | fd.close() |
666 | 9f774ee8 | Michael Hanselmann | |
667 | a3811745 | Michael Hanselmann | result = rpc.RpcRunner.call_jobqueue_update([node_name], |
668 | a3811745 | Michael Hanselmann | [node.primary_ip], |
669 | a3811745 | Michael Hanselmann | file_name, content) |
670 | c8457ce7 | Iustin Pop | msg = result[node_name].RemoteFailMsg() |
671 | c8457ce7 | Iustin Pop | if msg:
|
672 | c8457ce7 | Iustin Pop | logging.error("Failed to upload file %s to node %s: %s",
|
673 | c8457ce7 | Iustin Pop | file_name, node_name, msg) |
674 | d2e03a33 | Michael Hanselmann | |
675 | 99aabbed | Iustin Pop | self._nodes[node_name] = node.primary_ip
|
676 | d2e03a33 | Michael Hanselmann | |
677 | d2e03a33 | Michael Hanselmann | @utils.LockedMethod
|
678 | d2e03a33 | Michael Hanselmann | @_RequireOpenQueue
|
679 | d2e03a33 | Michael Hanselmann | def RemoveNode(self, node_name): |
680 | ea03467c | Iustin Pop | """Callback called when removing nodes from the cluster.
|
681 | ea03467c | Iustin Pop |
|
682 | ea03467c | Iustin Pop | @type node_name: str
|
683 | ea03467c | Iustin Pop | @param node_name: the name of the node to remove
|
684 | ea03467c | Iustin Pop |
|
685 | ea03467c | Iustin Pop | """
|
686 | 23752136 | Michael Hanselmann | try:
|
687 | d2e03a33 | Michael Hanselmann | # The queue is removed by the "leave node" RPC call.
|
688 | 99aabbed | Iustin Pop | del self._nodes[node_name] |
689 | d2e03a33 | Michael Hanselmann | except KeyError: |
690 | 23752136 | Michael Hanselmann | pass
|
691 | 23752136 | Michael Hanselmann | |
692 | e74798c1 | Michael Hanselmann | def _CheckRpcResult(self, result, nodes, failmsg): |
693 | ea03467c | Iustin Pop | """Verifies the status of an RPC call.
|
694 | ea03467c | Iustin Pop |
|
695 | ea03467c | Iustin Pop | Since we aim to keep consistency should this node (the current
|
696 | ea03467c | Iustin Pop | master) fail, we will log errors if our rpc fail, and especially
|
697 | 5bbd3f7f | Michael Hanselmann | log the case when more than half of the nodes fails.
|
698 | ea03467c | Iustin Pop |
|
699 | ea03467c | Iustin Pop | @param result: the data as returned from the rpc call
|
700 | ea03467c | Iustin Pop | @type nodes: list
|
701 | ea03467c | Iustin Pop | @param nodes: the list of nodes we made the call to
|
702 | ea03467c | Iustin Pop | @type failmsg: str
|
703 | ea03467c | Iustin Pop | @param failmsg: the identifier to be used for logging
|
704 | ea03467c | Iustin Pop |
|
705 | ea03467c | Iustin Pop | """
|
706 | e74798c1 | Michael Hanselmann | failed = [] |
707 | e74798c1 | Michael Hanselmann | success = [] |
708 | e74798c1 | Michael Hanselmann | |
709 | e74798c1 | Michael Hanselmann | for node in nodes: |
710 | c8457ce7 | Iustin Pop | msg = result[node].RemoteFailMsg() |
711 | c8457ce7 | Iustin Pop | if msg:
|
712 | e74798c1 | Michael Hanselmann | failed.append(node) |
713 | c8457ce7 | Iustin Pop | logging.error("RPC call %s failed on node %s: %s",
|
714 | c8457ce7 | Iustin Pop | result[node].call, node, msg) |
715 | c8457ce7 | Iustin Pop | else:
|
716 | c8457ce7 | Iustin Pop | success.append(node) |
717 | e74798c1 | Michael Hanselmann | |
718 | e74798c1 | Michael Hanselmann | # +1 for the master node
|
719 | e74798c1 | Michael Hanselmann | if (len(success) + 1) < len(failed): |
720 | e74798c1 | Michael Hanselmann | # TODO: Handle failing nodes
|
721 | e74798c1 | Michael Hanselmann | logging.error("More than half of the nodes failed")
|
722 | e74798c1 | Michael Hanselmann | |
723 | 99aabbed | Iustin Pop | def _GetNodeIp(self): |
724 | 99aabbed | Iustin Pop | """Helper for returning the node name/ip list.
|
725 | 99aabbed | Iustin Pop |
|
726 | ea03467c | Iustin Pop | @rtype: (list, list)
|
727 | ea03467c | Iustin Pop | @return: a tuple of two lists, the first one with the node
|
728 | ea03467c | Iustin Pop | names and the second one with the node addresses
|
729 | ea03467c | Iustin Pop |
|
730 | 99aabbed | Iustin Pop | """
|
731 | 99aabbed | Iustin Pop | name_list = self._nodes.keys()
|
732 | 99aabbed | Iustin Pop | addr_list = [self._nodes[name] for name in name_list] |
733 | 99aabbed | Iustin Pop | return name_list, addr_list
|
734 | 99aabbed | Iustin Pop | |
735 | 8e00939c | Michael Hanselmann | def _WriteAndReplicateFileUnlocked(self, file_name, data): |
736 | 8e00939c | Michael Hanselmann | """Writes a file locally and then replicates it to all nodes.
|
737 | 8e00939c | Michael Hanselmann |
|
738 | ea03467c | Iustin Pop | This function will replace the contents of a file on the local
|
739 | ea03467c | Iustin Pop | node and then replicate it to all the other nodes we have.
|
740 | ea03467c | Iustin Pop |
|
741 | ea03467c | Iustin Pop | @type file_name: str
|
742 | ea03467c | Iustin Pop | @param file_name: the path of the file to be replicated
|
743 | ea03467c | Iustin Pop | @type data: str
|
744 | ea03467c | Iustin Pop | @param data: the new contents of the file
|
745 | ea03467c | Iustin Pop |
|
746 | 8e00939c | Michael Hanselmann | """
|
747 | 8e00939c | Michael Hanselmann | utils.WriteFile(file_name, data=data) |
748 | 8e00939c | Michael Hanselmann | |
749 | 99aabbed | Iustin Pop | names, addrs = self._GetNodeIp()
|
750 | a3811745 | Michael Hanselmann | result = rpc.RpcRunner.call_jobqueue_update(names, addrs, file_name, data) |
751 | e74798c1 | Michael Hanselmann | self._CheckRpcResult(result, self._nodes, |
752 | e74798c1 | Michael Hanselmann | "Updating %s" % file_name)
|
753 | 23752136 | Michael Hanselmann | |
754 | d7fd1f28 | Michael Hanselmann | def _RenameFilesUnlocked(self, rename): |
755 | ea03467c | Iustin Pop | """Renames a file locally and then replicate the change.
|
756 | ea03467c | Iustin Pop |
|
757 | ea03467c | Iustin Pop | This function will rename a file in the local queue directory
|
758 | ea03467c | Iustin Pop | and then replicate this rename to all the other nodes we have.
|
759 | ea03467c | Iustin Pop |
|
760 | d7fd1f28 | Michael Hanselmann | @type rename: list of (old, new)
|
761 | d7fd1f28 | Michael Hanselmann | @param rename: List containing tuples mapping old to new names
|
762 | ea03467c | Iustin Pop |
|
763 | ea03467c | Iustin Pop | """
|
764 | dd875d32 | Michael Hanselmann | # Rename them locally
|
765 | d7fd1f28 | Michael Hanselmann | for old, new in rename: |
766 | d7fd1f28 | Michael Hanselmann | utils.RenameFile(old, new, mkdir=True)
|
767 | abc1f2ce | Michael Hanselmann | |
768 | dd875d32 | Michael Hanselmann | # ... and on all nodes
|
769 | dd875d32 | Michael Hanselmann | names, addrs = self._GetNodeIp()
|
770 | dd875d32 | Michael Hanselmann | result = rpc.RpcRunner.call_jobqueue_rename(names, addrs, rename) |
771 | dd875d32 | Michael Hanselmann | self._CheckRpcResult(result, self._nodes, "Renaming files (%r)" % rename) |
772 | abc1f2ce | Michael Hanselmann | |
773 | 85f03e0d | Michael Hanselmann | def _FormatJobID(self, job_id): |
774 | ea03467c | Iustin Pop | """Convert a job ID to string format.
|
775 | ea03467c | Iustin Pop |
|
776 | ea03467c | Iustin Pop | Currently this just does C{str(job_id)} after performing some
|
777 | ea03467c | Iustin Pop | checks, but if we want to change the job id format this will
|
778 | ea03467c | Iustin Pop | abstract this change.
|
779 | ea03467c | Iustin Pop |
|
780 | ea03467c | Iustin Pop | @type job_id: int or long
|
781 | ea03467c | Iustin Pop | @param job_id: the numeric job id
|
782 | ea03467c | Iustin Pop | @rtype: str
|
783 | ea03467c | Iustin Pop | @return: the formatted job id
|
784 | ea03467c | Iustin Pop |
|
785 | ea03467c | Iustin Pop | """
|
786 | 85f03e0d | Michael Hanselmann | if not isinstance(job_id, (int, long)): |
787 | 85f03e0d | Michael Hanselmann | raise errors.ProgrammerError("Job ID '%s' not numeric" % job_id) |
788 | 85f03e0d | Michael Hanselmann | if job_id < 0: |
789 | 85f03e0d | Michael Hanselmann | raise errors.ProgrammerError("Job ID %s is negative" % job_id) |
790 | 85f03e0d | Michael Hanselmann | |
791 | 85f03e0d | Michael Hanselmann | return str(job_id) |
792 | 85f03e0d | Michael Hanselmann | |
793 | 58b22b6e | Michael Hanselmann | @classmethod
|
794 | 58b22b6e | Michael Hanselmann | def _GetArchiveDirectory(cls, job_id): |
795 | 58b22b6e | Michael Hanselmann | """Returns the archive directory for a job.
|
796 | 58b22b6e | Michael Hanselmann |
|
797 | 58b22b6e | Michael Hanselmann | @type job_id: str
|
798 | 58b22b6e | Michael Hanselmann | @param job_id: Job identifier
|
799 | 58b22b6e | Michael Hanselmann | @rtype: str
|
800 | 58b22b6e | Michael Hanselmann | @return: Directory name
|
801 | 58b22b6e | Michael Hanselmann |
|
802 | 58b22b6e | Michael Hanselmann | """
|
803 | 58b22b6e | Michael Hanselmann | return str(int(job_id) / JOBS_PER_ARCHIVE_DIRECTORY) |
804 | 58b22b6e | Michael Hanselmann | |
805 | 4c848b18 | Michael Hanselmann | def _NewSerialUnlocked(self): |
806 | f1da30e6 | Michael Hanselmann | """Generates a new job identifier.
|
807 | f1da30e6 | Michael Hanselmann |
|
808 | f1da30e6 | Michael Hanselmann | Job identifiers are unique during the lifetime of a cluster.
|
809 | f1da30e6 | Michael Hanselmann |
|
810 | ea03467c | Iustin Pop | @rtype: str
|
811 | ea03467c | Iustin Pop | @return: a string representing the job identifier.
|
812 | f1da30e6 | Michael Hanselmann |
|
813 | f1da30e6 | Michael Hanselmann | """
|
814 | f1da30e6 | Michael Hanselmann | # New number
|
815 | f1da30e6 | Michael Hanselmann | serial = self._last_serial + 1 |
816 | f1da30e6 | Michael Hanselmann | |
817 | f1da30e6 | Michael Hanselmann | # Write to file
|
818 | 23752136 | Michael Hanselmann | self._WriteAndReplicateFileUnlocked(constants.JOB_QUEUE_SERIAL_FILE,
|
819 | 23752136 | Michael Hanselmann | "%s\n" % serial)
|
820 | f1da30e6 | Michael Hanselmann | |
821 | f1da30e6 | Michael Hanselmann | # Keep it only if we were able to write the file
|
822 | f1da30e6 | Michael Hanselmann | self._last_serial = serial
|
823 | f1da30e6 | Michael Hanselmann | |
824 | 85f03e0d | Michael Hanselmann | return self._FormatJobID(serial) |
825 | f1da30e6 | Michael Hanselmann | |
826 | 85f03e0d | Michael Hanselmann | @staticmethod
|
827 | 85f03e0d | Michael Hanselmann | def _GetJobPath(job_id): |
828 | ea03467c | Iustin Pop | """Returns the job file for a given job id.
|
829 | ea03467c | Iustin Pop |
|
830 | ea03467c | Iustin Pop | @type job_id: str
|
831 | ea03467c | Iustin Pop | @param job_id: the job identifier
|
832 | ea03467c | Iustin Pop | @rtype: str
|
833 | ea03467c | Iustin Pop | @return: the path to the job file
|
834 | ea03467c | Iustin Pop |
|
835 | ea03467c | Iustin Pop | """
|
836 | f1da30e6 | Michael Hanselmann | return os.path.join(constants.QUEUE_DIR, "job-%s" % job_id) |
837 | f1da30e6 | Michael Hanselmann | |
838 | 58b22b6e | Michael Hanselmann | @classmethod
|
839 | 58b22b6e | Michael Hanselmann | def _GetArchivedJobPath(cls, job_id): |
840 | ea03467c | Iustin Pop | """Returns the archived job file for a give job id.
|
841 | ea03467c | Iustin Pop |
|
842 | ea03467c | Iustin Pop | @type job_id: str
|
843 | ea03467c | Iustin Pop | @param job_id: the job identifier
|
844 | ea03467c | Iustin Pop | @rtype: str
|
845 | ea03467c | Iustin Pop | @return: the path to the archived job file
|
846 | ea03467c | Iustin Pop |
|
847 | ea03467c | Iustin Pop | """
|
848 | 58b22b6e | Michael Hanselmann | path = "%s/job-%s" % (cls._GetArchiveDirectory(job_id), job_id)
|
849 | 58b22b6e | Michael Hanselmann | return os.path.join(constants.JOB_QUEUE_ARCHIVE_DIR, path)
|
850 | 0cb94105 | Michael Hanselmann | |
851 | 85f03e0d | Michael Hanselmann | @classmethod
|
852 | 85f03e0d | Michael Hanselmann | def _ExtractJobID(cls, name): |
853 | ea03467c | Iustin Pop | """Extract the job id from a filename.
|
854 | ea03467c | Iustin Pop |
|
855 | ea03467c | Iustin Pop | @type name: str
|
856 | ea03467c | Iustin Pop | @param name: the job filename
|
857 | ea03467c | Iustin Pop | @rtype: job id or None
|
858 | ea03467c | Iustin Pop | @return: the job id corresponding to the given filename,
|
859 | ea03467c | Iustin Pop | or None if the filename does not represent a valid
|
860 | ea03467c | Iustin Pop | job file
|
861 | ea03467c | Iustin Pop |
|
862 | ea03467c | Iustin Pop | """
|
863 | 85f03e0d | Michael Hanselmann | m = cls._RE_JOB_FILE.match(name) |
864 | fae737ac | Michael Hanselmann | if m:
|
865 | fae737ac | Michael Hanselmann | return m.group(1) |
866 | fae737ac | Michael Hanselmann | else:
|
867 | fae737ac | Michael Hanselmann | return None |
868 | fae737ac | Michael Hanselmann | |
869 | 911a495b | Iustin Pop | def _GetJobIDsUnlocked(self, archived=False): |
870 | 911a495b | Iustin Pop | """Return all known job IDs.
|
871 | 911a495b | Iustin Pop |
|
872 | 911a495b | Iustin Pop | If the parameter archived is True, archived jobs IDs will be
|
873 | 911a495b | Iustin Pop | included. Currently this argument is unused.
|
874 | 911a495b | Iustin Pop |
|
875 | ac0930b9 | Iustin Pop | The method only looks at disk because it's a requirement that all
|
876 | ac0930b9 | Iustin Pop | jobs are present on disk (so in the _memcache we don't have any
|
877 | ac0930b9 | Iustin Pop | extra IDs).
|
878 | ac0930b9 | Iustin Pop |
|
879 | ea03467c | Iustin Pop | @rtype: list
|
880 | ea03467c | Iustin Pop | @return: the list of job IDs
|
881 | ea03467c | Iustin Pop |
|
882 | 911a495b | Iustin Pop | """
|
883 | fae737ac | Michael Hanselmann | jlist = [self._ExtractJobID(name) for name in self._ListJobFiles()] |
884 | 3b87986e | Iustin Pop | jlist = utils.NiceSort(jlist) |
885 | f0d874fe | Iustin Pop | return jlist
|
886 | 911a495b | Iustin Pop | |
887 | f1da30e6 | Michael Hanselmann | def _ListJobFiles(self): |
888 | ea03467c | Iustin Pop | """Returns the list of current job files.
|
889 | ea03467c | Iustin Pop |
|
890 | ea03467c | Iustin Pop | @rtype: list
|
891 | ea03467c | Iustin Pop | @return: the list of job file names
|
892 | ea03467c | Iustin Pop |
|
893 | ea03467c | Iustin Pop | """
|
894 | f1da30e6 | Michael Hanselmann | return [name for name in utils.ListVisibleFiles(constants.QUEUE_DIR) |
895 | f1da30e6 | Michael Hanselmann | if self._RE_JOB_FILE.match(name)] |
896 | f1da30e6 | Michael Hanselmann | |
897 | 911a495b | Iustin Pop | def _LoadJobUnlocked(self, job_id): |
898 | ea03467c | Iustin Pop | """Loads a job from the disk or memory.
|
899 | ea03467c | Iustin Pop |
|
900 | ea03467c | Iustin Pop | Given a job id, this will return the cached job object if
|
901 | ea03467c | Iustin Pop | existing, or try to load the job from the disk. If loading from
|
902 | ea03467c | Iustin Pop | disk, it will also add the job to the cache.
|
903 | ea03467c | Iustin Pop |
|
904 | ea03467c | Iustin Pop | @param job_id: the job id
|
905 | ea03467c | Iustin Pop | @rtype: L{_QueuedJob} or None
|
906 | ea03467c | Iustin Pop | @return: either None or the job object
|
907 | ea03467c | Iustin Pop |
|
908 | ea03467c | Iustin Pop | """
|
909 | 5685c1a5 | Michael Hanselmann | job = self._memcache.get(job_id, None) |
910 | 5685c1a5 | Michael Hanselmann | if job:
|
911 | 205d71fd | Michael Hanselmann | logging.debug("Found job %s in memcache", job_id)
|
912 | 5685c1a5 | Michael Hanselmann | return job
|
913 | ac0930b9 | Iustin Pop | |
914 | 911a495b | Iustin Pop | filepath = self._GetJobPath(job_id)
|
915 | f1da30e6 | Michael Hanselmann | logging.debug("Loading job from %s", filepath)
|
916 | f1da30e6 | Michael Hanselmann | try:
|
917 | f1da30e6 | Michael Hanselmann | fd = open(filepath, "r") |
918 | f1da30e6 | Michael Hanselmann | except IOError, err: |
919 | f1da30e6 | Michael Hanselmann | if err.errno in (errno.ENOENT, ): |
920 | f1da30e6 | Michael Hanselmann | return None |
921 | f1da30e6 | Michael Hanselmann | raise
|
922 | f1da30e6 | Michael Hanselmann | try:
|
923 | f1da30e6 | Michael Hanselmann | data = serializer.LoadJson(fd.read()) |
924 | f1da30e6 | Michael Hanselmann | finally:
|
925 | f1da30e6 | Michael Hanselmann | fd.close() |
926 | f1da30e6 | Michael Hanselmann | |
927 | 94ed59a5 | Iustin Pop | try:
|
928 | 94ed59a5 | Iustin Pop | job = _QueuedJob.Restore(self, data)
|
929 | 94ed59a5 | Iustin Pop | except Exception, err: |
930 | 94ed59a5 | Iustin Pop | new_path = self._GetArchivedJobPath(job_id)
|
931 | 94ed59a5 | Iustin Pop | if filepath == new_path:
|
932 | 94ed59a5 | Iustin Pop | # job already archived (future case)
|
933 | 94ed59a5 | Iustin Pop | logging.exception("Can't parse job %s", job_id)
|
934 | 94ed59a5 | Iustin Pop | else:
|
935 | 94ed59a5 | Iustin Pop | # non-archived case
|
936 | 94ed59a5 | Iustin Pop | logging.exception("Can't parse job %s, will archive.", job_id)
|
937 | d7fd1f28 | Michael Hanselmann | self._RenameFilesUnlocked([(filepath, new_path)])
|
938 | 94ed59a5 | Iustin Pop | return None |
939 | 94ed59a5 | Iustin Pop | |
940 | ac0930b9 | Iustin Pop | self._memcache[job_id] = job
|
941 | 205d71fd | Michael Hanselmann | logging.debug("Added job %s to the cache", job_id)
|
942 | ac0930b9 | Iustin Pop | return job
|
943 | f1da30e6 | Michael Hanselmann | |
944 | f1da30e6 | Michael Hanselmann | def _GetJobsUnlocked(self, job_ids): |
945 | ea03467c | Iustin Pop | """Return a list of jobs based on their IDs.
|
946 | ea03467c | Iustin Pop |
|
947 | ea03467c | Iustin Pop | @type job_ids: list
|
948 | ea03467c | Iustin Pop | @param job_ids: either an empty list (meaning all jobs),
|
949 | ea03467c | Iustin Pop | or a list of job IDs
|
950 | ea03467c | Iustin Pop | @rtype: list
|
951 | ea03467c | Iustin Pop | @return: the list of job objects
|
952 | ea03467c | Iustin Pop |
|
953 | ea03467c | Iustin Pop | """
|
954 | 911a495b | Iustin Pop | if not job_ids: |
955 | 911a495b | Iustin Pop | job_ids = self._GetJobIDsUnlocked()
|
956 | f1da30e6 | Michael Hanselmann | |
957 | 911a495b | Iustin Pop | return [self._LoadJobUnlocked(job_id) for job_id in job_ids] |
958 | f1da30e6 | Michael Hanselmann | |
959 | 686d7433 | Iustin Pop | @staticmethod
|
960 | 686d7433 | Iustin Pop | def _IsQueueMarkedDrain(): |
961 | 686d7433 | Iustin Pop | """Check if the queue is marked from drain.
|
962 | 686d7433 | Iustin Pop |
|
963 | 686d7433 | Iustin Pop | This currently uses the queue drain file, which makes it a
|
964 | 686d7433 | Iustin Pop | per-node flag. In the future this can be moved to the config file.
|
965 | 686d7433 | Iustin Pop |
|
966 | ea03467c | Iustin Pop | @rtype: boolean
|
967 | ea03467c | Iustin Pop | @return: True of the job queue is marked for draining
|
968 | ea03467c | Iustin Pop |
|
969 | 686d7433 | Iustin Pop | """
|
970 | 686d7433 | Iustin Pop | return os.path.exists(constants.JOB_QUEUE_DRAIN_FILE)
|
971 | 686d7433 | Iustin Pop | |
972 | 3ccafd0e | Iustin Pop | @staticmethod
|
973 | 3ccafd0e | Iustin Pop | def SetDrainFlag(drain_flag): |
974 | 3ccafd0e | Iustin Pop | """Sets the drain flag for the queue.
|
975 | 3ccafd0e | Iustin Pop |
|
976 | 3ccafd0e | Iustin Pop | This is similar to the function L{backend.JobQueueSetDrainFlag},
|
977 | 3ccafd0e | Iustin Pop | and in the future we might merge them.
|
978 | 3ccafd0e | Iustin Pop |
|
979 | ea03467c | Iustin Pop | @type drain_flag: boolean
|
980 | 5bbd3f7f | Michael Hanselmann | @param drain_flag: Whether to set or unset the drain flag
|
981 | ea03467c | Iustin Pop |
|
982 | 3ccafd0e | Iustin Pop | """
|
983 | 3ccafd0e | Iustin Pop | if drain_flag:
|
984 | 3ccafd0e | Iustin Pop | utils.WriteFile(constants.JOB_QUEUE_DRAIN_FILE, data="", close=True) |
985 | 3ccafd0e | Iustin Pop | else:
|
986 | 3ccafd0e | Iustin Pop | utils.RemoveFile(constants.JOB_QUEUE_DRAIN_FILE) |
987 | 3ccafd0e | Iustin Pop | return True |
988 | 3ccafd0e | Iustin Pop | |
989 | db37da70 | Michael Hanselmann | @_RequireOpenQueue
|
990 | 2971c913 | Iustin Pop | def _SubmitJobUnlocked(self, ops): |
991 | 85f03e0d | Michael Hanselmann | """Create and store a new job.
|
992 | f1da30e6 | Michael Hanselmann |
|
993 | 85f03e0d | Michael Hanselmann | This enters the job into our job queue and also puts it on the new
|
994 | 85f03e0d | Michael Hanselmann | queue, in order for it to be picked up by the queue processors.
|
995 | c3f0a12f | Iustin Pop |
|
996 | c3f0a12f | Iustin Pop | @type ops: list
|
997 | 205d71fd | Michael Hanselmann | @param ops: The list of OpCodes that will become the new job.
|
998 | ea03467c | Iustin Pop | @rtype: job ID
|
999 | ea03467c | Iustin Pop | @return: the job ID of the newly created job
|
1000 | ea03467c | Iustin Pop | @raise errors.JobQueueDrainError: if the job is marked for draining
|
1001 | c3f0a12f | Iustin Pop |
|
1002 | c3f0a12f | Iustin Pop | """
|
1003 | 686d7433 | Iustin Pop | if self._IsQueueMarkedDrain(): |
1004 | 2971c913 | Iustin Pop | raise errors.JobQueueDrainError("Job queue is drained, refusing job") |
1005 | f87b405e | Michael Hanselmann | |
1006 | f87b405e | Michael Hanselmann | # Check job queue size
|
1007 | f87b405e | Michael Hanselmann | size = len(self._ListJobFiles()) |
1008 | f87b405e | Michael Hanselmann | if size >= constants.JOB_QUEUE_SIZE_SOFT_LIMIT:
|
1009 | f87b405e | Michael Hanselmann | # TODO: Autoarchive jobs. Make sure it's not done on every job
|
1010 | f87b405e | Michael Hanselmann | # submission, though.
|
1011 | f87b405e | Michael Hanselmann | #size = ...
|
1012 | f87b405e | Michael Hanselmann | pass
|
1013 | f87b405e | Michael Hanselmann | |
1014 | f87b405e | Michael Hanselmann | if size >= constants.JOB_QUEUE_SIZE_HARD_LIMIT:
|
1015 | f87b405e | Michael Hanselmann | raise errors.JobQueueFull()
|
1016 | f87b405e | Michael Hanselmann | |
1017 | f1da30e6 | Michael Hanselmann | # Get job identifier
|
1018 | 4c848b18 | Michael Hanselmann | job_id = self._NewSerialUnlocked()
|
1019 | f1da30e6 | Michael Hanselmann | job = _QueuedJob(self, job_id, ops)
|
1020 | f1da30e6 | Michael Hanselmann | |
1021 | f1da30e6 | Michael Hanselmann | # Write to disk
|
1022 | 85f03e0d | Michael Hanselmann | self.UpdateJobUnlocked(job)
|
1023 | f1da30e6 | Michael Hanselmann | |
1024 | 5685c1a5 | Michael Hanselmann | logging.debug("Adding new job %s to the cache", job_id)
|
1025 | ac0930b9 | Iustin Pop | self._memcache[job_id] = job
|
1026 | ac0930b9 | Iustin Pop | |
1027 | 85f03e0d | Michael Hanselmann | # Add to worker pool
|
1028 | 85f03e0d | Michael Hanselmann | self._wpool.AddTask(job)
|
1029 | 85f03e0d | Michael Hanselmann | |
1030 | 85f03e0d | Michael Hanselmann | return job.id
|
1031 | f1da30e6 | Michael Hanselmann | |
1032 | 2971c913 | Iustin Pop | @utils.LockedMethod
|
1033 | 2971c913 | Iustin Pop | @_RequireOpenQueue
|
1034 | 2971c913 | Iustin Pop | def SubmitJob(self, ops): |
1035 | 2971c913 | Iustin Pop | """Create and store a new job.
|
1036 | 2971c913 | Iustin Pop |
|
1037 | 2971c913 | Iustin Pop | @see: L{_SubmitJobUnlocked}
|
1038 | 2971c913 | Iustin Pop |
|
1039 | 2971c913 | Iustin Pop | """
|
1040 | 2971c913 | Iustin Pop | return self._SubmitJobUnlocked(ops) |
1041 | 2971c913 | Iustin Pop | |
1042 | 2971c913 | Iustin Pop | @utils.LockedMethod
|
1043 | 2971c913 | Iustin Pop | @_RequireOpenQueue
|
1044 | 2971c913 | Iustin Pop | def SubmitManyJobs(self, jobs): |
1045 | 2971c913 | Iustin Pop | """Create and store multiple jobs.
|
1046 | 2971c913 | Iustin Pop |
|
1047 | 2971c913 | Iustin Pop | @see: L{_SubmitJobUnlocked}
|
1048 | 2971c913 | Iustin Pop |
|
1049 | 2971c913 | Iustin Pop | """
|
1050 | 2971c913 | Iustin Pop | results = [] |
1051 | 2971c913 | Iustin Pop | for ops in jobs: |
1052 | 2971c913 | Iustin Pop | try:
|
1053 | 2971c913 | Iustin Pop | data = self._SubmitJobUnlocked(ops)
|
1054 | 2971c913 | Iustin Pop | status = True
|
1055 | 2971c913 | Iustin Pop | except errors.GenericError, err:
|
1056 | 2971c913 | Iustin Pop | data = str(err)
|
1057 | 2971c913 | Iustin Pop | status = False
|
1058 | 2971c913 | Iustin Pop | results.append((status, data)) |
1059 | 2971c913 | Iustin Pop | |
1060 | 2971c913 | Iustin Pop | return results
|
1061 | 2971c913 | Iustin Pop | |
1062 | 2971c913 | Iustin Pop | |
1063 | db37da70 | Michael Hanselmann | @_RequireOpenQueue
|
1064 | 85f03e0d | Michael Hanselmann | def UpdateJobUnlocked(self, job): |
1065 | ea03467c | Iustin Pop | """Update a job's on disk storage.
|
1066 | ea03467c | Iustin Pop |
|
1067 | ea03467c | Iustin Pop | After a job has been modified, this function needs to be called in
|
1068 | ea03467c | Iustin Pop | order to write the changes to disk and replicate them to the other
|
1069 | ea03467c | Iustin Pop | nodes.
|
1070 | ea03467c | Iustin Pop |
|
1071 | ea03467c | Iustin Pop | @type job: L{_QueuedJob}
|
1072 | ea03467c | Iustin Pop | @param job: the changed job
|
1073 | ea03467c | Iustin Pop |
|
1074 | ea03467c | Iustin Pop | """
|
1075 | f1da30e6 | Michael Hanselmann | filename = self._GetJobPath(job.id)
|
1076 | 23752136 | Michael Hanselmann | data = serializer.DumpJson(job.Serialize(), indent=False)
|
1077 | f1da30e6 | Michael Hanselmann | logging.debug("Writing job %s to %s", job.id, filename)
|
1078 | 23752136 | Michael Hanselmann | self._WriteAndReplicateFileUnlocked(filename, data)
|
1079 | ac0930b9 | Iustin Pop | |
1080 | dfe57c22 | Michael Hanselmann | # Notify waiters about potential changes
|
1081 | 6c5a7090 | Michael Hanselmann | job.change.notifyAll() |
1082 | dfe57c22 | Michael Hanselmann | |
1083 | 6c5a7090 | Michael Hanselmann | @utils.LockedMethod
|
1084 | dfe57c22 | Michael Hanselmann | @_RequireOpenQueue
|
1085 | 5c735209 | Iustin Pop | def WaitForJobChanges(self, job_id, fields, prev_job_info, prev_log_serial, |
1086 | 5c735209 | Iustin Pop | timeout): |
1087 | 6c5a7090 | Michael Hanselmann | """Waits for changes in a job.
|
1088 | 6c5a7090 | Michael Hanselmann |
|
1089 | 6c5a7090 | Michael Hanselmann | @type job_id: string
|
1090 | 6c5a7090 | Michael Hanselmann | @param job_id: Job identifier
|
1091 | 6c5a7090 | Michael Hanselmann | @type fields: list of strings
|
1092 | 6c5a7090 | Michael Hanselmann | @param fields: Which fields to check for changes
|
1093 | 6c5a7090 | Michael Hanselmann | @type prev_job_info: list or None
|
1094 | 6c5a7090 | Michael Hanselmann | @param prev_job_info: Last job information returned
|
1095 | 6c5a7090 | Michael Hanselmann | @type prev_log_serial: int
|
1096 | 6c5a7090 | Michael Hanselmann | @param prev_log_serial: Last job message serial number
|
1097 | 5c735209 | Iustin Pop | @type timeout: float
|
1098 | 5c735209 | Iustin Pop | @param timeout: maximum time to wait
|
1099 | ea03467c | Iustin Pop | @rtype: tuple (job info, log entries)
|
1100 | ea03467c | Iustin Pop | @return: a tuple of the job information as required via
|
1101 | ea03467c | Iustin Pop | the fields parameter, and the log entries as a list
|
1102 | ea03467c | Iustin Pop |
|
1103 | ea03467c | Iustin Pop | if the job has not changed and the timeout has expired,
|
1104 | ea03467c | Iustin Pop | we instead return a special value,
|
1105 | ea03467c | Iustin Pop | L{constants.JOB_NOTCHANGED}, which should be interpreted
|
1106 | ea03467c | Iustin Pop | as such by the clients
|
1107 | 6c5a7090 | Michael Hanselmann |
|
1108 | 6c5a7090 | Michael Hanselmann | """
|
1109 | dfe57c22 | Michael Hanselmann | logging.debug("Waiting for changes in job %s", job_id)
|
1110 | 6e237482 | Michael Hanselmann | |
1111 | 6e237482 | Michael Hanselmann | job_info = None
|
1112 | 6e237482 | Michael Hanselmann | log_entries = None
|
1113 | 6e237482 | Michael Hanselmann | |
1114 | 5c735209 | Iustin Pop | end_time = time.time() + timeout |
1115 | dfe57c22 | Michael Hanselmann | while True: |
1116 | 5c735209 | Iustin Pop | delta_time = end_time - time.time() |
1117 | 5c735209 | Iustin Pop | if delta_time < 0: |
1118 | 5c735209 | Iustin Pop | return constants.JOB_NOTCHANGED
|
1119 | 5c735209 | Iustin Pop | |
1120 | 6c5a7090 | Michael Hanselmann | job = self._LoadJobUnlocked(job_id)
|
1121 | 6c5a7090 | Michael Hanselmann | if not job: |
1122 | 6c5a7090 | Michael Hanselmann | logging.debug("Job %s not found", job_id)
|
1123 | 6c5a7090 | Michael Hanselmann | break
|
1124 | dfe57c22 | Michael Hanselmann | |
1125 | 6c5a7090 | Michael Hanselmann | status = job.CalcStatus() |
1126 | 6c5a7090 | Michael Hanselmann | job_info = self._GetJobInfoUnlocked(job, fields)
|
1127 | 6c5a7090 | Michael Hanselmann | log_entries = job.GetLogEntries(prev_log_serial) |
1128 | dfe57c22 | Michael Hanselmann | |
1129 | dfe57c22 | Michael Hanselmann | # Serializing and deserializing data can cause type changes (e.g. from
|
1130 | dfe57c22 | Michael Hanselmann | # tuple to list) or precision loss. We're doing it here so that we get
|
1131 | dfe57c22 | Michael Hanselmann | # the same modifications as the data received from the client. Without
|
1132 | dfe57c22 | Michael Hanselmann | # this, the comparison afterwards might fail without the data being
|
1133 | dfe57c22 | Michael Hanselmann | # significantly different.
|
1134 | 6c5a7090 | Michael Hanselmann | job_info = serializer.LoadJson(serializer.DumpJson(job_info)) |
1135 | 6c5a7090 | Michael Hanselmann | log_entries = serializer.LoadJson(serializer.DumpJson(log_entries)) |
1136 | dfe57c22 | Michael Hanselmann | |
1137 | 6c5a7090 | Michael Hanselmann | if status not in (constants.JOB_STATUS_QUEUED, |
1138 | e92376d7 | Iustin Pop | constants.JOB_STATUS_RUNNING, |
1139 | e92376d7 | Iustin Pop | constants.JOB_STATUS_WAITLOCK): |
1140 | 6c5a7090 | Michael Hanselmann | # Don't even try to wait if the job is no longer running, there will be
|
1141 | 6c5a7090 | Michael Hanselmann | # no changes.
|
1142 | dfe57c22 | Michael Hanselmann | break
|
1143 | dfe57c22 | Michael Hanselmann | |
1144 | 6c5a7090 | Michael Hanselmann | if (prev_job_info != job_info or |
1145 | 6c5a7090 | Michael Hanselmann | (log_entries and prev_log_serial != log_entries[0][0])): |
1146 | 6c5a7090 | Michael Hanselmann | break
|
1147 | 6c5a7090 | Michael Hanselmann | |
1148 | 6c5a7090 | Michael Hanselmann | logging.debug("Waiting again")
|
1149 | 6c5a7090 | Michael Hanselmann | |
1150 | 6c5a7090 | Michael Hanselmann | # Release the queue lock while waiting
|
1151 | 5c735209 | Iustin Pop | job.change.wait(delta_time) |
1152 | dfe57c22 | Michael Hanselmann | |
1153 | dfe57c22 | Michael Hanselmann | logging.debug("Job %s changed", job_id)
|
1154 | dfe57c22 | Michael Hanselmann | |
1155 | 6e237482 | Michael Hanselmann | if job_info is None and log_entries is None: |
1156 | 6e237482 | Michael Hanselmann | return None |
1157 | 6e237482 | Michael Hanselmann | else:
|
1158 | 6e237482 | Michael Hanselmann | return (job_info, log_entries)
|
1159 | dfe57c22 | Michael Hanselmann | |
1160 | f1da30e6 | Michael Hanselmann | @utils.LockedMethod
|
1161 | db37da70 | Michael Hanselmann | @_RequireOpenQueue
|
1162 | 188c5e0a | Michael Hanselmann | def CancelJob(self, job_id): |
1163 | 188c5e0a | Michael Hanselmann | """Cancels a job.
|
1164 | 188c5e0a | Michael Hanselmann |
|
1165 | ea03467c | Iustin Pop | This will only succeed if the job has not started yet.
|
1166 | ea03467c | Iustin Pop |
|
1167 | 188c5e0a | Michael Hanselmann | @type job_id: string
|
1168 | ea03467c | Iustin Pop | @param job_id: job ID of job to be cancelled.
|
1169 | 188c5e0a | Michael Hanselmann |
|
1170 | 188c5e0a | Michael Hanselmann | """
|
1171 | fbf0262f | Michael Hanselmann | logging.info("Cancelling job %s", job_id)
|
1172 | 188c5e0a | Michael Hanselmann | |
1173 | 85f03e0d | Michael Hanselmann | job = self._LoadJobUnlocked(job_id)
|
1174 | 188c5e0a | Michael Hanselmann | if not job: |
1175 | 188c5e0a | Michael Hanselmann | logging.debug("Job %s not found", job_id)
|
1176 | fbf0262f | Michael Hanselmann | return (False, "Job %s not found" % job_id) |
1177 | fbf0262f | Michael Hanselmann | |
1178 | fbf0262f | Michael Hanselmann | job_status = job.CalcStatus() |
1179 | 188c5e0a | Michael Hanselmann | |
1180 | fbf0262f | Michael Hanselmann | if job_status not in (constants.JOB_STATUS_QUEUED, |
1181 | fbf0262f | Michael Hanselmann | constants.JOB_STATUS_WAITLOCK): |
1182 | a9e97393 | Michael Hanselmann | logging.debug("Job %s is no longer waiting in the queue", job.id)
|
1183 | a9e97393 | Michael Hanselmann | return (False, "Job %s is no longer waiting in the queue" % job.id) |
1184 | fbf0262f | Michael Hanselmann | |
1185 | fbf0262f | Michael Hanselmann | if job_status == constants.JOB_STATUS_QUEUED:
|
1186 | fbf0262f | Michael Hanselmann | self.CancelJobUnlocked(job)
|
1187 | fbf0262f | Michael Hanselmann | return (True, "Job %s canceled" % job.id) |
1188 | 188c5e0a | Michael Hanselmann | |
1189 | fbf0262f | Michael Hanselmann | elif job_status == constants.JOB_STATUS_WAITLOCK:
|
1190 | fbf0262f | Michael Hanselmann | # The worker will notice the new status and cancel the job
|
1191 | fbf0262f | Michael Hanselmann | try:
|
1192 | 34327f51 | Iustin Pop | job.MarkUnfinishedOps(constants.OP_STATUS_CANCELING, None)
|
1193 | fbf0262f | Michael Hanselmann | finally:
|
1194 | fbf0262f | Michael Hanselmann | self.UpdateJobUnlocked(job)
|
1195 | fbf0262f | Michael Hanselmann | return (True, "Job %s will be canceled" % job.id) |
1196 | fbf0262f | Michael Hanselmann | |
1197 | fbf0262f | Michael Hanselmann | @_RequireOpenQueue
|
1198 | fbf0262f | Michael Hanselmann | def CancelJobUnlocked(self, job): |
1199 | fbf0262f | Michael Hanselmann | """Marks a job as canceled.
|
1200 | fbf0262f | Michael Hanselmann |
|
1201 | fbf0262f | Michael Hanselmann | """
|
1202 | 85f03e0d | Michael Hanselmann | try:
|
1203 | 34327f51 | Iustin Pop | job.MarkUnfinishedOps(constants.OP_STATUS_CANCELED, |
1204 | 34327f51 | Iustin Pop | "Job canceled by request")
|
1205 | 85f03e0d | Michael Hanselmann | finally:
|
1206 | 85f03e0d | Michael Hanselmann | self.UpdateJobUnlocked(job)
|
1207 | 188c5e0a | Michael Hanselmann | |
1208 | db37da70 | Michael Hanselmann | @_RequireOpenQueue
|
1209 | d7fd1f28 | Michael Hanselmann | def _ArchiveJobsUnlocked(self, jobs): |
1210 | d7fd1f28 | Michael Hanselmann | """Archives jobs.
|
1211 | c609f802 | Michael Hanselmann |
|
1212 | d7fd1f28 | Michael Hanselmann | @type jobs: list of L{_QueuedJob}
|
1213 | 25e7b43f | Iustin Pop | @param jobs: Job objects
|
1214 | d7fd1f28 | Michael Hanselmann | @rtype: int
|
1215 | d7fd1f28 | Michael Hanselmann | @return: Number of archived jobs
|
1216 | c609f802 | Michael Hanselmann |
|
1217 | c609f802 | Michael Hanselmann | """
|
1218 | d7fd1f28 | Michael Hanselmann | archive_jobs = [] |
1219 | d7fd1f28 | Michael Hanselmann | rename_files = [] |
1220 | d7fd1f28 | Michael Hanselmann | for job in jobs: |
1221 | d7fd1f28 | Michael Hanselmann | if job.CalcStatus() not in (constants.JOB_STATUS_CANCELED, |
1222 | d7fd1f28 | Michael Hanselmann | constants.JOB_STATUS_SUCCESS, |
1223 | d7fd1f28 | Michael Hanselmann | constants.JOB_STATUS_ERROR): |
1224 | d7fd1f28 | Michael Hanselmann | logging.debug("Job %s is not yet done", job.id)
|
1225 | d7fd1f28 | Michael Hanselmann | continue
|
1226 | c609f802 | Michael Hanselmann | |
1227 | d7fd1f28 | Michael Hanselmann | archive_jobs.append(job) |
1228 | c609f802 | Michael Hanselmann | |
1229 | d7fd1f28 | Michael Hanselmann | old = self._GetJobPath(job.id)
|
1230 | d7fd1f28 | Michael Hanselmann | new = self._GetArchivedJobPath(job.id)
|
1231 | d7fd1f28 | Michael Hanselmann | rename_files.append((old, new)) |
1232 | c609f802 | Michael Hanselmann | |
1233 | d7fd1f28 | Michael Hanselmann | # TODO: What if 1..n files fail to rename?
|
1234 | d7fd1f28 | Michael Hanselmann | self._RenameFilesUnlocked(rename_files)
|
1235 | f1da30e6 | Michael Hanselmann | |
1236 | d7fd1f28 | Michael Hanselmann | logging.debug("Successfully archived job(s) %s",
|
1237 | d7fd1f28 | Michael Hanselmann | ", ".join(job.id for job in archive_jobs)) |
1238 | d7fd1f28 | Michael Hanselmann | |
1239 | d7fd1f28 | Michael Hanselmann | return len(archive_jobs) |
1240 | 78d12585 | Michael Hanselmann | |
1241 | 07cd723a | Iustin Pop | @utils.LockedMethod
|
1242 | 07cd723a | Iustin Pop | @_RequireOpenQueue
|
1243 | 07cd723a | Iustin Pop | def ArchiveJob(self, job_id): |
1244 | 07cd723a | Iustin Pop | """Archives a job.
|
1245 | 07cd723a | Iustin Pop |
|
1246 | 25e7b43f | Iustin Pop | This is just a wrapper over L{_ArchiveJobsUnlocked}.
|
1247 | ea03467c | Iustin Pop |
|
1248 | 07cd723a | Iustin Pop | @type job_id: string
|
1249 | 07cd723a | Iustin Pop | @param job_id: Job ID of job to be archived.
|
1250 | 78d12585 | Michael Hanselmann | @rtype: bool
|
1251 | 78d12585 | Michael Hanselmann | @return: Whether job was archived
|
1252 | 07cd723a | Iustin Pop |
|
1253 | 07cd723a | Iustin Pop | """
|
1254 | 78d12585 | Michael Hanselmann | logging.info("Archiving job %s", job_id)
|
1255 | 78d12585 | Michael Hanselmann | |
1256 | 78d12585 | Michael Hanselmann | job = self._LoadJobUnlocked(job_id)
|
1257 | 78d12585 | Michael Hanselmann | if not job: |
1258 | 78d12585 | Michael Hanselmann | logging.debug("Job %s not found", job_id)
|
1259 | 78d12585 | Michael Hanselmann | return False |
1260 | 78d12585 | Michael Hanselmann | |
1261 | 5278185a | Iustin Pop | return self._ArchiveJobsUnlocked([job]) == 1 |
1262 | 07cd723a | Iustin Pop | |
1263 | 07cd723a | Iustin Pop | @utils.LockedMethod
|
1264 | 07cd723a | Iustin Pop | @_RequireOpenQueue
|
1265 | f8ad5591 | Michael Hanselmann | def AutoArchiveJobs(self, age, timeout): |
1266 | 07cd723a | Iustin Pop | """Archives all jobs based on age.
|
1267 | 07cd723a | Iustin Pop |
|
1268 | 07cd723a | Iustin Pop | The method will archive all jobs which are older than the age
|
1269 | 07cd723a | Iustin Pop | parameter. For jobs that don't have an end timestamp, the start
|
1270 | 07cd723a | Iustin Pop | timestamp will be considered. The special '-1' age will cause
|
1271 | 07cd723a | Iustin Pop | archival of all jobs (that are not running or queued).
|
1272 | 07cd723a | Iustin Pop |
|
1273 | 07cd723a | Iustin Pop | @type age: int
|
1274 | 07cd723a | Iustin Pop | @param age: the minimum age in seconds
|
1275 | 07cd723a | Iustin Pop |
|
1276 | 07cd723a | Iustin Pop | """
|
1277 | 07cd723a | Iustin Pop | logging.info("Archiving jobs with age more than %s seconds", age)
|
1278 | 07cd723a | Iustin Pop | |
1279 | 07cd723a | Iustin Pop | now = time.time() |
1280 | f8ad5591 | Michael Hanselmann | end_time = now + timeout |
1281 | f8ad5591 | Michael Hanselmann | archived_count = 0
|
1282 | f8ad5591 | Michael Hanselmann | last_touched = 0
|
1283 | f8ad5591 | Michael Hanselmann | |
1284 | f8ad5591 | Michael Hanselmann | all_job_ids = self._GetJobIDsUnlocked(archived=False) |
1285 | d7fd1f28 | Michael Hanselmann | pending = [] |
1286 | f8ad5591 | Michael Hanselmann | for idx, job_id in enumerate(all_job_ids): |
1287 | f8ad5591 | Michael Hanselmann | last_touched = idx |
1288 | f8ad5591 | Michael Hanselmann | |
1289 | d7fd1f28 | Michael Hanselmann | # Not optimal because jobs could be pending
|
1290 | d7fd1f28 | Michael Hanselmann | # TODO: Measure average duration for job archival and take number of
|
1291 | d7fd1f28 | Michael Hanselmann | # pending jobs into account.
|
1292 | f8ad5591 | Michael Hanselmann | if time.time() > end_time:
|
1293 | f8ad5591 | Michael Hanselmann | break
|
1294 | f8ad5591 | Michael Hanselmann | |
1295 | 78d12585 | Michael Hanselmann | # Returns None if the job failed to load
|
1296 | 78d12585 | Michael Hanselmann | job = self._LoadJobUnlocked(job_id)
|
1297 | f8ad5591 | Michael Hanselmann | if job:
|
1298 | f8ad5591 | Michael Hanselmann | if job.end_timestamp is None: |
1299 | f8ad5591 | Michael Hanselmann | if job.start_timestamp is None: |
1300 | f8ad5591 | Michael Hanselmann | job_age = job.received_timestamp |
1301 | f8ad5591 | Michael Hanselmann | else:
|
1302 | f8ad5591 | Michael Hanselmann | job_age = job.start_timestamp |
1303 | 07cd723a | Iustin Pop | else:
|
1304 | f8ad5591 | Michael Hanselmann | job_age = job.end_timestamp |
1305 | f8ad5591 | Michael Hanselmann | |
1306 | f8ad5591 | Michael Hanselmann | if age == -1 or now - job_age[0] > age: |
1307 | d7fd1f28 | Michael Hanselmann | pending.append(job) |
1308 | d7fd1f28 | Michael Hanselmann | |
1309 | d7fd1f28 | Michael Hanselmann | # Archive 10 jobs at a time
|
1310 | d7fd1f28 | Michael Hanselmann | if len(pending) >= 10: |
1311 | d7fd1f28 | Michael Hanselmann | archived_count += self._ArchiveJobsUnlocked(pending)
|
1312 | d7fd1f28 | Michael Hanselmann | pending = [] |
1313 | f8ad5591 | Michael Hanselmann | |
1314 | d7fd1f28 | Michael Hanselmann | if pending:
|
1315 | d7fd1f28 | Michael Hanselmann | archived_count += self._ArchiveJobsUnlocked(pending)
|
1316 | 07cd723a | Iustin Pop | |
1317 | f8ad5591 | Michael Hanselmann | return (archived_count, len(all_job_ids) - last_touched - 1) |
1318 | 07cd723a | Iustin Pop | |
1319 | 85f03e0d | Michael Hanselmann | def _GetJobInfoUnlocked(self, job, fields): |
1320 | ea03467c | Iustin Pop | """Returns information about a job.
|
1321 | ea03467c | Iustin Pop |
|
1322 | ea03467c | Iustin Pop | @type job: L{_QueuedJob}
|
1323 | ea03467c | Iustin Pop | @param job: the job which we query
|
1324 | ea03467c | Iustin Pop | @type fields: list
|
1325 | ea03467c | Iustin Pop | @param fields: names of fields to return
|
1326 | ea03467c | Iustin Pop | @rtype: list
|
1327 | ea03467c | Iustin Pop | @return: list with one element for each field
|
1328 | ea03467c | Iustin Pop | @raise errors.OpExecError: when an invalid field
|
1329 | ea03467c | Iustin Pop | has been passed
|
1330 | ea03467c | Iustin Pop |
|
1331 | ea03467c | Iustin Pop | """
|
1332 | e2715f69 | Michael Hanselmann | row = [] |
1333 | e2715f69 | Michael Hanselmann | for fname in fields: |
1334 | e2715f69 | Michael Hanselmann | if fname == "id": |
1335 | e2715f69 | Michael Hanselmann | row.append(job.id) |
1336 | e2715f69 | Michael Hanselmann | elif fname == "status": |
1337 | 85f03e0d | Michael Hanselmann | row.append(job.CalcStatus()) |
1338 | af30b2fd | Michael Hanselmann | elif fname == "ops": |
1339 | 85f03e0d | Michael Hanselmann | row.append([op.input.__getstate__() for op in job.ops]) |
1340 | af30b2fd | Michael Hanselmann | elif fname == "opresult": |
1341 | 85f03e0d | Michael Hanselmann | row.append([op.result for op in job.ops]) |
1342 | af30b2fd | Michael Hanselmann | elif fname == "opstatus": |
1343 | 85f03e0d | Michael Hanselmann | row.append([op.status for op in job.ops]) |
1344 | 5b23c34c | Iustin Pop | elif fname == "oplog": |
1345 | 5b23c34c | Iustin Pop | row.append([op.log for op in job.ops]) |
1346 | c56ec146 | Iustin Pop | elif fname == "opstart": |
1347 | c56ec146 | Iustin Pop | row.append([op.start_timestamp for op in job.ops]) |
1348 | c56ec146 | Iustin Pop | elif fname == "opend": |
1349 | c56ec146 | Iustin Pop | row.append([op.end_timestamp for op in job.ops]) |
1350 | c56ec146 | Iustin Pop | elif fname == "received_ts": |
1351 | c56ec146 | Iustin Pop | row.append(job.received_timestamp) |
1352 | c56ec146 | Iustin Pop | elif fname == "start_ts": |
1353 | c56ec146 | Iustin Pop | row.append(job.start_timestamp) |
1354 | c56ec146 | Iustin Pop | elif fname == "end_ts": |
1355 | c56ec146 | Iustin Pop | row.append(job.end_timestamp) |
1356 | 60dd1473 | Iustin Pop | elif fname == "summary": |
1357 | 60dd1473 | Iustin Pop | row.append([op.input.Summary() for op in job.ops]) |
1358 | e2715f69 | Michael Hanselmann | else:
|
1359 | e2715f69 | Michael Hanselmann | raise errors.OpExecError("Invalid job query field '%s'" % fname) |
1360 | e2715f69 | Michael Hanselmann | return row
|
1361 | e2715f69 | Michael Hanselmann | |
1362 | 85f03e0d | Michael Hanselmann | @utils.LockedMethod
|
1363 | db37da70 | Michael Hanselmann | @_RequireOpenQueue
|
1364 | e2715f69 | Michael Hanselmann | def QueryJobs(self, job_ids, fields): |
1365 | e2715f69 | Michael Hanselmann | """Returns a list of jobs in queue.
|
1366 | e2715f69 | Michael Hanselmann |
|
1367 | ea03467c | Iustin Pop | This is a wrapper of L{_GetJobsUnlocked}, which actually does the
|
1368 | ea03467c | Iustin Pop | processing for each job.
|
1369 | ea03467c | Iustin Pop |
|
1370 | ea03467c | Iustin Pop | @type job_ids: list
|
1371 | ea03467c | Iustin Pop | @param job_ids: sequence of job identifiers or None for all
|
1372 | ea03467c | Iustin Pop | @type fields: list
|
1373 | ea03467c | Iustin Pop | @param fields: names of fields to return
|
1374 | ea03467c | Iustin Pop | @rtype: list
|
1375 | ea03467c | Iustin Pop | @return: list one element per job, each element being list with
|
1376 | ea03467c | Iustin Pop | the requested fields
|
1377 | e2715f69 | Michael Hanselmann |
|
1378 | e2715f69 | Michael Hanselmann | """
|
1379 | 85f03e0d | Michael Hanselmann | jobs = [] |
1380 | e2715f69 | Michael Hanselmann | |
1381 | 85f03e0d | Michael Hanselmann | for job in self._GetJobsUnlocked(job_ids): |
1382 | 85f03e0d | Michael Hanselmann | if job is None: |
1383 | 85f03e0d | Michael Hanselmann | jobs.append(None)
|
1384 | 85f03e0d | Michael Hanselmann | else:
|
1385 | 85f03e0d | Michael Hanselmann | jobs.append(self._GetJobInfoUnlocked(job, fields))
|
1386 | e2715f69 | Michael Hanselmann | |
1387 | 85f03e0d | Michael Hanselmann | return jobs
|
1388 | e2715f69 | Michael Hanselmann | |
1389 | f1da30e6 | Michael Hanselmann | @utils.LockedMethod
|
1390 | db37da70 | Michael Hanselmann | @_RequireOpenQueue
|
1391 | e2715f69 | Michael Hanselmann | def Shutdown(self): |
1392 | e2715f69 | Michael Hanselmann | """Stops the job queue.
|
1393 | e2715f69 | Michael Hanselmann |
|
1394 | ea03467c | Iustin Pop | This shutdowns all the worker threads an closes the queue.
|
1395 | ea03467c | Iustin Pop |
|
1396 | e2715f69 | Michael Hanselmann | """
|
1397 | e2715f69 | Michael Hanselmann | self._wpool.TerminateWorkers()
|
1398 | 85f03e0d | Michael Hanselmann | |
1399 | 04ab05ce | Michael Hanselmann | self._queue_lock.Close()
|
1400 | 04ab05ce | Michael Hanselmann | self._queue_lock = None |