Example #1
0
func (s *Server) parseSchemas() error {
	r, err := router.NewRouter(s.cfg)
	if err != nil {
		return err
	}

	s.schemas = make(map[string]*Schema)

	for _, v := range s.cfg.Schemas {
		if _, ok := s.schemas[v.DB]; ok {
			return fmt.Errorf("duplicate schema %s", v.DB)
		}

		if len(v.Nodes) == 0 {
			return fmt.Errorf("schema %s must have a node", v.DB)
		}

		nodes := make(map[string]*Node)
		for _, n := range v.Nodes {
			if s.getNode(n) == nil {
				return fmt.Errorf("schema %s node %s is not exists", v.DB, n)
			}

			if _, ok := nodes[n]; ok {
				return fmt.Errorf("schema %s node %s duplicate", v.DB, n)
			}

			nodes[n] = s.getNode(n)
		}

		dbRules := r.GetDBRules(v.DB)
		if dbRules == nil {
			if len(v.Nodes) != 1 {
				return fmt.Errorf("schema %s must be set a rule for multi nodes %v", v.DB, v.Nodes)
			} else {
				dbRules = router.NewDefaultDBRules(v.DB, v.Nodes[0])
			}
		}

		for _, v := range dbRules.Rules {
			for _, n := range v.Nodes {
				if s.getNode(n) == nil {
					return fmt.Errorf("rule %s node %s is not exists", v, n)
				}
			}
		}

		if s.getNode(dbRules.DefaultRule.Nodes[0]) == nil {
			return fmt.Errorf("rule %s node %s is not exists", v, dbRules.DefaultRule.Nodes[0])
		}

		s.schemas[v.DB] = &Schema{
			db:    v.DB,
			nodes: nodes,
			rule:  dbRules,
		}
	}

	return nil
}
Example #2
0
func newTestDBRule() *router.DBRules {
	var s = `
rules:
-
    db: mixer
    table: test1
    key: id
    type: hash
    nodes: node(1-10)

-
    db: mixer
    table: test2 
    key: id
    nodes: node1,node2,node3    
    type: range
    # range is -inf-10000 10000-20000 20000-+inf 
    range: -10000-20000-

-   db: mixer
    table: 
    key:
    nodes: node1
    type: default
`

	cfg, err := config.ParseConfigData([]byte(s))
	if err != nil {
		println(err.Error())
		panic(err)
	}

	var r *router.Router

	r, err = router.NewRouter(cfg)
	if err != nil {
		println(err.Error())
		panic(err)
	}

	return r.GetDBRules("mixer")
}
Example #3
0
func newTestDBRule() *router.Router {
	var s = `
schemas :
-
  db : mixer 
  nodes: [node1,node2,node3,node4,node5,node6,node7,node8,node9,node10]
  rules:
    default: node1
    shard:
      -   
        table: test1
        key: id
        nodes: [node1,node2,node3,node4,node5,node6,node7,node8,node9,node10]
        type: hash

      -   
        table: test2
        key: id
        type: range
        nodes: [node1,node2,node3]
        range: -10000-20000-
`

	cfg, err := config.ParseConfigData([]byte(s))
	if err != nil {
		println(err.Error())
		panic(err)
	}

	var r *router.Router

	r, err = router.NewRouter(&cfg.Schemas[0])
	if err != nil {
		println(err.Error())
		panic(err)
	}

	return r
}
Example #4
0
func (s *Server) parseSchemas() error {
	s.schemas = make(map[string]*Schema)

	for _, schemaCfg := range s.cfg.Schemas {
		if _, ok := s.schemas[schemaCfg.DB]; ok {
			return fmt.Errorf("duplicate schema [%s].", schemaCfg.DB)
		}
		if len(schemaCfg.Nodes) == 0 {
			return fmt.Errorf("schema [%s] must have a node.", schemaCfg.DB)
		}

		nodes := make(map[string]*Node)
		for _, n := range schemaCfg.Nodes {
			if s.getNode(n) == nil {
				return fmt.Errorf("schema [%s] node [%s] config is not exists.", schemaCfg.DB, n)
			}

			if _, ok := nodes[n]; ok {
				return fmt.Errorf("schema [%s] node [%s] duplicate.", schemaCfg.DB, n)
			}

			nodes[n] = s.getNode(n)
		}

		rule, err := router.NewRouter(&schemaCfg)
		if err != nil {
			return err
		}

		s.schemas[schemaCfg.DB] = &Schema{
			db:    schemaCfg.DB,
			nodes: nodes,
			rule:  rule,
		}
	}

	return nil
}