Skip to content

Commit 0f318b8

Browse files
Adds watermarking bounds testing, adds tests for table name filtering
1 parent bf5b4cb commit 0f318b8

File tree

2 files changed

+62
-30
lines changed

2 files changed

+62
-30
lines changed

src/sentry/tasks/deletion/hybrid_cloud.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,6 @@ def get_ids_for_tombstone_cascade_cross_db(
369369

370370
field_name = f"{field.name}__in"
371371
query_kwargs = {field_name: ids_to_check}
372-
rows_to_delete = list(model.objects.filter(**query_kwargs).values_list("id", flat=True))
372+
affected_rows = list(model.objects.filter(**query_kwargs).values_list("id", flat=True))
373373

374-
return rows_to_delete, oldest_seen
374+
return affected_rows, oldest_seen

tests/sentry/tasks/deletion/test_hybrid_cloud.py

Lines changed: 60 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ def setup_cross_db_deletion_data():
317317

318318
@django_db_all
319319
@region_silo_test
320-
def test_cross_database_same_silo_deletion(task_runner):
320+
def test_get_ids_for_tombstone_cascade_cross_db(task_runner):
321321
data = setup_cross_db_deletion_data()
322322

323323
unaffected_data = []
@@ -335,12 +335,13 @@ def test_cross_database_same_silo_deletion(task_runner):
335335
)
336336

337337
highest_tombstone_id = RegionTombstone.objects.aggregate(Max("id"))
338+
monitor_owner_field = Monitor._meta.get_field("owner_user_id")
338339

339340
with override_options({"hybrid_cloud.allow_cross_db_tombstones": True}):
340341
ids, oldest_obj = get_ids_for_tombstone_cascade_cross_db(
341342
tombstone_cls=RegionTombstone,
342343
model=Monitor,
343-
field=Monitor.owner_user_id.field,
344+
field=monitor_owner_field,
344345
watermark_batch=WatermarkBatch(
345346
low=0,
346347
up=highest_tombstone_id["id__max"] + 1,
@@ -351,33 +352,64 @@ def test_cross_database_same_silo_deletion(task_runner):
351352
assert ids == [monitor.id]
352353
assert oldest_obj == tombstone.created_at
353354

354-
# Validate lower watermark bound
355-
ids, oldest_obj = get_ids_for_tombstone_cascade_cross_db(
356-
tombstone_cls=RegionTombstone,
357-
model=Monitor,
358-
field=Monitor.owner_user_id.field,
359-
watermark_batch=WatermarkBatch(
360-
low=0,
361-
up=highest_tombstone_id["id__max"] - 1,
362-
has_more=False,
363-
transaction_id="foobar",
364-
),
365-
)
366-
assert ids == []
367355

368-
# Validate upper watermark bound
369-
ids, oldest_obj = get_ids_for_tombstone_cascade_cross_db(
370-
tombstone_cls=RegionTombstone,
371-
model=Monitor,
372-
field=Monitor.owner_user_id.field,
373-
watermark_batch=WatermarkBatch(
374-
low=highest_tombstone_id["id__max"] + 1,
375-
up=highest_tombstone_id["id__max"] + 5,
376-
has_more=False,
377-
transaction_id="foobar",
378-
),
356+
@django_db_all
357+
@region_silo_test
358+
def test_get_ids_for_tombstone_cascade_cross_db_watermark_bounds(task_runner):
359+
cascade_data = []
360+
for i in range(4):
361+
cascade_data.append(setup_cross_db_deletion_data())
362+
363+
unaffected_data = []
364+
for i in range(3):
365+
unaffected_data.append(setup_cross_db_deletion_data())
366+
367+
in_order_tombstones = []
368+
for data in cascade_data:
369+
user = data["user"]
370+
user_id = user.id
371+
with assume_test_silo_mode_of(User), outbox_runner():
372+
user.delete()
373+
374+
in_order_tombstones.append(
375+
RegionTombstone.objects.get(object_identifier=user_id, table_name=User._meta.db_table)
379376
)
380-
assert ids == []
377+
378+
bounds_with_expected_results = [
379+
(
380+
{"low": 0, "up": in_order_tombstones[1].id},
381+
[cascade_data[0]["monitor"].id, cascade_data[1]["monitor"].id],
382+
),
383+
(
384+
{"low": in_order_tombstones[1].id, "up": in_order_tombstones[2].id},
385+
[cascade_data[2]["monitor"].id],
386+
),
387+
(
388+
{"low": 0, "up": in_order_tombstones[0].id - 1},
389+
[],
390+
),
391+
(
392+
{"low": in_order_tombstones[2].id + 1, "up": in_order_tombstones[2].id + 5},
393+
[],
394+
),
395+
]
396+
397+
for bounds, bounds_with_expected_results in bounds_with_expected_results:
398+
monitor_owner_field = Monitor._meta.get_field("owner_user_id")
399+
400+
with override_options({"hybrid_cloud.allow_cross_db_tombstones": True}):
401+
ids, oldest_obj = get_ids_for_tombstone_cascade_cross_db(
402+
tombstone_cls=RegionTombstone,
403+
model=Monitor,
404+
field=monitor_owner_field,
405+
watermark_batch=WatermarkBatch(
406+
low=bounds["low"],
407+
up=bounds["up"],
408+
has_more=False,
409+
transaction_id="foobar",
410+
),
411+
)
412+
assert ids == bounds_with_expected_results
381413

382414

383415
@django_db_all
@@ -414,7 +446,7 @@ def test_get_ids_for_tombstone_cascade_cross_db_with_multiple_tombstone_types():
414446
ids, oldest_obj = get_ids_for_tombstone_cascade_cross_db(
415447
tombstone_cls=RegionTombstone,
416448
model=Monitor,
417-
field=Monitor.owner_user_id.field,
449+
field=Monitor._meta.get_field("owner_user_id"),
418450
watermark_batch=WatermarkBatch(
419451
low=0,
420452
up=highest_tombstone_id["id__max"] + 1,

0 commit comments

Comments
 (0)