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

Adding SECONDARY_READS meta flag to the query does not affect the readPreference #4676

Open
ryszardmakuch opened this issue Mar 29, 2024 · 2 comments
Assignees
Labels
status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged type: documentation A documentation update

Comments

@ryszardmakuch
Copy link

ryszardmakuch commented Mar 29, 2024

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())
    }
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 29, 2024
@christophstrobl
Copy link
Member

Thank you for getting in touch. I think we need to work on the documentation side of things here allowSecondaryReads will use the primary if available but allows secondary nodes to be used if not.
Please use Query#withReadPreference to set any ReadPreference directly.

@christophstrobl christophstrobl added type: documentation A documentation update status: waiting-for-feedback We need additional information before we can continue labels Apr 4, 2024
@christophstrobl christophstrobl self-assigned this Apr 4, 2024
@ryszardmakuch
Copy link
Author

@christophstrobl, thanks for the explanation. Updating the documentation would be useful.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Apr 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged type: documentation A documentation update
Projects
None yet
Development

No branches or pull requests

3 participants