001    /*
002     * RootdSyntaxGenerator.java
003     *
004     * Created on September 15, 2005, 8:18 PM
005     *
006     * This file is part of the STAR Scheduler.
007     * Copyright (c) 2002-2006 STAR Collaboration - Brookhaven National Laboratory
008     *
009     * STAR Scheduler is free software; you can redistribute it and/or modify
010     * it under the terms of the GNU General Public License as published by
011     * the Free Software Foundation; either version 2 of the License, or
012     * (at your option) any later version.
013     *
014     * STAR Scheduler is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017     * GNU General Public License for more details.
018     *
019     * You should have received a copy of the GNU General Public License
020     * along with STAR Scheduler; if not, write to the Free Software
021     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
022     */
023    
024    package gov.bnl.star.offline.scheduler.Dispatchers.lsf;
025    
026    import gov.bnl.star.offline.scheduler.Job;
027    import gov.bnl.star.offline.scheduler.catalog.PhysicalFile;
028    
029    import java.util.*;
030     
031    
032    /**
033     *This class builds the string used by LSF to state which node 
034     * or nodes the job would prefer to run on. Nodes which hold more 
035     * local files requested by the job will be given a higher priority 
036     * then nodes that hold less files  or do not hold any files by 
037     * LSF. The string is formated in such a way as to allow it to be 
038     * directly addable to the LSF command string.
039     *
040     * @author  Levente Hajdu
041     */
042    public class LSFNodePriorityStringGenerator {
043        
044        /** Creates a new instance of RootdSyntaxGenerator */
045        public LSFNodePriorityStringGenerator() {
046        }
047        
048        private String otherNodes ="";
049        public String getOtherNodes(){return otherNodes;}
050        public void setOtherNodes(String otherNodes){this.otherNodes = otherNodes;}
051        
052        private Map nodes = new TreeMap(); 
053        
054        /** Build the string used to describe node priority. 
055         *
056         * @param job The job jobject for which the LFS priorit string will be biult
057         * @return LSF node priority string
058         **/
059        public String generateSyntax(Job job){
060            nodes.clear();
061            List filelist = job.getInput(); 
062            List nodeNames = new ArrayList();
063            String syntax = "";
064            String node = "";
065            String storage ="";
066            
067            for(int i = 0; i != filelist.size(); i++){
068                node = ((PhysicalFile) filelist.get(i)).getNode();
069                storage = ((PhysicalFile) filelist.get(i)).getStorage();
070                
071                if(node != null && storage != null){
072                    if(node != "" && storage != ""){
073                        if(storage.compareTo("HPSS")!=0 && storage.compareTo("NFS")!=0 && node.compareTo("localhost")!=0){ //The node is aso checked just in case
074                        
075                            if(nodes.containsKey(node)){ //pull the node out of the table ++ its value and put it back in
076                                int  n = ((Integer) nodes.get(node)).intValue();
077                                nodes.remove(node);
078                                n ++;
079                                nodes.put(node, new Integer(n)); 
080                            }
081                            else{
082    
083                                nodeNames.add(node);
084                                nodes.put(node, new Integer(2)); //it has to start with a priority of 2       
085                            }
086                        }    
087                    }
088                }    
089              }  
090                
091            Iterator iter = nodes.keySet().iterator();
092    
093            while (iter.hasNext()) {
094                node = (String) iter.next();
095                syntax = syntax + node + "+" + ((Integer) nodes.get(node)).toString() + " ";  
096            }
097            
098            return addQuotes(  syntax + " " + otherNodes );
099     
100        }
101       
102        
103    
104        private String addQuotes(String string){
105            return "\" " + string.trim() + " \"";
106        }
107    
108        
109    }