Skip to content
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: Batch Upsert Accounts and Trust Lines #2073

Conversation

bartekn
Copy link
Contributor

@bartekn bartekn commented Dec 18, 2019

PR Checklist

PR Structure

  • This PR has reasonably narrow scope (if not, break it down into smaller PRs).
  • This PR avoids mixing refactoring changes with feature changes (split into two PRs
    otherwise).
  • This PR's title starts with name of package that is most changed in the PR, ex.
    services/friendbot, or all or doc if the changes are broad or impact many
    packages.

Thoroughness

  • This PR adds tests for the most critical parts of the new functionality or fixes.
  • I've updated any docs (developer docs, .md
    files, etc... affected by this change). Take a look in the docs folder for a given service,
    like this one.

Release planning

  • I've updated the relevant CHANGELOG (here for Horizon) if
    needed with deprecations, added features, breaking changes, and DB schema changes.
  • I've decided if this PR requires a new major/minor version according to
    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 commit changes DatabaseProcessor to insert/update (upsert) accounts and trust lines in batches.

Why

In #2004 we added LedgerEntryChangeCache that aims to decrease number of DB updates: all the changes connected to a single ledger entry are squashed into just one DB query. Even though it gives a nice performance boost when large number of ops change a small number of ledger entries, it turns out this is not enough. When many ledger entries are changed in a new ledger, DB connection round trip time takes significant percentage of time in overall ledger processing time.

The SQL query was borrowed from stellar-core.

Known limitations

  • This code shouldn't be the final code that is released. The aim of this PR is to deploy it to stg environment to check it's performance.
  • This is adding upsert queries for accounts and trust lines only. These types are the most common changes in the recent ledgers. The final solution should add batch upsert and batch delete for all ledger entries.
  • There's a potential to improve the code: autogenerating upsert query for any ledger entry type, better tests. Will be done in a separate PR.
  • It removes a code that check a number of rows affected by a query. Unfortunately, the performance of the current solution is too bad to keep it.

@cla-bot cla-bot bot added the cla: yes label Dec 18, 2019
@@ -217,7 +219,8 @@ func (p *DatabaseProcessor) ProcessLedger(ctx context.Context, store *pipeline.S
// Get all transactions
var transactions []io.LedgerTransaction
for {
transaction, err := r.Read()
var transaction io.LedgerTransaction
transaction, err = r.Read()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like staticcheck was upgraded (from 20190621195816-6e04913cbbac to 20191218040434-6f9e13bbec44). This is a fix for shadowed err.

sql := `
WITH r AS
(SELECT
unnest(?::text[]), /* account_id */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

Copy link
Contributor

@abuiles abuiles left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bartekn LGTM!

bartekn and others added 3 commits December 18, 2019 19:49
@bartekn bartekn merged commit 9d7f875 into stellar:release-horizon-v0.25.0 Dec 19, 2019
@bartekn bartekn deleted the batch-upsert-accounts-trust-lines branch December 19, 2019 15:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants