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

UpdateStreamHeadAsync upserts will error when using Mongo V3.0.3+ #35

Closed
mikegore1000 opened this issue Jun 8, 2016 · 8 comments
Closed

Comments

@mikegore1000
Copy link

The Mongo query that the UpdateStreamHeadAsync method will fail on versions of Mongo V3.0.3+. This seems to be the case with both MMAP storage and WiredTiger.

BsonDocument streamHeadId = GetStreamHeadId(bucketId, streamId);
                    PersistedStreamHeads.Update(
                        Query.EQ("_id", streamHeadId),
                        Update
                            .Set(MongoStreamHeadFields.HeadRevision, streamRevision)
                            .Inc(MongoStreamHeadFields.SnapshotRevision, 0)
                            .Inc(MongoStreamHeadFields.Unsnapshotted, eventsCount),
                        UpdateFlags.Upsert);

We've only tested on MMAP, but the JIRA ticket states this is a WiredTiger issue too:
https://jira.mongodb.org/browse/SERVER-14322

This only happens if two threads perform a commit at the same time, but as our app cannot catch the exception it terminates the process (raising a MongoDuplicateKeyException). In order to fix this a retry policy needs to be implemented so if the initial Upsert fails the second attempt then performs an update.

However, this does mean that the streams document isn't guaranteed to be the latest one, however this is true in the current implementation anyway due to the use of tasks to cover this.

The only other alternative I see is to swallow the exception and accept the failure here, which doesn't feel great.

@alkampfergit
Copy link
Contributor

Actually it should be caused by the old legacy driver. The released version of MongoDb Adapter uses the 1.10 version of the driver, and it is not fully compatible with the new version of mongo.

In develop branch of MongoDb adapter we migrated to new driver, but it is for the next version of NEventStore, probably it would be a good idea to publish another version of MongDb persistence package that uses new driver version, to allow people to use new version of mongo.

@mikegore1000
Copy link
Author

According to https://docs.mongodb.com/ecosystem/drivers/csharp/ the 1.10.0 driver is compatible with Mongo 3.0, just not 3.1 onwards.

Can you let me know what feature in the driver would cause the upsert failure issue? Our service worked fine apart from this particular upsert issue which caused our processes to terminate.

We had less loaded services that ran with no issues at all.

@mikegore1000
Copy link
Author

I do agree though that pushing a new package to get the latest drive support would be a very good thing. Quite happy to help with a pull request on this.

I do think we'd still see the upsert issue though as it seems to be server related rather than client related.

@alkampfergit
Copy link
Contributor

Ok, thanks a lot. Tomorrowwe will review the pull request.

alkampfergit added a commit that referenced this issue Jun 14, 2016
Actually when UpdateStreamAsync is called with high load
and WiredTiger, it is possible that the update of stream
head throws a MongoDuplicateKeyException. In such a
situation we can safely ignore the error, because some
other thread already updated the record and we are not
able to know which is the latest one.
This was referenced Jun 14, 2016
alkampfergit added a commit that referenced this issue Jun 14, 2016
@mikegore1000
Copy link
Author

Is the new version of the package going to be published to NuGet? I can't see it there at present.

@alkampfergit
Copy link
Contributor

alkampfergit commented Jul 12, 2016

Sorry for the delay actually it is published on myget feed

https://www.myget.org/feed/neventstore-ci/package/nuget/NEventStore.Persistence.MongoDB

version is 5.3.5.

Could you please test it? If everything is ok we will push to Nuget feed.

@mikegore1000
Copy link
Author

Sorry it's taken so long to get back to you on this. We have just deployed the fix to production with Mongo V3 + WiredTiger and it's working great.

@alkampfergit
Copy link
Contributor

Thanks a lor for confirming that the patch solved the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants