2020-08-30 16:12:00 +08:00
package models
import (
2021-09-08 19:34:31 +08:00
"encoding/json"
2020-08-30 16:12:00 +08:00
"errors"
2024-07-27 14:15:25 +08:00
2021-08-18 09:24:18 +08:00
"github.com/TeaOSLab/EdgeAPI/internal/db/models/dns"
2022-03-27 12:22:47 +08:00
dbutils "github.com/TeaOSLab/EdgeAPI/internal/db/utils"
2022-05-18 21:02:53 +08:00
"github.com/TeaOSLab/EdgeAPI/internal/utils"
2021-08-31 17:24:52 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
2021-05-26 14:40:05 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
2020-08-30 16:12:00 +08:00
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/types"
)
const (
NodeIPAddressStateEnabled = 1 // 已启用
NodeIPAddressStateDisabled = 0 // 已禁用
)
type NodeIPAddressDAO dbs . DAO
func NewNodeIPAddressDAO ( ) * NodeIPAddressDAO {
return dbs . NewDAO ( & NodeIPAddressDAO {
DAOObject : dbs . DAOObject {
DB : Tea . Env ,
Table : "edgeNodeIPAddresses" ,
Model : new ( NodeIPAddress ) ,
PkName : "id" ,
} ,
} ) . ( * NodeIPAddressDAO )
}
2020-10-13 20:05:13 +08:00
var SharedNodeIPAddressDAO * NodeIPAddressDAO
func init ( ) {
dbs . OnReady ( func ( ) {
SharedNodeIPAddressDAO = NewNodeIPAddressDAO ( )
} )
}
2020-08-30 16:12:00 +08:00
2021-05-26 14:40:05 +08:00
// EnableAddress 启用条目
2021-08-18 09:24:18 +08:00
func ( this * NodeIPAddressDAO ) EnableAddress ( tx * dbs . Tx , addressId int64 ) ( err error ) {
2021-01-01 23:31:30 +08:00
_ , err = this . Query ( tx ) .
2021-08-18 09:24:18 +08:00
Pk ( addressId ) .
2020-08-30 16:12:00 +08:00
Set ( "state" , NodeIPAddressStateEnabled ) .
Update ( )
2021-08-18 09:24:18 +08:00
if err != nil {
return err
}
return this . NotifyUpdate ( tx , addressId )
2020-08-30 16:12:00 +08:00
}
2021-05-26 14:40:05 +08:00
// DisableAddress 禁用IP地址
2021-08-18 09:24:18 +08:00
func ( this * NodeIPAddressDAO ) DisableAddress ( tx * dbs . Tx , addressId int64 ) ( err error ) {
2021-01-01 23:31:30 +08:00
_ , err = this . Query ( tx ) .
2021-08-18 09:24:18 +08:00
Pk ( addressId ) .
2020-08-30 16:12:00 +08:00
Set ( "state" , NodeIPAddressStateDisabled ) .
Update ( )
2021-08-18 09:24:18 +08:00
if err != nil {
return err
}
return this . NotifyUpdate ( tx , addressId )
2020-08-30 16:12:00 +08:00
}
2021-05-26 14:40:05 +08:00
// DisableAllAddressesWithNodeId 禁用节点的所有的IP地址
func ( this * NodeIPAddressDAO ) DisableAllAddressesWithNodeId ( tx * dbs . Tx , nodeId int64 , role nodeconfigs . NodeRole ) error {
2020-08-30 16:12:00 +08:00
if nodeId <= 0 {
return errors . New ( "invalid nodeId" )
}
2021-05-26 14:40:05 +08:00
if len ( role ) == 0 {
role = nodeconfigs . NodeRoleNode
}
2021-01-01 23:31:30 +08:00
_ , err := this . Query ( tx ) .
2020-08-30 16:12:00 +08:00
Attr ( "nodeId" , nodeId ) .
2021-08-03 13:35:29 +08:00
Attr ( "role" , role ) .
2020-08-30 16:12:00 +08:00
Set ( "state" , NodeIPAddressStateDisabled ) .
Update ( )
2021-08-18 09:24:18 +08:00
if err != nil {
return err
}
return SharedNodeDAO . NotifyDNSUpdate ( tx , nodeId )
2020-08-30 16:12:00 +08:00
}
2021-05-26 14:40:05 +08:00
// FindEnabledAddress 查找启用中的IP地址
2021-01-01 23:31:30 +08:00
func ( this * NodeIPAddressDAO ) FindEnabledAddress ( tx * dbs . Tx , id int64 ) ( * NodeIPAddress , error ) {
result , err := this . Query ( tx ) .
2020-08-30 16:12:00 +08:00
Pk ( id ) .
Attr ( "state" , NodeIPAddressStateEnabled ) .
Find ( )
if result == nil {
return nil , err
}
return result . ( * NodeIPAddress ) , err
}
2021-05-26 14:40:05 +08:00
// FindAddressName 根据主键查找名称
2021-01-01 23:31:30 +08:00
func ( this * NodeIPAddressDAO ) FindAddressName ( tx * dbs . Tx , id int64 ) ( string , error ) {
return this . Query ( tx ) .
2020-08-30 16:12:00 +08:00
Pk ( id ) .
Result ( "name" ) .
FindStringCol ( "" )
}
2021-11-18 14:30:53 +08:00
// FindAddressIsHealthy 判断IP地址是否健康
func ( this * NodeIPAddressDAO ) FindAddressIsHealthy ( tx * dbs . Tx , addressId int64 ) ( isHealthy bool , err error ) {
if addressId <= 0 {
return false , nil
}
one , err := this . Query ( tx ) .
Pk ( addressId ) .
Result ( "isHealthy" ) .
Find ( )
if err != nil || one == nil {
return false , err
}
var addr = one . ( * NodeIPAddress )
2022-03-22 22:11:32 +08:00
return addr . IsHealthy , nil
2021-11-18 14:30:53 +08:00
}
2021-05-26 14:40:05 +08:00
// CreateAddress 创建IP地址
2023-03-01 11:38:53 +08:00
func ( this * NodeIPAddressDAO ) CreateAddress ( tx * dbs . Tx , adminId int64 , nodeId int64 , role nodeconfigs . NodeRole , name string , ip string , canAccess bool , isUp bool , groupId int64 , clusterIds [ ] int64 ) ( addressId int64 , err error ) {
2021-05-26 14:40:05 +08:00
if len ( role ) == 0 {
role = nodeconfigs . NodeRoleNode
}
2022-04-22 21:53:38 +08:00
var op = NewNodeIPAddressOperator ( )
2020-08-30 16:12:00 +08:00
op . NodeId = nodeId
2021-05-26 14:40:05 +08:00
op . Role = role
2020-08-30 16:12:00 +08:00
op . Name = name
2020-10-17 21:15:31 +08:00
op . Ip = ip
op . CanAccess = canAccess
2021-09-15 11:45:00 +08:00
op . IsUp = isUp
2021-12-07 18:22:19 +08:00
op . GroupId = groupId
2021-08-18 16:19:16 +08:00
2023-03-01 11:38:53 +08:00
// 集群
if len ( clusterIds ) == 0 {
op . ClusterIds = "[]"
} else {
clusterIdsJSON , err := json . Marshal ( clusterIds )
if err != nil {
return 0 , err
}
op . ClusterIds = clusterIdsJSON
}
2020-08-30 16:12:00 +08:00
op . State = NodeIPAddressStateEnabled
2021-08-31 17:24:52 +08:00
addressId , err = this . SaveInt64 ( tx , op )
2020-08-30 16:12:00 +08:00
if err != nil {
return 0 , err
}
2021-01-27 23:00:02 +08:00
err = SharedNodeDAO . NotifyDNSUpdate ( tx , nodeId )
if err != nil {
return 0 , err
}
2021-08-31 17:24:52 +08:00
// 创建日志
err = SharedNodeIPAddressLogDAO . CreateLog ( tx , adminId , addressId , "创建IP" )
if err != nil {
return 0 , err
}
return addressId , nil
2020-08-30 16:12:00 +08:00
}
2021-05-26 14:40:05 +08:00
// UpdateAddress 修改IP地址
2023-03-01 11:38:53 +08:00
func ( this * NodeIPAddressDAO ) UpdateAddress ( tx * dbs . Tx , adminId int64 , addressId int64 , name string , ip string , canAccess bool , isOn bool , isUp bool , clusterIds [ ] int64 ) ( err error ) {
2020-08-30 16:12:00 +08:00
if addressId <= 0 {
return errors . New ( "invalid addressId" )
}
2022-07-24 09:56:27 +08:00
var op = NewNodeIPAddressOperator ( )
2020-08-30 16:12:00 +08:00
op . Id = addressId
op . Name = name
2020-10-17 21:15:31 +08:00
op . Ip = ip
op . CanAccess = canAccess
2021-08-18 09:24:18 +08:00
op . IsOn = isOn
2021-09-15 11:45:00 +08:00
op . IsUp = isUp
2021-08-18 16:19:16 +08:00
2023-03-01 11:38:53 +08:00
// 集群
if len ( clusterIds ) == 0 {
op . ClusterIds = "[]"
} else {
clusterIdsJSON , err := json . Marshal ( clusterIds )
if err != nil {
return err
}
op . ClusterIds = clusterIdsJSON
}
2020-10-17 21:15:31 +08:00
op . State = NodeIPAddressStateEnabled // 恢复状态
2021-01-01 23:31:30 +08:00
err = this . Save ( tx , op )
2021-08-18 09:24:18 +08:00
if err != nil {
return err
}
2021-08-31 17:24:52 +08:00
// 创建日志
err = SharedNodeIPAddressLogDAO . CreateLog ( tx , adminId , addressId , "修改IP" )
if err != nil {
return err
}
2021-08-18 09:24:18 +08:00
return this . NotifyUpdate ( tx , addressId )
2020-08-30 16:12:00 +08:00
}
2021-05-26 14:40:05 +08:00
// UpdateAddressIP 修改IP地址中的IP
2021-01-01 23:31:30 +08:00
func ( this * NodeIPAddressDAO ) UpdateAddressIP ( tx * dbs . Tx , addressId int64 , ip string ) error {
2020-11-14 09:41:58 +08:00
if addressId <= 0 {
return errors . New ( "invalid addressId" )
}
2022-07-24 09:56:27 +08:00
var op = NewNodeIPAddressOperator ( )
2020-11-14 09:41:58 +08:00
op . Id = addressId
op . Ip = ip
2021-01-01 23:31:30 +08:00
err := this . Save ( tx , op )
2021-08-18 09:24:18 +08:00
if err != nil {
return err
}
return this . NotifyUpdate ( tx , addressId )
2020-11-14 09:41:58 +08:00
}
2021-05-26 14:40:05 +08:00
// UpdateAddressNodeId 修改IP地址所属节点
2021-01-01 23:31:30 +08:00
func ( this * NodeIPAddressDAO ) UpdateAddressNodeId ( tx * dbs . Tx , addressId int64 , nodeId int64 ) error {
_ , err := this . Query ( tx ) .
2020-08-30 16:12:00 +08:00
Pk ( addressId ) .
Set ( "nodeId" , nodeId ) .
Set ( "state" , NodeIPAddressStateEnabled ) . // 恢复状态
Update ( )
2021-01-27 23:00:02 +08:00
if err != nil {
return err
}
err = SharedNodeDAO . NotifyDNSUpdate ( tx , nodeId )
if err != nil {
return err
}
return nil
2020-08-30 16:12:00 +08:00
}
2021-05-26 14:40:05 +08:00
// FindAllEnabledAddressesWithNode 查找节点的所有的IP地址
func ( this * NodeIPAddressDAO ) FindAllEnabledAddressesWithNode ( tx * dbs . Tx , nodeId int64 , role nodeconfigs . NodeRole ) ( result [ ] * NodeIPAddress , err error ) {
if len ( role ) == 0 {
role = nodeconfigs . NodeRoleNode
}
2021-01-01 23:31:30 +08:00
_ , err = this . Query ( tx ) .
2020-08-30 16:12:00 +08:00
Attr ( "nodeId" , nodeId ) .
2021-05-26 14:40:05 +08:00
Attr ( "role" , role ) .
2020-08-30 16:12:00 +08:00
State ( NodeIPAddressStateEnabled ) .
Desc ( "order" ) .
AscPk ( ) .
Slice ( & result ) .
FindAll ( )
return
}
2020-11-13 18:22:22 +08:00
2023-11-18 12:09:47 +08:00
// FindAllEnabledAddressStringsWithNode 查找节点的所有的IP地址地府传
func ( this * NodeIPAddressDAO ) FindAllEnabledAddressStringsWithNode ( tx * dbs . Tx , nodeId int64 , role nodeconfigs . NodeRole ) ( result [ ] string , err error ) {
if len ( role ) == 0 {
role = nodeconfigs . NodeRoleNode
}
ones , err := this . Query ( tx ) .
Attr ( "nodeId" , nodeId ) .
Attr ( "role" , role ) .
State ( NodeIPAddressStateEnabled ) .
Result ( "ip" , "backupIP" ) .
FindAll ( )
if err != nil {
return nil , err
}
for _ , one := range ones {
var addr = one . ( * NodeIPAddress )
result = append ( result , addr . Ip )
if len ( addr . BackupIP ) > 0 {
result = append ( result , addr . BackupIP )
}
}
return
}
2021-05-26 14:40:05 +08:00
// FindFirstNodeAccessIPAddress 查找节点的第一个可访问的IP地址
2022-04-22 21:53:38 +08:00
func ( this * NodeIPAddressDAO ) FindFirstNodeAccessIPAddress ( tx * dbs . Tx , nodeId int64 , mustUp bool , role nodeconfigs . NodeRole ) ( ip string , addrId int64 , err error ) {
2021-05-26 14:40:05 +08:00
if len ( role ) == 0 {
role = nodeconfigs . NodeRoleNode
}
2022-04-22 21:53:38 +08:00
var query = this . Query ( tx ) .
2020-11-13 18:22:22 +08:00
Attr ( "nodeId" , nodeId ) .
2021-05-26 14:40:05 +08:00
Attr ( "role" , role ) .
2020-11-13 18:22:22 +08:00
State ( NodeIPAddressStateEnabled ) .
Attr ( "canAccess" , true ) .
2022-04-22 21:53:38 +08:00
Attr ( "isOn" , true )
if mustUp {
query . Attr ( "isUp" , true )
}
one , err := query .
2020-11-13 18:22:22 +08:00
Desc ( "order" ) .
AscPk ( ) .
2021-11-18 14:30:53 +08:00
Result ( "id" , "ip" ) .
Find ( )
if err != nil {
return "" , 0 , err
}
if one == nil {
return
}
var addr = one . ( * NodeIPAddress )
return addr . Ip , int64 ( addr . Id ) , nil
2020-11-13 18:22:22 +08:00
}
2020-11-14 09:41:58 +08:00
2021-05-26 14:40:05 +08:00
// FindFirstNodeAccessIPAddressId 查找节点的第一个可访问的IP地址ID
2022-04-23 12:32:30 +08:00
func ( this * NodeIPAddressDAO ) FindFirstNodeAccessIPAddressId ( tx * dbs . Tx , nodeId int64 , mustUp bool , role nodeconfigs . NodeRole ) ( int64 , error ) {
2021-05-26 14:40:05 +08:00
if len ( role ) == 0 {
role = nodeconfigs . NodeRoleNode
}
2022-04-23 12:32:30 +08:00
var query = this . Query ( tx ) .
2020-11-14 09:41:58 +08:00
Attr ( "nodeId" , nodeId ) .
2021-05-26 14:40:05 +08:00
Attr ( "role" , role ) .
2020-11-14 09:41:58 +08:00
State ( NodeIPAddressStateEnabled ) .
Attr ( "canAccess" , true ) .
2022-04-23 12:32:30 +08:00
Attr ( "isOn" , true )
if mustUp {
query . Attr ( "isUp" , true )
}
return query .
2020-11-14 09:41:58 +08:00
Desc ( "order" ) .
AscPk ( ) .
Result ( "id" ) .
FindInt64Col ( 0 )
}
2021-01-27 23:00:02 +08:00
2023-04-26 10:50:29 +08:00
// FindNodeAccessAndUpIPAddresses 查找节点所有的可访问且在线的IP地址
2021-08-18 09:24:18 +08:00
func ( this * NodeIPAddressDAO ) FindNodeAccessAndUpIPAddresses ( tx * dbs . Tx , nodeId int64 , role nodeconfigs . NodeRole ) ( result [ ] * NodeIPAddress , err error ) {
2021-05-26 14:40:05 +08:00
if len ( role ) == 0 {
role = nodeconfigs . NodeRoleNode
}
2021-01-27 23:00:02 +08:00
_ , err = this . Query ( tx ) .
2021-06-27 21:59:37 +08:00
Attr ( "role" , role ) .
2021-01-27 23:00:02 +08:00
Attr ( "nodeId" , nodeId ) .
State ( NodeIPAddressStateEnabled ) .
Attr ( "canAccess" , true ) .
2021-08-18 09:24:18 +08:00
Attr ( "isOn" , true ) .
Attr ( "isUp" , true ) .
2021-01-27 23:00:02 +08:00
Desc ( "order" ) .
AscPk ( ) .
Slice ( & result ) .
FindAll ( )
return
}
2021-08-18 09:24:18 +08:00
2023-04-26 10:50:29 +08:00
// FindNodeAccessIPAddresses 查找节点所有的可访问的IP地址, 包括在线和离线
func ( this * NodeIPAddressDAO ) FindNodeAccessIPAddresses ( tx * dbs . Tx , nodeId int64 , role nodeconfigs . NodeRole ) ( result [ ] * NodeIPAddress , err error ) {
if len ( role ) == 0 {
role = nodeconfigs . NodeRoleNode
}
_ , err = this . Query ( tx ) .
Attr ( "role" , role ) .
Attr ( "nodeId" , nodeId ) .
State ( NodeIPAddressStateEnabled ) .
Attr ( "canAccess" , true ) .
Attr ( "isOn" , true ) .
Desc ( "order" ) .
AscPk ( ) .
Slice ( & result ) .
FindAll ( )
return
}
2021-08-31 17:24:52 +08:00
// CountAllEnabledIPAddresses 计算IP地址数量
// TODO 目前支持边缘节点, 将来支持NS节点
func ( this * NodeIPAddressDAO ) CountAllEnabledIPAddresses ( tx * dbs . Tx , role string , nodeClusterId int64 , upState configutils . BoolState , keyword string ) ( int64 , error ) {
var query = this . Query ( tx ) .
State ( NodeIPAddressStateEnabled ) .
Attr ( "role" , role )
// 集群
if nodeClusterId > 0 {
query . Where ( "nodeId IN (SELECT id FROM " + SharedNodeDAO . Table + " WHERE (clusterId=:clusterId OR JSON_CONTAINS(secondaryClusterIds, :clusterIdString)) AND state=1)" ) .
Param ( "clusterId" , nodeClusterId ) .
Param ( "clusterIdString" , types . String ( nodeClusterId ) )
} else {
query . Where ( "nodeId IN (SELECT id FROM " + SharedNodeDAO . Table + " WHERE state=1 AND clusterId IN (SELECT id FROM " + SharedNodeClusterDAO . Table + " WHERE state=1))" )
}
// 在线状态
switch upState {
case configutils . BoolStateYes :
query . Attr ( "isUp" , 1 )
case configutils . BoolStateNo :
query . Attr ( "isUp" , 0 )
}
// 关键词
if len ( keyword ) > 0 {
query . Where ( "(ip LIKE :keyword OR name LIKE :keyword OR description LIKE :keyword OR nodeId IN (SELECT id FROM " + SharedNodeDAO . Table + " WHERE state=1 AND name LIKE :keyword))" ) .
2022-03-27 12:22:47 +08:00
Param ( "keyword" , dbutils . QuoteLike ( keyword ) )
2021-08-31 17:24:52 +08:00
}
return query . Count ( )
}
// ListEnabledIPAddresses 列出单页的IP地址
func ( this * NodeIPAddressDAO ) ListEnabledIPAddresses ( tx * dbs . Tx , role string , nodeClusterId int64 , upState configutils . BoolState , keyword string , offset int64 , size int64 ) ( result [ ] * NodeIPAddress , err error ) {
var query = this . Query ( tx ) .
State ( NodeIPAddressStateEnabled ) .
Attr ( "role" , role )
// 集群
if nodeClusterId > 0 {
query . Where ( "nodeId IN (SELECT id FROM " + SharedNodeDAO . Table + " WHERE (clusterId=:clusterId OR JSON_CONTAINS(secondaryClusterIds, :clusterIdString)) AND state=1)" ) .
Param ( "clusterId" , nodeClusterId ) .
Param ( "clusterIdString" , types . String ( nodeClusterId ) )
} else {
query . Where ( "nodeId IN (SELECT id FROM " + SharedNodeDAO . Table + " WHERE state=1 AND clusterId IN (SELECT id FROM " + SharedNodeClusterDAO . Table + " WHERE state=1))" )
}
// 在线状态
switch upState {
case configutils . BoolStateYes :
query . Attr ( "isUp" , 1 )
case configutils . BoolStateNo :
query . Attr ( "isUp" , 0 )
}
// 关键词
if len ( keyword ) > 0 {
query . Where ( "(ip LIKE :keyword OR name LIKE :keyword OR description LIKE :keyword OR nodeId IN (SELECT id FROM " + SharedNodeDAO . Table + " WHERE state=1 AND name LIKE :keyword))" ) .
2022-03-27 12:22:47 +08:00
Param ( "keyword" , dbutils . QuoteLike ( keyword ) )
2021-08-31 17:24:52 +08:00
}
_ , err = query . Offset ( offset ) .
Limit ( size ) .
Asc ( "isUp" ) .
Desc ( "nodeId" ) .
Slice ( & result ) .
FindAll ( )
return
}
2021-09-08 19:34:31 +08:00
// FindAllAccessibleIPAddressesWithClusterId 列出所有的正在启用的IP地址
2022-05-18 21:02:53 +08:00
func ( this * NodeIPAddressDAO ) FindAllAccessibleIPAddressesWithClusterId ( tx * dbs . Tx , role string , clusterId int64 , cacheMap * utils . CacheMap ) ( result [ ] * NodeIPAddress , err error ) {
var cacheKey = this . Table + ":FindAllAccessibleIPAddressesWithClusterId:" + role + ":" + types . String ( clusterId )
if cacheMap != nil {
cache , ok := cacheMap . Get ( cacheKey )
if ok {
return cache . ( [ ] * NodeIPAddress ) , nil
}
}
2021-09-06 08:12:48 +08:00
_ , err = this . Query ( tx ) .
State ( NodeIPAddressStateEnabled ) .
Attr ( "role" , role ) .
Attr ( "isOn" , true ) .
2021-09-08 19:34:31 +08:00
Attr ( "canAccess" , true ) .
2021-09-06 08:12:48 +08:00
Where ( "nodeId IN (SELECT id FROM " + SharedNodeDAO . Table + " WHERE state=1 AND clusterId=:clusterId)" ) .
Param ( "clusterId" , clusterId ) .
Slice ( & result ) .
FindAll ( )
2022-05-18 21:02:53 +08:00
if err != nil {
return
}
if cacheMap != nil {
cacheMap . Put ( cacheKey , result )
}
2021-09-06 08:12:48 +08:00
return
}
2021-09-08 19:34:31 +08:00
// CountAllAccessibleIPAddressesWithClusterId 计算集群中的可用IP地址数量
func ( this * NodeIPAddressDAO ) CountAllAccessibleIPAddressesWithClusterId ( tx * dbs . Tx , role string , clusterId int64 ) ( count int64 , err error ) {
return this . Query ( tx ) .
State ( NodeIPAddressStateEnabled ) .
Attr ( "role" , role ) .
Attr ( "isOn" , true ) .
Attr ( "canAccess" , true ) .
Where ( "nodeId IN (SELECT id FROM " + SharedNodeDAO . Table + " WHERE state=1 AND clusterId=:clusterId)" ) .
Param ( "clusterId" , clusterId ) .
Count ( )
}
// ListAccessibleIPAddressesWithClusterId 列出单页集群中的可用IP地址
func ( this * NodeIPAddressDAO ) ListAccessibleIPAddressesWithClusterId ( tx * dbs . Tx , role string , clusterId int64 , offset int64 , size int64 ) ( result [ ] * NodeIPAddress , err error ) {
_ , err = this . Query ( tx ) .
State ( NodeIPAddressStateEnabled ) .
Attr ( "role" , role ) .
Attr ( "isOn" , true ) .
Attr ( "canAccess" , true ) .
Where ( "nodeId IN (SELECT id FROM " + SharedNodeDAO . Table + " WHERE state=1 AND clusterId=:clusterId)" ) .
Param ( "clusterId" , clusterId ) .
Offset ( offset ) .
Limit ( size ) .
Slice ( & result ) .
FindAll ( )
return
}
// UpdateAddressConnectivity 设置连通性数据
func ( this * NodeIPAddressDAO ) UpdateAddressConnectivity ( tx * dbs . Tx , addrId int64 , connectivity * nodeconfigs . Connectivity ) error {
connectivityJSON , err := json . Marshal ( connectivity )
if err != nil {
return err
}
return this . Query ( tx ) .
Pk ( addrId ) .
Set ( "connectivity" , connectivityJSON ) .
UpdateQuickly ( )
}
2021-09-12 20:21:42 +08:00
// UpdateAddressIsUp 设置IP地址在线状态
func ( this * NodeIPAddressDAO ) UpdateAddressIsUp ( tx * dbs . Tx , addressId int64 , isUp bool ) error {
var err = this . Query ( tx ) .
Pk ( addressId ) .
Set ( "isUp" , isUp ) .
2021-11-18 14:30:53 +08:00
Set ( "countUp" , 0 ) .
Set ( "countDown" , 0 ) .
Set ( "backupIP" , "" ) .
Set ( "backupThresholdId" , 0 ) .
2021-09-12 20:21:42 +08:00
UpdateQuickly ( )
if err != nil {
return err
}
return this . NotifyUpdate ( tx , addressId )
}
2021-09-13 10:51:05 +08:00
// UpdateAddressBackupIP 设置备用IP
func ( this * NodeIPAddressDAO ) UpdateAddressBackupIP ( tx * dbs . Tx , addressId int64 , thresholdId int64 , ip string ) error {
if addressId <= 0 {
return errors . New ( "invalid addressId" )
}
var op = NewNodeIPAddressOperator ( )
2021-12-16 10:09:19 +08:00
op . IsUp = true // IP必须在线备用IP才会有用
2021-09-13 10:51:05 +08:00
op . Id = addressId
op . BackupThresholdId = thresholdId
op . BackupIP = ip
err := this . Save ( tx , op )
if err != nil {
return err
}
return this . NotifyUpdate ( tx , addressId )
}
2021-11-18 14:30:53 +08:00
// UpdateAddressHealthCount 计算IP健康状态
2022-04-22 21:53:38 +08:00
func ( this * NodeIPAddressDAO ) UpdateAddressHealthCount ( tx * dbs . Tx , addrId int64 , newIsUp bool , maxUp int , maxDown int , autoUpDown bool ) ( changed bool , err error ) {
2021-11-18 14:30:53 +08:00
if addrId <= 0 {
return false , errors . New ( "invalid address id" )
}
one , err := this . Query ( tx ) .
Pk ( addrId ) .
2022-04-22 21:53:38 +08:00
Result ( "isHealthy" , "isUp" , "countUp" , "countDown" ) .
2021-11-18 14:30:53 +08:00
Find ( )
if err != nil {
return false , err
}
if one == nil {
return false , nil
}
2022-04-22 21:53:38 +08:00
var oldIsHealthy = one . ( * NodeIPAddress ) . IsHealthy
var oldIsUp = one . ( * NodeIPAddress ) . IsUp
2021-11-18 14:30:53 +08:00
// 如果新老状态一致,则不做任何事情
2022-04-22 21:53:38 +08:00
if oldIsHealthy == newIsUp {
// 如果自动上下线,则健康状况和是否在线保持一致
if autoUpDown {
if oldIsUp != oldIsHealthy {
err = this . Query ( tx ) .
Pk ( addrId ) .
Set ( "isUp" , newIsUp ) .
UpdateQuickly ( )
if err != nil {
return false , err
}
err = this . NotifyUpdate ( tx , addrId )
if err != nil {
return false , err
}
// 创建日志
if newIsUp {
err = SharedNodeIPAddressLogDAO . CreateLog ( tx , 0 , addrId , "健康检查上线" )
} else {
err = SharedNodeIPAddressLogDAO . CreateLog ( tx , 0 , addrId , "健康检查下线" )
}
if err != nil {
return true , err
}
return true , nil
}
}
2021-11-18 14:30:53 +08:00
return false , nil
}
2022-04-22 21:53:38 +08:00
var countUp = int ( one . ( * NodeIPAddress ) . CountUp )
var countDown = int ( one . ( * NodeIPAddress ) . CountDown )
2021-11-18 14:30:53 +08:00
2022-04-22 21:53:38 +08:00
var op = NewNodeIPAddressOperator ( )
2021-11-18 14:30:53 +08:00
op . Id = addrId
2022-04-22 21:53:38 +08:00
if newIsUp {
2021-11-18 14:30:53 +08:00
countUp ++
countDown = 0
if countUp >= maxUp {
changed = true
2022-04-22 21:53:38 +08:00
if autoUpDown {
op . IsUp = true
}
2021-11-18 14:30:53 +08:00
op . IsHealthy = true
}
} else {
countDown ++
countUp = 0
if countDown >= maxDown {
changed = true
2022-04-22 21:53:38 +08:00
if autoUpDown {
op . IsUp = false
}
2021-11-18 14:30:53 +08:00
op . IsHealthy = false
}
}
op . CountUp = countUp
op . CountDown = countDown
err = this . Save ( tx , op )
if err != nil {
return false , err
}
if changed {
err = this . NotifyUpdate ( tx , addrId )
if err != nil {
return true , err
}
2022-04-22 21:53:38 +08:00
// 创建日志
if autoUpDown {
if newIsUp {
err = SharedNodeIPAddressLogDAO . CreateLog ( tx , 0 , addrId , "健康检查上线" )
} else {
err = SharedNodeIPAddressLogDAO . CreateLog ( tx , 0 , addrId , "健康检查下线" )
}
}
2021-11-18 14:30:53 +08:00
}
return
}
2021-08-18 09:24:18 +08:00
// NotifyUpdate 通知更新
func ( this * NodeIPAddressDAO ) NotifyUpdate ( tx * dbs . Tx , addressId int64 ) error {
address , err := this . Query ( tx ) .
Pk ( addressId ) .
Result ( "nodeId" , "role" ) .
Find ( )
if err != nil {
return err
}
if address == nil {
return nil
}
var nodeId = int64 ( address . ( * NodeIPAddress ) . NodeId )
if nodeId == 0 {
return nil
}
var role = address . ( * NodeIPAddress ) . Role
switch role {
case nodeconfigs . NodeRoleNode :
2022-11-26 19:02:08 +08:00
err = dns . SharedDNSTaskDAO . CreateNodeTask ( tx , 0 , nodeId , dns . DNSTaskTypeNodeChange )
2022-04-04 12:08:08 +08:00
if err != nil {
return err
}
// 检查是否为L2以上级别
level , err := SharedNodeDAO . FindNodeLevel ( tx , nodeId )
if err != nil {
return err
}
if level > 1 {
err = SharedNodeDAO . NotifyLevelUpdate ( tx , nodeId )
if err != nil {
return err
}
}
2021-08-18 09:24:18 +08:00
}
return nil
}