diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2013-07-11 12:22:50 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2013-07-11 12:57:52 +0200 |
commit | 195d307840e5e4aaf6d64fab2227b5d8230108b3 (patch) | |
tree | dd28f7024e1c0985952415ade1ef3f01b8983342 /sonar-batch | |
parent | ac46595adb827cbc615fa6cb2702d00f7bf41937 (diff) | |
download | sonarqube-195d307840e5e4aaf6d64fab2227b5d8230108b3.tar.gz sonarqube-195d307840e5e4aaf6d64fab2227b5d8230108b3.zip |
SONAR-4488 Increase timeout for /batch_bootstrap/db
Diffstat (limited to 'sonar-batch')
3 files changed, 57 insertions, 15 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/DryRunDatabase.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/DryRunDatabase.java index 0f73212e337..e8326e916b1 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/DryRunDatabase.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/DryRunDatabase.java @@ -32,6 +32,7 @@ import org.sonar.api.utils.HttpDownloader.HttpException; import org.sonar.api.utils.SonarException; import java.io.File; +import java.net.SocketTimeoutException; /** * @since 3.4 @@ -45,6 +46,8 @@ public class DryRunDatabase implements BatchComponent { private static final String USER = "sonar"; private static final String PASSWORD = "sonar"; + private static final int DEFAULT_DRY_RUN_READ_TIMEOUT = 60 * 1000; + private final Settings settings; private final ServerClient server; private final TempDirectories tempDirectories; @@ -59,14 +62,19 @@ public class DryRunDatabase implements BatchComponent { if (settings.getBoolean(CoreProperties.DRY_RUN)) { LOG.info("Dry run"); File databaseFile = tempDirectories.getFile("", "dryrun.h2.db"); - downloadDatabase(databaseFile); + + // SONAR-4488 Allow to increase dryRun timeout + int readTimeout = settings.getInt(CoreProperties.DRY_RUN_READ_TIMEOUT); + readTimeout = (readTimeout == 0) ? DEFAULT_DRY_RUN_READ_TIMEOUT : readTimeout; + + downloadDatabase(databaseFile, readTimeout); String databasePath = StringUtils.removeEnd(databaseFile.getAbsolutePath(), ".h2.db"); replaceSettings(databasePath); } } - private void downloadDatabase(File toFile) { + private void downloadDatabase(File toFile, int readTimeout) { String projectKey = null; try { projectKey = settings.getString(CoreProperties.PROJECT_KEY_PROPERTY); @@ -75,15 +83,22 @@ public class DryRunDatabase implements BatchComponent { projectKey = String.format("%s:%s", projectKey, branch); } if (StringUtils.isBlank(projectKey)) { - server.download("/batch_bootstrap/db", toFile); + server.download("/batch_bootstrap/db", toFile, readTimeout); } else { - server.download("/batch_bootstrap/db?project=" + projectKey, toFile); + server.download("/batch_bootstrap/db?project=" + projectKey, toFile, readTimeout); } LOG.debug("Dry Run database size: {}", FileUtils.byteCountToDisplaySize(FileUtils.sizeOf(toFile))); } catch (SonarException e) { Throwable rootCause = Throwables.getRootCause(e); + if (rootCause instanceof SocketTimeoutException) { + // Pico will unwrap the first runtime exception + throw new SonarException(new SonarException(String.format("DryRun database read timed out after %s ms. You can try to increase read timeout with property -D" + + CoreProperties.DRY_RUN_READ_TIMEOUT, + readTimeout), e)); + } if (projectKey != null && (rootCause instanceof HttpException) && (((HttpException) rootCause).getResponseCode() == 401)) { - throw new SonarException(String.format("You don't have access rights to project [%s]", projectKey), e); + // Pico will unwrap the first runtime exception + throw new SonarException(new SonarException(String.format("You don't have access rights to project [%s]", projectKey), e)); } throw e; } diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java index 16028be1299..b04bd67a360 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java @@ -58,8 +58,12 @@ public class ServerClient implements BatchComponent { } public void download(String pathStartingWithSlash, File toFile) { + download(pathStartingWithSlash, toFile, null); + } + + public void download(String pathStartingWithSlash, File toFile, Integer readTimeoutMillis) { try { - InputSupplier<InputStream> inputSupplier = doRequest(pathStartingWithSlash); + InputSupplier<InputStream> inputSupplier = doRequest(pathStartingWithSlash, readTimeoutMillis); Files.copy(inputSupplier, toFile); } catch (HttpDownloader.HttpException he) { throw handleHttpException(he); @@ -73,7 +77,11 @@ public class ServerClient implements BatchComponent { } public String request(String pathStartingWithSlash, boolean wrapHttpException) { - InputSupplier<InputStream> inputSupplier = doRequest(pathStartingWithSlash); + return request(pathStartingWithSlash, wrapHttpException, null); + } + + public String request(String pathStartingWithSlash, boolean wrapHttpException, Integer timeoutMillis) { + InputSupplier<InputStream> inputSupplier = doRequest(pathStartingWithSlash, timeoutMillis); try { return IOUtils.toString(inputSupplier.getInput(), "UTF-8"); } catch (HttpDownloader.HttpException e) { @@ -83,7 +91,7 @@ public class ServerClient implements BatchComponent { } } - private InputSupplier<InputStream> doRequest(String pathStartingWithSlash) { + private InputSupplier<InputStream> doRequest(String pathStartingWithSlash, Integer timeoutMillis) { Preconditions.checkArgument(pathStartingWithSlash.startsWith("/"), "Path must start with slash /"); String path = StringEscapeUtils.escapeHtml(pathStartingWithSlash); @@ -91,9 +99,9 @@ public class ServerClient implements BatchComponent { try { InputSupplier<InputStream> inputSupplier; if (Strings.isNullOrEmpty(getLogin())) { - inputSupplier = downloader.newInputSupplier(uri); + inputSupplier = downloader.newInputSupplier(uri, timeoutMillis); } else { - inputSupplier = downloader.newInputSupplier(uri, getLogin(), getPassword()); + inputSupplier = downloader.newInputSupplier(uri, getLogin(), getPassword(), timeoutMillis); } return inputSupplier; } catch (Exception e) { diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/DryRunDatabaseTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/DryRunDatabaseTest.java index f19dbc663af..5e872ed0151 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/DryRunDatabaseTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/DryRunDatabaseTest.java @@ -32,6 +32,7 @@ import org.sonar.api.utils.HttpDownloader; import org.sonar.api.utils.SonarException; import java.io.File; +import java.net.SocketTimeoutException; import static org.fest.assertions.Assertions.assertThat; import static org.mockito.Mockito.doThrow; @@ -73,7 +74,15 @@ public class DryRunDatabaseTest { public void should_download_database() { new DryRunDatabase(settings, server, tempDirectories).start(); - verify(server).download("/batch_bootstrap/db?project=group:project", databaseFile); + verify(server).download("/batch_bootstrap/db?project=group:project", databaseFile, 60000); + } + + @Test + public void should_download_database_with_overriden_timeout() { + settings.setProperty(CoreProperties.DRY_RUN_READ_TIMEOUT, 80000); + new DryRunDatabase(settings, server, tempDirectories).start(); + + verify(server).download("/batch_bootstrap/db?project=group:project", databaseFile, 80000); } @Test @@ -81,7 +90,7 @@ public class DryRunDatabaseTest { settings.setProperty(CoreProperties.PROJECT_BRANCH_PROPERTY, "mybranch"); new DryRunDatabase(settings, server, tempDirectories).start(); - verify(server).download("/batch_bootstrap/db?project=group:project:mybranch", databaseFile); + verify(server).download("/batch_bootstrap/db?project=group:project:mybranch", databaseFile, 60000); } @Test @@ -97,7 +106,7 @@ public class DryRunDatabaseTest { @Test public void should_fail_on_invalid_role() { - doThrow(new SonarException(new HttpDownloader.HttpException(null, 401))).when(server).download("/batch_bootstrap/db?project=group:project", databaseFile); + doThrow(new SonarException(new HttpDownloader.HttpException(null, 401))).when(server).download("/batch_bootstrap/db?project=group:project", databaseFile, 60000); thrown.expect(SonarException.class); thrown.expectMessage("You don't have access rights to project [group:project]"); @@ -106,8 +115,18 @@ public class DryRunDatabaseTest { } @Test + public void should_fail_on_read_timeout() { + doThrow(new SonarException(new SocketTimeoutException())).when(server).download("/batch_bootstrap/db?project=group:project", databaseFile, 60000); + + thrown.expect(SonarException.class); + thrown.expectMessage("DryRun database read timed out after 60000 ms. You can try to increase read timeout with property -Dsonar.dryRun.readTimeout"); + + new DryRunDatabase(settings, server, tempDirectories).start(); + } + + @Test public void should_fail() { - doThrow(new SonarException("BUG")).when(server).download("/batch_bootstrap/db?project=group:project", databaseFile); + doThrow(new SonarException("BUG")).when(server).download("/batch_bootstrap/db?project=group:project", databaseFile, 60000); thrown.expect(SonarException.class); thrown.expectMessage("BUG"); @@ -120,6 +139,6 @@ public class DryRunDatabaseTest { // on non-scan tasks settings.removeProperty(CoreProperties.PROJECT_KEY_PROPERTY); new DryRunDatabase(settings, server, tempDirectories).start(); - verify(server).download("/batch_bootstrap/db", databaseFile); + verify(server).download("/batch_bootstrap/db", databaseFile, 60000); } } |