Add ScaleLimitsAnalyzer
using bluejay to function-runner
#351
+615
−2
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.
#gsd:37875
Addresses:
Description
Current scaling implementation: https://github.com/Shopify/shopify/pull/518219/files#diff-bef9ccfcca30dfc391c6ad9d0a3e0739bf8a2ab27e659473894e4e62d43bad08
We currently scale our function run limits based on the directive
scaleLimits
with an argumentrate
oncartLines
. We scale by the length of the field. We multiply the directiverate
found on the fields definition, by the length of that fields (seem by either array length or string length) to determine how much to scale limits our default limits by.This PR adds an analyzer that will determine the maximum scale factor from all fields with the scale limits directive on it. We chose to use this implementation because we want to ability to add directives for scale limits of different fields in the future. Currently we only scale on cart lines.
Notes on Visitor implementation:
ScaleLimitsAnalyzer
is used to dynamically adjust resource limits for function executions based on data provided in GraphQL queries. It does this by analyzing the query and associated input JSON, calculating a scaling factor based on the lengths of fields specified in the query that have an associated @scaleLimits directive.Avoiding Double Counting
A key feature of the ScaleLimitsAnalyzer is its ability to avoid double counting fields when calculating the scale factor. This is crucial for ensuring that the scaling adjustments are accurate and reflect the true resource needs of the function execution. Here’s how double counting is avoided:
Path Tracking: The analyzer uses a path_stack to keep track of the current path within the query during traversal. This helps in uniquely identifying each field's location within the query structure.
Unique Path Identification: Each field's path, combined with its index in case of arrays, is used to create a unique identifier (PathWithIndex). This ensures that each instance of a field, even if the field name is the same, is treated uniquely based on its context and position in the input data.
Rate Aggregation: When calculating the scale factor, the analyzer aggregates rates using these unique identifiers. If a field appears multiple times in different parts of the query or within nested structures, each occurrence is treated independently based on its unique path and context.
Maximal Increment Strategy: For fields that appear multiple times in the same context (e.g., duplicated fields in a query), the analyzer only considers the maximum scale increment calculated for these fields. This approach ensures that the scale factor reflects the maximum resource requirement for repeated fields without cumulatively adding the increments.
Tophat