2016-06-24 23:44:52 +03:00
|
|
|
// Copyright 2016 The etcd Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// +build cluster_proxy
|
|
|
|
|
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
2016-08-17 05:43:31 +03:00
|
|
|
"sync"
|
|
|
|
|
2016-06-24 23:44:52 +03:00
|
|
|
"github.com/coreos/etcd/clientv3"
|
2017-03-21 23:23:26 +03:00
|
|
|
"github.com/coreos/etcd/clientv3/namespace"
|
2016-06-24 23:44:52 +03:00
|
|
|
"github.com/coreos/etcd/proxy/grpcproxy"
|
2017-03-10 07:39:47 +03:00
|
|
|
"github.com/coreos/etcd/proxy/grpcproxy/adapter"
|
2016-06-24 23:44:52 +03:00
|
|
|
)
|
|
|
|
|
2016-08-17 05:43:31 +03:00
|
|
|
var (
|
|
|
|
pmu sync.Mutex
|
2016-12-30 22:24:23 +03:00
|
|
|
proxies map[*clientv3.Client]grpcClientProxy = make(map[*clientv3.Client]grpcClientProxy)
|
2016-08-17 05:43:31 +03:00
|
|
|
)
|
2016-07-23 02:51:43 +03:00
|
|
|
|
2017-03-21 23:23:26 +03:00
|
|
|
const proxyNamespace = "proxy-namespace"
|
|
|
|
|
2016-12-30 22:24:23 +03:00
|
|
|
type grpcClientProxy struct {
|
2017-01-14 03:20:09 +03:00
|
|
|
grpc grpcAPI
|
|
|
|
wdonec <-chan struct{}
|
|
|
|
kvdonec <-chan struct{}
|
2017-02-14 00:38:46 +03:00
|
|
|
lpdonec <-chan struct{}
|
2016-12-30 22:24:23 +03:00
|
|
|
}
|
|
|
|
|
2016-06-24 23:44:52 +03:00
|
|
|
func toGRPC(c *clientv3.Client) grpcAPI {
|
2016-08-17 05:43:31 +03:00
|
|
|
pmu.Lock()
|
|
|
|
defer pmu.Unlock()
|
|
|
|
|
2016-07-23 02:51:43 +03:00
|
|
|
if v, ok := proxies[c]; ok {
|
2016-12-30 22:24:23 +03:00
|
|
|
return v.grpc
|
2016-07-23 02:51:43 +03:00
|
|
|
}
|
2017-03-21 23:23:26 +03:00
|
|
|
|
|
|
|
// test namespacing proxy
|
|
|
|
c.KV = namespace.NewKV(c.KV, proxyNamespace)
|
|
|
|
c.Watcher = namespace.NewWatcher(c.Watcher, proxyNamespace)
|
|
|
|
c.Lease = namespace.NewLease(c.Lease, proxyNamespace)
|
|
|
|
// test coalescing/caching proxy
|
2017-01-14 03:20:09 +03:00
|
|
|
kvp, kvpch := grpcproxy.NewKvProxy(c)
|
2017-02-14 00:38:46 +03:00
|
|
|
wp, wpch := grpcproxy.NewWatchProxy(c)
|
|
|
|
lp, lpch := grpcproxy.NewLeaseProxy(c)
|
2017-03-28 00:30:47 +03:00
|
|
|
mp := grpcproxy.NewMaintenanceProxy(c)
|
2017-04-04 18:14:54 +03:00
|
|
|
clp, _ := grpcproxy.NewClusterProxy(c, "", "") // without registering proxy URLs
|
2017-06-09 08:03:40 +03:00
|
|
|
authp := grpcproxy.NewAuthProxy(c)
|
2017-05-10 22:53:39 +03:00
|
|
|
lockp := grpcproxy.NewLockProxy(c)
|
|
|
|
electp := grpcproxy.NewElectionProxy(c)
|
2017-03-21 23:23:26 +03:00
|
|
|
|
2016-12-30 22:24:23 +03:00
|
|
|
grpc := grpcAPI{
|
2017-04-04 18:14:54 +03:00
|
|
|
adapter.ClusterServerToClusterClient(clp),
|
2017-03-10 07:39:47 +03:00
|
|
|
adapter.KvServerToKvClient(kvp),
|
|
|
|
adapter.LeaseServerToLeaseClient(lp),
|
|
|
|
adapter.WatchServerToWatchClient(wp),
|
2017-03-28 00:30:47 +03:00
|
|
|
adapter.MaintenanceServerToMaintenanceClient(mp),
|
2017-06-09 08:03:40 +03:00
|
|
|
adapter.AuthServerToAuthClient(authp),
|
2017-05-10 22:53:39 +03:00
|
|
|
adapter.LockServerToLockClient(lockp),
|
|
|
|
adapter.ElectionServerToElectionClient(electp),
|
2016-06-24 23:44:52 +03:00
|
|
|
}
|
2017-02-14 00:38:46 +03:00
|
|
|
proxies[c] = grpcClientProxy{grpc: grpc, wdonec: wpch, kvdonec: kvpch, lpdonec: lpch}
|
2016-12-30 22:24:23 +03:00
|
|
|
return grpc
|
|
|
|
}
|
|
|
|
|
2017-01-14 03:20:09 +03:00
|
|
|
type proxyCloser struct {
|
2016-12-30 22:24:23 +03:00
|
|
|
clientv3.Watcher
|
2017-01-14 03:20:09 +03:00
|
|
|
wdonec <-chan struct{}
|
|
|
|
kvdonec <-chan struct{}
|
2017-04-25 08:33:56 +03:00
|
|
|
lclose func()
|
2017-02-14 00:38:46 +03:00
|
|
|
lpdonec <-chan struct{}
|
2016-12-30 22:24:23 +03:00
|
|
|
}
|
|
|
|
|
2017-01-14 03:20:09 +03:00
|
|
|
func (pc *proxyCloser) Close() error {
|
2017-02-14 00:38:46 +03:00
|
|
|
// client ctx is canceled before calling close, so kv and lp will close out
|
2017-01-14 03:20:09 +03:00
|
|
|
<-pc.kvdonec
|
|
|
|
err := pc.Watcher.Close()
|
|
|
|
<-pc.wdonec
|
2017-04-25 08:33:56 +03:00
|
|
|
pc.lclose()
|
2017-02-14 00:38:46 +03:00
|
|
|
<-pc.lpdonec
|
2016-12-30 22:24:23 +03:00
|
|
|
return err
|
2016-06-24 23:44:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func newClientV3(cfg clientv3.Config) (*clientv3.Client, error) {
|
|
|
|
c, err := clientv3.New(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-09-02 01:21:52 +03:00
|
|
|
rpc := toGRPC(c)
|
2017-12-20 02:06:57 +03:00
|
|
|
c.KV = clientv3.NewKVFromKVClient(rpc.KV, c)
|
2016-12-30 22:24:23 +03:00
|
|
|
pmu.Lock()
|
2017-04-25 08:33:56 +03:00
|
|
|
lc := c.Lease
|
2017-12-20 02:07:32 +03:00
|
|
|
c.Lease = clientv3.NewLeaseFromLeaseClient(rpc.Lease, c, cfg.DialTimeout)
|
2017-01-14 03:20:09 +03:00
|
|
|
c.Watcher = &proxyCloser{
|
2017-12-20 02:07:32 +03:00
|
|
|
Watcher: clientv3.NewWatchFromWatchClient(rpc.Watch, c),
|
2016-12-30 22:24:23 +03:00
|
|
|
wdonec: proxies[c].wdonec,
|
2017-01-14 03:20:09 +03:00
|
|
|
kvdonec: proxies[c].kvdonec,
|
2017-04-25 08:33:56 +03:00
|
|
|
lclose: func() { lc.Close() },
|
2017-02-14 00:38:46 +03:00
|
|
|
lpdonec: proxies[c].lpdonec,
|
2016-12-30 22:24:23 +03:00
|
|
|
}
|
|
|
|
pmu.Unlock()
|
2016-06-24 23:44:52 +03:00
|
|
|
return c, nil
|
|
|
|
}
|