Skip to content

Commit e96eea9

Browse files
committed
Tune gossip default bootstrap and skip localhost conn
Currently, the peer/core.yaml is configured to connect to 0.0.0.0:7051 This makes gossip emit annoying warnings about unable to connect to such an address to the logs. I changed the default to be 127.0.0.1:7051 On top of that, there is no reason to connect to a bootstrap address which is yourself. Also made the bootstrap connection process skip such an endpoint. Added a test that checks the filtering function I introduced, and also added a test in the discovery layer that fails if a connection to yourself is being attempted. Signed-off-by: Yacov Manevich <[email protected]> Change-Id: Ifeaa04aaf385cd21b6671c94441d7e3612ea59e0
1 parent b274f53 commit e96eea9

File tree

3 files changed

+75
-2
lines changed

3 files changed

+75
-2
lines changed

gossip/discovery/discovery_impl.go

+31
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ import (
2323
"sync/atomic"
2424
"time"
2525

26+
"strconv"
27+
"strings"
28+
2629
"github.com/hyperledger/fabric/gossip/common"
2730
"github.com/hyperledger/fabric/gossip/util"
2831
proto "github.com/hyperledger/fabric/protos/gossip"
@@ -153,8 +156,25 @@ func (d *gossipDiscoveryImpl) Connect(member NetworkMember) {
153156
}
154157

155158
func (d *gossipDiscoveryImpl) connect2BootstrapPeers(endpoints []string) {
159+
if d.self.InternalEndpoint == nil || len(d.self.InternalEndpoint.Endpoint) == 0 {
160+
d.logger.Panic("Internal endpoint is empty:", d.self.InternalEndpoint)
161+
}
162+
163+
if len(strings.Split(d.self.InternalEndpoint.Endpoint, ":")) != 2 {
164+
d.logger.Panicf("Self endpoint %s isn't formatted as 'host:port'", d.self.InternalEndpoint.Endpoint)
165+
}
166+
167+
myPort, err := strconv.ParseInt(strings.Split(d.self.InternalEndpoint.Endpoint, ":")[1], 10, 64)
168+
if err != nil {
169+
d.logger.Panicf("Self endpoint %s has not valid port'", d.self.InternalEndpoint.Endpoint)
170+
}
171+
156172
d.logger.Info("Entering:", endpoints)
157173
defer d.logger.Info("Exiting")
174+
endpoints = filterOutLocalhost(endpoints, int(myPort))
175+
if len(endpoints) == 0 {
176+
return
177+
}
158178

159179
for !d.somePeerIsKnown() {
160180
var wg sync.WaitGroup
@@ -822,3 +842,14 @@ func getAliveExpirationCheckInterval() time.Duration {
822842
func getReconnectInterval() time.Duration {
823843
return util.GetDurationOrDefault("peer.gossip.reconnectInterval", getAliveExpirationTimeout())
824844
}
845+
846+
func filterOutLocalhost(endpoints []string, port int) []string {
847+
var returnedEndpoints []string
848+
for _, endpoint := range endpoints {
849+
if endpoint == fmt.Sprintf("127.0.0.1:%d", port) || endpoint == fmt.Sprintf("localhost:%d", port) {
850+
continue
851+
}
852+
returnedEndpoints = append(returnedEndpoints, endpoint)
853+
}
854+
return returnedEndpoints
855+
}

gossip/discovery/discovery_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
proto "github.com/hyperledger/fabric/protos/gossip"
3131
"github.com/spf13/viper"
3232
"github.com/stretchr/testify/assert"
33+
"github.com/stretchr/testify/mock"
3334
"golang.org/x/net/context"
3435
"google.golang.org/grpc"
3536
)
@@ -54,6 +55,7 @@ type dummyCommModule struct {
5455
incMsgs chan *proto.GossipMessage
5556
lastSeqs map[string]uint64
5657
shouldGossip bool
58+
mock *mock.Mock
5759
}
5860

5961
type gossipMsg struct {
@@ -92,10 +94,16 @@ func (comm *dummyCommModule) Gossip(msg *proto.GossipMessage) {
9294
}
9395

9496
func (comm *dummyCommModule) SendToPeer(peer *NetworkMember, msg *proto.GossipMessage) {
97+
var mock *mock.Mock
9598
comm.lock.RLock()
9699
_, exists := comm.streams[peer.Endpoint]
100+
mock = comm.mock
97101
comm.lock.RUnlock()
98102

103+
if mock != nil {
104+
mock.Called()
105+
}
106+
99107
if !exists {
100108
if comm.Ping(peer) == false {
101109
fmt.Printf("Ping to %v failed\n", peer.Endpoint)
@@ -111,6 +119,10 @@ func (comm *dummyCommModule) Ping(peer *NetworkMember) bool {
111119
comm.lock.Lock()
112120
defer comm.lock.Unlock()
113121

122+
if comm.mock != nil {
123+
comm.mock.Called()
124+
}
125+
114126
_, alreadyExists := comm.streams[peer.Endpoint]
115127
if !alreadyExists {
116128
newConn, err := grpc.Dial(peer.Endpoint, grpc.WithInsecure())
@@ -451,6 +463,22 @@ func TestGossipDiscoveryStopping(t *testing.T) {
451463

452464
}
453465

466+
func TestGossipDiscoverySkipConnectingToLocalhostBootstrap(t *testing.T) {
467+
t.Parallel()
468+
inst := createDiscoveryInstance(11611, "d1", []string{"localhost:11611", "127.0.0.1:11611"})
469+
inst.comm.lock.Lock()
470+
inst.comm.mock = &mock.Mock{}
471+
inst.comm.mock.On("SendToPeer", mock.Anything).Run(func(mock.Arguments) {
472+
t.Fatal("Should not have connected to any peer")
473+
})
474+
inst.comm.mock.On("Ping", mock.Anything).Run(func(mock.Arguments) {
475+
t.Fatal("Should not have connected to any peer")
476+
})
477+
inst.comm.lock.Unlock()
478+
time.Sleep(time.Second * 3)
479+
waitUntilOrFailBlocking(t, inst.Stop)
480+
}
481+
454482
func TestConvergence(t *testing.T) {
455483
t.Parallel()
456484
// scenario:
@@ -522,6 +550,20 @@ func TestConfigFromFile(t *testing.T) {
522550
assert.Equal(t, time.Duration(25)*time.Second, getReconnectInterval())
523551
}
524552

553+
func TestFilterOutLocalhost(t *testing.T) {
554+
endpoints := []string{"localhost:5611", "127.0.0.1:5611", "1.2.3.4:5611"}
555+
assert.Len(t, filterOutLocalhost(endpoints, 5611), 1)
556+
endpoints = []string{"1.2.3.4:5611"}
557+
assert.Len(t, filterOutLocalhost(endpoints, 5611), 1)
558+
endpoints = []string{"localhost:5611", "127.0.0.1:5611"}
559+
assert.Len(t, filterOutLocalhost(endpoints, 5611), 0)
560+
// Check slice returned is a copy
561+
endpoints = []string{"localhost:5611", "127.0.0.1:5611", "1.2.3.4:5611"}
562+
endpoints2 := filterOutLocalhost(endpoints, 5611)
563+
endpoints2[0] = "bla bla"
564+
assert.NotEqual(t, endpoints[2], endpoints[0])
565+
}
566+
525567
func waitUntilOrFail(t *testing.T, pred func() bool) {
526568
start := time.Now()
527569
limit := start.UnixNano() + timeout.Nanoseconds()

peer/core.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ peer:
6969

7070
# Gossip related configuration
7171
gossip:
72-
bootstrap: 0.0.0.0:7051
73-
# For debug - is peer is its org leader and should pass blocks from orderer to other peers in org
72+
bootstrap: 127.0.0.1:7051
73+
# Is peer is its org leader and should pass blocks from orderer to other peers in org
7474
orgLeader: true
7575
# ID of this instance
7676
endpoint:

0 commit comments

Comments
 (0)