From 56175e151ae082bec12020d3b174164f441d063a Mon Sep 17 00:00:00 2001 From: Ketsia <115650494+ketsiambaku@users.noreply.github.com> Date: Wed, 13 Mar 2024 17:01:37 +0100 Subject: [PATCH] [code-coverage] Generate code for matching client timeout wrapper (#5771) * [code-coverage] generate matching client code * delete matching client file and update reference * Revert "delete matching client file and update reference" This reverts commit fa3207683ca8777d9a28984033e3931d00b2e342. * seperate timeout logic from matching client * move default timeout values * move matching timeout client line * lint --- client/clientfactory.go | 8 +- client/history/client.go | 5 - client/matching/client.go | 62 +--------- client/matching/interface.go | 1 + client/templates/timeout.tmpl | 10 +- client/wrappers/timeout/matching_generated.go | 117 ++++++++++++++++++ client/wrappers/timeout/timeout.go | 6 + 7 files changed, 137 insertions(+), 72 deletions(-) create mode 100644 client/wrappers/timeout/matching_generated.go diff --git a/client/clientfactory.go b/client/clientfactory.go index ae6ebf1627f..d453cd83e94 100644 --- a/client/clientfactory.go +++ b/client/clientfactory.go @@ -97,11 +97,11 @@ func NewRPCClientFactory( } func (cf *rpcClientFactory) NewHistoryClient() (history.Client, error) { - return cf.NewHistoryClientWithTimeout(history.DefaultTimeout) + return cf.NewHistoryClientWithTimeout(timeoutwrapper.HistoryDefaultTimeout) } func (cf *rpcClientFactory) NewMatchingClient(domainIDToName DomainIDToNameFunc) (matching.Client, error) { - return cf.NewMatchingClientWithTimeout(domainIDToName, matching.DefaultTimeout, matching.DefaultLongPollTimeout) + return cf.NewMatchingClientWithTimeout(domainIDToName, timeoutwrapper.MatchingDefaultTimeout, timeoutwrapper.MatchingDefaultLongPollTimeout) } func (cf *rpcClientFactory) NewHistoryClientWithTimeout(timeout time.Duration) (history.Client, error) { @@ -153,12 +153,11 @@ func (cf *rpcClientFactory) NewMatchingClientWithTimeout( peerResolver := matching.NewPeerResolver(cf.resolver, namedPort) client := matching.NewClient( - timeout, - longPollTimeout, rawClient, peerResolver, matching.NewLoadBalancer(domainIDToName, cf.dynConfig), ) + client = timeoutwrapper.NewMatchingClient(client, longPollTimeout, timeout) if errorRate := cf.dynConfig.GetFloat64Property(dynamicconfig.MatchingErrorInjectionRate)(); errorRate != 0 { client = errorinjectors.NewMatchingClient(client, errorRate, cf.logger) } @@ -166,7 +165,6 @@ func (cf *rpcClientFactory) NewMatchingClientWithTimeout( client = metered.NewMatchingClient(client, cf.metricsClient) } return client, nil - } func (cf *rpcClientFactory) NewAdminClientWithTimeoutAndConfig( diff --git a/client/history/client.go b/client/history/client.go index bb5ee0fb8ca..709a5209e52 100644 --- a/client/history/client.go +++ b/client/history/client.go @@ -39,11 +39,6 @@ import ( var _ Client = (*clientImpl)(nil) -const ( - // DefaultTimeout is the default timeout used to make calls - DefaultTimeout = time.Second * 30 -) - type ( clientImpl struct { numberOfShards int diff --git a/client/matching/client.go b/client/matching/client.go index df062479729..52c4eec2b80 100644 --- a/client/matching/client.go +++ b/client/matching/client.go @@ -22,7 +22,6 @@ package matching import ( "context" - "time" "go.uber.org/yarpc" @@ -33,35 +32,22 @@ import ( var _ Client = (*clientImpl)(nil) -const ( - // DefaultTimeout is the default timeout used to make calls - DefaultTimeout = time.Minute - // DefaultLongPollTimeout is the long poll default timeout used to make calls - DefaultLongPollTimeout = time.Minute * 2 -) - type clientImpl struct { - timeout time.Duration - longPollTimeout time.Duration - client Client - peerResolver PeerResolver - loadBalancer LoadBalancer + client Client + peerResolver PeerResolver + loadBalancer LoadBalancer } // NewClient creates a new history service TChannel client func NewClient( - timeout time.Duration, - longPollTimeout time.Duration, client Client, peerResolver PeerResolver, lb LoadBalancer, ) Client { return &clientImpl{ - timeout: timeout, - longPollTimeout: longPollTimeout, - client: client, - peerResolver: peerResolver, - loadBalancer: lb, + client: client, + peerResolver: peerResolver, + loadBalancer: lb, } } @@ -81,8 +67,6 @@ func (c *clientImpl) AddActivityTask( if err != nil { return err } - ctx, cancel := c.createContext(ctx) - defer cancel() return c.client.AddActivityTask(ctx, request, append(opts, yarpc.WithShardKey(peer))...) } @@ -102,8 +86,6 @@ func (c *clientImpl) AddDecisionTask( if err != nil { return err } - ctx, cancel := c.createContext(ctx) - defer cancel() return c.client.AddDecisionTask(ctx, request, append(opts, yarpc.WithShardKey(peer))...) } @@ -123,8 +105,6 @@ func (c *clientImpl) PollForActivityTask( if err != nil { return nil, err } - ctx, cancel := c.createLongPollContext(ctx) - defer cancel() return c.client.PollForActivityTask(ctx, request, append(opts, yarpc.WithShardKey(peer))...) } @@ -144,8 +124,6 @@ func (c *clientImpl) PollForDecisionTask( if err != nil { return nil, err } - ctx, cancel := c.createLongPollContext(ctx) - defer cancel() return c.client.PollForDecisionTask(ctx, request, append(opts, yarpc.WithShardKey(peer))...) } @@ -165,8 +143,6 @@ func (c *clientImpl) QueryWorkflow( if err != nil { return nil, err } - ctx, cancel := c.createContext(ctx) - defer cancel() return c.client.QueryWorkflow(ctx, request, append(opts, yarpc.WithShardKey(peer))...) } @@ -179,8 +155,6 @@ func (c *clientImpl) RespondQueryTaskCompleted( if err != nil { return err } - ctx, cancel := c.createContext(ctx) - defer cancel() return c.client.RespondQueryTaskCompleted(ctx, request, append(opts, yarpc.WithShardKey(peer))...) } @@ -193,8 +167,6 @@ func (c *clientImpl) CancelOutstandingPoll( if err != nil { return err } - ctx, cancel := c.createContext(ctx) - defer cancel() return c.client.CancelOutstandingPoll(ctx, request, append(opts, yarpc.WithShardKey(peer))...) } @@ -207,8 +179,6 @@ func (c *clientImpl) DescribeTaskList( if err != nil { return nil, err } - ctx, cancel := c.createContext(ctx) - defer cancel() return c.client.DescribeTaskList(ctx, request, append(opts, yarpc.WithShardKey(peer))...) } @@ -221,8 +191,6 @@ func (c *clientImpl) ListTaskListPartitions( if err != nil { return nil, err } - ctx, cancel := c.createContext(ctx) - defer cancel() return c.client.ListTaskListPartitions(ctx, request, append(opts, yarpc.WithShardKey(peer))...) } @@ -271,21 +239,3 @@ func (c *clientImpl) GetTaskListsByDomain( ActivityTaskListMap: activityTaskListMap, }, nil } - -func (c *clientImpl) createContext( - parent context.Context, -) (context.Context, context.CancelFunc) { - if parent == nil { - return context.WithTimeout(context.Background(), c.timeout) - } - return context.WithTimeout(parent, c.timeout) -} - -func (c *clientImpl) createLongPollContext( - parent context.Context, -) (context.Context, context.CancelFunc) { - if parent == nil { - return context.WithTimeout(context.Background(), c.longPollTimeout) - } - return context.WithTimeout(parent, c.longPollTimeout) -} diff --git a/client/matching/interface.go b/client/matching/interface.go index bb6f960dd3e..9093bb6ccac 100644 --- a/client/matching/interface.go +++ b/client/matching/interface.go @@ -34,6 +34,7 @@ import ( //go:generate gowrap gen -g -p . -i Client -t ../templates/errorinjectors.tmpl -o ../wrappers/errorinjectors/matching_generated.go -v client=Matching //go:generate gowrap gen -g -p . -i Client -t ../templates/grpc.tmpl -o ../wrappers/grpc/matching_generated.go -v client=Matching -v package=matchingv1 -v path=github.com/uber/cadence/.gen/proto/matching/v1 -v prefix=Matching //go:generate gowrap gen -g -p . -i Client -t ../templates/thrift.tmpl -o ../wrappers/thrift/matching_generated.go -v client=Matching -v prefix=Matching +//go:generate gowrap gen -g -p . -i Client -t ../templates/timeout.tmpl -o ../wrappers/timeout/matching_generated.go -v client=Matching // Client is the interface exposed by types service client type Client interface { diff --git a/client/templates/timeout.tmpl b/client/templates/timeout.tmpl index 2a0dd4b5343..d9023ea9770 100644 --- a/client/templates/timeout.tmpl +++ b/client/templates/timeout.tmpl @@ -4,8 +4,8 @@ {{ $decorator := (printf "%s%s" (down $clientName) .Interface.Name) }} {{ $Decorator := (printf "%s%s" $ClientName .Interface.Name) }} {{$largeTimeoutAPIs := list "adminClient.GetCrossClusterTasks" "adminClient.GetReplicationMessages"}} -{{$longPollTimeoutAPIs := list "frontendClient.ListArchivedWorkflowExecutions" "frontendClient.PollForActivityTask" "frontendClient.PollForDecisionTask"}} -{{$noTimeoutAPIs := list "historyClient.GetReplicationMessages" "historyClient.GetDLQReplicationMessages" "historyClient.CountDLQMessages" "historyClient.ReadDLQMessages" "historyClient.PurgeDLQMessages" "historyClient.MergeDLQMessages" "historyClient.GetCrossClusterTasks" "historyClient.GetFailoverInfo"}} +{{$longPollTimeoutAPIs := list "frontendClient.ListArchivedWorkflowExecutions" "frontendClient.PollForActivityTask" "frontendClient.PollForDecisionTask" "matchingClient.PollForActivityTask" "matchingClient.PollForDecisionTask"}} +{{$noTimeoutAPIs := list "historyClient.GetReplicationMessages" "historyClient.GetDLQReplicationMessages" "historyClient.CountDLQMessages" "historyClient.ReadDLQMessages" "historyClient.PurgeDLQMessages" "historyClient.MergeDLQMessages" "historyClient.GetCrossClusterTasks" "historyClient.GetFailoverInfo" "matchingClient.GetTaskListsByDomain"}} {{/* $fieldMap defines a map of the decorator struct fields with field name as the key and field type as the value @@ -15,10 +15,8 @@ {{$fieldMap = merge $fieldMap (dict "timeout" "time.Duration" "client" .Interface.Type) }} {{ else if eq $ClientName "Admin" }} {{$fieldMap = merge $fieldMap (dict "timeout" "time.Duration" "client" .Interface.Type "largeTimeout" "time.Duration") }} -{{ else if eq $ClientName "Frontend" }} +{{ else }} {{$fieldMap = merge $fieldMap (dict "timeout" "time.Duration" "client" .Interface.Type "longPollTimeout" "time.Duration") }} -{{ else if eq $ClientName "Matching" }} - {{$fieldMap = merge $fieldMap (dict "timeout" "time.Duration" "client" .Interface.Type "longPollTimeout" "time.Duration" "peerResolver" "matching.PeerResolver" "loadBalancer" "matching.LoadBalancer") }} {{ end }} import ( @@ -69,4 +67,4 @@ func (c * {{$decorator}}) {{$method.Declaration}} { {{- end }} {{$method.Pass ("c.client.") }} } -{{end}} +{{end}} \ No newline at end of file diff --git a/client/wrappers/timeout/matching_generated.go b/client/wrappers/timeout/matching_generated.go new file mode 100644 index 00000000000..2bdc741e127 --- /dev/null +++ b/client/wrappers/timeout/matching_generated.go @@ -0,0 +1,117 @@ +// The MIT License (MIT) + +// Copyright (c) 2017-2020 Uber Technologies Inc. + +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +package timeout + +// Code generated by gowrap. DO NOT EDIT. +// template: ../../templates/timeout.tmpl +// gowrap: http://github.com/hexdigest/gowrap + +import ( + "context" + "time" + + "go.uber.org/yarpc" + + "github.com/uber/cadence/client/matching" + "github.com/uber/cadence/common/types" +) + +var _ matching.Client = (*matchingClient)(nil) + +// matchingClient implements the matching.Client interface instrumented with timeouts +type matchingClient struct { + client matching.Client + longPollTimeout time.Duration + timeout time.Duration +} + +// NewMatchingClient creates a new matchingClient instance +func NewMatchingClient( + client matching.Client, + longPollTimeout time.Duration, + timeout time.Duration, +) matching.Client { + return &matchingClient{ + client: client, + longPollTimeout: longPollTimeout, + timeout: timeout, + } +} + +func (c *matchingClient) AddActivityTask(ctx context.Context, ap1 *types.AddActivityTaskRequest, p1 ...yarpc.CallOption) (err error) { + ctx, cancel := createContext(ctx, c.timeout) + defer cancel() + return c.client.AddActivityTask(ctx, ap1, p1...) +} + +func (c *matchingClient) AddDecisionTask(ctx context.Context, ap1 *types.AddDecisionTaskRequest, p1 ...yarpc.CallOption) (err error) { + ctx, cancel := createContext(ctx, c.timeout) + defer cancel() + return c.client.AddDecisionTask(ctx, ap1, p1...) +} + +func (c *matchingClient) CancelOutstandingPoll(ctx context.Context, cp1 *types.CancelOutstandingPollRequest, p1 ...yarpc.CallOption) (err error) { + ctx, cancel := createContext(ctx, c.timeout) + defer cancel() + return c.client.CancelOutstandingPoll(ctx, cp1, p1...) +} + +func (c *matchingClient) DescribeTaskList(ctx context.Context, mp1 *types.MatchingDescribeTaskListRequest, p1 ...yarpc.CallOption) (dp1 *types.DescribeTaskListResponse, err error) { + ctx, cancel := createContext(ctx, c.timeout) + defer cancel() + return c.client.DescribeTaskList(ctx, mp1, p1...) +} + +func (c *matchingClient) GetTaskListsByDomain(ctx context.Context, gp1 *types.GetTaskListsByDomainRequest, p1 ...yarpc.CallOption) (gp2 *types.GetTaskListsByDomainResponse, err error) { + return c.client.GetTaskListsByDomain(ctx, gp1, p1...) +} + +func (c *matchingClient) ListTaskListPartitions(ctx context.Context, mp1 *types.MatchingListTaskListPartitionsRequest, p1 ...yarpc.CallOption) (lp1 *types.ListTaskListPartitionsResponse, err error) { + ctx, cancel := createContext(ctx, c.timeout) + defer cancel() + return c.client.ListTaskListPartitions(ctx, mp1, p1...) +} + +func (c *matchingClient) PollForActivityTask(ctx context.Context, mp1 *types.MatchingPollForActivityTaskRequest, p1 ...yarpc.CallOption) (pp1 *types.PollForActivityTaskResponse, err error) { + ctx, cancel := createContext(ctx, c.longPollTimeout) + defer cancel() + return c.client.PollForActivityTask(ctx, mp1, p1...) +} + +func (c *matchingClient) PollForDecisionTask(ctx context.Context, mp1 *types.MatchingPollForDecisionTaskRequest, p1 ...yarpc.CallOption) (mp2 *types.MatchingPollForDecisionTaskResponse, err error) { + ctx, cancel := createContext(ctx, c.longPollTimeout) + defer cancel() + return c.client.PollForDecisionTask(ctx, mp1, p1...) +} + +func (c *matchingClient) QueryWorkflow(ctx context.Context, mp1 *types.MatchingQueryWorkflowRequest, p1 ...yarpc.CallOption) (qp1 *types.QueryWorkflowResponse, err error) { + ctx, cancel := createContext(ctx, c.timeout) + defer cancel() + return c.client.QueryWorkflow(ctx, mp1, p1...) +} + +func (c *matchingClient) RespondQueryTaskCompleted(ctx context.Context, mp1 *types.MatchingRespondQueryTaskCompletedRequest, p1 ...yarpc.CallOption) (err error) { + ctx, cancel := createContext(ctx, c.timeout) + defer cancel() + return c.client.RespondQueryTaskCompleted(ctx, mp1, p1...) +} diff --git a/client/wrappers/timeout/timeout.go b/client/wrappers/timeout/timeout.go index 15f7c2f62f4..e50b11f6694 100644 --- a/client/wrappers/timeout/timeout.go +++ b/client/wrappers/timeout/timeout.go @@ -36,6 +36,12 @@ const ( FrontendDefaultTimeout = 10 * time.Second // FrontendDefaultLongPollTimeout is the frontend service long poll default timeout used to make calls FrontendDefaultLongPollTimeout = time.Minute * 3 + // MatchingDefaultTimeout is the default timeout used to make calls + MatchingDefaultTimeout = time.Minute + // MatchingDefaultLongPollTimeout is the long poll default timeout used to make calls + MatchingDefaultLongPollTimeout = time.Minute * 2 + // HistoryDefaultTimeout is the default timeout used to make calls + HistoryDefaultTimeout = time.Second * 30 ) func createContext(parent context.Context, timeout time.Duration) (context.Context, context.CancelFunc) {