Esempio n. 1
0
func (ctx *ClusterContext) splitDescription() (map[string]dcluster.ContainerDescription, []string, []string) {
	gDeps := dcontainer.NewContainerGroupDeps()
	descriptions := map[string]dcluster.ContainerDescription{}
	sdGroups := map[string]bool{}

	sdNameGroupMap := map[string]string{}
	for sdName, sdd := range ctx.clusterDesc.ServiceDiscover {
		if group, ok := sdd["container"]; ok {
			sdNameGroupMap[sdName] = group
			sdGroups[group] = true
		} else {
			fmt.Printf("Service discover container missed. discovery name:%s.\n", sdName)
		}
	}

	fmt.Printf("Service discover container group:%+v\n", sdGroups)

	for _, description := range ctx.clusterDesc.Container.Topology {
		group := description.Group
		descriptions[group] = description
		fmt.Printf("Description: group:%s, description:%+v, total:%+v\n", group, description, descriptions)
		// add deps
		fmt.Printf("Add dependancy for group: group:%s, deps:%+v\n", group, description.Deps)

		gDeps.AddDeps(group, description.Deps)

		if description.ServiceDiscover != "" {
			if depGroup, ok := sdNameGroupMap[description.ServiceDiscover]; ok {
				fmt.Printf("Add dependancy. group:%s, deps:%+v\n", group, depGroup)
				gDeps.AddDeps(group, []string{depGroup})
			}
		}
	}

	fmt.Printf("Total description:%+v\n", descriptions)

	allGroups := gDeps.List()
	sdDGroups := []string{}
	bizDGroups := []string{}
	for _, group := range allGroups {
		if _, ok := sdGroups[group]; ok {
			sdDGroups = append(sdDGroups, group)
		} else {
			bizDGroups = append(bizDGroups, group)
		}
	}
	return descriptions, sdDGroups, bizDGroups
}
Esempio n. 2
0
// init the container description type
// init the container description dependancy level
func (ctx *ClusterContext) initContainerDescription() error {
	// handle service discovery related container description
	log.Debugf("Initing service discovery container description.")
	sdDeps := dcontainer.NewContainerGroupDeps()
	for name, sdd := range ctx.clusterDesc.ServiceDiscover {
		cgroup, _ := sdd["container"]
		description, exists := ctx.clusterDesc.Container.Topology.GetDescription(cgroup)
		if !exists {
			return fmt.Errorf("No container description found for service discovery. sd:%s", name)
		}
		if err := ctx.addContainerDescription(sdDeps, description); err != nil {
			return err
		}
	}
	sdVisit := func(lvl int, groups []string) error {
		for _, g := range groups {
			cd, exists := ctx.clusterDesc.Container.Topology.GetDescription(g)
			if !exists {
				return fmt.Errorf("No service discover container description found for group '%s'", g)
			}
			cd.Type = dcluster.ContainerDescription_TYPE_SD
			cd.DepLevel = lvl
			log.Debugf("init service discovery container description. group:%s. level:%d", g, lvl)
		}
		return nil
	}
	if err := sdDeps.VisitByLevel(sdVisit, false); err != nil {
		return err
	}
	log.Debugf("Service discovery container description inited.")

	bizDeps := dcontainer.NewContainerGroupDeps()
	log.Debugf("Initing biz container description.")
	for _, cd := range ctx.clusterDesc.Container.Topology {
		if err := ctx.addContainerDescription(bizDeps, &cd); err != nil {
			return err
		}
	}
	bizVisit := func(lvl int, groups []string) error {
		for _, g := range groups {
			cd, exists := ctx.clusterDesc.Container.Topology.GetDescription(g)
			if !exists {
				return fmt.Errorf("No biz container description found for group '%s'", g)
			}
			switch cd.Type {
			case dcluster.ContainerDescription_TYPE_SD:
				// do nothing
			default:
				cd.Type = dcluster.ContainerDescription_TYPE_BZ
				cd.DepLevel = lvl
				dlog.Debugf("init biz container description. group:%s. level:%d\n", g, lvl)
			}
		}
		return nil
	}
	if err := bizDeps.VisitByLevel(bizVisit, false); err != nil {
		return err
	}
	log.Debugf("biz container description inited. %+v", ctx.clusterDesc.Container.Topology)
	return nil
}