<!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>[15506] CalendarServer/trunk/txdav</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/15506">15506</a></dd>
<dt>Author</dt> <dd>sagen@apple.com</dd>
<dt>Date</dt> <dd>2016-04-11 17:07:53 -0700 (Mon, 11 Apr 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>Batch when cleaning out unused or missing groups</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServertrunktxdavcommondatastoresql_directorypy">CalendarServer/trunk/txdav/common/datastore/sql_directory.py</a></li>
<li><a href="#CalendarServertrunktxdavwhogroupspy">CalendarServer/trunk/txdav/who/groups.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServertrunktxdavcommondatastoresql_directorypy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/common/datastore/sql_directory.py (15505 => 15506)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/common/datastore/sql_directory.py        2016-04-11 21:09:14 UTC (rev 15505)
+++ CalendarServer/trunk/txdav/common/datastore/sql_directory.py        2016-04-12 00:07:53 UTC (rev 15506)
</span><span class="lines">@@ -52,7 +52,21 @@
</span><span class="cx">         )
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+    @classmethod
+    def allGroups(cls, txn):
</ins><span class="cx"> 
</span><ins>+        return GroupsRecord.query(txn, None)
+
+
+    @classmethod
+    def groupsMissingSince(cls, txn, since):
+
+        return GroupsRecord.query(
+            txn,
+            (cls.extant == 0).And(cls.modified &lt; since)
+        )
+
+
</ins><span class="cx"> class GroupMembershipRecord(SerializableRecord, fromTable(schema.GROUP_MEMBERSHIP)):
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx">     @DynamicAttrs
</span><span class="lines">@@ -452,7 +466,40 @@
</span><span class="cx">         returnValue(set([group.groupUID.decode(&quot;utf-8&quot;) for group in groups]))
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+    @inlineCallbacks
+    def allGroups(self):
+        &quot;&quot;&quot;
+        Return the UIDs of all groups.
</ins><span class="cx"> 
</span><ins>+        @returns: the UIDs of all groups
+        @rtype: a Deferred resulting in a set
+        &quot;&quot;&quot;
+
+        results = yield GroupsRecord.allGroups(self)
+        groups = set([record.groupUID.decode(&quot;utf-8&quot;) for record in results])
+
+        returnValue(groups)
+
+
+    @inlineCallbacks
+    def groupsMissingSince(self, since):
+        &quot;&quot;&quot;
+        @param since: datetime to compare against
+        @type since: C{datetime.datetime}
+
+        Return the UIDs of all groups marked as non-extant who have not been
+            modified since the given datetime.
+
+        @returns: UIDs
+        @rtype: a Deferred resulting in a set
+        &quot;&quot;&quot;
+
+        results = yield GroupsRecord.groupsMissingSince(self, since)
+        groups = set([record.groupUID.decode(&quot;utf-8&quot;) for record in results])
+
+        returnValue(groups)
+
+
</ins><span class="cx"> class DelegatesAPIMixin(object):
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx">     A mixin for L{CommonStoreTransaction} that covers the delegates API.
</span></span></pre></div>
<a id="CalendarServertrunktxdavwhogroupspy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/who/groups.py (15505 => 15506)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/who/groups.py        2016-04-11 21:09:14 UTC (rev 15505)
+++ CalendarServer/trunk/txdav/who/groups.py        2016-04-12 00:07:53 UTC (rev 15506)
</span><span class="lines">@@ -272,31 +272,40 @@
</span><span class="cx">         #     &quot;Groups to refresh: {g}&quot;, g=groupUIDs
</span><span class="cx">         # )
</span><span class="cx"> 
</span><del>-        if config.AutomaticPurging.Enabled and groupUIDs:
-            # remove unused groups and groups that have not been seen in a while
</del><ins>+        # Get the set of all known groups in the DB
+        knownGroupUIDs = yield txn.allGroups()
+
+        # We'll want to remove groups no longer in use
+        groupsToRemove = knownGroupUIDs - groupUIDs
+
+        # Also look for groups which have been marked as missing for a while
+        if config.AutomaticPurging.Enabled:
</ins><span class="cx">             dateLimit = (
</span><span class="cx">                 datetime.datetime.utcnow() -
</span><span class="cx">                 datetime.timedelta(seconds=float(config.AutomaticPurging.GroupPurgeIntervalSeconds))
</span><span class="cx">             )
</span><ins>+            missingGroups = yield txn.groupsMissingSince(dateLimit)
+            groupsToRemove |= missingGroups
+
+        # Delete the groups in batches
+        groupsToRemove = list(groupsToRemove)
+        batchSize = 100
+        deletedGroupUIDs = []
+        while groupsToRemove:
+            batch = groupsToRemove[:batchSize]
+            del groupsToRemove[:batchSize]
+
</ins><span class="cx">             rows = yield GroupsRecord.deletesome(
</span><span class="cx">                 txn,
</span><del>-                (
-                    (GroupsRecord.extant == 0).And(GroupsRecord.modified &lt; dateLimit)
-                ).Or(
-                    GroupsRecord.groupUID.NotIn(groupUIDs)
-                ),
</del><ins>+                GroupsRecord.groupUID.In(batch),
</ins><span class="cx">                 returnCols=GroupsRecord.groupUID,
</span><span class="cx">             )
</span><del>-        else:
-            # remove unused groups
-            rows = yield GroupsRecord.deletesome(
-                txn,
-                GroupsRecord.groupUID.NotIn(groupUIDs) if groupUIDs else None,
-                returnCols=GroupsRecord.groupUID,
</del><ins>+            deletedGroupUIDs.extend([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">             )
</span><del>-        deletedGroupUIDs = [row[0] for row in rows]
-        if deletedGroupUIDs:
-            self.log.debug(&quot;Deleted old or unused groups {d}&quot;, d=deletedGroupUIDs)
</del><span class="cx"> 
</span><span class="cx">         # For each of those groups, create a per-group refresh work item
</span><span class="cx">         for groupUID in set(groupUIDs) - set(deletedGroupUIDs):
</span><span class="lines">@@ -390,10 +399,10 @@
</span><span class="cx">     @inlineCallbacks
</span><span class="cx">     def refreshGroup(self, txn, groupUID):
</span><span class="cx">         &quot;&quot;&quot;
</span><del>-            Does the work of a per-group refresh work item
-            Faults in the flattened membership of a group, as UIDs
-            and updates the GROUP_MEMBERSHIP table
-            WorkProposal is returned for tests
</del><ins>+        Does the work of a per-group refresh work item
+        Faults in the flattened membership of a group, as UIDs
+        and updates the GROUP_MEMBERSHIP table
+        WorkProposal is returned for tests
</ins><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         self.log.debug(&quot;Refreshing group: {g}&quot;, g=groupUID)
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>