<!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>[15111] twext/trunk/twext/who/ldap</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/15111">15111</a></dd>
<dt>Author</dt> <dd>sagen@apple.com</dd>
<dt>Date</dt> <dd>2015-09-09 13:08:14 -0700 (Wed, 09 Sep 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>If we get ldap.SERVER_DOWN, retry a couple times with a new connection</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#twexttrunktwextwholdap__init__py">twext/trunk/twext/who/ldap/__init__.py</a></li>
<li><a href="#twexttrunktwextwholdap_servicepy">twext/trunk/twext/who/ldap/_service.py</a></li>
<li><a href="#twexttrunktwextwholdaptesttest_servicepy">twext/trunk/twext/who/ldap/test/test_service.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="twexttrunktwextwholdap__init__py"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/who/ldap/__init__.py (15110 => 15111)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/who/ldap/__init__.py        2015-09-08 19:11:06 UTC (rev 15110)
+++ twext/trunk/twext/who/ldap/__init__.py        2015-09-09 20:08:14 UTC (rev 15111)
</span><span class="lines">@@ -23,6 +23,7 @@
</span><span class="cx">     &quot;LDAPConfigurationError&quot;,
</span><span class="cx">     &quot;LDAPConnectionError&quot;,
</span><span class="cx">     &quot;LDAPBindAuthError&quot;,
</span><ins>+    &quot;LDAPQueryError&quot;,
</ins><span class="cx">     &quot;RecordTypeSchema&quot;,
</span><span class="cx">     &quot;DirectoryService&quot;,
</span><span class="cx">     &quot;LDAPAttribute&quot;,
</span><span class="lines">@@ -36,6 +37,7 @@
</span><span class="cx">     LDAPConfigurationError,
</span><span class="cx">     LDAPConnectionError,
</span><span class="cx">     LDAPBindAuthError,
</span><ins>+    LDAPQueryError,
</ins><span class="cx">     RecordTypeSchema,
</span><span class="cx">     DirectoryService,
</span><span class="cx">     FieldName
</span></span></pre></div>
<a id="twexttrunktwextwholdap_servicepy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/who/ldap/_service.py (15110 => 15111)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/who/ldap/_service.py        2015-09-08 19:11:06 UTC (rev 15110)
+++ twext/trunk/twext/who/ldap/_service.py        2015-09-09 20:08:14 UTC (rev 15111)
</span><span class="lines">@@ -603,105 +603,124 @@
</span><span class="cx">         This method is always called in a thread.
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx"> 
</span><del>-        records = []
</del><ins>+        if recordTypes is None:
+            recordTypes = list(self.recordTypes())
</ins><span class="cx"> 
</span><del>-        with DirectoryService.Connection(self) as connection:
</del><ins>+        # Retry if we get ldap.SERVER_DOWN
+        TRIES = 3
</ins><span class="cx"> 
</span><ins>+        for self._retryNumber in xrange(TRIES):
</ins><span class="cx"> 
</span><del>-            if recordTypes is None:
-                recordTypes = self.recordTypes()
</del><ins>+            records = []
</ins><span class="cx"> 
</span><del>-            for recordType in recordTypes:
</del><ins>+            try:
</ins><span class="cx"> 
</span><del>-                if limitResults is not None:
-                    if limitResults &lt; 1:
-                        break
</del><ins>+                with DirectoryService.Connection(self) as connection:
</ins><span class="cx"> 
</span><del>-                try:
-                    rdn = self._recordTypeSchemas[recordType].relativeDN
-                except KeyError:
-                    # Skip this unknown record type
-                    continue
</del><ins>+                    for recordType in recordTypes:
</ins><span class="cx"> 
</span><del>-                rdn = (
-                    ldap.dn.str2dn(rdn.lower()) +
-                    ldap.dn.str2dn(self._baseDN.lower())
-                )
-                filteredQuery = self._addExtraFilter(recordType, queryString)
-                self.log.debug(
-                    &quot;Performing LDAP query: {rdn} {query} {recordType}{limit}{timeout}&quot;,
-                    rdn=rdn,
-                    query=filteredQuery,
-                    recordType=recordType,
-                    limit=&quot; limit={}&quot;.format(limitResults) if limitResults else &quot;&quot;,
-                    timeout=&quot; timeout={}&quot;.format(timeoutSeconds) if timeoutSeconds else &quot;&quot;,
-                )
-                try:
-                    s = ldap.async.List(connection)
-                    s.startSearch(
-                        ldap.dn.dn2str(rdn),
-                        ldap.SCOPE_SUBTREE,
-                        filteredQuery,
-                        attrList=self._attributesToFetch,
-                        timeout=timeoutSeconds if timeoutSeconds else -1,
-                        sizelimit=limitResults if limitResults else 0
-                    )
-                    s.processResults()
</del><ins>+                        if limitResults is not None:
+                            if limitResults &lt; 1:
+                                break
</ins><span class="cx"> 
</span><del>-                except ldap.SIZELIMIT_EXCEEDED as e:
-                    self.log.debug(&quot;LDAP result limit exceeded: {}&quot;.format(limitResults,))
</del><ins>+                        try:
+                            rdn = self._recordTypeSchemas[recordType].relativeDN
+                        except KeyError:
+                            # Skip this unknown record type
+                            continue
</ins><span class="cx"> 
</span><del>-                except ldap.TIMELIMIT_EXCEEDED as e:
-                    self.log.warn(&quot;LDAP timeout exceeded: {} seconds&quot;.format(timeoutSeconds,))
</del><ins>+                        rdn = (
+                            ldap.dn.str2dn(rdn.lower()) +
+                            ldap.dn.str2dn(self._baseDN.lower())
+                        )
+                        filteredQuery = self._addExtraFilter(recordType, queryString)
+                        self.log.debug(
+                            &quot;Performing LDAP query: {rdn} {query} {recordType}{limit}{timeout}&quot;,
+                            rdn=rdn,
+                            query=filteredQuery,
+                            recordType=recordType,
+                            limit=&quot; limit={}&quot;.format(limitResults) if limitResults else &quot;&quot;,
+                            timeout=&quot; timeout={}&quot;.format(timeoutSeconds) if timeoutSeconds else &quot;&quot;,
+                        )
+                        try:
+                            s = ldap.async.List(connection)
+                            s.startSearch(
+                                ldap.dn.dn2str(rdn),
+                                ldap.SCOPE_SUBTREE,
+                                filteredQuery,
+                                attrList=self._attributesToFetch,
+                                timeout=timeoutSeconds if timeoutSeconds else -1,
+                                sizelimit=limitResults if limitResults else 0
+                            )
+                            s.processResults()
</ins><span class="cx"> 
</span><del>-                except ldap.FILTER_ERROR as e:
-                    self.log.error(
-                        &quot;Unable to perform query {query!r}: {err}&quot;,
-                        query=queryString, err=e
-                    )
-                    raise LDAPQueryError(&quot;Unable to perform query&quot;, e)
</del><ins>+                        except ldap.SIZELIMIT_EXCEEDED as e:
+                            self.log.debug(&quot;LDAP result limit exceeded: {}&quot;.format(limitResults,))
</ins><span class="cx"> 
</span><del>-                except ldap.NO_SUCH_OBJECT as e:
-                    # self.log.warn(&quot;RDN {rdn} does not exist, skipping&quot;, rdn=rdn)
-                    continue
</del><ins>+                        except ldap.TIMELIMIT_EXCEEDED as e:
+                            self.log.warn(&quot;LDAP timeout exceeded: {} seconds&quot;.format(timeoutSeconds,))
</ins><span class="cx"> 
</span><del>-                except ldap.INVALID_SYNTAX as e:
-                    self.log.error(
-                        &quot;LDAP invalid syntax {query!r}: {err}&quot;,
-                        query=queryString, err=e
-                    )
-                    continue
</del><ins>+                        except ldap.FILTER_ERROR as e:
+                            self.log.error(
+                                &quot;Unable to perform query {query!r}: {err}&quot;,
+                                query=queryString, err=e
+                            )
+                            raise LDAPQueryError(&quot;Unable to perform query&quot;, e)
</ins><span class="cx"> 
</span><del>-                except ldap.SERVER_DOWN as e:
-                    self.log.error(
-                        &quot;LDAP server unavailable&quot;
-                    )
-                    raise LDAPQueryError(&quot;LDAP server down&quot;, e)
</del><ins>+                        except ldap.NO_SUCH_OBJECT as e:
+                            # self.log.warn(&quot;RDN {rdn} does not exist, skipping&quot;, rdn=rdn)
+                            continue
</ins><span class="cx"> 
</span><del>-                except Exception as e:
-                    self.log.error(
-                        &quot;LDAP error {query!r}: {err}&quot;,
-                        query=queryString, err=e
-                    )
-                    raise LDAPQueryError(&quot;Unable to perform query&quot;, e)
</del><ins>+                        except ldap.INVALID_SYNTAX as e:
+                            self.log.error(
+                                &quot;LDAP invalid syntax {query!r}: {err}&quot;,
+                                query=queryString, err=e
+                            )
+                            continue
</ins><span class="cx"> 
</span><del>-                reply = [resultItem for _ignore_resultType, resultItem in s.allResults]
</del><ins>+                        except ldap.SERVER_DOWN as e:
+                            # Catch this below for retry
+                            raise e
</ins><span class="cx"> 
</span><del>-                newRecords = self._recordsFromReply(reply, recordType=recordType)
</del><ins>+                        except Exception as e:
+                            self.log.error(
+                                &quot;LDAP error {query!r}: {err}&quot;,
+                                query=queryString, err=e
+                            )
+                            raise LDAPQueryError(&quot;Unable to perform query&quot;, e)
</ins><span class="cx"> 
</span><del>-                self.log.debug(
-                    &quot;Records from LDAP query ({rdn} {query} {recordType}): {count}&quot;,
-                    rdn=rdn,
-                    query=queryString,
-                    recordType=recordType,
-                    count=len(newRecords)
</del><ins>+                        reply = [resultItem for _ignore_resultType, resultItem in s.allResults]
+
+                        newRecords = self._recordsFromReply(reply, recordType=recordType)
+
+                        self.log.debug(
+                            &quot;Records from LDAP query ({rdn} {query} {recordType}): {count}&quot;,
+                            rdn=rdn,
+                            query=queryString,
+                            recordType=recordType,
+                            count=len(newRecords)
+                        )
+
+                        if limitResults is not None:
+                            limitResults = limitResults - len(newRecords)
+
+                        records.extend(newRecords)
+
+            except ldap.SERVER_DOWN as e:
+                self.log.error(
+                    &quot;LDAP server unavailable&quot;
</ins><span class="cx">                 )
</span><ins>+                if self._retryNumber + 1 == TRIES:
+                    # We've hit SERVER_DOWN TRIES times, giving up
+                    raise LDAPQueryError(&quot;LDAP server down&quot;, e)
+                else:
+                    self.log.error(&quot;LDAP connection failure; retrying...&quot;)
</ins><span class="cx"> 
</span><del>-                if limitResults is not None:
-                    limitResults = limitResults - len(newRecords)
</del><ins>+            else:
+                # Only retry if we got ldap.SERVER_DOWN, otherwise break out of
+                # loop
+                break
</ins><span class="cx"> 
</span><del>-                records.extend(newRecords)
-
</del><span class="cx">         self.log.debug(
</span><span class="cx">             &quot;LDAP result count ({query}): {count}&quot;,
</span><span class="cx">             query=queryString,
</span><span class="lines">@@ -725,24 +744,49 @@
</span><span class="cx">         @param dn: The DN of the record to search for
</span><span class="cx">         @type dn: C{str}
</span><span class="cx">         &quot;&quot;&quot;
</span><del>-        with DirectoryService.Connection(self) as connection:
</del><span class="cx"> 
</span><del>-            self.log.debug(&quot;Performing LDAP DN query: {dn}&quot;, dn=dn)
</del><ins>+        records = []
</ins><span class="cx"> 
</span><ins>+        # Retry if we get ldap.SERVER_DOWN
+        TRIES = 3
+
+        for self._retryNumber in xrange(TRIES):
+
</ins><span class="cx">             try:
</span><del>-                reply = connection.search_s(
-                    dn,
-                    ldap.SCOPE_SUBTREE,
-                    &quot;(objectClass=*)&quot;,
-                    attrlist=self._attributesToFetch
</del><ins>+
+                with DirectoryService.Connection(self) as connection:
+
+                    self.log.debug(&quot;Performing LDAP DN query: {dn}&quot;, dn=dn)
+
+                    try:
+                        reply = connection.search_s(
+                            dn,
+                            ldap.SCOPE_SUBTREE,
+                            &quot;(objectClass=*)&quot;,
+                            attrlist=self._attributesToFetch
+                        )
+                        records = self._recordsFromReply(reply)
+                    except ldap.NO_SUCH_OBJECT:
+                        records = []
+                    except ldap.INVALID_DN_SYNTAX:
+                        self.log.warn(&quot;Invalid LDAP DN syntax: '{dn}'&quot;, dn=dn)
+                        records = []
+
+            except ldap.SERVER_DOWN as e:
+                self.log.error(
+                    &quot;LDAP server unavailable&quot;
</ins><span class="cx">                 )
</span><del>-                records = self._recordsFromReply(reply)
-            except ldap.NO_SUCH_OBJECT:
-                records = []
-            except ldap.INVALID_DN_SYNTAX:
-                self.log.warn(&quot;Invalid LDAP DN syntax: '{dn}'&quot;, dn=dn)
-                records = []
</del><ins>+                if self._retryNumber + 1 == TRIES:
+                    # We've hit SERVER_DOWN TRIES times, giving up
+                    raise LDAPQueryError(&quot;LDAP server down&quot;, e)
+                else:
+                    self.log.error(&quot;LDAP connection failure; retrying...&quot;)
</ins><span class="cx"> 
</span><ins>+            else:
+                # Only retry if we got ldap.SERVER_DOWN, otherwise break out of
+                # loop
+                break
+
</ins><span class="cx">         if len(records):
</span><span class="cx">             return records[0]
</span><span class="cx">         else:
</span></span></pre></div>
<a id="twexttrunktwextwholdaptesttest_servicepy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/who/ldap/test/test_service.py (15110 => 15111)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/who/ldap/test/test_service.py        2015-09-08 19:11:06 UTC (rev 15110)
+++ twext/trunk/twext/who/ldap/test/test_service.py        2015-09-09 20:08:14 UTC (rev 15111)
</span><span class="lines">@@ -58,7 +58,7 @@
</span><span class="cx"> from twext.python.types import MappingProxyType
</span><span class="cx"> from twext.who.idirectory import RecordType
</span><span class="cx"> from twext.who.ldap import (
</span><del>-    LDAPAttribute, RecordTypeSchema, LDAPObjectClass
</del><ins>+    LDAPAttribute, RecordTypeSchema, LDAPObjectClass, LDAPQueryError
</ins><span class="cx"> )
</span><span class="cx"> from twext.who.util import ConstantsContainer
</span><span class="cx"> 
</span><span class="lines">@@ -445,6 +445,55 @@
</span><span class="cx">         self.assertEquals(repr(service), u&quot;&lt;TestService u'ldap://localhost/'&gt;&quot;)
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+    def test_server_down(self):
+        &quot;&quot;&quot;
+        Verify an ldap.SERVER_DOWN error will retry 2 more times and that
+        the connection is closed if all attempts fail.
+        &quot;&quot;&quot;
+
+        service = self.service()
+
+        # Verify that without a SERVER_DOWN we don't need to retry, and we
+        # still have a connection in the pool
+        service._recordsFromQueryString_inThread(&quot;(this=that)&quot;)
+        self.assertEquals(service._retryNumber, 0)
+        self.assertEquals(len(service.connections), 1)
+
+        service._recordWithDN_inThread(&quot;cn=test&quot;)
+        self.assertEquals(service._retryNumber, 0)
+        self.assertEquals(len(service.connections), 1)
+
+        # Force a search to raise SERVER_DOWN
+        def raiseServerDown(*args, **kwds):
+            raise ldap.SERVER_DOWN
+        self.patch(LDAPObject, &quot;search_ext&quot;, raiseServerDown)
+        self.patch(LDAPObject, &quot;search_s&quot;, raiseServerDown)
+
+        # Now try recordsFromQueryString
+        try:
+            service._recordsFromQueryString_inThread(&quot;(this=that)&quot;)
+        except LDAPQueryError:
+            # Verify the number of times we retried
+            self.assertEquals(service._retryNumber, 2)
+        except:
+            self.fail(&quot;Should have raised LDAPQueryError&quot;)
+
+        # Verify the connections are all closed
+        self.assertEquals(len(service.connections), 0)
+
+        # Now try recordWithDN
+        try:
+            service._recordWithDN_inThread(&quot;cn=test&quot;)
+        except LDAPQueryError:
+            # Verify the number of times we retried
+            self.assertEquals(service._retryNumber, 2)
+        except:
+            self.fail(&quot;Should have raised LDAPQueryError&quot;)
+
+        # Verify the connections are all closed
+        self.assertEquals(len(service.connections), 0)
+
+
</ins><span class="cx"> class ExtraFiltersTest(BaseTestCase, unittest.TestCase):
</span><span class="cx"> 
</span><span class="cx">     def test_extraFilters(self):
</span></span></pre>
</div>
</div>

</body>
</html>