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

ledger: Remove redundant block header cache #5540

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
288e419
draft
algonautshant Jun 27, 2023
4a345d8
different locking scheme
algonautshant Jul 11, 2023
3f7ed04
Merge remote-tracking branch 'upstream/master' into shant/eliminate_B…
algonautshant Jul 11, 2023
7b80ba9
run the test
algonautshant Jul 11, 2023
098a347
test no longer needed
algonautshant Jul 11, 2023
c7ef1fd
remove blockHdr
algonautshant Jul 11, 2023
f1cb2b7
some cleanup
algonautshant Jul 12, 2023
a14171d
cleanup markers
algonautshant Jul 13, 2023
d309fb4
delete blockHeaderCache files
algonautshant Jul 13, 2023
99047f2
CR: fix the txTail locking.
algonautshant Jul 14, 2023
dcbf780
reverse lock position
algonautshant Jul 18, 2023
6b4a4ec
CR: remove unneded test function
algonautshant Jul 18, 2023
5f305d7
Merge remote-tracking branch 'upstream/master' into shant/eliminate_B…
algonautshant Jul 18, 2023
8b2d630
delete roundlru. no longer used
algonautshant Jul 19, 2023
502158b
update the test to account for more rounds being retained in the new …
algonautshant Jul 20, 2023
bb8e0eb
explain flushOffset viability
algonautshant Jul 20, 2023
8f3b02d
CR: removed unneded func in test
algonautshant Jul 20, 2023
62666bb
draft: remove blockHdrCached
algonautshant Jul 24, 2023
e35e88a
more cleanup: removal of BlockHdrCached from LedgerForLogic interface
algonautshant Jul 24, 2023
5502d3c
cleanup of all blockHdrCached
algonautshant Jul 24, 2023
21eb1f5
Merge remote-tracking branch 'origin/shant/eliminate_BlockHdrCached_p…
algonautshant Jul 24, 2023
a9cdf2b
CR: fix comment and loop param
algonautshant Aug 1, 2023
e60d63d
Merge remote-tracking branch 'upstream/master' into shant/eliminate_B…
algonautshant Aug 1, 2023
963965a
fix ledger interface issue in a new benchmark
algonautshant Aug 1, 2023
30ba0c0
add tests to cover new code not previously covered
algonautshant Aug 1, 2023
a1c9806
partitionTest missing
algonautshant Aug 1, 2023
8005526
fix remaining comments
algorandskiy Aug 2, 2023
8c8e123
remove unused putLV func
cce Aug 2, 2023
77c3b4c
update comment
algorandskiy Aug 2, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ledger/acctupdates.go
Original file line number Diff line number Diff line change
Expand Up @@ -1172,7 +1172,7 @@ func (au *accountUpdates) lookupLatest(addr basics.Address) (data basics.Account

if resourceDbRound < currentDbRound {
au.log.Errorf("accountUpdates.lookupLatest: resource database round %d is behind in-memory round %d", resourceDbRound, currentDbRound)
return basics.AccountData{}, basics.Round(0), basics.MicroAlgos{}, &StaleDatabaseRoundError{databaseRound: resourceDbRound, memoryRound: currentDbRound}
/*xxxxx*/ return basics.AccountData{}, basics.Round(0), basics.MicroAlgos{}, &StaleDatabaseRoundError{databaseRound: resourceDbRound, memoryRound: currentDbRound}
algonautshant marked this conversation as resolved.
Show resolved Hide resolved
}

tryAgain:
Expand Down
6 changes: 5 additions & 1 deletion ledger/acctupdates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ func (ml *mockLedgerForTracker) Block(rnd basics.Round) (bookkeeping.Block, erro
}

func (ml *mockLedgerForTracker) BlockHdr(rnd basics.Round) (bookkeeping.BlockHeader, error) {
return ml.blockHdr(rnd)
}

func (ml *mockLedgerForTracker) blockHdr(rnd basics.Round) (bookkeeping.BlockHeader, error) {
algonautshant marked this conversation as resolved.
Show resolved Hide resolved
if rnd > ml.Latest() {
return bookkeeping.BlockHeader{}, fmt.Errorf("rnd %d out of bounds", rnd)
}
Expand Down Expand Up @@ -2439,7 +2443,7 @@ func TestAcctUpdatesLookupLatestCacheRetry(t *testing.T) {
wg.Add(1)
done := make(chan struct{})
go func() {
ad, _, _, err = au.lookupLatest(addr1)
/*xxxxx*/ ad, _, _, err = au.lookupLatest(addr1)
algonautshant marked this conversation as resolved.
Show resolved Hide resolved
close(done)
wg.Done()
}()
Expand Down
4 changes: 4 additions & 0 deletions ledger/archival_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ func (wl *wrappedLedger) Block(rnd basics.Round) (bookkeeping.Block, error) {
}

func (wl *wrappedLedger) BlockHdr(rnd basics.Round) (bookkeeping.BlockHeader, error) {
return wl.blockHdr(rnd)
}

func (wl *wrappedLedger) blockHdr(rnd basics.Round) (bookkeeping.BlockHeader, error) {
algonautshant marked this conversation as resolved.
Show resolved Hide resolved
wl.recordBlockQuery(rnd)
return wl.l.BlockHdr(rnd)
}
Expand Down
23 changes: 7 additions & 16 deletions ledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ type Ledger struct {
trackers trackerRegistry
trackerMu deadlock.RWMutex

headerCache blockHeaderCache
// headerCache blockHeaderCache

// verifiedTxnCache holds all the verified transactions state
verifiedTxnCache verify.VerifiedTransactionCache
Expand Down Expand Up @@ -136,7 +136,7 @@ func OpenLedger(
tracer: tracer,
}

l.headerCache.initialize()
// l.headerCache.initialize()

defer func() {
if err != nil {
Expand Down Expand Up @@ -628,8 +628,6 @@ func (l *Ledger) OnlineCirculation(rnd basics.Round, voteRnd basics.Round) (basi

// CheckDup return whether a transaction is a duplicate one.
func (l *Ledger) CheckDup(currentProto config.ConsensusParams, current basics.Round, firstValid basics.Round, lastValid basics.Round, txid transactions.Txid, txl ledgercore.Txlease) error {
l.trackerMu.RLock()
cce marked this conversation as resolved.
Show resolved Hide resolved
defer l.trackerMu.RUnlock()
return l.txTail.checkDup(currentProto, current, firstValid, lastValid, txid, txl)
}

Expand All @@ -654,16 +652,11 @@ func (l *Ledger) Block(rnd basics.Round) (blk bookkeeping.Block, err error) {

// BlockHdr returns the BlockHeader of the block for round rnd.
func (l *Ledger) BlockHdr(rnd basics.Round) (blk bookkeeping.BlockHeader, err error) {
blk, exists := l.headerCache.get(rnd)
if exists {
return
}

blk, err = l.blockQ.getBlockHdr(rnd)
if err == nil {
l.headerCache.put(blk)
hdr, ok := l.txTail.blockHeader(rnd)
if !ok {
hdr, err = l.blockQ.getBlockHdr(rnd)
cce marked this conversation as resolved.
Show resolved Hide resolved
}
return
return hdr, err
cce marked this conversation as resolved.
Show resolved Hide resolved
}

// EncodedBlockCert returns the encoded block and the corresponding encoded certificate of the block for round rnd.
Expand Down Expand Up @@ -712,7 +705,7 @@ func (l *Ledger) AddValidatedBlock(vb ledgercore.ValidatedBlock, cert agreement.
if err != nil {
return err
}
l.headerCache.put(blk.BlockHeader)
// l.headerCache.put(blk.BlockHeader)
l.trackers.newBlock(blk, vb.Delta())
l.log.Debugf("ledger.AddValidatedBlock: added blk %d", blk.Round())
return nil
Expand Down Expand Up @@ -767,8 +760,6 @@ func (l *Ledger) GenesisAccounts() map[basics.Address]basics.AccountData {
// the deepest lookup happens when txn.LastValid == current => txn.LastValid == Latest + 1
// that gives Latest + 1 - (MaxTxnLife + 1) = Latest - MaxTxnLife as the first round to be accessible.
func (l *Ledger) BlockHdrCached(rnd basics.Round) (hdr bookkeeping.BlockHeader, err error) {
l.trackerMu.RLock()
defer l.trackerMu.RUnlock()
hdr, ok := l.txTail.blockHeader(rnd)
if !ok {
err = fmt.Errorf("no cached header data for round %d", rnd)
Expand Down
40 changes: 0 additions & 40 deletions ledger/ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1385,46 +1385,6 @@ func testLedgerRegressionFaultyLeaseFirstValidCheck2f3880f7(t *testing.T, versio
}
}

func TestLedgerBlockHdrCaching(t *testing.T) {
partitiontest.PartitionTest(t)
a := require.New(t)

dbName := fmt.Sprintf("%s.%d", t.Name(), crypto.RandUint64())
genesisInitState := getInitState()
const inMem = true
cfg := config.GetDefaultLocal()
cfg.Archival = true
log := logging.TestingLog(t)
log.SetLevel(logging.Info)
l, err := OpenLedger(log, dbName, inMem, genesisInitState, cfg)
a.NoError(err)
defer l.Close()

blk := genesisInitState.Block

for i := 0; i < 1024; i++ {
blk.BlockHeader.Round++
blk.BlockHeader.TimeStamp += int64(crypto.RandUint64() % 100 * 1000)
err := l.AddBlock(blk, agreement.Certificate{})
a.NoError(err)

hdr, err := l.BlockHdr(blk.BlockHeader.Round)
a.NoError(err)
a.Equal(blk.BlockHeader, hdr)
}

rnd := basics.Round(128)
hdr, err := l.BlockHdr(rnd) // should update LRU cache but not latestBlockHeaderCache
a.NoError(err)
a.Equal(rnd, hdr.Round)

_, exists := l.headerCache.lruCache.Get(rnd)
a.True(exists)

_, exists = l.headerCache.latestHeaderCache.get(rnd)
a.False(exists)
}

func BenchmarkLedgerBlockHdrCaching(b *testing.B) {
benchLedgerCache(b, 1024-256+1)
}
Expand Down
2 changes: 2 additions & 0 deletions ledger/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ func (dcc deferredCommitContext) newBase() basics.Round {
var errMissingAccountUpdateTracker = errors.New("initializeTrackerCaches : called without a valid accounts update tracker")

func (tr *trackerRegistry) initialize(l ledgerForTracker, trackers []ledgerTracker, cfg config.Local) (err error) {
tr.mu.Lock()
defer tr.mu.Unlock()
algorandskiy marked this conversation as resolved.
Show resolved Hide resolved
tr.dbs = l.trackerDB()
tr.log = l.trackerLog()

Expand Down
22 changes: 16 additions & 6 deletions ledger/txtail.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ type txTail struct {
}

func (t *txTail) loadFromDisk(l ledgerForTracker, dbRound basics.Round) error {
t.tailMu.Lock()
algorandskiy marked this conversation as resolved.
Show resolved Hide resolved
defer t.tailMu.Unlock()

t.log = l.trackerLog()

var roundData []*trackerdb.TxTailRound
Expand Down Expand Up @@ -196,6 +199,9 @@ func (t *txTail) newBlock(blk bookkeeping.Block, delta ledgercore.StateDelta) {
return
}

t.tailMu.Lock()
algorandskiy marked this conversation as resolved.
Show resolved Hide resolved
defer t.tailMu.Unlock()

var tail trackerdb.TxTailRound
tail.TxnIDs = make([]transactions.Txid, len(delta.Txids))
tail.LastValid = make([]basics.Round, len(delta.Txids))
Expand All @@ -215,8 +221,6 @@ func (t *txTail) newBlock(blk bookkeeping.Block, delta ledgercore.StateDelta) {
}
encodedTail, tailHash := tail.Encode()

t.tailMu.Lock()
defer t.tailMu.Unlock()
t.recent[rnd] = roundLeases{
txleases: delta.Txleases,
proto: config.Consensus[blk.CurrentProtocol],
Expand All @@ -229,6 +233,9 @@ func (t *txTail) newBlock(blk bookkeeping.Block, delta ledgercore.StateDelta) {
}

func (t *txTail) committedUpTo(rnd basics.Round) (retRound, lookback basics.Round) {
t.tailMu.Lock()
defer t.tailMu.Unlock()

proto := t.recent[rnd].proto
maxlife := basics.Round(proto.MaxTxnLife)

Expand All @@ -246,8 +253,8 @@ func (t *txTail) committedUpTo(rnd basics.Round) (retRound, lookback basics.Roun
}

func (t *txTail) prepareCommit(dcc *deferredCommitContext) (err error) {
dcc.txTailDeltas = make([][]byte, 0, dcc.offset)
t.tailMu.RLock()
dcc.txTailDeltas = make([][]byte, 0, dcc.offset)
iansuvak marked this conversation as resolved.
Show resolved Hide resolved
for i := uint64(0); i < dcc.offset; i++ {
dcc.txTailDeltas = append(dcc.txTailDeltas, t.roundTailSerializedDeltas[i])
}
Expand All @@ -273,6 +280,9 @@ func (t *txTail) prepareCommit(dcc *deferredCommitContext) (err error) {
}

func (t *txTail) commitRound(ctx context.Context, tx trackerdb.TransactionScope, dcc *deferredCommitContext) error {
t.tailMu.Lock()
algonautshant marked this conversation as resolved.
Show resolved Hide resolved
defer t.tailMu.Unlock()

aw, err := tx.MakeAccountsWriter()
if err != nil {
return err
Expand Down Expand Up @@ -333,6 +343,9 @@ func (t errTxTailMissingRound) Error() string {
// checkDup test to see if the given transaction id/lease already exists. It returns nil if neither exists, or
// TransactionInLedgerError / LeaseInLedgerError respectively.
func (t *txTail) checkDup(proto config.ConsensusParams, current basics.Round, firstValid basics.Round, lastValid basics.Round, txid transactions.Txid, txl ledgercore.Txlease) error {
t.tailMu.RLock()
defer t.tailMu.RUnlock()

if lastValid < t.lowWaterMark {
return &errTxTailMissingRound{round: lastValid}
}
Expand Down Expand Up @@ -387,8 +400,5 @@ func (t *txTail) blockHeader(rnd basics.Round) (bookkeeping.BlockHeader, bool) {
t.tailMu.RLock()
defer t.tailMu.RUnlock()
hdr, ok := t.blockHeaderData[rnd]
if !ok {
iansuvak marked this conversation as resolved.
Show resolved Hide resolved
t.log.Warnf("txtail failed to fetch blockHeader from rnd: %d", rnd)
}
return hdr, ok
}
1 change: 1 addition & 0 deletions stateproof/abstractions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package stateproof

import (
"context"

"github.com/algorand/go-algorand/crypto"
"github.com/algorand/go-algorand/data/account"
"github.com/algorand/go-algorand/data/basics"
Expand Down