cluster_proxy.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. // +build cluster_proxy
  15. package integration
  16. import (
  17. "sync"
  18. "go.etcd.io/etcd/clientv3"
  19. "go.etcd.io/etcd/clientv3/namespace"
  20. "go.etcd.io/etcd/proxy/grpcproxy"
  21. "go.etcd.io/etcd/proxy/grpcproxy/adapter"
  22. )
  23. var (
  24. pmu sync.Mutex
  25. proxies map[*clientv3.Client]grpcClientProxy = make(map[*clientv3.Client]grpcClientProxy)
  26. )
  27. const proxyNamespace = "proxy-namespace"
  28. type grpcClientProxy struct {
  29. grpc grpcAPI
  30. wdonec <-chan struct{}
  31. kvdonec <-chan struct{}
  32. lpdonec <-chan struct{}
  33. }
  34. func toGRPC(c *clientv3.Client) grpcAPI {
  35. pmu.Lock()
  36. defer pmu.Unlock()
  37. if v, ok := proxies[c]; ok {
  38. return v.grpc
  39. }
  40. // test namespacing proxy
  41. c.KV = namespace.NewKV(c.KV, proxyNamespace)
  42. c.Watcher = namespace.NewWatcher(c.Watcher, proxyNamespace)
  43. c.Lease = namespace.NewLease(c.Lease, proxyNamespace)
  44. // test coalescing/caching proxy
  45. kvp, kvpch := grpcproxy.NewKvProxy(c)
  46. wp, wpch := grpcproxy.NewWatchProxy(c)
  47. lp, lpch := grpcproxy.NewLeaseProxy(c)
  48. mp := grpcproxy.NewMaintenanceProxy(c)
  49. clp, _ := grpcproxy.NewClusterProxy(c, "", "") // without registering proxy URLs
  50. authp := grpcproxy.NewAuthProxy(c)
  51. lockp := grpcproxy.NewLockProxy(c)
  52. electp := grpcproxy.NewElectionProxy(c)
  53. grpc := grpcAPI{
  54. adapter.ClusterServerToClusterClient(clp),
  55. adapter.KvServerToKvClient(kvp),
  56. adapter.LeaseServerToLeaseClient(lp),
  57. adapter.WatchServerToWatchClient(wp),
  58. adapter.MaintenanceServerToMaintenanceClient(mp),
  59. adapter.AuthServerToAuthClient(authp),
  60. adapter.LockServerToLockClient(lockp),
  61. adapter.ElectionServerToElectionClient(electp),
  62. }
  63. proxies[c] = grpcClientProxy{grpc: grpc, wdonec: wpch, kvdonec: kvpch, lpdonec: lpch}
  64. return grpc
  65. }
  66. type proxyCloser struct {
  67. clientv3.Watcher
  68. wdonec <-chan struct{}
  69. kvdonec <-chan struct{}
  70. lclose func()
  71. lpdonec <-chan struct{}
  72. }
  73. func (pc *proxyCloser) Close() error {
  74. // client ctx is canceled before calling close, so kv and lp will close out
  75. <-pc.kvdonec
  76. err := pc.Watcher.Close()
  77. <-pc.wdonec
  78. pc.lclose()
  79. <-pc.lpdonec
  80. return err
  81. }
  82. func newClientV3(cfg clientv3.Config) (*clientv3.Client, error) {
  83. c, err := clientv3.New(cfg)
  84. if err != nil {
  85. return nil, err
  86. }
  87. rpc := toGRPC(c)
  88. c.KV = clientv3.NewKVFromKVClient(rpc.KV, c)
  89. pmu.Lock()
  90. lc := c.Lease
  91. c.Lease = clientv3.NewLeaseFromLeaseClient(rpc.Lease, c, cfg.DialTimeout)
  92. c.Watcher = &proxyCloser{
  93. Watcher: clientv3.NewWatchFromWatchClient(rpc.Watch, c),
  94. wdonec: proxies[c].wdonec,
  95. kvdonec: proxies[c].kvdonec,
  96. lclose: func() { lc.Close() },
  97. lpdonec: proxies[c].lpdonec,
  98. }
  99. pmu.Unlock()
  100. return c, nil
  101. }