-
Notifications
You must be signed in to change notification settings - Fork 535
Ordered account insertion into merkle trie (I) #1697
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
Merged
tsachiherman
merged 12 commits into
algorand:master
from
tsachiherman:tsachi/ordered_catchpoint_file
Nov 17, 2020
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
abe668e
refactor merkle trie rebuild
tsachiherman a67e490
Small refactor
tsachiherman aaa31e2
fix grammer
tsachiherman 8112001
Add benchmark
tsachiherman f488ab3
Update comments.
tsachiherman b1ea951
update per reviewer request.
tsachiherman faddefe
msgp ignore orderedAccountsIterStep
tsachiherman af2dedd
fix typo
tsachiherman f05bf27
Merge branch 'master' into tsachi/ordered_catchpoint_file
tsachiherman 78281f9
updating per reviewer's requested changes.
tsachiherman 756fb46
Add missing iterator.Close on error
tsachiherman 853d654
Merge branch 'master' into tsachi/ordered_catchpoint_file
tsachiherman 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 hidden or 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 |
|---|---|---|
|
|
@@ -1131,3 +1131,236 @@ func (iterator *encodedAccountsBatchIter) Close() { | |
| iterator.rows = nil | ||
| } | ||
| } | ||
|
|
||
| // orderedAccountsIterStep is used by orderedAccountsIter to define the current step | ||
| //msgp:ignore orderedAccountsIterStep | ||
| type orderedAccountsIterStep int | ||
|
|
||
| const ( | ||
| // startup step | ||
| oaiStepStartup = orderedAccountsIterStep(0) | ||
| // delete old ordering table if we have any leftover from previous invocation | ||
| oaiStepDeleteOldOrderingTable = orderedAccountsIterStep(0) | ||
| // create new ordering table | ||
| oaiStepCreateOrderingTable = orderedAccountsIterStep(1) | ||
| // query the existing accounts | ||
| oaiStepQueryAccounts = orderedAccountsIterStep(2) | ||
| // iterate over the existing accounts and insert their hash & address into the staging ordering table | ||
| oaiStepInsertAccountData = orderedAccountsIterStep(3) | ||
| // create an index on the ordering table so that we can efficiently scan it. | ||
| oaiStepCreateOrderingAccountIndex = orderedAccountsIterStep(4) | ||
| // query the ordering table | ||
| oaiStepSelectFromOrderedTable = orderedAccountsIterStep(5) | ||
| // iterate over the ordering table | ||
| oaiStepIterateOverOrderedTable = orderedAccountsIterStep(6) | ||
| // cleanup and delete ordering table | ||
| oaiStepShutdown = orderedAccountsIterStep(7) | ||
| // do nothing as we're done. | ||
| oaiStepDone = orderedAccountsIterStep(8) | ||
| ) | ||
|
|
||
| // orderedAccountsIter allows us to iterate over the accounts addresses in the order of the account hashes. | ||
| type orderedAccountsIter struct { | ||
| step orderedAccountsIterStep | ||
| rows *sql.Rows | ||
| tx *sql.Tx | ||
| accountCount int | ||
| fetchAccountData bool | ||
| insertStmt *sql.Stmt | ||
| } | ||
|
|
||
| // makeOrderedAccountsIter creates an ordered account iterator. Note that due to implementation reasons, | ||
| // only a single iterator can be active at a time. | ||
| func makeOrderedAccountsIter(tx *sql.Tx, accountCount int, fetchAccountData bool) *orderedAccountsIter { | ||
| return &orderedAccountsIter{ | ||
| tx: tx, | ||
| accountCount: accountCount, | ||
| fetchAccountData: fetchAccountData, | ||
| step: oaiStepStartup, | ||
| } | ||
| } | ||
|
|
||
| // accountAddressHashData is used by Next to return a single account address, hash and account data. | ||
| type accountAddressHashData struct { | ||
| address basics.Address | ||
| digest []byte | ||
| encodedAccountData []byte | ||
| } | ||
|
|
||
| // Next returns an array containing the account address, hash and potentially data | ||
| // the Next function works in multiple processing stages, where it first processs the current accounts and order them | ||
|
tsachiherman marked this conversation as resolved.
|
||
| // followed by returning the ordered accounts. In the first phase, it would return empty accountAddressHashData array | ||
| // and sets the processedRecords to the number of accounts that were processed. On the second phase, the acct | ||
| // would contain valid data ( and optionally the account data as well, if was asked in makeOrderedAccountsIter) and | ||
| // the processedRecords would be zero. If err is sql.ErrNoRows it means that the iterator have completed it's work and no further | ||
| // accounts exists. Otherwise, the caller is expected to keep calling "Next" to retrieve the next set of accounts | ||
| // ( or let the Next function make some progress toward that goal ) | ||
| func (iterator *orderedAccountsIter) Next(ctx context.Context) (acct []accountAddressHashData, processedRecords int, err error) { | ||
| if iterator.step == oaiStepDeleteOldOrderingTable { | ||
|
algonautshant marked this conversation as resolved.
|
||
| // although we're going to delete this table anyway when completing the iterator execution, we'll try to | ||
| // clean up any intermediate table. | ||
| _, err = iterator.tx.ExecContext(ctx, "DROP TABLE IF EXISTS accountsiteratorhashes") | ||
| if err != nil { | ||
| return | ||
| } | ||
| iterator.step = oaiStepCreateOrderingTable | ||
| return | ||
| } | ||
| if iterator.step == oaiStepCreateOrderingTable { | ||
| // create the temporary table | ||
| _, err = iterator.tx.ExecContext(ctx, "CREATE TABLE accountsiteratorhashes(address blob, hash blob)") | ||
| if err != nil { | ||
| return | ||
| } | ||
| iterator.step = oaiStepQueryAccounts | ||
| return | ||
| } | ||
| if iterator.step == oaiStepQueryAccounts { | ||
| // iterate over the existing accounts | ||
| iterator.rows, err = iterator.tx.QueryContext(ctx, "SELECT address, data FROM accountbase") | ||
| if err != nil { | ||
| return | ||
| } | ||
| // prepare the insert statement into the temporary table | ||
| iterator.insertStmt, err = iterator.tx.PrepareContext(ctx, "INSERT INTO accountsiteratorhashes(address, hash) VALUES(?, ?)") | ||
| if err != nil { | ||
| return | ||
| } | ||
| iterator.step = oaiStepInsertAccountData | ||
| return | ||
| } | ||
| if iterator.step == oaiStepInsertAccountData { | ||
| var addr basics.Address | ||
| count := 0 | ||
| for iterator.rows.Next() { | ||
| var addrbuf []byte | ||
| var buf []byte | ||
| err = iterator.rows.Scan(&addrbuf, &buf) | ||
| if err != nil { | ||
| iterator.Close(ctx) | ||
| return | ||
| } | ||
|
|
||
| if len(addrbuf) != len(addr) { | ||
| err = fmt.Errorf("Account DB address length mismatch: %d != %d", len(addrbuf), len(addr)) | ||
| iterator.Close(ctx) | ||
| return | ||
| } | ||
|
|
||
| copy(addr[:], addrbuf) | ||
|
|
||
| var accountData basics.AccountData | ||
| err = protocol.Decode(buf, &accountData) | ||
| if err != nil { | ||
| iterator.Close(ctx) | ||
| return | ||
| } | ||
| hash := accountHashBuilder(addr, accountData, buf) | ||
| _, err = iterator.insertStmt.ExecContext(ctx, addrbuf, hash) | ||
| if err != nil { | ||
| iterator.Close(ctx) | ||
| return | ||
| } | ||
|
|
||
| count++ | ||
| if count == iterator.accountCount { | ||
| // we're done with this iteration. | ||
| processedRecords = count | ||
| return | ||
| } | ||
| } | ||
| processedRecords = count | ||
| iterator.rows.Close() | ||
| iterator.rows = nil | ||
| iterator.insertStmt.Close() | ||
| iterator.insertStmt = nil | ||
| iterator.step = oaiStepCreateOrderingAccountIndex | ||
| return | ||
| } | ||
| if iterator.step == oaiStepCreateOrderingAccountIndex { | ||
|
Contributor
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. Why this step needs to return? Can't it directly call Next to advance to oaiStepSelectFromOrderedTable? |
||
| // create an index. It shown that even when we're making a single select statement in step 5, it would be better to have this index vs. not having it at all. | ||
| // note that this index is using the rowid of the accountsiteratorhashes table. | ||
| _, err = iterator.tx.ExecContext(ctx, "CREATE INDEX accountsiteratorhashesidx ON accountsiteratorhashes(hash)") | ||
| if err != nil { | ||
| iterator.Close(ctx) | ||
| return | ||
| } | ||
| iterator.step = oaiStepSelectFromOrderedTable | ||
| return | ||
| } | ||
| if iterator.step == oaiStepSelectFromOrderedTable { | ||
| // select the data from the ordered table | ||
| if iterator.fetchAccountData { | ||
|
algonautshant marked this conversation as resolved.
|
||
| iterator.rows, err = iterator.tx.QueryContext(ctx, "SELECT accountsiteratorhashes.address, accountsiteratorhashes.hash, accountbase.data FROM accountsiteratorhashes JOIN accountbase ON accountbase.address=accountsiteratorhashes.address ORDER BY accountsiteratorhashes.hash") | ||
| } else { | ||
| iterator.rows, err = iterator.tx.QueryContext(ctx, "SELECT address, hash FROM accountsiteratorhashes ORDER BY hash") | ||
| } | ||
|
|
||
| if err != nil { | ||
| iterator.Close(ctx) | ||
| return | ||
| } | ||
| iterator.step = oaiStepIterateOverOrderedTable | ||
|
tsachiherman marked this conversation as resolved.
|
||
| return | ||
| } | ||
|
|
||
| if iterator.step == oaiStepIterateOverOrderedTable { | ||
| acct = make([]accountAddressHashData, 0, iterator.accountCount) | ||
| var addr basics.Address | ||
| for iterator.rows.Next() { | ||
| var addrbuf []byte | ||
| var acctdata []byte | ||
| var hash []byte | ||
| if iterator.fetchAccountData { | ||
| err = iterator.rows.Scan(&addrbuf, &hash, &acctdata) | ||
| } else { | ||
| err = iterator.rows.Scan(&addrbuf, &hash) | ||
| } | ||
| if err != nil { | ||
| iterator.Close(ctx) | ||
| return | ||
| } | ||
|
|
||
| if len(addrbuf) != len(addr) { | ||
| err = fmt.Errorf("Account DB address length mismatch: %d != %d", len(addrbuf), len(addr)) | ||
| iterator.Close(ctx) | ||
| return | ||
| } | ||
|
|
||
| copy(addr[:], addrbuf) | ||
|
|
||
| acct = append(acct, accountAddressHashData{address: addr, digest: hash, encodedAccountData: acctdata}) | ||
| if len(acct) == iterator.accountCount { | ||
| // we're done with this iteration. | ||
| return | ||
| } | ||
| } | ||
| iterator.step = oaiStepShutdown | ||
| iterator.rows.Close() | ||
| iterator.rows = nil | ||
| return | ||
| } | ||
| if iterator.step == oaiStepShutdown { | ||
| err = iterator.Close(ctx) | ||
| if err != nil { | ||
| return | ||
| } | ||
| iterator.step = oaiStepDone | ||
| // fallthrough | ||
| } | ||
| return nil, 0, sql.ErrNoRows | ||
| } | ||
|
|
||
| // Close shuts down the orderedAccountsBuilderIter, releasing database resources. | ||
| func (iterator *orderedAccountsIter) Close(ctx context.Context) (err error) { | ||
| if iterator.rows != nil { | ||
| iterator.rows.Close() | ||
| iterator.rows = nil | ||
| } | ||
| if iterator.insertStmt != nil { | ||
| iterator.insertStmt.Close() | ||
| iterator.insertStmt = nil | ||
| } | ||
| _, err = iterator.tx.ExecContext(ctx, "DROP TABLE IF EXISTS accountsiteratorhashes") | ||
| return | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.