Skip to content

Refactor to separate sync and async connections #1594

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

Merged
merged 71 commits into from
Apr 9, 2025

Conversation

tsmith023
Copy link
Collaborator

@tsmith023 tsmith023 commented Mar 18, 2025

This PR constitutes a complete overhaul of the codebase structure with respect to the underlying connections of the WeaviateClient and WeaviateAsyncClient classes and how they are provided to them. Currently, the WeaviateClient works by using the same ConnectionV4 (which is async) object under-the-hood that the WeaviateAsyncClient uses only with the calls scheduled in a side-car event loop thread.

With this PR, this is has been refactored so that the connection can be dependency injected into the business logic layer of the client from the top-down so that all namespaces in WeaviateClient now explicitly depend on ConnectionSync, which itself depends on httpx.Client and grpc.Channel, with ConnectionV4 renamed to ConnectionAsync that still depends on httpx.AsyncClient and grpc.aio.Channel.

This is achieved using the command pattern whereby all business logic of the client is encapsulated into Executor classes, e.g. _DataExecutor, which expose methods that exactly match those of the public API plus an additional argument: connection: ConnectionSync | ConnectionAsync. Then, at runtime, the connection is injected into the executor call by the decorator @weaviate.connect.executor.wrap("sync" | "async") so that we can have a single implementation of the inner business logic in the executor that is agnostic to exactly which connection is passed to it. The executor methods work with the union of connections by utilising the logic in weaviate/connect/executor.py, which is effectively an ad hoc implementation of callbacks.

This PR introduces the fundamental structure of the overall pattern that the client should use moving forward. However, there is a large amount of boilerplate associated with the stitching together of the sync/async clients with the executors. This should be automated in the future through custom-built auto-generation tools that translate the API surfaces of the executors into the async_.pyi and sync.pyi stub files of each namespace. Until then effort should also be spent developing a custom flake8 linter to validate that the namespace async_.pyi and sync.pyi files are identical.

Copy link

@orca-security-eu orca-security-eu bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Orca Security Scan Summary

Status Check Issues by priority
Passed Passed Secrets high 0   medium 0   low 0   info 0 View in Orca

@weaviate-git-bot
Copy link

Great to see you again! Thanks for the contribution.

beep boop - the Weaviate bot 👋🤖

PS:
Are you already a member of the Weaviate Slack channel?

@codecov-commenter
Copy link

codecov-commenter commented Mar 25, 2025

Codecov Report

Attention: Patch coverage is 86.08113% with 374 lines in your changes missing coverage. Please review.

Project coverage is 87.82%. Comparing base (9ef4682) to head (4ce71b8).
Report is 123 commits behind head on main.

Files with missing lines Patch % Lines
weaviate/connect/v4.py 78.79% 102 Missing ⚠️
weaviate/backup/executor.py 68.91% 60 Missing ⚠️
weaviate/connect/authentication.py 86.33% 19 Missing ⚠️
weaviate/rbac/executor.py 80.00% 18 Missing ⚠️
weaviate/connect/executor.py 80.00% 16 Missing ⚠️
weaviate/collections/aggregations/executor.py 38.09% 13 Missing ⚠️
weaviate/collections/tenants/executor.py 77.96% 13 Missing ⚠️
weaviate/users/executor.py 89.62% 11 Missing ⚠️
weaviate/collections/config/executor.py 91.86% 10 Missing ⚠️
...lections/queries/fetch_objects_by_ids/executors.py 70.58% 10 Missing ⚠️
... and 28 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1594      +/-   ##
==========================================
- Coverage   88.39%   87.82%   -0.57%     
==========================================
  Files         187      219      +32     
  Lines       16015    17492    +1477     
==========================================
+ Hits        14156    15362    +1206     
- Misses       1859     2130     +271     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link

@orca-security-eu orca-security-eu bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Orca Security Scan Summary

Status Check Issues by priority
Passed Passed Secrets high 0   medium 0   low 0   info 0 View in Orca

@tsmith023 tsmith023 changed the base branch from dev/1.30 to main April 4, 2025 08:41
Copy link

@orca-security-eu orca-security-eu bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Orca Security Scan Summary

Status Check Issues by priority
Passed Passed Infrastructure as Code high 0   medium 0   low 0   info 0 View in Orca
Passed Passed SAST high 0   medium 0   low 0   info 0 View in Orca
Passed Passed Secrets high 0   medium 0   low 0   info 0 View in Orca
Passed Passed Vulnerabilities high 0   medium 0   low 0   info 0 View in Orca

Copy link

@orca-security-eu orca-security-eu bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Orca Security Scan Summary

Status Check Issues by priority
Passed Passed Infrastructure as Code high 0   medium 0   low 0   info 0 View in Orca
Passed Passed SAST high 0   medium 1   low 0   info 0 View in Orca
Passed Passed Secrets high 0   medium 0   low 0   info 0 View in Orca
Passed Passed Vulnerabilities high 0   medium 0   low 0   info 0 View in Orca
🛡️ The following SAST misconfigurations have been detected
NAME FILE
medium Flask app configured to run with host 0.0.0.0, exposing it publicly ...sts/gunicorn/wsgi.py View in code

@tsmith023 tsmith023 merged commit 229b4af into main Apr 9, 2025
44 of 46 checks passed
@tsmith023 tsmith023 deleted the poc-separate-sync-and-async branch April 9, 2025 12:51
@eavanvalkenburg
Copy link

This change broke unit tests for me, because suddenly the create and other method on this AsyncMock(spec=_CollectionsAsync) are created as MagicMock, rather then AsyncMock...

@tsmith023
Copy link
Collaborator Author

Hi @eavanvalkenburg, thanks for raising! I'll look into how we can work around that and release a fix

@eavanvalkenburg
Copy link

was able to work around it, by doing this:

# previous code:
async_mock = AsyncMock(spec=WeaviateAsyncClient)
async_mock.collections = AsyncMock(spec=_CollectionsAsync)
# new additions:
async_mock.collections.create = AsyncMock()
async_mock.collections.delete = AsyncMock()
async_mock.collections.exists = AsyncMock()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants