Browse Source

etcdserver: use ConfigType in protobuf

Yicheng Qin 11 years ago
parent
commit
4203569da2

+ 40 - 6
etcdserver/etcdserverpb/etcdserver.pb.go

@@ -28,6 +28,39 @@ var _ = proto.Marshal
 var _ = &json.SyntaxError{}
 var _ = math.Inf
 
+type ConfigType int32
+
+const (
+	ConfigAddNode    ConfigType = 0
+	ConfigRemoveNode ConfigType = 1
+)
+
+var ConfigType_name = map[int32]string{
+	0: "ConfigAddNode",
+	1: "ConfigRemoveNode",
+}
+var ConfigType_value = map[string]int32{
+	"ConfigAddNode":    0,
+	"ConfigRemoveNode": 1,
+}
+
+func (x ConfigType) Enum() *ConfigType {
+	p := new(ConfigType)
+	*p = x
+	return p
+}
+func (x ConfigType) String() string {
+	return proto.EnumName(ConfigType_name, int32(x))
+}
+func (x *ConfigType) UnmarshalJSON(data []byte) error {
+	value, err := proto.UnmarshalJSONEnum(ConfigType_value, data, "ConfigType")
+	if err != nil {
+		return err
+	}
+	*x = ConfigType(value)
+	return nil
+}
+
 type Request struct {
 	Id               int64  `protobuf:"varint,1,req,name=id" json:"id"`
 	Method           string `protobuf:"bytes,2,req,name=method" json:"method"`
@@ -52,11 +85,11 @@ func (m *Request) String() string { return proto.CompactTextString(m) }
 func (*Request) ProtoMessage()    {}
 
 type Config struct {
-	ID               int64  `protobuf:"varint,1,req" json:"ID"`
-	Type             int64  `protobuf:"varint,2,req" json:"Type"`
-	NodeID           int64  `protobuf:"varint,3,req" json:"NodeID"`
-	Context          []byte `protobuf:"bytes,4,opt" json:"Context"`
-	XXX_unrecognized []byte `json:"-"`
+	ID               int64      `protobuf:"varint,1,req" json:"ID"`
+	Type             ConfigType `protobuf:"varint,2,req,enum=etcdserverpb.ConfigType" json:"Type"`
+	NodeID           int64      `protobuf:"varint,3,req" json:"NodeID"`
+	Context          []byte     `protobuf:"bytes,4,opt" json:"Context"`
+	XXX_unrecognized []byte     `json:"-"`
 }
 
 func (m *Config) Reset()         { *m = Config{} }
@@ -64,6 +97,7 @@ func (m *Config) String() string { return proto.CompactTextString(m) }
 func (*Config) ProtoMessage()    {}
 
 func init() {
+	proto.RegisterEnum("etcdserverpb.ConfigType", ConfigType_name, ConfigType_value)
 }
 func (m *Request) Unmarshal(data []byte) error {
 	l := len(data)
@@ -417,7 +451,7 @@ func (m *Config) Unmarshal(data []byte) error {
 				}
 				b := data[index]
 				index++
-				m.Type |= (int64(b) & 0x7F) << shift
+				m.Type |= (ConfigType(b) & 0x7F) << shift
 				if b < 0x80 {
 					break
 				}

+ 10 - 4
etcdserver/etcdserverpb/etcdserver.proto

@@ -6,6 +6,7 @@ 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 Request {
 	required int64  id         =  1 [(gogoproto.nullable) = false];
@@ -25,9 +26,14 @@ message Request {
 	required int64  time       = 15 [(gogoproto.nullable) = false];
 }
 
+enum ConfigType {
+	ConfigAddNode    = 0;
+	ConfigRemoveNode = 1;
+}
+
 message Config {
-	required int64 ID      = 1 [(gogoproto.nullable) = false];
-	required int64 Type    = 2 [(gogoproto.nullable) = false];
-	required int64 NodeID  = 3 [(gogoproto.nullable) = false];
-	optional bytes Context = 4 [(gogoproto.nullable) = false];
+	required int64      ID      = 1 [(gogoproto.nullable) = false];
+	required ConfigType Type    = 2 [(gogoproto.nullable) = false];
+	required int64      NodeID  = 3 [(gogoproto.nullable) = false];
+	optional bytes      Context = 4 [(gogoproto.nullable) = false];
 }

+ 4 - 9
etcdserver/server.go

@@ -19,11 +19,6 @@ const (
 	DefaultSnapCount   = 10000
 )
 
-const (
-	configAddNode int64 = iota
-	configRemoveNode
-)
-
 var (
 	ErrUnknownMethod = errors.New("etcdserver: unknown method")
 	ErrStopped       = errors.New("etcdserver: server stopped")
@@ -238,7 +233,7 @@ func (s *EtcdServer) Do(ctx context.Context, r pb.Request) (Response, error) {
 func (s *EtcdServer) AddNode(ctx context.Context, id int64, context []byte) error {
 	req := pb.Config{
 		ID:      GenID(),
-		Type:    configAddNode,
+		Type:    pb.ConfigAddNode,
 		NodeID:  id,
 		Context: context,
 	}
@@ -248,7 +243,7 @@ func (s *EtcdServer) AddNode(ctx context.Context, id int64, context []byte) erro
 func (s *EtcdServer) RemoveNode(ctx context.Context, id int64) error {
 	req := pb.Config{
 		ID:     GenID(),
-		Type:   configRemoveNode,
+		Type:   pb.ConfigRemoveNode,
 		NodeID: id,
 	}
 	return s.configure(ctx, req)
@@ -349,9 +344,9 @@ func (s *EtcdServer) applyRequest(r pb.Request) Response {
 
 func (s *EtcdServer) applyConfig(r pb.Config) {
 	switch r.Type {
-	case configAddNode:
+	case pb.ConfigAddNode:
 		s.Node.AddNode(r.NodeID)
-	case configRemoveNode:
+	case pb.ConfigRemoveNode:
 		s.Node.RemoveNode(r.NodeID)
 	default:
 		// This should never be reached