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

Allow setting ReadPreference at a Repository and Query level [DATAMONGO-2103] #2971

Closed
spring-projects-issues opened this issue Oct 3, 2018 · 2 comments
Labels
in: repository Repositories abstraction status: ideal-for-contribution An issue that a contributor can help us with type: enhancement A general enhancement

Comments

@spring-projects-issues
Copy link

João Carvalho opened DATAMONGO-2103 and commented

Currently, setting the ReadPreference can only be done at the MongoTemplate level. This forces us to move down an abstraction layer to specify custom ReadPreferences for our entities and queries.

We would like to be able to specify the ReadPreference for an entity at the Repository and Query level. Our current use case involves a specific entity which can be accessed from a secondary node node in the majority of the use cases, but requires primary reads for other use cases. As our applications scale, it becomes critical to distribute load across all nodes in the MongoDB cluster, and currently, we can either change the global level (thus sacrificing correctness) or providing custom implementations on a repository-by-repository basis.

This could be achieved using an annotation, like such:

@ReadPreference(SECONDARY_PREFERRED)
class FooRepository : CrudRepository<Foo, String> {

  fun findByName(name: String): Foo?

  @ReadPreference(PRIMARY)
  fun strongFindByName(name: String): Foo?

}

In this example, all queries to this entity (both builtin an custom) would use the 'Secondary Preferred' level, except for strongFindByName, which would use the 'Primary' level.

 

 


1 votes, 2 watchers

@spring-projects-issues spring-projects-issues added in: repository Repositories abstraction type: enhancement A general enhancement labels Dec 30, 2020
@christophstrobl christophstrobl added the status: ideal-for-contribution An issue that a contributor can help us with label Jul 6, 2023
@jorgerod
Copy link
Contributor

Hi @christophstrobl
I am very interested in this feature. I see that @odrotbohm is assigned.

If there is no problem, I would like to contribute.

@christophstrobl
Copy link
Member

@jorgerod I cleared the assignee, happy to review a PR. Please make sure to check out our contribution guidelines.

jorgerod added a commit to jorgerod/spring-data-mongodb that referenced this issue Sep 14, 2023
jorgerod added a commit to jorgerod/spring-data-mongodb that referenced this issue Sep 14, 2023
christophstrobl pushed a commit that referenced this issue Oct 3, 2023
christophstrobl added a commit that referenced this issue Oct 12, 2023
…ethods.

This commit reduces the API surface of the ReadPreference annotation leaving fine grained control such as Tags to the Template API.
Next to supporting read preference for annotated queries we now also make sure to apply read preferences for annotated aggregation methods as well as predefined finder methods running queries.

See: #2971
Original Pull Request: #4503
christophstrobl pushed a commit that referenced this issue Oct 12, 2023
…idden base repository methods.

See: #2971
Original Pull Request: #4503
christophstrobl added a commit that referenced this issue Oct 12, 2023
Limit exposure of API methods to package visibility.
Also remove fields and methods not directly required to support current feature scope.
Finally add missing since tags and provide additional tests.

See: #2971
@christophstrobl christophstrobl added this to the 4.2 RC1 (2023.1.0) milestone Oct 12, 2023
christophstrobl added a commit that referenced this issue Oct 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: repository Repositories abstraction status: ideal-for-contribution An issue that a contributor can help us with type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants