-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1666 from Netflix/virtual-threads-experiment
Fix completablefuture wrapping
- Loading branch information
Showing
3 changed files
with
162 additions
and
39 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
graphql-dgs/src/main/kotlin/com/netflix/graphql/dgs/internal/CompletableFutureWrapper.kt
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,76 @@ | ||
/* | ||
* Copyright 2023 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.graphql.dgs.internal | ||
|
||
import org.reactivestreams.Publisher | ||
import org.springframework.core.task.AsyncTaskExecutor | ||
import java.lang.reflect.Method | ||
import java.util.concurrent.CompletableFuture | ||
import java.util.concurrent.CompletionStage | ||
import kotlin.reflect.KFunction | ||
import kotlin.reflect.KType | ||
import kotlin.reflect.full.isSubtypeOf | ||
import kotlin.reflect.typeOf | ||
|
||
internal class CompletableFutureWrapper(private val taskExecutor: AsyncTaskExecutor?) { | ||
private val supportsReactor: Boolean = try { | ||
Class.forName("org.reactivestreams.Publisher") | ||
true | ||
} catch (ex: Exception) { | ||
false | ||
} | ||
|
||
/** | ||
* Wrap the call to a data fetcher in CompletableFuture to enable parallel behavior. | ||
* Used when virtual threads are enabled. | ||
*/ | ||
fun wrapInCompletableFuture(function: () -> Any?): Any? { | ||
return CompletableFuture.supplyAsync({ | ||
return@supplyAsync function.invoke() | ||
}, taskExecutor) | ||
} | ||
|
||
/** | ||
* Decides if a data fetcher method should be wrapped in CompletableFuture automatically. | ||
* This is only done when a taskExecutor is available, and if the data fetcher doesn't explicitly return CompletableFuture already. | ||
* Used when virtual threads are enabled. | ||
*/ | ||
fun shouldWrapInCompletableFuture(kFunc: KFunction<*>): Boolean { | ||
return taskExecutor != null && | ||
!kFunc.returnType.isSubtypeOf(typeOf<CompletionStage<*>>()) && | ||
!isReactive(kFunc.returnType) | ||
} | ||
|
||
private fun isReactive(returnType: KType): Boolean { | ||
return supportsReactor && returnType.isSubtypeOf(typeOf<Publisher<*>>()) | ||
} | ||
|
||
/** | ||
* Decides if a data fetcher method should be wrapped in CompletableFuture automatically. | ||
* This is only done when a taskExecutor is available, and if the data fetcher doesn't explicitly return CompletableFuture already. | ||
* Used when virtual threads are enabled. | ||
*/ | ||
fun shouldWrapInCompletableFuture(method: Method): Boolean { | ||
return taskExecutor != null && | ||
!CompletionStage::class.java.isAssignableFrom(method.returnType) && | ||
!isReactive(method.returnType) | ||
} | ||
|
||
private fun isReactive(returnType: Class<*>): Boolean { | ||
return supportsReactor && Publisher::class.java.isAssignableFrom(returnType) | ||
} | ||
} |
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
78 changes: 78 additions & 0 deletions
78
graphql-dgs/src/test/kotlin/com/netflix/graphql/dgs/internal/CompletableFutureWrapperTest.kt
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,78 @@ | ||
package com.netflix.graphql.dgs.internal | ||
|
||
import io.mockk.impl.annotations.MockK | ||
import io.mockk.junit5.MockKExtension | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
import org.springframework.core.task.AsyncTaskExecutor | ||
import reactor.core.publisher.Mono | ||
import java.util.concurrent.CompletableFuture | ||
import java.util.function.Function | ||
import kotlin.reflect.jvm.ExperimentalReflectionOnLambdas | ||
import kotlin.reflect.jvm.reflect | ||
|
||
@OptIn(ExperimentalReflectionOnLambdas::class) | ||
@ExtendWith(MockKExtension::class) | ||
class CompletableFutureWrapperTest { | ||
@MockK(relaxUnitFun = true) | ||
lateinit var mockTaskExecutor: AsyncTaskExecutor | ||
|
||
@Test | ||
fun `If no taskExecutor is set, no wrapping should happen`() { | ||
val completableFutureWrapper = CompletableFutureWrapper(null) | ||
|
||
// Check Kotlin method | ||
assertThat(completableFutureWrapper.shouldWrapInCompletableFuture(fun(): String { return "hello" }.reflect()!!)).isFalse() | ||
|
||
// Check Java method | ||
val stringMethod = String::class.java.getMethod("toString") | ||
assertThat(completableFutureWrapper.shouldWrapInCompletableFuture(stringMethod)).isFalse() | ||
} | ||
|
||
@Test | ||
fun `A Kotlin String function should be wrapped`() { | ||
val completableFutureWrapper = CompletableFutureWrapper(VirtualThreadTaskExecutor()) | ||
assertThat(completableFutureWrapper.shouldWrapInCompletableFuture(fun(): String { return "hello" }.reflect()!!)).isTrue() | ||
} | ||
|
||
@Test | ||
fun `A Kotlin CompletableFuture function should not be wrapped`() { | ||
val completableFutureWrapper = CompletableFutureWrapper(VirtualThreadTaskExecutor()) | ||
assertThat(completableFutureWrapper.shouldWrapInCompletableFuture(fun(): CompletableFuture<String> { return CompletableFuture() }.reflect()!!)).isFalse() | ||
} | ||
|
||
@Test | ||
fun `A Kotlin Mono function should not be wrapped`() { | ||
val completableFutureWrapper = CompletableFutureWrapper(VirtualThreadTaskExecutor()) | ||
assertThat(completableFutureWrapper.shouldWrapInCompletableFuture(fun(): Mono<String> { return Mono.just("hi") }.reflect()!!)).isFalse() | ||
} | ||
|
||
@Test | ||
fun `A Java String method should be wrapped`() { | ||
val completableFutureWrapper = CompletableFutureWrapper(VirtualThreadTaskExecutor()) | ||
val stringMethod = String::class.java.getMethod("toString") | ||
assertThat(completableFutureWrapper.shouldWrapInCompletableFuture(stringMethod)).isTrue() | ||
} | ||
|
||
@Test | ||
fun `A Java CompletableFuture method should not be wrapped`() { | ||
val completableFutureWrapper = CompletableFutureWrapper(VirtualThreadTaskExecutor()) | ||
val cfMethod = CompletableFuture::class.java.getMethod("thenApplyAsync", Function::class.java) | ||
assertThat(completableFutureWrapper.shouldWrapInCompletableFuture(cfMethod)).isFalse() | ||
} | ||
|
||
@Test | ||
fun `A Java Mono method should not be wrapped`() { | ||
val completableFutureWrapper = CompletableFutureWrapper(VirtualThreadTaskExecutor()) | ||
val monoMethod = Mono::class.java.getMethod("empty") | ||
assertThat(completableFutureWrapper.shouldWrapInCompletableFuture(monoMethod)).isFalse() | ||
} | ||
|
||
@Test | ||
fun `A method should successfully get wrapped`() { | ||
val completableFutureWrapper = CompletableFutureWrapper(mockTaskExecutor) | ||
val wrapped = completableFutureWrapper.wrapInCompletableFuture { fun(): String { return "hello" } } | ||
assertThat(wrapped).isInstanceOf(CompletableFuture::class.java) | ||
} | ||
} |