seaweedfs/weed/cluster/cluster.go

214 lines
5.1 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/chrislusf/seaweedfs/weed/pb"
2021-11-06 11:07:38 +00:00
"github.com/chrislusf/seaweedfs/weed/pb/master_pb"
2021-11-04 07:54:38 +00:00
"math"
"sync"
"time"
)
2021-11-08 08:09:11 +00:00
const (
MasterType = "master"
2021-11-08 08:13:40 +00:00
FilerType = "filer"
BrokerType = "broker"
2021-11-08 08:09:11 +00:00
)
2021-11-04 07:54:38 +00:00
type ClusterNode struct {
Address pb.ServerAddress
Version string
counter int
createdTs time.Time
}
type Leaders struct {
leaders [3]pb.ServerAddress
}
type Cluster struct {
2021-11-08 08:13:40 +00:00
filers map[pb.ServerAddress]*ClusterNode
filersLock sync.RWMutex
filerLeaders *Leaders
2021-11-04 07:54:38 +00:00
}
func NewCluster() *Cluster {
return &Cluster{
2021-11-08 08:13:40 +00:00
filers: make(map[pb.ServerAddress]*ClusterNode),
filerLeaders: &Leaders{},
2021-11-04 07:54:38 +00:00
}
}
2021-11-06 11:07:38 +00:00
func (cluster *Cluster) AddClusterNode(nodeType string, address pb.ServerAddress, version string) []*master_pb.KeepConnectedResponse {
2021-11-04 07:54:38 +00:00
switch nodeType {
2021-11-08 08:09:11 +00:00
case FilerType:
2021-11-08 08:13:40 +00:00
cluster.filersLock.Lock()
defer cluster.filersLock.Unlock()
if existingNode, found := cluster.filers[address]; found {
2021-11-04 07:54:38 +00:00
existingNode.counter++
2021-11-06 11:07:38 +00:00
return nil
2021-11-04 07:54:38 +00:00
}
2021-11-08 08:13:40 +00:00
cluster.filers[address] = &ClusterNode{
2021-11-04 07:54:38 +00:00
Address: address,
Version: version,
counter: 1,
createdTs: time.Now(),
}
2021-11-08 08:13:40 +00:00
return cluster.ensureFilerLeaders(true, nodeType, address)
2021-11-08 08:09:11 +00:00
case MasterType:
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
}
2021-11-06 11:07:38 +00:00
func (cluster *Cluster) RemoveClusterNode(nodeType string, address pb.ServerAddress) []*master_pb.KeepConnectedResponse {
2021-11-04 07:54:38 +00:00
switch nodeType {
2021-11-08 08:09:11 +00:00
case FilerType:
2021-11-08 08:13:40 +00:00
cluster.filersLock.Lock()
defer cluster.filersLock.Unlock()
if existingNode, found := cluster.filers[address]; !found {
2021-11-06 11:07:38 +00:00
return nil
2021-11-04 07:54:38 +00:00
} else {
existingNode.counter--
if existingNode.counter <= 0 {
2021-11-08 08:13:40 +00:00
delete(cluster.filers, address)
return cluster.ensureFilerLeaders(false, nodeType, address)
2021-11-04 07:54:38 +00:00
}
}
2021-11-08 08:09:11 +00:00
case MasterType:
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
}
func (cluster *Cluster) ListClusterNode(nodeType string) (nodes []*ClusterNode) {
switch nodeType {
2021-11-08 08:09:11 +00:00
case FilerType:
2021-11-08 08:13:40 +00:00
cluster.filersLock.RLock()
defer cluster.filersLock.RUnlock()
for _, node := range cluster.filers {
2021-11-04 07:54:38 +00:00
nodes = append(nodes, node)
}
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
func (cluster *Cluster) IsOneLeader(address pb.ServerAddress) bool {
2021-11-08 08:13:40 +00:00
return cluster.filerLeaders.isOneLeader(address)
2021-11-06 21:23:35 +00:00
}
2021-11-04 07:54:38 +00:00
2021-11-08 08:13:40 +00:00
func (cluster *Cluster) ensureFilerLeaders(isAdd bool, nodeType string, address pb.ServerAddress) (result []*master_pb.KeepConnectedResponse) {
2021-11-04 07:54:38 +00:00
if isAdd {
2021-11-08 08:13:40 +00:00
if cluster.filerLeaders.addLeaderIfVacant(address) {
2021-11-04 07:54:38 +00:00
// has added the address as one leader
2021-11-06 11:07:38 +00:00
result = append(result, &master_pb.KeepConnectedResponse{
ClusterNodeUpdate: &master_pb.ClusterNodeUpdate{
NodeType: nodeType,
Address: string(address),
IsLeader: true,
IsAdd: true,
},
})
} else {
result = append(result, &master_pb.KeepConnectedResponse{
ClusterNodeUpdate: &master_pb.ClusterNodeUpdate{
NodeType: nodeType,
Address: string(address),
IsLeader: false,
IsAdd: true,
},
})
2021-11-04 07:54:38 +00:00
}
} else {
2021-11-08 08:13:40 +00:00
if cluster.filerLeaders.removeLeaderIfExists(address) {
2021-11-06 11:07:38 +00:00
result = append(result, &master_pb.KeepConnectedResponse{
ClusterNodeUpdate: &master_pb.ClusterNodeUpdate{
NodeType: nodeType,
Address: string(address),
IsLeader: true,
IsAdd: false,
},
})
2021-11-04 07:54:38 +00:00
// pick the freshest one, since it is less likely to go away
var shortestDuration int64 = math.MaxInt64
now := time.Now()
var candidateAddress pb.ServerAddress
2021-11-08 08:13:40 +00:00
for _, node := range cluster.filers {
if cluster.filerLeaders.isOneLeader(node.Address) {
2021-11-04 07:54:38 +00:00
continue
}
duration := now.Sub(node.createdTs).Nanoseconds()
if duration < shortestDuration {
shortestDuration = duration
candidateAddress = node.Address
}
}
if candidateAddress != "" {
2021-11-08 08:13:40 +00:00
cluster.filerLeaders.addLeaderIfVacant(candidateAddress)
2021-11-06 11:07:38 +00:00
// added a new leader
result = append(result, &master_pb.KeepConnectedResponse{
ClusterNodeUpdate: &master_pb.ClusterNodeUpdate{
NodeType: nodeType,
Address: string(candidateAddress),
IsLeader: true,
IsAdd: true,
},
})
2021-11-04 07:54:38 +00:00
}
2021-11-06 11:07:38 +00:00
} else {
result = append(result, &master_pb.KeepConnectedResponse{
ClusterNodeUpdate: &master_pb.ClusterNodeUpdate{
NodeType: nodeType,
Address: string(address),
IsLeader: false,
IsAdd: false,
},
})
2021-11-04 07:54:38 +00:00
}
}
2021-11-06 11:07:38 +00:00
return
2021-11-04 07:54:38 +00:00
}
func (leaders *Leaders) addLeaderIfVacant(address pb.ServerAddress) (hasChanged bool) {
if leaders.isOneLeader(address) {
return
}
for i := 0; i < len(leaders.leaders); i++ {
if leaders.leaders[i] == "" {
leaders.leaders[i] = address
hasChanged = true
return
}
}
return
}
func (leaders *Leaders) removeLeaderIfExists(address pb.ServerAddress) (hasChanged bool) {
if !leaders.isOneLeader(address) {
return
}
for i := 0; i < len(leaders.leaders); i++ {
if leaders.leaders[i] == address {
leaders.leaders[i] = ""
hasChanged = true
return
}
}
return
}
func (leaders *Leaders) isOneLeader(address pb.ServerAddress) bool {
for i := 0; i < len(leaders.leaders); i++ {
if leaders.leaders[i] == address {
return true
}
}
return false
}
func (leaders *Leaders) GetLeaders() (addresses []pb.ServerAddress) {
for i := 0; i < len(leaders.leaders); i++ {
if leaders.leaders[i] != "" {
addresses = append(addresses, leaders.leaders[i])
}
}
return
}