Repository navigation
Conversation
nnlugovoy
force-pushed
the
issue-517-bounded-event-selection
branch
from
November 28, 2026 16:40
004f3d8 to
c428a4e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #517
Related: #516
Problem
Events::get_events(), the query behind the runner'slist-due-batch, loads every due pending row into PHP, including theargslongtext, and then trims the result to the queue size.pre_get_cron_option()andpre_clear_scheduled_hook()page withLIMIT/OFFSET, so every page re-scans the table from the start. Both costs grow with the size of the pending queue.OFFSETpaging overORDER BY timestampis also not stable when many events share a timestamp. Page boundaries shift between queries, sopre_get_cron_option()can return some events twice and omit others.Changes
Due-event selection (
Events_Store::_query_due_event_candidates())reduce_queue()can pick, in two queries:SELECT action_hashed, MIN(timestamp) ... GROUP BY status, action_hashed: a loose index scan, about two index reads per action.UNION ALLof per-actionORDER BY timestamp, ID LIMIT <queue size + 1>branches for the<queue size>earliest actions. Internal actions are read without a limit, as before.ID, timestamp, action_hashed, instance;argsis never read. The new index covers the per-action branches, so they are answered from the index alone.reduce_queue(), exactly as the full set would, so the output is identical to the old selection. This includes the 15-per-action cap when one action dominates the queue.get_events()uses the old query, because without the index the bounded queries would scan the table once per action.add_filter( 'a8c_cron_control_use_projected_event_selection', '__return_false' ).Paging and bulk unscheduling
query_all_events_in_batches(), whichpre_get_cron_option()uses, pages by primary key (after_idwithorderby => 'ID'). Each page is a short primary-key range read, and every event is returned exactly once. Cron array order is unaffected, becauseformat_events_for_wp()already sorts by timestamp.pre_clear_scheduled_hook()andpre_unschedule_hook()no longer collect every match before acting. They fetch 500 active events with no ordering, complete them, and repeat. Completed events leave the active statuses, so each pass fetches the next batch, and the action/instance index serves the lookup. If an event fails to complete, the loop stops after that batch and returns the existingcron-control:wp:failed-event-deletingerror.Schema v2
action_instance_status_ts (action(191), instance, status, timestamp), for per-hook lookups such aswp_next_scheduled()and the scheduling duplicate check (Missing index causes a full table scan on action/instance lookups #516).status_action_hashed_ts_id_instance (status, action_hashed, timestamp, ID, instance), for due-event selection (Ready-event selection loads the entire pending queue into PHP #517).IDis listed explicitly beforeinstance, so the index stays in(timestamp, ID)order and the per-actionLIMITcan stop early instead of sorting every due row of the action.manage_options. Anonymous and REST requests never run the DDL.upgrade_lockheld for 15 minutes prevents concurrent index builds.SHOW INDEX, read from the primary database, confirms the expected definitions.intervalis declaredint(10) unsigned. On MariaDB,int unsignedmadedbDelta()issueALTER TABLE ... CHANGE COLUMN intervalon every table check, because core ignores integer display width only on MySQL 8.0.17+.Other
get_events()clamps the queue size toJOB_QUEUE_SIZE_MAX(250), which bounds the candidate query.__tests__/load-tests/Event_Selection_Load_Test.php, a benchmark that calls only public API, so the same file runs against any revision.Performance
Both
main(4ccb953) and this branch were run with__tests__/load-tests/Event_Selection_Load_Test.php:Handler_read_*counters; memory is peak PHP memory.get_events()at queue size 10 (the runner default):At queue size 250: 38k rows goes from 529 to 43 ms; 350k rows goes from 106.9 s to 144 ms, and from 1.69 GB to 59 MB.
pre_get_cron_option():On MySQL 8.0.28,
EXPLAIN ANALYZEwas used to check the query plans; these were not timed:GROUP BYquery usesUsing index for group-by.wp_next_scheduled()-style lookup for a hook with no rows reads 0 rows. On the v1 schema the same lookup scans the whole unique index.Correctness
mainat queue sizes 10, 50, and 250 in every benchmark scenario without timestamp ties (38k, 50k, and 350k rows).main's order, and so which tied events fit in the batch, depends on the query plan. The new code breaks ties by(action_hashed, ID), and its output equals the oldreduce_queue()run over the whole queue sorted that way.test_get_events_projected_selection_matches_legacy, across queue sizes 1-250.test_get_events_projected_selection_matches_legacy_for_one_backed_up_action: one action with 100 due rows at queue sizes 10-250. It fails if the per-action limit is reduced to the queue size.Upgrade
ALTERbriefly waits for a metadata lock at its start and end.Testing
__tests__/bin/test.sh(MariaDB 10.3, PHP 8.3): 69 tests pass single-site and multisite. The only skips are the multisite-only tests and one test that needsCREATE DATABASEpermission.ALTERon an up-to-date tablestatusindex not being requiredincludes/and__tests__/unit-tests/.Known limitations / follow-ups
queue size × (queue size + 1)index rows. That is 110 at the default queue size of 10, and up to 62,750 at 250. A smaller per-action limit would change which events are selected.statusindex is redundant withstatus_action_hashed_ts_id_instance. It is no longer required by the version check, but dropping it needs an explicit migration.get_events()now get 250.UPDATEper event; a set-based update per batch is a possible follow-up.