zhangzengfei
2023-10-24 c5654846d3b8b002284dee57aa50e95d67649f0e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package models
 
import (
    "errors"
    "fmt"
    "strconv"
    "time"
    "vamicro/config"
    "vamicro/extend/util"
 
    "basic.com/syncdb.git"
)
 
type Cluster struct {
    ClusterId   string `gorm:"primary_key;column:cluster_id" json:"cluster_id"`
    ClusterName string `gorm:"column:cluster_name" json:"cluster_name"`
    Password    string `gorm:"column:password" json:"password"`
    VirtualIp   string `gorm:"column:virtual_ip" json:"virtual_ip"`
}
 
func (Cluster) TableName() string {
    return "cluster"
}
 
func (c *Cluster) FindAll() (arr []Cluster, err error) {
    if err = db.Table("cluster").Find(&arr).Error; err != nil {
        return nil, err
    }
    return arr, nil
}
 
func (c *Cluster) FindByClusterId() (result Cluster, err error) {
    if c.ClusterId == "" {
        return Cluster{}, fmt.Errorf("param clusterId is nil error")
    }
    if err = db.Table("cluster").Where("cluster_id=?", c.ClusterId).First(&result).Error; err != nil {
        return Cluster{}, err
    }
    return result, nil
}
 
func (c *Cluster) FindByVirtualIp() (result Cluster, err error) {
    if c.VirtualIp == "" {
        return Cluster{}, fmt.Errorf("param virtual_ip is nil error")
    }
    if err = db.Table("cluster").Where("virtual_ip=?", c.VirtualIp).First(&result).Error; err != nil {
        return Cluster{}, err
    }
    return result, nil
}
 
func (c *Cluster) Create() error {
    var localConfig LocalConfig
    err := localConfig.Select()
    if err != nil {
        return err
    }
 
    serverId := config.Server.AnalyServerId
    if serverId == "" {
        return errors.New("serverId 为空")
    }
 
    serverIp, _, e := util.GetLocalIP(config.Server.NetworkAdapter)
    if e != nil {
        return e
    }
 
    tx := db.Begin()
    defer func() {
        if err != nil && tx != nil {
            tx.Rollback()
        }
    }()
 
    sql := "insert into cluster (cluster_id,cluster_name,password,virtual_ip) values ('" + c.ClusterId + "','" + c.ClusterName + "','" + c.Password + "','" + c.VirtualIp + "')"
    if err = tx.Exec(sql).Error; err != nil {
        return err
    }
    timeUnix := time.Now().Unix()
    fmtTimeStr := time.Unix(timeUnix, 0).Format("2006-01-02 15:04:05")
    //添加本身节点
    sql = "insert into cluster_node (id,cluster_id,node_name,node_id,node_ip,create_time,isDelete,device_type,drift_state) values ('" + serverId + "','" + c.ClusterId + "','" + localConfig.ServerName + "','" + serverId + "','" + (serverIp + ":" + strconv.Itoa(syncdb.DefaultBindPort)) + "','" + fmtTimeStr + "',0,'" + config.Server.DeviceType + "','master')"
    if err = tx.Exec(sql).Error; err != nil {
        return err
    }
 
    tx.Commit()
 
    return nil
}
 
func (c *Cluster) UpdateClusterName(clusterName string, virtualIp string) bool {
    arr, e := c.FindAll()
    if e == nil && arr != nil && len(arr) > 0 {
        result := db.Exec("update cluster set cluster_name='" + clusterName + "',virtual_ip='" + virtualIp + "' where cluster_id='" + arr[0].ClusterId + "'")
        if result.Error != nil {
            return false
        }
        if result.RowsAffected > 0 {
            return true
        }
    }
    return false
}
 
func (c *Cluster) UpdateNodeName(nodeName string, nodeId string) bool {
    arr, _ := c.FindAll()
    if arr != nil && len(arr) > 0 {
        c := arr[0]
        result := db.Exec("update cluster_node set node_name='" + nodeName + "' where cluster_id='" + c.ClusterId + "' and id='" + nodeId + "'")
        if result.Error != nil {
            return false
        }
        return result.RowsAffected > 0
    }
 
    return false
}
 
func (c *Cluster) FindAllNodeMap() map[string]Node {
    m := make(map[string]Node, 0)
    arr, err := c.FindAll()
    if err == nil && arr != nil {
        var nodeE Node
        for _, ct := range arr {
            nodes, e := nodeE.FindNodesByClusterId(ct.ClusterId)
            if e == nil && nodes != nil {
                for _, nd := range nodes {
                    m[nd.NodeId] = nd
                }
            }
        }
    }
 
    return m
}