KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > controller > loadbalancer > paralleldb > ParallelDB_RR


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): ______________________.
23  */

24
25 package org.objectweb.cjdbc.controller.loadbalancer.paralleldb;
26
27 import java.sql.SQLException JavaDoc;
28 import java.util.ArrayList JavaDoc;
29
30 import org.objectweb.cjdbc.common.i18n.Translate;
31 import org.objectweb.cjdbc.common.sql.AbstractRequest;
32 import org.objectweb.cjdbc.common.sql.AbstractWriteRequest;
33 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags;
34 import org.objectweb.cjdbc.controller.backend.DatabaseBackend;
35 import org.objectweb.cjdbc.controller.virtualdatabase.VirtualDatabase;
36
37 /**
38  * This class defines a ParallelDB_RR load balancer. This load balancer performs
39  * simple round-robin for read and write queries execution.
40  *
41  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
42  * @version 1.0
43  */

44 public class ParallelDB_RR extends ParallelDB
45 {
46
47   private int index = 0;
48
49   /**
50    * Creates a new <code>ParallelDB_RR</code> object
51    *
52    * @param vdb the virtual database this load balancer belongs to.
53    * @throws Exception if an error occurs
54    */

55   public ParallelDB_RR(VirtualDatabase vdb) throws Exception JavaDoc
56   {
57     super(vdb);
58   }
59
60   /**
61    * Choose a backend using a round-robin algorithm for read request execution.
62    *
63    * @param request request to execute
64    * @return the chosen backend
65    * @throws SQLException if an error occurs
66    */

67   public DatabaseBackend chooseBackendForReadRequest(AbstractRequest request)
68       throws SQLException JavaDoc
69   {
70     // Choose a backend
71
try
72     {
73       vdb.acquireReadLockBackendLists();
74     }
75     catch (InterruptedException JavaDoc e)
76     {
77       String JavaDoc msg = Translate.get(
78           "loadbalancer.backendlist.acquire.readlock.failed", e);
79       logger.error(msg);
80       throw new SQLException JavaDoc(msg);
81     }
82
83     DatabaseBackend backend = null; // The backend that will execute the query
84

85     // Note that vdb lock is released in the finally clause of this try/catch
86
// block
87
try
88     {
89       ArrayList JavaDoc backends = vdb.getBackends();
90       int size = backends.size();
91
92       if (size == 0)
93         throw new SQLException JavaDoc(Translate.get(
94             "loadbalancer.execute.no.backend.available", request.getId()));
95
96       // Take the next backend
97
int maxTries = size;
98       synchronized (this)
99       {
100         do
101         {
102           index = (index + 1) % size;
103           backend = (DatabaseBackend) backends.get(index);
104           maxTries--;
105         }
106         while ((!backend.isReadEnabled() && maxTries >= 0));
107       }
108
109       if (maxTries < 0)
110         throw new SQLException JavaDoc(Translate.get(
111             "loadbalancer.execute.no.backend.enabled", request.getId()));
112     }
113     catch (RuntimeException JavaDoc e)
114     {
115       String JavaDoc msg = Translate.get("loadbalancer.execute.find.backend.failed",
116           new String JavaDoc[]{request.getSQLShortForm(vdb.getSQLShortFormLength()),
117               e.getMessage()});
118       logger.error(msg, e);
119       throw new SQLException JavaDoc(msg);
120     }
121     finally
122     {
123       vdb.releaseReadLockBackendLists();
124     }
125     return backend;
126   }
127
128   /**
129    * Choose a backend using a round-robin algorithm for write request execution.
130    *
131    * @param request request to execute
132    * @return the chosen backend
133    * @throws SQLException if an error occurs
134    */

135   public DatabaseBackend chooseBackendForWriteRequest(
136       AbstractWriteRequest request) throws SQLException JavaDoc
137   {
138     // Choose a backend
139
try
140     {
141       vdb.acquireReadLockBackendLists();
142     }
143     catch (InterruptedException JavaDoc e)
144     {
145       String JavaDoc msg = Translate.get(
146           "loadbalancer.backendlist.acquire.readlock.failed", e);
147       logger.error(msg);
148       throw new SQLException JavaDoc(msg);
149     }
150
151     DatabaseBackend backend = null; // The backend that will execute the query
152

153     // Note that vdb lock is released in the finally clause of this try/catch
154
// block
155
try
156     {
157       ArrayList JavaDoc backends = vdb.getBackends();
158       int size = backends.size();
159
160       if (size == 0)
161         throw new SQLException JavaDoc(Translate.get(
162             "loadbalancer.execute.no.backend.available", request.getId()));
163
164       // Take the next backend
165
int maxTries = size;
166       synchronized (this)
167       {
168         do
169         {
170           index = (index + 1) % size;
171           backend = (DatabaseBackend) backends.get(index);
172           maxTries--;
173         }
174         while ((!backend.isWriteEnabled() || backend.isDisabling())
175             && (maxTries >= 0));
176       }
177
178       if (maxTries < 0)
179         throw new SQLException JavaDoc(Translate.get(
180             "loadbalancer.execute.no.backend.enabled", request.getId()));
181     }
182     catch (RuntimeException JavaDoc e)
183     {
184       String JavaDoc msg = Translate.get("loadbalancer.execute.find.backend.failed",
185           new String JavaDoc[]{request.getSQLShortForm(vdb.getSQLShortFormLength()),
186               e.getMessage()});
187       logger.error(msg, e);
188       throw new SQLException JavaDoc(msg);
189     }
190     finally
191     {
192       vdb.releaseReadLockBackendLists();
193     }
194     return backend;
195   }
196
197   /**
198    * @see org.objectweb.cjdbc.controller.loadbalancer.AbstractLoadBalancer#getInformation()
199    */

200   public String JavaDoc getInformation()
201   {
202     // We don't lock since we don't need a top accurate value
203
int size = vdb.getBackends().size();
204
205     if (size == 0)
206       return "ParallelDB Round-Robin Request load balancer: !!!Warning!!! No backend nodes found\n";
207     else
208       return "ParallelDB Round-Robin Request load balancer (" + size
209           + " backends)\n";
210   }
211
212   /**
213    * @see org.objectweb.cjdbc.controller.loadbalancer.paralleldb.ParallelDB#getParallelDBXml()
214    */

215   public String JavaDoc getParallelDBXml()
216   {
217     return "<" + DatabasesXmlTags.ELT_ParallelDB_RoundRobin + "/>";
218   }
219 }
Popular Tags