You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@manifoldcf.apache.org by kw...@apache.org on 2013/12/11 15:27:41 UTC
svn commit: r1550150 - in
/manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager:
ZooKeeperConnection.java ZooKeeperLockManager.java
Author: kwright
Date: Wed Dec 11 14:27:40 2013
New Revision: 1550150
URL: http://svn.apache.org/r1550150
Log:
Rework a couple of things in the zookeeper implementation; this is in conjunction with CONNECTORS-835
Modified:
manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/ZooKeeperConnection.java
manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/ZooKeeperLockManager.java
Modified: manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/ZooKeeperConnection.java
URL: http://svn.apache.org/viewvc/manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/ZooKeeperConnection.java?rev=1550150&r1=1550149&r2=1550150&view=diff
==============================================================================
--- manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/ZooKeeperConnection.java (original)
+++ manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/ZooKeeperConnection.java Wed Dec 11 14:27:40 2013
@@ -109,17 +109,7 @@ public class ZooKeeperConnection
public byte[] getNodeData(String nodePath)
throws ManifoldCFException, InterruptedException
{
- try
- {
- Stat s = zookeeper.exists(nodePath,false);
- if (s == null)
- return null;
- return zookeeper.getData(nodePath,false,s);
- }
- catch (KeeperException e)
- {
- throw new ManifoldCFException(e.getMessage(),e);
- }
+ return readData(nodePath);
}
/** Set node data.
@@ -127,14 +117,7 @@ public class ZooKeeperConnection
public void setNodeData(String nodePath, byte[] data)
throws ManifoldCFException, InterruptedException
{
- try
- {
- zookeeper.setData(nodePath, data, -1);
- }
- catch (KeeperException e)
- {
- throw new ManifoldCFException(e.getMessage(),e);
- }
+ writeData(nodePath, data);
}
/** Delete a node.
@@ -408,10 +391,11 @@ public class ZooKeeperConnection
{
try
{
- Stat s = zookeeper.exists(resourcePath,false);
- if (s == null)
- return null;
- return zookeeper.getData(resourcePath,null,s);
+ return zookeeper.getData(resourcePath,false,null);
+ }
+ catch (KeeperException.NoNodeException e)
+ {
+ return null;
}
catch (KeeperException e)
{
@@ -436,13 +420,25 @@ public class ZooKeeperConnection
}
else
{
- try
- {
- zookeeper.create(resourcePath, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
- }
- catch (KeeperException.NodeExistsException e)
+ while (true)
{
- zookeeper.setData(resourcePath, data, -1);
+ try
+ {
+ zookeeper.setData(resourcePath, data, -1);
+ break;
+ }
+ catch (KeeperException.NoNodeException e)
+ {
+ try
+ {
+ zookeeper.create(resourcePath, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+ break;
+ }
+ catch (KeeperException.NodeExistsException e2)
+ {
+ continue;
+ }
+ }
}
}
}
Modified: manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/ZooKeeperLockManager.java
URL: http://svn.apache.org/viewvc/manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/ZooKeeperLockManager.java?rev=1550150&r1=1550149&r2=1550150&view=diff
==============================================================================
--- manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/ZooKeeperLockManager.java (original)
+++ manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/ZooKeeperLockManager.java Wed Dec 11 14:27:40 2013
@@ -43,11 +43,11 @@ public class ZooKeeperLockManager extend
private final static String SERVICETYPE_LOCK_PATH_PREFIX = "/org.apache.manifoldcf.servicelock-";
private final static String SERVICETYPE_ACTIVE_PATH_PREFIX = "/org.apache.manifoldcf.serviceactive-";
private final static String SERVICETYPE_REGISTER_PATH_PREFIX = "/org.apache.manifoldcf.service-";
+ /** Anonymous global variable name prefix, to be followed by the service type */
+ private final static String SERVICETYPE_ANONYMOUS_COUNTER_PREFIX = "/org.apache.manifoldcf.serviceanon-";
/** Anonymous service name prefix, to be followed by an integer */
protected final static String anonymousServiceNamePrefix = "_ANON_";
- /** Anonymous global variable name prefix, to be followed by the service type */
- protected final static String anonymousServiceTypeCounter = "_SERVICECOUNTER_";
// ZooKeeper connection pool
protected static Integer connectionPoolLock = new Integer(0);
@@ -150,7 +150,7 @@ public class ZooKeeperLockManager extend
try
{
if (serviceName == null)
- serviceName = constructUniqueServiceName(serviceType);
+ serviceName = constructUniqueServiceName(connection, serviceType);
String activePath = buildServiceTypeActivePath(serviceType, serviceName);
if (connection.checkNodeExists(activePath))
@@ -563,12 +563,12 @@ public class ZooKeeperLockManager extend
/** Construct a unique service name given the service type.
*/
- protected String constructUniqueServiceName(String serviceType)
- throws ManifoldCFException
+ protected String constructUniqueServiceName(ZooKeeperConnection connection, String serviceType)
+ throws ManifoldCFException, InterruptedException
{
String serviceCounterName = makeServiceCounterName(serviceType);
- int serviceUID = readServiceCounter(serviceCounterName);
- writeServiceCounter(serviceCounterName,serviceUID+1);
+ int serviceUID = readServiceCounter(connection, serviceCounterName);
+ writeServiceCounter(connection, serviceCounterName,serviceUID+1);
return anonymousServiceNamePrefix + serviceUID;
}
@@ -576,34 +576,39 @@ public class ZooKeeperLockManager extend
*/
protected static String makeServiceCounterName(String serviceType)
{
- return anonymousServiceTypeCounter + serviceType;
+ return SERVICETYPE_ANONYMOUS_COUNTER_PREFIX + serviceType;
}
/** Read service counter.
*/
- protected int readServiceCounter(String serviceCounterName)
- throws ManifoldCFException
+ protected int readServiceCounter(ZooKeeperConnection connection, String serviceCounterName)
+ throws ManifoldCFException, InterruptedException
{
- byte[] serviceCounterData = readData(serviceCounterName);
+ int rval;
+ byte[] serviceCounterData = connection.readData(serviceCounterName);
if (serviceCounterData == null || serviceCounterData.length != 4)
- return 0;
- return ((int)serviceCounterData[0]) & 0xff +
- (((int)serviceCounterData[1]) << 8) & 0xff00 +
- (((int)serviceCounterData[2]) << 16) & 0xff0000 +
- (((int)serviceCounterData[3]) << 24) & 0xff000000;
+ rval = 0;
+ else
+ rval = ((int)serviceCounterData[0]) & 0xff +
+ (((int)serviceCounterData[1]) << 8) & 0xff00 +
+ (((int)serviceCounterData[2]) << 16) & 0xff0000 +
+ (((int)serviceCounterData[3]) << 24) & 0xff000000;
+ System.out.println("Read service counter '"+serviceCounterName+"'; value = "+rval);
+ return rval;
}
/** Write service counter.
*/
- protected void writeServiceCounter(String serviceCounterName, int counter)
- throws ManifoldCFException
+ protected void writeServiceCounter(ZooKeeperConnection connection, String serviceCounterName, int counter)
+ throws ManifoldCFException, InterruptedException
{
byte[] serviceCounterData = new byte[4];
serviceCounterData[0] = (byte)(counter & 0xff);
serviceCounterData[1] = (byte)((counter >> 8) & 0xff);
serviceCounterData[2] = (byte)((counter >> 16) & 0xff);
serviceCounterData[3] = (byte)((counter >> 24) & 0xff);
- writeData(serviceCounterName,serviceCounterData);
+ connection.writeData(serviceCounterName,serviceCounterData);
+ System.out.println("Wrote service counter '"+serviceCounterName+"'; value = "+counter);
}
/** Build a zk path for the lock for a specific service type.