节点根据健康检查自动上下线

This commit is contained in:
刘祥超
2020-11-15 21:17:42 +08:00
parent 2eb81b52a0
commit 58cae9a5e5
13 changed files with 450 additions and 183 deletions

View File

@@ -24,8 +24,9 @@ const (
type MessageType = string
const (
MessageTypeHealthCheckFail MessageType = "HealthCheckFail"
MessageTypeNodeInactive MessageType = "NodeInactive"
MessageTypeHealthCheckFailed MessageType = "HealthCheckFailed"
MessageTypeNodeInactive MessageType = "NodeInactive"
MessageTypeClusterDNSSyncFailed MessageType = "ClusterDNSSyncFailed"
)
type MessageDAO dbs.DAO

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
_ "github.com/go-sql-driver/mysql"
@@ -116,8 +117,20 @@ func (this *NodeClusterDAO) CreateCluster(name string, grantId int64, installDir
op.Name = name
op.GrantId = grantId
op.InstallDir = installDir
// DNS设置
op.DnsDomainId = dnsDomainId
op.DnsName = dnsName
dnsConfig := &dnsconfigs.ClusterDNSConfig{
NodesAutoSync: true,
ServersAutoSync: true,
}
dnsJSON, err := json.Marshal(dnsConfig)
if err != nil {
return 0, err
}
op.Dns = dnsJSON
op.UseAllAPINodes = 1
op.ApiNodes = "[]"
op.UniqueId = uniqueId
@@ -352,7 +365,7 @@ func (this *NodeClusterDAO) FindClusterGrantId(clusterId int64) (int64, error) {
func (this *NodeClusterDAO) FindClusterDNSInfo(clusterId int64) (*NodeCluster, error) {
one, err := this.Query().
Pk(clusterId).
Result("id", "name", "dnsName", "dnsDomainId").
Result("id", "name", "dnsName", "dnsDomainId", "dns").
Find()
if err != nil {
return nil, err
@@ -374,7 +387,7 @@ func (this *NodeClusterDAO) ExistClusterDNSName(dnsName string, excludeClusterId
}
// 修改集群DNS相关信息
func (this *NodeClusterDAO) UpdateClusterDNS(clusterId int64, dnsName string, dnsDomainId int64) error {
func (this *NodeClusterDAO) UpdateClusterDNS(clusterId int64, dnsName string, dnsDomainId int64, nodesAutoSync bool, serversAutoSync bool) error {
if clusterId <= 0 {
return errors.New("invalid clusterId")
}
@@ -382,7 +395,18 @@ func (this *NodeClusterDAO) UpdateClusterDNS(clusterId int64, dnsName string, dn
op.Id = clusterId
op.DnsName = dnsName
op.DnsDomainId = dnsDomainId
_, err := this.Save(op)
dnsConfig := &dnsconfigs.ClusterDNSConfig{
NodesAutoSync: nodesAutoSync,
ServersAutoSync: serversAutoSync,
}
dnsJSON, err := json.Marshal(dnsConfig)
if err != nil {
return err
}
op.Dns = dnsJSON
_, err = this.Save(op)
return err
}

View File

@@ -19,6 +19,7 @@ type NodeCluster struct {
HealthCheck string `field:"healthCheck"` // 健康检查
DnsName string `field:"dnsName"` // DNS名称
DnsDomainId uint32 `field:"dnsDomainId"` // 域名ID
Dns string `field:"dns"` // DNS配置
}
type NodeClusterOperator struct {
@@ -39,6 +40,7 @@ type NodeClusterOperator struct {
HealthCheck interface{} // 健康检查
DnsName interface{} // DNS名称
DnsDomainId interface{} // 域名ID
Dns interface{} // DNS配置
}
func NewNodeClusterOperator() *NodeClusterOperator {

View File

@@ -1 +1,23 @@
package models
import (
"encoding/json"
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
)
// 解析DNS配置
func (this *NodeCluster) DecodeDNSConfig() (*dnsconfigs.ClusterDNSConfig, error) {
if len(this.Dns) == 0 || this.Dns == "null" {
// 一定要返回一个默认的值防止产生nil
return &dnsconfigs.ClusterDNSConfig{
NodesAutoSync: false,
ServersAutoSync: false,
}, nil
}
dnsConfig := &dnsconfigs.ClusterDNSConfig{}
err := json.Unmarshal([]byte(this.Dns), &dnsConfig)
if err != nil {
return nil, err
}
return dnsConfig, nil
}

View File

@@ -604,6 +604,7 @@ func (this *NodeDAO) FindAllEnabledNodesDNSWithClusterId(clusterId int64) (resul
State(NodeStateEnabled).
Attr("clusterId", clusterId).
Attr("isOn", true).
Attr("isUp", true).
Result("id", "name", "dnsRoutes", "isOn").
DescPk().
Slice(&result).
@@ -644,6 +645,61 @@ func (this *NodeDAO) UpdateNodeDNS(nodeId int64, routes map[int64]string) error
return err
}
// 计算节点上线|下线状态
func (this *NodeDAO) UpdateNodeUp(nodeId int64, isUp bool, maxUp int, maxDown int) (changed bool, err error) {
if nodeId <= 0 {
return false, errors.New("invalid nodeId")
}
one, err := this.Query().
Pk(nodeId).
Result("isUp", "countUp", "countDown").
Find()
if err != nil {
return false, err
}
if one == nil {
return false, nil
}
oldIsUp := one.(*Node).IsUp == 1
// 如果新老状态一致,则不做任何事情
if oldIsUp == isUp {
return isUp, nil
}
countUp := int(one.(*Node).CountUp)
countDown := int(one.(*Node).CountDown)
op := NewNodeOperator()
op.Id = nodeId
if isUp {
countUp++
countDown = 0
if countUp >= maxUp {
changed = true
op.IsUp = true
}
} else {
countDown++
countUp = 0
if countDown >= maxDown {
changed = true
op.IsUp = false
}
}
op.CountUp = countUp
op.CountDown = countDown
_, err = this.Save(op)
if err != nil {
return false, err
}
return
}
// 生成唯一ID
func (this *NodeDAO) genUniqueId() (string, error) {
for {

View File

@@ -2,6 +2,7 @@ package models
import (
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/dbs"
"testing"
)
@@ -20,3 +21,12 @@ func TestNodeDAO_FindChangedClusterIds(t *testing.T) {
}
t.Log(clusterIds)
}
func TestNodeDAO_UpdateNodeUp(t *testing.T) {
dbs.NotifyReady()
isChanged, err := SharedNodeDAO.UpdateNodeUp(57, false, 3, 3)
if err != nil {
t.Fatal(err)
}
t.Log("changed:", isChanged)
}

View File

@@ -6,6 +6,9 @@ type Node struct {
AdminId uint32 `field:"adminId"` // 管理员ID
UserId uint32 `field:"userId"` // 用户ID
IsOn uint8 `field:"isOn"` // 是否启用
IsUp uint8 `field:"isUp"` // 是否在线
CountUp uint32 `field:"countUp"` // 连续在线次数
CountDown uint32 `field:"countDown"` // 连续下线次数
UniqueId string `field:"uniqueId"` // 节点ID
Secret string `field:"secret"` // 密钥
Name string `field:"name"` // 节点名
@@ -31,6 +34,9 @@ type NodeOperator struct {
AdminId interface{} // 管理员ID
UserId interface{} // 用户ID
IsOn interface{} // 是否启用
IsUp interface{} // 是否在线
CountUp interface{} // 连续在线次数
CountDown interface{} // 连续下线次数
UniqueId interface{} // 节点ID
Secret interface{} // 密钥
Name interface{} // 节点名