View Javadoc

1   /*
2    * Copyright 2007-2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.gwe.persistence.model;
18  
19  import java.io.Serializable;
20  import java.sql.Timestamp;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import javax.persistence.CascadeType;
25  import javax.persistence.Column;
26  import javax.persistence.Entity;
27  import javax.persistence.GeneratedValue;
28  import javax.persistence.GenerationType;
29  import javax.persistence.Id;
30  import javax.persistence.Lob;
31  import javax.persistence.OneToMany;
32  import javax.persistence.OneToOne;
33  import javax.persistence.Transient;
34  
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  import org.gwe.persistence.model.order.OrderDescriptor;
38  import org.gwe.persistence.model.order.p2el.POrderDescriptor;
39  import org.hibernate.annotations.GenericGenerator;
40  
41  /**
42   * @author Marco Ruiz
43   * @since Aug 3, 2007
44   */
45  @Entity
46  @GenericGenerator(name="ORDER_SEQ", strategy="increment")
47  //@javax.persistence.SequenceGenerator(name="ORDER_SEQ", initialValue=1000, allocationSize=1)
48  public final class OrderInfo extends BaseModelInfo<Integer> {
49  
50  	private static Log log = LogFactory.getLog(OrderInfo.class);
51  	
52      @Id 
53      @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="ORDER_SEQ") 
54      private int id;
55      
56      // Id of the order after which this one should run. The first one is 0.
57      private int priority = 0;  
58  
59      private String description;
60      private String email;
61      
62  //  @ManyToOne(fetch=FetchType.EAGER)
63      @Transient
64      private GridInfo grid;
65      
66      @Lob 
67      @Column(length=20480)
68      private OrderDescriptor descriptor; 
69      
70      @Lob 
71      @Column(length=4096)
72      private Serializable specificParameters = null;
73      
74      private Timestamp whenCompleted = null;
75      
76      private boolean paused = false;
77      private boolean aborted = false;
78      private boolean deleted = false;
79      private boolean failed = false;
80      
81      private int totalJobsCount = 0;
82      private int completedJobsCount = 0;
83      private int failedJobsCount = 0;
84  
85  	@OneToMany
86      private List<JobInfo> jobs;
87      
88      private Class<? extends ResultParser> resultParserClass = ResultParser.class;
89      
90      @OneToOne(cascade=CascadeType.ALL)
91  	private OrderExecutionProfileInfo executionProfile;
92  
93  	public OrderInfo() {}
94  	
95  	public OrderInfo(OrderDescriptor descriptor) {
96  		this(descriptor, null);
97  	}
98  
99  	public OrderInfo(OrderDescriptor descriptor, String email) {
100 		this("Order from " + email, descriptor, email);
101 	}
102 	
103 	public OrderInfo(String description, OrderDescriptor descriptor, String email) {
104 		this.description = description;
105 		this.descriptor = descriptor;
106 		this.email = email;
107 	}
108 	
109 	public Integer getId() {
110 		return id;
111 	}
112 
113 	public String getUniversalId() {
114 		return 	getId() + "-" + getWhenCreated().getTime();
115 	}
116 
117 	private void setId(int id) {
118 		this.id = id;
119 	}
120 
121 	public int getPriority() {
122     	return priority;
123     }
124 
125 	public void setPriority(int priority) {
126     	this.priority = priority;
127     }
128 
129 	public GridInfo getGrid() {
130 		return grid;
131 	}
132 
133 	public void setGrid(GridInfo grid) {
134 		this.grid = grid;
135 	}
136 
137 	public String getDescription() {
138 		return description;
139 	}
140 
141 	public void setDescription(String description) {
142 		this.description = description;
143 	}
144 
145 	public String getEmail() {
146 		return email;
147 	}
148 
149 	public void setEmail(String email) {
150 		this.email = email;
151 	}
152 
153 	public OrderDescriptor getDescriptor() {
154 		return descriptor;
155 	}
156 	
157 	public void setDescriptor(OrderDescriptor jobDescriptor) {
158 		this.descriptor = jobDescriptor;
159 	}
160 	
161 	public List<JobInfo> getJobs() {
162 		return jobs;
163 	}
164 
165 	public String getWorkspaceInDaemon(DaemonConfigDesc config) {
166 		return config.getHeadResource().getInstallation().getOrdersWorkspacePath(getId());		
167 	}
168 	
169 	public void generateJobs(DaemonConfigDesc config, OrderExecutionProfileInfo executionProfile) {
170 		this.executionProfile = executionProfile;
171 		try {
172 			jobs = descriptor.generateJobs(config);
173 		} catch(Exception e) {
174 			log.warn("Order descriptor failed while generating jobs: '" + descriptor + "'", e);
175 			jobs = new ArrayList<JobInfo>();
176 			failed = true;
177 		}
178 		
179 		JobInfoIdGenerator jobIdInfoGenerator = new JobInfoIdGenerator();
180 		for (JobInfo job : jobs) {
181 			job.setOrder(this);
182 			job.getDescriptor().processSystemDependencies(config, job);
183 			job.getRequest().setMaxJobRunningTime(executionProfile.getMaxJobRunningTime());
184 		}
185 		totalJobsCount = jobs.size();
186 		logEvent(EventType.ORDER_EXPANDED, jobs.toArray(new BaseModelInfo[]{}));
187 		checkIfCompleted();
188 	}
189 
190 	public String toCVS() {
191 		StringBuffer result = new StringBuffer("");
192 		List<String> varNames = descriptor.getVarNames();
193 
194 		result.append("Run");
195 		for (String varName : varNames) 
196 	        result.append(",").append(varName);
197 		
198 		result.append("\n");
199 		for (JobInfo job : jobs)
200 			result.append("\"").append(job.getJobNum()).append("\",").append(job.getDescriptor().toCSV(varNames)).append("\n");
201 
202 		return result.toString();
203 	}
204 
205 	public String toCommands() {
206 		StringBuffer result = new StringBuffer("");
207 		for (JobInfo job : jobs)
208 			result.append(job.getDescriptor()).append("\n");
209 
210 		return result.toString();
211 	}
212 
213 	public String toThinModel() {
214 		String stmt = ((POrderDescriptor)descriptor).getP2ELStatement();
215 		
216 		for (JobInfo job : jobs) 
217 			job.getDescriptor().getPermutationValues();
218 
219 		return "";//result.toString();
220 	}
221 
222 	public Serializable getSpecificParameters() {
223 		return specificParameters;
224 	}
225 
226 	public void setSpecificParameters(Serializable specificParameters) {
227 		this.specificParameters = specificParameters;
228 	}
229 
230 	public ResultParser createResultParser() throws InstantiationException, IllegalAccessException {
231 		return resultParserClass.newInstance();
232 	}
233 
234 	public Class<? extends ResultParser> getResultParserClass() {
235 		return resultParserClass;
236 	}
237 
238 	public void setResultParserClass(Class<? extends ResultParser> resultParserClass) {
239 		this.resultParserClass = resultParserClass;
240 	}
241 
242 	public boolean isPaused() {
243 		return paused;
244 	}
245 
246 	public void setPaused(boolean paused) {
247 		this.paused = paused;
248 	}
249 
250 	public boolean isAborted() {
251 		return aborted;
252 	}
253 
254 	public void setAborted(boolean aborted) {
255 		this.aborted = aborted;
256 	}
257 
258     public boolean isDeleted() {
259     	return deleted;
260     }
261 
262 	public void setDeleted(boolean deleted) {
263     	this.deleted = deleted;
264     }
265 
266 	public String toString() {
267 		return getDescriptor() + "\nTo be executed by " + getGrid(); 
268 	}
269 
270 	public Timestamp getWhenCompleted() {
271 		return whenCompleted;
272 	}
273 
274 	public void setWhenCompleted(Timestamp whenCompleted) {
275 		this.whenCompleted = whenCompleted;
276 	}
277 
278 	public void incrementJobsCompleted(int amount) {
279 		completedJobsCount += amount;
280 		log.info("More jobs completed for order " + id + ". Latest count: " + completedJobsCount);
281 		checkIfCompleted();
282 	}
283 
284 	public void incrementJobsFailed(int amount) {
285 		failedJobsCount += amount;
286 		log.info("More jobs failed for order " + id + ". Latest count: " + failedJobsCount);
287 		checkIfCompleted();
288 	}
289 
290 	private void checkIfCompleted() {
291 		if (isFinished()) whenCompleted = logEvent(EventType.ORDER_COMPLETED);
292 	}
293 	
294 	public boolean isFinished() {
295 		return completedJobsCount + failedJobsCount >= totalJobsCount;
296 	}
297 
298 	public int getTotalJobsCount() {
299 		return totalJobsCount;
300 	}
301 
302 	public int getCompletedJobsCount() {
303 		return completedJobsCount;
304 	}
305 
306     public int getFailedJobsCount() {
307     	return failedJobsCount;
308     }
309 
310     public OrderExecutionProfileInfo getExecutionProfile() {
311     	return executionProfile;
312     }
313 
314 	public void setExecutionProfile(OrderExecutionProfileInfo executionProfile) {
315     	this.executionProfile = executionProfile;
316     }
317 }