Esempio n. 1
0
func InitSlotAlloctionWithJson(data string) (*SlotAllocation, error) {
	var slots SlotAllocation
	err := utils.JsonParse(data, &slots)
	if err != nil {
		return nil, err
	}
	return &slots, nil
}
Esempio n. 2
0
/**
 * get nodeinfo from specify path
 */
func (self *NodeInfoMaps) getNodeInfoFromZk(path string) (*NodeInfo, error) {
	var nodeInfo NodeInfo
	bytes, _, err := self.zk.GetZkConn().Get(path)
	if err != nil {
		log.Errorf("Can not read the subpath of /yundis/nodes, err:%s", err)
		return nil, err
	}
	err = utils.JsonParse(string(bytes), &nodeInfo)
	if err != nil {
		log.Errorf("Can not parse data from node %s, err: %s", path, err)
		return nil, err
	}
	return &nodeInfo, nil
}
Esempio n. 3
0
/**
 * load the slot's info to map.
 */
func (self *SlotInfoMaps) GetSlotInfoMapFromZk() map[string]*SlotInfo {
	log.Info("Read the slot's info from zk.")
	if !self.zk.PathExist("/yundis/ids") {
		_, err := self.zk.Create("/yundis/ids", []byte{}, 0, zk.WorldACL(zk.PermAll))
		log.Errorf("can not create path %s, err: %s", "/yundis/ids", err)
	}
	infoMap := make(map[string]*SlotInfo)
	for i := 0; i < self.slotCount; i++ {
		strI := strconv.Itoa(i)
		bytes, _, err := self.zk.Get("/yundis/ids/" + strI)
		if err != nil || len(bytes) == 0 {
			slotInfo := &SlotInfo{
				SlotId: strI,
				State:  SlotStateNormal,
			}
			infoMap[strI] = slotInfo
			dataStr, err := utils.ToJson(slotInfo)
			if err != nil {
				log.Errorf("Can not convert %s to json. err:%s", slotInfo, err)
				continue
			}
			self.zk.Create("/yundis/ids/"+strI, []byte(dataStr), 0, zk.WorldACL(zk.PermAll))
		} else {
			log.Infof("Read the data form path %s", "/yundis/ids/"+strI)
			var slotInfo SlotInfo
			err = utils.JsonParse(string(bytes), &slotInfo)
			if err != nil {
				log.Errorf("Can not parse data from node %s, err: %s", "/yundis/ids/"+strI, err)
				continue
			} else {
				infoMap[strI] = &slotInfo
			}
		}
	}
	return infoMap
}