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

ErrorCheckingRandom.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 import java.util.Random;
00029 
00030 import org.objectweb.cjdbc.controller.backend.DatabaseBackend;
00031 
00032 /**
00033  * Chooses numberOfNodes nodes randomly for error checking.
00034  * 
00035  * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet</a>
00036  * @version 1.0
00037  */
00038 public class ErrorCheckingRandom extends ErrorCheckingPolicy
00039 {
00040   private Random random = new Random();
00041 
00042   /**
00043    * Creates a new <code>ErrorCheckingRandom</code> instance.
00044    * 
00045    * @param numberOfNodes number of nodes to use to check for errors on a query
00046    */
00047   public ErrorCheckingRandom(int numberOfNodes)
00048   {
00049     super(ErrorCheckingPolicy.RANDOM, numberOfNodes);
00050   }
00051 
00052   /**
00053    * @see org.objectweb.cjdbc.controller.loadbalancer.policies.errorchecking.ErrorCheckingPolicy#getBackends(ArrayList)
00054    */
00055   public ArrayList getBackends(ArrayList backends)
00056     throws ErrorCheckingException
00057   {
00058     int size = backends.size();
00059 
00060     if (nbOfNodes == size)
00061       return backends;
00062     else if (nbOfNodes > size)
00063       throw new ErrorCheckingException(
00064         "Asking for more backends ("
00065           + nbOfNodes
00066           + ") than available ("
00067           + size
00068           + ")");
00069 
00070     ArrayList result = new ArrayList(nbOfNodes);
00071     ArrayList clonedList = new ArrayList(size);
00072     for (int i = 0; i < size; i++)
00073     { // Take all enabled backends
00074       DatabaseBackend db = (DatabaseBackend) backends.get(i);
00075       if (db.isReadEnabled() || db.isWriteEnabled())
00076         clonedList.add(db);
00077     }
00078 
00079     int clonedSize = clonedList.size();
00080 
00081     if (nbOfNodes == clonedSize)
00082       return backends;
00083     else if (nbOfNodes > clonedSize)
00084       throw new ErrorCheckingException(
00085         "Asking for more backends ("
00086           + nbOfNodes
00087           + ") than available ("
00088           + clonedSize
00089           + ")");
00090 
00091     // Pickup the nodes randomly
00092     for (int i = 0; i < nbOfNodes; i++)
00093       result.add(clonedList.remove(random.nextInt(clonedSize)));
00094 
00095     return result;
00096   }
00097 
00098   /**
00099    * @see org.objectweb.cjdbc.controller.loadbalancer.policies.errorchecking.ErrorCheckingPolicy#getInformation()
00100    */
00101   public String getInformation()
00102   {
00103     return "Error checking using " + nbOfNodes + " nodes choosen randomly";
00104   }
00105 }

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