Skip to content

going full async - #545

Closed
andimarek wants to merge 3 commits into
masterfrom
full-async-spike
Closed

andimarek wants to merge 3 commits into
masterfrom
full-async-spike

Conversation

@andimarek

Copy link
Copy Markdown
Member

just a spike, not intended to be merged, doesn't even compile.

throw e;
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is painfully missing in Java :(

The ability to wait for a Collection support should be present

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What is painfully missing? Waiting for an async collection to finish?

}
}
return new ExecutionResultImpl(results, executionContext.getErrors());
return CompletableFuture.completedFuture(new ExecutionResultImpl(results, executionContext.getErrors()));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Notice here how we have a materialised ExecutionResult and then we wrap into a completed Future
so that to the outside it looks like asynch promise?

I am not sure of the value here. It allows thenApply style composition but it nots async all the way down.

That said I guess it allows other strategies to be more or less asynch and hence the signature is in the right shape.

Actually as I type this, it makes me see how this is useful

return CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()])).thenApply((ignored) -> {
Map<String, ExecutionResult> realResults = new LinkedHashMap<>();
for (String field : results.keySet()) {
realResults.put(field, results.get(field).join());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The join will throw the NonNull exceptions and so here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We will want a specific NonNullNull field test in the equivalence tests say

} else {
result = queryStrategy.execute(executionContext, parameters);
result = queryStrategy.execute(executionContext, parameters).toCompletableFuture().join();
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

One decision we need to make is whether to use CompletionStage or CompletableFuture (or a mix of both)

Standard Java best practice says use the interface CompletionStage. Except you can never get the value of a CompletionStage and so you have all these .toCompleteableFuture() calls

Since this code base is all about materialising values (eventually) perhaps we should use CompletableFuture?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Perhaps they will evolve the CompletionStage interface to make it useful. toCompletableFuture().get() is not horrible imo.

@LarsKrogJensen

Copy link
Copy Markdown
Contributor

FWIW, I few months I played with porting graphql-java over to kotlin (https://github.com/LarsKrogJensen/graphql-kotlin) just for the fun of it and learning. Kotlin is such a beautiful language. One of the features that I really wanted to implement was async data fetchers all the way without any usage a threads or thread pools.

I went with CompletionStage, which I regret - it didn't provide any real value as one of the comments above also concluded.
One change that I also made was to make DataFetcher interface return a CompletionStage, and that was maybe not the best choice as:

  • Most fields are simple leafs and don't require async, they should just return values.
  • It did not play well with subscription types. To support subscriptions executions I had to add additional PublisherFetcher. (There is a simple SubscriptionExecutionStrategy implementation)

What I really would like is the datafetcher to return a union type (not available i java or kotlin yet) of
Object | CompletableFuture | Publisher (reactive stuff). This would on the other hand make the execution strategy more complicated as it would have to examine the value and select different value completion strategies.

@andimarek

andimarek commented Jul 11, 2017 •

Copy link
Copy Markdown
Member Author

@LarsKrogJensen Our plan is indeed to handle the returned value from a DataFetcher differently based on the type: If it is a CompletableFuture all is good, if it is not, we will wrap it. So even if we can't express it with types (the return value is just Object) it is basically Object | CompletableFuture.

@dminkovsky dminkovsky left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for getting this rolling!

With these interface changed, the async strategy I wrote should be easy enough to integrate: https://github.com/dminkovsky/graphql-java/blob/async/src/main/java/graphql/execution/async/AsyncExecutionStrategy.java

You'll see it supports returning leaves and non-leaves from the data fetchers, synchronously and asynchronously: https://github.com/dminkovsky/graphql-java/blob/async/src/test/groovy/graphql/execution/async/AsyncExecutionStrategyTest.groovy#L36-L47

} else {
result = queryStrategy.execute(executionContext, parameters);
result = queryStrategy.execute(executionContext, parameters).toCompletableFuture().join();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Perhaps they will evolve the CompletionStage interface to make it useful. toCompletableFuture().get() is not horrible imo.

throw e;
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What is painfully missing? Waiting for an async collection to finish?

@dminkovsky

dminkovsky commented Jul 11, 2017 •

Copy link
Copy Markdown
Contributor

By the way, in my playing with this, I experimented with fetchers that return:

  • A future of a list of non-futures
  • A future of a list of futures
  • A non-future list of futures

...and various other permutations.

I think there should be limited support for which of these we support, as it gets weird and complicated pretty fast. Most importantly, given the recursive bottom-up resolution of GraphQL, I think only supporting async collections that you expect will be fully resolved made sense: https://github.com/dminkovsky/graphql-java/blob/async/src/test/groovy/graphql/execution/async/AsyncExecutionStrategyTest.groovy#L45.

Also, while we're talking about resolving future collections, this library is very useful until Java 9 comes out: https://github.com/spotify/completable-futures

@abassouk

Copy link
Copy Markdown

I have implemented a reactive version of ExecutionStrategy that works with RxJava 2, and on the happy path it works beautifully. If your DataFetcher is returning any of the Rx reactive streams, a Publisher, Futures/CompletableFutures or even a CompletionStage, it works as expected. Parallelism is up to the DataFetcher and the reactive stream object it returns - if the reactive object is on different scheduler then it's going to be fetched async.

This hasn't needed any changes to the ExecutionStrategy so far, and the only problem left at the moment is merging errors from async and non-async sources.

@andimarek

Copy link
Copy Markdown
Member Author

closing this issue: it was a spike and never intended to be merged.

the real implementation is PR #590

@andimarek andimarek closed this Aug 5, 2017
@andimarek
andimarek deleted the full-async-spike branch August 9, 2017 13:02
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.

5 participants