Skip to content

support async execution - #590

Merged
andimarek merged 6 commits into
masterfrom
full-async
Aug 8, 2017
Merged

andimarek merged 6 commits into
masterfrom
full-async

Conversation

@andimarek

@andimarek andimarek commented Aug 2, 2017 •

Copy link
Copy Markdown
Member

The main change is that ExecutionStrategy.fetchField returns now a CompletableFuture instead of Object and new ExecutionStrategys.

SimpleExecutionStrategy is now SerialExecutionStrategy and is not used anymore by default, because it is blocking.

The new default strategy is AsyncExecutionStrategy for queries and AsyncSerialExecutionStrategy for mutations. Both are non blocking but are compatible to the old behaviour if DataFetcher are still blocking and not async.

@andimarek
andimarek requested review from bbakerman and kaqqao August 4, 2017 09:27
@andimarek andimarek added this to the 4.0.0 milestone Aug 4, 2017
@andimarek andimarek added the breaking change requires a new major version to be relased label Aug 4, 2017
@andimarek andimarek mentioned this pull request Aug 5, 2017
@andimarek
andimarek requested a review from dminkovsky August 5, 2017 05:58
Comment thread src/main/java/graphql/GraphQL.java Outdated
private ExecutionStrategy mutationExecutionStrategy = new SimpleExecutionStrategy();
private ExecutionStrategy subscriptionExecutionStrategy = new SimpleExecutionStrategy();
private ExecutionStrategy queryExecutionStrategy = new AsyncExecutionStrategy();
private ExecutionStrategy mutationExecutionStrategy = new AsyncExecutionStrategy();

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.

Shouldn't this be AsyncSerialExecutionStrategy ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yes, you are right

@bbakerman bbakerman left a comment

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.

I think DataFetcher needs more JavaDoc on what you can return (eg a CF is ok)

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.

Make sure graphql.NonNullHandlingTest works as well. Currently we are using graphql.execution.NonNullableFieldWasNullException as a way to jump out of a flow but adjust behaviour.

By going full async the order of field evaluation is now changed since evaluating A -> B -C can now be in any order

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

this is outdated code and the NonNullHandlingTest works

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 have to agree on a JavaDoc set of settings

eg: lines between @params or not.

You and I are flip flopping here.

I argue for lines after each section but... lets make a decision and document it into the project README

});
}

private void futuresCompleted(ExecutionContext executionContext,

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.

IDEA tells me that a lot of this code is repeated from AsyncExecutionStrategy - can we create a static OR a AsyncExecutionStrategyHelper common helper that an be used ot contain the common code

Same below on error handling

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed with: 4b4be3b

result.complete(new ExecutionResultImpl(resolvedValuesByField, executionContext.getErrors()));
}

private void handleException(ExecutionContext executionContext, CompletableFuture<ExecutionResult> result, Throwable 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.

Again IDEA says this is repeated code. Can it go into a common place. Even into ExecutionStrategy perhaps as a protected method for this code

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed with: 4b4be3b

schema
}


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.

Check out the testing library awaitability

testCompile 'org.awaitility:awaitility:2.0.0'

https://github.com/awaitility/awaitility

examples where I used it

https://github.com/graphql-java/java-dataloader/blob/master/src/test/java/org/dataloader/DataLoaderTest.java#L87


then:
!result.isDone()
Thread.sleep(200)

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.

awaitability makes this much cleaner

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

done

/**
* The standard graphql execution strategy that runs fields asynchronously
*/
public class AsyncExecutionStrategy extends ExecutionStrategy {

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 need to explain to people that DataFetchers can returns CFs and that they will be chained ok.

That needs to go on DataFetcher and in the general documentation.

I think the doco should have a "aasyncrhnous support in graphql" section (but not in this PR)


fetchCtx.onEnd(fetchedValue);
Object fetchedValueRaw = dataFetcher.get(environment);
if (fetchedValueRaw instanceof CompletableFuture) {

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.

I think this should accept a CompletionStage at this specific point.

While the JDK only has 1 implementation, people can write their own.

http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletionStage.html#toCompletableFuture--

The toCompletableFuture is provided as a interop to allow 2 different CompletionStages to interop.

This would be a tiny minor thing to be honest since the work around is for them to call toCompletableFuture() themselves in the DF.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

done: f0436f1

fetchedValue = new CompletableFuture<>();
fetchedValue.completeExceptionally(e);
}
return fetchedValue;

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.

I think we need to fix Instrumentation up.

#601

We would use .whenComplete((result,throwable) -> ....) to do this.

This does not have to go in this PR

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.

Doesn't #593 take care of this?

.subscriptionExecutionStrategy(new AsyncExecutionStrategy())
.build();

}

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.

Do we have a test some where that tests how a mutation switches back to query strategy once the first level is resolved?

This will be more important with async if we get it wrong

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I added this test: 3cde546

this what you mean?

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;

public class AsyncSerialExecutionStrategy extends ExecutionStrategy {

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.

Can we get some JavaDoc on this please

this.mutationStrategy = mutationStrategy != null ? mutationStrategy : new SimpleExecutionStrategy();
this.subscriptionStrategy = subscriptionStrategy != null ? subscriptionStrategy : new SimpleExecutionStrategy();
this.queryStrategy = queryStrategy != null ? queryStrategy : new AsyncExecutionStrategy();
this.mutationStrategy = mutationStrategy != null ? mutationStrategy : new AsyncExecutionStrategy();

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.

AsyncSerial again?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yes ... you are right ... too many different places where we use a default strategy.

@andimarek
andimarek merged commit bb94feb into master Aug 8, 2017
@oexza

oexza commented Aug 8, 2017 via email

Copy link
Copy Markdown

@a-marcel

Copy link
Copy Markdown

Looking forward to this release !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking change requires a new major version to be relased

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants