Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process Repeaters, Part 1 #35033

Open
wants to merge 38 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
50c848a
Add `PROCESS_REPEATERS` toggle
kaapstorm Aug 23, 2024
0db6a6c
`process_repeaters()` task
kaapstorm Aug 23, 2024
e36296c
`get_repeater_lock()`
kaapstorm Aug 23, 2024
aeb10ba
`iter_ready_repeater_ids_once()`
kaapstorm Aug 23, 2024
01e4bc7
Skip rate-limited repeaters
kaapstorm Aug 23, 2024
db2fec2
`process_repeater()` task
kaapstorm Aug 23, 2024
85b952e
Add tests
kaapstorm Aug 4, 2024
c28c11b
`Repeater.max_workers` field
kaapstorm Aug 24, 2024
d8d9642
Index fields used by `RepeaterManager.all_ready()`
kaapstorm Aug 28, 2024
48c3d7c
Use quickcache. Prefilter enabled domains.
kaapstorm Aug 28, 2024
418ed3a
Check randomly-enabled domains
kaapstorm Aug 29, 2024
85bbfa3
Forward new records for synchronous case repeaters
kaapstorm Aug 29, 2024
d1119bb
Add explanatory docstrings and comments
kaapstorm Sep 9, 2024
03b26cf
get_redis_lock() ... acquire(): No TypeError ?!
kaapstorm Sep 9, 2024
de27ba0
Drop unnecessary `iter_domain_repeaters()`
kaapstorm Sep 10, 2024
4955ef4
Don't quickcache `domain_can_forward_now()`
kaapstorm Sep 24, 2024
59aae71
Migration to create indexes concurrently
kaapstorm Sep 24, 2024
f40e6f4
Merge branch 'master' into nh/iter_repeaters_1
orangejenny Oct 4, 2024
b70fc52
Add comment
kaapstorm Oct 19, 2024
7e65b3b
Don't squash BaseExceptions
kaapstorm Oct 19, 2024
4c41896
Drop timeout for `process_repeater_lock`.
kaapstorm Oct 19, 2024
30d4a6f
Add metric for monitoring health
kaapstorm Oct 19, 2024
fc0f174
Merge branch 'master' into nh/iter_repeaters_1
kaapstorm Oct 19, 2024
e32b465
Resolve migration conflict, fix index
kaapstorm Oct 19, 2024
e3bcd74
Fix metric
kaapstorm Oct 19, 2024
efc4dde
Change indexes
kaapstorm Oct 22, 2024
bd37a00
Add one more index. Use UNION ALL queries.
kaapstorm Oct 23, 2024
a448b9e
Don't report attempt too soon
kaapstorm Oct 26, 2024
4321fb7
Add metrics
kaapstorm Oct 26, 2024
74137f9
Improve backoff logic
kaapstorm Oct 26, 2024
968a922
Update comments
kaapstorm Oct 28, 2024
4fd14a0
Show "Next attempt at" in Forwarders page
kaapstorm Oct 28, 2024
07320b9
Merge branch 'master' into nh/iter_repeaters_1
kaapstorm Oct 28, 2024
b1eb171
Fixes migration
kaapstorm Oct 28, 2024
2463348
Merge remote-tracking branch 'origin/master' into nh/iter_repeaters_1
kaapstorm Oct 29, 2024
8a7f343
Add comment on other `True` return value
kaapstorm Nov 19, 2024
0f72ba9
Count repeater backoffs
kaapstorm Dec 2, 2024
7f18e52
Add documentation
kaapstorm Dec 2, 2024
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
60 changes: 36 additions & 24 deletions corehq/motech/repeaters/migrations/0016_add_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,49 +12,61 @@ class Migration(migrations.Migration):
migrations.SeparateDatabaseAndState(
state_operations=[
migrations.AlterField(
model_name="repeatrecord",
name="state",
field=models.PositiveSmallIntegerField(
choices=[
(1, "Pending"),
(2, "Failed"),
(4, "Succeeded"),
(8, "Cancelled"),
(16, "Empty"),
(32, "Invalid Payload"),
],
db_index=True,
default=1,
),
model_name="repeater",
name="is_deleted",
field=models.BooleanField(default=False),
),
migrations.AddIndex(
model_name="repeater",
index=models.Index(
condition=models.Q(("is_deleted", False), ("is_paused", False)),
fields=["next_attempt_at"],
name="next_attempt_at_partial_idx",
condition=models.Q(("is_deleted", False)),
fields=["id"],
name="is_deleted_partial_idx",
),
),
migrations.AddIndex(
model_name="repeatrecord",
index=models.Index(
condition=models.Q(("state__in", (1, 2))),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder what are the computational differences in Postgres for state IN (1, 2) vs state = 1 OR state = 2? I hope they are virtually the same, but it would be a shame if IN (...) does something complicated with a loop.

fields=["repeater_id"],
name="state_partial_idx",
),
),
],

database_operations=[
# Drop `Repeater.id_deleted` index
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Drop `Repeater.id_deleted` index
# Drop `Repeater.is_deleted` index

migrations.RunSQL(
sql="""
CREATE INDEX CONCURRENTLY "repeaters_repeatrecord_state_8055083b"
ON "repeaters_repeatrecord" ("state");
DROP INDEX CONCURRENTLY "repeaters_repeater_is_deleted_08441bf0";
""",
reverse_sql="""
DROP INDEX CONCURRENTLY "repeaters_repeatrecord_state_8055083b";
CREATE INDEX CONCURRENTLY "repeaters_repeater_is_deleted_08441bf0"
ON "repeaters_repeater" ("is_deleted");
"""
),

# Replace with a partial index on `id_` column
migrations.RunSQL(
sql="""
CREATE INDEX CONCURRENTLY "is_deleted_partial_idx"
ON "repeaters_repeater" ("id_")
WHERE NOT "is_deleted";
""",
reverse_sql="""
DROP INDEX CONCURRENTLY "is_deleted_partial_idx";
"""
),

# Add partial index for RepeatRecord.state on `repeater_id`
migrations.RunSQL(
sql="""
CREATE INDEX CONCURRENTLY "next_attempt_at_partial_idx"
ON "repeaters_repeater" ("next_attempt_at")
WHERE (NOT "is_deleted" AND NOT "is_paused");
CREATE INDEX CONCURRENTLY "state_partial_idx"
ON "repeaters_repeatrecord" ("repeater_id_")
WHERE "state" IN (1, 2);
""",
reverse_sql="""
DROP INDEX CONCURRENTLY "next_attempt_at_partial_idx";
DROP INDEX CONCURRENTLY "state_partial_idx";
"""
),
]
Expand Down
16 changes: 10 additions & 6 deletions corehq/motech/repeaters/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ class Repeater(RepeaterSuperProxy):
max_workers = models.IntegerField(default=0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of spinning this feature off in a separate PR (if we decided it's needed)? We have a lot of knobs to turn already, and I wonder if we'll need this one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We needed this one years ago. OpenMRS integrations involve Events and Observations, and it is really useful if we can send repeat records in the order in which their forms were submitted. More generally, setting max_workers to 1 allows us to ensure that repeat records are sent chronologically.

And setting it to [all of the workers] is an insurance policy for this PR to ensure that we can still handle the highest volume for repeaters that need it. I'd sleep easier if this is included.

options = JSONField(default=dict)
connection_settings_id = models.IntegerField(db_index=True)
is_deleted = models.BooleanField(default=False, db_index=True)
is_deleted = models.BooleanField(default=False)
last_modified = models.DateTimeField(auto_now=True)
date_created = models.DateTimeField(auto_now_add=True)

Expand All @@ -299,9 +299,9 @@ class Meta:
db_table = 'repeaters_repeater'
indexes = [
models.Index(
fields=['next_attempt_at'],
condition=models.Q(("is_deleted", False), ("is_paused", False)),
name='next_attempt_at_partial_idx',
name='is_deleted_partial_idx',
fields=['id'],
condition=models.Q(is_deleted=False),
),
]

Expand Down Expand Up @@ -1037,7 +1037,6 @@ class RepeatRecord(models.Model):
state = models.PositiveSmallIntegerField(
choices=State.choices,
default=State.Pending,
db_index=True,
)
registered_at = models.DateTimeField()
next_check = models.DateTimeField(null=True, default=None)
Expand All @@ -1053,7 +1052,12 @@ class Meta:
name="next_check_not_null",
fields=["next_check"],
condition=models.Q(next_check__isnull=False),
)
),
models.Index(
name="state_partial_idx",
fields=["repeater_id"],
condition=models.Q(state__in=(State.Pending, State.Fail)),
),
]
constraints = [
models.CheckConstraint(
Expand Down
Loading