<!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>[11933] CalendarServer/branches/users/sagen/groupcacher</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/11933">11933</a></dd>
<dt>Author</dt> <dd>gaya@apple.com</dd>
<dt>Date</dt> <dd>2013-11-11 16:22:37 -0800 (Mon, 11 Nov 2013)</dd>
</dl>
<h3>Log Message</h3>
<pre>implement GroupAttendeeReconciliationWork, GroupCacher.scheduleEventReconciliations(); add DirectoryRecord.attendee()</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServerbranchesuserssagengroupcachertwextwhogroupspy">CalendarServer/branches/users/sagen/groupcacher/twext/who/groups.py</a></li>
<li><a href="#CalendarServerbranchesuserssagengroupcachertwistedcaldavdirectorydirectorypy">CalendarServer/branches/users/sagen/groupcacher/twistedcaldav/directory/directory.py</a></li>
<li><a href="#CalendarServerbranchesuserssagengroupcachertxdavcaldavicalendardirectoryservicepy">CalendarServer/branches/users/sagen/groupcacher/txdav/caldav/icalendardirectoryservice.py</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServerbranchesuserssagengroupcachertwextwhogroupspy"></a>
<div class="modfile"><h4>Modified: CalendarServer/branches/users/sagen/groupcacher/twext/who/groups.py (11932 => 11933)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/branches/users/sagen/groupcacher/twext/who/groups.py        2013-11-10 03:40:56 UTC (rev 11932)
+++ CalendarServer/branches/users/sagen/groupcacher/twext/who/groups.py        2013-11-12 00:22:37 UTC (rev 11933)
</span><span class="lines">@@ -19,15 +19,16 @@
</span><span class="cx"> Group membership caching
</span><span class="cx"> """
</span><span class="cx">
</span><del>-import datetime
-import hashlib
</del><span class="cx"> from twext.enterprise.dal.record import fromTable
</span><ins>+from twext.enterprise.dal.syntax import Delete, Select
</ins><span class="cx"> from twext.enterprise.queue import WorkItem, PeerConnectionPool
</span><ins>+from twext.who.delegates import allGroupDelegates
+from twext.who.idirectory import RecordType
+from twisted.internet.defer import inlineCallbacks, returnValue
+from twistedcaldav.ical import ignoredComponents
</ins><span class="cx"> from txdav.common.datastore.sql_tables import schema
</span><del>-from twisted.internet.defer import inlineCallbacks, returnValue, succeed
-from twext.enterprise.dal.syntax import Delete
-from twext.who.idirectory import RecordType
-from twext.who.delegates import allGroupDelegates
</del><ins>+import datetime
+import hashlib
</ins><span class="cx">
</span><span class="cx"> from twext.python.log import Logger
</span><span class="cx"> log = Logger()
</span><span class="lines">@@ -37,7 +38,7 @@
</span><span class="cx"> fromTable(schema.GROUP_CACHER_POLLING_WORK)):
</span><span class="cx">
</span><span class="cx"> group = "group_cacher_polling"
</span><del>-
</del><ins>+
</ins><span class="cx"> @inlineCallbacks
</span><span class="cx"> def doWork(self):
</span><span class="cx">
</span><span class="lines">@@ -67,6 +68,7 @@
</span><span class="cx"> notBefore=notBefore)
</span><span class="cx">
</span><span class="cx">
</span><ins>+
</ins><span class="cx"> @inlineCallbacks
</span><span class="cx"> def scheduleNextGroupCachingUpdate(store, seconds):
</span><span class="cx"> txn = store.newTransaction()
</span><span class="lines">@@ -77,6 +79,7 @@
</span><span class="cx"> returnValue(wp)
</span><span class="cx">
</span><span class="cx">
</span><ins>+
</ins><span class="cx"> def schedulePolledGroupCachingUpdate(store):
</span><span class="cx"> """
</span><span class="cx"> Schedules a group caching update work item in "the past" so PeerConnectionPool's
</span><span class="lines">@@ -86,6 +89,7 @@
</span><span class="cx"> return scheduleNextGroupCachingUpdate(store, seconds)
</span><span class="cx">
</span><span class="cx">
</span><ins>+
</ins><span class="cx"> class GroupRefreshWork(WorkItem, fromTable(schema.GROUP_REFRESH_WORK)):
</span><span class="cx">
</span><span class="cx"> group = property(lambda self: self.groupGUID)
</span><span class="lines">@@ -114,10 +118,87 @@
</span><span class="cx"> groupGUID=self.groupGUID, notBefore=notBefore)
</span><span class="cx">
</span><span class="cx">
</span><ins>+
</ins><span class="cx"> class GroupAttendeeReconciliationWork(WorkItem, fromTable(schema.GROUP_ATTENDEE_RECONCILIATION_WORK)):
</span><del>- pass
</del><span class="cx">
</span><ins>+ group = property(lambda self: "%s, %s" % (self.groupID, self.eventID))
</ins><span class="cx">
</span><ins>+ @inlineCallbacks
+ def doWork(self):
+
+ # Delete all other work items for this group
+ yield Delete(From=self.table,
+ Where=((self.table.GROUP_ID == self.self.groupID).And(
+ self.table.RESOURCE_ID == self.self.eventID)
+ )
+ ).on(self.transaction)
+
+ # get group individual UIDs
+ groupMemember = schema.GROUP_MEMBERSHIP
+ rows = yield Select(
+ [groupMemember.MEMBER_GUID, ],
+ From=groupMemember,
+ Where=groupMemember.GROUP_ID == self.groupID,
+ ).on(self.transaction)
+ individualGUIDs = [row[0] for row in rows]
+
+ # get calendar Object
+ calObject = schema.CALENDAR_OBJECT
+ rows = yield Select(
+ [calObject.CALENDAR_RESOURCE_ID, ],
+ From=calObject,
+ Where=calObject.RESOURCE_ID == self.eventID,
+ ).on(self.transaction)
+
+ calendarID = row[0][0]
+ calendarHome = (yield self.Calendar._ownerHomeWithResourceID.on(
+ self.transaction, resourceID=calendarID)
+ )[0][0]
+
+ calendar = yield calendarHome.childWithID(calendarID)
+ calendarObject = yield calendar.objectResourceWithID(self.eventID)
+ changed = False
+
+ individualUUIDs = set(["urn:uuid:" + individualGUID for individualGUID in individualGUIDs])
+ groupUUID = "urn:uuid:" + self.groupGUID()
+ vcalendar = yield calendarObject.component()
+ for component in vcalendar.subcomponents():
+ if component.name() in ignoredComponents:
+ continue
+
+ oldAttendeeProps = component.getAttendees()
+ oldAttendeeUUIDs = set([attendeeProp.value() for attendeeProp in oldAttendeeProps])
+
+ # add new member attendees
+ for individualUUID in individualUUIDs - oldAttendeeUUIDs:
+ individualGUID = individualUUID[len("urn:uuid:"):]
+ directoryRecord = self.transaction.directoryService().recordWithUID(individualGUID)
+ newAttendeeProp = directoryRecord.attendee(params={"MEMBER": groupUUID})
+ component.addProperty(newAttendeeProp)
+ changed = True
+
+ # remove attendee or update MEMBER attribute for non-primary attendees in this group,
+ for attendeeProp in oldAttendeeProps:
+ memberParam = attendeeProp.getParameter("MEMBER")
+ if memberParam:
+ if groupUUID in memberParam.getValues():
+ if attendeeProp.value() not in individualUUIDs:
+ valueCount = memberParam.removeValue(groupUUID)
+ if valueCount == 0:
+ component.removeProperty(attendeeProp)
+ changed = True
+ else:
+ if attendeeProp.value() in individualUUIDs:
+ memberParam.addValue(groupUUID)
+ changed = True
+
+ # replace old with new
+ if changed:
+ # TODO: call calendarObject._setComponentInternal( vcalendar, mode ) instead?
+ yield calendarObject.setComponent(vcalendar)
+
+
+
</ins><span class="cx"> @inlineCallbacks
</span><span class="cx"> def _expandedMembers(record, members=None, records=None):
</span><span class="cx"> """
</span><span class="lines">@@ -140,6 +221,7 @@
</span><span class="cx"> returnValue(members)
</span><span class="cx">
</span><span class="cx">
</span><ins>+
</ins><span class="cx"> class GroupCacher(object):
</span><span class="cx"> log = Logger()
</span><span class="cx">
</span><span class="lines">@@ -179,11 +261,11 @@
</span><span class="cx"> membershipHashContent = hashlib.md5()
</span><span class="cx"> members = (yield _expandedMembers(record))
</span><span class="cx"> members = list(members)
</span><del>- members.sort(cmp=lambda x,y: cmp(x.guid, y.guid))
</del><ins>+ members.sort(cmp=lambda x, y: cmp(x.guid, y.guid))
</ins><span class="cx"> for member in members:
</span><span class="cx"> membershipHashContent.update(member.guid)
</span><span class="cx"> membershipHash = membershipHashContent.hexdigest()
</span><del>- groupID, cachedName, cachedMembershipHash = (yield
</del><ins>+ groupID, cachedName, cachedMembershipHash = (yield #@UnusedVariable
</ins><span class="cx"> txn.groupByGUID(groupGUID))
</span><span class="cx">
</span><span class="cx"> if cachedMembershipHash != membershipHash:
</span><span class="lines">@@ -200,7 +282,7 @@
</span><span class="cx"> newMemberGUIDs.add(member.guid)
</span><span class="cx"> yield self.synchronizeMembers(txn, groupID, newMemberGUIDs)
</span><span class="cx">
</span><del>- yield self.scheduleEventReconciliations(txn, groupID)
</del><ins>+ yield self.scheduleEventReconciliations(txn, groupID, groupGUID)
</ins><span class="cx">
</span><span class="cx">
</span><span class="cx"> @inlineCallbacks
</span><span class="lines">@@ -232,18 +314,38 @@
</span><span class="cx"> returnValue(members)
</span><span class="cx">
</span><span class="cx">
</span><del>-
-
</del><span class="cx"> # @inlineCallbacks
</span><del>- def scheduleEventReconciliations(self, txn, groupID):
</del><ins>+ def scheduleEventReconciliations(self, txn, groupID, groupGUID):
</ins><span class="cx"> """
</span><span class="cx"> Find all events who have this groupID as an attendee and create
</span><span class="cx"> work items for them.
</span><span class="cx"> """
</span><del>- return succeed(None)
</del><ins>+ groupAttendee = schema.GROUP_ATTENDEE
+ rows = yield Select(
+ [groupAttendee.RESOURCE_ID, ],
+ From=groupAttendee,
+ Where=groupAttendee.GROUP_ID == groupID,
+ ).on(txn)
+ eventIDs = [row[0] for row in rows]
</ins><span class="cx">
</span><ins>+ for eventID in eventIDs:
</ins><span class="cx">
</span><ins>+ notBefore = (datetime.datetime.utcnow() +
+ datetime.timedelta(seconds=10))
+ log.debug("scheduling group reconciliation for ({eventID}, {groupID}, {groupGUID}): {when}",
+ eventID=eventID,
+ groupID=groupID,
+ groupGUID=groupGUID,
+ when=notBefore)
</ins><span class="cx">
</span><ins>+ yield txn.enqueue(GroupAttendeeReconciliationWork,
+ eventID=eventID,
+ groupID=groupID,
+ groupGUID=groupGUID,
+ notBefore=notBefore
+ )
+
+
</ins><span class="cx"> @inlineCallbacks
</span><span class="cx"> def groupsToRefresh(self, txn):
</span><span class="cx"> delegatedGUIDs = set((yield allGroupDelegates(txn)))
</span></span></pre></div>
<a id="CalendarServerbranchesuserssagengroupcachertwistedcaldavdirectorydirectorypy"></a>
<div class="modfile"><h4>Modified: CalendarServer/branches/users/sagen/groupcacher/twistedcaldav/directory/directory.py (11932 => 11933)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/branches/users/sagen/groupcacher/twistedcaldav/directory/directory.py        2013-11-10 03:40:56 UTC (rev 11932)
+++ CalendarServer/branches/users/sagen/groupcacher/twistedcaldav/directory/directory.py        2013-11-12 00:22:37 UTC (rev 11933)
</span><span class="lines">@@ -43,6 +43,7 @@
</span><span class="cx"> from twistedcaldav.config import config
</span><span class="cx"> from twistedcaldav.directory.idirectory import IDirectoryService, IDirectoryRecord
</span><span class="cx"> from twistedcaldav.directory.util import uuidFromName, normalizeUUID
</span><ins>+from twistedcaldav.ical import Property
</ins><span class="cx"> from twistedcaldav.memcacher import Memcacher
</span><span class="cx"> from txdav.caldav.datastore.scheduling.cuaddress import normalizeCUAddr
</span><span class="cx"> from txdav.caldav.datastore.scheduling.ischedule.localservers import Servers
</span><span class="lines">@@ -85,8 +86,8 @@
</span><span class="cx">
</span><span class="cx"> searchContext_location = "location"
</span><span class="cx"> searchContext_resource = "resource"
</span><del>- searchContext_user = "user"
- searchContext_group = "group"
</del><ins>+ searchContext_user = "user"
+ searchContext_group = "group"
</ins><span class="cx"> searchContext_attendee = "attendee"
</span><span class="cx">
</span><span class="cx"> aggregateService = None
</span><span class="lines">@@ -240,7 +241,7 @@
</span><span class="cx"> return record if record and record.enabledForCalendaring else None
</span><span class="cx">
</span><span class="cx">
</span><del>- def recordWithCachedGroupsAlias(self, recordType, alias):
</del><ins>+ def recordWithCachedGroupsAlias(self, recordType, alias): #@UnusedVariable
</ins><span class="cx"> """
</span><span class="cx"> @param recordType: the type of the record to look up.
</span><span class="cx"> @param alias: the cached-groups alias of the record to look up.
</span><span class="lines">@@ -441,7 +442,7 @@
</span><span class="cx"> return succeed(yieldMatches(recordType))
</span><span class="cx">
</span><span class="cx">
</span><del>- def getGroups(self, guids):
</del><ins>+ def getGroups(self, guids): #@UnusedVariable
</ins><span class="cx"> """
</span><span class="cx"> This implementation returns all groups, not just the ones specified
</span><span class="cx"> by guids
</span><span class="lines">@@ -624,6 +625,7 @@
</span><span class="cx"> self.expireSeconds = expireSeconds
</span><span class="cx"> self.lockSeconds = lockSeconds
</span><span class="cx">
</span><ins>+
</ins><span class="cx"> def setGroupsFor(self, guid, memberships):
</span><span class="cx"> self.log.debug("set groups-for %s : %s" % (guid, memberships))
</span><span class="cx"> return self.set("groups-for:%s" %
</span><span class="lines">@@ -671,7 +673,6 @@
</span><span class="cx"> return self.add("group-cacher-lock", "1", expireTime=self.lockSeconds)
</span><span class="cx">
</span><span class="cx">
</span><del>-
</del><span class="cx"> def extendLock(self):
</span><span class="cx"> """
</span><span class="cx"> Update the expiration time of the memcached lock
</span><span class="lines">@@ -690,6 +691,7 @@
</span><span class="cx"> return self.delete("group-cacher-lock")
</span><span class="cx">
</span><span class="cx">
</span><ins>+
</ins><span class="cx"> class GroupMembershipCacheUpdater(object):
</span><span class="cx"> """
</span><span class="cx"> Responsible for updating memcached with group memberships. This will run
</span><span class="lines">@@ -1034,6 +1036,7 @@
</span><span class="cx"> returnValue((fast, len(members), len(changedMembers)))
</span><span class="cx">
</span><span class="cx">
</span><ins>+
</ins><span class="cx"> def diffAssignments(old, new):
</span><span class="cx"> """
</span><span class="cx"> Compare two proxy assignment lists and return their differences in the form of
</span><span class="lines">@@ -1087,7 +1090,7 @@
</span><span class="cx"> self, service, recordType, guid=None,
</span><span class="cx"> shortNames=(), authIDs=set(), fullName=None,
</span><span class="cx"> firstName=None, lastName=None, emailAddresses=set(),
</span><del>- calendarUserAddresses=set(),
</del><ins>+ calendarUserAddresses=set(), #@UnusedVariable
</ins><span class="cx"> autoSchedule=False, autoScheduleMode=None,
</span><span class="cx"> autoAcceptGroup="",
</span><span class="cx"> enabledForCalendaring=None,
</span><span class="lines">@@ -1325,7 +1328,7 @@
</span><span class="cx"> return set()
</span><span class="cx">
</span><span class="cx">
</span><del>- def verifyCredentials(self, credentials):
</del><ins>+ def verifyCredentials(self, credentials): #@UnusedVariable
</ins><span class="cx"> return False
</span><span class="cx">
</span><span class="cx">
</span><span class="lines">@@ -1513,7 +1516,35 @@
</span><span class="cx"> return self.service.isProxyFor(self, other)
</span><span class="cx">
</span><span class="cx">
</span><ins>+ def attendee(self, params=None):
+ """
+ Returns a pycalendar ATTENDEE property for this record.
</ins><span class="cx">
</span><ins>+ @param groupUIDs: group uids for the MEMBER parameter of returned property
+ @type organizer: C{List}
+
+ @return: the attendee property
+ @rtype: C{Property}
+ """
+ if params is None:
+ params = {}
+ if "PARTSTAT" not in params:
+ params["PARTSTAT"] = "NEEDS-ACTION"
+ if "CN"not in params:
+ if self.fullName():
+ params["CN"] = self.fullName()
+ if "EMAIL" not in params:
+ if self.emailAddresses():
+ params["EMAIL"] = self.emailAddresses()[0]
+ if "CUTYPE" not in params:
+ cuType = self.getCUType()
+ if cuType is not "INDIVIDUAL":
+ params["CUTYPE"] = cuType
+
+ return Property("ATTENDEE", "urn:uuid:" + self.uid(), params=params)
+
+
+
</ins><span class="cx"> class DirectoryError(RuntimeError):
</span><span class="cx"> """
</span><span class="cx"> Generic directory error.
</span></span></pre></div>
<a id="CalendarServerbranchesuserssagengroupcachertxdavcaldavicalendardirectoryservicepy"></a>
<div class="modfile"><h4>Modified: CalendarServer/branches/users/sagen/groupcacher/txdav/caldav/icalendardirectoryservice.py (11932 => 11933)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/branches/users/sagen/groupcacher/txdav/caldav/icalendardirectoryservice.py        2013-11-10 03:40:56 UTC (rev 11932)
+++ CalendarServer/branches/users/sagen/groupcacher/txdav/caldav/icalendardirectoryservice.py        2013-11-12 00:22:37 UTC (rev 11933)
</span><span class="lines">@@ -133,3 +133,14 @@
</span><span class="cx"> @return: C{True} if it is a proxy.
</span><span class="cx"> @rtype: C{bool}
</span><span class="cx"> """
</span><ins>+
+ def attendee(groupUIDs=None): #@NoSelf
+ """
+ Returns a pycalendar ATTENDEE property for this record.
+
+ @param groupUIDs: group uids for the MEMBER parameter of returned property
+ @type organizer: C{List}
+
+ @return: the attendee property
+ @rtype: C{Property}
+ """
</ins></span></pre>
</div>
</div>
</body>
</html>