Skip to content

Commit

Permalink
[code-coverage] Generate code for matching client timeout wrapper (#5771
Browse files Browse the repository at this point in the history
)

* [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
  • Loading branch information
ketsiambaku authored Mar 13, 2024
1 parent 227e27f commit 56175e1
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 72 deletions.
8 changes: 3 additions & 5 deletions client/clientfactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -153,20 +153,18 @@ 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)
}
if cf.metricsClient != nil {
client = metered.NewMatchingClient(client, cf.metricsClient)
}
return client, nil

}

func (cf *rpcClientFactory) NewAdminClientWithTimeoutAndConfig(
Expand Down
5 changes: 0 additions & 5 deletions client/history/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
62 changes: 6 additions & 56 deletions client/matching/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package matching

import (
"context"
"time"

"go.uber.org/yarpc"

Expand All @@ -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,
}
}

Expand All @@ -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))...)
}

Expand All @@ -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))...)
}

Expand All @@ -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))...)
}

Expand All @@ -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))...)
}

Expand All @@ -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))...)
}

Expand All @@ -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))...)
}

Expand All @@ -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))...)
}

Expand All @@ -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))...)
}

Expand All @@ -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))...)
}

Expand Down Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions client/matching/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 4 additions & 6 deletions client/templates/timeout.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 (
Expand Down Expand Up @@ -69,4 +67,4 @@ func (c * {{$decorator}}) {{$method.Declaration}} {
{{- end }}
{{$method.Pass ("c.client.") }}
}
{{end}}
{{end}}
117 changes: 117 additions & 0 deletions client/wrappers/timeout/matching_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions client/wrappers/timeout/timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 56175e1

Please sign in to comment.