-
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 #1818 from Netflix/ejd--new-dataloader-hooks
add new hooks for instrumenting dataloaders
- Loading branch information
Showing
11 changed files
with
518 additions
and
12 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
50 changes: 50 additions & 0 deletions
50
graphql-dgs/src/main/kotlin/com/netflix/graphql/dgs/DgsDataLoaderCustomizer.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,50 @@ | ||
/* | ||
* Copyright 2024 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 | ||
|
||
import org.dataloader.BatchLoader | ||
import org.dataloader.BatchLoaderWithContext | ||
import org.dataloader.MappedBatchLoader | ||
import org.dataloader.MappedBatchLoaderWithContext | ||
|
||
/** | ||
* Beans that implement this interface will be called during the component scan | ||
* that finds defined @DgsDataLoaders and allows the modification or wrapping of | ||
* each DataLoader. | ||
* | ||
* While this hook appears very similar to [com.netflix.graphql.dgs.DataLoaderInstrumentationExtensionProvider] | ||
* there are two important differences: | ||
* - DgsDataLoaderCustomizers are called when scanning for @DgsDataLoaders, whereas the | ||
* DataLoaderInstrumentationExtensionProviders are called once per request | ||
* - DgsDataLoaderCustomizers are afforded the opportunity to change the type of each DataLoader | ||
* as it is being initially registered. Most notably, this allows for converting BatchLoader and MappedBatchLoader | ||
* into their "WithContext" versions, which can simplify certain types of DataLoader instrumentation. | ||
*/ | ||
interface DgsDataLoaderCustomizer { | ||
fun provide(original: BatchLoader<*, *>, name: String): Any { | ||
return original | ||
} | ||
fun provide(original: BatchLoaderWithContext<*, *>, name: String): Any { | ||
return original | ||
} | ||
fun provide(original: MappedBatchLoader<*, *>, name: String): Any { | ||
return original | ||
} | ||
fun provide(original: MappedBatchLoaderWithContext<*, *>, name: String): Any { | ||
return original | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
graphql-dgs/src/main/kotlin/com/netflix/graphql/dgs/DgsDataLoaderInstrumentation.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,38 @@ | ||
/* | ||
* Copyright 2024 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 | ||
|
||
import org.dataloader.BatchLoaderEnvironment | ||
|
||
/** | ||
* Provides a simple interface to define instrumentation around the dispatch of data loaders. | ||
* | ||
* Requires [com.netflix.graphql.dgs.internal.DgsWrapWithContextDataLoaderCustomizer] to be enabled, | ||
* as DgsDataLoaderInstrumentation only provides hooks for the WithContext versions of batch loaders. | ||
*/ | ||
interface DgsDataLoaderInstrumentation { | ||
/** | ||
* onDispatch will run before the data loader itself is actually called. | ||
* | ||
* @param name the name of the data loader | ||
* @param keys the list of keys dispatched to the data loader | ||
* @param batchLoaderEnvironment the batchLoaderEnvironment for the current execution | ||
* | ||
* @return context object that also contains the other hooks | ||
*/ | ||
fun onDispatch(name: String, keys: List<Any>, batchLoaderEnvironment: BatchLoaderEnvironment): DgsDataLoaderInstrumentationContext | ||
} |
31 changes: 31 additions & 0 deletions
31
graphql-dgs/src/main/kotlin/com/netflix/graphql/dgs/DgsDataLoaderInstrumentationContext.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,31 @@ | ||
/* | ||
* Copyright 2024 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 | ||
|
||
interface DgsDataLoaderInstrumentationContext { | ||
/** | ||
* onComplete will run in a whenComplete attached to the data loader's returned CompletableFuture. | ||
* | ||
* @param result the actual results of the data loader. Will be a Map or List depending on the type of data loader. | ||
* @param exception any exception thrown by the data loader | ||
* | ||
* This means it will run on one of two possible threads: | ||
* - the thread associated with the data loader's CompletableFuture | ||
* - the thread that actually dispatched the data loader | ||
*/ | ||
fun onComplete(result: Any?, exception: Any?) | ||
} |
19 changes: 19 additions & 0 deletions
19
...c/main/kotlin/com/netflix/graphql/dgs/exceptions/DgsDataLoaderInstrumentationException.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,19 @@ | ||
/* | ||
* Copyright 2024 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.exceptions | ||
|
||
class DgsDataLoaderInstrumentationException(name: String) : RuntimeException("data loader `$name` is not a MappedBatchLoaderWithContext or BatchLoaderWithContext. Is dgs.graphql.convertAllDataLoadersToWithContext.enabled set to false?") |
89 changes: 89 additions & 0 deletions
89
...tlin/com/netflix/graphql/dgs/internal/DgsDataLoaderInstrumentationDataLoaderCustomizer.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,89 @@ | ||
/* | ||
* Copyright 2024 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 com.netflix.graphql.dgs.DgsDataLoaderCustomizer | ||
import com.netflix.graphql.dgs.DgsDataLoaderInstrumentation | ||
import com.netflix.graphql.dgs.exceptions.DgsDataLoaderInstrumentationException | ||
import org.dataloader.BatchLoader | ||
import org.dataloader.BatchLoaderEnvironment | ||
import org.dataloader.BatchLoaderWithContext | ||
import org.dataloader.MappedBatchLoader | ||
import org.dataloader.MappedBatchLoaderWithContext | ||
import java.util.concurrent.CompletionStage | ||
|
||
class DgsDataLoaderInstrumentationDataLoaderCustomizer( | ||
private val instrumentations: List<DgsDataLoaderInstrumentation> | ||
) : DgsDataLoaderCustomizer { | ||
override fun provide(original: BatchLoader<*, *>, name: String): Any { | ||
throw DgsDataLoaderInstrumentationException(name) | ||
} | ||
|
||
override fun provide(original: BatchLoaderWithContext<*, *>, name: String): Any { | ||
return BatchLoaderWithContextInstrumentationDriver(original, name, instrumentations) | ||
} | ||
|
||
override fun provide(original: MappedBatchLoader<*, *>, name: String): Any { | ||
throw DgsDataLoaderInstrumentationException(name) | ||
} | ||
|
||
override fun provide(original: MappedBatchLoaderWithContext<*, *>, name: String): Any { | ||
return MappedBatchLoaderWithContextInstrumentationDriver(original, name, instrumentations) | ||
} | ||
} | ||
|
||
internal class BatchLoaderWithContextInstrumentationDriver<K : Any, V>( | ||
private val original: BatchLoaderWithContext<K, V>, | ||
private val name: String, | ||
private val instrumentations: List<DgsDataLoaderInstrumentation> | ||
) : BatchLoaderWithContext<K, V> { | ||
override fun load(keys: List<K>, environment: BatchLoaderEnvironment): CompletionStage<List<V>> { | ||
val contexts = instrumentations.map { it.onDispatch(name, keys, environment) } | ||
val future = original.load(keys, environment) | ||
|
||
return future.whenComplete { result, exception -> | ||
try { | ||
contexts.asReversed().forEach { c -> | ||
c.onComplete(result, exception) | ||
} | ||
} catch (_: Throwable) { | ||
} | ||
} | ||
} | ||
} | ||
|
||
internal class MappedBatchLoaderWithContextInstrumentationDriver<K : Any, V>( | ||
private val original: MappedBatchLoaderWithContext<K, V>, | ||
private val name: String, | ||
private val instrumentations: List<DgsDataLoaderInstrumentation> | ||
) : MappedBatchLoaderWithContext<K, V> { | ||
override fun load(keys: Set<K>, environment: BatchLoaderEnvironment): CompletionStage<Map<K, V>> { | ||
val keysList = keys.toList() | ||
val contexts = instrumentations.map { it.onDispatch(name, keysList, environment) } | ||
|
||
val future = original.load(keys, environment) | ||
|
||
return future.whenComplete { result, exception -> | ||
try { | ||
contexts.asReversed().forEach { c -> | ||
c.onComplete(result, exception) | ||
} | ||
} catch (_: Throwable) { | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.