コード例 #1
0
ファイル: slotallocation.go プロジェクト: yuwnloyblog/yundis
/**
 * When allocation changed, modify the state of slot and update slotmaps.
 */
func HandleAllocationChange(oldAllocations, newAllocations *SlotAllocation, slotinfoMaps *SlotInfoMaps, zkHelper *utils.ZkHelper) {
	isChanged := false
	newSlotInfoMap := make(map[string]*SlotInfo) //create a new slotinfo maps
	for i := 0; i < oldAllocations.SlotCount; i++ {
		oldNodeId := oldAllocations.Allocations[strconv.Itoa(i)]
		newNodeId := newAllocations.Allocations[strconv.Itoa(i)]
		newSlotInfoMap[strconv.Itoa(i)] = slotinfoMaps.GetSlotInfoMap()[strconv.Itoa(i)].Clone()
		if oldNodeId != newNodeId {
			isChanged = true
			log.Infof("The slot %d's node changed to %d from %d.", i, newNodeId, oldNodeId)
			newSlotInfoMap[strconv.Itoa(i)].MigrateState = MigStateMigrating
			newSlotInfoMap[strconv.Itoa(i)].NodeId = strconv.Itoa(newNodeId)
			newSlotInfoMap[strconv.Itoa(i)].SrcNodeId = strconv.Itoa(oldNodeId)
			newSlotInfoMap[strconv.Itoa(i)].TargetNodeId = strconv.Itoa(newNodeId)
			//update the new slotinfo to zk
			jsonStr, err := utils.ToJson(newSlotInfoMap[strconv.Itoa(i)])
			if err != nil {
				log.Errorf("Can not convert to json string from obj [%s]", newSlotInfoMap[strconv.Itoa(i)])
			} else {
				_, err = zkHelper.CoverCreate("/yundis/ids/"+strconv.Itoa(i), []byte(jsonStr), 0, zk.WorldACL(zk.PermAll))
				if err != nil {
					log.Errorf("Change the value of /yundis/ids/%d fail, err:%s.", i, err)
				}
				//zkHelper.Set("/yundis/ids/"+strconv.Itoa(i), []byte(jsonStr), 1)
			}
		}
	}
	if isChanged {
		log.Info("Update the slotinfoMaps.")
		//slotinfoMaps.SetSlotInfoMap(infoMap)
		slotinfoMaps.SetSlotInfoMap(newSlotInfoMap)
	}
}
コード例 #2
0
ファイル: slotallocation.go プロジェクト: yuwnloyblog/yundis
func SyncToZk(alloc *SlotAllocation, zkHelper *utils.ZkHelper) error {
	allocationsPath := "/yundis/allocations"
	bytes, err := alloc.ToNodeData()
	if err == nil {
		_, err = zkHelper.Set(allocationsPath, bytes)
		return err
	}
	return err
}
コード例 #3
0
ファイル: slotallocation.go プロジェクト: yuwnloyblog/yundis
func HandleSlotAllocations(zkHelper *utils.ZkHelper, slotCount int, nodeId int) *SlotAllocation {
	allocationsPath := "/yundis/allocations"
	allocations := GetSlotAllocationsFromZk(zkHelper)
	if allocations != nil { //init from zk
		log.Info("Init slot allocation from zk.")
		//return allocations
	} else { //init the cluster, and save to zk
		allocations = CreateSlotAlloction(slotCount, nodeId)
		//self.SetAllocations(slotAllocations)
		bytes, err := allocations.ToNodeData()
		if err != nil {
			log.Errorf("Can convert slotallocation to json str. err : %s", err)
		}
		//create this path, and initial the data into zk
		_, err = zkHelper.GetZkConn().Create(allocationsPath, bytes, 0, zk.WorldACL(zk.PermAll))
		if err != nil {
			log.Errorf("Can not write allocations to zk. err : %s", err)
		}
	}
	return allocations
	/*
		log.Info("Watching the change of /yundis/allocations.")
		_, _, ch, err := zkHelper.GetZkConn().GetW("/yundis/allocations")
		if err != nil {
			log.Errorf("Can not watch path /yundis/allocations, err:%s", err)
			return err
		}
		go func() {
			for {
				event := <-ch
				log.Infof("The value of /yundis/allocations changed. %+v", event)
				values, _, ch1, err1 := zkHelper.GetZkConn().GetW("/yundis/allocations")
				if err1 == nil {
					ch = ch1
					log.Infof("The value of /yundis/allocations:%s", string(values))
					//handle the new value of /yundis/allocations
					newAllocations, err := InitSlotAlloctionWithData(string(values))
					if err != nil {
						log.Errorf("Can not init Allocation by data from zk. err: %s", err)
					} else {
						HandleAllocationChange(self.allocations, newAllocations, self.slotinfoMaps, self.zkHelper)
						log.Info("Update the allocations.")
						self.SetAllocations(newAllocations)
					}
				} else {
					log.Errorf("Can not watching the value of /yundis/allocations, err:%s", err1)
					break
				}
				time.Sleep(time.Second)
			}
		}()*/
}
コード例 #4
0
ファイル: slotallocation.go プロジェクト: yuwnloyblog/yundis
/**
 * init slot allocation from zk.
 */
func GetSlotAllocationsFromZk(zkHelper *utils.ZkHelper) *SlotAllocation {
	allocationsPath := "/yundis/allocations"
	if b := zkHelper.PathExist(allocationsPath); b {
		bytes, _, err := zkHelper.GetZkConn().Get(allocationsPath)
		if err != nil {
			log.Errorf("Read the data of '/yundis/allocations' error. err : %s", err)
		}
		allocations, err := InitSlotAlloctionWithJson(string(bytes))
		if err != nil {
			log.Errorf("Parse the json data error. err:%s", err)
		} else {
			return allocations
		}
	}
	return nil
}