[CalendarServer-changes] [15510] CalendarServer/trunk

source_changes at macosforge.org source_changes at macosforge.org
Fri Apr 15 12:11:37 PDT 2016


Revision: 15510
          http://trac.calendarserver.org//changeset/15510
Author:   cdaboo at apple.com
Date:     2016-04-15 12:11:37 -0700 (Fri, 15 Apr 2016)
Log Message:
-----------
Make sure calendarserver_manage_principals proxy changes invalidate the principal PROPFIND cache.

Modified Paths:
--------------
    CalendarServer/trunk/calendarserver/provision/root.py
    CalendarServer/trunk/calendarserver/tools/principals.py
    CalendarServer/trunk/calendarserver/tools/util.py
    CalendarServer/trunk/twistedcaldav/cache.py
    CalendarServer/trunk/txdav/who/delegates.py
    CalendarServer/trunk/txdav/who/test/test_delegates.py

Modified: CalendarServer/trunk/calendarserver/provision/root.py
===================================================================
--- CalendarServer/trunk/calendarserver/provision/root.py	2016-04-15 19:11:01 UTC (rev 15509)
+++ CalendarServer/trunk/calendarserver/provision/root.py	2016-04-15 19:11:37 UTC (rev 15510)
@@ -39,6 +39,7 @@
 from twistedcaldav.extensions import DirectoryPrincipalPropertySearchMixIn
 from twistedcaldav.extensions import ReadOnlyResourceMixIn
 from twistedcaldav.resource import CalDAVComplianceMixIn
+from txdav.who.delegates import CachingDelegates
 from txdav.who.wiki import DirectoryService as WikiDirectoryService
 from txdav.who.wiki import uidForAuthToken
 from txweb2 import responsecode
@@ -93,9 +94,8 @@
 
             # These class attributes need to be setup with our memcache\
             # notifier
-            DirectoryPrincipalResource.cacheNotifierFactory = (
-                MemcacheChangeNotifier
-            )
+            DirectoryPrincipalResource.cacheNotifierFactory = MemcacheChangeNotifier
+            CachingDelegates.cacheNotifier = MemcacheChangeNotifier(None, cacheHandle="PrincipalToken")
         else:
             self.responseCache = DisabledCache()
 

Modified: CalendarServer/trunk/calendarserver/tools/principals.py
===================================================================
--- CalendarServer/trunk/calendarserver/tools/principals.py	2016-04-15 19:11:01 UTC (rev 15509)
+++ CalendarServer/trunk/calendarserver/tools/principals.py	2016-04-15 19:11:37 UTC (rev 15510)
@@ -39,7 +39,9 @@
 from twisted.internet import reactor
 from twisted.internet.defer import inlineCallbacks, returnValue, succeed
 from twistedcaldav.config import config
-from txdav.who.delegates import Delegates, RecordType as DelegateRecordType
+from twistedcaldav.cache import MemcacheChangeNotifier
+from txdav.who.delegates import Delegates, RecordType as DelegateRecordType, \
+    CachingDelegates
 from txdav.who.idirectory import AutoScheduleMode
 from txdav.who.groups import GroupCacherPollingWork
 
@@ -125,12 +127,21 @@
         Calls the function that's been assigned to "function" and passes the root
         resource, directory, store, and whatever has been assigned to "params".
         """
+        if (
+            config.EnableResponseCache and
+            config.Memcached.Pools.Default.ClientEnabled
+        ):
+            # These class attributes need to be setup with our memcache\
+            # notifier
+            CachingDelegates.cacheNotifier = MemcacheChangeNotifier(None, cacheHandle="PrincipalToken")
+
         if self.function is not None:
             yield self.function(self.store, *self.params)
 
 
 
 def main():
+
     try:
         (optargs, args) = getopt(
             sys.argv[1:], "a:hf:P:v", [

Modified: CalendarServer/trunk/calendarserver/tools/util.py
===================================================================
--- CalendarServer/trunk/calendarserver/tools/util.py	2016-04-15 19:11:01 UTC (rev 15509)
+++ CalendarServer/trunk/calendarserver/tools/util.py	2016-04-15 19:11:37 UTC (rev 15510)
@@ -100,9 +100,14 @@
     for pool in config.Memcached.Pools.itervalues():
         if pool.ClientEnabled:
             try:
-                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-                s.connect((pool.BindAddress, pool.Port))
-                s.close()
+                if pool.MemcacheSocket:
+                    s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+                    s.connect(pool.MemcacheSocket)
+                    s.close()
+                else:
+                    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+                    s.connect((pool.BindAddress, pool.Port))
+                    s.close()
 
             except socket.error:
                 pool.ClientEnabled = False

Modified: CalendarServer/trunk/twistedcaldav/cache.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/cache.py	2016-04-15 19:11:01 UTC (rev 15509)
+++ CalendarServer/trunk/twistedcaldav/cache.py	2016-04-15 19:11:37 UTC (rev 15510)
@@ -75,7 +75,7 @@
         pass
 
 
-    def changed(self):
+    def changed(self, url=None):
         return succeed(None)
 
 
@@ -118,15 +118,20 @@
         return str(uuid.uuid4())
 
 
-    def changed(self):
+    def changed(self, url=None):
         """
         Change the cache token for a resource
 
+        @param url: the url whose token is being changed, or C{None} to determine
+            the URL from the supplied resource
+        @type: L{str}
+
         return: A L{Deferred} that fires when the token has been changed.
         """
 
         # For shared resources we use the owner URL as the cache key
-        url = self._resource.url()
+        if url is None:
+            url = self._resource.url()
 
         self.log.debug("Changing Cache Token for {url}", url=url)
         return self.getCachePool().set(

Modified: CalendarServer/trunk/txdav/who/delegates.py
===================================================================
--- CalendarServer/trunk/txdav/who/delegates.py	2016-04-15 19:11:01 UTC (rev 15509)
+++ CalendarServer/trunk/txdav/who/delegates.py	2016-04-15 19:11:37 UTC (rev 15510)
@@ -233,6 +233,8 @@
     Manages access to the store's delegates API, including caching of results.
     """
 
+    cacheNotifier = None
+
     class DelegatesMemcacher(Memcacher):
 
         def __init__(self, namespace):
@@ -358,6 +360,11 @@
         else:
             yield txn.addDelegate(delegator.uid, delegate.uid, readWrite)
 
+        # Make sure notifications are sent
+        if self.cacheNotifier is not None:
+            yield self.cacheNotifier.changed("/principals/__uids__/{}/".format(delegator.uid))
+            yield self.cacheNotifier.changed("/principals/__uids__/{}/".format(delegate.uid))
+
         # Update cache (remove the member cache entry first as we need to recalculate it for
         # memberships removal)
         yield self._memcacher.deleteMember(delegator.uid, readWrite)
@@ -393,6 +400,11 @@
         else:
             yield txn.removeDelegate(delegator.uid, delegate.uid, readWrite)
 
+        # Make sure notifications are sent
+        if self.cacheNotifier is not None:
+            yield self.cacheNotifier.changed("/principals/__uids__/{}/".format(delegator.uid))
+            yield self.cacheNotifier.changed("/principals/__uids__/{}/".format(delegate.uid))
+
         # Update cache (remove the member cache entry first as we need to recalculate it for
         # memberships removal)
         yield self._memcacher.deleteMember(delegator.uid, readWrite)

Modified: CalendarServer/trunk/txdav/who/test/test_delegates.py
===================================================================
--- CalendarServer/trunk/txdav/who/test/test_delegates.py	2016-04-15 19:11:01 UTC (rev 15509)
+++ CalendarServer/trunk/txdav/who/test/test_delegates.py	2016-04-15 19:11:37 UTC (rev 15510)
@@ -21,13 +21,29 @@
 from txdav.common.datastore.sql import CommonStoreTransaction
 from txdav.common.datastore.sql_directory import DelegateRecord, \
     DelegateGroupsRecord
-from txdav.who.delegates import Delegates, RecordType as DelegateRecordType
+from txdav.who.delegates import Delegates, RecordType as DelegateRecordType, \
+    CachingDelegates
 from txdav.who.groups import GroupCacher
 from twext.who.idirectory import RecordType
-from twisted.internet.defer import inlineCallbacks
+from twisted.internet.defer import inlineCallbacks, succeed
 from twistedcaldav.test.util import StoreTestCase
 
 
+class CapturingCacheNotifier(object):
+    def __init__(self, *args, **kwargs):
+        self.history = []
+
+
+    def changed(self, url=None):
+        self.history.append(url)
+        return succeed(None)
+
+
+    def clear(self):
+        self.history = []
+
+
+
 class DelegationTest(StoreTestCase):
 
     @inlineCallbacks
@@ -301,7 +317,9 @@
 
         yield Delegates._memcacher.flushAll()
 
+        self.patch(CachingDelegates, "cacheNotifier", CapturingCacheNotifier())
 
+
     @inlineCallbacks
     def _memcacherMemberResults(self, delegate, readWrite, expanded, results):
         delegateUIDs = yield Delegates._memcacher.getMembers(delegate.uid, readWrite, expanded)
@@ -447,6 +465,10 @@
         yield Delegates.addDelegate(self.transactionUnderTest(), delegator, delegate1, True)
         yield self.commit()
 
+        # Notifications
+        self.assertEqual(set(Delegates.cacheNotifier.history), set((u"/principals/__uids__/__wsanchez1__/", u"/principals/__uids__/__sagen1__/",))) #@UndefinedVariable
+        Delegates.cacheNotifier.clear() #@UndefinedVariable
+
         # Some cache entries invalid
         yield self._memcacherAllMemberResults(delegator, None, [delegate1], None, None)
         yield self._memcacherAllMemberResults(delegate1, None, None, None, None)
@@ -499,6 +521,10 @@
         yield Delegates.removeDelegate(self.transactionUnderTest(), delegator, delegate1, True)
         yield self.commit()
 
+        # Notifications
+        self.assertEqual(set(Delegates.cacheNotifier.history), set((u"/principals/__uids__/__wsanchez1__/", u"/principals/__uids__/__sagen1__/",))) #@UndefinedVariable
+        Delegates.cacheNotifier.clear() #@UndefinedVariable
+
         # Some cache entries invalid
         yield self._memcacherAllMemberResults(delegator, None, [], [], [])
         yield self._memcacherAllMemberResults(delegate1, [], [], [], [])
@@ -562,6 +588,10 @@
         yield Delegates.setDelegates(self.transactionUnderTest(), delegator, [delegates[0], delegates[1]], True)
         yield self.commit()
 
+        # Notifications
+        self.assertEqual(set(Delegates.cacheNotifier.history), set((u"/principals/__uids__/__wsanchez1__/", u"/principals/__uids__/__sagen1__/", u"/principals/__uids__/__cdaboo1__/",))) #@UndefinedVariable
+        Delegates.cacheNotifier.clear() #@UndefinedVariable
+
         # Some cache entries invalid
         yield self._memcacherAllMemberResults(delegator, None, [delegates[0], delegates[1]], None, None)
         yield self._memcacherAllMembershipResults(delegator, None, None)
@@ -593,6 +623,10 @@
         yield Delegates.setDelegates(self.transactionUnderTest(), delegator, [delegates[1], delegates[2]], True)
         yield self.commit()
 
+        # Notifications
+        self.assertEqual(set(Delegates.cacheNotifier.history), set((u"/principals/__uids__/__wsanchez1__/", u"/principals/__uids__/__sagen1__/", u"/principals/__uids__/__dre1__/"))) #@UndefinedVariable
+        Delegates.cacheNotifier.clear() #@UndefinedVariable
+
         # Some cache entries invalid
         yield self._memcacherAllMemberResults(delegator, None, [delegates[1], delegates[2]], [], [])
         for delegate in delegates:
@@ -626,6 +660,10 @@
         yield Delegates.setDelegates(self.transactionUnderTest(), delegator, [delegates[0]], False)
         yield self.commit()
 
+        # Notifications
+        self.assertEqual(set(Delegates.cacheNotifier.history), set((u"/principals/__uids__/__wsanchez1__/", u"/principals/__uids__/__sagen1__/"))) #@UndefinedVariable
+        Delegates.cacheNotifier.clear() #@UndefinedVariable
+
         # Some cache entries invalid
         yield self._memcacherAllMemberResults(delegator, [delegates[1], delegates[2]], [delegates[1], delegates[2]], None, [delegates[0]])
         for delegate in delegates:
@@ -679,6 +717,10 @@
         yield Delegates.setDelegates(self.transactionUnderTest(), delegator, [group1], True)
         yield self.commit()
 
+        # Notifications
+        self.assertEqual(set(Delegates.cacheNotifier.history), set((u"/principals/__uids__/__wsanchez1__/", u"/principals/__uids__/__top_group_1__/",))) #@UndefinedVariable
+        Delegates.cacheNotifier.clear() #@UndefinedVariable
+
         # Some cache entries invalid
         yield self._memcacherAllMemberResults(delegator, None, delegateMatch(0, 1, 2), None, None)
         yield self._memcacherAllMembershipResults(delegator, None, None)
@@ -712,6 +754,10 @@
         yield Delegates.setDelegates(self.transactionUnderTest(), delegator, [group1, delegates[3]], True)
         yield self.commit()
 
+        # Notifications
+        self.assertEqual(set(Delegates.cacheNotifier.history), set((u"/principals/__uids__/__wsanchez1__/", u"/principals/__uids__/__dre1__/"))) #@UndefinedVariable
+        Delegates.cacheNotifier.clear() #@UndefinedVariable
+
         # Some cache entries invalid
         yield self._memcacherAllMemberResults(delegator, None, delegateMatch(0, 1, 2, 3), [], [])
         for delegate in delegates:
@@ -748,6 +794,10 @@
         yield Delegates.setDelegates(self.transactionUnderTest(), delegator, [group2, delegates[3]], True)
         yield self.commit()
 
+        # Notifications
+        self.assertEqual(set(Delegates.cacheNotifier.history), set((u"/principals/__uids__/__wsanchez1__/", u"/principals/__uids__/__top_group_1__/", u"/principals/__uids__/__sub_group_1__/"))) #@UndefinedVariable
+        Delegates.cacheNotifier.clear() #@UndefinedVariable
+
         # Some cache entries invalid
         yield self._memcacherAllMemberResults(delegator, None, delegateMatch(0, 1, 3), [], [])
         for delegate in delegates:
@@ -784,6 +834,10 @@
         yield Delegates.setDelegates(self.transactionUnderTest(), delegator, [group2, delegates[0], delegates[3]], True)
         yield self.commit()
 
+        # Notifications
+        self.assertEqual(set(Delegates.cacheNotifier.history), set((u"/principals/__uids__/__wsanchez1__/", u"/principals/__uids__/__sagen1__/"))) #@UndefinedVariable
+        Delegates.cacheNotifier.clear() #@UndefinedVariable
+
         # Some cache entries invalid
         yield self._memcacherAllMemberResults(delegator, None, delegateMatch(0, 1, 3), [], [])
         for delegate in delegates:
@@ -820,6 +874,10 @@
         yield Delegates.setDelegates(self.transactionUnderTest(), delegator, [delegates[0], delegates[3]], True)
         yield self.commit()
 
+        # Notifications
+        self.assertEqual(set(Delegates.cacheNotifier.history), set((u"/principals/__uids__/__wsanchez1__/", u"/principals/__uids__/__sub_group_1__/"))) #@UndefinedVariable
+        Delegates.cacheNotifier.clear() #@UndefinedVariable
+
         # Some cache entries invalid
         yield self._memcacherAllMemberResults(delegator, None, delegateMatch(0, 3), [], [])
         for delegate in delegates:
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20160415/c551a0b8/attachment-0001.html>


More information about the calendarserver-changes mailing list