spring-projects / spring-projects/spring-data-mongodb
Adding `SECONDARY_READS` meta flag to the query does not affect the `readPreference`
@christophstrobl is already working on this.
Since Apr 4, 2024.
- Dominant language
- Java
- Stars
- 1.7k
- Forks
- 1.1k
- PR merge metrics
- No merged PRs in 30d
Description
Expected behavior:
When I add the SECONDARY_READS meta flag using the annotation @Meta(flags = { SECONDARY_READS }) or when .allowSecondaryReads() is invoked on a query, the read preference should be set as secondary or secondaryPreferred.
Actual behaviour:
When I add the SECONDARY_READS meta flag using the annotation @Meta(flags = { SECONDARY_READS }) or when .allowSecondaryReads() is invoked on a query, the read preference is instead set as primaryPreferred.
Is this the intended behavior?
- If it is, could you please explain why?
- If not, would you mind if I created a separate PR to correct it, as per the expected behavior outlined above?
Root cause:
The unit test to reproduce the potential bug discovered:
import com.mongodb.ReadPreference
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.springframework.data.mongodb.core.query.Criteria
import org.springframework.data.mongodb.core.query.Query
class TestToReproduceQueryReadPreference {
// It passes
@Test
fun `hasReadPreference returns true if read preference is set`() {
// given
val query = Query
.query(Criteria())
.withReadPreference(ReadPreference.secondaryPreferred())
// when
val hasReadPreference = query.hasReadPreference()
// then
assertThat(hasReadPreference).isTrue()
}
// It passes
// Expected : ReadPreference{name=secondaryPreferred, hedgeOptions=null}
// Actual : ReadPreference{name=secondaryPreferred, hedgeOptions=null}
@Test
fun `query has secondaryPreferred read preference if it is set`() {
// given
val query = Query
.query(Criteria())
.withReadPreference(ReadPreference.secondaryPreferred())
// when
val readPreference = query.readPreference
// then
assertThat(readPreference).isEqualTo(ReadPreference.secondaryPreferred())
}
// It passes
@Test
fun `hasReadPreference returns true if secondary reads are allowed`() {
// given
val query = Query
.query(Criteria())
.allowSecondaryReads()
// when
val hasReadPreference = query.hasReadPreference()
// then
assertThat(hasReadPreference).isTrue()
}
// It fails
// Expected : ReadPreference{name=secondaryPreferred, hedgeOptions=null}
// Actual : ReadPreference{name=primaryPreferred, hedgeOptions=null}
@Test
fun `query has secondaryPreferred read preference if secondary reads are allowed`() {
// given
val query = Query
.query(Criteria())
.allowSecondaryReads()
// when
val readPreference = query.readPreference
// then
assertThat(readPreference).isEqualTo(ReadPreference.secondaryPreferred())
}
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.