From fa96e243e2adc6a72f41dbab7152e0312c657733 Mon Sep 17 00:00:00 2001 From: sunty <1172534965@qq.com> Date: 星期四, 16 四月 2020 11:51:02 +0800 Subject: [PATCH] uasa unit test --- controllers/swfsControllers.go | 75 ++++++++++++++++++++++++++----------- 1 files changed, 53 insertions(+), 22 deletions(-) diff --git a/controllers/swfsControllers.go b/controllers/swfsControllers.go index 32aa128..5006f36 100644 --- a/controllers/swfsControllers.go +++ b/controllers/swfsControllers.go @@ -1,6 +1,7 @@ package controllers import ( + "encoding/json" "fmt" "github.com/gin-gonic/gin" "io/ioutil" @@ -80,12 +81,12 @@ func AsMaVo() { AsMaster() - ReplaceLineContentBySearch(StartScriptAsMaVo, Option, config.Server.ScriptPath, StartServerScript) + ReplaceLineContentBySearch(Option+StartScriptAsMaVo, Option, config.Server.ScriptPath, StartServerScript) } func (sc *SeaweedfsController) RoleOfVolumeToMasterController(c *gin.Context) { AsMaster() - ReplaceLineContentBySearch(StartScriptAsMaVo, Option, config.Server.ScriptPath, StartServerScript) + ReplaceLineContentBySearch(Option+StartScriptAsMaVo, Option, config.Server.ScriptPath, StartServerScript) } func (sc *SeaweedfsController) RestartServerController(c *gin.Context) { @@ -116,51 +117,75 @@ } //鏇存柊鎵�鏈夎妭鐐圭殑鑴氭湰鍙傛暟 -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{}) { + 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) + RestartAllServer(peersIp, coreBaseUnit) } //閲嶅惎鎵�鏈夎妭鐐规湇鍔″苟楠岃瘉 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) - startupItem := "" + 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-- { - Verification(startupItem, ip) + fmt.Println("绗竴娆¢獙璇�") + result := Verification(startupItem, ip) + fmt.Println("绗竴娆¢獙璇乺esult缁撴灉锛�", result) + if result == true { + break + } <-tick } } -func Verification(startupItem string, ip string) { +//楠岃瘉鏈嶅姟鐘舵�� +func Verification(startupItem string, ip string) bool { + resStatu := false switch startupItem { case StartScriptAsVolume: verificationVolumeUrl := "http://" + ip + ":6700" _, volume1Err := http.Get(verificationVolumeUrl) if volume1Err == nil { - break + resStatu = true } case StartScriptAsMaster: verificationMasterUrl := "http://" + ip + ":6333" _, masterErr := http.Get(verificationMasterUrl) if masterErr == nil { - break + resStatu = true } case StartScriptAsMaVo: verificationMasterUrl := "http://" + ip + ":6333" @@ -168,9 +193,10 @@ _, masterErr := http.Get(verificationMasterUrl) _, volume1Err := http.Get(verificationVolumeUrl) if masterErr == nil && volume1Err == nil { - break + resStatu = true } } + return resStatu } //鑾峰彇鏈湴鍚姩椤� @@ -181,16 +207,21 @@ } //鏋勫缓閲嶅惎娴佺▼ -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 } @@ -198,7 +229,7 @@ masterIp = make([]string, 0) } 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) @@ -209,7 +240,6 @@ break } } - continue } } @@ -226,7 +256,7 @@ if nowPeers == nil || len(nowPeers) == 0 { return false } - ReplaceLineContentBySearch(StartScriptAsVolume, Option, config.Server.ScriptPath, StartServerScript) + ReplaceLineContentBySearch(Option+StartScriptAsVolume, Option, config.Server.ScriptPath, StartServerScript) ReplaceLineContentBySearch(GetNewPeers(), Peer, config.Server.ScriptPath, StartServerScript) StartServer(config.Server.ScriptPath) return true @@ -236,6 +266,7 @@ func AsMaster() bool { AddNewMasterToPeers() nowPeers := GetNowPeersList() + fmt.Println("nowPeers: ", nowPeers) RequestMasterNodesOperation(nowPeers) return true } @@ -274,7 +305,7 @@ //鍚戦泦缇ゅ姞鍏ユ柊鐨刴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