001: /**
002: * This file or a portion of this file is licensed under the terms of
003: * the Globus Toolkit Public License, found in file GTPL, or at
004: * http://www.globus.org/toolkit/download/license.html. This notice must
005: * appear in redistributions of this file, with or without modification.
006: *
007: * Redistributions of this Software, with or without modification, must
008: * reproduce the GTPL in: (1) the Software, or (2) the Documentation or
009: * some other similar material which is provided with the Software (if
010: * any).
011: *
012: * Copyright 1999-2004 University of Chicago and The University of
013: * Southern California. All rights reserved.
014: */package org.griphyn.cPlanner.selector.replica;
015:
016: import org.griphyn.cPlanner.classes.ReplicaLocation;
017:
018: import org.griphyn.cPlanner.selector.ReplicaSelector;
019:
020: import org.griphyn.cPlanner.common.LogManager;
021: import org.griphyn.cPlanner.common.PegasusProperties;
022: import org.griphyn.cPlanner.common.PegRandom;
023:
024: import org.griphyn.common.catalog.ReplicaCatalogEntry;
025:
026: import java.util.ArrayList;
027: import java.util.Iterator;
028: import java.util.Vector;
029:
030: /**
031: * This replica selector only prefers replicas from the local host and that
032: * start with a file: URL scheme. It is useful, when you want to stagin
033: * files to a remote site from your submit host using the Condor file transfer
034: * mechanism.
035: *
036: * <p>
037: * In order to use the replica selector implemented by this class,
038: * <pre>
039: * - the property pegasus.selector.replica must be set to value Local
040: * </pre>
041: *
042: *
043: * @see org.griphyn.cPlanner.transfer.implementation.Condor
044: *
045: * @author Karan Vahi
046: * @version $Revision: 408 $
047: */
048: public class Local implements ReplicaSelector {
049:
050: /**
051: * A short description of the replica selector.
052: */
053: private static String mDescription = "Local from submit host";
054:
055: /**
056: * The scheme name for file url.
057: */
058: protected static final String FILE_URL_SCHEME = "file:";
059:
060: /**
061: * The handle to the logging object that is used to log the various debug
062: * messages.
063: */
064: protected LogManager mLogger;
065:
066: /**
067: * The properties object containing the properties passed to the planner.
068: */
069: protected PegasusProperties mProps;
070:
071: /**
072: * The overloaded constructor, that is called by load method.
073: *
074: * @param properties the <code>PegasusProperties</code> object containing all
075: * the properties required by Pegasus.
076: *
077: *
078: */
079: public Local(PegasusProperties properties) {
080: mProps = properties;
081: mLogger = LogManager.getInstance();
082: }
083:
084: /**
085: * Selects a random replica from all the replica's that have their
086: * site handle set to local and the pfn's start with a file url scheme.
087: *
088: * @param rl the <code>ReplicaLocation</code> object containing all
089: * the pfn's associated with that LFN.
090: * @param preferredSite the preffered site for picking up the replicas.
091: *
092: * @return <code>ReplicaCatalogEntry</code> corresponding to the location selected.
093: *
094: * @see org.griphyn.cPlanner.classes.ReplicaLocation
095: */
096: public ReplicaCatalogEntry selectReplica(ReplicaLocation rl,
097: String preferredSite) {
098:
099: ReplicaCatalogEntry rce;
100: ArrayList prefPFNs = new ArrayList();
101: int locSelected;
102: String site = null;
103:
104: // mLogger.log("Selecting a pfn for lfn " + lfn + "\n amongst" + locations ,
105: // LogManager.DEBUG_MESSAGE_LEVEL);
106:
107: for (Iterator it = rl.pfnIterator(); it.hasNext();) {
108: rce = (ReplicaCatalogEntry) it.next();
109: site = rce.getResourceHandle();
110:
111: if (site == null) {
112: //skip to next replica
113: continue;
114: }
115:
116: //check if has pool attribute as local, and at same time
117: //start with a file url scheme
118: if (site.equals("local")
119: && rce.getPFN().startsWith(FILE_URL_SCHEME)) {
120: prefPFNs.add(rce);
121: }
122: }
123:
124: if (prefPFNs.isEmpty()) {
125: //select a random location from
126: //all the matching locations
127: //in all likelihood all the urls were file urls and none
128: //were associated with the preference pool.
129: throw new RuntimeException(
130: "Unable to select any location on local site from "
131: + "the list passed for lfn " + rl.getLFN());
132:
133: } else {
134: //select a random location
135: //amongst the locations
136: //on the preference pool
137: int length = prefPFNs.size();
138: //System.out.println("No of locations found at pool " + prefPool + " are " + length);
139: locSelected = PegRandom.getInteger(length - 1);
140: rce = (ReplicaCatalogEntry) prefPFNs.get(locSelected);
141:
142: }
143:
144: return rce;
145:
146: }
147:
148: /**
149: * This chooses a location amongst all the locations returned by the
150: * Replica Mechanism. If a location is found with re/pool attribute same
151: * as the preference pool, it is taken. This returns all the locations which
152: * match to the preference pool. This function is called to determine if a
153: * file does exist on the output pool or not beforehand. We need all the
154: * location to ensure that we are able to make a match if it so exists.
155: * Else a random location is selected and returned
156: *
157: * @param rl the <code>ReplicaLocation</code> object containing all
158: * the pfn's associated with that LFN.
159: * @param preferredSite the preffered site for picking up the replicas.
160: *
161: * @return <code>ReplicaLocation</code> corresponding to the replicas selected.
162: *
163: * @see org.griphyn.cPlanner.classes.ReplicaLocation
164: */
165: public ReplicaLocation selectReplicas(ReplicaLocation rl,
166: String preferredSite) {
167:
168: String lfn = rl.getLFN();
169: ReplicaLocation result = new ReplicaLocation();
170: result.setLFN(rl.getLFN());
171:
172: ReplicaCatalogEntry rce;
173: String site;
174: int noOfLocs = 0;
175:
176: for (Iterator it = rl.pfnIterator(); it.hasNext();) {
177: noOfLocs++;
178: rce = (ReplicaCatalogEntry) it.next();
179: site = rce.getResourceHandle();
180:
181: if (site != null && site.equals(preferredSite)) {
182: result.addPFN(rce);
183: } else if (site == null) {
184: mLogger.log(
185: " pool attribute not specified for the location objects"
186: + " in the Replica Catalog",
187: LogManager.WARNING_MESSAGE_LEVEL);
188: }
189: }
190:
191: if (result.getPFNCount() == 0) {
192: //means we have to choose a random location between 0 and (noOfLocs -1)
193: int locSelected = PegRandom.getInteger(noOfLocs - 1);
194: rce = (ReplicaCatalogEntry) rl.getPFN(locSelected);
195: result.addPFN(rce);
196: }
197: return result;
198:
199: }
200:
201: /**
202: * Returns a short description of the replica selector.
203: *
204: * @return string corresponding to the description.
205: */
206: public String description() {
207: return mDescription;
208: }
209:
210: }
|