Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SCAN: Iterate over cluster nodes in parallel rather than sequentially #197

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions aredis/commands/iter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
from collections import defaultdict
import asyncio


class IterCommandMixin:
Expand Down Expand Up @@ -81,17 +82,31 @@ async def zscan_iter(self, name, match=None, count=None,
class ClusterIterCommandMixin(IterCommandMixin):

async def scan_iter(self, match=None, count=None):

async def iterate_node(node, queue):
nonlocal match, count
cursor = '0'
while cursor != 0:
pieces = [cursor]
if match is not None:
pieces.extend(['MATCH', match])
if count is not None:
pieces.extend(['COUNT', count])
response = await self.execute_command_on_nodes(
[node], 'SCAN', *pieces)
cursor, data = list(response.values())[0]
for item in data:
await queue.put(item) # blocks if queue is full

# maxsize ensures we don't pull too much data into
# memory if we are not processing it yet
queue = asyncio.Queue(maxsize=1000)
tasks = []
nodes = await self.cluster_nodes()
for node in nodes:
if 'master' in node['flags']:
cursor = '0'
while cursor != 0:
pieces = [cursor]
if match is not None:
pieces.extend(['MATCH', match])
if count is not None:
pieces.extend(['COUNT', count])
response = await self.execute_command_on_nodes([node], 'SCAN', *pieces)
cursor, data = list(response.values())[0]
for item in data:
yield item
t = asyncio.create_task(iterate_node(node, queue))
tasks.append(t)

while not all(t.done() for t in tasks):
yield await queue.get()