Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Fix MAU reaping where reserved users are specified.#6168

Merged
neilisfragile merged 7 commits intodevelopfrom
neilj/fix_double_counting_mau_reaping
Oct 11, 2019
Merged

Fix MAU reaping where reserved users are specified.#6168
neilisfragile merged 7 commits intodevelopfrom
neilj/fix_double_counting_mau_reaping

Conversation

@neilisfragile
Copy link
Copy Markdown
Contributor

The bug I am fixing is quite hard to explain, but in short, in some cases the reaping background task would delete too many users from the monthly_active_user table if reserved users had been specified.

@neilisfragile neilisfragile changed the title Fix mua reaping where reserved are specified. Fix mau reaping where reserved are specified. Oct 4, 2019
@neilisfragile neilisfragile requested a review from a team October 4, 2019 11:30
for i in range(initial_users):
user = "@user%d:server" % i
email = "user%[email protected]" % i
self.store.upsert_monthly_active_user(user)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
self.store.upsert_monthly_active_user(user)
self.get_success(self.store.upsert_monthly_active_user(user))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

etc for other store functions

Comment thread synapse/storage/monthly_active_users.py Outdated
Comment thread synapse/storage/monthly_active_users.py Outdated
query_args = []
query_args.extend(self.reserved_users)
query_args.append(safe_guard)
query_args.extend(self.reserved_users)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You can write this as query_args = [*self.reserved_users, safe_guard, *self.reserved_users]

Comment thread synapse/storage/monthly_active_users.py Outdated

# Update reserved_users to ensure it stays in sync, this is important
# for reaping.
self.reserved_users = users
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This feels wrong, as there's nothing to say that get_registered_reserved_users_count is going to get called to make this accurate. Can't we query the database to get the reserved_users when we need it? (And potentially cache it?)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, opted not to cache because method is called every hour and ought to be light.

Comment thread synapse/storage/monthly_active_users.py Outdated
else:
safe_guard = max_mau_value - len(self.reserved_users)
# Must be greater than zero for postgres
safe_guard = safe_guard if safe_guard > 0 else 0
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this not just safe_guard = max(max_mau_value - len(self.reserved_users), 0). Also can we rename it to something like num_users_to_remove?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

have renamed, but the sense is the opposite, because it is number of non reserved users to save.

@richvdh richvdh changed the title Fix mau reaping where reserved are specified. Fix MAU reaping where reserved users are specified. Oct 8, 2019
Copy link
Copy Markdown
Member

@erikjohnston erikjohnston left a comment

Choose a reason for hiding this comment

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

Just a couple of formatting nits, otherwise good to go

Comment thread synapse/storage/monthly_active_users.py Outdated
",".join(questionmarks)
)
query_args.extend(reserved_users)
sql = base_sql + """ AND user_id NOT IN ({})""".format(question_marks)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
sql = base_sql + """ AND user_id NOT IN ({})""".format(question_marks)
sql = base_sql + " AND user_id NOT IN ({})".format(question_marks)

Comment thread synapse/storage/monthly_active_users.py Outdated
ORDER BY timestamp DESC
LIMIT ?
)
AND user_id NOT IN ({})""".format(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
AND user_id NOT IN ({})""".format(
AND user_id NOT IN ({})
""".format(

Comment thread synapse/storage/monthly_active_users.py Outdated
if user_id:
count = count + 1
return count
users = users + (user_id,)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It'd probably be easier to return a list rather than using tuples, really.

@neilisfragile neilisfragile merged commit a0d0ba7 into develop Oct 11, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants