org/objectweb/proactive/scheduler/RessourceManager.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.scheduler;
00032 
00033 import java.util.Iterator;
00034 import java.util.Vector;
00035 
00036 import org.apache.log4j.Logger;
00037 import org.objectweb.proactive.core.node.Node;
00038 import org.objectweb.proactive.core.util.log.Loggers;
00039 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00040 import org.objectweb.proactive.core.util.wrapper.BooleanWrapper;
00041 
00042 
00050 public class RessourceManager {
00051     private static Logger logger = ProActiveLogger.getLogger(Loggers.RESSOURCE_MANAGER);
00052 
00053     // There are 3 HashMaps one for the idle nodes another for the nodes that 
00054     // are busy and one for the reservedNodes that haven't been activated yet. 
00055     // We can check the size of the HashMap unusedNodes to find out the number 
00056     // of nodes available ...
00057     private Vector unusedNodes;
00058     private Vector reservedNodes;
00059     private Vector usedNodes;
00060 
00061     public RessourceManager() {
00062     }
00063 
00069     public RessourceManager(BooleanWrapper b) {
00070         unusedNodes = new Vector();
00071         reservedNodes = new Vector();
00072         usedNodes = new Vector();
00073 
00074         // launches the specific ressourceListener that shall listen for the nodes
00075         // created and add the newly created node to the queue.
00076         String xmlURL = RessourceManager.class.getResource(
00077                 "/org/objectweb/proactive/scheduler/test.xml").getPath();
00078 
00079         Vector vnNames = new Vector();
00080         vnNames.add("SchedulerVN");
00081         new RessourceListener(this.unusedNodes, xmlURL, vnNames);
00082         logger.debug("ressource manager created");
00083     }
00084 
00089     public int getAvailableNodesNb() {
00090         return this.unusedNodes.size();
00091     }
00092 
00098     public void freeNodes(String jobId, boolean mainIsDead) {
00099         int index = 0;
00100         Vector nodes;
00101 
00102         while ((index != this.usedNodes.size()) &&
00103                 (!((Node) this.usedNodes.get(index)).getNodeInformation()
00104                        .getJobID().equals(jobId))) {
00105             index++;
00106         }
00107         if (index == this.usedNodes.size()) {
00108             nodes = this.reservedNodes;
00109         } else {
00110             nodes = this.usedNodes;
00111         }
00112 
00113         this.nodeFreer(nodes, jobId, mainIsDead);
00114     }
00115 
00124     private void nodeFreer(Vector nodes, String jobId, boolean mainIsDead) {
00125         int ressourceNumber = 0;
00126         int indexOfFirstNode = 0;
00127         int i;
00128 
00129         while (!(((Node) nodes.get(indexOfFirstNode)).getNodeInformation()
00130                       .getJobID().equals(jobId)))
00131             indexOfFirstNode++;
00132 
00133         int index = indexOfFirstNode;
00134 
00135         while ((index != nodes.size()) &&
00136                 (((Node) nodes.get(index)).getNodeInformation().getJobID()
00137                       .equals(jobId))) {
00138             ressourceNumber++;
00139             index++;
00140         }
00141 
00142         if (mainIsDead) {
00143             nodes.remove(indexOfFirstNode);
00144             logger.debug("removing dead node");
00145             i = 1;
00146         } else {
00147             i = 0;
00148         }
00149 
00150         for (; i < ressourceNumber; ++i) {
00151             Node node = (Node) nodes.get(indexOfFirstNode);
00152             try {
00153                 if (node.getNumberOfActiveObjects() != 0) {
00154                     node.killAllActiveObjects();
00155                 }
00156             } catch (Exception e) {
00157                 // TODO Auto-generated catch block
00158                 e.printStackTrace();
00159             }
00160             node.getNodeInformation().setJobID("-");
00161             nodes.remove(indexOfFirstNode);
00162             this.unusedNodes.add(node);
00163             logger.debug("node freed " + node.getNodeInformation().getURL());
00164         }
00165     }
00166 
00172     public Node reserveNodes(String jobId, int ressourceNumber) {
00173         if (ressourceNumber == 0) {
00174             return null;
00175         }
00176         Node node = (Node) this.unusedNodes.remove(0);
00177         node.getNodeInformation().setJobID(jobId);
00178         this.reservedNodes.add(node);
00179         logger.debug("node reserved " + node.getNodeInformation().getURL());
00180 
00181         for (int i = 1; i < ressourceNumber; ++i) {
00182             Node tmpNode = (Node) this.unusedNodes.remove(0);
00183             tmpNode.getNodeInformation().setJobID(jobId);
00184             this.reservedNodes.add(tmpNode);
00185         }
00186 
00187         return node;
00188     }
00189 
00195     public Node[] getNodes(String jobId, int askedNodes) {
00196         int indexOfFirstNode = 0;
00197         while (!(((Node) this.reservedNodes.get(indexOfFirstNode)).getNodeInformation()
00198                       .getJobID().equals(jobId)))
00199             indexOfFirstNode++;
00200 
00201         Node[] nodes = new Node[askedNodes];
00202 
00203         for (int i = 0; i < askedNodes; ++i) {
00204             Node node = (Node) this.reservedNodes.remove(indexOfFirstNode);
00205             this.usedNodes.add(node);
00206             nodes[i] = node;
00207             logger.debug("acquiring reserved node " +
00208                 node.getNodeInformation().getURL());
00209         }
00210 
00211         return nodes;
00212     }
00213 
00219     public BooleanWrapper isAvailable(int ressourceNumber) {
00220         return new BooleanWrapper(ressourceNumber <= this.getAvailableNodesNb());
00221     }
00222 
00229     public Vector nodes(String nodeURL) {
00230         Vector nodes = new Vector();
00231 
00232         nodes.addAll(this.unusedNodes);
00233         nodes.addAll(this.reservedNodes);
00234         nodes.addAll(this.usedNodes);
00235 
00236         if (nodeURL != null) {
00237             Iterator iterator = nodes.iterator();
00238             Node node = null;
00239 
00240             while (iterator.hasNext()) {
00241                 Node tmpNode = (Node) iterator.next();
00242                 if (tmpNode.getNodeInformation().getURL().equals(nodeURL)) {
00243                     node = tmpNode;
00244                     break;
00245                 }
00246             }
00247 
00248             nodes.clear();
00249             if (node != null) {
00250                 nodes.add(node);
00251             }
00252         }
00253 
00254         logger.debug("fetching node(s) description");
00255 
00256         return nodes;
00257     }
00258 
00259     public BooleanWrapper checkReservation(String jobId) {
00260         logger.debug("checking for node reservation");
00261         for (int i = 0; i < this.reservedNodes.size(); ++i) {
00262             Node node = (Node) this.reservedNodes.get(i);
00263             if (node.getNodeInformation().getJobID().equals(jobId)) {
00264                 return new BooleanWrapper(true);
00265             }
00266         }
00267 
00268         return new BooleanWrapper(false);
00269     }
00270 }

Generated on Mon Jan 22 15:16:12 2007 for ProActive by  doxygen 1.5.1