本文整理匯總了Golang中github.com/ngaut/zkhelper.Conn.Create方法的典型用法代碼示例。如果您正苦於以下問題:Golang Conn.Create方法的具體用法?Golang Conn.Create怎麽用?Golang Conn.Create使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/ngaut/zkhelper.Conn
的用法示例。
在下文中一共展示了Conn.Create方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: CreateProxyInfo
func CreateProxyInfo(zkConn zkhelper.Conn, productName string, pi *ProxyInfo) (string, error) {
data, err := json.Marshal(pi)
if err != nil {
return "", errors.Trace(err)
}
dir := GetProxyPath(productName)
zkhelper.CreateRecursive(zkConn, dir, "", 0, zkhelper.DefaultDirACLs())
return zkConn.Create(path.Join(dir, pi.Id), data, zk.FlagEphemeral, zkhelper.DefaultFileACLs())
}
示例2: saveTimestamp
func saveTimestamp(conn zkhelper.Conn, rootPath string, ts int64) error {
var buf [8]byte
binary.BigEndian.PutUint64(buf[:], uint64(ts))
tsPath := getTimestampPath(rootPath)
_, err := conn.Set(tsPath, buf[:], -1)
if zkhelper.ZkErrorEqual(err, zk.ErrNoNode) {
_, err = conn.Create(tsPath, buf[:], 0, zk.WorldACL(zkhelper.PERM_FILE))
}
return errors.Trace(err)
}
示例3: NewAction
func NewAction(zkConn zkhelper.Conn, productName string, actionType ActionType, target interface{}, desc string, needConfirm bool) error {
ts := strconv.FormatInt(time.Now().Unix(), 10)
action := &Action{
Type: actionType,
Desc: desc,
Target: target,
Ts: ts,
}
// set action receivers
proxies, err := ProxyList(zkConn, productName, func(p *ProxyInfo) bool {
return p.State == PROXY_STATE_ONLINE
})
if err != nil {
return errors.Trace(err)
}
for _, p := range proxies {
action.Receivers = append(action.Receivers, p.Id)
}
b, _ := json.Marshal(action)
prefix := GetWatchActionPath(productName)
err = CreateActionRootPath(zkConn, prefix)
if err != nil {
return errors.Trace(err)
}
// create action node
actionCreated, err := zkConn.Create(prefix+"/action_", b, int32(zk.FlagSequence), zkhelper.DefaultDirACLs())
if err != nil {
log.Error(err, prefix)
return errors.Trace(err)
}
if needConfirm {
if err := WaitForReceiver(zkConn, productName, actionCreated, proxies); err != nil {
return errors.Trace(err)
}
}
return nil
}
示例4: createDashboardNode
func createDashboardNode(conn zkhelper.Conn) error {
// make sure root dir is exists
rootDir := fmt.Sprintf("/zk/reborn/db_%s", globalEnv.ProductName())
zkhelper.CreateRecursive(conn, rootDir, "", 0, zkhelper.DefaultDirACLs())
coordPath := fmt.Sprintf("%s/dashboard", rootDir)
// make sure we're the only one dashboard
timeoutCh := time.After(60 * time.Second)
for {
if exists, _, ch, _ := conn.ExistsW(coordPath); exists {
data, _, _ := conn.Get(coordPath)
if checkDashboardAlive(data) {
return errors.Errorf("dashboard already exists: %s", string(data))
} else {
log.Warningf("dashboard %s exists in zk, wait it removed", data)
select {
case <-ch:
case <-timeoutCh:
return errors.Errorf("wait existed dashboard %s removed timeout", string(data))
}
}
} else {
break
}
}
content := fmt.Sprintf(`{"addr": "%v", "pid": %v}`, globalEnv.DashboardAddr(), os.Getpid())
pathCreated, err := conn.Create(coordPath, []byte(content),
zk.FlagEphemeral, zkhelper.DefaultFileACLs())
log.Infof("dashboard node %s created, data %s, err %v", pathCreated, string(content), err)
return errors.Trace(err)
}
示例5: NewActionWithTimeout
func NewActionWithTimeout(zkConn zkhelper.Conn, productName string, actionType ActionType, target interface{}, desc string, needConfirm bool, timeoutInMs int) error {
ts := strconv.FormatInt(time.Now().Unix(), 10)
action := &Action{
Type: actionType,
Desc: desc,
Target: target,
Ts: ts,
}
// set action receivers
proxies, err := ProxyList(zkConn, productName, func(p *ProxyInfo) bool {
return p.State == PROXY_STATE_ONLINE
})
if err != nil {
return errors.Trace(err)
}
if needConfirm {
// do fencing here, make sure 'offline' proxies are really offline
// now we only check whether the proxy lists are match
fenceProxies, err := GetFenceProxyMap(zkConn, productName)
if err != nil {
return errors.Trace(err)
}
for _, proxy := range proxies {
delete(fenceProxies, proxy.Addr)
}
if len(fenceProxies) > 0 {
errMsg := bytes.NewBufferString("Some proxies may not stop cleanly:")
for k, _ := range fenceProxies {
errMsg.WriteString(" ")
errMsg.WriteString(k)
}
return errors.New(errMsg.String())
}
}
for _, p := range proxies {
buf, err := json.Marshal(p)
if err != nil {
return errors.Trace(err)
}
action.Receivers = append(action.Receivers, string(buf))
}
b, _ := json.Marshal(action)
prefix := GetWatchActionPath(productName)
//action root path
err = CreateActionRootPath(zkConn, prefix)
if err != nil {
return errors.Trace(err)
}
//response path
respPath := path.Join(path.Dir(prefix), "ActionResponse")
err = CreateActionRootPath(zkConn, respPath)
if err != nil {
return errors.Trace(err)
}
//create response node, etcd do not support create in order directory
//get path first
actionRespPath, err := zkConn.Create(respPath+"/", b, int32(zk.FlagSequence), zkhelper.DefaultFileACLs())
if err != nil {
log.Error(err, respPath)
return errors.Trace(err)
}
//remove file then create directory
zkConn.Delete(actionRespPath, -1)
actionRespPath, err = zkConn.Create(actionRespPath, b, 0, zkhelper.DefaultDirACLs())
if err != nil {
log.Error(err, respPath)
return errors.Trace(err)
}
suffix := path.Base(actionRespPath)
// create action node
actionPath := path.Join(prefix, suffix)
_, err = zkConn.Create(actionPath, b, 0, zkhelper.DefaultFileACLs())
if err != nil {
log.Error(err, actionPath)
return errors.Trace(err)
}
if needConfirm {
if err := WaitForReceiverWithTimeout(zkConn, productName, actionRespPath, proxies, timeoutInMs); err != nil {
return errors.Trace(err)
}
}
return nil
}