Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

ErrorCheckingRoundRobin.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2004 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Emmanuel Cecchet.
00022  * Contributor(s): _______________________
00023  */
00024 
00025 package org.objectweb.cjdbc.controller.loadbalancer.policies.errorchecking;
00026 
00027 import java.util.ArrayList;
00028 
00029 import org.objectweb.cjdbc.controller.backend.DatabaseBackend;
00030 
00031 /**
00032  * Chooses the number of nodes nodes for error checking using a round-robin
00033  * algorithm.
00034  * 
00035  * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet</a>
00036  * @version 1.0
00037  */
00038 public class ErrorCheckingRoundRobin extends ErrorCheckingPolicy
00039 {
00040   /** Round-robin index. */
00041   private int index = 0;
00042 
00043   /**
00044    * Creates a new <code>ErrorCheckingRoundRobin</code> instance.
00045    * 
00046    * @param numberOfNodes number of nodes
00047    */
00048   public ErrorCheckingRoundRobin(int numberOfNodes)
00049   {
00050     super(ErrorCheckingPolicy.ROUND_ROBIN, numberOfNodes);
00051   }
00052 
00053   /**
00054    * @see org.objectweb.cjdbc.controller.loadbalancer.policies.errorchecking.ErrorCheckingPolicy#getBackends(ArrayList)
00055    */
00056   public ArrayList getBackends(ArrayList backends)
00057     throws ErrorCheckingException
00058   {
00059     int size = backends.size();
00060 
00061     if (nbOfNodes == 0)
00062       return null;
00063     else if (nbOfNodes == size)
00064       return backends;
00065 
00066     ArrayList result = new ArrayList(nbOfNodes);
00067     ArrayList clonedList = new ArrayList(size);
00068     for (int i = 0; i < size; i++)
00069     { // Take all enabled backends
00070       DatabaseBackend db = (DatabaseBackend) backends.get(i);
00071       if (db.isReadEnabled() || db.isWriteEnabled())
00072         clonedList.add(db);
00073     }
00074 
00075     int clonedSize = clonedList.size();
00076 
00077     if (nbOfNodes == clonedSize)
00078       return backends;
00079     else if (nbOfNodes > clonedSize)
00080       throw new ErrorCheckingException(
00081         "Asking for more backends ("
00082           + nbOfNodes
00083           + ") than available ("
00084           + clonedSize
00085           + ")");
00086 
00087     synchronized (this)
00088     { // index must be modified in mutual exclusion
00089       for (int i = 0; i < nbOfNodes; i++)
00090       {
00091         index = (index + 1) % clonedSize;
00092         result.add(clonedList.remove(index));
00093       }
00094     }
00095 
00096     return result;
00097   }
00098 
00099   /**
00100    * @see org.objectweb.cjdbc.controller.loadbalancer.policies.errorchecking.ErrorCheckingPolicy#getInformation()
00101    */
00102   public String getInformation()
00103   {
00104     return "Error checking using "
00105       + nbOfNodes
00106       + " nodes choosen using a round-robin algorithm";
00107   }
00108 }

Generated on Mon Apr 11 22:01:32 2005 for C-JDBC by  doxygen 1.3.9.1