-
Notifications
You must be signed in to change notification settings - Fork 3
/
ClientScript.java
319 lines (262 loc) · 8.47 KB
/
ClientScript.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package info.slony.clustertest.testcoordinator.script;
import info.slony.clustertest.testcoordinator.Coordinator;
import info.slony.clustertest.testcoordinator.Event;
import info.slony.clustertest.testcoordinator.EventSource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.Semaphore;
import org.apache.log4j.Logger;
/**
*
* The ClientScript class encapsulates running a test script on a test client
* instances.
*
*
*
*/
public class ClientScript implements EventSource {
private static Logger log = Logger.getLogger(ClientScript.class);
private String javascript;
private Coordinator coordinator=null;
private List<String> outputBuffer=new ArrayList<String>();
private String logicalDB;
private Properties properties= new Properties();
private Writer scriptWriter;
private boolean isFinished=false;
/**
* The Client connector that connects to the client execution instance.
*/
private ClientConnector client;
/**
* The server that returns Client worker instances for socket based
* conncetions (can be null)
*/
private ClientWorkerServer workerServer;
private Semaphore runningLock = new Semaphore(0);
/**
*
* A thread that monitors the input stream connected to the output of the
* client script.
*
*/
private class StreamMonitorThread implements Runnable {
private String LOCK_REQUEST_PREFIX="REQUEST_LOCK";
private String LOCK_RELEASE_PREFIX="RELEASE_LOCK";
private String ERROR_LOG="ERROR:";
private String FATAL_LOG="FATAL:";
private ClientScript myClientScript=null;
private InputStream inStream = null;
public StreamMonitorThread(InputStream inStream, ClientScript myClientScript) {
this.inStream=inStream;
this.myClientScript=myClientScript;
}
public void run() {
try {
String chunk;
int bytes=0;
BufferedReader streamReader = new BufferedReader(new InputStreamReader(inStream));
while( (chunk = streamReader.readLine())!=null ) {
synchronized(outputBuffer) {
outputBuffer.add(chunk);
}
log.debug(chunk);
if(chunk.startsWith(LOCK_REQUEST_PREFIX)) {
//This line is a request to obtain a lock.
//We launch a task to obtain the lock, and inform the
//client via stdin once the lock has been obtained.
//
//A separate thread is used for this because the client
//script might have multiple worker threads each might
//be requesting different locks and we want to
//be able to process this concurrently.
//
//We can not post an event because the main event
//thread (the one that calls Coordinator.processEvents()
//might itself be blocked on a lock request.
final String lockName = chunk.substring(LOCK_REQUEST_PREFIX.length());
Thread t= new Thread(new Runnable() {
public void run() {
StreamMonitorThread.this.myClientScript.coordinator.obtainLock(lockName);
StreamMonitorThread.this.myClientScript.lockObtained(lockName);
}
}
);
}
else if (chunk.startsWith(LOCK_RELEASE_PREFIX) ) {
//This line is a request to release a lock.
//Unlike the lock request we can do this in this thread.
final String lockName = chunk.substring(LOCK_RELEASE_PREFIX.length());
myClientScript.coordinator.releaseLock(lockName);
}
else if (chunk.startsWith(ERROR_LOG) ||
chunk.startsWith(FATAL_LOG)) {
log.warn("script reports error:"+chunk);
Event event = new Event();
event.eventName=Coordinator.EVENT_ERROR;
event.source=myClientScript;
coordinator.queueEvent(event);
}
else {
Event event = new Event();
event.eventName=Coordinator.EVENT_OUTPUT;
event.source=myClientScript;
coordinator.queueEvent(event);
}
}//while
}
catch(IOException e) {
log.error(e);
}
finally {
runningLock.release();
}
}
};
public ClientScript(String javascript, Coordinator coordinator,String logicalDB,
ClientWorkerServer workerServer) {
this.javascript=javascript;
this.coordinator=coordinator;
this.logicalDB = logicalDB;
this.workerServer=workerServer;
}
/**
* Runs the client script (asynchronously)
*
* This method will start up the client script.
* This method will return immediately it does not wait for the client
* script to finish. It will create a new thread for the client script.
*/
public void run() {
final ClientScript myScript = this;
Thread thread = new Thread(new Runnable() {
public void run() {
myScript.invokeScript();
}
});
thread.start();
}
/**
*
*/
private void invokeScript() {
final ClientScript myClientScript=this;
try {
StringReader reader = new StringReader(javascript);
//Start a thread to monitor outPipeStream.
log.info("Launching a script");
if(workerServer != null) {
client = new WorkerPoolClientConnector(workerServer);
}
else {
client = new JREClientConnector();
}
scriptWriter = new OutputStreamWriter(client.getOutputStream());
writeConfiguration(scriptWriter);
scriptWriter.write(javascript);
scriptWriter.flush();
StreamMonitorThread streamMonitor = new StreamMonitorThread(client.getInputStream(),this);
Thread monitorThread = new Thread(streamMonitor);
monitorThread.start();
try {
runningLock.acquire();
}
catch(InterruptedException e) {
log.error("interrupted exception waiting for a stream monitor to finish",e);
}
client.stop();
}
catch(IOException e) {
e.printStackTrace();
log.error("ioexception:",e);
}
finally {
Event finishedEvent = new Event();
finishedEvent.source=myClientScript;
finishedEvent.eventName=Coordinator.EVENT_FINISHED;
coordinator.queueEvent(finishedEvent);
synchronized(this) {
isFinished=true;
}
coordinator.scriptComplete(this);
}
}
public String getOutput() {
synchronized(outputBuffer) {
if(!outputBuffer.isEmpty()) {
String result = outputBuffer.remove(0);
return result;
}
return null;
}
}
public void setProperties(Properties properties) {
this.properties = properties;
}
/**
* Writes a javascript configuration preamble to writier stream.
*
* This method will write a set of javascript var foo=blah type lines
* that will export configuration information to the javascript script
* that will later (not by this function) be written to the writer/stream.
*
* Configuration values come from the properties data structure.
* @param writer
* @throws IOException
*/
protected void writeConfiguration(Writer writer) throws IOException {
String dbHost = properties.getProperty("database."+this.logicalDB + ".host");
String dbPort = properties.getProperty("database." + this.logicalDB + ".port");
String dbName = properties.getProperty("database." + this.logicalDB + ".dbname");
String jdbcURL = "jdbc:postgresql://" + dbHost;
if(dbPort != null) {
jdbcURL += ":" + dbPort;
}
jdbcURL += "/"+ dbName;
String jdbcUser = properties.getProperty("database."+this.logicalDB + ".user");
String jdbcPassword = properties.getProperty("database."+this.logicalDB + ".password");
log.debug("Starting script with " + jdbcURL + " ," + jdbcUser + "," + jdbcPassword);
writer.write("var JDBC_URL='" +jdbcURL+"';\n");
writer.write("var JDBC_USER='" + jdbcUser+"';\n");
writer.write("var JDBC_PASSWORD='" + jdbcPassword+"';\n");
}
public void stop() {
boolean localFinished=false;
synchronized(this) {
localFinished=isFinished;
}
if(scriptWriter != null && !localFinished) {
try {
log.info("asking the client script to stop");
scriptWriter.write("quit();\n");
scriptWriter.close();
}
catch(IOException e) {
log.debug("exception closing input stream",e);
}
}
if(client != null) {
client.stop();
}
}
public synchronized void lockObtained(String lockName) {
try {
scriptWriter.write("LOCK_OBTAINED:" + lockName + "\n");
}
catch(IOException e) {
//Could not write to output, somewhat serious.
log.error("error writing lock obtained notification",e);
//@todo propagate a serious test failure?
}
}
public synchronized boolean isFinished() {
return this.isFinished;
}
}