Repository navigation
feat: merge equivalent checks in BatchCheck #334
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
Open
Siddhant-K-code
wants to merge
41
commits into
main
Choose a base branch
from
siddhant-k-code-optimize-relation-aliases
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+11,164
−35
Open
Changes from all commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
f490b44
feat: merge equivalent checks in BatchCheck (beta)
Siddhant-K-code 3f707aa
fix: tighten merge_equivalent_checks validation
Siddhant-K-code e07c07f
test: cover an empty model response and the checks_sent setter
Siddhant-K-code fae9939
docs: note that merge_equivalent_checks may change while in beta
Siddhant-K-code 4805d23
fix: count merged checks even when a request fails
Siddhant-K-code 45d733c
fix: don't keep a model read that finished before it was registered
Siddhant-K-code d579913
test: check the sent context and contextual tuples in shared cases
Siddhant-K-code ccf64fb
test: update shared cases to declare contextual tuple condition params
Siddhant-K-code 23b945e
fix: send checks with deeply nested context as is when merging
Siddhant-K-code 1d4d01a
test: check that merged batch checks send the consistency option
Siddhant-K-code 882d25e
fix: fail early when the model doesn't exist and retry transient mode…
Siddhant-K-code ce0957a
fix: fail the call when the credentials can't read the model
Siddhant-K-code 16a29a2
docs: drop beta wording from merge_equivalent_checks
Siddhant-K-code f74eac1
test: update shared equivalent-checks cases to openfga/language@153c7a9
Siddhant-K-code c1f6de3
refactor: address review on merge_equivalent_checks
Siddhant-K-code 8d27822
Merge remote-tracking branch 'origin/main' into siddhant-k-code-optim…
Siddhant-K-code 56574bc
fix: don't retry the model read for merge_equivalent_checks
Siddhant-K-code 688c812
Merge remote-tracking branch 'origin/main' into siddhant-k-code-optim…
Siddhant-K-code 60aa1e8
test: check the model read isn't retried on network errors
Siddhant-K-code 7550d14
refactor: make the merge modules private
Siddhant-K-code 77a90b1
refactor: return the responses from _send_batch_checks and share the …
Siddhant-K-code 429aeac
docs: say when batch_check calls are sent unmerged
Siddhant-K-code 70d49d7
test: update the shared equivalent-checks cases to 67
Siddhant-K-code fbd127a
docs: say which token request failures fail the call
Siddhant-K-code 6f8957e
fix: fail the call when the token request fails, instead of falling back
Siddhant-K-code d7d3b2c
fix: tell token failures apart from model read failures
Siddhant-K-code 73c5b24
test: pin the shared equivalent-checks cases to openfga/language@b7cc548
Siddhant-K-code bfb72a7
refactor: stop remembering a 429 from the model read
Siddhant-K-code 46cbdfd
docs: say alias checks need the same context, and the cache holds 32 …
Siddhant-K-code 1f57c49
chore: mark the shared cases JSON as generated
Siddhant-K-code 15e49a1
refactor: read the model in the model cache, not in a callback from t…
Siddhant-K-code 9341f8d
perf: resolve model aliases in linear time
Siddhant-K-code 9a0fce5
Merge branch 'main' into siddhant-k-code-optimize-relation-aliases
Siddhant-K-code 8009f4e
test: cover more alias trees and document how aliases are numbered
Siddhant-K-code 0f1425d
test: pin shared cases to the openfga/language#718 merge commit
Siddhant-K-code 0c73f59
docs: drop a redundant sentence about the option being off
Siddhant-K-code bf9eccc
fix: catch token failures where the model read gets its token
Siddhant-K-code 8a3389a
ci: check the equivalent-checks cases match LANGUAGE_REF
Siddhant-K-code ab8b129
refactor: address review comments
Siddhant-K-code d72aa55
chore: check the equivalent-checks cases with make instead of CI
Siddhant-K-code 427ff3f
docs: say checks_sent is None when off, and how cache misses are counted
Siddhant-K-code 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| test/data/equivalent-checks-cases.json linguist-generated=true |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| """ | ||
| Steps of a BatchCheck call with `merge_equivalent_checks` on that are the same in the | ||
| async and sync clients. The model caches read the model, and the clients send the | ||
| checks. Not part of the public API. | ||
| """ | ||
|
|
||
| import logging | ||
|
|
||
| from openfga_sdk.client._equivalent_checks import ( | ||
| EquivalentCheckGroup, | ||
| resolve, | ||
| unmerged, | ||
| ) | ||
| from openfga_sdk.client._model_cache_base import ( | ||
| ModelLookup, | ||
| TokenRequestFailed, | ||
| model_read_fails_call, | ||
| ) | ||
| from openfga_sdk.client.models.batch_check_item import ClientBatchCheckItem | ||
| from openfga_sdk.configuration import RetryParams | ||
| from openfga_sdk.telemetry.attributes import TelemetryAttributes | ||
|
|
||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def model_read_kwargs( | ||
| options: dict[str, int | str | dict[str, int | str]] | None = None, | ||
| ) -> dict[str, int | str | dict[str, int | str]]: | ||
| """ | ||
| Return the kwargs for the authorization model read of a BatchCheck call: the | ||
| call's headers, and no retries. A failed read either fails the call (see | ||
| model_read_fails_call) or sends the checks unmerged right away. | ||
| """ | ||
| kwargs = {"_retry_params": RetryParams(max_retry=0)} | ||
|
Siddhant-K-code marked this conversation as resolved.
Resolved
|
||
| if options is not None and options.get("headers"): | ||
| kwargs["_headers"] = options["headers"] | ||
| return kwargs | ||
|
|
||
|
|
||
| def log_model_read_failure( | ||
| error: Exception, store_id: str, authorization_model_id: str | ||
| ) -> None: | ||
| """ | ||
| Warn that the checks go out unmerged, unless the error fails the call. | ||
| """ | ||
| if model_read_fails_call(error): | ||
| return | ||
| logger.warning( | ||
| "merge_equivalent_checks: couldn't read authorization model %s in store %s (status: %s). " | ||
|
Siddhant-K-code marked this conversation as resolved.
Resolved
|
||
| "Sending checks unmerged.", | ||
| authorization_model_id, | ||
| store_id, | ||
| getattr(error, "status", None), | ||
| ) | ||
|
|
||
|
|
||
| def checks_to_send( | ||
| api_client, | ||
| store_id: str, | ||
| authorization_model_id: str, | ||
| lookup: ModelLookup, | ||
| checks: list[ClientBatchCheckItem], | ||
| ) -> list[EquivalentCheckGroup]: | ||
| """ | ||
| Group the checks using the model from the cache, or send them unmerged if the model | ||
| couldn't be read or grouping failed. Raise if the model read error fails the call. | ||
| """ | ||
| metrics = _MergeMetrics(api_client, store_id, authorization_model_id) | ||
| metrics.model_cache_lookup(lookup.hit) | ||
|
|
||
| if lookup.aliases is None: | ||
| if model_read_fails_call(lookup.error): | ||
|
Siddhant-K-code marked this conversation as resolved.
Resolved
|
||
| # BatchCheck would fail too without a token or if the model is missing, and | ||
| # falling back on a 401 or 403 would hide wrong credentials. Fail before | ||
| # sending anything. | ||
| if isinstance(lookup.error, TokenRequestFailed): | ||
| raise lookup.error.error | ||
| raise lookup.error | ||
| groups = unmerged(checks) | ||
| metrics.fallback(getattr(lookup.error, "status", None)) | ||
| else: | ||
| try: | ||
| groups = resolve(lookup.aliases, checks) | ||
| except Exception: | ||
| logger.warning( | ||
| "merge_equivalent_checks: couldn't group the checks. Sending checks unmerged.", | ||
| exc_info=True, | ||
| ) | ||
| groups = unmerged(checks) | ||
| metrics.fallback(None) | ||
|
|
||
| # Every chunk is sent even if another one fails, so count them up front. | ||
| metrics.checks(requested=len(checks), sent=len(groups)) | ||
| return groups | ||
|
|
||
|
|
||
| class _MergeMetrics: | ||
| def __init__(self, api_client, store_id: str, authorization_model_id: str) -> None: | ||
| self._metrics = api_client._telemetry.metrics | ||
| self._configuration = api_client.configuration.telemetry | ||
| self._attributes = { | ||
| TelemetryAttributes.fga_client_request_store_id: store_id, | ||
| TelemetryAttributes.fga_client_request_model_id: authorization_model_id, | ||
| } | ||
|
|
||
| def model_cache_lookup(self, hit: bool) -> None: | ||
| self._metrics.modelCacheLookup( | ||
| attributes={ | ||
| **self._attributes, | ||
| TelemetryAttributes.fga_client_model_cache_result: ( | ||
| "hit" if hit else "miss" | ||
| ), | ||
| }, | ||
| configuration=self._configuration, | ||
| ) | ||
|
|
||
| def fallback(self, status: int | None) -> None: | ||
| self._metrics.batchCheckMergeFallback( | ||
| attributes={ | ||
| **self._attributes, | ||
| TelemetryAttributes.http_response_status_code: status, | ||
| }, | ||
| configuration=self._configuration, | ||
| ) | ||
|
|
||
| def checks(self, requested: int, sent: int) -> None: | ||
| self._metrics.batchCheckChecksRequested( | ||
| requested, attributes=self._attributes, configuration=self._configuration | ||
| ) | ||
| self._metrics.batchCheckChecksSent( | ||
| sent, attributes=self._attributes, configuration=self._configuration | ||
| ) | ||
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.