Sort the partition capacity response deterministically - #303
Merged
Conversation
The partition list is already sorted before the response is returned, with the comment "Prevent flaky tests". MachineSizeCapacities, FaultyMachines and OtherMachines are not. All three derive from the row order of Machine().List(), which builds its term without OrderBy, so the order RethinkDB returns is unspecified. Test_partitionServiceServer_Capacity passes today only because the document ids in the fixture happen to place a c1-large machine first. Swapping the ids of the n1-medium machine and one c1-large machine - the same scenario, only relabelled - makes the subtest fail on the ordering alone, with every counter intact. Uses the sort package that the file already imports, no new dependency and no change to any existing expectation. Generated-By: Claude Code (Opus 5) Generated-By: deepseek-v4-flash
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #303 +/- ##
==========================================
- Coverage 79.93% 79.92% -0.01%
==========================================
Files 199 199
Lines 14800 14804 +4
==========================================
+ Hits 11830 11832 +2
- Misses 2969 2971 +2
Partials 1 1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
muhittink
marked this pull request as ready for review
September 2, 2026 17:45
majst01
approved these changes
Sep 3, 2026
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.
Description
Follow-up to metal-stack/metal-api#656. That pull request fixed the flaky
TestPartitionCapacityin metal-api by sorting the capacity response; @majst01 asked for it to be done here instead. This is the same fix, reduced to what is actually missing in this repository.Capacityalready sorts the partition list before returning it:Three sibling fields are not sorted, and they come from the same place.
Machine().List()builds its term withoutOrderBy— the string appears nowhere inpkg/db— so the row order RethinkDB returns is unspecified, andMachineSizeCapacities(order of first appearance of each size),FaultyMachinesandOtherMachines(append order) inherit it.The test is green by accident, not by design
Test_partitionServiceServer_Capacity/non_filter_considers_all_machineshas one partition with two sizes and expectsc1-large-x86beforen1-medium-x86.Measured, with a temporary probe printing the ids as
Machine().List()returns them: the order is[3 4 2 1 5]. Not the insertion order (reversing the fixture changes nothing), not id order — arbitrary, and identical across 6 separate processes with a fresh RethinkDB container each. The test passes because machine…0003happens to be ac1-large-x86machine.Relabelling the fixture — giving
n1-medium-x86the id…0003and the thirdc1-large-x86machine the id…0004, the same scenario with the ids moved — makes the subtest fail:Every counter is intact; only the position moved. With this change the relabelled fixture passes 3 of 3.
What this does not claim
There is no reproducible flake here today, and this fixes no failing test. The genuinely random axis — Go map iteration over the partitions — is the one already covered by the existing sort. What is left is a response order that nothing specifies and that happens to be stable for the current set of document ids.
metal-stack/clialready re-sorts two of the three axes on the client (cmd/admin/v2/partition.gosortsMachineSizeCapacitiesby size, thenPartitionCapacitySorterby partition), so an operator does not see this. Nothing re-sortsFaultyMachinesandOtherMachines.Size of the change
Six lines, in the loop that already exists, using the
sortpackage the file already imports. No new dependency, no new import, no existing expectation touched.Verification
Two pre-existing failures, both reproduced on
mainwithout this change:-count=3on the capacity test fails inone_allocated_machineon the second and third iteration - a state leak between iterations of the test function.make testfails inTestMachineCreateIntegrationwithtoo many parallel machine allocations taking place: unable to acquire mutex: partition-1. 55 packages pass, that one does not - onmainas well. It is a parallel-allocation test under-raceand it does not touchCapacity.Known limits
sort.Stringscalls onFaultyMachinesandOtherMachinesare defensive. No fixture holds more than one entry in either slice, so no assertion can make them fail. They are argued correct, not shown correct.++/+=, every reservation delta is non-negative andFreeclamps at zero — not from a measurement.metal-stack/cli. No such consumer was checked.Used AI-Tools ✨
References: