raft: make entry type public

This commit is contained in:
Xiang Li 2014-06-19 14:08:35 -07:00 committed by Yicheng Qin
parent 584186c7ff
commit 9f315ffe10
4 changed files with 18 additions and 18 deletions

View File

@ -1,10 +1,10 @@
package raft
const (
normal int = iota
Normal int = iota
configAdd
configRemove
ConfigAdd
ConfigRemove
)
type Entry struct {

View File

@ -50,15 +50,15 @@ func (n *Node) Id() int { return n.sm.id }
func (n *Node) HasLeader() bool { return n.sm.lead != none }
// Propose asynchronously proposes data be applied to the underlying state machine.
func (n *Node) Propose(data []byte) { n.propose(normal, data) }
func (n *Node) Propose(data []byte) { n.propose(Normal, data) }
func (n *Node) propose(t int, data []byte) {
n.Step(Message{Type: msgProp, Entries: []Entry{{Type: t, Data: data}}})
}
func (n *Node) Add(id int) { n.updateConf(configAdd, &config{NodeId: id}) }
func (n *Node) Add(id int) { n.updateConf(ConfigAdd, &config{NodeId: id}) }
func (n *Node) Remove(id int) { n.updateConf(configRemove, &config{NodeId: id}) }
func (n *Node) Remove(id int) { n.updateConf(ConfigRemove, &config{NodeId: id}) }
func (n *Node) Msgs() []Message { return n.sm.Msgs() }
@ -87,15 +87,15 @@ func (n *Node) Next() []Entry {
ents := n.sm.nextEnts()
for i := range ents {
switch ents[i].Type {
case normal:
case configAdd:
case Normal:
case ConfigAdd:
c := new(config)
if err := json.Unmarshal(ents[i].Data, c); err != nil {
golog.Println(err)
continue
}
n.sm.addNode(c.NodeId)
case configRemove:
case ConfigRemove:
c := new(config)
if err := json.Unmarshal(ents[i].Data, c); err != nil {
golog.Println(err)

View File

@ -227,7 +227,7 @@ func (sm *stateMachine) becomeLeader() {
sm.state = stateLeader
for _, e := range sm.log.ents[sm.log.committed:] {
if e.Type == configAdd || e.Type == configRemove {
if e.Type == ConfigAdd || e.Type == ConfigRemove {
sm.pendingConf = true
}
}
@ -270,7 +270,7 @@ func (sm *stateMachine) Step(m Message) (ok bool) {
switch sm.lead {
case sm.id:
e := m.Entries[0]
if e.Type == configAdd || e.Type == configRemove {
if e.Type == ConfigAdd || e.Type == ConfigRemove {
if sm.pendingConf {
return false
}

View File

@ -497,19 +497,19 @@ func TestConf(t *testing.T) {
sm.becomeCandidate()
sm.becomeLeader()
sm.Step(Message{Type: msgProp, Entries: []Entry{{Type: configAdd}}})
sm.Step(Message{Type: msgProp, Entries: []Entry{{Type: ConfigAdd}}})
if sm.log.lastIndex() != 1 {
t.Errorf("lastindex = %d, want %d", sm.log.lastIndex(), 1)
}
if !sm.pendingConf {
t.Errorf("pendingConf = %v, want %v", sm.pendingConf, true)
}
if sm.log.ents[1].Type != configAdd {
t.Errorf("type = %d, want %d", sm.log.ents[1].Type, configAdd)
if sm.log.ents[1].Type != ConfigAdd {
t.Errorf("type = %d, want %d", sm.log.ents[1].Type, ConfigAdd)
}
// deny the second configuration change request if there is a pending one
sm.Step(Message{Type: msgProp, Entries: []Entry{{Type: configAdd}}})
sm.Step(Message{Type: msgProp, Entries: []Entry{{Type: ConfigAdd}}})
if sm.log.lastIndex() != 1 {
t.Errorf("lastindex = %d, want %d", sm.log.lastIndex(), 1)
}
@ -522,9 +522,9 @@ func TestConfChangeLeader(t *testing.T) {
et int
wPending bool
}{
{normal, false},
{configAdd, true},
{configRemove, true},
{Normal, false},
{ConfigAdd, true},
{ConfigRemove, true},
}
for i, tt := range tests {