member.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package v3rpc
  15. import (
  16. "time"
  17. "github.com/coreos/etcd/etcdserver"
  18. "github.com/coreos/etcd/etcdserver/api"
  19. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  20. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  21. "github.com/coreos/etcd/etcdserver/membership"
  22. "github.com/coreos/etcd/pkg/types"
  23. "golang.org/x/net/context"
  24. )
  25. type ClusterServer struct {
  26. cluster api.Cluster
  27. server etcdserver.Server
  28. raftTimer etcdserver.RaftTimer
  29. }
  30. func NewClusterServer(s *etcdserver.EtcdServer) *ClusterServer {
  31. return &ClusterServer{
  32. cluster: s.Cluster(),
  33. server: s,
  34. raftTimer: s,
  35. }
  36. }
  37. func (cs *ClusterServer) MemberAdd(ctx context.Context, r *pb.MemberAddRequest) (*pb.MemberAddResponse, error) {
  38. urls, err := types.NewURLs(r.PeerURLs)
  39. if err != nil {
  40. return nil, rpctypes.ErrGRPCMemberBadURLs
  41. }
  42. now := time.Now()
  43. m := membership.NewMember("", urls, "", &now)
  44. membs, merr := cs.server.AddMember(ctx, *m)
  45. if merr != nil {
  46. return nil, togRPCError(merr)
  47. }
  48. return &pb.MemberAddResponse{
  49. Header: cs.header(),
  50. Member: &pb.Member{ID: uint64(m.ID), PeerURLs: m.PeerURLs},
  51. Members: membersToProtoMembers(membs),
  52. }, nil
  53. }
  54. func (cs *ClusterServer) MemberRemove(ctx context.Context, r *pb.MemberRemoveRequest) (*pb.MemberRemoveResponse, error) {
  55. membs, err := cs.server.RemoveMember(ctx, r.ID)
  56. if err != nil {
  57. return nil, togRPCError(err)
  58. }
  59. return &pb.MemberRemoveResponse{Header: cs.header(), Members: membersToProtoMembers(membs)}, nil
  60. }
  61. func (cs *ClusterServer) MemberUpdate(ctx context.Context, r *pb.MemberUpdateRequest) (*pb.MemberUpdateResponse, error) {
  62. m := membership.Member{
  63. ID: types.ID(r.ID),
  64. RaftAttributes: membership.RaftAttributes{PeerURLs: r.PeerURLs},
  65. }
  66. membs, err := cs.server.UpdateMember(ctx, m)
  67. if err != nil {
  68. return nil, togRPCError(err)
  69. }
  70. return &pb.MemberUpdateResponse{Header: cs.header(), Members: membersToProtoMembers(membs)}, nil
  71. }
  72. func (cs *ClusterServer) MemberList(ctx context.Context, r *pb.MemberListRequest) (*pb.MemberListResponse, error) {
  73. membs := membersToProtoMembers(cs.cluster.Members())
  74. return &pb.MemberListResponse{Header: cs.header(), Members: membs}, nil
  75. }
  76. func (cs *ClusterServer) header() *pb.ResponseHeader {
  77. return &pb.ResponseHeader{ClusterId: uint64(cs.cluster.ID()), MemberId: uint64(cs.server.ID()), RaftTerm: cs.raftTimer.Term()}
  78. }
  79. func membersToProtoMembers(membs []*membership.Member) []*pb.Member {
  80. protoMembs := make([]*pb.Member, len(membs))
  81. for i := range membs {
  82. protoMembs[i] = &pb.Member{
  83. Name: membs[i].Name,
  84. ID: uint64(membs[i].ID),
  85. PeerURLs: membs[i].PeerURLs,
  86. ClientURLs: membs[i].ClientURLs,
  87. }
  88. }
  89. return protoMembs
  90. }