cluster.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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, isLearner bool) (*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, isLearner bool) (*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{
  66. PeerURLs: peerAddrs,
  67. IsLearner: isLearner,
  68. }
  69. resp, err := c.remote.MemberAdd(ctx, r, c.callOpts...)
  70. if err != nil {
  71. return nil, toErr(ctx, err)
  72. }
  73. return (*MemberAddResponse)(resp), nil
  74. }
  75. func (c *cluster) MemberRemove(ctx context.Context, id uint64) (*MemberRemoveResponse, error) {
  76. r := &pb.MemberRemoveRequest{ID: id}
  77. resp, err := c.remote.MemberRemove(ctx, r, c.callOpts...)
  78. if err != nil {
  79. return nil, toErr(ctx, err)
  80. }
  81. return (*MemberRemoveResponse)(resp), nil
  82. }
  83. func (c *cluster) MemberUpdate(ctx context.Context, id uint64, peerAddrs []string) (*MemberUpdateResponse, error) {
  84. // fail-fast before panic in rafthttp
  85. if _, err := types.NewURLs(peerAddrs); err != nil {
  86. return nil, err
  87. }
  88. // it is safe to retry on update.
  89. r := &pb.MemberUpdateRequest{ID: id, PeerURLs: peerAddrs}
  90. resp, err := c.remote.MemberUpdate(ctx, r, c.callOpts...)
  91. if err == nil {
  92. return (*MemberUpdateResponse)(resp), nil
  93. }
  94. return nil, toErr(ctx, err)
  95. }
  96. func (c *cluster) MemberList(ctx context.Context) (*MemberListResponse, error) {
  97. // it is safe to retry on list.
  98. resp, err := c.remote.MemberList(ctx, &pb.MemberListRequest{}, c.callOpts...)
  99. if err == nil {
  100. return (*MemberListResponse)(resp), nil
  101. }
  102. return nil, toErr(ctx, err)
  103. }
  104. func (c *cluster) MemberPromote(ctx context.Context, id uint64) (*MemberPromoteResponse, error) {
  105. // TODO: implement
  106. return nil, errors.New("not implemented")
  107. }