Elffuss BraidHub

kikocis/java-driver public

Merge pull request #294 from tolbertam/fixCCMBridgeHang

Fix CCMBridge.execute potentially hanging if waiting on output streams.  Support overriding PATH and JAVA_HOME variables used by CCM.
Changeset5cad9430ebec50efbfa636f6fd2c379124c4032a747a12aa4f5ed741b8743da8
AuthorAndrew Tolbert <andrew.tolbert@datastax.com> · 2015-04-07 08:18 -05:00
CommitterAndrew Tolbert <andrew.tolbert@datastax.com> · 2015-04-07 08:18 -05:00
Parents92ae90bf3cd7, 224ae57a24ac

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 +50 −45
@@ -28,9 +28,11 @@
2828
2929 import com.google.common.base.Joiner;
3030 import com.google.common.collect.ImmutableMap;
31+import com.google.common.collect.Maps;
3132 import com.google.common.io.ByteStreams;
3233 import com.google.common.io.Closer;
3334 import com.google.common.io.Files;
35+import org.apache.commons.exec.*;
3436 import org.slf4j.Logger;
3537 import org.slf4j.LoggerFactory;
3638 import org.testng.annotations.AfterClass;
@@ -71,6 +73,36 @@
7173
7274 private static final File DEFAULT_SERVER_KEYSTORE_FILE = createTempStore(DEFAULT_SERVER_KEYSTORE_PATH);
7375
76+ /**
77+ * The environment variables to use when invoking CCM. Inherits the current processes environment, but will also
78+ * prepend to the PATH variable the value of the 'ccm.path' property and set JAVA_HOME variable to the
79+ * 'ccm.java.home' variable.
80+ *
81+ * At times it is necessary to use a separate java install for CCM then what is being used for running tests.
82+ * For example, if you want to run tests with JDK 6 but against Cassandra 2.0, which requires JDK 7.
83+ */
84+ private static final Map<String,String> ENVIRONMENT_MAP;
85+
86+ static {
87+ // Inherit the current environment.
88+ Map<String,String> envMap = Maps.newHashMap(new ProcessBuilder().environment());
89+ // If ccm.path is set, override the PATH variable with it.
90+ String ccmPath = System.getProperty("ccm.path");
91+ if(ccmPath != null) {
92+ String existingPath = envMap.get("PATH");
93+ if(existingPath == null) {
94+ existingPath = "";
95+ }
96+ envMap.put("PATH", ccmPath + ":" + existingPath);
97+ }
98+ // If ccm.java.home is set, override the JAVA_HOME variable with it.
99+ String ccmJavaHome = System.getProperty("ccm.java.home");
100+ if(ccmJavaHome != null) {
101+ envMap.put("JAVA_HOME", ccmJavaHome);
102+ }
103+ ENVIRONMENT_MAP = ImmutableMap.copyOf(envMap);
104+ }
105+
74106 static final File CASSANDRA_DIR;
75107 static final String CASSANDRA_VERSION;
76108
@@ -226,14 +258,6 @@
226258 execute("ccm node%d remove", n);
227259 }
228260
229- public void ring() {
230- ring(1);
231- }
232-
233- public void ring(int n) {
234- executeAndPrint("ccm node%d ring", n);
235- }
236-
237261 public void bootstrapNode(int n) {
238262 bootstrapNode(n, null);
239263 }
@@ -280,49 +304,30 @@
280304 try {
281305 String fullCommand = String.format(command, args) + " --config-dir=" + ccmDir;
282306 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();
307+ CommandLine cli = CommandLine.parse(fullCommand);
308+ Executor executor = new DefaultExecutor();
309+
310+ LogOutputStream outStream = new LogOutputStream() {
311+ @Override protected void processLine(String line, int logLevel) {
312+ logger.debug("ccmout> " + line);
294313 }
295- line = errReader.readLine();
296- while (line != null) {
297- logger.error("err> " + line);
298- line = errReader.readLine();
314+ };
315+ LogOutputStream errStream = new LogOutputStream() {
316+ @Override protected void processLine(String line, int logLevel) {
317+ logger.error("ccmerr> " + line);
299318 }
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- }
319+ };
308320
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();
321+ ExecuteStreamHandler streamHandler = new PumpStreamHandler(outStream, errStream);
322+ executor.setStreamHandler(streamHandler);
323+
324+ int retValue = executor.execute(cli, ENVIRONMENT_MAP);
325+ if (retValue != 0) {
326+ logger.error("Non-zero exit code ({}) returned from executing ccm command: {}", retValue, fullCommand);
327+ throw new RuntimeException();
321328 }
322329 } catch (IOException e) {
323330 throw new RuntimeException(e);
324- } catch (InterruptedException e) {
325- throw new RuntimeException(e);
326331 }
327332 }
328333
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>