cluster.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright 2016 CoreOS, Inc.
  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. "sync"
  17. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  18. "github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc"
  19. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  20. )
  21. type (
  22. Member pb.Member
  23. MemberListResponse pb.MemberListResponse
  24. MemberAddResponse pb.MemberAddResponse
  25. MemberRemoveResponse pb.MemberRemoveResponse
  26. MemberUpdateResponse pb.MemberUpdateResponse
  27. )
  28. type Cluster interface {
  29. // MemberList lists the current cluster membership.
  30. MemberList(ctx context.Context) (*MemberListResponse, error)
  31. // MemberLeader returns the current leader member.
  32. MemberLeader(ctx context.Context) (*Member, 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. }
  40. type cluster struct {
  41. c *Client
  42. mu sync.Mutex
  43. conn *grpc.ClientConn // conn in-use
  44. remote pb.ClusterClient
  45. }
  46. func NewCluster(c *Client) Cluster {
  47. conn := c.ActiveConnection()
  48. return &cluster{
  49. c: c,
  50. conn: conn,
  51. remote: pb.NewClusterClient(conn),
  52. }
  53. }
  54. func (c *cluster) MemberAdd(ctx context.Context, peerAddrs []string) (*MemberAddResponse, error) {
  55. r := &pb.MemberAddRequest{PeerURLs: peerAddrs}
  56. resp, err := c.getRemote().MemberAdd(ctx, r)
  57. if err == nil {
  58. return (*MemberAddResponse)(resp), nil
  59. }
  60. if isHalted(ctx, err) {
  61. return nil, err
  62. }
  63. go c.switchRemote(err)
  64. return nil, err
  65. }
  66. func (c *cluster) MemberRemove(ctx context.Context, id uint64) (*MemberRemoveResponse, error) {
  67. r := &pb.MemberRemoveRequest{ID: id}
  68. resp, err := c.getRemote().MemberRemove(ctx, r)
  69. if err == nil {
  70. return (*MemberRemoveResponse)(resp), nil
  71. }
  72. if isHalted(ctx, err) {
  73. return nil, err
  74. }
  75. go c.switchRemote(err)
  76. return nil, err
  77. }
  78. func (c *cluster) MemberUpdate(ctx context.Context, id uint64, peerAddrs []string) (*MemberUpdateResponse, error) {
  79. // it is safe to retry on update.
  80. for {
  81. r := &pb.MemberUpdateRequest{ID: id, PeerURLs: peerAddrs}
  82. resp, err := c.getRemote().MemberUpdate(ctx, r)
  83. if err == nil {
  84. return (*MemberUpdateResponse)(resp), nil
  85. }
  86. if isHalted(ctx, err) {
  87. return nil, err
  88. }
  89. err = c.switchRemote(err)
  90. if err != nil {
  91. return nil, err
  92. }
  93. }
  94. }
  95. func (c *cluster) MemberList(ctx context.Context) (*MemberListResponse, error) {
  96. // it is safe to retry on list.
  97. for {
  98. resp, err := c.getRemote().MemberList(ctx, &pb.MemberListRequest{})
  99. if err == nil {
  100. return (*MemberListResponse)(resp), nil
  101. }
  102. if isHalted(ctx, err) {
  103. return nil, err
  104. }
  105. err = c.switchRemote(err)
  106. if err != nil {
  107. return nil, err
  108. }
  109. }
  110. }
  111. func (c *cluster) MemberLeader(ctx context.Context) (*Member, error) {
  112. resp, err := c.MemberList(ctx)
  113. if err != nil {
  114. return nil, err
  115. }
  116. for _, m := range resp.Members {
  117. if m.IsLeader {
  118. return (*Member)(m), nil
  119. }
  120. }
  121. return nil, nil
  122. }
  123. func (c *cluster) getRemote() pb.ClusterClient {
  124. c.mu.Lock()
  125. defer c.mu.Unlock()
  126. return c.remote
  127. }
  128. func (c *cluster) switchRemote(prevErr error) error {
  129. newConn, err := c.c.retryConnection(c.conn, prevErr)
  130. if err != nil {
  131. return err
  132. }
  133. c.mu.Lock()
  134. defer c.mu.Unlock()
  135. c.conn = newConn
  136. c.remote = pb.NewClusterClient(c.conn)
  137. return nil
  138. }