forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pullstore.go
368 lines (320 loc) · 10.6 KB
/
pullstore.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package pull
import (
"sync"
"time"
"github.com/hyperledger/fabric/gossip/comm"
"github.com/hyperledger/fabric/gossip/common"
"github.com/hyperledger/fabric/gossip/discovery"
"github.com/hyperledger/fabric/gossip/gossip/algo"
"github.com/hyperledger/fabric/gossip/util"
proto "github.com/hyperledger/fabric/protos/gossip"
"github.com/op/go-logging"
)
// Constants go here.
const (
HelloMsgType MsgType = iota
DigestMsgType
RequestMsgType
ResponseMsgType
)
// MsgType defines the type of a message that is sent to the PullStore
type MsgType int
// MessageHook defines a function that will run after a certain pull message is received
type MessageHook func(itemIDs []string, items []*proto.SignedGossipMessage, msg proto.ReceivedMessage)
// Sender sends messages to remote peers
type Sender interface {
// Send sends a message to a list of remote peers
Send(msg *proto.SignedGossipMessage, peers ...*comm.RemotePeer)
}
// MembershipService obtains membership information of alive peers
type MembershipService interface {
// GetMembership returns the membership of
GetMembership() []discovery.NetworkMember
}
// Config defines the configuration of the pull mediator
type Config struct {
ID string
PullInterval time.Duration // Duration between pull invocations
Channel common.ChainID
PeerCountToSelect int // Number of peers to initiate pull with
Tag proto.GossipMessage_Tag
MsgType proto.PullMsgType
}
// DigestFilter filters digests to be sent to a remote peer, that
// sent a hello with the following message
type DigestFilter func(helloMsg proto.ReceivedMessage) func(digestItem string) bool
// byContext converts this DigestFilter to an algo.DigestFilter
func (df DigestFilter) byContext() algo.DigestFilter {
return func(context interface{}) func(digestItem string) bool {
return func(digestItem string) bool {
return df(context.(proto.ReceivedMessage))(digestItem)
}
}
}
// PullAdapter defines methods of the pullStore to interact
// with various modules of gossip
type PullAdapter struct {
Sndr Sender
MemSvc MembershipService
IdExtractor proto.IdentifierExtractor
MsgCons proto.MsgConsumer
DigFilter DigestFilter
}
// Mediator is a component wrap a PullEngine and provides the methods
// it needs to perform pull synchronization.
// The specialization of a pull mediator to a certain type of message is
// done by the configuration, a IdentifierExtractor, IdentifierExtractor
// given at construction, and also hooks that can be registered for each
// type of pullMsgType (hello, digest, req, res).
type Mediator interface {
// Stop stop the Mediator
Stop()
// RegisterMsgHook registers a message hook to a specific type of pull message
RegisterMsgHook(MsgType, MessageHook)
// Add adds a GossipMessage to the Mediator
Add(*proto.SignedGossipMessage)
// Remove removes a GossipMessage from the Mediator with a matching digest,
// if such a message exits
Remove(digest string)
// HandleMessage handles a message from some remote peer
HandleMessage(msg proto.ReceivedMessage)
}
// pullMediatorImpl is an implementation of Mediator
type pullMediatorImpl struct {
sync.RWMutex
*PullAdapter
msgType2Hook map[MsgType][]MessageHook
config Config
logger *logging.Logger
itemID2Msg map[string]*proto.SignedGossipMessage
engine *algo.PullEngine
}
// NewPullMediator returns a new Mediator
func NewPullMediator(config Config, adapter *PullAdapter) Mediator {
digFilter := adapter.DigFilter
acceptAllFilter := func(_ proto.ReceivedMessage) func(string) bool {
return func(_ string) bool {
return true
}
}
if digFilter == nil {
digFilter = acceptAllFilter
}
p := &pullMediatorImpl{
PullAdapter: adapter,
msgType2Hook: make(map[MsgType][]MessageHook),
config: config,
logger: util.GetLogger(util.LoggingPullModule, config.ID),
itemID2Msg: make(map[string]*proto.SignedGossipMessage),
}
p.engine = algo.NewPullEngineWithFilter(p, config.PullInterval, digFilter.byContext())
return p
}
func (p *pullMediatorImpl) HandleMessage(m proto.ReceivedMessage) {
if m.GetGossipMessage() == nil || !m.GetGossipMessage().IsPullMsg() {
return
}
msg := m.GetGossipMessage()
msgType := msg.GetPullMsgType()
if msgType != p.config.MsgType {
return
}
p.logger.Debug(msg)
itemIDs := []string{}
items := []*proto.SignedGossipMessage{}
var pullMsgType MsgType
if helloMsg := msg.GetHello(); helloMsg != nil {
pullMsgType = HelloMsgType
p.engine.OnHello(helloMsg.Nonce, m)
}
if digest := msg.GetDataDig(); digest != nil {
itemIDs = digest.Digests
pullMsgType = DigestMsgType
p.engine.OnDigest(digest.Digests, digest.Nonce, m)
}
if req := msg.GetDataReq(); req != nil {
itemIDs = req.Digests
pullMsgType = RequestMsgType
p.engine.OnReq(req.Digests, req.Nonce, m)
}
if res := msg.GetDataUpdate(); res != nil {
itemIDs = make([]string, len(res.Data))
items = make([]*proto.SignedGossipMessage, len(res.Data))
pullMsgType = ResponseMsgType
for i, pulledMsg := range res.Data {
msg, err := pulledMsg.ToGossipMessage()
if err != nil {
p.logger.Warning("Data update contains an invalid message:", err)
return
}
p.MsgCons(msg)
itemIDs[i] = p.IdExtractor(msg)
items[i] = msg
p.Lock()
p.itemID2Msg[itemIDs[i]] = msg
p.Unlock()
}
p.engine.OnRes(itemIDs, res.Nonce)
}
// Invoke hooks for relevant message type
for _, h := range p.hooksByMsgType(pullMsgType) {
h(itemIDs, items, m)
}
}
func (p *pullMediatorImpl) Stop() {
p.engine.Stop()
}
// RegisterMsgHook registers a message hook to a specific type of pull message
func (p *pullMediatorImpl) RegisterMsgHook(pullMsgType MsgType, hook MessageHook) {
p.Lock()
defer p.Unlock()
p.msgType2Hook[pullMsgType] = append(p.msgType2Hook[pullMsgType], hook)
}
// Add adds a GossipMessage to the store
func (p *pullMediatorImpl) Add(msg *proto.SignedGossipMessage) {
p.Lock()
defer p.Unlock()
itemID := p.IdExtractor(msg)
p.itemID2Msg[itemID] = msg
p.engine.Add(itemID)
}
// Remove removes a GossipMessage from the Mediator with a matching digest,
// if such a message exits
func (p *pullMediatorImpl) Remove(digest string) {
p.Lock()
defer p.Unlock()
delete(p.itemID2Msg, digest)
p.engine.Remove(digest)
}
// SelectPeers returns a slice of peers which the engine will initiate the protocol with
func (p *pullMediatorImpl) SelectPeers() []string {
remotePeers := SelectEndpoints(p.config.PeerCountToSelect, p.MemSvc.GetMembership())
endpoints := make([]string, len(remotePeers))
for i, peer := range remotePeers {
endpoints[i] = peer.Endpoint
}
return endpoints
}
// Hello sends a hello message to initiate the protocol
// and returns an NONCE that is expected to be returned
// in the digest message.
func (p *pullMediatorImpl) Hello(dest string, nonce uint64) {
helloMsg := &proto.GossipMessage{
Channel: p.config.Channel,
Tag: p.config.Tag,
Content: &proto.GossipMessage_Hello{
Hello: &proto.GossipHello{
Nonce: nonce,
Metadata: nil,
MsgType: p.config.MsgType,
},
},
}
p.logger.Debug("Sending hello to", dest)
p.Sndr.Send(helloMsg.NoopSign(), p.peersWithEndpoints(dest)...)
}
// SendDigest sends a digest to a remote PullEngine.
// The context parameter specifies the remote engine to send to.
func (p *pullMediatorImpl) SendDigest(digest []string, nonce uint64, context interface{}) {
digMsg := &proto.GossipMessage{
Channel: p.config.Channel,
Tag: p.config.Tag,
Nonce: 0,
Content: &proto.GossipMessage_DataDig{
DataDig: &proto.DataDigest{
MsgType: p.config.MsgType,
Nonce: nonce,
Digests: digest,
},
},
}
p.logger.Debug("Sending digest", digMsg.GetDataDig().Digests)
context.(proto.ReceivedMessage).Respond(digMsg)
}
// SendReq sends an array of items to a certain remote PullEngine identified
// by a string
func (p *pullMediatorImpl) SendReq(dest string, items []string, nonce uint64) {
req := &proto.GossipMessage{
Channel: p.config.Channel,
Tag: p.config.Tag,
Nonce: 0,
Content: &proto.GossipMessage_DataReq{
DataReq: &proto.DataRequest{
MsgType: p.config.MsgType,
Nonce: nonce,
Digests: items,
},
},
}
p.logger.Debug("Sending", req, "to", dest)
p.Sndr.Send(req.NoopSign(), p.peersWithEndpoints(dest)...)
}
// SendRes sends an array of items to a remote PullEngine identified by a context.
func (p *pullMediatorImpl) SendRes(items []string, context interface{}, nonce uint64) {
items2return := []*proto.Envelope{}
p.RLock()
defer p.RUnlock()
for _, item := range items {
if msg, exists := p.itemID2Msg[item]; exists {
items2return = append(items2return, msg.Envelope)
}
}
returnedUpdate := &proto.GossipMessage{
Channel: p.config.Channel,
Tag: p.config.Tag,
Nonce: 0,
Content: &proto.GossipMessage_DataUpdate{
DataUpdate: &proto.DataUpdate{
MsgType: p.config.MsgType,
Nonce: nonce,
Data: items2return,
},
},
}
p.logger.Debug("Sending", returnedUpdate, "to")
context.(proto.ReceivedMessage).Respond(returnedUpdate)
}
func (p *pullMediatorImpl) peersWithEndpoints(endpoints ...string) []*comm.RemotePeer {
peers := []*comm.RemotePeer{}
for _, member := range p.MemSvc.GetMembership() {
for _, endpoint := range endpoints {
if member.PreferredEndpoint() == endpoint {
peers = append(peers, &comm.RemotePeer{Endpoint: member.PreferredEndpoint(), PKIID: member.PKIid})
}
}
}
return peers
}
func (p *pullMediatorImpl) hooksByMsgType(msgType MsgType) []MessageHook {
p.RLock()
defer p.RUnlock()
returnedHooks := []MessageHook{}
for _, h := range p.msgType2Hook[msgType] {
returnedHooks = append(returnedHooks, h)
}
return returnedHooks
}
// SelectEndpoints select k peers from peerPool and returns them.
func SelectEndpoints(k int, peerPool []discovery.NetworkMember) []*comm.RemotePeer {
if len(peerPool) < k {
k = len(peerPool)
}
indices := util.GetRandomIndices(k, len(peerPool)-1)
endpoints := make([]*comm.RemotePeer, len(indices))
for i, j := range indices {
endpoints[i] = &comm.RemotePeer{Endpoint: peerPool[j].PreferredEndpoint(), PKIID: peerPool[j].PKIid}
}
return endpoints
}