rename variables

This commit is contained in:
Chris Lu 2021-11-08 00:13:40 -08:00
parent 00fdab1284
commit 5c99b8acc3
2 changed files with 31 additions and 30 deletions

View file

@ -11,6 +11,7 @@ import (
const ( const (
MasterType = "master" MasterType = "master"
FilerType = "filer" FilerType = "filer"
BrokerType = "broker"
) )
type ClusterNode struct { type ClusterNode struct {
@ -25,34 +26,34 @@ type Leaders struct {
} }
type Cluster struct { type Cluster struct {
nodes map[pb.ServerAddress]*ClusterNode filers map[pb.ServerAddress]*ClusterNode
nodesLock sync.RWMutex filersLock sync.RWMutex
leaders *Leaders filerLeaders *Leaders
} }
func NewCluster() *Cluster { func NewCluster() *Cluster {
return &Cluster{ return &Cluster{
nodes: make(map[pb.ServerAddress]*ClusterNode), filers: make(map[pb.ServerAddress]*ClusterNode),
leaders: &Leaders{}, filerLeaders: &Leaders{},
} }
} }
func (cluster *Cluster) AddClusterNode(nodeType string, address pb.ServerAddress, version string) []*master_pb.KeepConnectedResponse { func (cluster *Cluster) AddClusterNode(nodeType string, address pb.ServerAddress, version string) []*master_pb.KeepConnectedResponse {
switch nodeType { switch nodeType {
case FilerType: case FilerType:
cluster.nodesLock.Lock() cluster.filersLock.Lock()
defer cluster.nodesLock.Unlock() defer cluster.filersLock.Unlock()
if existingNode, found := cluster.nodes[address]; found { if existingNode, found := cluster.filers[address]; found {
existingNode.counter++ existingNode.counter++
return nil return nil
} }
cluster.nodes[address] = &ClusterNode{ cluster.filers[address] = &ClusterNode{
Address: address, Address: address,
Version: version, Version: version,
counter: 1, counter: 1,
createdTs: time.Now(), createdTs: time.Now(),
} }
return cluster.ensureLeader(true, nodeType, address) return cluster.ensureFilerLeaders(true, nodeType, address)
case MasterType: case MasterType:
} }
return nil return nil
@ -61,15 +62,15 @@ func (cluster *Cluster) AddClusterNode(nodeType string, address pb.ServerAddress
func (cluster *Cluster) RemoveClusterNode(nodeType string, address pb.ServerAddress) []*master_pb.KeepConnectedResponse { func (cluster *Cluster) RemoveClusterNode(nodeType string, address pb.ServerAddress) []*master_pb.KeepConnectedResponse {
switch nodeType { switch nodeType {
case FilerType: case FilerType:
cluster.nodesLock.Lock() cluster.filersLock.Lock()
defer cluster.nodesLock.Unlock() defer cluster.filersLock.Unlock()
if existingNode, found := cluster.nodes[address]; !found { if existingNode, found := cluster.filers[address]; !found {
return nil return nil
} else { } else {
existingNode.counter-- existingNode.counter--
if existingNode.counter <= 0 { if existingNode.counter <= 0 {
delete(cluster.nodes, address) delete(cluster.filers, address)
return cluster.ensureLeader(false, nodeType, address) return cluster.ensureFilerLeaders(false, nodeType, address)
} }
} }
case MasterType: case MasterType:
@ -80,9 +81,9 @@ func (cluster *Cluster) RemoveClusterNode(nodeType string, address pb.ServerAddr
func (cluster *Cluster) ListClusterNode(nodeType string) (nodes []*ClusterNode) { func (cluster *Cluster) ListClusterNode(nodeType string) (nodes []*ClusterNode) {
switch nodeType { switch nodeType {
case FilerType: case FilerType:
cluster.nodesLock.RLock() cluster.filersLock.RLock()
defer cluster.nodesLock.RUnlock() defer cluster.filersLock.RUnlock()
for _, node := range cluster.nodes { for _, node := range cluster.filers {
nodes = append(nodes, node) nodes = append(nodes, node)
} }
case MasterType: case MasterType:
@ -91,12 +92,12 @@ func (cluster *Cluster) ListClusterNode(nodeType string) (nodes []*ClusterNode)
} }
func (cluster *Cluster) IsOneLeader(address pb.ServerAddress) bool { func (cluster *Cluster) IsOneLeader(address pb.ServerAddress) bool {
return cluster.leaders.isOneLeader(address) return cluster.filerLeaders.isOneLeader(address)
} }
func (cluster *Cluster) ensureLeader(isAdd bool, nodeType string, address pb.ServerAddress) (result []*master_pb.KeepConnectedResponse) { func (cluster *Cluster) ensureFilerLeaders(isAdd bool, nodeType string, address pb.ServerAddress) (result []*master_pb.KeepConnectedResponse) {
if isAdd { if isAdd {
if cluster.leaders.addLeaderIfVacant(address) { if cluster.filerLeaders.addLeaderIfVacant(address) {
// has added the address as one leader // has added the address as one leader
result = append(result, &master_pb.KeepConnectedResponse{ result = append(result, &master_pb.KeepConnectedResponse{
ClusterNodeUpdate: &master_pb.ClusterNodeUpdate{ ClusterNodeUpdate: &master_pb.ClusterNodeUpdate{
@ -117,7 +118,7 @@ func (cluster *Cluster) ensureLeader(isAdd bool, nodeType string, address pb.Ser
}) })
} }
} else { } else {
if cluster.leaders.removeLeaderIfExists(address) { if cluster.filerLeaders.removeLeaderIfExists(address) {
result = append(result, &master_pb.KeepConnectedResponse{ result = append(result, &master_pb.KeepConnectedResponse{
ClusterNodeUpdate: &master_pb.ClusterNodeUpdate{ ClusterNodeUpdate: &master_pb.ClusterNodeUpdate{
@ -132,8 +133,8 @@ func (cluster *Cluster) ensureLeader(isAdd bool, nodeType string, address pb.Ser
var shortestDuration int64 = math.MaxInt64 var shortestDuration int64 = math.MaxInt64
now := time.Now() now := time.Now()
var candidateAddress pb.ServerAddress var candidateAddress pb.ServerAddress
for _, node := range cluster.nodes { for _, node := range cluster.filers {
if cluster.leaders.isOneLeader(node.Address) { if cluster.filerLeaders.isOneLeader(node.Address) {
continue continue
} }
duration := now.Sub(node.createdTs).Nanoseconds() duration := now.Sub(node.createdTs).Nanoseconds()
@ -143,7 +144,7 @@ func (cluster *Cluster) ensureLeader(isAdd bool, nodeType string, address pb.Ser
} }
} }
if candidateAddress != "" { if candidateAddress != "" {
cluster.leaders.addLeaderIfVacant(candidateAddress) cluster.filerLeaders.addLeaderIfVacant(candidateAddress)
// added a new leader // added a new leader
result = append(result, &master_pb.KeepConnectedResponse{ result = append(result, &master_pb.KeepConnectedResponse{
ClusterNodeUpdate: &master_pb.ClusterNodeUpdate{ ClusterNodeUpdate: &master_pb.ClusterNodeUpdate{

View file

@ -14,7 +14,7 @@ func TestClusterAddRemoveNodes(t *testing.T) {
assert.Equal(t, []pb.ServerAddress{ assert.Equal(t, []pb.ServerAddress{
pb.ServerAddress("111:1"), pb.ServerAddress("111:1"),
pb.ServerAddress("111:2"), pb.ServerAddress("111:2"),
}, c.leaders.GetLeaders()) }, c.filerLeaders.GetLeaders())
c.AddClusterNode("filer", pb.ServerAddress("111:3"), "23.45") c.AddClusterNode("filer", pb.ServerAddress("111:3"), "23.45")
c.AddClusterNode("filer", pb.ServerAddress("111:4"), "23.45") c.AddClusterNode("filer", pb.ServerAddress("111:4"), "23.45")
@ -22,7 +22,7 @@ func TestClusterAddRemoveNodes(t *testing.T) {
pb.ServerAddress("111:1"), pb.ServerAddress("111:1"),
pb.ServerAddress("111:2"), pb.ServerAddress("111:2"),
pb.ServerAddress("111:3"), pb.ServerAddress("111:3"),
}, c.leaders.GetLeaders()) }, c.filerLeaders.GetLeaders())
c.AddClusterNode("filer", pb.ServerAddress("111:5"), "23.45") c.AddClusterNode("filer", pb.ServerAddress("111:5"), "23.45")
c.AddClusterNode("filer", pb.ServerAddress("111:6"), "23.45") c.AddClusterNode("filer", pb.ServerAddress("111:6"), "23.45")
@ -31,7 +31,7 @@ func TestClusterAddRemoveNodes(t *testing.T) {
pb.ServerAddress("111:1"), pb.ServerAddress("111:1"),
pb.ServerAddress("111:2"), pb.ServerAddress("111:2"),
pb.ServerAddress("111:3"), pb.ServerAddress("111:3"),
}, c.leaders.GetLeaders()) }, c.filerLeaders.GetLeaders())
// remove oldest // remove oldest
c.RemoveClusterNode("filer", pb.ServerAddress("111:1")) c.RemoveClusterNode("filer", pb.ServerAddress("111:1"))
@ -39,7 +39,7 @@ func TestClusterAddRemoveNodes(t *testing.T) {
pb.ServerAddress("111:6"), pb.ServerAddress("111:6"),
pb.ServerAddress("111:2"), pb.ServerAddress("111:2"),
pb.ServerAddress("111:3"), pb.ServerAddress("111:3"),
}, c.leaders.GetLeaders()) }, c.filerLeaders.GetLeaders())
// remove oldest // remove oldest
c.RemoveClusterNode("filer", pb.ServerAddress("111:1")) c.RemoveClusterNode("filer", pb.ServerAddress("111:1"))