-
Notifications
You must be signed in to change notification settings - Fork 499
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/internal/db2/history: Implement StreamAllOffers using batches #4397
Merged
tamirms
merged 3 commits into
stellar:horizon-db-optimizations
from
tamirms:stream-all-offers
May 24, 2022
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,16 @@ package history | |
|
||
import ( | ||
"context" | ||
"math" | ||
|
||
sq "github.com/Masterminds/squirrel" | ||
"github.com/jmoiron/sqlx" | ||
|
||
"github.com/stellar/go/support/errors" | ||
) | ||
|
||
const offersBatchSize = 50000 | ||
|
||
// QOffers defines offer related queries. | ||
type QOffers interface { | ||
StreamAllOffers(ctx context.Context, callback func(Offer) error) error | ||
|
@@ -83,28 +86,45 @@ func (q *Q) GetOffers(ctx context.Context, query OffersQuery) ([]Offer, error) { | |
|
||
// StreamAllOffers loads all non deleted offers | ||
func (q *Q) StreamAllOffers(ctx context.Context, callback func(Offer) error) error { | ||
lastID := int64(0) | ||
for { | ||
nextID, err := q.streamAllOffersBatch(ctx, lastID, offersBatchSize, callback) | ||
if err != nil { | ||
return err | ||
} | ||
if lastID == nextID { | ||
return nil | ||
} | ||
lastID = nextID | ||
} | ||
} | ||
|
||
func (q *Q) streamAllOffersBatch(ctx context.Context, lastId int64, limit uint64, callback func(Offer) error) (int64, error) { | ||
var rows *sqlx.Rows | ||
var err error | ||
|
||
if rows, err = q.Query(ctx, selectOffers.Where("deleted = ?", false)); err != nil { | ||
return errors.Wrap(err, "could not run all offers select query") | ||
rows, err = q.Query(ctx, selectOffers. | ||
Where("deleted = ?", false). | ||
Where("offer_id > ? ", lastId). | ||
OrderBy("offer_id asc").Limit(limit)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This may not work properly outside |
||
if err != nil { | ||
return 0, errors.Wrap(err, "could not run all offers select query") | ||
} | ||
|
||
defer rows.Close() | ||
|
||
for rows.Next() { | ||
offer := Offer{} | ||
if err = rows.StructScan(&offer); err != nil { | ||
return errors.Wrap(err, "could not scan row into offer struct") | ||
return 0, errors.Wrap(err, "could not scan row into offer struct") | ||
} | ||
|
||
if err = callback(offer); err != nil { | ||
return err | ||
return 0, err | ||
} | ||
lastId = offer.OfferID | ||
} | ||
|
||
return rows.Err() | ||
|
||
return lastId, rows.Err() | ||
} | ||
|
||
// GetUpdatedOffers returns all offers created, updated, or deleted after the given ledger sequence. | ||
|
This file contains 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
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.
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.
this is somewhat dependent on the compute resources of the db right, do you think it's worthwhile to be more conservative with a smaller batch size to keep the query under 10s, or does it them impact throughput here more-so?