Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(amazonq): Adding backoff and retry for export result archive streaming API. #5320

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

ashishrp-aws
Copy link
Contributor

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Description

We have seen rise of the ExportResultArchive API failures to 15 per week, with retryable error messages.

ExportResultsArchiveError: Unable to execute HTTP request: The connection was closed during the request. The request will usually succeed on a retry, but if it does not: consider disabling any proxies you have configured, enabling debug logging, or performing a TCP dump to identify the root cause. If this is a streaming operation, validate that data is being read or written in a timely manner.

Adding backoff and retry to mitigate these errors.

Checklist

  • My code follows the code style of this project
  • I have added tests to cover my changes
  • A short description of the change has been added to the CHANGELOG if the change is customer-facing in the IDE.
  • I have added metrics for my changes (if required)

License

I confirm that my contribution is made under the terms of the Apache 2.0 license.

@ashishrp-aws ashishrp-aws requested a review from a team as a code owner February 3, 2025 22:03
@ashishrp-aws ashishrp-aws requested a review from a team as a code owner February 3, 2025 22:54
@@ -85,8 +108,43 @@ class AmazonQStreamingClient(private val project: Project) {
return byteBufferList
}

/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: If possible we can move this to a shared place.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Felt it was alright to keep it here in AmazonQStreamingClient class. Assuming that any other streaming APIs will be added here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@ashishrp-aws ashishrp-aws Feb 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this streaming API is a suspend function. tried using a common function for both but way to many conflicts

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can have a thin wrapper for the non-suspending use case instead of duplicating the logic

Comment on lines +128 to +147
fun execute(
operation: () -> T,
isRetryable: (Exception) -> Boolean = { it is RetryableException },
errorHandler: ((Exception, Int) -> Nothing)? = null,
): T {
while (attempts < MAX_RETRY_ATTEMPTS) {
try {
return operation()
} catch (e: Exception) {
attempts++
if (attempts >= MAX_RETRY_ATTEMPTS || !isRetryable(e)) {
errorHandler?.invoke(e, attempts) ?: throw e
}

Thread.sleep(getJitteredDelay())
}
}

throw RuntimeException("Unexpected state after $attempts attempts")
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fun execute(
operation: () -> T,
isRetryable: (Exception) -> Boolean = { it is RetryableException },
errorHandler: ((Exception, Int) -> Nothing)? = null,
): T {
while (attempts < MAX_RETRY_ATTEMPTS) {
try {
return operation()
} catch (e: Exception) {
attempts++
if (attempts >= MAX_RETRY_ATTEMPTS || !isRetryable(e)) {
errorHandler?.invoke(e, attempts) ?: throw e
}
Thread.sleep(getJitteredDelay())
}
}
throw RuntimeException("Unexpected state after $attempts attempts")
}
fun execute(
operation: () -> T,
isRetryable: (Exception) -> Boolean = { it is RetryableException },
errorHandler: ((Exception, Int) -> Nothing)? = null,
): T = runBlocking {
executeSuspend(operation, isRetryable, errorHandler)
}

?

@@ -91,3 +115,60 @@ class AmazonQStreamingClient(private val project: Project) {
fun getInstance(project: Project) = project.service<AmazonQStreamingClient>()
}
}

class RetryableOperation<T> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to its own file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants