Skip to content

Commit

Permalink
check connectivity to peer before sending join cluster request.
Browse files Browse the repository at this point in the history
Fixes: gluster#1436

Signed-off-by: Oshank Kumar <[email protected]>
  • Loading branch information
Oshank Kumar authored and Madhu-1 committed Jan 3, 2019
1 parent a6fc33c commit 5dd1254
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
35 changes: 35 additions & 0 deletions e2e/peer_ops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,38 @@ func TestAddRemovePeer(t *testing.T) {
err = client.PeerRemove(g2.PeerID())
r.Nil(err)
}

func TestPeerAddFailure(t *testing.T) {
r := require.New(t)

// set up a cluster w/o glusterd instances for dependencies
tc, err := setupCluster(t)
r.NoError(err)
defer teardownCluster(tc)

g1, err := spawnGlusterd(t, "./config/1.toml", true)
r.Nil(err)
defer g1.Stop()
r.True(g1.IsRunning())

g2, err := spawnGlusterd(t, "./config/2.toml", true)
r.Nil(err)
defer g2.Stop()
r.True(g2.IsRunning())

client, err := initRestclient(g1)
r.Nil(err)
r.NotNil(client)

// using peer ID instead of peer address, this should get fail
peerAddReq := api.PeerAddReq{
Addresses: []string{g2.PeerID()},
Metadata: map[string]string{
"owner": "gd2test",
},
}

_, err = client.PeerAdd(peerAddReq)
r.NotNil(err)
r.Contains(err.Error(), "could not connect to host. Make sure host address is valid,")
}
6 changes: 6 additions & 0 deletions glusterd2/commands/peers/addpeer.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ func addPeerHandler(w http.ResponseWriter, r *http.Request) {
return
}

if err = utils.CheckPeerConnectivity(remotePeerAddress); err != nil {
logger.WithError(err).WithField("address", remotePeerAddress).Error("peer is not reachable from this node")
restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, errors.ErrConnectingHost)
return
}

// TODO: Try all addresses till the first one connects
client, err := getPeerServiceClient(remotePeerAddress)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/errors/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,5 @@ var (
ErrBrickMountFailed = errors.New("failed to mount brick")
ErrReservedGroupProfile = errors.New("reserved group profile")
ErrInvalidIntValue = errors.New("error parsing the value. Make sure the value is a valid integer")
ErrConnectingHost = errors.New("could not connect to host. Make sure host address is valid, network connection is active and gd2 is up and running")
)
11 changes: 11 additions & 0 deletions pkg/utils/peerutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"net"
"strings"
"time"

config "github.com/spf13/viper"
)
Expand Down Expand Up @@ -38,3 +39,13 @@ func IsPeerAddressSame(addr1 string, addr2 string) bool {
r2, _ := FormRemotePeerAddress(addr2)
return r1 == r2
}

// CheckPeerConnectivity will check whether given peer is reachable from this node or not.
func CheckPeerConnectivity(addr string) error {
conn, err := net.DialTimeout("tcp", addr, time.Second*5)
if err != nil {
return err
}
defer conn.Close()
return nil
}

0 comments on commit 5dd1254

Please sign in to comment.