1 17 18 package org.objectweb.jac.aspects.distribution; 19 20 21 import java.util.*; 22 import org.aopalliance.intercept.ConstructorInvocation; 23 import org.aopalliance.intercept.MethodInvocation; 24 import org.apache.log4j.Logger; 25 import org.objectweb.jac.core.*; 26 import org.objectweb.jac.core.dist.*; 27 import org.objectweb.jac.util.Log; 28 29 37 38 public class LoadBalancingAC 39 extends AspectComponent 40 implements LoadBalancingConf 41 { 42 static Logger logger = Logger.getLogger("loadbalancing"); 43 44 public void addRoundTripLoadBalancer( 45 String wrappeeName, 46 String methods, 47 String hostName, 48 String replicaExpr) { 49 50 pointcut( 51 wrappeeName, 52 ".*", 53 methods + " && !CONSTRUCTORS && !STATICS", 54 new LoadBalancingWrapper(this, replicaExpr), 55 hostName, 56 null); 57 } 58 59 public void addRandomLoadBalancer( 60 String wrappeeName, 61 String methods, 62 String hostName, 63 String replicaExpr) { 64 65 pointcut( 66 wrappeeName, 67 ".*", 68 methods + " && !CONSTRUCTORS && !STATICS", 69 new LoadBalancingWrapper(this, replicaExpr), 70 hostName, 71 null); 72 } 73 74 77 78 public class LoadBalancingWrapper extends Wrapper { 79 80 int count = 0; 81 Vector replicas = null; 82 Random random = new Random(); 83 String hostExpr; 84 boolean doFill = true; 85 86 public LoadBalancingWrapper(AspectComponent ac, String hostExpr) { 87 super(ac); 88 this.hostExpr = hostExpr; 89 } 90 91 public void invalidate() { 92 doFill = true; 93 } 94 95 public Object invoke(MethodInvocation invocation) throws Throwable { 96 return roundTripBalance((Interaction) invocation); 97 } 98 99 public Object construct(ConstructorInvocation invocation) 100 throws Throwable { 101 throw new Exception ("This wrapper does not support constructor wrapping"); 102 } 103 104 106 107 public Object roundTripBalance(Interaction interaction) { 108 if (doFill) { 109 replicas = 110 Topology.getPartialTopology(hostExpr).getReplicas( 111 interaction.wrappee); 112 logger.debug("filled partial topo with "+ hostExpr 113 + " on "+ Topology.get() + ": " + replicas); 114 doFill = false; 115 } 116 if (replicas.size() == 0) { 117 doFill = true; 120 logger.warn( 121 "load-balancing: no replica found, on " 122 + interaction.wrappee + ": local call performed"); 123 return proceed(interaction); 124 } 125 if (count >= replicas.size()) { 126 count = 0; 127 } 128 return ((RemoteRef) replicas.get(count++)).invoke( 129 interaction.method.getName(), 130 interaction.args); 131 } 132 133 135 136 public Object randomBalance(Interaction interaction) { 137 if (doFill) { 138 replicas = 139 Topology.getPartialTopology(hostExpr).getReplicas( 140 interaction.wrappee); 141 doFill = false; 142 } 143 if (replicas.size() == 0) { 144 doFill = true; 147 logger.warn("load-balancing: no replica found, local call performed"); 148 return proceed(interaction); 149 } 150 return ( 151 (RemoteRef) replicas.get( 152 random.nextInt(replicas.size()))).invoke( 153 interaction.method.getName(), 154 interaction.args); 155 } 156 } 157 158 } 159 | Popular Tags |