User: patriot1burke Date: 01/09/25 15:24:21 Modified: src/main/org/jboss/ha RoundRobin.java LoadBalancePolicy.java HARMIServerImpl.java HARMIResponse.java HARMIClient.java Log: added collocation support to HARMI Revision Changes Path 1.2 +4 -3 jbossmx/src/main/org/jboss/ha/RoundRobin.java Index: RoundRobin.java =================================================================== RCS file: /cvsroot/jboss/jbossmx/src/main/org/jboss/ha/RoundRobin.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- RoundRobin.java 2001/09/14 03:53:29 1.1 +++ RoundRobin.java 2001/09/25 22:24:21 1.2 @@ -1,12 +1,13 @@ package org.jboss.ha; +import java.util.ArrayList; public class RoundRobin implements LoadBalancePolicy { protected transient int cursorRemote = 0; - public Object chooseTarget(Object[] targets) + public Object chooseTarget(ArrayList targets) { - cursorRemote = ( (cursorRemote + 1) % targets.length ); - return targets[cursorRemote]; + cursorRemote = ( (cursorRemote + 1) % targets.size() ); + return targets.get(cursorRemote); } } 1.2 +2 -1 jbossmx/src/main/org/jboss/ha/LoadBalancePolicy.java Index: LoadBalancePolicy.java =================================================================== RCS file: /cvsroot/jboss/jbossmx/src/main/org/jboss/ha/LoadBalancePolicy.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- LoadBalancePolicy.java 2001/09/14 03:53:29 1.1 +++ LoadBalancePolicy.java 2001/09/25 22:24:21 1.2 @@ -1,6 +1,7 @@ package org.jboss.ha; +import java.util.ArrayList; public interface LoadBalancePolicy extends java.io.Serializable { - public Object chooseTarget(Object[] targets); + public Object chooseTarget(ArrayList targets); } 1.5 +28 -25 jbossmx/src/main/org/jboss/ha/HARMIServerImpl.java Index: HARMIServerImpl.java =================================================================== RCS file: /cvsroot/jboss/jbossmx/src/main/org/jboss/ha/HARMIServerImpl.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- HARMIServerImpl.java 2001/09/25 00:45:34 1.4 +++ HARMIServerImpl.java 2001/09/25 22:24:21 1.5 @@ -3,6 +3,7 @@ import java.util.Vector; import java.util.ArrayList; import java.util.HashMap; +import java.util.Hashtable; import java.util.Iterator; import java.util.Collection; import java.rmi.Remote; @@ -25,12 +26,11 @@ public class HARMIServerImpl implements HARMIServer, - DistributedReplicantManager.ReplicantListener, - java.lang.reflect.InvocationHandler + DistributedReplicantManager.ReplicantListener { protected String replicantName; protected long lastSet = System.currentTimeMillis(); - protected ArrayList replicants; + protected ArrayList replicants = new ArrayList(); protected Object handler; protected HashMap invokerMap = new HashMap (); protected HAPartition partition = null; @@ -38,8 +38,10 @@ protected HARMIClient client; protected RemoteStub rmistub; protected Object stub; - protected Object proxy; + protected String key; + public static Hashtable rmiServers = new Hashtable(); + public HARMIServerImpl (HAPartition partition, String replicantName, Class intf, Object handler, LoadBalancePolicy policy) throws Exception { this.replicantName = replicantName; @@ -52,17 +54,20 @@ invokerMap.put (new Long (RemoteMethodInvocation.calculateHash (methods[i])), methods[i]); this.rmistub = UnicastRemoteObject.exportObject (this); - this.client = new HARMIClient(new Object[0], policy); + this.key = partition.getPartitionName() + "/" + replicantName; + this.client = new HARMIClient(replicants, policy, key, handler); this.stub = Proxy.newProxyInstance( intf.getClassLoader(), new Class[] { intf }, this.client); - this.proxy = Proxy.newProxyInstance( - intf.getClassLoader(), - new Class[] { intf }, - this); partition.getDistributedReplicantManager ().registerListener (replicantName, this); partition.getDistributedReplicantManager ().add (replicantName, rmistub); + rmiServers.put(key, this); + } + + public ArrayList getReplicants() + { + return replicants; } public Object getHAStub() @@ -78,6 +83,7 @@ this.partition.getDistributedReplicantManager ().remove (this.replicantName); UnicastRemoteObject.unexportObject (this, true); + rmiServers.remove(key); } catch (Exception e) {e.printStackTrace ();} } @@ -86,12 +92,22 @@ { return lastSet; } + + public Object getLocal() + { + return handler; + } public void replicantsChanged (String key, ArrayList newReplicants) { log.info("replicantsChanged" + key + " to " + newReplicants.size()); - replicants = newReplicants; - client.setTargets(replicants.toArray()); + synchronized(replicants) + { + // client has reference to replicants so it will automatically get + // updated + replicants.clear(); + replicants.addAll(newReplicants); + } lastSet = System.currentTimeMillis (); } @@ -106,7 +122,7 @@ HARMIResponse rsp = new HARMIResponse (); if (tag < lastSet) { - rsp.newReplicants = replicants.toArray (); + rsp.newReplicants = new ArrayList(replicants); rsp.tag = lastSet; } @@ -127,17 +143,4 @@ } } - public Object invoke(Object proxy, Method method, Object[] args) throws Throwable - { - try - { - log.info("collocated call"); - return method.invoke(handler, args); - } - catch (InvocationTargetException ite) - { - throw ite.getTargetException (); - } - } - } 1.2 +2 -2 jbossmx/src/main/org/jboss/ha/HARMIResponse.java Index: HARMIResponse.java =================================================================== RCS file: /cvsroot/jboss/jbossmx/src/main/org/jboss/ha/HARMIResponse.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- HARMIResponse.java 2001/09/20 07:57:41 1.1 +++ HARMIResponse.java 2001/09/25 22:24:21 1.2 @@ -1,8 +1,8 @@ package org.jboss.ha; - +import java.util.ArrayList; public class HARMIResponse implements java.io.Serializable { - public Object[] newReplicants = null; + public ArrayList newReplicants = null; public long tag = 0; public Object response = null; } 1.2 +80 -21 jbossmx/src/main/org/jboss/ha/HARMIClient.java Index: HARMIClient.java =================================================================== RCS file: /cvsroot/jboss/jbossmx/src/main/org/jboss/ha/HARMIClient.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- HARMIClient.java 2001/09/20 07:57:41 1.1 +++ HARMIClient.java 2001/09/25 22:24:21 1.2 @@ -18,14 +18,14 @@ import java.lang.reflect.InvocationTargetException; import org.jboss.ejb.plugins.jrmp.interfaces.RemoteMethodInvocation; import java.rmi.MarshalledObject; +import java.io.IOException; - /** * * * @author <a href="mailto:[EMAIL PROTECTED]">Sacha Labourey</a> * @author <a href="mailto:[EMAIL PROTECTED]">Bill Burke</a> - * @version $Revision: 1.1 $ + * @version $Revision: 1.2 $ * * <p><b>Revisions:</b> * @@ -37,28 +37,50 @@ public class HARMIClient implements java.io.Serializable, java.lang.reflect.InvocationHandler { - - protected Object[] targets = new Object[0]; + protected String key = null; + protected ArrayList targets = null; protected LoadBalancePolicy loadBalancePolicy; - protected long tag = 0; + protected transient long tag = 0; + protected transient Object local = null; - public HARMIClient(Object[] targets, - LoadBalancePolicy policy) + public HARMIClient() {} + + public HARMIClient(ArrayList targets, + LoadBalancePolicy policy, + String key) + { + this.targets = targets; + this.loadBalancePolicy = policy; + this.key = key; + } + + public HARMIClient(ArrayList targets, + LoadBalancePolicy policy, + String key, + Object local) { this.targets = targets; this.loadBalancePolicy = policy; + this.key = key; + this.local = local; } - public Object[] getTargets() + public boolean isLocal() { + return local != null; + } + + public ArrayList getTargets() + { return targets; } - public void setTargets(Object[] newTargets) + public void setTargets(ArrayList newTargets) { synchronized(targets) { - targets = newTargets; + targets.clear(); + targets.addAll(newTargets); } } @@ -74,8 +96,8 @@ public Object getRemoteTarget() { - System.out.println("number of targets: " + targets.length); - if (targets.length == 0) + System.out.println("number of targets: " + targets.size()); + if (targets.size() == 0) { return null; } @@ -98,18 +120,12 @@ synchronized (targets) { //System.out.println("removeDeadTarget has been called"); - int length = targets.length; + int length = targets.size(); for (int i=0; i<length; ++i) { - if (targets[i] == target) + if (targets.get(i) == target) { - Object[] copy = new Object[length - 1]; - System.arraycopy(targets, 0, copy, 0, i); - if ( (i+1) < length) - { - System.arraycopy(targets, i+1, copy, i, length - i - 1); - } - targets = copy; + targets.remove(i); return; } } @@ -120,6 +136,25 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + if (local != null) + { + try + { + System.out.println("collocated call"); + return method.invoke(local, args); + } + catch (InvocationTargetException ite) + { + throw ite.getTargetException(); + } + } + else + { + return invokeRemote(null, method, args); + } + } + public Object invokeRemote(Object proxy, Method method, Object[] args) throws Throwable + { HARMIServer target = (HARMIServer)getRemoteTarget(); while (target != null) { @@ -158,5 +193,29 @@ } + private void readObject(java.io.ObjectInputStream stream) + throws IOException, ClassNotFoundException + { + key = (String)stream.readUTF(); + targets = (ArrayList)stream.readObject(); + loadBalancePolicy = (LoadBalancePolicy)stream.readObject(); + HARMIServerImpl server = (HARMIServerImpl)HARMIServerImpl.rmiServers.get(key); + if (server != null) + { + synchronized(targets) + { + targets = server.getReplicants(); + local = server.getLocal(); + } + } + } + private void writeObject(java.io.ObjectOutputStream stream) + throws IOException + { + stream.writeUTF(key); + stream.writeObject(targets); + stream.writeObject(loadBalancePolicy); + } + } _______________________________________________ Jboss-development mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/jboss-development