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 | 70 ++++++++++++++++++++++++---------- 1 files changed, 49 insertions(+), 21 deletions(-) diff --git a/controllers/swfsControllers.go b/controllers/swfsControllers.go index a52ec64..8f29871 100644 --- a/controllers/swfsControllers.go +++ b/controllers/swfsControllers.go @@ -57,7 +57,7 @@ c.BindJSON(&body) role := body.Role if role == "master" { - AsMaster() + AsMaster(role) util.ResponseFormat(c, code.AddSuccess, "鍔犲叆鑺傜偣鎴愬姛") return } else if role == "volume" { @@ -70,7 +70,7 @@ return } } else if role == "master+volume" { - AsMaVo() + AsMaVo(role) util.ResponseFormat(c, code.AddSuccess, "鍔犲叆鑺傜偣鎴愬姛") } else { util.ResponseFormat(c, code.RequestParamError, "閫夋嫨鑺傜偣绫诲瀷閿欒") @@ -79,13 +79,13 @@ } -func AsMaVo() { - AsMaster() +func AsMaVo(role string) { + AsMaster(role) ReplaceLineContentBySearch(Option+StartScriptAsMaVo, Option, config.Server.ScriptPath, StartServerScript) } func (sc *SeaweedfsController) RoleOfVolumeToMasterController(c *gin.Context) { - AsMaster() + AsMaster("master") ReplaceLineContentBySearch(Option+StartScriptAsMaVo, Option, config.Server.ScriptPath, StartServerScript) } @@ -117,21 +117,36 @@ } //鏇存柊鎵�鏈夎妭鐐圭殑鑴氭湰鍙傛暟 -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" fmt.Println("url", url) - http.Get(url) + resp, _ := http.Get(url) + fmt.Println("鏇存柊杩斿洖鐘舵�侊細", resp.StatusCode) + if resp.StatusCode == 200 { + fmt.Println("璇锋眰瀹屾瘯", resp.StatusCode) + } } } -//璇锋眰浣滀负涓昏妭鐐规搷浣滄祦绋� -func RequestMasterNodesOperation(nowPeers []interface{}) { +//璇锋眰浣滀负褰撳墠瑙掕壊鑺傜偣鎿嶄綔娴佺▼ +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) - UpdateAllNodesScriptArgument(nowPeers) - RestartAllServer(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) } //閲嶅惎鎵�鏈夎妭鐐规湇鍔″苟楠岃瘉 @@ -151,13 +166,17 @@ startupItem = string(body) } tick := time.Tick(1 * time.Second) + fmt.Println("鍑嗗寮�濮嬮獙璇佽妭鐐规湇鍔�") for countdown := timeOut; countdown > 0; countdown-- { + fmt.Println("绗�", countdown, "娆¢獙璇�") result := Verification(startupItem, ip) + fmt.Println("绗竴娆¢獙璇乺esult缁撴灉锛�", result) if result == true { break } <-tick } + fmt.Println("楠岃瘉瀹屾瘯") } //楠岃瘉鏈嶅姟鐘舵�� @@ -196,24 +215,31 @@ } //鏋勫缓閲嶅惎娴佺▼ -func RestartAllServer(nowPeers []interface{}, coreBaseUnit int) { - coreThread := len(nowPeers)/coreBaseUnit + 1 +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++ { + 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 Restart(masterIp[i], timeOut) @@ -224,8 +250,10 @@ break } } - continue + } + fmt.Println("鏈嶅姟娴佺▼鎵ц瀹屾瘯") + } //鑾峰彇鏌ユ壘鍐呭褰撳墠琛屽唴瀹� @@ -248,11 +276,11 @@ } //浣滀负涓昏妭鐐瑰姞鍏ワ紙榛樿鍖呭惈鏁版嵁鑺傜偣锛� -func AsMaster() bool { +func AsMaster(role string) bool { AddNewMasterToPeers() nowPeers := GetNowPeersList() fmt.Println("nowPeers: ", nowPeers) - RequestMasterNodesOperation(nowPeers) + RequestNodesOperation(nowPeers, role) return true } -- Gitblit v1.8.0