cluster.go 4.3 KB

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