Skip to content

Commit cacf5f9

Browse files
committed
retry catalog queries and jdk downloads
reduce CI failures when Elastic JDK catalog is slow to respond by retrying. Also retry download of the JDK itself (even though it doesn't typically fail).
1 parent 21ac3d8 commit cacf5f9

File tree

1 file changed

+81
-13
lines changed

1 file changed

+81
-13
lines changed

build.gradle

Lines changed: 81 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import org.logstash.gradle.tooling.ExtractBundledJdkVersion
4747
import org.logstash.gradle.tooling.SignAliasDefinitions
4848
import org.logstash.gradle.tooling.ToolingUtils
4949
import org.logstash.gradle.tooling.SnapshotArtifactURLs
50+
import java.net.HttpURLConnection
5051

5152
allprojects {
5253
group = 'org.logstash'
@@ -760,22 +761,88 @@ class JDKDetails {
760761
if (arch == "aarch64") {
761762
url += "-${arch}"
762763
}
763-
println "Retrieving JDK from catalog..."
764+
println "Retrieving JDK location URL from Elastic catalog..."
764765
def catalogMetadataUrl = URI.create(url).toURL()
765-
def catalogConnection = catalogMetadataUrl.openConnection()
766-
catalogConnection.requestMethod = 'GET'
767-
if (catalogConnection.responseCode != 200) {
768-
println "Can't find adoptiumjdk ${revision} for ${osName} on Elastic JVM catalog"
769-
throw new GradleException("JVM not present on catalog")
766+
int maxAttempts = 5
767+
long retryDelayMillis = 3000
768+
int connectTimeoutMillis = 15000
769+
int readTimeoutMillis = 60000
770+
int lastResponseCode = -1
771+
String lastResponseMessage = null
772+
String lastErrorBody = null
773+
Exception lastException = null
774+
775+
for (int attempt = 1; attempt <= maxAttempts; attempt++) {
776+
HttpURLConnection catalogConnection = null
777+
try {
778+
catalogConnection = catalogMetadataUrl.openConnection() as HttpURLConnection
779+
catalogConnection.requestMethod = 'GET'
780+
catalogConnection.connectTimeout = connectTimeoutMillis
781+
catalogConnection.readTimeout = readTimeoutMillis
782+
783+
int responseCode = catalogConnection.responseCode
784+
if (responseCode == 200) {
785+
def metadataRetrieved = catalogConnection.inputStream.withCloseable { stream -> stream.getText('UTF-8') }
786+
println "Retrieved!"
787+
788+
def catalogMetadata = new JsonSlurper().parseText(metadataRetrieved)
789+
validateMetadata(catalogMetadata)
790+
791+
return catalogMetadata.url
792+
}
793+
794+
lastResponseCode = responseCode
795+
lastResponseMessage = catalogConnection.responseMessage
796+
lastErrorBody = catalogConnection.errorStream?.withCloseable { stream -> stream.getText('UTF-8') }?.trim()
797+
lastException = null
798+
799+
def statusMessage = lastResponseMessage ? "${responseCode} ${lastResponseMessage}" : "${responseCode}"
800+
if (lastErrorBody) {
801+
statusMessage += ": ${lastErrorBody.take(200)}"
802+
}
803+
if (attempt < maxAttempts) {
804+
println "Attempt ${attempt} of ${maxAttempts} failed with HTTP ${statusMessage}. Retrying..."
805+
try {
806+
Thread.sleep(retryDelayMillis)
807+
} catch (InterruptedException interruptedException) {
808+
Thread.currentThread().interrupt()
809+
throw new GradleException("Interrupted while retrying Elastic JVM catalog metadata request", interruptedException)
810+
}
811+
}
812+
} catch (Exception e) {
813+
lastException = e
814+
lastResponseCode = -1
815+
lastResponseMessage = null
816+
lastErrorBody = null
817+
if (attempt < maxAttempts) {
818+
println "Attempt ${attempt} of ${maxAttempts} failed: ${e.message}. Retrying..."
819+
try {
820+
Thread.sleep(retryDelayMillis)
821+
} catch (InterruptedException interruptedException) {
822+
Thread.currentThread().interrupt()
823+
throw new GradleException("Interrupted while retrying Elastic JVM catalog metadata request", interruptedException)
824+
}
825+
}
826+
} finally {
827+
catalogConnection?.disconnect()
828+
}
770829
}
771830

772-
def metadataRetrieved = catalogConnection.content.text
773-
println "Retrieved!"
774-
775-
def catalogMetadata = new JsonSlurper().parseText(metadataRetrieved)
776-
validateMetadata(catalogMetadata)
777-
778-
return catalogMetadata.url
831+
def requestedArtifact = "adoptiumjdk-${revision}+${build}-${osName}${arch == "aarch64" ? "-${arch}" : ""}"
832+
if (lastResponseCode == 404) {
833+
throw new GradleException("Elastic JVM catalog returned 404 for ${requestedArtifact}. Verify the artifact exists.", lastException)
834+
}
835+
if (lastResponseCode != -1) {
836+
def statusMessage = lastResponseMessage ? "${lastResponseCode} ${lastResponseMessage}" : "${lastResponseCode}"
837+
if (lastErrorBody) {
838+
statusMessage += " (${lastErrorBody.take(200)})"
839+
}
840+
throw new GradleException("Failed to fetch Elastic JVM catalog metadata for ${requestedArtifact}. Last HTTP response: ${statusMessage}", lastException)
841+
}
842+
if (lastException != null) {
843+
throw new GradleException("Failed to fetch Elastic JVM catalog metadata for ${requestedArtifact}: ${lastException.message}", lastException)
844+
}
845+
throw new GradleException("Failed to fetch Elastic JVM catalog metadata for ${requestedArtifact}.")
779846
}
780847

781848
//Verify that the artifact metadata correspond to the request, if not throws an error
@@ -848,6 +915,7 @@ tasks.register("downloadJdk", Download) {
848915
src project.ext.jdkURL
849916
onlyIfNewer true
850917
overwrite false
918+
retries 3
851919
quiet true
852920
inputs.file("${projectDir}/versions.yml")
853921
outputs.file(project.ext.jdkDownloadLocation)

0 commit comments

Comments
 (0)