Skip to content
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

Remove CacheMap#containsKey before #get #146

Conversation

AlexandreCarlton
Copy link
Collaborator

Currently, when accessing values from the DataLoader's CacheMap, we first check #containsKey before invoking #get. This is problematic for two reasons:

  • the underlying cache's metrics are skewed (it will have a 100% hit rate).
  • if the cache has an automatic expiration policy, it is possible a value will expire between the containsKey and get invocations leading to an incorrect result.

To ameliorate this, we always invoke #get and check if it is null to deterine whether the key is present. We wrap the invocation in a try/catch as the #get method is allowed to through per its documentation.

Currently, when accessing values from the `DataLoader`'s `CacheMap`, we
first check `#containsKey` before invoking `#get`. This is problematic
for two reasons:

 - the underlying cache's metrics are skewed (it will have a 100% hit
   rate).
 - if the cache has an automatic expiration policy, it is possible a
   value will expire between the `containsKey` and `get` invocations
   leading to an incorrect result.

To ameliorate this, we always invoke `#get` and check if it is `null` to
deterine whether the key is present. We wrap the invocation in a
`try`/`catch` as the `#get` method is allowed to through per its
documentation.
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.

Thanks for this

@bbakerman bbakerman merged commit 3a16d9c into graphql-java:master Apr 29, 2024
1 check passed
AlexandreCarlton added a commit to AlexandreCarlton/java-dataloader that referenced this pull request Apr 30, 2024
As a fast follow to graphql-java#146, we add a regression test to ensure that we
still handle a `CacheMap` whose `#get` throws an exception (by falling
back to the underlying batch loader).
@dondonz dondonz added this to the Next release 3.4.0 milestone May 17, 2024
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