seaweedfs/weed/server/master_grpc_server_cluster.go

56 lines
1.6 KiB
Go
Raw Normal View History

package weed_server
import (
"context"
"github.com/seaweedfs/seaweedfs/weed/cluster"
"github.com/seaweedfs/seaweedfs/weed/pb"
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
2024-08-30 00:52:21 +08:00
"math/rand/v2"
)
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)
2023-06-20 09:19:13 +08:00
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,
CreatedAtNs: node.CreatedTs.UnixNano(),
DataCenter: string(node.DataCenter),
Rack: string(node.Rack),
})
}
return resp, nil
}
2022-07-11 05:01:53 +08:00
func (ms *MasterServer) GetOneFiler(filerGroup cluster.FilerGroupName) pb.ServerAddress {
2023-06-20 09:19:13 +08:00
filers := ms.Cluster.ListClusterNode(filerGroup, cluster.FilerType)
if len(filers) > 0 {
2024-08-30 00:52:21 +08:00
return filers[rand.IntN(len(filers))].Address
}
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
}
selectedSet := make(map[pb.ServerAddress]*cluster.ClusterNode)
2022-07-11 15:20:27 +08:00
for i := 0; i < int(limit)*3; i++ {
2024-08-30 00:52:21 +08:00
x := rand.IntN(len(nodes))
if _, found := selectedSet[nodes[x].Address]; found {
2022-07-11 15:20:27 +08:00
continue
}
selectedSet[nodes[x].Address] = nodes[x]
2022-07-11 15:20:27 +08:00
}
for _, node := range selectedSet {
2022-07-11 15:20:27 +08:00
selected = append(selected, node)
}
return
}