spring-projects / spring-projects/spring-data-mongodb

Adding `SECONDARY_READS` meta flag to the query does not affect the `readPreference`

Open
#4,676 5 comments 1 reaction 1 assignee View on GitHub

@christophstrobl is already working on this.

Since Apr 4, 2024.

status: feedback-provided status: waiting-for-triage type: documentation
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.