Skip to content

Adding a ScheduledDataLoaderRegistry #87

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
merged 7 commits into from
Jul 6, 2021

Conversation

bbakerman
Copy link
Member

A DataLoaderRegistry that will evaluate a predicate before actually calling dispatch on the dataloaders within

if they return false, then it reschedules the dispatch call for another time (schedule duration) and trys again.

This PR was inspired by the work in #77 - the difference is the threading is outside the DL classes and it has a flexible predicate on when to dispatch, allowing people to write custom logic here

@bbakerman
Copy link
Member Author

cc: @tinnou - could you have a look at this please

@bbakerman bbakerman added this to the 3.0.0 milestone Jun 27, 2021
public static class Builder {

private ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
private DispatchPredicate dispatchPredicate = (key, dl) -> true;
Copy link

Choose a reason for hiding this comment

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

what is the intent of having the dispatch predicate on the registry vs dataloader?

Copy link
Member Author

Choose a reason for hiding this comment

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

I thought about it on the dataloader itself - but the more I thought about it the more it felt like a cross cutting concern

eg in graphql you dispatch the DataLoaderRegistry and that delegates to each DataLoader

Plus it would mean all the extra baggage of Executors and so on would be in each DataLoader.

This allows for a collection of DataLoaders controlled by the one mechanism.

But since the predicate gets the DL and decides on dispatch or not - you can get as finicky as you like (as if it wa inside the dataloader)

/**
* This predicate will return true if the {@link DataLoader#dispatchDepth()} is greater than the specified depth.
*
* This will act as minimum batch size. There must be more then `depth` items queued for the predicate to return true.
Copy link

Choose a reason for hiding this comment

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

than

perform that check again. Once a predicate evaluated to true, it will not reschedule and another call to
`dispatchAll` is required to be made.

This allows you to do things like "dispatch ONLY if the queue depth is > 10 deep or more than 200 millis have passed
Copy link

Choose a reason for hiding this comment

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

gotcha, it allows to dial the aggressiveness of the batching strategy.

@tinnou
Copy link

tinnou commented Jun 30, 2021

Looks great thank you @bbakerman for this

* A predicate class used by {@link ScheduledDataLoaderRegistry} to decide whether to dispatch or not
*/
@FunctionalInterface
public interface DispatchPredicate {
Copy link
Member

Choose a reason for hiding this comment

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

This is very similar to Predicate, why not use that?

Copy link
Member Author

Choose a reason for hiding this comment

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

because it takes 2 arguments - so inspired by but not the same

@andimarek
Copy link
Member

I am not so sure about this PR and the relationship to DataLoaderDispatcherInstrumentation. The former is a subclass of DataLoaderRegistry and the latter is an Instrumentation invoking DataLoaderRegistry directly. Could we align both approached and make them even composable in a DataLoaderRegistryDispatcherChain or so?

The subclassing works, but I would argue it violates Liskov by changing the semantics of invokeAll for example.

@bbakerman bbakerman merged commit f2457ec into master Jul 6, 2021
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.

3 participants