-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
always provide logging and exception for batch writes with retries
- Loading branch information
Showing
8 changed files
with
112 additions
and
128 deletions.
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
2 changes: 2 additions & 0 deletions
2
...jection-dynamodb/src/main/mima-filters/1.6.5.backwards.excludes/offset-store-dao.excludes
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# internal | ||
ProblemFilters.exclude[MissingClassProblem]("akka.projection.dynamodb.internal.OffsetStoreDao$BatchWriteFailed") |
69 changes: 69 additions & 0 deletions
69
akka-projection-dynamodb/src/main/scala/akka/projection/dynamodb/Requests.scala
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright (C) 2024 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
package akka.projection.dynamodb | ||
|
||
import scala.concurrent.ExecutionContext | ||
import scala.concurrent.Future | ||
import scala.concurrent.duration.FiniteDuration | ||
import scala.jdk.CollectionConverters._ | ||
|
||
import akka.actor.typed.ActorSystem | ||
import akka.pattern.BackoffSupervisor | ||
import akka.pattern.after | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
import software.amazon.awssdk.services.dynamodb.model.BatchWriteItemResponse | ||
|
||
object Requests { | ||
|
||
final class BatchWriteFailed(val lastResponse: BatchWriteItemResponse) | ||
extends RuntimeException( | ||
s"Failed to batch write all items, [${batchUnprocessedTotal(lastResponse)}] unprocessed items remaining") | ||
|
||
private[akka] val log: Logger = LoggerFactory.getLogger(getClass) | ||
|
||
private[akka] def batchUnprocessedTotal(response: BatchWriteItemResponse): Int = | ||
response.unprocessedItems.asScala.valuesIterator.map(_.size).sum | ||
|
||
private[akka] def retry[Request, Response]( | ||
request: Request, | ||
attempt: Request => Future[Response], | ||
decideRetry: (Request, Response) => Option[Request], | ||
maxRetries: Int, | ||
minBackoff: FiniteDuration, | ||
maxBackoff: FiniteDuration, | ||
randomFactor: Double, | ||
onRetry: (Response, Int, FiniteDuration) => Unit, | ||
failOnMaxRetries: Response => Throwable, | ||
retries: Int = 0)(implicit system: ActorSystem[_]): Future[Seq[Response]] = { | ||
import system.executionContext | ||
attempt(request).flatMap { response => | ||
decideRetry(request, response) match { | ||
case Some(nextRequest) => | ||
if (retries >= maxRetries) { | ||
Future.failed(failOnMaxRetries(response)) | ||
} else { // retry after exponential backoff | ||
val nextRetry = retries + 1 | ||
val delay = BackoffSupervisor.calculateDelay(retries, minBackoff, maxBackoff, randomFactor) | ||
onRetry(response, nextRetry, delay) | ||
after(delay) { | ||
retry( | ||
nextRequest, | ||
attempt, | ||
decideRetry, | ||
maxRetries, | ||
minBackoff, | ||
maxBackoff, | ||
randomFactor, | ||
onRetry, | ||
failOnMaxRetries, | ||
nextRetry) | ||
}.map { responses => response +: responses }(ExecutionContext.parasitic) | ||
} | ||
case None => Future.successful(Seq(response)) | ||
} | ||
} | ||
} | ||
} |
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
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