member.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. "google.golang.org/grpc"
  25. "google.golang.org/grpc/codes"
  26. )
  27. type ClusterServer struct {
  28. cluster api.Cluster
  29. server etcdserver.Server
  30. raftTimer etcdserver.RaftTimer
  31. }
  32. func NewClusterServer(s *etcdserver.EtcdServer) *ClusterServer {
  33. return &ClusterServer{
  34. cluster: s.Cluster(),
  35. server: s,
  36. raftTimer: s,
  37. }
  38. }
  39. func (cs *ClusterServer) MemberAdd(ctx context.Context, r *pb.MemberAddRequest) (*pb.MemberAddResponse, error) {
  40. urls, err := types.NewURLs(r.PeerURLs)
  41. if err != nil {
  42. return nil, rpctypes.ErrGRPCMemberBadURLs
  43. }
  44. now := time.Now()
  45. m := membership.NewMember("", urls, "", &now)
  46. err = cs.server.AddMember(ctx, *m)
  47. switch {
  48. case err == membership.ErrIDExists:
  49. return nil, rpctypes.ErrGRPCMemberExist
  50. case err == membership.ErrPeerURLexists:
  51. return nil, rpctypes.ErrGRPCPeerURLExist
  52. case err != nil:
  53. return nil, grpc.Errorf(codes.Internal, err.Error())
  54. }
  55. return &pb.MemberAddResponse{
  56. Header: cs.header(),
  57. Member: &pb.Member{ID: uint64(m.ID), PeerURLs: m.PeerURLs},
  58. }, nil
  59. }
  60. func (cs *ClusterServer) MemberRemove(ctx context.Context, r *pb.MemberRemoveRequest) (*pb.MemberRemoveResponse, error) {
  61. err := cs.server.RemoveMember(ctx, r.ID)
  62. switch {
  63. case err == membership.ErrIDRemoved:
  64. fallthrough
  65. case err == membership.ErrIDNotFound:
  66. return nil, rpctypes.ErrGRPCMemberNotFound
  67. case err != nil:
  68. return nil, grpc.Errorf(codes.Internal, err.Error())
  69. }
  70. return &pb.MemberRemoveResponse{Header: cs.header()}, nil
  71. }
  72. func (cs *ClusterServer) MemberUpdate(ctx context.Context, r *pb.MemberUpdateRequest) (*pb.MemberUpdateResponse, error) {
  73. m := membership.Member{
  74. ID: types.ID(r.ID),
  75. RaftAttributes: membership.RaftAttributes{PeerURLs: r.PeerURLs},
  76. }
  77. err := cs.server.UpdateMember(ctx, m)
  78. switch {
  79. case err == membership.ErrPeerURLexists:
  80. return nil, rpctypes.ErrGRPCPeerURLExist
  81. case err == membership.ErrIDNotFound:
  82. return nil, rpctypes.ErrGRPCMemberNotFound
  83. case err != nil:
  84. return nil, grpc.Errorf(codes.Internal, err.Error())
  85. }
  86. return &pb.MemberUpdateResponse{Header: cs.header()}, nil
  87. }
  88. func (cs *ClusterServer) MemberList(ctx context.Context, r *pb.MemberListRequest) (*pb.MemberListResponse, error) {
  89. membs := cs.cluster.Members()
  90. protoMembs := make([]*pb.Member, len(membs))
  91. for i := range membs {
  92. protoMembs[i] = &pb.Member{
  93. Name: membs[i].Name,
  94. ID: uint64(membs[i].ID),
  95. PeerURLs: membs[i].PeerURLs,
  96. ClientURLs: membs[i].ClientURLs,
  97. }
  98. }
  99. return &pb.MemberListResponse{Header: cs.header(), Members: protoMembs}, nil
  100. }
  101. func (cs *ClusterServer) header() *pb.ResponseHeader {
  102. return &pb.ResponseHeader{ClusterId: uint64(cs.cluster.ID()), MemberId: uint64(cs.server.ID()), RaftTerm: cs.raftTimer.Term()}
  103. }