meteor-space / event-sourcing

CQRS and Event Sourcing Infrastructure for Meteor.
MIT License
63 stars 9 forks source link

Concurrency #75

Closed qejk closed 8 years ago

qejk commented 8 years ago

Rhys and I found a issue with concurrency exception being thrown on Space.eventSourcing.CommitStore.

This issue is related to lack of try catch on https://github.com/meteor-space/event-sourcing/blob/master/source/server/infrastructure/commit_store.coffee#L57

commitId = @commits.insert commit

Because of it, errors are thrown like:

{ [MongoError: insertDocument :: caused by :: 11000 E11000 duplicate key error index: admin.space_eventSourcing_commits.$sourceId_1_version_1  dup key: { : "18c18a9a-25da-42ab-84dd-61f3bfff6999", : 598 }] stack: [Getter] }

That causes not only commit not being inserted, can cause issues with commit publishing but also the subject of topic - aggregate in memory not being properly versioned since thats a silent fail, that still allows this code to be run: https://github.com/meteor-space/event-sourcing/blob/dee18c7acfc4c3dbb3b651f1385f285704158ecc/source/server/infrastructure/repository.coffee#L31

To reproduce error, two clients are required (it does not matter if via 'client' or meteor shell), that will send commands like on example of space-todos:

var newTodoData = {
  title: 'abcd'
};
var i = 0;
for (i = 0; i <= 2000; ++i) {
  Todos.app.send(
    new Todos.CreateTodo(_.extend({}, newTodoData, {
      targetId: Todos.app.configuration.todoListId
    }))
  )
}