cluster.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 nil, toErr(ctx, err)
  48. }
  49. return (*MemberAddResponse)(resp), nil
  50. }
  51. func (c *cluster) MemberRemove(ctx context.Context, id uint64) (*MemberRemoveResponse, error) {
  52. r := &pb.MemberRemoveRequest{ID: id}
  53. resp, err := c.remote.MemberRemove(ctx, r)
  54. if err != nil {
  55. return nil, toErr(ctx, err)
  56. }
  57. return (*MemberRemoveResponse)(resp), nil
  58. }
  59. func (c *cluster) MemberUpdate(ctx context.Context, id uint64, peerAddrs []string) (*MemberUpdateResponse, error) {
  60. // it is safe to retry on update.
  61. for {
  62. r := &pb.MemberUpdateRequest{ID: id, PeerURLs: peerAddrs}
  63. resp, err := c.remote.MemberUpdate(ctx, r, grpc.FailFast(false))
  64. if err == nil {
  65. return (*MemberUpdateResponse)(resp), nil
  66. }
  67. if isHaltErr(ctx, err) {
  68. return nil, toErr(ctx, err)
  69. }
  70. }
  71. }
  72. func (c *cluster) MemberList(ctx context.Context) (*MemberListResponse, error) {
  73. // it is safe to retry on list.
  74. for {
  75. resp, err := c.remote.MemberList(ctx, &pb.MemberListRequest{}, grpc.FailFast(false))
  76. if err == nil {
  77. return (*MemberListResponse)(resp), nil
  78. }
  79. if isHaltErr(ctx, err) {
  80. return nil, toErr(ctx, err)
  81. }
  82. }
  83. }