cluster.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 clientv3
  15. import (
  16. "context"
  17. pb "go.etcd.io/etcd/v3/etcdserver/etcdserverpb"
  18. "go.etcd.io/etcd/v3/pkg/types"
  19. "errors"
  20. "google.golang.org/grpc"
  21. )
  22. type (
  23. Member pb.Member
  24. MemberListResponse pb.MemberListResponse
  25. MemberAddResponse pb.MemberAddResponse
  26. MemberRemoveResponse pb.MemberRemoveResponse
  27. MemberUpdateResponse pb.MemberUpdateResponse
  28. MemberPromoteResponse pb.MemberPromoteResponse
  29. )
  30. type Cluster interface {
  31. // MemberList lists the current cluster membership.
  32. MemberList(ctx context.Context) (*MemberListResponse, error)
  33. // MemberAdd adds a new member into the cluster.
  34. MemberAdd(ctx context.Context, peerAddrs []string) (*MemberAddResponse, error)
  35. // MemberRemove removes an existing member from the cluster.
  36. MemberRemove(ctx context.Context, id uint64) (*MemberRemoveResponse, error)
  37. // MemberUpdate updates the peer addresses of the member.
  38. MemberUpdate(ctx context.Context, id uint64, peerAddrs []string) (*MemberUpdateResponse, error)
  39. // MemberPromote promotes a member from raft learner (non-voting) to raft voting member.
  40. MemberPromote(ctx context.Context, id uint64) (*MemberPromoteResponse, error)
  41. }
  42. type cluster struct {
  43. remote pb.ClusterClient
  44. callOpts []grpc.CallOption
  45. }
  46. func NewCluster(c *Client) Cluster {
  47. api := &cluster{remote: RetryClusterClient(c)}
  48. if c != nil {
  49. api.callOpts = c.callOpts
  50. }
  51. return api
  52. }
  53. func NewClusterFromClusterClient(remote pb.ClusterClient, c *Client) Cluster {
  54. api := &cluster{remote: remote}
  55. if c != nil {
  56. api.callOpts = c.callOpts
  57. }
  58. return api
  59. }
  60. func (c *cluster) MemberAdd(ctx context.Context, peerAddrs []string) (*MemberAddResponse, error) {
  61. // fail-fast before panic in rafthttp
  62. if _, err := types.NewURLs(peerAddrs); err != nil {
  63. return nil, err
  64. }
  65. r := &pb.MemberAddRequest{PeerURLs: peerAddrs}
  66. resp, err := c.remote.MemberAdd(ctx, r, c.callOpts...)
  67. if err != nil {
  68. return nil, toErr(ctx, err)
  69. }
  70. return (*MemberAddResponse)(resp), nil
  71. }
  72. func (c *cluster) MemberRemove(ctx context.Context, id uint64) (*MemberRemoveResponse, error) {
  73. r := &pb.MemberRemoveRequest{ID: id}
  74. resp, err := c.remote.MemberRemove(ctx, r, c.callOpts...)
  75. if err != nil {
  76. return nil, toErr(ctx, err)
  77. }
  78. return (*MemberRemoveResponse)(resp), nil
  79. }
  80. func (c *cluster) MemberUpdate(ctx context.Context, id uint64, peerAddrs []string) (*MemberUpdateResponse, error) {
  81. // fail-fast before panic in rafthttp
  82. if _, err := types.NewURLs(peerAddrs); err != nil {
  83. return nil, err
  84. }
  85. // it is safe to retry on update.
  86. r := &pb.MemberUpdateRequest{ID: id, PeerURLs: peerAddrs}
  87. resp, err := c.remote.MemberUpdate(ctx, r, c.callOpts...)
  88. if err == nil {
  89. return (*MemberUpdateResponse)(resp), nil
  90. }
  91. return nil, toErr(ctx, err)
  92. }
  93. func (c *cluster) MemberList(ctx context.Context) (*MemberListResponse, error) {
  94. // it is safe to retry on list.
  95. resp, err := c.remote.MemberList(ctx, &pb.MemberListRequest{}, c.callOpts...)
  96. if err == nil {
  97. return (*MemberListResponse)(resp), nil
  98. }
  99. return nil, toErr(ctx, err)
  100. }
  101. func (c *cluster) MemberPromote(ctx context.Context, id uint64) (*MemberPromoteResponse, error) {
  102. // TODO: implement
  103. return nil, errors.New("not implemented")
  104. }