Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/sentry/search/snuba/executors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1659,8 +1659,15 @@ def query(
# Check if any search filters are in POSTGRES_ONLY_SEARCH_FIELDS
search_filters = search_filters or ()
group_ids_to_pass_to_snuba = None
too_many_candidates = False
if any(sf.key.name in POSTGRES_ONLY_SEARCH_FIELDS for sf in search_filters):
group_ids_to_pass_to_snuba = list(group_queryset.values_list("id", flat=True))
max_candidates = options.get("snuba.search.max-pre-snuba-candidates")
group_ids_to_pass_to_snuba = list(
group_queryset.using_replica().values_list("id", flat=True)[: max_candidates + 1]
)
if too_many_candidates := (len(group_ids_to_pass_to_snuba) > max_candidates):
metrics.incr("snuba.search.too_many_candidates", skip_internal=False)
group_ids_to_pass_to_snuba = None

# remove the search filters that are only for postgres
search_filters = [
Expand Down Expand Up @@ -1692,6 +1699,7 @@ def query(
Condition(Column("timestamp", joined_entity), Op.GTE, start),
Condition(Column("timestamp", joined_entity), Op.LT, end),
]

having = []
# if we need to prefetch from postgres, we add filter by the group ids
if group_ids_to_pass_to_snuba is not None:
Expand Down Expand Up @@ -1838,6 +1846,15 @@ def query(
count += bulk_result[k]["data"][0]["count"]
k += 1

if too_many_candidates:
# If we had too many candidates to reasonably pass down to snuba,
# we need to apply the Postgres filter as a post-filtering step.
filtered_group_ids = group_queryset.filter(
id__in=[group["g.group_id"] for group in data]
).values_list("id", flat=True)

data = [group for group in data if group["g.group_id"] in filtered_group_ids]

paginator_results = SequencePaginator(
[(row[self.sort_strategies[sort_by]], row["g.group_id"]) for row in data],
reverse=True,
Expand Down
49 changes: 49 additions & 0 deletions tests/sentry/issues/endpoints/test_organization_group_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -3070,6 +3070,55 @@ def test_snuba_query_first_release_with_environments(self, mock_query: MagicMock
assert len(response.data) == len(expected_groups)
assert {int(r["id"]) for r in response.data} == set(expected_groups)

@patch(
"sentry.search.snuba.executors.GroupAttributesPostgresSnubaQueryExecutor.query",
side_effect=GroupAttributesPostgresSnubaQueryExecutor.query,
autospec=True,
)
@override_options({"issues.group_attributes.send_kafka": True})
def test_snuba_query_unlinked(self, mock_query: MagicMock) -> None:
self.project = self.create_project(organization=self.organization)
event1 = self.store_event(
data={"fingerprint": ["group-1"], "message": "MyMessage"},
project_id=self.project.id,
)
event2 = self.store_event(
data={"fingerprint": ["group-2"], "message": "AnotherMessage"},
project_id=self.project.id,
)
PlatformExternalIssue.objects.create(project_id=self.project.id, group_id=event1.group.id)
self.external_issue = ExternalIssue.objects.create(
organization_id=self.organization.id, integration_id=self.integration.id, key="123"
)
GroupLink.objects.create(
project_id=self.project.id,
group_id=event1.group.id,
linked_type=GroupLink.LinkedType.issue,
linked_id=self.external_issue.id,
)

self.login_as(user=self.user)
# give time for consumers to run and propogate changes to clickhouse
sleep(1)

for value in [0, 5]:
with override_options({"snuba.search.max-pre-snuba-candidates": value}):
response = self.get_success_response(
sort="new",
useGroupSnubaDataset=1,
query="is:linked",
)
assert len(response.data) == 1
assert int(response.data[0]["id"]) == event1.group.id

response = self.get_success_response(
sort="new",
useGroupSnubaDataset=1,
query="is:unlinked",
)
assert len(response.data) == 1
assert int(response.data[0]["id"]) == event2.group.id

@patch(
"sentry.search.snuba.executors.GroupAttributesPostgresSnubaQueryExecutor.query",
side_effect=GroupAttributesPostgresSnubaQueryExecutor.query,
Expand Down