This repository has been archived by the owner on Sep 23, 2020. It is now read-only.
/
XenTask.java
253 lines (205 loc) · 8.73 KB
/
XenTask.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
* Copyright 1999-2008 University of Chicago
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package org.globus.workspace.xen;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.globus.workspace.Lager;
import org.globus.workspace.ReturnException;
import org.globus.workspace.WorkspaceConstants;
import org.globus.workspace.WorkspaceException;
import org.globus.workspace.WorkspaceUtil;
import org.globus.workspace.service.binding.vm.VirtualMachine;
import org.globus.workspace.service.impls.async.ResourceMessage;
import org.globus.workspace.service.impls.async.WorkspaceRequest;
import org.globus.workspace.service.impls.async.WorkspaceRequestContext;
public abstract class XenTask implements WorkspaceRequest {
protected static final Log logger =
LogFactory.getLog(XenTask.class.getName());
protected WorkspaceRequestContext ctx;
protected String name;
protected boolean doFakeLag;
protected boolean doFakeFail;
protected String[] cmd;
// if success just means another asynchronous task was started
// then this task goes away without calling back to the service
protected boolean async;
public void setRequestContext(WorkspaceRequestContext context) {
this.ctx = context;
}
private void done(Exception err) {
if (this.ctx.getNotify() != WorkspaceConstants.STATE_INVALID) {
final ResourceMessage notif =
this.ctx.getLocator().getResourceMessage();
notif.message(this.ctx.getId(), this.ctx.getNotify(), err);
}
}
protected abstract void init() throws WorkspaceException;
// Give children a chance to interpret the context of this invocation
// and possibly track it etc.
protected Exception preExecute(boolean fake) {
return null;
}
// give children a chance to interpret the exception
protected Exception postExecute(Exception e, boolean fake) {
return e;
}
public void execute() {
if (this.ctx == null) {
logger.fatal("request had null request ctx: " + this.name +
" [[ " + this.getClass().getName() + " ]]");
return;
}
try {
this.init();
} catch (WorkspaceException e) {
this.done(e);
return;
}
final boolean fake = this.ctx.getLocator().getGlobalPolicies().isFake();
Exception e = this.preExecute(fake);
if (e == null) {
e = this._execute(fake);
e = this.postExecute(e, fake);
}
final boolean trace = this.ctx.lager().traceLog;
final boolean event = this.ctx.lager().eventLog;
if (this.async && e == null) {
if (trace) {
logger.trace(Lager.id(ctx.getId()) + ": " +
this.name + " async task started");
}
return;
}
if (e == null) {
if (event) {
logger.info(Lager.ev(ctx.getId()) + this.name + " succeeded");
} else if (trace) {
logger.trace(Lager.id(ctx.getId()) + ": " +
this.name + " succeeded");
}
} else {
if (event) {
logger.error(Lager.ev(ctx.getId()) + this.name + " failed");
} else if (trace) {
logger.trace(Lager.id(ctx.getId()) + ": " +
this.name + " failed");
}
}
this.done(e);
}
private Exception _execute(boolean fake) {
final int id = this.ctx.getId();
final boolean traceLog = this.ctx.lager().traceLog;
final boolean eventLog = this.ctx.lager().eventLog;
if (traceLog) {
logger.trace(Lager.id(id) + " " + this.name + ", invoking " +
"command: " + WorkspaceUtil.printCmd(this.cmd));
}
if (fake) {
if (eventLog) {
logger.info(Lager.ev(id) + "banner:" +
"\n\n ***** " + Lager.id(id) + " Fake " + this.name +
" Request Begins\n");
} else if (traceLog) {
logger.trace("banner:" +
"\n\n ***** " + Lager.id(id) + " Fake " + this.name +
" Request Begins\n");
}
if (this.cmd != null) {
logger.info("Command associated with fake '" + this.name +
"' is: " + WorkspaceUtil.printCmd(this.cmd));
}
if (this.doFakeLag) {
final long lag =
this.ctx.getLocator().getGlobalPolicies().getFakelag();
try {
if (lag > 0) {
logger.debug(Lager.id(id) + ": Fake " + this.name +
" request, sleeping for " + lag + " ms");
Thread.sleep(lag);
}
} catch (InterruptedException e) {
logger.error("",e);
}
}
// For testing with mock task system
if (this.doFakeFail) {
return XenUtil.translateReturnException(
new ReturnException(1, "Instructed to fail."));
}
if (eventLog) {
logger.info(Lager.ev(id) + "banner:" +
"\n\n ***** " + Lager.id(id) + " Fake " + this.name +
" Request Ends\n");
} else if (traceLog) {
logger.trace("banner:" +
"\n\n ***** " + Lager.id(id) + " Fake " + this.name +
" Request Ends\n");
} else {
logger.debug(Lager.id(id) + "Fake " + this.name
+ " request complete");
}
// Since this is fake mode, we can cheat all we need to. Simulate
// asynchronous notifications:
if (this.async) {
int stateNotify = WorkspaceConstants.STATE_INVALID;
if (this.name.equals("Propagate-To-Start")) {
stateNotify = WorkspaceConstants.STATE_STARTED;
} else if (this.name.equals("Propagate-Only")) {
stateNotify = WorkspaceConstants.STATE_PROPAGATED;
} else if (this.name.equals("Propagate-To-Pause")) {
stateNotify = WorkspaceConstants.STATE_PAUSED;
} else if (this.name.equals("Ready-For-Transport")) {
stateNotify = WorkspaceConstants.STATE_READY_FOR_TRANSPORT;
}
if (stateNotify != WorkspaceConstants.STATE_INVALID) {
final ResourceMessage notif =
this.ctx.getLocator().getResourceMessage();
notif.message(this.ctx.getId(), stateNotify, null);
}
}
return null;
}
try {
// See WorkspaceResource.isVMMaccessOK() javadoc
if (this.ctx != null && !this.ctx.isVmmAccessOK()) {
final VirtualMachine vm = this.ctx.getVm();
final String err;
if (vm != null) {
err = "Do not run have rights to run " +
this.name + " for VM " + vm.getID() +
" on node " + vm.getNode();
} else {
err = "Do not run have rights to run " +
this.name + " with ctx ID = " + id;
}
return new WorkspaceException(err);
}
// while developing it is sometimes helpful to set cmd to null
// before it is implemented (i.e., make just this one thing fake
// for the timebeing without using the fakeness infrastructure
// for other commands...).
if (this.cmd != null) {
WorkspaceUtil.runCommand(this.cmd, eventLog, traceLog, id);
}
return null;
} catch (ReturnException e) {
return XenUtil.translateReturnException(e);
} catch (WorkspaceException e) {
return e;
}
}
}