-
Notifications
You must be signed in to change notification settings - Fork 82
/
ping_request_sender.go
208 lines (171 loc) · 5.57 KB
/
ping_request_sender.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
199
200
201
202
203
204
205
206
207
208
// 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 (
"errors"
"sync"
"time"
log "github.com/uber-common/bark"
"github.com/uber/ringpop-go/logging"
"github.com/uber/ringpop-go/shared"
"github.com/uber/tchannel-go/json"
)
// A PingRequest is used to make a ping request to a remote node
type pingRequest struct {
Source string `json:"source"`
SourceIncarnation int64 `json:"sourceIncarnationNumber"`
Target string `json:"target"`
Checksum uint32 `json:"checksum"`
Changes []Change `json:"changes"`
}
// A PingRequestSender is used to make a ping request to a remote node
type pingRequestSender struct {
node *Node
peer string
target string
timeout time.Duration
logger log.Logger
}
// NewPingRequestSender returns a new PingRequestSender
func newPingRequestSender(node *Node, peer, target string, timeout time.Duration) *pingRequestSender {
p := &pingRequestSender{
node: node,
peer: peer,
target: target,
timeout: timeout,
logger: logging.Logger("ping").WithField("local", node.Address()),
}
return p
}
func (p *pingRequestSender) SendPingRequest() (*pingResponse, error) {
p.logger.WithFields(log.Fields{
"peer": p.peer,
"target": p.target,
}).Debug("ping request send")
ctx, cancel := shared.NewTChannelContext(p.timeout)
defer cancel()
var res pingResponse
select {
case err := <-p.MakeCall(ctx, &res):
if err == nil {
p.node.memberlist.Update(res.Changes)
}
return &res, err
case <-ctx.Done(): // call timed out
return nil, errors.New("ping request timed out")
}
}
func (p *pingRequestSender) MakeCall(ctx json.Context, res *pingResponse) <-chan error {
errC := make(chan error, 1)
go func() {
defer close(errC)
changes, bumpPiggybackCounters := p.node.disseminator.IssueAsSender()
req := &pingRequest{
Source: p.node.Address(),
SourceIncarnation: p.node.Incarnation(),
Checksum: p.node.memberlist.Checksum(),
Changes: changes,
Target: p.target,
}
peer := p.node.channel.Peers().GetOrAdd(p.peer)
err := json.CallPeer(ctx, peer, p.node.service, "/protocol/ping-req", req, &res)
if err != nil {
bumpPiggybackCounters()
errC <- err
return
}
errC <- nil
}()
return errC
}
// indirectPing is used to check if a target node can be reached indirectly.
// The indirectPing is performed by sending a specifiable amount of ping
// requests nodes in n's membership.
func indirectPing(n *Node, target string, amount int, timeout time.Duration) (reached bool, errs []error) {
resCh := sendPingRequests(n, target, amount, timeout)
// wait for responses from the ping-reqs
for result := range resCh {
switch res := result.(type) {
case *pingResponse:
if res.Ok {
return true, errs
}
// If the ping to the target was not-ok we want to wait for more results.
case error:
errs = append(errs, res)
}
}
return false, errs
}
// sendPingRequests sends ping requests to the target address and returns a channel
//containing the responses. Responses can be one of type:
// (1) error: if the call to peer failed
// (2) PingResponse: if the peer performed the ping request
func sendPingRequests(node *Node, target string, size int, timeout time.Duration) <-chan interface{} {
var peerAddresses []string
peers := node.memberlist.RandomPingableMembers(size, map[string]bool{target: true})
for _, peer := range peers {
peerAddresses = append(peerAddresses, peer.Address)
}
node.EmitEvent(PingRequestsSendEvent{
Local: node.Address(),
Target: target,
Peers: peerAddresses,
})
var wg sync.WaitGroup
resC := make(chan interface{}, size)
for _, peer := range peers {
wg.Add(1)
go func(peer Member) {
defer wg.Done()
p := newPingRequestSender(node, peer.Address, target, timeout)
p.logger.WithFields(log.Fields{
"peer": peer.Address,
"target": p.target,
}).Debug("sending ping request")
var startTime = time.Now()
res, err := p.SendPingRequest()
if err != nil {
node.EmitEvent(PingRequestSendErrorEvent{
Local: node.Address(),
Target: target,
Peers: peerAddresses,
Peer: peer.Address,
})
resC <- err
return
}
node.EmitEvent(PingRequestsSendCompleteEvent{
Local: node.Address(),
Target: target,
Peers: peerAddresses,
Peer: peer.Address,
Duration: time.Now().Sub(startTime),
})
resC <- res
}(peer)
}
// wait for all sends to complete before closing channel
go func() {
wg.Wait()
close(resC)
}()
return resC
}