From 0404c7fecdd982f1d3c54926daaaf719955db8aa Mon Sep 17 00:00:00 2001 From: sunty <1172534965@qq.com> Date: 星期四, 16 四月 2020 13:54:48 +0800 Subject: [PATCH] go restart unit test --- controllers/swfsControllers.go | 301 ++++++++++++++++++++++++++++++++++--------------- 1 files changed, 207 insertions(+), 94 deletions(-) diff --git a/controllers/swfsControllers.go b/controllers/swfsControllers.go index c4c8c38..8f29871 100644 --- a/controllers/swfsControllers.go +++ b/controllers/swfsControllers.go @@ -1,15 +1,34 @@ package controllers import ( + "encoding/json" "fmt" "github.com/gin-gonic/gin" + "io/ioutil" "net/http" "strconv" "strings" + "swfs/code" "swfs/config" "swfs/util" "sync" "time" +) + +const ( + StartServerScript = "seaweedfs_start.sh" + StopServerScript = "seaweedfs_stop.sh" + //浣滀负 volume 鍚姩 + StartScriptAsVolume = "1" + //浣滀负 master 鍚姩 + StartScriptAsMaster = "2" + //浣滀负 master + volume 鍚姩 + StartScriptAsMaVo = "3" + //鍚姩椤瑰弬鏁板ご + Option = "option=" + //master鍒楄〃椤瑰弬鏁板ご + Peer = "peers=" + //StartScriptAsFiler = "option=4" ) type SeaweedfsController struct{} @@ -18,97 +37,212 @@ Role string `json:"role"` } -//淇敼 func (sc *SeaweedfsController) UpdateSWFSServiceController(c *gin.Context) { - oldPeers := GetOldPeers() - newPeers := GetNewPeers() - UpdatePeers(oldPeers, newPeers) + ReplaceLineContentBySearch(GetNewPeers(), Peer, config.Server.ScriptPath, StartServerScript) + util.ResponseFormat(c, code.Success, config.Server.EsServerIp+"鏇存柊鎴愬姛") } +// @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 - nowPeers := GetNowPeersList() if role == "master" { - AsMaster(nowPeers) + AsMaster(role) + util.ResponseFormat(c, code.AddSuccess, "鍔犲叆鑺傜偣鎴愬姛") + return } else if role == "volume" { - AsVolume(nowPeers) + status := AsVolume() + if status == true { + util.ResponseFormat(c, code.AddSuccess, "鍔犲叆鑺傜偣鎴愬姛") + return + } else { + util.ResponseFormat(c, code.AddClusterInfoErr, "褰撳墠杩樻病鏈変富鑺傜偣") + return + } + } else if role == "master+volume" { + AsMaVo(role) + util.ResponseFormat(c, code.AddSuccess, "鍔犲叆鑺傜偣鎴愬姛") } else { + util.ResponseFormat(c, code.RequestParamError, "閫夋嫨鑺傜偣绫诲瀷閿欒") return } } -func ReplaceLineContentBySearch(replaceContent string) { - resContent := GetNowLineContent("/opt/vasystem/script/seaweedfs_start.sh", "#start_master_server") - replaceStr := "sed -ie 's/" + resContent + "/" + replaceContent + "/g' /opt/vasystem/seaweedfs_start.sh" +func AsMaVo(role string) { + AsMaster(role) + ReplaceLineContentBySearch(Option+StartScriptAsMaVo, Option, config.Server.ScriptPath, StartServerScript) +} + +func (sc *SeaweedfsController) RoleOfVolumeToMasterController(c *gin.Context) { + AsMaster("master") + ReplaceLineContentBySearch(Option+StartScriptAsMaVo, Option, config.Server.ScriptPath, StartServerScript) +} + +func (sc *SeaweedfsController) RestartServerController(c *gin.Context) { + StopServer(config.Server.ScriptPath) + time.Sleep(time.Second * 1) + StartServer(config.Server.ScriptPath) + //fmt.Println("GetLocalStartupItem: ", GetLocalStartupItem(config.Server.ScriptPath, StartServerScript)) + result := strings.Split(GetLocalStartupItem(config.Server.ScriptPath, StartServerScript), "=")[1] + fmt.Println("result: ", result) + util.ResponseFormat(c, code.Success, result) +} + +//鍚姩鏈嶅姟 +func StartServer(scriptPath string) { + util.RunScript("sh " + scriptPath + StartServerScript) +} + +//鍋滄鏈嶅姟 +func StopServer(scriptPath string) { + util.RunScript("sh " + scriptPath + StopServerScript) +} + +//鏍规嵁鎼滅储鍐呭鏇挎崲鏁磋鍐呭 +func ReplaceLineContentBySearch(replaceContent string, searchContent string, scriptPath string, scriptFile string) { + resSearchContent := GetNowLineContent(scriptPath+"/"+scriptFile, searchContent) + replaceStr := "sed -ie 's/" + resSearchContent + "/" + replaceContent + "/g' " + scriptPath + "/" + scriptFile util.RunScript(replaceStr) } -func (sc *SeaweedfsController) RestartMasterController(c *gin.Context) { - end := "sh /opt/vasystem/script/seaweedfs_stop.sh" - start := "sh /opt/vasystem/script/seaweedfs_start.sh" - util.RunScript(end) - util.RunScript(start) -} - -func UpdateAllNodesScriptArgument(nowPeers []interface{}) { - for _, val := range nowPeers { - ip := val.(string) +//鏇存柊鎵�鏈夎妭鐐圭殑鑴氭湰鍙傛暟 +func UpdateAllNodesScriptArgument(peersIp []string) { + fmt.Println("寮�濮嬫洿鏂版湰鍦伴厤缃枃浠�") + for _, ip := range peersIp { + fmt.Println("ip: ", ip) url := "http://" + ip + ":7020/node/api-v/swfs/updateSWFSService" - http.Get(url) + fmt.Println("url", url) + resp, _ := http.Get(url) + fmt.Println("鏇存柊杩斿洖鐘舵�侊細", resp.StatusCode) + if resp.StatusCode == 200 { + fmt.Println("璇锋眰瀹屾瘯", resp.StatusCode) + } } } -func RequestMasterNodesOperation(nowPeers []interface{}) { - //fmt.Println("config.Server.CoreBaseUnit", config.Server.CoreBaseUnit) +//璇锋眰浣滀负褰撳墠瑙掕壊鑺傜偣鎿嶄綔娴佺▼ +func RequestNodesOperation(nowPeers []interface{}, role string) { + peersIp := make([]string, 0) + for _, val := range nowPeers { + peersIp = append(peersIp, strings.Split(val.(string), ":")[0]) + } coreBaseUnit, _ := strconv.Atoi(config.Server.CoreBaseUnit) - //fmt.Println("nowPeers: ", nowPeers) - //fmt.Println("coreBaseUnit: ", coreBaseUnit) - UpdateAllNodesScriptArgument(nowPeers) - RestartAllMaster(nowPeers, coreBaseUnit) + UpdateAllNodesScriptArgument(peersIp) + replaceContent := "" + if role == "master" { + replaceContent = Option + StartScriptAsMaster + } else if role == "master+volume" { + replaceContent = Option + StartScriptAsMaVo + } + ReplaceLineContentBySearch(replaceContent, Option, config.Server.ScriptPath, StartServerScript) + RestartAllServer(peersIp, coreBaseUnit) } -func RestartServer(ip string, timeOut int) { - url := "http://" + ip + ":7020/node/api-v/swfs/restartMaster" - http.Get(url) +//閲嶅惎鎵�鏈夎妭鐐规湇鍔″苟楠岃瘉 +func Restart(ip string, timeOut int) { + url := "http://" + ip + ":7020/node/api-v/swfs/restartServer" + var info interface{} + httpRes, _ := http.Get(url) + defer httpRes.Body.Close() + body, _ := ioutil.ReadAll(httpRes.Body) + json.Unmarshal(body, &info) + res, ok := info.(map[string]interface{}) + if !ok { + fmt.Println("http response interface can not change map[string]interface{}") + } + startupItem := res["data"].(string) + if httpRes.StatusCode == 200 { + startupItem = string(body) + } tick := time.Tick(1 * time.Second) + fmt.Println("鍑嗗寮�濮嬮獙璇佽妭鐐规湇鍔�") 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 { + fmt.Println("绗�", countdown, "娆¢獙璇�") + result := Verification(startupItem, ip) + fmt.Println("绗竴娆¢獙璇乺esult缁撴灉锛�", result) + if result == true { break } <-tick } + fmt.Println("楠岃瘉瀹屾瘯") } -func RestartAllMaster(nowPeers []interface{}, coreBaseUnit int) { - coreThread := len(nowPeers)/coreBaseUnit + 1 +//楠岃瘉鏈嶅姟鐘舵�� +func Verification(startupItem string, ip string) bool { + resStatu := false + switch startupItem { + case StartScriptAsVolume: + verificationVolumeUrl := "http://" + ip + ":6700" + _, volume1Err := http.Get(verificationVolumeUrl) + if volume1Err == nil { + resStatu = true + } + case StartScriptAsMaster: + verificationMasterUrl := "http://" + ip + ":6333" + _, masterErr := http.Get(verificationMasterUrl) + if masterErr == nil { + resStatu = true + } + case StartScriptAsMaVo: + verificationMasterUrl := "http://" + ip + ":6333" + verificationVolumeUrl := "http://" + ip + ":6700" + _, masterErr := http.Get(verificationMasterUrl) + _, volume1Err := http.Get(verificationVolumeUrl) + if masterErr == nil && volume1Err == nil { + resStatu = true + } + } + return resStatu +} + +//鑾峰彇鏈湴鍚姩椤� +func GetLocalStartupItem(scriptPath string, scriptFile string) string { + startupItem := GetNowLineContent(scriptPath+"/"+scriptFile, Option) + fmt.Println("startupItem: ", startupItem) + return startupItem +} + +//鏋勫缓閲嶅惎娴佺▼ +func RestartAllServer(peersIp []string, coreBaseUnit int) { + fmt.Println("寮�濮嬫瀯寤洪噸鍚祦绋�") + coreThread := len(peersIp)/coreBaseUnit + 1 masterIp := make([]string, 0) timeOut, _ := strconv.Atoi(config.Server.TimeOut) var waitGroup sync.WaitGroup - for i, val := range nowPeers { - ip := val.(string) + fmt.Println("褰撳墠骞惰搴oreThread锛�", coreThread) + for i, ip := range peersIp { + fmt.Println("閲嶅惎褰撳墠缁勬湇鍔�" + ip) if (i+1)%coreThread == 0 { masterIp = append(masterIp, strings.Split(ip, ":")[0]) + fmt.Println("鍔犲叆绗竴缁勫苟寮�濮嬮獙璇佺涓�缁� masterIp: ", masterIp) for i := 0; i < len(masterIp); i++ { - go RestartServer(masterIp[i], timeOut) + fmt.Println("len masterIp: ", len(masterIp)) + fmt.Println("绗�" + strconv.Itoa(i) + "涓嚎绋�") + go Restart(masterIp[i], timeOut) waitGroup.Add(1) //姣忓垱寤轰竴涓猤oroutine锛屽氨鎶婁换鍔¢槦鍒椾腑浠诲姟鐨勬暟閲�+1 } waitGroup.Wait() //.Wait()杩欓噷浼氬彂鐢熼樆濉烇紝鐩村埌闃熷垪涓墍鏈夌殑浠诲姟缁撴潫灏变細瑙i櫎闃诲 + fmt.Println("褰撳墠缁勪换鍔″畬鎴�") masterIp = make([]string, 0) + fmt.Println("娓呯┖褰撳墠缁勬垚鍛橈細", masterIp) } else { masterIp = append(masterIp, strings.Split(ip, ":")[0]) - if len(nowPeers) == i+1 { + if len(peersIp) == i+1 { var waitGroup sync.WaitGroup for i := 0; i < len(masterIp); i++ { - go RestartServer(masterIp[i], timeOut) + go Restart(masterIp[i], timeOut) waitGroup.Add(1) //姣忓垱寤轰竴涓猤oroutine锛屽氨鎶婁换鍔¢槦鍒椾腑浠诲姟鐨勬暟閲�+1 } waitGroup.Wait() //.Wait()杩欓噷浼氬彂鐢熼樆濉烇紝鐩村埌闃熷垪涓墍鏈夌殑浠诲姟缁撴潫灏变細瑙i櫎闃诲 @@ -116,38 +250,42 @@ break } } - continue + } + fmt.Println("鏈嶅姟娴佺▼鎵ц瀹屾瘯") + } -func GetOldPeers() string { - str := "cat /opt/vasystem/seaweedfs_start.sh | grep peers=" - peers := strings.Split(util.RunScript(str), "\n")[0] - return peers -} - +//鑾峰彇鏌ユ壘鍐呭褰撳墠琛屽唴瀹� func GetNowLineContent(filePath string, searchContent string) string { - scriptStr := "cat" + filePath + "| grep " + searchContent - content := strings.Split(util.RunScript(scriptStr), "\n")[0] - return content + scriptStr := "cat " + filePath + "| grep " + searchContent + fmt.Println("scriptStr: ", scriptStr) + return strings.Split(util.RunScript(scriptStr), "\n")[0] } -func AsVolume(nowPeers []interface{}) { - ReplaceLineContentBySearch("start_master_server") - fmt.Println(nowPeers) +//浣滀负鏁版嵁鑺傜偣鍔犲叆 +func AsVolume() bool { + nowPeers := GetNowPeersList() + if nowPeers == nil || len(nowPeers) == 0 { + return false + } + ReplaceLineContentBySearch(Option+StartScriptAsVolume, Option, config.Server.ScriptPath, StartServerScript) + ReplaceLineContentBySearch(GetNewPeers(), Peer, config.Server.ScriptPath, StartServerScript) + StartServer(config.Server.ScriptPath) + return true } -func AsMaster(nowPeers []interface{}) { +//浣滀负涓昏妭鐐瑰姞鍏ワ紙榛樿鍖呭惈鏁版嵁鑺傜偣锛� +func AsMaster(role string) bool { AddNewMasterToPeers() - RequestMasterNodesOperation(nowPeers) + nowPeers := GetNowPeersList() + fmt.Println("nowPeers: ", nowPeers) + RequestNodesOperation(nowPeers, role) + return true } -func (sc *SeaweedfsController) RoleOfVolumeToMasterController(c *gin.Context) { - AsMaster(GetNowPeersList()) -} - +//鑾峰彇褰撳墠闆嗙兢鍒楄〃 func GetNowPeersList() []interface{} { - getUrl := "http://" + config.Server.EsServerIp + ":" + config.Server.EsServerPort + "/" + config.BasicFS.IndexName + "/_search" getJson := `{ "query": { @@ -166,46 +304,21 @@ buf, _ := util.EsReq("POST", getUrl, []byte(getJson)) source, _ := util.Sourcelist(buf) - //fmt.Println(source) peers := source[0]["peers"].([]interface{}) return peers } +//鑾峰彇褰撳墠闆嗙兢鍒楄〃鏍煎紡鍖栦俊鎭� func GetNewPeers() string { - - 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) - //fmt.Println(source) - peers := source[0]["peers"].([]interface{}) - fmt.Println(peers) - p := "peers=" + strings.Replace(strings.Trim(fmt.Sprint(peers), "[]"), " ", ",", -1) + peers := GetNowPeersList() + p := Peer + strings.Replace(strings.Trim(fmt.Sprint(peers), "[]"), " ", ",", -1) return p } -func UpdatePeers(oldPeers string, newPeers string) { - str := "sed -ie 's/" + oldPeers + "/" + newPeers + "/g' /opt/vasystem/seaweedfs_start.sh" - util.RunScript(str) -} - +//鍚戦泦缇ゅ姞鍏ユ柊鐨刴aster func AddNewMasterToPeers() (result bool) { peer := config.Server.EsServerIp + ":6333" - addUrl := "http://" + config.Server.EsServerIp + ":" + config.Server.EsServerPort + "/" + config.BasicFS.IndexName + "/_update_by_query" + addUrl := "http://" + config.Server.EsServerIp + ":" + config.Server.EsServerPort + "/" + config.BasicFS.IndexName + "/_update_by_query?refresh=true" addJson := `{ "script": { "lang": "painless", -- Gitblit v1.8.0