root / lib / jqueue.py @ 7996a135
History | View | Annotate | Download (6.6 kB)
1 |
#
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2006, 2007 Google Inc.
|
5 |
#
|
6 |
# This program is free software; you can redistribute it and/or modify
|
7 |
# it under the terms of the GNU General Public License as published by
|
8 |
# the Free Software Foundation; either version 2 of the License, or
|
9 |
# (at your option) any later version.
|
10 |
#
|
11 |
# This program is distributed in the hope that it will be useful, but
|
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
# General Public License for more details.
|
15 |
#
|
16 |
# You should have received a copy of the GNU General Public License
|
17 |
# along with this program; if not, write to the Free Software
|
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
# 02110-1301, USA.
|
20 |
|
21 |
|
22 |
"""Module implementing the job queue handling."""
|
23 |
|
24 |
import logging |
25 |
import threading |
26 |
|
27 |
from ganeti import constants |
28 |
from ganeti import workerpool |
29 |
from ganeti import errors |
30 |
from ganeti import mcpu |
31 |
from ganeti import utils |
32 |
|
33 |
|
34 |
JOBQUEUE_THREADS = 5
|
35 |
|
36 |
|
37 |
class _QueuedOpCode(object): |
38 |
"""Encasulates an opcode object.
|
39 |
|
40 |
Access must be synchronized by using an external lock.
|
41 |
|
42 |
"""
|
43 |
def __init__(self, op): |
44 |
self.input = op
|
45 |
self.status = constants.OP_STATUS_QUEUED
|
46 |
self.result = None |
47 |
|
48 |
|
49 |
class _QueuedJob(object): |
50 |
"""In-memory job representation.
|
51 |
|
52 |
This is what we use to track the user-submitted jobs.
|
53 |
|
54 |
"""
|
55 |
def __init__(self, ops, job_id): |
56 |
if not ops: |
57 |
# TODO
|
58 |
raise Exception("No opcodes") |
59 |
|
60 |
self.id = job_id
|
61 |
self._lock = threading.Lock()
|
62 |
|
63 |
# _ops should not be modified again because we don't acquire the lock
|
64 |
# to use it.
|
65 |
self._ops = [_QueuedOpCode(op) for op in ops] |
66 |
|
67 |
def _GetStatusUnlocked(self): |
68 |
status = constants.JOB_STATUS_QUEUED |
69 |
|
70 |
all_success = True
|
71 |
for op in self._ops: |
72 |
if op.status == constants.OP_STATUS_SUCCESS:
|
73 |
continue
|
74 |
|
75 |
all_success = False
|
76 |
|
77 |
if op.status == constants.OP_STATUS_QUEUED:
|
78 |
pass
|
79 |
elif op.status == constants.OP_STATUS_ERROR:
|
80 |
status = constants.JOB_STATUS_ERROR |
81 |
elif op.status == constants.OP_STATUS_RUNNING:
|
82 |
status = constants.JOB_STATUS_RUNNING |
83 |
|
84 |
if all_success:
|
85 |
status = constants.JOB_STATUS_SUCCESS |
86 |
|
87 |
return status
|
88 |
|
89 |
def GetStatus(self): |
90 |
self._lock.acquire()
|
91 |
try:
|
92 |
return self._GetStatusUnlocked() |
93 |
finally:
|
94 |
self._lock.release()
|
95 |
|
96 |
def Run(self, proc): |
97 |
"""Job executor.
|
98 |
|
99 |
This functions processes a this job in the context of given processor
|
100 |
instance.
|
101 |
|
102 |
Args:
|
103 |
- proc: Ganeti Processor to run the job with
|
104 |
|
105 |
"""
|
106 |
try:
|
107 |
count = len(self._ops) |
108 |
for idx, op in enumerate(self._ops): |
109 |
try:
|
110 |
self._lock.acquire()
|
111 |
try:
|
112 |
logging.debug("Op %s/%s: Starting %s", idx + 1, count, op) |
113 |
op.status = constants.OP_STATUS_RUNNING |
114 |
finally:
|
115 |
self._lock.release()
|
116 |
|
117 |
result = proc.ExecOpCode(op.input) |
118 |
|
119 |
self._lock.acquire()
|
120 |
try:
|
121 |
logging.debug("Op %s/%s: Successfully finished %s",
|
122 |
idx + 1, count, op)
|
123 |
op.status = constants.OP_STATUS_SUCCESS |
124 |
op.result = result |
125 |
finally:
|
126 |
self._lock.release()
|
127 |
except Exception, err: |
128 |
self._lock.acquire()
|
129 |
try:
|
130 |
logging.debug("Op %s/%s: Error in %s", idx + 1, count, op) |
131 |
op.status = constants.OP_STATUS_ERROR |
132 |
op.result = str(err)
|
133 |
finally:
|
134 |
self._lock.release()
|
135 |
raise
|
136 |
|
137 |
except errors.GenericError, err:
|
138 |
logging.error("ganeti exception %s", exc_info=err)
|
139 |
except Exception, err: |
140 |
logging.error("unhandled exception %s", exc_info=err)
|
141 |
except:
|
142 |
logging.error("unhandled unknown exception %s", exc_info=err)
|
143 |
|
144 |
|
145 |
class _JobQueueWorker(workerpool.BaseWorker): |
146 |
def RunTask(self, job): |
147 |
logging.debug("Worker %s processing job %s",
|
148 |
self.worker_id, job.id)
|
149 |
# TODO: feedback function
|
150 |
proc = mcpu.Processor(self.pool.context, feedback=lambda x: None) |
151 |
try:
|
152 |
job.Run(proc) |
153 |
finally:
|
154 |
logging.debug("Worker %s finished job %s, status = %s",
|
155 |
self.worker_id, job.id, job.GetStatus())
|
156 |
|
157 |
|
158 |
class _JobQueueWorkerPool(workerpool.WorkerPool): |
159 |
def __init__(self, context): |
160 |
super(_JobQueueWorkerPool, self).__init__(JOBQUEUE_THREADS, |
161 |
_JobQueueWorker) |
162 |
self.context = context
|
163 |
|
164 |
|
165 |
class JobQueue: |
166 |
"""The job queue.
|
167 |
|
168 |
"""
|
169 |
def __init__(self, context): |
170 |
self._lock = threading.Lock()
|
171 |
self._last_job_id = 0 |
172 |
self._jobs = {}
|
173 |
self._wpool = _JobQueueWorkerPool(context)
|
174 |
|
175 |
def _NewJobIdUnlocked(self): |
176 |
"""Generates a new job identifier.
|
177 |
|
178 |
Returns: A string representing the job identifier.
|
179 |
|
180 |
"""
|
181 |
self._last_job_id += 1 |
182 |
return str(self._last_job_id) |
183 |
|
184 |
def SubmitJob(self, ops): |
185 |
"""Add a new job to the queue.
|
186 |
|
187 |
This enters the job into our job queue and also puts it on the new
|
188 |
queue, in order for it to be picked up by the queue processors.
|
189 |
|
190 |
Args:
|
191 |
- ops: Sequence of opcodes
|
192 |
|
193 |
"""
|
194 |
# Get job identifier
|
195 |
self._lock.acquire()
|
196 |
try:
|
197 |
job_id = self._NewJobIdUnlocked()
|
198 |
finally:
|
199 |
self._lock.release()
|
200 |
|
201 |
job = _QueuedJob(ops, job_id) |
202 |
|
203 |
# Add it to our internal queue
|
204 |
self._lock.acquire()
|
205 |
try:
|
206 |
self._jobs[job_id] = job
|
207 |
finally:
|
208 |
self._lock.release()
|
209 |
|
210 |
# Add to worker pool
|
211 |
self._wpool.AddTask(job)
|
212 |
|
213 |
return job_id
|
214 |
|
215 |
def ArchiveJob(self, job_id): |
216 |
raise NotImplementedError() |
217 |
|
218 |
def CancelJob(self, job_id): |
219 |
raise NotImplementedError() |
220 |
|
221 |
def _GetJobInfo(self, job, fields): |
222 |
row = [] |
223 |
for fname in fields: |
224 |
if fname == "id": |
225 |
row.append(job.id) |
226 |
elif fname == "status": |
227 |
row.append(job.GetStatus()) |
228 |
elif fname == "result": |
229 |
# TODO
|
230 |
row.append(map(lambda op: op.result, job._ops)) |
231 |
else:
|
232 |
raise errors.OpExecError("Invalid job query field '%s'" % fname) |
233 |
return row
|
234 |
|
235 |
def QueryJobs(self, job_ids, fields): |
236 |
"""Returns a list of jobs in queue.
|
237 |
|
238 |
Args:
|
239 |
- job_ids: Sequence of job identifiers or None for all
|
240 |
- fields: Names of fields to return
|
241 |
|
242 |
"""
|
243 |
self._lock.acquire()
|
244 |
try:
|
245 |
if not job_ids: |
246 |
job_ids = self._jobs.keys()
|
247 |
|
248 |
# TODO: define sort order?
|
249 |
job_ids.sort() |
250 |
|
251 |
jobs = [] |
252 |
|
253 |
for job_id in job_ids: |
254 |
job = self._jobs.get(job_id, None) |
255 |
if job is None: |
256 |
jobs.append(None)
|
257 |
else:
|
258 |
jobs.append(self._GetJobInfo(job, fields))
|
259 |
|
260 |
return jobs
|
261 |
finally:
|
262 |
self._lock.release()
|
263 |
|
264 |
def Shutdown(self): |
265 |
"""Stops the job queue.
|
266 |
|
267 |
"""
|
268 |
self._wpool.TerminateWorkers()
|