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

DVT-1057 replace block data structure with LRU cache to fix memory leak #148

Merged
merged 24 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
77 changes: 46 additions & 31 deletions cmd/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sync"
"time"

lru "github.com/hashicorp/golang-lru"
"github.com/maticnetwork/polygon-cli/util"

_ "embed"
Expand Down Expand Up @@ -41,9 +42,8 @@ type (
PeerCount uint64
GasPrice *big.Int
PendingCount uint64
BlockCache *lru.Cache

Blocks map[string]rpctypes.PolyBlock `json:"-"`
BlocksLock sync.RWMutex `json:"-"`
MaxBlockRetrieved *big.Int
MinBlockRetrieved *big.Int
}
Expand Down Expand Up @@ -91,31 +91,35 @@ func monitor(ctx context.Context) error {
ec := ethclient.NewClient(rpc)

ms := new(monitorStatus)
ms.BlockCache, _ = lru.New(1000)
ms.MaxBlockRetrieved = big.NewInt(0)
ms.BlocksLock.Lock()
ms.Blocks = make(map[string]rpctypes.PolyBlock, 0)
ms.BlocksLock.Unlock()

ms.ChainID = big.NewInt(0)
ms.PendingCount = 0
observedPendingTxs = make(historicalRange, 0)

isUiRendered := false
errChan := make(chan error)
go func() {
for {
err = fetchBlocks(ctx, ec, ms, rpc, isUiRendered)
if err != nil {
continue
}
select {
case <-ctx.Done(): // listens for a cancellation signal
leovct marked this conversation as resolved.
Show resolved Hide resolved
return // exit the goroutine when the context is done
default:
for {
err = fetchBlocks(ctx, ec, ms, rpc, isUiRendered)
if err != nil {
continue
}

if !isUiRendered {
go func() {
errChan <- renderMonitorUI(ctx, ec, ms, rpc)
}()
isUiRendered = true
}
if !isUiRendered {
go func() {
errChan <- renderMonitorUI(ctx, ec, ms, rpc)
}()
isUiRendered = true
}

time.Sleep(interval)
time.Sleep(interval)
}
}
}()

Expand Down Expand Up @@ -220,6 +224,8 @@ func appendOlderBlocks(ctx context.Context, ms *monitorStatus, rpc *ethrpc.Clien
return nil
}

const maxHistoricalPoints = 1000 // Set a limit to the number of historical points

func fetchBlocks(ctx context.Context, ec *ethclient.Client, ms *monitorStatus, rpc *ethrpc.Client, isUiRendered bool) (err error) {
var cs *chainState
cs, err = getChainState(ctx, ec)
Expand All @@ -228,6 +234,10 @@ func fetchBlocks(ctx context.Context, ec *ethclient.Client, ms *monitorStatus, r
time.Sleep(interval)
return err
}
if len(observedPendingTxs) >= maxHistoricalPoints {
// Remove the oldest data point
observedPendingTxs = observedPendingTxs[1:]
}
observedPendingTxs = append(observedPendingTxs, historicalDataPoint{SampleTime: time.Now(), SampleValue: float64(cs.PendingCount)})

log.Debug().Uint64("PeerCount", cs.PeerCount).Uint64("ChainID", cs.ChainID.Uint64()).Uint64("HeadBlock", cs.HeadBlock).Uint64("GasPrice", cs.GasPrice.Uint64()).Msg("Fetching blocks")
Expand Down Expand Up @@ -307,9 +317,7 @@ func (ms *monitorStatus) getBlockRange(ctx context.Context, from, to *big.Int, r
}
pb := rpctypes.NewPolyBlock(b.Result.(*rpctypes.RawBlockResponse))

ms.BlocksLock.Lock()
ms.Blocks[pb.Number().String()] = pb
ms.BlocksLock.Unlock()
ms.BlockCache.Add(pb.Number().String(), pb)
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 Add method checks if the cache is at its maximum capacity.
  • If the cache is full, it evicts the least recently used item before adding the new item.
  • If the cache is not full, it simply adds the new item to the cache.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe you could add this in the code as a comment also?


if ms.MaxBlockRetrieved.Cmp(pb.Number()) == -1 {
ms.MaxBlockRetrieved = pb.Number()
Expand Down Expand Up @@ -416,18 +424,24 @@ func setUISkeleton() (blockTable *widgets.List, grid *ui.Grid, blockGrid *ui.Gri
}

func updateAllBlocks(ms *monitorStatus) []rpctypes.PolyBlock {
// default
blocks := make([]rpctypes.PolyBlock, 0)

ms.BlocksLock.RLock()
for _, b := range ms.Blocks {
blocks = append(blocks, b)
var blocks []rpctypes.PolyBlock

// Retrieve all current items from the LRU cache.
// Since the cache has no inherent order, we will need to sort them if necessary.
for _, key := range ms.BlockCache.Keys() {
if value, ok := ms.BlockCache.Peek(key); ok {
block, ok := value.(rpctypes.PolyBlock)
if ok {
blocks = append(blocks, block)
}
}
}
ms.BlocksLock.RUnlock()

allBlocks := metrics.SortableBlocks(blocks)
// Assuming blocks need to be sorted, you'd sort them here.
// This assumes that metrics.SortableBlocks is a type that can be sorted.
sort.Sort(metrics.SortableBlocks(blocks))

return allBlocks
return blocks
}

func renderMonitorUI(ctx context.Context, ec *ethclient.Client, ms *monitorStatus, rpc *ethrpc.Client) error {
Expand Down Expand Up @@ -512,7 +526,8 @@ func renderMonitorUI(ctx context.Context, ec *ethclient.Client, ms *monitorStatu

currentBn := ms.HeadBlock
uiEvents := ui.PollEvents()
ticker := time.NewTicker(time.Second).C
ticker := time.NewTicker(time.Second)
defer ticker.Stop()

redraw(ms)

Expand Down Expand Up @@ -653,7 +668,7 @@ func renderMonitorUI(ctx context.Context, ec *ethclient.Client, ms *monitorStatu
if !forceRedraw {
redraw(ms)
}
case <-ticker:
case <-ticker.C:
if currentBn != ms.HeadBlock {
currentBn = ms.HeadBlock
redraw(ms)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/ethereum/go-ethereum v1.13.2
github.com/gizak/termui/v3 v3.1.0
github.com/google/gofuzz v1.2.0
github.com/hashicorp/golang-lru v1.0.2
github.com/jedib0t/go-pretty/v6 v6.4.8
github.com/libp2p/go-libp2p v0.31.0
github.com/oasisprotocol/curve25519-voi v0.0.0-20230904125328-1f23a7beb09a
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ github.com/hashicorp/go-bexpr v0.1.10/go.mod h1:oxlubA2vC/gFVfX1A6JGp7ls7uCDlfJn
github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hashicorp/golang-lru v1.0.2 h1:dV3g9Z/unq5DpblPpw+Oqcv4dU/1omnb4Ok8iPY6p1c=
github.com/hashicorp/golang-lru v1.0.2/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/holiman/billy v0.0.0-20230718173358-1c7e68d277a7 h1:3JQNjnMRil1yD0IfZKHF9GxxWKDJGj8I0IqOUol//sw=
Expand Down