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

[ENH] Add quota component and test for static #1720

Merged
merged 5 commits into from
Feb 16, 2024
Merged

Conversation

nicolasgere
Copy link
Contributor

@nicolasgere nicolasgere commented Feb 14, 2024

Description of changes

Summarize the changes made by this PR.

  • New functionality
    • Add quota check, it will be use to be able to rate limit, apply static check to payload etc.

Test plan

How are these changes tested?

  • Tests pass locally with pytest, added unit test

Documentation Changes

CIP doc

Copy link

github-actions bot commented Feb 14, 2024

Reviewer Checklist

Please leverage this checklist to ensure your code review is thorough before approving

Testing, Bugs, Errors, Logs, Documentation

  • Can you think of any use case in which the code does not behave as intended? Have they been tested?
  • Can you think of any inputs or external events that could break the code? Is user input validated and safe? Have they been tested?
  • If appropriate, are there adequate property based tests?
  • If appropriate, are there adequate unit tests?
  • Should any logging, debugging, tracing information be added or removed?
  • Are error messages user-friendly?
  • Have all documentation changes needed been made?
  • Have all non-obvious changes been commented?

System Compatibility

  • Are there any potential impacts on other parts of the system or backward compatibility?
  • Does this change intersect with any items on our roadmap, and if so, is there a plan for fitting them together?

Quality

  • Is this code of a unexpectedly high quality (Readability, Modularity, Intuitiveness)

@nicolasgere nicolasgere changed the title WIP: add quota component and test for static Add quota component and test for static Feb 15, 2024
@HammadB HammadB changed the title Add quota component and test for static [ENH] Add quota component and test for static Feb 15, 2024
Copy link
Collaborator

@HammadB HammadB left a comment

Choose a reason for hiding this comment

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

Thanks , some nits and observations but is great overall!

chromadb/quota/__init__.py Outdated Show resolved Hide resolved
chromadb/quota/__init__.py Outdated Show resolved Hide resolved
self.resource = resource


class QuotaProvider(Component):
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit - add docstrings about this class

chromadb/quota/__init__.py Outdated Show resolved Hide resolved
@nicolasgere nicolasgere requested a review from tazarov February 15, 2024 21:11
@nicolasgere nicolasgere merged commit e6ceeee into main Feb 16, 2024
98 checks passed
Copy link
Contributor

@tazarov tazarov left a comment

Choose a reason for hiding this comment

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

As a general comment, I think that quota or any limit additions to the codebase are unnecessary technical debt better moved to the Proxy/Cache/Coordinator communication pattern. However, that is just my two cents on the long-term impacts.

self.should_enforce = True
self.system = system

def payload_static_check(self, metadatas: Optional[Metadatas] = None, documents: Optional[Documents] = None, embeddings: Optional[Embeddings]= None, collection_id: Optional[str]= None):
Copy link
Contributor

Choose a reason for hiding this comment

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

We should also check ids are those are user-passed strings and in the DB we store them as text. That can get pretty large.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will add them in another PR

@@ -101,6 +101,7 @@ def __init__(self, system: System):
self._settings = system.settings
self._sysdb = self.require(SysDB)
self._manager = self.require(SegmentManager)
self._quota = self.require(QuotaEnforcer)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we enforce metadata checks on the collection level?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question, I will ask with the team.

def payload_static_check(self, metadatas: Optional[Metadatas] = None, documents: Optional[Documents] = None, embeddings: Optional[Embeddings]= None, collection_id: Optional[str]= None):
if not self.should_enforce:
return
metadata_key_length_quota = self._quota_provider.get_for_subject(resource="METADATA_KEY_LENGTH", subject=collection_id)
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this imply that all checks can only be performed against specific collections? How about quotas at user, tenant, and DB levels? Does this approach force Chroma to maintain per-collection quotas? Why are per-collection quotas more interesting than quotas at other levels of object inheritance?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is just for static one, but yes, later, tenant based quota would also be possible

@@ -291,6 +293,12 @@ def app(self) -> fastapi.FastAPI:
def root(self) -> Dict[str, int]:
return {"nanosecond heartbeat": self._api.heartbeat()}

async def quota_exception_handler(request: Request, exc: QuotaError):
return JSONResponse(
status_code=429,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is 429 (Too Many Requests) more appropriate than 413 (Payload Too Large)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we want something generic, and 429 is also use for resource exhausted. (gcp, aws)

raise QuotaError(resource="DOCUMENT_SIZE", actual=len(document) , quota=document_size_quota)
embedding_dimension_quota = self._quota_provider.get_for_subject(resource="EMBEDDINGS_DIMENSION", subject=collection_id)
if embedding_dimension_quota and embeddings:
for embedding in embeddings:
Copy link
Contributor

Choose a reason for hiding this comment

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

using any() might be a bit more python and codebase idiomatic:

if any(len(embedding) > embedding_dimension_quota for embedding in embeddings):
    raise QuotaError("EMBEDDINGS_DIMENSION", actual=max(len(embedding) for embedding in embeddings), quota=embedding_dimension_quota)

Same applies for the other checks

@@ -140,6 +141,7 @@ def __init__(self, settings: Settings):
allow_origins=settings.chroma_server_cors_allow_origins,
allow_methods=["*"],
)
self._app.add_exception_handler(QuotaError, self.quota_exception_handler)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is a dedicated exception handler better than using the existing catch_exceptions_middleware to handle the error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did not see it, can be part of another refactoring. Or maybe we should use add_exception_handler which is the way of doing it in fastapi

@@ -0,0 +1,78 @@
import random
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think it makes sense that limits-related tests are subject to the rigor of property 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.

as there is no state, I think unit test is ok. wdyt?

atroyn pushed a commit to csbasil/chroma that referenced this pull request Apr 3, 2024
## Description of changes

*Summarize the changes made by this PR.*
 - New functionality
- Add quota check, it will be use to be able to rate limit, apply static
check to payload etc.


## Test plan
*How are these changes tested?*

- [ ] Tests pass locally with `pytest`, added unit test

---------

Co-authored-by: nicolas <[email protected]>
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.

3 participants