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