-
Notifications
You must be signed in to change notification settings - Fork 34
aioredis client with buffer queue #63
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
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b699096
aioredis client with buffer queue
DvirDukhan a3de93e
requirments.txt fix
DvirDukhan 2331e7e
python 3.8
DvirDukhan 2bcef89
fixed PR comments
DvirDukhan d92f8b9
after rebase
DvirDukhan 58a3f34
fixed tox file
DvirDukhan 35ae1ff
cosmetics
DvirDukhan f1245c5
fixed a bug on flush with 1 sized list
DvirDukhan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,134 @@ | ||
class QueryBuffer: | ||
def __init__(self, graphname, client, config): | ||
self.nodes = None | ||
self.top_node_id = 0 | ||
import asyncio | ||
|
||
# Redis client and data for each query | ||
class InternalBuffer: | ||
def __init__(self, graphname, client): | ||
self.client = client | ||
self.graphname = graphname | ||
|
||
# Create a node dictionary if we're building relations and as such require unique identifiers | ||
if config.store_node_identifiers: | ||
self.nodes = {} | ||
else: | ||
self.nodes = None | ||
|
||
# Sizes for buffer currently being constructed | ||
self.redis_token_count = 0 | ||
self.buffer_size = 0 | ||
|
||
# The first query should include a "BEGIN" token | ||
self.graphname = graphname | ||
self.initial_query = True | ||
|
||
self.node_count = 0 | ||
self.relation_count = 0 | ||
|
||
self.labels = [] # List containing all pending Label objects | ||
self.reltypes = [] # List containing all pending RelationType objects | ||
|
||
self.nodes_created = 0 # Total number of nodes created | ||
self.relations_created = 0 # Total number of relations created | ||
|
||
# TODO consider using a queue to send commands asynchronously | ||
def send_buffer(self): | ||
|
||
def send_buffer(self, initial_query): | ||
"""Send all pending inserts to Redis""" | ||
# Do nothing if we have no entities | ||
if self.node_count == 0 and self.relation_count == 0: | ||
return | ||
return None | ||
|
||
args = [self.node_count, self.relation_count, len(self.labels), len(self.reltypes)] + self.labels + self.reltypes | ||
# Prepend a "BEGIN" token if this is the first query | ||
if self.initial_query: | ||
if initial_query: | ||
args.insert(0, "BEGIN") | ||
self.initial_query = False | ||
|
||
result = self.client.execute_command("GRAPH.BULK", self.graphname, *args) | ||
stats = result.split(', '.encode()) | ||
self.nodes_created += int(stats[0].split(' '.encode())[0]) | ||
self.relations_created += int(stats[1].split(' '.encode())[0]) | ||
return self.client.execute_command("GRAPH.BULK", self.graphname, *args) | ||
|
||
self.clear_buffer() | ||
class QueryBuffer: | ||
def __init__(self, graphname, client, config): | ||
|
||
# Delete all entities that have been inserted | ||
def clear_buffer(self): | ||
del self.labels[:] | ||
del self.reltypes[:] | ||
self.client = client | ||
self.graphname = graphname | ||
self.config = config | ||
self.async_requests = config.async_requests | ||
|
||
self.redis_token_count = 0 | ||
self.buffer_size = 0 | ||
self.node_count = 0 | ||
self.relation_count = 0 | ||
# A queue of internal buffers | ||
self.internal_buffers = list() | ||
for i in range(self.async_requests): | ||
self.internal_buffers.append(InternalBuffer(graphname, client)) | ||
# Each buffer sent to RedisGraph returns awaitable | ||
self.awaitables = set() | ||
# Pop the first buffer | ||
self.current_buffer = self.internal_buffers.pop(0) | ||
|
||
self.initial_query = True | ||
self.nodes_created = 0 # Total number of nodes created | ||
self.relations_created = 0 # Total number of relations created | ||
|
||
self.nodes = None | ||
self.top_node_id = 0 | ||
# Create a node dictionary if we're building relations and as such require unique identifiers | ||
if config.store_node_identifiers: | ||
self.nodes = {} | ||
else: | ||
self.nodes = None | ||
|
||
async def send_buffer(self, flush=False): | ||
# If flush is needed all of the awaitables need to be complete, otherwise at least one is needed. | ||
return_when_flag = asyncio.ALL_COMPLETED if flush is True else asyncio.FIRST_COMPLETED | ||
coro = self.current_buffer.send_buffer(self.initial_query) | ||
if coro is not None: | ||
self.awaitables.add(asyncio.create_task(coro)) | ||
# Requests are flushed and awaited when: | ||
# 1. Flush is needed. | ||
# 2. Initial query with BEGIN token, to avoid race condition on async RedisGraph servers. | ||
# 3. The amount of async requests has reached the limit. | ||
if(len(self.awaitables) == self.async_requests or self.initial_query is True or (flush == True and len(self.awaitables) > 0)): | ||
done, pending = await asyncio.wait(self.awaitables, return_when = return_when_flag) | ||
for d in done: | ||
result = d.result() | ||
stats = result.split(', '.encode()) | ||
self.nodes_created += int(stats[0].split(' '.encode())[0]) | ||
self.relations_created += int(stats[1].split(' '.encode())[0]) | ||
# Create a new buffer of each completed task. | ||
self.internal_buffers.append(InternalBuffer(self.graphname, self.client)) | ||
# Store the pending tasks. | ||
self.awaitables = pending | ||
self.initial_query = False | ||
# Pop a new buffer. | ||
self.current_buffer = self.internal_buffers.pop(0) | ||
|
||
async def flush(self): | ||
await self.send_buffer(flush=True) | ||
|
||
def report_completion(self, runtime): | ||
print("Construction of graph '%s' complete: %d nodes created, %d relations created in %f seconds" | ||
% (self.graphname, self.nodes_created, self.relations_created, runtime)) | ||
|
||
@property | ||
def node_count(self): | ||
return self.current_buffer.node_count | ||
|
||
@node_count.setter | ||
def node_count(self, value): | ||
self.current_buffer.node_count = value | ||
|
||
@property | ||
def buffer_size(self): | ||
return self.current_buffer.buffer_size | ||
|
||
@property | ||
def labels(self): | ||
return self.current_buffer.labels | ||
|
||
@property | ||
def reltypes(self): | ||
return self.current_buffer.reltypes | ||
|
||
@property | ||
def relation_count(self): | ||
return self.current_buffer.relation_count | ||
|
||
@relation_count.setter | ||
def relation_count(self, value): | ||
self.current_buffer.relation_count = value | ||
|
||
@property | ||
def redis_token_count(self): | ||
return self.current_buffer.redis_token_count | ||
|
||
@redis_token_count.setter | ||
def redis_token_count(self, value): | ||
self.current_buffer.redis_token_count = value | ||
|
||
@property | ||
def buffer_size(self): | ||
return self.current_buffer.buffer_size | ||
|
||
@buffer_size.setter | ||
def buffer_size(self, value): | ||
self.current_buffer.buffer_size = value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I understand correctly, this function is
async
because otherwise it cannot makeasync
calls?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
correct