Skip to content

Commit baea89c

Browse files
committed
FAB-1045 Gossip pull refactoring: uint64->string
Currently the gossip pull mechanism can only handle items uint64 as keys (for digests). I refactored this to be string, because a string can hold both ints, hashes and can also be used as a key in a map. This is needed for disseminating entities such as certificates of peers. Change-Id: I06be757275dccd4e78f055bb703aeadd52787c33 Signed-off-by: Yacov Manevich <[email protected]>
1 parent ede30a4 commit baea89c

File tree

5 files changed

+245
-194
lines changed

5 files changed

+245
-194
lines changed

gossip/gossip/algo/pull.go

+17-17
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
)
2727

2828
/* PullEngine is an object that performs pull-based gossip, and maintains an internal state of items
29-
identified by uint64 numbers.
29+
identified by string numbers.
3030
The protocol is as follows:
3131
1) The Initiator sends a Hello message with a specific NONCE to a set of remote peers.
3232
2) Each remote peer responds with a digest of its messages and returns that NONCE.
@@ -82,14 +82,14 @@ type PullAdapter interface {
8282

8383
// SendDigest sends a digest to a remote PullEngine.
8484
// The context parameter specifies the remote engine to send to.
85-
SendDigest(digest []uint64, nonce uint64, context interface{})
85+
SendDigest(digest []string, nonce uint64, context interface{})
8686

8787
// SendReq sends an array of items to a certain remote PullEngine identified
8888
// by a string
89-
SendReq(dest string, items []uint64, nonce uint64)
89+
SendReq(dest string, items []string, nonce uint64)
9090

9191
// SendRes sends an array of items to a remote PullEngine identified by a context.
92-
SendRes(items []uint64, context interface{}, nonce uint64)
92+
SendRes(items []string, context interface{}, nonce uint64)
9393
}
9494

9595
// PullEngine is the component that actually invokes the pull algorithm
@@ -98,7 +98,7 @@ type PullEngine struct {
9898
PullAdapter
9999
stopFlag int32
100100
state *util.Set
101-
item2owners map[uint64][]string
101+
item2owners map[string][]string
102102
peers2nonces map[string]uint64
103103
nonces2peers map[uint64]string
104104
acceptingDigests int32
@@ -115,7 +115,7 @@ func NewPullEngine(participant PullAdapter, sleepTime time.Duration) *PullEngine
115115
PullAdapter: participant,
116116
stopFlag: int32(0),
117117
state: util.NewSet(),
118-
item2owners: make(map[uint64][]string),
118+
item2owners: make(map[string][]string),
119119
peers2nonces: make(map[string]uint64),
120120
nonces2peers: make(map[uint64]string),
121121
acceptingDigests: int32(0),
@@ -190,12 +190,12 @@ func (engine *PullEngine) processIncomingDigests() {
190190
engine.lock.Lock()
191191
defer engine.lock.Unlock()
192192

193-
requestMapping := make(map[string][]uint64)
193+
requestMapping := make(map[string][]string)
194194
for n, sources := range engine.item2owners {
195195
// select a random source
196196
source := sources[rand.Intn(len(sources))]
197197
if _, exists := requestMapping[source]; !exists {
198-
requestMapping[source] = make([]uint64, 0)
198+
requestMapping[source] = make([]string, 0)
199199
}
200200
// append the number to that source
201201
requestMapping[source] = append(requestMapping[source], n)
@@ -218,13 +218,13 @@ func (engine *PullEngine) endPull() {
218218
atomic.StoreInt32(&(engine.acceptingResponses), int32(0))
219219
engine.outgoingNONCES.Clear()
220220

221-
engine.item2owners = make(map[uint64][]string)
221+
engine.item2owners = make(map[string][]string)
222222
engine.peers2nonces = make(map[string]uint64)
223223
engine.nonces2peers = make(map[uint64]string)
224224
}
225225

226226
// OnDigest notifies the engine that a digest has arrived
227-
func (engine *PullEngine) OnDigest(digest []uint64, nonce uint64, context interface{}) {
227+
func (engine *PullEngine) OnDigest(digest []string, nonce uint64, context interface{}) {
228228
if !engine.isAcceptingDigests() || !engine.outgoingNONCES.Exists(nonce) {
229229
return
230230
}
@@ -246,14 +246,14 @@ func (engine *PullEngine) OnDigest(digest []uint64, nonce uint64, context interf
246246
}
247247

248248
// Add adds items to the state
249-
func (engine *PullEngine) Add(seqs ...uint64) {
249+
func (engine *PullEngine) Add(seqs ...string) {
250250
for _, seq := range seqs {
251251
engine.state.Add(seq)
252252
}
253253
}
254254

255255
// Remove removes items from the state
256-
func (engine *PullEngine) Remove(seqs ...uint64) {
256+
func (engine *PullEngine) Remove(seqs ...string) {
257257
for _, seq := range seqs {
258258
engine.state.Remove(seq)
259259
}
@@ -267,21 +267,21 @@ func (engine *PullEngine) OnHello(nonce uint64, context interface{}) {
267267
})
268268

269269
a := engine.state.ToArray()
270-
digest := make([]uint64, len(a))
270+
digest := make([]string, len(a))
271271
for i, item := range a {
272-
digest[i] = item.(uint64)
272+
digest[i] = item.(string)
273273
}
274274
engine.SendDigest(digest, nonce, context)
275275
}
276276

277277
// OnReq notifies the engine a request has arrived
278-
func (engine *PullEngine) OnReq(items []uint64, nonce uint64, context interface{}) {
278+
func (engine *PullEngine) OnReq(items []string, nonce uint64, context interface{}) {
279279
if !engine.incomingNONCES.Exists(nonce) {
280280
return
281281
}
282282
engine.lock.Lock()
283283

284-
var items2Send []uint64
284+
var items2Send []string
285285
for _, item := range items {
286286
if engine.state.Exists(item) {
287287
items2Send = append(items2Send, item)
@@ -294,7 +294,7 @@ func (engine *PullEngine) OnReq(items []uint64, nonce uint64, context interface{
294294
}
295295

296296
// OnRes notifies the engine a response has arrived
297-
func (engine *PullEngine) OnRes(items []uint64, nonce uint64) {
297+
func (engine *PullEngine) OnRes(items []string, nonce uint64) {
298298
if !engine.outgoingNONCES.Exists(nonce) || !engine.isAcceptingResponses() {
299299
return
300300
}

0 commit comments

Comments
 (0)