Skip to content

Reactive request is not properly propagated as cursor batch size #4543

Closed
@kschlesselmann

Description

@kschlesselmann

Since the upgrade to Spring Boot 3.1 we noticed a major performance degradation in comparison to our previous Spring Boot 2.7 environment. Now it looks like that the cursor batch size for mongo queries has changed. The old 2.7 service seems to use a default of 32 (24 getMore). The new 3.1 service seems to be using a cursor size of 2.

To reproduce run this litte snippet either with SB 2.7 or SB 3.1

@Document
data class Thing(val name: String, @Id val id: UUID = UUID.randomUUID())

interface ThingRepository : ReactiveCrudRepository<Thing, UUID>

@SpringBootApplication
class Demo1Application {

    @Bean
    fun test(thingRepository: ThingRepository) = ApplicationRunner {
        Flux.range(0, 1) // increase this to generate more samples
            .map { Thing(name = "Oimel $it") }
            .collectList()
            .flatMapMany { thingRepository.saveAll(it) }
            .flatMap {
                thingRepository.findAll()
                    .doOnRequest { println("Requested $it") }
                    //.flatMap({ Mono.just(it) }, 8) // use this to request multiple small batches
                    .collectList()
            }
            .blockLast()
    }
}

fun main(args: Array<String>) {
    runApplication<Demo1Application>(*args)
}

Running this with SB 3.1 results in the following log (with logging.level: org.mongodb: debug)

...

Requested 9223372036854775807
2023-11-02T15:20:11.202+01:00 DEBUG 913 --- [tter-2-thread-1] org.mongodb.driver.protocol.command      : Command "find" started on database test using a connection with driver-generated ID 3 and server-generated ID 152 to localhost:27017. The request ID is 6. Command: {"find": "thing", "filter": {}, "batchSize": 2, "$db": "test", "lsid": {"id": {"$binary": {"base64": "vTi0GdDgQACDenLxyS0dqQ==", "subType": "04"}}}}
2023-11-02T15:20:11.204+01:00 DEBUG 913 --- [ntLoopGroup-3-3] org.mongodb.driver.protocol.command      : Command "find" succeeded in 2,12 ms using a connection with driver-generated ID 3 and server-generated ID 152 to localhost:27017. The request ID is 6. Command reply: {"cursor": {"firstBatch": [{"_id": {"$binary": {"base64": "wU6wcB3AUdk171ISY6wilA==", "subType": "03"}}, "name": "Oimel 5", "_class": "com.example.demo.Thing"}, {"_id": {"$binary": {"base64": "JERXcnYlt6cdtn7GHVCEnQ==", "subType": "03"}}, "name": "Oimel 13", "_class": "com.example.demo.Thing"}], "id": 1440510796141382911, "ns": "test.thing"}, "ok": 1.0}
2023-11-02T15:20:11.206+01:00 DEBUG 913 --- [ntLoopGroup-3-3] org.mongodb.driver.operation             : Received batch of 2 documents with cursorId 1440510796141382911 from server localhost:27017
2023-11-02T15:20:11.212+01:00 DEBUG 913 --- [tter-2-thread-1] org.mongodb.driver.protocol.command      : Command "getMore" started on database test using a connection with driver-generated ID 3 and server-generated ID 152 to localhost:27017. The request ID is 7. Command: {"getMore": 1440510796141382911, "collection": "thing", "batchSize": 2, "$db": "test", "lsid": {"id": {"$binary": {"base64": "vTi0GdDgQACDenLxyS0dqQ==", "subType": "04"}}}}
2023-11-02T15:20:11.213+01:00 DEBUG 913 --- [ntLoopGroup-3-3] org.mongodb.driver.protocol.command      : Command "getMore" succeeded in 1,51 ms using a connection with driver-generated ID 3 and server-generated ID 152 to localhost:27017. The request ID is 7. Command reply: {"cursor": {"nextBatch": [{"_id": {"$binary": {"base64": "UUDQKQ+rQ6LcEK26ymaatg==", "subType": "03"}}, "name": "Oimel 15", "_class": "com.example.demo.Thing"}, {"_id": {"$binary": {"base64": "3UW8nReu/bwP+xR+2PMQtA==", "subType": "03"}}, "name": "Oimel 16", "_class": "com.example.demo.Thing"}], "id": 1440510796141382911, "ns": "test.thing"}, "ok": 1.0}
2023-11-02T15:20:11.214+01:00 DEBUG 913 --- [ntLoopGroup-3-3] org.mongodb.driver.operation             : Received batch of 2 documents with cursorId 1440510796141382911 from server localhost:27017
2023-11-02T15:20:11.214+01:00 DEBUG 913 --- [tter-2-thread-1] org.mongodb.driver.protocol.command      : Command "getMore" started on database test using a connection with driver-generated ID 3 and server-generated ID 152 to localhost:27017. The request ID is 8. Command: {"getMore": 1440510796141382911, "collection": "thing", "batchSize": 2, "$db": "test", "lsid": {"id": {"$binary": {"base64": "vTi0GdDgQACDenLxyS0dqQ==", "subType": "04"}}}}
2023-11-02T15:20:11.216+01:00 DEBUG 913 --- [ntLoopGroup-3-3] org.mongodb.driver.protocol.command      : Command "getMore" succeeded in 0,98 ms using a connection with driver-generated ID 3 and server-generated ID 152 to localhost:27017. The request ID is 8. Command reply: {"cursor": {"nextBatch": [{"_id": {"$binary": {"base64": "uUNBCdBVkj1InxjNI4bWkw==", "subType": "03"}}, "name": "Oimel 18", "_class": "com.example.demo.Thing"}, {"_id": {"$binary": {"base64": "rU6tQQIjfM7StaHMvAgXjQ==", "subType": "03"}}, "name": "Oimel 17", "_class": "com.example.demo.Thing"}], "id": 1440510796141382911, "ns": "test.thing"}, "ok": 1.0}
2023-11-02T15:20:11.216+01:00 DEBUG 913 --- [ntLoopGroup-3-3] org.mongodb.driver.operation             : Received batch of 2 documents with cursorId 1440510796141382911 from server localhost:27017
2023-11-02T15:20:11.216+01:00 DEBUG 913 --- [tter-2-thread-1] org.mongodb.driver.protocol.command      : Command "getMore" started on database test using a connection with driver-generated ID 3 and server-generated ID 152 to localhost:27017. The request ID is 9. Command: {"getMore": 1440510796141382911, "collection": "thing", "batchSize": 2, "$db": "test", "lsid": {"id": {"$binary": {"base64": "vTi0GdDgQACDenLxyS0dqQ==", "subType": "04"}}}}
2023-11-02T15:20:11.217+01:00 DEBUG 913 --- [ntLoopGroup-3-3] org.mongodb.driver.protocol.command      : Command "getMore" succeeded in 0,72 ms using a connection with driver-generated ID 3 and server-generated ID 152 to localhost:27017. The request ID is 9. Command reply: {"cursor": {"nextBatch": [{"_id": {"$binary": {"base64": "sE7piYIJMnvaI2qPymxWvA==", "subType": "03"}}, "name": "Oimel 20", "_class": "com.example.demo.Thing"}, {"_id": {"$binary": {"base64": "bEJTuvnZT13LQ4lQA+BGtQ==", "subType": "03"}}, "name": "Oimel 19", "_class": "com.example.demo.Thing"}], "id": 1440510796141382911, "ns": "test.thing"}, "ok": 1.0}
2023-11-02T15:20:11.218+01:00 DEBUG 913 --- [ntLoopGroup-3-3] org.mongodb.driver.operation             : Received batch of 2 documents with cursorId 1440510796141382911 from server localhost:27017

... and so on ...

I don't think this is working as intended, is it?

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions