From 32aba6aa23e78886ddca2743f760ff6de22f4b50 Mon Sep 17 00:00:00 2001 From: sunty <1172534965@qq.com> Date: 星期一, 13 四月 2020 10:46:04 +0800 Subject: [PATCH] perfect asVolume somethings --- controllers/swfsControllers.go | 203 ++++++++++++++++++++++++++++++++++++-------------- 1 files changed, 145 insertions(+), 58 deletions(-) diff --git a/controllers/swfsControllers.go b/controllers/swfsControllers.go index 17d0b7b..18d0eb9 100644 --- a/controllers/swfsControllers.go +++ b/controllers/swfsControllers.go @@ -3,10 +3,21 @@ import ( "fmt" "github.com/gin-gonic/gin" + "net/http" + "strconv" "strings" - "test/config" - "test/util" + "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{} @@ -15,81 +26,176 @@ Role string `json:"role"` } -//淇敼 func (sc *SeaweedfsController) UpdateSWFSServiceController(c *gin.Context) { - oldPeers := GetOldPeers() + oldPeers := GetOldPeers(config.Server.ScriptPath, StartServerScript) newPeers := GetNewPeers() - UpdatePeers(oldPeers, newPeers) + 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() + status := AsVolume() + if status == true { + util.ResponseFormat(c, code.AddSuccess, "鍔犲叆鑺傜偣鎴愬姛") + return + } else { + util.ResponseFormat(c, code.AddClusterInfoErr, "褰撳墠杩樻病鏈変富鑺傜偣") + return + } } else { - + util.ResponseFormat(c, code.RequestParamError, "閫夋嫨鑺傜偣绫诲瀷閿欒") return } } -func (sc *SeaweedfsController) RestartMaster(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 (sc *SeaweedfsController) RoleOfVolumeToMasterController(c *gin.Context) { + AsMaster() } -func RequestMasterNodesOperation() { - nowPeers := GetNowPeersList() - coreThread := len(nowPeers)/100 + 1 +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 _, val := range masterIp { - RestartOrtherMaster(val) - masterIp = append(masterIp[:0], masterIp[1:]...) + for i := 0; i < len(masterIp); i++ { + go RestartServer(masterIp[i], timeOut) + waitGroup.Add(1) //姣忓垱寤轰竴涓猤oroutine锛屽氨鎶婁换鍔¢槦鍒椾腑浠诲姟鐨勬暟閲�+1 } - time.Sleep(time.Second * 2) + waitGroup.Wait() //.Wait()杩欓噷浼氬彂鐢熼樆濉烇紝鐩村埌闃熷垪涓墍鏈夌殑浠诲姟缁撴潫灏变細瑙i櫎闃诲 + masterIp = make([]string, 0) } else { masterIp = append(masterIp, strings.Split(ip, ":")[0]) if len(nowPeers) == i+1 { - for _, val := range masterIp { - RestartOrtherMaster(val) + var waitGroup sync.WaitGroup + for i := 0; i < len(masterIp); i++ { + go RestartServer(masterIp[i], timeOut) + waitGroup.Add(1) //姣忓垱寤轰竴涓猤oroutine锛屽氨鎶婁换鍔¢槦鍒椾腑浠诲姟鐨勬暟閲�+1 } + waitGroup.Wait() //.Wait()杩欓噷浼氬彂鐢熼樆濉烇紝鐩村埌闃熷垪涓墍鏈夌殑浠诲姟缁撴潫灏变細瑙i櫎闃诲 + masterIp = make([]string, 0) break } - continue } + continue } } -func GetOldPeers() string { - str := "cat /opt/vasystem/seaweedfs_start.sh | grep peers=" - peers := strings.Split(util.RunScript(str), "\n")[0] - return peers +//鑾峰彇鏈湴浠ヤ娇鐢ㄩ泦缇よ〃鍗� +func GetOldPeers(scriptPath string, scriptFile string) string { + return GetNowLineContent(scriptPath+"/"+scriptFile, "peers=") } -func AsVolume() { +//鑾峰彇鏌ユ壘鍐呭褰撳墠琛屽唴瀹� +func GetNowLineContent(filePath string, searchContent string) string { + scriptStr := "cat" + filePath + "| grep " + searchContent + return strings.Split(util.RunScript(scriptStr), "\n")[0] +} + +//浣滀负鏁版嵁鑺傜偣鍔犲叆 +func AsVolume() bool { nowPeers := GetNowPeersList() - fmt.Println(nowPeers) + if nowPeers == nil || len(nowPeers) == 0 { + return false + } + UpdatePeers(config.Server.ScriptPath, StartServerScript, GetNewPeers(), GetNewPeers()) + ReplaceLineContentBySearch(StartScriptAsVolume, config.Server.ScriptPath, StartServerScript) + ReplaceLineContentBySearch("peers=", config.Server.ScriptPath, StartServerScript) + StartServer(config.Server.ScriptPath) + return true } -func AsMaster() { +//浣滀负涓昏妭鐐瑰姞鍏ワ紙榛樿鍖呭惈鏁版嵁鑺傜偣锛� +func AsMaster() bool { nowPeers := GetNowPeersList() - coreThread := len(nowPeers) / 100 - fmt.Println(coreThread) - + RequestMasterNodesOperation(nowPeers) + return true } +//鑾峰彇褰撳墠闆嗙兢鍒楄〃 func GetNowPeersList() []interface{} { - getUrl := "http://" + config.Server.EsServerIp + ":" + config.Server.EsServerPort + "/" + config.BasicFS.IndexName + "/_search" getJson := `{ "query": { @@ -108,43 +214,24 @@ 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) + peers := GetNowPeersList() p := "peers=" + 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" +//鏇存柊鏈湴闆嗙兢鍒楄〃 +func UpdatePeers(scriptPath string, scriptFile string, oldPeers string, newPeers string) { + str := "sed -ie 's/" + oldPeers + "/" + newPeers + "/g' " + scriptPath + "/" + scriptFile 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" -- Gitblit v1.8.0