2021-11-03 14:38:45 +08:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/cluster"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
|
2022-01-12 17:11:25 +08:00
|
|
|
"math/rand"
|
2021-11-03 14:38:45 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func (ms *MasterServer) ListClusterNodes(ctx context.Context, req *master_pb.ListClusterNodesRequest) (*master_pb.ListClusterNodesResponse, error) {
|
|
|
|
resp := &master_pb.ListClusterNodesResponse{}
|
2022-07-11 05:01:53 +08:00
|
|
|
filerGroup := cluster.FilerGroupName(req.FilerGroup)
|
2021-11-03 14:38:45 +08:00
|
|
|
|
2022-07-11 15:20:27 +08:00
|
|
|
if req.IsLeaderOnly {
|
|
|
|
leaders := ms.Cluster.ListClusterNodeLeaders(filerGroup, req.ClientType)
|
|
|
|
for _, node := range leaders {
|
|
|
|
resp.ClusterNodes = append(resp.ClusterNodes, &master_pb.ListClusterNodesResponse_ClusterNode{
|
|
|
|
Address: string(node),
|
|
|
|
IsLeader: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
clusterNodes := ms.Cluster.ListClusterNode(filerGroup, req.ClientType)
|
|
|
|
clusterNodes = limitTo(clusterNodes, req.Limit)
|
|
|
|
for _, node := range clusterNodes {
|
|
|
|
resp.ClusterNodes = append(resp.ClusterNodes, &master_pb.ListClusterNodesResponse_ClusterNode{
|
|
|
|
Address: string(node.Address),
|
|
|
|
Version: node.Version,
|
|
|
|
IsLeader: ms.Cluster.IsOneLeader(filerGroup, req.ClientType, node.Address),
|
|
|
|
CreatedAtNs: node.CreatedTs.UnixNano(),
|
|
|
|
DataCenter: string(node.DataCenter),
|
|
|
|
Rack: string(node.Rack),
|
|
|
|
})
|
|
|
|
}
|
2021-11-03 14:38:45 +08:00
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
2022-01-12 17:11:25 +08:00
|
|
|
|
2022-07-11 05:01:53 +08:00
|
|
|
func (ms *MasterServer) GetOneFiler(filerGroup cluster.FilerGroupName) pb.ServerAddress {
|
2022-01-12 17:11:25 +08:00
|
|
|
|
2022-05-02 12:59:16 +08:00
|
|
|
clusterNodes := ms.Cluster.ListClusterNode(filerGroup, cluster.FilerType)
|
2022-01-12 17:11:25 +08:00
|
|
|
|
|
|
|
var filers []pb.ServerAddress
|
|
|
|
for _, node := range clusterNodes {
|
2022-07-11 05:01:53 +08:00
|
|
|
if ms.Cluster.IsOneLeader(filerGroup, cluster.FilerType, node.Address) {
|
2022-01-12 17:11:25 +08:00
|
|
|
filers = append(filers, node.Address)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(filers) > 0 {
|
|
|
|
return filers[rand.Intn(len(filers))]
|
|
|
|
}
|
|
|
|
return "localhost:8888"
|
|
|
|
}
|
2022-07-11 15:20:27 +08:00
|
|
|
|
|
|
|
func limitTo(nodes []*cluster.ClusterNode, limit int32) (selected []*cluster.ClusterNode) {
|
|
|
|
if limit <= 0 || len(nodes) < int(limit) {
|
|
|
|
return nodes
|
|
|
|
}
|
2022-09-15 18:12:13 +08:00
|
|
|
selectedSet := make(map[pb.ServerAddress]*cluster.ClusterNode)
|
2022-07-11 15:20:27 +08:00
|
|
|
for i := 0; i < int(limit)*3; i++ {
|
|
|
|
x := rand.Intn(len(nodes))
|
2022-09-15 18:12:13 +08:00
|
|
|
if _, found := selectedSet[nodes[x].Address]; found {
|
2022-07-11 15:20:27 +08:00
|
|
|
continue
|
|
|
|
}
|
2022-09-15 18:12:13 +08:00
|
|
|
selectedSet[nodes[x].Address] = nodes[x]
|
2022-07-11 15:20:27 +08:00
|
|
|
}
|
2022-09-15 18:12:13 +08:00
|
|
|
for _, node := range selectedSet {
|
2022-07-11 15:20:27 +08:00
|
|
|
selected = append(selected, node)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|