Elffuss BraidHub

kikocis/java-driver public

Fix CCMBridge.execute potentially hanging if waiting on output streams.

Found that CCMBridge was hanging in my linux environment when
downloading cassandra 2.1.3.  This was because the output stream was not
being consumed, causing the process to block indefinitely.  See
documentation at
http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html for an
explanation.

Updated to use commons-exec's DefaultExecutor with PumpStreamHandler to
consume and log stdout and stderr as it occurs.
Changesetf3391d2c849ab11a4c3bcc1d237fd844a3295a21bf6a375f901345451b2339a1
AuthorAndrew Tolbert <andrew.tolbert@datastax.com> · 2015-03-10 20:27 -05:00
CommitterAndrew Tolbert <andrew.tolbert@datastax.com> · 2015-04-07 08:13 -05:00
Parents92ae90bf3cd7

Browse the files at this changeset

3 files changed

modified driver-core/pom.xml +7 −0
@@ -111,6 +111,13 @@
111111 </exclusions>
112112 </dependency>
113113
114+ <dependency>
115+ <groupId>org.apache.commons</groupId>
116+ <artifactId>commons-exec</artifactId>
117+ <version>1.3</version>
118+ <scope>test</scope>
119+ </dependency>
120+
114121 </dependencies>
115122
116123 <build>
modified driver-core/src/test/java/com/datastax/driver/core/CCMBridge.java +19 −45
@@ -31,6 +31,7 @@
3131 import com.google.common.io.ByteStreams;
3232 import com.google.common.io.Closer;
3333 import com.google.common.io.Files;
34+import org.apache.commons.exec.*;
3435 import org.slf4j.Logger;
3536 import org.slf4j.LoggerFactory;
3637 import org.testng.annotations.AfterClass;
@@ -226,14 +227,6 @@
226227 execute("ccm node%d remove", n);
227228 }
228229
229- public void ring() {
230- ring(1);
231- }
232-
233- public void ring(int n) {
234- executeAndPrint("ccm node%d ring", n);
235- }
236-
237230 public void bootstrapNode(int n) {
238231 bootstrapNode(n, null);
239232 }
@@ -280,49 +273,30 @@
280273 try {
281274 String fullCommand = String.format(command, args) + " --config-dir=" + ccmDir;
282275 logger.debug("Executing: " + fullCommand);
283- Process p = runtime.exec(fullCommand, null, CASSANDRA_DIR);
284- int retValue = p.waitFor();
285-
286- if (retValue != 0) {
287- BufferedReader outReader = new BufferedReader(new InputStreamReader(p.getInputStream()));
288- BufferedReader errReader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
289-
290- String line = outReader.readLine();
291- while (line != null) {
292- logger.info("out> " + line);
293- line = outReader.readLine();
276+ CommandLine cli = CommandLine.parse(fullCommand);
277+ Executor executor = new DefaultExecutor();
278+
279+ LogOutputStream outStream = new LogOutputStream() {
280+ @Override protected void processLine(String line, int logLevel) {
281+ logger.debug("ccmout> " + line);
294282 }
295- line = errReader.readLine();
296- while (line != null) {
297- logger.error("err> " + line);
298- line = errReader.readLine();
283+ };
284+ LogOutputStream errStream = new LogOutputStream() {
285+ @Override protected void processLine(String line, int logLevel) {
286+ logger.error("ccmerr> " + line);
299287 }
300- throw new RuntimeException();
301- }
302- } catch (IOException e) {
303- throw new RuntimeException(e);
304- } catch (InterruptedException e) {
305- throw new RuntimeException(e);
306- }
307- }
288+ };
308289
309- private void executeAndPrint(String command, Object... args) {
310- try {
311- String fullCommand = String.format(command, args) + " --config-dir=" + ccmDir;
312- logger.debug("Executing: " + fullCommand);
313- Process p = runtime.exec(fullCommand, null, CASSANDRA_DIR);
314- int retValue = p.waitFor();
315-
316- BufferedReader outReaderOutput = new BufferedReader(new InputStreamReader(p.getInputStream()));
317- String line = outReaderOutput.readLine();
318- while (line != null) {
319- System.out.println(line);
320- line = outReaderOutput.readLine();
290+ ExecuteStreamHandler streamHandler = new PumpStreamHandler(outStream, errStream);
291+ executor.setStreamHandler(streamHandler);
292+
293+ int retValue = executor.execute(cli);
294+ if (retValue != 0) {
295+ logger.error("Non-zero exit code ({}) returned from executing ccm command: {}", retValue, fullCommand);
296+ throw new RuntimeException();
321297 }
322298 } catch (IOException e) {
323299 throw new RuntimeException(e);
324- } catch (InterruptedException e) {
325- throw new RuntimeException(e);
326300 }
327301 }
328302
modified driver-examples/osgi/pom.xml +8 −0
@@ -117,12 +117,20 @@
117117 <version>3.1.0</version>
118118 <scope>test</scope>
119119 </dependency>
120+
120121 <dependency>
121122 <groupId>javax.inject</groupId>
122123 <artifactId>javax.inject</artifactId>
123124 <version>1</version>
124125 <scope>test</scope>
125126 </dependency>
127+
128+ <dependency>
129+ <groupId>org.apache.commons</groupId>
130+ <artifactId>commons-exec</artifactId>
131+ <version>1.3</version>
132+ <scope>test</scope>
133+ </dependency>
126134 </dependencies>
127135
128136 <build>