Skip to content

Commit

Permalink
GTC-2823 Fix retries when getting latest version of a dataset
Browse files Browse the repository at this point in the history
I noticed that the retry code in RasterCatalog.getLatestVersion was not
correct. It was using recursion to do one or more retries, but wasn't
actually using any successful response from a retry, but instead still
used the original unsuccessful response. Broke out a separate function
getLatestVersionResponse() to fix the problem. I am still doing 2 more
retries after the first request.

Also, increased the connection timeout from 10 seconds to 20 seconds.

These fixes should make Geotrellis more resilient to slow responses from
the Data API. With a bigger change, I could also get rid of the "latest
version" requests for FCD and AFI analyses, since they don't actually use
the integrated alerts dataset.
  • Loading branch information
danscales committed May 7, 2024
1 parent 0bda796 commit 12779ec
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/main/scala/org/globalforestwatch/config/RasterCatalog.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,35 @@ object RasterCatalog {
}
}

def getLatestVersion(dataset: String, retries: Int = 0): String = {
// Internal function to do a "latest" request to the Data API, doing retries
// recursively as necessary. Return the JSON response. Throws an exception if there
// was no successful response.
def getLatestVersionResponse(dataset: String, retries: Int = 0): HttpResponse[String] = {
val response: HttpResponse[String] = Http(
s"https://data-api.globalforestwatch.org/dataset/${dataset}/latest"
).option(HttpOptions
.followRedirects(true))
.option(HttpOptions.connTimeout(10000))
.option(HttpOptions.connTimeout(20000))
.option(HttpOptions.readTimeout(50000)).asString

if (!response.isSuccess) {
if (retries <= 2) {
Thread.sleep(10000)
getLatestVersion(dataset, retries + 1)
} else {
throw new IllegalArgumentException(
s"Dataset ${dataset} has no latest version or does not exist. Data API response code: ${response.code}"
)
}
if (response.isSuccess) {
response
} else if (retries > 2) {
// Do 3 total tries to get a successful response, then throw an exception.
throw new IllegalArgumentException(
s"Problem accessing latest version of dataset ${dataset}. Data API response code: ${response.code}"
)
} else {
// Sleep for ten seconds and then return any successful retry.
Thread.sleep(10000)
getLatestVersionResponse(dataset, retries + 1)
}
}

/** Get the latest version of a dataset, by calling the Data API. Do several retries if there are errors.
*/
def getLatestVersion(dataset: String): String = {
val response = getLatestVersionResponse(dataset)

val json: Map[String, Any] = jsonStrToMap(response.body)

Expand Down

0 comments on commit 12779ec

Please sign in to comment.