Skip to content

Commit b5e73c2

Browse files
Leader election log
Leader election log output peer id (PKIid) as a string. In output it looks gibberish and grepping those logs not possible. This change print this PKIid as bytes array. Change-Id: Id02c1a8fe380db7d2300baf7afc6ff800c45108e Signed-off-by: Gennady Laventman <[email protected]>
1 parent 0c23ac1 commit b5e73c2

File tree

4 files changed

+24
-20
lines changed

4 files changed

+24
-20
lines changed

gossip/election/adapter.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ type msgImpl struct {
3131
msg *proto.GossipMessage
3232
}
3333

34-
func (mi *msgImpl) SenderID() string {
35-
return string(mi.msg.GetLeadershipMsg().PkiID)
34+
func (mi *msgImpl) SenderID() peerID {
35+
return mi.msg.GetLeadershipMsg().PkiID
3636
}
3737

3838
func (mi *msgImpl) IsProposal() bool {
@@ -47,8 +47,8 @@ type peerImpl struct {
4747
member *discovery.NetworkMember
4848
}
4949

50-
func (pi *peerImpl) ID() string {
51-
return string(pi.member.PKIid)
50+
func (pi *peerImpl) ID() peerID {
51+
return peerID(pi.member.PKIid)
5252
}
5353

5454
type gossip interface {

gossip/election/adapter_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package election
1818

1919
import (
20+
"bytes"
2021
"fmt"
2122
"strings"
2223
"sync"
@@ -91,7 +92,7 @@ func TestAdapterImpl_Peers(t *testing.T) {
9192
}
9293

9394
for _, peer := range peers {
94-
if _, exist := peersPKIDs[peer.ID()]; !exist {
95+
if _, exist := peersPKIDs[string(peer.ID())]; !exist {
9596
t.Errorf("Peer %s PKID not found", peer.(*peerImpl).member.Endpoint)
9697
}
9798
}
@@ -141,15 +142,15 @@ func TestAdapterImpl_Gossip(t *testing.T) {
141142
case msg := <-channels[fmt.Sprintf("Peer%d", 1)]:
142143
if !msg.IsDeclaration() {
143144
t.Error("Msg should be declaration")
144-
} else if strings.Compare(msg.SenderID(), string(sender.self.PKIid)) != 0 {
145+
} else if !bytes.Equal(msg.SenderID(), sender.self.PKIid) {
145146
t.Error("Msg Sender is wrong")
146147
} else {
147148
totalMsg++
148149
}
149150
case msg := <-channels[fmt.Sprintf("Peer%d", 2)]:
150151
if !msg.IsDeclaration() {
151152
t.Error("Msg should be declaration")
152-
} else if strings.Compare(msg.SenderID(), string(sender.self.PKIid)) != 0 {
153+
} else if !bytes.Equal(msg.SenderID(), sender.self.PKIid) {
153154
t.Error("Msg Sender is wrong")
154155
} else {
155156
totalMsg++

gossip/election/election.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package election
1818

1919
import (
20+
"bytes"
2021
"fmt"
2122
"sync"
2223
"sync/atomic"
@@ -113,16 +114,18 @@ type LeaderElectionService interface {
113114
Stop()
114115
}
115116

117+
type peerID []byte
118+
116119
// Peer describes a remote peer
117120
type Peer interface {
118121
// ID returns the ID of the peer
119-
ID() string
122+
ID() peerID
120123
}
121124

122125
// Msg describes a message sent from a remote peer
123126
type Msg interface {
124127
// SenderID returns the ID of the peer sent the message
125-
SenderID() string
128+
SenderID() peerID
126129
// IsProposal returns whether this message is a leadership proposal
127130
IsProposal() bool
128131
// IsDeclaration returns whether this message is a leadership declaration
@@ -138,7 +141,7 @@ func NewLeaderElectionService(adapter LeaderElectionAdapter, id string, callback
138141
panic(fmt.Errorf("Empty id"))
139142
}
140143
le := &leaderElectionSvcImpl{
141-
id: id,
144+
id: peerID(id),
142145
proposals: util.NewSet(),
143146
adapter: adapter,
144147
stopChan: make(chan struct{}, 1),
@@ -157,7 +160,7 @@ func NewLeaderElectionService(adapter LeaderElectionAdapter, id string, callback
157160

158161
// leaderElectionSvcImpl is an implementation of a LeaderElectionService
159162
type leaderElectionSvcImpl struct {
160-
id string
163+
id peerID
161164
proposals *util.Set
162165
sync.Mutex
163166
stopChan chan struct{}
@@ -209,13 +212,13 @@ func (le *leaderElectionSvcImpl) handleMessage(msg Msg) {
209212
defer le.Unlock()
210213

211214
if msg.IsProposal() {
212-
le.proposals.Add(msg.SenderID())
215+
le.proposals.Add(string(msg.SenderID()))
213216
} else if msg.IsDeclaration() {
214217
atomic.StoreInt32(&le.leaderExists, int32(1))
215218
if le.sleeping && len(le.interruptChan) == 0 {
216219
le.interruptChan <- struct{}{}
217220
}
218-
if msg.SenderID() < le.id && le.IsLeader() {
221+
if bytes.Compare(msg.SenderID(), le.id) < 0 && le.IsLeader() {
219222
le.stopBeingLeader()
220223
}
221224
} else {
@@ -281,7 +284,7 @@ func (le *leaderElectionSvcImpl) leaderElection() {
281284
// for being a leader
282285
for _, o := range le.proposals.ToArray() {
283286
id := o.(string)
284-
if id < le.id {
287+
if bytes.Compare(peerID(id), le.id) < 0 {
285288
return
286289
}
287290
}
@@ -344,9 +347,9 @@ func (le *leaderElectionSvcImpl) drainInterruptChannel() {
344347
}
345348

346349
// isAlive returns whether peer of given id is considered alive
347-
func (le *leaderElectionSvcImpl) isAlive(id string) bool {
350+
func (le *leaderElectionSvcImpl) isAlive(id peerID) bool {
348351
for _, p := range le.adapter.Peers() {
349-
if p.ID() == id {
352+
if bytes.Equal(p.ID(), id) {
350353
return true
351354
}
352355
}

gossip/election/election_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ type msg struct {
4545
proposal bool
4646
}
4747

48-
func (m *msg) SenderID() string {
49-
return m.sender
48+
func (m *msg) SenderID() peerID {
49+
return peerID(m.sender)
5050
}
5151

5252
func (m *msg) IsProposal() bool {
@@ -76,8 +76,8 @@ func (p *peer) On(methodName string, arguments ...interface{}) *mock.Call {
7676
return p.Mock.On(methodName, arguments...)
7777
}
7878

79-
func (p *peer) ID() string {
80-
return p.id
79+
func (p *peer) ID() peerID {
80+
return peerID(p.id)
8181
}
8282

8383
func (p *peer) Gossip(m Msg) {

0 commit comments

Comments
 (0)