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

Remove IO.blocking from array copy in S3 client. #1265

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 12 additions & 28 deletions fs2-aws-s3/src/main/scala/fs2/aws/s3/S3.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fs2.aws.s3

import cats.effect.*
import cats.implicits.*
import cats.effect.Concurrent
import cats.syntax.all.*
import cats.{Applicative, ApplicativeThrow, ~>}
import eu.timepit.refined.auto.*
import fs2.aws.s3.S3.MultipartETagValidation
Expand All @@ -13,8 +13,6 @@ import software.amazon.awssdk.core.async.{AsyncRequestBody, AsyncResponseTransfo
import software.amazon.awssdk.services.s3.model.*

import java.security.MessageDigest
import scala.collection.immutable.ArraySeq
import scala.collection.immutable.ArraySeq.unsafeWrapArray
import scala.jdk.CollectionConverters.*
import scala.util.control.NoStackTrace

Expand Down Expand Up @@ -94,7 +92,7 @@ object S3 {
}

/** It creates an instance of the purely functional S3 API. */
def create[F[_]: Async](s3: S3AsyncClientOp[F]): S3[F] =
def create[F[_]: Concurrent](s3: S3AsyncClientOp[F]): S3[F] =
new S3[F] {

/** Deletes a file in a single request.
Expand Down Expand Up @@ -194,12 +192,10 @@ object S3 {
): Pipe[F, List[PartProcessingOutcome], (Option[ETag], Option[Checksum], Option[PartId])] =
_.evalMap[F, (Option[ETag], Option[Checksum], Option[PartId])] {
case Nil =>
cancelUpload(uploadId) *>
Async[F]
.ifM(Async[F].pure(uploadEmptyFiles))(
uploadEmptyFile.map(eTag => (Option(eTag.eTag()), Option.empty[Checksum], Option.empty[PartId])),
Async[F].pure((Option.empty[ETag], Option.empty[Checksum], Option.empty[PartId]))
)
cancelUpload(uploadId) *> (
if (uploadEmptyFiles) uploadEmptyFile.map(eTag => Option(eTag.eTag))
Copy link
Author

Choose a reason for hiding this comment

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

Could also do these two lines w/ like a OptionT.whenF(uploadEmptyFiles)(uploadEmptyFile), etc...

else Applicative[F].pure(Option.empty[ETag])
).map((_, Option.empty[Checksum], Option.empty[PartId]))
case tags =>
val parts = tags.map { case PartProcessingOutcome(t, i, _) =>
CompletedPart.builder().partNumber(i).eTag(t).build()
Expand Down Expand Up @@ -272,7 +268,7 @@ object S3 {
.through(uploadPart(uploadId))
.fold[List[PartProcessingOutcome]](List.empty)(_ :+ _)
.through(completeUpload(uploadId))
.handleErrorWith(ex => fs2.Stream.eval(cancelUpload(uploadId) >> Sync[F].raiseError(ex)))
.handleErrorWith(ex => fs2.Stream.eval(cancelUpload(uploadId) >> ApplicativeThrow[F].raiseError(ex)))
_ <- fs2.Stream.eval(validateETag(eTag, maxPartNum, checksum))
} yield eTag

Expand All @@ -294,7 +290,7 @@ object S3 {
AsyncResponseTransformer.toBytes[GetObjectResponse]
)
)
.flatMap(r => fs2.Stream.chunk(Chunk(ArraySeq.unsafeWrapArray(r.asByteArray)*)))
.flatMap(r => fs2.Stream.chunk(Chunk.array(r.asByteArrayUnsafe)))

/** Reads a file in multiple parts of the specified @partSize per request. Suitable for big files.
*
Expand Down Expand Up @@ -328,23 +324,11 @@ object S3 {
.last
.flatMap {
case Some(resp) =>
Pull.eval {
Async[F].blocking {
val bs = resp.asByteArray()
val len = bs.length
if (len < 0) None else Some(Chunk(unsafeWrapArray(bs)*))
Copy link
Author

Choose a reason for hiding this comment

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

Also, why are we checking to see if the array size is negative? What would that even mean? This SO post says that can't happen: https://stackoverflow.com/a/11530038/283260
Should this just always return Some?

Copy link
Author

Choose a reason for hiding this comment

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

Pushed a second commit removing this length check.

}
}
val chunk = Chunk.array(resp.asByteArrayUnsafe)
Pull.output(chunk) >> (if (chunk.size < chunkSizeBytes) Pull.done else go(offset + chunk.size))
case None =>
Pull.eval(none.pure[F])
}
.flatMap {
case Some(o) =>
if (o.size < chunkSizeBytes) Pull.output(o)
else Pull.output(o) >> go(offset + o.size)
case None => Pull.done
Pull.done
}

go(0).stream
}

Expand Down
Loading