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

API: Hotfix block endpoint #1397

Merged
merged 5 commits into from
Jan 3, 2023

Conversation

Eric-Warehime
Copy link
Contributor

#1395 Shows more details.

At a high level, the SQL used to fetch transactions for the /v2/blocks endpoint balloons the amount of memory usage when the block contains large numbers of inner transactions. These inner transactions end up getting discarded anyways after the query returns and is deserialized. This patch improves the query and memory usage by only fetching root transactions to begin with.

Test Plan

Ran benchmarks from #1396 to get memory usage data. The result of a block containing 250 transactions, each of which has 250 inner transactions, w/ a MaxTransactionsLimit set to the default of 1000 shows pre-patch memory usage around 9GB, and post-patch memory usage around 2GB.

Before patch:

BenchmarkBlockTransactions//v2/blocks_txns:_250_innerTxns:_250-10         	       1	9067736792 ns/op	3213189536 B/op	 2537356 allocs/op

After patch:

BenchmarkBlockTransactions//v2/blocks_txns:_250_innerTxns:_250-10         	       1	2378328417 ns/op	800444504 B/op	  633329 allocs/op

@codecov
Copy link

codecov bot commented Jan 3, 2023

Codecov Report

Merging #1397 (1f154fc) into hotfix/2.15.1 (300b69f) will not change coverage.
The diff coverage is 100.00%.

@@              Coverage Diff               @@
##           hotfix/2.15.1    #1397   +/-   ##
==============================================
  Coverage          65.02%   65.02%           
==============================================
  Files                 52       52           
  Lines               8311     8311           
==============================================
  Hits                5404     5404           
  Misses              2433     2433           
  Partials             474      474           
Impacted Files Coverage Δ
api/handlers.go 75.53% <ø> (-0.07%) ⬇️
idb/idb.go 69.35% <ø> (ø)
idb/postgres/postgres.go 64.86% <100.00%> (+0.04%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

idb/postgres/postgres.go Outdated Show resolved Hide resolved
Co-authored-by: Will Winder <wwinder.unh@gmail.com>
@@ -658,6 +658,9 @@ func buildTransactionQuery(tf idb.TransactionFilter) (query string, whereArgs []
if tf.RekeyTo != nil && (*tf.RekeyTo) {
whereParts = append(whereParts, "(t.txn -> 'txn' -> 'rekey') IS NOT NULL")
}
if tf.ReturnRootTxnsOnly {
whereParts = append(whereParts, "t.txid IS NOT NULL")
}

// If returnInnerTxnOnly flag is false, then return the root transaction
if !tf.ReturnInnerTxnOnly {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is also needed to make sure we don't try to reference root:

Suggested change
if !tf.ReturnInnerTxnOnly {
if !tf.ReturnInnerTxnOnly && !tf.ReturnRootTxnsOnly {


// If returnInnerTxnOnly flag is false, then return the root transaction
if !tf.ReturnInnerTxnOnly {
if !(tf.ReturnInnerTxnOnly || tf.ReturnRootTxnsOnly) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The ReturnInnerTxnOnly variable name does not really represent what this is doing--it makes it much harder to reason about these conditions.

Let's update the name on the develop branch--avoiding a bunch of refactoring on this hotfix branch seems more important.

I've updated the most recent patch to represent what we want, which is that if neither of these options is enabled we will fetch root.txn and also if neither is enabled we'll add the JOIN which references the intra index.

Copy link
Contributor

@winder winder left a comment

Choose a reason for hiding this comment

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

Changes look good. Tested them locally and it works great. Some big blocks that used to timeout are now returning almost instantly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants