Statistics
| Branch: | Tag: | Revision:

root / lib / jqueue.py @ 69b03fd7

History | View | Annotate | Download (41.8 kB)

1
#
2
#
3

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

    
21

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

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

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

30
"""
31

    
32
import os
33
import logging
34
import threading
35
import errno
36
import re
37
import time
38
import weakref
39

    
40
from ganeti import constants
41
from ganeti import serializer
42
from ganeti import workerpool
43
from ganeti import opcodes
44
from ganeti import errors
45
from ganeti import mcpu
46
from ganeti import utils
47
from ganeti import jstore
48
from ganeti import rpc
49

    
50

    
51
JOBQUEUE_THREADS = 25
52
JOBS_PER_ARCHIVE_DIRECTORY = 10000
53

    
54

    
55
class CancelJob(Exception):
56
  """Special exception to cancel a job.
57

58
  """
59

    
60

    
61
def TimeStampNow():
62
  """Returns the current timestamp.
63

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

67
  """
68
  return utils.SplitTime(time.time())
69

    
70

    
71
class _QueuedOpCode(object):
72
  """Encapsulates an opcode object.
73

74
  @ivar log: holds the execution log and consists of tuples
75
  of the form C{(log_serial, timestamp, level, message)}
76
  @ivar input: the OpCode we encapsulate
77
  @ivar status: the current status
78
  @ivar result: the result of the LU execution
79
  @ivar start_timestamp: timestamp for the start of the execution
80
  @ivar exec_timestamp: timestamp for the actual LU Exec() function invocation
81
  @ivar stop_timestamp: timestamp for the end of the execution
82

83
  """
84
  __slots__ = ["input", "status", "result", "log",
85
               "start_timestamp", "exec_timestamp", "end_timestamp",
86
               "__weakref__"]
87

    
88
  def __init__(self, op):
89
    """Constructor for the _QuededOpCode.
90

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

94
    """
95
    self.input = op
96
    self.status = constants.OP_STATUS_QUEUED
97
    self.result = None
98
    self.log = []
99
    self.start_timestamp = None
100
    self.exec_timestamp = None
101
    self.end_timestamp = None
102

    
103
  @classmethod
104
  def Restore(cls, state):
105
    """Restore the _QueuedOpCode from the serialized form.
106

107
    @type state: dict
108
    @param state: the serialized state
109
    @rtype: _QueuedOpCode
110
    @return: a new _QueuedOpCode instance
111

112
    """
113
    obj = _QueuedOpCode.__new__(cls)
114
    obj.input = opcodes.OpCode.LoadOpCode(state["input"])
115
    obj.status = state["status"]
116
    obj.result = state["result"]
117
    obj.log = state["log"]
118
    obj.start_timestamp = state.get("start_timestamp", None)
119
    obj.exec_timestamp = state.get("exec_timestamp", None)
120
    obj.end_timestamp = state.get("end_timestamp", None)
121
    return obj
122

    
123
  def Serialize(self):
124
    """Serializes this _QueuedOpCode.
125

126
    @rtype: dict
127
    @return: the dictionary holding the serialized state
128

129
    """
130
    return {
131
      "input": self.input.__getstate__(),
132
      "status": self.status,
133
      "result": self.result,
134
      "log": self.log,
135
      "start_timestamp": self.start_timestamp,
136
      "exec_timestamp": self.exec_timestamp,
137
      "end_timestamp": self.end_timestamp,
138
      }
139

    
140

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

144
  This is what we use to track the user-submitted jobs. Locking must
145
  be taken care of by users of this class.
146

147
  @type queue: L{JobQueue}
148
  @ivar queue: the parent queue
149
  @ivar id: the job ID
150
  @type ops: list
151
  @ivar ops: the list of _QueuedOpCode that constitute the job
152
  @type log_serial: int
153
  @ivar log_serial: holds the index for the next log entry
154
  @ivar received_timestamp: the timestamp for when the job was received
155
  @ivar start_timestmap: the timestamp for start of execution
156
  @ivar end_timestamp: the timestamp for end of execution
157
  @ivar lock_status: In-memory locking information for debugging
158
  @ivar change: a Condition variable we use for waiting for job changes
159

160
  """
161
  # pylint: disable-msg=W0212
162
  __slots__ = ["queue", "id", "ops", "log_serial",
163
               "received_timestamp", "start_timestamp", "end_timestamp",
164
               "lock_status", "change",
165
               "__weakref__"]
166

    
167
  def __init__(self, queue, job_id, ops):
168
    """Constructor for the _QueuedJob.
169

170
    @type queue: L{JobQueue}
171
    @param queue: our parent queue
172
    @type job_id: job_id
173
    @param job_id: our job id
174
    @type ops: list
175
    @param ops: the list of opcodes we hold, which will be encapsulated
176
        in _QueuedOpCodes
177

178
    """
179
    if not ops:
180
      raise errors.GenericError("A job needs at least one opcode")
181

    
182
    self.queue = queue
183
    self.id = job_id
184
    self.ops = [_QueuedOpCode(op) for op in ops]
185
    self.log_serial = 0
186
    self.received_timestamp = TimeStampNow()
187
    self.start_timestamp = None
188
    self.end_timestamp = None
189

    
190
    # In-memory attributes
191
    self.lock_status = None
192

    
193
    # Condition to wait for changes
194
    self.change = threading.Condition(self.queue._lock)
195

    
196
  def __repr__(self):
197
    status = ["%s.%s" % (self.__class__.__module__, self.__class__.__name__),
198
              "id=%s" % self.id,
199
              "ops=%s" % ",".join([op.input.Summary() for op in self.ops])]
200

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

    
203
  @classmethod
204
  def Restore(cls, queue, state):
205
    """Restore a _QueuedJob from serialized state:
206

207
    @type queue: L{JobQueue}
208
    @param queue: to which queue the restored job belongs
209
    @type state: dict
210
    @param state: the serialized state
211
    @rtype: _JobQueue
212
    @return: the restored _JobQueue instance
213

214
    """
215
    obj = _QueuedJob.__new__(cls)
216
    obj.queue = queue
217
    obj.id = state["id"]
218
    obj.received_timestamp = state.get("received_timestamp", None)
219
    obj.start_timestamp = state.get("start_timestamp", None)
220
    obj.end_timestamp = state.get("end_timestamp", None)
221

    
222
    # In-memory attributes
223
    obj.lock_status = None
224

    
225
    obj.ops = []
226
    obj.log_serial = 0
227
    for op_state in state["ops"]:
228
      op = _QueuedOpCode.Restore(op_state)
229
      for log_entry in op.log:
230
        obj.log_serial = max(obj.log_serial, log_entry[0])
231
      obj.ops.append(op)
232

    
233
    # Condition to wait for changes
234
    obj.change = threading.Condition(obj.queue._lock)
235

    
236
    return obj
237

    
238
  def Serialize(self):
239
    """Serialize the _JobQueue instance.
240

241
    @rtype: dict
242
    @return: the serialized state
243

244
    """
245
    return {
246
      "id": self.id,
247
      "ops": [op.Serialize() for op in self.ops],
248
      "start_timestamp": self.start_timestamp,
249
      "end_timestamp": self.end_timestamp,
250
      "received_timestamp": self.received_timestamp,
251
      }
252

    
253
  def CalcStatus(self):
254
    """Compute the status of this job.
255

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

259
    The algorithm is:
260
      - if we find a cancelled, or finished with error, the job
261
        status will be the same
262
      - otherwise, the last opcode with the status one of:
263
          - waitlock
264
          - canceling
265
          - running
266

267
        will determine the job status
268

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

272
    @return: the job status
273

274
    """
275
    status = constants.JOB_STATUS_QUEUED
276

    
277
    all_success = True
278
    for op in self.ops:
279
      if op.status == constants.OP_STATUS_SUCCESS:
280
        continue
281

    
282
      all_success = False
283

    
284
      if op.status == constants.OP_STATUS_QUEUED:
285
        pass
286
      elif op.status == constants.OP_STATUS_WAITLOCK:
287
        status = constants.JOB_STATUS_WAITLOCK
288
      elif op.status == constants.OP_STATUS_RUNNING:
289
        status = constants.JOB_STATUS_RUNNING
290
      elif op.status == constants.OP_STATUS_CANCELING:
291
        status = constants.JOB_STATUS_CANCELING
292
        break
293
      elif op.status == constants.OP_STATUS_ERROR:
294
        status = constants.JOB_STATUS_ERROR
295
        # The whole job fails if one opcode failed
296
        break
297
      elif op.status == constants.OP_STATUS_CANCELED:
298
        status = constants.OP_STATUS_CANCELED
299
        break
300

    
301
    if all_success:
302
      status = constants.JOB_STATUS_SUCCESS
303

    
304
    return status
305

    
306
  def GetLogEntries(self, newer_than):
307
    """Selectively returns the log entries.
308

309
    @type newer_than: None or int
310
    @param newer_than: if this is None, return all log entries,
311
        otherwise return only the log entries with serial higher
312
        than this value
313
    @rtype: list
314
    @return: the list of the log entries selected
315

316
    """
317
    if newer_than is None:
318
      serial = -1
319
    else:
320
      serial = newer_than
321

    
322
    entries = []
323
    for op in self.ops:
324
      entries.extend(filter(lambda entry: entry[0] > serial, op.log))
325

    
326
    return entries
327

    
328
  def MarkUnfinishedOps(self, status, result):
329
    """Mark unfinished opcodes with a given status and result.
330

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

335
    @param status: a given opcode status
336
    @param result: the opcode result
337

338
    """
339
    not_marked = True
340
    for op in self.ops:
341
      if op.status in constants.OPS_FINALIZED:
342
        assert not_marked, "Finalized opcodes found after non-finalized ones"
343
        continue
344
      op.status = status
345
      op.result = result
346
      not_marked = False
347

    
348

    
349
class _OpExecCallbacks(mcpu.OpExecCbBase):
350
  def __init__(self, queue, job, op):
351
    """Initializes this class.
352

353
    @type queue: L{JobQueue}
354
    @param queue: Job queue
355
    @type job: L{_QueuedJob}
356
    @param job: Job object
357
    @type op: L{_QueuedOpCode}
358
    @param op: OpCode
359

360
    """
361
    assert queue, "Queue is missing"
362
    assert job, "Job is missing"
363
    assert op, "Opcode is missing"
364

    
365
    self._queue = queue
366
    self._job = job
367
    self._op = op
368

    
369
  def NotifyStart(self):
370
    """Mark the opcode as running, not lock-waiting.
371

372
    This is called from the mcpu code as a notifier function, when the LU is
373
    finally about to start the Exec() method. Of course, to have end-user
374
    visible results, the opcode must be initially (before calling into
375
    Processor.ExecOpCode) set to OP_STATUS_WAITLOCK.
376

377
    """
378
    self._queue.acquire()
379
    try:
380
      assert self._op.status in (constants.OP_STATUS_WAITLOCK,
381
                                 constants.OP_STATUS_CANCELING)
382

    
383
      # All locks are acquired by now
384
      self._job.lock_status = None
385

    
386
      # Cancel here if we were asked to
387
      if self._op.status == constants.OP_STATUS_CANCELING:
388
        raise CancelJob()
389

    
390
      self._op.status = constants.OP_STATUS_RUNNING
391
      self._op.exec_timestamp = TimeStampNow()
392
    finally:
393
      self._queue.release()
394

    
395
  def Feedback(self, *args):
396
    """Append a log entry.
397

398
    """
399
    assert len(args) < 3
400

    
401
    if len(args) == 1:
402
      log_type = constants.ELOG_MESSAGE
403
      log_msg = args[0]
404
    else:
405
      (log_type, log_msg) = args
406

    
407
    # The time is split to make serialization easier and not lose
408
    # precision.
409
    timestamp = utils.SplitTime(time.time())
410

    
411
    self._queue.acquire()
412
    try:
413
      self._job.log_serial += 1
414
      self._op.log.append((self._job.log_serial, timestamp, log_type, log_msg))
415

    
416
      self._job.change.notifyAll()
417
    finally:
418
      self._queue.release()
419

    
420
  def ReportLocks(self, msg):
421
    """Write locking information to the job.
422

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

425
    """
426
    # Not getting the queue lock because this is a single assignment
427
    self._job.lock_status = msg
428

    
429

    
430
class _JobQueueWorker(workerpool.BaseWorker):
431
  """The actual job workers.
432

433
  """
434
  def RunTask(self, job): # pylint: disable-msg=W0221
435
    """Job executor.
436

437
    This functions processes a job. It is closely tied to the _QueuedJob and
438
    _QueuedOpCode classes.
439

440
    @type job: L{_QueuedJob}
441
    @param job: the job to be processed
442

443
    """
444
    logging.info("Processing job %s", job.id)
445
    proc = mcpu.Processor(self.pool.queue.context, job.id)
446
    queue = job.queue
447
    try:
448
      try:
449
        count = len(job.ops)
450
        for idx, op in enumerate(job.ops):
451
          op_summary = op.input.Summary()
452
          if op.status == constants.OP_STATUS_SUCCESS:
453
            # this is a job that was partially completed before master
454
            # daemon shutdown, so it can be expected that some opcodes
455
            # are already completed successfully (if any did error
456
            # out, then the whole job should have been aborted and not
457
            # resubmitted for processing)
458
            logging.info("Op %s/%s: opcode %s already processed, skipping",
459
                         idx + 1, count, op_summary)
460
            continue
461
          try:
462
            logging.info("Op %s/%s: Starting opcode %s", idx + 1, count,
463
                         op_summary)
464

    
465
            queue.acquire()
466
            try:
467
              if op.status == constants.OP_STATUS_CANCELED:
468
                raise CancelJob()
469
              assert op.status == constants.OP_STATUS_QUEUED
470
              op.status = constants.OP_STATUS_WAITLOCK
471
              op.result = None
472
              op.start_timestamp = TimeStampNow()
473
              if idx == 0: # first opcode
474
                job.start_timestamp = op.start_timestamp
475
              queue.UpdateJobUnlocked(job)
476

    
477
              input_opcode = op.input
478
            finally:
479
              queue.release()
480

    
481
            # Make sure not to hold queue lock while calling ExecOpCode
482
            result = proc.ExecOpCode(input_opcode,
483
                                     _OpExecCallbacks(queue, job, op))
484

    
485
            queue.acquire()
486
            try:
487
              op.status = constants.OP_STATUS_SUCCESS
488
              op.result = result
489
              op.end_timestamp = TimeStampNow()
490
              queue.UpdateJobUnlocked(job)
491
            finally:
492
              queue.release()
493

    
494
            logging.info("Op %s/%s: Successfully finished opcode %s",
495
                         idx + 1, count, op_summary)
496
          except CancelJob:
497
            # Will be handled further up
498
            raise
499
          except Exception, err:
500
            queue.acquire()
501
            try:
502
              try:
503
                op.status = constants.OP_STATUS_ERROR
504
                if isinstance(err, errors.GenericError):
505
                  op.result = errors.EncodeException(err)
506
                else:
507
                  op.result = str(err)
508
                op.end_timestamp = TimeStampNow()
509
                logging.info("Op %s/%s: Error in opcode %s: %s",
510
                             idx + 1, count, op_summary, err)
511
              finally:
512
                queue.UpdateJobUnlocked(job)
513
            finally:
514
              queue.release()
515
            raise
516

    
517
      except CancelJob:
518
        queue.acquire()
519
        try:
520
          queue.CancelJobUnlocked(job)
521
        finally:
522
          queue.release()
523
      except errors.GenericError, err:
524
        logging.exception("Ganeti exception")
525
      except:
526
        logging.exception("Unhandled exception")
527
    finally:
528
      queue.acquire()
529
      try:
530
        try:
531
          job.lock_status = None
532
          job.end_timestamp = TimeStampNow()
533
          queue.UpdateJobUnlocked(job)
534
        finally:
535
          job_id = job.id
536
          status = job.CalcStatus()
537
      finally:
538
        queue.release()
539

    
540
      logging.info("Finished job %s, status = %s", job_id, status)
541

    
542

    
543
class _JobQueueWorkerPool(workerpool.WorkerPool):
544
  """Simple class implementing a job-processing workerpool.
545

546
  """
547
  def __init__(self, queue):
548
    super(_JobQueueWorkerPool, self).__init__("JobQueue",
549
                                              JOBQUEUE_THREADS,
550
                                              _JobQueueWorker)
551
    self.queue = queue
552

    
553

    
554
def _RequireOpenQueue(fn):
555
  """Decorator for "public" functions.
556

557
  This function should be used for all 'public' functions. That is,
558
  functions usually called from other classes. Note that this should
559
  be applied only to methods (not plain functions), since it expects
560
  that the decorated function is called with a first argument that has
561
  a '_queue_lock' argument.
562

563
  @warning: Use this decorator only after utils.LockedMethod!
564

565
  Example::
566
    @utils.LockedMethod
567
    @_RequireOpenQueue
568
    def Example(self):
569
      pass
570

571
  """
572
  def wrapper(self, *args, **kwargs):
573
    # pylint: disable-msg=W0212
574
    assert self._queue_lock is not None, "Queue should be open"
575
    return fn(self, *args, **kwargs)
576
  return wrapper
577

    
578

    
579
class JobQueue(object):
580
  """Queue used to manage the jobs.
581

582
  @cvar _RE_JOB_FILE: regex matching the valid job file names
583

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

    
587
  def __init__(self, context):
588
    """Constructor for JobQueue.
589

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

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

599
    """
600
    self.context = context
601
    self._memcache = weakref.WeakValueDictionary()
602
    self._my_hostname = utils.HostInfo().name
603

    
604
    # Locking
605
    self._lock = threading.Lock()
606
    self.acquire = self._lock.acquire
607
    self.release = self._lock.release
608

    
609
    # Initialize
610
    self._queue_lock = jstore.InitAndVerifyQueue(must_lock=True)
611

    
612
    # Read serial file
613
    self._last_serial = jstore.ReadSerial()
614
    assert self._last_serial is not None, ("Serial file was modified between"
615
                                           " check in jstore and here")
616

    
617
    # Get initial list of nodes
618
    self._nodes = dict((n.name, n.primary_ip)
619
                       for n in self.context.cfg.GetAllNodesInfo().values()
620
                       if n.master_candidate)
621

    
622
    # Remove master node
623
    try:
624
      del self._nodes[self._my_hostname]
625
    except KeyError:
626
      pass
627

    
628
    # TODO: Check consistency across nodes
629

    
630
    # Setup worker pool
631
    self._wpool = _JobQueueWorkerPool(self)
632
    try:
633
      # We need to lock here because WorkerPool.AddTask() may start a job while
634
      # we're still doing our work.
635
      self.acquire()
636
      try:
637
        logging.info("Inspecting job queue")
638

    
639
        all_job_ids = self._GetJobIDsUnlocked()
640
        jobs_count = len(all_job_ids)
641
        lastinfo = time.time()
642
        for idx, job_id in enumerate(all_job_ids):
643
          # Give an update every 1000 jobs or 10 seconds
644
          if (idx % 1000 == 0 or time.time() >= (lastinfo + 10.0) or
645
              idx == (jobs_count - 1)):
646
            logging.info("Job queue inspection: %d/%d (%0.1f %%)",
647
                         idx, jobs_count - 1, 100.0 * (idx + 1) / jobs_count)
648
            lastinfo = time.time()
649

    
650
          job = self._LoadJobUnlocked(job_id)
651

    
652
          # a failure in loading the job can cause 'None' to be returned
653
          if job is None:
654
            continue
655

    
656
          status = job.CalcStatus()
657

    
658
          if status in (constants.JOB_STATUS_QUEUED, ):
659
            self._wpool.AddTask(job)
660

    
661
          elif status in (constants.JOB_STATUS_RUNNING,
662
                          constants.JOB_STATUS_WAITLOCK,
663
                          constants.JOB_STATUS_CANCELING):
664
            logging.warning("Unfinished job %s found: %s", job.id, job)
665
            try:
666
              job.MarkUnfinishedOps(constants.OP_STATUS_ERROR,
667
                                    "Unclean master daemon shutdown")
668
            finally:
669
              self.UpdateJobUnlocked(job)
670

    
671
        logging.info("Job queue inspection finished")
672
      finally:
673
        self.release()
674
    except:
675
      self._wpool.TerminateWorkers()
676
      raise
677

    
678
  @utils.LockedMethod
679
  @_RequireOpenQueue
680
  def AddNode(self, node):
681
    """Register a new node with the queue.
682

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

686
    """
687
    node_name = node.name
688
    assert node_name != self._my_hostname
689

    
690
    # Clean queue directory on added node
691
    result = rpc.RpcRunner.call_jobqueue_purge(node_name)
692
    msg = result.fail_msg
693
    if msg:
694
      logging.warning("Cannot cleanup queue directory on node %s: %s",
695
                      node_name, msg)
696

    
697
    if not node.master_candidate:
698
      # remove if existing, ignoring errors
699
      self._nodes.pop(node_name, None)
700
      # and skip the replication of the job ids
701
      return
702

    
703
    # Upload the whole queue excluding archived jobs
704
    files = [self._GetJobPath(job_id) for job_id in self._GetJobIDsUnlocked()]
705

    
706
    # Upload current serial file
707
    files.append(constants.JOB_QUEUE_SERIAL_FILE)
708

    
709
    for file_name in files:
710
      # Read file content
711
      content = utils.ReadFile(file_name)
712

    
713
      result = rpc.RpcRunner.call_jobqueue_update([node_name],
714
                                                  [node.primary_ip],
715
                                                  file_name, content)
716
      msg = result[node_name].fail_msg
717
      if msg:
718
        logging.error("Failed to upload file %s to node %s: %s",
719
                      file_name, node_name, msg)
720

    
721
    self._nodes[node_name] = node.primary_ip
722

    
723
  @utils.LockedMethod
724
  @_RequireOpenQueue
725
  def RemoveNode(self, node_name):
726
    """Callback called when removing nodes from the cluster.
727

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

731
    """
732
    try:
733
      # The queue is removed by the "leave node" RPC call.
734
      del self._nodes[node_name]
735
    except KeyError:
736
      pass
737

    
738
  @staticmethod
739
  def _CheckRpcResult(result, nodes, failmsg):
740
    """Verifies the status of an RPC call.
741

742
    Since we aim to keep consistency should this node (the current
743
    master) fail, we will log errors if our rpc fail, and especially
744
    log the case when more than half of the nodes fails.
745

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

752
    """
753
    failed = []
754
    success = []
755

    
756
    for node in nodes:
757
      msg = result[node].fail_msg
758
      if msg:
759
        failed.append(node)
760
        logging.error("RPC call %s (%s) failed on node %s: %s",
761
                      result[node].call, failmsg, node, msg)
762
      else:
763
        success.append(node)
764

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

    
770
  def _GetNodeIp(self):
771
    """Helper for returning the node name/ip list.
772

773
    @rtype: (list, list)
774
    @return: a tuple of two lists, the first one with the node
775
        names and the second one with the node addresses
776

777
    """
778
    name_list = self._nodes.keys()
779
    addr_list = [self._nodes[name] for name in name_list]
780
    return name_list, addr_list
781

    
782
  def _WriteAndReplicateFileUnlocked(self, file_name, data):
783
    """Writes a file locally and then replicates it to all nodes.
784

785
    This function will replace the contents of a file on the local
786
    node and then replicate it to all the other nodes we have.
787

788
    @type file_name: str
789
    @param file_name: the path of the file to be replicated
790
    @type data: str
791
    @param data: the new contents of the file
792

793
    """
794
    utils.WriteFile(file_name, data=data)
795

    
796
    names, addrs = self._GetNodeIp()
797
    result = rpc.RpcRunner.call_jobqueue_update(names, addrs, file_name, data)
798
    self._CheckRpcResult(result, self._nodes,
799
                         "Updating %s" % file_name)
800

    
801
  def _RenameFilesUnlocked(self, rename):
802
    """Renames a file locally and then replicate the change.
803

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

807
    @type rename: list of (old, new)
808
    @param rename: List containing tuples mapping old to new names
809

810
    """
811
    # Rename them locally
812
    for old, new in rename:
813
      utils.RenameFile(old, new, mkdir=True)
814

    
815
    # ... and on all nodes
816
    names, addrs = self._GetNodeIp()
817
    result = rpc.RpcRunner.call_jobqueue_rename(names, addrs, rename)
818
    self._CheckRpcResult(result, self._nodes, "Renaming files (%r)" % rename)
819

    
820
  @staticmethod
821
  def _FormatJobID(job_id):
822
    """Convert a job ID to string format.
823

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

828
    @type job_id: int or long
829
    @param job_id: the numeric job id
830
    @rtype: str
831
    @return: the formatted job id
832

833
    """
834
    if not isinstance(job_id, (int, long)):
835
      raise errors.ProgrammerError("Job ID '%s' not numeric" % job_id)
836
    if job_id < 0:
837
      raise errors.ProgrammerError("Job ID %s is negative" % job_id)
838

    
839
    return str(job_id)
840

    
841
  @classmethod
842
  def _GetArchiveDirectory(cls, job_id):
843
    """Returns the archive directory for a job.
844

845
    @type job_id: str
846
    @param job_id: Job identifier
847
    @rtype: str
848
    @return: Directory name
849

850
    """
851
    return str(int(job_id) / JOBS_PER_ARCHIVE_DIRECTORY)
852

    
853
  def _NewSerialsUnlocked(self, count):
854
    """Generates a new job identifier.
855

856
    Job identifiers are unique during the lifetime of a cluster.
857

858
    @type count: integer
859
    @param count: how many serials to return
860
    @rtype: str
861
    @return: a string representing the job identifier.
862

863
    """
864
    assert count > 0
865
    # New number
866
    serial = self._last_serial + count
867

    
868
    # Write to file
869
    self._WriteAndReplicateFileUnlocked(constants.JOB_QUEUE_SERIAL_FILE,
870
                                        "%s\n" % serial)
871

    
872
    result = [self._FormatJobID(v)
873
              for v in range(self._last_serial, serial + 1)]
874
    # Keep it only if we were able to write the file
875
    self._last_serial = serial
876

    
877
    return result
878

    
879
  @staticmethod
880
  def _GetJobPath(job_id):
881
    """Returns the job file for a given job id.
882

883
    @type job_id: str
884
    @param job_id: the job identifier
885
    @rtype: str
886
    @return: the path to the job file
887

888
    """
889
    return utils.PathJoin(constants.QUEUE_DIR, "job-%s" % job_id)
890

    
891
  @classmethod
892
  def _GetArchivedJobPath(cls, job_id):
893
    """Returns the archived job file for a give job id.
894

895
    @type job_id: str
896
    @param job_id: the job identifier
897
    @rtype: str
898
    @return: the path to the archived job file
899

900
    """
901
    return utils.PathJoin(constants.JOB_QUEUE_ARCHIVE_DIR,
902
                          cls._GetArchiveDirectory(job_id), "job-%s" % job_id)
903

    
904
  @classmethod
905
  def _ExtractJobID(cls, name):
906
    """Extract the job id from a filename.
907

908
    @type name: str
909
    @param name: the job filename
910
    @rtype: job id or None
911
    @return: the job id corresponding to the given filename,
912
        or None if the filename does not represent a valid
913
        job file
914

915
    """
916
    m = cls._RE_JOB_FILE.match(name)
917
    if m:
918
      return m.group(1)
919
    else:
920
      return None
921

    
922
  def _GetJobIDsUnlocked(self):
923
    """Return all known job IDs.
924

925
    The method only looks at disk because it's a requirement that all
926
    jobs are present on disk (so in the _memcache we don't have any
927
    extra IDs).
928

929
    @rtype: list
930
    @return: the list of job IDs
931

932
    """
933
    jlist = [self._ExtractJobID(name) for name in self._ListJobFiles()]
934
    jlist = utils.NiceSort(jlist)
935
    return jlist
936

    
937
  def _ListJobFiles(self):
938
    """Returns the list of current job files.
939

940
    @rtype: list
941
    @return: the list of job file names
942

943
    """
944
    return [name for name in utils.ListVisibleFiles(constants.QUEUE_DIR)
945
            if self._RE_JOB_FILE.match(name)]
946

    
947
  def _LoadJobUnlocked(self, job_id):
948
    """Loads a job from the disk or memory.
949

950
    Given a job id, this will return the cached job object if
951
    existing, or try to load the job from the disk. If loading from
952
    disk, it will also add the job to the cache.
953

954
    @param job_id: the job id
955
    @rtype: L{_QueuedJob} or None
956
    @return: either None or the job object
957

958
    """
959
    job = self._memcache.get(job_id, None)
960
    if job:
961
      logging.debug("Found job %s in memcache", job_id)
962
      return job
963

    
964
    filepath = self._GetJobPath(job_id)
965
    logging.debug("Loading job from %s", filepath)
966
    try:
967
      raw_data = utils.ReadFile(filepath)
968
    except IOError, err:
969
      if err.errno in (errno.ENOENT, ):
970
        return None
971
      raise
972

    
973
    data = serializer.LoadJson(raw_data)
974

    
975
    try:
976
      job = _QueuedJob.Restore(self, data)
977
    except Exception, err: # pylint: disable-msg=W0703
978
      new_path = self._GetArchivedJobPath(job_id)
979
      if filepath == new_path:
980
        # job already archived (future case)
981
        logging.exception("Can't parse job %s", job_id)
982
      else:
983
        # non-archived case
984
        logging.exception("Can't parse job %s, will archive.", job_id)
985
        self._RenameFilesUnlocked([(filepath, new_path)])
986
      return None
987

    
988
    self._memcache[job_id] = job
989
    logging.debug("Added job %s to the cache", job_id)
990
    return job
991

    
992
  def _GetJobsUnlocked(self, job_ids):
993
    """Return a list of jobs based on their IDs.
994

995
    @type job_ids: list
996
    @param job_ids: either an empty list (meaning all jobs),
997
        or a list of job IDs
998
    @rtype: list
999
    @return: the list of job objects
1000

1001
    """
1002
    if not job_ids:
1003
      job_ids = self._GetJobIDsUnlocked()
1004

    
1005
    return [self._LoadJobUnlocked(job_id) for job_id in job_ids]
1006

    
1007
  @staticmethod
1008
  def _IsQueueMarkedDrain():
1009
    """Check if the queue is marked from drain.
1010

1011
    This currently uses the queue drain file, which makes it a
1012
    per-node flag. In the future this can be moved to the config file.
1013

1014
    @rtype: boolean
1015
    @return: True of the job queue is marked for draining
1016

1017
    """
1018
    return os.path.exists(constants.JOB_QUEUE_DRAIN_FILE)
1019

    
1020
  @staticmethod
1021
  def SetDrainFlag(drain_flag):
1022
    """Sets the drain flag for the queue.
1023

1024
    @type drain_flag: boolean
1025
    @param drain_flag: Whether to set or unset the drain flag
1026

1027
    """
1028
    if drain_flag:
1029
      utils.WriteFile(constants.JOB_QUEUE_DRAIN_FILE, data="", close=True)
1030
    else:
1031
      utils.RemoveFile(constants.JOB_QUEUE_DRAIN_FILE)
1032
    return True
1033

    
1034
  @_RequireOpenQueue
1035
  def _SubmitJobUnlocked(self, job_id, ops):
1036
    """Create and store a new job.
1037

1038
    This enters the job into our job queue and also puts it on the new
1039
    queue, in order for it to be picked up by the queue processors.
1040

1041
    @type job_id: job ID
1042
    @param job_id: the job ID for the new job
1043
    @type ops: list
1044
    @param ops: The list of OpCodes that will become the new job.
1045
    @rtype: job ID
1046
    @return: the job ID of the newly created job
1047
    @raise errors.JobQueueDrainError: if the job is marked for draining
1048

1049
    """
1050
    if self._IsQueueMarkedDrain():
1051
      raise errors.JobQueueDrainError("Job queue is drained, refusing job")
1052

    
1053
    # Check job queue size
1054
    size = len(self._ListJobFiles())
1055
    if size >= constants.JOB_QUEUE_SIZE_SOFT_LIMIT:
1056
      # TODO: Autoarchive jobs. Make sure it's not done on every job
1057
      # submission, though.
1058
      #size = ...
1059
      pass
1060

    
1061
    if size >= constants.JOB_QUEUE_SIZE_HARD_LIMIT:
1062
      raise errors.JobQueueFull()
1063

    
1064
    job = _QueuedJob(self, job_id, ops)
1065

    
1066
    # Write to disk
1067
    self.UpdateJobUnlocked(job)
1068

    
1069
    logging.debug("Adding new job %s to the cache", job_id)
1070
    self._memcache[job_id] = job
1071

    
1072
    # Add to worker pool
1073
    self._wpool.AddTask(job)
1074

    
1075
    return job.id
1076

    
1077
  @utils.LockedMethod
1078
  @_RequireOpenQueue
1079
  def SubmitJob(self, ops):
1080
    """Create and store a new job.
1081

1082
    @see: L{_SubmitJobUnlocked}
1083

1084
    """
1085
    job_id = self._NewSerialsUnlocked(1)[0]
1086
    return self._SubmitJobUnlocked(job_id, ops)
1087

    
1088
  @utils.LockedMethod
1089
  @_RequireOpenQueue
1090
  def SubmitManyJobs(self, jobs):
1091
    """Create and store multiple jobs.
1092

1093
    @see: L{_SubmitJobUnlocked}
1094

1095
    """
1096
    results = []
1097
    all_job_ids = self._NewSerialsUnlocked(len(jobs))
1098
    for job_id, ops in zip(all_job_ids, jobs):
1099
      try:
1100
        data = self._SubmitJobUnlocked(job_id, ops)
1101
        status = True
1102
      except errors.GenericError, err:
1103
        data = str(err)
1104
        status = False
1105
      results.append((status, data))
1106

    
1107
    return results
1108

    
1109
  @_RequireOpenQueue
1110
  def UpdateJobUnlocked(self, job):
1111
    """Update a job's on disk storage.
1112

1113
    After a job has been modified, this function needs to be called in
1114
    order to write the changes to disk and replicate them to the other
1115
    nodes.
1116

1117
    @type job: L{_QueuedJob}
1118
    @param job: the changed job
1119

1120
    """
1121
    filename = self._GetJobPath(job.id)
1122
    data = serializer.DumpJson(job.Serialize(), indent=False)
1123
    logging.debug("Writing job %s to %s", job.id, filename)
1124
    self._WriteAndReplicateFileUnlocked(filename, data)
1125

    
1126
    # Notify waiters about potential changes
1127
    job.change.notifyAll()
1128

    
1129
  @utils.LockedMethod
1130
  @_RequireOpenQueue
1131
  def WaitForJobChanges(self, job_id, fields, prev_job_info, prev_log_serial,
1132
                        timeout):
1133
    """Waits for changes in a job.
1134

1135
    @type job_id: string
1136
    @param job_id: Job identifier
1137
    @type fields: list of strings
1138
    @param fields: Which fields to check for changes
1139
    @type prev_job_info: list or None
1140
    @param prev_job_info: Last job information returned
1141
    @type prev_log_serial: int
1142
    @param prev_log_serial: Last job message serial number
1143
    @type timeout: float
1144
    @param timeout: maximum time to wait
1145
    @rtype: tuple (job info, log entries)
1146
    @return: a tuple of the job information as required via
1147
        the fields parameter, and the log entries as a list
1148

1149
        if the job has not changed and the timeout has expired,
1150
        we instead return a special value,
1151
        L{constants.JOB_NOTCHANGED}, which should be interpreted
1152
        as such by the clients
1153

1154
    """
1155
    job = self._LoadJobUnlocked(job_id)
1156
    if not job:
1157
      logging.debug("Job %s not found", job_id)
1158
      return None
1159

    
1160
    def _CheckForChanges():
1161
      logging.debug("Waiting for changes in job %s", job_id)
1162

    
1163
      status = job.CalcStatus()
1164
      job_info = self._GetJobInfoUnlocked(job, fields)
1165
      log_entries = job.GetLogEntries(prev_log_serial)
1166

    
1167
      # Serializing and deserializing data can cause type changes (e.g. from
1168
      # tuple to list) or precision loss. We're doing it here so that we get
1169
      # the same modifications as the data received from the client. Without
1170
      # this, the comparison afterwards might fail without the data being
1171
      # significantly different.
1172
      job_info = serializer.LoadJson(serializer.DumpJson(job_info))
1173
      log_entries = serializer.LoadJson(serializer.DumpJson(log_entries))
1174

    
1175
      # Don't even try to wait if the job is no longer running, there will be
1176
      # no changes.
1177
      if (status not in (constants.JOB_STATUS_QUEUED,
1178
                         constants.JOB_STATUS_RUNNING,
1179
                         constants.JOB_STATUS_WAITLOCK) or
1180
          prev_job_info != job_info or
1181
          (log_entries and prev_log_serial != log_entries[0][0])):
1182
        logging.debug("Job %s changed", job_id)
1183
        return (job_info, log_entries)
1184

    
1185
      raise utils.RetryAgain()
1186

    
1187
    try:
1188
      # Setting wait function to release the queue lock while waiting
1189
      return utils.Retry(_CheckForChanges, utils.RETRY_REMAINING_TIME, timeout,
1190
                         wait_fn=job.change.wait)
1191
    except utils.RetryTimeout:
1192
      return constants.JOB_NOTCHANGED
1193

    
1194
  @utils.LockedMethod
1195
  @_RequireOpenQueue
1196
  def CancelJob(self, job_id):
1197
    """Cancels a job.
1198

1199
    This will only succeed if the job has not started yet.
1200

1201
    @type job_id: string
1202
    @param job_id: job ID of job to be cancelled.
1203

1204
    """
1205
    logging.info("Cancelling job %s", job_id)
1206

    
1207
    job = self._LoadJobUnlocked(job_id)
1208
    if not job:
1209
      logging.debug("Job %s not found", job_id)
1210
      return (False, "Job %s not found" % job_id)
1211

    
1212
    job_status = job.CalcStatus()
1213

    
1214
    if job_status not in (constants.JOB_STATUS_QUEUED,
1215
                          constants.JOB_STATUS_WAITLOCK):
1216
      logging.debug("Job %s is no longer waiting in the queue", job.id)
1217
      return (False, "Job %s is no longer waiting in the queue" % job.id)
1218

    
1219
    if job_status == constants.JOB_STATUS_QUEUED:
1220
      self.CancelJobUnlocked(job)
1221
      return (True, "Job %s canceled" % job.id)
1222

    
1223
    elif job_status == constants.JOB_STATUS_WAITLOCK:
1224
      # The worker will notice the new status and cancel the job
1225
      try:
1226
        job.MarkUnfinishedOps(constants.OP_STATUS_CANCELING, None)
1227
      finally:
1228
        self.UpdateJobUnlocked(job)
1229
      return (True, "Job %s will be canceled" % job.id)
1230

    
1231
  @_RequireOpenQueue
1232
  def CancelJobUnlocked(self, job):
1233
    """Marks a job as canceled.
1234

1235
    """
1236
    try:
1237
      job.MarkUnfinishedOps(constants.OP_STATUS_CANCELED,
1238
                            "Job canceled by request")
1239
    finally:
1240
      self.UpdateJobUnlocked(job)
1241

    
1242
  @_RequireOpenQueue
1243
  def _ArchiveJobsUnlocked(self, jobs):
1244
    """Archives jobs.
1245

1246
    @type jobs: list of L{_QueuedJob}
1247
    @param jobs: Job objects
1248
    @rtype: int
1249
    @return: Number of archived jobs
1250

1251
    """
1252
    archive_jobs = []
1253
    rename_files = []
1254
    for job in jobs:
1255
      if job.CalcStatus() not in (constants.JOB_STATUS_CANCELED,
1256
                                  constants.JOB_STATUS_SUCCESS,
1257
                                  constants.JOB_STATUS_ERROR):
1258
        logging.debug("Job %s is not yet done", job.id)
1259
        continue
1260

    
1261
      archive_jobs.append(job)
1262

    
1263
      old = self._GetJobPath(job.id)
1264
      new = self._GetArchivedJobPath(job.id)
1265
      rename_files.append((old, new))
1266

    
1267
    # TODO: What if 1..n files fail to rename?
1268
    self._RenameFilesUnlocked(rename_files)
1269

    
1270
    logging.debug("Successfully archived job(s) %s",
1271
                  utils.CommaJoin(job.id for job in archive_jobs))
1272

    
1273
    return len(archive_jobs)
1274

    
1275
  @utils.LockedMethod
1276
  @_RequireOpenQueue
1277
  def ArchiveJob(self, job_id):
1278
    """Archives a job.
1279

1280
    This is just a wrapper over L{_ArchiveJobsUnlocked}.
1281

1282
    @type job_id: string
1283
    @param job_id: Job ID of job to be archived.
1284
    @rtype: bool
1285
    @return: Whether job was archived
1286

1287
    """
1288
    logging.info("Archiving job %s", job_id)
1289

    
1290
    job = self._LoadJobUnlocked(job_id)
1291
    if not job:
1292
      logging.debug("Job %s not found", job_id)
1293
      return False
1294

    
1295
    return self._ArchiveJobsUnlocked([job]) == 1
1296

    
1297
  @utils.LockedMethod
1298
  @_RequireOpenQueue
1299
  def AutoArchiveJobs(self, age, timeout):
1300
    """Archives all jobs based on age.
1301

1302
    The method will archive all jobs which are older than the age
1303
    parameter. For jobs that don't have an end timestamp, the start
1304
    timestamp will be considered. The special '-1' age will cause
1305
    archival of all jobs (that are not running or queued).
1306

1307
    @type age: int
1308
    @param age: the minimum age in seconds
1309

1310
    """
1311
    logging.info("Archiving jobs with age more than %s seconds", age)
1312

    
1313
    now = time.time()
1314
    end_time = now + timeout
1315
    archived_count = 0
1316
    last_touched = 0
1317

    
1318
    all_job_ids = self._GetJobIDsUnlocked()
1319
    pending = []
1320
    for idx, job_id in enumerate(all_job_ids):
1321
      last_touched = idx + 1
1322

    
1323
      # Not optimal because jobs could be pending
1324
      # TODO: Measure average duration for job archival and take number of
1325
      # pending jobs into account.
1326
      if time.time() > end_time:
1327
        break
1328

    
1329
      # Returns None if the job failed to load
1330
      job = self._LoadJobUnlocked(job_id)
1331
      if job:
1332
        if job.end_timestamp is None:
1333
          if job.start_timestamp is None:
1334
            job_age = job.received_timestamp
1335
          else:
1336
            job_age = job.start_timestamp
1337
        else:
1338
          job_age = job.end_timestamp
1339

    
1340
        if age == -1 or now - job_age[0] > age:
1341
          pending.append(job)
1342

    
1343
          # Archive 10 jobs at a time
1344
          if len(pending) >= 10:
1345
            archived_count += self._ArchiveJobsUnlocked(pending)
1346
            pending = []
1347

    
1348
    if pending:
1349
      archived_count += self._ArchiveJobsUnlocked(pending)
1350

    
1351
    return (archived_count, len(all_job_ids) - last_touched)
1352

    
1353
  @staticmethod
1354
  def _GetJobInfoUnlocked(job, fields):
1355
    """Returns information about a job.
1356

1357
    @type job: L{_QueuedJob}
1358
    @param job: the job which we query
1359
    @type fields: list
1360
    @param fields: names of fields to return
1361
    @rtype: list
1362
    @return: list with one element for each field
1363
    @raise errors.OpExecError: when an invalid field
1364
        has been passed
1365

1366
    """
1367
    row = []
1368
    for fname in fields:
1369
      if fname == "id":
1370
        row.append(job.id)
1371
      elif fname == "status":
1372
        row.append(job.CalcStatus())
1373
      elif fname == "ops":
1374
        row.append([op.input.__getstate__() for op in job.ops])
1375
      elif fname == "opresult":
1376
        row.append([op.result for op in job.ops])
1377
      elif fname == "opstatus":
1378
        row.append([op.status for op in job.ops])
1379
      elif fname == "oplog":
1380
        row.append([op.log for op in job.ops])
1381
      elif fname == "opstart":
1382
        row.append([op.start_timestamp for op in job.ops])
1383
      elif fname == "opexec":
1384
        row.append([op.exec_timestamp for op in job.ops])
1385
      elif fname == "opend":
1386
        row.append([op.end_timestamp for op in job.ops])
1387
      elif fname == "received_ts":
1388
        row.append(job.received_timestamp)
1389
      elif fname == "start_ts":
1390
        row.append(job.start_timestamp)
1391
      elif fname == "end_ts":
1392
        row.append(job.end_timestamp)
1393
      elif fname == "lock_status":
1394
        row.append(job.lock_status)
1395
      elif fname == "summary":
1396
        row.append([op.input.Summary() for op in job.ops])
1397
      else:
1398
        raise errors.OpExecError("Invalid job query field '%s'" % fname)
1399
    return row
1400

    
1401
  @utils.LockedMethod
1402
  @_RequireOpenQueue
1403
  def QueryJobs(self, job_ids, fields):
1404
    """Returns a list of jobs in queue.
1405

1406
    This is a wrapper of L{_GetJobsUnlocked}, which actually does the
1407
    processing for each job.
1408

1409
    @type job_ids: list
1410
    @param job_ids: sequence of job identifiers or None for all
1411
    @type fields: list
1412
    @param fields: names of fields to return
1413
    @rtype: list
1414
    @return: list one element per job, each element being list with
1415
        the requested fields
1416

1417
    """
1418
    jobs = []
1419

    
1420
    for job in self._GetJobsUnlocked(job_ids):
1421
      if job is None:
1422
        jobs.append(None)
1423
      else:
1424
        jobs.append(self._GetJobInfoUnlocked(job, fields))
1425

    
1426
    return jobs
1427

    
1428
  @utils.LockedMethod
1429
  @_RequireOpenQueue
1430
  def Shutdown(self):
1431
    """Stops the job queue.
1432

1433
    This shutdowns all the worker threads an closes the queue.
1434

1435
    """
1436
    self._wpool.TerminateWorkers()
1437

    
1438
    self._queue_lock.Close()
1439
    self._queue_lock = None