-
Notifications
You must be signed in to change notification settings - Fork 671
/
set.go
197 lines (164 loc) · 4.77 KB
/
set.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package poll
import (
"fmt"
"strings"
"time"
"github.com/ava-labs/avalanchego/utils/linkedhashmap"
"github.com/prometheus/client_golang/prometheus"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/metric"
)
type pollHolder interface {
GetPoll() Poll
StartTime() time.Time
}
type poll struct {
Poll
start time.Time
}
func (p poll) GetPoll() Poll {
return p
}
func (p poll) StartTime() time.Time {
return p.start
}
type set struct {
log logging.Logger
numPolls prometheus.Gauge
durPolls metric.Averager
factory Factory
// maps requestID -> poll
polls linkedhashmap.LinkedHashmap
}
// NewSet returns a new empty set of polls
func NewSet(
factory Factory,
log logging.Logger,
namespace string,
reg prometheus.Registerer,
) Set {
numPolls := prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "polls",
Help: "Number of pending network polls",
})
if err := reg.Register(numPolls); err != nil {
log.Error("failed to register polls statistics due to %s", err)
}
durPolls, err := metric.NewAverager(
namespace,
"poll_duration",
"time (in ns) this poll took to complete",
reg,
)
if err != nil {
log.Error("failed to register poll_duration statistics due to %s", err)
}
return &set{
log: log,
numPolls: numPolls,
durPolls: durPolls,
factory: factory,
polls: linkedhashmap.New(),
}
}
// Add to the current set of polls
// Returns true if the poll was registered correctly and the network sample
// should be made.
func (s *set) Add(requestID uint32, vdrs ids.NodeIDBag) bool {
if _, exists := s.polls.Get(requestID); exists {
s.log.Debug("dropping poll due to duplicated requestID: %d", requestID)
return false
}
s.log.Verbo("creating poll with requestID %d and validators %s",
requestID,
&vdrs)
s.polls.Put(requestID, poll{
Poll: s.factory.New(vdrs), // create the new poll
start: time.Now(),
})
s.numPolls.Inc() // increase the metrics
return true
}
// Vote registers the connections response to a query for [id]. If there was no
// query, or the response has already be registered, nothing is performed.
func (s *set) Vote(requestID uint32, vdr ids.NodeID, vote ids.ID) []ids.Bag {
pollHolderIntf, exists := s.polls.Get(requestID)
if !exists {
s.log.Verbo("dropping vote from %s to an unknown poll with requestID: %d",
vdr,
requestID)
return nil
}
holder := pollHolderIntf.(pollHolder)
p := holder.GetPoll()
s.log.Verbo("processing vote from %s in the poll with requestID: %d with the vote %s",
vdr,
requestID,
vote)
p.Vote(vdr, vote)
if !p.Finished() {
return nil
}
return s.processFinishedPolls()
}
// processFinishedPolls checks for other dependent finished polls and returns them all if finished
func (s *set) processFinishedPolls() []ids.Bag {
var results []ids.Bag
// iterate from oldest to newest
iter := s.polls.NewIterator()
for iter.Next() {
holder := iter.Value().(pollHolder)
p := holder.GetPoll()
if !p.Finished() {
// since we're iterating from oldest to newest, if the next poll has not finished,
// we can break and return what we have so far
break
}
s.log.Verbo("poll with requestID %d finished as %s", iter.Key(), holder.GetPoll())
s.durPolls.Observe(float64(time.Since(holder.StartTime())))
s.numPolls.Dec() // decrease the metrics
results = append(results, p.Result())
s.polls.Delete(iter.Key())
}
// only gets here if the poll has finished
// results will have values if this and other newer polls have finished
return results
}
// Drop registers the connections response to a query for [id]. If there was no
// query, or the response has already be registered, nothing is performed.
func (s *set) Drop(requestID uint32, vdr ids.NodeID) []ids.Bag {
pollHolderIntf, exists := s.polls.Get(requestID)
if !exists {
s.log.Verbo("dropping vote from %s to an unknown poll with requestID: %d",
vdr,
requestID)
return nil
}
s.log.Verbo("processing dropped vote from %s in the poll with requestID: %d",
vdr,
requestID)
pollHolder := pollHolderIntf.(pollHolder)
poll := pollHolder.GetPoll()
poll.Drop(vdr)
if !poll.Finished() {
return nil
}
return s.processFinishedPolls()
}
// Len returns the number of outstanding polls
func (s *set) Len() int { return s.polls.Len() }
func (s *set) String() string {
sb := strings.Builder{}
sb.WriteString(fmt.Sprintf("current polls: (Size = %d)", s.polls.Len()))
iter := s.polls.NewIterator()
for iter.Next() {
requestID := iter.Key()
poll := iter.Value().(Poll)
sb.WriteString(fmt.Sprintf("\n RequestID %d:\n %s", requestID, poll.PrefixedString(" ")))
}
return sb.String()
}