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

feat(lib/grandpa) implement grandpa finality round metrics #1655

Merged
merged 20 commits into from
Jul 6, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
4b12288
chore: implement grandpa finality round
EclesioMeloJunior Jun 23, 2021
ca68868
Merge branch 'development' into eclesio/grandpa-final-round-metrics
EclesioMeloJunior Jun 24, 2021
681cfaf
pool ready transaction metrics
EclesioMeloJunior Jun 24, 2021
5f62d09
chore: add priority queue metrics
EclesioMeloJunior Jun 24, 2021
365b8d7
Merge branch 'development' into eclesio/ready-txs-metrics
EclesioMeloJunior Jun 24, 2021
34b7c33
chore: fix lint
EclesioMeloJunior Jun 24, 2021
58b9e99
Merge branch 'eclesio/ready-txs-metrics' into eclesio/grandpa-final-r…
EclesioMeloJunior Jun 24, 2021
23c4f5b
chore: add gauge collector interface
EclesioMeloJunior Jun 24, 2021
fb7e3b2
Merge branch 'development' into eclesio/grandpa-final-round-metrics
EclesioMeloJunior Jun 29, 2021
c5c8076
Merge branch 'development' into eclesio/grandpa-final-round-metrics
EclesioMeloJunior Jun 30, 2021
08d29c6
chore: solve conflicts
EclesioMeloJunior Jul 2, 2021
1767fb4
Merge branch 'eclesio/grandpa-final-round-metrics' of github.com:Chai…
EclesioMeloJunior Jul 2, 2021
89d7629
chore: fix lint
EclesioMeloJunior Jul 2, 2021
a1ed8ca
remove unused metrics timeout
EclesioMeloJunior Jul 2, 2021
2b35f99
Merge branch 'development' into eclesio/grandpa-final-round-metrics
EclesioMeloJunior Jul 5, 2021
24ab066
chore: remove unused test
EclesioMeloJunior Jul 5, 2021
628f3db
remove unused consts
EclesioMeloJunior Jul 5, 2021
55d3368
Merge branch 'development' into eclesio/grandpa-final-round-metrics
EclesioMeloJunior Jul 5, 2021
2b3436a
chore: adding tests
EclesioMeloJunior Jul 6, 2021
b07413d
Merge branch 'development' into eclesio/grandpa-final-round-metrics
EclesioMeloJunior Jul 6, 2021
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
32 changes: 32 additions & 0 deletions dot/metrics/collector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package metrics

import (
"time"

ethmetrics "github.com/ethereum/go-ethereum/metrics"
)

// GaugeCollector abstracts the Update function to collectors
// just implement the collection
type GaugeCollector interface {
Update() int64
}

// CollectGaugeMetrics receives an timeout, label and a gauge collector
// and acquire the metrics timeout by timeout to a ethereum metrics gauge
func CollectGaugeMetrics(timeout time.Duration, label string, c GaugeCollector) {
t := time.NewTicker(timeout)
defer t.Stop()

collectGauge(label, c)

for range t.C {
collectGauge(label, c)
}
}

func collectGauge(label string, c GaugeCollector) {
ethmetrics.Enabled = true
pooltx := ethmetrics.GetOrRegisterGauge(label, nil)
pooltx.Update(c.Update())
}
15 changes: 15 additions & 0 deletions lib/grandpa/grandpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"sync/atomic"
"time"

"github.com/ChainSafe/gossamer/dot/metrics"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/blocktree"
"github.com/ChainSafe/gossamer/lib/common"
Expand All @@ -34,6 +35,10 @@ import (
log "github.com/ChainSafe/log15"
)

const (
finalityGrandpaRoundMetrics = "gossamer/finality/grandpa/round"
)

var (
interval = time.Second // TODO: make this configurable; currently 1s is same as substrate; total round length is then 2s
logger = log.New("pkg", "grandpa")
Expand Down Expand Up @@ -197,6 +202,13 @@ func (s *Service) Start() error {
}()

go s.sendNeighbourMessage()

go metrics.CollectGaugeMetrics(
metrics.Refresh,
finalityGrandpaRoundMetrics,
s,
)

return nil
}

Expand Down Expand Up @@ -231,6 +243,8 @@ func (s *Service) authorities() []*types.Authority {
return ad
}

func (s *Service) Update() int64 { return int64(s.state.round) }

// updateAuthorities updates the grandpa voter set, increments the setID, and resets the round numbers
func (s *Service) updateAuthorities() error {
currSetID, err := s.grandpaState.GetCurrentSetID()
Expand Down Expand Up @@ -279,6 +293,7 @@ func (s *Service) initiate() error {
s.roundLock.Lock()
s.state.round++
logger.Trace("incrementing grandpa round", "next round", s.state.round)

if s.tracker != nil {
s.tracker.stop()
}
Expand Down
18 changes: 17 additions & 1 deletion lib/transaction/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ package transaction

import (
"sync"
"time"

"github.com/ChainSafe/gossamer/dot/metrics"
"github.com/ChainSafe/gossamer/lib/common"
)

const collectTxMetricsTimeout = time.Second * 5
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be nice to have this defined in the NewPool constructor rather than package level const. You could even create a private newPool that takes in configurable durations for this so you can adjust them in the tests.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've removed this and used the metrics default value

const readyTransactionsMetrics = "gossamer/ready/transaction/metrics"

// Pool represents the transaction pool
type Pool struct {
transactions map[common.Hash]*ValidTransaction
Expand All @@ -14,9 +19,17 @@ type Pool struct {

// NewPool returns a new empty Pool
func NewPool() *Pool {
return &Pool{
p := &Pool{
transactions: make(map[common.Hash]*ValidTransaction),
}

go metrics.CollectGaugeMetrics(
collectTxMetricsTimeout,
readyTransactionsMetrics,
p,
)

return p
}

// Transactions returns all the transactions in the pool
Expand Down Expand Up @@ -49,3 +62,6 @@ func (p *Pool) Remove(hash common.Hash) {
defer p.mu.Unlock()
delete(p.transactions, hash)
}

// Update returns the total of valid transactions in the pool
func (p *Pool) Update() int64 { return int64(len(p.transactions)) }
52 changes: 52 additions & 0 deletions lib/transaction/pool_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package transaction

import (
"fmt"
"sort"
"testing"
"time"

"github.com/ChainSafe/gossamer/lib/common"
"github.com/stretchr/testify/require"

ethmetrics "github.com/ethereum/go-ethereum/metrics"
)

func TestPool(t *testing.T) {
Expand Down Expand Up @@ -50,3 +54,51 @@ func TestPool(t *testing.T) {
}
require.Equal(t, 0, len(p.Transactions()))
}

func TestPoolCollectMetrics(t *testing.T) {
//reset metric
ethmetrics.Unregister(readyTransactionsMetrics)

ethmetrics.Enabled = true
txmetrics := ethmetrics.GetOrRegisterGauge(readyTransactionsMetrics, nil)

require.Equal(t, int64(0), txmetrics.Value())

validtx := []*ValidTransaction{
{
Extrinsic: []byte("a"),
Validity: &Validity{Priority: 1},
},
{
Extrinsic: []byte("b"),
Validity: &Validity{Priority: 4},
},
{
Extrinsic: []byte("c"),
Validity: &Validity{Priority: 2},
},
{
Extrinsic: []byte("d"),
Validity: &Validity{Priority: 17},
},
{
Extrinsic: []byte("e"),
Validity: &Validity{Priority: 2},
},
}

h := make([]common.Hash, len(validtx))
p := NewPool()
for i, v := range validtx {
h[i] = p.Insert(v)
}

time.Sleep(collectTxMetricsTimeout + time.Second)
require.Equal(t, int64(len(validtx)), txmetrics.Value())

p.Remove(h[0])

time.Sleep(collectTxMetricsTimeout + time.Second)
fmt.Println(len(p.transactions))
require.Equal(t, int64(len(validtx)-1), txmetrics.Value())
}
13 changes: 13 additions & 0 deletions lib/transaction/priority_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ import (
"errors"
"sync"

"github.com/ChainSafe/gossamer/dot/metrics"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
)

const readyPriorityQueueTransactions = "gossamer/ready/transaction/metrics"

// ErrTransactionExists is returned when trying to add a transaction to the queue that already exists
var ErrTransactionExists = errors.New("transaction is already in queue")

Expand Down Expand Up @@ -94,6 +97,13 @@ func NewPriorityQueue() *PriorityQueue {
pq: make(priorityQueue, 0),
txs: make(map[common.Hash]*Item),
}

go metrics.CollectGaugeMetrics(
collectTxMetricsTimeout,
readyPriorityQueueTransactions,
spq,
)

heap.Init(&spq.pq)
return spq
}
Expand Down Expand Up @@ -171,3 +181,6 @@ func (spq *PriorityQueue) Pending() []*ValidTransaction {
}
return txns
}

// Update returns the total of valid transactions in the priority queue
func (spq *PriorityQueue) Update() int64 { return int64(spq.pq.Len()) }
19 changes: 19 additions & 0 deletions lib/transaction/priority_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ package transaction
import (
"reflect"
"testing"
"time"

ethmetrics "github.com/ethereum/go-ethereum/metrics"
"github.com/stretchr/testify/require"
)

func TestPriorityQueue(t *testing.T) {
Expand All @@ -45,13 +49,24 @@ func TestPriorityQueue(t *testing.T) {
},
}

//check metrics
ethmetrics.Unregister(readyPriorityQueueTransactions)
ethmetrics.Enabled = true

priorityQueueM := ethmetrics.GetOrRegisterGauge(readyPriorityQueueTransactions, nil)
require.Equal(t, int64(0), priorityQueueM.Value())

pq := NewPriorityQueue()
expected := []int{3, 1, 2, 4, 0}

for _, node := range tests {
pq.Push(node)
}

// wait for metrics to be collected
time.Sleep(collectTxMetricsTimeout + time.Second)
require.Equal(t, int64(len(tests)), priorityQueueM.Value())

for i, exp := range expected {
n := pq.Pop()
if !reflect.DeepEqual(n, tests[exp]) {
Expand All @@ -60,6 +75,10 @@ func TestPriorityQueue(t *testing.T) {
t.Fatalf("Fail: iteration %d got %v expected %v", i, n, tests[exp])
}
}

// wait for metrics to be collected
time.Sleep(collectTxMetricsTimeout + time.Second)
require.Equal(t, int64(pq.pq.Len()), priorityQueueM.Value())
}

func TestPriorityQueueAgain(t *testing.T) {
Expand Down