当前位置: 首页>>代码示例>>Golang>>正文


Golang zookeeper.IsError函数代码示例

本文整理汇总了Golang中launchpad/net/gozk/zookeeper.IsError函数的典型用法代码示例。如果您正苦于以下问题:Golang IsError函数的具体用法?Golang IsError怎么用?Golang IsError使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了IsError函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: UpdateEndPoints

// UpdateEndPoints is part of the topo.Server interface
func (zkts *Server) UpdateEndPoints(ctx context.Context, cell, keyspace, shard string, tabletType topodatapb.TabletType, addrs *topodatapb.EndPoints, existingVersion int64) error {
	path := zkPathForVtName(cell, keyspace, shard, tabletType)
	data, err := json.MarshalIndent(addrs, "", "  ")
	if err != nil {
		return err
	}

	if existingVersion == -1 {
		// Update or create unconditionally.
		_, err := zk.CreateRecursive(zkts.zconn, path, string(data), 0, zookeeper.WorldACL(zookeeper.PERM_ALL))
		if err != nil {
			if zookeeper.IsError(err, zookeeper.ZNODEEXISTS) {
				// Node already exists - just stomp away. Multiple writers shouldn't be here.
				// We use RetryChange here because it won't update the node unnecessarily.
				f := func(oldValue string, oldStat zk.Stat) (string, error) {
					return string(data), nil
				}
				err = zkts.zconn.RetryChange(path, 0, zookeeper.WorldACL(zookeeper.PERM_ALL), f)
			}
		}
		return err
	}

	// Compare And Set
	if _, err = zkts.zconn.Set(path, string(data), int(existingVersion)); err != nil {
		if zookeeper.IsError(err, zookeeper.ZBADVERSION) {
			err = topo.ErrBadVersion
		} else if zookeeper.IsError(err, zookeeper.ZNONODE) {
			err = topo.ErrNoNode
		}
	}
	return err
}
开发者ID:aaijazi,项目名称:vitess,代码行数:34,代码来源:serving_graph.go

示例2: Create

// Create implements Conn.
func (conn *MetaConn) Create(path, value string, flags int, aclv []zookeeper.ACL) (pathCreated string, err error) {
	var zconn Conn
	for i := 0; i < maxAttempts; i++ {
		zconn, err = conn.connCache.ConnForPath(path)
		if err != nil {
			return
		}
		path = resolveZkPath(path)
		pathCreated, err = zconn.Create(path, value, flags, aclv)
		if err != nil && zookeeper.IsError(err, zookeeper.ZNONODE) {
			parts := strings.Split(path, "/")
			if len(parts) == 3 && parts[0] == "" && parts[1] == MagicPrefix {
				// We were asked to create a /zk/<cell> path, but /zk doesn't exist.
				// We should create /zk automatically in this case, because it's
				// impossible to create /zk via MetaConn, since there's no cell name.
				_, err = zconn.Create("/"+MagicPrefix, "", 0, zookeeper.WorldACL(zookeeper.PERM_ALL))
				if err != nil {
					if shouldRetry(err) {
						continue
					}
					if !zookeeper.IsError(err, zookeeper.ZNODEEXISTS) {
						return "", err
					}
				}
				// Now try the original path again.
				pathCreated, err = zconn.Create(path, value, flags, aclv)
			}
		}
		if !shouldRetry(err) {
			return
		}
	}
	return
}
开发者ID:CowLeo,项目名称:vitess,代码行数:35,代码来源:metaconn.go

示例3: RetryChange

func (conn *zconn) RetryChange(path string, flags int, acl []zookeeper.ACL, changeFunc zk.ChangeFunc) error {
	for {
		oldValue, oldStat, err := conn.Get(path)
		if err != nil && !zookeeper.IsError(err, zookeeper.ZNONODE) {
			return err
		}
		newValue, err := changeFunc(oldValue, oldStat)
		if err != nil {
			return err
		}
		if oldStat == nil {
			_, err := conn.Create(path, newValue, flags, acl)
			if err == nil || !zookeeper.IsError(err, zookeeper.ZNODEEXISTS) {
				return err
			}
			continue
		}
		if newValue == oldValue {
			return nil // Nothing to do.
		}
		_, err = conn.Set(path, newValue, oldStat.Version())
		if err == nil || !zookeeper.IsError(err, zookeeper.ZBADVERSION) && !zookeeper.IsError(err, zookeeper.ZNONODE) {
			return err
		}
	}
}
开发者ID:CowLeo,项目名称:vitess,代码行数:26,代码来源:fakezk.go

示例4: DeleteRecursive

func DeleteRecursive(zconn Conn, zkPath string, version int) error {
	// version: -1 delete any version of the node at path - only applies to the top node
	err := zconn.Delete(zkPath, version)
	if err == nil {
		return nil
	}
	if !zookeeper.IsError(err, zookeeper.ZNOTEMPTY) {
		return err
	}
	// Remove the ability for other nodes to get created while we are trying to delete.
	// Otherwise, you can enter a race condition, or get starved out from deleting.
	err = zconn.SetACL(zkPath, zookeeper.WorldACL(zookeeper.PERM_ADMIN|zookeeper.PERM_DELETE|zookeeper.PERM_READ), version)
	if err != nil {
		return err
	}
	children, _, err := zconn.Children(zkPath)
	if err != nil {
		return err
	}
	for _, child := range children {
		err := DeleteRecursive(zconn, path.Join(zkPath, child), -1)
		if err != nil && !zookeeper.IsError(err, zookeeper.ZNONODE) {
			return fmt.Errorf("zkutil: recursive delete failed: %v", err)
		}
	}

	err = zconn.Delete(zkPath, version)
	if err != nil && !zookeeper.IsError(err, zookeeper.ZNOTEMPTY) {
		err = fmt.Errorf("zkutil: nodes getting recreated underneath delete (app race condition): %v", zkPath)
	}
	return err
}
开发者ID:kingpro,项目名称:vitess,代码行数:32,代码来源:zkutil.go

示例5: UpdateKeyspace

func (zkts *Server) UpdateKeyspace(ki *topo.KeyspaceInfo) error {
	keyspacePath := path.Join(globalKeyspacesPath, ki.KeyspaceName())
	data := jscfg.ToJson(ki.Keyspace)
	_, err := zkts.zconn.Set(keyspacePath, data, -1)
	if err != nil {
		if zookeeper.IsError(err, zookeeper.ZNONODE) {
			// The code should be:
			//   err = topo.ErrNoNode
			// Temporary code until we have Keyspace object
			// everywhere:
			_, err = zkts.zconn.Create(keyspacePath, data, 0, zookeeper.WorldACL(zookeeper.PERM_ALL))
			if err != nil {
				if zookeeper.IsError(err, zookeeper.ZNONODE) {
					// the directory doesn't even exist
					err = topo.ErrNoNode
				}
				return err
			}

			event.Dispatch(&events.KeyspaceChange{
				KeyspaceInfo: *ki,
				Status:       "updated (had to create Keyspace object)",
			})
			return nil
		}
		return err
	}

	event.Dispatch(&events.KeyspaceChange{
		KeyspaceInfo: *ki,
		Status:       "updated",
	})
	return nil
}
开发者ID:ninqing,项目名称:vitess,代码行数:34,代码来源:keyspace.go

示例6: Create

// Create the persistence node in zookeeper
func (zk *ZK) Create(path string, node string) error {
	// create zk root path
	tpath := ""
	for _, str := range strings.Split(path, "/")[1:] {
		tpath += "/" + str
		Log.Debug("create zookeeper path:%s", tpath)
		_, err := zk.conn.Create(tpath, "", 0, zookeeper.WorldACL(zookeeper.PERM_ALL))
		if err != nil {
			if zookeeper.IsError(err, zookeeper.ZNODEEXISTS) {
				Log.Warn("zk.Create(\"%s\") exists", tpath)
			} else {
				Log.Error("zk.Create(\"%s\") failed (%s)", tpath, err.Error())
				return err
			}
		}
	}

	// create node path
	fpath := path + "/" + node
	Log.Debug("create zookeeper path:%s", fpath)
	_, err := zk.conn.Create(fpath, "", 0, zookeeper.WorldACL(zookeeper.PERM_ALL))
	if err != nil {
		if zookeeper.IsError(err, zookeeper.ZNODEEXISTS) {
			Log.Warn("zk.Create(\"%s\") exists", fpath)
		} else {
			Log.Error("zk.Create(\"%s\") failed (%s)", fpath, err.Error())
			return err
		}
	}

	return nil
}
开发者ID:Ehuos,项目名称:gopush-cluster,代码行数:33,代码来源:zk.go

示例7: PurgeActions

// PurgeActions removes all queued actions, leaving the action node
// itself in place.
//
// This inherently breaks the locking mechanism of the action queue,
// so this is a rare cleanup action, not a normal part of the flow.
//
// This can be used for tablets, shards and keyspaces.
func (zkts *Server) PurgeActions(zkActionPath string, canBePurged func(data string) bool) error {
	if path.Base(zkActionPath) != "action" {
		return fmt.Errorf("not action path: %v", zkActionPath)
	}

	children, _, err := zkts.zconn.Children(zkActionPath)
	if err != nil {
		return err
	}

	sort.Strings(children)
	// Purge newer items first so the action queues don't try to process something.
	for i := len(children) - 1; i >= 0; i-- {
		actionPath := path.Join(zkActionPath, children[i])
		data, _, err := zkts.zconn.Get(actionPath)
		if err != nil && !zookeeper.IsError(err, zookeeper.ZNONODE) {
			return fmt.Errorf("PurgeActions(%v) err: %v", zkActionPath, err)
		}
		if !canBePurged(data) {
			continue
		}

		err = zk.DeleteRecursive(zkts.zconn, actionPath, -1)
		if err != nil && !zookeeper.IsError(err, zookeeper.ZNONODE) {
			return fmt.Errorf("PurgeActions(%v) err: %v", zkActionPath, err)
		}
	}
	return nil
}
开发者ID:henryanand,项目名称:vitess,代码行数:36,代码来源:server.go

示例8: create

// Create the persistence node in zookeeper
func (zk *ZK) create() error {
	// create zk root path
	tpath := ""
	for _, str := range strings.Split(Conf.ZookeeperPath, "/")[1:] {
		tpath += "/" + str
		Log.Debug("create zookeeper path:%s", tpath)
		_, err := zk.conn.Create(tpath, "", 0, zookeeper.WorldACL(zookeeper.PERM_ALL))
		if err != nil {
			if zookeeper.IsError(err, zookeeper.ZNODEEXISTS) {
				Log.Warn("zk.create(\"%s\") exists", tpath)
			} else {
				Log.Error("zk.create(\"%s\") error(%v)", tpath, err)
				return err
			}
		}
	}
	// create node path
	fpath := fmt.Sprintf("%s/%s", Conf.ZookeeperPath, Conf.ZookeeperNode)
	Log.Debug("create zookeeper path:%s", fpath)
	_, err := zk.conn.Create(fpath, "", 0, zookeeper.WorldACL(zookeeper.PERM_ALL))
	if err != nil {
		if zookeeper.IsError(err, zookeeper.ZNODEEXISTS) {
			Log.Warn("zk.Create(\"%s\") exists", fpath)
		} else {
			Log.Error("zk.Create(\"%s\") error(%v)", fpath, err)
			return err
		}
	}
	return nil
}
开发者ID:Brant-Zhang,项目名称:gopush-cluster,代码行数:31,代码来源:zk.go

示例9: refreshSomeValues

// return a few values that need to be refreshed
func (zkc *ZkCache) refreshSomeValues(zconn zk.Conn, maxToRefresh int) {
	// build a list of a few values we want to refresh
	refreshThreshold := time.Now().Add(-10 * time.Minute)

	// range will randomize the traversal order, so we will not always try
	// the same entries in the same order
	dataEntries := make([]*zkCacheEntry, 0, maxToRefresh)
	childrenEntries := make([]*zkCacheEntry, 0, maxToRefresh)
	zkc.mutex.Lock()
	for _, entry := range zkc.Cache {
		shouldBeDataRefreshed, shouldBeChildrenRefreshed := entry.checkForRefresh(refreshThreshold)
		if shouldBeDataRefreshed {
			dataEntries = append(dataEntries, entry)
		}
		if shouldBeChildrenRefreshed {
			childrenEntries = append(childrenEntries, entry)
		}

		// check if we have enough work to do
		if len(dataEntries) == maxToRefresh || len(childrenEntries) == maxToRefresh {
			break
		}
	}
	zkc.mutex.Unlock()

	// now refresh the values
	for _, entry := range dataEntries {
		data, stat, watch, err := zconn.GetW(entry.node.Path)
		if err == nil {
			zkStat := &zk.ZkStat{}
			zkStat.FromZookeeperStat(stat)
			entry.updateData(data, zkStat, watch)
		} else if zookeeper.IsError(err, zookeeper.ZCLOSING) {
			// connection is closing, no point in asking for more
			relog.Warning("failed to refresh cache: %v (and stopping refresh)", err.Error())
			return
		} else {
			// individual failure
			relog.Warning("failed to refresh cache: %v", err.Error())
		}
	}

	for _, entry := range childrenEntries {
		children, stat, watch, err := zconn.ChildrenW(entry.node.Path)
		if err == nil {
			zkStat := &zk.ZkStat{}
			zkStat.FromZookeeperStat(stat)
			entry.updateChildren(children, zkStat, watch)
		} else if zookeeper.IsError(err, zookeeper.ZCLOSING) {
			// connection is closing, no point in asking for more
			relog.Warning("failed to refresh cache: %v (and stopping refresh)", err.Error())
			return
		} else {
			// individual failure
			relog.Warning("failed to refresh cache: %v", err.Error())
		}
	}
}
开发者ID:Eric-Chen,项目名称:vitess,代码行数:59,代码来源:cache.go

示例10: init

func (zkd *Zkd) init(preserveData bool) error {
	log.Infof("zkd.Init")
	for _, path := range zkd.config.DirectoryList() {
		if err := os.MkdirAll(path, 0775); err != nil {
			log.Errorf(err.Error())
			return err
		}
		// FIXME(msolomon) validate permissions?
	}

	configData, err := zkd.makeCfg()
	if err == nil {
		err = ioutil.WriteFile(zkd.config.ConfigFile(), []byte(configData), 0664)
	}
	if err != nil {
		log.Errorf("failed creating %v: %v", zkd.config.ConfigFile(), err)
		return err
	}

	err = zkd.config.WriteMyid()
	if err != nil {
		log.Errorf("failed creating %v: %v", zkd.config.MyidFile(), err)
		return err
	}

	if err = zkd.Start(); err != nil {
		log.Errorf("failed starting, check %v", zkd.config.LogDir())
		return err
	}

	zkAddr := fmt.Sprintf("localhost:%v", zkd.config.ClientPort)
	zk, session, err := zookeeper.Dial(zkAddr, StartWaitTime*time.Second)
	if err != nil {
		return err
	}
	event := <-session
	if event.State != zookeeper.STATE_CONNECTED {
		return err
	}
	defer zk.Close()

	if !preserveData {
		_, err = zk.Create("/zk", "", 0, zookeeper.WorldACL(zookeeper.PERM_ALL))
		if err != nil && !zookeeper.IsError(err, zookeeper.ZNODEEXISTS) {
			return err
		}

		if zkd.config.Global {
			_, err = zk.Create("/zk/global", "", 0, zookeeper.WorldACL(zookeeper.PERM_ALL))
			if err != nil && !zookeeper.IsError(err, zookeeper.ZNODEEXISTS) {
				return err
			}
		}
	}

	return nil
}
开发者ID:littleyang,项目名称:vitess,代码行数:57,代码来源:zkctl.go

示例11: DeleteEndPoints

// DeleteEndPoints is part of the topo.Server interface
func (zkts *Server) DeleteEndPoints(ctx context.Context, cell, keyspace, shard string, tabletType topodatapb.TabletType, existingVersion int64) error {
	path := zkPathForVtName(cell, keyspace, shard, tabletType)
	if err := zkts.zconn.Delete(path, int(existingVersion)); err != nil {
		switch {
		case zookeeper.IsError(err, zookeeper.ZNONODE):
			err = topo.ErrNoNode
		case zookeeper.IsError(err, zookeeper.ZBADVERSION):
			err = topo.ErrBadVersion
		}
		return err
	}
	return nil
}
开发者ID:aaijazi,项目名称:vitess,代码行数:14,代码来源:serving_graph.go

示例12: DeleteReplicationPath

func (zkts *Server) DeleteReplicationPath(keyspace, shard, repPath string) error {
	replicationPath := path.Join(globalKeyspacesPath, keyspace, "shards", shard, repPath)
	err := zkts.zconn.Delete(replicationPath, -1)
	if err != nil {
		if zookeeper.IsError(err, zookeeper.ZNONODE) {
			err = topo.ErrNoNode
		} else if zookeeper.IsError(err, zookeeper.ZNOTEMPTY) {
			err = topo.ErrNotEmpty
		}
		return err
	}
	return nil
}
开发者ID:CERN-Stage-3,项目名称:vitess,代码行数:13,代码来源:replication_graph.go

示例13: checkForRefresh

func (entry *zkCacheEntry) checkForRefresh(refreshThreshold time.Time) (shouldBeDataRefreshed, shouldBeChildrenRefreshed bool) {
	entry.mutex.Lock()
	defer entry.mutex.Unlock()

	if entry.dataTime.IsZero() {
		// the entry was never set
		// if we had an error getting it, it means we asked for it,
		// see if we can get a good value
		if entry.dataError != nil {
			if zookeeper.IsError(entry.dataError, zookeeper.ZNONODE) {
				// we had no node, we can try next time a client asks
				entry.dataError = nil
				// at this point, we have both
				// dataTime.IsZero() and
				// entry.dataError = nil, as if we
				// never asked for it
			} else {
				// we had a valid error, let's try again
				shouldBeDataRefreshed = true
			}
		}
	} else {
		// 1. we got a value at some point, then it got stale
		// 2. we got a value a long time ago, refresh it
		shouldBeDataRefreshed = entry.dataError != nil || entry.dataTime.Before(refreshThreshold)
	}

	if entry.childrenTime.IsZero() {
		// the entry was never set
		// if we had an error getting it, it means we asked for it,
		// see if we can get a good value
		if entry.childrenError != nil {
			if zookeeper.IsError(entry.childrenError, zookeeper.ZNONODE) {
				// we had no node, we can try next time a client asks
				entry.childrenError = nil
				// at this point, we have both
				// childrenTime.IsZero() and
				// entry.childrenError = nil, as if we
				// never asked for it
			} else {
				shouldBeChildrenRefreshed = true
				// we had a valid error, let's try again
			}
		}
	} else {
		// 1. we got a value at some point, then it got stale
		// 2. we got a value a long time ago, refresh it
		shouldBeChildrenRefreshed = entry.childrenError != nil || entry.childrenTime.Before(refreshThreshold)
	}
	return
}
开发者ID:Eric-Chen,项目名称:vitess,代码行数:51,代码来源:cache.go

示例14: UpdateTablet

func (zkts *Server) UpdateTablet(tablet *topo.TabletInfo, existingVersion int) (int, error) {
	zkTabletPath := TabletPathForAlias(tablet.Alias())
	stat, err := zkts.zconn.Set(zkTabletPath, tablet.Json(), existingVersion)
	if err != nil {
		if zookeeper.IsError(err, zookeeper.ZBADVERSION) {
			err = topo.ErrBadVersion
		} else if zookeeper.IsError(err, zookeeper.ZNONODE) {
			err = topo.ErrNoNode
		}

		return 0, err
	}
	return stat.Version(), nil
}
开发者ID:johnvilsack,项目名称:golang-stuff,代码行数:14,代码来源:cell.go

示例15: StaleActions

// StaleActions returns a list of queued actions that have been
// sitting for more than some amount of time.
func (zkts *Server) StaleActions(zkActionPath string, maxStaleness time.Duration, isStale func(data string) bool) ([]string, error) {
	if path.Base(zkActionPath) != "action" {
		return nil, fmt.Errorf("not action path: %v", zkActionPath)
	}

	children, _, err := zkts.zconn.Children(zkActionPath)
	if err != nil {
		return nil, err
	}

	staleActions := make([]string, 0, 16)
	// Purge newer items first so the action queues don't try to process something.
	sort.Strings(children)
	for i := 0; i < len(children); i++ {
		actionPath := path.Join(zkActionPath, children[i])
		data, stat, err := zkts.zconn.Get(actionPath)
		if err != nil && !zookeeper.IsError(err, zookeeper.ZNONODE) {
			return nil, fmt.Errorf("stale action err: %v", err)
		}
		if stat == nil || time.Since(stat.MTime()) <= maxStaleness {
			continue
		}
		if isStale(data) {
			staleActions = append(staleActions, data)
		}
	}
	return staleActions, nil
}
开发者ID:henryanand,项目名称:vitess,代码行数:30,代码来源:server.go


注:本文中的launchpad/net/gozk/zookeeper.IsError函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。