• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Golang raft.Context类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/coreos/etcd/third_party/github.com/goraft/raft.Context的典型用法代码示例。如果您正苦于以下问题:Golang Context类的具体用法?Golang Context怎么用?Golang Context使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



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

示例1: Apply

// Get the key
func (c *GetCommand) Apply(context raft.Context) (interface{}, error) {
	s, _ := context.Server().StateMachine().(store.Store)
	e, err := s.Get(c.Key, c.Recursive, c.Sorted)

	if err != nil {
		log.Debug(err)
		return nil, err
	}
	return e, nil
}
开发者ID:jerrysearch,项目名称:etcd,代码行数:11,代码来源:get_command.go


示例2: Apply

// Set the key-value pair if the current value of the key equals to the given prevValue
func (c *CompareAndSwapCommand) Apply(context raft.Context) (interface{}, error) {
	s, _ := context.Server().StateMachine().(store.Store)

	e, err := s.CompareAndSwap(c.Key, c.PrevValue, c.PrevIndex, c.Value, c.ExpireTime)

	if err != nil {
		log.Debug(err)
		return nil, err
	}

	return e, nil
}
开发者ID:BREWTAN,项目名称:etcd,代码行数:13,代码来源:compare_and_swap_command.go


示例3: Apply

// Create node
func (c *CreateCommand) Apply(context raft.Context) (interface{}, error) {
	s, _ := context.Server().StateMachine().(store.Store)

	e, err := s.Create(c.Key, c.Dir, c.Value, c.Unique, c.ExpireTime)

	if err != nil {
		log.Debug(err)
		return nil, err
	}

	return e, nil
}
开发者ID:BREWTAN,项目名称:etcd,代码行数:13,代码来源:create_command.go


示例4: Apply

// Create node
func (c *SetCommand) Apply(context raft.Context) (interface{}, error) {
	s, _ := context.Server().StateMachine().(store.Store)

	// create a new node or replace the old node.
	e, err := s.Set(c.Key, c.Dir, c.Value, c.ExpireTime)

	if err != nil {
		log.Debug(err)
		return nil, err
	}

	return e, nil
}
开发者ID:BREWTAN,项目名称:etcd,代码行数:14,代码来源:set_command.go


示例5: applyJoin

// applyJoin attempts to join a machine to the cluster.
func applyJoin(c *JoinCommand, context raft.Context) (uint64, error) {
	ps, _ := context.Server().Context().(*PeerServer)
	commitIndex := context.CommitIndex()

	// Make sure we're not getting a cached value from the registry.
	ps.registry.Invalidate(c.Name)

	// Check if the join command is from a previous peer, who lost all its previous log.
	if peerURL, ok := ps.registry.PeerURL(c.Name); ok {
		// If previous node restarts with different peer URL,
		// update its information.
		if peerURL != c.RaftURL {
			log.Infof("Rejoin with %v instead of %v from %v", c.RaftURL, peerURL, c.Name)
			if err := updatePeerURL(c, ps); err != nil {
				return 0, err
			}
		}
		return commitIndex, nil
	}

	// Check if the join command adds an instance that collides with existing one on peer URL.
	peerURLs := ps.registry.PeerURLs(ps.raftServer.Leader(), c.Name)
	for _, peerURL := range peerURLs {
		if peerURL == c.RaftURL {
			log.Warnf("%v tries to join the cluster with existing URL %v", c.Name, c.EtcdURL)
			return 0, etcdErr.NewError(etcdErr.EcodeExistingPeerAddr, c.EtcdURL, context.CommitIndex())
		}
	}

	// Check peer number in the cluster
	if ps.registry.Count() >= ps.ClusterConfig().ActiveSize {
		log.Debug("Reject join request from ", c.Name)
		return 0, etcdErr.NewError(etcdErr.EcodeNoMorePeer, "", context.CommitIndex())
	}

	// Add to shared peer registry.
	ps.registry.Register(c.Name, c.RaftURL, c.EtcdURL)

	// Add peer in raft
	if err := context.Server().AddPeer(c.Name, ""); err != nil {
		return 0, err
	}

	// Add peer stats
	if c.Name != ps.RaftServer().Name() {
		ps.followersStats.Followers[c.Name] = &raftFollowerStats{}
		ps.followersStats.Followers[c.Name].Latency.Minimum = 1 << 63
	}

	return commitIndex, nil
}
开发者ID:nvdnkpr,项目名称:etcd,代码行数:52,代码来源:join_command.go


示例6: Apply

// Join a server to the cluster
func (c *JoinCommandV1) Apply(context raft.Context) (interface{}, error) {
	ps, _ := context.Server().Context().(*PeerServer)

	b := make([]byte, 8)
	binary.PutUvarint(b, context.CommitIndex())

	// Make sure we're not getting a cached value from the registry.
	ps.registry.Invalidate(c.Name)

	// Check if the join command is from a previous peer, who lost all its previous log.
	if _, ok := ps.registry.ClientURL(c.Name); ok {
		return b, nil
	}

	// Check peer number in the cluster
	if ps.registry.PeerCount() >= ps.ClusterConfig().ActiveSize {
		log.Debug("Reject join request from ", c.Name)
		return []byte{0}, etcdErr.NewError(etcdErr.EcodeNoMorePeer, "", context.CommitIndex())
	}

	// Add to shared peer registry.
	ps.registry.RegisterPeer(c.Name, c.RaftURL, c.EtcdURL)

	// Add peer in raft
	err := context.Server().AddPeer(c.Name, "")

	// Add peer stats
	if c.Name != ps.RaftServer().Name() {
		ps.followersStats.Followers[c.Name] = &raftFollowerStats{}
		ps.followersStats.Followers[c.Name].Latency.Minimum = 1 << 63
	}

	return b, err
}
开发者ID:joshuaconner,项目名称:etcd,代码行数:35,代码来源:join_command.go


示例7: Apply

// Delete the key
func (c *DeleteCommand) Apply(context raft.Context) (interface{}, error) {
	s, _ := context.Server().StateMachine().(store.Store)

	if c.Recursive {
		// recursive implies dir
		c.Dir = true
	}

	e, err := s.Delete(c.Key, c.Dir, c.Recursive)

	if err != nil {
		log.Debug(err)
		return nil, err
	}

	return e, nil
}
开发者ID:BREWTAN,项目名称:etcd,代码行数:18,代码来源:delete_command.go


示例8: Apply

// Apply executes the command.
func (c *DemoteCommand) Apply(context raft.Context) (interface{}, error) {
	ps, _ := context.Server().Context().(*PeerServer)

	// Ignore this command if there is no peer.
	if !ps.registry.PeerExists(c.Name) {
		return nil, fmt.Errorf("peer does not exist: %s", c.Name)
	}

	// Save URLs.
	clientURL, _ := ps.registry.ClientURL(c.Name)
	peerURL, _ := ps.registry.PeerURL(c.Name)

	// Remove node from the shared registry.
	err := ps.registry.UnregisterPeer(c.Name)
	if err != nil {
		log.Debugf("Demote peer %s: Error while unregistering (%v)", c.Name, err)
		return nil, err
	}

	// Delete from stats
	delete(ps.followersStats.Followers, c.Name)

	// Remove peer in raft
	err = context.Server().RemovePeer(c.Name)
	if err != nil {
		log.Debugf("Demote peer %s: (%v)", c.Name, err)
		return nil, err
	}

	// Register node as a standby.
	ps.registry.RegisterStandby(c.Name, peerURL, clientURL)

	// Update mode if this change applies to this server.
	if c.Name == ps.Config.Name {
		log.Infof("Demote peer %s: Set mode to standby with %s", c.Name, ps.server.Leader())
		ps.standbyPeerURL, _ = ps.registry.PeerURL(ps.server.Leader())
		go ps.setMode(StandbyMode)
	}

	return nil, nil
}
开发者ID:kennylixi,项目名称:etcd,代码行数:42,代码来源:demote_command.go


示例9: Apply

// Apply removes the given machine from the cluster.
func (c *RemoveCommandV2) Apply(context raft.Context) (interface{}, error) {
	ps, _ := context.Server().Context().(*PeerServer)
	ret, _ := json.Marshal(removeMessageV2{CommitIndex: context.CommitIndex()})

	// If this is a standby then remove it and exit.
	if ps.registry.StandbyExists(c.Name) {
		if err := ps.registry.UnregisterStandby(c.Name); err != nil {
			return nil, err
		}
		return ret, nil
	}

	// Remove node from the shared registry.
	err := ps.registry.UnregisterPeer(c.Name)

	// Delete from stats
	delete(ps.followersStats.Followers, c.Name)

	if err != nil {
		log.Debugf("Error while unregistering: %s (%v)", c.Name, err)
		return nil, err
	}

	// Remove peer in raft
	if err := context.Server().RemovePeer(c.Name); err != nil {
		log.Debugf("Unable to remove peer: %s (%v)", c.Name, err)
		return nil, err
	}

	if c.Name == context.Server().Name() {
		// the removed node is this node

		// if the node is not replaying the previous logs
		// and the node has sent out a join request in this
		// start. It is sure that this node received a new remove
		// command and need to be removed
		if context.CommitIndex() > ps.joinIndex && ps.joinIndex != 0 {
			log.Debugf("server [%s] is removed", context.Server().Name())
			os.Exit(0)
		} else {
			// else ignore remove
			log.Debugf("ignore previous remove command.")
		}
	}
	return ret, nil
}
开发者ID:kennylixi,项目名称:etcd,代码行数:47,代码来源:remove_command.go


示例10: applyRemove

// applyRemove removes the given machine from the cluster.
func applyRemove(c *RemoveCommand, context raft.Context) (uint64, error) {
	ps, _ := context.Server().Context().(*PeerServer)
	commitIndex := context.CommitIndex()

	// Remove node from the shared registry.
	err := ps.registry.Unregister(c.Name)

	// Delete from stats
	delete(ps.followersStats.Followers, c.Name)

	if err != nil {
		log.Debugf("Error while unregistering: %s (%v)", c.Name, err)
		return 0, err
	}

	// Remove peer in raft
	if err := context.Server().RemovePeer(c.Name); err != nil {
		log.Debugf("Unable to remove peer: %s (%v)", c.Name, err)
		return 0, err
	}

	if c.Name == context.Server().Name() {
		// the removed node is this node

		// if the node is not replaying the previous logs
		// and the node has sent out a join request in this
		// start. It is sure that this node received a new remove
		// command and need to be removed
		if context.CommitIndex() > ps.joinIndex && ps.joinIndex != 0 {
			log.Debugf("server [%s] is removed", context.Server().Name())
			ps.asyncRemove()
		} else {
			// else ignore remove
			log.Debugf("ignore previous remove command.")
		}
	}
	return commitIndex, nil
}
开发者ID:nvdnkpr,项目名称:etcd,代码行数:39,代码来源:remove_command.go


示例11: Apply

// Apply updates the cluster configuration.
func (c *SetClusterConfigCommand) Apply(context raft.Context) (interface{}, error) {
	ps, _ := context.Server().Context().(*PeerServer)
	ps.SetClusterConfig(c.Config)
	return nil, nil
}
开发者ID:BREWTAN,项目名称:etcd,代码行数:6,代码来源:set_cluster_config_command.go


示例12: Apply

func (c SyncCommand) Apply(context raft.Context) (interface{}, error) {
	s, _ := context.Server().StateMachine().(store.Store)
	s.DeleteExpiredKeys(c.Time)

	return nil, nil
}
开发者ID:BREWTAN,项目名称:etcd,代码行数:6,代码来源:sync_command.go


示例13: Apply

// Remove a server from the cluster
func (c *RemoveCommandV1) Apply(context raft.Context) (interface{}, error) {
	ps, _ := context.Server().Context().(*PeerServer)

	// If this is a proxy then remove it and exit.
	if ps.registry.ProxyExists(c.Name) {
		return []byte{0}, ps.registry.UnregisterProxy(c.Name)
	}

	// Remove node from the shared registry.
	err := ps.registry.UnregisterPeer(c.Name)

	// Delete from stats
	delete(ps.followersStats.Followers, c.Name)

	if err != nil {
		log.Debugf("Error while unregistering: %s (%v)", c.Name, err)
		return []byte{0}, err
	}

	// Remove peer in raft
	err = context.Server().RemovePeer(c.Name)
	if err != nil {
		log.Debugf("Unable to remove peer: %s (%v)", c.Name, err)
		return []byte{0}, err
	}

	if c.Name == context.Server().Name() {
		// the removed node is this node

		// if the node is not replaying the previous logs
		// and the node has sent out a join request in this
		// start. It is sure that this node received a new remove
		// command and need to be removed
		if context.CommitIndex() > ps.joinIndex && ps.joinIndex != 0 {
			log.Debugf("server [%s] is removed", context.Server().Name())
			os.Exit(0)
		} else {
			// else ignore remove
			log.Debugf("ignore previous remove command.")
		}
	}

	b := make([]byte, 8)
	binary.PutUvarint(b, context.CommitIndex())

	return b, err
}
开发者ID:joshuaconner,项目名称:etcd,代码行数:48,代码来源:remove_command.go


示例14: Apply

// Apply attempts to join a machine to the cluster.
func (c *JoinCommandV2) Apply(context raft.Context) (interface{}, error) {
	ps, _ := context.Server().Context().(*PeerServer)
	var msg = joinMessageV2{
		Mode:        PeerMode,
		CommitIndex: context.CommitIndex(),
	}

	// Make sure we're not getting a cached value from the registry.
	ps.registry.Invalidate(c.Name)

	// Check if the join command is from a previous peer, who lost all its previous log.
	if peerURL, ok := ps.registry.PeerURL(c.Name); ok {
		// If previous node restarts with different peer URL,
		// update its information.
		if peerURL != c.PeerURL {
			log.Infof("Rejoin with %v instead of %v from %v", c.PeerURL, peerURL, c.Name)
			if err := c.updatePeerURL(ps); err != nil {
				return []byte{0}, err
			}
		}
		return json.Marshal(msg)
	}

	// Check if the join command adds an instance that collides with existing one on peer URL.
	peerURLs := ps.registry.PeerURLs(ps.raftServer.Leader(), c.Name)
	for _, peerURL := range peerURLs {
		if peerURL == c.PeerURL {
			log.Warnf("%v tries to join the cluster with existing URL %v", c.Name, c.PeerURL)
			return []byte{0}, etcdErr.NewError(etcdErr.EcodeExistingPeerAddr, c.PeerURL, context.CommitIndex())
		}
	}

	// Check peer number in the cluster.
	if ps.registry.PeerCount() >= ps.ClusterConfig().ActiveSize {
		log.Debug("Join as standby ", c.Name)
		ps.registry.RegisterStandby(c.Name, c.PeerURL, c.ClientURL)
		msg.Mode = StandbyMode
		return json.Marshal(msg)
	}

	// Remove it as a standby if it is one.
	if ps.registry.StandbyExists(c.Name) {
		ps.registry.UnregisterStandby(c.Name)
	}

	// Add to shared peer registry.
	ps.registry.RegisterPeer(c.Name, c.PeerURL, c.ClientURL)

	// Add peer in raft
	if err := context.Server().AddPeer(c.Name, ""); err != nil {
		b, _ := json.Marshal(msg)
		return b, err
	}

	// Add peer stats
	if c.Name != ps.RaftServer().Name() {
		ps.followersStats.Followers[c.Name] = &raftFollowerStats{}
		ps.followersStats.Followers[c.Name].Latency.Minimum = 1 << 63
	}

	return json.Marshal(msg)
}
开发者ID:kennylixi,项目名称:etcd,代码行数:63,代码来源:join_command.go



注:本文中的github.com/coreos/etcd/third_party/github.com/goraft/raft.Context类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang raft.Event类代码示例发布时间:2022-05-23
下一篇:
Golang raft.Server类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap