<!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>[15715] 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/15715">15715</a></dd>
<dt>Author</dt> <dd>cdaboo@apple.com</dd>
<dt>Date</dt> <dd>2016-06-28 10:04:59 -0700 (Tue, 28 Jun 2016)</dd>
</dl>
<h3>Log Message</h3>
<pre>When splitting events, make sure we uncache attendee homes as we go around the attendee loop to ensure we don't use too much memory.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServertrunktxdavcaldavdatastoresqlpy">CalendarServer/trunk/txdav/caldav/datastore/sql.py</a></li>
<li><a href="#CalendarServertrunktxdavcaldavdatastoretesttest_sqlpy">CalendarServer/trunk/txdav/caldav/datastore/test/test_sql.py</a></li>
<li><a href="#CalendarServertrunktxdavcommondatastoresqlpy">CalendarServer/trunk/txdav/common/datastore/sql.py</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServertrunktxdavcaldavdatastoresqlpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/caldav/datastore/sql.py (15714 => 15715)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/caldav/datastore/sql.py        2016-06-28 16:16:43 UTC (rev 15714)
+++ CalendarServer/trunk/txdav/caldav/datastore/sql.py        2016-06-28 17:04:59 UTC (rev 15715)
</span><span class="lines">@@ -339,11 +339,47 @@
</span><span class="cx">
</span><span class="cx">
</span><span class="cx"> @inlineCallbacks
</span><ins>+ def calendarObjectDetailsWithUID(self, txn, uid):
+ """
+ Return details about all child object resources with the specified UID.
+ Only "owned" resources are returned, no shared resources.
+
+ @param txn: transaction to use
+ @type txn: L{CommonStoreTransaction}
+ @param uid: UID to query
+ @type uid: C{str}
+
+ @return: list of C{list} of home id, calendar id, and calendar object id
+ @rtype: C{list} of C{list}
+ """
+
+ # First find the resource-ids of the (home, parent, object) for each object matching the UID.
+ obj = CalendarHome._objectSchema
+ bind = CalendarHome._bindSchema
+ rows = (yield Select(
+ [bind.HOME_RESOURCE_ID, obj.PARENT_RESOURCE_ID, obj.RESOURCE_ID],
+ From=obj.join(bind, obj.PARENT_RESOURCE_ID == bind.RESOURCE_ID),
+ Where=(obj.UID == Parameter("uid")).And(bind.BIND_MODE == _BIND_MODE_OWN),
+ ).on(txn, uid=uid))
+
+ returnValue(rows)
+
+
+ @inlineCallbacks
</ins><span class="cx"> def calendarObjectsWithUID(self, txn, uid):
</span><span class="cx"> """
</span><span class="cx"> Return all child object resources with the specified UID. Only "owned" resources are returned,
</span><span class="cx"> no shared resources.
</span><span class="cx">
</span><ins>+ WARNING: this method loads all the home, calendar, and calendar object
+ resources for the specified UID. This is potentially a memory hog for
+ the case of an event with a very large number of attendees. Please avoid
+ using this. Instead use L{calendarObjectDetailsWithUID}, which just
+ looks up the IDs, which can then be loaded one at a time when needed.
+ When doing that, make sure that all references to previously processed
+ objects are removed so that they don't hang around in memory and cause
+ problems.
+
</ins><span class="cx"> @param txn: transaction to use
</span><span class="cx"> @type txn: L{CommonStoreTransaction}
</span><span class="cx"> @param uid: UID to query
</span><span class="lines">@@ -5350,7 +5386,7 @@
</span><span class="cx"> if onlyThis:
</span><span class="cx"> resources = ()
</span><span class="cx"> else:
</span><del>- resources = (yield CalendarStoreFeatures(self._txn._store).calendarObjectsWithUID(self._txn, self._uid))
</del><ins>+ resources = (yield CalendarStoreFeatures(self._txn._store).calendarObjectDetailsWithUID(self._txn, self._uid))
</ins><span class="cx">
</span><span class="cx"> if splitter is None:
</span><span class="cx"> splitter = iCalSplitter(config.Scheduling.Options.Splitting.Size, config.Scheduling.Options.Splitting.PastDays)
</span><span class="lines">@@ -5395,15 +5431,40 @@
</span><span class="cx"> )
</span><span class="cx">
</span><span class="cx"> # Split each one - but not this resource
</span><del>- for resource in resources:
- if resource is None or resource._resourceID == self._resourceID:
</del><ins>+ lastHome = None
+ for homeID, parentID, resourceID in sorted(resources):
+
+ # Each time the home changes, wipe it from the txn cache so that
+ # we do not accumulate them in memory whilst we go through this loop
+ if lastHome is not None:
+ if lastHome.id() != homeID:
+ self._txn.cleanHomeCache(lastHome)
+
+ if resourceID == self._resourceID:
</ins><span class="cx"> continue
</span><ins>+ lastHome = home = (yield self._txn.calendarHomeWithResourceID(homeID))
+ if home is None:
+ continue
+ parent = (yield home.childWithID(parentID))
+ if parent is None:
+ continue
+ resource = (yield parent.objectResourceWithID(resourceID))
+ if resource is None:
+ continue
</ins><span class="cx"> yield resource.splitForAttendee(
</span><span class="cx"> rid,
</span><span class="cx"> olderUID,
</span><span class="cx"> coercePartstatsInExistingResource=coercePartstatsInExistingResource
</span><span class="cx"> )
</span><ins>+ resource = None
+ parent = None
+ home = None
</ins><span class="cx">
</span><ins>+ # Clean up the last one
+ if lastHome is not None:
+ self._txn.cleanHomeCache(lastHome)
+ lastHome = None
+
</ins><span class="cx"> returnValue(olderObject)
</span><span class="cx">
</span><span class="cx">
</span></span></pre></div>
<a id="CalendarServertrunktxdavcaldavdatastoretesttest_sqlpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/caldav/datastore/test/test_sql.py (15714 => 15715)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/caldav/datastore/test/test_sql.py        2016-06-28 16:16:43 UTC (rev 15714)
+++ CalendarServer/trunk/txdav/caldav/datastore/test/test_sql.py        2016-06-28 17:04:59 UTC (rev 15715)
</span><span class="lines">@@ -52,11 +52,12 @@
</span><span class="cx"> from txdav.caldav.datastore.scheduling.itip import iTIPRequestStatus
</span><span class="cx"> from txdav.caldav.datastore.scheduling.processing import ImplicitProcessor
</span><span class="cx"> from txdav.caldav.datastore.scheduling.scheduler import ScheduleResponseQueue
</span><del>-from txdav.caldav.datastore.sql import CalendarStoreFeatures, CalendarObject
</del><ins>+from txdav.caldav.datastore.sql import CalendarStoreFeatures, CalendarObject, \
+ CalendarHome
</ins><span class="cx"> from txdav.common.datastore.sql import ECALENDARTYPE, CommonObjectResource, \
</span><span class="cx"> CommonStoreTransactionMonitor
</span><span class="cx"> from txdav.common.datastore.sql_tables import schema, _BIND_MODE_DIRECT, \
</span><del>- _BIND_STATUS_ACCEPTED, _TRANSP_OPAQUE, _BIND_MODE_WRITE
</del><ins>+ _BIND_STATUS_ACCEPTED, _TRANSP_OPAQUE, _BIND_MODE_WRITE, _HOME_STATUS_NORMAL
</ins><span class="cx"> from txdav.caldav.datastore.test.common import CommonTests as CalendarCommonTests, \
</span><span class="cx"> test_event_text, cal1Root, OTHER_HOME_UID
</span><span class="cx"> from txdav.caldav.datastore.test.test_file import setUpCalendarStore
</span><span class="lines">@@ -78,6 +79,7 @@
</span><span class="cx"> from twext.enterprise.util import parseSQLTimestamp
</span><span class="cx">
</span><span class="cx"> import datetime
</span><ins>+import gc
</ins><span class="cx"> import os
</span><span class="cx">
</span><span class="cx">
</span><span class="lines">@@ -3544,6 +3546,7 @@
</span><span class="cx"> for ctr in range(1, 5):
</span><span class="cx"> home_uid = yield txn.homeWithUID(ECALENDARTYPE, "user%02d" % (ctr,), create=True)
</span><span class="cx"> self.assertNotEqual(home_uid, None)
</span><ins>+ self.transactionUnderTest().resetHomeCache()
</ins><span class="cx"> yield self.commit()
</span><span class="cx">
</span><span class="cx"> self.setupDateTimeValues()
</span><span class="lines">@@ -7638,7 +7641,7 @@
</span><span class="cx"> yield self.commit()
</span><span class="cx">
</span><span class="cx"> # Patch resource lookup code so that it deletes the inbox resource after lookup is done
</span><del>- oldLookup = CalendarStoreFeatures.calendarObjectsWithUID
</del><ins>+ oldLookup = CalendarStoreFeatures.calendarObjectDetailsWithUID
</ins><span class="cx"> @inlineCallbacks
</span><span class="cx"> def _lookup(csself, txn, uid):
</span><span class="cx"> results = yield oldLookup(csself, txn, uid)
</span><span class="lines">@@ -7650,19 +7653,19 @@
</span><span class="cx"> yield cobjs[0].remove()
</span><span class="cx"> yield newtxn.commit()
</span><span class="cx">
</span><del>- # Remove inbox item in another txn
</del><ins>+ # Remove inbox item in another txn by setting the resourceID to an invalid value
</ins><span class="cx"> newtxn = self.concurrentTransaction()
</span><span class="cx"> cal = yield self.calendarUnderTest(name="inbox", home="user03")
</span><span class="cx"> cobjs = yield cal.calendarObjects()
</span><span class="cx"> for ctr in range(len(results)):
</span><del>- if results[ctr]._resourceID == cobjs[0]._resourceID:
- results[ctr] = None
</del><ins>+ if results[ctr][2] == cobjs[0]._resourceID:
+ results[ctr][2] += 100000
</ins><span class="cx"> break
</span><span class="cx"> yield newtxn.commit()
</span><span class="cx">
</span><span class="cx"> returnValue(results)
</span><span class="cx">
</span><del>- self.patch(CalendarStoreFeatures, "calendarObjectsWithUID", _lookup)
</del><ins>+ self.patch(CalendarStoreFeatures, "calendarObjectDetailsWithUID", _lookup)
</ins><span class="cx">
</span><span class="cx"> cobj = yield self.calendarObjectUnderTest(name="data1.ics", calendar_name="calendar", home="user01")
</span><span class="cx"> yield cobj.split()
</span><span class="lines">@@ -7713,6 +7716,125 @@
</span><span class="cx">
</span><span class="cx">
</span><span class="cx"> @inlineCallbacks
</span><ins>+ def test_calendarObjectSplit_clean_cache(self):
+ """
+ Test that (manual) splitting of calendar objects works and does not use excessive memory
+ by caching store objects for each attendee.
+ """
+
+ self.patch(config.Scheduling.Options.Splitting, "Enabled", False)
+ self.patch(config.Scheduling.Options.Splitting, "Size", 100)
+ self.patch(config.Scheduling.Options.Splitting, "PastDays", 14)
+ self.patch(config, "MaxResourceSize", 10 * 1024 * 1024)
+
+ # Create one event that will split
+ calendar = yield self.calendarUnderTest(name="calendar", home="user01")
+
+ attendees = "\n".join(["ATTENDEE:mailto:user{:02d}@example.com".format(i + 2) for i in range(3)])
+ data = """BEGIN:VCALENDAR
+VERSION:2.0
+PRODID:-//CALENDARSERVER.ORG//NONSGML Version 1//EN
+BEGIN:VEVENT
+UID:12345-67890
+DTSTART:%(now_back30)s
+DURATION:PT2H
+ATTENDEE;PARTSTAT=ACCEPTED:mailto:user01@example.com
+{attendees}
+DTSTAMP:20051222T210507Z
+ORGANIZER:mailto:user01@example.com
+RRULE:FREQ=DAILY
+SUMMARY:1234567890123456789012345678901234567890
+ 1234567890123456789012345678901234567890
+ 1234567890123456789012345678901234567890
+ 1234567890123456789012345678901234567890
+DESCRIPTION:{description}
+END:VEVENT
+BEGIN:VEVENT
+UID:12345-67890
+RECURRENCE-ID:%(now_back25)s
+DTSTART:%(now_back25)s
+DURATION:PT1H
+ATTENDEE;PARTSTAT=ACCEPTED:mailto:user01@example.com
+{attendees}
+DTSTAMP:20051222T210507Z
+ORGANIZER:mailto:user01@example.com
+DESCRIPTION:{description}
+END:VEVENT
+BEGIN:VEVENT
+UID:12345-67890
+RECURRENCE-ID:%(now_back24)s
+DTSTART:%(now_back24)s
+DURATION:PT1H
+ATTENDEE;PARTSTAT=ACCEPTED:mailto:user01@example.com
+{attendees}
+DTSTAMP:20051222T210507Z
+ORGANIZER:mailto:user01@example.com
+DESCRIPTION:{description}
+END:VEVENT
+BEGIN:VEVENT
+UID:12345-67890
+RECURRENCE-ID:%(now_fwd10)s
+DTSTART:%(now_fwd10)s
+DURATION:PT1H
+{attendees}
+DTSTAMP:20051222T210507Z
+ORGANIZER:mailto:user01@example.com
+DESCRIPTION:{description}
+END:VEVENT
+END:VCALENDAR
+""".format(description=" " * 1024 * 1024, attendees=attendees)
+
+ component = Component.fromString(data % self.dtsubs)
+ cobj = yield calendar.createCalendarObjectWithName("data1.ics", component)
+ self.assertFalse(hasattr(cobj, "_workItems"))
+ self.transactionUnderTest().resetHomeCache()
+ yield self.commit()
+ cobj = None
+ component = None
+ calendar = None
+
+ w = schema.CALENDAR_OBJECT_SPLITTER_WORK
+ rows = yield Select(
+ [w.RESOURCE_ID, ],
+ From=w
+ ).on(self.transactionUnderTest())
+ self.assertEqual(len(rows), 0)
+ self.transactionUnderTest().resetHomeCache()
+ yield self.abort()
+
+ # Do manual split
+ cobj = yield self.calendarObjectUnderTest(name="data1.ics", calendar_name="calendar", home="user01")
+ will = yield cobj.willSplit()
+ self.assertTrue(will)
+
+ yield cobj.split()
+
+ # Make sure caches are clean
+ txn = self.transactionUnderTest()
+ self.assertEqual(1, len(txn._cachedHomes[ECALENDARTYPE]["byUID"][_HOME_STATUS_NORMAL]))
+ self.assertEqual(1, len(txn._cachedHomes[ECALENDARTYPE]["byID"][None]))
+
+ # Make sure local objects are gone
+ cobj = None
+ txn = None
+ yield self.commit()
+
+ # Garbage collect then look for the number of home/resource objects and
+ # make sure only those for user01 still exist
+ gc.collect()
+ gcobjs = gc.get_objects()
+ hcount = 0
+ ocount = 0
+ for obj in gcobjs:
+ if isinstance(obj, CalendarHome):
+ hcount += 1
+ elif isinstance(obj, CalendarObject):
+ ocount += 1
+ self.assertTrue(hcount <= 2) # Just user01 home left (maybe user05 too)
+ self.assertTrue(ocount <= 4) # Old resource + new resource (and maybe user05 too)
+
+
+ @inlineCallbacks
</ins><span class="cx"> def _setupSplitAt(self):
</span><span class="cx"> """
</span><span class="cx"> Test that user triggered splitting of calendar objects works.
</span></span></pre></div>
<a id="CalendarServertrunktxdavcommondatastoresqlpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/common/datastore/sql.py (15714 => 15715)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/common/datastore/sql.py        2016-06-28 16:16:43 UTC (rev 15714)
+++ CalendarServer/trunk/txdav/common/datastore/sql.py        2016-06-28 17:04:59 UTC (rev 15715)
</span><span class="lines">@@ -754,11 +754,19 @@
</span><span class="cx"> if storeType not in (ECALENDARTYPE, EADDRESSBOOKTYPE):
</span><span class="cx"> raise RuntimeError("Unknown home type.")
</span><span class="cx">
</span><del>- result = self._determineMemo(storeType, "byUID", status).get(uid)
</del><ins>+ if status is None:
+ # Look for any that match possible status and return the first one.
+ # The order of status here matches that in L{CommonHome.homeWith}.
+ for possible_status in (_HOME_STATUS_NORMAL, _HOME_STATUS_DISABLED, _HOME_STATUS_EXTERNAL,):
+ result = self._determineMemo(storeType, "byUID", possible_status).get(uid)
+ if result is not None:
+ break
+ else:
+ result = self._determineMemo(storeType, "byUID", status).get(uid)
</ins><span class="cx"> if result is None:
</span><span class="cx"> result = yield self._homeClass[storeType].homeWithUID(self, uid, status, create, authzUID)
</span><span class="cx"> if result:
</span><del>- self._determineMemo(storeType, "byUID", status)[uid] = result
</del><ins>+ self._determineMemo(storeType, "byUID", result.status())[uid] = result
</ins><span class="cx"> self._determineMemo(storeType, "byID", None)[result.id()] = result
</span><span class="cx"> returnValue(result)
</span><span class="cx">
</span><span class="lines">@@ -796,6 +804,42 @@
</span><span class="cx"> return self.homeWithResourceID(EADDRESSBOOKTYPE, rid)
</span><span class="cx">
</span><span class="cx">
</span><ins>+ def resetHomeCache(self):
+ """
+ Initialize the home caches, wiping any that existed before. This is only
+ used in testing when we want to be sure all cached objects have been
+ removed.
+ """
+ self._cachedHomes = {
+ ECALENDARTYPE: {
+ "byUID": defaultdict(dict),
+ "byID": defaultdict(dict),
+ },
+ EADDRESSBOOKTYPE: {
+ "byUID": defaultdict(dict),
+ "byID": defaultdict(dict),
+ },
+ }
+ self._notificationHomes = {
+ "byUID": defaultdict(dict),
+ "byID": defaultdict(dict),
+ }
+
+
+ def cleanHomeCache(self, home):
+ """
+ Remove the specified home from the home cache.
+ @param home: home to remove
+ @type home: L{CommonHome}
+ """
+ rid_memo = self._determineMemo(home._homeType, "byID", None)
+ if home.id() in rid_memo:
+ del rid_memo[home.id()]
+ uid_memo = self._determineMemo(home._homeType, "byUID", home.status())
+ if home.uid() in uid_memo:
+ del uid_memo[home.uid()]
+
+
</ins><span class="cx"> @inlineCallbacks
</span><span class="cx"> def notificationsWithUID(self, uid, status=None, create=False):
</span><span class="cx"> """
</span></span></pre>
</div>
</div>
</body>
</html>