KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > distribution > LoadBalancingAC


1 /*
2   Copyright (C) 2001-2002 Renaud Pawlak <renaud@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

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 /**
30  * This Aspect Component allows the programmer to easily implement
31  * load-balancing features for its application when JAC is running in
32  * distributed mode.
33  *
34  * @see LoadBalancingConf
35  * @author Renaud Pawlak
36  */

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 JavaDoc wrappeeName,
46         String JavaDoc methods,
47         String JavaDoc hostName,
48         String JavaDoc 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 JavaDoc wrappeeName,
61         String JavaDoc methods,
62         String JavaDoc hostName,
63         String JavaDoc replicaExpr) {
64
65         pointcut(
66             wrappeeName,
67             ".*",
68             methods + " && !CONSTRUCTORS && !STATICS",
69             new LoadBalancingWrapper(this, replicaExpr),
70             hostName,
71             null);
72     }
73
74     /**
75      * This inner-wrapper handles the load-balancing wrapping methods that
76      * actually implement the load-balancing algorithms. */

77
78     public class LoadBalancingWrapper extends Wrapper {
79
80         int count = 0;
81         Vector replicas = null;
82         Random random = new Random();
83         String JavaDoc hostExpr;
84         boolean doFill = true;
85
86         public LoadBalancingWrapper(AspectComponent ac, String JavaDoc hostExpr) {
87             super(ac);
88             this.hostExpr = hostExpr;
89         }
90
91         public void invalidate() {
92             doFill = true;
93         }
94
95         public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
96             return roundTripBalance((Interaction) invocation);
97         }
98
99         public Object JavaDoc construct(ConstructorInvocation invocation)
100             throws Throwable JavaDoc {
101             throw new Exception JavaDoc("This wrapper does not support constructor wrapping");
102         }
103
104         /**
105          * Performs a round-trip load-balancing. */

106
107         public Object JavaDoc 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                 // none replicas where found, we perform a local call and
118
// will try to get them again on the next call
119
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         /**
134          * Performs a random load-balancing. */

135
136         public Object JavaDoc 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                 // none replicas where found, we perform a local call and
145
// will try to get them again on the next call
146
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