[CalendarServer-changes] [12182] CalendarServer/branches/users/gaya/cleanrevisions

source_changes at macosforge.org source_changes at macosforge.org
Wed Mar 12 11:21:18 PDT 2014


Revision: 12182
          http://trac.calendarserver.org//changeset/12182
Author:   gaya at apple.com
Date:     2013-12-20 20:32:24 -0800 (Fri, 20 Dec 2013)
Log Message:
-----------
checkpoint

Modified Paths:
--------------
    CalendarServer/branches/users/gaya/cleanrevisions/calendarserver/tap/caldav.py
    CalendarServer/branches/users/gaya/cleanrevisions/twistedcaldav/stdconfig.py
    CalendarServer/branches/users/gaya/cleanrevisions/txdav/carddav/datastore/sql.py
    CalendarServer/branches/users/gaya/cleanrevisions/txdav/common/datastore/sql.py
    CalendarServer/branches/users/gaya/cleanrevisions/txdav/common/datastore/sql_schema/current-oracle-dialect.sql
    CalendarServer/branches/users/gaya/cleanrevisions/txdav/common/datastore/sql_schema/current.sql
    CalendarServer/branches/users/gaya/cleanrevisions/txdav/common/datastore/sql_schema/upgrades/oracle-dialect/upgrade_from_30_to_31.sql
    CalendarServer/branches/users/gaya/cleanrevisions/txdav/common/datastore/sql_schema/upgrades/postgres-dialect/upgrade_from_30_to_31.sql

Added Paths:
-----------
    CalendarServer/branches/users/gaya/cleanrevisions/txdav/common/datastore/work/
    CalendarServer/branches/users/gaya/cleanrevisions/txdav/common/datastore/work/__init__.py
    CalendarServer/branches/users/gaya/cleanrevisions/txdav/common/datastore/work/revision_cleanup.py

Modified: CalendarServer/branches/users/gaya/cleanrevisions/calendarserver/tap/caldav.py
===================================================================
--- CalendarServer/branches/users/gaya/cleanrevisions/calendarserver/tap/caldav.py	2013-12-21 00:38:11 UTC (rev 12181)
+++ CalendarServer/branches/users/gaya/cleanrevisions/calendarserver/tap/caldav.py	2013-12-21 04:32:24 UTC (rev 12182)
@@ -37,54 +37,55 @@
 
 from zope.interface import implements
 
+from twisted.application.internet import TCPServer, UNIXServer
+from twisted.application.service import MultiService, IServiceMaker
+from twisted.application.service import Service
+from twisted.internet.defer import gatherResults, Deferred, inlineCallbacks, succeed
+from twisted.internet.endpoints import UNIXClientEndpoint, TCP4ClientEndpoint
+from twisted.internet.process import ProcessExitedAlready
+from twisted.internet.protocol import ProcessProtocol
+from twisted.internet.protocol import Protocol, Factory
+from twisted.plugin import IPlugin
+from twisted.protocols.amp import AMP
 from twisted.python.log import FileLogObserver, ILogObserver
 from twisted.python.logfile import LogFile
 from twisted.python.usage import Options, UsageError
 from twisted.python.util import uidFromString, gidFromString
-from twisted.plugin import IPlugin
-from twisted.internet.defer import gatherResults, Deferred, inlineCallbacks, succeed
-from twisted.internet.process import ProcessExitedAlready
-from twisted.internet.protocol import Protocol, Factory
-from twisted.internet.protocol import ProcessProtocol
-from twisted.internet.endpoints import UNIXClientEndpoint, TCP4ClientEndpoint
-from twisted.application.internet import TCPServer, UNIXServer
-from twisted.application.service import MultiService, IServiceMaker
-from twisted.application.service import Service
-from twisted.protocols.amp import AMP
 
-from txweb2.server import Site
-from twext.python.log import Logger, LogLevel, replaceTwistedLoggers
-from twext.python.filepath import CachingFilePath
+from twext.enterprise.adbapi2 import ConnectionPool
+from twext.enterprise.ienterprise import ORACLE_DIALECT
+from twext.enterprise.ienterprise import POSTGRES_DIALECT
+from twext.enterprise.queue import NonPerformingQueuer
+from twext.enterprise.queue import PeerConnectionPool
+from twext.enterprise.queue import WorkerFactory as QueueWorkerFactory
+from twext.internet.fswatch import DirectoryChangeListener, IDirectoryChangeListenee
 from twext.internet.ssl import ChainingOpenSSLContextFactory
 from twext.internet.tcp import MaxAcceptTCPServer, MaxAcceptSSLServer
-from twext.internet.fswatch import DirectoryChangeListener, IDirectoryChangeListenee
+from twext.python.filepath import CachingFilePath
+from twext.python.log import Logger, LogLevel, replaceTwistedLoggers
 from txweb2.channel.http import LimitingHTTPFactory, SSLRedirectRequest, \
     HTTPChannel
 from txweb2.metafd import ConnectionLimiter, ReportingHTTPService
-from twext.enterprise.ienterprise import POSTGRES_DIALECT
-from twext.enterprise.ienterprise import ORACLE_DIALECT
-from twext.enterprise.adbapi2 import ConnectionPool
-from twext.enterprise.queue import NonPerformingQueuer
-from twext.enterprise.queue import PeerConnectionPool
-from twext.enterprise.queue import WorkerFactory as QueueWorkerFactory
+from txweb2.server import Site
 
+from txdav.caldav.datastore.scheduling.imip.inbound import MailRetriever
+from txdav.caldav.datastore.scheduling.imip.inbound import scheduleNextMailPoll
 from txdav.common.datastore.sql_tables import schema
+from txdav.common.datastore.upgrade.migrate import UpgradeToDatabaseStep
 from txdav.common.datastore.upgrade.sql.upgrade import (
+    UpgradeDatabaseCalendarDataStep, UpgradeDatabaseOtherStep,
     UpgradeDatabaseSchemaStep, UpgradeDatabaseAddressBookDataStep,
-    UpgradeDatabaseCalendarDataStep, UpgradeDatabaseOtherStep,
     UpgradeAcquireLockStep, UpgradeReleaseLockStep, UpgradeDatabaseNotificationDataStep)
-from txdav.common.datastore.upgrade.migrate import UpgradeToDatabaseStep
-from txdav.caldav.datastore.scheduling.imip.inbound import MailRetriever
-from txdav.caldav.datastore.scheduling.imip.inbound import scheduleNextMailPoll
+from txdav.common.datastore.work.revision_cleanup import scheduleFirstFindMinRevision
 
+from twistedcaldav import memcachepool
 from twistedcaldav.config import config, ConfigurationError
-from twistedcaldav.stdconfig import DEFAULT_CONFIG, DEFAULT_CONFIG_FILE
 from twistedcaldav.directory import calendaruserproxy
 from twistedcaldav.directory.directory import GroupMembershipCacheUpdater
+from twistedcaldav.directory.directory import scheduleNextGroupCachingUpdate
 from twistedcaldav.localization import processLocalizationFiles
-from twistedcaldav import memcachepool
+from twistedcaldav.stdconfig import DEFAULT_CONFIG, DEFAULT_CONFIG_FILE
 from twistedcaldav.upgrade import UpgradeFileSystemFormatStep, PostDBImportStep
-from twistedcaldav.directory.directory import scheduleNextGroupCachingUpdate
 
 try:
     from twistedcaldav.authkerb import NegotiateCredentialFactory
@@ -92,22 +93,22 @@
 except ImportError:
     NegotiateCredentialFactory = None
 
-from calendarserver.tap.util import pgServiceFromConfig, getDBPool, MemoryLimitService
-from calendarserver.tap.util import checkDirectories
-from calendarserver.tap.util import Stepper
-from calendarserver.tap.util import ConnectionDispenser
-from calendarserver.tap.util import getRootResource
-from calendarserver.tap.util import storeFromConfig
-from calendarserver.tap.util import pgConnectorFromConfig
-from calendarserver.tap.util import oracleConnectorFromConfig
-from calendarserver.controlsocket import ControlSocket
-from calendarserver.controlsocket import ControlSocketConnectingService
 from calendarserver.accesslog import AMPCommonAccessLoggingObserver
 from calendarserver.accesslog import AMPLoggingFactory
 from calendarserver.accesslog import RotatingFileAccessLoggingObserver
-from calendarserver.push.notifier import PushDistributor
+from calendarserver.controlsocket import ControlSocket
+from calendarserver.controlsocket import ControlSocketConnectingService
 from calendarserver.push.amppush import AMPPushMaster, AMPPushForwarder
 from calendarserver.push.applepush import ApplePushNotifierService
+from calendarserver.push.notifier import PushDistributor
+from calendarserver.tap.util import ConnectionDispenser
+from calendarserver.tap.util import Stepper
+from calendarserver.tap.util import checkDirectories
+from calendarserver.tap.util import getRootResource
+from calendarserver.tap.util import oracleConnectorFromConfig
+from calendarserver.tap.util import pgConnectorFromConfig
+from calendarserver.tap.util import pgServiceFromConfig, getDBPool, MemoryLimitService
+from calendarserver.tap.util import storeFromConfig
 
 try:
     from calendarserver.version import version
@@ -550,6 +551,7 @@
             yield scheduleNextMailPoll(self.store, int(config.LogID) if config.LogID else 5)
         if self.doGroupCaching:
             yield scheduleNextGroupCachingUpdate(self.store, int(config.LogID) if config.LogID else 5)
+        yield scheduleFirstFindMinRevision(self.store)
 
 
 

Modified: CalendarServer/branches/users/gaya/cleanrevisions/twistedcaldav/stdconfig.py
===================================================================
--- CalendarServer/branches/users/gaya/cleanrevisions/twistedcaldav/stdconfig.py	2013-12-21 00:38:11 UTC (rev 12181)
+++ CalendarServer/branches/users/gaya/cleanrevisions/twistedcaldav/stdconfig.py	2013-12-21 04:32:24 UTC (rev 12182)
@@ -625,7 +625,9 @@
 
     "RemoveDuplicatePrivateComments": False, # Remove duplicate private comments on PUT
 
-    "SyncTokenLifetimeDays" : 14, # Number of days that a client sync report token is valid
+    "SyncTokenLifetimeDays" : 16,       # Number of days that a client sync report token is valid
+    "RevisionCleanupPeriodDays" : 2,    # Number of days between revision cleanups
+    "RevisionCleanupPhaseDays" : 0.25,  # Number of days between startup and the first revision cleanup
 
 
     # CardDAV Features

Modified: CalendarServer/branches/users/gaya/cleanrevisions/txdav/carddav/datastore/sql.py
===================================================================
--- CalendarServer/branches/users/gaya/cleanrevisions/txdav/carddav/datastore/sql.py	2013-12-21 00:38:11 UTC (rev 12181)
+++ CalendarServer/branches/users/gaya/cleanrevisions/txdav/carddav/datastore/sql.py	2013-12-21 04:32:24 UTC (rev 12182)
@@ -569,15 +569,14 @@
 
         if revision:
             cs = schema.CALENDARSERVER
-            minRevisionRows = yield Select(
+            minRevision = int((yield Select(
                 [cs.VALUE],
                 From=cs,
                 Where=(cs.NAME == "MIN-REVISION")
-            ).on(self._txn)
+            ).on(self._txn))[0][0])
 
-            if minRevisionRows:
-                if revision < int(minRevisionRows[0][0]):
-                    raise SyncTokenValidException
+            if revision < minRevision:
+                raise SyncTokenValidException
 
         # call sharedChildResourceNamesSinceRevision() and filter results
         sharedChildChanged, sharedChildDeleted = yield self.sharedChildResourceNamesSinceRevision(revision, "infinity")
@@ -2345,7 +2344,7 @@
         return Delete(
             aboMembers,
             Where=(aboMembers.GROUP_ID.In(Parameter("groupIDs", len(groupIDs)))).And(
-                aboMembers.GROUP_ID.In(Parameter("memberIDs", len(memberIDs)))).And(
+                aboMembers.MEMBER_ID.In(Parameter("memberIDs", len(memberIDs)))).And(
                     aboMembers.REVISION < Parameter("revision")
                 )
         )

Modified: CalendarServer/branches/users/gaya/cleanrevisions/txdav/common/datastore/sql.py
===================================================================
--- CalendarServer/branches/users/gaya/cleanrevisions/txdav/common/datastore/sql.py	2013-12-21 00:38:11 UTC (rev 12181)
+++ CalendarServer/branches/users/gaya/cleanrevisions/txdav/common/datastore/sql.py	2013-12-21 04:32:24 UTC (rev 12182)
@@ -2035,15 +2035,14 @@
         deleted = set()
         if revision:
             cs = schema.CALENDARSERVER
-            minRevisionRows = yield Select(
+            minRevision = int((yield Select(
                 [cs.VALUE],
                 From=cs,
                 Where=(cs.NAME == "MIN-REVISION")
-            ).on(self._txn)
+            ).on(self._txn))[0][0])
 
-            if minRevisionRows:
-                if revision < int(minRevisionRows[0][0]):
-                    raise SyncTokenValidException
+            if revision < minRevision:
+                raise SyncTokenValidException
 
             results = [
                 (
@@ -2479,15 +2478,14 @@
         deleted = []
         if revision:
             cs = schema.CALENDARSERVER
-            minRevisionRows = yield Select(
+            minRevision = int((yield Select(
                 [cs.VALUE],
                 From=cs,
                 Where=(cs.NAME == "MIN-REVISION")
-            ).on(self._txn)
+            ).on(self._txn))[0][0])
 
-            if minRevisionRows:
-                if revision < int(minRevisionRows[0][0]):
-                    raise SyncTokenValidException
+            if revision < minRevision:
+                raise SyncTokenValidException
 
             results = [
                 (name if name else "", removed) for name, removed in
@@ -6207,3 +6205,66 @@
         # obscure bug.
     else:
         yield t.commit()
+
+
+
+ at inlineCallbacks
+def deleteRevisionsBefore(txn, minRevision):
+    """
+    Delete revisions before minRevision
+    """
+    # Delete old revisions
+    for table in (
+        schema.CALENDAR_OBJECT_REVISIONS,
+        schema.NOTIFICATION_OBJECT_REVISIONS,
+        schema.ADDRESSBOOK_OBJECT_REVISIONS,
+    ):
+        yield Delete(
+            From=table,
+            Where=(table.REVISION < minRevision)
+        ).on(txn)
+
+    # get groups where this object was once a member and version info
+    aboMembers = schema.ABO_MEMBERS
+    groupRows = yield Select([aboMembers.GROUP_ID, aboMembers.MEMBER_ID, aboMembers.DELETED, aboMembers.REVISION],
+        From=aboMembers,
+    ).on(txn)
+
+    # group results by group, member, and revisionInfo
+    groupIDToMemberIDMap = {}
+    for groupRow in groupRows:
+        groupID, memberID, deleted, revision = groupRow
+        revisionInfo = [deleted, revision]
+        if groupID not in groupIDToMemberIDMap:
+            groupIDToMemberIDMap[groupID] = {}
+        memberIDToRevisionsMap = groupIDToMemberIDMap[groupID]
+        if memberID not in memberIDToRevisionsMap:
+            memberIDToRevisionsMap[memberID] = []
+        revisionInfoList = memberIDToRevisionsMap[memberID]
+        revisionInfoList.append(revisionInfo)
+
+    # go though list an delete old revisions, leaving at least one undeleted member
+    for groupID, memberIDToRevisionsMap in groupIDToMemberIDMap.iteritems():
+        for memberID, revisionInfoList in memberIDToRevisionsMap.iteritems():
+
+            revisionsToRemove = []
+            maxRevisionInfoToRemove = None
+            revisionsToSave = []
+            for revisionInfo in revisionInfoList:
+                deleted, revision = revisionInfo
+                if revision < minRevision:
+                    revisionsToRemove.append(revision)
+                    if not maxRevisionInfoToRemove or revision > maxRevisionInfoToRemove[1]:
+                        maxRevisionInfoToRemove = revisionInfo
+                else:
+                    revisionsToSave.append(revision)
+
+            if revisionsToRemove and (revisionsToSave or maxRevisionInfoToRemove[0]):
+                aboMembers = schema.ABO_MEMBERS
+                yield Delete(
+                    aboMembers,
+                    Where=(aboMembers.GROUP_ID == groupID).And(
+                        aboMembers.MEMBER_ID == memberID).And(
+                            aboMembers.REVISION.In(revisionsToRemove)
+                        )
+                )

Modified: CalendarServer/branches/users/gaya/cleanrevisions/txdav/common/datastore/sql_schema/current-oracle-dialect.sql
===================================================================
--- CalendarServer/branches/users/gaya/cleanrevisions/txdav/common/datastore/sql_schema/current-oracle-dialect.sql	2013-12-21 00:38:11 UTC (rev 12181)
+++ CalendarServer/branches/users/gaya/cleanrevisions/txdav/common/datastore/sql_schema/current-oracle-dialect.sql	2013-12-21 04:32:24 UTC (rev 12182)
@@ -372,6 +372,11 @@
     "RESOURCE_ID" integer not null references CALENDAR_OBJECT on delete cascade
 );
 
+create table REVISION_CLEANUP_WORK (
+    "WORK_ID" integer primary key not null,
+    "NOT_BEFORE" timestamp default CURRENT_TIMESTAMP at time zone 'UTC'
+);
+
 create table CALENDARSERVER (
     "NAME" nvarchar2(255) primary key,
     "VALUE" nvarchar2(255)

Modified: CalendarServer/branches/users/gaya/cleanrevisions/txdav/common/datastore/sql_schema/current.sql
===================================================================
--- CalendarServer/branches/users/gaya/cleanrevisions/txdav/common/datastore/sql_schema/current.sql	2013-12-21 00:38:11 UTC (rev 12181)
+++ CalendarServer/branches/users/gaya/cleanrevisions/txdav/common/datastore/sql_schema/current.sql	2013-12-21 04:32:24 UTC (rev 12182)
@@ -706,6 +706,15 @@
 create index CALENDAR_OBJECT_SPLITTER_WORK_RESOURCE_ID on
 	CALENDAR_OBJECT_SPLITTER_WORK(RESOURCE_ID);
 
+---------------------------
+-- Revision Cleaner Work --
+---------------------------
+
+create table REVISION_CLEANUP_WORK (
+  WORK_ID                       integer      primary key default nextval('WORKITEM_SEQ') not null, -- implicit index
+  NOT_BEFORE                    timestamp    default timezone('UTC', CURRENT_TIMESTAMP)
+);
+
 --------------------
 -- Schema Version --
 --------------------

Modified: CalendarServer/branches/users/gaya/cleanrevisions/txdav/common/datastore/sql_schema/upgrades/oracle-dialect/upgrade_from_30_to_31.sql
===================================================================
--- CalendarServer/branches/users/gaya/cleanrevisions/txdav/common/datastore/sql_schema/upgrades/oracle-dialect/upgrade_from_30_to_31.sql	2013-12-21 00:38:11 UTC (rev 12181)
+++ CalendarServer/branches/users/gaya/cleanrevisions/txdav/common/datastore/sql_schema/upgrades/oracle-dialect/upgrade_from_30_to_31.sql	2013-12-21 04:32:24 UTC (rev 12182)
@@ -29,7 +29,13 @@
 alter table NOTIFICATION_OBJECT_REVISIONS
  add ("MODIFIED" timestamp default CURRENT_TIMESTAMP at time zone 'UTC');
 
+ -- Add cleanup work table --
  
+create table REVISION_CLEANUP_WORK (
+  "WORK_ID" integer primary key not null,
+  "NOT_BEFORE" timestamp default CURRENT_TIMESTAMP at time zone 'UTC'
+);
+ 
 -- Update version --
 
 update CALENDARSERVER set VALUE = '31' where NAME = 'VERSION';

Modified: CalendarServer/branches/users/gaya/cleanrevisions/txdav/common/datastore/sql_schema/upgrades/postgres-dialect/upgrade_from_30_to_31.sql
===================================================================
--- CalendarServer/branches/users/gaya/cleanrevisions/txdav/common/datastore/sql_schema/upgrades/postgres-dialect/upgrade_from_30_to_31.sql	2013-12-21 00:38:11 UTC (rev 12181)
+++ CalendarServer/branches/users/gaya/cleanrevisions/txdav/common/datastore/sql_schema/upgrades/postgres-dialect/upgrade_from_30_to_31.sql	2013-12-21 04:32:24 UTC (rev 12182)
@@ -29,6 +29,12 @@
 alter table NOTIFICATION_OBJECT_REVISIONS
   add column MODIFIED timestamp default timezone('UTC', CURRENT_TIMESTAMP);
 
+ -- Add cleanup work table --
+ 
+create table REVISION_CLEANUP_WORK (
+  "WORK_ID" integer primary key not null,
+  "NOT_BEFORE" timestamp default CURRENT_TIMESTAMP at time zone 'UTC'
+);
   
 -- Update version --
 

Added: CalendarServer/branches/users/gaya/cleanrevisions/txdav/common/datastore/work/__init__.py
===================================================================
--- CalendarServer/branches/users/gaya/cleanrevisions/txdav/common/datastore/work/__init__.py	                        (rev 0)
+++ CalendarServer/branches/users/gaya/cleanrevisions/txdav/common/datastore/work/__init__.py	2013-12-21 04:32:24 UTC (rev 12182)
@@ -0,0 +1,19 @@
+##
+# Copyright (c) 2005-2013 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+
+"""
+Common work items
+"""

Added: CalendarServer/branches/users/gaya/cleanrevisions/txdav/common/datastore/work/revision_cleanup.py
===================================================================
--- CalendarServer/branches/users/gaya/cleanrevisions/txdav/common/datastore/work/revision_cleanup.py	                        (rev 0)
+++ CalendarServer/branches/users/gaya/cleanrevisions/txdav/common/datastore/work/revision_cleanup.py	2013-12-21 04:32:24 UTC (rev 12182)
@@ -0,0 +1,143 @@
+# -*- test-case-name: twext.who.test.test_groups -*-
+##
+# Copyright (c) 2013 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+
+"""
+Remove old and unused REVISION rows
+"""
+
+from twext.enterprise.dal.record import fromTable
+from twext.enterprise.dal.syntax import Delete, Select, Update, Max
+from twext.enterprise.queue import WorkItem
+from twext.python.log import Logger
+from twisted.internet.defer import inlineCallbacks, returnValue
+from twistedcaldav.config import config
+from txdav.common.datastore.sql import deleteRevisionsBefore
+from txdav.common.datastore.sql_tables import schema
+import datetime
+
+log = Logger()
+
+
+class FindMinRevisionWork(WorkItem,
+    fromTable(schema.REVISION_CLEANUP_WORK)):
+
+    group = "find_min_revision"
+
+    @inlineCallbacks
+    def doWork(self):
+
+        # Delete all other work items
+        yield Delete(From=self.table, Where=None).on(self.transaction)
+
+        # Get the minimum valid revision
+        cs = schema.CALENDARSERVER
+        minRevision = int((yield Select(
+            [cs.VALUE],
+            From=cs,
+            Where=(cs.NAME == "MIN-REVISION")
+        ).on(self.transaction))[0][0])
+
+        # get max revision on table rows before dateLimit
+        dateLimit = (datetime.datetime.utcnow() -
+            datetime.timedelta(days=float(config.SyncTokenLifetimeDays)))
+        maxRevision = 0
+
+        # TODO: Use one Select statement
+        for table in (
+            schema.CALENDAR_OBJECT_REVISIONS,
+            schema.NOTIFICATION_OBJECT_REVISIONS,
+            schema.ADDRESSBOOK_OBJECT_REVISIONS,
+            schema.ABO_MEMBERS,
+        ):
+            revisionRows = yield Select(
+                [Max(table.REVISION)],
+                From=Select(
+                    [table.REVISION],
+                    From=table,
+                    Where=(table.MODIFIED < dateLimit),
+                ).on(self.transaction)
+            )
+            if revisionRows:
+                tableMaxRevision = revisionRows[0][0]
+                if tableMaxRevision > maxRevision:
+                    maxRevison = tableMaxRevision
+
+        if maxRevision > minRevision:
+            # save it
+            cs = schema.CALENDARSERVER
+            yield Update(
+                {cs.VALUE: minRevision},
+                Where=cs.NAME == "MIN-REVISION",
+            ).on(self.transaction)
+
+            # Schedule revision cleanup
+            notBefore = (datetime.datetime.utcnow() +
+                datetime.timedelta(seconds=10))
+            log.debug("Scheduling revision cleanup: %s" % (notBefore,))
+            yield self.transaction.enqueue(RevisionCleanupWork,
+                notBefore=notBefore)
+
+        else:
+            # Schedule next update
+            notBefore = (datetime.datetime.utcnow() +
+                datetime.timedelta(days=float(config.RevisionCleanupPeriodDays)))
+            log.debug("Rescheduling find minimum revision work: %s" % (notBefore,))
+            yield self.transaction.enqueue(FindMinRevisionWork,
+                notBefore=notBefore)
+
+
+
+class RevisionCleanupWork(WorkItem,
+    fromTable(schema.REVISION_CLEANUP_WORK)):
+
+    group = "group_revsion_cleanup"
+
+    @inlineCallbacks
+    def doWork(self):
+
+        # Delete all other work items
+        yield Delete(From=self.table, Where=None).on(self.transaction)
+
+        # Get the minimum valid revision
+        cs = schema.CALENDARSERVER
+        minRevision = int((yield Select(
+            [cs.VALUE],
+            From=cs,
+            Where=(cs.NAME == "MIN-REVISION")
+        ).on(self.transaction))[0][0])
+
+        # delete revisions
+        yield deleteRevisionsBefore(self.transaction, minRevision)
+
+        # Schedule next update
+        notBefore = (datetime.datetime.utcnow() +
+            datetime.timedelta(days=float(config.RevisionCleanupPeriodDays)))
+        log.debug("Rescheduling find minimum revision work: %s" % (notBefore,))
+        yield self.transaction.enqueue(FindMinRevisionWork,
+            notBefore=notBefore)
+
+
+
+ at inlineCallbacks
+def scheduleFirstFindMinRevision(store):
+    txn = store.newTransaction()
+    notBefore = (datetime.datetime.utcnow() +
+        datetime.timedelta(days=float(config.RevisionCleanupPhaseDays)))
+    log.debug("Scheduling find minimum revision work: %s" % (notBefore,))
+    wp = (yield txn.enqueue(FindMinRevisionWork, notBefore=notBefore))
+    yield txn.commit()
+    returnValue(wp)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20140312/8aa963f1/attachment.html>


More information about the calendarserver-changes mailing list