4 # Copyright (C) 2006, 2007, 2008 Google Inc.
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.
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.
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
22 """Module implementing the job queue handling.
24 Locking: there's a single, large lock in the L{JobQueue} class. It's
25 used by all other classes in this module.
27 @var JOBQUEUE_THREADS: the number of worker threads we start for
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
52 JOBS_PER_ARCHIVE_DIRECTORY = 10000
55 class CancelJob(Exception):
56 """Special exception to cancel a job.
62 """Returns the current timestamp.
65 @return: the current time in the (seconds, microseconds) format
68 return utils.SplitTime(time.time())
71 class _QueuedOpCode(object):
72 """Encapsulates an opcode object.
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 stop_timestamp: timestamp for the end of the execution
83 __slots__ = ["input", "status", "result", "log",
84 "start_timestamp", "end_timestamp",
87 def __init__(self, op):
88 """Constructor for the _QuededOpCode.
90 @type op: L{opcodes.OpCode}
91 @param op: the opcode we encapsulate
95 self.status = constants.OP_STATUS_QUEUED
98 self.start_timestamp = None
99 self.end_timestamp = None
102 def Restore(cls, state):
103 """Restore the _QueuedOpCode from the serialized form.
106 @param state: the serialized state
107 @rtype: _QueuedOpCode
108 @return: a new _QueuedOpCode instance
111 obj = _QueuedOpCode.__new__(cls)
112 obj.input = opcodes.OpCode.LoadOpCode(state["input"])
113 obj.status = state["status"]
114 obj.result = state["result"]
115 obj.log = state["log"]
116 obj.start_timestamp = state.get("start_timestamp", None)
117 obj.end_timestamp = state.get("end_timestamp", None)
121 """Serializes this _QueuedOpCode.
124 @return: the dictionary holding the serialized state
128 "input": self.input.__getstate__(),
129 "status": self.status,
130 "result": self.result,
132 "start_timestamp": self.start_timestamp,
133 "end_timestamp": self.end_timestamp,
137 class _QueuedJob(object):
138 """In-memory job representation.
140 This is what we use to track the user-submitted jobs. Locking must
141 be taken care of by users of this class.
143 @type queue: L{JobQueue}
144 @ivar queue: the parent queue
147 @ivar ops: the list of _QueuedOpCode that constitute the job
148 @type run_op_index: int
149 @ivar run_op_index: the currently executing opcode, or -1 if
150 we didn't yet start executing
151 @type log_serial: int
152 @ivar log_serial: holds the index for the next log entry
153 @ivar received_timestamp: the timestamp for when the job was received
154 @ivar start_timestmap: the timestamp for start of execution
155 @ivar end_timestamp: the timestamp for end of execution
156 @ivar change: a Condition variable we use for waiting for job changes
159 __slots__ = ["queue", "id", "ops", "run_op_index", "log_serial",
160 "received_timestamp", "start_timestamp", "end_timestamp",
164 def __init__(self, queue, job_id, ops):
165 """Constructor for the _QueuedJob.
167 @type queue: L{JobQueue}
168 @param queue: our parent queue
170 @param job_id: our job id
172 @param ops: the list of opcodes we hold, which will be encapsulated
177 # TODO: use a better exception
178 raise Exception("No opcodes")
182 self.ops = [_QueuedOpCode(op) for op in ops]
183 self.run_op_index = -1
185 self.received_timestamp = TimeStampNow()
186 self.start_timestamp = None
187 self.end_timestamp = None
189 # Condition to wait for changes
190 self.change = threading.Condition(self.queue._lock)
193 def Restore(cls, queue, state):
194 """Restore a _QueuedJob from serialized state:
196 @type queue: L{JobQueue}
197 @param queue: to which queue the restored job belongs
199 @param state: the serialized state
201 @return: the restored _JobQueue instance
204 obj = _QueuedJob.__new__(cls)
207 obj.run_op_index = state["run_op_index"]
208 obj.received_timestamp = state.get("received_timestamp", None)
209 obj.start_timestamp = state.get("start_timestamp", None)
210 obj.end_timestamp = state.get("end_timestamp", None)
214 for op_state in state["ops"]:
215 op = _QueuedOpCode.Restore(op_state)
216 for log_entry in op.log:
217 obj.log_serial = max(obj.log_serial, log_entry[0])
220 # Condition to wait for changes
221 obj.change = threading.Condition(obj.queue._lock)
226 """Serialize the _JobQueue instance.
229 @return: the serialized state
234 "ops": [op.Serialize() for op in self.ops],
235 "run_op_index": self.run_op_index,
236 "start_timestamp": self.start_timestamp,
237 "end_timestamp": self.end_timestamp,
238 "received_timestamp": self.received_timestamp,
241 def CalcStatus(self):
242 """Compute the status of this job.
244 This function iterates over all the _QueuedOpCodes in the job and
245 based on their status, computes the job status.
248 - if we find a cancelled, or finished with error, the job
249 status will be the same
250 - otherwise, the last opcode with the status one of:
255 will determine the job status
257 - otherwise, it means either all opcodes are queued, or success,
258 and the job status will be the same
260 @return: the job status
263 status = constants.JOB_STATUS_QUEUED
267 if op.status == constants.OP_STATUS_SUCCESS:
272 if op.status == constants.OP_STATUS_QUEUED:
274 elif op.status == constants.OP_STATUS_WAITLOCK:
275 status = constants.JOB_STATUS_WAITLOCK
276 elif op.status == constants.OP_STATUS_RUNNING:
277 status = constants.JOB_STATUS_RUNNING
278 elif op.status == constants.OP_STATUS_CANCELING:
279 status = constants.JOB_STATUS_CANCELING
281 elif op.status == constants.OP_STATUS_ERROR:
282 status = constants.JOB_STATUS_ERROR
283 # The whole job fails if one opcode failed
285 elif op.status == constants.OP_STATUS_CANCELED:
286 status = constants.OP_STATUS_CANCELED
290 status = constants.JOB_STATUS_SUCCESS
294 def GetLogEntries(self, newer_than):
295 """Selectively returns the log entries.
297 @type newer_than: None or int
298 @param newer_than: if this is None, return all log entries,
299 otherwise return only the log entries with serial higher
302 @return: the list of the log entries selected
305 if newer_than is None:
312 entries.extend(filter(lambda entry: entry[0] > serial, op.log))
316 def MarkUnfinishedOps(self, status, result):
317 """Mark unfinished opcodes with a given status and result.
319 This is an utility function for marking all running or waiting to
320 be run opcodes with a given status. Opcodes which are already
321 finalised are not changed.
323 @param status: a given opcode status
324 @param result: the opcode result
329 if op.status in constants.OPS_FINALIZED:
330 assert not_marked, "Finalized opcodes found after non-finalized ones"
337 class _JobQueueWorker(workerpool.BaseWorker):
338 """The actual job workers.
341 def _NotifyStart(self):
342 """Mark the opcode as running, not lock-waiting.
344 This is called from the mcpu code as a notifier function, when the
345 LU is finally about to start the Exec() method. Of course, to have
346 end-user visible results, the opcode must be initially (before
347 calling into Processor.ExecOpCode) set to OP_STATUS_WAITLOCK.
350 assert self.queue, "Queue attribute is missing"
351 assert self.opcode, "Opcode attribute is missing"
355 assert self.opcode.status in (constants.OP_STATUS_WAITLOCK,
356 constants.OP_STATUS_CANCELING)
358 # Cancel here if we were asked to
359 if self.opcode.status == constants.OP_STATUS_CANCELING:
362 self.opcode.status = constants.OP_STATUS_RUNNING
366 def RunTask(self, job):
369 This functions processes a job. It is closely tied to the _QueuedJob and
370 _QueuedOpCode classes.
372 @type job: L{_QueuedJob}
373 @param job: the job to be processed
376 logging.info("Worker %s processing job %s",
377 self.worker_id, job.id)
378 proc = mcpu.Processor(self.pool.queue.context)
379 self.queue = queue = job.queue
383 for idx, op in enumerate(job.ops):
384 op_summary = op.input.Summary()
385 if op.status == constants.OP_STATUS_SUCCESS:
386 # this is a job that was partially completed before master
387 # daemon shutdown, so it can be expected that some opcodes
388 # are already completed successfully (if any did error
389 # out, then the whole job should have been aborted and not
390 # resubmitted for processing)
391 logging.info("Op %s/%s: opcode %s already processed, skipping",
392 idx + 1, count, op_summary)
395 logging.info("Op %s/%s: Starting opcode %s", idx + 1, count,
400 if op.status == constants.OP_STATUS_CANCELED:
402 assert op.status == constants.OP_STATUS_QUEUED
403 job.run_op_index = idx
404 op.status = constants.OP_STATUS_WAITLOCK
406 op.start_timestamp = TimeStampNow()
407 if idx == 0: # first opcode
408 job.start_timestamp = op.start_timestamp
409 queue.UpdateJobUnlocked(job)
411 input_opcode = op.input
416 """Append a log entry.
422 log_type = constants.ELOG_MESSAGE
425 log_type, log_msg = args
427 # The time is split to make serialization easier and not lose
429 timestamp = utils.SplitTime(time.time())
434 op.log.append((job.log_serial, timestamp, log_type, log_msg))
436 job.change.notifyAll()
440 # Make sure not to hold lock while _Log is called
442 result = proc.ExecOpCode(input_opcode, _Log, self._NotifyStart)
446 op.status = constants.OP_STATUS_SUCCESS
448 op.end_timestamp = TimeStampNow()
449 queue.UpdateJobUnlocked(job)
453 logging.info("Op %s/%s: Successfully finished opcode %s",
454 idx + 1, count, op_summary)
456 # Will be handled further up
458 except Exception, err:
462 op.status = constants.OP_STATUS_ERROR
464 op.end_timestamp = TimeStampNow()
465 logging.info("Op %s/%s: Error in opcode %s: %s",
466 idx + 1, count, op_summary, err)
468 queue.UpdateJobUnlocked(job)
476 queue.CancelJobUnlocked(job)
479 except errors.GenericError, err:
480 logging.exception("Ganeti exception")
482 logging.exception("Unhandled exception")
487 job.run_op_index = -1
488 job.end_timestamp = TimeStampNow()
489 queue.UpdateJobUnlocked(job)
492 status = job.CalcStatus()
495 logging.info("Worker %s finished job %s, status = %s",
496 self.worker_id, job_id, status)
499 class _JobQueueWorkerPool(workerpool.WorkerPool):
500 """Simple class implementing a job-processing workerpool.
503 def __init__(self, queue):
504 super(_JobQueueWorkerPool, self).__init__(JOBQUEUE_THREADS,
509 class JobQueue(object):
510 """Queue used to manage the jobs.
512 @cvar _RE_JOB_FILE: regex matching the valid job file names
515 _RE_JOB_FILE = re.compile(r"^job-(%s)$" % constants.JOB_ID_TEMPLATE)
517 def _RequireOpenQueue(fn):
518 """Decorator for "public" functions.
520 This function should be used for all 'public' functions. That is,
521 functions usually called from other classes.
523 @warning: Use this decorator only after utils.LockedMethod!
532 def wrapper(self, *args, **kwargs):
533 assert self._queue_lock is not None, "Queue should be open"
534 return fn(self, *args, **kwargs)
537 def __init__(self, context):
538 """Constructor for JobQueue.
540 The constructor will initialize the job queue object and then
541 start loading the current jobs from disk, either for starting them
542 (if they were queue) or for aborting them (if they were already
545 @type context: GanetiContext
546 @param context: the context object for access to the configuration
547 data and other ganeti objects
550 self.context = context
551 self._memcache = weakref.WeakValueDictionary()
552 self._my_hostname = utils.HostInfo().name
555 self._lock = threading.Lock()
556 self.acquire = self._lock.acquire
557 self.release = self._lock.release
560 self._queue_lock = jstore.InitAndVerifyQueue(must_lock=True)
563 self._last_serial = jstore.ReadSerial()
564 assert self._last_serial is not None, ("Serial file was modified between"
565 " check in jstore and here")
567 # Get initial list of nodes
568 self._nodes = dict((n.name, n.primary_ip)
569 for n in self.context.cfg.GetAllNodesInfo().values()
570 if n.master_candidate)
574 del self._nodes[self._my_hostname]
578 # TODO: Check consistency across nodes
581 self._wpool = _JobQueueWorkerPool(self)
583 # We need to lock here because WorkerPool.AddTask() may start a job while
584 # we're still doing our work.
587 logging.info("Inspecting job queue")
589 all_job_ids = self._GetJobIDsUnlocked()
590 jobs_count = len(all_job_ids)
591 lastinfo = time.time()
592 for idx, job_id in enumerate(all_job_ids):
593 # Give an update every 1000 jobs or 10 seconds
594 if (idx % 1000 == 0 or time.time() >= (lastinfo + 10.0) or
595 idx == (jobs_count - 1)):
596 logging.info("Job queue inspection: %d/%d (%0.1f %%)",
597 idx, jobs_count - 1, 100.0 * (idx + 1) / jobs_count)
598 lastinfo = time.time()
600 job = self._LoadJobUnlocked(job_id)
602 # a failure in loading the job can cause 'None' to be returned
606 status = job.CalcStatus()
608 if status in (constants.JOB_STATUS_QUEUED, ):
609 self._wpool.AddTask(job)
611 elif status in (constants.JOB_STATUS_RUNNING,
612 constants.JOB_STATUS_WAITLOCK,
613 constants.JOB_STATUS_CANCELING):
614 logging.warning("Unfinished job %s found: %s", job.id, job)
616 job.MarkUnfinishedOps(constants.OP_STATUS_ERROR,
617 "Unclean master daemon shutdown")
619 self.UpdateJobUnlocked(job)
621 logging.info("Job queue inspection finished")
625 self._wpool.TerminateWorkers()
630 def AddNode(self, node):
631 """Register a new node with the queue.
633 @type node: L{objects.Node}
634 @param node: the node object to be added
637 node_name = node.name
638 assert node_name != self._my_hostname
640 # Clean queue directory on added node
641 result = rpc.RpcRunner.call_jobqueue_purge(node_name)
642 msg = result.RemoteFailMsg()
644 logging.warning("Cannot cleanup queue directory on node %s: %s",
647 if not node.master_candidate:
648 # remove if existing, ignoring errors
649 self._nodes.pop(node_name, None)
650 # and skip the replication of the job ids
653 # Upload the whole queue excluding archived jobs
654 files = [self._GetJobPath(job_id) for job_id in self._GetJobIDsUnlocked()]
656 # Upload current serial file
657 files.append(constants.JOB_QUEUE_SERIAL_FILE)
659 for file_name in files:
661 fd = open(file_name, "r")
667 result = rpc.RpcRunner.call_jobqueue_update([node_name],
670 msg = result[node_name].RemoteFailMsg()
672 logging.error("Failed to upload file %s to node %s: %s",
673 file_name, node_name, msg)
675 self._nodes[node_name] = node.primary_ip
679 def RemoveNode(self, node_name):
680 """Callback called when removing nodes from the cluster.
683 @param node_name: the name of the node to remove
687 # The queue is removed by the "leave node" RPC call.
688 del self._nodes[node_name]
692 def _CheckRpcResult(self, result, nodes, failmsg):
693 """Verifies the status of an RPC call.
695 Since we aim to keep consistency should this node (the current
696 master) fail, we will log errors if our rpc fail, and especially
697 log the case when more than half of the nodes fails.
699 @param result: the data as returned from the rpc call
701 @param nodes: the list of nodes we made the call to
703 @param failmsg: the identifier to be used for logging
710 msg = result[node].RemoteFailMsg()
713 logging.error("RPC call %s failed on node %s: %s",
714 result[node].call, node, msg)
718 # +1 for the master node
719 if (len(success) + 1) < len(failed):
720 # TODO: Handle failing nodes
721 logging.error("More than half of the nodes failed")
723 def _GetNodeIp(self):
724 """Helper for returning the node name/ip list.
727 @return: a tuple of two lists, the first one with the node
728 names and the second one with the node addresses
731 name_list = self._nodes.keys()
732 addr_list = [self._nodes[name] for name in name_list]
733 return name_list, addr_list
735 def _WriteAndReplicateFileUnlocked(self, file_name, data):
736 """Writes a file locally and then replicates it to all nodes.
738 This function will replace the contents of a file on the local
739 node and then replicate it to all the other nodes we have.
742 @param file_name: the path of the file to be replicated
744 @param data: the new contents of the file
747 utils.WriteFile(file_name, data=data)
749 names, addrs = self._GetNodeIp()
750 result = rpc.RpcRunner.call_jobqueue_update(names, addrs, file_name, data)
751 self._CheckRpcResult(result, self._nodes,
752 "Updating %s" % file_name)
754 def _RenameFilesUnlocked(self, rename):
755 """Renames a file locally and then replicate the change.
757 This function will rename a file in the local queue directory
758 and then replicate this rename to all the other nodes we have.
760 @type rename: list of (old, new)
761 @param rename: List containing tuples mapping old to new names
764 # Rename them locally
765 for old, new in rename:
766 utils.RenameFile(old, new, mkdir=True)
768 # ... and on all nodes
769 names, addrs = self._GetNodeIp()
770 result = rpc.RpcRunner.call_jobqueue_rename(names, addrs, rename)
771 self._CheckRpcResult(result, self._nodes, "Renaming files (%r)" % rename)
773 def _FormatJobID(self, job_id):
774 """Convert a job ID to string format.
776 Currently this just does C{str(job_id)} after performing some
777 checks, but if we want to change the job id format this will
778 abstract this change.
780 @type job_id: int or long
781 @param job_id: the numeric job id
783 @return: the formatted job id
786 if not isinstance(job_id, (int, long)):
787 raise errors.ProgrammerError("Job ID '%s' not numeric" % job_id)
789 raise errors.ProgrammerError("Job ID %s is negative" % job_id)
794 def _GetArchiveDirectory(cls, job_id):
795 """Returns the archive directory for a job.
798 @param job_id: Job identifier
800 @return: Directory name
803 return str(int(job_id) / JOBS_PER_ARCHIVE_DIRECTORY)
805 def _NewSerialUnlocked(self):
806 """Generates a new job identifier.
808 Job identifiers are unique during the lifetime of a cluster.
811 @return: a string representing the job identifier.
815 serial = self._last_serial + 1
818 self._WriteAndReplicateFileUnlocked(constants.JOB_QUEUE_SERIAL_FILE,
821 # Keep it only if we were able to write the file
822 self._last_serial = serial
824 return self._FormatJobID(serial)
827 def _GetJobPath(job_id):
828 """Returns the job file for a given job id.
831 @param job_id: the job identifier
833 @return: the path to the job file
836 return os.path.join(constants.QUEUE_DIR, "job-%s" % job_id)
839 def _GetArchivedJobPath(cls, job_id):
840 """Returns the archived job file for a give job id.
843 @param job_id: the job identifier
845 @return: the path to the archived job file
848 path = "%s/job-%s" % (cls._GetArchiveDirectory(job_id), job_id)
849 return os.path.join(constants.JOB_QUEUE_ARCHIVE_DIR, path)
852 def _ExtractJobID(cls, name):
853 """Extract the job id from a filename.
856 @param name: the job filename
857 @rtype: job id or None
858 @return: the job id corresponding to the given filename,
859 or None if the filename does not represent a valid
863 m = cls._RE_JOB_FILE.match(name)
869 def _GetJobIDsUnlocked(self, archived=False):
870 """Return all known job IDs.
872 If the parameter archived is True, archived jobs IDs will be
873 included. Currently this argument is unused.
875 The method only looks at disk because it's a requirement that all
876 jobs are present on disk (so in the _memcache we don't have any
880 @return: the list of job IDs
883 jlist = [self._ExtractJobID(name) for name in self._ListJobFiles()]
884 jlist = utils.NiceSort(jlist)
887 def _ListJobFiles(self):
888 """Returns the list of current job files.
891 @return: the list of job file names
894 return [name for name in utils.ListVisibleFiles(constants.QUEUE_DIR)
895 if self._RE_JOB_FILE.match(name)]
897 def _LoadJobUnlocked(self, job_id):
898 """Loads a job from the disk or memory.
900 Given a job id, this will return the cached job object if
901 existing, or try to load the job from the disk. If loading from
902 disk, it will also add the job to the cache.
904 @param job_id: the job id
905 @rtype: L{_QueuedJob} or None
906 @return: either None or the job object
909 job = self._memcache.get(job_id, None)
911 logging.debug("Found job %s in memcache", job_id)
914 filepath = self._GetJobPath(job_id)
915 logging.debug("Loading job from %s", filepath)
917 fd = open(filepath, "r")
919 if err.errno in (errno.ENOENT, ):
923 data = serializer.LoadJson(fd.read())
928 job = _QueuedJob.Restore(self, data)
929 except Exception, err:
930 new_path = self._GetArchivedJobPath(job_id)
931 if filepath == new_path:
932 # job already archived (future case)
933 logging.exception("Can't parse job %s", job_id)
936 logging.exception("Can't parse job %s, will archive.", job_id)
937 self._RenameFilesUnlocked([(filepath, new_path)])
940 self._memcache[job_id] = job
941 logging.debug("Added job %s to the cache", job_id)
944 def _GetJobsUnlocked(self, job_ids):
945 """Return a list of jobs based on their IDs.
948 @param job_ids: either an empty list (meaning all jobs),
951 @return: the list of job objects
955 job_ids = self._GetJobIDsUnlocked()
957 return [self._LoadJobUnlocked(job_id) for job_id in job_ids]
960 def _IsQueueMarkedDrain():
961 """Check if the queue is marked from drain.
963 This currently uses the queue drain file, which makes it a
964 per-node flag. In the future this can be moved to the config file.
967 @return: True of the job queue is marked for draining
970 return os.path.exists(constants.JOB_QUEUE_DRAIN_FILE)
973 def SetDrainFlag(drain_flag):
974 """Sets the drain flag for the queue.
976 This is similar to the function L{backend.JobQueueSetDrainFlag},
977 and in the future we might merge them.
979 @type drain_flag: boolean
980 @param drain_flag: Whether to set or unset the drain flag
984 utils.WriteFile(constants.JOB_QUEUE_DRAIN_FILE, data="", close=True)
986 utils.RemoveFile(constants.JOB_QUEUE_DRAIN_FILE)
990 def _SubmitJobUnlocked(self, ops):
991 """Create and store a new job.
993 This enters the job into our job queue and also puts it on the new
994 queue, in order for it to be picked up by the queue processors.
997 @param ops: The list of OpCodes that will become the new job.
999 @return: the job ID of the newly created job
1000 @raise errors.JobQueueDrainError: if the job is marked for draining
1003 if self._IsQueueMarkedDrain():
1004 raise errors.JobQueueDrainError("Job queue is drained, refusing job")
1006 # Check job queue size
1007 size = len(self._ListJobFiles())
1008 if size >= constants.JOB_QUEUE_SIZE_SOFT_LIMIT:
1009 # TODO: Autoarchive jobs. Make sure it's not done on every job
1010 # submission, though.
1014 if size >= constants.JOB_QUEUE_SIZE_HARD_LIMIT:
1015 raise errors.JobQueueFull()
1017 # Get job identifier
1018 job_id = self._NewSerialUnlocked()
1019 job = _QueuedJob(self, job_id, ops)
1022 self.UpdateJobUnlocked(job)
1024 logging.debug("Adding new job %s to the cache", job_id)
1025 self._memcache[job_id] = job
1027 # Add to worker pool
1028 self._wpool.AddTask(job)
1034 def SubmitJob(self, ops):
1035 """Create and store a new job.
1037 @see: L{_SubmitJobUnlocked}
1040 return self._SubmitJobUnlocked(ops)
1044 def SubmitManyJobs(self, jobs):
1045 """Create and store multiple jobs.
1047 @see: L{_SubmitJobUnlocked}
1053 data = self._SubmitJobUnlocked(ops)
1055 except errors.GenericError, err:
1058 results.append((status, data))
1064 def UpdateJobUnlocked(self, job):
1065 """Update a job's on disk storage.
1067 After a job has been modified, this function needs to be called in
1068 order to write the changes to disk and replicate them to the other
1071 @type job: L{_QueuedJob}
1072 @param job: the changed job
1075 filename = self._GetJobPath(job.id)
1076 data = serializer.DumpJson(job.Serialize(), indent=False)
1077 logging.debug("Writing job %s to %s", job.id, filename)
1078 self._WriteAndReplicateFileUnlocked(filename, data)
1080 # Notify waiters about potential changes
1081 job.change.notifyAll()
1085 def WaitForJobChanges(self, job_id, fields, prev_job_info, prev_log_serial,
1087 """Waits for changes in a job.
1089 @type job_id: string
1090 @param job_id: Job identifier
1091 @type fields: list of strings
1092 @param fields: Which fields to check for changes
1093 @type prev_job_info: list or None
1094 @param prev_job_info: Last job information returned
1095 @type prev_log_serial: int
1096 @param prev_log_serial: Last job message serial number
1097 @type timeout: float
1098 @param timeout: maximum time to wait
1099 @rtype: tuple (job info, log entries)
1100 @return: a tuple of the job information as required via
1101 the fields parameter, and the log entries as a list
1103 if the job has not changed and the timeout has expired,
1104 we instead return a special value,
1105 L{constants.JOB_NOTCHANGED}, which should be interpreted
1106 as such by the clients
1109 logging.debug("Waiting for changes in job %s", job_id)
1110 end_time = time.time() + timeout
1112 delta_time = end_time - time.time()
1114 return constants.JOB_NOTCHANGED
1116 job = self._LoadJobUnlocked(job_id)
1118 logging.debug("Job %s not found", job_id)
1121 status = job.CalcStatus()
1122 job_info = self._GetJobInfoUnlocked(job, fields)
1123 log_entries = job.GetLogEntries(prev_log_serial)
1125 # Serializing and deserializing data can cause type changes (e.g. from
1126 # tuple to list) or precision loss. We're doing it here so that we get
1127 # the same modifications as the data received from the client. Without
1128 # this, the comparison afterwards might fail without the data being
1129 # significantly different.
1130 job_info = serializer.LoadJson(serializer.DumpJson(job_info))
1131 log_entries = serializer.LoadJson(serializer.DumpJson(log_entries))
1133 if status not in (constants.JOB_STATUS_QUEUED,
1134 constants.JOB_STATUS_RUNNING,
1135 constants.JOB_STATUS_WAITLOCK):
1136 # Don't even try to wait if the job is no longer running, there will be
1140 if (prev_job_info != job_info or
1141 (log_entries and prev_log_serial != log_entries[0][0])):
1144 logging.debug("Waiting again")
1146 # Release the queue lock while waiting
1147 job.change.wait(delta_time)
1149 logging.debug("Job %s changed", job_id)
1151 return (job_info, log_entries)
1155 def CancelJob(self, job_id):
1158 This will only succeed if the job has not started yet.
1160 @type job_id: string
1161 @param job_id: job ID of job to be cancelled.
1164 logging.info("Cancelling job %s", job_id)
1166 job = self._LoadJobUnlocked(job_id)
1168 logging.debug("Job %s not found", job_id)
1169 return (False, "Job %s not found" % job_id)
1171 job_status = job.CalcStatus()
1173 if job_status not in (constants.JOB_STATUS_QUEUED,
1174 constants.JOB_STATUS_WAITLOCK):
1175 logging.debug("Job %s is no longer in the queue", job.id)
1176 return (False, "Job %s is no longer in the queue" % job.id)
1178 if job_status == constants.JOB_STATUS_QUEUED:
1179 self.CancelJobUnlocked(job)
1180 return (True, "Job %s canceled" % job.id)
1182 elif job_status == constants.JOB_STATUS_WAITLOCK:
1183 # The worker will notice the new status and cancel the job
1185 job.MarkUnfinishedOps(constants.OP_STATUS_CANCELING, None)
1187 self.UpdateJobUnlocked(job)
1188 return (True, "Job %s will be canceled" % job.id)
1191 def CancelJobUnlocked(self, job):
1192 """Marks a job as canceled.
1196 job.MarkUnfinishedOps(constants.OP_STATUS_CANCELED,
1197 "Job canceled by request")
1199 self.UpdateJobUnlocked(job)
1202 def _ArchiveJobsUnlocked(self, jobs):
1205 @type jobs: list of L{_QueuedJob}
1206 @param jobs: Job objects
1208 @return: Number of archived jobs
1214 if job.CalcStatus() not in (constants.JOB_STATUS_CANCELED,
1215 constants.JOB_STATUS_SUCCESS,
1216 constants.JOB_STATUS_ERROR):
1217 logging.debug("Job %s is not yet done", job.id)
1220 archive_jobs.append(job)
1222 old = self._GetJobPath(job.id)
1223 new = self._GetArchivedJobPath(job.id)
1224 rename_files.append((old, new))
1226 # TODO: What if 1..n files fail to rename?
1227 self._RenameFilesUnlocked(rename_files)
1229 logging.debug("Successfully archived job(s) %s",
1230 ", ".join(job.id for job in archive_jobs))
1232 return len(archive_jobs)
1236 def ArchiveJob(self, job_id):
1239 This is just a wrapper over L{_ArchiveJobsUnlocked}.
1241 @type job_id: string
1242 @param job_id: Job ID of job to be archived.
1244 @return: Whether job was archived
1247 logging.info("Archiving job %s", job_id)
1249 job = self._LoadJobUnlocked(job_id)
1251 logging.debug("Job %s not found", job_id)
1254 return self._ArchiveJobsUnlocked([job]) == 1
1258 def AutoArchiveJobs(self, age, timeout):
1259 """Archives all jobs based on age.
1261 The method will archive all jobs which are older than the age
1262 parameter. For jobs that don't have an end timestamp, the start
1263 timestamp will be considered. The special '-1' age will cause
1264 archival of all jobs (that are not running or queued).
1267 @param age: the minimum age in seconds
1270 logging.info("Archiving jobs with age more than %s seconds", age)
1273 end_time = now + timeout
1277 all_job_ids = self._GetJobIDsUnlocked(archived=False)
1279 for idx, job_id in enumerate(all_job_ids):
1282 # Not optimal because jobs could be pending
1283 # TODO: Measure average duration for job archival and take number of
1284 # pending jobs into account.
1285 if time.time() > end_time:
1288 # Returns None if the job failed to load
1289 job = self._LoadJobUnlocked(job_id)
1291 if job.end_timestamp is None:
1292 if job.start_timestamp is None:
1293 job_age = job.received_timestamp
1295 job_age = job.start_timestamp
1297 job_age = job.end_timestamp
1299 if age == -1 or now - job_age[0] > age:
1302 # Archive 10 jobs at a time
1303 if len(pending) >= 10:
1304 archived_count += self._ArchiveJobsUnlocked(pending)
1308 archived_count += self._ArchiveJobsUnlocked(pending)
1310 return (archived_count, len(all_job_ids) - last_touched - 1)
1312 def _GetJobInfoUnlocked(self, job, fields):
1313 """Returns information about a job.
1315 @type job: L{_QueuedJob}
1316 @param job: the job which we query
1318 @param fields: names of fields to return
1320 @return: list with one element for each field
1321 @raise errors.OpExecError: when an invalid field
1326 for fname in fields:
1329 elif fname == "status":
1330 row.append(job.CalcStatus())
1331 elif fname == "ops":
1332 row.append([op.input.__getstate__() for op in job.ops])
1333 elif fname == "opresult":
1334 row.append([op.result for op in job.ops])
1335 elif fname == "opstatus":
1336 row.append([op.status for op in job.ops])
1337 elif fname == "oplog":
1338 row.append([op.log for op in job.ops])
1339 elif fname == "opstart":
1340 row.append([op.start_timestamp for op in job.ops])
1341 elif fname == "opend":
1342 row.append([op.end_timestamp for op in job.ops])
1343 elif fname == "received_ts":
1344 row.append(job.received_timestamp)
1345 elif fname == "start_ts":
1346 row.append(job.start_timestamp)
1347 elif fname == "end_ts":
1348 row.append(job.end_timestamp)
1349 elif fname == "summary":
1350 row.append([op.input.Summary() for op in job.ops])
1352 raise errors.OpExecError("Invalid job query field '%s'" % fname)
1357 def QueryJobs(self, job_ids, fields):
1358 """Returns a list of jobs in queue.
1360 This is a wrapper of L{_GetJobsUnlocked}, which actually does the
1361 processing for each job.
1364 @param job_ids: sequence of job identifiers or None for all
1366 @param fields: names of fields to return
1368 @return: list one element per job, each element being list with
1369 the requested fields
1374 for job in self._GetJobsUnlocked(job_ids):
1378 jobs.append(self._GetJobInfoUnlocked(job, fields))
1385 """Stops the job queue.
1387 This shutdowns all the worker threads an closes the queue.
1390 self._wpool.TerminateWorkers()
1392 self._queue_lock.Close()
1393 self._queue_lock = None