Make CouchBase.conn thread-safe
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
Microfiber |
Fix Released
|
High
|
Jason Gerard DeRose |
Bug Description
Currently Microfiber isn't thread-safe as there wasn't really a pressing need in Novacut-land and I wanted to keep things as simple as possible.
However, it's trivial to make Microfiber thread-safe as the HTTPConnection instance is the only thing that isn't thread-safe (the http connection is available on the `CouchBase.conn` attribute). Steps needed to make it thread-safe:
1) Change `CouchBase.
2) Add a `CouchBase._conn` instance attribute that is a threading.local() instance (this should be created in `CouchBase.
3) Change `CouchBase.conn` into a property that creates the connection instance if it doesn't exist in the current thread
Now I added a "?" in the bug title because we'll do this on one condition: as long as this doesn't cause any performance regression. I doubt this will, but we're going to measure it to be sure.
With the way we're using CouchDB for event propagation, we have to be careful about performance otherwise it will introduce too much latency. Currently Microfiber is fast enough for our needs, but we can't just take that for granted because, as a cautionary tale, python-couch is *not* fast enough for our needs. I've never been able to figure out why python-couchdb is so much slower when you PUT or POST a doc to CouchDB, but it can only do about 20 docs a second (or in other words, it adds around 50ms latency).
In comparison, Microfiber can PUT/POST about 160 docs a second, meaning it only adds around 6ms latency.
Oh, also note that reusing the connection provides a substantial performance improvement. I wanted to keep microfiber as simple as possible, so at first I tried just creating a HTTPConnection inside the CouchBase.
Honestly, there isn't a huge pressing need for Microfiber to be thread-safe in Novacut-land (if needed, each thread can just create its own Server or Database instance). But my hunch is having Microfiber thread-safe will probably help it get more widely adopted elsewhere.
Feedback welcomed as to whether this is even worth the trouble.
Related branches
- Jason Gerard DeRose: Approve
-
Diff: 177 lines (+37/-4)4 files modifiedbenchmark_microfiber.py (+3/-1)
benchmark_pycouchdb.py (+4/-0)
microfiber.py (+9/-2)
test_microfiber.py (+21/-1)
Changed in microfiber: | |
milestone: | 11.10 → 11.11 |
Changed in microfiber: | |
milestone: | 11.11 → 11.12 |
Changed in microfiber: | |
milestone: | 11.12 → 12.01 |
Changed in microfiber: | |
milestone: | 12.01 → 12.02 |
Changed in microfiber: | |
milestone: | 12.02 → 12.03 |
Changed in microfiber: | |
milestone: | 12.03 → 12.04 |
Changed in microfiber: | |
milestone: | 12.04 → 12.05 |
Changed in microfiber: | |
status: | Triaged → In Progress |
assignee: | nobody → Jason Gerard DeRose (jderose) |
Changed in microfiber: | |
status: | In Progress → Fix Committed |
summary: |
- Make `CouchBase.conn` thread-safe? + Make CouchBase.conn thread-safe |
Changed in microfiber: | |
status: | Fix Committed → Fix Released |
Lately there have been cases coming up in both Dmedia and Novacut where having CouchBase.conn thread-safe would be very handy, and as there seems to be no performance penalty for this, I'm pushing forward on this change.