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

Add support to provide your own executor #114

Merged
merged 1 commit into from
Oct 16, 2023
Merged

Conversation

slinkydeveloper
Copy link
Contributor

@slinkydeveloper slinkydeveloper commented Oct 9, 2023

Add method to support providing your own executor for blocking services.

This allows to integrate with virtual threads:

RestateHttpEndpointBuilder.builder()
                    .withService(BlockingGreeterService(), Executors.newVirtualThreadPerTaskExecutor())

I tested this feature locally with the virtual thread executor and it works. Unfortunately I couldn't easily setup a test within this project because i need to change the minimum jdk version, and i need to use a beta version of the kotlin plugin. Perhaps we can add a test to the e2e repo.

@github-actions
Copy link
Contributor

github-actions bot commented Oct 9, 2023

Unit Test Results

145 tests  ±0   145 ✔️ ±0   8s ⏱️ ±0s
  15 suites ±0       0 💤 ±0 
  15 files   ±0       0 ±0 

Results for commit b6a14c8. ± Comparison against base commit 529f59f.

♻️ This comment has been updated with latest results.

Copy link
Contributor

@tillrohrmann tillrohrmann left a comment

Choose a reason for hiding this comment

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

The change looks good to me. +1 for merging. Thanks for providing this PR @slinkydeveloper.

+1 for adding a test to our e2e test suite.

@tillrohrmann
Copy link
Contributor

Unfortunately I couldn't easily setup a test within this project because i need to change the minimum jdk version, and i need to use a beta version of the kotlin plugin.

Do you need to use a beta version of the Kotlin plugin to make the SDK work with Java 21?

What should be the minimum required Java version for the SDK? Currently it is Java 11, right?

Are there some benefits of running the services in a virtual thread? I guess when doing blocking I/O. Would this also help when waiting on a future result?

@slinkydeveloper
Copy link
Contributor Author

slinkydeveloper commented Oct 16, 2023

Do you need to use a beta version of the Kotlin plugin to make the SDK work with Java 21?

Yes, Kotlin on JDK 21 is not GA yet. Will be in december I think, the JDK 21 is just too new.

What should be the minimum required Java version for the SDK? Currently it is Java 11, right?

Yes, we had no reason to bump it to 17 yet. 21 is the latest LTS, 17 is the previous LTS.

Are there some benefits of running the services in a virtual thread?

As the current status quo of the sdk, when combined with the Vert.x http implementation, you get synchronous coding style in java (no futures, callbacks, etc) very cheaply on top of async i/o. Without virtual threads, you would use vertx.executeBlocking which under the hood uses a thread pool to achieve the same result. This thread pool is also unbounded, meaning that it potentially spawns one OS thread per connection in the worst case.

Would this also help when waiting on a future result?

In the virtual threads world you don't use futures.

I think in general, independently from virtual threads or not, this change is useful, as I can see users that want to bring their own executors to run their own code.

@tillrohrmann
Copy link
Contributor

tillrohrmann commented Oct 16, 2023

As the current status quo of the sdk, when combined with the Vert.x http implementation, you get synchronous coding style in java (no futures, callbacks, etc) very cheaply on top of async i/o. Without virtual threads, you would use vertx.executeBlocking which under the hood uses a thread pool to achieve the same result. This thread pool is also unbounded, meaning that it potentially spawns one OS thread per connection in the worst case.

This is beneficial if the users calls an external service from a side effect closure via some http client, I assume, right?

For the communication between the vertx based Restate communication layer and the service code we block on some CompleteableFutures, right? Would this also benefit from virtual threads or would this simply result in a blocked platform thread as before because the virtual thread cannot be unmounted?

@slinkydeveloper
Copy link
Contributor Author

slinkydeveloper commented Oct 16, 2023

This is beneficial if the users calls an external service from a side effect closure via some http client, I assume, right?

I guess so, also this is also beneficial if you have many concurrent requests to this service endpoint, because for each of them you spawn one of these virtual threads.

For the communication between the vertx based Restate communication layer and the service code we block on some CompleteableFutures, right? Would this also benefit from virtual threads or would this simply result in a blocked platform thread as before because the virtual thread cannot be unmounted?

As far as i understood, all the locking primitives are virtual threads aware, so it would not block the OS thread but rather execute another virtual thread. But i'm fuzzy on the details TBH: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Thread.html#virtual-threads

@slinkydeveloper slinkydeveloper merged commit 0058656 into main Oct 16, 2023
3 checks passed
@slinkydeveloper slinkydeveloper deleted the issues/executor branch October 16, 2023 10:40
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.

2 participants