-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Shard key not honored in ReferenceLookupDelegate when DocumentReference resolves to a empty collection #4612
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
Comments
Here is a hotfix for the problem: Just use the following ReferenceResolver @Bean
@Override
public MappingMongoConverter mappingMongoConverter(MongoDatabaseFactory databaseFactory, MongoCustomConversions customConversions, MongoMappingContext mappingContext) {
DbRefResolver dbRefResolver = new MyReferenceResolver(databaseFactory);
MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, mappingContext);
converter.setCustomConversions(customConversions);
converter.setCodecRegistryProvider(databaseFactory);
return converter;
}
private class MyReferenceResolver extends DefaultDbRefResolver {
public MyReferenceResolver(MongoDatabaseFactory mongoDbFactory) {
super(mongoDbFactory);
}
@Override
public Object resolveReference(MongoPersistentProperty property, Object source, ReferenceLookupDelegate referenceLookupDelegate, MongoEntityReader entityReader) {
if(source instanceof DocumentReferenceSource s && s.getTargetSource() instanceof Collection<?> c && c.isEmpty()) {
return new ArrayList<>();
}
return super.resolveReference(property, source, referenceLookupDelegate, entityReader);
}
} |
…ference resolves to an empty collection Fixes spring-projects#4612
Thanks for reaching out and taking the time to provide a PR. We'll have a look. |
Hi, any news on this issue? |
@stefanbildl it's on the list - just not on the top of it. |
…ference resolves to an empty collection Fixes #4612
@stefanbildl we've slightly revised the proposed fix. You may want to have a look at the current issue branch. |
@christophstrobl thanks, looks good to me. Your refactor makes it way easier to understand! |
Oh, one thing I just noticed: This is immutable: Collections.emptyList(); This could produce another bug where a user would try to add new entries to the empty list. |
@christophstrobl please review this, thanks! |
I think I get it now. the iterable is converted to the target type, am I right? |
@stefanbildl this is the way! yes. |
…ference resolves to an empty collection Fixes #4612
…lection. Fixes spring-projects#4612 Original pull request: spring-projects#4613
Wrap no results predicate in dedicated lookup filter and make sure to omit loading attempt also for map properties. See spring-projects#4612 Original pull request: spring-projects#4613
Add Override annotations and comment why we optimize. See spring-projects#4612 Original pull request: spring-projects#4613
Shard key not honored in ReferenceLookupDelegate
I have identified an issue in org.springframework.data.mongodb.core.convert.ReferenceLookupDelegate when using sharded mongodb collections.
The shard key of our "Task" collection includes the fields "datacenter" and "flowId":
If "dependsOn" is a empty set, the ReferenceLookupDelegate method computeFilter
returns
which - in our case - causes a global query. If a datacenter is currently unreachable, the query fails!
I don't understand, why this NO_RESULTS_PREDICATE is needed here. Is this just a way to say: query for a empty list?
Proposition
Instead of handling the empty list case in computeFilter, just add the following lines to readReference:
Thank you very much!
The text was updated successfully, but these errors were encountered: