Writing own Load Balance Policy
mfrost Jun 25, 2004 6:22 AMWe wish to implement a load balance policy that has the following behaviour:
Much like the FirstAvailable (org.jboss.ha.framework.interfaces.FirstAvailable) load balance policy except the the initial elected member is NOT randomly chosen but is well known.
e.g.
Client A will always elect target node A
Client B will always elect target node B
Client C will always elect target node C
In the event that a node dies, its client will elect another target node.
Our initial hope was that the list returned by clusterFamily.getTarget() would always be in the same order. We could then simply use our desired index to get to the well known node. This code is at the end of the posting.
However, it appears that this list is NOT always presented in the same order.
Calling toString() on the objects returned by clusterFamily.getTargets() shows the following:
Target 0:org.jboss.invocation.jrmp.server.JRMPInvoker_Stub[RemoteStub [ref: [endpoint:[172.22.10.58:4645](remote),objID:[12f9ee:fd5b0274f8:-8000, 3]]]]
Clearly we could parse this string to match the correct target to our well-known node (i.e. the endpoint address). This seems fragile.
Is there a better way?
The target objects are of class org.jboss.invocation.jrmp.server.JRMPInvoker_Stub. We notice that class org.jboss.invocation.jrmp.server.JRMPInvokerhas some very useful methods that appear to do the trick e.g. getServerAddress().
How do you use the stub object to invoke these methods?
Many thanks
regards
mark
package uk.co.hemscott.utils.ejb; import java.util.ArrayList; import org.jboss.invocation.Invocation; import org.jboss.ha.framework.interfaces.*; public class StickyFirstAvailableLoadBalancePolicy implements LoadBalancePolicy { protected transient Object electedTarget = null; public void init (HARMIClient father) { // do not use the HARMIClient in this policy } public Object chooseTarget (FamilyClusterInfo clusterFamily) { return chooseTarget(clusterFamily, null); } public Object chooseTarget (FamilyClusterInfo clusterFamily, Invocation routingDecision) { ArrayList targets = clusterFamily.getTargets (); if (targets.size () == 0) return null; if ( (this.electedTarget != null) && targets.contains (this.electedTarget) ) { return this.electedTarget; } else { int desiredClusterIndex = Integer.parseInt(System.getProperty("uk.co.hemscott.utils.ejb.desiredClusterIndex")); int cursor = desiredClusterIndex % targets.size(); this.electedTarget = targets.get(cursor); return this.electedTarget; } } }