cluster.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  17. "golang.org/x/net/context"
  18. "google.golang.org/grpc"
  19. )
  20. type (
  21. Member pb.Member
  22. MemberListResponse pb.MemberListResponse
  23. MemberAddResponse pb.MemberAddResponse
  24. MemberRemoveResponse pb.MemberRemoveResponse
  25. MemberUpdateResponse pb.MemberUpdateResponse
  26. )
  27. type Cluster interface {
  28. // MemberList lists the current cluster membership.
  29. MemberList(ctx context.Context) (*MemberListResponse, error)
  30. // MemberAdd adds a new member into the cluster.
  31. MemberAdd(ctx context.Context, peerAddrs []string) (*MemberAddResponse, error)
  32. // MemberRemove removes an existing member from the cluster.
  33. MemberRemove(ctx context.Context, id uint64) (*MemberRemoveResponse, error)
  34. // MemberUpdate updates the peer addresses of the member.
  35. MemberUpdate(ctx context.Context, id uint64, peerAddrs []string) (*MemberUpdateResponse, error)
  36. }
  37. type cluster struct {
  38. remote pb.ClusterClient
  39. }
  40. func NewCluster(c *Client) Cluster {
  41. return &cluster{remote: RetryClusterClient(c)}
  42. }
  43. func (c *cluster) MemberAdd(ctx context.Context, peerAddrs []string) (*MemberAddResponse, error) {
  44. r := &pb.MemberAddRequest{PeerURLs: peerAddrs}
  45. resp, err := c.remote.MemberAdd(ctx, r)
  46. if err == nil {
  47. return (*MemberAddResponse)(resp), nil
  48. }
  49. if isHaltErr(ctx, err) {
  50. return nil, toErr(ctx, err)
  51. }
  52. return nil, toErr(ctx, err)
  53. }
  54. func (c *cluster) MemberRemove(ctx context.Context, id uint64) (*MemberRemoveResponse, error) {
  55. r := &pb.MemberRemoveRequest{ID: id}
  56. resp, err := c.remote.MemberRemove(ctx, r)
  57. if err == nil {
  58. return (*MemberRemoveResponse)(resp), nil
  59. }
  60. if isHaltErr(ctx, err) {
  61. return nil, toErr(ctx, err)
  62. }
  63. return nil, toErr(ctx, err)
  64. }
  65. func (c *cluster) MemberUpdate(ctx context.Context, id uint64, peerAddrs []string) (*MemberUpdateResponse, error) {
  66. // it is safe to retry on update.
  67. for {
  68. r := &pb.MemberUpdateRequest{ID: id, PeerURLs: peerAddrs}
  69. resp, err := c.remote.MemberUpdate(ctx, r)
  70. if err == nil {
  71. return (*MemberUpdateResponse)(resp), nil
  72. }
  73. if isHaltErr(ctx, err) {
  74. return nil, toErr(ctx, err)
  75. }
  76. }
  77. }
  78. func (c *cluster) MemberList(ctx context.Context) (*MemberListResponse, error) {
  79. // it is safe to retry on list.
  80. for {
  81. resp, err := c.remote.MemberList(ctx, &pb.MemberListRequest{}, grpc.FailFast(false))
  82. if err == nil {
  83. return (*MemberListResponse)(resp), nil
  84. }
  85. if isHaltErr(ctx, err) {
  86. return nil, toErr(ctx, err)
  87. }
  88. }
  89. }