001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2002-2004 French National Institute For Research In Computer
004: * Science And Control (INRIA).
005: * Copyright (C) 2005 AmicoSoft, Inc. dba Emic Networks
006: * Contact: sequoia@continuent.org
007: *
008: * Licensed under the Apache License, Version 2.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: * Initial developer(s): Emmanuel Cecchet.
021: * Contributor(s): Jean-Bernard van Zuylen
022: */package org.continuent.sequoia.controller.loadbalancer.policies.createtable;
023:
024: import java.util.ArrayList;
025:
026: /**
027: * Implements a round-robin strategy for <code>CREATE TABLE</code>
028: * statements.
029: *
030: * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet</a>
031: * @author <a href="mailto:jbvanzuylen@transwide.com">Jean-Bernard van Zuylen
032: * </a>
033: * @version 1.0
034: */
035: public class CreateTableRoundRobin extends CreateTableRule {
036: private int index = 0;
037:
038: /**
039: * Creates a new <code>CreateTableRoundRobin</code>.
040: */
041: public CreateTableRoundRobin() {
042: super (CreateTablePolicy.ROUND_ROBIN);
043: }
044:
045: /**
046: * Creates a new <code>CreateTableRoundRobin</code>.
047: *
048: * @param backendList <code>ArrayList</code> of <code>DatabaseBackend</code>
049: */
050: public CreateTableRoundRobin(ArrayList backendList) {
051: super (CreateTablePolicy.ROUND_ROBIN, backendList);
052: }
053:
054: /**
055: * @see org.continuent.sequoia.controller.loadbalancer.policies.createtable.CreateTableRule#getBackends(ArrayList)
056: */
057: public ArrayList getBackends(ArrayList backends)
058: throws CreateTableException {
059: if (nbOfNodes == 0)
060: return null;
061:
062: ArrayList clonedList = super .getBackends(backends);
063:
064: int clonedSize = clonedList.size();
065:
066: if (nbOfNodes == clonedSize)
067: return clonedList;
068: else if (nbOfNodes > clonedSize)
069: throw new CreateTableException("Asking for more backends ("
070: + nbOfNodes + ") than available (" + clonedSize
071: + ")");
072:
073: ArrayList result = new ArrayList(nbOfNodes);
074:
075: synchronized (this ) { // index must be modified in mutual exclusion
076: for (int i = 0; i < nbOfNodes; i++) {
077: index = (index + 1) % clonedSize;
078: if (index - i < 0)
079: result.add(clonedList.remove(0));
080: else
081: result.add(clonedList.remove(index - i));
082: }
083: }
084:
085: return result;
086: }
087:
088: /**
089: * @see org.continuent.sequoia.controller.loadbalancer.policies.createtable.CreateTableRule#getInformation()
090: */
091: public String getInformation() {
092: String s;
093: if (tableName == null)
094: s = "Default rule create table on ";
095: else
096: s = "Rule for table " + tableName + " create table on ";
097:
098: return s + nbOfNodes + " node(s) in round-robin from "
099: + backendList;
100: }
101: }
|