root / lib / jqueue.py @ 188c5e0a
History | View | Annotate | Download (18.8 kB)
1 | 498ae1cc | Iustin Pop | #
|
---|---|---|---|
2 | 498ae1cc | Iustin Pop | #
|
3 | 498ae1cc | Iustin Pop | |
4 | 498ae1cc | Iustin Pop | # Copyright (C) 2006, 2007 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 | 498ae1cc | Iustin Pop | """Module implementing the job queue handling."""
|
23 | 498ae1cc | Iustin Pop | |
24 | f1da30e6 | Michael Hanselmann | import os |
25 | e2715f69 | Michael Hanselmann | import logging |
26 | e2715f69 | Michael Hanselmann | import threading |
27 | f1da30e6 | Michael Hanselmann | import errno |
28 | f1da30e6 | Michael Hanselmann | import re |
29 | f1048938 | Iustin Pop | import time |
30 | 498ae1cc | Iustin Pop | |
31 | e2715f69 | Michael Hanselmann | from ganeti import constants |
32 | f1da30e6 | Michael Hanselmann | from ganeti import serializer |
33 | e2715f69 | Michael Hanselmann | from ganeti import workerpool |
34 | f1da30e6 | Michael Hanselmann | from ganeti import opcodes |
35 | 7a1ecaed | Iustin Pop | from ganeti import errors |
36 | e2715f69 | Michael Hanselmann | from ganeti import mcpu |
37 | 7996a135 | Iustin Pop | from ganeti import utils |
38 | c3f0a12f | Iustin Pop | from ganeti import rpc |
39 | e2715f69 | Michael Hanselmann | |
40 | e2715f69 | Michael Hanselmann | |
41 | e2715f69 | Michael Hanselmann | JOBQUEUE_THREADS = 5
|
42 | e2715f69 | Michael Hanselmann | |
43 | 498ae1cc | Iustin Pop | |
44 | e2715f69 | Michael Hanselmann | class _QueuedOpCode(object): |
45 | e2715f69 | Michael Hanselmann | """Encasulates an opcode object.
|
46 | e2715f69 | Michael Hanselmann |
|
47 | 307149a8 | Iustin Pop | Access is synchronized by the '_lock' attribute.
|
48 | e2715f69 | Michael Hanselmann |
|
49 | f1048938 | Iustin Pop | The 'log' attribute holds the execution log and consists of tuples
|
50 | f1048938 | Iustin Pop | of the form (timestamp, level, message).
|
51 | f1048938 | Iustin Pop |
|
52 | e2715f69 | Michael Hanselmann | """
|
53 | e2715f69 | Michael Hanselmann | def __init__(self, op): |
54 | f1048938 | Iustin Pop | self.__Setup(op, constants.OP_STATUS_QUEUED, None, []) |
55 | f1da30e6 | Michael Hanselmann | |
56 | f1048938 | Iustin Pop | def __Setup(self, input_, status, result, log): |
57 | 307149a8 | Iustin Pop | self._lock = threading.Lock()
|
58 | f1048938 | Iustin Pop | self.input = input_
|
59 | f1da30e6 | Michael Hanselmann | self.status = status
|
60 | f1da30e6 | Michael Hanselmann | self.result = result
|
61 | f1048938 | Iustin Pop | self.log = log
|
62 | f1da30e6 | Michael Hanselmann | |
63 | f1da30e6 | Michael Hanselmann | @classmethod
|
64 | f1da30e6 | Michael Hanselmann | def Restore(cls, state): |
65 | f1da30e6 | Michael Hanselmann | obj = object.__new__(cls)
|
66 | f1da30e6 | Michael Hanselmann | obj.__Setup(opcodes.OpCode.LoadOpCode(state["input"]),
|
67 | f1048938 | Iustin Pop | state["status"], state["result"], state["log"]) |
68 | f1da30e6 | Michael Hanselmann | return obj
|
69 | f1da30e6 | Michael Hanselmann | |
70 | f1da30e6 | Michael Hanselmann | @utils.LockedMethod
|
71 | f1da30e6 | Michael Hanselmann | def Serialize(self): |
72 | f1da30e6 | Michael Hanselmann | return {
|
73 | f1da30e6 | Michael Hanselmann | "input": self.input.__getstate__(), |
74 | f1da30e6 | Michael Hanselmann | "status": self.status, |
75 | f1da30e6 | Michael Hanselmann | "result": self.result, |
76 | f1048938 | Iustin Pop | "log": self.log, |
77 | f1da30e6 | Michael Hanselmann | } |
78 | 307149a8 | Iustin Pop | |
79 | 307149a8 | Iustin Pop | @utils.LockedMethod
|
80 | af30b2fd | Michael Hanselmann | def GetInput(self): |
81 | af30b2fd | Michael Hanselmann | """Returns the original opcode.
|
82 | af30b2fd | Michael Hanselmann |
|
83 | af30b2fd | Michael Hanselmann | """
|
84 | af30b2fd | Michael Hanselmann | return self.input |
85 | af30b2fd | Michael Hanselmann | |
86 | af30b2fd | Michael Hanselmann | @utils.LockedMethod
|
87 | 307149a8 | Iustin Pop | def SetStatus(self, status, result): |
88 | 307149a8 | Iustin Pop | """Update the opcode status and result.
|
89 | 307149a8 | Iustin Pop |
|
90 | 307149a8 | Iustin Pop | """
|
91 | 307149a8 | Iustin Pop | self.status = status
|
92 | 307149a8 | Iustin Pop | self.result = result
|
93 | 307149a8 | Iustin Pop | |
94 | 307149a8 | Iustin Pop | @utils.LockedMethod
|
95 | 307149a8 | Iustin Pop | def GetStatus(self): |
96 | 307149a8 | Iustin Pop | """Get the opcode status.
|
97 | 307149a8 | Iustin Pop |
|
98 | 307149a8 | Iustin Pop | """
|
99 | 307149a8 | Iustin Pop | return self.status |
100 | 307149a8 | Iustin Pop | |
101 | 307149a8 | Iustin Pop | @utils.LockedMethod
|
102 | 307149a8 | Iustin Pop | def GetResult(self): |
103 | 307149a8 | Iustin Pop | """Get the opcode result.
|
104 | 307149a8 | Iustin Pop |
|
105 | 307149a8 | Iustin Pop | """
|
106 | 307149a8 | Iustin Pop | return self.result |
107 | e2715f69 | Michael Hanselmann | |
108 | f1048938 | Iustin Pop | @utils.LockedMethod
|
109 | f1048938 | Iustin Pop | def Log(self, *args): |
110 | f1048938 | Iustin Pop | """Append a log entry.
|
111 | f1048938 | Iustin Pop |
|
112 | f1048938 | Iustin Pop | """
|
113 | f1048938 | Iustin Pop | assert len(args) < 2 |
114 | f1048938 | Iustin Pop | |
115 | f1048938 | Iustin Pop | if len(args) == 1: |
116 | f1048938 | Iustin Pop | log_type = constants.ELOG_MESSAGE |
117 | f1048938 | Iustin Pop | log_msg = args[0]
|
118 | f1048938 | Iustin Pop | else:
|
119 | f1048938 | Iustin Pop | log_type, log_msg = args |
120 | f1048938 | Iustin Pop | self.log.append((time.time(), log_type, log_msg))
|
121 | f1048938 | Iustin Pop | |
122 | f1048938 | Iustin Pop | @utils.LockedMethod
|
123 | f1048938 | Iustin Pop | def RetrieveLog(self, start_at=0): |
124 | f1048938 | Iustin Pop | """Retrieve (a part of) the execution log.
|
125 | f1048938 | Iustin Pop |
|
126 | f1048938 | Iustin Pop | """
|
127 | f1048938 | Iustin Pop | return self.log[start_at:] |
128 | f1048938 | Iustin Pop | |
129 | e2715f69 | Michael Hanselmann | |
130 | e2715f69 | Michael Hanselmann | class _QueuedJob(object): |
131 | e2715f69 | Michael Hanselmann | """In-memory job representation.
|
132 | e2715f69 | Michael Hanselmann |
|
133 | e2715f69 | Michael Hanselmann | This is what we use to track the user-submitted jobs.
|
134 | e2715f69 | Michael Hanselmann |
|
135 | e2715f69 | Michael Hanselmann | """
|
136 | f1da30e6 | Michael Hanselmann | def __init__(self, storage, job_id, ops): |
137 | e2715f69 | Michael Hanselmann | if not ops: |
138 | e2715f69 | Michael Hanselmann | # TODO
|
139 | e2715f69 | Michael Hanselmann | raise Exception("No opcodes") |
140 | e2715f69 | Michael Hanselmann | |
141 | f1048938 | Iustin Pop | self.__Setup(storage, job_id, [_QueuedOpCode(op) for op in ops], -1) |
142 | e2715f69 | Michael Hanselmann | |
143 | f1048938 | Iustin Pop | def __Setup(self, storage, job_id, ops, run_op_index): |
144 | f1048938 | Iustin Pop | self._lock = threading.Lock()
|
145 | f1da30e6 | Michael Hanselmann | self.storage = storage
|
146 | f1da30e6 | Michael Hanselmann | self.id = job_id
|
147 | f1da30e6 | Michael Hanselmann | self._ops = ops
|
148 | f1048938 | Iustin Pop | self.run_op_index = run_op_index
|
149 | f1da30e6 | Michael Hanselmann | |
150 | f1da30e6 | Michael Hanselmann | @classmethod
|
151 | f1da30e6 | Michael Hanselmann | def Restore(cls, storage, state): |
152 | f1da30e6 | Michael Hanselmann | obj = object.__new__(cls)
|
153 | f1048938 | Iustin Pop | op_list = [_QueuedOpCode.Restore(op_state) for op_state in state["ops"]] |
154 | f1048938 | Iustin Pop | obj.__Setup(storage, state["id"], op_list, state["run_op_index"]) |
155 | f1da30e6 | Michael Hanselmann | return obj
|
156 | f1da30e6 | Michael Hanselmann | |
157 | f1da30e6 | Michael Hanselmann | def Serialize(self): |
158 | f1da30e6 | Michael Hanselmann | return {
|
159 | f1da30e6 | Michael Hanselmann | "id": self.id, |
160 | f1da30e6 | Michael Hanselmann | "ops": [op.Serialize() for op in self._ops], |
161 | f1048938 | Iustin Pop | "run_op_index": self.run_op_index, |
162 | f1da30e6 | Michael Hanselmann | } |
163 | f1da30e6 | Michael Hanselmann | |
164 | 4cb1d919 | Michael Hanselmann | def _SetStatus(self, status, msg): |
165 | f1da30e6 | Michael Hanselmann | try:
|
166 | f1da30e6 | Michael Hanselmann | for op in self._ops: |
167 | 4cb1d919 | Michael Hanselmann | op.SetStatus(status, msg) |
168 | f1da30e6 | Michael Hanselmann | finally:
|
169 | f1da30e6 | Michael Hanselmann | self.storage.UpdateJob(self) |
170 | e2715f69 | Michael Hanselmann | |
171 | 4cb1d919 | Michael Hanselmann | def SetUnclean(self, msg): |
172 | 4cb1d919 | Michael Hanselmann | return self._SetStatus(constants.OP_STATUS_ERROR, msg) |
173 | 4cb1d919 | Michael Hanselmann | |
174 | 4cb1d919 | Michael Hanselmann | def SetCanceled(self, msg): |
175 | 4cb1d919 | Michael Hanselmann | return self._SetStatus(constants.JOB_STATUS_CANCELED, msg) |
176 | 4cb1d919 | Michael Hanselmann | |
177 | 307149a8 | Iustin Pop | def GetStatus(self): |
178 | e2715f69 | Michael Hanselmann | status = constants.JOB_STATUS_QUEUED |
179 | e2715f69 | Michael Hanselmann | |
180 | e2715f69 | Michael Hanselmann | all_success = True
|
181 | e2715f69 | Michael Hanselmann | for op in self._ops: |
182 | 307149a8 | Iustin Pop | op_status = op.GetStatus() |
183 | 307149a8 | Iustin Pop | if op_status == constants.OP_STATUS_SUCCESS:
|
184 | e2715f69 | Michael Hanselmann | continue
|
185 | e2715f69 | Michael Hanselmann | |
186 | e2715f69 | Michael Hanselmann | all_success = False
|
187 | e2715f69 | Michael Hanselmann | |
188 | 307149a8 | Iustin Pop | if op_status == constants.OP_STATUS_QUEUED:
|
189 | e2715f69 | Michael Hanselmann | pass
|
190 | 307149a8 | Iustin Pop | elif op_status == constants.OP_STATUS_RUNNING:
|
191 | e2715f69 | Michael Hanselmann | status = constants.JOB_STATUS_RUNNING |
192 | f1da30e6 | Michael Hanselmann | elif op_status == constants.OP_STATUS_ERROR:
|
193 | f1da30e6 | Michael Hanselmann | status = constants.JOB_STATUS_ERROR |
194 | f1da30e6 | Michael Hanselmann | # The whole job fails if one opcode failed
|
195 | f1da30e6 | Michael Hanselmann | break
|
196 | 4cb1d919 | Michael Hanselmann | elif op_status == constants.OP_STATUS_CANCELED:
|
197 | 4cb1d919 | Michael Hanselmann | status = constants.OP_STATUS_CANCELED |
198 | 4cb1d919 | Michael Hanselmann | break
|
199 | e2715f69 | Michael Hanselmann | |
200 | e2715f69 | Michael Hanselmann | if all_success:
|
201 | e2715f69 | Michael Hanselmann | status = constants.JOB_STATUS_SUCCESS |
202 | e2715f69 | Michael Hanselmann | |
203 | e2715f69 | Michael Hanselmann | return status
|
204 | e2715f69 | Michael Hanselmann | |
205 | f1048938 | Iustin Pop | @utils.LockedMethod
|
206 | f1048938 | Iustin Pop | def GetRunOpIndex(self): |
207 | f1048938 | Iustin Pop | return self.run_op_index |
208 | f1048938 | Iustin Pop | |
209 | e2715f69 | Michael Hanselmann | def Run(self, proc): |
210 | e2715f69 | Michael Hanselmann | """Job executor.
|
211 | e2715f69 | Michael Hanselmann |
|
212 | e2715f69 | Michael Hanselmann | This functions processes a this job in the context of given processor
|
213 | e2715f69 | Michael Hanselmann | instance.
|
214 | e2715f69 | Michael Hanselmann |
|
215 | e2715f69 | Michael Hanselmann | Args:
|
216 | e2715f69 | Michael Hanselmann | - proc: Ganeti Processor to run the job with
|
217 | e2715f69 | Michael Hanselmann |
|
218 | e2715f69 | Michael Hanselmann | """
|
219 | e2715f69 | Michael Hanselmann | try:
|
220 | c8549bfd | Michael Hanselmann | count = len(self._ops) |
221 | c8549bfd | Michael Hanselmann | for idx, op in enumerate(self._ops): |
222 | e2715f69 | Michael Hanselmann | try:
|
223 | 307149a8 | Iustin Pop | logging.debug("Op %s/%s: Starting %s", idx + 1, count, op) |
224 | f1048938 | Iustin Pop | |
225 | f1048938 | Iustin Pop | self._lock.acquire()
|
226 | f1048938 | Iustin Pop | try:
|
227 | f1048938 | Iustin Pop | self.run_op_index = idx
|
228 | f1048938 | Iustin Pop | finally:
|
229 | f1048938 | Iustin Pop | self._lock.release()
|
230 | f1048938 | Iustin Pop | |
231 | 307149a8 | Iustin Pop | op.SetStatus(constants.OP_STATUS_RUNNING, None)
|
232 | f1da30e6 | Michael Hanselmann | self.storage.UpdateJob(self) |
233 | e2715f69 | Michael Hanselmann | |
234 | f1048938 | Iustin Pop | result = proc.ExecOpCode(op.input, op.Log) |
235 | e2715f69 | Michael Hanselmann | |
236 | 307149a8 | Iustin Pop | op.SetStatus(constants.OP_STATUS_SUCCESS, result) |
237 | f1da30e6 | Michael Hanselmann | self.storage.UpdateJob(self) |
238 | 307149a8 | Iustin Pop | logging.debug("Op %s/%s: Successfully finished %s",
|
239 | 307149a8 | Iustin Pop | idx + 1, count, op)
|
240 | e2715f69 | Michael Hanselmann | except Exception, err: |
241 | f1da30e6 | Michael Hanselmann | try:
|
242 | f1da30e6 | Michael Hanselmann | op.SetStatus(constants.OP_STATUS_ERROR, str(err))
|
243 | f1da30e6 | Michael Hanselmann | logging.debug("Op %s/%s: Error in %s", idx + 1, count, op) |
244 | f1da30e6 | Michael Hanselmann | finally:
|
245 | f1da30e6 | Michael Hanselmann | self.storage.UpdateJob(self) |
246 | e2715f69 | Michael Hanselmann | raise
|
247 | e2715f69 | Michael Hanselmann | |
248 | e2715f69 | Michael Hanselmann | except errors.GenericError, err:
|
249 | e2715f69 | Michael Hanselmann | logging.error("ganeti exception %s", exc_info=err)
|
250 | e2715f69 | Michael Hanselmann | except Exception, err: |
251 | e2715f69 | Michael Hanselmann | logging.error("unhandled exception %s", exc_info=err)
|
252 | e2715f69 | Michael Hanselmann | except:
|
253 | e2715f69 | Michael Hanselmann | logging.error("unhandled unknown exception %s", exc_info=err)
|
254 | e2715f69 | Michael Hanselmann | |
255 | e2715f69 | Michael Hanselmann | |
256 | e2715f69 | Michael Hanselmann | class _JobQueueWorker(workerpool.BaseWorker): |
257 | e2715f69 | Michael Hanselmann | def RunTask(self, job): |
258 | e2715f69 | Michael Hanselmann | logging.debug("Worker %s processing job %s",
|
259 | e2715f69 | Michael Hanselmann | self.worker_id, job.id)
|
260 | e2715f69 | Michael Hanselmann | # TODO: feedback function
|
261 | f1048938 | Iustin Pop | proc = mcpu.Processor(self.pool.context)
|
262 | e2715f69 | Michael Hanselmann | try:
|
263 | e2715f69 | Michael Hanselmann | job.Run(proc) |
264 | e2715f69 | Michael Hanselmann | finally:
|
265 | e2715f69 | Michael Hanselmann | logging.debug("Worker %s finished job %s, status = %s",
|
266 | e2715f69 | Michael Hanselmann | self.worker_id, job.id, job.GetStatus())
|
267 | e2715f69 | Michael Hanselmann | |
268 | e2715f69 | Michael Hanselmann | |
269 | e2715f69 | Michael Hanselmann | class _JobQueueWorkerPool(workerpool.WorkerPool): |
270 | e2715f69 | Michael Hanselmann | def __init__(self, context): |
271 | e2715f69 | Michael Hanselmann | super(_JobQueueWorkerPool, self).__init__(JOBQUEUE_THREADS, |
272 | e2715f69 | Michael Hanselmann | _JobQueueWorker) |
273 | e2715f69 | Michael Hanselmann | self.context = context
|
274 | e2715f69 | Michael Hanselmann | |
275 | e2715f69 | Michael Hanselmann | |
276 | ce594241 | Michael Hanselmann | class JobStorageBase(object): |
277 | ce594241 | Michael Hanselmann | def __init__(self, id_prefix): |
278 | ce594241 | Michael Hanselmann | self.id_prefix = id_prefix
|
279 | ce594241 | Michael Hanselmann | |
280 | ce594241 | Michael Hanselmann | if id_prefix:
|
281 | ce594241 | Michael Hanselmann | prefix_pattern = re.escape("%s-" % id_prefix)
|
282 | ce594241 | Michael Hanselmann | else:
|
283 | ce594241 | Michael Hanselmann | prefix_pattern = ""
|
284 | ce594241 | Michael Hanselmann | |
285 | ce594241 | Michael Hanselmann | # Apart from the prefix, all job IDs are numeric
|
286 | ce594241 | Michael Hanselmann | self._re_job_id = re.compile(r"^%s\d+$" % prefix_pattern) |
287 | ce594241 | Michael Hanselmann | |
288 | ce594241 | Michael Hanselmann | def OwnsJobId(self, job_id): |
289 | ce594241 | Michael Hanselmann | return self._re_job_id.match(job_id) |
290 | ce594241 | Michael Hanselmann | |
291 | ce594241 | Michael Hanselmann | def FormatJobID(self, job_id): |
292 | ce594241 | Michael Hanselmann | if not isinstance(job_id, (int, long)): |
293 | ce594241 | Michael Hanselmann | raise errors.ProgrammerError("Job ID '%s' not numeric" % job_id) |
294 | ce594241 | Michael Hanselmann | if job_id < 0: |
295 | ce594241 | Michael Hanselmann | raise errors.ProgrammerError("Job ID %s is negative" % job_id) |
296 | ce594241 | Michael Hanselmann | |
297 | ce594241 | Michael Hanselmann | if self.id_prefix: |
298 | ce594241 | Michael Hanselmann | prefix = "%s-" % self.id_prefix |
299 | ce594241 | Michael Hanselmann | else:
|
300 | ce594241 | Michael Hanselmann | prefix = ""
|
301 | ce594241 | Michael Hanselmann | |
302 | ce594241 | Michael Hanselmann | return "%s%010d" % (prefix, job_id) |
303 | ce594241 | Michael Hanselmann | |
304 | c609f802 | Michael Hanselmann | def _ShouldJobBeArchivedUnlocked(self, job): |
305 | c609f802 | Michael Hanselmann | if job.GetStatus() not in (constants.JOB_STATUS_CANCELED, |
306 | c609f802 | Michael Hanselmann | constants.JOB_STATUS_SUCCESS, |
307 | c609f802 | Michael Hanselmann | constants.JOB_STATUS_ERROR): |
308 | c609f802 | Michael Hanselmann | logging.debug("Job %s is not yet done", job.id)
|
309 | c609f802 | Michael Hanselmann | return False |
310 | c609f802 | Michael Hanselmann | return True |
311 | c609f802 | Michael Hanselmann | |
312 | ce594241 | Michael Hanselmann | |
313 | ce594241 | Michael Hanselmann | class DiskJobStorage(JobStorageBase): |
314 | bac5ffc3 | Oleksiy Mishchenko | _RE_JOB_FILE = re.compile(r"^job-(%s)$" % constants.JOB_ID_TEMPLATE)
|
315 | f1da30e6 | Michael Hanselmann | |
316 | ce594241 | Michael Hanselmann | def __init__(self, id_prefix): |
317 | ce594241 | Michael Hanselmann | JobStorageBase.__init__(self, id_prefix)
|
318 | ce594241 | Michael Hanselmann | |
319 | f1da30e6 | Michael Hanselmann | self._lock = threading.Lock()
|
320 | ac0930b9 | Iustin Pop | self._memcache = {}
|
321 | c3f0a12f | Iustin Pop | self._my_hostname = utils.HostInfo().name
|
322 | f1da30e6 | Michael Hanselmann | |
323 | 0cb94105 | Michael Hanselmann | # Make sure our directories exists
|
324 | 0cb94105 | Michael Hanselmann | for path in (constants.QUEUE_DIR, constants.JOB_QUEUE_ARCHIVE_DIR): |
325 | 0cb94105 | Michael Hanselmann | try:
|
326 | 0cb94105 | Michael Hanselmann | os.mkdir(path, 0700)
|
327 | 0cb94105 | Michael Hanselmann | except OSError, err: |
328 | 0cb94105 | Michael Hanselmann | if err.errno not in (errno.EEXIST, ): |
329 | 0cb94105 | Michael Hanselmann | raise
|
330 | f1da30e6 | Michael Hanselmann | |
331 | f1da30e6 | Michael Hanselmann | # Get queue lock
|
332 | f1da30e6 | Michael Hanselmann | self.lock_fd = open(constants.JOB_QUEUE_LOCK_FILE, "w") |
333 | f1da30e6 | Michael Hanselmann | try:
|
334 | f1da30e6 | Michael Hanselmann | utils.LockFile(self.lock_fd)
|
335 | f1da30e6 | Michael Hanselmann | except:
|
336 | f1da30e6 | Michael Hanselmann | self.lock_fd.close()
|
337 | f1da30e6 | Michael Hanselmann | raise
|
338 | f1da30e6 | Michael Hanselmann | |
339 | f1da30e6 | Michael Hanselmann | # Read version
|
340 | f1da30e6 | Michael Hanselmann | try:
|
341 | f1da30e6 | Michael Hanselmann | version_fd = open(constants.JOB_QUEUE_VERSION_FILE, "r") |
342 | f1da30e6 | Michael Hanselmann | except IOError, err: |
343 | f1da30e6 | Michael Hanselmann | if err.errno not in (errno.ENOENT, ): |
344 | f1da30e6 | Michael Hanselmann | raise
|
345 | f1da30e6 | Michael Hanselmann | |
346 | f1da30e6 | Michael Hanselmann | # Setup a new queue
|
347 | f1da30e6 | Michael Hanselmann | self._InitQueueUnlocked()
|
348 | f1da30e6 | Michael Hanselmann | |
349 | f1da30e6 | Michael Hanselmann | # Try to open again
|
350 | f1da30e6 | Michael Hanselmann | version_fd = open(constants.JOB_QUEUE_VERSION_FILE, "r") |
351 | f1da30e6 | Michael Hanselmann | |
352 | f1da30e6 | Michael Hanselmann | try:
|
353 | f1da30e6 | Michael Hanselmann | # Try to read version
|
354 | f1da30e6 | Michael Hanselmann | version = int(version_fd.read(128)) |
355 | f1da30e6 | Michael Hanselmann | |
356 | f1da30e6 | Michael Hanselmann | # Verify version
|
357 | f1da30e6 | Michael Hanselmann | if version != constants.JOB_QUEUE_VERSION:
|
358 | f1da30e6 | Michael Hanselmann | raise errors.JobQueueError("Found version %s, expected %s", |
359 | f1da30e6 | Michael Hanselmann | version, constants.JOB_QUEUE_VERSION) |
360 | f1da30e6 | Michael Hanselmann | finally:
|
361 | f1da30e6 | Michael Hanselmann | version_fd.close() |
362 | f1da30e6 | Michael Hanselmann | |
363 | c4beba1c | Iustin Pop | self._last_serial = self._ReadSerial() |
364 | c4beba1c | Iustin Pop | if self._last_serial is None: |
365 | c4beba1c | Iustin Pop | raise errors.ConfigurationError("Can't read/parse the job queue serial" |
366 | c4beba1c | Iustin Pop | " file")
|
367 | c4beba1c | Iustin Pop | |
368 | c4beba1c | Iustin Pop | @staticmethod
|
369 | c4beba1c | Iustin Pop | def _ReadSerial(): |
370 | c4beba1c | Iustin Pop | """Try to read the job serial file.
|
371 | c4beba1c | Iustin Pop |
|
372 | c4beba1c | Iustin Pop | @rtype: None or int
|
373 | c4beba1c | Iustin Pop | @return: If the serial can be read, then it is returned. Otherwise None
|
374 | c4beba1c | Iustin Pop | is returned.
|
375 | c4beba1c | Iustin Pop |
|
376 | c4beba1c | Iustin Pop | """
|
377 | f1da30e6 | Michael Hanselmann | try:
|
378 | c4beba1c | Iustin Pop | serial_fd = open(constants.JOB_QUEUE_SERIAL_FILE, "r") |
379 | c4beba1c | Iustin Pop | try:
|
380 | c4beba1c | Iustin Pop | # Read last serial
|
381 | c4beba1c | Iustin Pop | serial = int(serial_fd.read(1024).strip()) |
382 | c4beba1c | Iustin Pop | finally:
|
383 | c4beba1c | Iustin Pop | serial_fd.close() |
384 | c4beba1c | Iustin Pop | except (ValueError, EnvironmentError): |
385 | c4beba1c | Iustin Pop | serial = None
|
386 | c4beba1c | Iustin Pop | |
387 | c4beba1c | Iustin Pop | return serial
|
388 | f1da30e6 | Michael Hanselmann | |
389 | f1da30e6 | Michael Hanselmann | def Close(self): |
390 | f1da30e6 | Michael Hanselmann | assert self.lock_fd, "Queue should be open" |
391 | f1da30e6 | Michael Hanselmann | |
392 | f1da30e6 | Michael Hanselmann | self.lock_fd.close()
|
393 | f1da30e6 | Michael Hanselmann | self.lock_fd = None |
394 | f1da30e6 | Michael Hanselmann | |
395 | f1da30e6 | Michael Hanselmann | def _InitQueueUnlocked(self): |
396 | f1da30e6 | Michael Hanselmann | assert self.lock_fd, "Queue should be open" |
397 | f1da30e6 | Michael Hanselmann | |
398 | f1da30e6 | Michael Hanselmann | utils.WriteFile(constants.JOB_QUEUE_VERSION_FILE, |
399 | f1da30e6 | Michael Hanselmann | data="%s\n" % constants.JOB_QUEUE_VERSION)
|
400 | c4beba1c | Iustin Pop | if self._ReadSerial() is None: |
401 | c4beba1c | Iustin Pop | utils.WriteFile(constants.JOB_QUEUE_SERIAL_FILE, |
402 | c4beba1c | Iustin Pop | data="%s\n" % 0) |
403 | f1da30e6 | Michael Hanselmann | |
404 | c3f0a12f | Iustin Pop | def _NewSerialUnlocked(self, nodes): |
405 | f1da30e6 | Michael Hanselmann | """Generates a new job identifier.
|
406 | f1da30e6 | Michael Hanselmann |
|
407 | f1da30e6 | Michael Hanselmann | Job identifiers are unique during the lifetime of a cluster.
|
408 | f1da30e6 | Michael Hanselmann |
|
409 | f1da30e6 | Michael Hanselmann | Returns: A string representing the job identifier.
|
410 | f1da30e6 | Michael Hanselmann |
|
411 | f1da30e6 | Michael Hanselmann | """
|
412 | f1da30e6 | Michael Hanselmann | assert self.lock_fd, "Queue should be open" |
413 | f1da30e6 | Michael Hanselmann | |
414 | f1da30e6 | Michael Hanselmann | # New number
|
415 | f1da30e6 | Michael Hanselmann | serial = self._last_serial + 1 |
416 | f1da30e6 | Michael Hanselmann | |
417 | f1da30e6 | Michael Hanselmann | # Write to file
|
418 | f1da30e6 | Michael Hanselmann | utils.WriteFile(constants.JOB_QUEUE_SERIAL_FILE, |
419 | f1da30e6 | Michael Hanselmann | data="%s\n" % serial)
|
420 | f1da30e6 | Michael Hanselmann | |
421 | f1da30e6 | Michael Hanselmann | # Keep it only if we were able to write the file
|
422 | f1da30e6 | Michael Hanselmann | self._last_serial = serial
|
423 | f1da30e6 | Michael Hanselmann | |
424 | c3f0a12f | Iustin Pop | # Distribute the serial to the other nodes
|
425 | c3f0a12f | Iustin Pop | try:
|
426 | c3f0a12f | Iustin Pop | nodes.remove(self._my_hostname)
|
427 | c3f0a12f | Iustin Pop | except ValueError: |
428 | c3f0a12f | Iustin Pop | pass
|
429 | c3f0a12f | Iustin Pop | |
430 | c3f0a12f | Iustin Pop | result = rpc.call_upload_file(nodes, constants.JOB_QUEUE_SERIAL_FILE) |
431 | c3f0a12f | Iustin Pop | for node in nodes: |
432 | c3f0a12f | Iustin Pop | if not result[node]: |
433 | c3f0a12f | Iustin Pop | logging.error("copy of job queue file to node %s failed", node)
|
434 | c3f0a12f | Iustin Pop | |
435 | ce594241 | Michael Hanselmann | return self.FormatJobID(serial) |
436 | f1da30e6 | Michael Hanselmann | |
437 | f1da30e6 | Michael Hanselmann | def _GetJobPath(self, job_id): |
438 | f1da30e6 | Michael Hanselmann | return os.path.join(constants.QUEUE_DIR, "job-%s" % job_id) |
439 | f1da30e6 | Michael Hanselmann | |
440 | 0cb94105 | Michael Hanselmann | def _GetArchivedJobPath(self, job_id): |
441 | 0cb94105 | Michael Hanselmann | return os.path.join(constants.JOB_QUEUE_ARCHIVE_DIR, "job-%s" % job_id) |
442 | 0cb94105 | Michael Hanselmann | |
443 | fae737ac | Michael Hanselmann | def _ExtractJobID(self, name): |
444 | fae737ac | Michael Hanselmann | m = self._RE_JOB_FILE.match(name)
|
445 | fae737ac | Michael Hanselmann | if m:
|
446 | fae737ac | Michael Hanselmann | return m.group(1) |
447 | fae737ac | Michael Hanselmann | else:
|
448 | fae737ac | Michael Hanselmann | return None |
449 | fae737ac | Michael Hanselmann | |
450 | 911a495b | Iustin Pop | def _GetJobIDsUnlocked(self, archived=False): |
451 | 911a495b | Iustin Pop | """Return all known job IDs.
|
452 | 911a495b | Iustin Pop |
|
453 | 911a495b | Iustin Pop | If the parameter archived is True, archived jobs IDs will be
|
454 | 911a495b | Iustin Pop | included. Currently this argument is unused.
|
455 | 911a495b | Iustin Pop |
|
456 | ac0930b9 | Iustin Pop | The method only looks at disk because it's a requirement that all
|
457 | ac0930b9 | Iustin Pop | jobs are present on disk (so in the _memcache we don't have any
|
458 | ac0930b9 | Iustin Pop | extra IDs).
|
459 | ac0930b9 | Iustin Pop |
|
460 | 911a495b | Iustin Pop | """
|
461 | fae737ac | Michael Hanselmann | jlist = [self._ExtractJobID(name) for name in self._ListJobFiles()] |
462 | f0d874fe | Iustin Pop | jlist.sort() |
463 | f0d874fe | Iustin Pop | return jlist
|
464 | 911a495b | Iustin Pop | |
465 | f1da30e6 | Michael Hanselmann | def _ListJobFiles(self): |
466 | f1da30e6 | Michael Hanselmann | assert self.lock_fd, "Queue should be open" |
467 | f1da30e6 | Michael Hanselmann | |
468 | f1da30e6 | Michael Hanselmann | return [name for name in utils.ListVisibleFiles(constants.QUEUE_DIR) |
469 | f1da30e6 | Michael Hanselmann | if self._RE_JOB_FILE.match(name)] |
470 | f1da30e6 | Michael Hanselmann | |
471 | 911a495b | Iustin Pop | def _LoadJobUnlocked(self, job_id): |
472 | f1da30e6 | Michael Hanselmann | assert self.lock_fd, "Queue should be open" |
473 | f1da30e6 | Michael Hanselmann | |
474 | ac0930b9 | Iustin Pop | if job_id in self._memcache: |
475 | 205d71fd | Michael Hanselmann | logging.debug("Found job %s in memcache", job_id)
|
476 | ac0930b9 | Iustin Pop | return self._memcache[job_id] |
477 | ac0930b9 | Iustin Pop | |
478 | 911a495b | Iustin Pop | filepath = self._GetJobPath(job_id)
|
479 | f1da30e6 | Michael Hanselmann | logging.debug("Loading job from %s", filepath)
|
480 | f1da30e6 | Michael Hanselmann | try:
|
481 | f1da30e6 | Michael Hanselmann | fd = open(filepath, "r") |
482 | f1da30e6 | Michael Hanselmann | except IOError, err: |
483 | f1da30e6 | Michael Hanselmann | if err.errno in (errno.ENOENT, ): |
484 | f1da30e6 | Michael Hanselmann | return None |
485 | f1da30e6 | Michael Hanselmann | raise
|
486 | f1da30e6 | Michael Hanselmann | try:
|
487 | f1da30e6 | Michael Hanselmann | data = serializer.LoadJson(fd.read()) |
488 | f1da30e6 | Michael Hanselmann | finally:
|
489 | f1da30e6 | Michael Hanselmann | fd.close() |
490 | f1da30e6 | Michael Hanselmann | |
491 | ac0930b9 | Iustin Pop | job = _QueuedJob.Restore(self, data)
|
492 | ac0930b9 | Iustin Pop | self._memcache[job_id] = job
|
493 | 205d71fd | Michael Hanselmann | logging.debug("Added job %s to the cache", job_id)
|
494 | ac0930b9 | Iustin Pop | return job
|
495 | f1da30e6 | Michael Hanselmann | |
496 | f1da30e6 | Michael Hanselmann | def _GetJobsUnlocked(self, job_ids): |
497 | 911a495b | Iustin Pop | if not job_ids: |
498 | 911a495b | Iustin Pop | job_ids = self._GetJobIDsUnlocked()
|
499 | f1da30e6 | Michael Hanselmann | |
500 | 911a495b | Iustin Pop | return [self._LoadJobUnlocked(job_id) for job_id in job_ids] |
501 | f1da30e6 | Michael Hanselmann | |
502 | f1da30e6 | Michael Hanselmann | @utils.LockedMethod
|
503 | f1da30e6 | Michael Hanselmann | def GetJobs(self, job_ids): |
504 | f1da30e6 | Michael Hanselmann | return self._GetJobsUnlocked(job_ids) |
505 | f1da30e6 | Michael Hanselmann | |
506 | f1da30e6 | Michael Hanselmann | @utils.LockedMethod
|
507 | c3f0a12f | Iustin Pop | def AddJob(self, ops, nodes): |
508 | c3f0a12f | Iustin Pop | """Create and store on disk a new job.
|
509 | c3f0a12f | Iustin Pop |
|
510 | c3f0a12f | Iustin Pop | @type ops: list
|
511 | 205d71fd | Michael Hanselmann | @param ops: The list of OpCodes that will become the new job.
|
512 | c3f0a12f | Iustin Pop | @type nodes: list
|
513 | c3f0a12f | Iustin Pop | @param nodes: The list of nodes to which the new job serial will be
|
514 | c3f0a12f | Iustin Pop | distributed.
|
515 | c3f0a12f | Iustin Pop |
|
516 | c3f0a12f | Iustin Pop | """
|
517 | f1da30e6 | Michael Hanselmann | assert self.lock_fd, "Queue should be open" |
518 | f1da30e6 | Michael Hanselmann | |
519 | f1da30e6 | Michael Hanselmann | # Get job identifier
|
520 | c3f0a12f | Iustin Pop | job_id = self._NewSerialUnlocked(nodes)
|
521 | f1da30e6 | Michael Hanselmann | job = _QueuedJob(self, job_id, ops)
|
522 | f1da30e6 | Michael Hanselmann | |
523 | f1da30e6 | Michael Hanselmann | # Write to disk
|
524 | f1da30e6 | Michael Hanselmann | self._UpdateJobUnlocked(job)
|
525 | f1da30e6 | Michael Hanselmann | |
526 | 205d71fd | Michael Hanselmann | logging.debug("Added new job %s to the cache", job_id)
|
527 | ac0930b9 | Iustin Pop | self._memcache[job_id] = job
|
528 | ac0930b9 | Iustin Pop | |
529 | f1da30e6 | Michael Hanselmann | return job
|
530 | f1da30e6 | Michael Hanselmann | |
531 | f1da30e6 | Michael Hanselmann | def _UpdateJobUnlocked(self, job): |
532 | f1da30e6 | Michael Hanselmann | assert self.lock_fd, "Queue should be open" |
533 | f1da30e6 | Michael Hanselmann | |
534 | f1da30e6 | Michael Hanselmann | filename = self._GetJobPath(job.id)
|
535 | f1da30e6 | Michael Hanselmann | logging.debug("Writing job %s to %s", job.id, filename)
|
536 | f1da30e6 | Michael Hanselmann | utils.WriteFile(filename, |
537 | f1da30e6 | Michael Hanselmann | data=serializer.DumpJson(job.Serialize(), indent=False))
|
538 | 57f8615f | Michael Hanselmann | self._CleanCacheUnlocked([job.id])
|
539 | ac0930b9 | Iustin Pop | |
540 | 57f8615f | Michael Hanselmann | def _CleanCacheUnlocked(self, exclude): |
541 | ac0930b9 | Iustin Pop | """Clean the memory cache.
|
542 | ac0930b9 | Iustin Pop |
|
543 | ac0930b9 | Iustin Pop | The exceptions argument contains job IDs that should not be
|
544 | ac0930b9 | Iustin Pop | cleaned.
|
545 | ac0930b9 | Iustin Pop |
|
546 | ac0930b9 | Iustin Pop | """
|
547 | 57f8615f | Michael Hanselmann | assert isinstance(exclude, list) |
548 | ac0930b9 | Iustin Pop | for job in self._memcache.values(): |
549 | 57f8615f | Michael Hanselmann | if job.id in exclude: |
550 | ac0930b9 | Iustin Pop | continue
|
551 | ac0930b9 | Iustin Pop | if job.GetStatus() not in (constants.JOB_STATUS_QUEUED, |
552 | ac0930b9 | Iustin Pop | constants.JOB_STATUS_RUNNING): |
553 | 205d71fd | Michael Hanselmann | logging.debug("Cleaning job %s from the cache", job.id)
|
554 | ac0930b9 | Iustin Pop | try:
|
555 | ac0930b9 | Iustin Pop | del self._memcache[job.id] |
556 | ac0930b9 | Iustin Pop | except KeyError: |
557 | ac0930b9 | Iustin Pop | pass
|
558 | f1da30e6 | Michael Hanselmann | |
559 | f1da30e6 | Michael Hanselmann | @utils.LockedMethod
|
560 | f1da30e6 | Michael Hanselmann | def UpdateJob(self, job): |
561 | f1da30e6 | Michael Hanselmann | return self._UpdateJobUnlocked(job) |
562 | f1da30e6 | Michael Hanselmann | |
563 | 188c5e0a | Michael Hanselmann | # TODO: Figure out locking
|
564 | 188c5e0a | Michael Hanselmann | #@utils.LockedMethod
|
565 | 188c5e0a | Michael Hanselmann | def CancelJob(self, job_id): |
566 | 188c5e0a | Michael Hanselmann | """Cancels a job.
|
567 | 188c5e0a | Michael Hanselmann |
|
568 | 188c5e0a | Michael Hanselmann | @type job_id: string
|
569 | 188c5e0a | Michael Hanselmann | @param job_id: Job ID of job to be cancelled.
|
570 | 188c5e0a | Michael Hanselmann |
|
571 | 188c5e0a | Michael Hanselmann | """
|
572 | 188c5e0a | Michael Hanselmann | logging.debug("Cancelling job %s", job_id)
|
573 | 188c5e0a | Michael Hanselmann | |
574 | 188c5e0a | Michael Hanselmann | self._lock.acquire()
|
575 | 188c5e0a | Michael Hanselmann | try:
|
576 | 188c5e0a | Michael Hanselmann | job = self._LoadJobUnlocked(job_id)
|
577 | 188c5e0a | Michael Hanselmann | finally:
|
578 | 188c5e0a | Michael Hanselmann | self._lock.release()
|
579 | 188c5e0a | Michael Hanselmann | if not job: |
580 | 188c5e0a | Michael Hanselmann | logging.debug("Job %s not found", job_id)
|
581 | 188c5e0a | Michael Hanselmann | return
|
582 | 188c5e0a | Michael Hanselmann | |
583 | 188c5e0a | Michael Hanselmann | if job.GetStatus() not in (constants.JOB_STATUS_QUEUED,): |
584 | 188c5e0a | Michael Hanselmann | logging.debug("Job %s is no longer in the queue", job.id)
|
585 | 188c5e0a | Michael Hanselmann | return
|
586 | 188c5e0a | Michael Hanselmann | |
587 | 188c5e0a | Michael Hanselmann | job.SetCanceled("Job cancelled by request")
|
588 | 188c5e0a | Michael Hanselmann | |
589 | c609f802 | Michael Hanselmann | @utils.LockedMethod
|
590 | f1da30e6 | Michael Hanselmann | def ArchiveJob(self, job_id): |
591 | c609f802 | Michael Hanselmann | """Archives a job.
|
592 | c609f802 | Michael Hanselmann |
|
593 | c609f802 | Michael Hanselmann | @type job_id: string
|
594 | c609f802 | Michael Hanselmann | @param job_id: Job ID of job to be archived.
|
595 | c609f802 | Michael Hanselmann |
|
596 | c609f802 | Michael Hanselmann | """
|
597 | c609f802 | Michael Hanselmann | logging.debug("Archiving job %s", job_id)
|
598 | c609f802 | Michael Hanselmann | |
599 | c609f802 | Michael Hanselmann | job = self._LoadJobUnlocked(job_id)
|
600 | c609f802 | Michael Hanselmann | if not job: |
601 | c609f802 | Michael Hanselmann | logging.debug("Job %s not found", job_id)
|
602 | c609f802 | Michael Hanselmann | return
|
603 | c609f802 | Michael Hanselmann | |
604 | c609f802 | Michael Hanselmann | if not self._ShouldJobBeArchivedUnlocked(job): |
605 | c609f802 | Michael Hanselmann | return
|
606 | c609f802 | Michael Hanselmann | |
607 | c609f802 | Michael Hanselmann | try:
|
608 | c609f802 | Michael Hanselmann | old = self._GetJobPath(job.id)
|
609 | c609f802 | Michael Hanselmann | new = self._GetArchivedJobPath(job.id)
|
610 | c609f802 | Michael Hanselmann | |
611 | c609f802 | Michael Hanselmann | os.rename(old, new) |
612 | c609f802 | Michael Hanselmann | |
613 | c609f802 | Michael Hanselmann | logging.debug("Successfully archived job %s", job.id)
|
614 | c609f802 | Michael Hanselmann | finally:
|
615 | c609f802 | Michael Hanselmann | # Cleaning the cache because we don't know what os.rename actually did
|
616 | c609f802 | Michael Hanselmann | # and to be on the safe side.
|
617 | c609f802 | Michael Hanselmann | self._CleanCacheUnlocked([])
|
618 | f1da30e6 | Michael Hanselmann | |
619 | f1da30e6 | Michael Hanselmann | |
620 | e2715f69 | Michael Hanselmann | class JobQueue: |
621 | e2715f69 | Michael Hanselmann | """The job queue.
|
622 | e2715f69 | Michael Hanselmann |
|
623 | ce594241 | Michael Hanselmann | """
|
624 | e2715f69 | Michael Hanselmann | def __init__(self, context): |
625 | e2715f69 | Michael Hanselmann | self._lock = threading.Lock()
|
626 | ce594241 | Michael Hanselmann | self._jobs = DiskJobStorage("") |
627 | e2715f69 | Michael Hanselmann | self._wpool = _JobQueueWorkerPool(context)
|
628 | e2715f69 | Michael Hanselmann | |
629 | f1da30e6 | Michael Hanselmann | for job in self._jobs.GetJobs(None): |
630 | f1da30e6 | Michael Hanselmann | status = job.GetStatus() |
631 | f1da30e6 | Michael Hanselmann | if status in (constants.JOB_STATUS_QUEUED, ): |
632 | f1da30e6 | Michael Hanselmann | self._wpool.AddTask(job)
|
633 | e2715f69 | Michael Hanselmann | |
634 | f1da30e6 | Michael Hanselmann | elif status in (constants.JOB_STATUS_RUNNING, ): |
635 | f1da30e6 | Michael Hanselmann | logging.warning("Unfinished job %s found: %s", job.id, job)
|
636 | f1da30e6 | Michael Hanselmann | job.SetUnclean("Unclean master daemon shutdown")
|
637 | e2715f69 | Michael Hanselmann | |
638 | f1da30e6 | Michael Hanselmann | @utils.LockedMethod
|
639 | c3f0a12f | Iustin Pop | def SubmitJob(self, ops, nodes): |
640 | e2715f69 | Michael Hanselmann | """Add a new job to the queue.
|
641 | e2715f69 | Michael Hanselmann |
|
642 | e2715f69 | Michael Hanselmann | This enters the job into our job queue and also puts it on the new
|
643 | e2715f69 | Michael Hanselmann | queue, in order for it to be picked up by the queue processors.
|
644 | e2715f69 | Michael Hanselmann |
|
645 | c3f0a12f | Iustin Pop | @type ops: list
|
646 | c3f0a12f | Iustin Pop | @param ops: the sequence of opcodes that will become the new job
|
647 | c3f0a12f | Iustin Pop | @type nodes: list
|
648 | c3f0a12f | Iustin Pop | @param nodes: the list of nodes to which the queue should be
|
649 | c3f0a12f | Iustin Pop | distributed
|
650 | e2715f69 | Michael Hanselmann |
|
651 | e2715f69 | Michael Hanselmann | """
|
652 | c3f0a12f | Iustin Pop | job = self._jobs.AddJob(ops, nodes)
|
653 | e2715f69 | Michael Hanselmann | |
654 | e2715f69 | Michael Hanselmann | # Add to worker pool
|
655 | e2715f69 | Michael Hanselmann | self._wpool.AddTask(job)
|
656 | e2715f69 | Michael Hanselmann | |
657 | f1da30e6 | Michael Hanselmann | return job.id
|
658 | e2715f69 | Michael Hanselmann | |
659 | e2715f69 | Michael Hanselmann | def ArchiveJob(self, job_id): |
660 | c609f802 | Michael Hanselmann | self._jobs.ArchiveJob(job_id)
|
661 | e2715f69 | Michael Hanselmann | |
662 | 188c5e0a | Michael Hanselmann | @utils.LockedMethod
|
663 | e2715f69 | Michael Hanselmann | def CancelJob(self, job_id): |
664 | 188c5e0a | Michael Hanselmann | self._jobs.CancelJob(job_id)
|
665 | e2715f69 | Michael Hanselmann | |
666 | e2715f69 | Michael Hanselmann | def _GetJobInfo(self, job, fields): |
667 | e2715f69 | Michael Hanselmann | row = [] |
668 | e2715f69 | Michael Hanselmann | for fname in fields: |
669 | e2715f69 | Michael Hanselmann | if fname == "id": |
670 | e2715f69 | Michael Hanselmann | row.append(job.id) |
671 | e2715f69 | Michael Hanselmann | elif fname == "status": |
672 | e2715f69 | Michael Hanselmann | row.append(job.GetStatus()) |
673 | af30b2fd | Michael Hanselmann | elif fname == "ops": |
674 | af30b2fd | Michael Hanselmann | row.append([op.GetInput().__getstate__() for op in job._ops]) |
675 | af30b2fd | Michael Hanselmann | elif fname == "opresult": |
676 | 307149a8 | Iustin Pop | row.append([op.GetResult() for op in job._ops]) |
677 | af30b2fd | Michael Hanselmann | elif fname == "opstatus": |
678 | af30b2fd | Michael Hanselmann | row.append([op.GetStatus() for op in job._ops]) |
679 | f1048938 | Iustin Pop | elif fname == "ticker": |
680 | f1048938 | Iustin Pop | ji = job.GetRunOpIndex() |
681 | f1048938 | Iustin Pop | if ji < 0: |
682 | f1048938 | Iustin Pop | lmsg = None
|
683 | f1048938 | Iustin Pop | else:
|
684 | f1048938 | Iustin Pop | lmsg = job._ops[ji].RetrieveLog(-1)
|
685 | f1048938 | Iustin Pop | # message might be empty here
|
686 | f1048938 | Iustin Pop | if lmsg:
|
687 | f1048938 | Iustin Pop | lmsg = lmsg[0]
|
688 | f1048938 | Iustin Pop | else:
|
689 | f1048938 | Iustin Pop | lmsg = None
|
690 | f1048938 | Iustin Pop | row.append(lmsg) |
691 | e2715f69 | Michael Hanselmann | else:
|
692 | e2715f69 | Michael Hanselmann | raise errors.OpExecError("Invalid job query field '%s'" % fname) |
693 | e2715f69 | Michael Hanselmann | return row
|
694 | e2715f69 | Michael Hanselmann | |
695 | e2715f69 | Michael Hanselmann | def QueryJobs(self, job_ids, fields): |
696 | e2715f69 | Michael Hanselmann | """Returns a list of jobs in queue.
|
697 | e2715f69 | Michael Hanselmann |
|
698 | e2715f69 | Michael Hanselmann | Args:
|
699 | e2715f69 | Michael Hanselmann | - job_ids: Sequence of job identifiers or None for all
|
700 | e2715f69 | Michael Hanselmann | - fields: Names of fields to return
|
701 | e2715f69 | Michael Hanselmann |
|
702 | e2715f69 | Michael Hanselmann | """
|
703 | e2715f69 | Michael Hanselmann | self._lock.acquire()
|
704 | e2715f69 | Michael Hanselmann | try:
|
705 | e2715f69 | Michael Hanselmann | jobs = [] |
706 | e2715f69 | Michael Hanselmann | |
707 | f1da30e6 | Michael Hanselmann | for job in self._jobs.GetJobs(job_ids): |
708 | e2715f69 | Michael Hanselmann | if job is None: |
709 | e2715f69 | Michael Hanselmann | jobs.append(None)
|
710 | e2715f69 | Michael Hanselmann | else:
|
711 | e2715f69 | Michael Hanselmann | jobs.append(self._GetJobInfo(job, fields))
|
712 | e2715f69 | Michael Hanselmann | |
713 | e2715f69 | Michael Hanselmann | return jobs
|
714 | e2715f69 | Michael Hanselmann | finally:
|
715 | e2715f69 | Michael Hanselmann | self._lock.release()
|
716 | e2715f69 | Michael Hanselmann | |
717 | f1da30e6 | Michael Hanselmann | @utils.LockedMethod
|
718 | e2715f69 | Michael Hanselmann | def Shutdown(self): |
719 | e2715f69 | Michael Hanselmann | """Stops the job queue.
|
720 | e2715f69 | Michael Hanselmann |
|
721 | e2715f69 | Michael Hanselmann | """
|
722 | e2715f69 | Michael Hanselmann | self._wpool.TerminateWorkers()
|
723 | f1da30e6 | Michael Hanselmann | self._jobs.Close() |