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

Change the feast serve endpoint to be sync rather than async. #2119

Merged
merged 1 commit into from
Dec 17, 2021

Conversation

nossrannug
Copy link
Contributor

Signed-off-by: Gunnar Sv Sigurbjörnsson gunnar.sigurbjornsson@gmail.com

What this PR does / why we need it:
When an endpoint is async, everything happens on the event loop thread.
This is great for functionality that is IO heavy since the job can be
awaited and other requests processed in the meantime. But if there are
no awaits, then everything happens synchronously on one thread which
means that while we're waiting for a response from a DB or API call,
nothing else gets processed.
To address this I've removed the async keyword from the endpoint.
This makes FastAPI process each request on a thread in a thread pool.

Testing this with postgres as the online store, running everything locally,
and 1 worker, req/sec went from 200 to 800.

Does this PR introduce a user-facing change?:

NONE

@nossrannug nossrannug requested a review from a team as a code owner December 8, 2021 00:37
@nossrannug nossrannug requested review from woop and removed request for a team December 8, 2021 00:37
@feast-ci-bot
Copy link
Collaborator

Hi @nossrannug. Thanks for your PR.

I'm waiting for a feast-dev member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@codecov-commenter
Copy link

codecov-commenter commented Dec 8, 2021

Codecov Report

Merging #2119 (a3b1798) into master (97fbd3e) will not change coverage.
The diff coverage is 72.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #2119   +/-   ##
=======================================
  Coverage   84.49%   84.49%           
=======================================
  Files         101      101           
  Lines        8107     8107           
=======================================
  Hits         6850     6850           
  Misses       1257     1257           
Flag Coverage Δ
integrationtests 74.46% <72.00%> (-0.29%) ⬇️
unittests 58.73% <64.00%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
sdk/python/feast/feature_server.py 30.76% <28.57%> (+1.03%) ⬆️
sdk/python/feast/cli.py 38.53% <66.66%> (+0.18%) ⬆️
sdk/python/feast/feature_store.py 91.36% <85.71%> (ø)
sdk/python/feast/registry.py 87.10% <100.00%> (-0.14%) ⬇️
.../integration/online_store/test_universal_online.py 98.13% <0.00%> (+0.46%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 97fbd3e...a3b1798. Read the comment docs.

Copy link
Member

@achals achals left a comment

Choose a reason for hiding this comment

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

Thnaks @nossrannug! This looks very promising.

@feast-ci-bot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: achals, nossrannug

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

app = get_app(store)
click.echo(
"This is an "
+ click.style("experimental", fg="yellow", bold=True, underline=True)
+ " feature. It's intended for early testing and feedback, and could change without warnings in future releases."
)
uvicorn.run(app, host=host, port=port)
uvicorn.run(app, host=host, port=port, access_log=(not no_access_log))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hey @nossrannug , thanks for your PR. But I'm confused, what's the benefit of moving to the synchronous interface if we'll continue to use uvicorn as our server and at the end of the day we'll have a single-threaded app.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Did you use some other server in your tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey @pyalex,
We're not running a single-threaded app. FastAPI supports mixing async and sync methods(endpoints) as much as you want and it will take care of running it correctly. When a method is async it will be executed using the event loop, but when a method is synchronous it will be executed in a thread pool.

sync:
image

async:
image

@pyalex
Copy link
Collaborator

pyalex commented Dec 10, 2021

I totally agree with raised problem, but the proposed solution looks strange to me. I think we either should remove async frameworks (fastapi and uvicorn) from our code completely or use asynchronous dispatcher but run heavy parts in thread executors, eg:

async def get_online_features(request: Request):
     await loop.run_in_executor(ex, fs.get_online_features, *args)

@nossrannug
Copy link
Contributor Author

I think we either should remove async frameworks (fastapi and uvicorn) from our code completely or use asynchronous dispatcher but run heavy parts in thread executors

FastAPI does what you're suggesting when the method isn't async.
https://github.com/tiangolo/fastapi/blob/0a87bc88b85445018d3ced9d07ac6a37f9e6d8f1/fastapi/routing.py#L151
If the method is async, it will be awaited. But if the method isn't async then it is run in a threadpool.

@pyalex
Copy link
Collaborator

pyalex commented Dec 13, 2021

Thanks for this clarification @nossrannug . In that case, this approach makes sense. Can you please fix conflicts?

@@ -104,6 +104,7 @@ def __init__(self, registry_config: RegistryConfig, repo_path: Path):
repo_path: Path to the base of the Feast repository
or where it will be created if it does not exist yet.
"""
self.lock = Lock()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we have a more descriptive name for this variable, like refresh_lock. And can you please add _ as a prefix?

@nossrannug
Copy link
Contributor Author

Thanks for this clarification @nossrannug . In that case, this approach makes sense. Can you please fix conflicts?

Resolved

When an endpoint is async, everything happens on the event loop thread.
This is great for functionality that is IO heavy since the job can be
awaited and other requests processed in the meantime. But if there are
no awaits, then everything happens synchronously on one thread which
means that while we're waiting for a response from a DB or API call,
nothing else gets processed.
To address this I've removed the async keyword from the endpoint.
This makes FastAPI process each request on a thread in a thread pool.

Signed-off-by: Gunnar Sv Sigurbjörnsson <gunnar.sigurbjornsson@gmail.com>
@pyalex
Copy link
Collaborator

pyalex commented Dec 17, 2021

/lgtm

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

Successfully merging this pull request may close these issues.

6 participants