When updating tag scopes following system shutdown/restore, be smarter about quickly skipping tag scopes that another (new) thread is currently working on

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@23153 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Nick Burch
2010-10-15 14:54:46 +00:00
parent 1d93411944
commit c47322b28e
2 changed files with 41 additions and 5 deletions

View File

@@ -1082,17 +1082,31 @@ public class TaggingServiceImplTest extends TestCase
assertTrue("Not found in " + pendingScopes, pendingScopes.contains(this.subFolder)); assertTrue("Not found in " + pendingScopes, pendingScopes.contains(this.subFolder));
// Have the Quartz bean fire now
// It won't be able to do anything, as the locks are taken
UpdateTagScopesQuartzJob job = new UpdateTagScopesQuartzJob();
job.execute(actionService, updateTagsAction);
tx = waitForActionExecution(tx);
// Check that things are still pending despite the quartz run
assertEquals(2, updateTagsAction.searchForTagScopesPendingUpdates().size());
pendingScopes = updateTagsAction.searchForTagScopesPendingUpdates();
assertTrue("Not found in " + pendingScopes, pendingScopes.contains(this.folder));
assertTrue("Not found in " + pendingScopes, pendingScopes.contains(this.subFolder));
// Give back our locks, so we can proceed // Give back our locks, so we can proceed
updateTagsAction.unlockTagScope(this.folder, lockF); updateTagsAction.unlockTagScope(this.folder, lockF);
updateTagsAction.unlockTagScope(this.subFolder, lockSF); updateTagsAction.unlockTagScope(this.subFolder, lockSF);
// Fire off the quartz bean // Fire off the quartz bean, this time it can really work
UpdateTagScopesQuartzJob job = new UpdateTagScopesQuartzJob(); job = new UpdateTagScopesQuartzJob();
job.execute(actionService, updateTagsAction); job.execute(actionService, updateTagsAction);
tx = waitForActionExecution(tx);
// Now check again // Now check again - nothing should be pending
assertEquals(0, updateTagsAction.searchForTagScopesPendingUpdates().size()); assertEquals(0, updateTagsAction.searchForTagScopesPendingUpdates().size());
ts1 = this.taggingService.findTagScope(this.folder); ts1 = this.taggingService.findTagScope(this.folder);

View File

@@ -19,6 +19,8 @@
package org.alfresco.repo.tagging; package org.alfresco.repo.tagging;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import org.alfresco.error.AlfrescoRuntimeException; import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.repo.security.authentication.AuthenticationUtil; import org.alfresco.repo.security.authentication.AuthenticationUtil;
@@ -77,9 +79,11 @@ public class UpdateTagScopesQuartzJob implements Job {
{ {
// Process // Process
final ArrayList<NodeRef> tagNodes = new ArrayList<NodeRef>(); final ArrayList<NodeRef> tagNodes = new ArrayList<NodeRef>();
final HashSet<NodeRef> handledTagNodes = new HashSet<NodeRef>();
while(true) while(true)
{ {
// Fetch the list of changes // Fetch a batch of the pending changes
AuthenticationUtil.runAs(new AuthenticationUtil.RunAsWork<Void>() AuthenticationUtil.runAs(new AuthenticationUtil.RunAsWork<Void>()
{ {
public Void doWork() throws Exception public Void doWork() throws Exception
@@ -93,12 +97,27 @@ public class UpdateTagScopesQuartzJob implements Job {
}, AuthenticationUtil.getSystemUserName() }, AuthenticationUtil.getSystemUserName()
); );
// Log what we found // If we're on our 2nd loop round for any of them, then skip them from now on
// (This can happen if another thread is already processing one of them)
Iterator<NodeRef> it = tagNodes.iterator();
while(it.hasNext())
{
NodeRef nodeRef = it.next();
if(handledTagNodes.contains(nodeRef))
{
it.remove();
if(logger.isDebugEnabled())
logger.debug("Tag scope " + nodeRef + " must be being processed by another Thread, not updating it");
}
}
// Log what we found to process
if(logger.isDebugEnabled()) if(logger.isDebugEnabled())
{ {
logger.debug("Checked for tag scopes with pending tag updates, found " + tagNodes); logger.debug("Checked for tag scopes with pending tag updates, found " + tagNodes);
} }
// If we're out of tag scopes, stop!
if(tagNodes.size() == 0) if(tagNodes.size() == 0)
break; break;
@@ -107,6 +126,9 @@ public class UpdateTagScopesQuartzJob implements Job {
Action action = actionService.createAction(UpdateTagScopesActionExecuter.NAME); Action action = actionService.createAction(UpdateTagScopesActionExecuter.NAME);
action.setParameterValue(UpdateTagScopesActionExecuter.PARAM_TAG_SCOPES, (Serializable)tagNodes); action.setParameterValue(UpdateTagScopesActionExecuter.PARAM_TAG_SCOPES, (Serializable)tagNodes);
actionService.executeAction(action, null, false, false); actionService.executeAction(action, null, false, false);
// Record the scopes we've just done
handledTagNodes.addAll(tagNodes);
} }
} }
} }