Skip to content

Commit d271d5d

Browse files
jsvdmergify[bot]
authored andcommitted
retry catalog queries and jdk downloads (#18345)
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). (cherry picked from commit 458efab)
1 parent 990cdf7 commit d271d5d

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
@@ -45,6 +45,7 @@ import org.logstash.gradle.tooling.ExtractBundledJdkVersion
4545
import org.logstash.gradle.tooling.SignAliasDefinitions
4646
import org.logstash.gradle.tooling.ToolingUtils
4747
import org.logstash.gradle.tooling.SnapshotArtifactURLs
48+
import java.net.HttpURLConnection
4849

4950
allprojects {
5051
group = 'org.logstash'
@@ -708,22 +709,88 @@ class JDKDetails {
708709
if (arch == "aarch64") {
709710
url += "-${arch}"
710711
}
711-
println "Retrieving JDK from catalog..."
712+
println "Retrieving JDK location URL from Elastic catalog..."
712713
def catalogMetadataUrl = URI.create(url).toURL()
713-
def catalogConnection = catalogMetadataUrl.openConnection()
714-
catalogConnection.requestMethod = 'GET'
715-
if (catalogConnection.responseCode != 200) {
716-
println "Can't find adoptiumjdk ${revision} for ${osName} on Elastic JVM catalog"
717-
throw new GradleException("JVM not present on catalog")
714+
int maxAttempts = 5
715+
long retryDelayMillis = 3000
716+
int connectTimeoutMillis = 15000
717+
int readTimeoutMillis = 60000
718+
int lastResponseCode = -1
719+
String lastResponseMessage = null
720+
String lastErrorBody = null
721+
Exception lastException = null
722+
723+
for (int attempt = 1; attempt <= maxAttempts; attempt++) {
724+
HttpURLConnection catalogConnection = null
725+
try {
726+
catalogConnection = catalogMetadataUrl.openConnection() as HttpURLConnection
727+
catalogConnection.requestMethod = 'GET'
728+
catalogConnection.connectTimeout = connectTimeoutMillis
729+
catalogConnection.readTimeout = readTimeoutMillis
730+
731+
int responseCode = catalogConnection.responseCode
732+
if (responseCode == 200) {
733+
def metadataRetrieved = catalogConnection.inputStream.withCloseable { stream -> stream.getText('UTF-8') }
734+
println "Retrieved!"
735+
736+
def catalogMetadata = new JsonSlurper().parseText(metadataRetrieved)
737+
validateMetadata(catalogMetadata)
738+
739+
return catalogMetadata.url
740+
}
741+
742+
lastResponseCode = responseCode
743+
lastResponseMessage = catalogConnection.responseMessage
744+
lastErrorBody = catalogConnection.errorStream?.withCloseable { stream -> stream.getText('UTF-8') }?.trim()
745+
lastException = null
746+
747+
def statusMessage = lastResponseMessage ? "${responseCode} ${lastResponseMessage}" : "${responseCode}"
748+
if (lastErrorBody) {
749+
statusMessage += ": ${lastErrorBody.take(200)}"
750+
}
751+
if (attempt < maxAttempts) {
752+
println "Attempt ${attempt} of ${maxAttempts} failed with HTTP ${statusMessage}. Retrying..."
753+
try {
754+
Thread.sleep(retryDelayMillis)
755+
} catch (InterruptedException interruptedException) {
756+
Thread.currentThread().interrupt()
757+
throw new GradleException("Interrupted while retrying Elastic JVM catalog metadata request", interruptedException)
758+
}
759+
}
760+
} catch (Exception e) {
761+
lastException = e
762+
lastResponseCode = -1
763+
lastResponseMessage = null
764+
lastErrorBody = null
765+
if (attempt < maxAttempts) {
766+
println "Attempt ${attempt} of ${maxAttempts} failed: ${e.message}. Retrying..."
767+
try {
768+
Thread.sleep(retryDelayMillis)
769+
} catch (InterruptedException interruptedException) {
770+
Thread.currentThread().interrupt()
771+
throw new GradleException("Interrupted while retrying Elastic JVM catalog metadata request", interruptedException)
772+
}
773+
}
774+
} finally {
775+
catalogConnection?.disconnect()
776+
}
718777
}
719778

720-
def metadataRetrieved = catalogConnection.content.text
721-
println "Retrieved!"
722-
723-
def catalogMetadata = new JsonSlurper().parseText(metadataRetrieved)
724-
validateMetadata(catalogMetadata)
725-
726-
return catalogMetadata.url
779+
def requestedArtifact = "adoptiumjdk-${revision}+${build}-${osName}${arch == "aarch64" ? "-${arch}" : ""}"
780+
if (lastResponseCode == 404) {
781+
throw new GradleException("Elastic JVM catalog returned 404 for ${requestedArtifact}. Verify the artifact exists.", lastException)
782+
}
783+
if (lastResponseCode != -1) {
784+
def statusMessage = lastResponseMessage ? "${lastResponseCode} ${lastResponseMessage}" : "${lastResponseCode}"
785+
if (lastErrorBody) {
786+
statusMessage += " (${lastErrorBody.take(200)})"
787+
}
788+
throw new GradleException("Failed to fetch Elastic JVM catalog metadata for ${requestedArtifact}. Last HTTP response: ${statusMessage}", lastException)
789+
}
790+
if (lastException != null) {
791+
throw new GradleException("Failed to fetch Elastic JVM catalog metadata for ${requestedArtifact}: ${lastException.message}", lastException)
792+
}
793+
throw new GradleException("Failed to fetch Elastic JVM catalog metadata for ${requestedArtifact}.")
727794
}
728795

729796
//Verify that the artifact metadata correspond to the request, if not throws an error
@@ -796,6 +863,7 @@ tasks.register("downloadJdk", Download) {
796863
src project.ext.jdkURL
797864
onlyIfNewer true
798865
overwrite false
866+
retries 3
799867
quiet true
800868
inputs.file("${projectDir}/versions.yml")
801869
outputs.file(project.ext.jdkDownloadLocation)

0 commit comments

Comments
 (0)