|
35 | 35 | from sentry.search.events.types import ParamsType, QueryBuilderConfig
|
36 | 36 | from sentry.snuba.dataset import Dataset
|
37 | 37 | from sentry.snuba.metrics.extraction import (
|
38 |
| - WIDGET_QUERY_CACHE_MAX_CHUNKS, |
39 | 38 | MetricSpec,
|
40 | 39 | MetricSpecType,
|
41 | 40 | OnDemandMetricSpec,
|
@@ -205,39 +204,20 @@ def _get_alert_metric_specs(
|
205 | 204 | return specs
|
206 | 205 |
|
207 | 206 |
|
208 |
| -def _bulk_cache_query_key(project: Project, chunk: int) -> str: |
209 |
| - return f"on-demand.bulk-query-cache.{chunk}.{project.organization.id}" |
| 207 | +def _bulk_cache_query_key(project: Project) -> str: |
| 208 | + return f"on-demand.bulk-query-cache.{project.organization.id}" |
210 | 209 |
|
211 | 210 |
|
212 |
| -def _get_bulk_cached_query(project: Project) -> tuple[dict[int, dict[str, bool]], list[int]]: |
213 |
| - cache_result = {} |
214 |
| - cold_cache_chunks = [] |
215 |
| - for i in range(WIDGET_QUERY_CACHE_MAX_CHUNKS): |
216 |
| - query_bulk_cache_key = _bulk_cache_query_key(project, i) |
217 |
| - chunk_result = cache.get(query_bulk_cache_key, None) |
218 |
| - if chunk_result is None: |
219 |
| - cold_cache_chunks.append(i) |
220 |
| - sentry_sdk.set_tag(f"on_demand_metrics.query_cache.{i}", chunk_result is None) |
221 |
| - cache_result[i] = chunk_result or {} |
222 |
| - sentry_sdk.set_extra("cold_cache_chunks", cold_cache_chunks) |
223 |
| - metrics.incr("on_demand_metrics.query_cache_cold_keys", amount=len(cold_cache_chunks)) |
224 |
| - return cache_result, cold_cache_chunks |
| 211 | +def _get_bulk_cached_query(project: Project) -> dict[str, Any]: |
| 212 | + query_bulk_cache_key = _bulk_cache_query_key(project) |
| 213 | + cache_result = cache.get(query_bulk_cache_key, None) |
| 214 | + sentry_sdk.set_tag("on_demand_metrics.query_cache", cache_result is None) |
| 215 | + return cache_result |
225 | 216 |
|
226 | 217 |
|
227 |
| -def _set_bulk_cached_query_chunk( |
228 |
| - project: Project, chunk_cache: dict[str, bool], chunk: int |
229 |
| -) -> None: |
230 |
| - query_bulk_cache_key = _bulk_cache_query_key(project, chunk) |
231 |
| - cache.set( |
232 |
| - query_bulk_cache_key, chunk_cache, timeout=900 + (137 * chunk) |
233 |
| - ) # Add prime number jitter per cache. All cache turns over between 15-25 mins |
234 |
| - |
235 |
| - |
236 |
| -def _set_bulk_cached_query( |
237 |
| - project: Project, query_cache: dict[int, dict[str, bool]], cold_cache_chunks: list[int] |
238 |
| -) -> None: |
239 |
| - for i in cold_cache_chunks: |
240 |
| - _set_bulk_cached_query_chunk(project, query_cache[i], i) |
| 218 | +def _set_bulk_cached_query(project: Project, query_cache: dict[str, Any]) -> None: |
| 219 | + query_bulk_cache_key = _bulk_cache_query_key(project) |
| 220 | + cache.set(query_bulk_cache_key, query_cache, timeout=5400) |
241 | 221 |
|
242 | 222 |
|
243 | 223 | @metrics.wraps("on_demand_metrics._get_widget_metric_specs")
|
@@ -267,7 +247,9 @@ def _get_widget_metric_specs(
|
267 | 247 | "on_demand_metrics.widgets_to_process", amount=len(widget_queries), sample_rate=1.0
|
268 | 248 | )
|
269 | 249 |
|
270 |
| - organization_bulk_query_cache, cold_bulk_cache_chunks = _get_bulk_cached_query(project) |
| 250 | + organization_bulk_query_cache = _get_bulk_cached_query(project) |
| 251 | + save_organization_bulk_cache = not bool(organization_bulk_query_cache) |
| 252 | + organization_bulk_query_cache = {} |
271 | 253 |
|
272 | 254 | ignored_widget_ids: dict[int, bool] = {}
|
273 | 255 | specs_for_widget: dict[int, list[HashedMetricSpec]] = defaultdict(list)
|
@@ -327,7 +309,8 @@ def _get_widget_metric_specs(
|
327 | 309 | _update_state_with_spec_limit(trimmed_specs, widget_query_for_spec_hash)
|
328 | 310 | metrics.incr("on_demand_metrics.widget_query_specs", amount=len(specs))
|
329 | 311 | if in_random_rollout("on_demand_metrics.cache_should_use_on_demand"):
|
330 |
| - _set_bulk_cached_query(project, organization_bulk_query_cache, cold_bulk_cache_chunks) |
| 312 | + if save_organization_bulk_cache: |
| 313 | + _set_bulk_cached_query(project, organization_bulk_query_cache) |
331 | 314 | return specs
|
332 | 315 |
|
333 | 316 |
|
@@ -456,7 +439,7 @@ def convert_widget_query_to_metric(
|
456 | 439 | project: Project,
|
457 | 440 | widget_query: DashboardWidgetQuery,
|
458 | 441 | prefilling: bool,
|
459 |
| - organization_bulk_query_cache: dict[int, dict[str, bool]] | None = None, |
| 442 | + organization_bulk_query_cache: dict[str, Any] | None = None, |
460 | 443 | ) -> list[HashedMetricSpec]:
|
461 | 444 | """
|
462 | 445 | Converts a passed metrics widget query to one or more MetricSpecs.
|
@@ -484,7 +467,7 @@ def _generate_metric_specs(
|
484 | 467 | project: Project,
|
485 | 468 | prefilling: bool,
|
486 | 469 | groupbys: Sequence[str] | None = None,
|
487 |
| - organization_bulk_query_cache: dict[int, dict[str, bool]] | None = None, |
| 470 | + organization_bulk_query_cache: dict[str, Any] | None = None, |
488 | 471 | ) -> list[HashedMetricSpec]:
|
489 | 472 | metrics_specs = []
|
490 | 473 | metrics.incr("on_demand_metrics.before_widget_spec_generation")
|
@@ -756,7 +739,7 @@ def _convert_aggregate_and_query_to_metrics(
|
756 | 739 | prefilling: bool,
|
757 | 740 | spec_type: MetricSpecType = MetricSpecType.SIMPLE_QUERY,
|
758 | 741 | groupbys: Sequence[str] | None = None,
|
759 |
| - organization_bulk_query_cache: dict[int, dict[str, bool]] | None = None, |
| 742 | + organization_bulk_query_cache: dict[str, Any] | None = None, |
760 | 743 | ) -> Sequence[HashedMetricSpec] | None:
|
761 | 744 | """
|
762 | 745 | Converts an aggregate and a query to a metric spec with its hash value.
|
|
0 commit comments