Skip to content

Commit

Permalink
code (#6024)
Browse files Browse the repository at this point in the history
Co-authored-by: Rachel Chen <[email protected]>
  • Loading branch information
xurui-c and Rachel Chen authored Jun 12, 2024
1 parent dfb5d93 commit eb00fd5
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
32 changes: 32 additions & 0 deletions snuba/query/allocation_policies/bytes_scanned_rejecting_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
PETABYTE = 10**12
DEFAULT_BYTES_SCANNED_LIMIT = int(1.28 * PETABYTE)
DEFAULT_TIMEOUT_PENALIZATION = DEFAULT_BYTES_SCANNED_LIMIT // 40
DEFAULT_BYTES_THROTTLE_DIVIDER = 1
DEFAULT_THREADS_THROTTLE_DIVIDER = 1


class BytesScannedRejectingPolicy(AllocationPolicy):
Expand Down Expand Up @@ -91,6 +93,18 @@ def _additional_config_definitions(self) -> list[AllocationPolicyConfig]:
int,
DEFAULT_TIMEOUT_PENALIZATION,
),
AllocationPolicyConfig(
"bytes_throttle_divider",
"Divide the scan limit by this number gives the throttling threshold",
int,
DEFAULT_BYTES_THROTTLE_DIVIDER,
),
AllocationPolicyConfig(
"threads_throttle_divider",
"max threads divided by this number is the number of threads we use to execute queries for a throttled (project_id|organization_id, referrer)",
int,
DEFAULT_BYTES_THROTTLE_DIVIDER,
),
]

def _are_tenant_ids_valid(
Expand Down Expand Up @@ -206,6 +220,24 @@ def _get_quota_allowance(
},
)
return QuotaAllowance(False, self.max_threads, explanation)

throttle_threshold = max(
1, scan_limit // self.get_config_value("bytes_throttle_divider")
)
if granted_quota.granted < throttle_threshold:
self.metrics.increment(
"bytes_scanned_queries_throttled",
tags={"referrer": str(tenant_ids.get("referrer", "no_referrer"))},
)
return QuotaAllowance(
True,
max(
1,
self.max_threads
// self.get_config_value("threads_throttle_divider"),
),
{"reason": "within_limit but throttled"},
)
return QuotaAllowance(True, self.max_threads, {"reason": "within_limit"})

def _get_bytes_scanned_in_query(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,53 @@ def test_consume_quota(
assert allowance.can_run


@pytest.mark.redis_db
def test_throttles(
policy: BytesScannedRejectingPolicy,
) -> None:
_configure_policy(policy)
policy.set_config_value("bytes_throttle_divider", 2)
policy.set_config_value("threads_throttle_divider", 2)
tenant_ids: dict[str, int | str] = {
"organization_id": 123,
"project_id": 12345,
"referrer": "some_referrer",
}
allowance = policy.get_quota_allowance(tenant_ids, QUERY_ID)
assert allowance.max_threads == MAX_THREAD_NUMBER
policy.update_quota_balance(
tenant_ids,
QUERY_ID,
QueryResultOrError(
query_result=QueryResult(
result={"profile": {"progress_bytes": 1}},
extra={"stats": {}, "sql": "", "experiments": {}},
),
error=None,
),
)

allowance = policy.get_quota_allowance(tenant_ids, QUERY_ID)
assert allowance.max_threads == MAX_THREAD_NUMBER
policy.update_quota_balance(
tenant_ids,
QUERY_ID,
QueryResultOrError(
query_result=QueryResult(
result={
"profile": {"progress_bytes": PROJECT_REFERRER_SCAN_LIMIT // 2}
},
extra={"stats": {}, "sql": "", "experiments": {}},
),
error=None,
),
)

allowance = policy.get_quota_allowance(tenant_ids, QUERY_ID)
assert allowance.max_threads == MAX_THREAD_NUMBER // 2
assert allowance.explanation["reason"] == "within_limit but throttled"


@pytest.mark.redis_db
def test_cross_org_query(policy: BytesScannedRejectingPolicy) -> None:
_configure_policy(policy)
Expand Down
2 changes: 1 addition & 1 deletion tests/web/test_db_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def test_db_query_success() -> None:
"can_run": True,
"max_threads": 10,
"explanation": {
"reason": "within_limit",
"reason": "within_limit but throttled",
"storage_key": "StorageKey.ERRORS_RO",
},
},
Expand Down

0 comments on commit eb00fd5

Please sign in to comment.