Esempio n. 1
0
func (this *electServer) syncCluster() (err error) {
	b, err := json.Marshal(this.peerConfig)
	if err != nil {
		return
	}

	var peers = make(PeerConfigMap)
	for name, peer := range this.raftServer.Peers() {
		resp, err := http.Post(peer.ConnectionString+"/peer_exchange", "application/json", bytes.NewBuffer(b))
		if err != nil {
			log.Errorf("post faild : %s", err.Error())
			continue
		}
		defer resp.Body.Close()
		if resp.StatusCode != 200 {
			log.Errorf("%s : resp code not 200", peer.ConnectionString)
			continue
		}
		var peerConfig = new(PeerConfig)
		if err = json.NewDecoder(resp.Body).Decode(peerConfig); err != nil {
			log.Errorf("decode response from %s faild : %s", peer.ConnectionString, err.Error())
			continue
		}
		peers[name] = peerConfig
	}
	peers[this.Name] = this.peerConfig

	this.peers = peers
	return
}
Esempio n. 2
0
func (this *electServer) Router(w http.ResponseWriter, r *http.Request) {
	var err error
	switch r.URL.Path {
	case "/join":
		err = this.doJoin(r)
	case "/peers":
		err = json.NewEncoder(w).Encode(this.doPeers())
	case "/leader":
		err = json.NewEncoder(w).Encode(this.doLeader())
	case "/peer_config":
		err = json.NewEncoder(w).Encode(this.doPeerConfig())
	case "/peer_exchange":
		err = this.doExchangePeerConfig(r, w)
	case "/stat":
		err = this.doStat(w)
	default:
		resp := web.ApiResponse{404, "NOT_FOUND", nil}
		resp.Write(w)
	}
	if err != nil {
		resp := web.ApiResponse{502, err.Error(), nil}
		resp.Write(w)
		log.Errorf("%s handel faild : %s", r.URL.Path, err.Error())
	}
}
Esempio n. 3
0
func NewHttpServe(conf Config, leader string) (err error) {
	var server = new(httpServer)

	// 初始化daemon配置
	if err = conf.DaemonBase.InitAll(); err != nil {
		log.Noticef("new http serve daemon base init faild: %+v ---  %s\n ", conf.DaemonBase, err.Error())
		return
	}
	server.DaemonBase = conf.DaemonBase

	// 初始化collie配置
	server.ctx = NewContex()
	if err = server.ctx.LoadConfig(conf.ContexConfig); err != nil {
		log.Errorf("new http serve contex init faild: %+v\n", conf)
		return
	}

	// 初始化http配置
	if err = conf.Http.Check(); err != nil {
		log.Noticef("new http serve net base init faild: %+v ---  %s\n ", conf, err.Error())
		return
	}
	server.NetBase = conf.Http

	if server.elect, err = NewElectServer(conf, leader); err != nil {
		log.Error("new elect server faild : ", err.Error())
		return
	}

	// 持久化配置
	if err = server.Persistence(); err != nil {
		log.Errorf("persistence faild : %s", err.Error())
		return
	}

	// 启动http服务
	err = conf.Http.StartHttp(server)
	return
}
Esempio n. 4
0
func (this *Collied) LoadConfig(conf *ContexConfig) (err error) {
	conf.Expand()
	for name, processConf := range conf.Process {
		sheep := process.NewSheep()
		if err = sheep.Create(processConf); err != nil {
			log.Errorf("load sheep : %+v --- %s", name, err.Error())
			return
		}
		// log.Logf("load sheep : %+v", processConf)
		this.sheeps[name] = sheep
	}
	this.conf.ProcessModel = conf.ProcessModel
	this.conf.ConfigPath = conf.ConfigPath
	return
}
Esempio n. 5
0
func (this *Sheep) Start() (err error) {
	log.Logf("sheep start : %+v", this.Name)
	if err = this.Config.InitAll(); err != nil {
		return
	}

	this.Status = constant.STATUS_STARTING

	err = this.cmd.Start()
	if err != nil {
		this.Status = constant.STATUS_START_FAILED
		log.Errorf("start faild:%s:%+v", err.Error(), *this)
		return err
	}

	log.Logf("sheep %s started", this.Name)
	go this.watch()
	this.Status = constant.STATUS_STARTED
	time.Sleep(time.Second)
	return nil
}
Esempio n. 6
0
func (this *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	peers := this.elect.doPeers()

	// get peers
	var ret = web.ApiResponse{200, "ok", nil}
	var collies = make(PeerConfigMap)
	if len(r.URL.Query()["collie"]) == 0 {
		collies = peers
	} else {
		for _, collie := range r.URL.Query()["collie"] {
			if peer, ok := peers[collie]; ok {
				collies[collie] = peer
			} else {
				ret.Set(collie, web.ApiResponse{500, "collie unvalaible : " + collie, nil})
			}
		}
	}

	for name, peer := range collies {
		if name == this.Name {
			ret.Set(name, this.router(r))
		} else {
			query := r.URL.Query()
			query["collie"] = []string{name}
			resps := this.proxy(fmt.Sprintf("http://%s%s?%s", peer.HttpAddr, r.URL.Path, query.Encode()), r.Body)
			if _, ok := resps.Data.(map[string]interface{}); ok {
				for name, resp := range resps.Data.(map[string]interface{}) {
					ret.Set(name, resp)
				}
			} else {
				log.Errorf("bad format : %+v", reflect.TypeOf(resps.Data))
			}
		}

	}
	ret.Write(w)
}