sunty
2020-04-13 ae710c2a5d10cb0c0556f6dc7814312185debb66
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package controllers
 
import (
    "fmt"
    "github.com/gin-gonic/gin"
    "net/http"
    "strconv"
    "strings"
    "swfs/code"
    "swfs/config"
    "swfs/util"
    "sync"
    "time"
)
 
const (
    StartServerScript   = "seaweedfs_start.sh"
    StopServerScript    = "seaweedfs_stop.sh"
    StartScriptAsVolume = "option=1"
    StartScriptAsMaster = "option=2"
)
 
type SeaweedfsController struct{}
 
type SWFSInfo struct {
    Role string `json:"role"`
}
 
func (sc *SeaweedfsController) UpdateSWFSServiceController(c *gin.Context) {
    oldPeers := GetOldPeers(config.Server.ScriptPath, StartServerScript)
    newPeers := GetNewPeers()
    UpdatePeers(config.Server.ScriptPath, StartServerScript, oldPeers, newPeers)
    ReplaceLineContentBySearch(StartScriptAsVolume, config.Server.ScriptPath, StartServerScript)
}
 
// @Security ApiKeyAuth
// @Summary 新节点加入
// @Description  新节点加入
// @Accept  json
// @Produce json
// @Tags swfs
// @Param obj body SWFSInfo true "加入角色参数"
// @Success 200 {string} json "{"code":200, msg:"", success:true}"
// @Failure 500 {string} json "{"code":500, msg:"", success:false}"
// @Router /node/api-v/swfs/addSWFSNode [POST]
func (sc *SeaweedfsController) AddSWFSNodeController(c *gin.Context) {
    var body SWFSInfo
    c.BindJSON(&body)
    role := body.Role
    if role == "master" {
        AddNewMasterToPeers()
        AsMaster()
        util.ResponseFormat(c, code.AddSuccess, "加入节点成功")
        return
    } else if role == "volume" {
        AsVolume()
        util.ResponseFormat(c, code.AddSuccess, "加入节点成功")
        return
    } else {
        util.ResponseFormat(c, code.RequestParamError, "选择节点类型错误")
        return
    }
 
}
 
func (sc *SeaweedfsController) RoleOfVolumeToMasterController(c *gin.Context) {
    AsMaster()
}
 
func (sc *SeaweedfsController) RestartMasterController(c *gin.Context) {
    StopServer(config.Server.ScriptPath)
    time.Sleep(time.Second * 1)
    StartServer(config.Server.ScriptPath)
}
 
//启动服务
func StartServer(scriptPath string) {
    util.RunScript("sh " + scriptPath + StartServerScript)
}
 
//停止服务
func StopServer(scriptPath string) {
    util.RunScript("sh " + scriptPath + StopServerScript)
}
 
//根据搜索内容替换整行内容
func ReplaceLineContentBySearch(replaceContent string, scriptPath string, scriptFile string) {
    resContent := GetNowLineContent(scriptPath+"/"+scriptFile, "option=")
    replaceStr := "sed -ie 's/" + resContent + "/" + replaceContent + "/g' " + scriptPath + "/" + scriptFile
    util.RunScript(replaceStr)
}
 
//更新所有节点的脚本参数
func UpdateAllNodesScriptArgument(nowPeers []interface{}) {
    for _, val := range nowPeers {
        ip := val.(string)
        url := "http://" + ip + ":7020/node/api-v/swfs/updateSWFSService"
        http.Get(url)
    }
}
 
//请求作为主节点操作流程
func RequestMasterNodesOperation(nowPeers []interface{}) {
    coreBaseUnit, _ := strconv.Atoi(config.Server.CoreBaseUnit)
    UpdateAllNodesScriptArgument(nowPeers)
    RestartAllServer(nowPeers, coreBaseUnit)
}
 
//重启其他节点服务并验证
func RestartServer(ip string, timeOut int) {
    url := "http://" + ip + ":7020/node/api-v/swfs/restartMaster"
    http.Get(url)
    tick := time.Tick(1 * time.Second)
    for countdown := timeOut; countdown > 0; countdown-- {
        verificationMasterUrl := "http://" + ip + ":6333"
        verificationVolume1Url := "http://" + ip + ":6700"
        verificationVolume2Url := "http://" + ip + ":6701"
        _, masterErr := http.Get(verificationMasterUrl)
        _, volume1Err := http.Get(verificationVolume1Url)
        _, volume2Err := http.Get(verificationVolume2Url)
        if masterErr == nil && volume1Err == nil && volume2Err == nil {
            break
        }
        <-tick
    }
}
 
//构建重启流程
func RestartAllServer(nowPeers []interface{}, coreBaseUnit int) {
    coreThread := len(nowPeers)/coreBaseUnit + 1
    masterIp := make([]string, 0)
    timeOut, _ := strconv.Atoi(config.Server.TimeOut)
    var waitGroup sync.WaitGroup
    for i, val := range nowPeers {
        ip := val.(string)
        if (i+1)%coreThread == 0 {
            masterIp = append(masterIp, strings.Split(ip, ":")[0])
            for i := 0; i < len(masterIp); i++ {
                go RestartServer(masterIp[i], timeOut)
                waitGroup.Add(1) //每创建一个goroutine,就把任务队列中任务的数量+1
            }
            waitGroup.Wait() //.Wait()这里会发生阻塞,直到队列中所有的任务结束就会解除阻塞
            masterIp = make([]string, 0)
        } else {
            masterIp = append(masterIp, strings.Split(ip, ":")[0])
            if len(nowPeers) == i+1 {
                var waitGroup sync.WaitGroup
                for i := 0; i < len(masterIp); i++ {
                    go RestartServer(masterIp[i], timeOut)
                    waitGroup.Add(1) //每创建一个goroutine,就把任务队列中任务的数量+1
                }
                waitGroup.Wait() //.Wait()这里会发生阻塞,直到队列中所有的任务结束就会解除阻塞
                masterIp = make([]string, 0)
                break
            }
        }
        continue
    }
}
 
//获取本地以使用集群表单
func GetOldPeers(scriptPath string, scriptFile string) string {
    return GetNowLineContent(scriptPath+"/"+scriptFile, "peers=")
}
 
//获取查找内容当前行内容
func GetNowLineContent(filePath string, searchContent string) string {
    scriptStr := "cat" + filePath + "| grep " + searchContent
    return strings.Split(util.RunScript(scriptStr), "\n")[0]
}
 
//作为数据节点加入
func AsVolume() {
    UpdatePeers(config.Server.ScriptPath, StartServerScript, GetNewPeers(), GetNewPeers())
    ReplaceLineContentBySearch(StartScriptAsVolume, config.Server.ScriptPath, StartServerScript)
    ReplaceLineContentBySearch("peers=", config.Server.ScriptPath, StartServerScript)
    StartServer(config.Server.ScriptPath)
}
 
//作为主节点加入(默认包含数据节点)
func AsMaster() {
    nowPeers := GetNowPeersList()
    RequestMasterNodesOperation(nowPeers)
}
 
//获取当前集群列表
func GetNowPeersList() []interface{} {
    getUrl := "http://" + config.Server.EsServerIp + ":" + config.Server.EsServerPort + "/" + config.BasicFS.IndexName + "/_search"
    getJson := `{
    "query": {
        "bool": {
            "filter": [
                {
                    "term": {
                        "application":"nodeOperation"
                    }
                }
            ]
        }
    },
    "size": 1
}`
 
    buf, _ := util.EsReq("POST", getUrl, []byte(getJson))
    source, _ := util.Sourcelist(buf)
    peers := source[0]["peers"].([]interface{})
    return peers
}
 
//获取当前集群列表格式化信息
func GetNewPeers() string {
    peers := GetNowPeersList()
    p := "peers=" + strings.Replace(strings.Trim(fmt.Sprint(peers), "[]"), " ", ",", -1)
    return p
}
 
//更新本地集群列表
func UpdatePeers(scriptPath string, scriptFile string, oldPeers string, newPeers string) {
    str := "sed -ie 's/" + oldPeers + "/" + newPeers + "/g' " + scriptPath + "/" + scriptFile
    util.RunScript(str)
}
 
//向集群加入新的master
func AddNewMasterToPeers() (result bool) {
    peer := config.Server.EsServerIp + ":6333"
    addUrl := "http://" + config.Server.EsServerIp + ":" + config.Server.EsServerPort + "/" + config.BasicFS.IndexName + "/_update_by_query"
    addJson := `{
    "script": {
        "lang": "painless",
        "inline": "ctx._source.peers.add(params.newpeer)",
        "params": {
            "newpeer": "` + peer + `"
        }
    },
    "query": {
        "bool": {
            "filter": [
                {
                    "term": {
                        "application": "nodeOperation"
                    }
                }
            ]
        }
    }
}`
    buf, _ := util.EsReq("POST", addUrl, []byte(addJson))
    updateRes, _ := util.SourceUpdated(buf)
    if updateRes == -1 {
        result = false
    } else {
        result = true
    }
    return result
}