1
0
mirror of https://github.com/seaweedfs/seaweedfs.git synced 2024-12-17 12:07:50 +08:00
seaweedfs/weed/cluster/cluster.go

152 lines
4.3 KiB
Go
Raw Normal View History

2021-11-08 15:52:40 +08:00
package cluster
2021-11-04 15:54:38 +08:00
import (
"github.com/seaweedfs/seaweedfs/weed/pb"
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
2021-11-04 15:54:38 +08:00
"sync"
"time"
)
2021-11-08 16:09:11 +08:00
const (
2022-04-02 07:55:26 +08:00
MasterType = "master"
VolumeServerType = "volumeServer"
FilerType = "filer"
BrokerType = "broker"
2021-11-08 16:09:11 +08:00
)
2022-07-11 05:01:53 +08:00
type FilerGroupName string
2022-07-03 15:29:25 +08:00
type DataCenter string
type Rack string
2021-11-04 15:54:38 +08:00
type ClusterNode struct {
Address pb.ServerAddress
Version string
counter int
CreatedTs time.Time
DataCenter DataCenter
Rack Rack
2021-11-04 15:54:38 +08:00
}
2023-06-20 14:51:04 +08:00
2022-07-11 05:01:53 +08:00
type ClusterNodeGroups struct {
groupMembers map[FilerGroupName]*GroupMembers
sync.RWMutex
}
2021-11-04 15:54:38 +08:00
type Cluster struct {
2022-07-11 05:01:53 +08:00
filerGroups *ClusterNodeGroups
brokerGroups *ClusterNodeGroups
2021-11-04 15:54:38 +08:00
}
2022-07-11 05:01:53 +08:00
func newClusterNodeGroups() *ClusterNodeGroups {
return &ClusterNodeGroups{
groupMembers: map[FilerGroupName]*GroupMembers{},
2021-11-04 15:54:38 +08:00
}
}
2022-07-11 05:01:53 +08:00
func (g *ClusterNodeGroups) getGroupMembers(filerGroup FilerGroupName, createIfNotFound bool) *GroupMembers {
2022-07-11 15:20:27 +08:00
members, found := g.groupMembers[filerGroup]
2022-05-02 12:59:16 +08:00
if !found && createIfNotFound {
2023-06-20 15:12:59 +08:00
members = newGroupMembers()
2022-07-11 15:20:27 +08:00
g.groupMembers[filerGroup] = members
2022-05-02 12:59:16 +08:00
}
2022-07-11 15:20:27 +08:00
return members
2022-05-02 12:59:16 +08:00
}
2022-07-11 05:01:53 +08:00
func (g *ClusterNodeGroups) AddClusterNode(filerGroup FilerGroupName, nodeType string, dataCenter DataCenter, rack Rack, address pb.ServerAddress, version string) []*master_pb.KeepConnectedResponse {
g.Lock()
defer g.Unlock()
m := g.getGroupMembers(filerGroup, true)
if t := m.addMember(dataCenter, rack, address, version); t != nil {
2023-06-20 09:19:13 +08:00
return buildClusterNodeUpdateMessage(true, filerGroup, nodeType, address)
2022-07-11 05:01:53 +08:00
}
return nil
}
func (g *ClusterNodeGroups) RemoveClusterNode(filerGroup FilerGroupName, nodeType string, address pb.ServerAddress) []*master_pb.KeepConnectedResponse {
g.Lock()
defer g.Unlock()
m := g.getGroupMembers(filerGroup, false)
if m == nil {
return nil
}
if m.removeMember(address) {
2023-06-20 09:19:13 +08:00
return buildClusterNodeUpdateMessage(false, filerGroup, nodeType, address)
2022-07-11 05:01:53 +08:00
}
return nil
}
func (g *ClusterNodeGroups) ListClusterNode(filerGroup FilerGroupName) (nodes []*ClusterNode) {
g.Lock()
defer g.Unlock()
m := g.getGroupMembers(filerGroup, false)
if m == nil {
return nil
}
for _, node := range m.members {
nodes = append(nodes, node)
}
return
}
2022-07-11 15:20:27 +08:00
2022-07-11 05:01:53 +08:00
func NewCluster() *Cluster {
return &Cluster{
filerGroups: newClusterNodeGroups(),
brokerGroups: newClusterNodeGroups(),
}
}
func (cluster *Cluster) getGroupMembers(filerGroup FilerGroupName, nodeType string, createIfNotFound bool) *GroupMembers {
2021-11-04 15:54:38 +08:00
switch nodeType {
2021-11-08 16:09:11 +08:00
case FilerType:
2022-07-11 05:01:53 +08:00
return cluster.filerGroups.getGroupMembers(filerGroup, createIfNotFound)
2021-11-10 00:50:55 +08:00
case BrokerType:
2022-07-11 05:01:53 +08:00
return cluster.brokerGroups.getGroupMembers(filerGroup, createIfNotFound)
}
return nil
}
2022-07-03 15:29:25 +08:00
2022-07-11 05:01:53 +08:00
func (cluster *Cluster) AddClusterNode(ns, nodeType string, dataCenter DataCenter, rack Rack, address pb.ServerAddress, version string) []*master_pb.KeepConnectedResponse {
filerGroup := FilerGroupName(ns)
switch nodeType {
case FilerType:
return cluster.filerGroups.AddClusterNode(filerGroup, nodeType, dataCenter, rack, address, version)
case BrokerType:
return cluster.brokerGroups.AddClusterNode(filerGroup, nodeType, dataCenter, rack, address, version)
2021-11-08 16:09:11 +08:00
case MasterType:
2023-06-20 09:21:07 +08:00
return buildClusterNodeUpdateMessage(true, filerGroup, nodeType, address)
2021-11-04 15:54:38 +08:00
}
2021-11-06 19:07:38 +08:00
return nil
2021-11-04 15:54:38 +08:00
}
2022-07-11 05:01:53 +08:00
func (cluster *Cluster) RemoveClusterNode(ns string, nodeType string, address pb.ServerAddress) []*master_pb.KeepConnectedResponse {
filerGroup := FilerGroupName(ns)
2021-11-04 15:54:38 +08:00
switch nodeType {
2021-11-08 16:09:11 +08:00
case FilerType:
2022-07-11 05:01:53 +08:00
return cluster.filerGroups.RemoveClusterNode(filerGroup, nodeType, address)
2021-11-10 00:50:55 +08:00
case BrokerType:
2022-07-11 05:01:53 +08:00
return cluster.brokerGroups.RemoveClusterNode(filerGroup, nodeType, address)
2021-11-08 16:09:11 +08:00
case MasterType:
2023-06-20 09:21:07 +08:00
return buildClusterNodeUpdateMessage(false, filerGroup, nodeType, address)
2021-11-04 15:54:38 +08:00
}
2021-11-06 19:07:38 +08:00
return nil
2021-11-04 15:54:38 +08:00
}
2022-07-11 05:01:53 +08:00
func (cluster *Cluster) ListClusterNode(filerGroup FilerGroupName, nodeType string) (nodes []*ClusterNode) {
2021-11-04 15:54:38 +08:00
switch nodeType {
2021-11-08 16:09:11 +08:00
case FilerType:
2022-07-11 05:01:53 +08:00
return cluster.filerGroups.ListClusterNode(filerGroup)
2021-11-10 00:50:55 +08:00
case BrokerType:
2022-07-11 05:01:53 +08:00
return cluster.brokerGroups.ListClusterNode(filerGroup)
2021-11-08 16:09:11 +08:00
case MasterType:
2021-11-04 15:54:38 +08:00
}
return
}
2021-11-07 05:23:35 +08:00
2023-06-20 09:19:13 +08:00
func buildClusterNodeUpdateMessage(isAdd bool, filerGroup FilerGroupName, nodeType string, address pb.ServerAddress) (result []*master_pb.KeepConnectedResponse) {
result = append(result, &master_pb.KeepConnectedResponse{
ClusterNodeUpdate: &master_pb.ClusterNodeUpdate{
FilerGroup: string(filerGroup),
NodeType: nodeType,
Address: string(address),
IsAdd: isAdd,
},
})
2021-11-04 15:54:38 +08:00
return
}