Skip to content

feat: make cache map values accesible for read only purposes #115

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

Conversation

samuelAndalon
Copy link

@samuelAndalon samuelAndalon commented May 5, 2022

in an attempt so write a better Instrumentation that could dispatch a DataLoaderRegistry in a more optimized way we created a custom Instrumentation that tracks the state of each ExecutionStrategy of an ExecutionInput and dispatch the DataLoaderRegistry when no more synchronous code could be executed.

more information

graphql-java/graphql-java#2715

this PR adds a method to the DataLoader class that provides a read-only list of CacheMaps associated with the DataLoader, this will help to keep track of the numberOfDependents of each future.

by doing this we would also be solving a problem highlighted here

graphql-java/graphql-java#1198

considering this queries:

astronaut can have many mission
mission can have many planets

in order to access to astronaut planets we need 2 DataLoaders to be chained in the planets resolver

  1. GetMissionsByAstronaut
  2. GetPlanetsByMission
query GetAstronautPlanets {
    astronaut(id: 1) {
        name
        planets {
            name
        }
    }
}
query GetAstronautMissions {
    astronaut(id: 3) {
        name
        missions {
            designation
            planets {
                name
            }
        }
    }
}

With the custom DataLoaderSyncExecutionExhaustedInstrumentation and this change we were able to get data for those 2 queries by dispatching those 2 dataLoader only once.

we wrote some logic to decorate a DataLoaderRegistry to make CacheMap values public for read-only purposes and that helped to achieve above logic

more detailed test here

https://github.com/ExpediaGroup/graphql-kotlin/blob/1758f9c4fed47f086891c9340ebc995eca1e9083/executions/graphql-kotlin-dataloader-instrumentation/src/test/kotlin/com/expediagroup/graphql/dataloader/instrumentation/syncexhaustion/DataLoaderSyncExecutionExhaustedInstrumentationTest.kt#L345

Copy link
Member

@bbakerman bbakerman left a comment

Choose a reason for hiding this comment

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

I am happy to take this PR in general - but please see my other comment about what we expose

*/
@Override
public Collection<CompletableFuture<V>> getAll() {
return cache.values();
Copy link
Member

Choose a reason for hiding this comment

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

Rather than expose the getAll down in the helper and then in the data loader,

I think we should expose the CacheMap itself on DataLoader

    private final CacheMap<Object, V> futureCache;

is inside DataLoader.

Change this to

    public  CacheMap<Object, V> getCacheMap() 

And for symmetry (since we are going to do this) we should expose the other atts

    private final StatisticsCollector stats;
    private final CacheMap<Object, V> futureCache;
    private final ValueCache<K, V> valueCache;

Copy link
Author

@samuelAndalon samuelAndalon May 6, 2022

Choose a reason for hiding this comment

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

Should we worry about making futureCache and valueCache public ? i was trying to just expose the CacheMap values as read-only with unmodifiableCollection to avoid allowing the mutation of their state -- Let me know what you think.

For now i removed the method in the DataLoaderHelper and directly exposed the read-only Collection from the DataLoader

Copy link
Author

Choose a reason for hiding this comment

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

also was wondering, given that we are adding a new method in the CacheMap interface, should we define a default behavior to return maybe empty collection or null ? otherwise this could be a breaking change if ppl is using a custom implementation of CacheMap

Copy link
Member

Choose a reason for hiding this comment

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

I get your point on trying to reduce danger but the fact is the DataLoaderOptions passed in the CachMap / ValueMap or it get defaulted.

I guess we could make a UnmodifiableCacheMap / ValueCache wrapper say so gain write safety

Should we worry about making futureCache and valueCache public ?

My view is sure it could be a foot gun... so dont shoot yourself with it. You needed access to the future cache so lets give it out. After all it was nominally passed in.

But UnmodifiableCacheMap is not a crazy idea

Copy link
Author

Choose a reason for hiding this comment

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

added the getters for cacheMap and valueCache,
will think about the wrapper interface to remove the mutable methods clear, delete and set and probably open a separate PR, if we decide that we need it

@bbakerman
Copy link
Member

Thanks for this PR

@bbakerman bbakerman merged commit fa94732 into graphql-java:master May 19, 2022
@samuelAndalon samuelAndalon deleted the feat/cache-map-get-values branch May 19, 2022 20:58
@samuelAndalon
Copy link
Author

@bbakerman Thank you for the merge, was wondering, when this can be released ?

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.

2 participants