[CalendarServer-changes] [11817] CalendarServer/trunk

source_changes at macosforge.org source_changes at macosforge.org
Wed Mar 12 11:19:24 PDT 2014


Revision: 11817
          http://trac.calendarserver.org//changeset/11817
Author:   sagen at apple.com
Date:     2013-10-15 16:24:11 -0700 (Tue, 15 Oct 2013)
Log Message:
-----------
Don't create new transactions for adding push work items -- create them within the transaction whose changes caused the notification in the first place.

Modified Paths:
--------------
    CalendarServer/trunk/calendarserver/push/notifier.py
    CalendarServer/trunk/calendarserver/push/test/test_notifier.py
    CalendarServer/trunk/txdav/caldav/datastore/test/common.py
    CalendarServer/trunk/txdav/carddav/datastore/test/common.py
    CalendarServer/trunk/txdav/common/datastore/file.py
    CalendarServer/trunk/txdav/common/datastore/sql.py
    CalendarServer/trunk/txdav/common/datastore/test/util.py

Modified: CalendarServer/trunk/calendarserver/push/notifier.py
===================================================================
--- CalendarServer/trunk/calendarserver/push/notifier.py	2013-10-15 17:00:57 UTC (rev 11816)
+++ CalendarServer/trunk/calendarserver/push/notifier.py	2013-10-15 23:24:11 UTC (rev 11817)
@@ -84,10 +84,13 @@
 
 
     @inlineCallbacks
-    def notify(self):
+    def notify(self, txn):
         """
         Send the notification. For a home object we just push using the home id. For a home
         child we push both the owner home id and the owned home child id.
+
+        @param txn: The transaction to create the work item with
+        @type txn: L{CommonStoreTransaction}
         """
         # Push ids from the store objects are a tuple of (prefix, name,) and we need to compose that
         # into a single token.
@@ -100,7 +103,7 @@
         for prefix, id in ids:
             if self._notify:
                 self.log.debug("Notifications are enabled: %s %s/%s" % (self._storeObject, prefix, id,))
-                yield self._notifierFactory.send(prefix, id)
+                yield self._notifierFactory.send(prefix, id, txn)
             else:
                 self.log.debug("Skipping notification for: %s %s/%s" % (self._storeObject, prefix, id,))
 
@@ -147,11 +150,12 @@
 
 
     @inlineCallbacks
-    def send(self, prefix, id):
-        txn = self.store.newTransaction()
+    def send(self, prefix, id, txn):
+        """
+        Enqueue a push notification work item on the provided transaction.
+        """
         notBefore = datetime.datetime.utcnow() + datetime.timedelta(seconds=self.coalesceSeconds)
         yield txn.enqueue(PushNotificationWork, pushID=self.pushKeyForId(prefix, id), notBefore=notBefore)
-        yield txn.commit()
 
 
     def newNotifier(self, storeObject):

Modified: CalendarServer/trunk/calendarserver/push/test/test_notifier.py
===================================================================
--- CalendarServer/trunk/calendarserver/push/test/test_notifier.py	2013-10-15 17:00:57 UTC (rev 11816)
+++ CalendarServer/trunk/calendarserver/push/test/test_notifier.py	2013-10-15 23:24:11 UTC (rev 11817)
@@ -169,8 +169,8 @@
 
         home = yield self.homeUnderTest()
         yield home.notifyChanged()
+        self.assertEquals(self.notifierFactory.history, ["/CalDAV/example.com/home1/"])
         yield self.commit()
-        self.assertEquals(self.notifierFactory.history, ["/CalDAV/example.com/home1/"])
 
 
     @inlineCallbacks
@@ -178,11 +178,11 @@
 
         calendar = yield self.calendarUnderTest()
         yield calendar.notifyChanged()
-        yield self.commit()
         self.assertEquals(
             set(self.notifierFactory.history),
             set(["/CalDAV/example.com/home1/", "/CalDAV/example.com/home1/calendar_1/"])
         )
+        yield self.commit()
 
 
     @inlineCallbacks
@@ -191,7 +191,6 @@
         calendar = yield self.calendarUnderTest()
         home2 = yield self.homeUnderTest(name="home2")
         yield calendar.shareWith(home2, _BIND_MODE_WRITE)
-        yield self.commit()
         self.assertEquals(
             set(self.notifierFactory.history),
             set([
@@ -200,11 +199,11 @@
                 "/CalDAV/example.com/home2/"
             ])
         )
+        yield self.commit()
 
         calendar = yield self.calendarUnderTest()
         home2 = yield self.homeUnderTest(name="home2")
         yield calendar.unshareWith(home2)
-        yield self.commit()
         self.assertEquals(
             set(self.notifierFactory.history),
             set([
@@ -213,6 +212,7 @@
                 "/CalDAV/example.com/home2/"
             ])
         )
+        yield self.commit()
 
 
     @inlineCallbacks
@@ -226,11 +226,11 @@
 
         shared = yield self.calendarUnderTest(home="home2", name=shareName)
         yield shared.notifyChanged()
-        yield self.commit()
         self.assertEquals(
             set(self.notifierFactory.history),
             set(["/CalDAV/example.com/home1/", "/CalDAV/example.com/home1/calendar_1/"])
         )
+        yield self.commit()
 
 
     @inlineCallbacks
@@ -238,8 +238,8 @@
 
         notifications = yield self.transactionUnderTest().notificationsWithUID("home1")
         yield notifications.notifyChanged()
-        yield self.commit()
         self.assertEquals(
             set(self.notifierFactory.history),
             set(["/CalDAV/example.com/home1/", "/CalDAV/example.com/home1/notification/"])
         )
+        yield self.commit()

Modified: CalendarServer/trunk/txdav/caldav/datastore/test/common.py
===================================================================
--- CalendarServer/trunk/txdav/caldav/datastore/test/common.py	2013-10-15 17:00:57 UTC (rev 11816)
+++ CalendarServer/trunk/txdav/caldav/datastore/test/common.py	2013-10-15 23:24:11 UTC (rev 11817)
@@ -450,9 +450,7 @@
         yield notifications.writeNotificationObject("abc", inviteNotification,
             inviteNotification.toxml())
 
-        yield self.commit()
-
-        # Make sure notification fired after commit
+        # notify is called prior to commit
         self.assertEquals(
             set(self.notifierFactory.history),
             set([
@@ -460,6 +458,7 @@
                 "/CalDAV/example.com/home1/notification/",
             ])
         )
+        yield self.commit()
 
         notifications = yield self.transactionUnderTest().notificationsWithUID(
             "home1"
@@ -469,9 +468,7 @@
         abc = yield notifications.notificationObjectWithUID("abc")
         self.assertEquals(abc, None)
 
-        yield self.commit()
-
-        # Make sure notification fired after commit
+        # notify is called prior to commit
         self.assertEquals(
             set(self.notifierFactory.history),
             set([
@@ -479,6 +476,7 @@
                 "/CalDAV/example.com/home1/notification/",
             ])
         )
+        yield self.commit()
 
 
     @inlineCallbacks
@@ -697,11 +695,10 @@
         self.assertNotIdentical((yield home.calendarWithName(name)), None)
         calendarProperties = (yield home.calendarWithName(name)).properties()
         self.assertEqual(len(calendarProperties), 0)
+        # notify is called prior to commit
+        self.assertTrue("/CalDAV/example.com/home1/" in self.notifierFactory.history)
         yield self.commit()
 
-        # Make sure notification fired after commit
-        self.assertTrue("/CalDAV/example.com/home1/" in self.notifierFactory.history)
-
         # Make sure it's available in a new transaction; i.e. test the commit.
         home = yield self.homeUnderTest()
         self.assertNotIdentical((yield home.calendarWithName(name)), None)
@@ -915,8 +912,7 @@
                 None
             )
 
-        # Make sure notifications are fired after commit
-        yield self.commit()
+        # notify is called prior to commit
         self.assertEquals(
             set(self.notifierFactory.history),
             set([
@@ -924,6 +920,7 @@
                 "/CalDAV/example.com/home1/calendar_1/",
             ])
         )
+        yield self.commit()
 
 
     @inlineCallbacks
@@ -1471,9 +1468,7 @@
         self.assertEquals((yield calendarObject.componentForUser()), component)
         self.assertEquals((yield calendarObject.getMetadata()), metadata)
 
-        yield self.commit()
-
-        # Make sure notifications fire after commit
+        # notify is called prior to commit
         self.assertEquals(
             set(self.notifierFactory.history),
             set([
@@ -1481,6 +1476,7 @@
                 "/CalDAV/example.com/home1/calendar_1/",
             ])
         )
+        yield self.commit()
 
 
     @inlineCallbacks
@@ -1591,9 +1587,7 @@
         calendarObject = yield calendar1.calendarObjectWithName("1.ics")
         self.assertEquals((yield calendarObject.componentForUser()), component)
 
-        yield self.commit()
-
-        # Make sure notification fired after commit
+        # notify is called prior to commit
         self.assertEquals(
             set(self.notifierFactory.history),
             set([
@@ -1601,6 +1595,7 @@
                 "/CalDAV/example.com/home1/calendar_1/",
             ])
         )
+        yield self.commit()
 
 
     def checkPropertiesMethod(self, thunk):

Modified: CalendarServer/trunk/txdav/carddav/datastore/test/common.py
===================================================================
--- CalendarServer/trunk/txdav/carddav/datastore/test/common.py	2013-10-15 17:00:57 UTC (rev 11816)
+++ CalendarServer/trunk/txdav/carddav/datastore/test/common.py	2013-10-15 23:24:11 UTC (rev 11817)
@@ -371,10 +371,10 @@
         #self.assertIdentical((yield home.addressbookWithName(name)), None)
         yield home.removeAddressBookWithName(name)
         self.assertNotIdentical((yield home.addressbookWithName(name)), None)
+        # notify is called prior to commit
+        self.assertTrue("/CardDAV/example.com/home1/" in self.notifierFactory.history)
         yield self.commit()
 
-        # Make sure notification fired after commit
-        self.assertTrue("/CardDAV/example.com/home1/" in self.notifierFactory.history)
 
         # Make sure it's available in a new transaction; i.e. test the commit.
         home = yield self.homeUnderTest()
@@ -396,9 +396,7 @@
             ab = yield home.addressbookWithName(name)
             self.assertEquals((yield ab.listAddressBookObjects()), [])
 
-        yield self.commit()
-
-        # Make sure notification fired after commit
+        # notify is called prior to commit
         self.assertEquals(
             set(self.notifierFactory.history),
             set([
@@ -407,7 +405,10 @@
             ])
         )
 
+        yield self.commit()
 
+
+
     @inlineCallbacks
     def test_removeAddressBookWithName_absent(self):
         """
@@ -530,8 +531,6 @@
                 (yield addressbook.addressbookObjectWithName(name)), None
             )
 
-        # Make sure notifications are fired after commit
-        yield self.commit()
         self.assertEquals(
             set(self.notifierFactory.history),
             set([
@@ -692,9 +691,7 @@
         addressbookObject = yield addressbook1.addressbookObjectWithName(name)
         self.assertEquals((yield addressbookObject.component()), component)
 
-        yield self.commit()
-
-        # Make sure notifications fire after commit
+        # notify is called prior to commit
         self.assertEquals(
             set(self.notifierFactory.history),
             set([
@@ -703,7 +700,9 @@
             ])
         )
 
+        yield self.commit()
 
+
     @inlineCallbacks
     def test_createAddressBookObjectWithName_exists(self):
         """
@@ -808,9 +807,7 @@
         addressbookObject = yield addressbook1.addressbookObjectWithName("1.vcf")
         self.assertEquals((yield addressbookObject.component()), component)
 
-        yield self.commit()
-
-        # Make sure notification fired after commit
+        # notify is called prior to commit
         self.assertEquals(
             set(self.notifierFactory.history),
             set([
@@ -819,7 +816,9 @@
             ])
         )
 
+        yield self.commit()
 
+
     def checkPropertiesMethod(self, thunk):
         """
         Verify that the given object has a properties method that returns an

Modified: CalendarServer/trunk/txdav/common/datastore/file.py
===================================================================
--- CalendarServer/trunk/txdav/common/datastore/file.py	2013-10-15 17:00:57 UTC (rev 11816)
+++ CalendarServer/trunk/txdav/common/datastore/file.py	2013-10-15 23:24:11 UTC (rev 11817)
@@ -926,6 +926,7 @@
         return (self._notifierPrefix, self.uid(),)
 
 
+    @inlineCallbacks
     def notifyChanged(self):
         """
         Trigger a notification of a change
@@ -933,8 +934,14 @@
 
         # Only send one set of change notifications per transaction
         if self._notifiers and not self._transaction.isNotifiedAlready(self):
-            for notifier in self._notifiers.values():
+            # cache notifiers run in post commit
+            notifier = self._notifiers.get("cache", None)
+            if notifier:
                 self._transaction.postCommit(notifier.notify)
+            # push notifiers add their work items immediately
+            notifier = self._notifiers.get("push", None)
+            if notifier:
+                yield notifier.notify(self._transaction)
             self._transaction.notificationAddedForObject(self)
 
 
@@ -1272,6 +1279,7 @@
         return self.ownerHome().notifierID()
 
 
+    @inlineCallbacks
     def notifyChanged(self):
         """
         Trigger a notification of a change
@@ -1279,8 +1287,14 @@
 
         # Only send one set of change notifications per transaction
         if self._notifiers and not self._transaction.isNotifiedAlready(self):
-            for notifier in self._notifiers.values():
+            # cache notifiers run in post commit
+            notifier = self._notifiers.get("cache", None)
+            if notifier:
                 self._transaction.postCommit(notifier.notify)
+            # push notifiers add their work items immediately
+            notifier = self._notifiers.get("push", None)
+            if notifier:
+                yield notifier.notify(self._transaction)
             self._transaction.notificationAddedForObject(self)
 
 

Modified: CalendarServer/trunk/txdav/common/datastore/sql.py
===================================================================
--- CalendarServer/trunk/txdav/common/datastore/sql.py	2013-10-15 17:00:57 UTC (rev 11816)
+++ CalendarServer/trunk/txdav/common/datastore/sql.py	2013-10-15 23:24:11 UTC (rev 11817)
@@ -2197,6 +2197,7 @@
         the resource has changed.  We ensure we only do this once per object
         per transaction.
         """
+
         if self._txn.isNotifiedAlready(self):
             returnValue(None)
         self._txn.notificationAddedForObject(self)
@@ -2207,8 +2208,14 @@
 
         # Send notifications
         if self._notifiers:
-            for notifier in self._notifiers.values():
+            # cache notifiers run in post commit
+            notifier = self._notifiers.get("cache", None)
+            if notifier:
                 self._txn.postCommit(notifier.notify)
+            # push notifiers add their work items immediately
+            notifier = self._notifiers.get("push", None)
+            if notifier:
+                yield notifier.notify(self._txn)
 
 
     @classproperty
@@ -4261,8 +4268,14 @@
 
         # Send notifications
         if self._notifiers:
-            for notifier in self._notifiers.values():
+            # cache notifiers run in post commit
+            notifier = self._notifiers.get("cache", None)
+            if notifier:
                 self._txn.postCommit(notifier.notify)
+            # push notifiers add their work items immediately
+            notifier = self._notifiers.get("push", None)
+            if notifier:
+                yield notifier.notify(self._txn)
 
 
     @classproperty
@@ -5082,15 +5095,21 @@
         the resource has changed.  We ensure we only do this once per object
         per transaction.
         """
-        yield
         if self._txn.isNotifiedAlready(self):
             returnValue(None)
         self._txn.notificationAddedForObject(self)
 
         # Send notifications
         if self._notifiers:
-            for notifier in self._notifiers.values():
+            # cache notifiers run in post commit
+            notifier = self._notifiers.get("cache", None)
+            if notifier:
                 self._txn.postCommit(notifier.notify)
+            # push notifiers add their work items immediately
+            notifier = self._notifiers.get("push", None)
+            if notifier:
+                yield notifier.notify(self._txn)
+
         returnValue(None)
 
 

Modified: CalendarServer/trunk/txdav/common/datastore/test/util.py
===================================================================
--- CalendarServer/trunk/txdav/common/datastore/test/util.py	2013-10-15 17:00:57 UTC (rev 11816)
+++ CalendarServer/trunk/txdav/common/datastore/test/util.py	2013-10-15 23:24:11 UTC (rev 11817)
@@ -726,7 +726,7 @@
         return "/%s/%s/%s/" % (prefix, self.hostname, id)
 
 
-    def send(self, prefix, id):
+    def send(self, prefix, id, txn):
         self.history.append(self.pushKeyForId(prefix, id))
 
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20140312/a8542bf9/attachment.html>


More information about the calendarserver-changes mailing list