From b82d70871f21b1f9c5f40871b12f0242bcdc6a9b Mon Sep 17 00:00:00 2001 From: Yicheng Qin Date: Sat, 20 Sep 2014 22:48:17 -0700 Subject: [PATCH] raft: use EntryType in protobuf --- etcdserver/server.go | 4 ++-- etcdserver/server_test.go | 2 +- raft/log.go | 5 ----- raft/node.go | 2 +- raft/raft.go | 4 ++-- raft/raft_test.go | 16 +++++++------- raft/raftpb/raft.pb.go | 46 ++++++++++++++++++++++++++++++++++----- raft/raftpb/raft.proto | 14 ++++++++---- 8 files changed, 64 insertions(+), 29 deletions(-) diff --git a/etcdserver/server.go b/etcdserver/server.go index 3e99b1007..7a45a9b38 100644 --- a/etcdserver/server.go +++ b/etcdserver/server.go @@ -127,13 +127,13 @@ func (s *EtcdServer) run() { // race them. for _, e := range rd.CommittedEntries { switch e.Type { - case raft.EntryNormal: + case raftpb.EntryNormal: var r pb.Request if err := r.Unmarshal(e.Data); err != nil { panic("TODO: this is bad, what do we do about it?") } s.w.Trigger(r.Id, s.applyRequest(r)) - case raft.EntryConfig: + case raftpb.EntryConfig: var c pb.Config if err := c.Unmarshal(e.Data); err != nil { panic("TODO: this is bad, what do we do about it?") diff --git a/etcdserver/server_test.go b/etcdserver/server_test.go index ba48434f4..87845559c 100644 --- a/etcdserver/server_test.go +++ b/etcdserver/server_test.go @@ -895,7 +895,7 @@ func (n *nodeCommitterRecorder) Propose(ctx context.Context, data []byte) error return n.nodeRecorder.Propose(ctx, data) } func (n *nodeCommitterRecorder) Configure(ctx context.Context, data []byte) error { - n.readyc <- raft.Ready{CommittedEntries: []raftpb.Entry{{Type: raft.EntryConfig, Data: data}}} + n.readyc <- raft.Ready{CommittedEntries: []raftpb.Entry{{Type: raftpb.EntryConfig, Data: data}}} return n.nodeRecorder.Configure(ctx, data) } func (n *nodeCommitterRecorder) Ready() <-chan raft.Ready { diff --git a/raft/log.go b/raft/log.go index b17dba8df..562afae9f 100644 --- a/raft/log.go +++ b/raft/log.go @@ -10,11 +10,6 @@ const ( defaultCompactThreshold = 10000 ) -const ( - EntryNormal int64 = iota - EntryConfig -) - type raftLog struct { ents []pb.Entry unstable int64 diff --git a/raft/node.go b/raft/node.go index af0e98a0c..43c55b8f1 100644 --- a/raft/node.go +++ b/raft/node.go @@ -247,7 +247,7 @@ func (n *node) Propose(ctx context.Context, data []byte) error { } func (n *node) Configure(ctx context.Context, data []byte) error { - return n.Step(ctx, pb.Message{Type: msgProp, Entries: []pb.Entry{{Type: EntryConfig, Data: data}}}) + return n.Step(ctx, pb.Message{Type: msgProp, Entries: []pb.Entry{{Type: pb.EntryConfig, Data: data}}}) } // Step advances the state machine using msgs. The ctx.Err() will be returned, diff --git a/raft/raft.go b/raft/raft.go index 63d1e7de2..ddd9eb1e7 100644 --- a/raft/raft.go +++ b/raft/raft.go @@ -313,7 +313,7 @@ func (r *raft) becomeLeader() { r.lead = r.id r.state = StateLeader for _, e := range r.raftLog.entries(r.raftLog.committed + 1) { - if e.Type != EntryConfig { + if e.Type != pb.EntryConfig { continue } if r.pendingConf { @@ -407,7 +407,7 @@ func stepLeader(r *raft, m pb.Message) { panic("unexpected length(entries) of a msgProp") } e := m.Entries[0] - if e.Type == EntryConfig { + if e.Type == pb.EntryConfig { if r.pendingConf { return } diff --git a/raft/raft_test.go b/raft/raft_test.go index 37274f683..1d56c4ff8 100644 --- a/raft/raft_test.go +++ b/raft/raft_test.go @@ -956,7 +956,7 @@ func TestStepConfig(t *testing.T) { r.becomeCandidate() r.becomeLeader() index := r.raftLog.lastIndex() - r.Step(pb.Message{From: 1, To: 1, Type: msgProp, Entries: []pb.Entry{{Type: EntryConfig}}}) + r.Step(pb.Message{From: 1, To: 1, Type: msgProp, Entries: []pb.Entry{{Type: pb.EntryConfig}}}) if g := r.raftLog.lastIndex(); g != index+1 { t.Errorf("index = %d, want %d", g, index+1) } @@ -973,10 +973,10 @@ func TestStepIgnoreConfig(t *testing.T) { r := newRaft(1, []int64{1, 2}, 0, 0) r.becomeCandidate() r.becomeLeader() - r.Step(pb.Message{From: 1, To: 1, Type: msgProp, Entries: []pb.Entry{{Type: EntryConfig}}}) + r.Step(pb.Message{From: 1, To: 1, Type: msgProp, Entries: []pb.Entry{{Type: pb.EntryConfig}}}) index := r.raftLog.lastIndex() pendingConf := r.pendingConf - r.Step(pb.Message{From: 1, To: 1, Type: msgProp, Entries: []pb.Entry{{Type: EntryConfig}}}) + r.Step(pb.Message{From: 1, To: 1, Type: msgProp, Entries: []pb.Entry{{Type: pb.EntryConfig}}}) if g := r.raftLog.lastIndex(); g != index { t.Errorf("index = %d, want %d", g, index) } @@ -989,11 +989,11 @@ func TestStepIgnoreConfig(t *testing.T) { // based on uncommitted entries. func TestRecoverPendingConfig(t *testing.T) { tests := []struct { - entType int64 + entType pb.EntryType wpending bool }{ - {EntryNormal, false}, - {EntryConfig, true}, + {pb.EntryNormal, false}, + {pb.EntryConfig, true}, } for i, tt := range tests { r := newRaft(1, []int64{1, 2}, 0, 0) @@ -1016,8 +1016,8 @@ func TestRecoverDoublePendingConfig(t *testing.T) { } }() r := newRaft(1, []int64{1, 2}, 0, 0) - r.appendEntry(pb.Entry{Type: EntryConfig}) - r.appendEntry(pb.Entry{Type: EntryConfig}) + r.appendEntry(pb.Entry{Type: pb.EntryConfig}) + r.appendEntry(pb.Entry{Type: pb.EntryConfig}) r.becomeCandidate() r.becomeLeader() }() diff --git a/raft/raftpb/raft.pb.go b/raft/raftpb/raft.pb.go index 1f5808f21..c12a97417 100644 --- a/raft/raftpb/raft.pb.go +++ b/raft/raftpb/raft.pb.go @@ -31,6 +31,39 @@ var _ = proto.Marshal var _ = &json.SyntaxError{} var _ = math.Inf +type EntryType int32 + +const ( + EntryNormal EntryType = 0 + EntryConfig EntryType = 1 +) + +var EntryType_name = map[int32]string{ + 0: "EntryNormal", + 1: "EntryConfig", +} +var EntryType_value = map[string]int32{ + "EntryNormal": 0, + "EntryConfig": 1, +} + +func (x EntryType) Enum() *EntryType { + p := new(EntryType) + *p = x + return p +} +func (x EntryType) String() string { + return proto.EnumName(EntryType_name, int32(x)) +} +func (x *EntryType) UnmarshalJSON(data []byte) error { + value, err := proto.UnmarshalJSONEnum(EntryType_value, data, "EntryType") + if err != nil { + return err + } + *x = EntryType(value) + return nil +} + type Info struct { Id int64 `protobuf:"varint,1,req,name=id" json:"id"` XXX_unrecognized []byte `json:"-"` @@ -41,11 +74,11 @@ func (m *Info) String() string { return proto.CompactTextString(m) } func (*Info) ProtoMessage() {} type Entry struct { - Type int64 `protobuf:"varint,1,req,name=type" json:"type"` - Term int64 `protobuf:"varint,2,req,name=term" json:"term"` - Index int64 `protobuf:"varint,3,req,name=index" json:"index"` - Data []byte `protobuf:"bytes,4,opt,name=data" json:"data"` - XXX_unrecognized []byte `json:"-"` + Type EntryType `protobuf:"varint,1,req,enum=raftpb.EntryType" json:"Type"` + Term int64 `protobuf:"varint,2,req" json:"Term"` + Index int64 `protobuf:"varint,3,req" json:"Index"` + Data []byte `protobuf:"bytes,4,opt" json:"Data"` + XXX_unrecognized []byte `json:"-"` } func (m *Entry) Reset() { *m = Entry{} } @@ -93,6 +126,7 @@ func (m *HardState) String() string { return proto.CompactTextString(m) } func (*HardState) ProtoMessage() {} func init() { + proto.RegisterEnum("raftpb.EntryType", EntryType_name, EntryType_value) } func (m *Info) Unmarshal(data []byte) error { l := len(data) @@ -180,7 +214,7 @@ func (m *Entry) Unmarshal(data []byte) error { } b := data[index] index++ - m.Type |= (int64(b) & 0x7F) << shift + m.Type |= (EntryType(b) & 0x7F) << shift if b < 0x80 { break } diff --git a/raft/raftpb/raft.proto b/raft/raftpb/raft.proto index f5249c1a5..6eff384ca 100644 --- a/raft/raftpb/raft.proto +++ b/raft/raftpb/raft.proto @@ -6,16 +6,22 @@ option (gogoproto.marshaler_all) = true; option (gogoproto.sizer_all) = true; option (gogoproto.unmarshaler_all) = true; option (gogoproto.goproto_getters_all) = false; +option (gogoproto.goproto_enum_prefix_all) = false; message Info { required int64 id = 1 [(gogoproto.nullable) = false]; } +enum EntryType { + EntryNormal = 0; + EntryConfig = 1; +} + message Entry { - required int64 type = 1 [(gogoproto.nullable) = false]; - required int64 term = 2 [(gogoproto.nullable) = false]; - required int64 index = 3 [(gogoproto.nullable) = false]; - optional bytes data = 4 [(gogoproto.nullable) = false]; + required EntryType Type = 1 [(gogoproto.nullable) = false]; + required int64 Term = 2 [(gogoproto.nullable) = false]; + required int64 Index = 3 [(gogoproto.nullable) = false]; + optional bytes Data = 4 [(gogoproto.nullable) = false]; } message Snapshot {