Description
In addition to relaxing synchronization in MongoMK we also need to look at thread safety in MongoDocumentStore. AFAICS MemoryDocumentStore is thread-safe and access to the store is consistent. This is currently not the case for MongoDocumentStore. The main problem is maintaining the document cache in MongoDocumentStore. Access to the cache is not coordinated between multiple threads. I avoided the term synchronized, because the actual cache implementation is thread-safe. What needs to be fixed is how MongoDocumentStore interacts with the cache. E.g. findAndModify() sends the update to MongoDB, gets the old document back, applies the updates to the old document and then puts it into the cache. If this happens concurrently with multiple threads it may happen that these operations overlap and a thread puts an outdated document into the cache.