jqueue.py 49.4 KB
Newer Older
Iustin Pop's avatar
Iustin Pop committed
1
2
3
#
#

4
# Copyright (C) 2006, 2007, 2008, 2009, 2010 Google Inc.
Iustin Pop's avatar
Iustin Pop committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.


22
23
"""Module implementing the job queue handling.

24
25
26
27
28
Locking: there's a single, large lock in the L{JobQueue} class. It's
used by all other classes in this module.

@var JOBQUEUE_THREADS: the number of worker threads we start for
    processing jobs
29
30

"""
Iustin Pop's avatar
Iustin Pop committed
31

32
import os
Michael Hanselmann's avatar
Michael Hanselmann committed
33
import logging
34
35
import errno
import re
36
import time
37
import weakref
Iustin Pop's avatar
Iustin Pop committed
38

Guido Trotter's avatar
Guido Trotter committed
39
40
41
42
43
44
45
try:
  # pylint: disable-msg=E0611
  from pyinotify import pyinotify
except ImportError:
  import pyinotify

from ganeti import asyncnotifier
Michael Hanselmann's avatar
Michael Hanselmann committed
46
from ganeti import constants
47
from ganeti import serializer
Michael Hanselmann's avatar
Michael Hanselmann committed
48
from ganeti import workerpool
49
from ganeti import locking
50
from ganeti import opcodes
Iustin Pop's avatar
Iustin Pop committed
51
from ganeti import errors
Michael Hanselmann's avatar
Michael Hanselmann committed
52
from ganeti import mcpu
53
from ganeti import utils
54
from ganeti import jstore
55
from ganeti import rpc
56
from ganeti import netutils
57
from ganeti import compat
Michael Hanselmann's avatar
Michael Hanselmann committed
58

59

60
JOBQUEUE_THREADS = 25
61
JOBS_PER_ARCHIVE_DIRECTORY = 10000
Michael Hanselmann's avatar
Michael Hanselmann committed
62

63
64
65
# member lock names to be passed to @ssynchronized decorator
_LOCK = "_lock"
_QUEUE = "_queue"
66

Iustin Pop's avatar
Iustin Pop committed
67

68
class CancelJob(Exception):
69
70
71
72
73
  """Special exception to cancel a job.

  """


74
def TimeStampNow():
75
76
77
78
79
80
  """Returns the current timestamp.

  @rtype: tuple
  @return: the current time in the (seconds, microseconds) format

  """
81
82
83
  return utils.SplitTime(time.time())


Michael Hanselmann's avatar
Michael Hanselmann committed
84
class _QueuedOpCode(object):
Michael Hanselmann's avatar
Michael Hanselmann committed
85
  """Encapsulates an opcode object.
Michael Hanselmann's avatar
Michael Hanselmann committed
86

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

Michael Hanselmann's avatar
Michael Hanselmann committed
96
  """
97
  __slots__ = ["input", "status", "result", "log",
Iustin Pop's avatar
Iustin Pop committed
98
               "start_timestamp", "exec_timestamp", "end_timestamp",
99
100
               "__weakref__"]

Michael Hanselmann's avatar
Michael Hanselmann committed
101
  def __init__(self, op):
102
103
104
105
106
107
    """Constructor for the _QuededOpCode.

    @type op: L{opcodes.OpCode}
    @param op: the opcode we encapsulate

    """
Michael Hanselmann's avatar
Michael Hanselmann committed
108
109
110
111
    self.input = op
    self.status = constants.OP_STATUS_QUEUED
    self.result = None
    self.log = []
112
    self.start_timestamp = None
Iustin Pop's avatar
Iustin Pop committed
113
    self.exec_timestamp = None
114
    self.end_timestamp = None
115
116
117

  @classmethod
  def Restore(cls, state):
118
119
120
121
122
123
124
125
    """Restore the _QueuedOpCode from the serialized form.

    @type state: dict
    @param state: the serialized state
    @rtype: _QueuedOpCode
    @return: a new _QueuedOpCode instance

    """
Michael Hanselmann's avatar
Michael Hanselmann committed
126
127
128
129
130
    obj = _QueuedOpCode.__new__(cls)
    obj.input = opcodes.OpCode.LoadOpCode(state["input"])
    obj.status = state["status"]
    obj.result = state["result"]
    obj.log = state["log"]
131
    obj.start_timestamp = state.get("start_timestamp", None)
Iustin Pop's avatar
Iustin Pop committed
132
    obj.exec_timestamp = state.get("exec_timestamp", None)
133
    obj.end_timestamp = state.get("end_timestamp", None)
134
135
136
    return obj

  def Serialize(self):
137
138
139
140
141
142
    """Serializes this _QueuedOpCode.

    @rtype: dict
    @return: the dictionary holding the serialized state

    """
143
144
145
146
147
    return {
      "input": self.input.__getstate__(),
      "status": self.status,
      "result": self.result,
      "log": self.log,
148
      "start_timestamp": self.start_timestamp,
Iustin Pop's avatar
Iustin Pop committed
149
      "exec_timestamp": self.exec_timestamp,
150
      "end_timestamp": self.end_timestamp,
151
      }
152

Michael Hanselmann's avatar
Michael Hanselmann committed
153
154
155
156

class _QueuedJob(object):
  """In-memory job representation.

157
158
159
160
161
162
163
164
165
166
167
168
169
  This is what we use to track the user-submitted jobs. Locking must
  be taken care of by users of this class.

  @type queue: L{JobQueue}
  @ivar queue: the parent queue
  @ivar id: the job ID
  @type ops: list
  @ivar ops: the list of _QueuedOpCode that constitute the job
  @type log_serial: int
  @ivar log_serial: holds the index for the next log entry
  @ivar received_timestamp: the timestamp for when the job was received
  @ivar start_timestmap: the timestamp for start of execution
  @ivar end_timestamp: the timestamp for end of execution
170
  @ivar lock_status: In-memory locking information for debugging
Michael Hanselmann's avatar
Michael Hanselmann committed
171
172

  """
Iustin Pop's avatar
Iustin Pop committed
173
  # pylint: disable-msg=W0212
174
  __slots__ = ["queue", "id", "ops", "log_serial",
175
               "received_timestamp", "start_timestamp", "end_timestamp",
176
               "lock_status", "change",
177
178
               "__weakref__"]

Michael Hanselmann's avatar
Michael Hanselmann committed
179
  def __init__(self, queue, job_id, ops):
180
181
182
183
184
185
186
187
188
189
190
    """Constructor for the _QueuedJob.

    @type queue: L{JobQueue}
    @param queue: our parent queue
    @type job_id: job_id
    @param job_id: our job id
    @type ops: list
    @param ops: the list of opcodes we hold, which will be encapsulated
        in _QueuedOpCodes

    """
Michael Hanselmann's avatar
Michael Hanselmann committed
191
    if not ops:
Guido Trotter's avatar
Guido Trotter committed
192
      raise errors.GenericError("A job needs at least one opcode")
Michael Hanselmann's avatar
Michael Hanselmann committed
193

Michael Hanselmann's avatar
Michael Hanselmann committed
194
    self.queue = queue
195
    self.id = job_id
Michael Hanselmann's avatar
Michael Hanselmann committed
196
    self.ops = [_QueuedOpCode(op) for op in ops]
197
    self.log_serial = 0
198
199
200
    self.received_timestamp = TimeStampNow()
    self.start_timestamp = None
    self.end_timestamp = None
201

202
203
204
    # In-memory attributes
    self.lock_status = None

205
206
207
208
209
210
211
  def __repr__(self):
    status = ["%s.%s" % (self.__class__.__module__, self.__class__.__name__),
              "id=%s" % self.id,
              "ops=%s" % ",".join([op.input.Summary() for op in self.ops])]

    return "<%s at %#x>" % (" ".join(status), id(self))

212
  @classmethod
Michael Hanselmann's avatar
Michael Hanselmann committed
213
  def Restore(cls, queue, state):
214
215
216
217
218
219
220
221
222
223
    """Restore a _QueuedJob from serialized state:

    @type queue: L{JobQueue}
    @param queue: to which queue the restored job belongs
    @type state: dict
    @param state: the serialized state
    @rtype: _JobQueue
    @return: the restored _JobQueue instance

    """
Michael Hanselmann's avatar
Michael Hanselmann committed
224
225
226
    obj = _QueuedJob.__new__(cls)
    obj.queue = queue
    obj.id = state["id"]
227
228
229
    obj.received_timestamp = state.get("received_timestamp", None)
    obj.start_timestamp = state.get("start_timestamp", None)
    obj.end_timestamp = state.get("end_timestamp", None)
230

231
232
233
    # In-memory attributes
    obj.lock_status = None

234
235
236
237
238
239
240
241
    obj.ops = []
    obj.log_serial = 0
    for op_state in state["ops"]:
      op = _QueuedOpCode.Restore(op_state)
      for log_entry in op.log:
        obj.log_serial = max(obj.log_serial, log_entry[0])
      obj.ops.append(op)

242
243
244
    return obj

  def Serialize(self):
245
246
247
248
249
250
    """Serialize the _JobQueue instance.

    @rtype: dict
    @return: the serialized state

    """
251
252
    return {
      "id": self.id,
Michael Hanselmann's avatar
Michael Hanselmann committed
253
      "ops": [op.Serialize() for op in self.ops],
254
255
256
      "start_timestamp": self.start_timestamp,
      "end_timestamp": self.end_timestamp,
      "received_timestamp": self.received_timestamp,
257
258
      }

Michael Hanselmann's avatar
Michael Hanselmann committed
259
  def CalcStatus(self):
260
261
262
263
264
265
266
267
268
269
    """Compute the status of this job.

    This function iterates over all the _QueuedOpCodes in the job and
    based on their status, computes the job status.

    The algorithm is:
      - if we find a cancelled, or finished with error, the job
        status will be the same
      - otherwise, the last opcode with the status one of:
          - waitlock
270
          - canceling
271
272
273
274
275
276
277
278
279
280
          - running

        will determine the job status

      - otherwise, it means either all opcodes are queued, or success,
        and the job status will be the same

    @return: the job status

    """
Michael Hanselmann's avatar
Michael Hanselmann committed
281
282
283
    status = constants.JOB_STATUS_QUEUED

    all_success = True
Michael Hanselmann's avatar
Michael Hanselmann committed
284
285
    for op in self.ops:
      if op.status == constants.OP_STATUS_SUCCESS:
Michael Hanselmann's avatar
Michael Hanselmann committed
286
287
288
289
        continue

      all_success = False

Michael Hanselmann's avatar
Michael Hanselmann committed
290
      if op.status == constants.OP_STATUS_QUEUED:
Michael Hanselmann's avatar
Michael Hanselmann committed
291
        pass
Iustin Pop's avatar
Iustin Pop committed
292
293
      elif op.status == constants.OP_STATUS_WAITLOCK:
        status = constants.JOB_STATUS_WAITLOCK
Michael Hanselmann's avatar
Michael Hanselmann committed
294
      elif op.status == constants.OP_STATUS_RUNNING:
Michael Hanselmann's avatar
Michael Hanselmann committed
295
        status = constants.JOB_STATUS_RUNNING
296
297
298
      elif op.status == constants.OP_STATUS_CANCELING:
        status = constants.JOB_STATUS_CANCELING
        break
Michael Hanselmann's avatar
Michael Hanselmann committed
299
      elif op.status == constants.OP_STATUS_ERROR:
300
301
302
        status = constants.JOB_STATUS_ERROR
        # The whole job fails if one opcode failed
        break
Michael Hanselmann's avatar
Michael Hanselmann committed
303
      elif op.status == constants.OP_STATUS_CANCELED:
304
305
        status = constants.OP_STATUS_CANCELED
        break
Michael Hanselmann's avatar
Michael Hanselmann committed
306
307
308
309
310
311

    if all_success:
      status = constants.JOB_STATUS_SUCCESS

    return status

312
  def GetLogEntries(self, newer_than):
313
314
315
    """Selectively returns the log entries.

    @type newer_than: None or int
Michael Hanselmann's avatar
Michael Hanselmann committed
316
    @param newer_than: if this is None, return all log entries,
317
318
319
320
321
322
        otherwise return only the log entries with serial higher
        than this value
    @rtype: list
    @return: the list of the log entries selected

    """
323
324
325
326
327
328
329
    if newer_than is None:
      serial = -1
    else:
      serial = newer_than

    entries = []
    for op in self.ops:
330
      entries.extend(filter(lambda entry: entry[0] > serial, op.log))
331
332
333

    return entries

334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
  def GetInfo(self, fields):
    """Returns information about a job.

    @type fields: list
    @param fields: names of fields to return
    @rtype: list
    @return: list with one element for each field
    @raise errors.OpExecError: when an invalid field
        has been passed

    """
    row = []
    for fname in fields:
      if fname == "id":
        row.append(self.id)
      elif fname == "status":
        row.append(self.CalcStatus())
      elif fname == "ops":
        row.append([op.input.__getstate__() for op in self.ops])
      elif fname == "opresult":
        row.append([op.result for op in self.ops])
      elif fname == "opstatus":
        row.append([op.status for op in self.ops])
      elif fname == "oplog":
        row.append([op.log for op in self.ops])
      elif fname == "opstart":
        row.append([op.start_timestamp for op in self.ops])
      elif fname == "opexec":
        row.append([op.exec_timestamp for op in self.ops])
      elif fname == "opend":
        row.append([op.end_timestamp for op in self.ops])
      elif fname == "received_ts":
        row.append(self.received_timestamp)
      elif fname == "start_ts":
        row.append(self.start_timestamp)
      elif fname == "end_ts":
        row.append(self.end_timestamp)
      elif fname == "lock_status":
        row.append(self.lock_status)
      elif fname == "summary":
        row.append([op.input.Summary() for op in self.ops])
      else:
        raise errors.OpExecError("Invalid self query field '%s'" % fname)
    return row

379
380
381
382
383
384
385
386
387
388
389
  def MarkUnfinishedOps(self, status, result):
    """Mark unfinished opcodes with a given status and result.

    This is an utility function for marking all running or waiting to
    be run opcodes with a given status. Opcodes which are already
    finalised are not changed.

    @param status: a given opcode status
    @param result: the opcode result

    """
390
391
392
393
394
395
396
397
398
399
400
    try:
      not_marked = True
      for op in self.ops:
        if op.status in constants.OPS_FINALIZED:
          assert not_marked, "Finalized opcodes found after non-finalized ones"
          continue
        op.status = status
        op.result = result
        not_marked = False
    finally:
      self.queue.UpdateJobUnlocked(self)
401

402

403
class _OpExecCallbacks(mcpu.OpExecCbBase):
404
405
  def __init__(self, queue, job, op):
    """Initializes this class.
406

407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
    @type queue: L{JobQueue}
    @param queue: Job queue
    @type job: L{_QueuedJob}
    @param job: Job object
    @type op: L{_QueuedOpCode}
    @param op: OpCode

    """
    assert queue, "Queue is missing"
    assert job, "Job is missing"
    assert op, "Opcode is missing"

    self._queue = queue
    self._job = job
    self._op = op

423
424
425
426
427
428
429
430
431
  def _CheckCancel(self):
    """Raises an exception to cancel the job if asked to.

    """
    # Cancel here if we were asked to
    if self._op.status == constants.OP_STATUS_CANCELING:
      logging.debug("Canceling opcode")
      raise CancelJob()

432
  @locking.ssynchronized(_QUEUE, shared=1)
433
  def NotifyStart(self):
Iustin Pop's avatar
Iustin Pop committed
434
435
    """Mark the opcode as running, not lock-waiting.

436
437
438
439
    This is called from the mcpu code as a notifier function, when the LU is
    finally about to start the Exec() method. Of course, to have end-user
    visible results, the opcode must be initially (before calling into
    Processor.ExecOpCode) set to OP_STATUS_WAITLOCK.
Iustin Pop's avatar
Iustin Pop committed
440
441

    """
442
443
    assert self._op.status in (constants.OP_STATUS_WAITLOCK,
                               constants.OP_STATUS_CANCELING)
444

445
446
    # All locks are acquired by now
    self._job.lock_status = None
447

448
    # Cancel here if we were asked to
449
    self._CheckCancel()
450

451
    logging.debug("Opcode is now running")
452
453
454
455
456
    self._op.status = constants.OP_STATUS_RUNNING
    self._op.exec_timestamp = TimeStampNow()

    # And finally replicate the job status
    self._queue.UpdateJobUnlocked(self._job)
457

458
  @locking.ssynchronized(_QUEUE, shared=1)
459
460
461
462
463
464
465
466
  def _AppendFeedback(self, timestamp, log_type, log_msg):
    """Internal feedback append function, with locks

    """
    self._job.log_serial += 1
    self._op.log.append((self._job.log_serial, timestamp, log_type, log_msg))
    self._queue.UpdateJobUnlocked(self._job, replicate=False)

467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
  def Feedback(self, *args):
    """Append a log entry.

    """
    assert len(args) < 3

    if len(args) == 1:
      log_type = constants.ELOG_MESSAGE
      log_msg = args[0]
    else:
      (log_type, log_msg) = args

    # The time is split to make serialization easier and not lose
    # precision.
    timestamp = utils.SplitTime(time.time())
482
    self._AppendFeedback(timestamp, log_type, log_msg)
483

484
485
486
487
488
489
  def ReportLocks(self, msg):
    """Write locking information to the job.

    Called whenever the LU processor is waiting for a lock or has acquired one.

    """
490
491
492
    assert self._op.status in (constants.OP_STATUS_WAITLOCK,
                               constants.OP_STATUS_CANCELING)

493
494
495
    # Not getting the queue lock because this is a single assignment
    self._job.lock_status = msg

496
497
498
    # Cancel here if we were asked to
    self._CheckCancel()

499

500
501
502
class _JobChangesChecker(object):
  def __init__(self, fields, prev_job_info, prev_log_serial):
    """Initializes this class.
Guido Trotter's avatar
Guido Trotter committed
503

504
505
506
507
508
509
    @type fields: list of strings
    @param fields: Fields requested by LUXI client
    @type prev_job_info: string
    @param prev_job_info: previous job info, as passed by the LUXI client
    @type prev_log_serial: string
    @param prev_log_serial: previous job serial, as passed by the LUXI client
Guido Trotter's avatar
Guido Trotter committed
510

511
512
513
514
    """
    self._fields = fields
    self._prev_job_info = prev_job_info
    self._prev_log_serial = prev_log_serial
Guido Trotter's avatar
Guido Trotter committed
515

516
517
  def __call__(self, job):
    """Checks whether job has changed.
Guido Trotter's avatar
Guido Trotter committed
518

519
520
    @type job: L{_QueuedJob}
    @param job: Job object
Guido Trotter's avatar
Guido Trotter committed
521
522

    """
523
524
525
    status = job.CalcStatus()
    job_info = job.GetInfo(self._fields)
    log_entries = job.GetLogEntries(self._prev_log_serial)
Guido Trotter's avatar
Guido Trotter committed
526
527
528
529
530
531
532
533

    # Serializing and deserializing data can cause type changes (e.g. from
    # tuple to list) or precision loss. We're doing it here so that we get
    # the same modifications as the data received from the client. Without
    # this, the comparison afterwards might fail without the data being
    # significantly different.
    # TODO: we just deserialized from disk, investigate how to make sure that
    # the job info and log entries are compatible to avoid this further step.
534
535
536
537
    # TODO: Doing something like in testutils.py:UnifyValueType might be more
    # efficient, though floats will be tricky
    job_info = serializer.LoadJson(serializer.DumpJson(job_info))
    log_entries = serializer.LoadJson(serializer.DumpJson(log_entries))
Guido Trotter's avatar
Guido Trotter committed
538
539
540

    # Don't even try to wait if the job is no longer running, there will be
    # no changes.
541
542
543
544
545
546
547
    if (status not in (constants.JOB_STATUS_QUEUED,
                       constants.JOB_STATUS_RUNNING,
                       constants.JOB_STATUS_WAITLOCK) or
        job_info != self._prev_job_info or
        (log_entries and self._prev_log_serial != log_entries[0][0])):
      logging.debug("Job %s changed", job.id)
      return (job_info, log_entries)
Guido Trotter's avatar
Guido Trotter committed
548

549
550
551
552
553
554
555
556
557
558
    return None


class _JobFileChangesWaiter(object):
  def __init__(self, filename):
    """Initializes this class.

    @type filename: string
    @param filename: Path to job file
    @raises errors.InotifyError: if the notifier cannot be setup
Guido Trotter's avatar
Guido Trotter committed
559

560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
    """
    self._wm = pyinotify.WatchManager()
    self._inotify_handler = \
      asyncnotifier.SingleFileEventHandler(self._wm, self._OnInotify, filename)
    self._notifier = \
      pyinotify.Notifier(self._wm, default_proc_fun=self._inotify_handler)
    try:
      self._inotify_handler.enable()
    except Exception:
      # pyinotify doesn't close file descriptors automatically
      self._notifier.stop()
      raise

  def _OnInotify(self, notifier_enabled):
    """Callback for inotify.

    """
Guido Trotter's avatar
Guido Trotter committed
577
    if not notifier_enabled:
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
      self._inotify_handler.enable()

  def Wait(self, timeout):
    """Waits for the job file to change.

    @type timeout: float
    @param timeout: Timeout in seconds
    @return: Whether there have been events

    """
    assert timeout >= 0
    have_events = self._notifier.check_events(timeout * 1000)
    if have_events:
      self._notifier.read_events()
    self._notifier.process_events()
    return have_events

  def Close(self):
    """Closes underlying notifier and its file descriptor.

    """
    self._notifier.stop()


class _JobChangesWaiter(object):
  def __init__(self, filename):
    """Initializes this class.

    @type filename: string
    @param filename: Path to job file

    """
    self._filewaiter = None
    self._filename = filename
Guido Trotter's avatar
Guido Trotter committed
612

613
614
  def Wait(self, timeout):
    """Waits for a job to change.
Guido Trotter's avatar
Guido Trotter committed
615

616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
    @type timeout: float
    @param timeout: Timeout in seconds
    @return: Whether there have been events

    """
    if self._filewaiter:
      return self._filewaiter.Wait(timeout)

    # Lazy setup: Avoid inotify setup cost when job file has already changed.
    # If this point is reached, return immediately and let caller check the job
    # file again in case there were changes since the last check. This avoids a
    # race condition.
    self._filewaiter = _JobFileChangesWaiter(self._filename)

    return True

  def Close(self):
    """Closes underlying waiter.

    """
    if self._filewaiter:
      self._filewaiter.Close()


class _WaitForJobChangesHelper(object):
  """Helper class using inotify to wait for changes in a job file.

  This class takes a previous job status and serial, and alerts the client when
  the current job status has changed.

  """
  @staticmethod
  def _CheckForChanges(job_load_fn, check_fn):
    job = job_load_fn()
    if not job:
      raise errors.JobLost()

    result = check_fn(job)
    if result is None:
      raise utils.RetryAgain()

    return result

  def __call__(self, filename, job_load_fn,
               fields, prev_job_info, prev_log_serial, timeout):
    """Waits for changes on a job.

    @type filename: string
    @param filename: File on which to wait for changes
    @type job_load_fn: callable
    @param job_load_fn: Function to load job
    @type fields: list of strings
    @param fields: Which fields to check for changes
    @type prev_job_info: list or None
    @param prev_job_info: Last job information returned
    @type prev_log_serial: int
    @param prev_log_serial: Last job message serial number
    @type timeout: float
    @param timeout: maximum time to wait in seconds

    """
Guido Trotter's avatar
Guido Trotter committed
677
    try:
678
679
680
681
682
683
684
685
686
      check_fn = _JobChangesChecker(fields, prev_job_info, prev_log_serial)
      waiter = _JobChangesWaiter(filename)
      try:
        return utils.Retry(compat.partial(self._CheckForChanges,
                                          job_load_fn, check_fn),
                           utils.RETRY_REMAINING_TIME, timeout,
                           wait_fn=waiter.Wait)
      finally:
        waiter.Close()
Guido Trotter's avatar
Guido Trotter committed
687
688
689
690
691
692
    except (errors.InotifyError, errors.JobLost):
      return None
    except utils.RetryTimeout:
      return constants.JOB_NOTCHANGED


693
694
695
696
class _JobQueueWorker(workerpool.BaseWorker):
  """The actual job workers.

  """
Iustin Pop's avatar
Iustin Pop committed
697
  def RunTask(self, job): # pylint: disable-msg=W0221
Michael Hanselmann's avatar
Michael Hanselmann committed
698
699
    """Job executor.

700
701
    This functions processes a job. It is closely tied to the _QueuedJob and
    _QueuedOpCode classes.
Michael Hanselmann's avatar
Michael Hanselmann committed
702

703
704
705
    @type job: L{_QueuedJob}
    @param job: the job to be processed

Michael Hanselmann's avatar
Michael Hanselmann committed
706
    """
707
    logging.info("Processing job %s", job.id)
708
    proc = mcpu.Processor(self.pool.queue.context, job.id)
709
    queue = job.queue
Michael Hanselmann's avatar
Michael Hanselmann committed
710
    try:
Michael Hanselmann's avatar
Michael Hanselmann committed
711
712
713
      try:
        count = len(job.ops)
        for idx, op in enumerate(job.ops):
714
          op_summary = op.input.Summary()
715
716
717
718
719
720
721
722
723
          if op.status == constants.OP_STATUS_SUCCESS:
            # this is a job that was partially completed before master
            # daemon shutdown, so it can be expected that some opcodes
            # are already completed successfully (if any did error
            # out, then the whole job should have been aborted and not
            # resubmitted for processing)
            logging.info("Op %s/%s: opcode %s already processed, skipping",
                         idx + 1, count, op_summary)
            continue
Michael Hanselmann's avatar
Michael Hanselmann committed
724
          try:
725
726
            logging.info("Op %s/%s: Starting opcode %s", idx + 1, count,
                         op_summary)
Michael Hanselmann's avatar
Michael Hanselmann committed
727

728
            queue.acquire(shared=1)
Michael Hanselmann's avatar
Michael Hanselmann committed
729
            try:
730
              if op.status == constants.OP_STATUS_CANCELED:
731
                logging.debug("Canceling opcode")
732
                raise CancelJob()
733
              assert op.status == constants.OP_STATUS_QUEUED
734
735
              logging.debug("Opcode %s/%s waiting for locks",
                            idx + 1, count)
Iustin Pop's avatar
Iustin Pop committed
736
              op.status = constants.OP_STATUS_WAITLOCK
Michael Hanselmann's avatar
Michael Hanselmann committed
737
              op.result = None
738
              op.start_timestamp = TimeStampNow()
739
740
              if idx == 0: # first opcode
                job.start_timestamp = op.start_timestamp
Michael Hanselmann's avatar
Michael Hanselmann committed
741
742
              queue.UpdateJobUnlocked(job)

Iustin Pop's avatar
Iustin Pop committed
743
              input_opcode = op.input
Michael Hanselmann's avatar
Michael Hanselmann committed
744
745
746
            finally:
              queue.release()

747
748
            # Make sure not to hold queue lock while calling ExecOpCode
            result = proc.ExecOpCode(input_opcode,
749
                                     _OpExecCallbacks(queue, job, op))
Michael Hanselmann's avatar
Michael Hanselmann committed
750

751
            queue.acquire(shared=1)
Michael Hanselmann's avatar
Michael Hanselmann committed
752
            try:
753
              logging.debug("Opcode %s/%s succeeded", idx + 1, count)
Michael Hanselmann's avatar
Michael Hanselmann committed
754
755
              op.status = constants.OP_STATUS_SUCCESS
              op.result = result
756
              op.end_timestamp = TimeStampNow()
757
758
759
              if idx == count - 1:
                job.lock_status = None
                job.end_timestamp = TimeStampNow()
760
761
762
763
764

                # Consistency check
                assert compat.all(i.status == constants.OP_STATUS_SUCCESS
                                  for i in job.ops)

Michael Hanselmann's avatar
Michael Hanselmann committed
765
766
767
768
              queue.UpdateJobUnlocked(job)
            finally:
              queue.release()

769
770
            logging.info("Op %s/%s: Successfully finished opcode %s",
                         idx + 1, count, op_summary)
771
772
773
          except CancelJob:
            # Will be handled further up
            raise
Michael Hanselmann's avatar
Michael Hanselmann committed
774
          except Exception, err:
775
            queue.acquire(shared=1)
Michael Hanselmann's avatar
Michael Hanselmann committed
776
777
            try:
              try:
778
                logging.debug("Opcode %s/%s failed", idx + 1, count)
Michael Hanselmann's avatar
Michael Hanselmann committed
779
                op.status = constants.OP_STATUS_ERROR
780
                if isinstance(err, errors.GenericError):
781
                  to_encode = err
782
                else:
783
784
                  to_encode = errors.OpExecError(str(err))
                op.result = errors.EncodeException(to_encode)
785
                op.end_timestamp = TimeStampNow()
786
787
                logging.info("Op %s/%s: Error in opcode %s: %s",
                             idx + 1, count, op_summary, err)
788
789
790
791
792
793
794
795
796
797
798

                to_encode = errors.OpExecError("Preceding opcode failed")
                job.MarkUnfinishedOps(constants.OP_STATUS_ERROR,
                                      errors.EncodeException(to_encode))

                # Consistency check
                assert compat.all(i.status == constants.OP_STATUS_SUCCESS
                                  for i in job.ops[:idx])
                assert compat.all(i.status == constants.OP_STATUS_ERROR and
                                  errors.GetEncodedError(i.result)
                                  for i in job.ops[idx:])
Michael Hanselmann's avatar
Michael Hanselmann committed
799
              finally:
800
801
                job.lock_status = None
                job.end_timestamp = TimeStampNow()
Michael Hanselmann's avatar
Michael Hanselmann committed
802
803
804
805
806
                queue.UpdateJobUnlocked(job)
            finally:
              queue.release()
            raise

807
      except CancelJob:
808
        queue.acquire(shared=1)
809
        try:
810
811
          job.MarkUnfinishedOps(constants.OP_STATUS_CANCELED,
                                "Job canceled by request")
812
813
814
          job.lock_status = None
          job.end_timestamp = TimeStampNow()
          queue.UpdateJobUnlocked(job)
815
816
        finally:
          queue.release()
Michael Hanselmann's avatar
Michael Hanselmann committed
817
818
819
820
      except errors.GenericError, err:
        logging.exception("Ganeti exception")
      except:
        logging.exception("Unhandled exception")
Michael Hanselmann's avatar
Michael Hanselmann committed
821
    finally:
822
823
      status = job.CalcStatus()
      logging.info("Finished job %s, status = %s", job.id, status)
Michael Hanselmann's avatar
Michael Hanselmann committed
824
825
826


class _JobQueueWorkerPool(workerpool.WorkerPool):
827
828
829
  """Simple class implementing a job-processing workerpool.

  """
830
  def __init__(self, queue):
831
832
    super(_JobQueueWorkerPool, self).__init__("JobQueue",
                                              JOBQUEUE_THREADS,
Michael Hanselmann's avatar
Michael Hanselmann committed
833
                                              _JobQueueWorker)
834
    self.queue = queue
Michael Hanselmann's avatar
Michael Hanselmann committed
835
836


Iustin Pop's avatar
Iustin Pop committed
837
838
def _RequireOpenQueue(fn):
  """Decorator for "public" functions.
839

Iustin Pop's avatar
Iustin Pop committed
840
841
842
843
  This function should be used for all 'public' functions. That is,
  functions usually called from other classes. Note that this should
  be applied only to methods (not plain functions), since it expects
  that the decorated function is called with a first argument that has
844
  a '_queue_filelock' argument.
845

846
  @warning: Use this decorator only after locking.ssynchronized
847

Iustin Pop's avatar
Iustin Pop committed
848
  Example::
849
    @locking.ssynchronized(_LOCK)
Iustin Pop's avatar
Iustin Pop committed
850
851
852
    @_RequireOpenQueue
    def Example(self):
      pass
853

Iustin Pop's avatar
Iustin Pop committed
854
855
  """
  def wrapper(self, *args, **kwargs):
Iustin Pop's avatar
Iustin Pop committed
856
    # pylint: disable-msg=W0212
857
    assert self._queue_filelock is not None, "Queue should be open"
Iustin Pop's avatar
Iustin Pop committed
858
859
    return fn(self, *args, **kwargs)
  return wrapper
860
861


Iustin Pop's avatar
Iustin Pop committed
862
863
class JobQueue(object):
  """Queue used to manage the jobs.
864

Iustin Pop's avatar
Iustin Pop committed
865
866
867
868
  @cvar _RE_JOB_FILE: regex matching the valid job file names

  """
  _RE_JOB_FILE = re.compile(r"^job-(%s)$" % constants.JOB_ID_TEMPLATE)
869

Michael Hanselmann's avatar
Michael Hanselmann committed
870
  def __init__(self, context):
871
872
873
874
875
876
877
878
879
880
881
882
    """Constructor for JobQueue.

    The constructor will initialize the job queue object and then
    start loading the current jobs from disk, either for starting them
    (if they were queue) or for aborting them (if they were already
    running).

    @type context: GanetiContext
    @param context: the context object for access to the configuration
        data and other ganeti objects

    """
883
    self.context = context
884
    self._memcache = weakref.WeakValueDictionary()
885
    self._my_hostname = netutils.HostInfo().name
886

887
888
889
890
891
    # The Big JobQueue lock. If a code block or method acquires it in shared
    # mode safe it must guarantee concurrency with all the code acquiring it in
    # shared mode, including itself. In order not to acquire it at all
    # concurrency must be guaranteed with all code acquiring it in shared mode
    # and all code acquiring it exclusively.
892
    self._lock = locking.SharedLock("JobQueue")
893
894
895

    self.acquire = self._lock.acquire
    self.release = self._lock.release
Michael Hanselmann's avatar
Michael Hanselmann committed
896

897
898
899
    # Initialize the queue, and acquire the filelock.
    # This ensures no other process is working on the job queue.
    self._queue_filelock = jstore.InitAndVerifyQueue(must_lock=True)
900

901
902
903
904
    # Read serial file
    self._last_serial = jstore.ReadSerial()
    assert self._last_serial is not None, ("Serial file was modified between"
                                           " check in jstore and here")
905

906
    # Get initial list of nodes
907
    self._nodes = dict((n.name, n.primary_ip)
908
909
                       for n in self.context.cfg.GetAllNodesInfo().values()
                       if n.master_candidate)
910
911

    # Remove master node
912
    self._nodes.pop(self._my_hostname, None)
913
914
915

    # TODO: Check consistency across nodes

916
917
918
919
    self._queue_size = 0
    self._UpdateQueueSizeUnlocked()
    self._drained = self._IsQueueMarkedDrain()

Michael Hanselmann's avatar
Michael Hanselmann committed
920
    # Setup worker pool
921
    self._wpool = _JobQueueWorkerPool(self)
Michael Hanselmann's avatar
Michael Hanselmann committed
922
    try:
923
924
925
926
      # We need to lock here because WorkerPool.AddTask() may start a job while
      # we're still doing our work.
      self.acquire()
      try:
927
928
929
        logging.info("Inspecting job queue")

        all_job_ids = self._GetJobIDsUnlocked()
930
        jobs_count = len(all_job_ids)
931
932
933
        lastinfo = time.time()
        for idx, job_id in enumerate(all_job_ids):
          # Give an update every 1000 jobs or 10 seconds
934
935
          if (idx % 1000 == 0 or time.time() >= (lastinfo + 10.0) or
              idx == (jobs_count - 1)):
936
            logging.info("Job queue inspection: %d/%d (%0.1f %%)",
937
                         idx, jobs_count - 1, 100.0 * (idx + 1) / jobs_count)
938
939
940
941
            lastinfo = time.time()

          job = self._LoadJobUnlocked(job_id)

942
943
944
          # a failure in loading the job can cause 'None' to be returned
          if job is None:
            continue
945

946
          status = job.CalcStatus()
Michael Hanselmann's avatar
Michael Hanselmann committed
947

948
          if status in (constants.JOB_STATUS_QUEUED, ):
949
            self._wpool.AddTask((job, ))
Michael Hanselmann's avatar
Michael Hanselmann committed
950

951
          elif status in (constants.JOB_STATUS_RUNNING,
952
953
                          constants.JOB_STATUS_WAITLOCK,
                          constants.JOB_STATUS_CANCELING):
954
            logging.warning("Unfinished job %s found: %s", job.id, job)
955
956
            job.MarkUnfinishedOps(constants.OP_STATUS_ERROR,
                                  "Unclean master daemon shutdown")
957
958

        logging.info("Job queue inspection finished")
959
960
961
962
963
      finally:
        self.release()
    except:
      self._wpool.TerminateWorkers()
      raise
Michael Hanselmann's avatar
Michael Hanselmann committed
964

965
  @locking.ssynchronized(_LOCK)
966
  @_RequireOpenQueue
967
968
969
970
971
972
973
974
  def AddNode(self, node):
    """Register a new node with the queue.

    @type node: L{objects.Node}
    @param node: the node object to be added

    """
    node_name = node.name
975
    assert node_name != self._my_hostname
976

977
    # Clean queue directory on added node
978
    result = rpc.RpcRunner.call_jobqueue_purge(node_name)
979
    msg = result.fail_msg
980
981
982
    if msg:
      logging.warning("Cannot cleanup queue directory on node %s: %s",
                      node_name, msg)
983

984
985
986
987
988
989
    if not node.master_candidate:
      # remove if existing, ignoring errors
      self._nodes.pop(node_name, None)
      # and skip the replication of the job ids
      return

990
991
    # Upload the whole queue excluding archived jobs
    files = [self._GetJobPath(job_id) for job_id in self._GetJobIDsUnlocked()]
992

993
994
995
996
    # Upload current serial file
    files.append(constants.JOB_QUEUE_SERIAL_FILE)

    for file_name in files:
997
      # Read file content
998
      content = utils.ReadFile(file_name)
999

1000
1001
1002
      result = rpc.RpcRunner.call_jobqueue_update([node_name],
                                                  [node.primary_ip],
                                                  file_name, content)
1003
      msg = result[node_name].fail_msg
1004
1005
1006
      if msg:
        logging.error("Failed to upload file %s to node %s: %s",
                      file_name, node_name, msg)
1007

1008
    self._nodes[node_name] = node.primary_ip
1009

1010
  @locking.ssynchronized(_LOCK)
1011
1012
  @_RequireOpenQueue
  def RemoveNode(self, node_name):
1013
1014
1015
1016
1017
1018
    """Callback called when removing nodes from the cluster.

    @type node_name: str
    @param node_name: the name of the node to remove

    """
1019
    self._nodes.pop(node_name, None)
1020

1021
1022
  @staticmethod
  def _CheckRpcResult(result, nodes, failmsg):
1023
1024
1025
1026
    """Verifies the status of an RPC call.

    Since we aim to keep consistency should this node (the current
    master) fail, we will log errors if our rpc fail, and especially
Michael Hanselmann's avatar
Michael Hanselmann committed
1027
    log the case when more than half of the nodes fails.
1028
1029
1030
1031
1032
1033
1034
1035

    @param result: the data as returned from the rpc call
    @type nodes: list
    @param nodes: the list of nodes we made the call to
    @type failmsg: str
    @param failmsg: the identifier to be used for logging

    """
1036
1037
1038
1039
    failed = []
    success = []

    for node in nodes:
1040
      msg = result[node].fail_msg
1041
      if msg:
1042
        failed.append(node)
1043
1044
        logging.error("RPC call %s (%s) failed on node %s: %s",
                      result[node].call, failmsg, node, msg)
1045
1046
      else:
        success.append(node)
1047
1048
1049
1050
1051
1052

    # +1 for the master node
    if (len(success) + 1) < len(failed):
      # TODO: Handle failing nodes
      logging.error("More than half of the nodes failed")

1053
1054
1055
  def _GetNodeIp(self):
    """Helper for returning the node name/ip list.

1056
1057
1058
1059
    @rtype: (list, list)
    @return: a tuple of two lists, the first one with the node
        names and the second one with the node addresses

1060
    """
1061
    # TODO: Change to "tuple(map(list, zip(*self._nodes.items())))"?
1062
1063
1064
1065
    name_list = self._nodes.keys()
    addr_list = [self._nodes[name] for name in name_list]
    return name_list, addr_list

1066
  def _UpdateJobQueueFile(self, file_name, data, replicate):
1067
1068
    """Writes a file locally and then replicates it to all nodes.

1069
1070
1071
1072
1073
1074
1075
    This function will replace the contents of a file on the local
    node and then replicate it to all the other nodes we have.

    @type file_name: str
    @param file_name: the path of the file to be replicated
    @type data: str
    @param data: the new contents of the file
1076
1077
    @type replicate: boolean
    @param replicate: whether to spread the changes to the remote nodes
1078

1079
1080
1081
    """
    utils.WriteFile(file_name, data=data)

1082
1083
1084
1085
    if replicate:
      names, addrs = self._GetNodeIp()
      result = rpc.RpcRunner.call_jobqueue_update(names, addrs, file_name, data)
      self._CheckRpcResult(result, self._nodes, "Updating %s" % file_name)
1086

1087
  def _RenameFilesUnlocked(self, rename):
1088
1089
1090
1091
1092
    """Renames a file locally and then replicate the change.

    This function will rename a file in the local queue directory
    and then replicate this rename to all the other nodes we have.

1093
1094
    @type rename: list of (old, new)
    @param rename: List containing tuples mapping old to new names
1095
1096

    """
1097
    # Rename them locally
1098
1099
    for old, new in rename:
      utils.RenameFile(old, new, mkdir=True)
1100

1101
1102
1103
1104
    # ... and on all nodes
    names, addrs = self._GetNodeIp()
    result = rpc.RpcRunner.call_jobqueue_rename(names, addrs, rename)
    self._CheckRpcResult(result, self._nodes, "Renaming files (%r)" % rename)
1105

1106
1107
  @staticmethod
  def _FormatJobID(job_id):
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
    """Convert a job ID to string format.

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

    @type job_id: int or long
    @param job_id: the numeric job id
    @rtype: str
    @return: the formatted job id

    """
Michael Hanselmann's avatar
Michael Hanselmann committed
1120
1121
1122
1123
1124
1125
1126
    if not isinstance(job_id, (int, long)):
      raise errors.ProgrammerError("Job ID '%s' not numeric" % job_id)
    if job_id < 0:
      raise errors.ProgrammerError("Job ID %s is negative" % job_id)

    return str(job_id)

1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
  @classmethod
  def _GetArchiveDirectory(cls, job_id):
    """Returns the archive directory for a job.

    @type job_id: str
    @param job_id: Job identifier
    @rtype: str
    @return: Directory name

    """
    return str(int(job_id) / JOBS_PER_ARCHIVE_DIRECTORY)

Iustin Pop's avatar
Iustin Pop committed
1139
  def _NewSerialsUnlocked(self, count):
1140
1141
1142
1143
    """Generates a new job identifier.

    Job identifiers are unique during the lifetime of a cluster.

Iustin Pop's avatar
Iustin Pop committed
1144
1145
    @type count: integer
    @param count: how many serials to return
1146
1147
    @rtype: str
    @return: a string representing the job identifier.
1148
1149

    """
Iustin Pop's avatar
Iustin Pop committed
1150
    assert count > 0
1151
    # New number
Iustin Pop's avatar
Iustin Pop committed
1152
    serial = self._last_serial + count
1153
1154

    # Write to file
1155
1156
    self._UpdateJobQueueFile(constants.JOB_QUEUE_SERIAL_FILE,
                             "%s\n" % serial, True)
1157