Page MenuHomePhabricator

Handling Flagged Revs for Reference Counts in Attribution API
Open, Needs TriagePublic

Description

What is flagged_rev?
flagged_revs are revisions that are pending review. We call the approved revision the stable revision, which can be older than the most recent aka latest revision. Logged out users do not see the latest revision. Logged in users can choose to see the latest revision, it comes with a warning saying revision is a pending review. See Special:PendingChanges for examples. Change language wiki to see other wikis.
Some analysis on number of pending reviews per wiki: Analysis.

Attribution API and Feature Counts
Attribution API (Prep Pantry Doc) wants to surface feature counts such as number of citations, references, sources. Per the design, we plan on warming the Linked Artifact Cache (LAC) with the feature counts on every page change. This means LAC will always have the latest revision cached. Other revision IDs can be queried too, but it won't usually be cached and will require an on-demand compute. Scenarios:

  • stable/latest rev1 in LAC. Typical scenario. Attribution API calls for rev1 and values returned from cache.
  • New latest rev2 in cache, invalidates all old revisions, including stable rev1. Attribution API wants reference counts for rev1. LAC computes and returns feature counts for rev1. Now LAC has both rev1 and rev2 cached.
  • New latest revision rev3, invalidates all old revisions, including rev1 and rev2. Attribution API wants stable rev1. LAC computes and returns feature counts for rev1. Now LAC has both rev1 and rev3 cached.

The frequency of such scenarios is not known, but given the single digit percentage of wiki pages with pending revisions, and given that active wikis and pages have revisions reviewed fast, this should not be too often.

Also note, every revision does not necessarily change reference counts. See comment. The distribution can be difference across wikis: see Analysis.

To Do
The purpose of this ticket is to work on the requirements for flagged revisions. As of design review of 20 Aug, 2026; the idea was to return null when the reference/citation counts are not available in cache to prevent Attribution API from waiting on on-demand compute. LAC can still run and compute the revision asked for async and will have warmed the cache for when Attribution API asks for the revision again.

Resolved is: We know how to handle flagged revisions for reference counts LAC design.

Please comment with more details/questions/solutions.

Related: T433607: Provide a way to query the LAC without triggering a compute job on a cache miss

Event Timeline

AKhatun_WMF renamed this task from Handling Flagged Revs for Attribution API to Handling Flagged Revs for Reference Counts in Attribution API.Aug 21 2026, 7:12 PM
AKhatun_WMF updated the task description. (Show Details)

Average hourly citation and reference count changes across wikipedias:

spark.sql("""
SELECT AVG(cite_inserted), AVG(cite_modified), AVG(cite_removed), AVG(reference_inserted), AVG(reference_modified), AVG(reference_removed)
FROM
(   SELECT sum(delta.revision.rendering.features.counts.citation.inserted) as cite_inserted,
    sum(delta.revision.rendering.features.counts.citation.modified) as cite_modified,
    sum(delta.revision.rendering.features.counts.citation.removed) as cite_removed,

    sum(delta.revision.rendering.features.counts.reference.inserted) as reference_inserted,
    sum(delta.revision.rendering.features.counts.reference.modified) as reference_modified,
    sum(delta.revision.rendering.features.counts.reference.removed) as reference_removed

    FROM event.mediawiki_page_html_feature_counts_change_v1 
    GROUP BY year, month, day, hour
) tbl
""").show(truncate=0, vertical=True)
avg(cite_inserted)      | 4614.683050068902  
avg(cite_modified)      | 5525.568672485071  
avg(cite_removed)       | 1613.5672944418925 
avg(reference_inserted) | 3332.02067064768   
avg(reference_modified) | 5250.581074873679  
avg(reference_removed)  | 1198.907671107028

Average number of revisions that have citations and/or references changed every hour

spark.sql("""
SELECT
    AVG(rows_total)          AS avg_rows_total,
    AVG(rows_with_citation)  AS avg_rows_with_citation,
    AVG(rows_with_reference) AS avg_rows_with_reference,
    AVG(rows_with_either)    AS avg_rows_with_either
FROM (
    SELECT
        count(*)                                                       AS rows_total,
        sum(CASE WHEN cite_total > 0 THEN 1 ELSE 0 END)                AS rows_with_citation,
        sum(CASE WHEN ref_total  > 0 THEN 1 ELSE 0 END)                AS rows_with_reference,
        sum(CASE WHEN cite_total > 0 OR ref_total > 0 THEN 1 ELSE 0 END) AS rows_with_either
    FROM (
        SELECT
            year, month, day, hour,
            coalesce(delta.revision.rendering.features.counts.citation.inserted, 0)
              + coalesce(delta.revision.rendering.features.counts.citation.modified, 0)
              + coalesce(delta.revision.rendering.features.counts.citation.removed,  0) AS cite_total,
            coalesce(delta.revision.rendering.features.counts.reference.inserted, 0)
              + coalesce(delta.revision.rendering.features.counts.reference.modified, 0)
              + coalesce(delta.revision.rendering.features.counts.reference.removed,  0) AS ref_total
        FROM event.mediawiki_page_html_feature_counts_change_v1
    ) r
    GROUP BY year, month, day, hour
) tbl
""").show(truncate=0, vertical=True)
avg_rows_total          | 49845.61093247589  
avg_rows_with_citation  | 3114.579696830501  
avg_rows_with_reference | 3503.1401010564996 
avg_rows_with_either    | 3506.796968305007

Therefore, worst case, we would show incorrect reference/citation counts for ~3500 pages every hour, across wikipedias -- IF we only ever use the latest revision in Attribution API.

the idea was to return null when the reference/citation counts are not available in cache to prevent Attribution API from waiting on on-demand compute. LAC can still run and compute the revision asked for async and will have warmed the cache for when Attribution API asks for the revision again.

Per our last discussion we wanted to refine the design around flagged_revs. Is there anything we need to think about or is the above good for now? If so, I can close this ticket.

@HCoplin-WMF @pmiazga @Mooeypoo

Just a suggestion: if we go in that direction (and I think we should!), it might make more sense to also not change the default behavior of LAC yet, and instead have Attribution API set a timeout client-side, after which it gives up on the request, but the async computation still happens.

That way different LAC clients, with different appetites for latency, can decide how long to wait on their own (up to indefinitely). SCROLL also nudges teams to implement client-side timeouts as a matter of graceful degradation when dependencies are down (see item 5.6 in the template), so I think this is in line with what SRE would tell client teams anyway.

it might make more sense to also not change the default behavior of LAC yet, and instead have Attribution API set a timeout client-side

+1 that's how we were thinking of setting up async as well. LAC will have a longer timeout to accommodate cache miss and big html fetches. Client can timeout whenever they want and call LAC again later.

We had some discussions T433607#12355443. Here's the summary and update:

Previous state:

As of design review of 20 Aug, 2026; the idea was to return null when the reference/citation counts are not available in cache to prevent Attribution API from waiting on on-demand compute. LAC can still run and compute the revision asked for async and will have warmed the cache for when Attribution API asks for the revision again.

New state:

  • Attribution API needs to call LAC with only-if-cached in Cache-Control header to ensure fast response from cached values. On cache miss (504) you would set a null.
  • The cache miss will NOT automatically compute the value behind the scenes. To ensure Attribution API should perform a async call with a longer timeout and no cache control. This way misses will be computed in lambda and returned (also cached). Following Attribution API calls will get this cached value as expected.

In short: LAC clients would need a mechanism to wait async for long running jobs to complete and cache in case of a case miss.

LAC clients would need a mechanism to wait async

Hm, you know...MW PHP clients could submit a new LinkedArtifactPrecomputeJob. We'd need a way to properly describe why the job was submitted, so that users of the resulting RevisionLinkedArtifactComputedEvent know that it happened because of a client request to compute, rather than after the configured input Event (e.g. edit event).

Maybe no RevisionLinkedArtifactComputedEvent for such compute calls, because they are essentially API calls? Something similar to isReconciliationRequest but for API calls?

Yeha, I think we may still emit RevisionLinkedArtifactComputedEvent, but with the 'trigger' set appropriately.

  • $triggerEventType: ?
  • $triggerCause: ?
  • $isReconciliationRequest: true

We can also adapt this 'trigger' concept too.

But, for sure we would not emit a revision_html_feature_counts_compute event.

From @Ottomata

BTW,
 Following Attribution API calls will get this cached value as expected.This might not be true.  Old revs are deleted whenever a new rev is cached.  You will want to save the flaggedrevs published value in your MW cache.  There will be a DomainEvent you can subscribe to receive the value once it is computed, you can stash it by listening to that.

So Attribution API

  • can wait async and store the value in their own cache to ensure the value is always available.
  • can use LinkedArtifactPrecomputeJob to run the async job (will need some small changes)
  • use DomainEvents

And, FWIW, once patches for T436695 land and are in use, I'll update wikitech docs with instructions for how to do all that ^ :)

As asked, I diagrammed a proposal here.

01-attribution-revision-feature-count-LAC-flow.png (4,979×3,681 px, 1 MB)

The code for all of this is in flight as part of T436695, but I think we are converging on its final form.

For the Attribution API + Reference Counts + FlaggedRevs problem, the basic idea is:

  • Attribution API stores reference counts it needs in a MW cache
  • At read time for revision R, Attribution API:
    • Checks its cache for R, if present, serves it.
    • Else, Attribution API requests value from LAC with only-if-cached
    • If LAC has value, Attribution API stores value in its MW Cache, and serves it.
    • Else, Attribution API submits a LinkedArtifactsPrefetchJob.
  • Attribution API implements a RevisionLinkedArtifactPrefetchedListener, e.g. AttributionApiIngress.
    • AttributionApiIngress:: handleRevisionLinkedArtifactPrefetchedEvent listens for revision artifacts it needs, and stores them in its MW cache.

An example of implementing a RevisionLinkedArtifactPrefetchedListener is in RevisionHtmlFeatureCountsComputeIngress.

Updated: Percentage of pages with stable rev != latest rev, filtered for ns 0 and pages that are under flagged revs.

flaggedrevs_ns0 = {}
for wiki in flaggedrevs_wikis:
    df = wmf.mariadb.run(
        """SELECT
              SUM(fp.fp_stable <> p.page_latest) AS stale,
              COUNT(*)                           AS total,
              100 * SUM(fp.fp_stable <> p.page_latest) / COUNT(*) AS pct
            FROM page p
            JOIN flaggedpages fp ON fp.fp_page_id = p.page_id
            WHERE p.page_namespace = 0
              AND p.page_is_redirect = 0;
""",
        wiki,           # the wiki db code = the `dbs` arg
    )
    flaggedrevs_ns0[wiki] = df
                       stale    total      pct
idwiki               5730     6933  82.6482
test2wiki            1018     1818  55.9956
dewikiquote           988     2050  48.1951
iawiki               2515     6358  39.5565
hiwiki                 82      232  35.3448
zh_classicalwiki     1548     6047  25.5995
ptwikibooks          1895    11536  16.4268
ukwiktionary         6806    49284  13.8098
fawiki                173     1253  13.8069
ruwiki             283026  2055632  13.7683
ukwiki             125692  1298118   9.6826
eowiki              19154   362573   5.2828
enwikibooks          3824    76776   4.9807
arwiki              51361  1319089   3.8937
vecwiki              2344    69224   3.3861
ruwiktionary        33762  1151312   2.9325
huwiki              15516   568387   2.7298
ckbwiki                 3      126   2.3810
bnwiki                  6      369   1.6260
sqwiki               1357   114039   1.1899
trwiki               7553   702508   1.0751
kawiki               1856   176538   1.0513
iswiktionary          994   103429   0.9610
plwiktionary         7178   802176   0.8948
plwiki              13275  1710520   0.7761
ruwikiquote           145    18827   0.7702
cewiki               5585   849480   0.6575
bswiki                506    96994   0.5217
dewiki              10913  3158554   0.3455
ruwikisource         1768   570830   0.3097
fiwiki               1573   624732   0.2518
mkwiki                 17   164739   0.0103
alswiki                 3    31838   0.0094
plwikisource            5   276560   0.0018
dewiktionary           19  1263458   0.0015
bewiki                  0   257613   0.0000
trwikiquote             0    6437   0.0000
hewikisource            0    14487   0.0000
enwiki                  0     4516   0.0000

Specifically for arwiki:

So I see ~4% pages that will be effected by only serving latest page. I dont understand how the parser cache hit values determine that arwiki is a bad candidate and has too many flagged revs.

Is being behind flagged rev the problem itself? Even if the reviews are fast enough such that most of the times, stable rev == latest rev.

Help me understand 😐

@Ottomata @pmiazga @HCoplin-WMF

Halley Coplin 
... Yes, let's proceed with always returning latest for now. To properly support stable, we need to go through the proper design & planning steps, likely inclusive of creating domain events for flagging when the stable version chagnes.

So we are going to only provide latest revisions. Flagged revs will not be cached for now.

Calling this ticket done. Further flagged revs planning can be done separately as a new project.