[CalendarServer-changes] [11454] CalendarServer/trunk

source_changes at macosforge.org source_changes at macosforge.org
Tue Jul 2 20:38:58 PDT 2013


Revision: 11454
          http://trac.calendarserver.org//changeset/11454
Author:   sagen at apple.com
Date:     2013-07-02 20:38:57 -0700 (Tue, 02 Jul 2013)
Log Message:
-----------
Put a lock around upgrade logic

Modified Paths:
--------------
    CalendarServer/trunk/calendarserver/tap/caldav.py
    CalendarServer/trunk/txdav/base/datastore/subpostgres.py
    CalendarServer/trunk/txdav/common/datastore/sql.py
    CalendarServer/trunk/txdav/common/datastore/upgrade/sql/upgrade.py

Modified: CalendarServer/trunk/calendarserver/tap/caldav.py
===================================================================
--- CalendarServer/trunk/calendarserver/tap/caldav.py	2013-07-02 22:35:26 UTC (rev 11453)
+++ CalendarServer/trunk/calendarserver/tap/caldav.py	2013-07-03 03:38:57 UTC (rev 11454)
@@ -69,6 +69,7 @@
 from txdav.common.datastore.upgrade.sql.upgrade import (
     UpgradeDatabaseSchemaStep, UpgradeDatabaseAddressBookDataStep,
     UpgradeDatabaseCalendarDataStep, UpgradeDatabaseOtherStep,
+    UpgradeAcquireLockStep, UpgradeReleaseLockStep
 )
 from txdav.common.datastore.upgrade.migrate import UpgradeToDatabaseStep
 from txdav.caldav.datastore.scheduling.imip.inbound import MailRetriever
@@ -1307,6 +1308,10 @@
                 # the subsequent steps' stepWithFailure methods will be called
                 # instead, until one of them returns a non-Failure.
 
+                pps.addStep(
+                    UpgradeAcquireLockStep(store)
+                )
+
                 # Still need this for Snow Leopard support
                 pps.addStep(
                     UpgradeFileSystemFormatStep(config)
@@ -1324,6 +1329,7 @@
                         store, uid=overrideUID, gid=overrideGID
                     )
                 )
+
                 pps.addStep(
                     UpgradeDatabaseCalendarDataStep(
                         store, uid=overrideUID, gid=overrideGID
@@ -1356,6 +1362,11 @@
                         getattr(self, "doPostImport", True)
                     )
                 )
+
+                pps.addStep(
+                    UpgradeReleaseLockStep(store)
+                )
+
                 pps.setServiceParent(ms)
                 return ms
 

Modified: CalendarServer/trunk/txdav/base/datastore/subpostgres.py
===================================================================
--- CalendarServer/trunk/txdav/base/datastore/subpostgres.py	2013-07-02 22:35:26 UTC (rev 11453)
+++ CalendarServer/trunk/txdav/base/datastore/subpostgres.py	2013-07-03 03:38:57 UTC (rev 11454)
@@ -402,6 +402,22 @@
             createDatabaseCursor.execute("commit")
             return createDatabaseConn, createDatabaseCursor
 
+        # TODO: always go through pg_ctl start
+        try:
+            createDatabaseConn, createDatabaseCursor = createConnection()
+        except pgdb.DatabaseError:
+            # We could not connect the database, so attempt to start it
+            pass
+        except Exception, e:
+            # Some other unexpected error is preventing us from connecting
+            # to the database
+            log.warn("Failed to connect to Postgres: {e}", e=e)
+        else:
+            # Database is running, so just use our connection
+            self.ready(createDatabaseConn, createDatabaseCursor)
+            self.deactivateDelayedShutdown()
+            return
+
         monitor = _PostgresMonitor(self)
         pgCtl = self.pgCtl()
         # check consistency of initdb and postgres?

Modified: CalendarServer/trunk/txdav/common/datastore/sql.py
===================================================================
--- CalendarServer/trunk/txdav/common/datastore/sql.py	2013-07-02 22:35:26 UTC (rev 11453)
+++ CalendarServer/trunk/txdav/common/datastore/sql.py	2013-07-03 03:38:57 UTC (rev 11454)
@@ -1362,7 +1362,13 @@
         returnValue(count)
 
 
+    def acquireUpgradeLock(self):
+        return self.execSQL("select pg_advisory_lock(1)")
 
+    def releaseUpgradeLock(self):
+        return self.execSQL("select pg_advisory_unlock(1)")
+
+
 class _EmptyCacher(object):
 
     def set(self, key, value):  #@UnusedVariable

Modified: CalendarServer/trunk/txdav/common/datastore/upgrade/sql/upgrade.py
===================================================================
--- CalendarServer/trunk/txdav/common/datastore/upgrade/sql/upgrade.py	2013-07-02 22:35:26 UTC (rev 11453)
+++ CalendarServer/trunk/txdav/common/datastore/upgrade/sql/upgrade.py	2013-07-03 03:38:57 UTC (rev 11454)
@@ -31,6 +31,49 @@
 
 from txdav.common.datastore.upgrade.sql.others import attachment_migration
 
+
+class UpgradeAcquireLockStep(object):
+    """
+    A Step which acquires the upgrade lock, blocking later Steps until it's
+    been acquired.
+
+    @ivar sqlStore: The store to operate on.
+
+    @type sqlStore: L{txdav.idav.IDataStore}
+    """
+
+    def __init__(self, sqlStore):
+        self.sqlStore = sqlStore
+
+    @inlineCallbacks
+    def stepWithResult(self, result):
+        sqlTxn = self.sqlStore.newTransaction()
+        yield sqlTxn.acquireUpgradeLock()
+        yield sqlTxn.commit()
+
+
+class UpgradeReleaseLockStep(object):
+    """
+    A Step which releases the upgrade lock.
+
+    @ivar sqlStore: The store to operate on.
+
+    @type sqlStore: L{txdav.idav.IDataStore}
+    """
+
+    def __init__(self, sqlStore):
+        self.sqlStore = sqlStore
+
+    @inlineCallbacks
+    def stepWithResult(self, result):
+        sqlTxn = self.sqlStore.newTransaction()
+        yield sqlTxn.releaseUpgradeLock()
+        yield sqlTxn.commit()
+
+    def stepWithFailure(self, failure):
+        return self.stepWithResult(None)
+
+
 class UpgradeDatabaseCoreStep(object):
     """
     Base class for either schema or data upgrades on the database.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20130702/df0e31f5/attachment.html>


More information about the calendarserver-changes mailing list