001    /*
002     * $RCSfile: ClusterAssignmentByMonitorPolicy.java,v $
003     *
004     * Created on August 6, 2002, 10:15 AM
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    package gov.bnl.star.offline.scheduler.policy;
024    
025    import gov.bnl.star.offline.scheduler.*;
026    import gov.bnl.star.offline.scheduler.Policy;
027    import gov.bnl.star.offline.scheduler.catalog.*;
028    import gov.bnl.star.offline.scheduler.catalog.CatalogTask;
029    import gov.bnl.star.offline.scheduler.catalog.QueryResult;
030    import gov.bnl.star.offline.scheduler.catalog.StarCatalog;
031    import gov.bnl.star.offline.scheduler.monitor.ClusterInfo;
032    import gov.bnl.star.offline.scheduler.monitor.ClusterInfoFinder;
033    import gov.bnl.star.offline.scheduler.policy.copyselector.*;
034    import gov.bnl.star.offline.scheduler.request.Request;
035    import java.io.File;
036    import java.io.FileOutputStream;
037    import java.io.InputStreamReader;
038    import java.io.PrintStream;
039    import java.net.*;
040    import java.util.*;
041    
042    import java.util.Iterator;
043    import java.util.List;
044    import java.util.Set;
045    import java.util.logging.Level;
046    import org.apache.log4j.Logger;
047    
048    
049    /** This policy takes the assignment from another policy, and assigns clusters
050     * depending on the result of the monitoring information. The class will have
051     * a primary cluster and a secondary cluster. If the secondary cluster is not
052     * completely full, some jobs will be diverted there. All the jobs that won't
053     * fit in the secondary cluster will be diverted to the primary cluster. One can
054     * set a specific queue for which jobs will be sent to the secondary, leaving
055     * all the other queues to the primary.
056     *
057     * @author Gabriele Carcassi
058     * @version $Revision: 1.6 $ $Date: 2006/11/21 00:41:32 $
059     */
060    public class ClusterAssignmentByMonitorPolicy implements Policy {
061        static private Logger log = Logger.getLogger(ClusterAssignmentByMonitorPolicy.class.getName());
062        
063        /** Holds value of property policy. */
064        private Policy policy;
065        
066        /** Holds value of property clusterMonitoring. */
067        private ClusterInfoFinder clusterMonitoring;
068        
069        /** Holds value of property primaryCluster. */
070        private String primaryCluster;
071        
072        /** Holds value of property secondaryCluster. */
073        private String secondaryCluster;
074        
075        /** Holds value of property queueToRedirect. */
076        private String queueToRedirect;
077        
078        public List assignTargetMachine(Request request) {
079            List jobs = getPolicy().assignTargetMachine(request);
080            
081            int nRedirected = 0;
082            int maxRedirect = calulateNJobsToDirectToSecondaryCluster();
083            Iterator iter = jobs.iterator();
084            while (iter.hasNext()) {
085                Job job = (Job) iter.next();
086                if ((nRedirected < maxRedirect) && ((getQueueToRedirect() == null) ||
087                (getQueueToRedirect().equals(job.getQueue())))) {
088                    job.setCluster(getSecondaryCluster());
089                    nRedirected++;
090                } else  {
091                    job.setCluster(getPrimaryCluster());
092                }
093            }
094            
095            return jobs;
096        }
097        
098        int calulateNJobsToDirectToSecondaryCluster() {
099            ClusterInfo secondaryInfo = getClusterMonitoring().getClusterInfo(getSecondaryCluster());
100            //System.out.println("AverageLoad5m() = " + secondaryInfo.getAverageLoad5m()); //test lbh
101            if (secondaryInfo == null) return 0;
102            if (secondaryInfo.getAverageLoad5m() >= 1.0) return 0;
103            return (int) ((1.0 - secondaryInfo.getAverageLoad5m()) * (double) secondaryInfo.getCPUCount());
104        }
105        
106        /** Getter for property policy.
107         * @return Value of property policy.
108         *
109         */
110        public Policy getPolicy() {
111            return this.policy;
112        }    
113    
114        /** Setter for property policy.
115         * @param policy New value of property policy.
116         *
117         */
118        public void setPolicy(Policy policy) {
119            this.policy = policy;
120        }
121        
122        /** Getter for property clusterMonitoring.
123         * @return Value of property clusterMonitoring.
124         *
125         */
126        public ClusterInfoFinder getClusterMonitoring() {
127            return this.clusterMonitoring;
128        }
129        
130        /** Setter for property clusterMonitoring.
131         * @param clusterMonitoring New value of property clusterMonitoring.
132         *
133         */
134        public void setClusterMonitoring(ClusterInfoFinder clusterMonitoring) {
135            this.clusterMonitoring = clusterMonitoring;
136        }
137        
138        /** Getter for property primaryCluster.
139         * @return Value of property primaryCluster.
140         *
141         */
142        public String getPrimaryCluster() {
143            return this.primaryCluster;
144        }
145        
146        /** Setter for property primaryCluster.
147         * @param primaryCluster New value of property primaryCluster.
148         *
149         */
150        public void setPrimaryCluster(String primaryCluster) {
151            this.primaryCluster = primaryCluster;
152        }
153        
154        /** Getter for property secondaryCluster.
155         * @return Value of property secondaryCluster.
156         *
157         */
158        public String getSecondaryCluster() {
159            return this.secondaryCluster;
160        }
161        
162        /** Setter for property secondaryCluster.
163         * @param secondaryCluster New value of property secondaryCluster.
164         *
165         */
166        public void setSecondaryCluster(String secondaryCluster) {
167            this.secondaryCluster = secondaryCluster;
168        }
169        
170        /** Getter for property queueToRedirect.
171         * @return Value of property queueToRedirect.
172         *
173         */
174        public String getQueueToRedirect() {
175            return this.queueToRedirect;
176        }
177        
178        /** Setter for property queueToRedirect.
179         * @param queueToRedirect New value of property queueToRedirect.
180         *
181         */
182        public void setQueueToRedirect(String queueToRedirect) {
183            this.queueToRedirect = queueToRedirect;
184        }
185        
186    }