Commit 476bdff
authored
Post process settlements in parallel (#3853)
# Description
Currently we post-process settlements (i.e. associate the tx with a
solution proposed by a solver) serially. IIRC it was done that way
because it was simpler and that step simply ran in a background task at
the time.
Since then this logic has been moved onto the hot path so all the time
we spend there delays the creation of the next auction.
Additionally with the introduction of combinatorial auctions it's now
possible and relatively common to have multiple settlement transactions
in the same block. Whenever we have multiple settlements in one block
this PR should result in a significant performance uplift because we
process multiple concurrently.
# Changes
replaced loop to post-process auctions serially with logic that first
fetches ALL unprocessed settlements and then works on up to 10 of them
concurrently. This is fine for the post processing logic (as opposed to
the raw event indexing) because it's okay for post-processing to happen
out of order. So if the we successfully post-process settlement `n+1`
but not settlement `n` (e.g. due to a crash) the DB query would still
just return all unprocessed events instead of all NEW unprocessed
events.
I also adjusted how retrying works in this code. Instead of returning
some result enum (ok, invalid, nothing_to_do) to make the serial loop
retry something we now have a function `retry_with_sleep` that simply
retires a passed in future n times and returns an `Option` to indicate
whether it was successful or not.
This is used in 2 places:
1. fetching all outstanding settlements
2. post-processing individual outstanding settlement
With that the new logic should be as robust as the old one while being
IMO easier to reason about.
## How to test
Not sure how to test the improvement specifically. Since this is a
performance optimization and I don't really want to test the internals
of the implementation having a new test that makes sure that a big
number of settlements can be post-processed would be enough to test
correctness and for the performance aspect we'd have to deploy this on
the cluster.
Regarding the performance I temporarily deployed it to staging and it
produced the expected effect of reducing the spikiness that comes from
multiple settlements needing to be post-processed in the same block.
<img width="954" height="270" alt="Screenshot 2025-11-03 at 22 12 26"
src="https://github.com/user-attachments/assets/b459fb76-bf44-40db-894e-46ffd3d67f7d"
/>1 parent 914055c commit 476bdff
File tree
4 files changed
+90
-74
lines changed- crates
- autopilot/src
- boundary/events
- domain/settlement
- infra/persistence
- database/src
4 files changed
+90
-74
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
78 | | - | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
79 | 81 | | |
80 | 82 | | |
81 | 83 | | |
| |||
84 | 86 | | |
85 | 87 | | |
86 | 88 | | |
87 | | - | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
88 | 92 | | |
89 | 93 | | |
90 | 94 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
| 24 | + | |
23 | 25 | | |
24 | 26 | | |
25 | 27 | | |
| |||
29 | 31 | | |
30 | 32 | | |
31 | 33 | | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | 34 | | |
39 | 35 | | |
40 | 36 | | |
41 | 37 | | |
42 | 38 | | |
43 | 39 | | |
44 | 40 | | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
61 | 57 | | |
62 | 58 | | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
| 59 | + | |
72 | 60 | | |
73 | | - | |
74 | | - | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
75 | 64 | | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | 65 | | |
92 | | - | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
93 | 87 | | |
| 88 | + | |
94 | 89 | | |
95 | | - | |
| 90 | + | |
96 | 91 | | |
97 | 92 | | |
98 | | - | |
| 93 | + | |
99 | 94 | | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
| 95 | + | |
106 | 96 | | |
107 | 97 | | |
108 | 98 | | |
| |||
179 | 169 | | |
180 | 170 | | |
181 | 171 | | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
182 | 196 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
594 | 594 | | |
595 | 595 | | |
596 | 596 | | |
597 | | - | |
| 597 | + | |
598 | 598 | | |
599 | | - | |
| 599 | + | |
600 | 600 | | |
601 | 601 | | |
602 | 602 | | |
603 | 603 | | |
604 | 604 | | |
605 | 605 | | |
606 | | - | |
| 606 | + | |
607 | 607 | | |
| 608 | + | |
608 | 609 | | |
609 | 610 | | |
610 | 611 | | |
| |||
615 | 616 | | |
616 | 617 | | |
617 | 618 | | |
618 | | - | |
619 | | - | |
| 619 | + | |
| 620 | + | |
620 | 621 | | |
621 | 622 | | |
622 | 623 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
34 | | - | |
| 34 | + | |
35 | 35 | | |
36 | | - | |
| 36 | + | |
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
43 | 42 | | |
44 | | - | |
| 43 | + | |
45 | 44 | | |
46 | 45 | | |
47 | 46 | | |
| |||
195 | 194 | | |
196 | 195 | | |
197 | 196 | | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
| 197 | + | |
| 198 | + | |
202 | 199 | | |
203 | 200 | | |
204 | 201 | | |
| |||
207 | 204 | | |
208 | 205 | | |
209 | 206 | | |
210 | | - | |
| 207 | + | |
211 | 208 | | |
212 | | - | |
| 209 | + | |
213 | 210 | | |
214 | 211 | | |
0 commit comments