KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > controller > loadbalancer > raidb2 > RAIDb2_WRR


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): Julie Marguerite.
23  */

24
25 package org.objectweb.cjdbc.controller.loadbalancer.raidb2;
26
27 import java.sql.SQLException JavaDoc;
28 import java.util.HashMap JavaDoc;
29
30 import org.objectweb.cjdbc.common.exceptions.NotImplementedException;
31 import org.objectweb.cjdbc.common.sql.SelectRequest;
32 import org.objectweb.cjdbc.common.sql.StoredProcedure;
33 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags;
34 import org.objectweb.cjdbc.controller.cache.metadata.MetadataCache;
35 import org.objectweb.cjdbc.controller.loadbalancer.WeightedBalancer;
36 import org.objectweb.cjdbc.controller.loadbalancer.policies.WaitForCompletionPolicy;
37 import org.objectweb.cjdbc.controller.loadbalancer.policies.createtable.CreateTablePolicy;
38 import org.objectweb.cjdbc.controller.virtualdatabase.ControllerResultSet;
39 import org.objectweb.cjdbc.controller.virtualdatabase.VirtualDatabase;
40
41 /**
42  * RAIDb-2 Weighted Round Robin load balancer.
43  * <p>
44  * The read requests coming from the request manager are sent to the backend
45  * nodes using a weighted round robin. Write requests are broadcasted to all
46  * backends.
47  *
48  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
49  * @author <a HREF="mailto:Julie.Marguerite@inria.fr">Julie Marguerite </a>
50  * @version 1.0
51  */

52 public class RAIDb2_WRR extends RAIDb2
53 {
54   /*
55    * How the code is organized ? 1. Member variables 2. Constructor(s) 3.
56    * Request handling 4. Debug/Monitoring
57    */

58
59   private HashMap JavaDoc weights;
60
61   /*
62    * Constructors
63    */

64
65   /**
66    * Creates a new RAIDb-2 Weighted Round Robin request load balancer.
67    *
68    * @param vdb The virtual database this load balancer belongs to.
69    * @param waitForCompletionPolicy How many backends must complete before
70    * returning the result?
71    * @param createTablePolicy The policy defining how 'create table' statements
72    * should be handled
73    * @exception Exception if an error occurs
74    */

75   public RAIDb2_WRR(VirtualDatabase vdb,
76       WaitForCompletionPolicy waitForCompletionPolicy,
77       CreateTablePolicy createTablePolicy) throws Exception JavaDoc
78   {
79     super(vdb, waitForCompletionPolicy, createTablePolicy);
80   }
81
82   /*
83    * Request Handling
84    */

85
86   /**
87    * Performs a read request. It is up to the implementation to choose to which
88    * backend node(s) this request should be sent.
89    *
90    * @param request an <code>SelectRequest</code>
91    * @param metadataCache cached metadata to use to construct the result set
92    * @return the corresponding <code>java.sql.ResultSet</code>
93    * @exception SQLException if an error occurs
94    * @see org.objectweb.cjdbc.controller.loadbalancer.raidb2.RAIDb2#execReadRequest(SelectRequest,
95    * MetadataCache)
96    */

97   public ControllerResultSet execReadRequest(SelectRequest request,
98       MetadataCache metadataCache) throws SQLException JavaDoc
99   {
100     throw new NotImplementedException(this.getClass().getName()
101         + ":execReadRequest");
102   }
103
104   /**
105    * Chooses the node to execute the stored procedure using a round-robin
106    * algorithm. If the next node has not the needed stored procedure, we try the
107    * next one and so on until a suitable backend is found.
108    *
109    * @see org.objectweb.cjdbc.controller.loadbalancer.AbstractLoadBalancer#execReadOnlyReadStoredProcedure(StoredProcedure,
110    * MetadataCache)
111    */

112   public ControllerResultSet execReadOnlyReadStoredProcedure(
113       StoredProcedure proc, MetadataCache metadataCache) throws SQLException JavaDoc
114   {
115     throw new NotImplementedException(this.getClass().getName()
116         + ":execReadStoredProcedure");
117   }
118
119   /*
120    * Backends management
121    */

122
123   /**
124    * @see org.objectweb.cjdbc.controller.loadbalancer.AbstractLoadBalancer#setWeight(String,
125    * int)
126    */

127   public void setWeight(String JavaDoc name, int w) throws SQLException JavaDoc
128   {
129     throw new SQLException JavaDoc("Weight is not supported with this load balancer");
130   }
131
132   /*
133    * Debug/Monitoring
134    */

135
136   /**
137    * Gets information about the request load balancer.
138    *
139    * @return <code>String</code> containing information
140    */

141   public String JavaDoc getInformation()
142   {
143     if (weights == null)
144       return "RAIDb-2 Weighted Round Robin Request load balancer: !!!Warning!!! No backend nodes found\n";
145     else
146       return "RAIDb-2 Weighted Round Robin Request load balancer balancing over "
147           + weights.size() + " nodes\n";
148   }
149
150   /**
151    * @see org.objectweb.cjdbc.controller.loadbalancer.raidb2.RAIDb2#getRaidb2Xml
152    */

153   public String JavaDoc getRaidb2Xml()
154   {
155     return WeightedBalancer.getRaidbXml(weights,
156         DatabasesXmlTags.ELT_RAIDb_2_WeightedRoundRobin);
157   }
158 }
Popular Tags