Fix non-deterministic ordering in message polling queries#15
Closed
Fix non-deterministic ordering in message polling queries#15
Conversation
Add secondary sort by id column to ORDER BY clauses in both acquireManyOptimistically and selectFirstAvailableWithLock methods across all database adapters (H2, HSQLDB, SQLite, PostgreSQL, MariaDB, MS SQL Server, Oracle). This ensures deterministic ordering when multiple messages have the same scheduledAt timestamp, which became a problem after H2 upgrade from 2.3.232 to 2.4.240. Co-authored-by: alexandru <11753+alexandru@users.noreply.github.com>
Add 'id' column to KindPlusScheduledAtIndex in all database migrations to ensure ORDER BY (scheduledAt, id) queries are fully index-covered. Also add migration #2 to update existing databases by dropping and recreating the index with the additional column. This ensures both deterministic ordering and optimal query performance without requiring additional sort operations. Co-authored-by: alexandru <11753+alexandru@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix non-deterministic test failure in DelayedQueueJDBCH2Test
Fix non-deterministic ordering in message polling queries
Feb 7, 2026
alexandru
reviewed
Feb 7, 2026
| Migration( | ||
| sql = | ||
| """ | ||
| DROP INDEX IF EXISTS "${tableName}__KindPlusScheduledAtIndex"; |
Member
There was a problem hiding this comment.
@copilot This isn't deployed in any production systems yet, I don't want a second migration.
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.
H2 2.4.240 upgrade exposed non-deterministic test failures when messages share identical
scheduledAttimestamps.ORDER BY scheduledAtalone permits arbitrary row ordering in this case.Changes
Query Updates
idsort toacquireManyOptimisticallyandselectFirstAvailableWithLockacross all adaptersORDER BY scheduledAt, idIndex Optimization
KindPlusScheduledAtIndexfrom(pKind, scheduledAt)to(pKind, scheduledAt, id)Affected Adapters
H2, HSQLDB, SQLite, PostgreSQL, MariaDB, MS SQL Server, Oracle
Example
Index
(pKind, scheduledAt, id)satisfies WHERE clause filtering and complete ORDER BY without additional sorting.Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
settings.data.microsoft.com/usr/bin/dotnet dotnet /app/sqlceip/EdgeTelemetry.dll 00000001-0000-002a-0000-080000000100 funfix/delayedqueue/jvm/internals/jdbc/mariadb/ vm/s�� vm/src/main/kotlin/org/funfix/delayedqueue/jvm/DelayedQueue.kt vm/src/main/kotlin/org/funfix/delayedqueue/jvm/DelayedQueueInMemory.kt sBatchSizeLimit().html(dns block)vortex.data.microsoft.com/usr/bin/dotnet dotnet /app/sqlceip/EdgeTelemetry.dll 00000001-0000-002a-0000-080000000100 funfix/delayedqueue/jvm/internals/jdbc/mariadb/ vm/s�� vm/src/main/kotlin/org/funfix/delayedqueue/jvm/DelayedQueue.kt vm/src/main/kotlin/org/funfix/delayedqueue/jvm/DelayedQueueInMemory.kt sBatchSizeLimit().html(dns block)www.example.com/opt/oracle/product/23ai/dbhomeFree/bin/oracle oracleFREE (DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq))) docker0 -p tcp -d 172.17.0.4 cb8649b3e444b810554843cca28f75dd65a/log.json 1433 -j ACCEPT bash --no�� cb8649b3e444b810554843cca28f75dd65a(dns block)/opt/oracle/product/23ai/dbhomeFree/bin/oracle oracleFREE (DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq))) 3a86a0ed3c4d5acc32a1e65cc5372f841ca87fe7fe6ddbd03417ea16abb85772 9 -c 3f22f63db41c6b5b-w(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.