001    /*
002     * $RCSfile: ClusterAssignmentByPercentagePolicy.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 decides
050     * to which cluster to assign them according to the percentage. For each job,
051     * a random number is generated between 0 and 1. If the number is less than
052     * the percentage set, the job will be forwarded. The number of jobs forwarded,
053     * then, is not strictly the percentage set. It will be for large number.
054     *
055     * @author Gabriele Carcassi
056     * @version $Revision: 1.4 $ $Date: 2006/11/21 00:41:32 $
057     */
058    public class ClusterAssignmentByPercentagePolicy implements Policy {
059        static private Logger log = Logger.getLogger(ClusterAssignmentByPercentagePolicy.class.getName());
060        
061        /** Holds value of property policy. */
062        private Policy policy;
063        
064        /** Holds value of property primaryCluster. */
065        private String primaryCluster;
066        
067        /** Holds value of property secondaryCluster. */
068        private String secondaryCluster;
069        
070        /** Holds value of property queueToRedirect. */
071        private String queueToRedirect;
072        
073        /** Holds value of property redirectedPercentage. */
074        private double redirectedPercentage;
075        
076        public List assignTargetMachine(Request request) {
077            List jobs = getPolicy().assignTargetMachine(request);
078            
079            Iterator iter = jobs.iterator();
080            Random rand = new Random();
081            while (iter.hasNext()) {
082                Job job = (Job) iter.next();
083                if (rand.nextFloat() < getRedirectedPercentage()) {
084                    job.setCluster(getSecondaryCluster());
085                } else {
086                    job.setCluster(getPrimaryCluster());
087                }
088            }
089            
090            return jobs;
091        }
092        
093        /** Getter for property policy.
094         * @return Value of property policy.
095         *
096         */
097        public Policy getPolicy() {
098            return this.policy;
099        }    
100    
101        /** Setter for property policy.
102         * @param policy New value of property policy.
103         *
104         */
105        public void setPolicy(Policy policy) {
106            this.policy = policy;
107        }
108    
109        /** Getter for property primaryCluster.
110         * @return Value of property primaryCluster.
111         *
112         */
113        public String getPrimaryCluster() {
114            return this.primaryCluster;
115        }
116        
117        /** Setter for property primaryCluster.
118         * @param primaryCluster New value of property primaryCluster.
119         *
120         */
121        public void setPrimaryCluster(String primaryCluster) {
122            this.primaryCluster = primaryCluster;
123        }
124        
125        /** Getter for property secondaryCluster.
126         * @return Value of property secondaryCluster.
127         *
128         */
129        public String getSecondaryCluster() {
130            return this.secondaryCluster;
131        }
132        
133        /** Setter for property secondaryCluster.
134         * @param secondaryCluster New value of property secondaryCluster.
135         *
136         */
137        public void setSecondaryCluster(String secondaryCluster) {
138            this.secondaryCluster = secondaryCluster;
139        }
140        
141        /** Getter for property queueToRedirect.
142         * @return Value of property queueToRedirect.
143         *
144         */
145        public String getQueueToRedirect() {
146            return this.queueToRedirect;
147        }
148        
149        /** Setter for property queueToRedirect.
150         * @param queueToRedirect New value of property queueToRedirect.
151         *
152         */
153        public void setQueueToRedirect(String queueToRedirect) {
154            this.queueToRedirect = queueToRedirect;
155        }
156        
157        /** Getter for property redirectedPercentage.
158         * @return Value of property redirectedPercentage.
159         *
160         */
161        public double getRedirectedPercentage() {
162            return this.redirectedPercentage;
163        }
164        
165        /** Setter for property redirectedPercentage.
166         * @param redirectedPercentage New value of property redirectedPercentage.
167         *
168         */
169        public void setRedirectedPercentage(double redirectedPercentage) {
170            this.redirectedPercentage = redirectedPercentage;
171        }
172        
173    }