Skip to content

Bug 2085398 - Read flag changes from flag_activity and stop writing them to bugs_activity - #2796

Open
Xzzz wants to merge 56 commits into
mozilla:masterfrom
Xzzz:bug-2085398
Open

Xzzz wants to merge 56 commits into
mozilla:masterfrom
Xzzz:bug-2085398

Conversation

@Xzzz

@Xzzz Xzzz commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Stops writing flag changes to bugs_activity: new ones are only recorded in flag_activity, and everything that reads flag changes now reads both tables, bugs_activity for the changes up to a cutoff stored at checksetup and flag_activity for the later ones.

Part 2 of 1806896, whose part 1 (#2657) moved flag_state_activity to core as flag_activity and writes every flag change to both tables.

Changes

  • Bugzilla::Bug and Bugzilla::Attachment no longer log flagtypes.name to bugs_activity; the user merge in editusers.cgi writes its flag changes to flag_activity.
  • flag_activity.who, the user who made the change, becomes NOT NULL. #2808 (bug 2088705) added it as a nullable column and writes it on every change, so it must be deployed in an earlier release than this PR. On MySQL its foreign key is dropped for the change and added back at the end of checksetup.
  • checksetup gives each flag that has no flag_activity row (flags last changed before flag_activity logged every type) a row with its current state, dated at its last change, in batches of 10,000 flags. History and bugmail don't show it, but the flag's next change gets its previous state. It then stores the cutoff, the time it ran, in the new one-row table flag_activity_migration, which also keeps it from running again.
  • Bug history (GetBugActivity), bugmail (_get_diffs), the BMO user activity report and the UserProfile per-product counts read the flagtypes.name rows in bugs_activity up to the cutoff and the flag_activity rows after it (Bugzilla::FlagActivity->cutoff), so a change written to both tables, by a pod still on the older code during the deploy, is listed once. A request that changes a flag several times shows its net change, compared with the flag's last row before the request.
  • Search changedbefore/changedafter/changedfrom/changedto/changedby/everchanged on flags, and the ReleaseTracking report, match either table.
  • The BugModal activity stream finds a flag's last change by its flag_id in flag_activity.
  • The other readers of a user's activity also read flag_activity: the non-bot last change time, the UserProfile stats, the TagNewUsers first activity, owner idle time, and changed* searches on any field.
  • A flag type that has flag_activity rows can only be deactivated, not deleted, as its history would be deleted with it. The user deletion check in editusers.cgi counts flag_activity rows, and scripts/remove-non-public-data.pl keeps who.
  • New tests: t/flag-activity-history.t, -search.t, -who.t, -user-stats.t, -stream.t, -start-rows.t and t/flag-type-delete.t. They set the cutoff with the new set_flag_activity_cutoff in Bugzilla::Test::Util.

The old flagtypes.name rows stay in bugs_activity; nothing is deleted.

Deploy

  1. Run checksetup with the new code, and let it finish, before any pod serves the new code. The new code reads flag_activity_migration, which checksetup creates: until then, bug history, bugmail and flag searches fail with a missing-table error. Until checksetup stores the cutoff at the end, they also miss the older flag changes in bugs_activity. The flag backfill can take a while on production.
  2. Then roll out the pods. They load the new schema at startup, so no extra restart or memcached clear is needed. Pods still on the older code keep working while this happens: they write who and log to both tables, and the new code lists those changes once.

The order is based on what I checked in the code:

  • New pods before checksetup are the real risk. The new code reads the cutoff with raw SQL (FlagActivity.pm:98). If the table doesn't exist yet, that query fails on every bug history view, bugmail and flag search.
  • Today's stale-schema problem doesn't recur here. The PR adds no field that goes through _check_field. The only cached-schema effect is that who becomes NOT NULL, which only affects the required-fields check for create(), and FlagActivity already requires who.
  • Old pods are safe. They already write who (from Bug 2088705 - Record the acting user in flag_activity.who, as a nullable column #2808) and log to both tables.

Test plan

  • Set a needinfo, a review on an attachment and an approval flag, then clear one: bug history, the activity stream and the bugmail show each change with its previous state, and bugs_activity gets no new flagtypes.name row
  • Redirect a needinfo to someone else as another user: history shows that user
  • On a bug with flag changes from before the deploy: history shows them once, with their previous state
  • Search "Flags changed to needinfo?" and the user activity report find both older and new changes
  • Merge two users with pending requests in editusers.cgi: the reassigned requests appear in bug history
  • Delete a flag type that was used: only deactivation is offered

References

@Xzzz

Xzzz commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator Author

Please note that the test_sanity critic failure is the $$ on master fixed by #2795. I'll re-run CI once it's merged, it should go green, unless I missed stg ;)

@Xzzz
Xzzz requested a review from dklawren October 23, 2026 19:06
Comment thread Bugzilla/Bug.pm Resolved
Comment thread Bugzilla/Bug.pm Resolved
Comment thread Bugzilla/Bug.pm Resolved
Comment thread Bugzilla/Bug.pm Resolved
@Xzzz
Xzzz requested a review from dklawren October 27, 2026 17:05
Comment thread Bugzilla/Bug.pm Resolved
Comment thread Bugzilla/DB/Schema.pm Resolved
Comment thread editusers.cgi Resolved
Comment thread editusers.cgi Resolved
@Xzzz
Xzzz requested a review from dklawren October 29, 2026 21:27
Comment thread Bugzilla/DB/Schema.pm Resolved
Comment thread Bugzilla/Bug.pm Resolved
Comment thread Bugzilla/Search.pm Resolved
Comment thread Bugzilla/FlagActivity.pm Outdated
# bugs_activity, which has them all; flag_activity has only some of them,
# written to both tables, which must not be listed twice. Older rows also don't
# always know their who: it was filled in from the setter.
sub newer_than_bugs_activity_sql {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

per-bug MAX(bug_when) cutoff hides flag_activity rows whenever a later flagtypes.name row lands in bugs_activity, e.g. an old pod still writing both tables during the rolling deploy makes earlier flag_activity-only changes on that bug vanish from history, reports and changedby search, could we use a fixed cutoff stored at migration time instead

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes sure, would a row in the params table (written once by checksetup, undeclared param then it won't appear in admin UI) works for you, or would you prefer a dedicated one-row table (most explicit but it's a schema change for a single value)?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

a dedicated one-row table please. an undeclared param doesn't survive well: migrate_params in Bugzilla/Config.pm drops params with no definition from the hash on every checksetup run and then pushes that hash to memcached, so Bugzilla->params loses the value even though the db row is still there (saving editparams does the same). a table like flag_activity_migration (cutoff DATETIME NOT NULL) filled once with NOW() next to the start rows, then read once per request via request_cache, keeps it out of the admin ui and is easy to drop later. with a fixed cutoff, bugs_activity flag rows need bug_when <= cutoff and flag_activity rows need flag_when > cutoff, and the per-row subquery and bugs_activity_bug_field_when_idx can probably go

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Thanks, and you're right about migrate_params, I missed that. I'll do it as you describe:

  • flag_activity_migration (cutoff DATETIME NOT NULL), filled once with LOCALTIMESTAMP(0) at the end of the start rows migration, which also uses it as its run-once guard. Read once per request through request_cache.
  • flag_activity rows after the cutoff, and bugs_activity flag rows up to it in the readers that list or count changes: history, bugmail, the user activity report and the per-product counts. The ones that take a MAX or an EXISTS (non-bot last change, last user activity, touched, the search conditions, ReleaseTracking) only need the flag_activity side, as a change listed twice doesn't change their result.
  • The per-row subquery and bugs_activity_bug_field_when_idx go away.

I'll push it with the NOT NULL step once #2808 is merged.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done as described:
flag_activity_migration (cutoff) is filled with LOCALTIMESTAMP(0) at the end of the start rows migration, which uses it as its run-once guard, and Bugzilla::FlagActivity->cutoff reads it once per request. History, bugmail, the user activity report and the per-product counts read the bugs_activity flag rows up to the cutoff and the flag_activity rows after it. The other readers only use the flag_activity side. bugs_activity_bug_field_when_idx is gone.

The tests set the cutoff with a new set_flag_activity_cutoff helper in Bugzilla::Test::Util. The history and user stats tests also have a change after the cutoff written to both tables, as a pod on the older code does, and check that it's listed and counted once.

Fixed in 3c0d478

Comment thread Bugzilla/BugMail.pm Resolved
@Xzzz
Xzzz requested a review from dklawren November 3, 2026 22:03
Comment thread Bugzilla/Install/DB.pm Outdated
{TYPE => 'INT3', REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'}})
if !$who;
$dbh->do('UPDATE flag_activity SET who = setter_id WHERE who IS NULL');
$dbh->bz_alter_column('flag_activity', 'who',

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

during the rolling deploy old pods still insert into flag_activity without who. strict mode is turned off in Bugzilla/DB/Mysql.pm so mysql fills in 0, then the new fk to profiles rejects it and flag changes on old pods fail until the rollout ends (if the fk isn't there yet, the row is saved with who = 0 and the inner join on profiles in history drops it). could we ship this in two steps: first add who as nullable and write it while still writing bugs_activity, then make it NOT NULL and stop writing bugs_activity in a follow-up. or at least keep the column nullable for now and backfill who = setter_id WHERE who IS NULL OR who = 0 on every checksetup run

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Right, thanks! I went with the two steps:

  • Bug 2088705 - Record the acting user in flag_activity.who, as a nullable column #2808 (bug 2088705) adds who as a nullable column, with its foreign key and index, and writes it on every flag change, still writing bugs_activity. checksetup fills who = setter_id on the rows without it, on every run.
  • Once it's deployed, this PR makes who NOT NULL (after checksetup has filled the rows left without it) and stops writing bugs_activity. During its rolling deploy, the old pods already write who.

So #2808 needs to go out in an earlier release than this one, I'll update this PR once #2808 is merged.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done in 5c3d058

master with #2808 is merged in, and _set_flag_activity_who_not_null makes who NOT NULL right after _add_flag_activity_who has filled the rows left without it.

Checking it on MySQL showed one more thing: once #2808 has added the foreign key, MySQL refuses to make who NOT NULL ("Cannot change column 'who': used in a foreign key constraint"). The step now drops the key first, and bz_setup_foreign_keys adds it back at the end of checksetup, as the oauth2 migration does.

Comment thread Bugzilla/FlagActivity.pm Outdated
# bugs_activity, which has them all; flag_activity has only some of them,
# written to both tables, which must not be listed twice. Older rows also don't
# always know their who: it was filled in from the setter.
sub newer_than_bugs_activity_sql {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

a dedicated one-row table please. an undeclared param doesn't survive well: migrate_params in Bugzilla/Config.pm drops params with no definition from the hash on every checksetup run and then pushes that hash to memcached, so Bugzilla->params loses the value even though the db row is still there (saving editparams does the same). a table like flag_activity_migration (cutoff DATETIME NOT NULL) filled once with NOW() next to the start rows, then read once per request via request_cache, keeps it out of the admin ui and is easy to drop later. with a fixed cutoff, bugs_activity flag rows need bug_when <= cutoff and flag_activity rows need flag_when > cutoff, and the per-row subquery and bugs_activity_bug_field_when_idx can probably go

Xzzz added 3 commits November 5, 2026 16:23
# Conflicts:
#	Bugzilla/DB/Schema.pm
#	Bugzilla/Install/DB.pm
#	t/flag-activity-who.t
@Xzzz
Xzzz requested a review from dklawren November 5, 2026 17:27
Comment thread Bugzilla/Bug.pm Outdated
$flag_bug_when AS bug_when,
CASE WHEN prev.id IS NULL THEN ''
WHEN prev.status = '?' AND preq.login_name IS NOT NULL
THEN CONCAT(ft.name, '?(', preq.login_name, ')')

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: raw CONCAT(...) here skips the DB layer, could use $dbh->sql_string_concat like the rest of the codebase. same in BugMail.pm and Reports/UserActivity.pm

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done => now the four CONCAT(...) come from $dbh->sql_string_concat, built next to the other $flag_* SQL fragments.

Fixed in cc875d3

Comment thread Bugzilla/BugMail.pm Outdated
fsa.flag_when AS bug_when,
CASE WHEN prev.id IS NULL THEN ''
WHEN prev.status = '?' AND preq.login_name IS NOT NULL
THEN CONCAT(ft.name, '?(', preq.login_name, ')')

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: same raw CONCAT as in Bug.pm, $dbh->sql_string_concat would keep it portable

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done the same way in _get_diffs

Fixed in 715c65f

. " AS ts,
CASE WHEN prev.id IS NULL THEN ''
WHEN prev.status = CHAR(63) AND preq.login_name IS NOT NULL
THEN CONCAT(ft.name, prev.status, '(', preq.login_name, ')')

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: same raw CONCAT, could use $dbh->sql_string_concat

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done here too (the CHAR(63) for the ? stays, the status column still provides it).

Fixed in dc04011

Comment thread Bugzilla/BugMail.pm
my $dbh = Bugzilla->dbh;

my @args = ($bug->id);
my @args = ($bug->id);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: alignment is off here, looks like perltidy wasn't run. perl scripts/perl-fmt.pl on the changed files should fix this plus the long $flag_suppjoins line in Bug.pm and the hash indent in Install/DB.pm

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Ah yes, done with the repo's .perltidyrc...
ran perl scripts/perl-fmt.pl on the files this PR modifies. Most weren't perltidy-clean on master, so this also reformats existing code in them.

Fixed in c1c4690

Comment thread Bugzilla/Search.pm
# SQL matching a flag_activity row against a history value like
# "review+" or "review?(user@example.com)". Returns nothing if the value
# isn't a flag state.
# SQL condition on flag_activity row $fa for changedto or changedfrom $value

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: two doc comments stacked above _flag_state_changed_sql, one can go

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

My fault: in fact the first one is the _flag_state_match comment, I think it got separated when I added _flag_state_changed_sql...:/
=> Moved it back above that sub rather than deleting it

Fixed in af5884e

@Xzzz

Xzzz commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator Author

Besides the 5 previous points, found 4 small things, fixed in separate commits:

  • ff7a278: the owner idle time search counted flag_activity rows up to the cutoff, whose who is a guess (changedby and the others already skip them) + new case in t/flag-activity-search.t
  • 7ba522f: the release tracking report now only counts the net change of a request, like history
  • 259baba: find_activity_id_for_flag uses sql_limit instead of a raw LIMIT 1
  • 262c8aa: stale comments in Flag.pm (flags tracked in bugs_activity) and Install/DB.pm (who still nullable)

I also merged master: extensions/Needinfo/t/bmo/mentions-on-create.t (from #2809) looked for the needinfo set when filing in bugs_activity, it now reads flag_activity.
Merging also pushed ProhibitComplexMappings over the critic baseline, so three map blocks of this PR are now loops.

Comment thread Bugzilla/Bug.pm Outdated
my $verified = $params->{product_change_confirmed};

# BMO - if everything is ok then we can skip the verfication page when using bug_modal
# BMO - if everything is ok then we can skip the verfication page when using bug_modal

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

please drop the unrelated perltidy reformatting, it hides the real change and pushes some comments out to column 0 (like this one and the one in _migrate_profiles_modification_ts in Install/DB.pm)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in ca55922: Changes to code this PR does not touch are now reverted, including comments.

# can look up the exact timestamp without relying on flag-type name matching.
my $dbh = Bugzilla->dbh;
my ($flag_when, $who) = $dbh->selectrow_array(
"SELECT flag_when, who FROM flag_activity WHERE flag_id = ?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

this runs one flag_activity query per flag on every bug page, could you load the last rows for all the bug's flags in one query

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done in 7f1ecfc: The first call now loads the last flag_activity row of every flag of the bug in one query (in flag_activity_bug_id_idx) and keeps them for the other flags, with a new case in t/flag-activity-stream.t for two flags on one bug.

@Xzzz

Xzzz commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator Author

As agreed, the schema part of this PR is split out to #2830 (flag_activity.who NOT NULL, the starting rows and the cutoff table), which runs on the current code.
This PR stays on hold until #2830 is deployed, I'll then merge master into it, so its diff only keeps the code changes.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants