Browse Source

raft: make entry type public

Xiang Li 11 years ago
parent
commit
9f315ffe10
4 changed files with 18 additions and 18 deletions
  1. 3 3
      raft/log.go
  2. 6 6
      raft/node.go
  3. 2 2
      raft/raft.go
  4. 7 7
      raft/raft_test.go

+ 3 - 3
raft/log.go

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

+ 6 - 6
raft/node.go

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

+ 2 - 2
raft/raft.go

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

+ 7 - 7
raft/raft_test.go

@@ -497,19 +497,19 @@ func TestConf(t *testing.T) {
 	sm.becomeCandidate()
 	sm.becomeCandidate()
 	sm.becomeLeader()
 	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 {
 	if sm.log.lastIndex() != 1 {
 		t.Errorf("lastindex = %d, want %d", sm.log.lastIndex(), 1)
 		t.Errorf("lastindex = %d, want %d", sm.log.lastIndex(), 1)
 	}
 	}
 	if !sm.pendingConf {
 	if !sm.pendingConf {
 		t.Errorf("pendingConf = %v, want %v", sm.pendingConf, true)
 		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
 	// 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 {
 	if sm.log.lastIndex() != 1 {
 		t.Errorf("lastindex = %d, want %d", 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
 		et       int
 		wPending bool
 		wPending bool
 	}{
 	}{
-		{normal, false},
-		{configAdd, true},
-		{configRemove, true},
+		{Normal, false},
+		{ConfigAdd, true},
+		{ConfigRemove, true},
 	}
 	}
 
 
 	for i, tt := range tests {
 	for i, tt := range tests {