-
Notifications
You must be signed in to change notification settings - Fork 506
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
services/horizon: Fixing Claimable Balances Query Limit Issue #5032
services/horizon: Fixing Claimable Balances Query Limit Issue #5032
Conversation
40a9c16
to
734938a
Compare
@urvisavla you can test the performance of this fix by deploying your branch to the staging horizon cluster. Afterwards you can send requests to Horizon which exercise this query. You can also run SQL queries directly on the staging horizon DB |
I executed the query against both the staging pubnet database, experimenting with different limit values. The parameters I selected were:
Here are the various scenarios I tested:
I think the correct approach to address this issue would involve adding asset information to the claimants' table. However, as others have noted, this will require schema migration and data reingestion for the claimants table. Alternatively, following the suggestion of others, implementing an arbitrary limit should work well in most of cases. While it doesn't fix the bug, it significantly reduces the chances of running into it. |
069481d
to
9a42fff
Compare
Awesome analysis @urvisavla! This gives a really good picture.
This seems the most reasonable to me. Yes it's not perfect, but it's probably more than good enough to immediately help people affected by this. Is the 0.1% a random chance, or is there a particular type of query that would continue to trigger this bug? And how certain is that %? Or do you just colloquially mean "really unlikely it'll fail"? |
Currently, there are 615539 unique claimants, out of which 90 have more than 1000 entries in the claimants table, accounting for approximately 0.14% of the total claimants. When executing the query with a specific asset filter for these claimants when the limit is set to 1000, there is a possibility that the query may return incomplete result if all (1000) balance Ids returned by the subquery are for a different asset than the one the user has requested. |
We will have to reingest all the state tables (including claimable_balance_claimants) because of the soroban release. So I think we should try changing the schema as long as the new query will still be efficient. If you change the schema, you will need to bump this variable so that the reingestion of state tables occurs automatically: https://github.com/stellar/go/blob/master/services/horizon/internal/ingest/main.go#L61 |
920cf28
to
fa48973
Compare
services/horizon/internal/db2/schema/migrations/65_add_asset_in_claimable_balance_claimants.sql
Outdated
Show resolved
Hide resolved
services/horizon/internal/db2/schema/migrations/65_add_asset_in_claimable_balance_claimants.sql
Outdated
Show resolved
Hide resolved
services/horizon/internal/db2/history/claimable_balances_test.go
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice job, this issue was not easy!
b4b9bc5
to
b96d983
Compare
… use SQL JOIN to filter by destination
b96d983
to
fb28b78
Compare
PR Checklist
PR Structure
otherwise).
services/friendbot
, orall
ordoc
if the changes are broad or impact manypackages.
Thoroughness
.md
files, etc... affected by this change). Take a look in the
docs
folder for a given service,like this one.
Release planning
needed with deprecations, added features, breaking changes, and DB schema changes.
semver, or if it's mainly a patch change. The PR is targeted at the next
release branch if it's not a patch change.
What
This PR addresses the issue identified in #4907.
Issue:
When querying claimable balances with a combination of asset and claimant and setting a limit of 200, the response displayed the items correctly. However, when reducing the limit to 1 in the query, the response showed zero results.
Analysis:
The query to filter claimable balances by claimants is as follows:
In this query, there are no asset or sponsor filters in the subquery because there is no asset or sponsor information in
caimable_balance_claimants
table. Consequently, when the same limit as the outer query is applied to the subquery and an asset or sponsor filter is provided, the balance ids returned by the subquery may not necessarily match the asset type hence returning incorrect (incomplete) result. The same observation was made for the sponsor filter.Solution:
The solution is to use a different query that uses a JOIN with
claimable_balance_claimants
when asset or sponsor filters are specified:During testing, it was found that the JOIN query performs poorly when no filters other than the claimant are supplied. Therefore, in such case, we will continue to use the original subquery to retrieve claimable balances by claimants.
Test coverage:
Implemented a unit test to replicate the scenario described in the bug.
Why
Fixes #4907
Known limitations
N/A