[jboss-cvs] JBossCache/src/org/jboss/cache/buddyreplication ...
Brian Stansberry
brian.stansberry at jboss.com
Mon Oct 23 01:46:39 EDT 2006
User: bstansberry
Date: 06/10/23 01:46:39
Modified: src/org/jboss/cache/buddyreplication
NextMemberBuddyLocator.java BuddyLocator.java
BuddyManager.java
Log:
[JBCACHE-809] Pojo-style configuration for use by Microcontainer
Revision Changes Path
1.6 +132 -16 JBossCache/src/org/jboss/cache/buddyreplication/NextMemberBuddyLocator.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: NextMemberBuddyLocator.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/buddyreplication/NextMemberBuddyLocator.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -b -r1.5 -r1.6
--- NextMemberBuddyLocator.java 11 Oct 2006 11:53:39 -0000 1.5
+++ NextMemberBuddyLocator.java 23 Oct 2006 05:46:39 -0000 1.6
@@ -8,6 +8,9 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.jboss.cache.config.BuddyReplicationConfig;
+import org.jboss.cache.config.Dynamic;
+import org.jboss.cache.config.BuddyReplicationConfig.BuddyLocatorConfig;
import org.jgroups.stack.IpAddress;
import java.net.InetAddress;
@@ -20,8 +23,8 @@
import java.util.Properties;
/**
- * This buddy locator uses a next-in-line algorithm to select buddies for a buddy group. This algorithm
- * allows for the following properties, all of which are optional.
+ * This buddy locator uses a next-in-line algorithm to select buddies for a buddy group.
+ * This algorithm allows for the following properties, all of which are optional.
* <p/>
* <ul>
* <li>More than one buddy per group - the <b>numBuddies</b> property, defaulting to 1 if ommitted.</li>
@@ -37,32 +40,39 @@
{
private Log log = LogFactory.getLog(NextMemberBuddyLocator.class);
- int numBuddies = 1;
- boolean ignoreColocatedBuddies = true;
+ private Config config = new Config();
- public void init(Properties props)
+ public BuddyLocatorConfig getConfig()
{
- if (props != null)
+ return config;
+ }
+
+ public void init(BuddyLocatorConfig buddyLocatorConfig)
{
- String numBuddiesStr = props.getProperty("numBuddies");
- String ignoreColocatedBuddiesStr = props.getProperty("ignoreColocatedBuddies");
- if (numBuddiesStr != null) numBuddies = Integer.parseInt(numBuddiesStr);
- if (ignoreColocatedBuddiesStr != null)
+ if (buddyLocatorConfig instanceof Config)
{
- ignoreColocatedBuddies = Boolean.valueOf(ignoreColocatedBuddiesStr);
+ this.config = (Config) buddyLocatorConfig;
+ }
+ else if (buddyLocatorConfig != null)
+ {
+ this.config = new Config(buddyLocatorConfig);
}
+ else
+ {
+ // We were passed null; just use a default config
+ this.config = new Config();
}
}
public List<IpAddress> locateBuddies(Map<IpAddress, String> buddyPoolMap, List<IpAddress> currentMembership, IpAddress dataOwner)
{
- int numBuddiesToFind = Math.min(numBuddies, currentMembership.size());
+ int numBuddiesToFind = Math.min(config.getNumBuddies(), currentMembership.size());
List<IpAddress> buddies = new ArrayList<IpAddress>(numBuddiesToFind);
// find where we are in the list.
int dataOwnerSubscript = currentMembership.indexOf(dataOwner);
int i = 0;
- boolean ignoreColocatedBuddiesForSession = ignoreColocatedBuddies;
+ boolean ignoreColocatedBuddiesForSession = config.isIgnoreColocatedBuddies();
while (buddies.size() < numBuddiesToFind)
@@ -87,7 +97,7 @@
if (subscript >= currentMembership.size() && buddyPoolMap != null)
{
buddyPoolMap = null;
- ignoreColocatedBuddiesForSession = ignoreColocatedBuddies; // reset this flag
+ ignoreColocatedBuddiesForSession = config.isIgnoreColocatedBuddies(); // reset this flag
i = 0;
if (log.isInfoEnabled())
log.info("Expected to look for " + numBuddiesToFind + " buddies but could only find " + buddies.size() + " suitable candidates - trying again, ignoring buddy pool hints.");
@@ -161,4 +171,110 @@
return false;
}
+
+ /**
+ * Type-specific configuration object for NextMemberBuddyLocator.
+ *
+ */
+ public static class Config extends BuddyLocatorConfig
+ {
+ private static final long serialVersionUID = 2443438867383733851L;
+
+ @Dynamic
+ private int numBuddies = 1;
+ @Dynamic
+ private boolean ignoreColocatedBuddies = true;
+
+ /**
+ * Default constructor.
+ */
+ public Config()
+ {
+ setBuddyLocatorClass(NextMemberBuddyLocator.class.getName());
+ }
+
+ /**
+ * Constructor for use by {@link NextMemberBuddyLocator#init(BuddyLocatorConfig)}.
+ *
+ * @param base the config passed in to <code>init()</code>.
+ */
+ private Config(BuddyReplicationConfig.BuddyLocatorConfig base)
+ {
+ this();
+ setBuddyLocatorProperties(base.getBuddyLocatorProperties());
+ }
+
+ public String getBuddyLocatorClass()
+ {
+ return NextMemberBuddyLocator.class.getName();
+ }
+
+ public void setBuddyLocatorClass(String buddyLocatorClass)
+ {
+ // ignore it
+ }
+
+ public boolean isIgnoreColocatedBuddies()
+ {
+ return ignoreColocatedBuddies;
+ }
+
+ public void setIgnoreColocatedBuddies(boolean ignoreColocatedBuddies)
+ {
+ testImmutability("ignoreColocatedBuddies");
+ this.ignoreColocatedBuddies = ignoreColocatedBuddies;
+ }
+
+ public int getNumBuddies()
+ {
+ return numBuddies;
+ }
+
+ public void setNumBuddies(int numBuddies)
+ {
+ testImmutability("numBuddies");
+ this.numBuddies = numBuddies;
+ }
+
+ public void setBuddyLocatorProperties(Properties props)
+ {
+ super.setBuddyLocatorProperties(props);
+ if (props != null)
+ {
+ String numBuddiesStr = props.getProperty("numBuddies");
+ String ignoreColocatedBuddiesStr = props.getProperty("ignoreColocatedBuddies");
+ if (numBuddiesStr != null) numBuddies = Integer.parseInt(numBuddiesStr);
+ if (ignoreColocatedBuddiesStr != null)
+ {
+ ignoreColocatedBuddies = Boolean.valueOf(ignoreColocatedBuddiesStr);
+ }
+ }
+ }
+
+ public boolean equals(Object obj)
+ {
+ if (this == obj)
+ return true;
+
+ if (obj instanceof Config)
+ {
+ Config other = (Config) obj;
+ return (other.ignoreColocatedBuddies == this.ignoreColocatedBuddies)
+ && (other.numBuddies == this.numBuddies);
+ }
+ return false;
+ }
+
+ public int hashCode()
+ {
+ int result = 13;
+ result = 23 * result + (ignoreColocatedBuddies ? 0 : 1);
+ result = 23 * result + numBuddies;
+ return result;
+ }
+
+
+
+
+ }
}
\ No newline at end of file
1.4 +17 -5 JBossCache/src/org/jboss/cache/buddyreplication/BuddyLocator.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: BuddyLocator.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/buddyreplication/BuddyLocator.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -b -r1.3 -r1.4
--- BuddyLocator.java 11 Oct 2006 11:53:39 -0000 1.3
+++ BuddyLocator.java 23 Oct 2006 05:46:39 -0000 1.4
@@ -6,11 +6,12 @@
*/
package org.jboss.cache.buddyreplication;
+import org.jboss.cache.config.BuddyReplicationConfig;
+import org.jboss.cache.config.BuddyReplicationConfig.BuddyLocatorConfig;
import org.jgroups.stack.IpAddress;
import java.util.List;
import java.util.Map;
-import java.util.Properties;
/**
* Buddy Locators help the {@see BuddyManager} select buddies for its buddy group.
@@ -23,12 +24,23 @@
public interface BuddyLocator
{
/**
- * Initialize this <code>BuddyLocator</code>.
+ * Gets the configuration for this BuddyLocator.
*
- * @param props an implementation-specific set of configuration properties.
- * May be <code>null</code>.
+ * @return object encapsulating this object's configuration.
+ * Should not return <code>null</code>. If {@link #init(BuddyLocatorConfig)}
+ * has not been called or <code>null</code> was passed to it, the
+ * returned value should be the default config for the
+ * given BuddyLocator implementation.
+ */
+ public BuddyLocatorConfig getConfig();
+
+ /**
+ * Initialize this <code>BuddyLocator</code>.
+ * @param config configuration for this <code>BuddyLocator</code>. May be
+ * <code>null</code>, in which case the implementation should
+ * use its default configuration.
*/
- public void init(Properties props);
+ public void init(BuddyReplicationConfig.BuddyLocatorConfig config);
/**
* Choose a set of buddies for the given node. Invoked when a change in
1.46 +44 -80 JBossCache/src/org/jboss/cache/buddyreplication/BuddyManager.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: BuddyManager.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/buddyreplication/BuddyManager.java,v
retrieving revision 1.45
retrieving revision 1.46
diff -u -b -r1.45 -r1.46
--- BuddyManager.java 12 Oct 2006 23:03:58 -0000 1.45
+++ BuddyManager.java 23 Oct 2006 05:46:39 -0000 1.46
@@ -16,19 +16,18 @@
import org.jboss.cache.Fqn;
import org.jboss.cache.Region;
import org.jboss.cache.TreeCache;
+import org.jboss.cache.config.BuddyReplicationConfig;
+import org.jboss.cache.config.BuddyReplicationConfig.BuddyLocatorConfig;
import org.jboss.cache.lock.TimeoutException;
import org.jboss.cache.marshall.MethodCall;
import org.jboss.cache.marshall.MethodCallFactory;
import org.jboss.cache.marshall.MethodDeclarations;
import org.jboss.cache.marshall.VersionAwareMarshaller;
import org.jboss.cache.statetransfer.StateTransferManager;
-import org.jboss.cache.xml.XmlHelper;
import org.jgroups.Address;
import org.jgroups.View;
import org.jgroups.stack.IpAddress;
-import org.w3c.dom.Element;
-import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -36,7 +35,6 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
-import java.util.Properties;
import java.util.Vector;
/**
@@ -49,9 +47,9 @@
private static Log log = LogFactory.getLog(BuddyManager.class);
/**
- * Test whether buddy replication is enabled.
+ * Configuration object.
*/
- private boolean enabled;
+ final BuddyReplicationConfig config;
/**
* Buddy locator class
@@ -72,14 +70,6 @@
* Map of buddy pools received from broadcasts
*/
Map buddyPool;
- /**
- * Name of the buddy pool for current instance. May be null if buddy pooling is not used.
- */
- String buddyPoolName;
-
- boolean autoDataGravitation = true;
- boolean dataGravitationRemoveOnFind = true;
- boolean dataGravitationSearchBackupTrees = true;
/**
* Map of bddy groups the current instance participates in as a backup node.
@@ -106,90 +96,64 @@
public static final String BUDDY_BACKUP_SUBTREE = "_BUDDY_BACKUP_";
public static final Fqn BUDDY_BACKUP_SUBTREE_FQN = Fqn.fromString(BUDDY_BACKUP_SUBTREE);
- int buddyCommunicationTimeout = 10000;
-
/**
* Flag to prevent us receiving and processing remote calls before we've started
*/
private boolean initialised = false;
// private Latch initLatch = new Latch();
- public BuddyManager(Element element)
+ public BuddyManager(BuddyReplicationConfig config)
{
- enabled = XmlHelper.readBooleanContents(element, "buddyReplicationEnabled");
- dataGravitationRemoveOnFind = XmlHelper.readBooleanContents(element, "dataGravitationRemoveOnFind", true);
- dataGravitationSearchBackupTrees = XmlHelper.readBooleanContents(element, "dataGravitationSearchBackupTrees", true);
- autoDataGravitation = enabled && XmlHelper.readBooleanContents(element, "autoDataGravitation", false);
+ this.config = config;
+
+ if (config.getBuddyPoolName() != null)
+ buddyPool = new ConcurrentReaderHashMap();
- String strBuddyCommunicationTimeout = XmlHelper.readStringContents(element, "buddyCommunicationTimeout");
+ BuddyLocatorConfig blc = config.getBuddyLocatorConfig();
try
{
- buddyCommunicationTimeout = Integer.parseInt(strBuddyCommunicationTimeout);
+ // its OK if the buddy locator config is null.
+ buddyLocator = (blc == null) ? createDefaultBuddyLocator() : createBuddyLocator(blc);
}
catch (Exception e)
{
+ log.warn("Caught exception instantiating buddy locator", e);
+ log.error("Unable to instantiate specified buddyLocatorClass [" + blc.getBuddyLocatorClass() + "]. Using default buddyLocator [" + NextMemberBuddyLocator.class.getName() + "] instead, with default properties.");
+ buddyLocator = createDefaultBuddyLocator();
}
- finally
- {
- if (log.isDebugEnabled())
- log.debug("Using buddy communication timeout of " + buddyCommunicationTimeout + " millis");
- }
- buddyPoolName = XmlHelper.readStringContents(element, "buddyPoolName");
- if (buddyPoolName != null && buddyPoolName.equals("")) buddyPoolName = null;
-
- // initialise buddy pool map
- if (buddyPoolName != null) buddyPool = new ConcurrentReaderHashMap();
-
- // now read the buddy locator details and create accordingly.
- String buddyLocatorClass = null;
- Properties buddyLocatorProperties = null;
- try
- {
- buddyLocatorClass = XmlHelper.readStringContents(element, "buddyLocatorClass");
- try
- {
- buddyLocatorProperties = XmlHelper.readPropertiesContents(element, "buddyLocatorProperties");
- }
- catch (IOException e)
- {
- log.warn("Caught exception reading buddyLocatorProperties", e);
- log.error("Unable to read buddyLocatorProperties specified! Using defaults for [" + buddyLocatorClass + "]");
+ // Update the overall config with the BuddyLocatorConfig actually used
+ if (blc != buddyLocator.getConfig())
+ config.setBuddyLocatorConfig(buddyLocator.getConfig());
}
- // its OK if the buddy locator class or properties are null.
- buddyLocator = (buddyLocatorClass == null || buddyLocatorClass.equals("")) ? createDefaultBuddyLocator(buddyLocatorProperties) : createBuddyLocator(buddyLocatorClass, buddyLocatorProperties);
- }
- catch (Exception e)
+ public BuddyReplicationConfig getConfig()
{
- log.warn("Caught exception instantiating buddy locator", e);
- log.error("Unable to instantiate specified buddyLocatorClass [" + buddyLocatorClass + "]. Using default buddyLocator [" + NextMemberBuddyLocator.class.getName() + "] instead, with default properties.");
- buddyLocator = createDefaultBuddyLocator(null);
- }
+ return config;
}
- protected BuddyLocator createBuddyLocator(String className, Properties props) throws ClassNotFoundException, IllegalAccessException, InstantiationException
+ protected BuddyLocator createBuddyLocator(BuddyLocatorConfig config) throws ClassNotFoundException, IllegalAccessException, InstantiationException
{
- BuddyLocator bl = (BuddyLocator) Class.forName(className).newInstance();
- bl.init(props);
+ BuddyLocator bl = (BuddyLocator) Class.forName(config.getBuddyLocatorClass()).newInstance();
+ bl.init(config);
return bl;
}
- protected BuddyLocator createDefaultBuddyLocator(Properties props)
+ protected BuddyLocator createDefaultBuddyLocator()
{
BuddyLocator bl = new NextMemberBuddyLocator();
- bl.init(props);
+ bl.init(null);
return bl;
}
public boolean isEnabled()
{
- return enabled;
+ return config.isEnabled();
}
public String getBuddyPoolName()
{
- return buddyPoolName;
+ return config.getBuddyPoolName();
}
public static String getGroupNameFromAddress(Object address)
@@ -206,9 +170,9 @@
buddyGroup.setDataOwner((IpAddress) cache.getLocalAddress());
buddyGroup.setGroupName(getGroupNameFromAddress(cache.getLocalAddress()));
- if (buddyPoolName != null)
+ if (config.getBuddyPoolName() != null)
{
- buddyPool.put(buddyGroup.getDataOwner(), buddyPoolName);
+ buddyPool.put(buddyGroup.getDataOwner(), config.getBuddyPoolName());
}
// allow waiting threads to process.
@@ -227,7 +191,7 @@
Vector newMembers = newView.getMembers();
// the whole 'oldMembers' concept is only used for buddy pool announcements.
- if (buddyPoolName == null)
+ if (config.getBuddyPoolName() == null)
{
enqueueViewChange(null, newMembers);
}
@@ -248,22 +212,22 @@
public boolean isAutoDataGravitation()
{
- return autoDataGravitation;
+ return config.isAutoDataGravitation();
}
public boolean isDataGravitationRemoveOnFind()
{
- return dataGravitationRemoveOnFind;
+ return config.isDataGravitationRemoveOnFind();
}
public boolean isDataGravitationSearchBackupTrees()
{
- return dataGravitationSearchBackupTrees;
+ return config.isDataGravitationSearchBackupTrees();
}
public int getBuddyCommunicationTimeout()
{
- return buddyCommunicationTimeout;
+ return config.getBuddyCommunicationTimeout();
}
// -------------- methods to be called by the tree cache listener --------------------
@@ -645,9 +609,9 @@
{
// broadcast to other caches
if (log.isDebugEnabled())
- log.debug("Instance " + buddyGroup.getDataOwner() + " broadcasting membership in buddy pool " + buddyPoolName + " to recipients " + recipients);
+ log.debug("Instance " + buddyGroup.getDataOwner() + " broadcasting membership in buddy pool " + config.getBuddyPoolName() + " to recipients " + recipients);
- MethodCall membershipCall = MethodCallFactory.create(MethodDeclarations.remoteAnnounceBuddyPoolNameMethod, buddyGroup.getDataOwner(), buddyPoolName);
+ MethodCall membershipCall = MethodCallFactory.create(MethodDeclarations.remoteAnnounceBuddyPoolNameMethod, buddyGroup.getDataOwner(), config.getBuddyPoolName());
MethodCall replicateCall = MethodCallFactory.create(MethodDeclarations.replicateMethod, membershipCall);
try
@@ -677,7 +641,7 @@
}
}
- cache.callRemoteMethods(recipients, call, true, true, buddyCommunicationTimeout);
+ cache.callRemoteMethods(recipients, call, true, true, config.getBuddyCommunicationTimeout());
}
@@ -803,7 +767,7 @@
// always refresh buddy list.
reassignBuddies(members[1]);
- if (buddyPoolName != null)
+ if (config.getBuddyPoolName() != null)
{
log.trace("Broadcasting pool membership details, triggered by view change.");
broadcastBuddyPoolMembership();
More information about the jboss-cvs-commits
mailing list