Skip to content

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

Open
@ryszardmakuch

Description

@ryszardmakuch

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())
    }
}

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions