seaweedfs/weed/cluster/cluster_test.go

82 lines
2.3 KiB
Go
Raw Normal View History

2021-11-08 07:52:40 +00:00
package cluster
2021-11-04 07:54:38 +00:00
import (
"github.com/seaweedfs/seaweedfs/weed/pb"
2021-11-04 07:54:38 +00:00
"github.com/stretchr/testify/assert"
"strconv"
"sync"
2021-11-04 07:54:38 +00:00
"testing"
)
func TestClusterAddRemoveNodes(t *testing.T) {
c := NewCluster()
2022-07-03 07:29:25 +00:00
c.AddClusterNode("", "filer", "", "", pb.ServerAddress("111:1"), "23.45")
c.AddClusterNode("", "filer", "", "", pb.ServerAddress("111:2"), "23.45")
2021-11-04 07:54:38 +00:00
assert.Equal(t, []pb.ServerAddress{
pb.ServerAddress("111:1"),
pb.ServerAddress("111:2"),
2022-07-10 21:01:53 +00:00
}, c.getGroupMembers("", "filer", true).leaders.GetLeaders())
2021-11-04 07:54:38 +00:00
2022-07-03 07:29:25 +00:00
c.AddClusterNode("", "filer", "", "", pb.ServerAddress("111:3"), "23.45")
c.AddClusterNode("", "filer", "", "", pb.ServerAddress("111:4"), "23.45")
2021-11-04 07:54:38 +00:00
assert.Equal(t, []pb.ServerAddress{
pb.ServerAddress("111:1"),
pb.ServerAddress("111:2"),
pb.ServerAddress("111:3"),
2022-07-10 21:01:53 +00:00
}, c.getGroupMembers("", "filer", true).leaders.GetLeaders())
2021-11-04 07:54:38 +00:00
2022-07-03 07:29:25 +00:00
c.AddClusterNode("", "filer", "", "", pb.ServerAddress("111:5"), "23.45")
c.AddClusterNode("", "filer", "", "", pb.ServerAddress("111:6"), "23.45")
2022-07-10 21:01:53 +00:00
c.RemoveClusterNode("", "filer", pb.ServerAddress("111:4"))
2021-11-04 07:54:38 +00:00
assert.Equal(t, []pb.ServerAddress{
pb.ServerAddress("111:1"),
pb.ServerAddress("111:2"),
pb.ServerAddress("111:3"),
2022-07-10 21:01:53 +00:00
}, c.getGroupMembers("", "filer", true).leaders.GetLeaders())
2021-11-04 07:54:38 +00:00
// remove oldest
2022-07-10 21:01:53 +00:00
c.RemoveClusterNode("", "filer", pb.ServerAddress("111:1"))
2021-11-04 07:54:38 +00:00
assert.Equal(t, []pb.ServerAddress{
pb.ServerAddress("111:6"),
pb.ServerAddress("111:2"),
pb.ServerAddress("111:3"),
2022-07-10 21:01:53 +00:00
}, c.getGroupMembers("", "filer", true).leaders.GetLeaders())
2021-11-04 07:54:38 +00:00
// remove oldest
2022-07-10 21:01:53 +00:00
c.RemoveClusterNode("", "filer", pb.ServerAddress("111:1"))
2021-11-04 07:54:38 +00:00
}
func TestConcurrentAddRemoveNodes(t *testing.T) {
c := NewCluster()
var wg sync.WaitGroup
for i := 0; i < 50; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
address := strconv.Itoa(i)
2022-07-03 07:29:25 +00:00
c.AddClusterNode("", "filer", "", "", pb.ServerAddress(address), "23.45")
}(i)
}
wg.Wait()
for i := 0; i < 50; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
address := strconv.Itoa(i)
2022-07-10 21:01:53 +00:00
node := c.RemoveClusterNode("", "filer", pb.ServerAddress(address))
if len(node) == 0 {
t.Errorf("TestConcurrentAddRemoveNodes: node[%s] not found", address)
return
} else if node[0].ClusterNodeUpdate.Address != address {
t.Errorf("TestConcurrentAddRemoveNodes: expect:%s, actual:%s", address, node[0].ClusterNodeUpdate.Address)
return
}
}(i)
}
wg.Wait()
}