Example #1
0
func ParseConfigData(data []byte) (*Config, error) {
	var cfg Config
	if err := yaml.Unmarshal([]byte(data), &cfg); err != nil {
		return nil, err
	}
	return &cfg, nil
}
Example #2
0
func TestParseRule(t *testing.T) {
	var s = `
schemas:
-
  db: kingshard
  nodes: [node1, node2, node3]
  rules:
    default: node1
    shard:      
     -
      table: test_shard_hash
      key: id
      nodes: [node2, node3]
      locations: [16,16]
      type: hash
     -
      table: test_shard_range
      key: id
      type: range
      nodes: [node2, node3]
      locations: [16,16]
      table_row_limit: 10000
`
	var cfg config.Config
	if err := yaml.Unmarshal([]byte(s), &cfg); err != nil {
		t.Fatal(err)
	}

	rt, err := NewRouter(&cfg.Schemas[0])
	if err != nil {
		t.Fatal(err)
	}
	if rt.DefaultRule.Nodes[0] != "node1" {
		t.Fatal("default rule parse not correct.")
	}

	hashRule := rt.GetRule("test_shard_hash")
	if hashRule.Type != HashRuleType {
		t.Fatal(hashRule.Type)
	}

	if len(hashRule.Nodes) != 2 || hashRule.Nodes[0] != "node2" || hashRule.Nodes[1] != "node3" {
		t.Fatal("parse nodes not correct.")
	}

	if n, _ := hashRule.FindNode(uint64(11)); n != "node2" {
		t.Fatal(n)
	}

	rangeRule := rt.GetRule("test_shard_range")
	if rangeRule.Type != RangeRuleType {
		t.Fatal(rangeRule.Type)
	}

	if n, _ := rangeRule.FindNode(10000 - 1); n != "node2" {
		t.Fatal(n)
	}

	defaultRule := rt.GetRule("defaultRule_table")
	if defaultRule == nil {
		t.Fatal("must not nil")
	}

	if defaultRule.Type != DefaultRuleType {
		t.Fatal(defaultRule.Type)
	}

	if defaultRule.Shard == nil {
		t.Fatal("nil error")
	}

	if n, _ := defaultRule.FindNode(11); n != "node1" {
		t.Fatal(n)
	}
}
Example #3
0
//TODO YYYY-MM-DD HH:MM:SS,YYYY-MM-DD test
func TestParseDateRule(t *testing.T) {
	var s = `
schema:
  db: kingshard
  nodes: [node1, node2, node3]
  default: node1
  shard:
    -
      table: test_shard_year
      key: date
      nodes: [node2, node3]
      date_range: [2012-2015,2016-2018]
      type: date_year
    -
      table: test_shard_month
      key: date
      type: date_month
      nodes: [node2, node3]
      date_range: [201512-201603,201604-201608]
    -
      table: test_shard_day
      key: date
      type: date_day
      nodes: [node2, node3]
      date_range: [20151201-20160122,20160202-20160308]
`
	var cfg config.Config
	if err := yaml.Unmarshal([]byte(s), &cfg); err != nil {
		t.Fatal(err)
	}

	rt, err := NewRouter(&cfg.Schema)
	if err != nil {
		t.Fatal(err)
	}
	if rt.DefaultRule.Nodes[0] != "node1" {
		t.Fatal("default rule parse not correct.")
	}

	yearRule := rt.GetRule("test_shard_year")
	if yearRule.Type != DateYearRuleType {
		t.Fatal(yearRule.Type)
	}

	if len(yearRule.Nodes) != 2 || yearRule.Nodes[0] != "node2" || yearRule.Nodes[1] != "node3" {
		t.Fatal("parse nodes not correct.")
	}

	if n, _ := yearRule.FindNode(1457082679); n != "node3" {
		t.Fatal(n)
	}

	monthRule := rt.GetRule("test_shard_month")
	if monthRule.Type != DateMonthRuleType {
		t.Fatal(monthRule.Type)
	}

	if n, _ := monthRule.FindNode(1457082679); n != "node2" {
		t.Fatal(n)
	}

	dayRule := rt.GetRule("test_shard_day")
	if dayRule.Type != DateDayRuleType {
		t.Fatal(monthRule.Type)
	}

	if n, _ := dayRule.FindNode(1457082679); n != "node3" {
		t.Fatal(n)
	}

}