[CalendarServer-changes] [8257] CalendarServer/trunk

source_changes at macosforge.org source_changes at macosforge.org
Fri Nov 4 09:43:30 PDT 2011


Revision: 8257
          http://trac.macosforge.org/projects/calendarserver/changeset/8257
Author:   sagen at apple.com
Date:     2011-11-04 09:43:30 -0700 (Fri, 04 Nov 2011)
Log Message:
-----------
Refactor makeService_Slave so the db connection pool logic can be shared with the notification sidecar.

Modified Paths:
--------------
    CalendarServer/trunk/calendarserver/tap/caldav.py
    CalendarServer/trunk/calendarserver/tap/util.py
    CalendarServer/trunk/twistedcaldav/notify.py

Modified: CalendarServer/trunk/calendarserver/tap/caldav.py
===================================================================
--- CalendarServer/trunk/calendarserver/tap/caldav.py	2011-11-03 19:59:15 UTC (rev 8256)
+++ CalendarServer/trunk/calendarserver/tap/caldav.py	2011-11-04 16:43:30 UTC (rev 8257)
@@ -68,7 +68,7 @@
 from twistedcaldav.stdconfig import DEFAULT_CONFIG, DEFAULT_CONFIG_FILE
 from twistedcaldav.upgrade import UpgradeFileSystemFormatService, PostDBImportService
 
-from calendarserver.tap.util import pgServiceFromConfig
+from calendarserver.tap.util import pgServiceFromConfig, getDBPool
 
 from twext.enterprise.ienterprise import POSTGRES_DIALECT
 from twext.enterprise.ienterprise import ORACLE_DIALECT
@@ -87,7 +87,6 @@
 from calendarserver.tap.util import getRootResource, computeProcessCount
 from calendarserver.tap.util import ConnectionWithPeer
 from calendarserver.tap.util import storeFromConfig
-from calendarserver.tap.util import transactionFactoryFromFD
 from calendarserver.tap.util import pgConnectorFromConfig
 from calendarserver.tap.util import oracleConnectorFromConfig
 from calendarserver.tools.util import checkDirectory
@@ -638,40 +637,7 @@
         L{makeService_Combined}, which does the work of actually handling
         CalDAV and CardDAV requests.
         """
-        if config.DBType == 'oracle':
-            dialect = ORACLE_DIALECT
-            paramstyle = 'numeric'
-        else:
-            dialect = POSTGRES_DIALECT
-            paramstyle = 'pyformat'
-        pool = None
-        if config.DBAMPFD:
-            txnFactory = transactionFactoryFromFD(
-                int(config.DBAMPFD), dialect, paramstyle
-            )
-        elif not config.UseDatabase:
-            txnFactory = None
-        elif not config.SharedConnectionPool:
-            if config.DBType == '':
-                # get a PostgresService to tell us what the local connection
-                # info is, but *don't* start it (that would start one postgres
-                # master per slave, resulting in all kinds of mayhem...)
-                connectionFactory = pgServiceFromConfig(
-                    config, None).produceConnection
-            elif config.DBType == 'postgres':
-                connectionFactory = pgConnectorFromConfig(config)
-            elif config.DBType == 'oracle':
-                connectionFactory = oracleConnectorFromConfig(config)
-            else:
-                raise UsageError("unknown DB type: %r" % (config.DBType,))
-            pool = ConnectionPool(connectionFactory, dialect=dialect,
-                                  paramstyle=paramstyle,
-                                  maxConnections=config.MaxDBConnectionsPerPool)
-            txnFactory = pool.connection
-        else:
-            raise UsageError(
-                "trying to use DB in slave, but no connection info from parent"
-            )
+        pool, txnFactory = getDBPool(config)
         store = storeFromConfig(config, txnFactory)
         result = self.requestProcessingService(options, store)
         if pool is not None:

Modified: CalendarServer/trunk/calendarserver/tap/util.py
===================================================================
--- CalendarServer/trunk/calendarserver/tap/util.py	2011-11-03 19:59:15 UTC (rev 8256)
+++ CalendarServer/trunk/calendarserver/tap/util.py	2011-11-04 16:43:30 UTC (rev 8257)
@@ -21,6 +21,7 @@
 
 __all__ = [
     "getRootResource",
+    "getDBPool",
     "FakeRequest",
 ]
 
@@ -65,6 +66,9 @@
 from twistedcaldav.timezoneservice import TimezoneServiceResource
 from twistedcaldav.timezonestdservice import TimezoneStdServiceResource
 from twistedcaldav.util import getMemorySize, getNCPU
+from twext.enterprise.ienterprise import POSTGRES_DIALECT
+from twext.enterprise.ienterprise import ORACLE_DIALECT
+from twext.enterprise.adbapi2 import ConnectionPool
 
 try:
     from twistedcaldav.authkerb import NegotiateCredentialFactory
@@ -87,6 +91,7 @@
 from txdav.common.datastore.sql import current_sql_schema
 from twext.python.filepath import CachingFilePath
 from urllib import quote
+from twisted.python.usage import UsageError
 
 
 log = Logger()
@@ -651,7 +656,51 @@
     return logWrapper
 
 
+def getDBPool(config):
+    """
+    Inspect configuration to determine what database connection pool
+    to set up.
+    return: (L{ConnectionPool}, transactionFactory)
+    """
+    if config.DBType == 'oracle':
+        dialect = ORACLE_DIALECT
+        paramstyle = 'numeric'
+    else:
+        dialect = POSTGRES_DIALECT
+        paramstyle = 'pyformat'
+    pool = None
+    if config.DBAMPFD:
+        txnFactory = transactionFactoryFromFD(
+            int(config.DBAMPFD), dialect, paramstyle
+        )
+    elif not config.UseDatabase:
+        txnFactory = None
+    elif not config.SharedConnectionPool:
+        if config.DBType == '':
+            # get a PostgresService to tell us what the local connection
+            # info is, but *don't* start it (that would start one postgres
+            # master per slave, resulting in all kinds of mayhem...)
+            connectionFactory = pgServiceFromConfig(
+                config, None).produceConnection
+        elif config.DBType == 'postgres':
+            connectionFactory = pgConnectorFromConfig(config)
+        elif config.DBType == 'oracle':
+            connectionFactory = oracleConnectorFromConfig(config)
+        else:
+            raise UsageError("unknown DB type: %r" % (config.DBType,))
+        pool = ConnectionPool(connectionFactory, dialect=dialect,
+                              paramstyle=paramstyle,
+                              maxConnections=config.MaxDBConnectionsPerPool)
+        txnFactory = pool.connection
+    else:
+        raise UsageError(
+            "trying to use DB in slave, but no connection info from parent"
+        )
 
+    return (pool, txnFactory)
+
+
+
 def computeProcessCount(minimum, perCPU, perGB, cpuCount=None, memSize=None):
     """
     Determine how many process to spawn based on installed RAM and CPUs,

Modified: CalendarServer/trunk/twistedcaldav/notify.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/notify.py	2011-11-03 19:59:15 UTC (rev 8256)
+++ CalendarServer/trunk/twistedcaldav/notify.py	2011-11-04 16:43:30 UTC (rev 8257)
@@ -1458,44 +1458,14 @@
             config.Memcached.MaxClients,
         )
 
-        # TODO: This code is copied from makeService_Slave, and needs to be
-        # refactored so it can be shared instead.
-        from calendarserver.tap.util import (
-            storeFromConfig, pgConnectorFromConfig, oracleConnectorFromConfig,
-            pgServiceFromConfig
-        )
-        from twext.enterprise.ienterprise import POSTGRES_DIALECT
-        from twext.enterprise.ienterprise import ORACLE_DIALECT
-        from twext.enterprise.adbapi2 import ConnectionPool
+        multiService = service.MultiService()
 
-        pool = None
-        if not config.UseDatabase:
-            txnFactory = None
-        else:
-            dialect = POSTGRES_DIALECT
-            paramstyle = 'pyformat'
-            if config.DBType == '':
-                # get a PostgresService to tell us what the local connection
-                # info is, but *don't* start it (that would start one postgres
-                # master per slave, resulting in all kinds of mayhem...)
-                connectionFactory = pgServiceFromConfig(
-                    config, None).produceConnection
-            elif config.DBType == 'postgres':
-                connectionFactory = pgConnectorFromConfig(config)
-            elif config.DBType == 'oracle':
-                dialect = ORACLE_DIALECT
-                paramstyle = 'numeric'
-                connectionFactory = oracleConnectorFromConfig(config)
-            else:
-                raise UsageError("unknown DB type: %r" % (config.DBType,))
-            pool = ConnectionPool(connectionFactory, dialect=dialect,
-                                  paramstyle=paramstyle)
-            txnFactory = pool.connection
-
+        from calendarserver.tap.util import storeFromConfig, getDBPool
+        pool, txnFactory = getDBPool(config)
+        if pool is not None:
+            pool.setServiceParent(multiService)
         store = storeFromConfig(config, txnFactory)
 
-        multiService = service.MultiService()
-
         notifiers = []
         for key, settings in config.Notifications.Services.iteritems():
             if settings["Enabled"]:
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20111104/a21530b7/attachment.html>


More information about the calendarserver-changes mailing list