Repository navigation
feat: add async methods for FalkorDBDocumentStore - #3999
Conversation
Coverage report (falkordb)Click to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||||||||||||||||||||
bogdankostic
left a comment
There was a problem hiding this comment.
Thanks for the PR @maxdswain! I had a first pass and found mostly small things, but one needs fixing before we merge: with recreate_graph=True, whichever of the sync and async paths warms up second drops the graph the other one populated, so data written through one is silently lost.
Also, let's try to make sure that unit test coverage stays high.
| if self.recreate_graph: | ||
| try: | ||
| await async_client.select_graph(self.graph_name).delete() |
There was a problem hiding this comment.
recreate_graph=True silently wipes data once both paths warm up.
warm_up() and warm_up_async() each honour self.recreate_graph independently, so whichever warms up second drops the graph the first one populated.
store.write_documents([Document(content="hello")])
store.count_documents() # -> 1
await store.count_documents_async() # -> 0 warm_up_async() just dropped the graph
store.count_documents() # -> 0 data is gone| self.warm_up() | ||
| graph = self.graph | ||
| if graph is None: | ||
| msg = "The synchronous FalkorDB graph is unavailable after warm-up." | ||
| raise RuntimeError(msg) |
There was a problem hiding this comment.
This is repeated throughout multiple methods. Let's move this to a separate method to reduce code duplication.
Also, I'm not sure if graph ever can be None in these cases after calling self.warm_up, so if the RuntieError is reachable.
There was a problem hiding this comment.
You're right that this code isn't reachable due to the self.warm_up method, but the type checker doesn't know that. It is a lot of duplicate code so I looked at other integrations and assert graph is not None # noqa: S101 is used instead so I've switched to that to make the type checker happy.
There was a problem hiding this comment.
Can we do this throughout the file? Now there's a mix between assert graph is not None and the RuntimeError.
Also, the local graph variable isn't needed, mypy narrows self.graph directly after the assert:
self.warm_up()
assert self.graph is not None # noqa: S101
result = self.graph.query(...)This should also bring back most of the unit test coverage, as 48 of the 92 uncovered statements in document_store.py are the unreachable msg/raise lines of the RuntimeError checks.
| await self.warm_up_async() | ||
| graph = self.async_graph | ||
| if graph is None: | ||
| msg = "The asynchronous FalkorDB graph is unavailable after warm-up." | ||
| raise RuntimeError(msg) |
There was a problem hiding this comment.
Same here regarding duplicate code, let's move this to a separate method to be used by the async methods.
There was a problem hiding this comment.
Addressed by reply to your other comment.
| assert float_values == [1.5] and type(float_values[0]) is float | ||
| assert bool_values == [True] and type(bool_values[0]) is bool | ||
|
|
||
| def test_close_and_reopen(self, document_store): |
There was a problem hiding this comment.
close() resets initialized, so this warm_up() runs the recreate_graph delete from the fixture a second time:
store.write_documents([Document(content="hello")])
store.count_documents() # 1
store.close()
store.warm_up()
store.count_documents() # 0 <- graph dropped againThe test only passes because the store is empty at this point. Once the shared-flag fix is in, let's write a document before close() and assert it survives the reopen.
| # Internal retrieval helpers (called by retriever components) | ||
| # ------------------------------------------------------------------ | ||
|
|
||
| def _embedding_retrieval( |
There was a problem hiding this comment.
What do you think about adding _embedding_retrieval_async and _cypher_retrieval_async in this PR and making use of them in the retrievers through run_async? Or should this rather be tackled in a separate PR?
| row = result.result_set[0] | ||
| return {"min": row[0], "max": row[1]} | ||
|
|
||
| async def get_metadata_field_unique_values_async( |
There was a problem hiding this comment.
The sync get_metadata_field_unique_values documents that Cypher's DISTINCT collapses a whole-number float into an equal int.
Let's carry that note over here.
Same for write_documents_async (flat meta layout, NONE treated as FAIL), and _handle_duplicate_documents_async / _write_batch_async / delete_all_documents_async whose doc strings are much shorter compared to their sync counterparts which document params.
| rows = result.result_set | ||
| return int(rows[0][0]) if rows else 0 | ||
|
|
||
| async def filter_documents_async(self, filters: dict[str, Any] | None = None) -> list[Document]: |
There was a problem hiding this comment.
Let's keep the link to the metadata filtering docs that the sync filter_documents has in its :param filters:. Also, this one correctly says :raises FilterError: — the sync one still says :raises ValueError:, could we fix that while we're here?
| def assert_documents_are_equal(received: list[Document], expected: list[Document]) -> None: | ||
| """Compare documents while allowing for FalkorDB's float32 embeddings.""" | ||
| assert len(received) == len(expected), f"Expected {len(expected)} documents but got {len(received)}" | ||
| received_sorted = sorted(received, key=lambda document: document.id) | ||
| expected_sorted = sorted(expected, key=lambda document: document.id) | ||
| for received_document, expected_document in zip(received_sorted, expected_sorted, strict=True): | ||
| assert received_document.id == expected_document.id | ||
| assert received_document.content == expected_document.content | ||
| assert received_document.meta == expected_document.meta | ||
| assert (received_document.embedding is None) == (expected_document.embedding is None) |
There was a problem hiding this comment.
This is a copy of assert_documents_are_equal in test_document_store.py. Let's move it to a shared helper so the two can't drift.
|
Thanks for the review @bogdankostic, I should have now addressed all of your comments. |
bogdankostic
left a comment
There was a problem hiding this comment.
Thanks for addressing my comments @maxdswain! This PR is almost good to go, just a few minor things before we can ship it.
| self.warm_up() | ||
| graph = self.graph | ||
| if graph is None: | ||
| msg = "The synchronous FalkorDB graph is unavailable after warm-up." | ||
| raise RuntimeError(msg) |
There was a problem hiding this comment.
Can we do this throughout the file? Now there's a mix between assert graph is not None and the RuntimeError.
Also, the local graph variable isn't needed, mypy narrows self.graph directly after the assert:
self.warm_up()
assert self.graph is not None # noqa: S101
result = self.graph.query(...)This should also bring back most of the unit test coverage, as 48 of the 92 uncovered statements in document_store.py are the unreachable msg/raise lines of the RuntimeError checks.
|
|
||
| def _handle_duplicate_documents( | ||
| self, | ||
| graph: Graph, |
There was a problem hiding this comment.
Before this PR, _handle_duplicate_documents and _write_batch didn't take a graph parameter. Let's remove it again and instead do assert self.graph is not None inside these methods, so their signatures stay the same as on main. Same for _handle_duplicate_documents_async and _write_batch_async with self.async_graph.
| if self.recreate_graph: | ||
| if self.recreate_graph and not self._recreate_graph_applied: | ||
| # Recreation is shared by the sync and async connection lifecycles. | ||
| self._recreate_graph_applied = True |
There was a problem hiding this comment.
The flag is set before the delete, and any error during the delete is only logged at debug level. So if the first warm-up fails, e.g. because the server isn't reachable yet, the graph is never recreated on retry and we keep the old data. Should we only catch the ResponseError raised when the graph doesn't exist yet, and reset the flag on any other error? Same in warm_up_async.
…and fix recreate graph bug with sync/async warm_up methods
…om funcs, catch ResponseError from falkordb graph not existing
0910d01 to
3933f7f
Compare
|
Thanks for reviewing once again @bogdankostic, should have now addressed all of your comments. |
bogdankostic
left a comment
There was a problem hiding this comment.
Thanks for addressing all my comments @maxdswain! I pushed a small fix on top: when several async calls hit a fresh store at the same time, each one created its own client, and with recreate_graph=True one of them could query the graph while another was still deleting it. warm_up_async() is now guarded by an asyncio.Lock
Related Issues
_asyncmethods for FalkorDB Integration #3998Proposed Changes:
Add
asyncequivalent methods toFalkorDBDocumentStoreusing falkordb's Python client async client.How did you test it?
Added unit and integration tests, mostly coming from the document store mixin tests.
Notes for the reviewer
Checklist
fix:,feat:,build:,chore:,ci:,docs:,style:,refactor:,perf:,test:.