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

MongoDB - Fix Unhealthy if no collections exists in the specified database #1626

Merged
merged 3 commits into from
Jan 17, 2023

Conversation

ThumNet
Copy link
Contributor

@ThumNet ThumNet commented Jan 11, 2023

What this PR does / why we need it:
We are experiencing unhealthy checks if a specified mongo database has no collections yet.

System.InvalidOperationException: Sequence contains no elements at System.Linq.ThrowHelper.ThrowNoElementsException() at System.Linq.Enumerable.First[TSource](IEnumerable`1 source) at MongoDB.Driver.IAsyncCursorExtensions.FirstAsync[TDocument](IAsyncCursor`1 cursor, CancellationToken cancellationToken) at HealthChecks.MongoDb.MongoDbHealthCheck.CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken) in /_/src/HealthChecks.MongoDb/MongoDbHealthCheck.cs:line 43

Which issue(s) this PR fixes:
#617

Special notes for your reviewer:
None

Does this PR introduce a user-facing change?:
No

Please make sure you've completed the relevant tasks for this PR, out of the following list:

  • Code compiles correctly
  • Created/updated tests
  • Unit tests passing
  • End-to-end tests passing
  • Extended the documentation
  • Provided sample for the feature

@sungam3r
Copy link
Collaborator

Thanks.

@sungam3r
Copy link
Collaborator

sungam3r commented Jan 14, 2023

@ThumNet One test failed. I think mongoClient.GetDatabase(_specifiedDatabase) returns an instance even for non existing database so First() method correctly failed HC before. I'm fine using FirstOrDefault but of course some other changes are required to fix failed test.

@ThumNet
Copy link
Contributor Author

ThumNet commented Jan 14, 2023

Looking into the failing test, will update the PR when fixed.

@ThumNet
Copy link
Contributor Author

ThumNet commented Jan 14, 2023

After looking into the failing test, which is called be_unhealthy_on_connectionstring_specified_database_if_mongodb_is_available_and_database_exist.

I think that this test should NOT exists, because of the way mongodb works. MongoDB only creates the database when you first store data in that database. This data could be a collection or a document.

Sources: official mongo docs and blog post

@sungam3r Do you agree the test can be removed?

@sungam3r
Copy link
Collaborator

I've read provided links. Do you think the current method of checking the state of MongoDB is sufficient? I have no experience with MongoDB.

@ThumNet
Copy link
Contributor Author

ThumNet commented Jan 14, 2023

Instead of getting the collections i think it is better to implement a ping to the database. Others also recommend this.

I will change the code to allow this and update the PR. Also change the test accordingly.

@ThumNet
Copy link
Contributor Author

ThumNet commented Jan 15, 2023

@sungam3r Update has just been pushed.

@sungam3r
Copy link
Collaborator

@ThumNet Thanks.

@sungam3r sungam3r merged commit bb178e9 into Xabaril:master Jan 17, 2023
@sungam3r sungam3r mentioned this pull request Jul 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MongoDb health check fails to be healthy when connecting to a database that has no collections yet
2 participants