Repository navigation
Add multi_set() - #531
Add multi_set()#531RayBB wants to merge 4 commits into
Conversation
multi_get() has no counterpart for writing, so setting many keys costs a round trip each (aio-libs#436). multi_set() pipelines a set command per item on one connection. Arguments are validated, and the flag handler run, before a connection is taken. If memcached rejects only some items, as it does for one too large, every reply is still read and the connection is kept, so the ClientException can name each rejected key and the caller knows the rest were stored. Any other unexpected reply raises at once and discards the connection, as the stream can't be trusted. Commands go out in batches of up to 1000 items or about 1 MB, and the replies to each batch are read while the next is sent. Writing never waits for the transport to drain, so it can't block on memcached, which stops reading while its replies back up. Batching bounds memory (on Python 3.11 writelines() copies what it is given) and the time asyncio spends per write, which grows with the number of chunks buffered.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #531 +/- ##
==========================================
+ Coverage 98.37% 98.55% +0.18%
==========================================
Files 10 11 +1
Lines 1229 1386 +157
Branches 65 81 +16
==========================================
+ Hits 1209 1366 +157
Misses 12 12
Partials 8 8 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
[Medium impact] The PR appears safe to merge, with no new actionable issues found. Reviews (3) · Last reviewed commit: "Merge master" · Reviewed by Greptile |
The flag handler runs for every item before a connection is taken, so the timeout doesn't cover it, and its results are kept until the call ends.
# Conflicts: # CHANGES.rst
| keys.append(key) | ||
| header = b"set %b %a %a %a\r\n" % (key, flags, exptime, len(value)) | ||
| chunks.extend((header, value, b"\r\n")) |
There was a problem hiding this comment.
multi_get() is just one command, right? I had assumed from the feature request that memcached had another command for doing multiple sets in the same fashion.
If the only thing this is gaining is pipelining a sequence of sets I'm a little more skeptical on its utility (I'd expect the performance to not be that much better than just doing asyncio.gather() on a sequence of set() requests). If we really want this, maybe there's a way to make pipelining a general purpose solution that can be used with any method?
There was a problem hiding this comment.
@Dreamsorcerer Right, there's no multi-set command; get takes several keys but set doesn't, so it's only pipelining. It makes a big difference though, as gather() pays a round trip per item across pool_size connections. Against local memcached (default pool_size=2):
| Items | gather(set) |
multi_set |
|---|---|---|
| 100 × 100 B | 54 ms | 1.8 ms |
| 1,000 × 100 B | 209 ms | 3.7 ms |
| 10,000 × 100 B | 1,367 ms | 33 ms |
| 1,000 × 10 KB | 114 ms | 28 ms |
With pool_size=10 it's still 3–21× faster, and over a real network the gap would be wider. A general pipeline API would be nice, but it's a bigger change: each command's reply parsing would need splitting from sending it. I'm happy to try that if you'd prefer it over multi_set(), or as a follow-up.
Fixes #436.
Adds
multi_set(mapping, exptime=0), the counterpart ofmulti_get(). It pipelines asetfor each item on one connection instead of a round trip per key. It returnsNoneand raises on failure, likeset()does for an error reply. A list of failed keys (pymemcache, Django) would be the only sign that a too-large item was dropped. Per-key bools would always be True, because memcached never answerssetwith NOT_STORED.exptimeand the flag handler are checked before a connection is taken. An empty mapping doesn't touch the pool.SERVER_ERROR object too large for cache), every reply is still read. The connection is kept, andClientException("Memcached multi_set failed for some keys", {key: reply})is raised. All the other items were stored.ClientException("Memcached multi_set failed", reply)at once and discards the connection.drain(), so it can't block on memcached, which stops reading while its replies back up. Batching keeps memory bounded, along with the time asyncio spends per write, which grows with the number of chunks buffered. 1001 × 1 MB takes about as long as a loop ofset(), and 100k small keys take about 0.2s.