2021-11-08 07:52:40 +00:00
|
|
|
package cluster
|
2021-11-04 07:54:38 +00:00
|
|
|
|
|
|
|
import (
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
|
2021-11-04 07:54:38 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2021-11-08 08:09:11 +00:00
|
|
|
const (
|
2022-04-01 23:55:26 +00:00
|
|
|
MasterType = "master"
|
|
|
|
VolumeServerType = "volumeServer"
|
|
|
|
FilerType = "filer"
|
|
|
|
BrokerType = "broker"
|
2021-11-08 08:09:11 +00:00
|
|
|
)
|
|
|
|
|
2022-07-10 21:01:53 +00:00
|
|
|
type FilerGroupName string
|
2022-07-03 07:29:25 +00:00
|
|
|
type DataCenter string
|
|
|
|
type Rack string
|
|
|
|
|
2021-11-04 07:54:38 +00:00
|
|
|
type ClusterNode struct {
|
2022-07-03 07:55:37 +00:00
|
|
|
Address pb.ServerAddress
|
|
|
|
Version string
|
|
|
|
counter int
|
|
|
|
CreatedTs time.Time
|
|
|
|
DataCenter DataCenter
|
|
|
|
Rack Rack
|
2021-11-04 07:54:38 +00:00
|
|
|
}
|
2023-06-20 06:51:04 +00:00
|
|
|
|
2022-07-10 21:01:53 +00:00
|
|
|
type ClusterNodeGroups struct {
|
|
|
|
groupMembers map[FilerGroupName]*GroupMembers
|
|
|
|
sync.RWMutex
|
|
|
|
}
|
2021-11-04 07:54:38 +00:00
|
|
|
type Cluster struct {
|
2022-07-10 21:01:53 +00:00
|
|
|
filerGroups *ClusterNodeGroups
|
|
|
|
brokerGroups *ClusterNodeGroups
|
2021-11-04 07:54:38 +00:00
|
|
|
}
|
|
|
|
|
2022-07-10 21:01:53 +00:00
|
|
|
func newClusterNodeGroups() *ClusterNodeGroups {
|
|
|
|
return &ClusterNodeGroups{
|
|
|
|
groupMembers: map[FilerGroupName]*GroupMembers{},
|
2021-11-04 07:54:38 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-10 21:01:53 +00:00
|
|
|
func (g *ClusterNodeGroups) getGroupMembers(filerGroup FilerGroupName, createIfNotFound bool) *GroupMembers {
|
2022-07-11 07:20:27 +00:00
|
|
|
members, found := g.groupMembers[filerGroup]
|
2022-05-02 04:59:16 +00:00
|
|
|
if !found && createIfNotFound {
|
2023-06-20 07:12:59 +00:00
|
|
|
members = newGroupMembers()
|
2022-07-11 07:20:27 +00:00
|
|
|
g.groupMembers[filerGroup] = members
|
2022-05-02 04:59:16 +00:00
|
|
|
}
|
2022-07-11 07:20:27 +00:00
|
|
|
return members
|
2022-05-02 04:59:16 +00:00
|
|
|
}
|
|
|
|
|
2022-07-10 21:01:53 +00: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 01:19:13 +00:00
|
|
|
return buildClusterNodeUpdateMessage(true, filerGroup, nodeType, address)
|
2022-07-10 21:01:53 +00: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 01:19:13 +00:00
|
|
|
return buildClusterNodeUpdateMessage(false, filerGroup, nodeType, address)
|
2022-07-10 21:01:53 +00: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 07:20:27 +00:00
|
|
|
|
2022-07-10 21:01:53 +00:00
|
|
|
func NewCluster() *Cluster {
|
|
|
|
return &Cluster{
|
|
|
|
filerGroups: newClusterNodeGroups(),
|
|
|
|
brokerGroups: newClusterNodeGroups(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cluster *Cluster) getGroupMembers(filerGroup FilerGroupName, nodeType string, createIfNotFound bool) *GroupMembers {
|
2021-11-04 07:54:38 +00:00
|
|
|
switch nodeType {
|
2021-11-08 08:09:11 +00:00
|
|
|
case FilerType:
|
2022-07-10 21:01:53 +00:00
|
|
|
return cluster.filerGroups.getGroupMembers(filerGroup, createIfNotFound)
|
2021-11-09 16:50:55 +00:00
|
|
|
case BrokerType:
|
2022-07-10 21:01:53 +00:00
|
|
|
return cluster.brokerGroups.getGroupMembers(filerGroup, createIfNotFound)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-07-03 07:29:25 +00:00
|
|
|
|
2022-07-10 21:01:53 +00: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 08:09:11 +00:00
|
|
|
case MasterType:
|
2023-06-20 01:21:07 +00:00
|
|
|
return buildClusterNodeUpdateMessage(true, filerGroup, nodeType, address)
|
2021-11-04 07:54:38 +00:00
|
|
|
}
|
2021-11-06 11:07:38 +00:00
|
|
|
return nil
|
2021-11-04 07:54:38 +00:00
|
|
|
}
|
|
|
|
|
2022-07-10 21:01:53 +00:00
|
|
|
func (cluster *Cluster) RemoveClusterNode(ns string, nodeType string, address pb.ServerAddress) []*master_pb.KeepConnectedResponse {
|
|
|
|
filerGroup := FilerGroupName(ns)
|
2021-11-04 07:54:38 +00:00
|
|
|
switch nodeType {
|
2021-11-08 08:09:11 +00:00
|
|
|
case FilerType:
|
2022-07-10 21:01:53 +00:00
|
|
|
return cluster.filerGroups.RemoveClusterNode(filerGroup, nodeType, address)
|
2021-11-09 16:50:55 +00:00
|
|
|
case BrokerType:
|
2022-07-10 21:01:53 +00:00
|
|
|
return cluster.brokerGroups.RemoveClusterNode(filerGroup, nodeType, address)
|
2021-11-08 08:09:11 +00:00
|
|
|
case MasterType:
|
2023-06-20 01:21:07 +00:00
|
|
|
return buildClusterNodeUpdateMessage(false, filerGroup, nodeType, address)
|
2021-11-04 07:54:38 +00:00
|
|
|
}
|
2021-11-06 11:07:38 +00:00
|
|
|
return nil
|
2021-11-04 07:54:38 +00:00
|
|
|
}
|
|
|
|
|
2022-07-10 21:01:53 +00:00
|
|
|
func (cluster *Cluster) ListClusterNode(filerGroup FilerGroupName, nodeType string) (nodes []*ClusterNode) {
|
2021-11-04 07:54:38 +00:00
|
|
|
switch nodeType {
|
2021-11-08 08:09:11 +00:00
|
|
|
case FilerType:
|
2022-07-10 21:01:53 +00:00
|
|
|
return cluster.filerGroups.ListClusterNode(filerGroup)
|
2021-11-09 16:50:55 +00:00
|
|
|
case BrokerType:
|
2022-07-10 21:01:53 +00:00
|
|
|
return cluster.brokerGroups.ListClusterNode(filerGroup)
|
2021-11-08 08:09:11 +00:00
|
|
|
case MasterType:
|
2021-11-04 07:54:38 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2021-11-06 21:23:35 +00:00
|
|
|
|
2023-06-20 01:19:13 +00: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 07:54:38 +00:00
|
|
|
return
|
|
|
|
}
|