cluster.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 grpcproxy
  15. import (
  16. "context"
  17. "errors"
  18. "fmt"
  19. "os"
  20. "sync"
  21. "go.etcd.io/etcd/clientv3"
  22. "go.etcd.io/etcd/clientv3/naming"
  23. "go.etcd.io/etcd/etcdserver/api/v3rpc/rpctypes"
  24. pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
  25. "golang.org/x/time/rate"
  26. gnaming "google.golang.org/grpc/naming"
  27. )
  28. // allow maximum 1 retry per second
  29. const resolveRetryRate = 1
  30. type clusterProxy struct {
  31. clus clientv3.Cluster
  32. ctx context.Context
  33. gr *naming.GRPCResolver
  34. // advertise client URL
  35. advaddr string
  36. prefix string
  37. umu sync.RWMutex
  38. umap map[string]gnaming.Update
  39. }
  40. // NewClusterProxy takes optional prefix to fetch grpc-proxy member endpoints.
  41. // The returned channel is closed when there is grpc-proxy endpoint registered
  42. // and the client's context is canceled so the 'register' loop returns.
  43. func NewClusterProxy(c *clientv3.Client, advaddr string, prefix string) (pb.ClusterServer, <-chan struct{}) {
  44. cp := &clusterProxy{
  45. clus: c.Cluster,
  46. ctx: c.Ctx(),
  47. gr: &naming.GRPCResolver{Client: c},
  48. advaddr: advaddr,
  49. prefix: prefix,
  50. umap: make(map[string]gnaming.Update),
  51. }
  52. donec := make(chan struct{})
  53. if advaddr != "" && prefix != "" {
  54. go func() {
  55. defer close(donec)
  56. cp.resolve(prefix)
  57. }()
  58. return cp, donec
  59. }
  60. close(donec)
  61. return cp, donec
  62. }
  63. func (cp *clusterProxy) resolve(prefix string) {
  64. rm := rate.NewLimiter(rate.Limit(resolveRetryRate), resolveRetryRate)
  65. for rm.Wait(cp.ctx) == nil {
  66. wa, err := cp.gr.Resolve(prefix)
  67. if err != nil {
  68. plog.Warningf("failed to resolve %q (%v)", prefix, err)
  69. continue
  70. }
  71. cp.monitor(wa)
  72. }
  73. }
  74. func (cp *clusterProxy) monitor(wa gnaming.Watcher) {
  75. for cp.ctx.Err() == nil {
  76. ups, err := wa.Next()
  77. if err != nil {
  78. plog.Warningf("clusterProxy watcher error (%v)", err)
  79. if rpctypes.ErrorDesc(err) == naming.ErrWatcherClosed.Error() {
  80. return
  81. }
  82. }
  83. cp.umu.Lock()
  84. for i := range ups {
  85. switch ups[i].Op {
  86. case gnaming.Add:
  87. cp.umap[ups[i].Addr] = *ups[i]
  88. case gnaming.Delete:
  89. delete(cp.umap, ups[i].Addr)
  90. }
  91. }
  92. cp.umu.Unlock()
  93. }
  94. }
  95. func (cp *clusterProxy) MemberAdd(ctx context.Context, r *pb.MemberAddRequest) (*pb.MemberAddResponse, error) {
  96. if r.IsLearner {
  97. return cp.memberAddAsLearner(ctx, r.PeerURLs)
  98. }
  99. return cp.memberAdd(ctx, r.PeerURLs)
  100. }
  101. func (cp *clusterProxy) memberAdd(ctx context.Context, peerURLs []string) (*pb.MemberAddResponse, error) {
  102. mresp, err := cp.clus.MemberAdd(ctx, peerURLs)
  103. if err != nil {
  104. return nil, err
  105. }
  106. resp := (pb.MemberAddResponse)(*mresp)
  107. return &resp, err
  108. }
  109. func (cp *clusterProxy) memberAddAsLearner(ctx context.Context, peerURLs []string) (*pb.MemberAddResponse, error) {
  110. mresp, err := cp.clus.MemberAddAsLearner(ctx, peerURLs)
  111. if err != nil {
  112. return nil, err
  113. }
  114. resp := (pb.MemberAddResponse)(*mresp)
  115. return &resp, err
  116. }
  117. func (cp *clusterProxy) MemberRemove(ctx context.Context, r *pb.MemberRemoveRequest) (*pb.MemberRemoveResponse, error) {
  118. mresp, err := cp.clus.MemberRemove(ctx, r.ID)
  119. if err != nil {
  120. return nil, err
  121. }
  122. resp := (pb.MemberRemoveResponse)(*mresp)
  123. return &resp, err
  124. }
  125. func (cp *clusterProxy) MemberUpdate(ctx context.Context, r *pb.MemberUpdateRequest) (*pb.MemberUpdateResponse, error) {
  126. mresp, err := cp.clus.MemberUpdate(ctx, r.ID, r.PeerURLs)
  127. if err != nil {
  128. return nil, err
  129. }
  130. resp := (pb.MemberUpdateResponse)(*mresp)
  131. return &resp, err
  132. }
  133. func (cp *clusterProxy) membersFromUpdates() ([]*pb.Member, error) {
  134. cp.umu.RLock()
  135. defer cp.umu.RUnlock()
  136. mbs := make([]*pb.Member, 0, len(cp.umap))
  137. for addr, upt := range cp.umap {
  138. m, err := decodeMeta(fmt.Sprint(upt.Metadata))
  139. if err != nil {
  140. return nil, err
  141. }
  142. mbs = append(mbs, &pb.Member{Name: m.Name, ClientURLs: []string{addr}})
  143. }
  144. return mbs, nil
  145. }
  146. // MemberList wraps member list API with following rules:
  147. // - If 'advaddr' is not empty and 'prefix' is not empty, return registered member lists via resolver
  148. // - If 'advaddr' is not empty and 'prefix' is not empty and registered grpc-proxy members haven't been fetched, return the 'advaddr'
  149. // - If 'advaddr' is not empty and 'prefix' is empty, return 'advaddr' without forcing it to 'register'
  150. // - If 'advaddr' is empty, forward to member list API
  151. func (cp *clusterProxy) MemberList(ctx context.Context, r *pb.MemberListRequest) (*pb.MemberListResponse, error) {
  152. if cp.advaddr != "" {
  153. if cp.prefix != "" {
  154. mbs, err := cp.membersFromUpdates()
  155. if err != nil {
  156. return nil, err
  157. }
  158. if len(mbs) > 0 {
  159. return &pb.MemberListResponse{Members: mbs}, nil
  160. }
  161. }
  162. // prefix is empty or no grpc-proxy members haven't been registered
  163. hostname, _ := os.Hostname()
  164. return &pb.MemberListResponse{Members: []*pb.Member{{Name: hostname, ClientURLs: []string{cp.advaddr}}}}, nil
  165. }
  166. mresp, err := cp.clus.MemberList(ctx)
  167. if err != nil {
  168. return nil, err
  169. }
  170. resp := (pb.MemberListResponse)(*mresp)
  171. return &resp, err
  172. }
  173. func (cp *clusterProxy) MemberPromote(ctx context.Context, r *pb.MemberPromoteRequest) (*pb.MemberPromoteResponse, error) {
  174. // TODO: implement
  175. return nil, errors.New("not implemented")
  176. }