<!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>[15095] twext/trunk/twext/enterprise/adbapi2.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/15095">15095</a></dd>
<dt>Author</dt> <dd>cdaboo@apple.com</dd>
<dt>Date</dt> <dd>2015-09-03 13:40:30 -0700 (Thu, 03 Sep 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Add warning log messages when a txn stalls due to DB pool being fully utilized.</pre>

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

</div>
<div id="patch">
<h3>Diff</h3>
<a id="twexttrunktwextenterpriseadbapi2py"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/enterprise/adbapi2.py (15094 => 15095)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/enterprise/adbapi2.py        2015-09-03 20:39:06 UTC (rev 15094)
+++ twext/trunk/twext/enterprise/adbapi2.py        2015-09-03 20:40:30 UTC (rev 15095)
</span><span class="lines">@@ -31,6 +31,7 @@
</span><span class="cx"> 
</span><span class="cx"> import sys
</span><span class="cx"> import weakref
</span><ins>+import time
</ins><span class="cx"> 
</span><span class="cx"> from cStringIO import StringIO
</span><span class="cx"> from cPickle import dumps, loads
</span><span class="lines">@@ -47,7 +48,6 @@
</span><span class="cx"> from twisted.protocols.amp import Argument, String, Command, AMP, Integer
</span><span class="cx"> from twisted.internet import reactor as _reactor
</span><span class="cx"> from twisted.application.service import Service
</span><del>-from twisted.python import log
</del><span class="cx"> from twisted.internet.defer import maybeDeferred
</span><span class="cx"> from twisted.python.components import proxyForInterface
</span><span class="cx"> 
</span><span class="lines">@@ -63,7 +63,10 @@
</span><span class="cx">     AlreadyFinishedError, IAsyncTransaction, POSTGRES_DIALECT, ICommandBlock
</span><span class="cx"> )
</span><span class="cx"> 
</span><ins>+from twext.python.log import Logger
+log = Logger()
</ins><span class="cx"> 
</span><ins>+
</ins><span class="cx"> # FIXME: there should be no defaults for connection metadata, it should be
</span><span class="cx"> # discovered dynamically everywhere.  Right now it's specified as an explicit
</span><span class="cx"> # argument to the ConnectionPool but it should probably be determined
</span><span class="lines">@@ -252,11 +255,11 @@
</span><span class="cx">                 # Report the error before doing anything else, since doing
</span><span class="cx">                 # other things may cause the traceback stack to be eliminated
</span><span class="cx">                 # if they raise exceptions (even internally).
</span><del>-                log.err(
-                    Failure(),
</del><ins>+                log.failure(
</ins><span class="cx">                     &quot;Exception from execute() on first statement in &quot;
</span><span class="cx">                     &quot;transaction.  Possibly caused by a database server &quot;
</span><del>-                    &quot;restart.  Automatically reconnecting now.&quot;
</del><ins>+                    &quot;restart.  Automatically reconnecting now.&quot;,
+                    failure=Failure(),
</ins><span class="cx">                 )
</span><span class="cx">                 try:
</span><span class="cx">                     self._connection.close()
</span><span class="lines">@@ -273,10 +276,10 @@
</span><span class="cx">                     # making debugging surprising error conditions very
</span><span class="cx">                     # difficult, so let's make sure that the error is logged
</span><span class="cx">                     # just in case.
</span><del>-                    log.err(
-                        Failure(),
</del><ins>+                    log.failure(
</ins><span class="cx">                         &quot;Exception from close() while automatically &quot;
</span><del>-                        &quot;reconnecting. (Probably not serious.)&quot;
</del><ins>+                        &quot;reconnecting. (Probably not serious.)&quot;,
+                        failure=Failure(),
</ins><span class="cx">                     )
</span><span class="cx"> 
</span><span class="cx">                 # Now, if either of *these* things fail, there's an error here
</span><span class="lines">@@ -373,11 +376,15 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     def abort(self):
</span><del>-        return self._end(self._connection.rollback).addErrback(log.err)
</del><ins>+        def _report(f):
+            log.failure(&quot;txn abort&quot;, failure=f)
+        return self._end(self._connection.rollback).addErrback(_report)
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     def terminate(self):
</span><del>-        return self._end(self._connection.rollback, terminate=True).addErrback(log.err)
</del><ins>+        def _report(f):
+            log.failure(&quot;txn abort&quot;, failure=f)
+        return self._end(self._connection.rollback, terminate=True).addErrback(_report)
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     def reset(self):
</span><span class="lines">@@ -1121,12 +1128,31 @@
</span><span class="cx">             basetxn._label = label
</span><span class="cx">             self._busy.append(basetxn)
</span><span class="cx">             txn = _SingleTxn(self, basetxn)
</span><ins>+            log.debug(
+                &quot;ConnectionPool: txn busy '{label}': free={free}, busy={busy}, waiting={waiting}&quot;,
+                label=label,
+                free=len(self._free),
+                busy=len(self._busy) + len(self._finishing),
+                waiting=len(self._waiting),
+            )
</ins><span class="cx">         else:
</span><span class="cx">             txn = _SingleTxn(self, _WaitingTxn(self, label=label))
</span><span class="cx">             self._waiting.append(txn)
</span><ins>+            blocked = self._activeConnectionCount() &gt;= self.maxConnections
+            if blocked:
+                txn._blocked_waiting_time = time.time()
+                log.warn(&quot;ConnectionPool: txn blocked '{label}'&quot;, label=label)
+            log.debug(
+                &quot;ConnectionPool: txn waiting add '{label}': free={free}, busy={busy}, waiting={waiting} {blocked}&quot;,
+                label=label,
+                free=len(self._free),
+                busy=len(self._busy) + len(self._finishing),
+                waiting=len(self._waiting),
+                blocked=&quot;blocked&quot; if blocked else &quot;&quot;,
+            )
</ins><span class="cx">             # FIXME/TESTME: should be len(self._busy) + len(self._finishing)
</span><span class="cx">             # (free doesn't need to be considered, as it's tested above)
</span><del>-            if self._activeConnectionCount() &lt; self.maxConnections:
</del><ins>+            if not blocked:
</ins><span class="cx">                 self._startOneMore()
</span><span class="cx"> 
</span><span class="cx">         return txn
</span><span class="lines">@@ -1172,7 +1198,7 @@
</span><span class="cx">             self._repoolNow(baseTxn)
</span><span class="cx"> 
</span><span class="cx">         def maybeTryAgain(f):
</span><del>-            log.err(f, &quot;Re-trying connection due to connection failure&quot;)
</del><ins>+            log.failure(&quot;Re-trying connection due to connection failure&quot;, failure=f)
</ins><span class="cx">             txn._retry = self.reactor.callLater(self.RETRY_TIMEOUT, resubmit)
</span><span class="cx"> 
</span><span class="cx">         def resubmit():
</span><span class="lines">@@ -1214,12 +1240,32 @@
</span><span class="cx">             waiting = self._waiting.pop(0)
</span><span class="cx">             self._busy.append(txn)
</span><span class="cx">             waiting._unspoolOnto(txn)
</span><ins>+            if hasattr(waiting, &quot;_blocked_waiting_time&quot;):
+                log.warn(
+                    &quot;ConnectionPool: txn unblocked '{label}': delay {delay:.1f}ms&quot;,
+                    label=waiting._label,
+                    delay=1000 * (time.time() - waiting._blocked_waiting_time),
+                )
+            log.debug(
+                &quot;ConnectionPool: txn waiting remove '{label}': free={free}, busy={busy}, waiting={waiting}&quot;,
+                label=waiting._label,
+                free=len(self._free),
+                busy=len(self._busy) + len(self._finishing),
+                waiting=len(self._waiting),
+            )
</ins><span class="cx">         else:
</span><span class="cx">             # If we are stopping, never add to the free list - release it
</span><span class="cx">             if self._stopping:
</span><span class="cx">                 txn._releaseConnection()
</span><span class="cx">             else:
</span><span class="cx">                 self._free.append(txn)
</span><ins>+                log.debug(
+                    &quot;ConnectionPool: txn free '{label}': free={free}, busy={busy}, waiting={waiting}&quot;,
+                    label=txn._label,
+                    free=len(self._free),
+                    busy=len(self._busy) + len(self._finishing),
+                    waiting=len(self._waiting),
+                )
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="lines">@@ -1309,7 +1355,7 @@
</span><span class="cx">                 if f.type in command.errors:
</span><span class="cx">                     returnValue(f)
</span><span class="cx">                 else:
</span><del>-                    log.err(Failure(), &quot;shared database connection pool error&quot;)
</del><ins>+                    log.failure(&quot;shared database connection pool error&quot;, failure=f)
</ins><span class="cx">                     raise FailsafeException()
</span><span class="cx">             else:
</span><span class="cx">                 returnValue(val)
</span><span class="lines">@@ -1423,7 +1469,7 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     def stopReceivingBoxes(self, why):
</span><del>-        log.msg(&quot;(S) Stopped receiving boxes: &quot; + why.getTraceback())
</del><ins>+        log.info(&quot;(S) Stopped receiving boxes: {}tb&quot;, tb=why.getTraceback())
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     def unhandledError(self, failure):
</span><span class="lines">@@ -1431,7 +1477,7 @@
</span><span class="cx">         An unhandled error has occurred.  Since we can't really classify errors
</span><span class="cx">         well on this protocol, log it and forget it.
</span><span class="cx">         &quot;&quot;&quot;
</span><del>-        log.err(failure, &quot;Shared connection pool server encountered an error.&quot;)
</del><ins>+        log.failure(&quot;Shared connection pool server encountered an error.&quot;, failure=failure)
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     @failsafeResponder(StartTxn)
</span><span class="lines">@@ -1544,11 +1590,11 @@
</span><span class="cx">         An unhandled error has occurred.  Since we can't really classify errors
</span><span class="cx">         well on this protocol, log it and forget it.
</span><span class="cx">         &quot;&quot;&quot;
</span><del>-        log.err(failure, &quot;Shared connection pool client encountered an error.&quot;)
</del><ins>+        log.failure(&quot;Shared connection pool client encountered an error.&quot;, failure=failure)
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     def stopReceivingBoxes(self, why):
</span><del>-        log.msg(&quot;(C) Stopped receiving boxes: &quot; + why.getTraceback())
</del><ins>+        log.info(&quot;(C) Stopped receiving boxes: {tb}&quot;, tb=why.getTraceback())
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     def newTransaction(self):
</span></span></pre>
</div>
</div>

</body>
</html>