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

Prefetch a chunk of result for stream operation #2137

Merged
merged 1 commit into from
Nov 21, 2024

Conversation

wb14123
Copy link
Contributor

@wb14123 wb14123 commented Nov 13, 2024

Resolves #2132.

Use fs2 Stream's prefetchN to buffer query results so that slow downstream operations don't slow down transaction commit.

The buffered result size is chunkSize (equals to fetchSize) by default. If want a different buffer size, can use transactNoPrefetc to get a stream without prefetchN and append any prefetch operations wanted.


// if buffer is less than result set, it will be still block new connection since the result set is not drained
// use sleep to test the result set can be drained
val streamSmallerBuffer = fr"select * from stream_cancel_test".query[Int].stream.transactBuffer(xa, 50)
Copy link
Collaborator

@jatcwang jatcwang Nov 17, 2024

Choose a reason for hiding this comment

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

Hm sorry I can't quite understand how this is testing that the connection is closed before the stream is completed 🤔

An alternative way I can think of to test this is to use some sort of atomic boolean and set it to true when the transactor finishes the transaction, and then you can assert that this was set to true before you process the last chunk of the stream.

  import doobie.util.transactor.Strategy
  import doobie.FC
  import java.util.concurrent.atomic.AtomicBoolean
  val hasClosed = new AtomicBoolean(false)
  xa.copy(
    strategy0 = Strategy.default.copy(
      always = Strategy.default.always.flatMap(_ => FC.delay(hasClosed.set(true)))
    )
  )

It's probably possible to use Deferred instead of AtomicBoolean but there will be more ceremony to instantiates a Deferred inside ConnectionIO

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed the test to assert if it's closed

@wb14123 wb14123 force-pushed the stream-leak branch 2 times, most recently from dc077b6 to 267dfbe Compare November 17, 2024 23:43
@wb14123 wb14123 changed the title [Draft] Stream buffer prototype Prefetch a chunk of result for stream operation Nov 17, 2024
@wb14123 wb14123 force-pushed the stream-leak branch 2 times, most recently from 66da6e4 to 2bd4a8d Compare November 17, 2024 23:54
Resolves typelevel#2132.

Use fs2 Stream's `prefetchN` to buffer query results so that slow
downstream operations don't slow down transaction commit.

The buffered result size is `chunkSize` (equals to `fetchSize`) by
default. If want a different buffer size, can use `transactNoPrefetc` to
get a stream without `prefetchN` and append any prefetch operations
wanted.
@wb14123
Copy link
Contributor Author

wb14123 commented Nov 21, 2024

Hi @jatcwang just follow up on this PR. Please let me know if the updated code addressed your comments or not.

Copy link
Collaborator

@jatcwang jatcwang left a comment

Choose a reason for hiding this comment

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

Thanks for your patience :)

@jatcwang jatcwang merged commit 941f66d into typelevel:main Nov 21, 2024
5 checks passed
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.

Transaction only commited after stream is drained
2 participants