-
-
Notifications
You must be signed in to change notification settings - Fork 4k
/
hybrid_cloud.py
306 lines (257 loc) · 10.5 KB
/
hybrid_cloud.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
"""
Executes the eventually consistent cascades dictated by HybridCloudForeignKey fields on application models.
One job schedules on a regular interval the execute of smaller, per HCFK column jobs that each do a small chunk of work,
and then possibly reschedule to keep processing as necessary to stay caught up.
Notably, this job is only responsible for cascading to models that are related to deletions that have occurred in the
opposing silo and are stored in Tombstone rows. Deletions that are not successfully synchronized via Outbox to a
Tombstone row will not, therefore, cascade to any related cross silo rows.
"""
import datetime
from dataclasses import dataclass
from hashlib import sha1
from typing import Any, List, Tuple, Type
from uuid import uuid4
import sentry_sdk
from celery import Task
from django.apps import apps
from django.db import connections, router
from django.db.models import Max
from django.db.models.manager import BaseManager
from django.utils import timezone
from sentry.db.models.fields.hybrid_cloud_foreign_key import HybridCloudForeignKey
from sentry.models.tombstone import TombstoneBase
from sentry.silo import SiloMode
from sentry.tasks.base import instrumented_task
from sentry.utils import json, metrics, redis
@dataclass
class WatermarkBatch:
low: int
up: int
has_more: bool
transaction_id: str
def get_watermark_key(prefix: str, field: HybridCloudForeignKey) -> str:
return f"{prefix}.{field.model._meta.db_table}.{field.name}"
def get_watermark(prefix: str, field: HybridCloudForeignKey) -> Tuple[int, str]:
with redis.clusters.get("default").get_local_client_for_key("deletions.watermark") as client:
key = get_watermark_key(prefix, field)
v = client.get(key)
if v is None:
result = (0, uuid4().hex)
client.set(key, json.dumps(result))
return result
lower, transaction_id = json.loads(v)
if not (isinstance(lower, int) and isinstance(transaction_id, str)):
raise TypeError("Expected watermarks data to be a tuple of (int, str)")
return lower, transaction_id
def set_watermark(
prefix: str, field: HybridCloudForeignKey, value: int, prev_transaction_id: str
) -> None:
with redis.clusters.get("default").get_local_client_for_key("deletions.watermark") as client:
client.set(
get_watermark_key(prefix, field),
json.dumps((value, sha1(prev_transaction_id.encode("utf8")).hexdigest())),
)
metrics.gauge(
"deletion.hybrid_cloud.low_bound",
value,
tags=dict(
field_name=f"{field.model._meta.db_table}.{field.name}",
watermark=prefix,
),
)
def _chunk_watermark_batch(
prefix: str, field: HybridCloudForeignKey, manager: BaseManager, *, batch_size: int
) -> WatermarkBatch:
lower, transaction_id = get_watermark(prefix, field)
upper = manager.aggregate(Max("id"))["id__max"] or 0
batch_upper = min(upper, lower + batch_size)
# cap to batch size so that query timeouts don't get us.
capped = upper
if upper >= batch_upper:
capped = batch_upper
return WatermarkBatch(
low=lower, up=capped, has_more=batch_upper < upper, transaction_id=transaction_id
)
@instrumented_task(
name="sentry.tasks.deletion.hybrid_cloud.schedule_hybrid_cloud_foreign_key_jobs_control",
queue="cleanup.control",
acks_late=True,
silo_mode=SiloMode.CONTROL,
)
def schedule_hybrid_cloud_foreign_key_jobs_control():
_schedule_hybrid_cloud_foreign_key(
SiloMode.CONTROL, process_hybrid_cloud_foreign_key_cascade_batch_control
)
@instrumented_task(
name="sentry.tasks.deletion.hybrid_cloud.schedule_hybrid_cloud_foreign_key_jobs",
queue="cleanup",
acks_late=True,
silo_mode=SiloMode.REGION,
)
def schedule_hybrid_cloud_foreign_key_jobs():
_schedule_hybrid_cloud_foreign_key(
SiloMode.REGION, process_hybrid_cloud_foreign_key_cascade_batch
)
def _schedule_hybrid_cloud_foreign_key(silo_mode: SiloMode, cascade_task: Task) -> None:
for app, app_models in apps.all_models.items():
for model in app_models.values():
if not hasattr(model._meta, "silo_limit"):
continue
# Only process models local this operational mode.
if silo_mode not in model._meta.silo_limit.modes:
continue
for field in model._meta.fields:
if not isinstance(field, HybridCloudForeignKey):
continue
cascade_task.delay(
app_name=app,
model_name=model.__name__,
field_name=field.name,
silo_mode=silo_mode.name,
)
@instrumented_task(
name="sentry.tasks.deletion.process_hybrid_cloud_foreign_key_cascade_batch_control",
queue="cleanup.control",
acks_late=True,
silo_mode=SiloMode.CONTROL,
)
def process_hybrid_cloud_foreign_key_cascade_batch_control(
app_name: str, model_name: str, field_name: str, **kwargs: Any
) -> None:
_process_hybrid_cloud_foreign_key_cascade(
app_name=app_name,
model_name=model_name,
field_name=field_name,
process_task=process_hybrid_cloud_foreign_key_cascade_batch_control,
silo_mode=SiloMode.CONTROL,
)
@instrumented_task(
name="sentry.tasks.deletion.process_hybrid_cloud_foreign_key_cascade_batch",
queue="cleanup",
acks_late=True,
silo_mode=SiloMode.REGION,
)
def process_hybrid_cloud_foreign_key_cascade_batch(
app_name: str, model_name: str, field_name: str, **kwargs: Any
) -> None:
_process_hybrid_cloud_foreign_key_cascade(
app_name=app_name,
model_name=model_name,
field_name=field_name,
process_task=process_hybrid_cloud_foreign_key_cascade_batch,
silo_mode=SiloMode.REGION,
)
def _process_hybrid_cloud_foreign_key_cascade(
app_name: str, model_name: str, field_name: str, process_task: Task, silo_mode: SiloMode
) -> None:
"""
Called by the silo bound tasks above.
"""
try:
model = apps.get_model(app_label=app_name, model_name=model_name)
try:
field = model._meta.get_field(field_name)
if not isinstance(field, HybridCloudForeignKey):
raise Exception(f"The {field_name} field is not a HybridCloudForeignKey")
except Exception as err:
sentry_sdk.capture_exception(err)
raise LookupError(f"Could not find field {field_name} on model {app_name}.{model_name}")
tombstone_cls = TombstoneBase.class_for_silo_mode(silo_mode)
assert tombstone_cls, "A tombstone class is required"
# We rely on the return value of _process_tombstone_reconciliation
# to short circuit the second half of this `or` so that the terminal batch
# also updates the tombstone watermark.
if _process_tombstone_reconciliation(
field, model, tombstone_cls, True
) or _process_tombstone_reconciliation(field, model, tombstone_cls, False):
process_task.apply_async(
kwargs=dict(
app_name=app_name,
model_name=model_name,
field_name=field_name,
silo_mode=silo_mode.name,
),
countdown=15,
)
except Exception as err:
sentry_sdk.set_context(
"deletion.hybrid_cloud",
dict(
app_name=app_name,
model_name=model_name,
field_name=field_name,
silo_mode=silo_mode,
),
)
sentry_sdk.capture_exception(err)
raise err
# Convenience wrapper for mocking in tests
def get_batch_size() -> int:
return 500
def _process_tombstone_reconciliation(
field: HybridCloudForeignKey,
model: Any,
tombstone_cls: Type[TombstoneBase],
row_after_tombstone: bool,
) -> bool:
from sentry import deletions
prefix = "tombstone"
watermark_manager: BaseManager = tombstone_cls.objects
watermark_target = "t"
if row_after_tombstone:
prefix = "row"
watermark_manager = field.model.objects
watermark_target = "r"
watermark_batch = _chunk_watermark_batch(
prefix, field, watermark_manager, batch_size=get_batch_size()
)
has_more = watermark_batch.has_more
to_delete_ids: List[int] = []
if watermark_batch.low < watermark_batch.up:
oldest_seen: datetime.datetime = timezone.now()
with connections[router.db_for_read(model)].cursor() as conn:
conn.execute(
f"""
SELECT r.id, t.created_at
FROM {model._meta.db_table} r
JOIN {tombstone_cls._meta.db_table} t
ON t.table_name = %(table_name)s AND t.object_identifier = r.{field.name}
WHERE {watermark_target}.id > %(low)s AND {watermark_target}.id <= %(up)s
""",
{
"table_name": field.foreign_table_name,
"low": watermark_batch.low,
"up": watermark_batch.up,
},
)
for (row_id, tomb_created) in conn.fetchall():
to_delete_ids.append(row_id)
oldest_seen = min(oldest_seen, tomb_created)
if field.on_delete == "CASCADE":
task = deletions.get(
model=model,
query={"id__in": to_delete_ids},
transaction_id=watermark_batch.transaction_id,
)
if task.chunk():
has_more = True # The current batch is not complete, rerun this task again
else:
set_watermark(prefix, field, watermark_batch.up, watermark_batch.transaction_id)
elif field.on_delete == "SET_NULL":
model.objects.filter(id__in=to_delete_ids).update(**{field.name: None})
set_watermark(prefix, field, watermark_batch.up, watermark_batch.transaction_id)
elif field.on_delete == "DO_NOTHING":
set_watermark(prefix, field, watermark_batch.up, watermark_batch.transaction_id)
else:
raise ValueError(
f"{field.model.__name__}.{field.name} has unexpected on_delete={field.on_delete}, could not process delete!"
)
metrics.timing(
"deletion.hybrid_cloud.processing_lag",
datetime.datetime.now().timestamp() - oldest_seen.timestamp(),
tags=dict(
field_name=f"{model._meta.db_table}.{field.name}",
watermark=prefix,
),
)
return has_more