deployer.go 8.2 KB
Newer Older
D
dangyifei 已提交
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
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package transfer

import (
	"fmt"
	"github.com/Badangel/logex"
	"strconv"
	"time"
	"transfer/dict"
)

func DeployStart(versionInfo dict.DictVersionInfo) error {
	fmt.Printf("[entry]start deploy\n")
	logex.Noticef("[entry]start deploy")
	var err error
	for {
		switch Dict.WaitVersionInfo.Status {
		case dict.Dict_Status_Deploying, dict.Dict_Status_Downloading:
			CmdInstsDownload()
		case dict.Dict_Status_Download_Succ, dict.Dict_Status_Reloading:
			CmdInstsReload()
		case dict.Dict_Status_Reload_Succ, dict.Dict_Status_Enabling:
			CmdInstsEnable()
		default:
			logex.Noticef("dict status %v", Dict.WaitVersionInfo.Status)
		}
		if Dict.WaitVersionInfo.Status == dict.Dict_Status_Finished {
			Dict.WaitVersionInfo.FinishTime = int(time.Now().Unix())
			Dict.CurrentVersionInfo = append(Dict.CurrentVersionInfo, Dict.WaitVersionInfo)
			fmt.Printf("[deploy finish]version:%v\n", Dict.WaitVersionInfo)
			logex.Noticef("[deploy finish]version:%v", Dict.WaitVersionInfo)
			var newVersionInfo dict.DictVersionInfo
			Dict.WaitVersionInfo = newVersionInfo
			WriteCurrentVersionInfoToFile()
			WriteWaitVersionInfoToFile()
			break
		} else {
			time.Sleep(time.Duration(10) * time.Second)
		}
	}
	return err
}

func CmdInstsDownload() {
	for {
		chs := make([]chan error, len(Dict.Instances))
		keyAndRespSlice := make([]dict.CubeAgentResponse, len(Dict.Instances))
		for i, inst := range Dict.Instances {
			if inst.Status != dict.Instance_Status_Download_Succ {
				chs[i] = make(chan error)
				var json_params dict.CubeAgentRequest
				json_params.Command = dict.DOWNLOAD
				json_params.DictName = inst.DictName
				json_params.DeployPath = inst.DeployPath
				json_params.Version = strconv.Itoa(Dict.WaitVersionInfo.Version)
				json_params.Depend = strconv.Itoa(Dict.WaitVersionInfo.Depend)
				json_params.Id = strconv.Itoa(Dict.WaitVersionInfo.Id)
				json_params.Key = strconv.Itoa(Dict.WaitVersionInfo.Key)
				json_params.Mode = Dict.WaitVersionInfo.Mode
				json_params.ShardSeq = inst.Shard
				json_params.Port = strconv.Itoa(inst.Port)
75
				json_params.Source = dict.GetFileHead(Dict.DownloadMode, TransferAddr, Dict.WgetPort) + Dict.WaitVersionInfo.Output + "/" + json_params.DictName + "_part" + strconv.Itoa(inst.Shard) + ".tar"
D
dangyifei 已提交
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
				var address = fmt.Sprintf("http://%v:%v/agent/cmd", inst.AgentIp, inst.AgentPort)
				logex.Noticef("[download cmd]%v:%v", address, json_params)
				go nonBlockSendJsonReq("POST2", address, 120, &json_params, &keyAndRespSlice[i], chs[i])
				Dict.Instances[i].DownloadStartTime = int(time.Now().Unix())
				Dict.Instances[i].Mode = Dict.WaitVersionInfo.Mode
			}
		}
		for i, inst := range Dict.Instances {
			err := <-chs[i]
			logex.Noticef("[instance resp]download:%v", Dict.Instances)
			if err != nil || keyAndRespSlice[i].Success != "0" {
				logex.Warningf("cmd cube online downlaod of %v:%v, shard:%v failed", inst.AgentIp, inst.AgentPort, inst.Shard)
				continue
			}
			if inst.Status < dict.Instance_Status_Download_Succ {
				Dict.Instances[i].Status = dict.Instance_Status_Download_Succ
				Dict.Instances[i].DownloadedTime = int(time.Now().Unix())
				Dict.DownloadSuccInsts++
			}
		}
		if Dict.DownloadSuccInsts == Dict.InstancesNum {
			Dict.WaitVersionInfo.Status = dict.Dict_Status_Download_Succ
			fmt.Printf("[all download ok]inst :%v\n", Dict.Instances)
			logex.Noticef("[all download ok]inst :%v", Dict.Instances)
			break
		}
		time.Sleep(5 * time.Second)
	}
}

func CmdInstsReload() {
	for {
		chs := make([]chan error, len(Dict.Instances))
		keyAndRespSlice := make([]dict.CubeAgentResponse, len(Dict.Instances))
		for i, inst := range Dict.Instances {
			if inst.Status != dict.Instance_Status_Reload_Succ {
				chs[i] = make(chan error)
				var json_params dict.CubeAgentRequest
				json_params.Command = dict.RELOAD
				json_params.DictName = inst.DictName
				json_params.DeployPath = inst.DeployPath
				json_params.Version = strconv.Itoa(Dict.WaitVersionInfo.Version)
				json_params.Depend = strconv.Itoa(Dict.WaitVersionInfo.Depend)
				json_params.Id = strconv.Itoa(Dict.WaitVersionInfo.Id)
				json_params.Key = strconv.Itoa(Dict.WaitVersionInfo.Key)
				json_params.Mode = Dict.WaitVersionInfo.Mode
				json_params.ShardSeq = inst.Shard
				json_params.Port = strconv.Itoa(inst.Port)
124
				json_params.Source = dict.GetFileHead(Dict.DownloadMode, TransferAddr, Dict.WgetPort) + Dict.WaitVersionInfo.Output + "/" + json_params.DictName + "_part" + strconv.Itoa(inst.Shard) + ".tar"
D
dangyifei 已提交
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

				var address = fmt.Sprintf("http://%v:%v/agent/cmd", inst.AgentIp, inst.AgentPort)
				logex.Noticef("[reload cmd]%v:%v", address, json_params)
				go nonBlockSendJsonReq("POST2", address, 120, &json_params, &keyAndRespSlice[i], chs[i])
				Dict.Instances[i].ReloadStartTime = int(time.Now().Unix())
			}
		}
		for i, inst := range Dict.Instances {
			err := <-chs[i]
			logex.Noticef("[instance resp]reload:%v", Dict.Instances)
			if err != nil || keyAndRespSlice[i].Success != "0" {
				logex.Warningf("cmd cube online reload of %v:%v, shard:%v failed", inst.AgentIp, inst.AgentPort, inst.Shard)
				continue
			}
			if inst.Status < dict.Instance_Status_Reload_Succ {
				Dict.Instances[i].Status = dict.Instance_Status_Reload_Succ
				Dict.Instances[i].ReloadedTime = int(time.Now().Unix())
				Dict.ReloadSuccInsts++
			}
		}
		if Dict.ReloadSuccInsts == Dict.InstancesNum {
			Dict.WaitVersionInfo.Status = dict.Dict_Status_Reload_Succ
			fmt.Printf("[all reload ok]inst:%v\n", Dict.Instances)
			logex.Noticef("[all reload ok]inst :%v", Dict.Instances)
			break
		}
		time.Sleep(5 * time.Second)
	}
}

func CmdInstsEnable() {
	for {
		chs := make([]chan error, len(Dict.Instances))
		keyAndRespSlice := make([]dict.CubeAgentResponse, len(Dict.Instances))
		for i, inst := range Dict.Instances {
			if inst.Status != dict.Instance_Status_Enable_Succ {
				chs[i] = make(chan error)
				var json_params dict.CubeAgentRequest
				json_params.Command = dict.ENABLE
				json_params.DictName = inst.DictName
				json_params.DeployPath = inst.DeployPath
				json_params.Version = strconv.Itoa(Dict.WaitVersionInfo.Version)
				json_params.Depend = strconv.Itoa(Dict.WaitVersionInfo.Depend)
				json_params.Id = strconv.Itoa(Dict.WaitVersionInfo.Id)
				json_params.Key = strconv.Itoa(Dict.WaitVersionInfo.Key)
				json_params.Mode = Dict.WaitVersionInfo.Mode
				json_params.ShardSeq = inst.Shard
				json_params.Port = strconv.Itoa(inst.Port)
173
				json_params.Source = dict.GetFileHead(Dict.DownloadMode, TransferAddr, Dict.WgetPort) + Dict.WaitVersionInfo.Output + "/" + json_params.DictName + "_part" + strconv.Itoa(inst.Shard) + ".tar"
D
dangyifei 已提交
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

				var address = fmt.Sprintf("http://%v:%v/agent/cmd", inst.AgentIp, inst.AgentPort)
				logex.Noticef("[enable cmd]%v:%v", address, json_params)
				go nonBlockSendJsonReq("POST2", address, 120, &json_params, &keyAndRespSlice[i], chs[i])
				Dict.Instances[i].EnablStartTime = int(time.Now().Unix())
			}
		}
		for i, inst := range Dict.Instances {
			err := <-chs[i]
			logex.Noticef("[instance resp]enable:%v", Dict.Instances)
			if err != nil || keyAndRespSlice[i].Success != "0" {
				logex.Warningf("cmd cube online enable of %v:%v, shard:%v failed", inst.AgentIp, inst.AgentPort, inst.Shard)
				continue
			}
			if inst.Status < dict.Instance_Status_Enable_Succ {
				Dict.Instances[i].Status = dict.Instance_Status_Enable_Succ
				Dict.Instances[i].EnabledTime = int(time.Now().Unix())
				Dict.EnableSuccInsts++
			}
		}
		if Dict.EnableSuccInsts == Dict.InstancesNum {
			Dict.WaitVersionInfo.Status = dict.Dict_Status_Finished
			fmt.Printf("[all enable ok]inst :%v\n", Dict.Instances)
			logex.Noticef("[all enable ok]inst :%v", Dict.Instances)

			break
		}
		time.Sleep(5 * time.Second)
	}
}