spring-projects / spring-boot

Spring Boot
https://spring.io/projects/spring-boot
Apache License 2.0
74.76k stars 40.59k forks source link

MongoHealthIndicator not compliant with Mongo stable API with strict setting #41101

Closed jorgelc closed 3 months ago

jorgelc commented 3 months ago

MongoHealthIndicator uses isMaster command, which is not part of Mongo's stable API https://www.mongodb.com/docs/v5.0/reference/stable-api-changelog/

If the underlying Mongo connection is configured to use the stable API and strict setting, the health indicator yields an error, as Mongo rejects the isMaster command.

Affected versions: Tested with sprinboot 3.3.0, potentially since 3.0.0

To reproduce it, create a blank springboot project including spring-boot-starter-data-mongodb, spring-boot-starter-web and spring-boot-starter-actuator dependencies. Then, set up both Mongo stable API version and strict to true by creating the following bean:

    @Bean
    public MongoClientSettings mongoClientSettings() {
        return MongoClientSettings.builder()
            .applicationName("testApp")
            .serverApi(ServerApi.builder()
                .strict(true)
                .version(ServerApiVersion.V1)
                .build())
            .build();
    }

For reference, such project has been created here https://github.com/jorgelc/mongo-stable-api-springboot-health, with steps about how to reproduced the error and compare the behaviour with when strict is set to false.

wilkinsona commented 3 months ago

Thank you. I've reproduced the problem.

I think we should switch to the hello command. It's supported in MongoDB 5.0 and later and Mongo 4.x reached end of life in February 2024.

jorgelc commented 3 months ago

Thanks @wilkinsona

We implemented that as a temporary workaround locally and it worked for us, but it would be nice to have that supported out of the box.

jyemin commented 3 months ago

Just FYI, the hello command was backported all the way to MongoDB 3.6, so this should be a safe change even for EOL'd MongoDB releases.

pkunk commented 3 months ago

FYI: This is not compatible with Azure Cosmos MongoDB API: https://learn.microsoft.com/en-us/azure/cosmos-db/mongodb/feature-support-60#database-commands

wilkinsona commented 3 months ago

Thanks, @pkunk, but I'm not sure there's much we can do about that. I don't see any mention of isMaster in the list of supported database commands either so it's not clear that this would have worked before. I'd recommend raising this with Microsoft to see if they can improve Cosmos' compliance with the MongoDB API.

In the meantime, anyone who's affected can work around the non-compliance by defining their own bean named mongoHealthIndicator or mongoHealthContributor.

SimonedeGijt commented 1 month ago

We have the same @pkunk . Did you already raise this with Microsoft? Would be great if I can follow the progress on this item :)

pkunk commented 3 weeks ago

We have the same @pkunk . Did you already raise this with Microsoft?

No. We just use custom mongoHealthIndicator bean.

SimonedeGijt commented 2 weeks ago

I created a post now, would be great if many people can vote on it 😄 : https://feedback.azure.com/d365community/idea/c7b19748-9276-ef11-a4e6-000d3a059eeb