<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[13486] twext/trunk/twext/enterprise/jobqueue.py</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.calendarserver.org//changeset/13486">13486</a></dd>
<dt>Author</dt> <dd>cdaboo@apple.com</dd>
<dt>Date</dt> <dd>2014-05-16 11:05:32 -0700 (Fri, 16 May 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Additional tracking of job stats.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#twexttrunktwextenterprisejobqueuepy">twext/trunk/twext/enterprise/jobqueue.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="twexttrunktwextenterprisejobqueuepy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/enterprise/jobqueue.py (13485 => 13486)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/enterprise/jobqueue.py        2014-05-16 14:30:46 UTC (rev 13485)
+++ twext/trunk/twext/enterprise/jobqueue.py        2014-05-16 18:05:32 UTC (rev 13486)
</span><span class="lines">@@ -138,6 +138,7 @@
</span><span class="cx"> from zope.interface.interface import Interface
</span><span class="cx"> from twext.enterprise.locking import NamedLock
</span><span class="cx"> 
</span><ins>+import collections
</ins><span class="cx"> import time
</span><span class="cx"> 
</span><span class="cx"> log = Logger()
</span><span class="lines">@@ -319,7 +320,7 @@
</span><span class="cx">     _workTypeMap = None
</span><span class="cx"> 
</span><span class="cx">     def descriptor(self):
</span><del>-        return JobDescriptor(self.jobID, self.weight)
</del><ins>+        return JobDescriptor(self.jobID, self.weight, self.workType)
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     def assign(self, now):
</span><span class="lines">@@ -621,25 +622,33 @@
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         results = {}
</span><span class="cx">         now = datetime.utcnow()
</span><del>-        for workType in cls.workTypes():
-            results.setdefault(workType.table.model.name, [0, 0, 0, 0])
</del><ins>+        for workItemType in cls.workTypes():
+            workType = workItemType.table.model.name
+            results.setdefault(workType, {
+                &quot;queued&quot;: 0,
+                &quot;assigned&quot;: 0,
+                &quot;late&quot;: 0,
+                &quot;failed&quot;: 0,
+                &quot;completed&quot;: WorkerConnectionPool.completed.get(workType, 0),
+                &quot;time&quot;: WorkerConnectionPool.timing.get(workType, 0.0)
+            })
</ins><span class="cx"> 
</span><span class="cx">         jobs = yield cls.all(txn)
</span><span class="cx"> 
</span><span class="cx">         for job in jobs:
</span><span class="cx">             r = results[job.workType]
</span><del>-            r[0] += 1
</del><ins>+            r[&quot;queued&quot;] += 1
</ins><span class="cx">             if job.assigned is not None:
</span><del>-                r[1] += 1
</del><ins>+                r[&quot;assigned&quot;] += 1
+            if job.assigned is None and job.notBefore &lt; now:
+                r[&quot;late&quot;] += 1
</ins><span class="cx">             if job.failed:
</span><del>-                r[2] += 1
-            if job.assigned is None and job.notBefore &lt; now:
-                r[3] += 1
</del><ins>+                r[&quot;failed&quot;] += 1
</ins><span class="cx"> 
</span><span class="cx">         returnValue(results)
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-JobDescriptor = namedtuple(&quot;JobDescriptor&quot;, [&quot;jobID&quot;, &quot;weight&quot;])
</del><ins>+JobDescriptor = namedtuple(&quot;JobDescriptor&quot;, [&quot;jobID&quot;, &quot;weight&quot;, &quot;type&quot;])
</ins><span class="cx"> 
</span><span class="cx"> class JobDescriptorArg(Argument):
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="lines">@@ -650,7 +659,7 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     def fromString(self, inString):
</span><del>-        return JobDescriptor(*map(int, inString.split(&quot;,&quot;)))
</del><ins>+        return JobDescriptor(*[f(s) for f, s in zip((int, int, str,), inString.split(&quot;,&quot;))])
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> # Priority for work - used to order work items in the job queue
</span><span class="lines">@@ -1018,6 +1027,9 @@
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx">     implements(_IJobPerformer)
</span><span class="cx"> 
</span><ins>+    completed = collections.defaultdict(int)
+    timing = collections.defaultdict(float)
+
</ins><span class="cx">     def __init__(self, maximumLoadPerWorker=WORK_WEIGHT_CAPACITY):
</span><span class="cx">         self.workers = []
</span><span class="cx">         self.maximumLoadPerWorker = maximumLoadPerWorker
</span><span class="lines">@@ -1088,6 +1100,7 @@
</span><span class="cx">         return sorted(self.workers[:], key=lambda w: w.currentLoad)[0]
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+    @inlineCallbacks
</ins><span class="cx">     def performJob(self, job):
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Select a local worker that is idle enough to perform the given job,
</span><span class="lines">@@ -1100,9 +1113,15 @@
</span><span class="cx">             complete.
</span><span class="cx">         @rtype: L{Deferred} firing L{dict}
</span><span class="cx">         &quot;&quot;&quot;
</span><ins>+
+        t = time.time()
</ins><span class="cx">         preferredWorker = self._selectLowestLoadWorker()
</span><del>-        result = preferredWorker.performJob(job)
-        return result
</del><ins>+        try:
+            result = yield preferredWorker.performJob(job)
+        finally:
+            self.completed[job.type] += 1
+            self.timing[job.type] += time.time() - t
+        returnValue(result)
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="lines">@@ -1536,6 +1555,7 @@
</span><span class="cx">         self._listeningPort = None
</span><span class="cx">         self._lastSeenTotalNodes = 1
</span><span class="cx">         self._lastSeenNodeIndex = 1
</span><ins>+        self._lastMinPriority = WORK_PRIORITY_LOW
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     def addPeerConnection(self, peer):
</span><span class="lines">@@ -1653,16 +1673,19 @@
</span><span class="cx"> 
</span><span class="cx">             # Check overload level first
</span><span class="cx">             if level &gt; self.overloadLevel:
</span><del>-                log.error(&quot;workCheck: jobqueue is overloaded&quot;)
</del><ins>+                if self._lastMinPriority != WORK_PRIORITY_HIGH + 1:
+                    log.error(&quot;workCheck: jobqueue is overloaded&quot;)
+                self._lastMinPriority = WORK_PRIORITY_HIGH + 1
</ins><span class="cx">                 break
</span><span class="cx">             elif level &gt; self.highPriorityLevel:
</span><del>-                log.debug(&quot;workCheck: jobqueue high priority only&quot;)
</del><span class="cx">                 minPriority = WORK_PRIORITY_HIGH
</span><span class="cx">             elif level &gt; self.mediumPriorityLevel:
</span><del>-                log.debug(&quot;workCheck: jobqueue high/medium priority only&quot;)
</del><span class="cx">                 minPriority = WORK_PRIORITY_MEDIUM
</span><span class="cx">             else:
</span><span class="cx">                 minPriority = WORK_PRIORITY_LOW
</span><ins>+            if self._lastMinPriority != minPriority:
+                log.debug(&quot;workCheck: jobqueue priority limit change: {}&quot;.format(minPriority))
+            self._lastMinPriority = minPriority
</ins><span class="cx"> 
</span><span class="cx">             # Determine what the timestamp cutoff
</span><span class="cx">             # TODO: here is where we should iterate over the unlocked items
</span></span></pre>
</div>
</div>

</body>
</html>