-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Alternative fix to dead lock challenge #1255
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
Merged
bbakerman
merged 18 commits into
graphql-java:master
from
bbakerman:hanging_dataloader_callstack
Oct 3, 2018
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
38dfab1
Added @Override as part of errorprone code health check
bbakerman a90a885
Revert "Added @Override as part of errorprone code health check"
bbakerman 2653ea0
Merge remote-tracking branch 'upstream/master'
bbakerman ead56c4
Merge remote-tracking branch 'upstream/master'
bbakerman c893cee
Merge remote-tracking branch 'upstream/master'
bbakerman 654fd8f
Merge remote-tracking branch 'upstream/master'
bbakerman bb2b874
Merge remote-tracking branch 'upstream/master'
bbakerman d149b85
Merge remote-tracking branch 'upstream/master'
bbakerman e4f451c
Merge remote-tracking branch 'upstream/master'
bbakerman 79a4df8
Merge remote-tracking branch 'upstream/master'
bbakerman b116476
Merge remote-tracking branch 'upstream/master'
bbakerman d652315
Merge remote-tracking branch 'upstream/master'
bbakerman 9e59603
Merge remote-tracking branch 'upstream/master'
bbakerman cda28e6
Merge remote-tracking branch 'upstream/master'
bbakerman 735a989
Merge remote-tracking branch 'upstream/master'
bbakerman 25ed640
Merge remote-tracking branch 'upstream/master'
bbakerman 75d21b1
Brads attempt at https://github.com/graphql-java/graphql-java/pull/1234
bbakerman c88ba77
Missed the test
bbakerman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
219 changes: 219 additions & 0 deletions
219
src/test/groovy/graphql/execution/instrumentation/dataloader/DataLoaderHangingTest.groovy
This file contains hidden or 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,219 @@ | ||
| package graphql.execution.instrumentation.dataloader | ||
|
|
||
| import graphql.ExecutionInput | ||
| import graphql.ExecutionResult | ||
| import graphql.GraphQL | ||
| import graphql.TestUtil | ||
| import graphql.execution.Async | ||
| import graphql.schema.DataFetcher | ||
| import graphql.schema.DataFetchingEnvironment | ||
| import graphql.schema.idl.RuntimeWiring | ||
| import org.apache.commons.lang3.concurrent.BasicThreadFactory | ||
| import org.dataloader.BatchLoader | ||
| import org.dataloader.DataLoader | ||
| import org.dataloader.DataLoaderOptions | ||
| import org.dataloader.DataLoaderRegistry | ||
| import spock.lang.Specification | ||
|
|
||
| import java.util.concurrent.CompletableFuture | ||
| import java.util.concurrent.CompletionStage | ||
| import java.util.concurrent.SynchronousQueue | ||
| import java.util.concurrent.ThreadFactory | ||
| import java.util.concurrent.ThreadPoolExecutor | ||
| import java.util.concurrent.TimeUnit | ||
|
|
||
| import static graphql.schema.idl.TypeRuntimeWiring.newTypeWiring | ||
|
|
||
| class DataLoaderHangingTest extends Specification { | ||
|
|
||
| public static final int NUM_OF_REPS = 50 | ||
|
|
||
| def "deadlock attempt"() { | ||
| setup: | ||
| def sdl = """ | ||
| type Album { | ||
| id: ID! | ||
| title: String! | ||
| artist: Artist | ||
| songs( | ||
| limit: Int, | ||
| nextToken: String | ||
| ): ModelSongConnection | ||
| } | ||
|
|
||
| type Artist { | ||
| id: ID! | ||
| name: String! | ||
| albums( | ||
| limit: Int, | ||
| nextToken: String | ||
| ): ModelAlbumConnection | ||
| songs( | ||
| limit: Int, | ||
| nextToken: String | ||
| ): ModelSongConnection | ||
| } | ||
|
|
||
| type ModelAlbumConnection { | ||
| items: [Album] | ||
| nextToken: String | ||
| } | ||
|
|
||
| type ModelArtistConnection { | ||
| items: [Artist] | ||
| nextToken: String | ||
| } | ||
|
|
||
| type ModelSongConnection { | ||
| items: [Song] | ||
| nextToken: String | ||
| } | ||
|
|
||
| type Query { | ||
| listArtists(limit: Int, nextToken: String): ModelArtistConnection | ||
| } | ||
|
|
||
| type Song { | ||
| id: ID! | ||
| title: String! | ||
| artist: Artist | ||
| album: Album | ||
| } | ||
| """ | ||
|
|
||
| ThreadFactory threadFactory = new BasicThreadFactory.Builder() | ||
| .namingPattern("resolver-chain-thread-%d").build() | ||
| def executor = new ThreadPoolExecutor(15, 15, 0L, | ||
| TimeUnit.MILLISECONDS, new SynchronousQueue<>(), threadFactory, | ||
| new ThreadPoolExecutor.CallerRunsPolicy()) | ||
|
|
||
| def dataLoaderAlbums = new DataLoader<Object, Object>(new BatchLoader<DataFetchingEnvironment, List<Object>>() { | ||
| @Override | ||
| CompletionStage<List<List<Object>>> load(List<DataFetchingEnvironment> keys) { | ||
| return CompletableFuture.supplyAsync({ | ||
| def limit = keys.first().getArgument("limit") as Integer | ||
| return keys.collect({ k -> | ||
| def albums = [] | ||
| for (int i = 1; i <= limit; i++) { | ||
| albums.add(['id': "artist-$k.source.id-$i", 'title': "album-$i"]) | ||
| } | ||
| def albumsConnection = ['nextToken': 'album-next', 'items': albums] | ||
| return albumsConnection | ||
| }) | ||
| }, executor) | ||
| } | ||
| }, DataLoaderOptions.newOptions().setMaxBatchSize(5)) | ||
|
|
||
| def dataLoaderSongs = new DataLoader<Object, Object>(new BatchLoader<DataFetchingEnvironment, List<Object>>() { | ||
| @Override | ||
| CompletionStage<List<List<Object>>> load(List<DataFetchingEnvironment> keys) { | ||
| return CompletableFuture.supplyAsync({ | ||
| def limit = keys.first().getArgument("limit") as Integer | ||
| return keys.collect({ k -> | ||
| def songs = [] | ||
| for (int i = 1; i <= limit; i++) { | ||
| songs.add(['id': "album-$k.source.id-$i", 'title': "song-$i"]) | ||
| } | ||
| def songsConnection = ['nextToken': 'song-next', 'items': songs] | ||
| return songsConnection | ||
| }) | ||
| }, executor) | ||
| } | ||
| }, DataLoaderOptions.newOptions().setMaxBatchSize(5)) | ||
|
|
||
| def dataLoaderRegistry = new DataLoaderRegistry() | ||
| dataLoaderRegistry.register("artist.albums", dataLoaderAlbums) | ||
| dataLoaderRegistry.register("album.songs", dataLoaderSongs) | ||
|
|
||
|
|
||
| def albumsDf = new MyForwardingDataFetcher(dataLoaderAlbums) | ||
| def songsDf = new MyForwardingDataFetcher(dataLoaderSongs) | ||
|
|
||
| def dataFetcherArtists = new DataFetcher() { | ||
| @Override | ||
| Object get(DataFetchingEnvironment environment) { | ||
| def limit = environment.getArgument("limit") as Integer | ||
| def artists = [] | ||
| for (int i = 1; i <= limit; i++) { | ||
| artists.add(['id': "artist-$i", 'name': "artist-$i"]) | ||
| } | ||
| return ['nextToken': 'artist-next', 'items': artists] | ||
| } | ||
| } | ||
|
|
||
| def wiring = RuntimeWiring.newRuntimeWiring() | ||
| .type(newTypeWiring("Query") | ||
| .dataFetcher("listArtists", dataFetcherArtists)) | ||
| .type(newTypeWiring("Artist") | ||
| .dataFetcher("albums", albumsDf)) | ||
| .type(newTypeWiring("Album") | ||
| .dataFetcher("songs", songsDf)) | ||
| .build() | ||
|
|
||
| def schema = TestUtil.schema(sdl, wiring) | ||
|
|
||
| when: | ||
| def graphql = GraphQL.newGraphQL(schema) | ||
| .instrumentation(new DataLoaderDispatcherInstrumentation(dataLoaderRegistry)) | ||
| .build() | ||
|
|
||
| then: "execution shouldn't hang" | ||
| List<CompletableFuture<ExecutionResult>> futures = [] | ||
| for (int i = 0; i < NUM_OF_REPS; i++) { | ||
| def result = graphql.executeAsync(ExecutionInput.newExecutionInput() | ||
| .query(""" | ||
| query getArtistsWithData { | ||
| listArtists(limit: 1) { | ||
| items { | ||
| name | ||
| albums(limit: 200) { | ||
| items { | ||
| title | ||
| # Uncommenting the following causes query to timeout | ||
| songs(limit: 5) { | ||
| nextToken | ||
| items { | ||
| title | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| """) | ||
| .build()) | ||
| result.whenComplete({ res, error -> | ||
| if (error) { | ||
| throw error | ||
| } | ||
| assert res.errors.empty | ||
| }) | ||
| // add all futures | ||
| futures.add(result) | ||
| } | ||
| // wait for each future to complete and grab the results | ||
| Async.each(futures) | ||
| .whenComplete({ results, error -> | ||
| if (error) { | ||
| throw error | ||
| } | ||
| results.each { assert it.errors.empty } | ||
| }) | ||
| .join() | ||
| } | ||
|
|
||
| static class MyForwardingDataFetcher implements DataFetcher<CompletableFuture<Object>> { | ||
|
|
||
| private final DataLoader dataLoader | ||
|
|
||
| public MyForwardingDataFetcher(DataLoader dataLoader) { | ||
| this.dataLoader = dataLoader | ||
| } | ||
|
|
||
| @Override | ||
| CompletableFuture<Object> get(DataFetchingEnvironment environment) { | ||
| return dataLoader.load(environment) | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you could actually return the Assert