<!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>[13815] CalendarServer/trunk</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/13815">13815</a></dd>
<dt>Author</dt> <dd>gaya@apple.com</dd>
<dt>Date</dt> <dd>2014-07-31 21:41:47 -0700 (Thu, 31 Jul 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>delete cached groups after they go missing from the directory for config.AutomaticPurging.GroupPurgeIntervalSeconds</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServertrunktwistedcaldavstdconfigpy">CalendarServer/trunk/twistedcaldav/stdconfig.py</a></li>
<li><a href="#CalendarServertrunktxdavwhogroupspy">CalendarServer/trunk/txdav/who/groups.py</a></li>
<li><a href="#CalendarServertrunktxdavwhotesttest_groupspy">CalendarServer/trunk/txdav/who/test/test_groups.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServertrunktwistedcaldavstdconfigpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/twistedcaldav/stdconfig.py (13814 => 13815)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/twistedcaldav/stdconfig.py        2014-08-01 02:18:10 UTC (rev 13814)
+++ CalendarServer/trunk/twistedcaldav/stdconfig.py        2014-08-01 04:41:47 UTC (rev 13815)
</span><span class="lines">@@ -1084,6 +1084,7 @@
</span><span class="cx">         &quot;CheckStaggerSeconds&quot;: 0, # No staggering
</span><span class="cx">         &quot;PurgeIntervalSeconds&quot;: 7 * 24 * 60 * 60,   # 7 days
</span><span class="cx">         &quot;HomePurgeDelaySeconds&quot;: 60,
</span><ins>+        &quot;GroupPurgeIntervalSeconds&quot;: 7 * 24 * 60 * 60,   # 7 days
</ins><span class="cx">     },
</span><span class="cx"> 
</span><span class="cx">     &quot;Manhole&quot;: {
</span></span></pre></div>
<a id="CalendarServertrunktxdavwhogroupspy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/who/groups.py (13814 => 13815)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/who/groups.py        2014-08-01 02:18:10 UTC (rev 13814)
+++ CalendarServer/trunk/txdav/who/groups.py        2014-08-01 04:41:47 UTC (rev 13815)
</span><span class="lines">@@ -104,18 +104,12 @@
</span><span class="cx">                 )
</span><span class="cx"> 
</span><span class="cx">         else:
</span><del>-            notBefore = (
-                datetime.datetime.utcnow() +
-                datetime.timedelta(seconds=10)
-            )
</del><span class="cx">             log.debug(
</span><span class="cx">                 &quot;Rescheduling group refresh for {group}: {when}&quot;,
</span><del>-                group=self.groupUid, when=notBefore
</del><ins>+                group=self.groupUid,
+                when=datetime.datetime.utcnow() + datetime.timedelta(seconds=10)
</ins><span class="cx">             )
</span><del>-            yield self.transaction.enqueue(
-                GroupRefreshWork,
-                groupUID=self.groupUid, notBefore=notBefore
-            )
</del><ins>+            yield self.reschedule(self.transaction, 10, groupUID=self.groupUid)
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="lines">@@ -317,30 +311,46 @@
</span><span class="cx">         # Figure out which groups matter
</span><span class="cx">         groupUIDs = yield self.groupsToRefresh(txn)
</span><span class="cx">         self.log.debug(
</span><del>-            &quot;Number of groups to refresh: {num}&quot;, num=len(groupUIDs)
</del><ins>+            &quot;Groups to refresh: {g}&quot;, g=groupUIDs
</ins><span class="cx">         )
</span><ins>+
+        gr = schema.GROUPS
+        if config.AutomaticPurging.Enabled and groupUIDs:
+            # remove unused groups and groups that have not been seen in a while
+            dateLimit = (
+                datetime.datetime.utcnow() -
+                datetime.timedelta(seconds=float(config.AutomaticPurging.GroupPurgeIntervalSeconds))
+            )
+            rows = yield Delete(
+                From=gr,
+                Where=(
+                    (gr.EXTANT == 0).And(gr.MODIFIED &lt; dateLimit)
+                ).Or(
+                    gr.GROUP_UID.NotIn(
+                        Parameter(&quot;groupUIDs&quot;, len(groupUIDs))
+                    )
+                ) if groupUIDs else None,
+                Return=[gr.GROUP_UID]
+            ).on(txn, groupUIDs=groupUIDs)
+        else:
+            # remove unused groups
+            rows = yield Delete(
+                From=gr,
+                Where=gr.GROUP_UID.NotIn(
+                    Parameter(&quot;groupUIDs&quot;, len(groupUIDs))
+                ) if groupUIDs else None,
+                Return=[gr.GROUP_UID]
+            ).on(txn, groupUIDs=groupUIDs)
+        deletedGroupUIDs = [row[0] for row in rows]
+        if deletedGroupUIDs:
+            self.log.debug(&quot;Deleted old or unused groups {d}&quot;, d=deletedGroupUIDs)
+
</ins><span class="cx">         # For each of those groups, create a per-group refresh work item
</span><del>-        for groupUID in groupUIDs:
-            notBefore = (
-                datetime.datetime.utcnow() +
-                datetime.timedelta(seconds=1)
-            )
</del><ins>+        for groupUID in set(groupUIDs) - set(deletedGroupUIDs):
</ins><span class="cx">             self.log.debug(&quot;Enqueuing group refresh for {u}&quot;, u=groupUID)
</span><del>-            yield txn.enqueue(
-                GroupRefreshWork, groupUid=groupUID, notBefore=notBefore
-            )
-            self.log.debug(&quot;Enqueued group refresh for {u}&quot;, u=groupUID)
</del><ins>+            yield GroupRefreshWork.reschedule(txn, 0, groupUid=groupUID)
</ins><span class="cx"> 
</span><del>-        # remove unused groups
-        gr = schema.GROUPS
-        yield Delete(
-            From=gr,
-            Where=gr.GROUP_UID.NotIn(
-                Parameter(&quot;groupUIDs&quot;, len(groupUIDs))
-            ) if groupUIDs else None
-        ).on(txn, groupUIDs=groupUIDs)
</del><span class="cx"> 
</span><del>-
</del><span class="cx">     @inlineCallbacks
</span><span class="cx">     def applyExternalAssignments(self, txn, newAssignments):
</span><span class="cx"> 
</span></span></pre></div>
<a id="CalendarServertrunktxdavwhotesttest_groupspy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/who/test/test_groups.py (13814 => 13815)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/who/test/test_groups.py        2014-08-01 02:18:10 UTC (rev 13814)
+++ CalendarServer/trunk/txdav/who/test/test_groups.py        2014-08-01 04:41:47 UTC (rev 13815)
</span><span class="lines">@@ -18,11 +18,14 @@
</span><span class="cx"> Group membership caching implementation tests
</span><span class="cx"> &quot;&quot;&quot;
</span><span class="cx"> 
</span><ins>+from twext.enterprise.jobqueue import JobItem
</ins><span class="cx"> from twext.who.idirectory import RecordType
</span><ins>+from twisted.internet import reactor
</ins><span class="cx"> from twisted.internet.defer import inlineCallbacks
</span><ins>+from twistedcaldav.stdconfig import config
</ins><span class="cx"> from twistedcaldav.test.util import StoreTestCase
</span><span class="cx"> from txdav.common.icommondatastore import NotFoundError
</span><del>-from txdav.who.groups import GroupCacher, diffAssignments
</del><ins>+from txdav.who.groups import GroupCacher, diffAssignments, GroupRefreshWork
</ins><span class="cx"> from txdav.who.test.support import TestRecord, CalendarInMemoryDirectoryService
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="lines">@@ -498,6 +501,17 @@
</span><span class="cx">         yield group.setMembers(members)
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+        def doWork(self):
+            self.transaction._groupCacher = groupCacher
+            return unpatchedDoWork(self)
+
+        groupCacher = self.groupCacher
+        unpatchedDoWork = GroupRefreshWork.doWork
+        self.patch(GroupRefreshWork, &quot;doWork&quot;, doWork)
+
+        config.AutomaticPurging.Enabled = True
+
+
</ins><span class="cx">     @inlineCallbacks
</span><span class="cx">     def test_extant(self):
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="lines">@@ -577,7 +591,7 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     @inlineCallbacks
</span><del>-    def test_update_delete(self):
</del><ins>+    def test_update_delete_unused(self):
</ins><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Verify that unused groups are deleted from group cache
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="lines">@@ -596,6 +610,7 @@
</span><span class="cx">             txn = store.newTransaction()
</span><span class="cx">             yield self.groupCacher.update(txn)
</span><span class="cx">             groupID = (yield txn.groupByUID(uid, create=False))[0]
</span><ins>+            yield txn.commit()
</ins><span class="cx"> 
</span><span class="cx">             self.assertEqual(groupID, None)
</span><span class="cx"> 
</span><span class="lines">@@ -611,8 +626,13 @@
</span><span class="cx"> 
</span><span class="cx">             txn = store.newTransaction()
</span><span class="cx">             yield self.groupCacher.update(txn)
</span><ins>+            yield txn.commit()
+            yield JobItem.waitEmpty(store.newTransaction, reactor, 60)
+
+            txn = store.newTransaction()
</ins><span class="cx">             groupID = (yield txn.groupByUID(uid, create=False))[0]
</span><span class="cx">             yield txn.commit()
</span><ins>+
</ins><span class="cx">             self.assertNotEqual(groupID, None)
</span><span class="cx"> 
</span><span class="cx">         # delegate group is deleted. unused group is deleted
</span><span class="lines">@@ -628,9 +648,67 @@
</span><span class="cx"> 
</span><span class="cx">         txn = store.newTransaction()
</span><span class="cx">         yield self.groupCacher.update(txn)
</span><ins>+        yield txn.commit()
+        yield JobItem.waitEmpty(store.newTransaction, reactor, 60)
+
+        txn = store.newTransaction()
</ins><span class="cx">         testGroupID = (yield txn.groupByUID(u&quot;testgroup&quot;, create=False))[0]
</span><span class="cx">         emptyGroupID = (yield txn.groupByUID(u&quot;emptygroup&quot;, create=False))[0]
</span><span class="cx">         yield txn.commit()
</span><span class="cx"> 
</span><span class="cx">         self.assertEqual(testGroupID, None)
</span><span class="cx">         self.assertNotEqual(emptyGroupID, None)
</span><ins>+
+
+    @inlineCallbacks
+    def test_update_delete_old_nonextant(self):
+        &quot;&quot;&quot;
+        Verify that old missing groups are deleted from group cache
+        &quot;&quot;&quot;
+
+        oldGroupPurgeIntervalSeconds = config.AutomaticPurging.GroupPurgeIntervalSeconds
+        store = self.storeUnderTest()
+
+        for uid in (u&quot;testgroup&quot;, u&quot;emptygroup&quot;,):
+
+            config.AutomaticPurging.GroupPurgeIntervalSeconds = oldGroupPurgeIntervalSeconds
+            txn = store.newTransaction()
+            groupID = (yield txn.groupByUID(uid))[0]
+            yield txn.addDelegateGroup(delegator=u&quot;sagen&quot;, delegateGroupID=groupID, readWrite=True)
+            (
+                groupID, _ignore_name, _ignore_membershipHash, _ignore_modified,
+                extant
+            ) = yield txn.groupByUID(uid, create=False)
+            yield txn.commit()
+
+            self.assertTrue(extant)
+            self.assertNotEqual(groupID, None)
+
+            # Remove the group, still cached
+            yield self.directory.removeRecords([uid])
+            txn = store.newTransaction()
+            yield self.groupCacher.update(txn)
+            (
+                groupID, _ignore_name, _ignore_membershipHash, _ignore_modified,
+                extant
+            ) = yield txn.groupByUID(uid, create=False)
+            yield txn.commit()
+            yield JobItem.waitEmpty(store.newTransaction, reactor, 60)
+
+            txn = store.newTransaction()
+            (
+                groupID, _ignore_name, _ignore_membershipHash, _ignore_modified,
+                extant
+            ) = yield txn.groupByUID(uid, create=False)
+            yield txn.commit()
+            self.assertNotEqual(groupID, None)
+            self.assertFalse(extant)
+
+            # delete the group
+            config.AutomaticPurging.GroupPurgeIntervalSeconds = &quot;0.0&quot;
+
+            txn = store.newTransaction()
+            yield self.groupCacher.update(txn)
+            groupID = (yield txn.groupByUID(uid, create=False))[0]
+            yield txn.commit()
+            self.assertEqual(groupID, None)
</ins></span></pre>
</div>
</div>

</body>
</html>