forked from uber/ringpop-go
/
gossip.go
198 lines (163 loc) · 4.82 KB
/
gossip.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
198
// Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package swim
import (
"math"
"math/rand"
"sync"
"time"
"github.com/rcrowley/go-metrics"
log "github.com/uber-common/bark"
"github.com/uber/ringpop-go/logging"
)
// Gossip handles the protocol period of the SWIM protocol
type gossip struct {
node *Node
state struct {
stopped bool
sync.RWMutex
}
minProtocolPeriod time.Duration
protocol struct {
numPeriods int
lastPeriod time.Time
lastRate time.Duration
timing metrics.Histogram
sync.RWMutex
}
logger log.Logger
}
// newGossip returns a new gossip SWIM sub-protocol with the given protocol period
func newGossip(node *Node, minProtocolPeriod time.Duration) *gossip {
gossip := &gossip{
node: node,
minProtocolPeriod: minProtocolPeriod,
logger: logging.Logger("gossip").WithField("local", node.Address()),
}
gossip.SetStopped(true)
gossip.protocol.timing = metrics.NewHistogram(metrics.NewUniformSample(10))
gossip.protocol.timing.Update(int64(gossip.minProtocolPeriod))
return gossip
}
// returns whether or not the gossip sub-protocol is stopped
func (g *gossip) Stopped() bool {
g.state.RLock()
stopped := g.state.stopped
g.state.RUnlock()
return stopped
}
// sets the gossip sub-protocol to stopped or not stopped
func (g *gossip) SetStopped(stopped bool) {
g.state.Lock()
g.state.stopped = stopped
g.state.Unlock()
}
// computes a delay for the gossip protocol period
func (g *gossip) ComputeProtocolDelay() time.Duration {
g.protocol.RLock()
defer g.protocol.RUnlock()
if g.protocol.numPeriods != 0 {
target := g.protocol.lastPeriod.Add(g.protocol.lastRate)
delay := math.Max(float64(target.Sub(time.Now())), float64(g.minProtocolPeriod))
return time.Duration(delay)
}
// delay for first tick in [0, minProtocolPeriod]ms
return time.Duration(rand.Intn(int(g.minProtocolPeriod + 1)))
}
func (g *gossip) ProtocolRate() time.Duration {
g.protocol.RLock()
rate := g.protocol.lastRate
g.protocol.RUnlock()
return rate
}
// computes a ProtocolRate for the Gossip
func (g *gossip) AdjustProtocolRate() {
g.protocol.Lock()
observed := g.protocol.timing.Percentile(0.5) * 2.0
g.protocol.lastRate = time.Duration(math.Max(observed, float64(g.minProtocolPeriod)))
g.protocol.Unlock()
}
func (g *gossip) ProtocolTiming() metrics.Histogram {
g.protocol.RLock()
histogram := g.protocol.timing
g.protocol.RUnlock()
return histogram
}
// start the gossip protocol
func (g *gossip) Start() {
if !g.Stopped() {
g.logger.Warn("gossip already started")
return
}
g.SetStopped(false)
g.RunProtocolRateLoop()
g.RunProtocolPeriodLoop()
g.logger.Debug("started gossip protocol")
}
// stop the gossip protocol
func (g *gossip) Stop() {
if g.Stopped() {
g.logger.Warn("gossip already stopped")
return
}
g.SetStopped(true)
g.logger.Debug("stopped gossip protocol")
}
// run the gossip protocol period loop
func (g *gossip) RunProtocolPeriodLoop() {
go func() {
startTime := time.Now()
for !g.Stopped() {
delay := g.ComputeProtocolDelay()
g.node.emit(ProtocolDelayComputeEvent{
Duration: delay,
})
startTimeFreq := time.Now()
g.ProtocolPeriod()
time.Sleep(delay)
g.node.emit(ProtocolFrequencyEvent{
Duration: time.Now().Sub(startTimeFreq),
})
}
g.logger.WithFields(log.Fields{
"start": startTime,
"end": time.Now(),
}).Debug("stopped protocol period loop")
}()
}
// run a gossip protocol period
func (g *gossip) ProtocolPeriod() {
startTime := time.Now()
g.node.pingNextMember()
g.protocol.Lock()
g.protocol.lastPeriod = time.Now()
g.protocol.numPeriods++
g.protocol.timing.Update(int64(time.Now().Sub(startTime)))
g.protocol.Unlock()
}
//
func (g *gossip) RunProtocolRateLoop() {
go func() {
for !g.Stopped() {
time.Sleep(time.Second)
g.AdjustProtocolRate()
}
}()
}