-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
[Feat] Performance - Don't create 1 task for every hanging request alert #11385
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
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d3fe402
feat: add async_get_oldest_n_keys in memory cache
ishaan-jaff 2911ff0
fix: add add_request_to_hanging_request_check
ishaan-jaff fafb880
test: alerting
ishaan-jaff 00fb143
feat: v2 hanging request check
ishaan-jaff 457d027
fix: HangingRequestData
ishaan-jaff 7ecacaf
fix: AlertingHangingRequestCheck
ishaan-jaff 366126f
fix: check_for_hanging_requests
ishaan-jaff 33ee0f4
fix: use correct metadata location for hanging requests
ishaan-jaff feac7cf
fix: formatting alert
ishaan-jaff 0d0036e
test hanging request check
ishaan-jaff 0ff3ea8
fix: add guard flags for background tasks alerting
ishaan-jaff 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
175 changes: 175 additions & 0 deletions
175
litellm/integrations/SlackAlerting/hanging_request_check.py
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 |
---|---|---|
@@ -0,0 +1,175 @@ | ||
""" | ||
Class to check for LLM API hanging requests | ||
|
||
|
||
Notes: | ||
- Do not create tasks that sleep, that can saturate the event loop | ||
- Do not store large objects (eg. messages in memory) that can increase RAM usage | ||
""" | ||
|
||
import asyncio | ||
from typing import TYPE_CHECKING, Any, Optional | ||
|
||
import litellm | ||
from litellm._logging import verbose_proxy_logger | ||
from litellm.caching.in_memory_cache import InMemoryCache | ||
from litellm.litellm_core_utils.core_helpers import get_litellm_metadata_from_kwargs | ||
from litellm.types.integrations.slack_alerting import ( | ||
HANGING_ALERT_BUFFER_TIME_SECONDS, | ||
MAX_OLDEST_HANGING_REQUESTS_TO_CHECK, | ||
HangingRequestData, | ||
) | ||
|
||
if TYPE_CHECKING: | ||
from litellm.integrations.SlackAlerting.slack_alerting import SlackAlerting | ||
else: | ||
SlackAlerting = Any | ||
|
||
|
||
class AlertingHangingRequestCheck: | ||
""" | ||
Class to safely handle checking hanging requests alerts | ||
""" | ||
|
||
def __init__( | ||
self, | ||
slack_alerting_object: SlackAlerting, | ||
): | ||
self.slack_alerting_object = slack_alerting_object | ||
self.hanging_request_cache = InMemoryCache( | ||
default_ttl=int( | ||
self.slack_alerting_object.alerting_threshold | ||
+ HANGING_ALERT_BUFFER_TIME_SECONDS | ||
), | ||
) | ||
|
||
async def add_request_to_hanging_request_check( | ||
self, | ||
request_data: Optional[dict] = None, | ||
): | ||
""" | ||
Add a request to the hanging request cache. This is the list of request_ids that gets periodicall checked for hanging requests | ||
""" | ||
if request_data is None: | ||
return | ||
|
||
request_metadata = get_litellm_metadata_from_kwargs(kwargs=request_data) | ||
model = request_data.get("model", "") | ||
api_base: Optional[str] = None | ||
|
||
if request_data.get("deployment", None) is not None and isinstance( | ||
request_data["deployment"], dict | ||
): | ||
api_base = litellm.get_api_base( | ||
model=model, | ||
optional_params=request_data["deployment"].get("litellm_params", {}), | ||
) | ||
|
||
hanging_request_data = HangingRequestData( | ||
request_id=request_data.get("litellm_call_id", ""), | ||
model=model, | ||
api_base=api_base, | ||
key_alias=request_metadata.get("user_api_key_alias", ""), | ||
team_alias=request_metadata.get("user_api_key_team_alias", ""), | ||
) | ||
|
||
await self.hanging_request_cache.async_set_cache( | ||
key=hanging_request_data.request_id, | ||
value=hanging_request_data, | ||
ttl=int( | ||
self.slack_alerting_object.alerting_threshold | ||
+ HANGING_ALERT_BUFFER_TIME_SECONDS | ||
), | ||
) | ||
return | ||
|
||
async def send_alerts_for_hanging_requests(self): | ||
""" | ||
Send alerts for hanging requests | ||
""" | ||
from litellm.proxy.proxy_server import proxy_logging_obj | ||
|
||
######################################################### | ||
# Find all requests that have been hanging for more than the alerting threshold | ||
# Get the last 50 oldest items in the cache and check if they have completed | ||
######################################################### | ||
# check if request_id is in internal usage cache | ||
if proxy_logging_obj.internal_usage_cache is None: | ||
return | ||
|
||
hanging_requests = await self.hanging_request_cache.async_get_oldest_n_keys( | ||
n=MAX_OLDEST_HANGING_REQUESTS_TO_CHECK, | ||
) | ||
|
||
for request_id in hanging_requests: | ||
hanging_request_data: Optional[HangingRequestData] = ( | ||
await self.hanging_request_cache.async_get_cache( | ||
key=request_id, | ||
) | ||
) | ||
|
||
if hanging_request_data is None: | ||
continue | ||
|
||
request_status = ( | ||
await proxy_logging_obj.internal_usage_cache.async_get_cache( | ||
key="request_status:{}".format(hanging_request_data.request_id), | ||
litellm_parent_otel_span=None, | ||
local_only=True, | ||
) | ||
) | ||
# this means the request status was either success or fail | ||
# and is not hanging | ||
if request_status is not None: | ||
# clear this request from hanging request cache since the request was either success or failed | ||
self.hanging_request_cache._remove_key( | ||
key=request_id, | ||
) | ||
continue | ||
|
||
################ | ||
# Send the Alert on Slack | ||
################ | ||
await self.send_hanging_request_alert( | ||
hanging_request_data=hanging_request_data | ||
) | ||
|
||
return | ||
|
||
async def check_for_hanging_requests( | ||
self, | ||
): | ||
""" | ||
Background task that checks all request ids in self.hanging_request_cache to check if they have completed | ||
|
||
Runs every alerting_threshold/2 seconds to check for hanging requests | ||
""" | ||
while True: | ||
verbose_proxy_logger.debug("Checking for hanging requests....") | ||
await self.send_alerts_for_hanging_requests() | ||
await asyncio.sleep(self.slack_alerting_object.alerting_threshold / 2) | ||
|
||
async def send_hanging_request_alert( | ||
self, | ||
hanging_request_data: HangingRequestData, | ||
): | ||
""" | ||
Send a hanging request alert | ||
""" | ||
from litellm.integrations.SlackAlerting.slack_alerting import AlertType | ||
|
||
################ | ||
# Send the Alert on Slack | ||
################ | ||
request_info = f"""Request Model: `{hanging_request_data.model}` | ||
API Base: `{hanging_request_data.api_base}` | ||
Key Alias: `{hanging_request_data.key_alias}` | ||
Team Alias: `{hanging_request_data.team_alias}`""" | ||
|
||
alerting_message = f"`Requests are hanging - {self.slack_alerting_object.alerting_threshold}s+ request time`" | ||
await self.slack_alerting_object.send_alert( | ||
message=alerting_message + "\n" + request_info, | ||
level="Medium", | ||
alert_type=AlertType.llm_requests_hanging, | ||
alerting_metadata=hanging_request_data.alerting_metadata or {}, | ||
) |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.