raft: atomic load id

release-2.0
Xiang Li 2014-07-10 20:54:16 -07:00 committed by Yicheng Qin
parent 18d95b336f
commit 3f435571d3
1 changed files with 7 additions and 4 deletions

View File

@ -3,6 +3,7 @@ package raft
import (
"encoding/json"
golog "log"
"sync/atomic"
)
type Interface interface {
@ -40,7 +41,9 @@ func New(id int64, heartbeat, election tick) *Node {
return n
}
func (n *Node) Id() int64 { return n.sm.id }
func (n *Node) Id() int64 {
return atomic.LoadInt64(&n.sm.id)
}
func (n *Node) Index() int { return n.sm.log.lastIndex() }
@ -48,11 +51,11 @@ func (n *Node) Term() int { return n.sm.term }
func (n *Node) Applied() int { return n.sm.log.applied }
func (n *Node) HasLeader() bool { return n.sm.lead != none }
func (n *Node) HasLeader() bool { return n.Leader() != none }
func (n *Node) IsLeader() bool { return n.sm.lead == n.Id() }
func (n *Node) IsLeader() bool { return n.Leader() == n.Id() }
func (n *Node) Leader() int64 { return n.sm.lead }
func (n *Node) Leader() int64 { return atomic.LoadInt64(&n.sm.lead) }
// Propose asynchronously proposes data be applied to the underlying state machine.
func (n *Node) Propose(data []byte) { n.propose(Normal, data) }