[JAVA-365] Poor concurrency handling in DBApiLayer.doGetCollection Created: 02/Jun/11  Updated: 25/Jun/13  Resolved: 12/Aug/11

Status: Closed
Project: Java Driver
Component/s: Performance
Affects Version/s: 2.6.1
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Tal Liron Assignee: Antoine Girbal
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

It seems that the synchronized block has been put in place in order to guarantee that a particular DBCollection is uniquely created and returned to the caller.

The synchronization is needed because the operation as a whole must be atomic. However, it adds an extra lock/unlock beyond that provided by synchronizedMap on _collections, and this can significantly negatively affect performance in highly concurrent situations which call this essential function.

One solution could be to not use synchronizedMap, and instead handle locking ourselves by always wrapping uses of _collections with synchronized blocks. This would reduce the number of blocking lock acquisitions.

However, a much better solution is for _collection to be ConcurrentHashMap. We would then never have to use synchronized blocks, and in this case could check the result of a putIfAbsent call:

MyCollection existing = _collections.putIfAbsent(name , c);
if (existing != null) c = existing;

This code would guarantee uniqueness of the collection, and offer much better throughput in high concurrency.



 Comments   
Comment by auto [ 12/Aug/11 ]

Author:

{u'login': u'agirbal', u'name': u'agirbal', u'email': u'antoine@10gen.com'}

Message: JAVA-365: Poor concurrency handling in DBApiLayer.doGetCollection
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/31980422622c49e40f6213c634c3267b07f7af80

Comment by Antoine Girbal [ 12/Aug/11 ]

the concurrency performance impact is minimal here, regular java sync is very fast now and rarely a bottleneck.
But for sake of code cleanliness I'll switch to concurrentMap

Comment by Tal Liron [ 10/Aug/11 ]

Two months, and still no comment from the dev team?

Generated at Thu Feb 08 08:52:06 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.