Exemplo n.º 1
0
// group:
//  /zk/myshard/groups/group_000001
//  /zk/myshard/groups/group_000002
func (t *Topo) CreateGroup(groupID int) (string, error) {
	group := model.Group{groupID, model.GroupUpStatus, nil}

	zkhelper.CreateRecursive(t.conn, t.GetGroupBasePath(), "", 0, zkhelper.DefaultDirACLs())

	data, _ := json.Marshal(group)

	return t.conn.Create(t.GetGroupPath(groupID), data, 0, zkhelper.DefaultDirACLs())
}
Exemplo n.º 2
0
func (t *Topo) CreateProxy(p *model.Proxy) (string, error) {
	data, err := json.Marshal(p)
	if err != nil {
		return "", err
	}

	base := t.GetProxyBasePath()

	zkhelper.CreateRecursive(t.conn, base, "", 0, zkhelper.DefaultDirACLs())

	return t.conn.Create(path.Join(base, p.Addr), data, zk.FlagEphemeral, zkhelper.DefaultDirACLs())
}
Exemplo n.º 3
0
func (t *Topo) CreateSchema(db string, groupIDs []int, defaultGroupID int) (string, error) {
	s := model.Schema{Name: db, GroupIDs: groupIDs, DefaultGroupID: defaultGroupID, Status: ""}

	sort.Ints(groupIDs)

	if !checkGroupIDExist(defaultGroupID, groupIDs) {
		return "", fmt.Errorf("group %d not in schema groups", defaultGroupID)
	}

	zkhelper.CreateRecursive(t.conn, t.GetSchemaBasePath(), "", 0, zkhelper.DefaultDirACLs())

	data, _ := json.Marshal(s)

	return t.conn.Create(t.GetSchemaPath(db), data, 0, zkhelper.DefaultDirACLs())
}
Exemplo n.º 4
0
func (t *Topo) AddGroupNode(groupID int, addr string, tp string, status string, weight int) (string, error) {
	node := model.Node{GroupID: groupID, Addr: addr, Type: tp, Status: status, Weight: weight}

	data, _ := json.Marshal(node)

	return t.conn.Create(t.GetGroupNodePath(groupID, addr), data, 0, zkhelper.DefaultDirACLs())
}
Exemplo n.º 5
0
func newZk(cfg *Config, fsm *masterFSM) (Cluster, error) {
	z := new(Zk)

	var err error

	if !strings.HasPrefix(cfg.Zk.BaseDir, "/zk") {
		return nil, fmt.Errorf("invalid zk base dir %s, must have prefix /zk", cfg.Zk.BaseDir)
	}

	addr := strings.Join(cfg.Zk.Addr, ",")
	if addr == "memory" {
		// only for test
		log.Infof("only for test, use memory")
		z.conn = zkhelper.NewConn()
	} else {
		z.conn, err = zkhelper.ConnectToZk(addr)
	}

	if err != nil {
		return nil, err
	}

	z.c = cfg
	z.fsm = fsm
	z.isLeader.Set(false)
	z.leaderCh = make(chan bool, 1)
	z.actionCh = make(chan *zkAction, 10)

	z.quit = make(chan struct{})

	if _, err = zkhelper.CreateOrUpdate(z.conn, cfg.Zk.BaseDir, "", 0, zkhelper.DefaultDirACLs(), true); err != nil {
		log.Errorf("create %s error: %v", cfg.Zk.BaseDir, err)
		return nil, err
	}

	onRetryLock := func() {
		z.noticeLeaderCh(false)
	}

	z.elector = createElection(z.conn, cfg.Zk.BaseDir, cfg.Addr, onRetryLock)

	z.checkLeader()

	return z, nil
}
Exemplo n.º 6
0
func (t *Topo) CreateAgent(nodeAddr string) (string, error) {
	zkhelper.CreateRecursive(t.conn, t.GetAgentBasePath(), "", 0, zkhelper.DefaultDirACLs())

	return t.conn.Create(path.Join(t.GetAgentBasePath(), nodeAddr), nil, zk.FlagEphemeral, zkhelper.DefaultFileACLs())
}