Merge pull request #14170 from ahrtr/3.4_proxy_fix_20220628

Fix deadlock in 'go test -tags cluster_proxy -v ./integration/... ./client'
This commit is contained in:
Marek Siarkowicz 2022-06-28 17:56:44 +02:00 committed by GitHub
commit f1c59dcfac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 28 deletions

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package naming package naming_test
import ( import (
"context" "context"
@ -21,6 +21,7 @@ import (
"testing" "testing"
etcd "go.etcd.io/etcd/clientv3" etcd "go.etcd.io/etcd/clientv3"
namingv3 "go.etcd.io/etcd/clientv3/naming"
"go.etcd.io/etcd/integration" "go.etcd.io/etcd/integration"
"go.etcd.io/etcd/pkg/testutil" "go.etcd.io/etcd/pkg/testutil"
@ -33,7 +34,7 @@ func TestGRPCResolver(t *testing.T) {
clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1}) clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
defer clus.Terminate(t) defer clus.Terminate(t)
r := GRPCResolver{ r := namingv3.GRPCResolver{
Client: clus.RandClient(), Client: clus.RandClient(),
} }
@ -107,7 +108,7 @@ func TestGRPCResolverMulti(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
r := GRPCResolver{c} r := namingv3.GRPCResolver{c}
w, err := r.Resolve("foo") w, err := r.Resolve("foo")
if err != nil { if err != nil {

View File

@ -350,12 +350,12 @@ func newGRPCProxyServer(lg *zap.Logger, client *clientv3.Client) *grpc.Server {
} }
kvp, _ := grpcproxy.NewKvProxy(client) kvp, _ := grpcproxy.NewKvProxy(client)
watchp, _ := grpcproxy.NewWatchProxy(client) watchp, _ := grpcproxy.NewWatchProxy(client.Ctx(), client)
if grpcProxyResolverPrefix != "" { if grpcProxyResolverPrefix != "" {
grpcproxy.Register(client, grpcProxyResolverPrefix, grpcProxyAdvertiseClientURL, grpcProxyResolverTTL) grpcproxy.Register(client, grpcProxyResolverPrefix, grpcProxyAdvertiseClientURL, grpcProxyResolverTTL)
} }
clusterp, _ := grpcproxy.NewClusterProxy(client, grpcProxyAdvertiseClientURL, grpcProxyResolverPrefix) clusterp, _ := grpcproxy.NewClusterProxy(client, grpcProxyAdvertiseClientURL, grpcProxyResolverPrefix)
leasep, _ := grpcproxy.NewLeaseProxy(client) leasep, _ := grpcproxy.NewLeaseProxy(client.Ctx(), client)
mainp := grpcproxy.NewMaintenanceProxy(client) mainp := grpcproxy.NewMaintenanceProxy(client)
authp := grpcproxy.NewAuthProxy(client) authp := grpcproxy.NewAuthProxy(client)
electionp := grpcproxy.NewElectionProxy(client) electionp := grpcproxy.NewElectionProxy(client)

View File

@ -17,6 +17,7 @@
package integration package integration
import ( import (
"context"
"sync" "sync"
"go.etcd.io/etcd/clientv3" "go.etcd.io/etcd/clientv3"
@ -35,16 +36,23 @@ var (
const proxyNamespace = "proxy-namespace" const proxyNamespace = "proxy-namespace"
type grpcClientProxy struct { type grpcClientProxy struct {
grpc grpcAPI ctx context.Context
wdonec <-chan struct{} ctxCancel func()
kvdonec <-chan struct{} grpc grpcAPI
lpdonec <-chan struct{} wdonec <-chan struct{}
kvdonec <-chan struct{}
lpdonec <-chan struct{}
} }
func toGRPC(c *clientv3.Client) grpcAPI { func toGRPC(c *clientv3.Client) grpcAPI {
pmu.Lock() pmu.Lock()
defer pmu.Unlock() defer pmu.Unlock()
// dedicated context bound to 'grpc-proxy' lifetype
// (so in practice lifetime of the client connection to the proxy).
// TODO: Refactor to a separate clientv3.Client instance instead of the context alone.
ctx, ctxCancel := context.WithCancel(context.WithValue(context.TODO(), "_name", "grpcProxyContext"))
if v, ok := proxies[c]; ok { if v, ok := proxies[c]; ok {
return v.grpc return v.grpc
} }
@ -55,8 +63,8 @@ func toGRPC(c *clientv3.Client) grpcAPI {
c.Lease = namespace.NewLease(c.Lease, proxyNamespace) c.Lease = namespace.NewLease(c.Lease, proxyNamespace)
// test coalescing/caching proxy // test coalescing/caching proxy
kvp, kvpch := grpcproxy.NewKvProxy(c) kvp, kvpch := grpcproxy.NewKvProxy(c)
wp, wpch := grpcproxy.NewWatchProxy(c) wp, wpch := grpcproxy.NewWatchProxy(ctx, c)
lp, lpch := grpcproxy.NewLeaseProxy(c) lp, lpch := grpcproxy.NewLeaseProxy(ctx, c)
mp := grpcproxy.NewMaintenanceProxy(c) mp := grpcproxy.NewMaintenanceProxy(c)
clp, _ := grpcproxy.NewClusterProxy(c, "", "") // without registering proxy URLs clp, _ := grpcproxy.NewClusterProxy(c, "", "") // without registering proxy URLs
authp := grpcproxy.NewAuthProxy(c) authp := grpcproxy.NewAuthProxy(c)
@ -73,20 +81,21 @@ func toGRPC(c *clientv3.Client) grpcAPI {
adapter.LockServerToLockClient(lockp), adapter.LockServerToLockClient(lockp),
adapter.ElectionServerToElectionClient(electp), adapter.ElectionServerToElectionClient(electp),
} }
proxies[c] = grpcClientProxy{grpc: grpc, wdonec: wpch, kvdonec: kvpch, lpdonec: lpch} proxies[c] = grpcClientProxy{ctx: ctx, ctxCancel: ctxCancel, grpc: grpc, wdonec: wpch, kvdonec: kvpch, lpdonec: lpch}
return grpc return grpc
} }
type proxyCloser struct { type proxyCloser struct {
clientv3.Watcher clientv3.Watcher
wdonec <-chan struct{} proxyCtxCancel func()
kvdonec <-chan struct{} wdonec <-chan struct{}
lclose func() kvdonec <-chan struct{}
lpdonec <-chan struct{} lclose func()
lpdonec <-chan struct{}
} }
func (pc *proxyCloser) Close() error { func (pc *proxyCloser) Close() error {
// client ctx is canceled before calling close, so kv and lp will close out pc.proxyCtxCancel()
<-pc.kvdonec <-pc.kvdonec
err := pc.Watcher.Close() err := pc.Watcher.Close()
<-pc.wdonec <-pc.wdonec
@ -106,11 +115,12 @@ func newClientV3(cfg clientv3.Config) (*clientv3.Client, error) {
lc := c.Lease lc := c.Lease
c.Lease = clientv3.NewLeaseFromLeaseClient(rpc.Lease, c, cfg.DialTimeout) c.Lease = clientv3.NewLeaseFromLeaseClient(rpc.Lease, c, cfg.DialTimeout)
c.Watcher = &proxyCloser{ c.Watcher = &proxyCloser{
Watcher: clientv3.NewWatchFromWatchClient(rpc.Watch, c), Watcher: clientv3.NewWatchFromWatchClient(rpc.Watch, c),
wdonec: proxies[c].wdonec, wdonec: proxies[c].wdonec,
kvdonec: proxies[c].kvdonec, kvdonec: proxies[c].kvdonec,
lclose: func() { lc.Close() }, lclose: func() { lc.Close() },
lpdonec: proxies[c].lpdonec, lpdonec: proxies[c].lpdonec,
proxyCtxCancel: proxies[c].ctxCancel,
} }
pmu.Unlock() pmu.Unlock()
return c, nil return c, nil

View File

@ -48,13 +48,13 @@ type leaseProxy struct {
wg sync.WaitGroup wg sync.WaitGroup
} }
func NewLeaseProxy(c *clientv3.Client) (pb.LeaseServer, <-chan struct{}) { func NewLeaseProxy(ctx context.Context, c *clientv3.Client) (pb.LeaseServer, <-chan struct{}) {
cctx, cancel := context.WithCancel(c.Ctx()) cctx, cancel := context.WithCancel(ctx)
lp := &leaseProxy{ lp := &leaseProxy{
leaseClient: pb.NewLeaseClient(c.ActiveConnection()), leaseClient: pb.NewLeaseClient(c.ActiveConnection()),
lessor: c.Lease, lessor: c.Lease,
ctx: cctx, ctx: cctx,
leader: newLeader(c.Ctx(), c.Watcher), leader: newLeader(ctx, c.Watcher),
} }
ch := make(chan struct{}) ch := make(chan struct{})
go func() { go func() {

View File

@ -46,12 +46,12 @@ type watchProxy struct {
kv clientv3.KV kv clientv3.KV
} }
func NewWatchProxy(c *clientv3.Client) (pb.WatchServer, <-chan struct{}) { func NewWatchProxy(ctx context.Context, c *clientv3.Client) (pb.WatchServer, <-chan struct{}) {
cctx, cancel := context.WithCancel(c.Ctx()) cctx, cancel := context.WithCancel(ctx)
wp := &watchProxy{ wp := &watchProxy{
cw: c.Watcher, cw: c.Watcher,
ctx: cctx, ctx: cctx,
leader: newLeader(c.Ctx(), c.Watcher), leader: newLeader(ctx, c.Watcher),
kv: c.KV, // for permission checking kv: c.KV, // for permission checking
} }