SHA-1966: Deleting a link causes huge memory and cache usage and eventually OOM error

- Replaced XPATH search for links to a canned query
   - Removed the XPATH search for existing nodes with the same name =>catch exception

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@133960 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Ramona Neamtu
2016-12-21 10:04:36 +00:00
parent f371dd270a
commit 5d546877f2
8 changed files with 373 additions and 135 deletions

View File

@@ -229,6 +229,7 @@ Inbound settings from iBatis
<mapper resource="alfresco/ibatis/#resource.dialect#/query-downloads-common-SqlMap.xml"/>
<mapper resource="alfresco/ibatis/#resource.dialect#/query-discussion-common-SqlMap.xml"/>
<mapper resource="alfresco/ibatis/#resource.dialect#/query-archived-nodes-common-SqlMap.xml"/>
<mapper resource="alfresco/ibatis/#resource.dialect#/query-doclink-nodes-common-SqlMap.xml"/>
</mappers>
</configuration>

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="alfresco.query.doclinks">
<!-- GetDoclinkNodes Canned Query -->
<select id="select_GetDoclinkNodesCannedQuery" resultType="long">
select
node_properties.node_id as nodeId
from
alf_node_properties node_properties
where
node_properties.qname_id = #{typeQNameId} and
node_properties.string_value = #{parentNodeStringValue}
</select>
</mapper>

View File

@@ -102,6 +102,9 @@
<property name="checkOutCheckInService" ref="checkOutCheckInService"/>
<property name="policyComponent" ref="policyComponent"/>
<property name="behaviourFilter" ref="policyBehaviourFilter" />
<property name="permissionService" ref="permissionService" />
<property name="cannedQueryDAO" ref="cannedQueryDAO"/>
<property name="qnameDAO" ref="qnameDAO"/>
</bean>
<bean id="mlTranslationInterceptor" class="org.alfresco.repo.model.filefolder.MLTranslationInterceptor" >

View File

@@ -33,28 +33,34 @@ import java.util.Map;
import org.alfresco.model.ApplicationModel;
import org.alfresco.model.ContentModel;
import org.alfresco.query.CannedQuery;
import org.alfresco.query.CannedQueryParameters;
import org.alfresco.query.CannedQueryResults;
import org.alfresco.query.PagingRequest;
import org.alfresco.repo.domain.qname.QNameDAO;
import org.alfresco.repo.domain.query.CannedQueryDAO;
import org.alfresco.repo.node.NodeServicePolicies;
import org.alfresco.repo.node.NodeServicePolicies.BeforeDeleteNodePolicy;
import org.alfresco.repo.policy.BehaviourFilter;
import org.alfresco.repo.policy.JavaBehaviour;
import org.alfresco.repo.policy.PolicyComponent;
import org.alfresco.repo.search.QueryParameterDefImpl;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.security.permissions.AccessDeniedException;
import org.alfresco.repo.site.SiteModel;
import org.alfresco.service.cmr.coci.CheckOutCheckInService;
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.DeleteLinksStatusReport;
import org.alfresco.service.cmr.repository.DocumentLinkService;
import org.alfresco.service.cmr.repository.DuplicateChildNodeNameException;
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.QueryParameterDefinition;
import org.alfresco.service.cmr.search.SearchService;
import org.alfresco.service.cmr.security.AccessStatus;
import org.alfresco.service.cmr.security.PermissionService;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.Pair;
import org.alfresco.util.PropertyCheck;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -71,6 +77,8 @@ public class DocumentLinkServiceImpl implements DocumentLinkService, NodeService
{
private static Log logger = LogFactory.getLog(DocumentLinkServiceImpl.class);
protected static final String CANNED_QUERY_GET_DOC_LINKS = "getDoclinkNodesCannedQueryFactory";
private NodeService nodeService;
private DictionaryService dictionaryService;
private SearchService searchService;
@@ -78,18 +86,17 @@ public class DocumentLinkServiceImpl implements DocumentLinkService, NodeService
private CheckOutCheckInService checkOutCheckInService;
private PolicyComponent policyComponent;
private BehaviourFilter behaviourFilter;
/** Shallow search for nodes with a name pattern */
private static final String XPATH_QUERY_NODE_NAME_MATCH = "./*[like(@cm:name, $cm:name, false)]";
/** Shallow search for links with a destination pattern */
private static final String XPATH_QUERY_LINK_DEST_MATCH = ".//*[like(@cm:destination, $cm:destination, false)]";
private PermissionService permissionService;
private CannedQueryDAO cannedQueryDAO;
private QNameDAO qnameDAO;
private static final String LINK_NODE_EXTENSION = ".url";
/* I18N labels */
private static final String LINK_TO_LABEL = "doclink_service.link_to_label";
private boolean isNodePendingDelete = false;
/**
* The initialise method. Register our policies.
*/
@@ -102,10 +109,13 @@ public class DocumentLinkServiceImpl implements DocumentLinkService, NodeService
PropertyCheck.mandatory(this, "checkOutCheckInService", checkOutCheckInService);
PropertyCheck.mandatory(this, "policyComponent", policyComponent);
PropertyCheck.mandatory(this, "behaviourFilter", behaviourFilter);
PropertyCheck.mandatory(this, "permissionService", permissionService);
PropertyCheck.mandatory(this, "cannedQueryDAO", cannedQueryDAO);
PropertyCheck.mandatory(this, "qnameDAO", qnameDAO);
// Register interest in the beforeDeleteNode policy
//for nodes that have app:linked aspect
// for nodes that have app:linked aspect
policyComponent.bindClassBehaviour(
BeforeDeleteNodePolicy.QNAME,
ApplicationModel.ASPECT_LINKED,
@@ -166,12 +176,6 @@ public class DocumentLinkServiceImpl implements DocumentLinkService, NodeService
QName assocQName = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, QName.createValidLocalName(newName));
// check if the link node already exists
if (checkExists(newName, destination))
{
throw new IllegalArgumentException("A file with the name '" + newName + "' already exists in the destination folder");
}
ChildAssociationRef childRef = null;
QName sourceType = nodeService.getType(source);
@@ -180,6 +184,8 @@ public class DocumentLinkServiceImpl implements DocumentLinkService, NodeService
throw new IllegalArgumentException("Cannot perform operation since the node (id:" + source.getId() + ") is locked.");
}
try
{
if (dictionaryService.isSubClass(sourceType, ContentModel.TYPE_CONTENT))
{
// create File Link node
@@ -195,7 +201,12 @@ public class DocumentLinkServiceImpl implements DocumentLinkService, NodeService
}
else
{
throw new IllegalArgumentException("unsupported source node type : " + nodeService.getType(source));
throw new IllegalArgumentException("Unsupported source node type : " + nodeService.getType(source));
}
}
catch (DuplicateChildNodeNameException ex)
{
throw new IllegalArgumentException("A file with the name '" + newName + "' already exists in the destination folder", ex);
}
// add linked aspect to the sourceNode - run as System
@@ -220,25 +231,6 @@ public class DocumentLinkServiceImpl implements DocumentLinkService, NodeService
return childRef.getChildRef();
}
/**
* Check if node with specified <code>name</code> exists within a
* <code>parent</code> folder.
*
* @param name
* @param parent
* @return
*/
private boolean checkExists(String name, NodeRef parent)
{
QueryParameterDefinition[] params = new QueryParameterDefinition[1];
params[0] = new QueryParameterDefImpl(ContentModel.PROP_NAME, dictionaryService.getDataType(DataTypeDefinition.TEXT), true, name);
// execute the query
List<NodeRef> nodeRefs = searchService.selectNodes(parent, XPATH_QUERY_NODE_NAME_MATCH, params, namespaceService, false);
return (nodeRefs.size() != 0);
}
@Override
public NodeRef getLinkDestination(NodeRef linkNodeRef)
{
@@ -261,27 +253,37 @@ public class DocumentLinkServiceImpl implements DocumentLinkService, NodeService
}
@Override
public List<NodeRef> getNodeLinks(NodeRef nodeRef)
public List<Long> getNodeLinksIds(NodeRef nodeRef)
{
/* Validate input */
PropertyCheck.mandatory(this, "nodeRef", nodeRef);
/* Get all links of the given nodeRef */
QueryParameterDefinition[] params = new QueryParameterDefinition[1];
params[0] = new QueryParameterDefImpl(ContentModel.PROP_LINK_DESTINATION, dictionaryService.getDataType(DataTypeDefinition.NODE_REF), true, nodeRef.toString());
PagingRequest pagingRequest = new PagingRequest(0, 100000);
List<Long> nodeLinks = new ArrayList<Long>();
List<NodeRef> nodeLinks = new ArrayList<NodeRef>();
List<NodeRef> nodeRefs;
/* Search for links in all stores */
for (StoreRef store : nodeService.getStores())
Pair<Long, QName> nameQName = qnameDAO.getQName(ContentModel.PROP_LINK_DESTINATION);
if (nameQName != null)
{
/* Get the root node */
NodeRef rootNodeRef = nodeService.getRootNode(store);
// Execute the canned query if there are links in the database
GetDoclinkNodesCannedQueryParams parameterBean = new GetDoclinkNodesCannedQueryParams(nodeRef.toString(),
nameQName.getFirst(),
pagingRequest.getMaxItems());
CannedQueryParameters params = new CannedQueryParameters(parameterBean,
null,
null,
pagingRequest.getRequestTotalCountMax(),
pagingRequest.getQueryExecutionId());
CannedQuery<Long> query = new GetDoclinkNodesCannedQuery(cannedQueryDAO,
params);
CannedQueryResults<Long> results = query.execute();
/* Execute the query, retrieve links to the document */
nodeRefs = searchService.selectNodes(rootNodeRef, XPATH_QUERY_LINK_DEST_MATCH, params, namespaceService, true);
nodeLinks.addAll(nodeRefs);
for (Long nodeId : results.getPage())
{
nodeLinks.add(nodeId);
}
}
return nodeLinks;
}
@@ -298,29 +300,40 @@ public class DocumentLinkServiceImpl implements DocumentLinkService, NodeService
DeleteLinksStatusReport report = new DeleteLinksStatusReport();
List<NodeRef> linkNodeRefs = getNodeLinks(document);
report.addTotalLinksFoundCount(linkNodeRefs.size());
List<Long> linkNodeIds = getNodeLinksIds(document);
report.addTotalLinksFoundCount(linkNodeIds.size());
for (NodeRef linkRef : linkNodeRefs)
for (Long linkId : linkNodeIds)
{
try
NodeRef linkNodeRef = AuthenticationUtil.runAs(new AuthenticationUtil.RunAsWork<NodeRef>()
{
if (!nodeService.hasAspect(linkRef, ContentModel.ASPECT_PENDING_DELETE))
public NodeRef doWork() throws Exception
{
nodeService.deleteNode(linkRef);
NodeRef nodeRef = nodeService.getNodeRef(linkId);
isNodePendingDelete = nodeService.hasAspect(nodeRef, ContentModel.ASPECT_PENDING_DELETE);
return nodeRef;
}
}, AuthenticationUtil.getSystemUserName());
/* if the node was successfully deleted increment the count */
if (!isNodePendingDelete)
{
if (permissionService.hasPermission(linkNodeRef, PermissionService.DELETE_NODE) == AccessStatus.DENIED)
{
report.addErrorDetail(linkNodeRef,
new AccessDeniedException("User '" + AuthenticationUtil.getFullyAuthenticatedUser() + "' doesn't have permission to create discussion on node '" + linkNodeRef + "'"));
}
else
{
nodeService.deleteNode(linkNodeRef);
// if the node was successfully deleted increment the count
report.incrementDeletedLinksCount();
}
}
catch (AccessDeniedException ex)
{
/* if the node could not be deleted add it to the report */
report.addErrorDetail(linkRef, ex);
}
}
// remove also the aspect app:linked
// remove also the aspect app:linked if all links were deleted with success
if (report.getTotalLinksFoundCount() == report.getDeletedLinksCount())
{
behaviourFilter.disableBehaviour(document, ContentModel.ASPECT_AUDITABLE);
behaviourFilter.disableBehaviour(document, ContentModel.ASPECT_LOCKABLE);
try
@@ -332,6 +345,7 @@ public class DocumentLinkServiceImpl implements DocumentLinkService, NodeService
behaviourFilter.enableBehaviour(document, ContentModel.ASPECT_AUDITABLE);
behaviourFilter.enableBehaviour(document, ContentModel.ASPECT_LOCKABLE);
}
}
return report;
}
@@ -355,9 +369,11 @@ public class DocumentLinkServiceImpl implements DocumentLinkService, NodeService
{
final NodeRef nodeRef = getLinkDestination(linkNodeRef);
List<NodeRef> nodeRefLinks = getNodeLinks(nodeRef);
List<Long> nodeRefLinks = getNodeLinksIds(nodeRef);
if (nodeRefLinks.size() == 1 && nodeRefLinks.contains(linkNodeRef))
long linkNodeId = (Long) nodeService.getProperty(linkNodeRef, ContentModel.PROP_NODE_DBID);
if (nodeRefLinks.size() == 1 && nodeRefLinks.contains(linkNodeId))
{
AuthenticationUtil.runAs(new AuthenticationUtil.RunAsWork<Object>()
{
@@ -415,4 +431,20 @@ public class DocumentLinkServiceImpl implements DocumentLinkService, NodeService
{
this.behaviourFilter = behaviourFilter;
}
public void setPermissionService(PermissionService permissionService)
{
this.permissionService = permissionService;
}
public void setCannedQueryDAO(CannedQueryDAO cannedQueryDAO)
{
this.cannedQueryDAO = cannedQueryDAO;
}
public void setQnameDAO(QNameDAO qnameDAO)
{
this.qnameDAO = qnameDAO;
}
}

View File

@@ -0,0 +1,104 @@
/*
* #%L
* Alfresco Repository
* %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.repo.doclink;
import java.util.Collections;
import java.util.List;
import org.alfresco.query.AbstractCannedQuery;
import org.alfresco.query.CannedQueryParameters;
import org.alfresco.repo.domain.qname.QNameDAO;
import org.alfresco.repo.domain.query.CannedQueryDAO;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Parameter object for {@link GetDoclinkNodesCannedQuery}.
*
* @author Ramona Popa
* @since 5.2.1
*/
public class GetDoclinkNodesCannedQuery extends AbstractCannedQuery<Long>
{
private Log logger = LogFactory.getLog(GetDoclinkNodesCannedQuery.class);
private static final String QUERY_NAMESPACE = "alfresco.query.doclinks";
private static final String QUERY_SELECT_GET_DOCLINK_NODES = "select_GetDoclinkNodesCannedQuery";
private CannedQueryDAO cannedQueryDAO;
public GetDoclinkNodesCannedQuery(CannedQueryDAO cannedQueryDAO, CannedQueryParameters params)
{
super(params);
this.cannedQueryDAO = cannedQueryDAO;
}
@Override
protected List<Long> queryAndFilter(CannedQueryParameters parameters)
{
Long start = (logger.isDebugEnabled() ? System.currentTimeMillis() : null);
Object paramBeanObj = parameters.getParameterBean();
if (paramBeanObj == null)
{
throw new NullPointerException("Null GetDoclinkNodes query params");
}
// Get parameters
GetDoclinkNodesCannedQueryParams paramBean = (GetDoclinkNodesCannedQueryParams) paramBeanObj;
if (paramBean.getParentNodeStringValue() == null)
{
return Collections.emptyList();
}
List<Long> nodeIds = cannedQueryDAO.executeQuery(QUERY_NAMESPACE, QUERY_SELECT_GET_DOCLINK_NODES, paramBean, 0, paramBean.getLimit());
// preload the node for later when we want to get the properties of the node
if (logger.isDebugEnabled())
{
if (start != null)
{
logger.debug("Base query: " + nodeIds.size() + " in " + (System.currentTimeMillis() - start) + " msecs");
}
if (paramBean.getLimit() == nodeIds.size())
{
logger.debug("Node " + paramBean.getParentNodeStringValue()+ " has at least 100,000 link nodes attached. Results have been truncated.");
}
}
return nodeIds;
}
@Override
protected boolean isApplyPostQuerySorting()
{
// No post-query sorting
return false;
}
}

View File

@@ -0,0 +1,79 @@
/*
* #%L
* Alfresco Repository
* %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.repo.doclink;
/**
* Parameter object for {@link GetDoclinkNodesCannedQuery}.
*
* @author Ramona Popa
* @since 5.2.1
*/
public class GetDoclinkNodesCannedQueryParams
{
private String parentNodeStringValue;
private Long typeQNameId;
private int limit;
public GetDoclinkNodesCannedQueryParams(String parentNodeStringValue, Long typeQNameId, int limit)
{
this.setParentNodeStringValue(parentNodeStringValue);
this.setTypeQNameId(typeQNameId);
this.setLimit(limit);
}
public String getParentNodeStringValue()
{
return parentNodeStringValue;
}
public void setParentNodeStringValue(String parentNodeStringValue)
{
this.parentNodeStringValue = parentNodeStringValue;
}
public Long getTypeQNameId()
{
return typeQNameId;
}
public void setTypeQNameId(Long typeQNameId)
{
this.typeQNameId = typeQNameId;
}
public int getLimit()
{
return limit;
}
public void setLimit(int limit)
{
this.limit = limit;
}
}

View File

@@ -57,12 +57,12 @@ public interface DocumentLinkService
public NodeRef getLinkDestination(NodeRef linkNodeRef);
/**
* Returns the associated links for a node, from all stores
* Returns the associated link ids for a node, from all stores
*
* @param nodeRef
* @return A list of link nodeRefs for given node
* @return A list of link nodeIds for given node
*/
public List<NodeRef> getNodeLinks(NodeRef nodeRef);
public List<Long> getNodeLinksIds(NodeRef nodeRef);
/**
* Deletes all links having the provided node as destination.

View File

@@ -329,10 +329,12 @@ public class DocumentLinkServiceImplTest extends TestCase
}, TEST_USER);
// check if the service found 2 links of the document
assertEquals(report.getTotalLinksFoundCount(), 2);
assertEquals(2, report.getTotalLinksFoundCount());
// check if the service successfully deleted one
assertEquals(report.getDeletedLinksCount(), 1);
assertEquals(1, report.getDeletedLinksCount());
assertEquals(true, nodeService.hasAspect(site1File2, ApplicationModel.ASPECT_LINKED));
// check if the second one failed with access denied
Throwable ex = report.getErrorDetails().get(linkOfFile1Site2);