Statistics
| Branch: | Tag: | Revision:

root / lib / jqueue.py @ 7578ab0a

History | View | Annotate | Download (58.1 kB)

1 498ae1cc Iustin Pop
#
2 498ae1cc Iustin Pop
#
3 498ae1cc Iustin Pop
4 7f93570a Iustin Pop
# Copyright (C) 2006, 2007, 2008, 2009, 2010 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 e2715f69 Michael Hanselmann
import logging
33 f1da30e6 Michael Hanselmann
import errno
34 f1da30e6 Michael Hanselmann
import re
35 f1048938 Iustin Pop
import time
36 5685c1a5 Michael Hanselmann
import weakref
37 498ae1cc Iustin Pop
38 6c2549d6 Guido Trotter
try:
39 6c2549d6 Guido Trotter
  # pylint: disable-msg=E0611
40 6c2549d6 Guido Trotter
  from pyinotify import pyinotify
41 6c2549d6 Guido Trotter
except ImportError:
42 6c2549d6 Guido Trotter
  import pyinotify
43 6c2549d6 Guido Trotter
44 6c2549d6 Guido Trotter
from ganeti import asyncnotifier
45 e2715f69 Michael Hanselmann
from ganeti import constants
46 f1da30e6 Michael Hanselmann
from ganeti import serializer
47 e2715f69 Michael Hanselmann
from ganeti import workerpool
48 99bd4f0a Guido Trotter
from ganeti import locking
49 f1da30e6 Michael Hanselmann
from ganeti import opcodes
50 7a1ecaed Iustin Pop
from ganeti import errors
51 e2715f69 Michael Hanselmann
from ganeti import mcpu
52 7996a135 Iustin Pop
from ganeti import utils
53 04ab05ce Michael Hanselmann
from ganeti import jstore
54 c3f0a12f Iustin Pop
from ganeti import rpc
55 82b22e19 René Nussbaumer
from ganeti import runtime
56 a744b676 Manuel Franceschini
from ganeti import netutils
57 989a8bee Michael Hanselmann
from ganeti import compat
58 e2715f69 Michael Hanselmann
59 fbf0262f Michael Hanselmann
60 1daae384 Iustin Pop
JOBQUEUE_THREADS = 25
61 58b22b6e Michael Hanselmann
JOBS_PER_ARCHIVE_DIRECTORY = 10000
62 e2715f69 Michael Hanselmann
63 ebb80afa Guido Trotter
# member lock names to be passed to @ssynchronized decorator
64 ebb80afa Guido Trotter
_LOCK = "_lock"
65 ebb80afa Guido Trotter
_QUEUE = "_queue"
66 99bd4f0a Guido Trotter
67 498ae1cc Iustin Pop
68 9728ae5d Iustin Pop
class CancelJob(Exception):
69 fbf0262f Michael Hanselmann
  """Special exception to cancel a job.
70 fbf0262f Michael Hanselmann

71 fbf0262f Michael Hanselmann
  """
72 fbf0262f Michael Hanselmann
73 fbf0262f Michael Hanselmann
74 70552c46 Michael Hanselmann
def TimeStampNow():
75 ea03467c Iustin Pop
  """Returns the current timestamp.
76 ea03467c Iustin Pop

77 ea03467c Iustin Pop
  @rtype: tuple
78 ea03467c Iustin Pop
  @return: the current time in the (seconds, microseconds) format
79 ea03467c Iustin Pop

80 ea03467c Iustin Pop
  """
81 70552c46 Michael Hanselmann
  return utils.SplitTime(time.time())
82 70552c46 Michael Hanselmann
83 70552c46 Michael Hanselmann
84 e2715f69 Michael Hanselmann
class _QueuedOpCode(object):
85 5bbd3f7f Michael Hanselmann
  """Encapsulates an opcode object.
86 e2715f69 Michael Hanselmann

87 ea03467c Iustin Pop
  @ivar log: holds the execution log and consists of tuples
88 ea03467c Iustin Pop
  of the form C{(log_serial, timestamp, level, message)}
89 ea03467c Iustin Pop
  @ivar input: the OpCode we encapsulate
90 ea03467c Iustin Pop
  @ivar status: the current status
91 ea03467c Iustin Pop
  @ivar result: the result of the LU execution
92 ea03467c Iustin Pop
  @ivar start_timestamp: timestamp for the start of the execution
93 b9b5abcb Iustin Pop
  @ivar exec_timestamp: timestamp for the actual LU Exec() function invocation
94 ea03467c Iustin Pop
  @ivar stop_timestamp: timestamp for the end of the execution
95 f1048938 Iustin Pop

96 e2715f69 Michael Hanselmann
  """
97 8f5c488d Michael Hanselmann
  __slots__ = ["input", "status", "result", "log", "priority",
98 b9b5abcb Iustin Pop
               "start_timestamp", "exec_timestamp", "end_timestamp",
99 66d895a8 Iustin Pop
               "__weakref__"]
100 66d895a8 Iustin Pop
101 85f03e0d Michael Hanselmann
  def __init__(self, op):
102 ea03467c Iustin Pop
    """Constructor for the _QuededOpCode.
103 ea03467c Iustin Pop

104 ea03467c Iustin Pop
    @type op: L{opcodes.OpCode}
105 ea03467c Iustin Pop
    @param op: the opcode we encapsulate
106 ea03467c Iustin Pop

107 ea03467c Iustin Pop
    """
108 85f03e0d Michael Hanselmann
    self.input = op
109 85f03e0d Michael Hanselmann
    self.status = constants.OP_STATUS_QUEUED
110 85f03e0d Michael Hanselmann
    self.result = None
111 85f03e0d Michael Hanselmann
    self.log = []
112 70552c46 Michael Hanselmann
    self.start_timestamp = None
113 b9b5abcb Iustin Pop
    self.exec_timestamp = None
114 70552c46 Michael Hanselmann
    self.end_timestamp = None
115 f1da30e6 Michael Hanselmann
116 8f5c488d Michael Hanselmann
    # Get initial priority (it might change during the lifetime of this opcode)
117 8f5c488d Michael Hanselmann
    self.priority = getattr(op, "priority", constants.OP_PRIO_DEFAULT)
118 8f5c488d Michael Hanselmann
119 f1da30e6 Michael Hanselmann
  @classmethod
120 f1da30e6 Michael Hanselmann
  def Restore(cls, state):
121 ea03467c Iustin Pop
    """Restore the _QueuedOpCode from the serialized form.
122 ea03467c Iustin Pop

123 ea03467c Iustin Pop
    @type state: dict
124 ea03467c Iustin Pop
    @param state: the serialized state
125 ea03467c Iustin Pop
    @rtype: _QueuedOpCode
126 ea03467c Iustin Pop
    @return: a new _QueuedOpCode instance
127 ea03467c Iustin Pop

128 ea03467c Iustin Pop
    """
129 85f03e0d Michael Hanselmann
    obj = _QueuedOpCode.__new__(cls)
130 85f03e0d Michael Hanselmann
    obj.input = opcodes.OpCode.LoadOpCode(state["input"])
131 85f03e0d Michael Hanselmann
    obj.status = state["status"]
132 85f03e0d Michael Hanselmann
    obj.result = state["result"]
133 85f03e0d Michael Hanselmann
    obj.log = state["log"]
134 70552c46 Michael Hanselmann
    obj.start_timestamp = state.get("start_timestamp", None)
135 b9b5abcb Iustin Pop
    obj.exec_timestamp = state.get("exec_timestamp", None)
136 70552c46 Michael Hanselmann
    obj.end_timestamp = state.get("end_timestamp", None)
137 8f5c488d Michael Hanselmann
    obj.priority = state.get("priority", constants.OP_PRIO_DEFAULT)
138 f1da30e6 Michael Hanselmann
    return obj
139 f1da30e6 Michael Hanselmann
140 f1da30e6 Michael Hanselmann
  def Serialize(self):
141 ea03467c Iustin Pop
    """Serializes this _QueuedOpCode.
142 ea03467c Iustin Pop

143 ea03467c Iustin Pop
    @rtype: dict
144 ea03467c Iustin Pop
    @return: the dictionary holding the serialized state
145 ea03467c Iustin Pop

146 ea03467c Iustin Pop
    """
147 6c5a7090 Michael Hanselmann
    return {
148 6c5a7090 Michael Hanselmann
      "input": self.input.__getstate__(),
149 6c5a7090 Michael Hanselmann
      "status": self.status,
150 6c5a7090 Michael Hanselmann
      "result": self.result,
151 6c5a7090 Michael Hanselmann
      "log": self.log,
152 70552c46 Michael Hanselmann
      "start_timestamp": self.start_timestamp,
153 b9b5abcb Iustin Pop
      "exec_timestamp": self.exec_timestamp,
154 70552c46 Michael Hanselmann
      "end_timestamp": self.end_timestamp,
155 8f5c488d Michael Hanselmann
      "priority": self.priority,
156 6c5a7090 Michael Hanselmann
      }
157 f1048938 Iustin Pop
158 e2715f69 Michael Hanselmann
159 e2715f69 Michael Hanselmann
class _QueuedJob(object):
160 e2715f69 Michael Hanselmann
  """In-memory job representation.
161 e2715f69 Michael Hanselmann

162 ea03467c Iustin Pop
  This is what we use to track the user-submitted jobs. Locking must
163 ea03467c Iustin Pop
  be taken care of by users of this class.
164 ea03467c Iustin Pop

165 ea03467c Iustin Pop
  @type queue: L{JobQueue}
166 ea03467c Iustin Pop
  @ivar queue: the parent queue
167 ea03467c Iustin Pop
  @ivar id: the job ID
168 ea03467c Iustin Pop
  @type ops: list
169 ea03467c Iustin Pop
  @ivar ops: the list of _QueuedOpCode that constitute the job
170 ea03467c Iustin Pop
  @type log_serial: int
171 ea03467c Iustin Pop
  @ivar log_serial: holds the index for the next log entry
172 ea03467c Iustin Pop
  @ivar received_timestamp: the timestamp for when the job was received
173 ea03467c Iustin Pop
  @ivar start_timestmap: the timestamp for start of execution
174 ea03467c Iustin Pop
  @ivar end_timestamp: the timestamp for end of execution
175 e2715f69 Michael Hanselmann

176 e2715f69 Michael Hanselmann
  """
177 7260cfbe Iustin Pop
  # pylint: disable-msg=W0212
178 26d3fd2f Michael Hanselmann
  __slots__ = ["queue", "id", "ops", "log_serial", "ops_iter", "cur_opctx",
179 66d895a8 Iustin Pop
               "received_timestamp", "start_timestamp", "end_timestamp",
180 66d895a8 Iustin Pop
               "__weakref__"]
181 66d895a8 Iustin Pop
182 85f03e0d Michael Hanselmann
  def __init__(self, queue, job_id, ops):
183 ea03467c Iustin Pop
    """Constructor for the _QueuedJob.
184 ea03467c Iustin Pop

185 ea03467c Iustin Pop
    @type queue: L{JobQueue}
186 ea03467c Iustin Pop
    @param queue: our parent queue
187 ea03467c Iustin Pop
    @type job_id: job_id
188 ea03467c Iustin Pop
    @param job_id: our job id
189 ea03467c Iustin Pop
    @type ops: list
190 ea03467c Iustin Pop
    @param ops: the list of opcodes we hold, which will be encapsulated
191 ea03467c Iustin Pop
        in _QueuedOpCodes
192 ea03467c Iustin Pop

193 ea03467c Iustin Pop
    """
194 e2715f69 Michael Hanselmann
    if not ops:
195 c910bccb Guido Trotter
      raise errors.GenericError("A job needs at least one opcode")
196 e2715f69 Michael Hanselmann
197 85f03e0d Michael Hanselmann
    self.queue = queue
198 f1da30e6 Michael Hanselmann
    self.id = job_id
199 85f03e0d Michael Hanselmann
    self.ops = [_QueuedOpCode(op) for op in ops]
200 6c5a7090 Michael Hanselmann
    self.log_serial = 0
201 c56ec146 Iustin Pop
    self.received_timestamp = TimeStampNow()
202 c56ec146 Iustin Pop
    self.start_timestamp = None
203 c56ec146 Iustin Pop
    self.end_timestamp = None
204 6c5a7090 Michael Hanselmann
205 fa4aa6b4 Michael Hanselmann
    self._InitInMemory(self)
206 fa4aa6b4 Michael Hanselmann
207 fa4aa6b4 Michael Hanselmann
  @staticmethod
208 fa4aa6b4 Michael Hanselmann
  def _InitInMemory(obj):
209 fa4aa6b4 Michael Hanselmann
    """Initializes in-memory variables.
210 fa4aa6b4 Michael Hanselmann

211 fa4aa6b4 Michael Hanselmann
    """
212 03b63608 Michael Hanselmann
    obj.ops_iter = None
213 26d3fd2f Michael Hanselmann
    obj.cur_opctx = None
214 be760ba8 Michael Hanselmann
215 9fa2e150 Michael Hanselmann
  def __repr__(self):
216 9fa2e150 Michael Hanselmann
    status = ["%s.%s" % (self.__class__.__module__, self.__class__.__name__),
217 9fa2e150 Michael Hanselmann
              "id=%s" % self.id,
218 9fa2e150 Michael Hanselmann
              "ops=%s" % ",".join([op.input.Summary() for op in self.ops])]
219 9fa2e150 Michael Hanselmann
220 9fa2e150 Michael Hanselmann
    return "<%s at %#x>" % (" ".join(status), id(self))
221 9fa2e150 Michael Hanselmann
222 f1da30e6 Michael Hanselmann
  @classmethod
223 85f03e0d Michael Hanselmann
  def Restore(cls, queue, state):
224 ea03467c Iustin Pop
    """Restore a _QueuedJob from serialized state:
225 ea03467c Iustin Pop

226 ea03467c Iustin Pop
    @type queue: L{JobQueue}
227 ea03467c Iustin Pop
    @param queue: to which queue the restored job belongs
228 ea03467c Iustin Pop
    @type state: dict
229 ea03467c Iustin Pop
    @param state: the serialized state
230 ea03467c Iustin Pop
    @rtype: _JobQueue
231 ea03467c Iustin Pop
    @return: the restored _JobQueue instance
232 ea03467c Iustin Pop

233 ea03467c Iustin Pop
    """
234 85f03e0d Michael Hanselmann
    obj = _QueuedJob.__new__(cls)
235 85f03e0d Michael Hanselmann
    obj.queue = queue
236 85f03e0d Michael Hanselmann
    obj.id = state["id"]
237 c56ec146 Iustin Pop
    obj.received_timestamp = state.get("received_timestamp", None)
238 c56ec146 Iustin Pop
    obj.start_timestamp = state.get("start_timestamp", None)
239 c56ec146 Iustin Pop
    obj.end_timestamp = state.get("end_timestamp", None)
240 6c5a7090 Michael Hanselmann
241 6c5a7090 Michael Hanselmann
    obj.ops = []
242 6c5a7090 Michael Hanselmann
    obj.log_serial = 0
243 6c5a7090 Michael Hanselmann
    for op_state in state["ops"]:
244 6c5a7090 Michael Hanselmann
      op = _QueuedOpCode.Restore(op_state)
245 6c5a7090 Michael Hanselmann
      for log_entry in op.log:
246 6c5a7090 Michael Hanselmann
        obj.log_serial = max(obj.log_serial, log_entry[0])
247 6c5a7090 Michael Hanselmann
      obj.ops.append(op)
248 6c5a7090 Michael Hanselmann
249 fa4aa6b4 Michael Hanselmann
    cls._InitInMemory(obj)
250 be760ba8 Michael Hanselmann
251 f1da30e6 Michael Hanselmann
    return obj
252 f1da30e6 Michael Hanselmann
253 f1da30e6 Michael Hanselmann
  def Serialize(self):
254 ea03467c Iustin Pop
    """Serialize the _JobQueue instance.
255 ea03467c Iustin Pop

256 ea03467c Iustin Pop
    @rtype: dict
257 ea03467c Iustin Pop
    @return: the serialized state
258 ea03467c Iustin Pop

259 ea03467c Iustin Pop
    """
260 f1da30e6 Michael Hanselmann
    return {
261 f1da30e6 Michael Hanselmann
      "id": self.id,
262 85f03e0d Michael Hanselmann
      "ops": [op.Serialize() for op in self.ops],
263 c56ec146 Iustin Pop
      "start_timestamp": self.start_timestamp,
264 c56ec146 Iustin Pop
      "end_timestamp": self.end_timestamp,
265 c56ec146 Iustin Pop
      "received_timestamp": self.received_timestamp,
266 f1da30e6 Michael Hanselmann
      }
267 f1da30e6 Michael Hanselmann
268 85f03e0d Michael Hanselmann
  def CalcStatus(self):
269 ea03467c Iustin Pop
    """Compute the status of this job.
270 ea03467c Iustin Pop

271 ea03467c Iustin Pop
    This function iterates over all the _QueuedOpCodes in the job and
272 ea03467c Iustin Pop
    based on their status, computes the job status.
273 ea03467c Iustin Pop

274 ea03467c Iustin Pop
    The algorithm is:
275 ea03467c Iustin Pop
      - if we find a cancelled, or finished with error, the job
276 ea03467c Iustin Pop
        status will be the same
277 ea03467c Iustin Pop
      - otherwise, the last opcode with the status one of:
278 ea03467c Iustin Pop
          - waitlock
279 fbf0262f Michael Hanselmann
          - canceling
280 ea03467c Iustin Pop
          - running
281 ea03467c Iustin Pop

282 ea03467c Iustin Pop
        will determine the job status
283 ea03467c Iustin Pop

284 ea03467c Iustin Pop
      - otherwise, it means either all opcodes are queued, or success,
285 ea03467c Iustin Pop
        and the job status will be the same
286 ea03467c Iustin Pop

287 ea03467c Iustin Pop
    @return: the job status
288 ea03467c Iustin Pop

289 ea03467c Iustin Pop
    """
290 e2715f69 Michael Hanselmann
    status = constants.JOB_STATUS_QUEUED
291 e2715f69 Michael Hanselmann
292 e2715f69 Michael Hanselmann
    all_success = True
293 85f03e0d Michael Hanselmann
    for op in self.ops:
294 85f03e0d Michael Hanselmann
      if op.status == constants.OP_STATUS_SUCCESS:
295 e2715f69 Michael Hanselmann
        continue
296 e2715f69 Michael Hanselmann
297 e2715f69 Michael Hanselmann
      all_success = False
298 e2715f69 Michael Hanselmann
299 85f03e0d Michael Hanselmann
      if op.status == constants.OP_STATUS_QUEUED:
300 e2715f69 Michael Hanselmann
        pass
301 e92376d7 Iustin Pop
      elif op.status == constants.OP_STATUS_WAITLOCK:
302 e92376d7 Iustin Pop
        status = constants.JOB_STATUS_WAITLOCK
303 85f03e0d Michael Hanselmann
      elif op.status == constants.OP_STATUS_RUNNING:
304 e2715f69 Michael Hanselmann
        status = constants.JOB_STATUS_RUNNING
305 fbf0262f Michael Hanselmann
      elif op.status == constants.OP_STATUS_CANCELING:
306 fbf0262f Michael Hanselmann
        status = constants.JOB_STATUS_CANCELING
307 fbf0262f Michael Hanselmann
        break
308 85f03e0d Michael Hanselmann
      elif op.status == constants.OP_STATUS_ERROR:
309 f1da30e6 Michael Hanselmann
        status = constants.JOB_STATUS_ERROR
310 f1da30e6 Michael Hanselmann
        # The whole job fails if one opcode failed
311 f1da30e6 Michael Hanselmann
        break
312 85f03e0d Michael Hanselmann
      elif op.status == constants.OP_STATUS_CANCELED:
313 4cb1d919 Michael Hanselmann
        status = constants.OP_STATUS_CANCELED
314 4cb1d919 Michael Hanselmann
        break
315 e2715f69 Michael Hanselmann
316 e2715f69 Michael Hanselmann
    if all_success:
317 e2715f69 Michael Hanselmann
      status = constants.JOB_STATUS_SUCCESS
318 e2715f69 Michael Hanselmann
319 e2715f69 Michael Hanselmann
    return status
320 e2715f69 Michael Hanselmann
321 8f5c488d Michael Hanselmann
  def CalcPriority(self):
322 8f5c488d Michael Hanselmann
    """Gets the current priority for this job.
323 8f5c488d Michael Hanselmann

324 8f5c488d Michael Hanselmann
    Only unfinished opcodes are considered. When all are done, the default
325 8f5c488d Michael Hanselmann
    priority is used.
326 8f5c488d Michael Hanselmann

327 8f5c488d Michael Hanselmann
    @rtype: int
328 8f5c488d Michael Hanselmann

329 8f5c488d Michael Hanselmann
    """
330 8f5c488d Michael Hanselmann
    priorities = [op.priority for op in self.ops
331 8f5c488d Michael Hanselmann
                  if op.status not in constants.OPS_FINALIZED]
332 8f5c488d Michael Hanselmann
333 8f5c488d Michael Hanselmann
    if not priorities:
334 8f5c488d Michael Hanselmann
      # All opcodes are done, assume default priority
335 8f5c488d Michael Hanselmann
      return constants.OP_PRIO_DEFAULT
336 8f5c488d Michael Hanselmann
337 8f5c488d Michael Hanselmann
    return min(priorities)
338 8f5c488d Michael Hanselmann
339 6c5a7090 Michael Hanselmann
  def GetLogEntries(self, newer_than):
340 ea03467c Iustin Pop
    """Selectively returns the log entries.
341 ea03467c Iustin Pop

342 ea03467c Iustin Pop
    @type newer_than: None or int
343 5bbd3f7f Michael Hanselmann
    @param newer_than: if this is None, return all log entries,
344 ea03467c Iustin Pop
        otherwise return only the log entries with serial higher
345 ea03467c Iustin Pop
        than this value
346 ea03467c Iustin Pop
    @rtype: list
347 ea03467c Iustin Pop
    @return: the list of the log entries selected
348 ea03467c Iustin Pop

349 ea03467c Iustin Pop
    """
350 6c5a7090 Michael Hanselmann
    if newer_than is None:
351 6c5a7090 Michael Hanselmann
      serial = -1
352 6c5a7090 Michael Hanselmann
    else:
353 6c5a7090 Michael Hanselmann
      serial = newer_than
354 6c5a7090 Michael Hanselmann
355 6c5a7090 Michael Hanselmann
    entries = []
356 6c5a7090 Michael Hanselmann
    for op in self.ops:
357 63712a09 Iustin Pop
      entries.extend(filter(lambda entry: entry[0] > serial, op.log))
358 6c5a7090 Michael Hanselmann
359 6c5a7090 Michael Hanselmann
    return entries
360 6c5a7090 Michael Hanselmann
361 6a290889 Guido Trotter
  def GetInfo(self, fields):
362 6a290889 Guido Trotter
    """Returns information about a job.
363 6a290889 Guido Trotter

364 6a290889 Guido Trotter
    @type fields: list
365 6a290889 Guido Trotter
    @param fields: names of fields to return
366 6a290889 Guido Trotter
    @rtype: list
367 6a290889 Guido Trotter
    @return: list with one element for each field
368 6a290889 Guido Trotter
    @raise errors.OpExecError: when an invalid field
369 6a290889 Guido Trotter
        has been passed
370 6a290889 Guido Trotter

371 6a290889 Guido Trotter
    """
372 6a290889 Guido Trotter
    row = []
373 6a290889 Guido Trotter
    for fname in fields:
374 6a290889 Guido Trotter
      if fname == "id":
375 6a290889 Guido Trotter
        row.append(self.id)
376 6a290889 Guido Trotter
      elif fname == "status":
377 6a290889 Guido Trotter
        row.append(self.CalcStatus())
378 b8802cc4 Michael Hanselmann
      elif fname == "priority":
379 b8802cc4 Michael Hanselmann
        row.append(self.CalcPriority())
380 6a290889 Guido Trotter
      elif fname == "ops":
381 6a290889 Guido Trotter
        row.append([op.input.__getstate__() for op in self.ops])
382 6a290889 Guido Trotter
      elif fname == "opresult":
383 6a290889 Guido Trotter
        row.append([op.result for op in self.ops])
384 6a290889 Guido Trotter
      elif fname == "opstatus":
385 6a290889 Guido Trotter
        row.append([op.status for op in self.ops])
386 6a290889 Guido Trotter
      elif fname == "oplog":
387 6a290889 Guido Trotter
        row.append([op.log for op in self.ops])
388 6a290889 Guido Trotter
      elif fname == "opstart":
389 6a290889 Guido Trotter
        row.append([op.start_timestamp for op in self.ops])
390 6a290889 Guido Trotter
      elif fname == "opexec":
391 6a290889 Guido Trotter
        row.append([op.exec_timestamp for op in self.ops])
392 6a290889 Guido Trotter
      elif fname == "opend":
393 6a290889 Guido Trotter
        row.append([op.end_timestamp for op in self.ops])
394 b8802cc4 Michael Hanselmann
      elif fname == "oppriority":
395 b8802cc4 Michael Hanselmann
        row.append([op.priority for op in self.ops])
396 6a290889 Guido Trotter
      elif fname == "received_ts":
397 6a290889 Guido Trotter
        row.append(self.received_timestamp)
398 6a290889 Guido Trotter
      elif fname == "start_ts":
399 6a290889 Guido Trotter
        row.append(self.start_timestamp)
400 6a290889 Guido Trotter
      elif fname == "end_ts":
401 6a290889 Guido Trotter
        row.append(self.end_timestamp)
402 6a290889 Guido Trotter
      elif fname == "summary":
403 6a290889 Guido Trotter
        row.append([op.input.Summary() for op in self.ops])
404 6a290889 Guido Trotter
      else:
405 6a290889 Guido Trotter
        raise errors.OpExecError("Invalid self query field '%s'" % fname)
406 6a290889 Guido Trotter
    return row
407 6a290889 Guido Trotter
408 34327f51 Iustin Pop
  def MarkUnfinishedOps(self, status, result):
409 34327f51 Iustin Pop
    """Mark unfinished opcodes with a given status and result.
410 34327f51 Iustin Pop

411 34327f51 Iustin Pop
    This is an utility function for marking all running or waiting to
412 34327f51 Iustin Pop
    be run opcodes with a given status. Opcodes which are already
413 34327f51 Iustin Pop
    finalised are not changed.
414 34327f51 Iustin Pop

415 34327f51 Iustin Pop
    @param status: a given opcode status
416 34327f51 Iustin Pop
    @param result: the opcode result
417 34327f51 Iustin Pop

418 34327f51 Iustin Pop
    """
419 747f6113 Michael Hanselmann
    not_marked = True
420 747f6113 Michael Hanselmann
    for op in self.ops:
421 747f6113 Michael Hanselmann
      if op.status in constants.OPS_FINALIZED:
422 747f6113 Michael Hanselmann
        assert not_marked, "Finalized opcodes found after non-finalized ones"
423 747f6113 Michael Hanselmann
        continue
424 747f6113 Michael Hanselmann
      op.status = status
425 747f6113 Michael Hanselmann
      op.result = result
426 747f6113 Michael Hanselmann
      not_marked = False
427 34327f51 Iustin Pop
428 099b2870 Michael Hanselmann
  def Cancel(self):
429 a0d2fe2c Michael Hanselmann
    """Marks job as canceled/-ing if possible.
430 a0d2fe2c Michael Hanselmann

431 a0d2fe2c Michael Hanselmann
    @rtype: tuple; (bool, string)
432 a0d2fe2c Michael Hanselmann
    @return: Boolean describing whether job was successfully canceled or marked
433 a0d2fe2c Michael Hanselmann
      as canceling and a text message
434 a0d2fe2c Michael Hanselmann

435 a0d2fe2c Michael Hanselmann
    """
436 099b2870 Michael Hanselmann
    status = self.CalcStatus()
437 099b2870 Michael Hanselmann
438 099b2870 Michael Hanselmann
    if status == constants.JOB_STATUS_QUEUED:
439 099b2870 Michael Hanselmann
      self.MarkUnfinishedOps(constants.OP_STATUS_CANCELED,
440 099b2870 Michael Hanselmann
                             "Job canceled by request")
441 86b16e9d Michael Hanselmann
      return (True, "Job %s canceled" % self.id)
442 099b2870 Michael Hanselmann
443 099b2870 Michael Hanselmann
    elif status == constants.JOB_STATUS_WAITLOCK:
444 099b2870 Michael Hanselmann
      # The worker will notice the new status and cancel the job
445 099b2870 Michael Hanselmann
      self.MarkUnfinishedOps(constants.OP_STATUS_CANCELING, None)
446 86b16e9d Michael Hanselmann
      return (True, "Job %s will be canceled" % self.id)
447 099b2870 Michael Hanselmann
448 86b16e9d Michael Hanselmann
    else:
449 86b16e9d Michael Hanselmann
      logging.debug("Job %s is no longer waiting in the queue", self.id)
450 86b16e9d Michael Hanselmann
      return (False, "Job %s is no longer waiting in the queue" % self.id)
451 099b2870 Michael Hanselmann
452 f1048938 Iustin Pop
453 ef2df7d3 Michael Hanselmann
class _OpExecCallbacks(mcpu.OpExecCbBase):
454 031a3e57 Michael Hanselmann
  def __init__(self, queue, job, op):
455 031a3e57 Michael Hanselmann
    """Initializes this class.
456 ea03467c Iustin Pop

457 031a3e57 Michael Hanselmann
    @type queue: L{JobQueue}
458 031a3e57 Michael Hanselmann
    @param queue: Job queue
459 031a3e57 Michael Hanselmann
    @type job: L{_QueuedJob}
460 031a3e57 Michael Hanselmann
    @param job: Job object
461 031a3e57 Michael Hanselmann
    @type op: L{_QueuedOpCode}
462 031a3e57 Michael Hanselmann
    @param op: OpCode
463 031a3e57 Michael Hanselmann

464 031a3e57 Michael Hanselmann
    """
465 031a3e57 Michael Hanselmann
    assert queue, "Queue is missing"
466 031a3e57 Michael Hanselmann
    assert job, "Job is missing"
467 031a3e57 Michael Hanselmann
    assert op, "Opcode is missing"
468 031a3e57 Michael Hanselmann
469 031a3e57 Michael Hanselmann
    self._queue = queue
470 031a3e57 Michael Hanselmann
    self._job = job
471 031a3e57 Michael Hanselmann
    self._op = op
472 031a3e57 Michael Hanselmann
473 dc1e2262 Michael Hanselmann
  def _CheckCancel(self):
474 dc1e2262 Michael Hanselmann
    """Raises an exception to cancel the job if asked to.
475 dc1e2262 Michael Hanselmann

476 dc1e2262 Michael Hanselmann
    """
477 dc1e2262 Michael Hanselmann
    # Cancel here if we were asked to
478 dc1e2262 Michael Hanselmann
    if self._op.status == constants.OP_STATUS_CANCELING:
479 dc1e2262 Michael Hanselmann
      logging.debug("Canceling opcode")
480 dc1e2262 Michael Hanselmann
      raise CancelJob()
481 dc1e2262 Michael Hanselmann
482 271daef8 Iustin Pop
  @locking.ssynchronized(_QUEUE, shared=1)
483 031a3e57 Michael Hanselmann
  def NotifyStart(self):
484 e92376d7 Iustin Pop
    """Mark the opcode as running, not lock-waiting.
485 e92376d7 Iustin Pop

486 031a3e57 Michael Hanselmann
    This is called from the mcpu code as a notifier function, when the LU is
487 031a3e57 Michael Hanselmann
    finally about to start the Exec() method. Of course, to have end-user
488 031a3e57 Michael Hanselmann
    visible results, the opcode must be initially (before calling into
489 031a3e57 Michael Hanselmann
    Processor.ExecOpCode) set to OP_STATUS_WAITLOCK.
490 e92376d7 Iustin Pop

491 e92376d7 Iustin Pop
    """
492 9bdab621 Michael Hanselmann
    assert self._op in self._job.ops
493 271daef8 Iustin Pop
    assert self._op.status in (constants.OP_STATUS_WAITLOCK,
494 271daef8 Iustin Pop
                               constants.OP_STATUS_CANCELING)
495 fbf0262f Michael Hanselmann
496 271daef8 Iustin Pop
    # Cancel here if we were asked to
497 dc1e2262 Michael Hanselmann
    self._CheckCancel()
498 fbf0262f Michael Hanselmann
499 e35344b4 Michael Hanselmann
    logging.debug("Opcode is now running")
500 9bdab621 Michael Hanselmann
501 271daef8 Iustin Pop
    self._op.status = constants.OP_STATUS_RUNNING
502 271daef8 Iustin Pop
    self._op.exec_timestamp = TimeStampNow()
503 271daef8 Iustin Pop
504 271daef8 Iustin Pop
    # And finally replicate the job status
505 271daef8 Iustin Pop
    self._queue.UpdateJobUnlocked(self._job)
506 031a3e57 Michael Hanselmann
507 ebb80afa Guido Trotter
  @locking.ssynchronized(_QUEUE, shared=1)
508 9bf5e01f Guido Trotter
  def _AppendFeedback(self, timestamp, log_type, log_msg):
509 9bf5e01f Guido Trotter
    """Internal feedback append function, with locks
510 9bf5e01f Guido Trotter

511 9bf5e01f Guido Trotter
    """
512 9bf5e01f Guido Trotter
    self._job.log_serial += 1
513 9bf5e01f Guido Trotter
    self._op.log.append((self._job.log_serial, timestamp, log_type, log_msg))
514 9bf5e01f Guido Trotter
    self._queue.UpdateJobUnlocked(self._job, replicate=False)
515 9bf5e01f Guido Trotter
516 031a3e57 Michael Hanselmann
  def Feedback(self, *args):
517 031a3e57 Michael Hanselmann
    """Append a log entry.
518 031a3e57 Michael Hanselmann

519 031a3e57 Michael Hanselmann
    """
520 031a3e57 Michael Hanselmann
    assert len(args) < 3
521 031a3e57 Michael Hanselmann
522 031a3e57 Michael Hanselmann
    if len(args) == 1:
523 031a3e57 Michael Hanselmann
      log_type = constants.ELOG_MESSAGE
524 031a3e57 Michael Hanselmann
      log_msg = args[0]
525 031a3e57 Michael Hanselmann
    else:
526 031a3e57 Michael Hanselmann
      (log_type, log_msg) = args
527 031a3e57 Michael Hanselmann
528 031a3e57 Michael Hanselmann
    # The time is split to make serialization easier and not lose
529 031a3e57 Michael Hanselmann
    # precision.
530 031a3e57 Michael Hanselmann
    timestamp = utils.SplitTime(time.time())
531 9bf5e01f Guido Trotter
    self._AppendFeedback(timestamp, log_type, log_msg)
532 031a3e57 Michael Hanselmann
533 acf931b7 Michael Hanselmann
  def CheckCancel(self):
534 acf931b7 Michael Hanselmann
    """Check whether job has been cancelled.
535 ef2df7d3 Michael Hanselmann

536 ef2df7d3 Michael Hanselmann
    """
537 dc1e2262 Michael Hanselmann
    assert self._op.status in (constants.OP_STATUS_WAITLOCK,
538 dc1e2262 Michael Hanselmann
                               constants.OP_STATUS_CANCELING)
539 dc1e2262 Michael Hanselmann
540 dc1e2262 Michael Hanselmann
    # Cancel here if we were asked to
541 dc1e2262 Michael Hanselmann
    self._CheckCancel()
542 dc1e2262 Michael Hanselmann
543 6a373640 Michael Hanselmann
  def SubmitManyJobs(self, jobs):
544 6a373640 Michael Hanselmann
    """Submits jobs for processing.
545 6a373640 Michael Hanselmann

546 6a373640 Michael Hanselmann
    See L{JobQueue.SubmitManyJobs}.
547 6a373640 Michael Hanselmann

548 6a373640 Michael Hanselmann
    """
549 6a373640 Michael Hanselmann
    # Locking is done in job queue
550 6a373640 Michael Hanselmann
    return self._queue.SubmitManyJobs(jobs)
551 6a373640 Michael Hanselmann
552 031a3e57 Michael Hanselmann
553 989a8bee Michael Hanselmann
class _JobChangesChecker(object):
554 989a8bee Michael Hanselmann
  def __init__(self, fields, prev_job_info, prev_log_serial):
555 989a8bee Michael Hanselmann
    """Initializes this class.
556 6c2549d6 Guido Trotter

557 989a8bee Michael Hanselmann
    @type fields: list of strings
558 989a8bee Michael Hanselmann
    @param fields: Fields requested by LUXI client
559 989a8bee Michael Hanselmann
    @type prev_job_info: string
560 989a8bee Michael Hanselmann
    @param prev_job_info: previous job info, as passed by the LUXI client
561 989a8bee Michael Hanselmann
    @type prev_log_serial: string
562 989a8bee Michael Hanselmann
    @param prev_log_serial: previous job serial, as passed by the LUXI client
563 6c2549d6 Guido Trotter

564 989a8bee Michael Hanselmann
    """
565 989a8bee Michael Hanselmann
    self._fields = fields
566 989a8bee Michael Hanselmann
    self._prev_job_info = prev_job_info
567 989a8bee Michael Hanselmann
    self._prev_log_serial = prev_log_serial
568 6c2549d6 Guido Trotter
569 989a8bee Michael Hanselmann
  def __call__(self, job):
570 989a8bee Michael Hanselmann
    """Checks whether job has changed.
571 6c2549d6 Guido Trotter

572 989a8bee Michael Hanselmann
    @type job: L{_QueuedJob}
573 989a8bee Michael Hanselmann
    @param job: Job object
574 6c2549d6 Guido Trotter

575 6c2549d6 Guido Trotter
    """
576 989a8bee Michael Hanselmann
    status = job.CalcStatus()
577 989a8bee Michael Hanselmann
    job_info = job.GetInfo(self._fields)
578 989a8bee Michael Hanselmann
    log_entries = job.GetLogEntries(self._prev_log_serial)
579 6c2549d6 Guido Trotter
580 6c2549d6 Guido Trotter
    # Serializing and deserializing data can cause type changes (e.g. from
581 6c2549d6 Guido Trotter
    # tuple to list) or precision loss. We're doing it here so that we get
582 6c2549d6 Guido Trotter
    # the same modifications as the data received from the client. Without
583 6c2549d6 Guido Trotter
    # this, the comparison afterwards might fail without the data being
584 6c2549d6 Guido Trotter
    # significantly different.
585 6c2549d6 Guido Trotter
    # TODO: we just deserialized from disk, investigate how to make sure that
586 6c2549d6 Guido Trotter
    # the job info and log entries are compatible to avoid this further step.
587 989a8bee Michael Hanselmann
    # TODO: Doing something like in testutils.py:UnifyValueType might be more
588 989a8bee Michael Hanselmann
    # efficient, though floats will be tricky
589 989a8bee Michael Hanselmann
    job_info = serializer.LoadJson(serializer.DumpJson(job_info))
590 989a8bee Michael Hanselmann
    log_entries = serializer.LoadJson(serializer.DumpJson(log_entries))
591 6c2549d6 Guido Trotter
592 6c2549d6 Guido Trotter
    # Don't even try to wait if the job is no longer running, there will be
593 6c2549d6 Guido Trotter
    # no changes.
594 989a8bee Michael Hanselmann
    if (status not in (constants.JOB_STATUS_QUEUED,
595 989a8bee Michael Hanselmann
                       constants.JOB_STATUS_RUNNING,
596 989a8bee Michael Hanselmann
                       constants.JOB_STATUS_WAITLOCK) or
597 989a8bee Michael Hanselmann
        job_info != self._prev_job_info or
598 989a8bee Michael Hanselmann
        (log_entries and self._prev_log_serial != log_entries[0][0])):
599 989a8bee Michael Hanselmann
      logging.debug("Job %s changed", job.id)
600 989a8bee Michael Hanselmann
      return (job_info, log_entries)
601 6c2549d6 Guido Trotter
602 989a8bee Michael Hanselmann
    return None
603 989a8bee Michael Hanselmann
604 989a8bee Michael Hanselmann
605 989a8bee Michael Hanselmann
class _JobFileChangesWaiter(object):
606 989a8bee Michael Hanselmann
  def __init__(self, filename):
607 989a8bee Michael Hanselmann
    """Initializes this class.
608 989a8bee Michael Hanselmann

609 989a8bee Michael Hanselmann
    @type filename: string
610 989a8bee Michael Hanselmann
    @param filename: Path to job file
611 989a8bee Michael Hanselmann
    @raises errors.InotifyError: if the notifier cannot be setup
612 6c2549d6 Guido Trotter

613 989a8bee Michael Hanselmann
    """
614 989a8bee Michael Hanselmann
    self._wm = pyinotify.WatchManager()
615 989a8bee Michael Hanselmann
    self._inotify_handler = \
616 989a8bee Michael Hanselmann
      asyncnotifier.SingleFileEventHandler(self._wm, self._OnInotify, filename)
617 989a8bee Michael Hanselmann
    self._notifier = \
618 989a8bee Michael Hanselmann
      pyinotify.Notifier(self._wm, default_proc_fun=self._inotify_handler)
619 989a8bee Michael Hanselmann
    try:
620 989a8bee Michael Hanselmann
      self._inotify_handler.enable()
621 989a8bee Michael Hanselmann
    except Exception:
622 989a8bee Michael Hanselmann
      # pyinotify doesn't close file descriptors automatically
623 989a8bee Michael Hanselmann
      self._notifier.stop()
624 989a8bee Michael Hanselmann
      raise
625 989a8bee Michael Hanselmann
626 989a8bee Michael Hanselmann
  def _OnInotify(self, notifier_enabled):
627 989a8bee Michael Hanselmann
    """Callback for inotify.
628 989a8bee Michael Hanselmann

629 989a8bee Michael Hanselmann
    """
630 6c2549d6 Guido Trotter
    if not notifier_enabled:
631 989a8bee Michael Hanselmann
      self._inotify_handler.enable()
632 989a8bee Michael Hanselmann
633 989a8bee Michael Hanselmann
  def Wait(self, timeout):
634 989a8bee Michael Hanselmann
    """Waits for the job file to change.
635 989a8bee Michael Hanselmann

636 989a8bee Michael Hanselmann
    @type timeout: float
637 989a8bee Michael Hanselmann
    @param timeout: Timeout in seconds
638 989a8bee Michael Hanselmann
    @return: Whether there have been events
639 989a8bee Michael Hanselmann

640 989a8bee Michael Hanselmann
    """
641 989a8bee Michael Hanselmann
    assert timeout >= 0
642 989a8bee Michael Hanselmann
    have_events = self._notifier.check_events(timeout * 1000)
643 989a8bee Michael Hanselmann
    if have_events:
644 989a8bee Michael Hanselmann
      self._notifier.read_events()
645 989a8bee Michael Hanselmann
    self._notifier.process_events()
646 989a8bee Michael Hanselmann
    return have_events
647 989a8bee Michael Hanselmann
648 989a8bee Michael Hanselmann
  def Close(self):
649 989a8bee Michael Hanselmann
    """Closes underlying notifier and its file descriptor.
650 989a8bee Michael Hanselmann

651 989a8bee Michael Hanselmann
    """
652 989a8bee Michael Hanselmann
    self._notifier.stop()
653 989a8bee Michael Hanselmann
654 989a8bee Michael Hanselmann
655 989a8bee Michael Hanselmann
class _JobChangesWaiter(object):
656 989a8bee Michael Hanselmann
  def __init__(self, filename):
657 989a8bee Michael Hanselmann
    """Initializes this class.
658 989a8bee Michael Hanselmann

659 989a8bee Michael Hanselmann
    @type filename: string
660 989a8bee Michael Hanselmann
    @param filename: Path to job file
661 989a8bee Michael Hanselmann

662 989a8bee Michael Hanselmann
    """
663 989a8bee Michael Hanselmann
    self._filewaiter = None
664 989a8bee Michael Hanselmann
    self._filename = filename
665 6c2549d6 Guido Trotter
666 989a8bee Michael Hanselmann
  def Wait(self, timeout):
667 989a8bee Michael Hanselmann
    """Waits for a job to change.
668 6c2549d6 Guido Trotter

669 989a8bee Michael Hanselmann
    @type timeout: float
670 989a8bee Michael Hanselmann
    @param timeout: Timeout in seconds
671 989a8bee Michael Hanselmann
    @return: Whether there have been events
672 989a8bee Michael Hanselmann

673 989a8bee Michael Hanselmann
    """
674 989a8bee Michael Hanselmann
    if self._filewaiter:
675 989a8bee Michael Hanselmann
      return self._filewaiter.Wait(timeout)
676 989a8bee Michael Hanselmann
677 989a8bee Michael Hanselmann
    # Lazy setup: Avoid inotify setup cost when job file has already changed.
678 989a8bee Michael Hanselmann
    # If this point is reached, return immediately and let caller check the job
679 989a8bee Michael Hanselmann
    # file again in case there were changes since the last check. This avoids a
680 989a8bee Michael Hanselmann
    # race condition.
681 989a8bee Michael Hanselmann
    self._filewaiter = _JobFileChangesWaiter(self._filename)
682 989a8bee Michael Hanselmann
683 989a8bee Michael Hanselmann
    return True
684 989a8bee Michael Hanselmann
685 989a8bee Michael Hanselmann
  def Close(self):
686 989a8bee Michael Hanselmann
    """Closes underlying waiter.
687 989a8bee Michael Hanselmann

688 989a8bee Michael Hanselmann
    """
689 989a8bee Michael Hanselmann
    if self._filewaiter:
690 989a8bee Michael Hanselmann
      self._filewaiter.Close()
691 989a8bee Michael Hanselmann
692 989a8bee Michael Hanselmann
693 989a8bee Michael Hanselmann
class _WaitForJobChangesHelper(object):
694 989a8bee Michael Hanselmann
  """Helper class using inotify to wait for changes in a job file.
695 989a8bee Michael Hanselmann

696 989a8bee Michael Hanselmann
  This class takes a previous job status and serial, and alerts the client when
697 989a8bee Michael Hanselmann
  the current job status has changed.
698 989a8bee Michael Hanselmann

699 989a8bee Michael Hanselmann
  """
700 989a8bee Michael Hanselmann
  @staticmethod
701 989a8bee Michael Hanselmann
  def _CheckForChanges(job_load_fn, check_fn):
702 989a8bee Michael Hanselmann
    job = job_load_fn()
703 989a8bee Michael Hanselmann
    if not job:
704 989a8bee Michael Hanselmann
      raise errors.JobLost()
705 989a8bee Michael Hanselmann
706 989a8bee Michael Hanselmann
    result = check_fn(job)
707 989a8bee Michael Hanselmann
    if result is None:
708 989a8bee Michael Hanselmann
      raise utils.RetryAgain()
709 989a8bee Michael Hanselmann
710 989a8bee Michael Hanselmann
    return result
711 989a8bee Michael Hanselmann
712 989a8bee Michael Hanselmann
  def __call__(self, filename, job_load_fn,
713 989a8bee Michael Hanselmann
               fields, prev_job_info, prev_log_serial, timeout):
714 989a8bee Michael Hanselmann
    """Waits for changes on a job.
715 989a8bee Michael Hanselmann

716 989a8bee Michael Hanselmann
    @type filename: string
717 989a8bee Michael Hanselmann
    @param filename: File on which to wait for changes
718 989a8bee Michael Hanselmann
    @type job_load_fn: callable
719 989a8bee Michael Hanselmann
    @param job_load_fn: Function to load job
720 989a8bee Michael Hanselmann
    @type fields: list of strings
721 989a8bee Michael Hanselmann
    @param fields: Which fields to check for changes
722 989a8bee Michael Hanselmann
    @type prev_job_info: list or None
723 989a8bee Michael Hanselmann
    @param prev_job_info: Last job information returned
724 989a8bee Michael Hanselmann
    @type prev_log_serial: int
725 989a8bee Michael Hanselmann
    @param prev_log_serial: Last job message serial number
726 989a8bee Michael Hanselmann
    @type timeout: float
727 989a8bee Michael Hanselmann
    @param timeout: maximum time to wait in seconds
728 989a8bee Michael Hanselmann

729 989a8bee Michael Hanselmann
    """
730 6c2549d6 Guido Trotter
    try:
731 989a8bee Michael Hanselmann
      check_fn = _JobChangesChecker(fields, prev_job_info, prev_log_serial)
732 989a8bee Michael Hanselmann
      waiter = _JobChangesWaiter(filename)
733 989a8bee Michael Hanselmann
      try:
734 989a8bee Michael Hanselmann
        return utils.Retry(compat.partial(self._CheckForChanges,
735 989a8bee Michael Hanselmann
                                          job_load_fn, check_fn),
736 989a8bee Michael Hanselmann
                           utils.RETRY_REMAINING_TIME, timeout,
737 989a8bee Michael Hanselmann
                           wait_fn=waiter.Wait)
738 989a8bee Michael Hanselmann
      finally:
739 989a8bee Michael Hanselmann
        waiter.Close()
740 6c2549d6 Guido Trotter
    except (errors.InotifyError, errors.JobLost):
741 6c2549d6 Guido Trotter
      return None
742 6c2549d6 Guido Trotter
    except utils.RetryTimeout:
743 6c2549d6 Guido Trotter
      return constants.JOB_NOTCHANGED
744 6c2549d6 Guido Trotter
745 6c2549d6 Guido Trotter
746 6760e4ed Michael Hanselmann
def _EncodeOpError(err):
747 6760e4ed Michael Hanselmann
  """Encodes an error which occurred while processing an opcode.
748 6760e4ed Michael Hanselmann

749 6760e4ed Michael Hanselmann
  """
750 6760e4ed Michael Hanselmann
  if isinstance(err, errors.GenericError):
751 6760e4ed Michael Hanselmann
    to_encode = err
752 6760e4ed Michael Hanselmann
  else:
753 6760e4ed Michael Hanselmann
    to_encode = errors.OpExecError(str(err))
754 6760e4ed Michael Hanselmann
755 6760e4ed Michael Hanselmann
  return errors.EncodeException(to_encode)
756 6760e4ed Michael Hanselmann
757 6760e4ed Michael Hanselmann
758 26d3fd2f Michael Hanselmann
class _TimeoutStrategyWrapper:
759 26d3fd2f Michael Hanselmann
  def __init__(self, fn):
760 26d3fd2f Michael Hanselmann
    """Initializes this class.
761 26d3fd2f Michael Hanselmann

762 26d3fd2f Michael Hanselmann
    """
763 26d3fd2f Michael Hanselmann
    self._fn = fn
764 26d3fd2f Michael Hanselmann
    self._next = None
765 26d3fd2f Michael Hanselmann
766 26d3fd2f Michael Hanselmann
  def _Advance(self):
767 26d3fd2f Michael Hanselmann
    """Gets the next timeout if necessary.
768 26d3fd2f Michael Hanselmann

769 26d3fd2f Michael Hanselmann
    """
770 26d3fd2f Michael Hanselmann
    if self._next is None:
771 26d3fd2f Michael Hanselmann
      self._next = self._fn()
772 26d3fd2f Michael Hanselmann
773 26d3fd2f Michael Hanselmann
  def Peek(self):
774 26d3fd2f Michael Hanselmann
    """Returns the next timeout.
775 26d3fd2f Michael Hanselmann

776 26d3fd2f Michael Hanselmann
    """
777 26d3fd2f Michael Hanselmann
    self._Advance()
778 26d3fd2f Michael Hanselmann
    return self._next
779 26d3fd2f Michael Hanselmann
780 26d3fd2f Michael Hanselmann
  def Next(self):
781 26d3fd2f Michael Hanselmann
    """Returns the current timeout and advances the internal state.
782 26d3fd2f Michael Hanselmann

783 26d3fd2f Michael Hanselmann
    """
784 26d3fd2f Michael Hanselmann
    self._Advance()
785 26d3fd2f Michael Hanselmann
    result = self._next
786 26d3fd2f Michael Hanselmann
    self._next = None
787 26d3fd2f Michael Hanselmann
    return result
788 26d3fd2f Michael Hanselmann
789 26d3fd2f Michael Hanselmann
790 b80cc518 Michael Hanselmann
class _OpExecContext:
791 26d3fd2f Michael Hanselmann
  def __init__(self, op, index, log_prefix, timeout_strategy_factory):
792 b80cc518 Michael Hanselmann
    """Initializes this class.
793 b80cc518 Michael Hanselmann

794 b80cc518 Michael Hanselmann
    """
795 b80cc518 Michael Hanselmann
    self.op = op
796 b80cc518 Michael Hanselmann
    self.index = index
797 b80cc518 Michael Hanselmann
    self.log_prefix = log_prefix
798 b80cc518 Michael Hanselmann
    self.summary = op.input.Summary()
799 b80cc518 Michael Hanselmann
800 26d3fd2f Michael Hanselmann
    self._timeout_strategy_factory = timeout_strategy_factory
801 26d3fd2f Michael Hanselmann
    self._ResetTimeoutStrategy()
802 26d3fd2f Michael Hanselmann
803 26d3fd2f Michael Hanselmann
  def _ResetTimeoutStrategy(self):
804 26d3fd2f Michael Hanselmann
    """Creates a new timeout strategy.
805 26d3fd2f Michael Hanselmann

806 26d3fd2f Michael Hanselmann
    """
807 26d3fd2f Michael Hanselmann
    self._timeout_strategy = \
808 26d3fd2f Michael Hanselmann
      _TimeoutStrategyWrapper(self._timeout_strategy_factory().NextAttempt)
809 26d3fd2f Michael Hanselmann
810 26d3fd2f Michael Hanselmann
  def CheckPriorityIncrease(self):
811 26d3fd2f Michael Hanselmann
    """Checks whether priority can and should be increased.
812 26d3fd2f Michael Hanselmann

813 26d3fd2f Michael Hanselmann
    Called when locks couldn't be acquired.
814 26d3fd2f Michael Hanselmann

815 26d3fd2f Michael Hanselmann
    """
816 26d3fd2f Michael Hanselmann
    op = self.op
817 26d3fd2f Michael Hanselmann
818 26d3fd2f Michael Hanselmann
    # Exhausted all retries and next round should not use blocking acquire
819 26d3fd2f Michael Hanselmann
    # for locks?
820 26d3fd2f Michael Hanselmann
    if (self._timeout_strategy.Peek() is None and
821 26d3fd2f Michael Hanselmann
        op.priority > constants.OP_PRIO_HIGHEST):
822 26d3fd2f Michael Hanselmann
      logging.debug("Increasing priority")
823 26d3fd2f Michael Hanselmann
      op.priority -= 1
824 26d3fd2f Michael Hanselmann
      self._ResetTimeoutStrategy()
825 26d3fd2f Michael Hanselmann
      return True
826 26d3fd2f Michael Hanselmann
827 26d3fd2f Michael Hanselmann
    return False
828 26d3fd2f Michael Hanselmann
829 26d3fd2f Michael Hanselmann
  def GetNextLockTimeout(self):
830 26d3fd2f Michael Hanselmann
    """Returns the next lock acquire timeout.
831 26d3fd2f Michael Hanselmann

832 26d3fd2f Michael Hanselmann
    """
833 26d3fd2f Michael Hanselmann
    return self._timeout_strategy.Next()
834 26d3fd2f Michael Hanselmann
835 b80cc518 Michael Hanselmann
836 be760ba8 Michael Hanselmann
class _JobProcessor(object):
837 26d3fd2f Michael Hanselmann
  def __init__(self, queue, opexec_fn, job,
838 26d3fd2f Michael Hanselmann
               _timeout_strategy_factory=mcpu.LockAttemptTimeoutStrategy):
839 be760ba8 Michael Hanselmann
    """Initializes this class.
840 be760ba8 Michael Hanselmann

841 be760ba8 Michael Hanselmann
    """
842 be760ba8 Michael Hanselmann
    self.queue = queue
843 be760ba8 Michael Hanselmann
    self.opexec_fn = opexec_fn
844 be760ba8 Michael Hanselmann
    self.job = job
845 26d3fd2f Michael Hanselmann
    self._timeout_strategy_factory = _timeout_strategy_factory
846 be760ba8 Michael Hanselmann
847 be760ba8 Michael Hanselmann
  @staticmethod
848 26d3fd2f Michael Hanselmann
  def _FindNextOpcode(job, timeout_strategy_factory):
849 be760ba8 Michael Hanselmann
    """Locates the next opcode to run.
850 be760ba8 Michael Hanselmann

851 be760ba8 Michael Hanselmann
    @type job: L{_QueuedJob}
852 be760ba8 Michael Hanselmann
    @param job: Job object
853 26d3fd2f Michael Hanselmann
    @param timeout_strategy_factory: Callable to create new timeout strategy
854 be760ba8 Michael Hanselmann

855 be760ba8 Michael Hanselmann
    """
856 be760ba8 Michael Hanselmann
    # Create some sort of a cache to speed up locating next opcode for future
857 be760ba8 Michael Hanselmann
    # lookups
858 be760ba8 Michael Hanselmann
    # TODO: Consider splitting _QueuedJob.ops into two separate lists, one for
859 be760ba8 Michael Hanselmann
    # pending and one for processed ops.
860 03b63608 Michael Hanselmann
    if job.ops_iter is None:
861 03b63608 Michael Hanselmann
      job.ops_iter = enumerate(job.ops)
862 be760ba8 Michael Hanselmann
863 be760ba8 Michael Hanselmann
    # Find next opcode to run
864 be760ba8 Michael Hanselmann
    while True:
865 be760ba8 Michael Hanselmann
      try:
866 03b63608 Michael Hanselmann
        (idx, op) = job.ops_iter.next()
867 be760ba8 Michael Hanselmann
      except StopIteration:
868 be760ba8 Michael Hanselmann
        raise errors.ProgrammerError("Called for a finished job")
869 be760ba8 Michael Hanselmann
870 be760ba8 Michael Hanselmann
      if op.status == constants.OP_STATUS_RUNNING:
871 be760ba8 Michael Hanselmann
        # Found an opcode already marked as running
872 be760ba8 Michael Hanselmann
        raise errors.ProgrammerError("Called for job marked as running")
873 be760ba8 Michael Hanselmann
874 26d3fd2f Michael Hanselmann
      opctx = _OpExecContext(op, idx, "Op %s/%s" % (idx + 1, len(job.ops)),
875 26d3fd2f Michael Hanselmann
                             timeout_strategy_factory)
876 be760ba8 Michael Hanselmann
877 be760ba8 Michael Hanselmann
      if op.status == constants.OP_STATUS_CANCELED:
878 be760ba8 Michael Hanselmann
        # Cancelled jobs are handled by the caller
879 be760ba8 Michael Hanselmann
        assert not compat.any(i.status != constants.OP_STATUS_CANCELED
880 be760ba8 Michael Hanselmann
                              for i in job.ops[idx:])
881 be760ba8 Michael Hanselmann
882 be760ba8 Michael Hanselmann
      elif op.status in constants.OPS_FINALIZED:
883 be760ba8 Michael Hanselmann
        # This is a job that was partially completed before master daemon
884 be760ba8 Michael Hanselmann
        # shutdown, so it can be expected that some opcodes are already
885 be760ba8 Michael Hanselmann
        # completed successfully (if any did error out, then the whole job
886 be760ba8 Michael Hanselmann
        # should have been aborted and not resubmitted for processing).
887 be760ba8 Michael Hanselmann
        logging.info("%s: opcode %s already processed, skipping",
888 b80cc518 Michael Hanselmann
                     opctx.log_prefix, opctx.summary)
889 be760ba8 Michael Hanselmann
        continue
890 be760ba8 Michael Hanselmann
891 b80cc518 Michael Hanselmann
      return opctx
892 be760ba8 Michael Hanselmann
893 be760ba8 Michael Hanselmann
  @staticmethod
894 be760ba8 Michael Hanselmann
  def _MarkWaitlock(job, op):
895 be760ba8 Michael Hanselmann
    """Marks an opcode as waiting for locks.
896 be760ba8 Michael Hanselmann

897 be760ba8 Michael Hanselmann
    The job's start timestamp is also set if necessary.
898 be760ba8 Michael Hanselmann

899 be760ba8 Michael Hanselmann
    @type job: L{_QueuedJob}
900 be760ba8 Michael Hanselmann
    @param job: Job object
901 a38e8674 Michael Hanselmann
    @type op: L{_QueuedOpCode}
902 a38e8674 Michael Hanselmann
    @param op: Opcode object
903 be760ba8 Michael Hanselmann

904 be760ba8 Michael Hanselmann
    """
905 be760ba8 Michael Hanselmann
    assert op in job.ops
906 5fd6b694 Michael Hanselmann
    assert op.status in (constants.OP_STATUS_QUEUED,
907 5fd6b694 Michael Hanselmann
                         constants.OP_STATUS_WAITLOCK)
908 5fd6b694 Michael Hanselmann
909 5fd6b694 Michael Hanselmann
    update = False
910 be760ba8 Michael Hanselmann
911 be760ba8 Michael Hanselmann
    op.result = None
912 5fd6b694 Michael Hanselmann
913 5fd6b694 Michael Hanselmann
    if op.status == constants.OP_STATUS_QUEUED:
914 5fd6b694 Michael Hanselmann
      op.status = constants.OP_STATUS_WAITLOCK
915 5fd6b694 Michael Hanselmann
      update = True
916 5fd6b694 Michael Hanselmann
917 5fd6b694 Michael Hanselmann
    if op.start_timestamp is None:
918 5fd6b694 Michael Hanselmann
      op.start_timestamp = TimeStampNow()
919 5fd6b694 Michael Hanselmann
      update = True
920 be760ba8 Michael Hanselmann
921 be760ba8 Michael Hanselmann
    if job.start_timestamp is None:
922 be760ba8 Michael Hanselmann
      job.start_timestamp = op.start_timestamp
923 5fd6b694 Michael Hanselmann
      update = True
924 5fd6b694 Michael Hanselmann
925 5fd6b694 Michael Hanselmann
    assert op.status == constants.OP_STATUS_WAITLOCK
926 5fd6b694 Michael Hanselmann
927 5fd6b694 Michael Hanselmann
    return update
928 be760ba8 Michael Hanselmann
929 b80cc518 Michael Hanselmann
  def _ExecOpCodeUnlocked(self, opctx):
930 be760ba8 Michael Hanselmann
    """Processes one opcode and returns the result.
931 be760ba8 Michael Hanselmann

932 be760ba8 Michael Hanselmann
    """
933 b80cc518 Michael Hanselmann
    op = opctx.op
934 b80cc518 Michael Hanselmann
935 be760ba8 Michael Hanselmann
    assert op.status == constants.OP_STATUS_WAITLOCK
936 be760ba8 Michael Hanselmann
937 26d3fd2f Michael Hanselmann
    timeout = opctx.GetNextLockTimeout()
938 26d3fd2f Michael Hanselmann
939 be760ba8 Michael Hanselmann
    try:
940 be760ba8 Michael Hanselmann
      # Make sure not to hold queue lock while calling ExecOpCode
941 be760ba8 Michael Hanselmann
      result = self.opexec_fn(op.input,
942 26d3fd2f Michael Hanselmann
                              _OpExecCallbacks(self.queue, self.job, op),
943 f23db633 Michael Hanselmann
                              timeout=timeout, priority=op.priority)
944 26d3fd2f Michael Hanselmann
    except mcpu.LockAcquireTimeout:
945 26d3fd2f Michael Hanselmann
      assert timeout is not None, "Received timeout for blocking acquire"
946 26d3fd2f Michael Hanselmann
      logging.debug("Couldn't acquire locks in %0.6fs", timeout)
947 9e49dfc5 Michael Hanselmann
948 9e49dfc5 Michael Hanselmann
      assert op.status in (constants.OP_STATUS_WAITLOCK,
949 9e49dfc5 Michael Hanselmann
                           constants.OP_STATUS_CANCELING)
950 9e49dfc5 Michael Hanselmann
951 9e49dfc5 Michael Hanselmann
      # Was job cancelled while we were waiting for the lock?
952 9e49dfc5 Michael Hanselmann
      if op.status == constants.OP_STATUS_CANCELING:
953 9e49dfc5 Michael Hanselmann
        return (constants.OP_STATUS_CANCELING, None)
954 9e49dfc5 Michael Hanselmann
955 5fd6b694 Michael Hanselmann
      # Stay in waitlock while trying to re-acquire lock
956 5fd6b694 Michael Hanselmann
      return (constants.OP_STATUS_WAITLOCK, None)
957 be760ba8 Michael Hanselmann
    except CancelJob:
958 b80cc518 Michael Hanselmann
      logging.exception("%s: Canceling job", opctx.log_prefix)
959 be760ba8 Michael Hanselmann
      assert op.status == constants.OP_STATUS_CANCELING
960 be760ba8 Michael Hanselmann
      return (constants.OP_STATUS_CANCELING, None)
961 be760ba8 Michael Hanselmann
    except Exception, err: # pylint: disable-msg=W0703
962 b80cc518 Michael Hanselmann
      logging.exception("%s: Caught exception in %s",
963 b80cc518 Michael Hanselmann
                        opctx.log_prefix, opctx.summary)
964 be760ba8 Michael Hanselmann
      return (constants.OP_STATUS_ERROR, _EncodeOpError(err))
965 be760ba8 Michael Hanselmann
    else:
966 b80cc518 Michael Hanselmann
      logging.debug("%s: %s successful",
967 b80cc518 Michael Hanselmann
                    opctx.log_prefix, opctx.summary)
968 be760ba8 Michael Hanselmann
      return (constants.OP_STATUS_SUCCESS, result)
969 be760ba8 Michael Hanselmann
970 26d3fd2f Michael Hanselmann
  def __call__(self, _nextop_fn=None):
971 be760ba8 Michael Hanselmann
    """Continues execution of a job.
972 be760ba8 Michael Hanselmann

973 26d3fd2f Michael Hanselmann
    @param _nextop_fn: Callback function for tests
974 be760ba8 Michael Hanselmann
    @rtype: bool
975 be760ba8 Michael Hanselmann
    @return: True if job is finished, False if processor needs to be called
976 be760ba8 Michael Hanselmann
             again
977 be760ba8 Michael Hanselmann

978 be760ba8 Michael Hanselmann
    """
979 be760ba8 Michael Hanselmann
    queue = self.queue
980 be760ba8 Michael Hanselmann
    job = self.job
981 be760ba8 Michael Hanselmann
982 be760ba8 Michael Hanselmann
    logging.debug("Processing job %s", job.id)
983 be760ba8 Michael Hanselmann
984 be760ba8 Michael Hanselmann
    queue.acquire(shared=1)
985 be760ba8 Michael Hanselmann
    try:
986 be760ba8 Michael Hanselmann
      opcount = len(job.ops)
987 be760ba8 Michael Hanselmann
988 26d3fd2f Michael Hanselmann
      # Is a previous opcode still pending?
989 26d3fd2f Michael Hanselmann
      if job.cur_opctx:
990 26d3fd2f Michael Hanselmann
        opctx = job.cur_opctx
991 5fd6b694 Michael Hanselmann
        job.cur_opctx = None
992 26d3fd2f Michael Hanselmann
      else:
993 26d3fd2f Michael Hanselmann
        if __debug__ and _nextop_fn:
994 26d3fd2f Michael Hanselmann
          _nextop_fn()
995 26d3fd2f Michael Hanselmann
        opctx = self._FindNextOpcode(job, self._timeout_strategy_factory)
996 26d3fd2f Michael Hanselmann
997 b80cc518 Michael Hanselmann
      op = opctx.op
998 be760ba8 Michael Hanselmann
999 be760ba8 Michael Hanselmann
      # Consistency check
1000 be760ba8 Michael Hanselmann
      assert compat.all(i.status in (constants.OP_STATUS_QUEUED,
1001 30c945d0 Michael Hanselmann
                                     constants.OP_STATUS_CANCELING,
1002 be760ba8 Michael Hanselmann
                                     constants.OP_STATUS_CANCELED)
1003 5fd6b694 Michael Hanselmann
                        for i in job.ops[opctx.index + 1:])
1004 be760ba8 Michael Hanselmann
1005 be760ba8 Michael Hanselmann
      assert op.status in (constants.OP_STATUS_QUEUED,
1006 be760ba8 Michael Hanselmann
                           constants.OP_STATUS_WAITLOCK,
1007 30c945d0 Michael Hanselmann
                           constants.OP_STATUS_CANCELING,
1008 be760ba8 Michael Hanselmann
                           constants.OP_STATUS_CANCELED)
1009 be760ba8 Michael Hanselmann
1010 26d3fd2f Michael Hanselmann
      assert (op.priority <= constants.OP_PRIO_LOWEST and
1011 26d3fd2f Michael Hanselmann
              op.priority >= constants.OP_PRIO_HIGHEST)
1012 26d3fd2f Michael Hanselmann
1013 30c945d0 Michael Hanselmann
      if op.status not in (constants.OP_STATUS_CANCELING,
1014 30c945d0 Michael Hanselmann
                           constants.OP_STATUS_CANCELED):
1015 30c945d0 Michael Hanselmann
        assert op.status in (constants.OP_STATUS_QUEUED,
1016 30c945d0 Michael Hanselmann
                             constants.OP_STATUS_WAITLOCK)
1017 30c945d0 Michael Hanselmann
1018 be760ba8 Michael Hanselmann
        # Prepare to start opcode
1019 5fd6b694 Michael Hanselmann
        if self._MarkWaitlock(job, op):
1020 5fd6b694 Michael Hanselmann
          # Write to disk
1021 5fd6b694 Michael Hanselmann
          queue.UpdateJobUnlocked(job)
1022 be760ba8 Michael Hanselmann
1023 be760ba8 Michael Hanselmann
        assert op.status == constants.OP_STATUS_WAITLOCK
1024 be760ba8 Michael Hanselmann
        assert job.CalcStatus() == constants.JOB_STATUS_WAITLOCK
1025 5fd6b694 Michael Hanselmann
        assert job.start_timestamp and op.start_timestamp
1026 be760ba8 Michael Hanselmann
1027 b80cc518 Michael Hanselmann
        logging.info("%s: opcode %s waiting for locks",
1028 b80cc518 Michael Hanselmann
                     opctx.log_prefix, opctx.summary)
1029 be760ba8 Michael Hanselmann
1030 be760ba8 Michael Hanselmann
        queue.release()
1031 be760ba8 Michael Hanselmann
        try:
1032 b80cc518 Michael Hanselmann
          (op_status, op_result) = self._ExecOpCodeUnlocked(opctx)
1033 be760ba8 Michael Hanselmann
        finally:
1034 be760ba8 Michael Hanselmann
          queue.acquire(shared=1)
1035 be760ba8 Michael Hanselmann
1036 be760ba8 Michael Hanselmann
        op.status = op_status
1037 be760ba8 Michael Hanselmann
        op.result = op_result
1038 be760ba8 Michael Hanselmann
1039 5fd6b694 Michael Hanselmann
        if op.status == constants.OP_STATUS_WAITLOCK:
1040 26d3fd2f Michael Hanselmann
          # Couldn't get locks in time
1041 26d3fd2f Michael Hanselmann
          assert not op.end_timestamp
1042 be760ba8 Michael Hanselmann
        else:
1043 26d3fd2f Michael Hanselmann
          # Finalize opcode
1044 26d3fd2f Michael Hanselmann
          op.end_timestamp = TimeStampNow()
1045 be760ba8 Michael Hanselmann
1046 26d3fd2f Michael Hanselmann
          if op.status == constants.OP_STATUS_CANCELING:
1047 26d3fd2f Michael Hanselmann
            assert not compat.any(i.status != constants.OP_STATUS_CANCELING
1048 26d3fd2f Michael Hanselmann
                                  for i in job.ops[opctx.index:])
1049 26d3fd2f Michael Hanselmann
          else:
1050 26d3fd2f Michael Hanselmann
            assert op.status in constants.OPS_FINALIZED
1051 be760ba8 Michael Hanselmann
1052 5fd6b694 Michael Hanselmann
      if op.status == constants.OP_STATUS_WAITLOCK:
1053 be760ba8 Michael Hanselmann
        finalize = False
1054 be760ba8 Michael Hanselmann
1055 5fd6b694 Michael Hanselmann
        if opctx.CheckPriorityIncrease():
1056 5fd6b694 Michael Hanselmann
          # Priority was changed, need to update on-disk file
1057 5fd6b694 Michael Hanselmann
          queue.UpdateJobUnlocked(job)
1058 be760ba8 Michael Hanselmann
1059 26d3fd2f Michael Hanselmann
        # Keep around for another round
1060 26d3fd2f Michael Hanselmann
        job.cur_opctx = opctx
1061 be760ba8 Michael Hanselmann
1062 26d3fd2f Michael Hanselmann
        assert (op.priority <= constants.OP_PRIO_LOWEST and
1063 26d3fd2f Michael Hanselmann
                op.priority >= constants.OP_PRIO_HIGHEST)
1064 be760ba8 Michael Hanselmann
1065 26d3fd2f Michael Hanselmann
        # In no case must the status be finalized here
1066 5fd6b694 Michael Hanselmann
        assert job.CalcStatus() == constants.JOB_STATUS_WAITLOCK
1067 be760ba8 Michael Hanselmann
1068 be760ba8 Michael Hanselmann
      else:
1069 26d3fd2f Michael Hanselmann
        # Ensure all opcodes so far have been successful
1070 26d3fd2f Michael Hanselmann
        assert (opctx.index == 0 or
1071 26d3fd2f Michael Hanselmann
                compat.all(i.status == constants.OP_STATUS_SUCCESS
1072 26d3fd2f Michael Hanselmann
                           for i in job.ops[:opctx.index]))
1073 26d3fd2f Michael Hanselmann
1074 26d3fd2f Michael Hanselmann
        # Reset context
1075 26d3fd2f Michael Hanselmann
        job.cur_opctx = None
1076 26d3fd2f Michael Hanselmann
1077 26d3fd2f Michael Hanselmann
        if op.status == constants.OP_STATUS_SUCCESS:
1078 26d3fd2f Michael Hanselmann
          finalize = False
1079 26d3fd2f Michael Hanselmann
1080 26d3fd2f Michael Hanselmann
        elif op.status == constants.OP_STATUS_ERROR:
1081 26d3fd2f Michael Hanselmann
          # Ensure failed opcode has an exception as its result
1082 26d3fd2f Michael Hanselmann
          assert errors.GetEncodedError(job.ops[opctx.index].result)
1083 26d3fd2f Michael Hanselmann
1084 26d3fd2f Michael Hanselmann
          to_encode = errors.OpExecError("Preceding opcode failed")
1085 26d3fd2f Michael Hanselmann
          job.MarkUnfinishedOps(constants.OP_STATUS_ERROR,
1086 26d3fd2f Michael Hanselmann
                                _EncodeOpError(to_encode))
1087 26d3fd2f Michael Hanselmann
          finalize = True
1088 be760ba8 Michael Hanselmann
1089 26d3fd2f Michael Hanselmann
          # Consistency check
1090 26d3fd2f Michael Hanselmann
          assert compat.all(i.status == constants.OP_STATUS_ERROR and
1091 26d3fd2f Michael Hanselmann
                            errors.GetEncodedError(i.result)
1092 26d3fd2f Michael Hanselmann
                            for i in job.ops[opctx.index:])
1093 be760ba8 Michael Hanselmann
1094 26d3fd2f Michael Hanselmann
        elif op.status == constants.OP_STATUS_CANCELING:
1095 26d3fd2f Michael Hanselmann
          job.MarkUnfinishedOps(constants.OP_STATUS_CANCELED,
1096 26d3fd2f Michael Hanselmann
                                "Job canceled by request")
1097 26d3fd2f Michael Hanselmann
          finalize = True
1098 26d3fd2f Michael Hanselmann
1099 26d3fd2f Michael Hanselmann
        elif op.status == constants.OP_STATUS_CANCELED:
1100 26d3fd2f Michael Hanselmann
          finalize = True
1101 26d3fd2f Michael Hanselmann
1102 26d3fd2f Michael Hanselmann
        else:
1103 26d3fd2f Michael Hanselmann
          raise errors.ProgrammerError("Unknown status '%s'" % op.status)
1104 26d3fd2f Michael Hanselmann
1105 26d3fd2f Michael Hanselmann
        # Finalizing or last opcode?
1106 26d3fd2f Michael Hanselmann
        if finalize or opctx.index == (opcount - 1):
1107 26d3fd2f Michael Hanselmann
          # All opcodes have been run, finalize job
1108 26d3fd2f Michael Hanselmann
          job.end_timestamp = TimeStampNow()
1109 26d3fd2f Michael Hanselmann
1110 26d3fd2f Michael Hanselmann
        # Write to disk. If the job status is final, this is the final write
1111 26d3fd2f Michael Hanselmann
        # allowed. Once the file has been written, it can be archived anytime.
1112 26d3fd2f Michael Hanselmann
        queue.UpdateJobUnlocked(job)
1113 be760ba8 Michael Hanselmann
1114 26d3fd2f Michael Hanselmann
        if finalize or opctx.index == (opcount - 1):
1115 26d3fd2f Michael Hanselmann
          logging.info("Finished job %s, status = %s", job.id, job.CalcStatus())
1116 26d3fd2f Michael Hanselmann
          return True
1117 be760ba8 Michael Hanselmann
1118 be760ba8 Michael Hanselmann
      return False
1119 be760ba8 Michael Hanselmann
    finally:
1120 be760ba8 Michael Hanselmann
      queue.release()
1121 be760ba8 Michael Hanselmann
1122 be760ba8 Michael Hanselmann
1123 031a3e57 Michael Hanselmann
class _JobQueueWorker(workerpool.BaseWorker):
1124 031a3e57 Michael Hanselmann
  """The actual job workers.
1125 031a3e57 Michael Hanselmann

1126 031a3e57 Michael Hanselmann
  """
1127 7260cfbe Iustin Pop
  def RunTask(self, job): # pylint: disable-msg=W0221
1128 e2715f69 Michael Hanselmann
    """Job executor.
1129 e2715f69 Michael Hanselmann

1130 be760ba8 Michael Hanselmann
    This functions processes a job. It is closely tied to the L{_QueuedJob} and
1131 be760ba8 Michael Hanselmann
    L{_QueuedOpCode} classes.
1132 e2715f69 Michael Hanselmann

1133 ea03467c Iustin Pop
    @type job: L{_QueuedJob}
1134 ea03467c Iustin Pop
    @param job: the job to be processed
1135 ea03467c Iustin Pop

1136 e2715f69 Michael Hanselmann
    """
1137 be760ba8 Michael Hanselmann
    queue = job.queue
1138 be760ba8 Michael Hanselmann
    assert queue == self.pool.queue
1139 be760ba8 Michael Hanselmann
1140 daba67c7 Michael Hanselmann
    self.SetTaskName("Job%s" % job.id)
1141 daba67c7 Michael Hanselmann
1142 be760ba8 Michael Hanselmann
    proc = mcpu.Processor(queue.context, job.id)
1143 be760ba8 Michael Hanselmann
1144 be760ba8 Michael Hanselmann
    if not _JobProcessor(queue, proc.ExecOpCode, job)():
1145 be760ba8 Michael Hanselmann
      # Schedule again
1146 26d3fd2f Michael Hanselmann
      raise workerpool.DeferTask(priority=job.CalcPriority())
1147 e2715f69 Michael Hanselmann
1148 e2715f69 Michael Hanselmann
1149 e2715f69 Michael Hanselmann
class _JobQueueWorkerPool(workerpool.WorkerPool):
1150 ea03467c Iustin Pop
  """Simple class implementing a job-processing workerpool.
1151 ea03467c Iustin Pop

1152 ea03467c Iustin Pop
  """
1153 5bdce580 Michael Hanselmann
  def __init__(self, queue):
1154 89e2b4d2 Michael Hanselmann
    super(_JobQueueWorkerPool, self).__init__("JobQueue",
1155 89e2b4d2 Michael Hanselmann
                                              JOBQUEUE_THREADS,
1156 e2715f69 Michael Hanselmann
                                              _JobQueueWorker)
1157 5bdce580 Michael Hanselmann
    self.queue = queue
1158 e2715f69 Michael Hanselmann
1159 e2715f69 Michael Hanselmann
1160 6c881c52 Iustin Pop
def _RequireOpenQueue(fn):
1161 6c881c52 Iustin Pop
  """Decorator for "public" functions.
1162 ea03467c Iustin Pop

1163 6c881c52 Iustin Pop
  This function should be used for all 'public' functions. That is,
1164 6c881c52 Iustin Pop
  functions usually called from other classes. Note that this should
1165 6c881c52 Iustin Pop
  be applied only to methods (not plain functions), since it expects
1166 6c881c52 Iustin Pop
  that the decorated function is called with a first argument that has
1167 a71f9c7d Guido Trotter
  a '_queue_filelock' argument.
1168 ea03467c Iustin Pop

1169 99bd4f0a Guido Trotter
  @warning: Use this decorator only after locking.ssynchronized
1170 f1da30e6 Michael Hanselmann

1171 6c881c52 Iustin Pop
  Example::
1172 ebb80afa Guido Trotter
    @locking.ssynchronized(_LOCK)
1173 6c881c52 Iustin Pop
    @_RequireOpenQueue
1174 6c881c52 Iustin Pop
    def Example(self):
1175 6c881c52 Iustin Pop
      pass
1176 db37da70 Michael Hanselmann

1177 6c881c52 Iustin Pop
  """
1178 6c881c52 Iustin Pop
  def wrapper(self, *args, **kwargs):
1179 7260cfbe Iustin Pop
    # pylint: disable-msg=W0212
1180 a71f9c7d Guido Trotter
    assert self._queue_filelock is not None, "Queue should be open"
1181 6c881c52 Iustin Pop
    return fn(self, *args, **kwargs)
1182 6c881c52 Iustin Pop
  return wrapper
1183 db37da70 Michael Hanselmann
1184 db37da70 Michael Hanselmann
1185 6c881c52 Iustin Pop
class JobQueue(object):
1186 6c881c52 Iustin Pop
  """Queue used to manage the jobs.
1187 db37da70 Michael Hanselmann

1188 6c881c52 Iustin Pop
  @cvar _RE_JOB_FILE: regex matching the valid job file names
1189 6c881c52 Iustin Pop

1190 6c881c52 Iustin Pop
  """
1191 6c881c52 Iustin Pop
  _RE_JOB_FILE = re.compile(r"^job-(%s)$" % constants.JOB_ID_TEMPLATE)
1192 db37da70 Michael Hanselmann
1193 85f03e0d Michael Hanselmann
  def __init__(self, context):
1194 ea03467c Iustin Pop
    """Constructor for JobQueue.
1195 ea03467c Iustin Pop

1196 ea03467c Iustin Pop
    The constructor will initialize the job queue object and then
1197 ea03467c Iustin Pop
    start loading the current jobs from disk, either for starting them
1198 ea03467c Iustin Pop
    (if they were queue) or for aborting them (if they were already
1199 ea03467c Iustin Pop
    running).
1200 ea03467c Iustin Pop

1201 ea03467c Iustin Pop
    @type context: GanetiContext
1202 ea03467c Iustin Pop
    @param context: the context object for access to the configuration
1203 ea03467c Iustin Pop
        data and other ganeti objects
1204 ea03467c Iustin Pop

1205 ea03467c Iustin Pop
    """
1206 5bdce580 Michael Hanselmann
    self.context = context
1207 5685c1a5 Michael Hanselmann
    self._memcache = weakref.WeakValueDictionary()
1208 b705c7a6 Manuel Franceschini
    self._my_hostname = netutils.Hostname.GetSysName()
1209 f1da30e6 Michael Hanselmann
1210 ebb80afa Guido Trotter
    # The Big JobQueue lock. If a code block or method acquires it in shared
1211 ebb80afa Guido Trotter
    # mode safe it must guarantee concurrency with all the code acquiring it in
1212 ebb80afa Guido Trotter
    # shared mode, including itself. In order not to acquire it at all
1213 ebb80afa Guido Trotter
    # concurrency must be guaranteed with all code acquiring it in shared mode
1214 ebb80afa Guido Trotter
    # and all code acquiring it exclusively.
1215 7f93570a Iustin Pop
    self._lock = locking.SharedLock("JobQueue")
1216 ebb80afa Guido Trotter
1217 ebb80afa Guido Trotter
    self.acquire = self._lock.acquire
1218 ebb80afa Guido Trotter
    self.release = self._lock.release
1219 85f03e0d Michael Hanselmann
1220 a71f9c7d Guido Trotter
    # Initialize the queue, and acquire the filelock.
1221 a71f9c7d Guido Trotter
    # This ensures no other process is working on the job queue.
1222 a71f9c7d Guido Trotter
    self._queue_filelock = jstore.InitAndVerifyQueue(must_lock=True)
1223 f1da30e6 Michael Hanselmann
1224 04ab05ce Michael Hanselmann
    # Read serial file
1225 04ab05ce Michael Hanselmann
    self._last_serial = jstore.ReadSerial()
1226 04ab05ce Michael Hanselmann
    assert self._last_serial is not None, ("Serial file was modified between"
1227 04ab05ce Michael Hanselmann
                                           " check in jstore and here")
1228 c4beba1c Iustin Pop
1229 23752136 Michael Hanselmann
    # Get initial list of nodes
1230 99aabbed Iustin Pop
    self._nodes = dict((n.name, n.primary_ip)
1231 59303563 Iustin Pop
                       for n in self.context.cfg.GetAllNodesInfo().values()
1232 59303563 Iustin Pop
                       if n.master_candidate)
1233 8e00939c Michael Hanselmann
1234 8e00939c Michael Hanselmann
    # Remove master node
1235 d8e0dc17 Guido Trotter
    self._nodes.pop(self._my_hostname, None)
1236 23752136 Michael Hanselmann
1237 23752136 Michael Hanselmann
    # TODO: Check consistency across nodes
1238 23752136 Michael Hanselmann
1239 20571a26 Guido Trotter
    self._queue_size = 0
1240 20571a26 Guido Trotter
    self._UpdateQueueSizeUnlocked()
1241 ff699aa9 Michael Hanselmann
    self._drained = jstore.CheckDrainFlag()
1242 20571a26 Guido Trotter
1243 85f03e0d Michael Hanselmann
    # Setup worker pool
1244 5bdce580 Michael Hanselmann
    self._wpool = _JobQueueWorkerPool(self)
1245 85f03e0d Michael Hanselmann
    try:
1246 de9d02c7 Michael Hanselmann
      self._InspectQueue()
1247 de9d02c7 Michael Hanselmann
    except:
1248 de9d02c7 Michael Hanselmann
      self._wpool.TerminateWorkers()
1249 de9d02c7 Michael Hanselmann
      raise
1250 711b5124 Michael Hanselmann
1251 de9d02c7 Michael Hanselmann
  @locking.ssynchronized(_LOCK)
1252 de9d02c7 Michael Hanselmann
  @_RequireOpenQueue
1253 de9d02c7 Michael Hanselmann
  def _InspectQueue(self):
1254 de9d02c7 Michael Hanselmann
    """Loads the whole job queue and resumes unfinished jobs.
1255 de9d02c7 Michael Hanselmann

1256 de9d02c7 Michael Hanselmann
    This function needs the lock here because WorkerPool.AddTask() may start a
1257 de9d02c7 Michael Hanselmann
    job while we're still doing our work.
1258 711b5124 Michael Hanselmann

1259 de9d02c7 Michael Hanselmann
    """
1260 de9d02c7 Michael Hanselmann
    logging.info("Inspecting job queue")
1261 de9d02c7 Michael Hanselmann
1262 7b5c4a69 Michael Hanselmann
    restartjobs = []
1263 7b5c4a69 Michael Hanselmann
1264 de9d02c7 Michael Hanselmann
    all_job_ids = self._GetJobIDsUnlocked()
1265 de9d02c7 Michael Hanselmann
    jobs_count = len(all_job_ids)
1266 de9d02c7 Michael Hanselmann
    lastinfo = time.time()
1267 de9d02c7 Michael Hanselmann
    for idx, job_id in enumerate(all_job_ids):
1268 de9d02c7 Michael Hanselmann
      # Give an update every 1000 jobs or 10 seconds
1269 de9d02c7 Michael Hanselmann
      if (idx % 1000 == 0 or time.time() >= (lastinfo + 10.0) or
1270 de9d02c7 Michael Hanselmann
          idx == (jobs_count - 1)):
1271 de9d02c7 Michael Hanselmann
        logging.info("Job queue inspection: %d/%d (%0.1f %%)",
1272 de9d02c7 Michael Hanselmann
                     idx, jobs_count - 1, 100.0 * (idx + 1) / jobs_count)
1273 711b5124 Michael Hanselmann
        lastinfo = time.time()
1274 94ed59a5 Iustin Pop
1275 de9d02c7 Michael Hanselmann
      job = self._LoadJobUnlocked(job_id)
1276 85f03e0d Michael Hanselmann
1277 de9d02c7 Michael Hanselmann
      # a failure in loading the job can cause 'None' to be returned
1278 de9d02c7 Michael Hanselmann
      if job is None:
1279 de9d02c7 Michael Hanselmann
        continue
1280 85f03e0d Michael Hanselmann
1281 de9d02c7 Michael Hanselmann
      status = job.CalcStatus()
1282 711b5124 Michael Hanselmann
1283 320d1daf Michael Hanselmann
      if status == constants.JOB_STATUS_QUEUED:
1284 7b5c4a69 Michael Hanselmann
        restartjobs.append(job)
1285 de9d02c7 Michael Hanselmann
1286 de9d02c7 Michael Hanselmann
      elif status in (constants.JOB_STATUS_RUNNING,
1287 5ef699a0 Michael Hanselmann
                      constants.JOB_STATUS_WAITLOCK,
1288 de9d02c7 Michael Hanselmann
                      constants.JOB_STATUS_CANCELING):
1289 de9d02c7 Michael Hanselmann
        logging.warning("Unfinished job %s found: %s", job.id, job)
1290 320d1daf Michael Hanselmann
1291 320d1daf Michael Hanselmann
        if status == constants.JOB_STATUS_WAITLOCK:
1292 320d1daf Michael Hanselmann
          # Restart job
1293 320d1daf Michael Hanselmann
          job.MarkUnfinishedOps(constants.OP_STATUS_QUEUED, None)
1294 320d1daf Michael Hanselmann
          restartjobs.append(job)
1295 320d1daf Michael Hanselmann
        else:
1296 320d1daf Michael Hanselmann
          job.MarkUnfinishedOps(constants.OP_STATUS_ERROR,
1297 320d1daf Michael Hanselmann
                                "Unclean master daemon shutdown")
1298 320d1daf Michael Hanselmann
1299 de9d02c7 Michael Hanselmann
        self.UpdateJobUnlocked(job)
1300 de9d02c7 Michael Hanselmann
1301 7b5c4a69 Michael Hanselmann
    if restartjobs:
1302 7b5c4a69 Michael Hanselmann
      logging.info("Restarting %s jobs", len(restartjobs))
1303 7b5c4a69 Michael Hanselmann
      self._EnqueueJobs(restartjobs)
1304 7b5c4a69 Michael Hanselmann
1305 de9d02c7 Michael Hanselmann
    logging.info("Job queue inspection finished")
1306 85f03e0d Michael Hanselmann
1307 ebb80afa Guido Trotter
  @locking.ssynchronized(_LOCK)
1308 d2e03a33 Michael Hanselmann
  @_RequireOpenQueue
1309 99aabbed Iustin Pop
  def AddNode(self, node):
1310 99aabbed Iustin Pop
    """Register a new node with the queue.
1311 99aabbed Iustin Pop

1312 99aabbed Iustin Pop
    @type node: L{objects.Node}
1313 99aabbed Iustin Pop
    @param node: the node object to be added
1314 99aabbed Iustin Pop

1315 99aabbed Iustin Pop
    """
1316 99aabbed Iustin Pop
    node_name = node.name
1317 d2e03a33 Michael Hanselmann
    assert node_name != self._my_hostname
1318 23752136 Michael Hanselmann
1319 9f774ee8 Michael Hanselmann
    # Clean queue directory on added node
1320 c8457ce7 Iustin Pop
    result = rpc.RpcRunner.call_jobqueue_purge(node_name)
1321 3cebe102 Michael Hanselmann
    msg = result.fail_msg
1322 c8457ce7 Iustin Pop
    if msg:
1323 c8457ce7 Iustin Pop
      logging.warning("Cannot cleanup queue directory on node %s: %s",
1324 c8457ce7 Iustin Pop
                      node_name, msg)
1325 23752136 Michael Hanselmann
1326 59303563 Iustin Pop
    if not node.master_candidate:
1327 59303563 Iustin Pop
      # remove if existing, ignoring errors
1328 59303563 Iustin Pop
      self._nodes.pop(node_name, None)
1329 59303563 Iustin Pop
      # and skip the replication of the job ids
1330 59303563 Iustin Pop
      return
1331 59303563 Iustin Pop
1332 d2e03a33 Michael Hanselmann
    # Upload the whole queue excluding archived jobs
1333 d2e03a33 Michael Hanselmann
    files = [self._GetJobPath(job_id) for job_id in self._GetJobIDsUnlocked()]
1334 23752136 Michael Hanselmann
1335 d2e03a33 Michael Hanselmann
    # Upload current serial file
1336 d2e03a33 Michael Hanselmann
    files.append(constants.JOB_QUEUE_SERIAL_FILE)
1337 d2e03a33 Michael Hanselmann
1338 d2e03a33 Michael Hanselmann
    for file_name in files:
1339 9f774ee8 Michael Hanselmann
      # Read file content
1340 13998ef2 Michael Hanselmann
      content = utils.ReadFile(file_name)
1341 9f774ee8 Michael Hanselmann
1342 a3811745 Michael Hanselmann
      result = rpc.RpcRunner.call_jobqueue_update([node_name],
1343 a3811745 Michael Hanselmann
                                                  [node.primary_ip],
1344 a3811745 Michael Hanselmann
                                                  file_name, content)
1345 3cebe102 Michael Hanselmann
      msg = result[node_name].fail_msg
1346 c8457ce7 Iustin Pop
      if msg:
1347 c8457ce7 Iustin Pop
        logging.error("Failed to upload file %s to node %s: %s",
1348 c8457ce7 Iustin Pop
                      file_name, node_name, msg)
1349 d2e03a33 Michael Hanselmann
1350 99aabbed Iustin Pop
    self._nodes[node_name] = node.primary_ip
1351 d2e03a33 Michael Hanselmann
1352 ebb80afa Guido Trotter
  @locking.ssynchronized(_LOCK)
1353 d2e03a33 Michael Hanselmann
  @_RequireOpenQueue
1354 d2e03a33 Michael Hanselmann
  def RemoveNode(self, node_name):
1355 ea03467c Iustin Pop
    """Callback called when removing nodes from the cluster.
1356 ea03467c Iustin Pop

1357 ea03467c Iustin Pop
    @type node_name: str
1358 ea03467c Iustin Pop
    @param node_name: the name of the node to remove
1359 ea03467c Iustin Pop

1360 ea03467c Iustin Pop
    """
1361 d8e0dc17 Guido Trotter
    self._nodes.pop(node_name, None)
1362 23752136 Michael Hanselmann
1363 7e950d31 Iustin Pop
  @staticmethod
1364 7e950d31 Iustin Pop
  def _CheckRpcResult(result, nodes, failmsg):
1365 ea03467c Iustin Pop
    """Verifies the status of an RPC call.
1366 ea03467c Iustin Pop

1367 ea03467c Iustin Pop
    Since we aim to keep consistency should this node (the current
1368 ea03467c Iustin Pop
    master) fail, we will log errors if our rpc fail, and especially
1369 5bbd3f7f Michael Hanselmann
    log the case when more than half of the nodes fails.
1370 ea03467c Iustin Pop

1371 ea03467c Iustin Pop
    @param result: the data as returned from the rpc call
1372 ea03467c Iustin Pop
    @type nodes: list
1373 ea03467c Iustin Pop
    @param nodes: the list of nodes we made the call to
1374 ea03467c Iustin Pop
    @type failmsg: str
1375 ea03467c Iustin Pop
    @param failmsg: the identifier to be used for logging
1376 ea03467c Iustin Pop

1377 ea03467c Iustin Pop
    """
1378 e74798c1 Michael Hanselmann
    failed = []
1379 e74798c1 Michael Hanselmann
    success = []
1380 e74798c1 Michael Hanselmann
1381 e74798c1 Michael Hanselmann
    for node in nodes:
1382 3cebe102 Michael Hanselmann
      msg = result[node].fail_msg
1383 c8457ce7 Iustin Pop
      if msg:
1384 e74798c1 Michael Hanselmann
        failed.append(node)
1385 45e0d704 Iustin Pop
        logging.error("RPC call %s (%s) failed on node %s: %s",
1386 45e0d704 Iustin Pop
                      result[node].call, failmsg, node, msg)
1387 c8457ce7 Iustin Pop
      else:
1388 c8457ce7 Iustin Pop
        success.append(node)
1389 e74798c1 Michael Hanselmann
1390 e74798c1 Michael Hanselmann
    # +1 for the master node
1391 e74798c1 Michael Hanselmann
    if (len(success) + 1) < len(failed):
1392 e74798c1 Michael Hanselmann
      # TODO: Handle failing nodes
1393 e74798c1 Michael Hanselmann
      logging.error("More than half of the nodes failed")
1394 e74798c1 Michael Hanselmann
1395 99aabbed Iustin Pop
  def _GetNodeIp(self):
1396 99aabbed Iustin Pop
    """Helper for returning the node name/ip list.
1397 99aabbed Iustin Pop

1398 ea03467c Iustin Pop
    @rtype: (list, list)
1399 ea03467c Iustin Pop
    @return: a tuple of two lists, the first one with the node
1400 ea03467c Iustin Pop
        names and the second one with the node addresses
1401 ea03467c Iustin Pop

1402 99aabbed Iustin Pop
    """
1403 e35344b4 Michael Hanselmann
    # TODO: Change to "tuple(map(list, zip(*self._nodes.items())))"?
1404 99aabbed Iustin Pop
    name_list = self._nodes.keys()
1405 99aabbed Iustin Pop
    addr_list = [self._nodes[name] for name in name_list]
1406 99aabbed Iustin Pop
    return name_list, addr_list
1407 99aabbed Iustin Pop
1408 4c36bdf5 Guido Trotter
  def _UpdateJobQueueFile(self, file_name, data, replicate):
1409 8e00939c Michael Hanselmann
    """Writes a file locally and then replicates it to all nodes.
1410 8e00939c Michael Hanselmann

1411 ea03467c Iustin Pop
    This function will replace the contents of a file on the local
1412 ea03467c Iustin Pop
    node and then replicate it to all the other nodes we have.
1413 ea03467c Iustin Pop

1414 ea03467c Iustin Pop
    @type file_name: str
1415 ea03467c Iustin Pop
    @param file_name: the path of the file to be replicated
1416 ea03467c Iustin Pop
    @type data: str
1417 ea03467c Iustin Pop
    @param data: the new contents of the file
1418 4c36bdf5 Guido Trotter
    @type replicate: boolean
1419 4c36bdf5 Guido Trotter
    @param replicate: whether to spread the changes to the remote nodes
1420 ea03467c Iustin Pop

1421 8e00939c Michael Hanselmann
    """
1422 82b22e19 René Nussbaumer
    getents = runtime.GetEnts()
1423 82b22e19 René Nussbaumer
    utils.WriteFile(file_name, data=data, uid=getents.masterd_uid,
1424 82b22e19 René Nussbaumer
                    gid=getents.masterd_gid)
1425 8e00939c Michael Hanselmann
1426 4c36bdf5 Guido Trotter
    if replicate:
1427 4c36bdf5 Guido Trotter
      names, addrs = self._GetNodeIp()
1428 4c36bdf5 Guido Trotter
      result = rpc.RpcRunner.call_jobqueue_update(names, addrs, file_name, data)
1429 4c36bdf5 Guido Trotter
      self._CheckRpcResult(result, self._nodes, "Updating %s" % file_name)
1430 23752136 Michael Hanselmann
1431 d7fd1f28 Michael Hanselmann
  def _RenameFilesUnlocked(self, rename):
1432 ea03467c Iustin Pop
    """Renames a file locally and then replicate the change.
1433 ea03467c Iustin Pop

1434 ea03467c Iustin Pop
    This function will rename a file in the local queue directory
1435 ea03467c Iustin Pop
    and then replicate this rename to all the other nodes we have.
1436 ea03467c Iustin Pop

1437 d7fd1f28 Michael Hanselmann
    @type rename: list of (old, new)
1438 d7fd1f28 Michael Hanselmann
    @param rename: List containing tuples mapping old to new names
1439 ea03467c Iustin Pop

1440 ea03467c Iustin Pop
    """
1441 dd875d32 Michael Hanselmann
    # Rename them locally
1442 d7fd1f28 Michael Hanselmann
    for old, new in rename:
1443 d7fd1f28 Michael Hanselmann
      utils.RenameFile(old, new, mkdir=True)
1444 abc1f2ce Michael Hanselmann
1445 dd875d32 Michael Hanselmann
    # ... and on all nodes
1446 dd875d32 Michael Hanselmann
    names, addrs = self._GetNodeIp()
1447 dd875d32 Michael Hanselmann
    result = rpc.RpcRunner.call_jobqueue_rename(names, addrs, rename)
1448 dd875d32 Michael Hanselmann
    self._CheckRpcResult(result, self._nodes, "Renaming files (%r)" % rename)
1449 abc1f2ce Michael Hanselmann
1450 7e950d31 Iustin Pop
  @staticmethod
1451 7e950d31 Iustin Pop
  def _FormatJobID(job_id):
1452 ea03467c Iustin Pop
    """Convert a job ID to string format.
1453 ea03467c Iustin Pop

1454 ea03467c Iustin Pop
    Currently this just does C{str(job_id)} after performing some
1455 ea03467c Iustin Pop
    checks, but if we want to change the job id format this will
1456 ea03467c Iustin Pop
    abstract this change.
1457 ea03467c Iustin Pop

1458 ea03467c Iustin Pop
    @type job_id: int or long
1459 ea03467c Iustin Pop
    @param job_id: the numeric job id
1460 ea03467c Iustin Pop
    @rtype: str
1461 ea03467c Iustin Pop
    @return: the formatted job id
1462 ea03467c Iustin Pop

1463 ea03467c Iustin Pop
    """
1464 85f03e0d Michael Hanselmann
    if not isinstance(job_id, (int, long)):
1465 85f03e0d Michael Hanselmann
      raise errors.ProgrammerError("Job ID '%s' not numeric" % job_id)
1466 85f03e0d Michael Hanselmann
    if job_id < 0:
1467 85f03e0d Michael Hanselmann
      raise errors.ProgrammerError("Job ID %s is negative" % job_id)
1468 85f03e0d Michael Hanselmann
1469 85f03e0d Michael Hanselmann
    return str(job_id)
1470 85f03e0d Michael Hanselmann
1471 58b22b6e Michael Hanselmann
  @classmethod
1472 58b22b6e Michael Hanselmann
  def _GetArchiveDirectory(cls, job_id):
1473 58b22b6e Michael Hanselmann
    """Returns the archive directory for a job.
1474 58b22b6e Michael Hanselmann

1475 58b22b6e Michael Hanselmann
    @type job_id: str
1476 58b22b6e Michael Hanselmann
    @param job_id: Job identifier
1477 58b22b6e Michael Hanselmann
    @rtype: str
1478 58b22b6e Michael Hanselmann
    @return: Directory name
1479 58b22b6e Michael Hanselmann

1480 58b22b6e Michael Hanselmann
    """
1481 58b22b6e Michael Hanselmann
    return str(int(job_id) / JOBS_PER_ARCHIVE_DIRECTORY)
1482 58b22b6e Michael Hanselmann
1483 009e73d0 Iustin Pop
  def _NewSerialsUnlocked(self, count):
1484 f1da30e6 Michael Hanselmann
    """Generates a new job identifier.
1485 f1da30e6 Michael Hanselmann

1486 f1da30e6 Michael Hanselmann
    Job identifiers are unique during the lifetime of a cluster.
1487 f1da30e6 Michael Hanselmann

1488 009e73d0 Iustin Pop
    @type count: integer
1489 009e73d0 Iustin Pop
    @param count: how many serials to return
1490 ea03467c Iustin Pop
    @rtype: str
1491 ea03467c Iustin Pop
    @return: a string representing the job identifier.
1492 f1da30e6 Michael Hanselmann

1493 f1da30e6 Michael Hanselmann
    """
1494 009e73d0 Iustin Pop
    assert count > 0
1495 f1da30e6 Michael Hanselmann
    # New number
1496 009e73d0 Iustin Pop
    serial = self._last_serial + count
1497 f1da30e6 Michael Hanselmann
1498 f1da30e6 Michael Hanselmann
    # Write to file
1499 4c36bdf5 Guido Trotter
    self._UpdateJobQueueFile(constants.JOB_QUEUE_SERIAL_FILE,
1500 4c36bdf5 Guido Trotter
                             "%s\n" % serial, True)
1501 f1da30e6 Michael Hanselmann
1502 009e73d0 Iustin Pop
    result = [self._FormatJobID(v)
1503 009e73d0 Iustin Pop
              for v in range(self._last_serial, serial + 1)]
1504 f1da30e6 Michael Hanselmann
    # Keep it only if we were able to write the file
1505 f1da30e6 Michael Hanselmann
    self._last_serial = serial
1506 f1da30e6 Michael Hanselmann
1507 009e73d0 Iustin Pop
    return result
1508 f1da30e6 Michael Hanselmann
1509 85f03e0d Michael Hanselmann
  @staticmethod
1510 85f03e0d Michael Hanselmann
  def _GetJobPath(job_id):
1511 ea03467c Iustin Pop
    """Returns the job file for a given job id.
1512 ea03467c Iustin Pop

1513 ea03467c Iustin Pop
    @type job_id: str
1514 ea03467c Iustin Pop
    @param job_id: the job identifier
1515 ea03467c Iustin Pop
    @rtype: str
1516 ea03467c Iustin Pop
    @return: the path to the job file
1517 ea03467c Iustin Pop

1518 ea03467c Iustin Pop
    """
1519 c4feafe8 Iustin Pop
    return utils.PathJoin(constants.QUEUE_DIR, "job-%s" % job_id)
1520 f1da30e6 Michael Hanselmann
1521 58b22b6e Michael Hanselmann
  @classmethod
1522 58b22b6e Michael Hanselmann
  def _GetArchivedJobPath(cls, job_id):
1523 ea03467c Iustin Pop
    """Returns the archived job file for a give job id.
1524 ea03467c Iustin Pop

1525 ea03467c Iustin Pop
    @type job_id: str
1526 ea03467c Iustin Pop
    @param job_id: the job identifier
1527 ea03467c Iustin Pop
    @rtype: str
1528 ea03467c Iustin Pop
    @return: the path to the archived job file
1529 ea03467c Iustin Pop

1530 ea03467c Iustin Pop
    """
1531 0411c011 Iustin Pop
    return utils.PathJoin(constants.JOB_QUEUE_ARCHIVE_DIR,
1532 0411c011 Iustin Pop
                          cls._GetArchiveDirectory(job_id), "job-%s" % job_id)
1533 0cb94105 Michael Hanselmann
1534 85a1c57d Guido Trotter
  def _GetJobIDsUnlocked(self, sort=True):
1535 911a495b Iustin Pop
    """Return all known job IDs.
1536 911a495b Iustin Pop

1537 ac0930b9 Iustin Pop
    The method only looks at disk because it's a requirement that all
1538 ac0930b9 Iustin Pop
    jobs are present on disk (so in the _memcache we don't have any
1539 ac0930b9 Iustin Pop
    extra IDs).
1540 ac0930b9 Iustin Pop

1541 85a1c57d Guido Trotter
    @type sort: boolean
1542 85a1c57d Guido Trotter
    @param sort: perform sorting on the returned job ids
1543 ea03467c Iustin Pop
    @rtype: list
1544 ea03467c Iustin Pop
    @return: the list of job IDs
1545 ea03467c Iustin Pop

1546 911a495b Iustin Pop
    """
1547 85a1c57d Guido Trotter
    jlist = []
1548 b5b8309d Guido Trotter
    for filename in utils.ListVisibleFiles(constants.QUEUE_DIR):
1549 85a1c57d Guido Trotter
      m = self._RE_JOB_FILE.match(filename)
1550 85a1c57d Guido Trotter
      if m:
1551 85a1c57d Guido Trotter
        jlist.append(m.group(1))
1552 85a1c57d Guido Trotter
    if sort:
1553 85a1c57d Guido Trotter
      jlist = utils.NiceSort(jlist)
1554 f0d874fe Iustin Pop
    return jlist
1555 911a495b Iustin Pop
1556 911a495b Iustin Pop
  def _LoadJobUnlocked(self, job_id):
1557 ea03467c Iustin Pop
    """Loads a job from the disk or memory.
1558 ea03467c Iustin Pop

1559 ea03467c Iustin Pop
    Given a job id, this will return the cached job object if
1560 ea03467c Iustin Pop
    existing, or try to load the job from the disk. If loading from
1561 ea03467c Iustin Pop
    disk, it will also add the job to the cache.
1562 ea03467c Iustin Pop

1563 ea03467c Iustin Pop
    @param job_id: the job id
1564 ea03467c Iustin Pop
    @rtype: L{_QueuedJob} or None
1565 ea03467c Iustin Pop
    @return: either None or the job object
1566 ea03467c Iustin Pop

1567 ea03467c Iustin Pop
    """
1568 5685c1a5 Michael Hanselmann
    job = self._memcache.get(job_id, None)
1569 5685c1a5 Michael Hanselmann
    if job:
1570 205d71fd Michael Hanselmann
      logging.debug("Found job %s in memcache", job_id)
1571 5685c1a5 Michael Hanselmann
      return job
1572 ac0930b9 Iustin Pop
1573 3d6c5566 Guido Trotter
    try:
1574 3d6c5566 Guido Trotter
      job = self._LoadJobFromDisk(job_id)
1575 aa9f8167 Iustin Pop
      if job is None:
1576 aa9f8167 Iustin Pop
        return job
1577 3d6c5566 Guido Trotter
    except errors.JobFileCorrupted:
1578 3d6c5566 Guido Trotter
      old_path = self._GetJobPath(job_id)
1579 3d6c5566 Guido Trotter
      new_path = self._GetArchivedJobPath(job_id)
1580 3d6c5566 Guido Trotter
      if old_path == new_path:
1581 3d6c5566 Guido Trotter
        # job already archived (future case)
1582 3d6c5566 Guido Trotter
        logging.exception("Can't parse job %s", job_id)
1583 3d6c5566 Guido Trotter
      else:
1584 3d6c5566 Guido Trotter
        # non-archived case
1585 3d6c5566 Guido Trotter
        logging.exception("Can't parse job %s, will archive.", job_id)
1586 3d6c5566 Guido Trotter
        self._RenameFilesUnlocked([(old_path, new_path)])
1587 3d6c5566 Guido Trotter
      return None
1588 162c8636 Guido Trotter
1589 162c8636 Guido Trotter
    self._memcache[job_id] = job
1590 162c8636 Guido Trotter
    logging.debug("Added job %s to the cache", job_id)
1591 162c8636 Guido Trotter
    return job
1592 162c8636 Guido Trotter
1593 162c8636 Guido Trotter
  def _LoadJobFromDisk(self, job_id):
1594 162c8636 Guido Trotter
    """Load the given job file from disk.
1595 162c8636 Guido Trotter

1596 162c8636 Guido Trotter
    Given a job file, read, load and restore it in a _QueuedJob format.
1597 162c8636 Guido Trotter

1598 162c8636 Guido Trotter
    @type job_id: string
1599 162c8636 Guido Trotter
    @param job_id: job identifier
1600 162c8636 Guido Trotter
    @rtype: L{_QueuedJob} or None
1601 162c8636 Guido Trotter
    @return: either None or the job object
1602 162c8636 Guido Trotter

1603 162c8636 Guido Trotter
    """
1604 911a495b Iustin Pop
    filepath = self._GetJobPath(job_id)
1605 f1da30e6 Michael Hanselmann
    logging.debug("Loading job from %s", filepath)
1606 f1da30e6 Michael Hanselmann
    try:
1607 13998ef2 Michael Hanselmann
      raw_data = utils.ReadFile(filepath)
1608 162c8636 Guido Trotter
    except EnvironmentError, err:
1609 f1da30e6 Michael Hanselmann
      if err.errno in (errno.ENOENT, ):
1610 f1da30e6 Michael Hanselmann
        return None
1611 f1da30e6 Michael Hanselmann
      raise
1612 13998ef2 Michael Hanselmann
1613 94ed59a5 Iustin Pop
    try:
1614 162c8636 Guido Trotter
      data = serializer.LoadJson(raw_data)
1615 94ed59a5 Iustin Pop
      job = _QueuedJob.Restore(self, data)
1616 7260cfbe Iustin Pop
    except Exception, err: # pylint: disable-msg=W0703
1617 3d6c5566 Guido Trotter
      raise errors.JobFileCorrupted(err)
1618 94ed59a5 Iustin Pop
1619 ac0930b9 Iustin Pop
    return job
1620 f1da30e6 Michael Hanselmann
1621 0f9c08dc Guido Trotter
  def SafeLoadJobFromDisk(self, job_id):
1622 0f9c08dc Guido Trotter
    """Load the given job file from disk.
1623 0f9c08dc Guido Trotter

1624 0f9c08dc Guido Trotter
    Given a job file, read, load and restore it in a _QueuedJob format.
1625 0f9c08dc Guido Trotter
    In case of error reading the job, it gets returned as None, and the
1626 0f9c08dc Guido Trotter
    exception is logged.
1627 0f9c08dc Guido Trotter

1628 0f9c08dc Guido Trotter
    @type job_id: string
1629 0f9c08dc Guido Trotter
    @param job_id: job identifier
1630 0f9c08dc Guido Trotter
    @rtype: L{_QueuedJob} or None
1631 0f9c08dc Guido Trotter
    @return: either None or the job object
1632 0f9c08dc Guido Trotter

1633 0f9c08dc Guido Trotter
    """
1634 0f9c08dc Guido Trotter
    try:
1635 0f9c08dc Guido Trotter
      return self._LoadJobFromDisk(job_id)
1636 0f9c08dc Guido Trotter
    except (errors.JobFileCorrupted, EnvironmentError):
1637 0f9c08dc Guido Trotter
      logging.exception("Can't load/parse job %s", job_id)
1638 0f9c08dc Guido Trotter
      return None
1639 0f9c08dc Guido Trotter
1640 20571a26 Guido Trotter
  def _UpdateQueueSizeUnlocked(self):
1641 20571a26 Guido Trotter
    """Update the queue size.
1642 20571a26 Guido Trotter

1643 20571a26 Guido Trotter
    """
1644 20571a26 Guido Trotter
    self._queue_size = len(self._GetJobIDsUnlocked(sort=False))
1645 20571a26 Guido Trotter
1646 ebb80afa Guido Trotter
  @locking.ssynchronized(_LOCK)
1647 20571a26 Guido Trotter
  @_RequireOpenQueue
1648 20571a26 Guido Trotter
  def SetDrainFlag(self, drain_flag):
1649 3ccafd0e Iustin Pop
    """Sets the drain flag for the queue.
1650 3ccafd0e Iustin Pop

1651 ea03467c Iustin Pop
    @type drain_flag: boolean
1652 5bbd3f7f Michael Hanselmann
    @param drain_flag: Whether to set or unset the drain flag
1653 ea03467c Iustin Pop

1654 3ccafd0e Iustin Pop
    """
1655 ff699aa9 Michael Hanselmann
    jstore.SetDrainFlag(drain_flag)
1656 20571a26 Guido Trotter
1657 20571a26 Guido Trotter
    self._drained = drain_flag
1658 20571a26 Guido Trotter
1659 3ccafd0e Iustin Pop
    return True
1660 3ccafd0e Iustin Pop
1661 db37da70 Michael Hanselmann
  @_RequireOpenQueue
1662 009e73d0 Iustin Pop
  def _SubmitJobUnlocked(self, job_id, ops):
1663 85f03e0d Michael Hanselmann
    """Create and store a new job.
1664 f1da30e6 Michael Hanselmann

1665 85f03e0d Michael Hanselmann
    This enters the job into our job queue and also puts it on the new
1666 85f03e0d Michael Hanselmann
    queue, in order for it to be picked up by the queue processors.
1667 c3f0a12f Iustin Pop

1668 009e73d0 Iustin Pop
    @type job_id: job ID
1669 69b99987 Michael Hanselmann
    @param job_id: the job ID for the new job
1670 c3f0a12f Iustin Pop
    @type ops: list
1671 205d71fd Michael Hanselmann
    @param ops: The list of OpCodes that will become the new job.
1672 7beb1e53 Guido Trotter
    @rtype: L{_QueuedJob}
1673 7beb1e53 Guido Trotter
    @return: the job object to be queued
1674 7beb1e53 Guido Trotter
    @raise errors.JobQueueDrainError: if the job queue is marked for draining
1675 7beb1e53 Guido Trotter
    @raise errors.JobQueueFull: if the job queue has too many jobs in it
1676 e71c8147 Michael Hanselmann
    @raise errors.GenericError: If an opcode is not valid
1677 c3f0a12f Iustin Pop

1678 c3f0a12f Iustin Pop
    """
1679 20571a26 Guido Trotter
    # Ok when sharing the big job queue lock, as the drain file is created when
1680 20571a26 Guido Trotter
    # the lock is exclusive.
1681 20571a26 Guido Trotter
    if self._drained:
1682 2971c913 Iustin Pop
      raise errors.JobQueueDrainError("Job queue is drained, refusing job")
1683 f87b405e Michael Hanselmann
1684 20571a26 Guido Trotter
    if self._queue_size >= constants.JOB_QUEUE_SIZE_HARD_LIMIT:
1685 f87b405e Michael Hanselmann
      raise errors.JobQueueFull()
1686 f87b405e Michael Hanselmann
1687 f1da30e6 Michael Hanselmann
    job = _QueuedJob(self, job_id, ops)
1688 f1da30e6 Michael Hanselmann
1689 e71c8147 Michael Hanselmann
    # Check priority
1690 e71c8147 Michael Hanselmann
    for idx, op in enumerate(job.ops):
1691 e71c8147 Michael Hanselmann
      if op.priority not in constants.OP_PRIO_SUBMIT_VALID:
1692 e71c8147 Michael Hanselmann
        allowed = utils.CommaJoin(constants.OP_PRIO_SUBMIT_VALID)
1693 e71c8147 Michael Hanselmann
        raise errors.GenericError("Opcode %s has invalid priority %s, allowed"
1694 e71c8147 Michael Hanselmann
                                  " are %s" % (idx, op.priority, allowed))
1695 e71c8147 Michael Hanselmann
1696 f1da30e6 Michael Hanselmann
    # Write to disk
1697 85f03e0d Michael Hanselmann
    self.UpdateJobUnlocked(job)
1698 f1da30e6 Michael Hanselmann
1699 20571a26 Guido Trotter
    self._queue_size += 1
1700 20571a26 Guido Trotter
1701 5685c1a5 Michael Hanselmann
    logging.debug("Adding new job %s to the cache", job_id)
1702 ac0930b9 Iustin Pop
    self._memcache[job_id] = job
1703 ac0930b9 Iustin Pop
1704 7beb1e53 Guido Trotter
    return job
1705 f1da30e6 Michael Hanselmann
1706 ebb80afa Guido Trotter
  @locking.ssynchronized(_LOCK)
1707 2971c913 Iustin Pop
  @_RequireOpenQueue
1708 2971c913 Iustin Pop
  def SubmitJob(self, ops):
1709 2971c913 Iustin Pop
    """Create and store a new job.
1710 2971c913 Iustin Pop

1711 2971c913 Iustin Pop
    @see: L{_SubmitJobUnlocked}
1712 2971c913 Iustin Pop

1713 2971c913 Iustin Pop
    """
1714 009e73d0 Iustin Pop
    job_id = self._NewSerialsUnlocked(1)[0]
1715 7b5c4a69 Michael Hanselmann
    self._EnqueueJobs([self._SubmitJobUnlocked(job_id, ops)])
1716 7beb1e53 Guido Trotter
    return job_id
1717 2971c913 Iustin Pop
1718 ebb80afa Guido Trotter
  @locking.ssynchronized(_LOCK)
1719 2971c913 Iustin Pop
  @_RequireOpenQueue
1720 2971c913 Iustin Pop
  def SubmitManyJobs(self, jobs):
1721 2971c913 Iustin Pop
    """Create and store multiple jobs.
1722 2971c913 Iustin Pop

1723 2971c913 Iustin Pop
    @see: L{_SubmitJobUnlocked}
1724 2971c913 Iustin Pop

1725 2971c913 Iustin Pop
    """
1726 2971c913 Iustin Pop
    results = []
1727 7b5c4a69 Michael Hanselmann
    added_jobs = []
1728 009e73d0 Iustin Pop
    all_job_ids = self._NewSerialsUnlocked(len(jobs))
1729 009e73d0 Iustin Pop
    for job_id, ops in zip(all_job_ids, jobs):
1730 2971c913 Iustin Pop
      try:
1731 7b5c4a69 Michael Hanselmann
        added_jobs.append(self._SubmitJobUnlocked(job_id, ops))
1732 2971c913 Iustin Pop
        status = True
1733 7beb1e53 Guido Trotter
        data = job_id
1734 2971c913 Iustin Pop
      except errors.GenericError, err:
1735 98ed5092 Michael Hanselmann
        data = ("%s; opcodes %s" %
1736 98ed5092 Michael Hanselmann
                (err, utils.CommaJoin(op.Summary() for op in ops)))
1737 2971c913 Iustin Pop
        status = False
1738 2971c913 Iustin Pop
      results.append((status, data))
1739 7b5c4a69 Michael Hanselmann
1740 7b5c4a69 Michael Hanselmann
    self._EnqueueJobs(added_jobs)
1741 2971c913 Iustin Pop
1742 2971c913 Iustin Pop
    return results
1743 2971c913 Iustin Pop
1744 7b5c4a69 Michael Hanselmann
  def _EnqueueJobs(self, jobs):
1745 7b5c4a69 Michael Hanselmann
    """Helper function to add jobs to worker pool's queue.
1746 7b5c4a69 Michael Hanselmann

1747 7b5c4a69 Michael Hanselmann
    @type jobs: list
1748 7b5c4a69 Michael Hanselmann
    @param jobs: List of all jobs
1749 7b5c4a69 Michael Hanselmann

1750 7b5c4a69 Michael Hanselmann
    """
1751 7b5c4a69 Michael Hanselmann
    self._wpool.AddManyTasks([(job, ) for job in jobs],
1752 7b5c4a69 Michael Hanselmann
                             priority=[job.CalcPriority() for job in jobs])
1753 7b5c4a69 Michael Hanselmann
1754 db37da70 Michael Hanselmann
  @_RequireOpenQueue
1755 4c36bdf5 Guido Trotter
  def UpdateJobUnlocked(self, job, replicate=True):
1756 ea03467c Iustin Pop
    """Update a job's on disk storage.
1757 ea03467c Iustin Pop

1758 ea03467c Iustin Pop
    After a job has been modified, this function needs to be called in
1759 ea03467c Iustin Pop
    order to write the changes to disk and replicate them to the other
1760 ea03467c Iustin Pop
    nodes.
1761 ea03467c Iustin Pop

1762 ea03467c Iustin Pop
    @type job: L{_QueuedJob}
1763 ea03467c Iustin Pop
    @param job: the changed job
1764 4c36bdf5 Guido Trotter
    @type replicate: boolean
1765 4c36bdf5 Guido Trotter
    @param replicate: whether to replicate the change to remote nodes
1766 ea03467c Iustin Pop

1767 ea03467c Iustin Pop
    """
1768 f1da30e6 Michael Hanselmann
    filename = self._GetJobPath(job.id)
1769 23752136 Michael Hanselmann
    data = serializer.DumpJson(job.Serialize(), indent=False)
1770 f1da30e6 Michael Hanselmann
    logging.debug("Writing job %s to %s", job.id, filename)
1771 4c36bdf5 Guido Trotter
    self._UpdateJobQueueFile(filename, data, replicate)
1772 ac0930b9 Iustin Pop
1773 5c735209 Iustin Pop
  def WaitForJobChanges(self, job_id, fields, prev_job_info, prev_log_serial,
1774 5c735209 Iustin Pop
                        timeout):
1775 6c5a7090 Michael Hanselmann
    """Waits for changes in a job.
1776 6c5a7090 Michael Hanselmann

1777 6c5a7090 Michael Hanselmann
    @type job_id: string
1778 6c5a7090 Michael Hanselmann
    @param job_id: Job identifier
1779 6c5a7090 Michael Hanselmann
    @type fields: list of strings
1780 6c5a7090 Michael Hanselmann
    @param fields: Which fields to check for changes
1781 6c5a7090 Michael Hanselmann
    @type prev_job_info: list or None
1782 6c5a7090 Michael Hanselmann
    @param prev_job_info: Last job information returned
1783 6c5a7090 Michael Hanselmann
    @type prev_log_serial: int
1784 6c5a7090 Michael Hanselmann
    @param prev_log_serial: Last job message serial number
1785 5c735209 Iustin Pop
    @type timeout: float
1786 989a8bee Michael Hanselmann
    @param timeout: maximum time to wait in seconds
1787 ea03467c Iustin Pop
    @rtype: tuple (job info, log entries)
1788 ea03467c Iustin Pop
    @return: a tuple of the job information as required via
1789 ea03467c Iustin Pop
        the fields parameter, and the log entries as a list
1790 ea03467c Iustin Pop

1791 ea03467c Iustin Pop
        if the job has not changed and the timeout has expired,
1792 ea03467c Iustin Pop
        we instead return a special value,
1793 ea03467c Iustin Pop
        L{constants.JOB_NOTCHANGED}, which should be interpreted
1794 ea03467c Iustin Pop
        as such by the clients
1795 6c5a7090 Michael Hanselmann

1796 6c5a7090 Michael Hanselmann
    """
1797 989a8bee Michael Hanselmann
    load_fn = compat.partial(self.SafeLoadJobFromDisk, job_id)
1798 989a8bee Michael Hanselmann
1799 989a8bee Michael Hanselmann
    helper = _WaitForJobChangesHelper()
1800 989a8bee Michael Hanselmann
1801 989a8bee Michael Hanselmann
    return helper(self._GetJobPath(job_id), load_fn,
1802 989a8bee Michael Hanselmann
                  fields, prev_job_info, prev_log_serial, timeout)
1803 dfe57c22 Michael Hanselmann
1804 ebb80afa Guido Trotter
  @locking.ssynchronized(_LOCK)
1805 db37da70 Michael Hanselmann
  @_RequireOpenQueue
1806 188c5e0a Michael Hanselmann
  def CancelJob(self, job_id):
1807 188c5e0a Michael Hanselmann
    """Cancels a job.
1808 188c5e0a Michael Hanselmann

1809 ea03467c Iustin Pop
    This will only succeed if the job has not started yet.
1810 ea03467c Iustin Pop

1811 188c5e0a Michael Hanselmann
    @type job_id: string
1812 ea03467c Iustin Pop
    @param job_id: job ID of job to be cancelled.
1813 188c5e0a Michael Hanselmann

1814 188c5e0a Michael Hanselmann
    """
1815 fbf0262f Michael Hanselmann
    logging.info("Cancelling job %s", job_id)
1816 188c5e0a Michael Hanselmann
1817 85f03e0d Michael Hanselmann
    job = self._LoadJobUnlocked(job_id)
1818 188c5e0a Michael Hanselmann
    if not job:
1819 188c5e0a Michael Hanselmann
      logging.debug("Job %s not found", job_id)
1820 fbf0262f Michael Hanselmann
      return (False, "Job %s not found" % job_id)
1821 fbf0262f Michael Hanselmann
1822 099b2870 Michael Hanselmann
    (success, msg) = job.Cancel()
1823 188c5e0a Michael Hanselmann
1824 099b2870 Michael Hanselmann
    if success:
1825 099b2870 Michael Hanselmann
      self.UpdateJobUnlocked(job)
1826 fbf0262f Michael Hanselmann
1827 099b2870 Michael Hanselmann
    return (success, msg)
1828 fbf0262f Michael Hanselmann
1829 fbf0262f Michael Hanselmann
  @_RequireOpenQueue
1830 d7fd1f28 Michael Hanselmann
  def _ArchiveJobsUnlocked(self, jobs):
1831 d7fd1f28 Michael Hanselmann
    """Archives jobs.
1832 c609f802 Michael Hanselmann

1833 d7fd1f28 Michael Hanselmann
    @type jobs: list of L{_QueuedJob}
1834 25e7b43f Iustin Pop
    @param jobs: Job objects
1835 d7fd1f28 Michael Hanselmann
    @rtype: int
1836 d7fd1f28 Michael Hanselmann
    @return: Number of archived jobs
1837 c609f802 Michael Hanselmann

1838 c609f802 Michael Hanselmann
    """
1839 d7fd1f28 Michael Hanselmann
    archive_jobs = []
1840 d7fd1f28 Michael Hanselmann
    rename_files = []
1841 d7fd1f28 Michael Hanselmann
    for job in jobs:
1842 989a8bee Michael Hanselmann
      if job.CalcStatus() not in constants.JOBS_FINALIZED:
1843 d7fd1f28 Michael Hanselmann
        logging.debug("Job %s is not yet done", job.id)
1844 d7fd1f28 Michael Hanselmann
        continue
1845 c609f802 Michael Hanselmann
1846 d7fd1f28 Michael Hanselmann
      archive_jobs.append(job)
1847 c609f802 Michael Hanselmann
1848 d7fd1f28 Michael Hanselmann
      old = self._GetJobPath(job.id)
1849 d7fd1f28 Michael Hanselmann
      new = self._GetArchivedJobPath(job.id)
1850 d7fd1f28 Michael Hanselmann
      rename_files.append((old, new))
1851 c609f802 Michael Hanselmann
1852 d7fd1f28 Michael Hanselmann
    # TODO: What if 1..n files fail to rename?
1853 d7fd1f28 Michael Hanselmann
    self._RenameFilesUnlocked(rename_files)
1854 f1da30e6 Michael Hanselmann
1855 d7fd1f28 Michael Hanselmann
    logging.debug("Successfully archived job(s) %s",
1856 1f864b60 Iustin Pop
                  utils.CommaJoin(job.id for job in archive_jobs))
1857 d7fd1f28 Michael Hanselmann
1858 20571a26 Guido Trotter
    # Since we haven't quite checked, above, if we succeeded or failed renaming
1859 20571a26 Guido Trotter
    # the files, we update the cached queue size from the filesystem. When we
1860 20571a26 Guido Trotter
    # get around to fix the TODO: above, we can use the number of actually
1861 20571a26 Guido Trotter
    # archived jobs to fix this.
1862 20571a26 Guido Trotter
    self._UpdateQueueSizeUnlocked()
1863 d7fd1f28 Michael Hanselmann
    return len(archive_jobs)
1864 78d12585 Michael Hanselmann
1865 ebb80afa Guido Trotter
  @locking.ssynchronized(_LOCK)
1866 07cd723a Iustin Pop
  @_RequireOpenQueue
1867 07cd723a Iustin Pop
  def ArchiveJob(self, job_id):
1868 07cd723a Iustin Pop
    """Archives a job.
1869 07cd723a Iustin Pop

1870 25e7b43f Iustin Pop
    This is just a wrapper over L{_ArchiveJobsUnlocked}.
1871 ea03467c Iustin Pop

1872 07cd723a Iustin Pop
    @type job_id: string
1873 07cd723a Iustin Pop
    @param job_id: Job ID of job to be archived.
1874 78d12585 Michael Hanselmann
    @rtype: bool
1875 78d12585 Michael Hanselmann
    @return: Whether job was archived
1876 07cd723a Iustin Pop

1877 07cd723a Iustin Pop
    """
1878 78d12585 Michael Hanselmann
    logging.info("Archiving job %s", job_id)
1879 78d12585 Michael Hanselmann
1880 78d12585 Michael Hanselmann
    job = self._LoadJobUnlocked(job_id)
1881 78d12585 Michael Hanselmann
    if not job:
1882 78d12585 Michael Hanselmann
      logging.debug("Job %s not found", job_id)
1883 78d12585 Michael Hanselmann
      return False
1884 78d12585 Michael Hanselmann
1885 5278185a Iustin Pop
    return self._ArchiveJobsUnlocked([job]) == 1
1886 07cd723a Iustin Pop
1887 ebb80afa Guido Trotter
  @locking.ssynchronized(_LOCK)
1888 07cd723a Iustin Pop
  @_RequireOpenQueue
1889 f8ad5591 Michael Hanselmann
  def AutoArchiveJobs(self, age, timeout):
1890 07cd723a Iustin Pop
    """Archives all jobs based on age.
1891 07cd723a Iustin Pop

1892 07cd723a Iustin Pop
    The method will archive all jobs which are older than the age
1893 07cd723a Iustin Pop
    parameter. For jobs that don't have an end timestamp, the start
1894 07cd723a Iustin Pop
    timestamp will be considered. The special '-1' age will cause
1895 07cd723a Iustin Pop
    archival of all jobs (that are not running or queued).
1896 07cd723a Iustin Pop

1897 07cd723a Iustin Pop
    @type age: int
1898 07cd723a Iustin Pop
    @param age: the minimum age in seconds
1899 07cd723a Iustin Pop

1900 07cd723a Iustin Pop
    """
1901 07cd723a Iustin Pop
    logging.info("Archiving jobs with age more than %s seconds", age)
1902 07cd723a Iustin Pop
1903 07cd723a Iustin Pop
    now = time.time()
1904 f8ad5591 Michael Hanselmann
    end_time = now + timeout
1905 f8ad5591 Michael Hanselmann
    archived_count = 0
1906 f8ad5591 Michael Hanselmann
    last_touched = 0
1907 f8ad5591 Michael Hanselmann
1908 69b03fd7 Guido Trotter
    all_job_ids = self._GetJobIDsUnlocked()
1909 d7fd1f28 Michael Hanselmann
    pending = []
1910 f8ad5591 Michael Hanselmann
    for idx, job_id in enumerate(all_job_ids):
1911 d2c8afb1 Michael Hanselmann
      last_touched = idx + 1
1912 f8ad5591 Michael Hanselmann
1913 d7fd1f28 Michael Hanselmann
      # Not optimal because jobs could be pending
1914 d7fd1f28 Michael Hanselmann
      # TODO: Measure average duration for job archival and take number of
1915 d7fd1f28 Michael Hanselmann
      # pending jobs into account.
1916 f8ad5591 Michael Hanselmann
      if time.time() > end_time:
1917 f8ad5591 Michael Hanselmann
        break
1918 f8ad5591 Michael Hanselmann
1919 78d12585 Michael Hanselmann
      # Returns None if the job failed to load
1920 78d12585 Michael Hanselmann
      job = self._LoadJobUnlocked(job_id)
1921 f8ad5591 Michael Hanselmann
      if job:
1922 f8ad5591 Michael Hanselmann
        if job.end_timestamp is None:
1923 f8ad5591 Michael Hanselmann
          if job.start_timestamp is None:
1924 f8ad5591 Michael Hanselmann
            job_age = job.received_timestamp
1925 f8ad5591 Michael Hanselmann
          else:
1926 f8ad5591 Michael Hanselmann
            job_age = job.start_timestamp
1927 07cd723a Iustin Pop
        else:
1928 f8ad5591 Michael Hanselmann
          job_age = job.end_timestamp
1929 f8ad5591 Michael Hanselmann
1930 f8ad5591 Michael Hanselmann
        if age == -1 or now - job_age[0] > age:
1931 d7fd1f28 Michael Hanselmann
          pending.append(job)
1932 d7fd1f28 Michael Hanselmann
1933 d7fd1f28 Michael Hanselmann
          # Archive 10 jobs at a time
1934 d7fd1f28 Michael Hanselmann
          if len(pending) >= 10:
1935 d7fd1f28 Michael Hanselmann
            archived_count += self._ArchiveJobsUnlocked(pending)
1936 d7fd1f28 Michael Hanselmann
            pending = []
1937 f8ad5591 Michael Hanselmann
1938 d7fd1f28 Michael Hanselmann
    if pending:
1939 d7fd1f28 Michael Hanselmann
      archived_count += self._ArchiveJobsUnlocked(pending)
1940 07cd723a Iustin Pop
1941 d2c8afb1 Michael Hanselmann
    return (archived_count, len(all_job_ids) - last_touched)
1942 07cd723a Iustin Pop
1943 e2715f69 Michael Hanselmann
  def QueryJobs(self, job_ids, fields):
1944 e2715f69 Michael Hanselmann
    """Returns a list of jobs in queue.
1945 e2715f69 Michael Hanselmann

1946 ea03467c Iustin Pop
    @type job_ids: list
1947 ea03467c Iustin Pop
    @param job_ids: sequence of job identifiers or None for all
1948 ea03467c Iustin Pop
    @type fields: list
1949 ea03467c Iustin Pop
    @param fields: names of fields to return
1950 ea03467c Iustin Pop
    @rtype: list
1951 ea03467c Iustin Pop
    @return: list one element per job, each element being list with
1952 ea03467c Iustin Pop
        the requested fields
1953 e2715f69 Michael Hanselmann

1954 e2715f69 Michael Hanselmann
    """
1955 85f03e0d Michael Hanselmann
    jobs = []
1956 9f7b4967 Guido Trotter
    list_all = False
1957 9f7b4967 Guido Trotter
    if not job_ids:
1958 9f7b4967 Guido Trotter
      # Since files are added to/removed from the queue atomically, there's no
1959 9f7b4967 Guido Trotter
      # risk of getting the job ids in an inconsistent state.
1960 9f7b4967 Guido Trotter
      job_ids = self._GetJobIDsUnlocked()
1961 9f7b4967 Guido Trotter
      list_all = True
1962 e2715f69 Michael Hanselmann
1963 9f7b4967 Guido Trotter
    for job_id in job_ids:
1964 9f7b4967 Guido Trotter
      job = self.SafeLoadJobFromDisk(job_id)
1965 9f7b4967 Guido Trotter
      if job is not None:
1966 6a290889 Guido Trotter
        jobs.append(job.GetInfo(fields))
1967 9f7b4967 Guido Trotter
      elif not list_all:
1968 9f7b4967 Guido Trotter
        jobs.append(None)
1969 e2715f69 Michael Hanselmann
1970 85f03e0d Michael Hanselmann
    return jobs
1971 e2715f69 Michael Hanselmann
1972 ebb80afa Guido Trotter
  @locking.ssynchronized(_LOCK)
1973 db37da70 Michael Hanselmann
  @_RequireOpenQueue
1974 e2715f69 Michael Hanselmann
  def Shutdown(self):
1975 e2715f69 Michael Hanselmann
    """Stops the job queue.
1976 e2715f69 Michael Hanselmann

1977 ea03467c Iustin Pop
    This shutdowns all the worker threads an closes the queue.
1978 ea03467c Iustin Pop

1979 e2715f69 Michael Hanselmann
    """
1980 e2715f69 Michael Hanselmann
    self._wpool.TerminateWorkers()
1981 85f03e0d Michael Hanselmann
1982 a71f9c7d Guido Trotter
    self._queue_filelock.Close()
1983 a71f9c7d Guido Trotter
    self._queue_filelock = None