Dave Ward 82b9aba09a Merged V4.1-BUG-FIX to HEAD
41597: ALF-1907: Check out rule is active for spaces
    - Merged in fix from DEV
    - Changed List of applicable types to a Set
    - Added quick initial checks against the set before doing the subtype checks
    - Various typo fixes
   41598: Add an additional line of INFO to log the database details
   41599: Fix for ALF-8374 - Simple view: incorrect file type icon for *.page and *.eps files
   41600: Merge DEV to V4.1-BUG-FIX
      41478 : ALF-14078: CLONE - Saving files with apps on Mac OS X Lion in CIFS doesn't invoke rules
   41605: Fix for ALF-14653 - Share - Page link created wrongly in wiki. Solution to split wiki page name and title from Alex Bykov.
   41608: CIFS cluster saving of text file fails. ALF-14179
   The client releases the oplock by closing the file, if a remote oplock break was used then a notification to
   the remote node was missing in this case.
   41616: ALF-13577:modified the delete site REST API to set the status code to 409 if a node is locked.
   41624: MT: ALF-14331 - UserUsageTrackingComponent fails if disabled (in MT config)
   - defensive fix (exact steps do not repeat) - may also fix ALF-15956
   41652: more debug
   41653: Fix for ALF-15965 - IE9 - Script error occurs when manage aspect for a document
   41655: Merged BRANCHES/V4.1 to BRANCHES/DEV/V4.1-BUG-FIX (RECORD ONLY)
      41654: Fix for ALF-15965 - hand merged back from rev 41653
   41658: ALF-14967: Task operations through task-forms now audited
   41664: Reverse-merging change 41616 out of the product due to file formatting irregularities.
   This checkin was a fix for ALF-13577. A subsequent check-in will add the same fix but without the formatting problems.
   41665: ALF-11452: Disabling JBPM now prevents jobexecutor thread from starting and consuming unneeded resources
   41671: Reimplementation of fix for ALF-13577. Slight edit on previous fix.
   It now lets the NodeLockedException out of the Java layer and 'catches' this exception in the JavaScript layer. The API returns a 409 (conflict) if a NodeLockedException makes it to the JavaScript - the previous fix was returning 409 for *any* AlfrescoRuntimeException thrown during a site delete.
   Also this checkin preserves the whitespace/file layout of the preceding version.
   41688: ALF-15867 WikiServiceImplTest failing on SQL Server
      - Switched WikiService_transaction bean over to using RetryingTransactionInterceptor
   41720: ALF-15967: Using START_USER_ID_ instead of "initiator" property to query process instances started by user X to prevent extra joins
   41730: Fix for ALF-15995 NodeRef is missing in log on "Problem converting to Freemarker" error
   41743: ALF-9779	REGRESSION: FTP - Failed to retrieve directory listing for tenant user
   41745: Removed a HomeShareMapper - there were two
   41747: Fix for ALF-15811 SOLR query increases DocBitSet inefficiently
   Part of ALF-14861 SOLR to scale for non-admin users in 100k sites and a subgroup of each of 1000 independent groupings with 1000 subgroups
   41748: Fix for ALF-15950 Solr: throws NPE: org.alfresco.solr.SolrLuceneAnalyser.loadAnalyzer
   - return default analyzer
   41752: Fix for ALF-13634 Re-created category won't show up again on a node in Document Library.
   41779: ALF-11283 - Got the home share mapper up and working after some refactoring.
   41795: Fix for ALF-14361 CMIS object type definition for Document and Folder not spec compliant
   41796: ALF-16008: missing column on clean Alfresco and after upgrade from 3.4.10 (703)
     - Schema-Reference for DB2 referenced a non-existent column.
   41802: Fixed ALF-16029: TransactionCleanupTest throws constraint violations 
    - Fallout from ALF-12358; sys:deleted node types have a mandatory property
    - Before bulk-deleting nodes, we need to delete properties
    - Use overriding DAo for MySQL.  Other DBs can use standard delete with joins
    - NB: Fixed TransactionalCleanupTest to double-check the purge job
   41822: Refactor to both Share Mappers.
   41838: BDE-85: Deploy alfresco-mmt with a proper pom.xml containing dependencies
     Also, refactor Maven deployment to use a macro
   41858: ALF-14444 - CloudSync: Ensure unknown properties when synced to Cloud are ignored properly
   41876: Fix ALF-16067 - Items with cm:versionable aspect [custom aspect added] throw errors in Share UI
   41877: Updated Spring Surf libs (1148) - downgrades module deployment error messages
   41878: ALF-16069 - RU: 'Web Deployed' folder is not localized if Alfresco is installed with Russian locale.
   41879: Fix non-continuous build, trying to call continuous-init from maven-env-prerequisites
   41918: Merged PATCHES/V4.1.1 to V4.1-BUG-FIX
      41657: ALF-15965 - IE9 - Script error occurs when manage aspect for a document (correct 4.1.1 branch this time)
      41834: Merged DEV to V4.1.1 (4.1.1)
         41827: ALF-15983: SPP: Meeting space: all-day event is displayed on the day before specified during creation
         Removing of time zone has been modified to be sensitive to Outlook meeting events which are not requiring special conversion for the start and end dates
      41835: Merged DEV to V4.1.1 (4.1.1)
         41833: ALF-16038 : DB2: Upgrade script needed to remove ALFUSER.ACT_HI_ACTINST.OWNER_
         The patch was created to remove an unnecessary column in DB2.
      41845: Reverse merge: << In attempt to clear 12 new DB2 build errors - don't think it is going to work >> 
         41835: Merged DEV to V4.1.1 (4.1.1)
            41833: ALF-16038 : DB2: Upgrade script needed to remove ALFUSER.ACT_HI_ACTINST.OWNER_
            The patch was created to remove an unnecessary column in DB2.
      Merged V4.1-BUG-FIX (4.1.2) to V4.1.1 (4.1.1)
         41337: Merged V3.4-BUG-FIX (3.4.11) to V4.1-BUG-FIX (4.1.2)
            41336: TransformerDebug: Use debug rather than trace when there are transformers but they are all unavailable.
      41868: Reverse the reverse merge (back to r41835 state used in build #33 given to QA)
         41845: Reverse merge: << In attempt to clear 12 new DB2 build errors - don't think it is going to work >> 
            41835: Merged DEV to V4.1.1 (4.1.1)
               41833: ALF-16038 : DB2: Upgrade script needed to remove ALFUSER.ACT_HI_ACTINST.OWNER_
               The patch was created to remove an unnecessary column in DB2.
         Merged V4.1-BUG-FIX (4.1.2) to V4.1.1 (4.1.1)
            41337: Merged V3.4-BUG-FIX (3.4.11) to V4.1-BUG-FIX (4.1.2)
               41336: TransformerDebug: Use debug rather than trace when there are transformers but they are all unavailable.
      41914: Merged DEV to PATCHES/V4.1.1
         41909: ALF-16078 : DB2: it's impossible to upgrade Alfresco from 3.4.10 to 4.1.1 (build 33).
            The remove-column-activiti.sql was marked as optional.


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@41919 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2012-09-24 18:03:25 +00:00

287 lines
10 KiB
Java

package org.alfresco.repo.node.cleanup;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.cache.SimpleCache;
import org.alfresco.repo.domain.node.NodeDAO;
import org.alfresco.repo.domain.node.Transaction;
import org.alfresco.repo.node.db.DeletedNodeCleanupWorker;
import org.alfresco.repo.transaction.AlfrescoTransactionSupport;
import org.alfresco.repo.transaction.RetryingTransactionHelper;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.search.SearchService;
import org.alfresco.service.cmr.security.MutableAuthenticationService;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.alfresco.service.transaction.TransactionService;
import org.alfresco.util.ApplicationContextHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.extensions.webscripts.GUID;
/**
* @author Derek Hulley
* @since 4.0
*/
public class TransactionCleanupTest
{
private static Log logger = LogFactory.getLog(TransactionCleanupTest.class);
private static ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
private TransactionService transactionService;
private NodeService nodeService;
private SearchService searchService;
private MutableAuthenticationService authenticationService;
private NodeDAO nodeDAO;
private SimpleCache<Serializable, Serializable> nodesCache;
private DeletedNodeCleanupWorker worker;
private NodeRef nodeRef1;
private NodeRef nodeRef2;
private NodeRef nodeRef3;
private NodeRef nodeRef4;
private NodeRef nodeRef5;
private RetryingTransactionHelper helper;
@SuppressWarnings("unchecked")
@Before
public void before()
{
ServiceRegistry serviceRegistry = (ServiceRegistry)ctx.getBean("ServiceRegistry");
NamespaceService namespaceService = serviceRegistry.getNamespaceService();
this.transactionService = serviceRegistry.getTransactionService();
this.authenticationService = (MutableAuthenticationService)ctx.getBean("authenticationService");
this.nodeService = serviceRegistry.getNodeService();
this.searchService = serviceRegistry.getSearchService();
this.nodeDAO = (NodeDAO)ctx.getBean("nodeDAO");
this.nodesCache = (SimpleCache<Serializable, Serializable>) ctx.getBean("node.nodesSharedCache");
this.worker = (DeletedNodeCleanupWorker)ctx.getBean("nodeCleanup.deletedNodeCleanup");
this.worker.setMinPurgeAgeDays(0);
this.helper = transactionService.getRetryingTransactionHelper();
authenticationService.authenticate("admin", "admin".toCharArray());
StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");
NodeRef storeRoot = nodeService.getRootNode(storeRef);
List<NodeRef> nodeRefs = searchService.selectNodes(
storeRoot, "/app:company_home", null, namespaceService, false);
final NodeRef companyHome = nodeRefs.get(0);
RetryingTransactionHelper.RetryingTransactionCallback<NodeRef> createNode = new RetryingTransactionHelper.RetryingTransactionCallback<NodeRef>()
{
@Override
public NodeRef execute() throws Throwable
{
return nodeService.createNode(companyHome, ContentModel.ASSOC_CONTAINS, QName.createQName("test", GUID.generate()), ContentModel.TYPE_CONTENT).getChildRef();
}
};
this.nodeRef1 = helper.doInTransaction(createNode, false, true);
this.nodeRef2 = helper.doInTransaction(createNode, false, true);
this.nodeRef3 = helper.doInTransaction(createNode, false, true);
this.nodeRef4 = helper.doInTransaction(createNode, false, true);
this.nodeRef5 = helper.doInTransaction(createNode, false, true);
}
private Map<NodeRef, List<String>> createTransactions()
{
Map<NodeRef, List<String>> txnIds = new HashMap<NodeRef, List<String>>();
UpdateNode updateNode1 = new UpdateNode(nodeRef1);
UpdateNode updateNode2 = new UpdateNode(nodeRef2);
UpdateNode updateNode3 = new UpdateNode(nodeRef3);
DeleteNode deleteNode4 = new DeleteNode(nodeRef4);
DeleteNode deleteNode5 = new DeleteNode(nodeRef5);
List<String> txnIds1 = new ArrayList<String>();
List<String> txnIds2 = new ArrayList<String>();
List<String> txnIds3 = new ArrayList<String>();
List<String> txnIds4 = new ArrayList<String>();
List<String> txnIds5 = new ArrayList<String>();
txnIds.put(nodeRef1, txnIds1);
txnIds.put(nodeRef2, txnIds2);
txnIds.put(nodeRef3, txnIds3);
txnIds.put(nodeRef4, txnIds4);
txnIds.put(nodeRef5, txnIds5);
for(int i = 0; i < 10; i++)
{
String txnId1 = helper.doInTransaction(updateNode1, false, true);
txnIds1.add(txnId1);
if(i == 0)
{
String txnId2 = helper.doInTransaction(updateNode2, false, true);
txnIds2.add(txnId2);
}
if(i == 1)
{
String txnId3 = helper.doInTransaction(updateNode3, false, true);
txnIds3.add(txnId3);
}
}
String txnId4 = helper.doInTransaction(deleteNode4, false, true);
txnIds4.add(txnId4);
String txnId5 = helper.doInTransaction(deleteNode5, false, true);
txnIds5.add(txnId5);
return txnIds;
}
private boolean containsTransaction(List<Transaction> txns, String txnId)
{
boolean found = false;
for(Transaction tx : txns)
{
if(tx.getChangeTxnId().equals(txnId))
{
found = true;
break;
}
}
return found;
}
@Test
public void testPurgeUnusedTransactions() throws Exception
{
// Execute transactions that update a number of nodes. For nodeRef1, all but the last txn will be unused.
// run the transaction cleaner to clean up any existing unused transactions
worker.doClean();
long start = System.currentTimeMillis();
Long minTxnId = nodeDAO.getMinTxnId();
final Map<NodeRef, List<String>> txnIds = createTransactions();
final List<String> txnIds1 = txnIds.get(nodeRef1);
final List<String> txnIds2 = txnIds.get(nodeRef2);
final List<String> txnIds3 = txnIds.get(nodeRef3);
// Pure delete: final List<String> txnIds4 = txnIds.get(nodeRef4);
// Pure delete: final List<String> txnIds5 = txnIds.get(nodeRef5);
// Double-check that n4 and n5 are present in deleted form
nodesCache.clear();
assertNotNull("Node 4 is deleted but not purged", nodeDAO.getNodeRefStatus(nodeRef4));
assertNotNull("Node 5 is deleted but not purged", nodeDAO.getNodeRefStatus(nodeRef5));
// run the transaction cleaner
worker.setPurgeSize(5); // small purge size
List<String> reports = worker.doClean();
for (String report : reports)
{
logger.debug(report);
}
// Get transactions committed after the test started
List<Transaction> txns = nodeDAO.getTxnsByCommitTimeAscending(Long.valueOf(start), Long.valueOf(Long.MAX_VALUE), Integer.MAX_VALUE, null, false);
List<String> expectedUnusedTxnIds = new ArrayList<String>(10);
expectedUnusedTxnIds.addAll(txnIds1.subList(0, txnIds1.size() - 1));
List<String> expectedUsedTxnIds = new ArrayList<String>(5);
expectedUsedTxnIds.add(txnIds1.get(txnIds1.size() - 1));
expectedUsedTxnIds.addAll(txnIds2);
expectedUsedTxnIds.addAll(txnIds3);
// 4 and 5 should not be in the list because they are deletes
// check that the correct transactions have been purged i.e. all except the last one to update the node
// i.e. in this case, all but the last one in txnIds1
int numFoundUnusedTxnIds = 0;
for(String txnId : expectedUnusedTxnIds)
{
if(!containsTransaction(txns, txnId))
{
numFoundUnusedTxnIds++;
}
else if(txnIds1.contains(txnId))
{
fail("Unused transaction(s) were not purged: " + txnId);
}
}
assertEquals(9, numFoundUnusedTxnIds);
// check that the correct transactions remain i.e. all those in txnIds2, txnIds3, txnIds4 and txnIds5
int numFoundUsedTxnIds = 0;
for(String txnId : expectedUsedTxnIds)
{
if(containsTransaction(txns, txnId))
{
numFoundUsedTxnIds++;
}
}
assertEquals(3, numFoundUsedTxnIds);
List<Long> txnsUnused = nodeDAO.getTxnsUnused(minTxnId, Long.MAX_VALUE, Integer.MAX_VALUE);
assertEquals(0, txnsUnused.size());
// Double-check that n4 and n5 were removed as well
nodesCache.clear();
assertNull("Node 4 was not cleaned up", nodeDAO.getNodeRefStatus(nodeRef4));
assertNull("Node 5 was not cleaned up", nodeDAO.getNodeRefStatus(nodeRef5));
}
@After
public void after()
{
ApplicationContextHelper.closeApplicationContext();
}
private class UpdateNode implements RetryingTransactionCallback<String>
{
private NodeRef nodeRef;
UpdateNode(NodeRef nodeRef)
{
this.nodeRef = nodeRef;
}
@Override
public String execute() throws Throwable
{
nodeService.setProperty(nodeRef, ContentModel.PROP_NAME, GUID.generate());
String txnId = AlfrescoTransactionSupport.getTransactionId();
return txnId;
}
};
private class DeleteNode implements RetryingTransactionCallback<String>
{
private NodeRef nodeRef;
DeleteNode(NodeRef nodeRef)
{
this.nodeRef = nodeRef;
}
@Override
public String execute() throws Throwable
{
nodeService.addAspect(nodeRef, ContentModel.ASPECT_TEMPORARY, null);
nodeService.deleteNode(nodeRef);
String txnId = AlfrescoTransactionSupport.getTransactionId();
return txnId;
}
};
}