Skip to content

Commit

Permalink
ledger: move pieces of accountdb.go into a storage package (#4776)
Browse files Browse the repository at this point in the history
  • Loading branch information
icorderi authored Nov 21, 2022
1 parent b66a955 commit 60d0c09
Show file tree
Hide file tree
Showing 37 changed files with 7,090 additions and 6,788 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ GOLDFLAGS := $(GOLDFLAGS_BASE) \
UNIT_TEST_SOURCES := $(sort $(shell GOPATH=$(GOPATH) && GO111MODULE=off && go list ./... | grep -v /go-algorand/test/ ))
ALGOD_API_PACKAGES := $(sort $(shell GOPATH=$(GOPATH) && GO111MODULE=off && cd daemon/algod/api; go list ./... ))

MSGP_GENERATE := ./protocol ./protocol/test ./crypto ./crypto/merklearray ./crypto/merklesignature ./crypto/stateproof ./data/basics ./data/transactions ./data/stateproofmsg ./data/committee ./data/bookkeeping ./data/hashable ./agreement ./rpcs ./node ./ledger ./ledger/ledgercore ./stateproof ./data/account ./daemon/algod/api/spec/v2
MSGP_GENERATE := ./protocol ./protocol/test ./crypto ./crypto/merklearray ./crypto/merklesignature ./crypto/stateproof ./data/basics ./data/transactions ./data/stateproofmsg ./data/committee ./data/bookkeeping ./data/hashable ./agreement ./rpcs ./node ./ledger ./ledger/ledgercore ./ledger/store ./stateproof ./data/account ./daemon/algod/api/spec/v2

default: build

Expand All @@ -99,7 +99,7 @@ fix: build
$(GOPATH1)/bin/algofix */

lint: deps
$(GOPATH1)/bin/golangci-lint run -c .golangci.yml
$(GOPATH1)/bin/golangci-lint run -c .golangci.yml

check_shell:
find . -type f -name "*.sh" -exec shellcheck {} +
Expand Down
1,709 changes: 157 additions & 1,552 deletions ledger/accountdb.go

Large diffs are not rendered by default.

1,729 changes: 240 additions & 1,489 deletions ledger/accountdb_test.go

Large diffs are not rendered by default.

36 changes: 19 additions & 17 deletions ledger/acctonline.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/algorand/go-algorand/data/basics"
"github.com/algorand/go-algorand/data/bookkeeping"
"github.com/algorand/go-algorand/ledger/ledgercore"
"github.com/algorand/go-algorand/ledger/store"
"github.com/algorand/go-algorand/logging"
"github.com/algorand/go-algorand/util/db"
"github.com/algorand/go-algorand/util/metrics"
Expand All @@ -51,9 +52,10 @@ type modifiedOnlineAccount struct {
}

// cachedOnlineAccount is a light-weight version of persistedOnlineAccountData suitable for in-memory caching
//
//msgp:ignore cachedOnlineAccount
type cachedOnlineAccount struct {
baseOnlineAccountData
store.BaseOnlineAccountData
updRound basics.Round
}

Expand All @@ -63,7 +65,7 @@ type onlineAccounts struct {
dbs db.Pair

// Prepared SQL statements for fast accounts DB lookups.
accountsq *onlineAccountsDbQueries
accountsq store.OnlineAccountsReader

// cachedDBRoundOnline is always exactly tracker DB round (and therefore, onlineAccountsRound()),
// cached to use in lookup functions
Expand Down Expand Up @@ -172,7 +174,7 @@ func (ao *onlineAccounts) initializeFromDisk(l ledgerForTracker, lastBalancesRou
return
}

ao.accountsq, err = onlineAccountsInitDbQueries(ao.dbs.Rdb.Handle)
ao.accountsq, err = store.OnlineAccountsInitDbQueries(ao.dbs.Rdb.Handle)
if err != nil {
return
}
Expand All @@ -193,7 +195,7 @@ func (ao *onlineAccounts) latest() basics.Round {
// close closes the accountUpdates, waiting for all the child go-routine to complete
func (ao *onlineAccounts) close() {
if ao.accountsq != nil {
ao.accountsq.close()
ao.accountsq.Close()
ao.accountsq = nil
}

Expand Down Expand Up @@ -463,10 +465,10 @@ func (ao *onlineAccounts) postCommit(ctx context.Context, dcc *deferredCommitCon
for _, persistedAcct := range dcc.updatedPersistedOnlineAccounts {
ao.baseOnlineAccounts.write(persistedAcct)
ao.onlineAccountsCache.writeFrontIfExist(
persistedAcct.addr,
persistedAcct.Addr,
cachedOnlineAccount{
baseOnlineAccountData: persistedAcct.accountData,
updRound: persistedAcct.updRound,
BaseOnlineAccountData: persistedAcct.AccountData,
updRound: persistedAcct.UpdRound,
})
}

Expand Down Expand Up @@ -526,7 +528,7 @@ func (ao *onlineAccounts) onlineTotalsEx(rnd basics.Round) (basics.MicroAlgos, e
ao.log.Errorf("onlineTotalsImpl error: %v", err)
}

totalsOnline, err = ao.accountsq.lookupOnlineTotalsHistory(rnd)
totalsOnline, err = ao.accountsq.LookupOnlineTotalsHistory(rnd)
return totalsOnline, err
}

Expand Down Expand Up @@ -615,7 +617,7 @@ func (ao *onlineAccounts) lookupOnlineAccountData(rnd basics.Round, addr basics.
var paramsOffset uint64
var rewardsProto config.ConsensusParams
var rewardsLevel uint64
var persistedData persistedOnlineAccountData
var persistedData store.PersistedOnlineAccountData

// the loop serves retrying logic if the database advanced while
// the function was analyzing deltas or caches.
Expand Down Expand Up @@ -678,8 +680,8 @@ func (ao *onlineAccounts) lookupOnlineAccountData(rnd basics.Round, addr basics.
// As an optimization, we avoid creating
// a separate transaction here, and directly use a prepared SQL query
// against the database.
persistedData, err = ao.accountsq.lookupOnline(addr, rnd)
if err != nil || persistedData.rowid == 0 {
persistedData, err = ao.accountsq.LookupOnline(addr, rnd)
if err != nil || persistedData.Rowid == 0 {
// no such online account, return empty
return ledgercore.OnlineAccountData{}, err
}
Expand All @@ -694,7 +696,7 @@ func (ao *onlineAccounts) lookupOnlineAccountData(rnd basics.Round, addr basics.
// * if commitRound deletes some history after, the cache has additional entries and updRound comparison gets a right value
// 2. after commitRound but before postCommit => OK, read full history, ignore the update from postCommit in writeFront's updRound comparison
// 3. after postCommit => OK, postCommit does not add new entry with writeFrontIfExist, but here all the full history is loaded
persistedDataHistory, validThrough, err := ao.accountsq.lookupOnlineHistory(addr)
persistedDataHistory, validThrough, err := ao.accountsq.LookupOnlineHistory(addr)
if err != nil || len(persistedDataHistory) == 0 {
return ledgercore.OnlineAccountData{}, err
}
Expand All @@ -715,21 +717,21 @@ func (ao *onlineAccounts) lookupOnlineAccountData(rnd basics.Round, addr basics.
} else {
for _, data := range persistedDataHistory {
written := ao.onlineAccountsCache.writeFront(
data.addr,
data.Addr,
cachedOnlineAccount{
baseOnlineAccountData: data.accountData,
updRound: data.updRound,
BaseOnlineAccountData: data.AccountData,
updRound: data.UpdRound,
})
if !written {
ao.accountsMu.Unlock()
err = fmt.Errorf("failed to write history of acct %s for round %d into online accounts cache", data.addr.String(), data.updRound)
err = fmt.Errorf("failed to write history of acct %s for round %d into online accounts cache", data.Addr.String(), data.UpdRound)
return ledgercore.OnlineAccountData{}, err
}
}
ao.log.Info("inserted new item to onlineAccountsCache")
}
ao.accountsMu.Unlock()
return persistedData.accountData.GetOnlineAccountData(rewardsProto, rewardsLevel), nil
return persistedData.AccountData.GetOnlineAccountData(rewardsProto, rewardsLevel), nil
}
// case 3.3: retry (for loop iterates and queries again)
ao.accountsMu.Unlock()
Expand Down
Loading

0 comments on commit 60d0c09

Please sign in to comment.