Part of MOB-436: Cluster-wide Locking Service: Implement DB Changes and Service

- Unit test not checked in as it requires some schema change work that has been done manually
 - Moved activities DAO code and renamed data services to DAOs (as per iBatis and Wikipedia, etc)
 - DAO code should now go into org.alfresco.repo.domain...
 - DAO components are bean:xyzDAO and class:XyzDAO
 - Entity beans are XyzEntity, etc

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@13922 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2009-04-09 15:27:19 +00:00
parent 7205e4b956
commit 1fcdfc0a5d
33 changed files with 1136 additions and 226 deletions

View File

@@ -0,0 +1,255 @@
/*
* Copyright (C) 2005-2009 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program 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 General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception. You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.repo.domain.locks;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import org.alfresco.repo.domain.QNameDAO;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.EqualsHelper;
import org.springframework.dao.ConcurrencyFailureException;
/**
* Abstract implementation of the Locks DAO.
*
* @author Derek Hulley
* @since 3.2
*/
public abstract class AbstractLockDAOImpl implements LockDAO
{
private QNameDAO qnameDAO;
/**
* @return Returns the DAO for namespace ID resolution
*/
protected QNameDAO getQNameDAO()
{
return qnameDAO;
}
/**
* @param qnameDAO DAO for namespace ID resolution
*/
public void setQnameDAO(QNameDAO qnameDAO)
{
this.qnameDAO = qnameDAO;
}
public boolean getLock(QName lockQName, String lockApplicant, long timeToLive)
{
String qnameNamespaceUri = lockQName.getNamespaceURI();
String qnameLocalName = lockQName.getLocalName();
// Force lower case for case insensitivity
if (!qnameLocalName.toLowerCase().equals(qnameLocalName))
{
lockQName = QName.createQName(qnameNamespaceUri, qnameLocalName.toLowerCase());
qnameLocalName = lockQName.getLocalName();
}
// Force the lock applicant to lowercase
lockApplicant = lockApplicant.toLowerCase();
// Resolve the namespace
Long qnameNamespaceId = qnameDAO.getOrCreateNamespace(qnameNamespaceUri).getFirst();
// Get the lock resource for the exclusive lock.
// All the locks that are created will need the exclusive case.
LockResourceEntity exclusiveLockResource = getLockResource(qnameNamespaceId, qnameLocalName);
if (exclusiveLockResource == null)
{
// Create it
exclusiveLockResource = createLockResource(qnameNamespaceId, qnameLocalName);
}
Long requiredExclusiveLockResourceId = exclusiveLockResource.getId();
// Split the lock name
List<QName> lockQNames = splitLockQName(lockQName);
List<Long> requiredLockResourceIds = new ArrayList<Long>(lockQNames.size());
// Create the lock resources
for (QName lockQNameIter : lockQNames)
{
String localname = lockQNameIter.getLocalName();
// Get the basic lock resource, forcing a create
// TODO: Pull back all lock resources in a single query
LockResourceEntity lockResource = getLockResource(qnameNamespaceId, localname);
if (lockResource == null)
{
// Create it
lockResource = createLockResource(qnameNamespaceId, localname);
}
requiredLockResourceIds.add(lockResource.getId());
}
// Now, get all locks for the resources we will need
List<LockEntity> existingLocks = getLocks(requiredLockResourceIds);
Map<LockEntity, LockEntity> existingLocksMap = new HashMap<LockEntity, LockEntity>();
// Check them and make sure they don't prevent locks
for (LockEntity existingLock : existingLocks)
{
boolean canTakeLock = canTakeLock(existingLock, lockApplicant, requiredExclusiveLockResourceId);
if (!canTakeLock)
{
return false;
}
existingLocksMap.put(existingLock, existingLock);
}
// Take the locks for the resources.
// Existing locks must be updated, if required.
for (Long requiredLockResourceId : requiredLockResourceIds)
{
LockEntity requiredLock = new LockEntity();
requiredLock.setSharedResourceId(requiredLockResourceId);
requiredLock.setExclusiveResourceId(requiredExclusiveLockResourceId);
// Does it exist?
if (existingLocksMap.containsKey(requiredLock))
{
requiredLock = existingLocksMap.get(requiredLock);
// Do an update
throw new UnsupportedOperationException();
}
else
{
// Create it
requiredLock = createLock(
requiredLockResourceId,
requiredExclusiveLockResourceId,
lockApplicant,
timeToLive);
}
}
return true;
}
private boolean canTakeLock(LockEntity existingLock, String lockApplicant, Long desiredExclusiveLock)
{
if (EqualsHelper.nullSafeEquals(existingLock.getLockHolder(), lockApplicant))
{
// The lock applicant to be is also the current lock holder.
// Regardless of lock expiry, the lock can be taken
return true;
}
else if (existingLock.hasExpired())
{
// Expired locks are fair game
return true;
}
else if (existingLock.isExclusive())
{
// It's a valid, exclusive lock held by someone else ...
return false;
}
else if (desiredExclusiveLock.equals(existingLock.getSharedResourceId()))
{
// We can't take an exclusive lock if a shared lock is active
return false;
}
else
{
// Good to go
return true;
}
}
/**
* Override to get the unique, lock resource entity if one exists.
*
* @param qnameNamespaceId the namespace entity ID
* @param qnameLocalName the lock localname
* @return Returns the lock resource entity,
* or <tt>null</tt> if it doesn't exist
*/
protected abstract LockResourceEntity getLockResource(Long qnameNamespaceId, String qnameLocalName);
/**
* Create a unique lock resource
*
* @param qnameNamespaceId the namespace entity ID
* @param qnameLocalName the lock localname
* @return Returns the newly created lock resource entity
*/
protected abstract LockResourceEntity createLockResource(Long qnameNamespaceId, String qnameLocalName);
/**
* Get any existing lock data for the resources required. The locks returned are not filtered and
* may be expired.
*
* @param lockResourceIds a list of resource IDs for which to retrieve the current locks
* @return Returns a list of locks (expired or not) for the given lock resources
*/
protected abstract List<LockEntity> getLocks(List<Long> lockResourceIds);
/**
* Create a new lock.
* @param sharedResourceId the specific resource to lock
* @param exclusiveResourceId the exclusive lock that is being sought
* @param lockApplicant the ID of the lock applicant
* @param timeToLive the time, in milliseconds, for the lock to remain valid
* @return Returns the new lock
* @throws ConcurrencyFailureException if the lock was already taken at the time of creation
*/
protected abstract LockEntity createLock(
Long sharedResourceId,
Long exclusiveResourceId,
String lockApplicant,
long timeToLive);
/**
* Split a lock's qualified name into the component parts using the '.' (period) as a
* separator on the localname. The namespace is preserved. The provided qualified
* name will always be the last component in the returned list.
*
* @param lockQName the lock name to split into it's higher-level paths
* @return Returns the namespace ID along with the ordered localnames
*/
protected List<QName> splitLockQName(QName lockQName)
{
String ns = lockQName.getNamespaceURI();
String name = lockQName.getLocalName();
StringTokenizer tokenizer = new StringTokenizer(name, ".");
List<QName> ret = new ArrayList<QName>(tokenizer.countTokens());
StringBuilder sb = new StringBuilder();
// Fill it
boolean first = true;
while (tokenizer.hasMoreTokens())
{
if (first)
{
first = false;
}
else
{
sb.append(".");
}
sb.append(tokenizer.nextToken());
QName parentLockQName = QName.createQName(ns, sb.toString());
ret.add(parentLockQName);
}
// Done
return ret;
}
}

View File

@@ -0,0 +1,48 @@
/*
* Copyright (C) 2005-2009 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program 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 General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception. You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.repo.domain.locks;
import org.alfresco.service.namespace.QName;
/**
* DAO services for <b>alf_lock</b> and related tables
*
* @author Derek Hulley
* @since 3.2
*/
public interface LockDAO
{
/**
* Aquire a given exclusive lock, assigning it (and any implicitly shared locks) a
* timeout. All shared locks are implicitly taken as well.
*
* @param lockQName the unique name of the lock to acquire
* @param lockApplicant the potential lock holder's identifier (max 36 chars)
* @param timeToLive the time (in milliseconds) that the lock must remain
* @return Returns <tt>true</tt> if the lock was taken,
* otherwise <tt>false</tt>
*/
boolean getLock(QName lockQName, String lockApplicant, long timeToLive);
}

View File

@@ -0,0 +1,99 @@
/*
* Copyright (C) 2005-2009 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program 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 General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception. You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.repo.domain.locks;
import org.alfresco.service.namespace.QName;
/**
* Class to contain details regarding a lock. A lock is specific to a given
* qualified name. For any given lock, there may exist an <b>EXCLUSIVE</b>
* lock <u>or</u> several <b>SHARED</b> locks.
*
* @author Derek Hulley
* @since 3.2
*/
public class LockDetails
{
/**
* The type of lock
*
* @author Derek Hulley
* @since 3.2
*/
public enum LockType
{
/**
* A lock held by a specific transaction. No other (exclusive or shared) locks
* may be held for the same qualified name.
*/
EXCLUSIVE,
/**
* A lock that may be held by several transactions when no exclusive lock is held
* for the same qualified name.
*/
SHARED;
}
private final String txnId;
private final QName lockQName;
private final LockType lockType;
/**
*
* @param txnId the transaction holding the lock
* @param lockQName the qualified name of the lock
* @param lockType the type of lock
*/
public LockDetails(String txnId, QName lockQName, LockType lockType)
{
this.txnId = txnId;
this.lockQName = lockQName;
this.lockType = lockType;
}
/**
* @return Returns the transaction holding the lock
*/
public String getTxnId()
{
return txnId;
}
/**
* @return Returns the qualified name of the lock
*/
public QName getLockQName()
{
return lockQName;
}
/**
* @return Returns the lock type
*/
public LockType getLockType()
{
return lockType;
}
}

View File

@@ -0,0 +1,185 @@
/*
* Copyright (C) 2005-2009 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program 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 General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception. You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.repo.domain.locks;
import org.alfresco.util.EqualsHelper;
/**
* Entity bean for <b>alf_lock</b> table.
* <p>
* These are unique (see {@link #equals(Object) equals} and {@link #hashCode() hashCode}) based
* on the shared and exclusive resource ID combination.
*
* @author Derek Hulley
* @since 3.2
*/
public class LockEntity
{
private Long id;
private Long version;
private Long sharedResourceId;
private Long exclusiveResourceId;
private String lockHolder;
private Long startTime;
private Long expiryTime = Long.MAX_VALUE; // TODO:
@Override
public int hashCode()
{
return (sharedResourceId == null ? 0 : sharedResourceId.hashCode()) +
(exclusiveResourceId == null ? 0 : exclusiveResourceId.hashCode());
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
{
return true;
}
else if (obj instanceof LockEntity)
{
LockEntity that = (LockEntity) obj;
return EqualsHelper.nullSafeEquals(this.sharedResourceId, that.sharedResourceId) &&
EqualsHelper.nullSafeEquals(this.exclusiveResourceId, that.exclusiveResourceId);
}
else
{
return false;
}
}
/**
* Determine if the lock is logically exclusive. A lock is <b>exclusive</b> if the
* shared lock resource matches the exclusive lock resource.
*
* @return Returns <tt>true</tt> if the lock is exclusive or <tt>false<tt> if it is not
*/
public boolean isExclusive()
{
if (sharedResourceId == null || exclusiveResourceId == null)
{
throw new IllegalStateException("LockEntity has not been populated");
}
return sharedResourceId.equals(exclusiveResourceId);
}
public boolean hasExpired()
{
return System.currentTimeMillis() > expiryTime;
}
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id = id;
}
public Long getVersion()
{
return version;
}
public void setVersion(Long version)
{
this.version = version;
}
public void incrementVersion()
{
this.version = new Long(version.longValue() + 1L);
}
/**
* @return Returns the ID of the shared lock resource
*/
public Long getSharedResourceId()
{
return sharedResourceId;
}
/**
*
* @param sharedResourceId the ID of the shared lock resource
*/
public void setSharedResourceId(Long sharedResourceId)
{
this.sharedResourceId = sharedResourceId;
}
public Long getExclusiveResourceId()
{
return exclusiveResourceId;
}
public void setExclusiveResourceId(Long exclusiveResourceId)
{
this.exclusiveResourceId = exclusiveResourceId;
}
/**
* @return Returns the ID of the lock holder
*/
public String getLockHolder()
{
return lockHolder;
}
/**
* @param lockHolder the ID of the lock holder
*/
public void setLockHolder(String lockHolder)
{
this.lockHolder = lockHolder;
}
/**
*
* @return Returns the time when the lock was started
*/
public Long getStartTime()
{
return startTime;
}
public void setStartTime(Long startTime)
{
this.startTime = startTime;
}
public Long getExpiryTime()
{
return expiryTime;
}
public void setExpiryTime(Long expiryTime)
{
this.expiryTime = expiryTime;
}
}

View File

@@ -0,0 +1,91 @@
/*
* Copyright (C) 2005-2009 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program 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 General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception. You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.repo.domain.locks;
/**
* Entity bean for <b>alf_lock_resource</b> table.
*
* @author Derek Hulley
* @since 3.2
*/
public class LockResourceEntity
{
private Long id;
private Long version;
private Long qnameNamespaceId;
private String qnameLocalName;
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id = id;
}
public Long getVersion()
{
return version;
}
public void setVersion(Long version)
{
this.version = version;
}
/**
* @return Returns the ID of the namespace that the lock belongs to
*/
public Long getQnameNamespaceId()
{
return qnameNamespaceId;
}
/**
* @param namespaceId the ID of the namespace that the lock belongs to
*/
public void setQnameNamespaceId(Long namespaceId)
{
this.qnameNamespaceId = namespaceId;
}
/**
* @return Returns the lock qualified name localname
*/
public String getQnameLocalName()
{
return qnameLocalName;
}
/**
* @param qnameLocalName the lock qualified name localname
*/
public void setQnameLocalName(String qnameLocalName)
{
this.qnameLocalName = qnameLocalName;
}
}

View File

@@ -0,0 +1,109 @@
/*
* Copyright (C) 2005-2009 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program 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 General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception. You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.repo.domain.locks.ibatis;
import java.util.List;
import org.alfresco.repo.domain.locks.AbstractLockDAOImpl;
import org.alfresco.repo.domain.locks.LockEntity;
import org.alfresco.repo.domain.locks.LockResourceEntity;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
/**
* iBatis-specific implementation of the Locks DAO.
*
* @author Derek Hulley
* @since 3.2
*/
public class LockDAOImpl extends AbstractLockDAOImpl
{
private static final Long CONST_LONG_ZERO = new Long(0L);
private static final String SELECT_LOCKRESOURCE_BY_QNAME = "select.LockResourceByQName";
private static final String SELECT_LOCK_BY_SHARED_IDS = "select.LockBySharedIds";
private static final String INSERT_LOCKRESOURCE = "insert.LockResource";
private static final String INSERT_LOCK = "insert.Lock";
private SqlMapClientTemplate template;
public void setSqlMapClientTemplate(SqlMapClientTemplate sqlMapClientTemplate)
{
this.template = sqlMapClientTemplate;
}
@Override
protected LockResourceEntity getLockResource(Long qnameNamespaceId, String qnameLocalName)
{
LockResourceEntity lockResource = new LockResourceEntity();
lockResource.setQnameNamespaceId(qnameNamespaceId);
lockResource.setQnameLocalName(qnameLocalName);
lockResource = (LockResourceEntity) template.queryForObject(SELECT_LOCKRESOURCE_BY_QNAME, lockResource);
// Could be null
return lockResource;
}
@Override
protected LockResourceEntity createLockResource(Long qnameNamespaceId, String qnameLocalName)
{
LockResourceEntity lockResource = new LockResourceEntity();
lockResource.setVersion(CONST_LONG_ZERO);
lockResource.setQnameNamespaceId(qnameNamespaceId);
lockResource.setQnameLocalName(qnameLocalName);
Long id = (Long) template.insert(INSERT_LOCKRESOURCE, lockResource);
lockResource.setId(id);
// Done
return lockResource;
}
@SuppressWarnings("unchecked")
@Override
protected List<LockEntity> getLocks(List<Long> lockResourceIds)
{
List<LockEntity> locks = template.queryForList(SELECT_LOCK_BY_SHARED_IDS, lockResourceIds);
// Done
return locks;
}
@Override
protected LockEntity createLock(
Long sharedResourceId,
Long exclusiveResourceId,
String lockApplicant,
long timeToLive)
{
LockEntity lock = new LockEntity();
lock.setVersion(CONST_LONG_ZERO);
lock.setSharedResourceId(sharedResourceId);
lock.setExclusiveResourceId(exclusiveResourceId);
lock.setLockHolder(lockApplicant);
long now = System.currentTimeMillis();
long exp = now + timeToLive;
lock.setStartTime(now);
lock.setExpiryTime(exp);
Long id = (Long) template.insert(INSERT_LOCK, lock);
lock.setId(id);
// Done
return lock;
}
}