diff --git a/go.mod b/go.mod index 05e5ac2e614..d21b2eff73a 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( code.cloudfoundry.org/clock v1.0.0 github.com/IBM/idemix v0.0.0-20220112103229-701e7610d405 github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible - github.com/SmartBFT-Go/consensus v0.0.0-20230717102940-071648f76967 + github.com/SmartBFT-Go/consensus v0.0.0-20230907130931-31f018a899a6 github.com/VictoriaMetrics/fastcache v1.9.0 github.com/bits-and-blooms/bitset v1.2.1 github.com/cheggaaa/pb v1.0.29 diff --git a/go.sum b/go.sum index c7a3c78c7da..91e679acc56 100644 --- a/go.sum +++ b/go.sum @@ -56,8 +56,8 @@ github.com/Microsoft/hcsshim v0.8.25/go.mod h1:4zegtUJth7lAvFyc6cH2gGQ5B3OFQim01 github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo= github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI= -github.com/SmartBFT-Go/consensus v0.0.0-20230717102940-071648f76967 h1:YYRetq5PWdDYxeKL46VHk6qUe2Gxswhc3sw0HeYXz5s= -github.com/SmartBFT-Go/consensus v0.0.0-20230717102940-071648f76967/go.mod h1:KsqqnNjG+MiuEdm/9ufh5C0oKJqd1rkpQ5S2mYPvVcA= +github.com/SmartBFT-Go/consensus v0.0.0-20230907130931-31f018a899a6 h1:yfGSoZgsSZG1sygPBLz/KNk2rKGKOnd4+1LFMt7KXf0= +github.com/SmartBFT-Go/consensus v0.0.0-20230907130931-31f018a899a6/go.mod h1:KsqqnNjG+MiuEdm/9ufh5C0oKJqd1rkpQ5S2mYPvVcA= github.com/VictoriaMetrics/fastcache v1.9.0 h1:oMwsS6c8abz98B7ytAewQ7M1ZN/Im/iwKoE1euaFvhs= github.com/VictoriaMetrics/fastcache v1.9.0/go.mod h1:otoTS3xu+6IzF/qByjqzjp3rTuzM3Qf0ScU1UTj97iU= github.com/VividCortex/gohistogram v1.0.0 h1:6+hBz+qvs0JOrrNhhmR7lFxo5sINxBCGXrdtl/UvroE= diff --git a/orderer/consensus/smartbft/metrics_provider_converter.go b/orderer/consensus/smartbft/metrics_provider_converter.go index 6911551c265..4517f808f82 100644 --- a/orderer/consensus/smartbft/metrics_provider_converter.go +++ b/orderer/consensus/smartbft/metrics_provider_converter.go @@ -7,7 +7,7 @@ SPDX-License-Identifier: Apache-2.0 package smartbft import ( - "github.com/SmartBFT-Go/consensus/pkg/api" + api "github.com/SmartBFT-Go/consensus/pkg/metrics" "github.com/hyperledger/fabric/common/metrics" ) diff --git a/vendor/github.com/SmartBFT-Go/consensus/internal/bft/controller.go b/vendor/github.com/SmartBFT-Go/consensus/internal/bft/controller.go index b3af84d06a0..bbef1d91ed1 100644 --- a/vendor/github.com/SmartBFT-Go/consensus/internal/bft/controller.go +++ b/vendor/github.com/SmartBFT-Go/consensus/internal/bft/controller.go @@ -81,7 +81,7 @@ type Proposer interface { // //go:generate mockery -dir . -name ProposerBuilder -case underscore -output ./mocks/ type ProposerBuilder interface { - NewProposer(leader, proposalSequence, viewNum, decisionsInView uint64, quorumSize int) Proposer + NewProposer(leader, proposalSequence, viewNum, decisionsInView uint64, quorumSize int) (Proposer, Phase) } // Controller controls the entire flow of the consensus @@ -373,7 +373,7 @@ func (c *Controller) convertViewMessageToHeartbeat(m *protos.Message) *protos.Me } func (c *Controller) startView(proposalSequence uint64) { - view := c.ProposerBuilder.NewProposer(c.leaderID(), proposalSequence, c.currViewNumber, c.currDecisionsInView, c.quorum) + view, initPhase := c.ProposerBuilder.NewProposer(c.leaderID(), proposalSequence, c.currViewNumber, c.currDecisionsInView, c.quorum) c.currViewLock.Lock() c.currView = view @@ -383,6 +383,12 @@ func (c *Controller) startView(proposalSequence uint64) { role := Follower leader, _ := c.iAmTheLeader() if leader { + if initPhase == COMMITTED || initPhase == ABORT { + c.Logger.Debugf("Acquiring leader token when starting view with phase %s", initPhase.String()) + c.acquireLeaderToken() + } else { + c.Logger.Debugf("Not acquiring leader token when starting view with phase %s", initPhase.String()) + } role = Leader } c.LeaderMonitor.ChangeRole(role, c.currViewNumber, c.leaderID()) @@ -414,10 +420,8 @@ func (c *Controller) changeView(newViewNumber uint64, newProposalSequence uint64 c.Logger.Debugf("Starting view after setting decisions in view to %d", newDecisionsInView) c.startView(newProposalSequence) - // If I'm the leader, I can claim the leader token. if iAm, _ := c.iAmTheLeader(); iAm { c.Batcher.Reset() - c.acquireLeaderToken() } } @@ -468,24 +472,13 @@ func (c *Controller) ViewChanged(newViewNumber uint64, newProposalSequence uint6 c.viewChange <- viewInfo{proposalSeq: newProposalSequence, viewNumber: newViewNumber} } -func (c *Controller) getNextBatch() [][]byte { - var validRequests [][]byte - for len(validRequests) == 0 { // no valid requests in this batch - requests := c.Batcher.NextBatch() - if c.stopped() || c.Batcher.Closed() { - return nil - } - validRequests = append(validRequests, requests...) - } - return validRequests -} - func (c *Controller) propose() { - nextBatch := c.getNextBatch() - if len(nextBatch) == 0 { - // If our next batch is empty, - // it can only be because - // the batcher is stopped and so are we. + if c.stopped() || c.Batcher.Closed() { + return + } + nextBatch := c.Batcher.NextBatch() + if len(nextBatch) == 0 { // no requests in this batch + c.acquireLeaderToken() // try again later return } metadata := c.currView.GetMetadata() @@ -632,6 +625,28 @@ func (c *Controller) sync() (viewNum uint64, seq uint64, decisions uint64) { if md.ViewId < c.currViewNumber { c.Logger.Infof("Synchronizer returned with view number %d but the controller is at view number %d", md.ViewId, c.currViewNumber) + response := c.fetchState() + if response == nil { + c.Logger.Infof("Fetching state failed") + return 0, 0, 0 + } + if response.View > c.currViewNumber && response.Seq == md.LatestSequence+1 { + c.Logger.Infof("Collected state with view %d and sequence %d", response.View, response.Seq) + newViewToSave := &protos.SavedMessage{ + Content: &protos.SavedMessage_NewView{ + NewView: &protos.ViewMetadata{ + ViewId: response.View, + LatestSequence: md.LatestSequence, + DecisionsInView: 0, + }, + }, + } + if err := c.State.Save(newViewToSave); err != nil { + c.Logger.Panicf("Failed to save message to state, error: %v", err) + } + c.ViewChanger.InformNewView(response.View) + return response.View, response.Seq, 0 + } return 0, 0, 0 } @@ -800,9 +815,6 @@ func (c *Controller) Start(startViewNumber uint64, startProposalSequence uint64, c.currViewNumber = startViewNumber c.currDecisionsInView = startDecisionsInView c.startView(startProposalSequence) - if iAm, _ := c.iAmTheLeader(); iAm { - c.acquireLeaderToken() - } go func() { defer c.controllerDone.Done() diff --git a/vendor/github.com/SmartBFT-Go/consensus/internal/bft/requestpool.go b/vendor/github.com/SmartBFT-Go/consensus/internal/bft/requestpool.go index 2ab71e84b0d..eda967bf03d 100644 --- a/vendor/github.com/SmartBFT-Go/consensus/internal/bft/requestpool.go +++ b/vendor/github.com/SmartBFT-Go/consensus/internal/bft/requestpool.go @@ -188,7 +188,7 @@ func (rp *Pool) Submit(request []byte) error { if uint64(len(request)) > rp.options.RequestMaxBytes { rp.metrics.CountOfFailAddRequestToPool.With( - rp.metrics.LabelsForWith(api.NameReasonFailAdd, api.ReasonRequestMaxBytes)..., + rp.metrics.LabelsForWith("reason", api.ReasonRequestMaxBytes)..., ).Add(1) return fmt.Errorf( "submitted request (%d) is bigger than request max bytes (%d)", @@ -217,7 +217,7 @@ func (rp *Pool) Submit(request []byte) error { // do not wait for a semaphore with a lock, as it will prevent draining the pool. if err := rp.semaphore.Acquire(ctx, 1); err != nil { rp.metrics.CountOfFailAddRequestToPool.With( - rp.metrics.LabelsForWith(api.NameReasonFailAdd, api.ReasonSemaphoreAcquireFail)..., + rp.metrics.LabelsForWith("reason", api.ReasonSemaphoreAcquireFail)..., ).Add(1) return errors.Wrapf(err, "acquiring semaphore for request: %s", reqInfo) } diff --git a/vendor/github.com/SmartBFT-Go/consensus/internal/bft/util.go b/vendor/github.com/SmartBFT-Go/consensus/internal/bft/util.go index ebe8adf1fed..53ffeaf8dc4 100644 --- a/vendor/github.com/SmartBFT-Go/consensus/internal/bft/util.go +++ b/vendor/github.com/SmartBFT-Go/consensus/internal/bft/util.go @@ -276,7 +276,7 @@ type ProposalMaker struct { } // NewProposer returns a new view -func (pm *ProposalMaker) NewProposer(leader, proposalSequence, viewNum, decisionsInView uint64, quorumSize int) Proposer { +func (pm *ProposalMaker) NewProposer(leader, proposalSequence, viewNum, decisionsInView uint64, quorumSize int) (proposer Proposer, phase Phase) { view := &View{ RetrieveCheckpoint: pm.Checkpoint.Get, DecisionsPerLeader: pm.DecisionsPerLeader, @@ -330,7 +330,7 @@ func (pm *ProposalMaker) NewProposer(leader, proposalSequence, viewNum, decision view.MetricsView.DecisionsInView.Set(float64(view.DecisionsInView)) view.MetricsView.Phase.Set(float64(view.Phase)) - return view + return view, view.Phase } // ViewSequence indicates if a view is currently active and its current proposal sequence @@ -486,7 +486,7 @@ func (bl blacklist) computeUpdate() []uint64 { for _, node := range bl.nodes { inBlacklist := newBlacklistMap[node] bl.metricsBlacklist.NodesInBlackList.With( - bl.metricsBlacklist.LabelsForWith(api.NameBlackListNodeID, strconv.FormatUint(node, 10))..., + bl.metricsBlacklist.LabelsForWith("blackid", strconv.FormatUint(node, 10))..., ).Set(btoi(inBlacklist)) } bl.metricsBlacklist.CountBlackList.Set(float64(len(newBlacklist))) diff --git a/vendor/github.com/SmartBFT-Go/consensus/internal/bft/view.go b/vendor/github.com/SmartBFT-Go/consensus/internal/bft/view.go index 12ec2d30b89..f543024cf5c 100644 --- a/vendor/github.com/SmartBFT-Go/consensus/internal/bft/view.go +++ b/vendor/github.com/SmartBFT-Go/consensus/internal/bft/view.go @@ -30,6 +30,21 @@ const ( ABORT ) +func (p Phase) String() string { + switch p { + case COMMITTED: + return "COMMITTED" + case PROPOSED: + return "PROPOSED" + case PREPARED: + return "PREPARED" + case ABORT: + return "ABORT" + default: + return "Invalid Phase" + } +} + // State can save and restore the state // //go:generate mockery -dir . -name State -case underscore -output ./mocks/ @@ -885,6 +900,8 @@ func (v *View) GetMetadata() []byte { DecisionsInView: v.DecisionsInView, } + v.Logger.Debugf("GetMetadata with view %d, seq %d, dec %d", metadata.ViewId, metadata.LatestSequence, metadata.DecisionsInView) + var ( prevSigs []*protos.Signature prevProp *protos.Proposal diff --git a/vendor/github.com/SmartBFT-Go/consensus/pkg/api/metrics.go b/vendor/github.com/SmartBFT-Go/consensus/pkg/api/metrics.go index 2c25f61b172..7b7585b0487 100644 --- a/vendor/github.com/SmartBFT-Go/consensus/pkg/api/metrics.go +++ b/vendor/github.com/SmartBFT-Go/consensus/pkg/api/metrics.go @@ -1,15 +1,72 @@ package api -import "strconv" +import ( + "fmt" + "sort" + "strconv" -const ( - NameBlackListNodeID = "blackid" - NameReasonFailAdd = "reason" + "github.com/SmartBFT-Go/consensus/pkg/metrics" +) +const ( ReasonRequestMaxBytes = "MAX_BYTES" ReasonSemaphoreAcquireFail = "SEMAPHORE_ACQUIRE_FAIL" ) +func NewGaugeOpts(old metrics.GaugeOpts, labelNames []string) metrics.GaugeOpts { + return metrics.GaugeOpts{ + Namespace: old.Namespace, + Subsystem: old.Subsystem, + Name: old.Name, + Help: old.Help, + LabelNames: makeLabelNames(labelNames, old.LabelNames...), + LabelHelp: old.LabelHelp, + StatsdFormat: makeStatsdFormat(labelNames, old.StatsdFormat), + } +} + +func NewCounterOpts(old metrics.CounterOpts, labelNames []string) metrics.CounterOpts { + return metrics.CounterOpts{ + Namespace: old.Namespace, + Subsystem: old.Subsystem, + Name: old.Name, + Help: old.Help, + LabelNames: makeLabelNames(labelNames, old.LabelNames...), + LabelHelp: old.LabelHelp, + StatsdFormat: makeStatsdFormat(labelNames, old.StatsdFormat), + } +} + +func NewHistogramOpts(old metrics.HistogramOpts, labelNames []string) metrics.HistogramOpts { + return metrics.HistogramOpts{ + Namespace: old.Namespace, + Subsystem: old.Subsystem, + Name: old.Name, + Help: old.Help, + Buckets: old.Buckets, + LabelNames: makeLabelNames(labelNames, old.LabelNames...), + LabelHelp: old.LabelHelp, + StatsdFormat: makeStatsdFormat(labelNames, old.StatsdFormat), + } +} + +func makeStatsdFormat(labelNames []string, str string) string { + sort.Strings(labelNames) + for _, s := range labelNames { + str += fmt.Sprintf(".%%{%s}", s) + } + + return str +} + +func makeLabelNames(labelNames []string, names ...string) []string { + ln := make([]string, 0, len(names)+len(labelNames)) + ln = append(ln, names...) + sort.Strings(labelNames) + ln = append(ln, labelNames...) + return ln +} + type Metrics struct { MetricsRequestPool *MetricsRequestPool MetricsBlacklist *MetricsBlacklist @@ -18,7 +75,7 @@ type Metrics struct { MetricsViewChange *MetricsViewChange } -func NewMetrics(p Provider, labelNames ...string) *Metrics { +func NewMetrics(p metrics.Provider, labelNames ...string) *Metrics { return &Metrics{ MetricsRequestPool: NewMetricsRequestPool(p, labelNames...), MetricsBlacklist: NewMetricsBlacklist(p, labelNames...), @@ -46,64 +103,64 @@ func (m *Metrics) Initialize(nodes []uint64) { m.MetricsViewChange.Initialize() } -var countOfRequestPoolOpts = GaugeOpts{ +var countOfRequestPoolOpts = metrics.GaugeOpts{ Namespace: "consensus", - Subsystem: "bft", + Subsystem: "smartbft", Name: "pool_count_of_elements", Help: "Number of elements in the consensus request pool.", LabelNames: []string{}, StatsdFormat: "%{#fqname}", } -var countOfFailAddRequestToPoolOpts = CounterOpts{ +var countOfFailAddRequestToPoolOpts = metrics.CounterOpts{ Namespace: "consensus", - Subsystem: "bft", + Subsystem: "smartbft", Name: "pool_count_of_fail_add_request", Help: "Number of requests pool insertion failure.", - LabelNames: []string{NameReasonFailAdd}, - StatsdFormat: "%{#fqname}.%{" + NameReasonFailAdd + "}", + LabelNames: []string{"reason"}, + StatsdFormat: "%{#fqname}.%{reason}", } // ForwardTimeout -var countOfLeaderForwardRequestOpts = CounterOpts{ +var countOfLeaderForwardRequestOpts = metrics.CounterOpts{ Namespace: "consensus", - Subsystem: "bft", + Subsystem: "smartbft", Name: "pool_count_leader_forward_request", Help: "Number of requests forwarded to the leader.", LabelNames: []string{}, StatsdFormat: "%{#fqname}", } -var countTimeoutTwoStepOpts = CounterOpts{ +var countTimeoutTwoStepOpts = metrics.CounterOpts{ Namespace: "consensus", - Subsystem: "bft", + Subsystem: "smartbft", Name: "pool_count_timeout_two_step", Help: "Number of times requests reached second timeout.", LabelNames: []string{}, StatsdFormat: "%{#fqname}", } -var countOfDeleteRequestPoolOpts = CounterOpts{ +var countOfDeleteRequestPoolOpts = metrics.CounterOpts{ Namespace: "consensus", - Subsystem: "bft", + Subsystem: "smartbft", Name: "pool_count_of_delete_request", Help: "Number of elements removed from the request pool.", LabelNames: []string{}, StatsdFormat: "%{#fqname}", } -var countOfRequestPoolAllOpts = CounterOpts{ +var countOfRequestPoolAllOpts = metrics.CounterOpts{ Namespace: "consensus", - Subsystem: "bft", + Subsystem: "smartbft", Name: "pool_count_of_elements_all", Help: "Total amount of elements in the request pool.", LabelNames: []string{}, StatsdFormat: "%{#fqname}", } -var latencyOfRequestPoolOpts = HistogramOpts{ +var latencyOfRequestPoolOpts = metrics.HistogramOpts{ Namespace: "consensus", - Subsystem: "bft", + Subsystem: "smartbft", Name: "pool_latency_of_elements", Help: "The average request processing time, time request resides in the pool.", Buckets: []float64{0.005, 0.01, 0.015, 0.05, 0.1, 1, 10}, @@ -113,19 +170,19 @@ var latencyOfRequestPoolOpts = HistogramOpts{ // MetricsRequestPool encapsulates request pool metrics type MetricsRequestPool struct { - CountOfRequestPool Gauge - CountOfFailAddRequestToPool Counter - CountOfLeaderForwardRequest Counter - CountTimeoutTwoStep Counter - CountOfDeleteRequestPool Counter - CountOfRequestPoolAll Counter - LatencyOfRequestPool Histogram + CountOfRequestPool metrics.Gauge + CountOfFailAddRequestToPool metrics.Counter + CountOfLeaderForwardRequest metrics.Counter + CountTimeoutTwoStep metrics.Counter + CountOfDeleteRequestPool metrics.Counter + CountOfRequestPoolAll metrics.Counter + LatencyOfRequestPool metrics.Histogram labels []string } // NewMetricsRequestPool create new request pool metrics -func NewMetricsRequestPool(p Provider, labelNames ...string) *MetricsRequestPool { +func NewMetricsRequestPool(p metrics.Provider, labelNames ...string) *MetricsRequestPool { countOfRequestPoolOptsTmp := NewGaugeOpts(countOfRequestPoolOpts, labelNames) countOfFailAddRequestToPoolOptsTmp := NewCounterOpts(countOfFailAddRequestToPoolOpts, labelNames) countOfLeaderForwardRequestOptsTmp := NewCounterOpts(countOfLeaderForwardRequestOpts, labelNames) @@ -160,10 +217,10 @@ func (m *MetricsRequestPool) With(labelValues ...string) *MetricsRequestPool { func (m *MetricsRequestPool) Initialize() { m.CountOfRequestPool.Add(0) m.CountOfFailAddRequestToPool.With( - m.LabelsForWith(NameReasonFailAdd, ReasonRequestMaxBytes)..., + m.LabelsForWith("reason", ReasonRequestMaxBytes)..., ).Add(0) m.CountOfFailAddRequestToPool.With( - m.LabelsForWith(NameReasonFailAdd, ReasonSemaphoreAcquireFail)..., + m.LabelsForWith("reason", ReasonSemaphoreAcquireFail)..., ).Add(0) m.CountOfLeaderForwardRequest.Add(0) m.CountTimeoutTwoStep.Add(0) @@ -179,34 +236,34 @@ func (m *MetricsRequestPool) LabelsForWith(labelValues ...string) []string { return result } -var countBlackListOpts = GaugeOpts{ +var countBlackListOpts = metrics.GaugeOpts{ Namespace: "consensus", - Subsystem: "bft", + Subsystem: "smartbft", Name: "blacklist_count", Help: "Count of nodes in blacklist on this channel.", LabelNames: []string{}, StatsdFormat: "%{#fqname}", } -var nodesInBlackListOpts = GaugeOpts{ +var nodesInBlackListOpts = metrics.GaugeOpts{ Namespace: "consensus", - Subsystem: "bft", + Subsystem: "smartbft", Name: "node_id_in_blacklist", Help: "Node ID in blacklist on this channel.", - LabelNames: []string{NameBlackListNodeID}, - StatsdFormat: "%{#fqname}.%{" + NameBlackListNodeID + "}", + LabelNames: []string{"blackid"}, + StatsdFormat: "%{#fqname}.%{blackid}", } // MetricsBlacklist encapsulates blacklist metrics type MetricsBlacklist struct { - CountBlackList Gauge - NodesInBlackList Gauge + CountBlackList metrics.Gauge + NodesInBlackList metrics.Gauge labels []string } // NewMetricsBlacklist create new blacklist metrics -func NewMetricsBlacklist(p Provider, labelNames ...string) *MetricsBlacklist { +func NewMetricsBlacklist(p metrics.Provider, labelNames ...string) *MetricsBlacklist { countBlackListOptsTmp := NewGaugeOpts(countBlackListOpts, labelNames) nodesInBlackListOptsTmp := NewGaugeOpts(nodesInBlackListOpts, labelNames) return &MetricsBlacklist{ @@ -227,7 +284,7 @@ func (m *MetricsBlacklist) Initialize(nodes []uint64) { m.CountBlackList.Add(0) for _, n := range nodes { m.NodesInBlackList.With( - m.LabelsForWith(NameBlackListNodeID, strconv.FormatUint(n, 10))..., + m.LabelsForWith("blackid", strconv.FormatUint(n, 10))..., ).Set(0) } } @@ -239,18 +296,18 @@ func (m *MetricsBlacklist) LabelsForWith(labelValues ...string) []string { return result } -var consensusReconfigOpts = CounterOpts{ +var consensusReconfigOpts = metrics.CounterOpts{ Namespace: "consensus", - Subsystem: "bft", + Subsystem: "smartbft", Name: "consensus_reconfig", Help: "Number of reconfiguration requests.", LabelNames: []string{}, StatsdFormat: "%{#fqname}", } -var latencySyncOpts = HistogramOpts{ +var latencySyncOpts = metrics.HistogramOpts{ Namespace: "consensus", - Subsystem: "bft", + Subsystem: "smartbft", Name: "consensus_latency_sync", Help: "An average time it takes to sync node.", Buckets: []float64{0.005, 0.01, 0.015, 0.05, 0.1, 1, 10}, @@ -260,12 +317,12 @@ var latencySyncOpts = HistogramOpts{ // MetricsConsensus encapsulates consensus metrics type MetricsConsensus struct { - CountConsensusReconfig Counter - LatencySync Histogram + CountConsensusReconfig metrics.Counter + LatencySync metrics.Histogram } // NewMetricsConsensus create new consensus metrics -func NewMetricsConsensus(p Provider, labelNames ...string) *MetricsConsensus { +func NewMetricsConsensus(p metrics.Provider, labelNames ...string) *MetricsConsensus { consensusReconfigOptsTmp := NewCounterOpts(consensusReconfigOpts, labelNames) latencySyncOptsTmp := NewHistogramOpts(latencySyncOpts, labelNames) return &MetricsConsensus{ @@ -286,90 +343,90 @@ func (m *MetricsConsensus) Initialize() { m.LatencySync.Observe(0) } -var viewNumberOpts = GaugeOpts{ +var viewNumberOpts = metrics.GaugeOpts{ Namespace: "consensus", - Subsystem: "bft", + Subsystem: "smartbft", Name: "view_number", Help: "The View number value.", LabelNames: []string{}, StatsdFormat: "%{#fqname}", } -var leaderIDOpts = GaugeOpts{ +var leaderIDOpts = metrics.GaugeOpts{ Namespace: "consensus", - Subsystem: "bft", + Subsystem: "smartbft", Name: "view_leader_id", Help: "The leader id.", LabelNames: []string{}, StatsdFormat: "%{#fqname}", } -var proposalSequenceOpts = GaugeOpts{ +var proposalSequenceOpts = metrics.GaugeOpts{ Namespace: "consensus", - Subsystem: "bft", + Subsystem: "smartbft", Name: "view_proposal_sequence", Help: "The sequence number within current view.", LabelNames: []string{}, StatsdFormat: "%{#fqname}", } -var decisionsInViewOpts = GaugeOpts{ +var decisionsInViewOpts = metrics.GaugeOpts{ Namespace: "consensus", - Subsystem: "bft", + Subsystem: "smartbft", Name: "view_decisions", Help: "The number of decisions in the current view.", LabelNames: []string{}, StatsdFormat: "%{#fqname}", } -var phaseOpts = GaugeOpts{ +var phaseOpts = metrics.GaugeOpts{ Namespace: "consensus", - Subsystem: "bft", + Subsystem: "smartbft", Name: "view_phase", Help: "Current consensus phase.", LabelNames: []string{}, StatsdFormat: "%{#fqname}", } -var countTxsInBatchOpts = GaugeOpts{ +var countTxsInBatchOpts = metrics.GaugeOpts{ Namespace: "consensus", - Subsystem: "bft", + Subsystem: "smartbft", Name: "view_count_txs_in_batch", Help: "The number of transactions per batch.", LabelNames: []string{}, StatsdFormat: "%{#fqname}", } -var countBatchAllOpts = CounterOpts{ +var countBatchAllOpts = metrics.CounterOpts{ Namespace: "consensus", - Subsystem: "bft", + Subsystem: "smartbft", Name: "view_count_batch_all", Help: "Amount of batched processed.", LabelNames: []string{}, StatsdFormat: "%{#fqname}", } -var countTxsAllOpts = CounterOpts{ +var countTxsAllOpts = metrics.CounterOpts{ Namespace: "consensus", - Subsystem: "bft", + Subsystem: "smartbft", Name: "view_count_txs_all", Help: "Total amount of transactions.", LabelNames: []string{}, StatsdFormat: "%{#fqname}", } -var sizeOfBatchOpts = CounterOpts{ +var sizeOfBatchOpts = metrics.CounterOpts{ Namespace: "consensus", - Subsystem: "bft", + Subsystem: "smartbft", Name: "view_size_batch", Help: "An average batch size.", LabelNames: []string{}, StatsdFormat: "%{#fqname}", } -var latencyBatchProcessingOpts = HistogramOpts{ +var latencyBatchProcessingOpts = metrics.HistogramOpts{ Namespace: "consensus", - Subsystem: "bft", + Subsystem: "smartbft", Name: "view_latency_batch_processing", Help: "Amount of time it take to process batch.", Buckets: []float64{0.005, 0.01, 0.015, 0.05, 0.1, 1, 10}, @@ -377,9 +434,9 @@ var latencyBatchProcessingOpts = HistogramOpts{ StatsdFormat: "%{#fqname}", } -var latencyBatchSaveOpts = HistogramOpts{ +var latencyBatchSaveOpts = metrics.HistogramOpts{ Namespace: "consensus", - Subsystem: "bft", + Subsystem: "smartbft", Name: "view_latency_batch_save", Help: "An average time it takes to persist batch.", Buckets: []float64{0.005, 0.01, 0.015, 0.05, 0.1, 1, 10}, @@ -389,21 +446,21 @@ var latencyBatchSaveOpts = HistogramOpts{ // MetricsView encapsulates view metrics type MetricsView struct { - ViewNumber Gauge - LeaderID Gauge - ProposalSequence Gauge - DecisionsInView Gauge - Phase Gauge - CountTxsInBatch Gauge - CountBatchAll Counter - CountTxsAll Counter - SizeOfBatch Counter - LatencyBatchProcessing Histogram - LatencyBatchSave Histogram + ViewNumber metrics.Gauge + LeaderID metrics.Gauge + ProposalSequence metrics.Gauge + DecisionsInView metrics.Gauge + Phase metrics.Gauge + CountTxsInBatch metrics.Gauge + CountBatchAll metrics.Counter + CountTxsAll metrics.Counter + SizeOfBatch metrics.Counter + LatencyBatchProcessing metrics.Histogram + LatencyBatchSave metrics.Histogram } // NewMetricsView create new view metrics -func NewMetricsView(p Provider, labelNames ...string) *MetricsView { +func NewMetricsView(p metrics.Provider, labelNames ...string) *MetricsView { viewNumberOptsTmp := NewGaugeOpts(viewNumberOpts, labelNames) leaderIDOptsTmp := NewGaugeOpts(leaderIDOpts, labelNames) proposalSequenceOptsTmp := NewGaugeOpts(proposalSequenceOpts, labelNames) @@ -460,27 +517,27 @@ func (m *MetricsView) Initialize() { m.LatencyBatchSave.Observe(0) } -var currentViewOpts = GaugeOpts{ +var currentViewOpts = metrics.GaugeOpts{ Namespace: "consensus", - Subsystem: "bft", + Subsystem: "smartbft", Name: "viewchange_current_view", Help: "current view of viewchange on this channel.", LabelNames: []string{}, StatsdFormat: "%{#fqname}", } -var nextViewOpts = GaugeOpts{ +var nextViewOpts = metrics.GaugeOpts{ Namespace: "consensus", - Subsystem: "bft", + Subsystem: "smartbft", Name: "viewchange_next_view", Help: "next view of viewchange on this channel.", LabelNames: []string{}, StatsdFormat: "%{#fqname}", } -var realViewOpts = GaugeOpts{ +var realViewOpts = metrics.GaugeOpts{ Namespace: "consensus", - Subsystem: "bft", + Subsystem: "smartbft", Name: "viewchange_real_view", Help: "real view of viewchange on this channel.", LabelNames: []string{}, @@ -489,13 +546,13 @@ var realViewOpts = GaugeOpts{ // MetricsViewChange encapsulates view change metrics type MetricsViewChange struct { - CurrentView Gauge - NextView Gauge - RealView Gauge + CurrentView metrics.Gauge + NextView metrics.Gauge + RealView metrics.Gauge } // NewMetricsViewChange create new view change metrics -func NewMetricsViewChange(p Provider, labelNames ...string) *MetricsViewChange { +func NewMetricsViewChange(p metrics.Provider, labelNames ...string) *MetricsViewChange { currentViewOptsTmp := NewGaugeOpts(currentViewOpts, labelNames) nextViewOptsTmp := NewGaugeOpts(nextViewOpts, labelNames) realViewOptsTmp := NewGaugeOpts(realViewOpts, labelNames) diff --git a/vendor/github.com/SmartBFT-Go/consensus/pkg/consensus/consensus.go b/vendor/github.com/SmartBFT-Go/consensus/pkg/consensus/consensus.go index 15e4cff4124..50119e630b9 100644 --- a/vendor/github.com/SmartBFT-Go/consensus/pkg/consensus/consensus.go +++ b/vendor/github.com/SmartBFT-Go/consensus/pkg/consensus/consensus.go @@ -216,20 +216,9 @@ func (c *Consensus) reconfig(reconfig types.Reconfig) { c.Logger.Panicf("Configuration is invalid, error: %v", err) } - tmp := c.nodes - var newNodes []uint64 + old := c.nodes c.setNodes(reconfig.CurrentNodes) - -OuterLoop: - for _, i := range c.nodes { - for _, j := range tmp { - if i == j { - continue OuterLoop - } - } - newNodes = append(newNodes, i) - } - c.Metrics.MetricsBlacklist.Initialize(newNodes) + c.initMetricsBlacklistReconfigure(old) c.createComponents() opts := algorithm.PoolOptions{ @@ -262,6 +251,21 @@ OuterLoop: c.Logger.Debugf("Reconfig is done") } +func (c *Consensus) initMetricsBlacklistReconfigure(old []uint64) { + var newNodes []uint64 + +OuterLoop: + for _, i := range c.nodes { + for _, j := range old { + if i == j { + continue OuterLoop + } + } + newNodes = append(newNodes, i) + } + c.Metrics.MetricsBlacklist.Initialize(newNodes) +} + func (c *Consensus) close() { c.stopOnce.Do( func() { diff --git a/vendor/github.com/SmartBFT-Go/consensus/pkg/metrics/disabled/provider.go b/vendor/github.com/SmartBFT-Go/consensus/pkg/metrics/disabled/provider.go index e6babb97f65..457db1f1316 100644 --- a/vendor/github.com/SmartBFT-Go/consensus/pkg/metrics/disabled/provider.go +++ b/vendor/github.com/SmartBFT-Go/consensus/pkg/metrics/disabled/provider.go @@ -7,7 +7,7 @@ SPDX-License-Identifier: Apache-2.0 package disabled import ( - bft "github.com/SmartBFT-Go/consensus/pkg/api" + bft "github.com/SmartBFT-Go/consensus/pkg/metrics" ) type Provider struct{} diff --git a/vendor/github.com/SmartBFT-Go/consensus/pkg/api/metrics_provider.go b/vendor/github.com/SmartBFT-Go/consensus/pkg/metrics/provider.go similarity index 79% rename from vendor/github.com/SmartBFT-Go/consensus/pkg/api/metrics_provider.go rename to vendor/github.com/SmartBFT-Go/consensus/pkg/metrics/provider.go index ca421955159..21cfe46a1f4 100644 --- a/vendor/github.com/SmartBFT-Go/consensus/pkg/api/metrics_provider.go +++ b/vendor/github.com/SmartBFT-Go/consensus/pkg/metrics/provider.go @@ -4,66 +4,7 @@ Copyright IBM Corp. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 */ -package api - -import ( - "fmt" - "sort" -) - -func NewGaugeOpts(old GaugeOpts, labelNames []string) GaugeOpts { - return GaugeOpts{ - Namespace: old.Namespace, - Subsystem: old.Subsystem, - Name: old.Name, - Help: old.Help, - LabelNames: makeLabelNames(labelNames, old.LabelNames...), - LabelHelp: old.LabelHelp, - StatsdFormat: makeStatsdFormat(labelNames, old.StatsdFormat), - } -} - -func NewCounterOpts(old CounterOpts, labelNames []string) CounterOpts { - return CounterOpts{ - Namespace: old.Namespace, - Subsystem: old.Subsystem, - Name: old.Name, - Help: old.Help, - LabelNames: makeLabelNames(labelNames, old.LabelNames...), - LabelHelp: old.LabelHelp, - StatsdFormat: makeStatsdFormat(labelNames, old.StatsdFormat), - } -} - -func NewHistogramOpts(old HistogramOpts, labelNames []string) HistogramOpts { - return HistogramOpts{ - Namespace: old.Namespace, - Subsystem: old.Subsystem, - Name: old.Name, - Help: old.Help, - Buckets: old.Buckets, - LabelNames: makeLabelNames(labelNames, old.LabelNames...), - LabelHelp: old.LabelHelp, - StatsdFormat: makeStatsdFormat(labelNames, old.StatsdFormat), - } -} - -func makeStatsdFormat(labelNames []string, str string) string { - sort.Strings(labelNames) - for _, s := range labelNames { - str += fmt.Sprintf(".%%{%s}", s) - } - - return str -} - -func makeLabelNames(labelNames []string, names ...string) []string { - ln := make([]string, 0, len(names)+len(labelNames)) - ln = append(ln, names...) - sort.Strings(labelNames) - ln = append(ln, labelNames...) - return ln -} +package metrics // A Provider is an abstraction for a metrics provider. It is a factory for // Counter, Gauge, and Histogram meters. diff --git a/vendor/github.com/SmartBFT-Go/consensus/pkg/wal/metrics.go b/vendor/github.com/SmartBFT-Go/consensus/pkg/wal/metrics.go index 82c4c328dfc..e6d8ca75c4f 100644 --- a/vendor/github.com/SmartBFT-Go/consensus/pkg/wal/metrics.go +++ b/vendor/github.com/SmartBFT-Go/consensus/pkg/wal/metrics.go @@ -1,10 +1,13 @@ package wal -import metrics "github.com/SmartBFT-Go/consensus/pkg/api" +import ( + "github.com/SmartBFT-Go/consensus/pkg/api" + "github.com/SmartBFT-Go/consensus/pkg/metrics" +) var countOfFilesOpts = metrics.GaugeOpts{ Namespace: "consensus", - Subsystem: "bft", + Subsystem: "smartbft", Name: "wal_count_of_files", Help: "Count of wal-files.", LabelNames: []string{}, @@ -18,7 +21,7 @@ type Metrics struct { // NewMetrics create new wal metrics func NewMetrics(p metrics.Provider, labelNames ...string) *Metrics { - countOfFilesOptsTmp := metrics.NewGaugeOpts(countOfFilesOpts, labelNames) + countOfFilesOptsTmp := api.NewGaugeOpts(countOfFilesOpts, labelNames) return &Metrics{ CountOfFiles: p.NewGauge(countOfFilesOptsTmp), } diff --git a/vendor/modules.txt b/vendor/modules.txt index a8571cadbc1..0fa78490cc6 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -56,11 +56,12 @@ github.com/Microsoft/hcsshim/internal/vmcompute github.com/Microsoft/hcsshim/internal/wclayer github.com/Microsoft/hcsshim/internal/winapi github.com/Microsoft/hcsshim/osversion -# github.com/SmartBFT-Go/consensus v0.0.0-20230717102940-071648f76967 +# github.com/SmartBFT-Go/consensus v0.0.0-20230907130931-31f018a899a6 ## explicit; go 1.20 github.com/SmartBFT-Go/consensus/internal/bft github.com/SmartBFT-Go/consensus/pkg/api github.com/SmartBFT-Go/consensus/pkg/consensus +github.com/SmartBFT-Go/consensus/pkg/metrics github.com/SmartBFT-Go/consensus/pkg/metrics/disabled github.com/SmartBFT-Go/consensus/pkg/types github.com/SmartBFT-Go/consensus/pkg/wal