Browse Source

raft: atomic load id

Xiang Li 11 years ago
parent
commit
3f435571d3
1 changed files with 7 additions and 4 deletions
  1. 7 4
      raft/node.go

+ 7 - 4
raft/node.go

@@ -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) }