Exemple #1
0
func NewNsqElastic(config *models.ConfigModel) *NsqElastic {
	topicChan2NodeItemMap := models.NewTable()
	topic2TopicItemMap := models.NewTable()
	nodeItem2NodeMap := models.NewTable()
	nodeSourceList := make([]models.NodeState, 0)
	c := controllers.NewNsqController(config, topicChan2NodeItemMap, topic2TopicItemMap, nodeItem2NodeMap, nodeSourceList)
	n := &NsqElastic{
		Config:                config,
		HttpController:        c,
		TopicChan2NodeItemMap: topicChan2NodeItemMap,
		Topic2TopicItemMap:    topic2TopicItemMap,
		NodeItem2NodeMap:      nodeItem2NodeMap,
		NodeSourceList:        nodeSourceList,
	}

	for i := 0; i < len(config.NodeList); i++ {
		err :=
			n.addSourceByString(config.NodeList[i])
		if err != nil {
			n.logf(err.Error() + "\n")
		}
	}
	n.logf("New NsqElastic")
	return n
}
Exemple #2
0
func (n *NsqElastic) lookupUpdate() error {
	//todo : update sourceList
	u, _ := url.Parse(fmt.Sprintf("http://%s/nodes", n.Config.LookupdAddr))
	res, err := http.Get(u.String())
	if err != nil {
		return err
	}
	result, err := ioutil.ReadAll(res.Body)
	res.Body.Close()
	if err != nil {
		return err
	}
	var nodes models.NodeInfo_lookup
	//nodes.Data.Producers = make([]models.Node_lookup, 0)

	if err = json.Unmarshal(result, &nodes); err != nil {
		return err
	}
	if nodes.Status_code != 200 {
		return errors.New(nodes.Status_txt)
	}
	TopicChan2NodeItemMap := models.NewTable()
	Topic2TopicItemMap := models.NewTable()
	NodeItem2NodeMap := models.NewTable()
	//itemsList := make(models.NodeItem, 0)
	var WaitGroup util.WaitGroupWrapper
	for i := 0; i < len(nodes.Data.Producers); i++ {
		var item models.NodeItem
		item.Httpport = nodes.Data.Producers[i].HTTPPort
		item.Tcpport = nodes.Data.Producers[i].TCPPort
		item.Ip = nodes.Data.Producers[i].RemoteAddress
		if strings.Contains(item.Ip, ":") {
			item.Ip = strings.Split(item.Ip, ":")[0]
		}
		find := false
		del := false
		for loopi := 0; loopi < len(n.NodeSourceList); loopi++ {
			if n.NodeSourceList[loopi].NodeItem.Eq(item) {
				find = true

				if n.NodeSourceList[loopi].Stats == models.DEL { //delete
					del = true
					break
				} else {
					n.NodeSourceList[loopi].Stats = models.USING
				}
			}

		}
		if !find {
			nodeState := models.NodeState{
				NodeItem: item,
				Stats:    models.USING,
			}
			n.NodeSourceList = append(n.NodeSourceList, nodeState)
		}
		if del {
			continue
		}
		//itemsList = append(models.NodeItem, item)
		WaitGroup.Wrap(func() {
			n.nsqdUpdate(TopicChan2NodeItemMap, Topic2TopicItemMap, NodeItem2NodeMap, item)
		})
	}
	WaitGroup.Wait()
	n.NodeItem2NodeMap.Replace(NodeItem2NodeMap)
	n.Topic2TopicItemMap.Replace(Topic2TopicItemMap)
	n.TopicChan2NodeItemMap.Replace(TopicChan2NodeItemMap)
	//fmt.Printf("new %v \n", TopicChan2NodeItemMap)
	return nil
}