-
Notifications
You must be signed in to change notification settings - Fork 241
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
ashishrp-aws
wants to merge
6
commits into
aws:main
Choose a base branch
from
ashishrp-aws:export
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+260
−60
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
da9928f
Adding backoff and retry for export result archive.
ashishrp-aws 74ec75d
Merge branch 'main' into export
ashishrp-aws 93cd2a7
Adding tests for backoff and retry for export result archive.
ashishrp-aws 1a6ca91
Fix for detektTest errors.
ashishrp-aws f4196bc
Combining retryable into one class with wrapper functions.
ashishrp-aws ab927ba
Merge branch 'main' into export
ashishrp-aws File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -6,18 +6,26 @@ package software.aws.toolkits.jetbrains.services.amazonq.clients | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
import com.intellij.openapi.components.Service | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import com.intellij.openapi.components.service | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import com.intellij.openapi.project.Project | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import kotlinx.coroutines.delay | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import kotlinx.coroutines.future.await | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import software.amazon.awssdk.core.exception.RetryableException | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import software.amazon.awssdk.core.exception.SdkException | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import software.amazon.awssdk.services.codewhispererstreaming.CodeWhispererStreamingAsyncClient | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import software.amazon.awssdk.services.codewhispererstreaming.model.ExportContext | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import software.amazon.awssdk.services.codewhispererstreaming.model.ExportIntent | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import software.amazon.awssdk.services.codewhispererstreaming.model.ExportResultArchiveResponseHandler | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import software.amazon.awssdk.services.codewhispererstreaming.model.ThrottlingException | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import software.amazon.awssdk.services.codewhispererstreaming.model.ValidationException | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import software.aws.toolkits.core.utils.getLogger | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import software.aws.toolkits.core.utils.warn | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import software.aws.toolkits.jetbrains.core.awsClient | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import software.aws.toolkits.jetbrains.core.credentials.ToolkitConnectionManager | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import software.aws.toolkits.jetbrains.core.credentials.pinning.QConnection | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import java.time.Instant | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import java.util.concurrent.TimeoutException | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import java.util.concurrent.atomic.AtomicReference | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import javax.naming.ServiceUnavailableException | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import kotlin.random.Random | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@Service(Service.Level.PROJECT) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class AmazonQStreamingClient(private val project: Project) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -54,30 +62,46 @@ class AmazonQStreamingClient(private val project: Project) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
val checksum = AtomicReference("") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
val result = streamingBearerClient().exportResultArchive( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
it.exportId(exportId) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
it.exportIntent(exportIntent) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
it.exportContext(exportContext) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
RetryableOperation<Unit>().executeSuspend( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
operation = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
val result = streamingBearerClient().exportResultArchive( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
it.exportId(exportId) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
it.exportIntent(exportIntent) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
it.exportContext(exportContext) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ExportResultArchiveResponseHandler.builder().subscriber( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ExportResultArchiveResponseHandler.Visitor.builder() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.onBinaryMetadataEvent { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
checksum.set(it.contentChecksum()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}.onBinaryPayloadEvent { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
val payloadBytes = it.bytes().asByteArray() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
byteBufferList.add(payloadBytes) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}.onDefault { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
LOG.warn { "Received unknown payload stream: $it" } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.build() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.build() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
result.await() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ExportResultArchiveResponseHandler.builder().subscriber( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ExportResultArchiveResponseHandler.Visitor.builder() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.onBinaryMetadataEvent { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
checksum.set(it.contentChecksum()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}.onBinaryPayloadEvent { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
val payloadBytes = it.bytes().asByteArray() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
byteBufferList.add(payloadBytes) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}.onDefault { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
LOG.warn { "Received unknown payload stream: $it" } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.build() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.build() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
isRetryable = { e -> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
when (e) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
is ValidationException, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
is ThrottlingException, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
is ServiceUnavailableException, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
is SdkException, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
is TimeoutException, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
-> true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
else -> false | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
errorHandler = { e, attempts -> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
onError(e) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
throw e | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
result.await() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} catch (e: Exception) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
onError(e) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
throw e | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} finally { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
onStreamingFinished(startTime) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -91,3 +115,60 @@ class AmazonQStreamingClient(private val project: Project) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
fun getInstance(project: Project) = project.service<AmazonQStreamingClient>() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class RetryableOperation<T> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
private var attempts = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
private var currentDelay = INITIAL_DELAY | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
private fun getJitteredDelay(): Long { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
currentDelay = (currentDelay * 2).coerceAtMost(MAX_BACKOFF) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return (currentDelay * (0.5 + Random.nextDouble(0.5))).toLong() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+128
to
+147
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
? |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
suspend fun executeSuspend( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
operation: suspend () -> T, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
isRetryable: (Exception) -> Boolean = { it is RetryableException }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
errorHandler: (suspend (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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
delay(getJitteredDelay()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
throw RuntimeException("Unexpected state after $attempts attempts") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
companion object { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
private const val INITIAL_DELAY = 100L | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
private const val MAX_BACKOFF = 10000L | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
private const val MAX_RETRY_ATTEMPTS = 3 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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