-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[cherry-pick] PWX-35430: Honor cluster domains in round-robin balance…
…r. (#2391) - When the cluster is running with cluster-domains enabled, the round robin balancer should choose the nodes to forward the request within the same cluster domain. Signed-off-by: Aditya Dani <[email protected]>
- Loading branch information
1 parent
0566ec9
commit 553a9d1
Showing
5 changed files
with
209 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package loadbalancer | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/libopenstorage/openstorage/api" | ||
"github.com/libopenstorage/openstorage/cluster/mock" | ||
"github.com/libopenstorage/openstorage/pkg/sched" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var ( | ||
ips = []string{"127.0.0.1", "127.0.0.2", "127.0.0.3", "127.0.0.4", "127.0.0.5", "127.0.0.6"} | ||
ids = []string{"1", "2", "3", "4", "5", "6"} | ||
) | ||
|
||
func getMockClusterResponse(enableClusterDomain bool) *api.Cluster { | ||
nodes := make([]*api.Node, 0) | ||
for i := 0; i < len(ips); i++ { | ||
node := &api.Node{ | ||
MgmtIp: ips[i], | ||
Id: ids[i], | ||
} | ||
if enableClusterDomain { | ||
if i%2 == 0 { | ||
node.DomainID = "domain1" | ||
} else { | ||
node.DomainID = "domain2" | ||
} | ||
} | ||
nodes = append(nodes, node) | ||
} | ||
return &api.Cluster{ | ||
NodeId: ids[0], // self node | ||
Nodes: nodes, | ||
} | ||
} | ||
|
||
func TestGetRemoteNodeWithoutDomains(t *testing.T) { | ||
if sched.Instance() == nil { | ||
sched.Init(time.Second) | ||
} | ||
ctrl := gomock.NewController(t) | ||
cc := mock.NewMockCluster(ctrl) | ||
rr, err := NewRoundRobinBalancer(cc, "1234") | ||
require.NoError(t, err, "failed to create round robin balancer") | ||
|
||
cc.EXPECT().Enumerate().Return(*getMockClusterResponse(false), nil).AnyTimes() | ||
cc.EXPECT().Inspect(ids[0]).Return(api.Node{MgmtIp: ips[0], Id: ids[0]}, nil).AnyTimes() | ||
|
||
for loop := 0; loop < 2; loop++ { | ||
targetNode, isRemoteConn, err := rr.GetRemoteNode() | ||
require.NoError(t, err, "failed to get remote node") | ||
require.Equal(t, targetNode, ips[0], "target node is not as expected") | ||
require.False(t, isRemoteConn, "isRemoteConn is not as expected") | ||
|
||
for i := 1; i < len(ips); i++ { | ||
targetNode, isRemoteConn, err := rr.GetRemoteNode() | ||
require.NoError(t, err, "failed to get remote node") | ||
require.Equal(t, targetNode, ips[i], "target node is not as expected") | ||
require.True(t, isRemoteConn, "isRemoteConn is not as expected") | ||
|
||
} | ||
} | ||
} | ||
|
||
func TestGetRemoteNodeWithDomains(t *testing.T) { | ||
if sched.Instance() == nil { | ||
sched.Init(time.Second) | ||
} | ||
ctrl := gomock.NewController(t) | ||
cc := mock.NewMockCluster(ctrl) | ||
rr, err := NewRoundRobinBalancer(cc, "1234") | ||
require.NoError(t, err, "failed to create round robin balancer") | ||
|
||
cc.EXPECT().Enumerate().Return(*getMockClusterResponse(true), nil).AnyTimes() | ||
cc.EXPECT().Inspect(ids[0]).Return(api.Node{MgmtIp: ips[0], Id: ids[0], DomainID: "domain1"}, nil).AnyTimes() | ||
|
||
for loop := 0; loop < 2; loop++ { | ||
targetNode, isRemoteConn, err := rr.GetRemoteNode() | ||
require.NoError(t, err, "failed to get remote node") | ||
require.Equal(t, targetNode, ips[0], "target node is not as expected") | ||
require.False(t, isRemoteConn, "isRemoteConn is not as expected") | ||
|
||
for i := 1; i < 3; i++ { | ||
targetNode, isRemoteConn, err := rr.GetRemoteNode() | ||
require.NoError(t, err, "failed to get remote node") | ||
require.Equal(t, targetNode, ips[i*2], "target node is not as expected") | ||
require.True(t, isRemoteConn, "isRemoteConn is not as expected") | ||
|
||
} | ||
} | ||
} |