View Javadoc

1   package fr.in2p3.jsaga.adaptor.job;
2   
3   import fr.in2p3.jsaga.adaptor.base.defaults.Default;
4   import fr.in2p3.jsaga.adaptor.base.usage.Usage;
5   import fr.in2p3.jsaga.adaptor.job.control.JobControlAdaptor;
6   import fr.in2p3.jsaga.adaptor.job.control.description.JobDescriptionTranslator;
7   import fr.in2p3.jsaga.adaptor.job.control.description.JobDescriptionTranslatorXSLT;
8   import fr.in2p3.jsaga.adaptor.job.monitor.JobMonitorAdaptor;
9   import org.apache.log4j.Logger;
10  import org.globus.gram.*;
11  import org.globus.gram.internal.GRAMConstants;
12  import org.globus.gram.internal.GRAMProtocolErrorConstants;
13  import org.globus.rsl.*;
14  import org.ietf.jgss.GSSException;
15  import org.ogf.saga.error.*;
16  
17  import java.net.MalformedURLException;
18  import java.util.Map;
19  
20  /* ***************************************************
21   * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
22   * ***             http://cc.in2p3.fr/             ***
23   * ***************************************************
24   * File:   UnmonitoredJobControlAdaptor
25   * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
26   * Date:   25 juin 2010
27   * ***************************************************
28   * Description:                                      */
29  /**
30   * NOTE: This is a limited version of the GkCommonJobControlAdaptor, which does not support job monitoring.
31   *       However, it avoids crashing server when submitting a lot of jobs on the same server,
32   *       by stopping the job manager within 9 to 10 seconds.
33   */
34  public class UnmonitoredJobControlAdaptor extends GatekeeperJobAdaptorAbstract implements JobControlAdaptor {
35      private static Logger s_logger = Logger.getLogger(UnmonitoredJobControlAdaptor.class);
36  
37      private UnmonitoredJobMonitorAdaptor m_monitor = new UnmonitoredJobMonitorAdaptor();
38  
39      public String getType() {
40          return "unmonitored";
41      }
42  
43      public JobMonitorAdaptor getDefaultJobMonitor() {
44          return m_monitor;
45      }
46  
47      public Usage getUsage() {
48          return null;
49      }
50  
51      public Default[] getDefaults(Map attributes) throws IncorrectStateException {
52          return null;
53      }
54  
55      public void connect(String userInfo, String host, int port, String basePath, Map attributes) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, BadParameterException, TimeoutException, NoSuccessException {
56          super.connect(userInfo, host, port, basePath, attributes);
57      }
58  
59      public void disconnect() throws NoSuccessException {
60          super.disconnect();
61      }
62  
63      public JobDescriptionTranslator getJobDescriptionTranslator() throws NoSuccessException {
64          return new JobDescriptionTranslatorXSLT("xsl/job/lcgce.xsl");
65      }
66  
67      public String submit(String jobDesc, boolean checkMatch, String uniqId)  throws PermissionDeniedException, TimeoutException, NoSuccessException, BadResource {
68      	RslNode rslTree;
69          try {
70          	rslTree = RSLParser.parse(jobDesc);
71          } catch (ParseException e) {
72              throw new NoSuccessException(e);
73          }
74          return submit(rslTree, checkMatch, false);
75      }
76  
77      protected String submit(RslNode rslTree, boolean checkMatch, boolean isInteractive) throws PermissionDeniedException, TimeoutException, NoSuccessException, BadResource {
78          if(checkMatch) {
79  			s_logger.debug("CheckMatch not supported");
80  		}
81          GramJob job = new GramJob(m_credential, rslTree.toRSL(true));
82          try {
83              Gram.request(m_serverUrl, job, isInteractive);
84          } catch (GramException e) {
85              this.rethrowException(e);
86          } catch (GSSException e) {
87              throw new NoSuccessException("Failed to submit job", e);
88          }
89  
90          //NOTE: this prevents from crashing Gatekeeper but disables job monitoring
91          try {
92              job.signal(GRAMConstants.SIGNAL_STOP_MANAGER);
93          } catch (GramException e) {
94              this.rethrowException(e);
95          } catch (GSSException e) {
96              throw new NoSuccessException(e);
97          }
98          return job.getIDAsString();
99      }
100 
101     public void cancel(String nativeJobId) throws PermissionDeniedException, TimeoutException, NoSuccessException {
102         GramJob job = this.restart(nativeJobId);
103         try {
104             job.cancel();
105         } catch (GramException e) {
106             this.rethrowException(e);
107         } catch (GSSException e) {
108             throw new NoSuccessException("Failed to cancel job", e);
109         }
110 
111         // tell job monitor to return status CANCELED
112         m_monitor.cancel();
113     }
114 
115     private GramJob restart(String nativeJobId) throws PermissionDeniedException, TimeoutException, NoSuccessException {
116         GramJob job = new GramJob(m_credential, "&(restart="+nativeJobId+")(proxy_timeout=240)");
117         try {
118             job.setID(nativeJobId);
119         } catch (MalformedURLException e1) {
120             throw new NoSuccessException(e1);
121         }
122         try {
123             Gram.request(m_serverUrl, job);
124         } catch (GramException e) {
125                 this.rethrowException(e);
126         } catch (GSSException e) {
127             throw new NoSuccessException("Failed to restart jobmanager", e);
128         }
129         return job;
130     }
131 
132     private void rethrowException(GramException e) throws PermissionDeniedException, TimeoutException, NoSuccessException, BadResource{
133     	switch(e.getErrorCode()) {
134     		case GRAMProtocolErrorConstants.BAD_DIRECTORY:
135     			throw new BadResource(e);
136             case GRAMProtocolErrorConstants.ERROR_AUTHORIZATION:
137                 throw new PermissionDeniedException(e);
138             case GRAMProtocolErrorConstants.INVALID_JOB_CONTACT:
139             case GRAMProtocolErrorConstants.ERROR_CONNECTION_FAILED:
140                 throw new TimeoutException(e);
141             default:
142                 throw new NoSuccessException(e);
143         }
144     }
145 }