From 4b12288f75ca5c2316cc710f5994ff25962799df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ecl=C3=A9sio=20J=C3=BAnior?= Date: Wed, 23 Jun 2021 18:19:41 -0400 Subject: [PATCH 01/10] chore: implement grandpa finality round --- lib/grandpa/grandpa.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/lib/grandpa/grandpa.go b/lib/grandpa/grandpa.go index dfc085fc0e..c6fcd3c9f6 100644 --- a/lib/grandpa/grandpa.go +++ b/lib/grandpa/grandpa.go @@ -26,14 +26,21 @@ 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" "github.com/ChainSafe/gossamer/lib/crypto/ed25519" + ethmetrics "github.com/ethereum/go-ethereum/metrics" + log "github.com/ChainSafe/log15" ) +const ( + finalityGrandpaRound = "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") @@ -197,6 +204,7 @@ func (s *Service) Start() error { }() go s.sendNeighbourMessage() + go s.collectGrandpaMetrics() return nil } @@ -231,6 +239,26 @@ func (s *Service) authorities() []*types.Authority { return ad } +func (s *Service) collectGrandpaMetrics() { + s.collect() + tc := time.NewTicker(metrics.Refresh) + defer tc.Stop() + for { + select { + case <-s.ctx.Done(): + return + case <-tc.C: + s.collect() + } + } +} + +func (s *Service) collect() { + ethmetrics.Enabled = true + finalityCounterMetric := ethmetrics.GetOrRegisterGauge(finalityGrandpaRound, nil) + finalityCounterMetric.Update(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() @@ -279,6 +307,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() } From 681cfafbd22ceaf5d2e5bf632799a65984232816 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ecl=C3=A9sio=20J=C3=BAnior?= Date: Thu, 24 Jun 2021 10:14:07 -0400 Subject: [PATCH 02/10] pool ready transaction metrics --- lib/transaction/pool.go | 26 +++++++++++++++- lib/transaction/pool_test.go | 52 +++++++++++++++++++++++++++++++ lib/transaction/priority_queue.go | 2 ++ 3 files changed, 79 insertions(+), 1 deletion(-) diff --git a/lib/transaction/pool.go b/lib/transaction/pool.go index 3d126740eb..857737fd6f 100644 --- a/lib/transaction/pool.go +++ b/lib/transaction/pool.go @@ -2,10 +2,15 @@ package transaction import ( "sync" + "time" "github.com/ChainSafe/gossamer/lib/common" + ethmetrics "github.com/ethereum/go-ethereum/metrics" ) +const collectTxMetricsTimeout = time.Second * 5 +const readyTransactionsMetrics = "gossamer/ready/transaction/metrics" + // Pool represents the transaction pool type Pool struct { transactions map[common.Hash]*ValidTransaction @@ -14,9 +19,12 @@ type Pool struct { // NewPool returns a new empty Pool func NewPool() *Pool { - return &Pool{ + p := &Pool{ transactions: make(map[common.Hash]*ValidTransaction), } + + go p.collectMetrics() + return p } // Transactions returns all the transactions in the pool @@ -49,3 +57,19 @@ func (p *Pool) Remove(hash common.Hash) { defer p.mu.Unlock() delete(p.transactions, hash) } + +func (p *Pool) collectMetrics() { + t := time.NewTicker(collectTxMetricsTimeout) + defer t.Stop() + + for range t.C { + p.collect() + } + +} + +func (p *Pool) collect() { + ethmetrics.Enabled = true + pooltx := ethmetrics.GetOrRegisterGauge(readyTransactionsMetrics, nil) + pooltx.Update(int64(len(p.transactions))) +} diff --git a/lib/transaction/pool_test.go b/lib/transaction/pool_test.go index 8abbe4219a..9af5354ce0 100644 --- a/lib/transaction/pool_test.go +++ b/lib/transaction/pool_test.go @@ -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) { @@ -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()) +} diff --git a/lib/transaction/priority_queue.go b/lib/transaction/priority_queue.go index f14207bd3f..810bec4f6c 100644 --- a/lib/transaction/priority_queue.go +++ b/lib/transaction/priority_queue.go @@ -25,6 +25,8 @@ import ( "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") From 5f62d09a032e6958cb0faa1b3fa9f98e3a4fbc81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ecl=C3=A9sio=20J=C3=BAnior?= Date: Thu, 24 Jun 2021 10:46:08 -0400 Subject: [PATCH 03/10] chore: add priority queue metrics --- dot/metrics/collector.go | 28 ++++++++++++++++++++++++++ lib/transaction/pool.go | 25 ++++++++--------------- lib/transaction/priority_queue.go | 10 +++++++++ lib/transaction/priority_queue_test.go | 19 +++++++++++++++++ 4 files changed, 65 insertions(+), 17 deletions(-) create mode 100644 dot/metrics/collector.go diff --git a/dot/metrics/collector.go b/dot/metrics/collector.go new file mode 100644 index 0000000000..85ba3186d2 --- /dev/null +++ b/dot/metrics/collector.go @@ -0,0 +1,28 @@ +package metrics + +import ( + "time" + + ethmetrics "github.com/ethereum/go-ethereum/metrics" +) + +type GaugeCollector interface { + Update() int64 +} + +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()) +} diff --git a/lib/transaction/pool.go b/lib/transaction/pool.go index 857737fd6f..3595949375 100644 --- a/lib/transaction/pool.go +++ b/lib/transaction/pool.go @@ -4,8 +4,8 @@ import ( "sync" "time" + "github.com/ChainSafe/gossamer/dot/metrics" "github.com/ChainSafe/gossamer/lib/common" - ethmetrics "github.com/ethereum/go-ethereum/metrics" ) const collectTxMetricsTimeout = time.Second * 5 @@ -23,7 +23,12 @@ func NewPool() *Pool { transactions: make(map[common.Hash]*ValidTransaction), } - go p.collectMetrics() + go metrics.CollectGaugeMetrics( + collectTxMetricsTimeout, + readyTransactionsMetrics, + p, + ) + return p } @@ -58,18 +63,4 @@ func (p *Pool) Remove(hash common.Hash) { delete(p.transactions, hash) } -func (p *Pool) collectMetrics() { - t := time.NewTicker(collectTxMetricsTimeout) - defer t.Stop() - - for range t.C { - p.collect() - } - -} - -func (p *Pool) collect() { - ethmetrics.Enabled = true - pooltx := ethmetrics.GetOrRegisterGauge(readyTransactionsMetrics, nil) - pooltx.Update(int64(len(p.transactions))) -} +func (p *Pool) Update() int64 { return int64(len(p.transactions)) } diff --git a/lib/transaction/priority_queue.go b/lib/transaction/priority_queue.go index 810bec4f6c..8e9c0fc0ff 100644 --- a/lib/transaction/priority_queue.go +++ b/lib/transaction/priority_queue.go @@ -21,6 +21,7 @@ import ( "errors" "sync" + "github.com/ChainSafe/gossamer/dot/metrics" "github.com/ChainSafe/gossamer/dot/types" "github.com/ChainSafe/gossamer/lib/common" ) @@ -96,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 } @@ -173,3 +181,5 @@ func (spq *PriorityQueue) Pending() []*ValidTransaction { } return txns } + +func (spq *PriorityQueue) Update() int64 { return int64(spq.pq.Len()) } diff --git a/lib/transaction/priority_queue_test.go b/lib/transaction/priority_queue_test.go index 3f9e4e72a0..d8a08eb3fb 100644 --- a/lib/transaction/priority_queue_test.go +++ b/lib/transaction/priority_queue_test.go @@ -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) { @@ -45,6 +49,13 @@ 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} @@ -52,6 +63,10 @@ func TestPriorityQueue(t *testing.T) { 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]) { @@ -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) { From 34b7c33f0732efdc3b3b097f691fc0fad270c191 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ecl=C3=A9sio=20J=C3=BAnior?= Date: Thu, 24 Jun 2021 11:21:59 -0400 Subject: [PATCH 04/10] chore: fix lint --- dot/metrics/collector.go | 4 ++++ lib/transaction/pool.go | 1 + lib/transaction/priority_queue.go | 1 + 3 files changed, 6 insertions(+) diff --git a/dot/metrics/collector.go b/dot/metrics/collector.go index 85ba3186d2..40fce98a6f 100644 --- a/dot/metrics/collector.go +++ b/dot/metrics/collector.go @@ -6,10 +6,14 @@ import ( 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() diff --git a/lib/transaction/pool.go b/lib/transaction/pool.go index 3595949375..78dacacd3f 100644 --- a/lib/transaction/pool.go +++ b/lib/transaction/pool.go @@ -63,4 +63,5 @@ func (p *Pool) Remove(hash common.Hash) { delete(p.transactions, hash) } +// Update returns the total of valid transactions in the pool func (p *Pool) Update() int64 { return int64(len(p.transactions)) } diff --git a/lib/transaction/priority_queue.go b/lib/transaction/priority_queue.go index 8e9c0fc0ff..d9de455f03 100644 --- a/lib/transaction/priority_queue.go +++ b/lib/transaction/priority_queue.go @@ -182,4 +182,5 @@ 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()) } From 23c4f5b208617a6d6237364f78229be515445d5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ecl=C3=A9sio=20J=C3=BAnior?= Date: Thu, 24 Jun 2021 15:40:24 -0400 Subject: [PATCH 05/10] chore: add gauge collector interface --- lib/grandpa/grandpa.go | 32 +++++++++----------------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git a/lib/grandpa/grandpa.go b/lib/grandpa/grandpa.go index c6fcd3c9f6..83069268a1 100644 --- a/lib/grandpa/grandpa.go +++ b/lib/grandpa/grandpa.go @@ -32,13 +32,11 @@ import ( "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/crypto/ed25519" - ethmetrics "github.com/ethereum/go-ethereum/metrics" - log "github.com/ChainSafe/log15" ) const ( - finalityGrandpaRound = "gossamer/finality/grandpa/round" + finalityGrandpaRoundMetrics = "gossamer/finality/grandpa/round" ) var ( @@ -204,7 +202,13 @@ func (s *Service) Start() error { }() go s.sendNeighbourMessage() - go s.collectGrandpaMetrics() + + go metrics.CollectGaugeMetrics( + metrics.Refresh, + finalityGrandpaRoundMetrics, + s, + ) + return nil } @@ -239,25 +243,7 @@ func (s *Service) authorities() []*types.Authority { return ad } -func (s *Service) collectGrandpaMetrics() { - s.collect() - tc := time.NewTicker(metrics.Refresh) - defer tc.Stop() - for { - select { - case <-s.ctx.Done(): - return - case <-tc.C: - s.collect() - } - } -} - -func (s *Service) collect() { - ethmetrics.Enabled = true - finalityCounterMetric := ethmetrics.GetOrRegisterGauge(finalityGrandpaRound, nil) - finalityCounterMetric.Update(int64(s.state.round)) -} +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 { From 89d7629c5dca20a021f08fab5b1c932a8a14342a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ecl=C3=A9sio=20J=C3=BAnior?= Date: Fri, 2 Jul 2021 11:22:00 -0400 Subject: [PATCH 06/10] chore: fix lint --- lib/grandpa/grandpa.go | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/grandpa/grandpa.go b/lib/grandpa/grandpa.go index 55d1f3e6c9..60f5d08611 100644 --- a/lib/grandpa/grandpa.go +++ b/lib/grandpa/grandpa.go @@ -235,6 +235,7 @@ func (s *Service) authorities() []*types.Authority { return ad } +// CollectGauge returns the map between metrics label and value func (s *Service) CollectGauge() map[string]int64 { s.roundLock.Lock() defer s.roundLock.Unlock() From a1ed8ca14b57c09d3a6ff25aa5ecc96ffc2853c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ecl=C3=A9sio=20J=C3=BAnior?= Date: Fri, 2 Jul 2021 11:35:55 -0400 Subject: [PATCH 07/10] remove unused metrics timeout --- lib/transaction/pool.go | 2 -- lib/transaction/pool_test.go | 5 +++-- lib/transaction/priority_queue_test.go | 5 +++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/transaction/pool.go b/lib/transaction/pool.go index d014017ace..d56bd975d0 100644 --- a/lib/transaction/pool.go +++ b/lib/transaction/pool.go @@ -18,12 +18,10 @@ package transaction import ( "sync" - "time" "github.com/ChainSafe/gossamer/lib/common" ) -const collectTxMetricsTimeout = time.Second * 5 const readyTransactionsMetrics = "gossamer/ready/transaction/metrics" // Pool represents the transaction pool diff --git a/lib/transaction/pool_test.go b/lib/transaction/pool_test.go index 9af5354ce0..2af2271d99 100644 --- a/lib/transaction/pool_test.go +++ b/lib/transaction/pool_test.go @@ -6,6 +6,7 @@ import ( "testing" "time" + "github.com/ChainSafe/gossamer/dot/metrics" "github.com/ChainSafe/gossamer/lib/common" "github.com/stretchr/testify/require" @@ -93,12 +94,12 @@ func TestPoolCollectMetrics(t *testing.T) { h[i] = p.Insert(v) } - time.Sleep(collectTxMetricsTimeout + time.Second) + time.Sleep(metrics.Refresh + time.Second) require.Equal(t, int64(len(validtx)), txmetrics.Value()) p.Remove(h[0]) - time.Sleep(collectTxMetricsTimeout + time.Second) + time.Sleep(metrics.Refresh + time.Second) fmt.Println(len(p.transactions)) require.Equal(t, int64(len(validtx)-1), txmetrics.Value()) } diff --git a/lib/transaction/priority_queue_test.go b/lib/transaction/priority_queue_test.go index d8a08eb3fb..72255e14dc 100644 --- a/lib/transaction/priority_queue_test.go +++ b/lib/transaction/priority_queue_test.go @@ -21,6 +21,7 @@ import ( "testing" "time" + "github.com/ChainSafe/gossamer/dot/metrics" ethmetrics "github.com/ethereum/go-ethereum/metrics" "github.com/stretchr/testify/require" ) @@ -64,7 +65,7 @@ func TestPriorityQueue(t *testing.T) { } // wait for metrics to be collected - time.Sleep(collectTxMetricsTimeout + time.Second) + time.Sleep(metrics.Refresh + time.Second) require.Equal(t, int64(len(tests)), priorityQueueM.Value()) for i, exp := range expected { @@ -77,7 +78,7 @@ func TestPriorityQueue(t *testing.T) { } // wait for metrics to be collected - time.Sleep(collectTxMetricsTimeout + time.Second) + time.Sleep(metrics.Refresh + time.Second) require.Equal(t, int64(pq.pq.Len()), priorityQueueM.Value()) } From 24ab066e9362fd0112695c812071ba1bcda00132 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ecl=C3=A9sio=20J=C3=BAnior?= Date: Mon, 5 Jul 2021 14:44:52 -0400 Subject: [PATCH 08/10] chore: remove unused test --- lib/transaction/pool_test.go | 53 -------------------------- lib/transaction/priority_queue_test.go | 20 ---------- 2 files changed, 73 deletions(-) diff --git a/lib/transaction/pool_test.go b/lib/transaction/pool_test.go index 2af2271d99..8abbe4219a 100644 --- a/lib/transaction/pool_test.go +++ b/lib/transaction/pool_test.go @@ -1,16 +1,11 @@ package transaction import ( - "fmt" "sort" "testing" - "time" - "github.com/ChainSafe/gossamer/dot/metrics" "github.com/ChainSafe/gossamer/lib/common" "github.com/stretchr/testify/require" - - ethmetrics "github.com/ethereum/go-ethereum/metrics" ) func TestPool(t *testing.T) { @@ -55,51 +50,3 @@ 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(metrics.Refresh + time.Second) - require.Equal(t, int64(len(validtx)), txmetrics.Value()) - - p.Remove(h[0]) - - time.Sleep(metrics.Refresh + time.Second) - fmt.Println(len(p.transactions)) - require.Equal(t, int64(len(validtx)-1), txmetrics.Value()) -} diff --git a/lib/transaction/priority_queue_test.go b/lib/transaction/priority_queue_test.go index 72255e14dc..3f9e4e72a0 100644 --- a/lib/transaction/priority_queue_test.go +++ b/lib/transaction/priority_queue_test.go @@ -19,11 +19,6 @@ package transaction import ( "reflect" "testing" - "time" - - "github.com/ChainSafe/gossamer/dot/metrics" - ethmetrics "github.com/ethereum/go-ethereum/metrics" - "github.com/stretchr/testify/require" ) func TestPriorityQueue(t *testing.T) { @@ -50,13 +45,6 @@ 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} @@ -64,10 +52,6 @@ func TestPriorityQueue(t *testing.T) { pq.Push(node) } - // wait for metrics to be collected - time.Sleep(metrics.Refresh + time.Second) - require.Equal(t, int64(len(tests)), priorityQueueM.Value()) - for i, exp := range expected { n := pq.Pop() if !reflect.DeepEqual(n, tests[exp]) { @@ -76,10 +60,6 @@ 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(metrics.Refresh + time.Second) - require.Equal(t, int64(pq.pq.Len()), priorityQueueM.Value()) } func TestPriorityQueueAgain(t *testing.T) { From 628f3db96e343ca5ef751ac7b45c52728af7b47c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ecl=C3=A9sio=20J=C3=BAnior?= Date: Mon, 5 Jul 2021 14:57:49 -0400 Subject: [PATCH 09/10] remove unused consts --- lib/transaction/pool.go | 2 -- lib/transaction/priority_queue.go | 2 -- 2 files changed, 4 deletions(-) diff --git a/lib/transaction/pool.go b/lib/transaction/pool.go index d56bd975d0..a66bf28aca 100644 --- a/lib/transaction/pool.go +++ b/lib/transaction/pool.go @@ -22,8 +22,6 @@ import ( "github.com/ChainSafe/gossamer/lib/common" ) -const readyTransactionsMetrics = "gossamer/ready/transaction/metrics" - // Pool represents the transaction pool type Pool struct { transactions map[common.Hash]*ValidTransaction diff --git a/lib/transaction/priority_queue.go b/lib/transaction/priority_queue.go index 12767f73c3..538d72bc17 100644 --- a/lib/transaction/priority_queue.go +++ b/lib/transaction/priority_queue.go @@ -25,8 +25,6 @@ import ( "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") From 2b3436ad2c9aa2eb543bcca234d550b833f5c0fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ecl=C3=A9sio=20J=C3=BAnior?= Date: Tue, 6 Jul 2021 08:50:33 -0400 Subject: [PATCH 10/10] chore: adding tests --- dot/metrics/collector.go | 4 ++-- go.mod | 2 ++ go.sum | 9 +++++++-- lib/grandpa/grandpa_test.go | 21 +++++++++++++++++++++ lib/transaction/pool.go | 4 +--- 5 files changed, 33 insertions(+), 7 deletions(-) diff --git a/dot/metrics/collector.go b/dot/metrics/collector.go index eec1a93254..82f17e2336 100644 --- a/dot/metrics/collector.go +++ b/dot/metrics/collector.go @@ -80,8 +80,8 @@ func (c *Collector) startCollectGauges() { m := g.CollectGauge() for label, value := range m { - pooltx := ethmetrics.GetOrRegisterGauge(label, nil) - pooltx.Update(value) + gauge := ethmetrics.GetOrRegisterGauge(label, nil) + gauge.Update(value) } } } diff --git a/go.mod b/go.mod index 579279b921..a3f36cadeb 100644 --- a/go.mod +++ b/go.mod @@ -39,7 +39,9 @@ require ( github.com/nanobox-io/golang-scribble v0.0.0-20190309225732-aa3e7c118975 github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416 github.com/perlin-network/life v0.0.0-20191203030451-05c0e0f7eaea + github.com/shirou/gopsutil v3.21.6+incompatible // indirect github.com/stretchr/testify v1.7.0 + github.com/tklauser/go-sysconf v0.3.6 // indirect github.com/urfave/cli v1.22.1 github.com/wasmerio/go-ext-wasm v0.3.2-0.20200326095750-0a32be6068ec golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 diff --git a/go.sum b/go.sum index a083d43080..69581e72c2 100644 --- a/go.sum +++ b/go.sum @@ -273,7 +273,6 @@ github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfb github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y= github.com/golang/mock v1.4.0/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw= -github.com/golang/mock v1.4.4 h1:l75CXGRSwbaYNpl/Z2X1XIIAMSCquvXgpVZDhwEIJsc= github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.0/go.mod h1:Qd/q+1AKNOZr9uGQzbzCmRO6sUih6GTPZv6a1/R87v0= @@ -940,8 +939,9 @@ github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg github.com/segmentio/kafka-go v0.1.0/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfPOCvTvk+EJo= github.com/segmentio/kafka-go v0.2.0/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfPOCvTvk+EJo= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= -github.com/shirou/gopsutil v2.20.5+incompatible h1:tYH07UPoQt0OCQdgWWMgYHy3/a9bcxNpBIysykNIP7I= github.com/shirou/gopsutil v2.20.5+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= +github.com/shirou/gopsutil v3.21.6+incompatible h1:mmZtAlWSd8U2HeRTjswbnDLPxqsEoK01NK+GZ1P+nEM= +github.com/shirou/gopsutil v3.21.6+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= github.com/shurcooL/component v0.0.0-20170202220835-f88ec8f54cc4/go.mod h1:XhFIlyj5a1fBNx5aJTbKoIq0mNaPvOagO+HjB3EtxrY= github.com/shurcooL/events v0.0.0-20181021180414-410e4ca65f48/go.mod h1:5u70Mqkb5O5cxEA8nxTsgrgLehJeAw6Oc4Ab1c/P1HM= github.com/shurcooL/github_flavored_markdown v0.0.0-20181002035957-2122de532470/go.mod h1:2dOwnU2uBioM+SGy2aZoq1f/Sd1l9OkAeAUvjSyvgU0= @@ -1011,6 +1011,10 @@ github.com/syndtr/goleveldb v1.0.1-0.20210305035536-64b5b1c73954 h1:xQdMZ1WLrgkk github.com/syndtr/goleveldb v1.0.1-0.20210305035536-64b5b1c73954/go.mod h1:u2MKkTVTVJWe5D1rCvame8WqhBd88EuIwODJZ1VHCPM= github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA= github.com/tinylib/msgp v1.0.2/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE= +github.com/tklauser/go-sysconf v0.3.6 h1:oc1sJWvKkmvIxhDHeKWvZS4f6AW+YcoguSfRF2/Hmo4= +github.com/tklauser/go-sysconf v0.3.6/go.mod h1:MkWzOF4RMCshBAMXuhXJs64Rte09mITnppBXY/rYEFI= +github.com/tklauser/numcpus v0.2.2 h1:oyhllyrScuYI6g+h/zUvNXNp1wy7x8qQy3t/piefldA= +github.com/tklauser/numcpus v0.2.2/go.mod h1:x3qojaO3uyYt0i56EW/VUYs7uBvdl2fkfZFu0T9wgjM= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce h1:fb190+cK2Xz/dvi9Hv8eCYJYvIGUTN2/KLq1pT6CjEc= github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce/go.mod h1:o8v6yHRoik09Xen7gje4m9ERNah1d1PPsVq1VEx9vE4= @@ -1242,6 +1246,7 @@ golang.org/x/sys v0.0.0-20201221093633-bc327ba9c2f0/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210105210732-16f7687f5001/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210303074136-134d130e1a04/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210316164454-77fc1eacc6aa/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210415045647-66c3f260301c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210426080607-c94f62235c83/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/lib/grandpa/grandpa_test.go b/lib/grandpa/grandpa_test.go index 42dce94604..6485fbfc9c 100644 --- a/lib/grandpa/grandpa_test.go +++ b/lib/grandpa/grandpa_test.go @@ -17,6 +17,7 @@ package grandpa import ( + "context" "io/ioutil" "math/big" "math/rand" @@ -24,6 +25,7 @@ import ( "testing" "time" + "github.com/ChainSafe/gossamer/dot/metrics" "github.com/ChainSafe/gossamer/dot/state" "github.com/ChainSafe/gossamer/dot/types" "github.com/ChainSafe/gossamer/lib/common" @@ -35,6 +37,8 @@ import ( "github.com/stretchr/testify/require" . "github.com/ChainSafe/gossamer/lib/grandpa/mocks" + + ethmetrics "github.com/ethereum/go-ethereum/metrics" ) // testGenesisHeader is a test block header @@ -1115,3 +1119,20 @@ func TestGrandpa_NonAuthority(t *testing.T) { require.Equal(t, uint64(2), gs.state.round) require.Equal(t, uint64(0), gs.state.setID) } +func TestFinalRoundGaugeMetric(t *testing.T) { + gs, _ := newTestService(t) + ethmetrics.Enabled = true + + gs.state.round = uint64(180) + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + coll := metrics.NewCollector(ctx) + coll.AddGauge(gs) + + go coll.Start() + + time.Sleep(metrics.Refresh + time.Second) + gauge := ethmetrics.GetOrRegisterGauge(finalityGrandpaRoundMetrics, nil) + require.Equal(t, gauge.Value(), int64(180)) +} diff --git a/lib/transaction/pool.go b/lib/transaction/pool.go index a66bf28aca..1f9c3f8944 100644 --- a/lib/transaction/pool.go +++ b/lib/transaction/pool.go @@ -30,11 +30,9 @@ type Pool struct { // NewPool returns a new empty Pool func NewPool() *Pool { - p := &Pool{ + return &Pool{ transactions: make(map[common.Hash]*ValidTransaction), } - - return p } // Transactions returns all the transactions in the pool